题解 | #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
);
//可以用简单但是繁杂的三段式状态机写,也可以找找规律发现每个状态都是上一个状态右移
//所以可以设置初始状态为11110000的后四位
// 开始是这样想的,但是后来发现这样复位后会造成一拍的延迟,所以还是看了下别人的答案,妙啊
// reg [7:0] Q_tmp;
// always @(posedge clk or negedge rst_n)begin
// if(~rst_n) begin
// Q_tmp <= 8'b11110000;
// end
// else begin
// Q_tmp <= Q_tmp>>1;
// end
// end
// always @(posedge clk or negedge rst_n)begin
// if(~rst_n) begin
// Q <= 4'b0000;
// end
// else begin
// Q <= Q_tmp[3:0];
// end
// end
always @(posedge clk or negedge rst_n)begin
if(~rst_n) begin
Q <= 4'b0;
end
else begin
Q<= {~Q[0],Q[3:1]};
end
end
endmodule
