Verilog刷题HDLBits——Fsm serialdp

Verilog刷题HDLBits——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.

Change your FSM and datapath to perform odd parity checking. Assert the done signal only if a byte is correctly received and its parity check passes. Like the serial receiver FSM, this FSM needs to identify the start bit, wait for all 9 (data and parity) bits, then verify that the stop bit was correct. If the stop bit does not appear when expected, the FSM must wait until it finds a stop bit before attempting to receive the next byte.

You are provided with the following module that can be used to calculate the parity of the input stream (It’s a TFF with reset). The intended use is that it should be given the input bit stream, and reset at appropriate times so it counts the number of 1 bits in each byte.

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

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

endmodule

Note that the serial protocol sends the least significant bit first, and the parity bit after the 8 data bits.
在这里插入图片描述

代码

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

    // Modify FSM and datapath from Fsm_serialdata
    parameter idle=0,start=1,b1=2,b2=3,b3=4,b4=5,b5=6,b6=7,b7=8,b8=9,stop=10,error=11,bodd=12;
    reg[3:0] state,next_state;
    
    always@(*)
        case(state)
            idle:next_state=(~in)?start:idle;
            start:next_state=b1;
            b1:next_state=b2;
            b2:next_state=b3;
            b3:next_state=b4;
            b4:next_state=b5;
            b5:next_state=b6;
            b6:next_state=b7;
            b7:next_state=b8;
            b8:next_state=bodd;
            bodd:next_state=in?stop:error;
            stop:next_state=(~in)?start:idle;
            error:next_state=in?idle:error;
        endcase
    
    always@(posedge clk)
        if(reset)
            state<=idle;
    	else
            state<=next_state;

    reg[7:0] temp;
    always@(posedge clk)
        begin
            if(reset)
                temp <= 0;
            else
                case(state)
                    start:temp[0]<=in;
                    b1:temp[1]<=in;
                    b2:temp[2]<=in;
                    b3:temp[3]<=in;
                    b4:temp[4]<=in;
                    b5:temp[5]<=in;
                    b6:temp[6]<=in;
                    b7:temp[7]<=in;
                endcase
        end
    
    wire odd;
    parity check_odd(clk,(reset || next_state == idle || next_state == start),in,odd);
    
    assign done = (state==stop)&&(~odd); // 在stop状态多翻转了一次,所以要取反
    assign out_byte = done?temp:0;
    // New: Add parity checking.

endmodule

结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值