Verilog Merge
Verilog Merge
Verilog® HDL
Useful Modeling Techniques
Maziar Goudarzi
Today Program
Two methods
defparam statement
Module instance parameter value assignment
defparam statement
Keyword: defparam
Syntax:
defparam <parameter_hierarchical_name>=<value>;
Usage:
To compile some part of code under certain
conditions
Keywords:
‘ifdef, `else, `endif
‘define to define the flag
Behavioral Modeling
Timing controls
Other features
12/4/2022
Behavioral Modeling
Timing Controls in
Behavioral Modeling
Introduction
12/4/2022
Delay-based
Timing Controls
Delay Duration between encountering
and executing a statement
Delay symbol: #
Delay specification syntax:
<delay>
::= #<NUMBER>
||= #<identifier>
||= #<mintypmax_exp> <,<mintypmax_exp>>*)
12/4/2022
Delay-based
Timing Controls (cont’d)
Types of delay-based timing controls
1. Regular delay control
2. Intra-assignment delay control
3. Zero-delay control
12/4/2022
Delay-based
Timing Controls (cont’d)
Regular Delay Control
Symbol: non-zero delay before a procedural assignment
Used in most of our previous examples
12/4/2022
Delay-based
Timing Controls (cont’d)
Intra-assignment Delay Control
Symbol: non-zero delay to the right of the
assignment operator
Operation sequence:
1. Compute the right-hand-side expression at the
current time.
2. Defer the assignment of the above computed value
to the LHS by the specified delay.
12/4/2022
Delay-based
Timing Controls (cont’d)
Intra-assignment delay examples
12/4/2022
Delay-based
Timing Controls (cont’d)
Zero-Delay Control
Symbol: #0
Different initial/always blocks in the same
simulation time
Execution order non-deterministic
Zero-delay ensures execution after all other
statements
Eliminates race conditions
Multiple zero-delay statements
Non-deterministic execution order
12/4/2022
Delay-based
Timing Controls (cont’d)
Zero-delay control examples
12/4/2022
Event-based
Timing Control
Event
Change in the value of a register or net
Used to trigger execution of a statement or
block (reactive behavior/reactivity)
Types of Event-based timing control
1. Regular event control
2. Named event control
3. Event OR control
4. Level-sensitive timing control (next section)
12/4/2022
Event-based
Timing Control (cont’d)
Regular event control
Symbol: @(<event>)
Events to specify:
posedge sig
• Change of sig from any value to 1
or from 0 to any value
negedge sig
• Change of sig from any value to 0
or from 1 to any value
sig
• Any chage in sig value
12/4/2022
Event-based
Timing Control (cont’d)
Regular event control examples
12/4/2022
Event-based
Timing Control (cont’d)
Named event control
You can declare (name) an event, and then
trigger and recognize it.
Verilog keyword for declaration: event
event calc_finished;
Verilog symbol for triggering: ->
->calc_finished
Verilog symbol for recognizing: @()
@(calc_finished)
12/4/2022
Event-based
Timing Control (cont’d)
Named event control examples
12/4/2022
Event-based
Timing Control (cont’d)
Event OR control
Used when need to trigger a block upon occurrence of
any of a set of events.
The list of the events: sensitivity list
Verilog keyword: or
12/4/2022
Level-sensitive
Timing Control
Level-sensitive vs. event-based
event-based: wait for triggering of an event
(change in signal value)
level-sensitive: wait for a certain condition (on
values/levels of signals)
Verilog keyword: wait()
always
wait(count_enable) #20 count=count+1;
12/4/2022
Behavioral Modeling
Other Features
Contents
12/4/2022
Sequential and Parallel Blocks
Parallel Blocks
Keywords: fork, join
Statements in the blocks are executed
concurrently
Timing controls specify the order of execution of
the statements
All delays are relative to the time the block was
entered
The written order of statements is not important
12/4/2022
Sequential and Parallel Blocks (cont’d)
initial
begin
x=1’b0;
#5 y=1’b1;
#10 z={x,y};
#20 w={y,x};
end
initial
fork
x=1’b0;
#5 y=1’b1;
#10 z={x,y};
#20 w={y,x};
join
12/4/2022
Sequential and Parallel Blocks (cont’d)
12/4/2022
Special Features of Blocks
Contents
Nested blocks
Named blocks
Disabling named blocks
12/4/2022
Special Features of Blocks (cont’d)
Nested blocks
Sequential and parallel blocks can be mixed
initial
begin
x=1’b0;
fork
#5 y=1’b1;
#10 z={x,y};
join
#20 w={y,x};
end
12/4/2022
Special Features of Blocks (cont’d)
Named blocks
Syntax:
begin: <the_name> fork: <the_name>
… …
end join
Advantages:
Can have local variables
Are part of the design hierarchy.
Their local variables can be accessed using hierarchical
names
Can be disabled
12/4/2022
Special Features of Blocks (cont’d)
module top;
initial
begin : block1
integer i; //hiera. name: top.block1.i
…
end
initial
fork : block2
reg i; //hierarchical name: top.block2.i
…
join
endmodule
12/4/2022
Special Features of Blocks (cont’d)
12/4/2022
Special Features of Blocks (cont’d)
module find_true_bit; while(i < 16)
begin
reg [15:0] flag; if (flag[i])
integer i; begin
$display("Encountered a
initial TRUE bit at element
begin number %d", i);
flag = 16'b disable block1;
0010_0000_0000_0000; end // if
i = 0; i = i + 1;
begin: block1 end // while
end // block1
end //initial
endmodule
12/4/2022
Behavioral Modeling
Examples
4-to-1 Multiplexer
// 4-to-1 multiplexer. Port list is taken exactly from
// the I/O diagram.
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
endmodule
4-bit Counter
//Binary counter
module counter(Q , clock, clear);
// I/O ports
output [3:0] Q;
input clock, clear;
//output defined as register
reg [3:0] Q;
endmodule
12/4/2022
Traffic Signal Controller
12/4/2022
`define TRUE 1'b1
`define FALSE 1'b0
`define RED 2'd0
`define YELLOW 2'd1
`define GREEN 2'd2
//Delays
`define Y2RDELAY 3 //Yellow to red delay
`define R2GDELAY 2 //Red to Green Delay
//I/O ports
output [1:0] hwy, cntry; //2 bit output for 3 states of signal GREEN, YELLOW, RED;
reg [1:0] hwy, cntry; //declare output signals are registers
input X; //if TRUE, indicates that there is car on the country road, otherwise FALSE
12/4/2022
//Signal controller starts in S0 state
initial begin
state = `S0;
next_state = `S0;
hwy = `GREEN;
cntry = `RED;
end
endmodule
12/4/2022
Digital System Design
Verilog® HDL
Behavioral Modeling (1)
Today program
Behavioral Modeling
Concepts
Constructs
initial
begin
#5 a=1’b1;
#25 b=1’b0;
end
initial
begin
#10 x=1’b0;
#25 y=1’b1;
end
initial
What happens if such a
#1000 $finish; $finish is not included?
endmodule
12/4/2022 Verilog HDL 70
Procedural Assignments
<lvalue> can be
reg, integer, real, time
A bit-select of the above (e.g., addr[0])
A part-select of the above (e.g., addr[31:16])
A concatenation of any of the above
<expression> is the same as introduced in dataflow modeling
What happens if the widths do not match?
LHS wider than RHS => RHS is zero-extended
RHS wider than LHS => RHS is truncated (Least significant part is kept)
if (<expression>) true_statement;
else false_statement;
if (<expression>) true_statement1;
else if (<expression>) true_statement2;
else if (<expression>) true_statement3;
else default_statement;
True is 1 or non-zero
False is 0 or ambiguous (x or z)
More than one statement: begin end
if (alu_control==0)
y = x+z;
else if (alu_control==1)
y = x-z;
else if (alu_control==2)
y = x*z;
else
$display(“Invalid ALU control signal.”);
Recommendation
Concurrent data transfers => race condition
Use non-blocking assignments wherever
concurrent data transfers
Example: pipeline modeling
Disadvantage:
Lower simulation performance
Higher memory usage in the simulator
Generating Checking
inputs Circuit Under Design outputs
to CUD (CUD) of CUD
4
8
Test bench
Simulation- Test Bench Styles
Design Methodologies
4-bit Ripple Carry Counter
T-flipflop and the Hierarchy
Ports
Ports provide interface for by which a module can
communicate with its environment
Port connection rules
Connecting Ports
Suppose we have a module
Module- Basic building block
output [3:0] q;
initial begin
//monitor and display
module test (q, r);
…
output q, r;
initial begin
//drive the outputs with signals
…
Another view of this
• 3 chunks of verilog, one for each of:
Another piece of
hardware, called Your hardware
TEST, to generate called
interesting inputs DESIGN
Verilog Examples
Module testAdd generated inputs for module halfAdd and
displayed changes. Module halfAdd was the design
module testAdd(a, b, sum, cOut);
module tBench;
input sum, cOut;
wire su, co, a, b;
output a, b;
reg a, b;
halfAdd ad(su, co, a, b);
testAdd tb(a, b, su, co);
initial begin
endmodule
$monitor ($time,,
“a=%b, b=%b, sum=%b, cOut=%b”,
a, b, sum, cOut);
module halfAdd (sum, cOut, a, b); a = 0; b = 0;
output sum, cOut; #10 b = 1;
input a, b; #10 a = 1;
#10 b = 0;
xor #2 (sum, a, b); #10 $finish;
and #2 (cOut, a, b); end
endmodule endmodule
Gate Level Modeling
A logic circuit can be designed by use of logic
gates.
Verilog supports basic logic gates as predefined
primitives. These primitives are instantiated like
modules except that they are predefined in Verilog
and do not need a module definition.
Gate gate_name(out,in1,in2…)
Buf/not gates
Buflnot gates have one scalar input and
one or more scalar outputs.
Bufif/notif
Instantiation of bufif gates
Design of 4:1 Multiplexer
Contd..
Stimulus
4 bit full adder
Declaration:
Code contd..
4 bit adder using 1 bit adder
Stimulus
Gate Delays:
Rise Delay: Delay associated with a
o/p transition to 1 from any value.
//min delay=4
//type delay=5
//max delay=6
and #(4:5:6) a1(out, i1, i2) ;
output out;
input select, a, b;
table
//select a b : out
1 ? 1 : 1;
? 0 0 : 0;
UDP: Sequential Behavior
• In table description, n+2 columns for n
input
• n input columns + internal state column
+ output (next state) column
• Output port -> reg variable
Level-sensitive Behavior
primitive transparent_latch(out, enable, in);
enable
output out;
in Transparent out
input enable, in;
latch
reg out;
table
1 1 :? : 1;
1 0 :? : 0;
x 0 :0 : -;
Edge-sensitive Behavior
primitive d_flop( q, clock, d );
clock
output q;
d d_flop q
input clock, d;
reg q;
table
(0?) 1 : 1 : 1;
(0?) 0 : 0 : 0;
Maziar Goudarzi
Today Program
General syntax
#(rise_val, fall_val, turnoff_val)
#(min:typ:max, min:typ:max, min:typ:max)
Examples:
and #(5) a1(out, i1, i2);
and #(4, 6) a2(out, i1, i2);
and #(3, 4, 5) a2(out, i1, i2);
and #(1:2:3, 4:5:6, 5:6:7) a2(out, i1, i2);
Examples
wire #10 out = in1 & in2; wire out;
assign #10 out = in1 & in2;
Zero delay
Also called
Pin-to-Pin delay
Path delay
Gate-level, dataflow, and behavioral delays
Property of the elements in the module (white box)
Styles: Distributed or Lumped
Path delay
A property of the module (black box)
Delay from any input to any output port
specify block
Assign pin-to-pin delays
Define specparam constants
specparam constants
Similar to parameter, but only inside specify block
Recommended to be used instead of hard-coded delay
numbers
Handling x transitions
Pessimistic approach
Transition to x: minimum possible time
Transition from x: maximum possible time
Timing Checks
A number of system tasks defined for this
$setup: checks setup-time of a signal before
an event
$hold: checks hold-time of a signal after an
event
$width: checks width of pulses
Delays
Models
Inertial (distributed and lumped delay)
Transport (path/pin-to-pin delay)
Types
Rise/Fall/Turn-off
Min/Typ/Max Values
Delays in Verilog
Syntax and other common features
Gate-Level and Dataflow Modeling
Behavioral Modeling
2005 Verilog HDL 187
Other Notes
Homework 9
Chapter 10:
All exercises
Due date: Sunday, Day 11th
Maziar Goudarzi
Today program
Reusing code
Tasks and Functions
Procedures/Subroutines/Functions in SW
programming languages
The same functionality, in different places
Verilog equivalence:
Tasks and Functions
Used in behavioral modeling
Part of design hierarchy Hierarchical name
Functions
Tasks
Differences between tasks and functions
Semantics
much like function in Pascal
An internal implicit reg is declared inside the
function with the same name
The return value is specified by setting that
implicit reg
<range_or_type> defines width and type of
the implicit reg
<type> can be integer or real
default bit width is 1
12/4/2022 Verilog HDL 197
Function Examples
Parity Generator
module parity; function calc_parity;
reg [31:0] addr; input [31:0] address;
reg parity; begin
calc_parity = ^address;
initial begin end
… endfunction
end
endmodule
always @(addr)
begin
parity = calc_parity(addr);
$display("Parity calculated = %b",
calc_parity(addr) );
end
always @(addr)
begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end
12/4/2022 Verilog HDL 199
//Define a factorial with a recursive
// Call the function
function
integer result;
module top;
initial
...
begin
// Define the function
result = factorial(4); // Call
function automatic integer factorial;
the factorial of 7
input [31:0] oper;
$display("Factorial of 4 is
integer i;
%0d", result); //Displays 24
begin
end
if (operand >= 2)
...
factorial = factorial (oper -1) * oper;
...
//recursive call
endmodule
else
factorial = 1 ;
end
endfunction
task <task_name>;
<I/O declarations>
<variable and event declarations>
begin // if more than one statement needed
<statement(s)>
end // if begin used!
endtask
// clk2 runs at twice the frequency of clk // These two always blocks will
and is synchronous call the bitwise_xor task
// with clk.
// concurrently at each positive
module top;
reg [15:0] cd_xor, ef_xor; //variables in
edge of clk. However, since
module top // the task is re-entrant, these
reg [15:0] c, d, e, f; //variables in concurrent calls will work
module top
correctly.
-
task automatic bitwise_xor; always @(posedge clk)
output [15:0] ab_xor; //output from the bitwise_xor(ef_xor, e, f);
task
input [15:0] a, b; //inputs to the task
-
begin always @(posedge clk2) // twice the
#delay ab_and = a & b; frequency as the previous block
ab_or = a | b;
bitwise_xor(cd_xor, c, d);
ab_xor = a ^ b;
end -
endtask -
endmodule
12/4/2022 Verilog HDL 207
Tasks and Functions
Differences between
Tasks and Functions
Differences between...
Functions Tasks
Can enable (call) just Can enable other tasks
another function (not and functions
task) May execute in non-
Execute in 0 simulation zero simulation time
time May contain any timing
No timing control control statements
statements allowed May have arbitrary
At lease one input input, output, or
Return only a single inout
value Do not return any value
12/4/2022 Verilog HDL 209
Differences between… (cont’d)
Both
are defined in a module
are local to the module
can have local variables (registers, but not nets) and
events
contain only behavioral statements
do not contain initial or always statements
are called from initial or always statements or other
tasks or functions