Fsm serialdata

Now that you have a finite state machine that can identify when bytes are correctly received in a serial bitstream, add a datapath that will output the correctly-received data byte. out_byte needs to be valid when done is 1, and is don't-care otherwise.

Note that the serial protocol sends the least significant bit first.

cpp 复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output [7:0] out_byte,
    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; 
    
    // Use FSM from Fsm_serial
    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
    	
    always@ (posedge clk)
        if(reset)
            state <= IDLE;
    	else 
            state <= next_state;
    
    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)
            done <= 0;
    else if(next_state ==  END)
             done <= 1;
    else
    	done <= 0;
	
    always@ (posedge clk)
        if(reset)
    		out_byte <= 8'd0; 
    else if(next_state == IDLE)
        out_byte <= 8'd0;
    else if(next_state == TRANS  )
        out_byte <= {in,out_byte[7:1]};
    else 
        out_byte <= out_byte;
        
endmodule
相关推荐
FPGA小迷弟5 天前
FPGA在高速数据采集系统中的应用!!!
stm32·物联网·fpga开发·verilog·fpga
简简单单做算法5 天前
基于FPGA的图像双线性插值算法verilog实现,包括tb测试文件和MATLAB辅助验证
matlab·verilog·fpga·图像双线性插值
FPGA小迷弟7 天前
如何提高FPGA的逻辑利用率与资源效率!!!
stm32·物联网·fpga开发·verilog·fpga
FPGA小迷弟7 天前
基于FPGA的图像处理算法研究!!!
stm32·物联网·fpga开发·verilog·fpga
自激振荡器8 天前
0,国产FPGA(紫光同创)-新建PDS工程
fpga开发·verilog·国产fpga·紫光fpga
皮皮宽10 天前
数字IC开发:布局布线
fpga开发·bug·verilog·数字电路设计
FPGA小迷弟11 天前
SRIO接口,FPGA实现,学习笔记。
stm32·物联网·fpga开发·verilog·fpga·1024程序员节
FPGA小迷弟20 天前
FPGA实现SPI接口,用verilog实现,SPI接口使用例程!!!
stm32·物联网·fpga开发·verilog·fpga
Xminyang23 天前
[Mac + Icarus Verilog + gtkwave] Mac运行Verilog及查看波形图
macos·verilog
hjjdebug1 个月前
verilog 介绍(附状态机实例)
嵌入式硬件·fpga开发·verilog·状态机·密码锁