Verilog刷题笔记54

题目:
Fsm serialdp
See also: Serial receiver and datapath

We want to add parity checking to the serial receiver. Parity checking adds one extra bit after each data byte. We will use odd parity, where the number of 1s in the 9 bits received must be odd. For example, 101001011 satisfies odd parity (there are 5 1s), but 001001011 does not.
Verilog刷题笔记54_第1张图片

官方提供的例化程序:

module parity (
    input clk,
    input reset,
    input in,
    output reg odd);

    always @(posedge clk)
        if (reset) odd <= 0;
        else if (in) odd <= ~odd;

endmodule

解题:
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output [7:0] out_byte,
    output done
); //
	parameter idle=0,data_receive=1,check=2,stop=3,error=4;
    reg [2:0]state,next_state;
    reg [7:0]data;
    wire odd_temp,start;
    reg [3:0]cnt;
    reg jo;
    
    parity parity_inst(
        .clk(clk),
        .reset(reset|start),
        .in(in),
        .odd(odd_temp)
    );
    
    always@(posedge clk)begin
        if(reset)
            state<=idle;
        else
            state<=next_state;
    end
    always@(*)begin
        start=0;
        case(state)
            idle:begin next_state=in?idle:data_receive;start=1;end
            data_receive:next_state=(cnt==8)?check:data_receive;
            check:next_state=in?stop:error;
            stop:begin next_state=in?idle:data_receive;start=1;end
            error:next_state=in?idle:error;
        endcase
    end
    always@(posedge clk)begin
        if(reset)
            cnt<=0;
        else 
            case(state)
                data_receive:cnt<=cnt+1;
                default:cnt<=0;
            endcase
    
    end
    always@(posedge clk)begin
        if(reset)
            data<=0;
        else 
            case(next_state)
                data_receive:data<={in,data[7:1]};
            endcase
    end
    always@(posedge clk)begin
        if(reset)
            jo<=0;
        else
            jo<=odd_temp;
    end
    assign out_byte=data;
    assign done=jo&(state==stop);
    
            
    // Modify FSM and datapath from Fsm_serialdata

    // New: Add parity checking.

endmodule

结果正确:
Verilog刷题笔记54_第2张图片

总结:
多了个奇偶校验
为了方便例化程序的重新启用,所以减去了一个start状态,直接将start定义成一个变量。

你可能感兴趣的:(笔记,fpga开发)