Lec06 (DSD)
Lec06 (DSD)
Verilog: Part 1
Hardware description
languages (HDLs)
Not (SW) programming
languages
Parallel languages tailored to
digital design
Synthesize code to produce a
circuit
2
Hardware Description Language
HDL – a “language” for
describing hardware
Two industry IEEE standards:
Verilog
VHDL (Very High Speed
Integrated Circuit HDL)
Verilog
Originally designed for
simulation and verification
Gateway Design Automation,
1984
Functionality later added for
synthesis
4
History of the Verilog HDL (Cont’d)
1995: The IEEE standardized the Verilog HDL (IEEE 1364)
2001: The IEEE enhanced the Verilog HDL for modeling
scalable designs, deep sub-micron accuracy, etc.
2005: Verilog 2005 (IEEE Standard 1364-2005) consists
of minor corrections, spec clarifications, and a few new
language features
A separate part of the Verilog standard, Verilog-AMS, attempts
to integrate analog and mixed signal modeling with traditional
Verilog.
2009: Verilog standard (IEEE 1364-2005) was merged
into the SystemVerilog standard (IEEE 1800-2009)
Now Verilog is officially part of the SystemVerilog language
Current version is IEEE 1800-2017
5
Productivity is critical!
8
SystemVerilog Misunderstandings
Verilog is a design language, and
SystemVerilog is a verification
language And synthesis
compilers can’t
read in
SystemVerilog
What is SystemVerilog?
SystemVerilog offers productivity!
It is a concise, unified language for design and testbenches.
A single simulation tool can verify a design with advanced testbench, and
verification features included.
SystemVerilog (IEEE 1800) adds extensions to the IEEE Verilog
2001 standard:
C/C++ type language constructs for efficient programming
Language enhancements for synthesis and downstream tools
Interfaces to encapsulate communication between design blocks
Assertions and coverage for new verification techniques
System-level testbench features to allow advanced verification
methodologies
Lightweight interface to C/C++ programs
Verilog-2005
uwire `begin_keywords `pragma $clog2
Verilog-2001
ANSI C style ports standard file I/O (* attributes *) multi dimensional arrays
Generate $value$plusargs Configurations signed types
Localparam `ifndef `elsif `line memory part selects automatic
constant functions @* variable part select ** (power operator)
Verilog-1995 (created in 1984)
modules $finish $fopen $fclose initial wire reg begin–end
parameters $display $write disable integer real while + =* /
function/tasks $monitor Events time for forever %
always @ `define `ifdef `else wait # @ packed arrays if–else >> <<
assign `include `timescale fork–join 2D memory repeat
11
100M LOC
13
14
Design Flow
What functions are you want to program?
Input and output
Specify the Input/Output signals
Separate the whole circuit into smaller ones
Each block has its own function / purpose
Connect all blocks/modules
B1 B2
B3
15
Verification Scenario
Use test bench file to verify your design
Stimulus Response
& &
Control Signal Verification
16
Simulation and Synthesis
Simulation tools typically accept full set of Verilog
language constructs
Some language constructs and their use in a Verilog
description make simulation efficient and are ignored by
synthesis tools details will be covered later
Synthesis tools typically accept only a subset of the full
Verilog language constructs
Synthesizable subset
17
Simulation …
18
… and Synthesis
19
Value Meaning
0 Logic zero
1 Logic one
X Unknown logic value
Z High impedance, floating
20
wire is used to denote a standard hardware net
?
memory_req
instruction
instruction
small_net
21
8’b0xx0_1xx1
4’b10_11
Hexadecimal literals Underscores
32’h0a34_def1 are ignored
22
Testbench Approach
Use Verilog module to produce testing environment
including stimulus generation and/or response
monitoring
Stimulus Response
UUT
Module
Testbench Module
23
Modules
The Module Concept
Basic design unit for Verilog designs
Modules are:
Declared
Instantiated
24
What is a module?
module is a block of circuit
Special function
AND, OR, Mux, Adder, Shifter…
Most important
Input and Output
module
25
Module
General definition Example
module module_name
module HalfAdder (A, B, Sum, Carry);
( port_list );
input A, B;
port declarations; output Sum, Carry;
… assign Sum = A ^ B;
variable declaration; //^ denotes XOR
… assign Carry = A & B;
// & denotes AND
description of behavior
endmodule
endmodule
26
Lexical Conventions
Comments
// Single line comment
/* Another single line comment */
/* Begins multi-line (block) comment
All text within is ignored
Line below ends multi-line comment
*/
Number
decimal, hex, octal, binary
unsized decimal form
size base form
String
" Enclose between quotes on a single line"
27
28
Module name and a port list
// HDL modeling of
Adder // adder functionality
endmodule
4
29
30
Module instantiation to create a module hierarchy
endmodule
c
31
A B
Adder FA FA FA FA
cout S
32
Module instantiation to create a module hierarchy
A B
Adder FA FA FA FA
cout S
34
Schematic diagram into structural Verilog
FA FA FA FA
35
Behavioral
Algorithm V Abstract algorithmic description
Manual
36
Gate-level Verilog uses Structural Verilog
module mux4( input a, b, c, d, input [1:0] sel, output out );
wire [1:0] sel_b;
not not0( sel_b[0], sel[0] ); b d a c sel[1] sel[0]
not not1( sel_b[1], sel[1] );
wire n0, n1, n2, n3;
and and0( n0, c, sel[1] );
and and1( n1, a, sel_b[1] );
and and2( n2, d, sel[1] );
and and3( n3, b, sel_b[1] );
wire x0, x1;
nor nor0( x0, n0, n1 );
nor nor1( x1, n2, n3 );
wire y0, y1;
or or0( y0, x0, sel[0] );
or or1( y1, x1, sel_b[0] );
nand nand0( out, y0, y1 );
out
endmodule
37
Continuous Assignment
Explicit continuous assignment
38
Using Continuous Assignment for 4-to-1 Mux
endmodule
39
endmodule
40
Verilog RTL Operators
Arithmetic + - * / % ** Reduction & ~& | ~| ^ ^~(or ~^)
Logical ! && || Shift >> << >>> <<<
Relational > < >= <= Concatenation {}
Equality == != === !== Conditional ?:
Bitwise ~ & | ^ ^~(or ~^)
logical case equality (===), logical case inequality (!==): allows for checking of x and z
values
41
42
Verilog RTL Operators
Bitwise Operators
Logical operators result in logical 1, 0 or x
Bitwise operators results in a bit-by-bit value
43
// let a = 4, b = 3, and...
// i = 4'b1010, j = 4'b1101,
// k = 4'b1xxz, m = 4'b1xxz, n = 4'b1xxx
a == b //evaluates to logical 0
i != j //evaluates to logical 1
i == k //evaluates to x
k === m //evaluates to logical 1
k === n //evaluates to logical 0
m !== n //evaluates to logical 1
44
Verilog RTL Operators
Reduction Operators
and(&), nand(~&), or(|), nor(~|), xor(^), xnor (~^)
Operates on only one operand
Performs a bitwise operation on all bits of the operand
Returns a 1-bit result
Works from right to left, bit by bit
// let a = 4'b1010
45
// let a = 4'b1100
y = a >> 1; // y is 4'b0110
y = a << 1; // y is 4'b1000
y = a << 2; // y is 4'b0000
46
Verilog RTL Operators
Shift Operators
Arithmetic Shift Operators
arithmetic right shift (>>>)
Shift right specified number of bits, fill with value of sign bit if expression is
signed, otherwise fill with zero
arithmetic left shift (<<<)
Shift left specified number of bits, filling with zero
47
Functions
Use functions for complex combinational logic
module and_gate (out, in1, in2);
input in1, in2;
output out;
function myfunction;
input in1, in2; Benefit:
begin Functions force a result
myfunction = in1 & in2; Compiler will fail if function
end does not generate a result
endfunction
endmodule
48
Procedural Constructs
Two Procedural Constructs
initial Statement
always Statement
initial Statement : Executes only once
usually not used in design because it is not synthesizable.
always Statement : Executes in a loop
Example:
… …
initial begin always @(A or B) begin
Sum = 0; Sum = A ^ B;
Carry = 0; Carry = A & B;
end end
… …
49
@ Time Control
@(*)
@ (expression)
@ (expression or expression or …)
@ (posedge onebit)
@ (negedge onebit)
do not execute statement until event occurs
@(clk) is same as @(posedge clk or negedge clk)
50
Event Control
Event Control
Edge Triggered Event Control
Level Triggered Event Control
51
Verilog-2001 support of
Verilog-2001 style that
Verilog-95 style comma separated
only writes * in the list
sensitivity list
53
clk
in
outA
outB
outC
54
Loop Statements
Loop Statements
repeat
while
for
Repeat Loop
Example:
repeat (Count)
sum = sum + 5;
If condition is a x or z it is treated as 0
55
56
Conditional Statements - if … else if… else
Format:
if (condition)
procedural_statement
else if (condition)
procedural_statement
else
procedural_statement
Example:
if (Clk)
Q = 0;
else
Q = D;
57
Example 2:
case (3’b101 << 2)
3’b100: A = B + C;
4’b0100: A = B – C;
5’b10100: A = B / C; //This statement is executed
endcase
58
Conditional Statements (cont.)
Variants of case Statements:
casex and casez
Example:
casez (sel[1:0])
2’b0z: A = B + C;
2’b1z: A = B / C;
endcase https://vlsiverify.com/verilog/case-statement
59
Assignments
Blocking assignments (Q = A) always @(posedge CLK)
Variable is assigned immediately begin
B = A;
New value is used by subsequent A = B;
statements end
B=A, A=A
Non-blocking assignments (Q <= A)
Variable is assigned after all scheduled
always @(posedge CLK)
statements are executed begin
B <= A;
Value to be assigned is computed but
saved for later parallel assignment A <= B;
end
Usual use: Register assignment
Registers simultaneously take new values Swap A and B
after the clock edge
60
Blocking vs. Non-blocking Assignment
Blocking assignment Non-blocking assignment
‘B’ is not
always @(posedge CLK) always @(posedge CLK) affected by the
begin begin previous
statement
B = A; // evaluated first serially B <= A; // statement yet.
//wait for the above evaluation to complete C <= B; // statement
//before its own evaluation, end
//which makes the final evaluation to be // Here, statement and
// C = B = A // are executed concurrently.
C = B;
end
A D Q C B
CLK A D Q D Q C
CLK
D Q B
61
‘always’
‘always’ blocks have parallel inter-block and sequential
intra-block semantics
module mux4( input a, b, c, d,
input [1:0] sel,
output out );
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
end
62
‘always’
‘always’ blocks have parallel inter-block and sequential
intra-block semantics
module mux4( input a, b, c, d,
input [1:0] sel,
output out );
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
end
The order of these procedural assignment
endmodule statements does matter.
They essentially happen sequentially!
63
‘always’
‘always’ blocks have parallel inter-block and sequential
intra-block semantics
module mux4( input a, b, c, d,
input [1:0] sel,
output out );
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
end
LHS of procedural assignments must be
endmodule
declared as a reg type. Verilog reg is not
necessarily a hardware register!
64
‘always’
‘always’ blocks have parallel inter-block and sequential
intra-block semantics
module mux4( input a, b, c, d,
input [1:0] sel,
output out );
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
end
What happens if we accidentally forget
endmodule
a signal on the sensitivity list?
65
‘always’
‘always’ blocks have parallel inter-block and sequential
intra-block semantics
module mux4( input a, b, c, d,
input [1:0] sel,
output out );
always @( * )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
end
Verilog-2001 provides special syntax to
endmodule automatically create a sensitivity list for all
signals read in the always block
66
Continuous vs. Procedural Assignments
Continuous assignments are for naming and thus we cannot have
multiple assignments for the same wire.
wire out, t0, t1;
assign t0 = ~( (sel[1] & c) | (~sel[1] & a) );
assign t1 = ~( (sel[1] & d) | (~sel[1] & b) );
assign out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
68
What happens if ‘case’ statement is not complete?
endmodule
69
endmodule
70
How to make Latches and Flip-Flops?
71
Non-blocking Assignment
The order of non-blocking assignments does not matter!
72
Incorrect Flip-Flop in Verilog
Use always block's sensitivity list to wait for clock to
change
module dff (CLK, d, q);
input CLK, d;
output q; Double edge-triggered.
reg q; This should be avoided.
always @(CLK)
q <= d;
endmodule
73
input CLK, d;
output q;
reg q;
endmodule
74
Common Patterns for Latch and Flip-Flop Inference
always @( clk or D )
begin next_X D Q X
if ( clk )
clk
Q <= D;
end
75
More Flip-Flops
Synchronous/asynchronous reset
single thread that waits for the clock
two parallel threads – only one of which waits for the clock
Synchronous Asynchronous
Specifying
module dff (CLK, rb, en, d, q); module dff (CLK, rb, en, d, q);
active low
input CLK, rb, en, d; input CLK, rb, en, d;
output q; reset
output q;
reg q; reg q;
always @(posedge CLK)
if (!rb) q <= 1'b0; always @(negedge rb or posedge CLK)
else if (en) q <= d; if (!rb) q <= 1’b0;
endmodule
else if (en) q <= d;
endmodule
76
Writing Good Synthesizable Verilog (but not mandatory)
Only leaf modules should have functionality
All other modules are strictly structural, i.e., they only wire together sub-
modules
Use only positive-edge triggered flip-flops for state
Do not assign to the same variable from more than one always
block
Separate combinational logic from sequential logic
Combinational logic described using always @(*) and blocking =
assignments and assign statements
Sequential logic described with always @(posedge clk) and non-blocking
<= assignments
Combinational logic
assign C_in = B_out + 1;
always @( posedge clk ) Update of state only (don’t be tempted
C_out <= C_in; to add combinational logic into
clocked always block)
77
Behavioral Verilog
Behavioral Verilog is used to model the abstract function
of a hardware module
Characterized by heavy use of sequential blocking
statements in large always blocks
Many constructs are not synthesizable but can be useful
for behavioral modeling
Data dependent for and while loops
Additional behavioral data types : integer, real
Magic initialization blocks : initial
Magic delay statements: #<delay>
78
High-level Behavioral Hardware Model
module factorial( input [ 7:0] in, output reg [15:0] out );
integer num_calls;
initial num_calls = 0;
Initial statement
integer multiplier;
integer result; Variables of type integer
always @(*)
begin
multiplier = in;
result = 1;
while ( multiplier > 0 )
begin
result = result * multiplier; Data dependent while loop
multiplier = multiplier - 1;
end
out = result;
num_calls = num_calls + 1;
end
endmodule
79
Delay Statements
module mux4
(
input a,
input b,
input c, Although this will add a delay for simulation,
input d, these are ignored in synthesis
input [1:0] sel,
output out
);
endmodule
80
# Time Control
$time
A built-in variable that represents simulated time
a unitless integer
$display($time, “a=%d”, a);
# Time Control
#<number> statement
statement is not executed until <number> time units have
passed
control is released so that other processes can execute
used to model propagation delay in combinational logic
81
System Tasks
System tasks are used for test harnesses and simulation management
Initial begin
/* To enable all the features of vcs, put the following statement
at the very beginning of your "initial" block. */
$vcdpluson( ); //allows all signals to be visible in the simulation.
// This gets the program to load into memory from the command line
if ( $value$plusargs( "exe=%s", exe_filename ) )
$readmemh( exe_filename, mem.m );
else
begin
$display( "ERROR: No executable specified! (use +exe=<filename>)" );
$finish;
end
// Stobe reset
#0 reset = 1;
#38 reset = 0;
end
82
Which Abstraction is the Right One?
83
84
Interview Question: which is better?
A D
C
B
2x the
processes clk Combinational
= 2x the & sequential
complexity logics are not
separated.
assign D = A & B;
always @ (posedge clk)
always @ (posedge clk) begin
begin C <= A & B;
C <= D; end
end
85
Announcements
Homework #4 (Due: Next Week)
Textbook Problems : 2.1, 2.2, 2.4, 2.6, 2.7
86