Verilog有限状态机(7)

HDLBits链接


前言

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


题库

Q3a:FSM

题目里说当s为0时,进入B状态,然后会检查w的值,如果在接下来的三个周期中w值恰好有两个周期都为1,那么z输出1,否则z输出0。注意,由示例的波形图看应该是不重叠检测。

Solution

module top_module (input clk,input reset,   // Synchronous resetinput s,input w,output z
);parameter S0 = 1'd0, S1 = 1'd1;reg current_state, next_state;reg [1:0] counter,num_one;always @(*) begincase(current_state)S0:     next_state = s ? S1 : S0;S1:     next_state = S1;default:next_state = S0;endcaseendalways @(posedge clk) beginif(reset)begincurrent_state <= S0;endelse begincurrent_state <= next_state;endendalways @(posedge clk) beginif(reset)begincounter <= 2'd0;endelse if(counter == 2'd2) begincounter <= 2'd0;endelse if(current_state == S1) begincounter <= counter + 1'd1;endendalways @(posedge clk) beginif(reset) beginnum_one <= 1'd0;endelse beginif(counter == 2'd0)beginnum_one <= w ? 1'd1 : 1'd0;endelse if(current_state == S1) beginnum_one <= w ? (num_one+1'd1) : num_one;end   end          endassign z = (current_state == S1 && num_one == 2'd2 && counter == 2'd0);endmodule

此题中我用了两个计数器来完成,需特别注意的是,在满足哪种条件时计数器清零或者自加,一定注意!

Q3b:FSM

该题让实现下图所示的状态机,其中复位信号让状态转移至000;

Solution

module top_module (input clk,input reset,   // Synchronous resetinput x,output z
);parameter S0 = 3'b000, S1 = 3'b001, S2 = 3'b010;parameter S3 = 3'b011, S4 = 3'b100;reg [2:0] current_state, next_state;always @(*) begincase(current_state)S0:     next_state = x ? S1 : S0;S1:     next_state = x ? S4 : S1;S2:     next_state = x ? S1 : S2;S3:     next_state = x ? S2 : S1;S4:     next_state = x ? S4 : S3;default:next_state = S0;endcaseendalways @(posedge clk) beginif(reset)begincurrent_state <= S0;endelse begincurrent_state <= next_state;endendalways @(posedge clk) beginif(reset)beginz <= 1'b0;endelse begincase(next_state)S3:     z <= 1'b1;S4:     z <= 1'b1;default:z <= 1'b0;endcaseend      endendmodule

心得:第三段状态机输出用到next_state做条件时,记得要将reset条件加上,因为reset影响的是current_state,不影响next_state

Q3c:FSM logic

由下面的状态转移表将Y[0]与z表示出来。

Solution

module top_module (input clk,input [2:0] y,input x,output Y0,output z
);assign Y0 = ((~y[2]&y[0])|(y==3'b100))&~x | (~y[2]&~y[0])&x;assign z = (y == 3'b011) | (y == 3'b100);endmodule

Q6b:FSM next-state logic

通过下面的状态转移表将Y[2]的下一状态表示出来,其中w为输入。

Solution

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

按上面我列的状态转移表将Y2表示出来即可。

Q6c:FSM one-hot next-state logic

这里使用了one-hot编码,将Y2和Y4输出出来,思路与上题类似,这里不再赘述。

Solution

module top_module (input [6:1] y,input w,output Y2,output Y4);assign Y2 = ~w & y[1];assign Y4 = (w & y[2])|(w & y[3])|(w & y[5])|(w & y[6]);endmodule

Q6:FSM

这是个正宗的FSM题,照着图列状态即可。

Solution

module top_module (input clk,input reset,     // synchronous 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 ? A : B;B:      next_state = w ? D : C;C:      next_state = w ? D : E;D:      next_state = w ? A : F;E:      next_state = w ? D : E;F:      next_state = w ? D : C;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

结语

今天先更新这几题吧,FSM这部分题目都算比较基础的题目,代码有不足之处还望指正,状态机部分的题终于要结束了。

HDLBits答案(20)_Verilog有限状态机(7)相关推荐

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

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

  2. HDLBits答案(21)_Verilog有限状态机(8)

    Verilog有限状态机(8) HDLBits链接 前言 今天继续更新状态机小节的习题. 题库 Q2a:FSM 正宗的FSM题,没啥说的,看图写代码. Solution: module top_mod ...

  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答案(13)_Verilog移位寄存器附加题

    Verilog移位寄存器 HDLBits链接 前言 今天更新一节内容,该小节题目不多,共三道,但技巧性挺强. 题库 题目描述1:各单元的下一状态是此时当前单元相邻两位的异或. 在这个电路中,创建一个5 ...

最新文章

  1. Python3学习笔记01-环境安装和运行环境
  2. python能做什么游戏ll-一个简单的python game游戏
  3. CVPR2020 | 参数量减半,北大开源全新高效空域转换模块,还原图像逼真细节
  4. boost::spirit模块实现允许调整模板数据的技巧结构作为融合序列以用于直接属性传播的测试程序
  5. 【译】ES2018 新特性: 正则表达式的 s (dotAll) 标志
  6. java 程序是由什么组成的 java_从零开始的JAVA -2. java程序的构成及命名规则
  7. 2021华为杯建模---总结
  8. 智能会议系统(18)---如何进行视频电话
  9. 指针与引用的混合使用总结
  10. Bootstrap-代码样例
  11. 留言板分页php,php留言板代码[经典的分页代码](1/4)
  12. SpringBoot整合jsp的使用
  13. java菜鸟教程+视频笔记
  14. linux 打开权限不够,linux无法打开目录提示权限不够
  15. GOPROXY:解决 go get golang.org/x 包失败
  16. 使用Python 批量转移*.tif和*.mov文件
  17. 有鱼上钩!修改游戏数据前的准备
  18. 不只是coding_不只是外表
  19. MAC终端下使用IDEA自带的Git拉取github项目,提示SSL_ERROR_SYSCALL in connection to XX
  20. Highcharts实现走势图

热门文章

  1. How is jsonModel.getProperty implemented
  2. how is SAP UI5 bindItems implemented
  3. Extension project: 404 Not Found for resources/cus/crm/notes/ext/Component-dbg.js
  4. after markup mount - how is converted source code got executed
  5. 阮一峰react demo代码研究的学习笔记 - demo 3 debug
  6. BSP application view instance lifetime analysis
  7. 如何使用JavaScript开发AR(增强现实)移动应用 (一)
  8. 巧用代理设计模式(Proxy Design Pattern)改善前端图片加载体验
  9. CRM和ERP的Sales Organization的映射关系
  10. java js隐藏_Javascript匿名函数是否仍然可见? (使用Java applet,这是一种隐藏JS代码的方法)...