HDLBits之Fsm ps2data

Now that you have a state machine that will identify three-byte messages in a PS/2 byte stream, add a datapath that will also output the 24-bit (3 byte) message whenever a packet is received (out_bytes[23:16] is the first byte, out_bytes[15:8] is the second byte, etc.).

out_bytes needs to be valid whenever the done signal is asserted. You may output anything at other times (i.e., don't-care).

错误代码:

module top_module(
    input clk,
    input [7:0] in,
    input reset,    // Synchronous reset
    output [23:0] out_bytes,
    output done); //
    reg [2:0]state,next_state;
    reg [23:0]out_temp;
    parameter S0=3'd0,S1=3'd1,S2=3'd2,S3=3'd3,S4=3'd4;
    
    always@(posedge clk or posedge reset)begin
        if(reset)
           state <= S0;
        else
            state<=next_state;
    end
    always@(*)begin
        case(state)
            S0:next_state=in[3]?S1:S0;
            S1:next_state=S2;
            S2:next_state=S3;
            S3:next_state=in[3]?S1:S0;
        endcase
    end
    always@(*)begin
        case(state)
            S0:out_temp[23:16]=in;
            S1:out_temp[15:8]=in; 此处赋值不合理
            S2:out_temp[7:0]=in;
            S3:out_bytes=out_temp;
        endcase
    end

    // FSM from fsm_ps2

    // New: Datapath to store incoming bytes.

endmodule

out_bytes的输出值不合理。

此处分析是对out的赋值与时钟的关系不合理。

代码:

module top_module(
    input clk,
    input [7:0] in,
    input reset,    // Synchronous reset
    output [23:0] out_bytes,
    output done); //
    reg [2:0]state,next_state;
    reg [23:0]out_temp;
    parameter S0=3'd0,S1=3'd1,S2=3'd2,S3=3'd3,S4=3'd4;
    
    always@(posedge clk or posedge reset)begin
        if(reset)begin
           state <= S0;
            out_temp<=23'd0;
        end
        else begin
            state<=next_state;
            out_temp<={out_temp[15:0],in};
        end
    end
    always@(*)begin
        case(state)
            S0:next_state=in[3]?S1:S0;
            S1:next_state=S2;
            S2:next_state=S3;
            S3:next_state=in[3]?S1:S0;
        endcase
    end
    assign done = (state == S3);
    assign out_bytes = out_temp;
 

    // FSM from fsm_ps2

    // New: Datapath to store incoming bytes.

endmodule
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值