题解 | #序列发生器#
序列发生器
https://www.nowcoder.com/practice/1fe78a981bd640edb35b91d467341061
为了让思路比较清晰的体现,我用了三段式状态机来实现该需求。需要注意到是在rst_n之后的一小段里面,S0状态持续时间大概率不会为一个时钟周期,因此我们需要考虑复位的时候复位到空闲状态"IDLE",然后再进入序列的循环。
`timescale 1ns/1ns module sequence_generator( input clk, input rst_n, output reg data ); parameter S0=3'd0; parameter S1=3'd1; parameter S2=3'd2; parameter S3=3'd3; parameter S4=3'd4; parameter S5=3'd5; parameter IDLE=3'd6; reg[2:0]s,n_s; initial data = 0; always@(posedge clk, negedge rst_n)begin if(!rst_n) s <= IDLE; else s <= n_s; end always@(*)begin case(s) IDLE:n_s= S0; S0: n_s = S1; S1: n_s = S2; S2: n_s = S3; S3: n_s = S4; S4: n_s = S5; S5: n_s = S0; endcase end always@(*)begin case(s) S0: data = 'd0; S1: data = 'd0; S2: data = 'd1; S3: data = 'd0; S4: data = 'd1; S5: data = 'd1; endcase end endmodule