题解 | Johnson Counter
Johnson Counter
https://www.nowcoder.com/practice/7ee6e9ed687c40c3981d7586a65bc22d
`timescale 1ns/1ns
module JC_counter(
input clk ,
input rst_n,
output reg [3:0] Q
);
// 法1
/*
reg [2:0] cnt;
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
cnt <= 'd0;
end
else begin
cnt <= cnt + 1;
end
end
always@(*)begin
case(cnt)
3'b000: Q = 4'b0000;
3'b001: Q = 4'b1000;
3'b010: Q = 4'b1100;
3'b011: Q = 4'b1110;
3'b100: Q = 4'b1111;
3'b101: Q = 4'b0111;
3'b110: Q = 4'b0011;
3'b111: Q = 4'b0001;
endcase
end
*/
// 法2
always@(posedge clk or negedge rst_n)begin
if(!rst_n)begin
Q <= 4'b0000;
end
else begin
Q <= {~Q[0],Q[3:1]};
end
end
endmodule