Verilog有限状态机(8)

HDLBits链接


前言

今天继续更新状态机小节的习题。


题库

Q2a:FSM

正宗的FSM题,没啥说的,看图写代码。

Solution

module top_module (input clk,input reset,   // Synchronous active-high resetinput w,output z
);parameter A = 3'd0, B = 3'd1, C = 3'd2;parameter D = 3'd3, E = 3'd4, F = 3'd5;reg [2:0] current_state, next_state;always @(*) begincase(current_state)A:      next_state = w ? B : A;B:      next_state = w ? C : D;C:      next_state = w ? E : D;D:      next_state = w ? F : A;E:      next_state = w ? E : D;F:      next_state = w ? C : D;default:next_state = A;endcaseendalways @(posedge clk) beginif(reset) begincurrent_state <= A;endelse begincurrent_state <= next_state;endendassign z = (current_state == E) | (current_state == F);endmodule

Q2b:One-hot FSM equations

状态使用独热编码,将状态中的y[1]和y[3]表示出来。

Solution

module top_module (input [5:0] y,input w,output Y1,output Y3
);assign Y1 = y[0] & w;assign Y3 = (y[1] | y[2] | y[4] | y[5]) & ~w;endmodule

Q2a:FSM

本题是用状态机实现一个判优器。其中r1、r2、r3分别表示三种设备的request,g1、g2、g3表示资源的分配情况。由下面的状态图可见,设备1,2,3的优先级依次递减。当设备请求到资源时,需等其完成任务才能释放资源。

Solution

module top_module (input clk,input resetn,    // active-low synchronous resetinput [3:1] r,   // requestoutput [3:1] g   // grant
);parameter A = 2'd0, B = 2'd1;parameter C = 2'd2, D = 2'd3;reg [1:0] current_state, next_state;always @(*) begincase(current_state)A:beginif(r[1] == 1'b1) beginnext_state = B;endelse if(r[2] == 1'b1) beginnext_state = C;endelse if(r[3] == 1'b1) beginnext_state = D;endelse beginnext_state = A;endendB:       next_state = r[1] ? B : A;C:       next_state = r[2] ? C : A;D:       next_state = r[3] ? D : A;default: next_state = A;endcaseendalways @(posedge clk) beginif(~resetn) begincurrent_state <= A;endelse begincurrent_state <= next_state;endendassign g[1] = current_state == B;assign g[2] = current_state == C;assign g[3] = current_state == D;endmodule

Q2b:Another FSM

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

Solution

module top_module (input clk,input resetn,    // active-low synchronous resetinput x,input y,output f,output g
); parameter IDLE = 4'd0, FOUT = 4'd1, S1 = 4'd2;parameter S2 = 4'd3, S3 = 4'd4, S4 = 4'd5;parameter S5 = 4'd6, ALL_ONE = 4'd7, ALL_ZERO = 4'd8;reg [3:0] current_state, next_state;always @(*) begincase(current_state)IDLE:       next_state = FOUT;FOUT:       next_state = S1;S1:         next_state = x ? S2 : S1;S2:         next_state = x ? S2 : S3;S3:         next_state = x ? S4 : S1;S4:         next_state = y ? ALL_ONE : S5;S5:         next_state = y ? ALL_ONE : ALL_ZERO;ALL_ONE:    next_state = ALL_ONE;ALL_ZERO:   next_state = ALL_ZERO;default:    next_state = IDLE;endcaseendalways @(posedge clk) beginif(~resetn) begincurrent_state <= IDLE;endelse begincurrent_state <= next_state;endendassign f = current_state == FOUT;assign g = current_state == S4 | current_state == S5 | current_state == ALL_ONE;endmodule

心得:状态机设计的好坏决定代码的难易程度,在画状态转移图时可以多留点时间。

结语

状态机这部分题目终于更新完了,自己在做这些题目的过程中收获也非常大,很感谢该网站的作者。如果代码哪里有误,请大家指正,评论区也欢迎大家交流不同的解题思路。

HDLBits答案(21)_Verilog有限状态机(8)相关推荐

  1. HDLBits答案(18)_Verilog有限状态机(5)

    Verilog有限状态机(5) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 题目描述1: 第一道题目比较容易,题目中的in信号包含了一个起始位(0),8个数据位和一个停止位(1), ...

  2. HDLBits答案(20)_Verilog有限状态机(7)

    Verilog有限状态机(7) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 Q3a:FSM 题目里说当s为0时,进入B状态,然后会检查w的值,如果在接下来的三个周期中w值恰好有两个 ...

  3. HDLBits答案(19)_Verilog有限状态机(6)

    Verilog有限状态机(6) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 Fsm hdlc 同步帧检测涉及对数据的连续位流进行解码,以寻找指示帧(数据包)开始和结束的位模式. 6 ...

  4. HDLBits答案(17)_Verilog有限状态机(4)

    Verilog有限状态机(4) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 题目描述1: One-hot FSM 独热编码,根据状态转移图输出下一状态与结果. Solution1: ...

  5. HDLBits答案(16)_Verilog有限状态机(3)

    Verilog有限状态机(3) HDLBits链接 前言 今天继续更新状态机小节的习题,本章主要编写Lemmings Game部分. 题库 题目描述10: Game Lemmings1:有个小人左右走 ...

  6. HDLBits答案(15)_Verilog有限状态机(2)

    Verilog有限状态机(2) HDLBits链接 前言 继续更新状态机小节的习题. 题库 题目描述6: Solution6: module top_module(input in,input [3: ...

  7. HDLBits答案(14)_Verilog有限状态机(1)

    Verilog有限状态机(1) HDLBits链接 前言 今天来到了重要的部分:状态机.对该部分内容,可能不会一次更新一个小节:一方面是题目难度,另一方面是代码量过大:所以该节会分批更新,大家见谅. ...

  8. HDLBits答案(12)_Verilog移位寄存器

    Verilog移位寄存器 HDLBits链接 前言 今天更新一节寄存器相关内容,其中涉及CRC校验的内容是用线性反馈移位寄存器搭建而成的. 题库 题目描述1: 构建一个4bit的移位寄存器(右移),含 ...

  9. HDLBits答案(7)_Verilog多路选择器

    Verilog多路选择器 HDLBits链接 定义 多路选择器(Multiplexer)简称多路器,它是一个多输入.单输出的组合逻辑电路,在数字系统中有着广泛的应用.它可以根据地址码(选择码)的不同, ...

最新文章

  1. apache 不执行PHP,显示代码
  2. 中国已经过了做手机操作系统的窗口期
  3. 【Python-ML】非线性映射降维-KPCA方法
  4. 静态方法-应用场景和定义方式
  5. [转帖]解密微软中间语言MSIL之调试程序(1)
  6. spring boot 配置
  7. HDU-3987 Harry Potter and the Forbidden Forest(最大流)
  8. 主梁弹性模量计算_简支梁计算方法
  9. idea打包jar包,运行后显示 没有主清单属性
  10. XSS及CSRF攻击防御
  11. 扫地机器人漫谈(一):扫地机的形状
  12. 推荐一款自带很好用的桌面便签
  13. Java中的四种Reference
  14. 如何解决收到网监大队信息系统安全等级保护限期整改通知书
  15. clear 方法的解释及用法
  16. tftp 在嵌入式设备和主机之间传输文件
  17. python的“end=”介绍
  18. postgresql + postgis 离线安装
  19. python 往mysql数据库存储照片
  20. podcast什么意思php,什么是podcast?

热门文章

  1. Organization model change - product line item EC
  2. CDATA and comment
  3. Marketing Cloud tile的semantic信息
  4. ABAP并发计算的一个实例
  5. X3C to X9T
  6. /UI5/IF_UI5_REP_PERSISTENCE - why I cannot deploy app to GM6
  7. winrar命令行的一些参数例子
  8. SAP订单编排和流程增强概述
  9. 使用ABAP Push Channel(APC)开发的乒乓球游戏,可双打
  10. c语言 编程 牛顿迭代,C语言编写牛顿迭代法的跟踪