HDLbits: Fsm serial

根据题意设计了四个状态,写出代码如下:

cs 复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output done
); 
    parameter IDLE = 3'b000, START = 3'b001, DATA = 3'b010, STOP = 3'b100, bit_counter_end = 4'd7;
    
    reg [2:0] state,next_state;
    
    reg [3:0] bit_counter;
    
    always@(*)begin
        case(state)
            START: next_state = DATA;
            DATA: next_state = (bit_counter >= bit_counter_end)? (in?STOP:IDLE):DATA;
            STOP: next_state = in?IDLE:START;
            IDLE: next_state = in?IDLE:START;
            default: next_state = IDLE;
        endcase
    end
    
    always@(posedge clk)begin
        if(reset)
            state <= IDLE;
        else
            state <= next_state;
    end
    
    always@(posedge clk)begin
        if(reset)
           bit_counter <= 4'd0;            
        else if(state == DATA)
           bit_counter <= bit_counter + 4'd1;  
        else
           bit_counter <= 4'd0;
    end
        
    assign done = (state == STOP);    

endmodule

时序图如下,有误:

参考网上的答案,加入了一个ERROR状态表示例题时序图"?"的时候,下面代码没问题了

注意bit_counter计数的那块如果用state==DATA判断,那么上面长度判断就得用7,如果用next_state==DATA判断,上面长度判断就得用8

cs 复制代码
module top_module(
    input clk,
    input in,
    input reset,    // Synchronous reset
    output done
); 
    parameter IDLE = 4'b0000, START = 4'b0001, DATA = 4'b0010, STOP = 4'b0100, ERROR = 4'b1000, 
    bit_counter_end = 4'd7;
    
    reg [3:0] state,next_state;
    
    reg [3:0] bit_counter;
    
    always@(*)begin
        case(state)
            START: next_state = DATA;
            DATA: next_state = (bit_counter >= bit_counter_end)? (in?STOP:ERROR):DATA;
            STOP: next_state = in?IDLE:START;
            ERROR: next_state = in?IDLE:ERROR;
            IDLE: next_state = in?IDLE:START;
            default: next_state = IDLE;
        endcase
    end
    
    always@(posedge clk)begin
        if(reset)
            state <= IDLE;
        else
            state <= next_state;
    end
    
    always@(posedge clk)begin
        if(reset)
           bit_counter <= 0;            
        else if(state == DATA)          
           bit_counter <= bit_counter + 1;
        else
           bit_counter <= 0;
    end
        
    assign done = (state == STOP);    

endmodule
相关推荐
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog快速入门—VL70
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL74
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog进阶挑战—VL25
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL72、VL73
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL65
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL69
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL64
学习·fpga开发·verilog学习
IM_DALLA3 个月前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL62
学习·fpga开发·verilog学习
Dale_e10 个月前
18 19 SPI接口的74HC595驱动数码管实验
经验分享·笔记·学习·fpga开发·verilog学习
Dale_e10 个月前
15 ABC基于状态机的按键消抖原理与状态转移图
经验分享·笔记·学习·fpga开发·verilog学习