Fsm serial

In many (older) serial communications protocols, each data byte is sent along with a start bit and a stop bit, to help the receiver delimit bytes from the stream of bits. One common scheme is to use one start bit (0), 8 data bits, and 1 stop bit (1). The line is also at logic 1 when nothing is being transmitted (idle).

Design a finite state machine that will identify when bytes have been correctly received when given a stream of bits. It needs to identify the start bit, wait for all 8 data 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.

复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output done
); 
    parameter IDLE = 3'b000,
    			START = 3'b001,
    			TRANS = 3'b011,
    			END = 3'b010,
    			ERR = 3'b110;
    
    reg [2:0] state, next_state;
    reg [3:0] cnt_data; 
    
    
    always@ (posedge clk)
        if(reset)
            cnt_data <= 4'b0;
    else if(next_state == START)
        cnt_data <= 4'b0;
    else if(next_state == TRANS)
        cnt_data <= cnt_data + 1'd1;
    else 
        cnt_data <= cnt_data;
    
    
    always@ (posedge clk)
        if(reset)
            state <= IDLE;
    	else 
            state <= next_state;
    
    always@ (posedge clk)
        if(reset)
            done <= 0;
    else if(next_state ==  END)
             done <= 1;
    else
    	done <= 0;
        
    always@ (*)
        case (state)
        	IDLE:
                if(~in)
                    next_state <= START;
            	else 
                    next_state <= IDLE;
            START: 
              next_state <= TRANS;
            TRANS: 
                if(cnt_data == 4'd8) begin
                    if(in)
                        next_state <= END;
                    else 
                        next_state <= ERR;
                   end
            	else 
                    next_state <= TRANS;
            END: 
                if(in)
                    next_state <= IDLE;
            	else 
                    next_state <= START;
            ERR:  
                if(in)
                        next_state <= IDLE;
                    else 
                        next_state <= ERR;
            default:
                next_state <= IDLE;
        endcase

endmodule
相关推荐
北方孤寂的灵魂2 天前
systemverilog中随机std::randomize的用法
verilog·systemverilog·sv·数字验证
FPGA_小田老师4 天前
FPGA例程(4):按键消抖实验
fpga开发·verilog·fpga demo·fpga例程
FPGA小迷弟10 天前
京微齐力FPGA联合modelsim仿真操作
fpga开发·ic·verilog·fpga·仿真
FPGA_小田老师11 天前
FPGA例程(3):按键检测实验
fpga开发·verilog·vivado·led灯·按键测试
FPGA小迷弟14 天前
modelsim使用教程,仿真技巧,精华帖
fpga开发·verilog·fpga·modelsim
才鲸嵌入式22 天前
香山CPU(国产开源)的 SoC SDK底层程序编写,以及其它开源SoC芯片介绍
c语言·单片机·嵌入式·arm·cpu·verilog·fpga
莫问前程_满城风雨24 天前
verilog 可变范围的bit选择
运维·服务器·verilog
啄缘之间25 天前
10.基于 MARCH C+ 算法的SRAM BIST
经验分享·笔记·学习·verilog
s09071361 个月前
FPGA中同步与异步复位
fpga开发·verilog·xilinx·zynq
民乐团扒谱机1 个月前
十字路口交通信号灯控制器设计(Multisim 电路 + Vivado 仿真)
单片机·fpga开发·verilog·状态机·仿真·时序逻辑·multism