Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-2
2-3
gate
Behavioral
Algorithmic
Register transfer
algo
Structural
Gate
Switch
RTL
switch
gate
2-4
Books
Palnitkar S., Verilog HDL: A Guide to Digital Design
and Synthesis, Prentice Hall, NJ, 1996.
(ISBN: 0-13-451675-3)
Thomas D. and P. Moorby, The Verilog Hardware
Description Language, Kluwer Academic, MA, 1991.
(ISBN: 0-7923-9126-8)
Sternheim E., E. Singh, and Y. Trivedi, Digital
Design with Verilog HDL, Automata Publishing
Company, CA, 1990. (ISBN: 0-9627488-0-3)
Official Language Document:
Verilog Hardware Description Language Reference
Manual, IEEE Std 1364-1995, IEEE.
2-5
Outline
Introduction
Language elements
Modules
Lexical conventions
Data types
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-6
module
statements
ports
2-7
An Example
module FA_MIX (A, B, CIN, SUM, COUT);
input A,B,CIN;
output SUM, COUT;
reg COUT;
reg T1, T2, T3;
wire S1;
xor X1 (S1, A, B); // Gate instantiation.
always @ (A or B or CIN) // Always Block
begin
T1 = A & CIN;
T2 = B & CIN;
T3 = A & B;
COUT = (T1 | T2 | T3);
END
assign SUM = S1 ^ CIN; // Continuous assignment
endmodule
2-8
2-9
A
XOR2
AND2
CO
2-10
A
B
NET1
CIN
SUM
HA
U2
CO
NET3
HA
U1
CO
COUT
NET2
U3
2-11
Lexical Conventions
Verilog is a free-format language
Like C language
White space (blank, tab, newline) can be used freely
Keywords
Lexical Conventions
Comments: two forms
/* First form: can
extend over many
lines */
// Second form: ends at the end of this line
Strings
Enclosed in double quotes and must be specified in
one line
Sequence of characters
\n = newline
\t = tab
\\ = backslash
\= quote mark ()
%% = % sign
2-13
Value Set
0: logic-0 / FALSE
1: logic-1 / TRUE
x: unknown / dont care, can be 0, 1 or z.
z: high-impedance
2-14
Data Types
Nets
Connects between structural elements
Values come from its drivers
Continuous assignment
Module or gate instantiation
Registers
2-15
Net Types
Register Types
Examples:
reg CNT;
reg [31:0] SAT;
integer A, B, C; // 32-bit
real SWING;
realtime CURR_TIME;
time EVENT;
2-17
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-18
Primitive Gates
The following gates are built-in types in the simulator
and, nand, nor, or, xor, xnor
First terminal is output, followed by inputs
and a1 (out1, in1, in2);
nand a2 (out2, in21, in22, in23, in24);
buf, not
One or more outputs first, followed by one input
not N1 (OUT1, OUT2, OUT3, OUT4, INA);
buf B1 (BO1, BIN);
pullup, pulldown
Put 1 or 0 on all terminals
pullup PUP (PWRA, PWRB, PWRC);
Example
4 X 1 multiplexer circuit
2-20
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-21
Operators
Arithmetic Operators
+, -, *, /, %
Relational Operators
==, !=
===, !==
Logical Operators
!, &&, ||
Bit-Wise Operators
Shift Operators
>>, <<
Conditional Operators
?:
Concatenation Operator
{}
Replication Operator
{{}}
2-23
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-24
Behavioral Modeling
Procedural blocks:
initial block: executes only once
always block: executes in a loop
Initial Statement
Executes only once at the beginning of simulation
initial
statements
RIB_REG =0;
for (INX = 0; INX < 1024; INX = INX + 1)
RAM[INX] = 0;
end
2-26
Always Statement
Executes continuously; must be used with some form of
timing control
always (timing_control)
statements
always
CLK = ~CLK
// Will loop indefinitely
always @(A or B)
begin
case ({A, B})
2'b 00 : begin Y1=1; Y2=0; end
2'b 01 : begin Y1=1; Y2=0; end
2'b 10 : begin Y1=1; Y2=0; end
2'b 11 : begin Y1=0; Y2=1; end
endcase
end
A
0
0
1
1
B
0
1
0
1
Y1
1
1
1
0
Y2
0
0
0
1
endmodule
2-28
Other Examples
module example (D, CURRENT_STATE, Q, NEXT_STATE);
input D, CURRENT_STATE;
output Q, NEXT_STATE;
reg CLK, Q, NEXT_STATE;
always #5 CLK = ~CLK;
always @(posedge CLK)
begin
Q = D;
end
Procedural Assignments
The assignment statements that can be used
inside an always or initial block
The target must be a register or integer type
The following forms are allowed as a target
Register variables
Bit-select of register variables (ex: A[3])
Part-select of register variables (ex: A[4:2])
Concatenations of above (ex: {A, B[3:0]})
Integers
always @(posedge CLK) begin
B = A;
C = B;
end
2-30
Conditional Statements
if and else if statements
if (expression)
statements
{ else if (expression)
statements
}
[ else
statements
]
if (total < 60) begin
grade = C;
total_C = total_C + 1;
end
else if (sum < 75) begin
grade = B;
total_B = total_B + 1;
end
else grade = A;
case statement
case (case_expression)
case_item_expression
{, case_item_expression } :
statements
[ default: statements ]
endcase
case (OP_CODE)
2`b10:
Z = A + B;
2`b11:
Z = A B;
2`b01:
Z = A * B;
2`b00:
Z = A / B;
default: Z = 2`bx;
endcase
2-31
Loop Statements
Four loop statements are supported
2-32
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test benches
2-33
Combinational
Circuit
outputs
Wrong example:
always @ (a or b)
f = a & ~c | b & c ;
The changes of c will not change the output immediately
May cause functional mismatch in the synthesized circuits
Unlike simulation, synthesizers will skip the sensitivity
list and deal with the following statements directly
2-34
2-35
Behavior style
assign W = A|(B&(C|D));
assign X = ~B&(C|D)|(B&~C&~D);
assign Y = ~(C^D);
assign Z = ~D;
Decoder (1/2)
A decoder is to generate
the 2n (or fewer) minterms
of n input variables
Ex: a 3-to-8 line decoder
Inputs
x y
0 0
0 0
0 1
0 1
1 0
1 0
1 1
1 1
z
0
1
0
1
0
1
0
1
D0 D1 D2
1 0 0
0 1 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Outputs
D3 D4
0 0
0 0
0 0
1 0
0 1
0 0
0 0
0 0
D5 D6 D7
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
1 0 0
0 1 0
0 0 1
2-37
Decoder (2/2)
Behavior style 1
Behavior style 2
input x, y, z;
reg [7:0] D;
always @(x or y or z) begin
case ({x, y, z})
3`b000: D = 8`b00000001;
3`b001: D = 8`b00000010;
input x, y, z;
wire [2:0] addr;
reg [7:0] D;
assign addr = {x, y, z};
3`b111: D = 8`b10000000;
default: D = 8`b0;
endcase
end
Encoder (1/2)
An encoder performs the inverse operation of a decoder
Have 2n (or fewer) input lines and n output lines
The output lines generate the binary code of the input positions
D0 D1 D2
1 0 0
0 1 0
0 0 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Inputs
D3 D4 D5 D6 D7
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Outputs
x
y z
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
z = D1 + D3 + D5 + D7
y = D2 + D3 + D6 + D7
x = D4 + D5 + D6 + D7
2-39
Encoder (2/2)
Data-flow style
Behavior style
8`b10000000:
{x, y, z} = 3`b111;
default: {x, y, z} = 3`b000;
endcase
end
2-40
V = 0 : no valid inputs
D0
0
1
X
X
X
Inputs
D1 D2
0
0
0
0
1
0
X
1
X
X
D3
0
0
0
0
1
Outputs
x
y
V
X
X
0
0
0
1
0
1
1
1
0
1
1
1
1
x = D2 + D3
y = D3 + D1D2
V = D0 + D1 + D2 + D3
2-41
Behavior style
always @(D) begin
V = 1;
casex (D)
4`b1???: {x, y} = 2`b11;
4`b01??: {x, y} = 2`b10;
4`b001?: {x, y} = 2`b01;
4`b0001: {x, y} = 2`b00;
default: begin
{x, y} = 2`bx;
V = 0;
end
endcase
end
2-42
Multiplexer (1/2)
A multiplexer uses n selection bits to choose binary info.
from a maximum of 2n unique input lines
Like a decoder, it
decodes all minterms
internally
Unlike a decoder, it has
only one output line
decoder !!
2-43
Multiplexer (2/2)
Behavior style 1
Behavior style 2
input [1:0] S;
input [1:0] S;
input [3:0] I;
input [3:0] I;
output Y;
output Y;
always @(S or I) begin
always @(S or I) begin
case (S)
Y = I[S];
2`b00: Y = I[0];
end
2`b01: Y = I[1];
2`b10: Y = I[2];
2`b11: Y = I[3];
default: Y = 0;
suitable for the cases
endcase
that inputs are not coded
end
(ex: A, B, C, )
2-44
Demultiplexer (1/2)
It performs the inverse function of a multiplexer
It receives info from a single line and transmits it to
one of 2n possible output lines
A decoder with enable input can function as a
demultiplexer
Often referred to as a decoder/demultiplexer
data input
selection
2-45
Demultiplexer (2/2)
Behavior style 1
Behavior style 2
input A, B, E;
reg [3:0] D;
always @(A or B or E)
begin
D = 4`b1111;
case ({A, B})
2`b00: D[0] = E;
2`b01: D[1] = E;
2`b10: D[2] = E;
2`b11: D[3] = E;
default: D = 4`b0;
endcase
end
input A, B, E;
wire [1:0] S;
reg [3:0] D;
assign S = {A, B};
always @(S or E) begin
D = 4`b1;
D[S] = E;
end
2-46
2-47
Full adder
assign {C, S} = X + Y;
assign C = X & Y;
assign S = X ^ Y;
2-48
IF-ELSE Statement
if (A(0) == 0 and A(1) == 0) then
B = C;
else if (A(0) == 1 and A(1) == 0) then
B = D;
else if (A(0) == 0 and A(1) == 1) then
B = E;
A[0]
else
B = F;
end if;
A[1]
CDEF
M
U
X
2-50
Case Statement
case (A)
200 : B = C;
201 : B = D;
210 : B = E;
211 : B = F;
endcase;
2-51
Tri-State Buffers
Two popular ways to describe a tri-state buffer
// continuous assignment
assign out = (sela) ? a : 1bz ;
// RTL modeling
always @(selb or b)
if (selb)
out = b ;
else
out = 1bz;
out
EN
sela
b
EN
selb
2-52
Register Inference
Allow sequential logic design
Keep technology independent
Latch a level-sensitive memory device
Flip-Flop an edge-triggered memory device
2-53
Flip-Flop Inference
Wire (port) assigned in the synchronous section
module FF_PN (Clock, X1, X2, Y1, Y2);
input Clock;
X1
input X1, X2;
output Y1, Y2;
reg Y1, Y2;
Y1
Clock
Y2
Clock
2-54
Flip-Flop Inference
Asynchronous / synchronous reset / enable
module FFS (Clock, SReset, ASReset, En, Data1, Data2, Y1, Y2
Y3, Y4, Y5, Y6);
input Clock, SReset, ASReset, En, Data1, Data2;
output Y1, Y2, Y3, Y4, Y5, Y6;
reg Y1, Y2, Y3, Y4, Y5, Y6;
always @(posedge Clock)
begin
// Synchronous reset
if (! SReset)
Y1 = 0;
else
Y1 = Data1 | Data2;
end
2-55
Flip-Flop Inference
// Negative active asynchronous reset
always @(posedge Clock or negedge ASReset)
if (! ASReset)
Y2 = 0;
else
Y2 = Data1 & Data2;
// One synchronous & one asynchronous reset
always @(posedge Clock or negedge ASReset)
if(! ASReset)
Y3 = 0;
else if (SReset)
Y3 = 0;
else
Y3 = Data1 | Data2;
2-56
Flip-Flop Inference
// Single enable
2-57
Latch Inference
When if or case statements are used without specifying
outputs in all possible condition, a latch will be created
always@(enable or data)
if (enable)
q =data ;
data
q
DL0A
enable
always@(enable or data)
begin
if (enable)
q =data ;
else
q=0;
Specify all output value
end
Latch infer
2-58
More Latches
D latch with gated enable
always @(enable or d or gate)
if (enable & gate)
q = d;
d
q
enable
gate
result in a latch
always@(a or b or c)
Add default
case(a)
statement
2b11 : e = b;
2b10 : e = ~c;
default : e = 0 ;
end case
always@(a or b or c)
e =0;
case(a)
2b11 : e = b;
2b10 : e = ~c;
end case
Give initial
value
2-62
FSM Structures
Inputs
Next State Logic
Clock
(combinational)
Synchronous
reset
Current State
Register
(sequential)
Output Logic
(combinational)
Mealy
Outputs
Asynchronous
reset
Mealy
2-63
FSM Structures
Inputs
Next State Logic
Clock
(combinational)
Synchronous
reset
Current State
Register
(sequential)
Asynchronous
reset
Output Logic
(combinational)
Moore
Outputs
Moore
2-64
FSM Structures
Inputs
Next State Logic
(combinational) Clock
Synchronous
reset
Current State
Register
(sequential)
Asynchronous
reset
Output Logic
(combinational)
Output Logic
(combinational)
Mealy
Outputs
Moore
Outputs
Mealy/Moore
2-65
1-Process FSM
Lump all descriptions into a single process
module counter (clk, rst, load, in, count) ;
input
clk, rst, load ;
input [7:0] in ;
output [7:0] count ;
count
reg
[7:0] count ;
count
0
count
1
255
...
2-66
2-Process FSMs
functionally partitioned
Current State
Register
(sequential)
structurally partitioned
Clock
Asynchronous
reset
Inputs
Output Logic
(combinational)
Outputs
2-67
Reset
ST0 Y=1
Y=4 ST3
ST1 Y=2
ST2 Y=3
2-69
2-70
ST2 : Y = 3;
ST3 : Y = 4;
default : Y = 1;
endcase
end
endmodule
2-72
3-Process FSM
Current State
Register
(sequential)
Clock
Asynchronous
reset
Inputs
Output Logic
(combinational)
Outputs
2-73
ST2 : Y = 3;
ST3 : Y = 4;
default : Y = 1;
endcase
end
endmodule
always @(CS)
begin : OUT
case (CS)
ST0 : Y = 1;
ST1 : Y = 2;
2-76
Outline
Introduction
Language elements
Gate-level modeling
Data-flow modeling
Behavioral modeling
Modeling examples
Simulation and test bench
2-77
Simulation
Design, stimulus, control, saving responses, and verification
can be completed in a single language
Stimulus and control
Use initial procedural block
Saving responses
Save on change
Display data
Verification
Automatic compares with expected responses
Test Bench
module test_bench;
data type declaration
module instantiation
applying stimulus
display results
endmodule
Module instantiation
Instantiate pre-defined modules in current scope
Connect their I/O ports to other devices
Applying stimulus
Describe stimulus by behavior modeling
Display results
By text output, graphic output, or waveform display tools
2-80
An Example
module RS_FF (Q, QBAR, R, S);
output Q, QBAR;
input R, S;
1 time-unit(ns) delay
An Example (cont.)
//Apply stimulus;
initial begin
TR = 0; TS = 0;
#5 TS = 1;
#5 TS = 0; TR = 1;
#5 TS = 1; TR = 0;
#5 TS = 0;
#5 TR = 1;
#5 $stop;
end
//Display output :
initial
$monitor (At time %t, , $time, TR = %b, TS = %b, TQ
= %b, TQB = %b, TR, TS, TQ, TQB);
endmodule
system-defined task
2-82