Circuits--Build large --FSM

  1. FSM: Enable shift register

    module top_module (
    input clk,
    input reset, // Synchronous reset
    output reg shift_ena);

    复制代码
     reg[2:0] count;
     
     always@(posedge clk)
         begin
             if(reset)
                 begin
                 count = 3'd0;
             	shift_ena = 1'b1;
                 end
             else
                 begin
                     if(count==3'd3)
                         shift_ena = 1'b0;
                     else 
                         begin
                            count = count +3'd1 ;
                            shift_ena = 1'b1;
                         end
                 end
         end

    endmodule

2.FSM : the complete FSM

复制代码
module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output shift_ena,
    output counting,
    input done_counting,
    output done,
    input ack );
    
    parameter S = 4'd0;
    parameter S1 = 4'd1;
    parameter S11 = 4'd2;
    parameter S110 = 4'd3;
    parameter B0 = 4'd4;
    parameter B1 = 4'd5;
    parameter B2 = 4'd6;
    parameter B3 = 4'd7;
    parameter Count = 4'd8;
    parameter Wait = 4'd9;
   
    reg[3:0] state;
    reg[3:0] next_state;
    
    always@(*)
        begin
            case(state)
               S:	next_state = data ? S1 : S;
               S1:	next_state = data ? S11 : S;
               S11:	next_state = data ? S11 : S110;
               S110: next_state = data ? B0 : S;
               B0 : next_state = B1;
               B1 : next_state = B2;
               B2 : next_state = B3;
               B3 : next_state = Count;
               Count : next_state = done_counting ? Wait : Count;
               Wait : next_state = ack ? S : Wait;
                
                
            endcase
        end
    
    always@(posedge clk)
        begin
            if(reset)
                state = S;
            else
                state = next_state;
        end
    
    assign shift_ena = (state == B0)|(state == B1)|(state == B2)|(state == B3);
    assign counting = (state == Count);
    assign done = (state == Wait);
    
    

endmodule

3.the complete timer

复制代码
module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output [3:0] count,
    output counting,
    output done,
    input ack );
    
    parameter S0 = 4'd0;
    parameter S1 = 4'd1;
    parameter S2 = 4'd2;
    parameter S3 = 4'd3;
    parameter C0 = 4'd4;
    parameter C1 = 4'd5;
    parameter C2 = 4'd6;
    parameter C3 = 4'd7;
    parameter Count_1000 = 4'd8;
    parameter Done = 4'd9;
    
    reg[3:0] state;
    reg[3:0] next_state;
    reg[15:0] num;
    reg[3:0] delay;
    reg[3:0] acount;
    
    wire count_state;
    assign count_state = (num == (delay + 1'b1)*1000) ? 1'b1 : 1'b0;
    
    always@(*)
        begin
            if(num <= 16'd1000)
                acount = 4'd0;
            else if(num > 16'd1000&&num <= 16'd2000)
                acount = 4'd1;
            else if(num > 16'd2000&&num <= 16'd3000)
                acount = 4'd2;
            else if(num > 16'd3000&&num <= 16'd4000)
                acount = 4'd3;
            else if(num > 16'd4000&&num <= 16'd5000)
                acount = 4'd4;
            else if(num > 16'd5000&&num <= 16'd6000)
                acount = 4'd5;
            else if(num > 16'd6000&&num <= 16'd7000)
                acount = 4'd6;
            else if(num > 16'd7000&&num <= 16'd8000)
                acount = 4'd7;
            else if(num > 16'd8000&&num <= 16'd9000)
                acount = 4'd8;
            else if(num > 16'd9000&&num <= 16'd10000)
                acount = 4'd9;
            else if(num > 16'd10000&&num <= 16'd11000)
                acount = 4'd10;
            else if(num > 16'd11000&&num <= 16'd12000)
                acount = 4'd11;
            else if(num > 16'd12000&&num <= 16'd13000)
                acount = 4'd12;
            else if(num > 16'd13000&&num <= 16'd14000)
                acount = 4'd13;
            else if(num > 16'd14000&&num <= 16'd15000)
                acount = 4'd14;
            else
                acount = 4'd15;
        end
    
    always@(posedge clk)
        begin
            if(reset)
                num <= 16'd0;
            else if(next_state == Done)
                num <= 16'd0;
            else if(next_state == Count_1000)
                num <= num + 16'd1;
        end
    
    always@(*)
        begin
            case(state)
                S0: next_state = data ? S1 : S0;
                S1: next_state = data ? S2 : S0;
                S2: next_state = data ? S2 : S3;
                S3: next_state = data ? C0 : S0;
                C0:
                    begin
                       next_state = C1;
                        delay[3] = data;
                    end
                C1:
                    begin
                       next_state = C2;
                        delay[2] = data;
                    end
                C2:
                    begin
                       next_state = C3;
                        delay[1] = data;
                    end
                C3:
                    begin
                       next_state = Count_1000;
                        delay[0] = data;
                    end
                Count_1000:
                    next_state = count_state ? Done : Count_1000;
                Done:
                    next_state = ack ? S0 : Done;
                default:
                    next_state = S0;
                
            endcase
        end
    
    always@(posedge clk)
        begin
            if(reset)
                state <= S0;
            else
                state <= next_state;
        end
    
    assign count = (state == Count_1000) ? (delay - acount) : 4'd0;
    assign counting = (state == Count_1000);
    assign done = (state == Done);

endmodule
  1. FSM:one hot

    module top_module(
    input d,
    input done_counting,
    input ack,
    input [9:0] state, // 10-bit one-hot current state
    output B3_next,
    output S_next,
    output S1_next,
    output Count_next,
    output Wait_next,
    output done,
    output counting,
    output shift_ena
    ); //

    复制代码
     // You may use these parameters to access state bits using e.g., state[B2] instead of state[6].
     parameter S=0, S1=1, S11=2, S110=3, B0=4, B1=5, B2=6, B3=7, Count=8, Wait=9;
     assign B3_next = state[B2];
     assign S_next = ~d & state[S] | ~d & state[S1] | ~d & state[S110] | ack & state[Wait];
     assign S1_next = d & state[S];
     assign Count_next = state[B3] | ~done_counting & state[Count];
     assign Wait_next = done_counting & state[Count] | ~ack & state[Wait];
     assign done = state[Wait];
     assign counting = state[Count];
     assign shift_ena = state[B0] | state[B1] | state[B2] | state[B3];

    endmodule

相关推荐
xgbing9 小时前
在ubuntu中安装modelsim
fpga开发·modelsim
航Hang*9 小时前
Photoshop 图形与图像处理技术——第8章:图像的色彩与色彩调整和图像的输出与优化
图像处理·笔记·ui·photoshop
小桥流水---人工智能9 小时前
风电机组故障诊断与状态监测方法的研究局限性整理(背景笔记)
笔记
菩提小狗10 小时前
小迪安全笔记_第4天|扩展&整理|30+种加密编码进制全解析:特点、用处与实战识别指南|小迪安全笔记|网络安全|
笔记·安全·web安全
xian_wwq10 小时前
【学习笔记】OSI安全架构体系
网络·笔记·学习
love530love10 小时前
Windows 11 下再次成功本地编译 Flash-Attention 2.8.3 并生成自定义 Wheel(RTX 3090 sm_86 专属版)
人工智能·windows·笔记·编译·flash_attn·flash-attn·flash-attention
中屹指纹浏览器12 小时前
2025 硬核技术:中屹指纹浏览器进程级沙箱隔离,筑牢多开防关联壁垒
经验分享·笔记
再睡一夏就好12 小时前
多线程并发编程核心:互斥与同步的深度解析及生产者消费者模型两种实现
linux·运维·服务器·jvm·c++·笔记
m0_7269659812 小时前
RAG源代码笔记JAVA-高级RAG
笔记·ai·agent·rag
复业思维2024010813 小时前
Altium Designer (24.2.2)中更改库以及保持器件参数不变
笔记·学习·硬件工程