Assignment 3 - HDL for Counter Design
Assignment 3 - HDL for Counter Design
//-----------------------------------------------------------------------------
// Module: mod5upcounter
//-----------------------------------------------------------------------------
module mod5upcounter (
input wire clk,
input wire rst,
input wire z,
output wire b0,
output wire b1,
output wire b2
);
assign b0 = a0;
assign b1 = a1;
assign b2 = a2;
jk_ff ff1 (
.clk(clk),
.rst(rst),
.j(j1),
.k(k1),
.q(a1)
);
jk_ff ff2 (
.clk(clk),
.rst(rst),
.j(j2),
.k(k2),
.q(a2)
);
endmodule
//-----------------------------------------------------------------------------
// Module: jk_ff
//-----------------------------------------------------------------------------
module jk_ff (
input wire clk, // rising-edge clock
input wire rst, // async reset, active-low
input wire j, // J input
input wire k, // K input
output reg q // output
);
endmodule
RTL Schematic
Testbench code
`timescale 1ns/1ps
module tb_mod5upcounter;
// Testbench signals
reg clk;
reg rst;
reg w;
wire b0, b1, b2;
initial begin
// Initialize
rst = 0;
w = 0;
#10 rst = 1;
#10 w = 1;
#100;
w = 0;
#30;
w = 1;
#60;
// Finish simulation
$finish;
end
endmodule
Testbench waveform
Discussion
The Verilog source code implements a 3-bit MOD-5 up-counter by instantiating
three JK flip-flop modules connected with combinational logic that controls each J/K
input. In this design, the least significant bit toggles on each rising edge of the clock
when the enable is stated and the counter fails to reach its terminal state; the middle bit
toggles solely when the LSB is high, and the most significant bit toggles exclusively
during the transition from 011 to 100, thereby ensuring the correct five-state sequence
of 000→001→010→011→100→000. The reset is managed asynchronously, therefore
asserting the active-low reset instantaneously sets all outputs to 000, independent of
the clock, while releasing the reset restores the counter to its starting position.
The simulation utilizing the supplied testbench—with a 100 MHz clock, regulated
reset pulses, and enable gating—confirms functional accuracy and timing integrity.
Upon reset, all outputs instantaneously revert to 000 and remain in that state until the
reset is triggered. When the enable signal is high, the counter progresses smoothly
through its five stages with each clock edge; when the enable signal is low, the outputs
maintain their last value, illustrating effective gating of all J/K inputs. Resuming enable
assertion maintains the sequence without disruptions or stability, verifying that the
design satisfies the 10 ns clock-period requirement and operates reliably under all
tested situations.
Conclusion
The MOD-5 up-counter design effectively fulfills the assignment requirements.
The Verilog code uses particular, modular instantiation of JK flip-flops with explicitly
stated combinational logic for each J/K input, guaranteeing a dependable 5-state
sequence. The RTL diagram validates this configuration, demonstrating clear signal
routing and accurate logic gating. The testbench waveform ultimately confirms
functional accuracy under both reset and gated-enable settings, exhibiting reliable
performance at 100 MHz. This experiment demonstrates the efficacy of JK flip-flops in
small-scale counters and emphasizes optimal techniques in modular design, schematic
verification, and waveform validation.