题解 | #根据状态转移图实现时序电路#

根据状态转移图实现时序电路

https://www.nowcoder.com/practice/e405fe8975e844c3ab843d72f168f9f4

使用两段式状态机最为简便

`timescale 1ns/1ns

module seq_circuit(
   input                C   ,
   input                clk ,
   input                rst_n,
 
   output   wire        Y   
);

parameter S0 = 2'b00,
          S1 = 2'b01,
          S2 = 2'b10,
          S3 = 2'b11;

reg [1:0] c_state , n_state;
reg Y_buff;

assign Y = Y_buff;

always@(posedge clk or negedge rst_n)begin
    if(!rst_n)
        c_state <= S0;
    else
        c_state <= n_state;
end

always@(*)begin
    n_state = c_state;
    case(c_state)
        S0:begin
            Y_buff = 1'b0;
            if(C)
                n_state = S1;
            else
                n_state = S0;
        end
        S1:begin
            Y_buff = 1'b0;
            if(C)
                n_state = S1;
            else
                n_state = S3;
        end
        S2:begin
            if(C)begin
                Y_buff = 1'b1;
                n_state = S2;
            end else begin
                Y_buff = 1'b0;
                n_state = S0;
            end
        end
        S3:begin
            Y_buff = 1'b1;
            if(C)
                n_state = S2;
            else
                n_state = S3;
        end
    endcase
end


endmodule

全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务