目录

原题重现

一点解释

最终实现


原题重现

The following is the state transition table for a Moore state machine with one input, one output, and four states. Use the following one-hot state encoding: A=4'b0001, B=4'b0010, C=4'b0100, D=4'b1000.

Derive state transition and output logic equations by inspection assuming a one-hot encoding. Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. (The testbench will test with non-one hot inputs to make sure you're not trying to do something more complicated).

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

What does "derive equations by inspection" mean?

One-hot state machine encoding guarantees that exactly one state bit is 1. This means that it is possible to determine whether the state machine is in a particular state by examining only one state bit, not all state bits. This leads to simple logic equations for the state transitions by examining the incoming edges for each state in the state transition diagram.

For example, in the above state machine, how can the state machine can reach state A? It must use one of the two incoming edges: "Currently in state A and in=0" or "Currently in state C and in = 0". Due to the one-hot encoding, the logic equation to test for "currently in state A" is simply the state bit for state A. This leads to the final logic equation for the next state of state bit A: next_state[0] = state[0]&(~in) | state[2]&(~in). The one-hot encoding guarantees that at most one clause (product term) will be "active" at a time, so the clauses can just be ORed together.

When an exercise asks for state transition equations "by inspection", use this particular method. The judge will test with non-one-hot inputs to ensure your logic equations follow this method, rather that doing something else (such as resetting the FSM) for illegal (non-one-hot) combinations of the state bits.

Although knowing this algorithm isn't necessary for RTL-level design (the logic synthesizer handles this), it is illustrative of why one-hot FSMs often have simpler logic (at the expense of more state bit storage), and this topic frequently shows up on exams in digital logic courses.

Module Declaration

module top_module(input in,input [3:0] state,output [3:0] next_state,output out); 

一点解释

实现一个电路,先确定需要的输入输出,原题给出声明:

Module Declaration

module top_module(input in,input [3:0] state,output [3:0] next_state,output out); 

可见,输入了当前状态state,输入in,下一个状态(next_state)作为输出,还有最终的输出值。

这不是一个完整的状态机,完整的状态机,state以及next_state是在模块内部自己定义的,我们需要给当前状态一个初值,状态转移的条件等等。

这里当前状态通过外部给了,所以对于当前状态的转换的一个always的时序逻辑就不需要自己写了,我们需要做的是状态转换的组合逻辑部分,以及输出的组合逻辑部分。

其次,本题是使用独热码作为状态编码,那么几个状态,状态变量就有几位,这里显然是4位,也就是有4个状态,状态编码采用独热码的形式如下:

A=4'b0001, B=4'b0010, C=4'b0100, D=4'b1000.

状态转移条件可以通过状态转移图给出,也可以通过状态转移表给出,本题是状态转移表格:

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 [3:0] state,output [3:0] next_state,output out); parameter A = 4'b0001, B = 4'b0010, C = 4'b0100, D = 4'b1000;reg [3:0] next_state;always@(*) begincase(state)A: beginif(in == 0) next_state = A;else next_state = B;endB: beginif(in == 0) next_state = C;else next_state = B;endC: beginif(in == 0) next_state = A;else next_state = D;endD: beginif(in == 0) next_state = C;else next_state = B;endendcaseendassign out = (state == D)?1:0;endmodule

最终实现

显而易见,上述写法肯定是正确的,这是最常规的写法,无论什么编码的状态机都可以这么写,我个人也喜欢这样写,但是今天要谈论的实现方式不是这样的,我们要使用独热码的特点来简化表达,如下:

module top_module(input in,input [3:0] state,output [3:0] next_state,output out); //parameter A=0, B=1, C=2, D=3;// State transition logic: Derive an equation for each state flip-flop.assign next_state[A] = state[A]&(in == 0) | state[C] & (in == 0);assign next_state[B] = state[A]&in | state[B]&in | state[D]∈assign next_state[C] = state[B]&(in == 0) | state[D]&(in == 0);assign next_state[D] = state[C] & in;// Output logic: assign out = state[D]? 1:0;endmodule

由于是独热码编码的状态机,所以我们就默认状态变量就只有一位是1,其他都是零,所以我们可以利用这样的一个特点来实现状态机。

和传统写法不一样的地方在于状态跳转部分,如题目介绍:

For example, in the above state machine, how can the state machine can reach state A? It must use one of the two incoming edges: "Currently in state A and in=0" or "Currently in state C and in = 0". Due to the one-hot encoding, the logic equation to test for "currently in state A" is simply the state bit for state A. This leads to the final logic equation for the next state of state bit A: next_state[0] = state[0]&(~in) | state[2]&(~in). The one-hot encoding guarantees that at most one clause (product term) will be "active" at a time, so the clauses can just be ORed together.

如何实现下一个状态是A呢?有两种情况:

当前状态是A且输入为0,则下一个状态是A;

当前状态为C且输入为0,下一个状态也是A;

于是这两种情况任意一个满足,下一个状态都会跳转到A,而A是独热码,最后一位为1,所以可以这么实现:

next_state[0] = state[0]&(~in) | state[2]&(~in)

由于一开始我们定义:

parameter A=0, B=1, C=2, D=3;

所以,代码中的0,1,2,3都可以用A、B、C、D实现。

这样,就解释了为什么可以上述方式实现状态转移了。

HDLBits 系列(25)独热码有限状态机实现的简单方式相关推荐

  1. HDLBits 系列(26)独热码有限状态机实现的两种方式

    目录 序言 原题复现 设计1 设计2 最后一句话 序言 这篇博客的标题起的,好像就是为独热码而讨论的,其实不然,下面给出一个题目,用任何方式的状态编码都可以,但是我就想讨论下用独热码来实现. 一种写法 ...

  2. HDLBits 系列(37)此系列关于独热码的题目的疑问?

    目录 背景 我的做法 第一题 第二题 第三题 解决办法 第一题 第二题 第三题 推荐 背景 目前为止,关于状态机独热码的题目,几乎没一个题目能做对,这令我疑惑?是不是题目的答案有问题?在此请大家一试? ...

  3. Verilog专题(二十)one-hot FSM(独热码有限状态机)

    HDLBits网址:https://hdlbits.01xz.net/wiki/Main_Page 题目 The following is the state transition table for ...

  4. FPGA中有限状态机的状态编码采用格雷码还是独热码?

    今天看<从算法设计到硬件逻辑的实现>这本电子书时,遇到了一个问题,就是有限状态机的编写中,状态编码是采用格雷码还是独热码呢?究竟采用哪一种编码呢? 采用独热码为什么节省许多组合电路? 等等 ...

  5. 有限状态机:独热码vs格雷码

    有限状态机编码时采用格雷码和采用独热码的选择 格雷码:相邻之间只变1bit,编码密度高. 独热码:任何状态只有1bit为1,其余皆为0,编码密度低. 比如说,表示4个状态,那么状态机寄存器采用格雷码编 ...

  6. FPGA/数字IC之有限状态机:简介及其编码方式:顺序码、独热码与格雷码比较

    目录 一.状态机的分类 二.状态机的优点与存在的问题 三.状态的编码方式 四.三种方式的比较 一.状态机的分类 标准状态机分为摩尔(Moore)状态机与米勒(Mealy)状态机. 摩尔状态机的输出只与 ...

  7. 二进制码、格雷码、独热码的区别

    格雷码 在一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码(Gray Code),另外由于最大数与最小数之间也仅一位数不同,即"首尾相连",因此又称循 ...

  8. verilog独热码检测

    独热码介绍 独热码是一种二进制编码方式,它的特点是,用来编码这个数的N位bit中,有且只有一位是1,其余位全部为0.因为只有1位是1,所以叫做one-hot (对应的,还有一种编码方式是只有1位是0, ...

  9. Verilog-状态机编码方式对比:独热码 vs 格雷码

    博主福利:100G+电子设计学习资源包! http://mp.weixin.qq.com/mp/homepage?__biz=MzU3OTczMzk5Mg==&hid=7&sn=ad5 ...

最新文章

  1. 【开源方案共享】无序点云快速的线段分割算法
  2. 页面滚动动态加载数据,页面下拉自动加载内容
  3. h5首页加载慢_H5网站好不好?
  4. js 浅拷贝直接赋值_第二十二篇 JS中浅拷贝的方法有哪些?
  5. CSS的六种垂直居中
  6. Unity网格合并_材质合并
  7. 巧用枚举CommandBehavior关闭SqlDataReader联接数据库时的conn.open状态
  8. 计算机网络课程设计之Tracert与Ping程序设计与实现
  9. ansile(2)模块之get_url
  10. 学习机器学习总体感受
  11. LVGL littlevgl使用
  12. android scrollview滚动条初始位置,ScrollView 设置滚动条的位置
  13. 姜小白的Python日记Day9 变量与递归
  14. poj 2536 Gopher II
  15. Review of 2012 Goal for 2013
  16. mac framework
  17. Efficient multi-keyword ranked query over encrypted data in cloud computing (6)
  18. Multi-view Harmonized Bilinear Network for 3D Object Recognition
  19. js 函数传参实参包含路径“\”处理
  20. MP3文件分析之ID3v2.3版本

热门文章

  1. “醒事”就在一瞬间 --- 一个真实的故事 (文PPT)
  2. SQL获取某个时间段的数据
  3. java遍历查询的某一列_【jQuery:遍历相同class的所有值,遍历某一列td的值】
  4. 单片机怎么跳出循环_自学单片机第二十七篇:矩阵按键的硬件测试
  5. oracle空格太多,Oracle Sql字符串多余空格处理方法初记
  6. java Date.getTime()返回负数异常情况分析
  7. Springboot 打包时引入外部jar
  8. c语言有重复元素全排列,【求助】全排列 不重复 由小到大 输出 代码
  9. oracle 状态unknown,解决Oracle crs_stat状态为UNKNOWN有关问题
  10. count(*)效率提高_想使用多线程来提高处理速度,却还不知道使用CountDownLatch与CyclicBarries?...