Beruflich Dokumente
Kultur Dokumente
Lecture 2
2
Module Port List
Two basic ways to declare the ports of a module
5
Analog Circuit Simulation
Divide time into slices
Update information in whole circuit at each slice
Used by SPICE
6
Digital Simulation
Could update every signal on input change
0 1 0 1
0 1 1 0 0 1
1 0 1
1 1 0
1 1 1 1
0 0
8
Simulation
Update only if changed
0 1 0 1
0 1 1 0 0 1
1 0 1
1 1 0
1 1 1 1
0 0
9
Simulation of Verilog
Need to verify your design
Unit Under Test (UUT)
Use a testbench!
Special Verilog module with no ports
Generates or routes inputs to the UUT
Outputs information about the results
Testbench Testbench
10
Simulation Example
module adder4b (sum, c_out, a, b, c_in);
input [3:0] a, b;
input c_in;
output [3:0] sum;
output c_out;
assign {c_out, sum} = a + b + c_in;
endmodule
4
a[3:0] 4
4 sum[3:0]
b[3:0] adder4b
c_out
c_in
11
Simulation Example
t_adder4b
4
a[3:0] 4
4 adder4b sum[3:0]
b[3:0] (UUT) c_out
c_in
12
Example
not an
`timescale 1ns /100ps apostrophe! // time_unit/time_precision
module t_adder4b;
reg[8:0] stim; // inputs to UUT are regs
wire[3:0] S; // outputs of UUT are wires
wire C_out;
all inputs grouped
UUT into single vector
// instantiate UUT
adder4b(S, C_out, stim[8:5], stim[4:1], stim[0]); (not required)
// stimulus generation
initial begin
stim = 9'b000000000; // at 0 ns
#10 stim = 9'b111100001; // at 10 ns see response
Behav. #10 stim = 9'b000011111; // at 20 ns to each of these
Verilog: #10 stim = 9'b111100010; // at 30 ns input vectors
do this #10 stim = 9'b000111110; // at 40 ns
once #10 $stop; // at 50 ns stops simulation
end timing control
endmodule for simulation
13
Overview
Module Port List: Revisited
Simulation and Testbenches
Simulation Overview
Timing Controls and Delay
Testbenches
Verilog Shortcuts
Concatenating Vectors
Arrays of Instances
Introduction to RTL Verilog
(Continuous Assignments)
14
Timing Controls For Simulation
Can put delays in a Verilog design
Gates, wires, even behavioral statements!
SIMULATION
Used to approximate real operation while simulating
Used to control testbench
SYNTHESIS
Synthesis tool IGNORES these timing controls
Cannot tell a gate to wait 1.5 nanoseconds!
Delay is a result of physical properties!
The only timing that can be (easily) controlled is on a
clock-cycle basis
Can tell synthesizer to attempt to meet cycle-time restriction
15
No Delay vs. Unit Delay
When no timing controls specified: no delay
Unrealistic even electrons take time to move
OUT is updated at same time A and/or B change:
and(OUT, A, B)
Unit delay often used
Not accurate either, but closer
Depth of circuit does affect speed!
Easier to see how changes propagate through circuit
OUT is updated 1 unit after A and/or B change:
and #1 A0(OUT, A, B);
17
Types Of Delays
Inertial Delay (Gates)
Suppresses pulses shorter than delay amount
In reality, gates need to have inputs held a certain time
before output is accurate
This models that behavior
Transport Delay (Nets)
Time of flight from source to sink
Short pulses transmitted
18
Delay Examples
wire #5 net_1; // 5 unit transport delay
19
Delays In Testbenches
Most common use in class
20
Overview
Module Port List: Revisited
Simulation and Testbenches
Simulation Overview
Timing Controls and Delay
Testbenches
Introduction to RTL Verilog
(Continuous Assignments)
21
Testbench Basics
Instantiate the unit being tested (UUT)
Provide input to that unit
Usually a number of different input combinations!
Watch the results (outputs of UUT)
Can watch ModelSim Wave window
Can print out information to the screen or to a file
22
Print Test Information
A number of system calls to output info
$monitor
Give a list of nets and variables to monitor
Output the given values every time a one of them changes
$display, $strobe
Output a value at a specific time during simulation
Can use formatting strings with these commands
Also have system calls that write to files
24
Output Example
`timescale 1ns /100ps // time_unit/time_precision
module t_adder4b;
reg[8:0] stim; // inputs to UUT are regs
wire[3:0] S; // outputs of UUT are wires
wire C4;
All values will run
// instantiate UUT together; easier to read
adder4b(S, C4, stim[8:5], stim[4:1], stim[0]); with formatting string
// monitor statement
initial $monitor($time, C4, S, stim[8:5], stim[4:1], stim[0]);
// stimulus generation
initial begin
stim = 9'b000000000; // at 0 ns
#10 stim = 9'b111100001; // at 10 ns
#10 stim = 9'b000011111; // at 20 ns
#10 stim = 9'b111100010; // at 30 ns
#10 stim = 9'b000111110; // at 40 ns
#10 $stop; // at 50 ns stops simulation
end
endmodule
25
Output Example
`timescale 1ns /100ps // time_unit/time_precision
module t_adder4b;
reg[8:0] stim; // inputs to UUT are regs
wire[3:0] S; // outputs of UUT are wires
wire C4;
Formatted with spaces
// instantiate UUT between values, vectors
adder4b(S, C4, stim[8:5], stim[4:1], stim[0]); shown as decimal
// monitor statement
initial $monitor(%t %b %d %d %d %b,
$time, C4, S, stim[8:5], stim[4:1], stim[0]);
// stimulus generation
initial begin
stim = 9'b000000000; // at 0 ns
#10 stim = 9'b111100001; // at 10 ns
#10 stim = 9'b000011111; // at 20 ns
#10 stim = 9'b111100010; // at 30 ns
#10 stim = 9'b000111110; // at 40 ns
#10 $stop; // at 50 ns stops simulation
end
endmodule
26
Exhaustive Testing
For combinational designs with few inputs
Test ALL combinations of inputs to verify output
Could enumerate all test vectors, but dont
Generate them using a for loop!
reg [4:0] x;
initial begin
for (x = 0; x < 16; x = x + 1)
#5 // need a delay here!
end
Need to use reg type for x. Why?
Its a variable assigned a value in a behavioral block
What would happen without the delay?
27
Why Loop Vector Has Extra Bit
Want to test all vectors 0000 to 1111
reg [3:0] x;
initial begin
for (x = 0; x < 16; x = x + 1)
#5 // need a delay here!
end
If x is 4 bits, it only gets up to 1111 => 15
1100 => 1101 => 1110 => 1111 => 0000 => 0001
x is never >= 16 so loop goes forever!
28
Exhaustive Example: UUT
29
Exhaustive Example: Testbench
module t_Comp_4_str();
wire A_gt_B, A_lt_B, A_eq_B;
reg [4:0] A, B; // sized to prevent loop wrap around
wire [3:0] A_bus, B_bus;
assign A_stim = A[3:0]; // display only 4 bit values
assign B_stim = B[3:0];
initial begin
#5
for (A = 0; A < 16; A = A + 1) begin // exhaustive test of valid inputs
for (B = 0; B < 16; B = B + 1) begin #5; // may want to test xs and zs
end // first for note multiple
end // second for initial blocks
end // initial
endmodule
30
Generating Clocks
Hard way:
initial begin
#5 clk = 0;
#5 clk = 1;
#5 clk = 0;
(repeat hundreds of times)
end
Better way:
initial begin
clk = 0;
forever #5 clk = ~clk;
end
31
FSM Testing
Response to input vector depends on state
For each state:
Check all transitions
For Moore, check output at each state
For Mealy, check output for each transition
This includes any transitions back to same state!
32
Example : Gray Code Counter
Write a testbench to test a 3-bit gray code counter.
33
Solution : Gray Code Counter Test1
module t1_gray_counter();
wire [2:0] out;
reg clk, rst;
gray_counter GC(out, clk, rst); // UUT
initial begin
clk = 0; forever #5 clk = ~clk; // What is the clock period?
end
initial begin
rst = 1; #10 rst = 0; // When does rst change relative to clock?
end // initial
endmodule
35
Force/Release In Testbenches
Allows you to override value FOR SIMULATION
Does NOT apply to hardware
Cant tell the synthesis tools when you get here, make
this value become 3d5
Synthesizer wont allow forcerelease
How does this help testing?
Pinpoint bug by controlling other signals
Can use with FSMs to override state
Force to a state
Test all edges/outputs for that state
Force the next state to be tested, and repeat
assign y = a & b;
assign z = y | c;
initial begin t a b c y z
a = 0; b = 0; c = 0; 0 0 0 0 0 0
#5 a = 0; b = 1; c = 0; 5 0 1 0 0 0
#5 force y = 1; 10 0 1 0 1 1
#5 b = 0; 15 0 0 0 1 1
#5 release y; 20 0 0 0 0 0
#5 $stop;
end
37
Example : Gray Code Counter Test2
Write a testbench to exhaustively test a 3-bit gray
code counter.
38
Example : Gray Code Counter Test2
module t2_gray_counter();
wire [2:0] out;
reg clk, rst;
gray_counter GC(out, clk, rst); // UUT
# 0 out: xxx rst: 1 # 100 out: 001 rst: 1 # 215 out: 000 rst: 1
# 5 out: 000 rst: 1 # 105 out: 000 rst: 1 # 225 out: 101 rst: 1
# 10 out: 000 rst: 0 // at #115 out = 000 # 235 out: 000 rst: 1
# 15 out: 001 rst: 0 # 125 out: 001 rst: 1 # 245 out: 100 rst: 1
# 25 out: 011 rst: 0 # 135 out: 000 rst: 1 # 255 out: 000 rst: 1
# 35 out: 010 rst: 0 # 145 out: 011 rst: 1
# 45 out: 110 rst: 0 # 155 out: 000 rst: 1
# 55 out: 111 rst: 0 # 165 out: 010 rst: 1
# 65 out: 101 rst: 0 # 175 out: 000 rst: 1
# 75 out: 100 rst: 0 # 185 out: 110 rst: 1
# 85 out: 000 rst: 0 # 195 out: 000 rst: 1
# 95 out: 001 rst: 0 # 205 out: 111 rst: 1
40
Review Questions
What are the two ways to declare module ports?
What is the difference between transport and
inertial delay? When is each used?
What does it mean to exhaustively test a design?
Why are force/release statements useful?
How do you generate a clock with a period of 16
time units?
41
Overview
Module Port List: Revisited
Simulation and Testbenches
Simulation Overview
Timing Controls and Delay
Testbenches
Introduction to RTL Verilog
(Continuous Assignments)
42
RTL Verilog
Higher-level of description than structural
Dont always need to specify each individual gate
Can take advantage of operators
input A, B, CI ; CO
output S, CO ;
endmodule
45
Continuous Assignment LHS
Can assign values to:
Scalar nets
Vector nets
Single bits of vector nets
Part-selects of vector nets
Concatenation of any of the above
Examples:
assign out[7:4] = a[3:0] | b[7:4];
assign val[3] = c & d;
assign {a, b} = stimulus[15:0];
46
Continuous Assignment RHS
Operators:
Arithmetic, Logical, Relational, Equality, Bitwise,
Reduction, Shift, Concatenation, Replication, Conditional
Same set as used in Behavioral Verilog
Can also be a pass-through!
assign a = stimulus[16:9];
assign b = stimulus[8:1];
assign cin = stimulus[0];
Note: aliasing is only in one direction
Cannot give a a new value elsewhere to set
stimulus[16:9]!
47
Example: adder4b
4
a[3:0] 4
4 sum[3:0]
b[3:0] adder4b
c_out
c_in
48
Can Often Replace Primitive Arrays
Module/Instance Array:
module array_of_xor (output [3:0] y, input [3:0] a, b);
xor Xarray [3:0] (y, a, b); // instantiates 4 xor gates
endmodule
Continuous Assignment
module xor_4bit (output [3:0] y, input [3:0] a, b);
assign y = a ^ b; // instantiates 4 xor gates
endmodule
51
Solution: Unsigned MAC Unit
Alternative method:
52
Operators
Shift (<<, >>)
Relational (<, >, <=, >=)
Equality (==, !=, ===, !==)
===, !== test xs, zs! ONLY USE FOR SIMULATION!
Logical Operators (&&, ||, !)
Build clause for if statement or conditional expression
Returns single bit values
Bitwise Operators (&, |, ^, ~)
Applies bit-by-bit!
Watch ~ vs !, | vs. ||, and & vs. &&
53
Example: Comparator
Complete the following module using three continuous assignment
statements.
endmodule
54
Operators
Reduction (&, |, ^)
Unary!
&(4b0111), |(3b010), ^(12h502)
Parentheses ( () )
Use to make calculations clear!
Concatenation ( {} )
Assembles vectors
Replication ( {{}} )
// a is a 4-bit vector with the value of b
// as the value of each bit
wire [3:0] a = {4{b}};
// same as a = {b,b,b,b}
55
Operators: Conditional
Can do an if else assignment in continuous assign
<clause> ? <T exp> : <F exp>
Examples:
assign mux_out = sel ? in1 : in0;
assign and2 = a ? b : 0;
assign xor2 = in1 ? ~in2 : in2;
assign triVal = sel ? in : 1bz;
Can nest the conditionals!
assign trimux = trisel ? (muxsel ? a : b) : 1bz;
56
Review Questions
Create 6 parallel AND gates with a delay of 4 time
units, inputs X[5:0] and Y[7:2], and outputs Z[1:6]
using instance arrays. Call the array AND_array
using a single continuous assignment statement
57