当前位置:   article > 正文

Verilog刷题笔记54

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.
在这里插入图片描述

官方提供的例化程序:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

结果正确:
在这里插入图片描述

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/1016474
推荐阅读
  

闽ICP备14008679号