Verilog Tutorial: Chin-Lung Su
Verilog Tutorial: Chin-Lung Su
Chin-Lung Su
Outline
Introduction
Combinational Circuit
Programming style
How to program
Test-bench
Introduction
Hardware Description Language (HDL)
Verilog is useful and popular language Parallel not serial (Not like C language) Meaningful declaration
parameter, port name, module name,
Identifies
Even and even are different
Structure Programming
Module
Module is a block of circuit
Special function and, or, mux, adder,
Most important
Input and Output
Module
Flow
What functions are you want to program ?
Input and output
B1
B2
B3
6
Scenario
Test-Bench
Module
Monitor Signals
Verilog Model
Module Declaration
module module_name(input1, input2, , output1, output2, )
Example
module mux2(a, b, c);
module adder(x, y, z);
endmodule
Datatypes
Nests
Wire C[1:0] = {a,b}
Registers
reg
Integers
4d3, 4b0100, 6h20
Array
reg [9:0] ram
Parameter
parameter word_size = 16 wire [word_size-1:0] bus;
Preprocessor Directive
`define BEQ 4b0100
No =
10
Synthesizable Description
`define
parameter wire always if/else
`include
input reg assign case/casex
module/endmodule
output begin/end posedge/negedge
11
Un-synthesizable Description
Delay
forever join deassign primitive
initial
wait event force
Repeat
Foke Time Relace
12
Synthesizable Operator
Binary Bitwise operator
~ &
|
^ ~^
Example
Assign a=~b;
if b = 4b0010; then a = 4b1101;
13
Logic
x y
y
0 1
z
1 0
x y x 0 0 1 1 y 0 1 0 1 z 0 0 0 1
x y x 0 0 1 1 y z
0 1 0 1
0 1 1 1
14
Example
assign a=b & c
b = 4b0011 c = 4b0101
a = 4b0001
assgin a=b | c
b = 4b0011 c = 4b0101
a = 4b0111
15
Program 1
Purpose
A circuit can calculate the addition and subtraction
+ Mux s
op
16
Program 1 (cont.)
/* A first program in Verilog */ module adder_or_subtract( a, b, op, s); parameter SIZE = 8; parameter ADD = 1b1; input op; input [SIZE-1:0] a,b; output [SIZE-1:0] s; a wire add, sub; assign add = a+b; b assign sub = a-b; assign s = (op==ADD)? add : sub; endmodule
comment
Model declaration
add
+ Mux sub op
17
Program 1 (cont.)
module adder_or_subtract( a, b, op, s);
parameter parameter input input SIZE = 8; ADD = 1b1; op; [SIZE-1:0] a,b;
output
[SIZE-1:0] s;
18
Program 2
1-to-2 De-multiplexer
D Select 0 1 y0 D 0 y1 0 D
y0
y1
Select
19
Program 2 (cont.)
/* A deMux program in Verilog */
module demux ( D, select, y0, y1); input D;
Model declaration comment
input
output wire assign assign
select;
y0,y1; y0,y1; y0 = (~select) & D; y1 = select & D;
endmodule
20
Sub Module
Complex circuit
Many modules in a circuit Module, sub-module, sub-sub-module,
B1
B2
B3
21
endmodule
22
endmodule
23
.
B1 b1(w, q, a, c); endmodule
input q, w;
input [3:0] e; output [1:0] f; . endmodule
w q
a
q w
e
B1
24
q
w e
B1
output [1:0] f;
. endmodule
26
Reuse Module
Using the same module many times
Example
Constructing 4-bits adder with four 1-bit adder
b3 a3
b2 a2
b1 a1
b0 a0
c3
c2
c1
s4
s3
s2
s1
s0
27
4-bists Adder
module Adder(x, y, cin, sum, cout);
input x, y, c; output sum, cout; wire x, y, c, sum, cout; assign sum = x ^ y ^ cin;
cout y x
cin
sum
endmodule
28
b3 a3
b2 a2
b1 a1
b0 a0 cin
c3
c2
c1
s3
s2
s1
s0
Mistake
module and endmodule
module();Module(); (;) port bus
30
Always Block
Syntax
always @(event-expression)
assignment or block
Level type
always @(a or b or c)
Edge type
always @(posedge clock) always @(negedge clock)
if-else and case statement are only in always block wire and reg
31
Example
wire a;
reg b; always @(x or y or z) begin a <= x & y;
error
correct
b <= x | z;
end
32
Program 2 (cont.)
/* A deMux program in Verilog */
module demux ( D, select, y0, y1); input output y0,y1; D, select;
reg
y0,y1;
D y0
always @( D or select ) begin if( select == 1b0)begin y0 = D; end else begin y0 = 1b0; y1 = D; y1 = 1b0;
y1
end
end endmodule
33
Select
= <=
A <= B B <= A
A=B B=A
A A B
34
35
if statement
Like C language
Only in always block
reg out; always @(sel or a or b) begin if(sel == 1b1) out = a; else out = b; end
36
Sel [1:0]
always @(sel or a or b or c or d) begin case (sel[1:0] ) 2b00, 2b11 : out <= a; 2b01 : out <= b; 2b10 : out <= c; endcase end
a b out
c
d
Sel [2:0]
always @(sel or a or b or c or d) begin casex (sel[2:0] ) 3b011 : out <= a; 3b00x : out <= b; 3b100 : out <= c; default : out <= d; endcasex All others end
38
39
Z = (a + b) + (c + d) + e
c +
+ d +
+ Three adders
e
Four adders
e
+
+ Z Z
40
Test-bench
Input data of the circuit
All inputs of original circuit are assigned reg
Store data
41
Test-bench Example
`timescale 1ns/100fs
module Adder_testbench; reg [3:0] x,y; reg cin; wire [4:0] sum;
Initialization
y = 4d0;
cin=1b0;
InIn 10ns 25ns x=3; x=1; y=0; y=5; In 15ns sum=3; x=3; sum=6; y=10; sum=13;
y = 4d5;
endmodule