This 3-bit Gray counter is giving me problems because it can’t count in both directions. When the reset is activated, it will only count in the direction of the dir input at the moment of the reset. Why? Can someone help me?
module gray_counter(
input clk,
input reset,
input dir,
output [2:0] out
);
wire [2:0] stato_attuale;
reg [2:0] eccitazione;
always @(posedge clk) begin
if (dir == 0) begin
eccitazione[0] <= ((~stato_attuale[2] & ~stato_attuale[1] & ~stato_attuale[0]) |
(~stato_attuale[2] & stato_attuale[1] & stato_attuale[0]) |
(stato_attuale[2] & stato_attuale[1] & ~stato_attuale[0]) |
(stato_attuale[2] & ~stato_attuale[1] & stato_attuale[0]));
eccitazione[1] <= ((~stato_attuale[2] & ~stato_attuale[1] & stato_attuale[0]) |
(stato_attuale[2] & stato_attuale[1] & stato_attuale[0]));
eccitazione[2] <= ((~stato_attuale[2] & stato_attuale[1] & ~stato_attuale[0]) |
(stato_attuale[2] & ~stato_attuale[1] & ~stato_attuale[0]));
end
else if (dir == 1) begin
eccitazione[0] <= ((stato_attuale[2] & ~stato_attuale[1] & ~stato_attuale[0]) |
(stato_attuale[2] & stato_attuale[1] & stato_attuale[0]) |
(~stato_attuale[2] & stato_attuale[1] & ~stato_attuale[0]) |
(~stato_attuale[2] & ~stato_attuale[1] & stato_attuale[0]));
eccitazione[1] <= ((stato_attuale[2] & ~stato_attuale[1] & stato_attuale[0]) |
(~stato_attuale[2] & stato_attuale[1] & stato_attuale[0]));
eccitazione[2] <= ((~stato_attuale[2] & ~stato_attuale[1] & ~stato_attuale[0]) |
(stato_attuale[2] & stato_attuale[1] & ~stato_attuale[0]));
end
end
FF ff0 (.X(eccitazione[0]), .C(eccitazione[0]), .R(reset), .Z(stato_attuale[0]));
FF ff1 (.X(eccitazione[1]), .C(eccitazione[1]), .R(reset), .Z(stato_attuale[1]));
FF ff2 (.X(eccitazione[2]), .C(eccitazione[2]), .R(reset), .Z(stato_attuale[2]));
assign out[0] = stato_attuale[0];
assign out[1] = stato_attuale[1];
assign out[2] = stato_attuale[2];
endmodule
module FF(
input X, C, R,
output Z
);
reg y1, y2;
always @(posedge C or posedge R) begin
if (R) begin
y1 <= 1'b0;
y2 <= 1'b0;
end else begin
y1 <= (~y2 & C & X) | (y1 & (~C | ~y2));
y2 <= y1 ^ C;
end
end
assign Z = y1;
endmodule
I’ve tried to solve it in every way. I want to keep the flip-flop inputs as they are; I don’t want to modify the circuit too much. Please help me asap
New contributor
Enrico Maset is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.