题解 | #根据状态转移图实现时序电路#
根据状态转移图实现时序电路
https://www.nowcoder.com/practice/e405fe8975e844c3ab843d72f168f9f4
`timescale 1ns/1ns module seq_circuit( input C , input clk , input rst_n, output wire Y ); localparam s0 = 2'b00 ; localparam s1 = 2'b01 ; localparam s2 = 2'b10 ; localparam s3 = 2'b11 ; reg [1:0] current_state ; reg [1:0] next_state ; always@(posedge clk or negedge rst_n)begin if(!rst_n) current_state <= s0; else current_state <= next_state; end always@(*)begin case(current_state) s0:begin if(C == 1) next_state = s1; else next_state = s0; end s1:begin if(C == 1) next_state = s1; else next_state = s3; end s2:begin if(C == 1) next_state = s2; else next_state = s0; end s3:begin if(C == 1) next_state = s2; else next_state = s3; end default:next_state <= s0; endcase end reg y_temp ; always@(*)begin // if(!rst_n) // Y <= 1'b0; // else begin case(current_state) s0:begin if(C == 1) y_temp <= 1'b0; else y_temp <= 1'b0; end s1:begin if(C == 1) y_temp <= 1'b0; else y_temp <= 1'b0; end s2:begin if(C == 1) y_temp <= 1'b1; else y_temp <= 1'b0; end s3:begin if(C == 1) y_temp <= 1'b1; else y_temp <= 1'b1; end default:y_temp <= 1'b0; endcase end // end assign Y = y_temp; endmodule
y应该是随着状态的变化而变化,而不是随着时钟上升沿变化,这样会得出错误的驱动物理意义