Fsm serialdp(HDLBits 137)

文章描述了一个串口接收器的设计,增加了奇偶校验功能,用于检测数据的正确性。接收器采用状态机FSM来识别开始位、读取8位数据、接收奇偶校验位,并进行校验。当接收到的数据字节的奇偶校验正确且停止位正确时,才会触发done信号。提供的代码示例展示了如何实现这个功能,包括状态转换和数据处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题目:有奇偶校验位的串口接收器

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.

Some timing diagrams

No framing errors. Odd parity passes for first byte, fails for second byte.
在这里插入图片描述

思路与代码

题目本身没有什么难度,但需要注意一些细节问题,给出我的状态转换图如下:
在这里插入图片描述

S 0 S_0 S0:等待开始的状态。
S 1 S_1 S1:接受字节状态(读8位)
S 2 S_2 S2:接受额外位(读1位)
S 3 S_3 S3:检验成功后,进入该状态
S 4 S_4 S4:检验失败后,进入该状态
S 5 S_5 S5:检验到停止位,从 S 3 S_3 S3进入到 S 5 S_5 S5,并输出字节,拉高done

具体细节,见代码注释。

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

localparam[2:0]    //6个状态
s0 = 3'b000,
s1 = 3'b001,
s2 = 3'b010,
s3 = 3'b011,
s4 = 3'b100,
s5 = 3'b101;

reg[2:0] cs,nst;    // 当前状态,下一个状态
reg[3:0] Count;     // 计数,读了多少位
wire odd;
reg temp;           //temp配合odd使用,odd全程在更新,temp记录在开始进入读取字节时那一刻的odd
reg [7:0] val;      // 输出

always@(posedge clk) begin
    if(reset) cs<=s0;
    else cs<=nst;
end

always@(*) begin     
    // 状态转换,注意odd^temp^in为1表示检验成功,否则检验失败   
    // Count==7时,表示在读第八个字节的时候,状态从S1到S2
    case(cs) 
        s0: if(in==1'b1) nst = s0;
            else nst = s1;
        s1: if(Count==7) nst = s2;
            else nst = s1;
        s2: if((odd^temp)^in) nst = s3;
            else nst = s4;
        s3: if(in==1'b1) nst = s5;
            else nst = s4;
        s4: if(in==1'b0) nst = s4;
            else nst = s0;
        s5: if(in==1'b1) nst = s0;
            else nst = s1;
        default: nst = s0;
    endcase
end

always@(posedge clk) begin  
    // 更新 Count      
    if(reset) Count<=0;
    else begin
        if(cs==s1) begin
            Count <= Count+1'b1;
        end
        else begin
            Count <= 0;
        end

    end
end

always@(posedge clk) begin
    // 更新 val
    if(reset) val <= 0;
    else begin
        if(cs==s1) begin
            val[Count] <= in;
        end
    end
end

always@(posedge clk) begin
    // 更新 temp
    if(reset) temp <= 0;
    else if(cs==s0||cs==s5) temp <= odd;
end

always@(*) begin
    // 输出字节
    if(cs==s5) out_byte = val;
    else out_byte = 8'b00000000;
end

always@(*) begin
    // 拉高done
    if(cs==s5) done = 1'b1;
    else done = 1'b0;
end

parity yyh(clk,reset,in,odd);
endmodule
### HDLBits FSM 设计思路与解题方法 #### 1. 状态机基本概念 状态机分为两种主要类型:**Moore型**和**Mealy型**。其中,Moore型的状态机输出仅依赖于当前状态,而Mealy型的状态机输出不仅取决于当前状态还可能受到输入的影响[^1]。 对于HDLBits中的FSM题目,通常涉及以下几个核心部分的设计: - **状态定义**:明确系统的各个状态及其含义。 - **状态转换逻辑**:描述如何从一个状态转移到另一个状态。 - **输出逻辑**:确定每个状态下应产生的输出信号。 --- #### 2. 题目解析 根据引用内容可知,目标是设计一个基于给定条件的有限状态机(FSM),具体如下: ##### (a) 水流控制系统 该问题要求设计一个能够响应水位变化并控制水流的装置。此任务属于实际应用类问题,需通过传感器反馈的信息调整设备行为[^2]。 ##### (b) Moore FSM 实现 提供了一个简单的两态(Moore型)模型实例——具有“A”和“B”两个状态,并特别指出初始重置位置设为“B”。此外强调本练习区别在于采用同步复位方式而非异步版本[^3][^4]。 --- #### 3. 解决方案框架 以下是解决此类问题的一般策略和技术要点说明: ##### (i) 明确需求规格说明书(SRS) 仔细阅读题目描述以理解预期功能以及任何特殊约束(比如特定类型的重启机制). ##### (ii) 绘制状态转移图(Diagram) 利用图形化表示帮助直观展示不同条件下可能发生的变化路径. 下面是一个简化版的例子: ```plaintext State A --input=0--> State B <--input=1-- ``` 此处假设存在单一布尔变量作为外部刺激源; 当其值等于零时促使系统进入"B"; 反之保持原状或者返回"A". ##### (iii) 编写Verilog代码片段 下面给出一段针对上述情形定制化的初步草稿供参考学习: ```verilog module fsm_example ( input wire clk, input wire reset, // Synchronous Reset Signal input wire sensor_input,// Water Level Sensor Input output reg out_signal // Control Output to Actuator ); // Define States parameter STATE_A = 1'b0; parameter STATE_B = 1'b1; reg current_state, next_state; always @(posedge clk or posedge reset) begin if(reset) current_state <= STATE_B; // Initialize on Sync Reset else current_state <= next_state; end always @(*) begin : state_transition_logic case(current_state) STATE_A:begin if(sensor_input == 1'b0) next_state = STATE_B; else next_state = STATE_A; end STATE_B:begin if(sensor_input == 1'b1) next_state = STATE_A; else next_state = STATE_B; end default:next_state=current_state;//Safety Default Clause endcase end assign out_signal=(current_state==STATE_A)?1'b1:1'b0;//Output Logic Based On Current_State endmodule ``` 注意这段脚本仅为示范用途,在真实项目开发前还需进一步验证和完善细节设置. --- #### 4. 关键技术点探讨 - **Reset Mechanism**: 同步步骤意味着只有当全局时钟脉冲到达之后才会生效;相对而言异步则不受限于此限制可以直接立即作用到电路内部元件上. - **Edge Detection vs Level Sensitivity**: 根据应用场景选取合适的检测模式可以有效提升整体性能表现. - **Optimization Techniques**: 对大型复杂结构考虑运用状态编码优化手段减少资源消耗提高运行效率. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值