The Verilog Language
The Verilog Language
Structural Modeling
Comparison
STRUCTURAL
Structural: Logic is
described in terms of
Verilog gate primitives
Example:
not n1(sel_n, sel);
and a1(sel_b, b,
sel_b);
and a2(sel_a, a, sel);
or o1(out, sel_b,
sel_a);
DATAFLOW
Dataflow: Specify
output signals in
terms of input
signals
Example:
assign out = (sel &
a) | (~sel & b);
Structural modelling
Dataflow modelling
b
b
sel
n1
a1
sel_b
sel
sel_n
o1
a
a2
out
sel_b
sel_n
out
sel_a
sel_a
Behavioral Modeling
More flexible
Provides sequencing
Verilog succeeded in part because it allowed both
the model and the testbench to be described
together
Behavioral contd..
Example:
if (select == 0) begin
out = b;
end
else if (select == 1) begin
out = a;
end
a
b
Black Box
2x1 MUX
sel
out
Dataflow modelling
Example:
`timescale 1ns/100ps
module HalfAdder (A, B, Sum, Carry);
input A, B; output Sum, Carry;
assign #3 Sum = A ^ B;
assign #6 Carry = A & B;
endmodule
Contd..
Timescale
`timescale 1ns/100ps
1 Time unit = 1 ns
Time precision is 100ps (0.1 ns)
10.512ns is interpreted as 10.5ns
Structure (Plumbing)
Verilog program build from modules with I/O
interfaces
Modules may contain instances of other modules
Modules contain local signals, etc.
Module configuration is static and all run concurrently
Data types
Default Values
Net Types : z
Register Type : x
Example
wire Reset; // A 1-bit wire
wire [6:0] Clear; // A 7-bit wire
Example
reg [ 3: 0 ] cla; // A 4-bit register
reg cla; // A 1-bit register
Behavioral Modeling
Can
Memories
An array of registers
reg [ msb : lsb ] memory1 [ upper : lower ];
Example
reg [ 0 : 3 ] mem [ 0 : 63 ];
// An array of 64 4-bit registers
reg mem [ 0 : 4 ];
// An array of 5 1-bit registers
Compiler directives
Example:
`define BUS_WIDTH 16
reg [ `BUS_WIDTH - 1 : 0 ] System_Bus;
Example:
`define BUS_WIDTH 16
`undef BUS_WIDTH
Contd..
Example
`include ./fulladder.v
Four-valued Data
0, 1
Obvious
Four-valued Logic
Z
Output 0 if one input is 0
0
1
X
Z
0
0
0
0
0
1
X
X
0
X
X
X
0
X
X
X
Operators
Arithmetic(Unary )
Relational
Bitwise
Logical
Equality
STRUCTURAL
MODELING
wire a;
// Simple wire
tri [15:0] dbus;
// 16-bit tristate bus
tri #(5,4,8) b;
// Wire with delay
reg [-1:4] vec;
// Six-bit register
trireg (small) q;
// Wire stores a small
charge
integer imem[0:1023]; // Array of 1024 integers
reg [31:0] dcache[0:63]; // A 32-bit memory
endmodule
Instantiating a Module
Instances of
look like
Gate-level Primitives
and
or
xor
buf
bufif0
bifif1
nand
nor
xnor
not
notif0
notif1
logical AND/NAND
logical OR/NOR
logical XOR/XNOR
buffer/inverter
Tristate with low enable
Tristate with high enable
buf
b1(a, b);
buf #3
b2(c, d);
buf #(4,5) b3(e, f);
buf #(3:4:5)
b4(g, h);
// Zero delay
// Delay of 3
// Rise=4, fall=5
// Min-typ-max
User-Defined Primitives
A Carry Primitive
primitive carry(out, a, b, c);
Always have exactly one
output out;
output
input a, b, c;
table
00? : 0;
Truth table may include
0?0 : 0;
dont-care (?) entries
?00 : 0;
11? : 1;
1?1 : 1;
?11 : 1;
endtable
endprimitive
A Sequential Primitive
Primitive dff( q, clk, data);
output q; reg q;
input clk, data;
table
// clk data q new-q
(01) 0 : ? : 0;
// Latch a 0
(01) 1 : ? : 1;
// Latch a 1
(0x) 1 : 1 : 1;
// Hold when d and q both 1
(0x) 0 : 0 : 0;
// Hold when d and q both 0
(?0) ? : ? : -;
// Hold when clk falls
? (??) : ? : -;
// Hold when clk stable
endtable
endprimitive
Continuous Assignment
Continuous assignment:
permanently sets the
value of sum to be
a+b+carryin
Recomputed when a, b, or
carryin changes
tb
`include "udp_body.v"
module udp_body_tb();
reg b,c;
wire a;
udp_body udp (a,b,c);
initial begin
$monitor(" B = %b C = %b A = %b",b,c,a);
b = 0; c = 0;
#1 b = 1;
#1 b = 0;
#1 c = 1;
#1 b = 1'bx;
#1 c = 0;
#1 b = 1;
#1 c = 1'bx;
#1 b = 0;
#1 $finish;
endmodule
output
B=0C=0A=0
B=1C=0A=1
B=0C=0A=0
B=0C=1A=1
B=xC=1A=1
B=xC=0A=x
B=1C=0A=1
B=1C=xA=1
B=0C=xA=x
table
symbols
interpretation
logic
0,1,X
0,1
(10)
(01)
(??)
All transitions
No change
No change
edge
BEHAVIORAL
MODELING
initial
begin
imperative statements
end
always
begin
imperative statements
end
initial begin
#10 a = 1; b = 0;
#10 a = 0; b = 1;
end
Procedural Assignment
sum = a + b + cin;
Contd..
Statement
always Statement
initial begin
Sum = 0;
Carry = 0;
end
module initial_fork_join();
reg clk,reset,enable,data;
initial begin
$monitor("%g clk=%b reset=%b enable=%b data=%b",
$time, clk, reset, enable, data);
fork
#1 clk = 0;
#10 reset = 0;
#5 enable = 0;
#3 data = 0;
join
#1 $display ("%g Terminating simulation", $time);
$finish;
end
Output
clk=x reset=x enable=x data=x
1 clk=0 reset=x enable=x data=x
3 clk=0 reset=x enable=x data=0
5 clk=0 reset=x enable=0 data=0
10 clk=0 reset=0 enable=0 data=0
11 Terminating simulation
Imperative Statements
if (select == 1)
else
y = a;
y = b;
case (op)
2b00: y = a + b;
2b01: y = a b;
2b10: y = a ^ b;
default: y = hxxxx;
endcase
Delay
Inter-Assignment Delay
Example:
Sum = A ^ B;
#2 Carry = A & B;
Delayed execution
Intra-Assignment Delay
Example:
Sum = A ^ B;
Carry = #2 A & B;
Delayed assignment
module edge_wait_example();
reg enable, clk, trigger;
always @ (posedge enable)
begin
trigger = 0;
// Wait for 5 clock cycles
repeat (5) begin
@ (posedge clk) ;
end
trigger = 1;
end
Discrete-event Simulation
Event
Event Control
Edge Triggered Event Control
Level Triggered Event Control
For Loops
While Loops
i = 0;
while (I <= 15) begin
output = i;
#10 i = i + 1;
end
reg q;
Fundamental problem:
In
Non-blocking Assignments
Nonblocking rule:
RHS evaluated when
assignment runs
b = a;
c = b;
b <= a;
c <= b;
Blocking assignment:
a=b=c=1
Nonblocking assignment:
a=1
b = old value of a
c = old value of b
b = a;
c = b;
1
a <= 1;
b <= a;
c <= b;
b
c
Conditional statements
if Statement
Format:
if (condition)
procedural_statement
else if (condition)
procedural_statement
else
procedural_statement
Example:
if (Clk)
Q = 0;
else
Q = D;
Contd..
Case Statement
Example 1:
case (X)
2b00: Y = A + B;
2b01: Y = A B;
2b10: Y = A / B;
endcase
Example 2:
case (3b101 << 2)
3b100: A = B + C;
4b0100: A = B C;
5b10100: A = B / C; //This statement is executed
endcase
Contd..
Logic Synthesis
Logic Synthesis
Register inference
a netlist
Continuous
assignment
Expressions
Structural definitions
Behavioral blocks
Everything
Depends on sensitivity list
Only when they have reasonable interpretation as
combinational logic, edge, or level-sensitive latches
Blocks sensitive to both edges of the clock, changes on
unrelated signals, changing sensitivity lists, etc. cannot be
synthesized
User-defined primitives
Initial blocks
Used to set up initial state or describe finite testbench
stimuli
Dont have obvious hardware component
Delays
Register Inference
Register Inference
Combinational:
reg y;
always @(a or b or sel)
if (sel) y = a;
else y = b;
Sensitive to changes on
all of the variables it reads
Y is always assigned
Sequential:
reg q;
always @(d or clk)
if (clk) q = d;
Register Inference
always @(a or b)
case ({a, b})
2b00 : f = 0;
2b01 : f = 1;
2b10 : f = 1;
endcase
Register Inference
always @(a or b)
case ({a, b})
2b00: f = 0;
2b01: f = 1;
2b10: f = 1;
default: f = 0;
endcase
f is always assigned
Simulation-synthesis
Mismatches
Compared to VHDL
VHDL is better-behaved