Week5-Slides Watermark
Week5-Slides Watermark
1
02/09/17
• Basic requirements:
– The inputs of the DUT need to be connected to the test bench.
– The outputs of the DUT needs also to be connected to the test bench.
• Points to note:
– Test benches use the “ini,al” procedural block that executes only once.
– Can also use “always” for generaFng some test inputs, like a clock signal.
TEST BENCH
Design Under
SFmulus Test Monitor
(DUT)
2
02/09/17
3
02/09/17
4
02/09/17
• $finish;
– Terminates the simulaFon process.
• $dumpfile (<filename>);
– Specifies the file that will be used for storing the values of the selected variables so
that they can be graphically visualized later.
– The file typically has an extension .vcd (Value Change Dump), and contains
informaFon about any value changes on the selected variables.
• $dumpoff;
– This direcFve stops the dumping of variables. All variables are dumped with “x”
values and the next change of variables will not be dumped.
• $dumpon;
– This direcFve starts previously stopped dumping of variables.
5
02/09/17
6
02/09/17
END OF LECTURE 21
7
02/09/17
8
02/09/17
module testbench;
reg a, b, c; wire sum, cout;
full_adder FA (sum, cout, a, b, c);
initial
begin
$monitor ($time,” a=%b, b=%b, c=%b, sum=%b, cout=%b”,
a, b, c, sum, cout);
#5 a=0; b=0; c=1;
#5 b=1; 0 a=x, b=x, c=x, sum=x, cout=x
#5 a=1; 5 a=0, b=0, c=1, sum=1, cout=0
#5 a=0; b=0; c=0; 10 a=0, b=1, c=1, sum=0, cout=1
#5 $finish; 15 a=1, b=1, c=1, sum=1, cout=1
end 20 a=0, b=0, c=0, sum=0, cout=0
endmodule
9
by writing {a,b,c} , a b c have been concatenated ie: they act as a single 3 bit unit
eg: 000, 001, 111 etc 02/09/17
module testbench;
reg a, b, c; wire sum, cout; T= 5, a=0, b=0, c=0, sum=0, cout=0
integer i; T=10, a=0, b=0, c=1, sum=1, cout=0
full_adder FA (sum, cout, a, b, c); T=15, a=0, b=1, c=0, sum=1, cout=0
T=20, a=0, b=1, c=1, sum=0, cout=1
initial T=25, a=1, b=0, c=0, sum=1, cout=0
begin
$dumpfile (“fulladder.vcd”);
T=30, a=1, b=0, c=1, sum=0, cout=1
$dumpvars (0, testbench); T=35, a=1, b=1, c=0, sum=0, cout=1
for (i=0; i<8; i=i+1) T=40, a=1, b=1, c=1, sum=1, cout=1
begin
{a,b,c} = i; #5;
$display ("T=%2d, a=%b, b=%b, c=%b, sum=%b, cout=%b",
$time, a, b, c, sum, cout);
end
#5 $finish;
end
endmodule
10
02/09/17
11
02/09/17
module shift_test;
reg clk, clr, in; wire out; integer i;
shiftreg_4bit SR (clk, clr, in, out);
initial
begin clk = 1'b0; #2 clr = 0; #5 clr = 1; end
initial
begin
$dumpfile ("shifter.vcd");
$dumpvars (0, shift_test);
#200 $finish;
end
endmodule
12
02/09/17
module test_counter;
reg clk, clr;
wire [7:0] out;
13
02/09/17
14
02/09/17
endmodule
15
02/09/17
module test_adder;
reg [7:0] a, b; T: 10, a: 00, b: 52, sum: 52
wire [7:0] sum; wire cout; T: 20, a: ca, b: 08, sum: d2
integer myseed; T: 30, a: 0c, b: 6a, sum: 76
adder ADD (sum, cout, a, b); T: 40, a: b1, b: 71, sum: 22
initial myseed = 15; T: 50, a: 23, b: df, sum: 02
initial
begin
repeat (5)
begin
a = $random(myseed);
b = $random(myseed); #10;
$display ("T: %3d, a: %h, b: %h, sum: %h", $time, a, b, sum);
end
end
endmodule
16
02/09/17
END OF LECTURE 22
17
02/09/17
IntroducAon
• CombinaFonal and SequenFal Circuits
– In a combinaFonal circuit, the outputs depend only on the applied input
values and not on the past history.
– In a sequenFal circuit, the outputs depend not only on the applied input
values but also on the internal state.
• The internal states also change with Fme.
• The number of states is finite, and hence a sequenFal circuit is also referred to
as a Finite State Machine (FSM).
• Most of the pracFcal circuits are sequenFal in nature.
18
02/09/17
19
02/09/17
Pictorial DepicAon
PI NS NS Output Mealy
F/F PS PO
Logic Logic Machine
PI NS NS PS Output
PO Moore
F/F
Logic Logic Machine
Example 1
• There are three lamps, RED, GREEN and YELLOW, that should glow cyclically
with a fixed Fme interval (say, 1 second).
• Some observaFons:
– The FSM will have three states, corresponding to the glowing state of the lamps.
– The input set is null; state transiFon will occur whenever clock signal comes.
– This is a Moore Machine, since the lamp that will glow only depends on the state
and not on the inputs (here null).
RED
RED
clock GREEN
GREEN YELLOW
YELLOW
20
02/09/17
module test_cyclic_lamp;
reg clk; 0 RGY: xxx
wire [0:2] light; 5 RGY: 100
cyclic_lamp LAMP (clk, light); 15 RGY: 010
25 RGY: 001
always #5 clk = ~clk;
35 RGY: 100
initial 45 RGY: 010
begin 55 RGY: 001
clk = 1'b0; 65 RGY: 100
#100 $finish; 75 RGY: 010
end
85 RGY: 001
initial 95 RGY: 100
begin
$dumpfile ("cyclic.vcd"); $dumpvars (0, test_cyclic_lamp);
$monitor ($time, " RGY: %b", light);
end
endmodule
21
02/09/17
22
02/09/17
END OF LECTURE 23
23
02/09/17
Example 2
• Design of a serial parity detector.
– A conFnuous stream of bits is fed to a circuit in synchronism with a clock. The
circuit will be generaFng a bit stream as output, where a 0 will indicate “even
number of 1’s seen so far” and a 1 will indicate “odd number of 1’s seen so far”.
– Also a Moore Machine.
0/0 0/1
1/1
x
z EVEN ODD
clk
IniFal 1/0
state
24
02/09/17
module test_parity;
reg clk, x; wire z;
parity_gen PAR (x, clk, z);
initial
begin
$dumpfile ("parity.vcd"); $dumpvars (0, test_parity);
clk = 1'b0;
end
always #5 clk = ~clk;
initial
begin
#2 x = 0; #10 x = 1; #10 x = 1; #10 x = 1;
#10 x = 0; #10 x = 1; #10 x = 1; #10 x = 0;
#10 x = 0; #10 x = 1; #10 x = 1; #10 x = 0;
#10 $finish;
end
endmodule
Hardware Modeling Using Verilog 50
25
02/09/17
26
02/09/17
Example 3
• Design of a sequence detector.
– A circuit accepts a serial bit stream “x” as input and produces a serial bit stream “z”
as output.
– Whenever the bit padern “0110” appears in the input stream, it outputs z = 1; at all
other Fmes, z = 0.
– Overlapping occurrences of the padern are also detected.
– This is a Mealy Machine.
– Example: x :- 0 1 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 0
z :- 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0
reset
1/0
1/0 0/0
0/0 1/0 1/0
S0 S1 S2 S3
0/0
reset 0/1
27
02/09/17
module test_sequence;
reg clk, x, reset; wire z;
seq_detector SEQ (x, clk, reset, z);
initial
begin
$dumpfile ("sequence.vcd"); $dumpvars (0, test_sequence);
clk = 1'b0; reset = 1'b1;
#15 reset = 1'b0;
end
always #5 clk = ~clk;
initial
begin
#12 x = 0; #10 x = 0; #10 x = 1; #10 x = 1;
#10 x = 0; #10 x = 1; #10 x = 1; #10 x = 0;
#10 x = 0; #10 x = 1; #10 x = 1; #10 x = 0;
#10 $finish;
end
endmodule
28
02/09/17
Example 4
• Design a sequence detector for the bit padern “101010”.
– Work out the state diagram in a similar way.
– Then code the state diagram in Verilog.
29
02/09/17
END OF LECTURE 24
30