​HDLBits网址:https://hdlbits.01xz.net/wiki/Main_Page

前言

HDLBits前面几题简单状态机的训练,同样的状态转移图分为异步复位和同步复位,除了复位不一样,基本上其他是一样的,所以这里就只介绍异步复位的情况。

1.Simple FSM 1

This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.

Module Declaration

module top_module(input clk,input areset,    // Asynchronous reset to state Binput in,output out);

我的分析

状态机分两种类型:Moore状态机和Mealy状态机,两种状态机的区别如下:

Moore状态机:下一状态只由当前状态决定,即次态=f(现状,输入),输出=f(现状)
    Mealy状态机:下一状态不但与当前状态有关,还与当前输入值有关,即次态=f(现状,输入),输出=f(现状,输入)。

这里是最简单的Moore状态机(输出只与状态有关),只有两个状态,一个输入和一个输出,还有一个异步复位信号。我的答案用采用三段式编写:

module top_module(    input clk,    input areset,    // Asynchronous reset to state B    input in,    output out);//    parameter A=0, B=1;     reg state, next_state;    always @(*) begin    // This is a combinational always block        case(state)            A:begin                if(in) next_state <= A;                else next_state <= B;            end            B:begin                if(in) next_state <= B;                else next_state <= A;            end        endcase    end    always @(posedge clk, posedge areset) begin    // This is a sequential always block        // State flip-flops with asynchronous reset        if(areset)            state <= B;        else            state <= next_state;    end    // Output logic        assign out = (state == B)? 1:0;endmodule

2. Simple FSM 2

This is a Moore state machine with two states, two inputs, and one output. Implement this state machine.

Module Declaration

module top_module(
   input clk,
   input areset,    // Asynchronous reset to OFF
   input j,
   input k,
   output out);

我的设计

这题与上一题原理一样,只是多了一个输入,通过状态转移图可以看出这里实现的是JK filp-flop,代码如下:

module top_module(    input clk,    input areset,    // Asynchronous reset to OFF    input j,    input k,    output out); //       parameter OFF=0, ON=1;     reg state, next_state;     always @(*) begin        // State transition logic        case(state)            OFF: begin                if(j == 1) next_state = ON;                else next_state = OFF;            end            ON: begin                if(k == 1) next_state = OFF;                else next_state = ON;            end                        endcase    end     always @(posedge clk, posedge areset) begin        // State flip-flops with asynchronous reset        if(areset) state <= OFF;        else state <= next_state;    end     // Output logic    // assign out = (state == ...);    assign out = (state == ON)? 1 : 0; endmodule

3. Simple state transition 3

The following is the state transition table for a Moore state machine with one input, one output, and four states. Use the following state encoding: A=2'b00, B=2'b01, C=2'b10, D=2'b11.

Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. Given the current state (state), compute the next_state and output (out) based on the state transition table.

State Next state Output
in=0 in=1
A A B 0
B C B 0
C A D 0
D C B 1

我的设计

这里表格其实跟状态转移图是一样的,也是表示状态转移,具体代码如下:

module top_module(    input in,    input [1:0] state,    output [1:0] next_state,    output out); //     parameter A=0, B=1, C=2, D=3;     // State transition logic: next_state = f(state, in)    always@(*) begin        case(state)            A: begin                if(in == 0) next_state = A;                else next_state = B;            end            B: begin                if(in == 0) next_state = C;                else next_state = B;            end            C: begin                if(in == 0) next_state = A;                else next_state = D;            end            D: begin                if(in == 0) next_state = C;                else next_state = B;            end        endcase    end     // Output logic:  out = f(state) for a Moore state machine    assign out = (state == D) ? 1:0; endmodule

4. Simple FSM 3

The following is the state transition table for a Moore state machine with one input, one output, and four states. Implement this state machine. Include an asynchronous reset that resets the FSM to state A.

State Next state Output
in=0 in=1
A A B 0
B C B 0
C A D 0
D C B 1

Module Declaration

module top_module(input clk,input in,input areset,output out);

状态转移图

我的设计

module top_module(    input clk,    input in,    input areset,    output out); //    parameter A = 4'b0001, B = 4'b0010, C = 4'b0100, D = 4'b1000;    reg [3:0] state, next_state;    // State transition logic    always@(*) begin        case(state)            A: begin                if(in == 0) next_state = A;                else next_state = B;            end            B: begin                if(in == 0) next_state = C;                else next_state = B;            end            C: begin                if(in == 0) next_state = A;                else next_state = D;            end            D: begin                if(in == 0) next_state = C;                else next_state = B;            end         endcase    end    // State flip-flops with asynchronous reset    always@(posedge clk or posedge areset) begin        if(areset) state <= A;        else state <= next_state;    end    // Output logic    assign out = (state == D) ? 1 : 0;endmodule

HDL-Bits提供的答案

module top_module (  input clk,  input in,  input areset,  output out);  // Give state names and assignments. I'm lazy, so I like to use decimal numbers.  // It doesn't really matter what assignment is used, as long as they're unique.  parameter A=0, B=1, C=2, D=3;  reg [1:0] state;    // Make sure state and next are big enough to hold the state encodings.  reg [1:0] next;    // Combinational always block for state transition logic. Given the current state and inputs,    // what should be next state be?    // Combinational always block: Use blocking assignments.        always@(*) begin    case (state)      A: next = in ? B : A;      B: next = in ? B : C;      C: next = in ? D : A;      D: next = in ? B : C;    endcase    end    // Edge-triggered always block (DFFs) for state flip-flops. Asynchronous reset.    always @(posedge clk, posedge areset) begin    if (areset) state <= A;        else state <= next;  end          // Combinational output logic. In this problem, an assign statement is the simplest.      assign out = (state==D);  endmodule

上面两段代码孰优孰劣?有一说一,其实两段代码描述的都是同一张状态转移图,唯一的区别就是next_state的赋值。我个人角度是觉得三段式的代码风格会比较好,但是HDL-Bits提供的写法也没有问题,比较简练,还是根据个人口味选择吧,都是可行的代码格式。

微信公众号(同步更新)

建立了一个微信公众号“Andy的ICer之路”,此公众号主要分享数字IC相关的学习经验,做公众号的目的就是记录自己的学习过程,很多东西回过头来可能就忘记了,为了记住知识和分享知识,希望自己可以保持更新,有兴趣的朋友可以关注一下!

Verilog专题(十九)新世界的大门——状态机相关推荐

  1. Verilog专题(九)DFF、Dlatch、JK flip-flop

    前言 对于verilog的学习,这里推荐一个比较好的实践网站HDLBits:https://hdlbits.01xz.net/wiki/Main_Page 本系列记录一些我觉得有价值的题目,希望通过这 ...

  2. “COMSOL Multiphysics多物理场仿真技术与应用”光电专题(二十九期)

    (一) 案列应用实操教学: 案例一 光子晶体能带分析.能谱计算.光纤模态计算.微腔腔膜求解 案例二 类比凝聚态领域魔角石墨烯的moiré 光子晶体建模以及物理分析 案例三 传播表面等离激元和表面等离激 ...

  3. 微服务接入oauth2_SpringCloud微服务实战系列(十九)Ouath2在真实场景中的应用之客户端接入(第一种写法)...

    SpringCloud微服务实战系列(十九)Ouath2在真实场景中的应用之客户端接入(第一种写法) 一.概述 在<SpringCloud微服务实战系列(十七)Ouath2在真实场景中的应用之资 ...

  4. ROS探索总结(十六)(十七)(十八)(十九)——HRMRP机器人的设计 构建完整的机器人应用系统 重读tf 如何配置机器人的导航功能

    ROS探索总结(十六)--HRMRP机器人的设计 1. HRMRP简介         HRMRP(Hybrid Real-time Mobile Robot Platform,混合实时移动机器人平台 ...

  5. 3dobject用什么打开_第一次用开塞露是什么感觉?网友:像打开了新世界的大门

    第一次用开塞露是什么感觉?网友:像打开了新世界的大门 我妈说我小时候便秘去医院,医生给开了支开塞露,然后在医院的公厕里使用的,我妈的描述是:"要不是我手挪走的快点,就直接喷我手了" ...

  6. 第十九章:李丽质入狱

    第十九章:李丽质入狱 于是第二天,在李淳风的号召下,一众长安城的居民在皇宫下,再一次的聚集开了. 李淳风开始指挥大家说道:"你们想要争取到不让自家孩子身上再绑上红丝带吗?" &qu ...

  7. (95)FPGA二四译码器设计,面试必问(十九)(第19天)

    (95)FPGA二四译码器设计,面试必问(十九)(第19天) 1 文章目录 1)文章目录 2)FPGA初级课程介绍 3)FPGA初级课程架构 4)FPGA二四译码器设计,面试必问(十九)(第19天) ...

  8. 【正点原子FPGA连载】第十九章IP核之双端口RAM实验 -摘自【正点原子】新起点之FPGA开发指南_V2.1

    1)实验平台:正点原子新起点V2开发板 2)平台购买地址:https://detail.tmall.com/item.htm?id=609758951113 2)全套实验源码+手册+视频下载地址:ht ...

  9. 第十九讲:爱情:如何让爱情天长地久 第二十讲:幽默 第二十一讲:爱情自尊

    (注:此为课程第十九课,更新于2017年7月16日) 大家好! 今天我们继续谈论爱情,讲之前先说一下,一位叫Nadia的同学,你的钥匙链落在这教室了,就在我这里,课后请来我这里取. 那我们来讲讲爱情吧 ...

最新文章

  1. 普渡大学计算机硕士申请条件,普渡大学计算机与信息技术理学硕士研究生申请要求及申请材料要求清单...
  2. EPSON机械臂TCP通讯,实现手眼标定(附EPSON代码)
  3. 关于File.separator 文件路径:wind与linux下路径问题 .
  4. Eclipse插件使用links目录的用法
  5. 设计模式(一)----单例模式
  6. CRM Fiori Customer report filter过滤器
  7. 最短路径算法----floyd(转)
  8. 集成学习之参数调整策略
  9. 爹地,我找到了!15个极好的Linux find命令示例
  10. 运行npm install命令后的执行过程
  11. 每天进步一点点《ML - 从极大似然到EM算法》
  12. cudnn版本_踩过cuda、cudnn的坑[持续更新]
  13. VUE图片预览放大缩小插件viewer
  14. 力扣-102. 二叉树的层序遍历
  15. ubuntu下载字体
  16. 计算机安装系统后鼠标无法使用,电脑重装系统后鼠标键盘不能用怎么办,鼠标键盘不能用解决方法...
  17. 小手拍拍机器人_手指谣
  18. 高并发分布式场景下的应用---分布式锁
  19. 疯狂的java 目录,疯狂创客圈 JAVA死磕系列 总目录
  20. 智能家用洗地机哪个牌子好?实际好用的家用洗地机推荐

热门文章

  1. 【纪中受难记】——Day20:祈祷落幕时
  2. python将图片转动漫_如何将照片动漫化
  3. 不锈钢常识 - Powered by Discuz!
  4. 史上最全!234个财务数据分析数据指标归纳总结
  5. Hive元数据库中各个表的含义(十)
  6. 求一亿个数字里面最小的10个数字
  7. Vue引入第三方字体
  8. java中GRID_size的作用_Grid布局简介
  9. hank's sap blog
  10. 比较DirectX和OpenGL的区别