I’m attempting to create a 4bit Binary Counter with the structural model. X=0 will make the output count-up while X=1 will make it count down.
Counting up, I want it to only have a limit of 1001 (0000 … 1001) then it will reset and wrap back to 0000 and repeat the cycle. For Counting down, I want it to start at 1001, meaning when it reaches 0000 (1001 … 0000), it will reset back to 1001. I was able to do this without using the structural model (using increments), but I don’t know how to implement the wrap around with this model.
`module FF_T(Q, T, clk, rst);
output reg Q;
input T, clk, rst;
always @(posedge clk or negedge rst) begin
if (!rst)
Q <= 0; // Reset to 0 when rst==0
else if (T)
Q <= ~Q; // Toggle if T==1
end
endmodule
module counter_4bit(Q, x, clk, rst);
input clk;
input rst;
input x; // Control up/down count
output [3:0] Q;
wire [3:0] T; // Toggle signals for each flip-flop
// Instantiate four T flip-flops
FF_T ff0(Q[0], T[0], clk, rst);
FF_T ff1(Q[1], T[1], clk, rst);
FF_T ff2(Q[2], T[2], clk, rst);
FF_T ff3(Q[3], T[3], clk, rst);
// Define the toggle signals for each bit
assign T[0] = 1'b1; // Always toggle the least significant bit
// Toggle signals for other bits based on current state and direction
assign T[1] = (x == 1'b0 ? Q[0] : ~Q[0]);
assign T[2] = (x == 1'b0 ? (Q[1] & Q[0]) : (~Q[0] & ~Q[1]));
assign T[3] = (x == 1'b0 ? (Q[2] & Q[1] & Q[0]) : (~Q[0] & ~Q[1] & ~Q[2]));
endmodule`
lolwhat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.