目录

背景

原题复现

我的方案

状态转移图

我的设计

更新方案

FPGA/IC群推荐



背景

这是这个系列中的一个状态机的题目,但是相比于给了你完整状态转移图之类的题目,这个题目还是稍微有点难的,我实在不知道该怎么给这个博客起个什么名字?

我在线等一个简单的方式去解决今天的问题,而如题所说,我用最无能的方式来解决这个问题,但简单的方式一定存在。

2019/12/16更新

今天一个帅兄弟给了我一个答案,很巧妙,这里十分感谢。我把它放到后面来给出。

原题复现

原题复现:

Consider a finite state machine with inputs s and w. Assume that the FSM begins in a reset state called A, as depicted below. The FSM remains in state A as long as s = 0, and it moves to state B when s = 1. Once in state B the FSM examines the value of the input w in the next three clock cycles. If w = 1 in exactly two of these clock cycles, then the FSM has to set an output z to 1 in the following clock cycle. Otherwise z has to be 0. The FSM continues checking w for the next three clock cycles, and so on. The timing diagram below illustrates the required values of z for different values of w.

Use as few states as possible. Note that the s input is used only in state A, so you need to consider just the w input.

我的方案

状态转移图

我的解决方案,有点无奈,多加了一些状态:

我的设计

根据此状态转移图给出我的设计:

module top_module (input clk,input reset,   // Synchronous resetinput s,input w,output z
);localparam A = 0, B = 1, S0 = 3, S1 = 4, S2 = 5, S3 = 6, S4 = 7, S5 = 8, S6 = 9, S7 = 10, S8 = 11, S9 = 12, S10 = 13, S11 = 14;reg [3:0] state, next_state;always@(*) begincase(state)A: beginif(s) next_state = B;else next_state = A;endB: beginif(w) next_state = S1;else next_state = S0;endS0: beginif(w) next_state = S2;else next_state = S4;endS1: beginif(w) next_state = S9;else next_state = S6;endS2: beginif(w) next_state = S3;else next_state = S5;endS3: beginif(w) next_state = S1;else next_state = S0;endS4: beginnext_state = S5;endS5: beginif(w) next_state = S1;else next_state = S0;endS6: beginif(w) next_state = S7;else next_state = S8;endS7: beginif(w) next_state = S1;else next_state = S0;endS8: beginif(w) next_state = S1;else next_state = S0;endS9: beginif(w) next_state = S11;else next_state = S10;endS10: beginif(w) next_state = S1;else next_state = S0;endS11: beginif(w) next_state = S1;else next_state = S0;enddefault: beginnext_state = A;endendcaseendalways@(posedge clk) beginif(reset) state <= 0;else state <= next_state;endassign z = (state == S10 || state == S7 || state == S3) ? 1 : 0;endmodule

测试成功。


更新方案

今天群里的大佬给了一种简单的方法,状态转移图确实简单了,但是理解起来呢?

我之前用的方案是在B之后的三个周期内,列举w的值,取值情况有8种,然后添加更多的状态去解决这个问题,不得不说状态转移图看起来复杂很多,也绝对不是推荐的方案。

我等待的确实是今天的这个方案,通过计数,不需要添加更多的状态,这也是我一开始就想用的方法,只是计数的时序当时没有搞定,今天很感谢,群里的一位兄弟。

直接给出设计,通过代码就应该能看出来设计的思路:

module top_module (input clk,input reset,   // Synchronous resetinput s,input w,output z
);parameter A = 1'b0, B = 1'b1;reg current_state;reg next_state;always@(posedge clk)beginif(reset)begincurrent_state <= A;endelse begincurrent_state <= next_state;endendalways@(*)begincase(current_state)A:beginnext_state = s ? B : A;endB:beginnext_state = B;endendcaseendreg w_reg1;reg w_reg2;always@(posedge clk)beginif(reset)beginw_reg1 <= 1'b0;w_reg2 <= 1'b0;endelse if(next_state == B)beginw_reg1 <= w;w_reg2 <= w_reg1;endelse beginw_reg1 <= 1'b0;w_reg2 <= 1'b0;endendalways@(posedge clk)beginif(reset)beginz <= 1'b0;endelse if(next_state == B && counter == 2'd0)beginif(~w & w_reg1 & w_reg2 | w & ~w_reg1 & w_reg2 | w & w_reg1 & ~w_reg2)beginz <= 1'b1;endelse beginz <= 1'b0;endendelse beginz <= 1'b0;endend   reg [1:0] counter;always@(posedge clk)beginif(reset)begincounter <= 2'd0;endelse if(counter == 2'd2)begincounter <= 2'd0;endelse if(next_state == B)begincounter <= counter + 1'b1;endendendmodule

巧妙之处在于将输入w延迟两拍之后进行判断,如果有两个w为1,则在下一个周期将输出z置位.

有的朋友,也许在状态机的设计中,习惯将第三段使用组合逻辑来实现,这个题目的第三段也可以使用组合逻辑,但是呢?确实也没有必要,因为状态机的第三段本身既可以使用组合逻辑,也可以使用时序逻辑,如果使用时序逻辑。

下面给出组合逻辑的方案:

module top_module (input clk,input reset,   // Synchronous resetinput s,input w,output reg z
);parameter A = 1'b0, B = 1'b1;reg current_state;reg next_state;always@(posedge clk)beginif(reset)begincurrent_state <= A;endelse begincurrent_state <= next_state;endendalways@(*)begincase(current_state)A:beginnext_state = s ? B : A;endB:beginnext_state = B;endendcaseendreg w_reg1;reg w_reg2;always@(posedge clk)beginif(reset)beginw_reg1 <= 1'b0;w_reg2 <= 1'b0;endelse if(next_state == B)beginw_reg1 <= w;w_reg2 <= w_reg1;endelse beginw_reg1 <= 1'b0;w_reg2 <= 1'b0;endendreg z_mid;always@(*)beginif(reset)beginz_mid <= 1'b0;endelse if(current_state == B && counter == 2'd0)beginif(~w & w_reg1 & w_reg2 | w & ~w_reg1 & w_reg2 | w & w_reg1 & ~w_reg2)beginz_mid <= 1'b1;endelse beginz_mid <= 1'b0;endendelse beginz_mid <= 1'b0;endendalways@(posedge clk)beginif(reset)beginz <= 1'b0;endelse beginz <= z_mid;endendreg [1:0] counter;always@(posedge clk)beginif(reset)begincounter <= 2'd0;endelse if(counter == 2'd2)begincounter <= 2'd0;endelse if(next_state == B)begincounter <= counter + 1'b1;endendendmodule

只需要将第三段改为组合逻辑,但是输出需要延迟一拍,为什么?看时序图。

FPGA/IC群推荐

IC/FPGA 技术交流

有幸在这个群里认识了诸多俊杰,让我欣慰。

HDLBits 系列(38)值得一看的状态机设计题目相关推荐

  1. 干货!2018年你值得一看的网页设计作品集赏析

    网页设计作品集=门面+能力 网页设计作品集对网页设计师而言,既是网页门面,也是个人专业素养的体现.那么在作品集设计上万不能掉以轻心.无论是制作一份简约大方还是极具表现力的精良作品集,设计师们都必须付出 ...

  2. HDLBits 系列(0)专题目录

    本篇博文是近来总结HDLBits系列的目录,点击蓝色字体即可进入查看具体内容. HDLBits 系列(1)从HDLBits中获取灵感,整顿自己,稳步前行 HDLBits 系列(2)如何避免生成锁存器? ...

  3. HDLBits 系列(44)状态机补录

    文章目录 前言 原题复现 题目解析 状态转移图 设计文件 前言 今天补一个状态机的题目,也是这个系列的题目之一,但是由于之前对题目有点疑惑,今天得到博友反馈,让我明白了这个题目的意思,记录一下. 原题 ...

  4. [转]大三下,我们该做什么?一篇被转万次的日志,你值得一看

    大三下,我们该做什么?一篇被转万次的日志,你值得一看 还有几个月,你就不得不参加考研.就业大军了,你做好准备了么?你知道211学校.985学校和非211.985的主要区别么?找工作机会一定是不均等的. ...

  5. 爬取了 48048 条评论数据,解读 9.3 分的《毒液》是否值得一看?

    微信改版,加星标不迷路! 9.3 分的<毒液>是否值得一看? 作者 | Ryan 公众号 | 格雷福斯 编辑 | 阿广 概述 前言 获取数据 数据可视化 用户评论,词云图 毒液中六大搞笑台 ...

  6. 高级程序员值得一看的33本编程书籍

    作为一名程序员,编程语言只是基础,只是工具,想实现从程序员到高级工程师的进步,需要花更多的精力在底层原理,算法,数据结构,编程思想上.推荐了33本高级程序员值得一看的书籍,注重底层知识,思想,用空的时 ...

  7. 大三下,我们该做什么?一篇被转万次的日志,你值得一看

    大三下,我们该做什么?一篇被转万次的日志,你值得一看 还有几个月,你就不得不参加考研.就业大军了,你做好准备了么?你知道211学校.985学校和非211.985的主要区别么?找工作机会一定是不均等的. ...

  8. 爬取了 36141 条评论数据,解读 9.5 分的《海王》是否值得一看

    这是第一个python项目,之前看到了<爬取了 48048 条评论数据,解读 9.3 分的<毒液>是否值得一看?>这篇文章,一直想自己动手做一个,刚刚好前两天看了<海王& ...

  9. 值得一看的电脑教程下载

    -=-=-=-=-=> [值得一看的电脑教程下载] 电脑技巧二○○三合集 http://www.starinfo.net.cn/photography/gif/f6/shuji/computer ...

最新文章

  1. 技术图文:如何爬取一个地区的气象数据(下)?
  2. 奇偶个数_只愿与一人十指紧扣_新浪博客
  3. Swift 中枚举、结构体、类(enum、struct、class)
  4. C++学习之路 | PTA(天梯赛)—— L2-010 排座位 (25分)(带注释)(并查集)(精简)
  5. mono for android mysql_mono for android 自定义titleBar Actionbar 顶部导航栏 修改 样式 学习...
  6. VSCode如何关闭右侧预览功能 - 截图示下
  7. 非空验证方法(多值)和BindingResult提示验证信息
  8. 计算n的阶乘以及n个阶乘相加
  9. linux定时备份前一天,linux定时备份
  10. 201521460005 实验五
  11. erlang四大behaviour简述
  12. 用 Python 计算综合测评中的专业成绩加权平均分
  13. 程炳皓:关于技术领导力,十个耸人听闻的观点
  14. YARN 作业执行流程
  15. java ppt转换为html5,Apache POI PPT - PPT转换为图片
  16. django-连接Oracle数据库
  17. 焦虑是学习的最大障碍
  18. 通过struts提供的源码生成chw帮助文档
  19. PHP获取跳转后的真实地址
  20. PPT:智能工厂的智能物流规划方案

热门文章

  1. [转帖]一位“鬼佬”总经理的管理艺术
  2. VMware Server 2.0简单学习!
  3. jquery mysql jsp搜索功能_实现搜索框自动提示功能(jquery+php)
  4. android 实现表格横向混动_Flutter混合开发和Android动态更新实践
  5. ubuntu 安装GPU黑屏 修改GRUB_Ubuntu安装mysql后修改用户名和密码
  6. 华硕服务器如何安装系统安装win7系统,华硕电脑怎么重新安装win7系统
  7. 建立UDP到虚拟串口的映射软件 : USR-VCOM,并应用到WiFi调试ESP32
  8. 疑邻盗斧 - 杭电节能信标限流争论
  9. 修改ESP8266-01S MicroPython下载固件,看是否能够烧录程序
  10. SP-1CL3 陶瓷接收管 光电接收二极管 红外线接收管