PS/2 packet parser

The PS/2 mouse protocol sends messages that are three bytes long. However, within a continuous byte stream, it's not obvious where messages start and end. The only indication is that the first byte of each three byte message always has bit[3]=1 (but bit[3] of the other two bytes may be 1 or 0 depending on data).

We want a finite state machine that will search for message boundaries when given an input byte stream. The algorithm we'll use is to discard bytes until we see one with bit[3]=1. We then assume that this is byte 1 of a message, and signal the receipt of a message once all 3 bytes have been received (done).

The FSM should signal done in the cycle immediately after the third byte of each message was successfully received.

PS/2 鼠标协议发送长度为 3 个字节的消息。但是,在连续的字节流中,消息的开始和结束位置并不明显。唯一的指示是,每三个字节消息的第一个字节始终具有 bit[3]=1(但其他两个字节的 bit[3] 可能是 1 或 0,具体取决于数据)。

我们想要一个有限状态机,当给定一个输入字节流时,它将搜索消息边界。我们将使用的算法是丢弃字节,直到我们看到一个 bit[3]=1 的字节。然后,我们假设这是消息的字节 1,并在收到所有 3 个字节(完成)后发出接收消息的信号。

FSM 应在成功接收到每条消息的第三个字节后立即在周期内发出信号。
题目网站
状态转换图如下:
a

module top_module(
    input clk,
    input [7:0] in,
    input reset,    // Synchronous reset
    output done); //

    parameter idle=4'b0001,
    		first=4'b0010,
    		second=4'b0100,
    		third=4'b1000;
    reg [3:0]state,nstate;
    
    always@(*)begin
        case(state)
            idle:begin
                if(in[3])begin
                    nstate=first;
                end
                else begin
                    nstate=idle;
                end
            end
            first:nstate=second;
            second:nstate=third;
            third:begin
                if(!in[3])begin
                    nstate=idle;
                end
                else begin
                    nstate=first;
                end
            end
        endcase
    end
    
    always@(posedge clk)begin
        if(reset)begin
            state<=idle;
        end
        else begin
            state<=nstate;
        end
    end
    
    assign done=(state==third);

endmodule

关键点:

  1. 这道题的状态机要能画出来,理解题目含义
  2. 这是一个数据流,是一个连续的,所以有一个循环存在,只要in[3]==1一直满足,就会一直循环下去,可以在计算机网络数据包的设计中使用
posted @ 2024-04-15 17:37  江左子固  阅读(31)  评论(0编辑  收藏  举报