Verilog Lab File
Verilog Lab File
LAB_10
Aim: To perform following tasks in Verilog HDL.
1. (i) Design 4-bit loadable up/down binary counter using
(a) if-else statements
(b) case conditional module
(c) casez conditional module
(ii) Write a test-bench to check functionality of the module in 1(i).
Verilog HDL:
1. If-else statements:
Test-Bench:
module tb_counter;
// Inputs
reg [3:0] loaddata;
reg reset;
reg clk;
reg load;
reg updown;
// Outputs
wire [3:0] count;
// Instantiate the Unit Under Test (UUT)
counter uut (
.loaddata(loaddata),
.reset(reset),
.clk(clk),
.load(load),
.updown(updown),
.count(count)
);
initial begin
// Initialize Inputs
loaddata = 0;
reset = 0;
clk = 0;
load = 0;
updown = 0;
end
always #1 clk=~clk;
initial begin
// Wait 100 ns for global reset to finish
#100;
loaddata=4'b0110;
reset=1'b1;
load=1'b0;
updown=1'b1;
#100;
loaddata=4'b0110;
reset=1'b0;
load=1'b1;
updown=1'b1;
#100;
loaddata=4'b0110;
reset=1'b0;
load=1'b0;
updown=1'b0;
#100;
loaddata=4'b0110;
reset=1'b0;
load=1'b0;
updown=1'b1;
#100;
loaddata=4'b0111;
reset=1'b0;
load=1'b1;
updown=1'b0;
end
endmodule
Simulation Results:
2. (i) Design a 4–bit asynchronous ripple carry counter in Verilog HDL as shown
in Fig. 1.
The T flip-flops in Fig. 1 are to be instantiated and are designed using D flip-flop
as shown in Fig. 2.
(ii) Write test bench to validate the functionality of ripple carry counter
designed in 2(i).
Verilog HDL Code:
TestBench:
module vtf2;
// Inputs
reg tin;
reg reset;
reg clock;
// Outputs
wire [3:0] q;
// Instantiate the Unit Under Test (UUT)
rcc2 uut (
.q(q),
.tin(tin),
.reset(reset),
.clock(clock)
);
always
begin
#5 clock=~clock;
end
initial begin
// Initialize Inputs
#7 tin = 1;
clock = 0;
reset = 1;
#5 reset=0;
// Wait 100 ns for global reset to finish
// Add stimulus here
end
endmodule
Simulation Results:
3. (i) Design a 4–bit synchronous ripple carry counter in Verilog HDL as shown
in Fig. 3.
module rcconlinetf;
// Inputs
reg tin;
reg reset;
reg clock;
// Outputs
wire [3:0] q;
wire out;
// Instantiate the Unit Under Test (UUT)
rcconline uut (
.q(q),
.tin(tin),
.out(out),
.reset(reset),
.clock(clock)
);
always
begin
#5 clock=~clock;
end
initial begin
// Initialize Inputs
#7 tin = 1;
clock = 0;
#8 reset = 1;
#5 reset=0;
// Wait 100 ns for global reset to finish
// Add stimulus here
end
endmodule
Simulation Results:
CONCLUSION:
The equivalence of the counter output waveforms imply similar operation
although, asynchronous design employed here, is supposedly slower than the
synchronous design implemented here.
Also, casex and casez cover more boundary cases than case, thus, increasing
the ease of creating the cases in more concise manner.