Chapter 3 Testbench, Dataflow and Behavioral Verilog
Chapter 3 Testbench, Dataflow and Behavioral Verilog
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Syllabus
2
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Content
1.Simulator Mechanics
2.Testbench Basics
3.Dataflow Verilog
4.Behavior Verilog
5.Blocking and Non-blocking
6.Conditional statements
3
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
1. Simulation Mechanics
Analog Simulation
• Divide “time” into slices
• Update information in whole circuit at each slice
• Used by LTSPICE
• Allows detailed modeling of current and voltage
• Computationally intensive and slow
• Don’t need this level of detail for most digital logic simulation
4
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
1. Simulation Mechanics
Digital Simulation
• Could update every signal on an input change
6
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
1. Simulation Mechanics
Event-Driven Simulation
• Update only if changed
7
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics
Need to verify your design
• “Design Under Test” (DUT)
Use a “testbench”
• Special Verilog module without input/output ports
• Generates out routes inputs to the DUT
• For now, we will monitor outputs via human interface
8
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics
9
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Requirements)
Instantiate the unit being tested (DUT)
Provide input to that unit
• Usually a number of different input combinations!
Watch the “results” (outputs of the DUT)
• Can watch ModelSim Wave window …
• Can print out information to the screen or to the file
This way of monitoring outputs (human interface) is dangerous &
incomplete
• Subject to human error
• Cannot be automated into batch jobs
• Self checking testbenches will be covered later
10
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Output Test
Info)
Several different system calls to output info
• $monitor
Output the given values whenever one changes
Can use when simulating Structural, RTL, and/or Behavioral
• $display, $strobe
Output specific information like a printf in a C program
Used in Behavioral Verilog
Can use formatting strings with these commands
Only means anything in simulation
Ignored by synthesizer
11
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Output Format
Strings)
12
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Output Format
Strings)
Formatting string
• %h, %H hex
• %d, %D decimal
• %o, %O octal
• %b, %B binary
• %t time
$monitor(“%t: %b %h %h %h %b\n”,
$time, c_out, sum, a, b, c_in);
16
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
Step 1: Declare module testbench
• Usually add _tb to the testbench module name
17
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
Step 4: Monitor the signals that are connected to the outputs of the DUT
and stop/finish simulation
• Use the system task $monitor with time stamp
• Stop/finish with $stop/$finish (what is the different between them?)
Step 5: Generate the signals that are connected to the inputs of the DUT
• Usually use exhaustive test to cover all input combinations
18
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
19
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
20
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
Let do more ModelSim example here!
Example of 3-bit wide adder block
21
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
22
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (DUT Example)
23
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Generating
Clocks)
Wrong way:
Right way:
LESS TYPING
Easier to read, harder to make mistake 24
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Response to input vector depends on state
25
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Write a testbench to test a gray code counter
module gray_counter(out, clk, rst);
Initially reset the counter and then test all states, but do not test reset
in each state.
26
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Solution for gray code counter – Test 1
27
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Solution for gray code counter – Test 1
28
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Force/Release)
Example
29
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (Force/Release)
Allows you to “override” value FOR SIMULATION
Does not do anything in “real life”
30
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Write a testbench to test a gray code counter example above
module gray_counter(out, clk, rst);
Initially reset the counter and then test all states, then test reset in
each state
31
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Solution for gray code counter – Test 2
32
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
2. Testbench Basics (FSM Testing)
Solution for gray code counter – Test 2
33
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
3. Dataflow Verilog
This continuous assign statement
• It is the main construct of Dataflow Verilog
• It is deceptively powerful & useful
Generic form:
34
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
3. Dataflow Verilog
Simplest form:
// out is a net, a & b are also nets
assign out = a & b; // and gate functionality
Using vectors
wire [15:0] results, src1, src2; // 3 16-bit wide vectors
assign results = src1 ^ src2; // 16-bit wide XOR
Can you implement a 32-bit adder in a single line?
35
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Vector Concatenation
Can build vectors using smaller vectors and/or scalar values
Use the {} operator
Example 1
36
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Vector Concatenation
Example 2
39
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Operators: Arithmetic
Much easier than structural!
40
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Operators
Shift (<<, >>, <<<, >>>)
• <<<, >>> shift bit while keeping the sign bit
Relational (<, >, <=, >=)
Equality (==, !=, ===, !===)
• ===, !== test x’s, z’s! ONLY USE FOR SIMULATION!
Logical Operators (&&, ||, !)
• Build clause for if statement or conditional expression
• Return single bit values
Bitwise Operators (&, |, ^, ~)
• Applies bit-by-bit
Watch ~ vs. !, | vs. ||, and & vs. &&
41
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Reduction Operators
Reduction operators are reduce all the bits of a vector to a
single bit by performing an operation across all bits.
• Reduction AND
assign all_ones = &accumulator; // are all bits set?
• Reduction OR
assign not_zero = |accumulator; // are any bits set?
• Reduction XOR
assign parity = data_out; // even parity bit
42
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Conditional Operators
The functionality of a 2:1 MUX
assign out = conditional_expr ? true_expr : false_expr;
43
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Conditional Operators
44
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Operators: Precedence
Operators Operator Symbols Precedence
Reduction !~&| Highest Precedence
~& ~| ^ ~^
Unary + -
Multiply, Divide, Modulus * / %
Add, Subtract + -
Shift << >>
Relational < <= > >=
Equality == != === !==
Bitwise & ^ ~^ |
Logical && ||
Conditional ?: Lowest Precedence
45
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
3. Dataflow Verilog (Latches)
What does the following statement imply/do?
assign q_out = enable ? data_in : q_out;
• It acts as a latch. If enable is high, new data goes to q_out. Otherwise,
q_out maintains its previous value.
• How does it synthesize??
46
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog
initial and always form basis of all behavioral Verilog
• All other behavioral statements occur within these
• initial and always blocks cannot be nested
• All <LHS> assignments must be to type reg
initial statements start at time 0 and execute once
• If there are multiple initial blocks, they all start at time 0 and execute
independently. They may finish independently.
If multiple behavioral statements are needed within the initial
statement then the initial statement can be made compound with use
of begin/end
Initial statement very useful for testbenches.
47
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (initial Blocks)
48
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (initial Blocks)
49
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (always)
Behavioral block operates CONTINUOUSLY
• Executes at time zero but loop continuously
• Can use a trigger list (or sensitivity list) to control operation; @(a, b, c)
• In absence of a trigger list, it will re-evaluate when the last <LHS>
assignment completes.
50
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (always vs initial)
52
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (Trigger lists)
Conditionally “execute” inside of always block
• Any change on trigger (sensitivity) list, triggers block
54
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (FlipFlops)
A negedge is on the transitions
• 1 x, z, 0
• x, z 0
A posedge is on the transitions
• 0 x, z, 1
• x, z 1
Used for clocked (synchronous) logic
55
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (FlipFlops)
56
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (FlipFlops)
57
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (FlipFlops)
58
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (FlipFlops)
59
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
Know your cell library
What type of flops are available
• + (posedge) or – (negedge) edge triggered (most are positive)
• Is the asynchronous reset active high or active low?
• Is a synchronous reset available?
60
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
4. Behavioral Verilog (Combinational
vs. Sequential)
Combinational
• Not edge-triggered
• All “input” (RHS nets/variables) are triggers
• Does not depend on clock
Sequential
• Edge-triggered by clock signal
• Only clock (and possibly reset) appear in trigger list
• Can include combinational logic that feeds a FF or register
61
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
Blocking “Evaluated” sequentially
Used for combinational logic
62
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
Non blocking “updated” simultaneously if no delays given
Used for sequential logic
Use <= operator
63
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
Swapping if done in Blocking
Need a “temp” variable
64
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
Called blocking because …
• The evaluation of subsequent statements <RHS> are blocked, until
<LHS> assignment of the current statement is completed.
65
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
With non-blocking statements, the <RHS> of subsequent
statements are not blocked.
They are all evaluated simultaneously.
66
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
Why not Non-Blocking for combinational?
Can we make this example work?
67
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
5. Blocking and non-Blocking
So Blocking is no good and we should always use Non-
Blocking?
Consider combinational logic
68
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (If…else)
General form
69
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (If…else)
How does and if…else if…else statement synthesize?
• Does not conditionally “execute” block of code!
• Does not conditionally create hardware!
• It makes a multiplexer or selecting logic
• Generally:
Hardware for both is created
Both paths “compute” simultaneously
The result is selected depending on the condition
70
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (If…else)
How does and if…else if…else statement synthesize?
71
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (If…else)
Watch the sensitivity lists…what is missing in this example?
72
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (case)
Verilog has three types of case statements:
• case, casex, and casez
Performs bitwise match of expression and case item
• Both must have same bitwidth to match!
case
• Can detect x and z! (good for testbenches)
casez
• Uses z and ? as “don’t’ care” bits in case items and expression
casex
Uses x, z, and ? as “don’t’ care” bits in case items and expression
73
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (case)
General form
74
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (case)
Using case to detect x and z
Only use this functionality in a testbenches!
Example
75
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn
6. Conditional Statements (casex)
Uses x, z, and ? as single-bit wildcards in case item and
expression
Uses first match encountered
78
thuan.levan@phenikaa-
pail.phenikaa-uni.edu.vn