Sequential Circuits in Verilog
Sequential Circuits in Verilog
Q1: D-Latch
Verilog Code
// D-Latch
if (En)
Q = D;
end
endmodule
Testbench
module tb_D_Latch;
reg D, En;
wire Q;
// Instantiate D_Latch
initial begin
$dumpfile("d_latch.vcd");
$dumpvars(0, tb_D_Latch);
D = 0; En = 0; #10;
D = 1; En = 0; #10;
D = 0; En = 1; #10;
D = 1; En = 1; #10;
$finish;
end
endmodule
Q2: SR-Latch
Verilog Code
// SR-Latch
always @ (S or R) begin
Q = 1;
Qbar = 0;
Q = 0;
Qbar = 1;
Q = 1'bx;
Qbar = 1'bx;
end
end
endmodule
Testbench
module tb_SR_Latch;
reg S, R;
wire Q, Qbar;
// Instantiate SR_Latch
$dumpfile("sr_latch.vcd");
$dumpvars(0, tb_SR_Latch);
S = 0; R = 0; #10;
S = 1; R = 0; #10;
S = 0; R = 1; #10;
S = 1; R = 1; #10;
S = 0; R = 0; #10;
$finish;
end
endmodule
Q3: JK-Latch
Verilog Code
// JK-Latch
if (En) begin
if (~J & K)
Q = 0;
Q = 1;
else if (J & K)
Q = ~Q;
end
end
endmodule
Testbench
module tb_JK_Latch;
reg J, K, En;
wire Q;
// Instantiate JK_Latch
initial begin
$dumpfile("jk_latch.vcd");
$dumpvars(0, tb_JK_Latch);
J = 0; K = 0; En = 1; #10;
J = 1; K = 0; En = 1; #10;
J = 0; K = 1; En = 1; #10;
J = 1; K = 1; En = 1; #10;
J = 1; K = 1; En = 0; #10;
$finish;
end
endmodule
Verilog Code
module D_FF_Async_Reset (input D, input clk, input reset_n, output reg Q);
if (!reset_n)
Q <= 0;
else
Q <= D;
end
endmodule
Testbench
module tb_D_FF_Async_Reset;
wire Q;
// Instantiate D_FF_Async_Reset
initial begin
$dumpfile("d_ff_async_reset.vcd");
$dumpvars(0, tb_D_FF_Async_Reset);
reset_n = 1; D = 1; #10;
D = 0; #10;
D = 1; #10;
reset_n = 0; #10;
reset_n = 1; D = 0; #10;
$finish;
end
endmodule
(More circuits and testbenches will be added for the remaining questions.)