HDLbits答案更新系列18(3.2.5 Finite State Machines 3.2.5.27 Q6b: FSM next-state logic等)

本文深入探讨了状态机设计的多种方法,包括FSM和one-hot编码应用,通过具体题目解析,展示了如何实现状态转移逻辑及输出控制。适合于希望深入了解状态机原理及其在实际项目中应用的读者。

目录

前言

3.2.5 Finite State Machines

3.2.5.27 Q6b: FSM next-state logic(Exams/m2014 q6b)

3.2.5.28 Q6c: FSM one-hot next-state logic(Exams/m2014 q6c)

3.2.5.29 Q6: FSM(Exams/m2014 q6)

3.2.5.30 Q2a: FSM(Exams/2012 q2fsm)

3.2.5.31 Q2b: One-hot FSM equations(Exams/2012 q2b)

3.2.5.32 Q2a: FSM(Exams/2013 q2afsm)

3.2.5.33 Q2b: Another FSM(Exams/2013 q2bfsm)

结语

HDLbits网站链接


前言

今天更新状态机这节的最后几道题目,这个网站的“大老虎”状态机小节终于要完结啦。

3.2.5 Finite State Machines

3.2.5.27 Q6b: FSM next-state logic(Exams/m2014 q6b)

module top_module (
    input [3:1] y,
    input w,
    output Y2);
    
    assign Y2 = (y == 3'b001 | y == 3'b101) & ~w | 
                (y == 3'b001 | y == 3'b010 | y == 3'b100 | y == 3'b101) & w;

endmodule

这道题目作者说用y[3:1]来表示ABCDEF几个状态,我的答案纯粹是为了解题,大家看看就好。

3.2.5.28 Q6c: FSM one-hot next-state logic(Exams/m2014 q6c)

module top_module (
    input [6:1] y,
    input w,
    output Y2,
    output Y4);
    
    parameter A = 3'd1, B = 3'd2, C = 3'd3;
    parameter D = 3'd4, E = 3'd5, F = 3'd6;
    
    assign Y2 = ~w & y[A];
    assign Y4 = w & (y[B] | y[C] | y[E] | y[F]);
    
    //assign Y2 = ~w & y[1];
    //assign Y4 = w & (y[2] | y[3] | y[5] | y[6]);

endmodule

这道题和上一道题目差不多,只不过这里作者让我们使用one-hot编码来完成。

3.2.5.29 Q6: FSM(Exams/m2014 q6)

module top_module (
    input clk,
    input reset,     // synchronous reset
    input w,
    output z);

    parameter A = 3'd0, B = 3'd1, C = 3'd2, D = 3'd3, E = 3'd4, F = 3'd5;
    reg	[2:0]	current_state;
    reg [2:0]	next_state;
    
    always@(posedge clk)begin
        if(reset)begin
            current_state <= A;
        end
        else begin
            current_state <= next_state;
        end
    end
    
    always@(*)begin
        case(current_state)
            A:begin
                next_state = w ? A : B;
            end
            B:begin
                next_state = w ? D : C;
            end
            C:begin
                next_state = w ? D : E;
            end
            D:begin
                next_state = w ? A : F;
            end
            E:begin
                next_state = w ? D : E;
            end
            F:begin
                next_state = w ? D : C;
            end
            default:begin
                next_state = A;
            end
        endcase
    end
    
    assign z = (current_state == E || current_state == F);
               
endmodule

这是一道无比正宗的FSM题目,大家看着状态转移图完成就好了,正宗地不需要任何解题思路。

3.2.5.30 Q2a: FSM(Exams/2012 q2fsm)

module top_module (
    input clk,
    input reset,   // Synchronous active-high reset
    input w,
    output z
);
    parameter A = 3'd0, B = 3'd1, C = 3'd2, D = 3'd3, E = 3'd4, F = 3'd5;
    reg [2:0]	current_state;
    reg [2:0]	next_state;
    
    always@(posedge clk)begin
        if(reset)begin
            current_state <= A;
        end
        else begin
            current_state <= next_state;
        end
    end
    
    always@(*)begin
        case(current_state)
            A:begin
                next_state = w ? B : A;
            end
            B:begin
                next_state = w ? C : D;
            end
            C:begin
                next_state = w ? E : D;
            end
            D:begin
                next_state = w ? F : A;
            end
            E:begin
                next_state = w ? E : D;
            end
            F:begin
                next_state = w ? C : D;
            end
            default:begin
                next_state = A;
            end
        endcase
    end
    
    assign z = (current_state == E || current_state == F);

endmodule

和上一道题目一样,相当正宗,不过大家要注意,这里的状态转移图和上一道题目不一样,比如w为1的时候才从A状态跳到B状态。

3.2.5.31 Q2b: One-hot FSM equations(Exams/2012 q2b)

module top_module (
    input [5:0] y,
    input w,
    output Y1,
    output Y3
);

    parameter A = 3'd0, B = 3'd1, C = 3'd2;
    parameter D = 3'd3, E = 3'd4, F = 3'd5;
    
    assign Y1 = w & y[A];
    assign Y3 = ~w & (y[B] | y[C] | y[E] | y[F]);

endmodule

这道题目又是要使用one-hot编码逻辑,大家也看出来了,这个系列,作者给出的one-hot编码思想和我们平时的思想不太一样,我们的习惯是将状态定义为one-hot编码。不过,one-hot编码的优点就在于只需要判断一位的数据就可以判定状态,速度快,这点作者和我们的思想相同,所以大家也不用太过纠结,学习作者的思想,然后完成题目就好。另一种可能性,由于我们写状态机用的one-hot编码和正常的二进制编码,输出波形都是一致的,作者的tb检查不出来我们是否使用了one-hot编码,所以只好按照这种方式了。

3.2.5.32 Q2a: FSM(Exams/2013 q2afsm)

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input [3:1] r,   // request
    output [3:1] g   // grant
); 

    parameter A = 2'd0, B = 2'd1, C = 2'd2, D = 2'd3;  
    reg	[1:0]	current_state;
    reg	[1:0]	next_state;
    
    always@(posedge clk)begin
        if(resetn == 1'b0)begin
            current_state <= A;
        end
        else begin
            current_state <= next_state;
        end
    end
    
    always@(*)begin
        case(current_state)
            A:begin
                if(r[1])begin
                    next_state = B;
                end
                else if(r == 3'd0)begin
                    next_state = A;
                end
                else if(r[2:1] == 2'd2)begin
                    next_state = C;
                end
                else if(r == 3'd4)begin
                    next_state = D;
                end
                else begin
                    next_state = A;
                end
            end
            B:begin
                if(r[1])begin
                    next_state = B;
                end
                else if(r[1] == 1'b0)begin
                    next_state = A;
                end
                else begin
                    next_state = A;
                end
            end
            C:begin
                if(r[2])begin
                    next_state = C;
                end
                else if(r[2] == 1'b0)begin
                    next_state = A;
                end
                else begin
                    next_state = A;
                end
            end
            D:begin
                if(r[3])begin
                    next_state = D;
                end
                else if(r[3] == 1'b0)begin
                    next_state = A;
                end
                else begin
                    next_state = A;
                end
            end
            default:begin
                next_state = A;
            end
        endcase
    end
    
    always@(*)begin
        if(current_state == B)begin
            g[1] = 1'b1;
        end
        else begin
            g[1] = 1'b0;
        end
        if(current_state == C)begin
            g[2] = 1'b1;
        end
        else begin
            g[2] = 1'b0;
        end
        if(current_state == D)begin
            g[3] = 1'b1;
        end
        else begin
            g[3] = 1'b0;
        end
    end  

endmodule

这道题目和前面的题目又不一样了,前面只需要判断in为0或者为1就可以判定状态的转移了,这道题目需要判定3位输入,大家对着状态转移图完成题目就好了。

3.2.5.33 Q2b: Another FSM(Exams/2013 q2bfsm)

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input x,
    input y,
    output f,
    output g
); 
    
    parameter IDLE = 4'd0, S1 = 4'd1, S2 = 4'd2, S3 = 4'd3, S4 = 4'd4;
    parameter S5 = 4'd5, FOREVER_ONE = 4'd6, FOREVER_ZERO = 4'd7;
    parameter F_OUT = 4'd8;
    
    reg	[3:0]	current_state;
    reg	[3:0]	next_state;
    
    always@(posedge clk)begin
        if(resetn == 1'b0)begin
            current_state <= IDLE;
        end
        else begin
            current_state <= next_state;
        end
    end
    
    always@(*)begin
        case(current_state)
            IDLE:begin
                next_state = F_OUT;
            end
            F_OUT:begin
                next_state = S1;
            end
            S1:begin
                next_state = x ? S2 : S1;
            end
            S2:begin
                next_state = x ? S2 : S3;
            end
            S3:begin
                next_state = x ? S4 : S1;
            end  
            S4:begin
                next_state = y ? FOREVER_ONE : S5;
            end
            S5:begin
                next_state = y ? FOREVER_ONE : FOREVER_ZERO;
            end
            FOREVER_ONE:begin
                next_state = FOREVER_ONE;
            end
            FOREVER_ZERO:begin
                next_state = FOREVER_ZERO;
            end
            default:begin
                next_state = IDLE;
            end
        endcase
    end
    
    assign f = (current_state == F_OUT);
    assign g = (current_state == S4 || current_state == S5 || current_state == FOREVER_ONE);
                
endmodule

题意中说,复位信号撤销时,在下一个周期内将f输出为1,注意f为1只保持一个周期,然后状态机取决于x的值,当x在连续的三个周期中产生值为1、0、1时,下一周期将g输出为1,在保持g为1时判断y的输入,如果y在两个周期中有任意一个周期为1了,那么g永久保持1;如果两个周期都没有1,那么g将永久保持0。

我在这里增加了几个状态,比如FOREVER_ONE和FOREVER_ZERO状态,这两个状态完全是根据题意增加的。

结语

状态机这个小节终于更新完啦,这个小节的内容真的是非常多,涉及到的思想不论在工程中还是面试中都是必须要掌握的,这个小节完成后,后面的内容就基本没有特别难的了,希望大家可以好好温习这个小节内容,争取多用几种方法完成,感受每种方法的异同点,完成这个小节,对状态机的了解会加深不少。最后,如果哪里代码或者解析有误,欢迎大家留言~

HDLbits网站链接

https://hdlbits.01xz.net/wiki/Main_Page

jiejiatao@jiejiataodeMacBook-Pro wuyang_android % ./gradlew :module_common:dependencies --configuration api > Configure project :app EcLicensePlugin :当前工程的gradle版本为: 7.5 app添加编译模块:module_aar app添加编译模块:module_account app添加编译模块:module_car_service app添加编译模块:module_carInfo app添加编译模块:module_equipment app添加编译模块:module_main app添加编译模块:module_mall app添加编译模块:module_showroom app添加编译模块:module_thirdPlatform app添加编译模块:module_web app添加编译模块:resource EcLicensePlugin :packageName: null EcLicensePlugin :variant: formalDebug appPackageName: null flavorPackageName: com.whmapp.wuyang.honda EcLicensePlugin :variant: uatDebug appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: developDebug appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: formalRelease appPackageName: null flavorPackageName: com.whmapp.wuyang.honda EcLicensePlugin :variant: uatRelease appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda EcLicensePlugin :variant: developRelease appPackageName: null flavorPackageName: com.whmappcs.wuyang.honda --W- The variant: formalDebug, There&#39;s no json file --W- The variant: uatDebug, There&#39;s no json file --W- The variant: developDebug, There&#39;s no json file --W- The variant: formalRelease, There&#39;s no json file --W- The variant: uatRelease, There&#39;s no json file --W- The variant: developRelease, There&#39;s no json file > Configure project :module_common module_common: &#39;annotationProcessor&#39; dependencies won&#39;t be recognized as kapt annotation processors. Please change the configuration name to &#39;kapt&#39; for these artifacts: &#39;com.github.bumptech.glide:compiler:4.9.0&#39;. > Task :module_common:dependencies ------------------------------------------------------------ Project &#39;:module_common&#39; ------------------------------------------------------------ api - API dependencies for &#39;main&#39; sources. (n) +--- unspecified (n) +--- unspecified (n) +--- androidx.databinding:viewbinding:8.3.2 (n) +--- com.blankj:utilcodex:1.31.1 (n) +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.10 (n) +--- org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0 (n) +--- androidx.core:core-ktx:1.6.0 (n) +--- com.squareup.okhttp3:okhttp:4.9.0 (n) +--- com.squareup.okhttp3:logging-interceptor:4.9.0 (n) +--- io.reactivex.rxjava2:rxjava:2.2.19 (n) +--- com.squareup.retrofit2:retrofit:2.9.0 (n) +--- io.reactivex.rxjava2:rxandroid:2.1.0 (n) +--- com.squareup.retrofit2:converter-gson:2.9.0 (n) +--- com.squareup.retrofit2:adapter-rxjava2:2.4.0 (n) +--- com.google.code.gson:gson:2.10.1 (n) +--- org.greenrobot:eventbus:3.2.0 (n) +--- com.geyifeng.immersionbar:immersionbar:3.2.2 (n) +--- com.guolindev.permissionx:permissionx:1.6.0 (n) +--- com.github.li-xiaojun:XPopup:2.10.0 (n) +--- com.github.bumptech.glide:glide:4.14.2 (n) +--- jp.wasabeef:glide-transformations:4.3.0 (n) +--- com.github.zjupure:webpdecoder:2.0.4.12.0 (n) +--- io.github.scwang90:refresh-layout-kernel:2.1.0 (n) +--- io.github.scwang90:refresh-header-classics:2.1.0 (n) +--- io.github.scwang90:refresh-footer-classics:2.1.0 (n) +--- com.github.JessYanCoding:AndroidAutoSize:v1.2.1 (n) +--- com.android.support:appcompat-v7:27.+ (n) +--- com.android.support:design:27.+ (n) +--- com.orhanobut:hawk:2.0.1 (n) +--- com.baidu.lbsyun:baidu-lbs-aar:1.5 (n) +--- com.baidu.lbsyun:java-poet:1.1 (n) +--- com.baidu.lbsyun:onsdk_all:1.7.1 (n) +--- com.baidu.lbsyun:protobuf_gens-map:1.4 (n) +--- cn.jiguang.sdk:jpush:5.7.0 (n) +--- com.huawei.hms:push:6.13.0.300 (n) +--- cn.jiguang.sdk.plugin:huawei:5.7.0 (n) +--- cn.jiguang.sdk.plugin:vivo:5.7.0 (n) +--- cn.jiguang.sdk.plugin:xiaomi:5.7.0 (n) +--- cn.jiguang.sdk.plugin:oppo:5.7.0 (n) +--- commons-codec:commons-codec:1.6 (n) +--- androidx.annotation:annotation:1.1.0 (n) +--- cn.jiguang.sdk.plugin:honor:5.7.0 (n) +--- cn.jiguang.sdk:jverification:3.2.5 (n) +--- com.tencent.bugly:crashreport:latest.release (n) +--- io.github.idisfkj:android-startup:1.1.0 (n) +--- com.auth0:java-jwt:4.4.0 (n) +--- io.jsonwebtoken:jjwt:0.9.1 (n) +--- com.github.lzyzsd:jsbridge:1.0.4 (n) +--- com.google.android.material:material:1.6.0 (n) +--- unspecified (n) +--- com.github.yyued:SVGAPlayer-Android:2.6.1 (n) +--- unspecified (n) +--- com.google.android.exoplayer:exoplayer:2.18.1 (n) +--- io.github.youth5201314:banner:2.2.3 (n) +--- com.github.zhpanvip:bannerviewpager:3.5.12 (n) +--- com.github.getActivity:XXPermissions:20.0 (n) +--- com.airbnb.android:lottie:5.2.0 (n) +--- com.huawei.hms:scanplus:2.12.0.300 (n) +--- net.easyconn:ec:12.2.1.0415 (n) +--- net.easyconn:common:12.2.1.0415 (n) +--- net.easyconn:carmanble:12.2.1.0415 (n) +--- net.easyconn:rotation:12.2.1.0415 (n) +--- net.easyconn:speech_iflytek:12.2.1.0415 (n) +--- net.easyconn:theme_main:+ (n) +--- net.easyconn:carman_baidumap:0.6.16.2 (n) +--- com.huawei.hms:scanplus:2.8.0.300 (n) +--- com.github.hackware1993:MagicIndicator:1.6.0 (n) +--- com.github.hackware1993:MagicIndicator:1.7.0 (n) +--- project module_aar (n) +--- project resource (n) +--- com.ligo:LogRecord:1.0.5 (n) +--- com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.8.4 (n) \--- androidx.databinding:viewbinding:7.1.0 (n) (n) - Not resolved (configuration is not meant to be resolved) A web-based, searchable dependency report is available by adding the --scan option. Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use &#39;--warning-mode all&#39; to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings BUILD SUCCESSFUL in 686ms 1 actionable task: 1 executed 只列出有哪些依赖,但是无法出现依赖的大小
最新发布
06-26
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值