verilog刷题笔记006

该博客介绍了如何使用Verilog语言设计一个有限状态机(FSM)来实现串行数据的奇偶校验功能。代码中包含了状态机的状态转换逻辑和数据路径,并且在接收到数据时更新校验位。同时,还添加了对非法状态的处理和同步复位功能。
摘要由CSDN通过智能技术生成

Fsm serialdp的一种解法
预置代码:

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

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

endmodule

我的Verilog描述

// Use FSM from Fsm_serial
	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
    reg [3:0] state;
    reg [3:0] next_state;
    reg odd;
    wire p_reset = reset | (~(state>=1&&state<=8));//不是接收数据或者同步复位奇校验都应该置零
    parameter WAIT = 4'd0, CONFUSED = 4'd11, C_WAIT = 4'd12;
    always@(*)begin
        if(state == WAIT)begin
            if(in == 0) next_state =  1;
            else next_state = C_WAIT;
        end
        else if(state == C_WAIT)begin
            if(in == 0) next_state = 1;
            else next_state = C_WAIT;
        end
        else if(state >=1 && state <=8)begin
            next_state = state +1;
        end
        else if(state == 9)begin
            if(in == ~odd) next_state = state + 1;//因为状态8的时候仍然可以再使odd取反一次
            else next_state = CONFUSED;
        end
        else if(state == 10)begin
            if(in == 1) next_state = WAIT;
            else next_state = CONFUSED;
        end
        else if (state == CONFUSED)begin
            if(in == 1) next_state = C_WAIT;
            else next_state = CONFUSED;
        end
    end
    
    
    always@(posedge clk)begin
        if(reset) state <= C_WAIT;
        else state <= next_state;
        if(state >=1 && state <=8)
            out_byte[state-1] = in;
    end
    
    assign done = (state == WAIT);
    // New: Add parity checking.
    parity parity1(clk,p_reset,in,odd);

endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值