题解 | #根据状态转移图实现时序电路#
根据状态转移图实现时序电路
https://www.nowcoder.com/practice/e405fe8975e844c3ab843d72f168f9f4
`timescale 1ns/1ns module seq_circuit( input C , input clk , input rst_n, output wire Y ); parameter s1 = 4'b0001; parameter s2 = 4'b0010; parameter s3 = 4'b0100; parameter s4 = 4'b1000; reg [3:0]statue; reg y_reg; always@(posedge clk or negedge rst_n) if (rst_n == 1'b0) statue <= s1; else case(statue) s1 : if (C == 1'b1) statue <= s2; else if(C == 1'b0) statue <= s1; s2 : if(C == 1'b1) statue <= s2; else if(C == 1'b0) statue <= s3; s3 : if (C == 1'b1) statue <= s4; else if(C == 1'b0) statue <= s3; s4 : if (C == 1'b1) statue <= s4; else if(C == 1'b0) statue <= s1; default : statue <= s1; endcase always@(*) if (rst_n == 1'b0) y_reg <= 1'b0; else if (statue == s3) y_reg <= 1'b1; else if (statue == s4 && C == 1'b1) y_reg <= 1'b1; else y_reg <= 1'b0; assign Y = y_reg; endmodule
这里采用二段式状态机进行解题,第一段为状态赋值块,第二段是输出赋值块。
(个人认为二段式比三段式好理解一点)