HDLBits Lemmings1-4

Lemming1

网址:https://hdlbits.01xz.net/wiki/Lemmings1

The game Lemmings involves critters with fairly simple brains. So simple that we are going to model it using a finite state machine.

In the Lemmings’ 2D world, Lemmings can be in one of two states: walking left or walking right. It will switch directions if it hits an obstacle. In particular, if a Lemming is bumped on the left, it will walk right. If it’s bumped on the right, it will walk left. If it’s bumped on both sides at the same time, it will still switch directions.

Implement a Moore state machine with two states, two inputs, and one output that models this behaviour.

两个状态,向左向右,向左时遇见左障碍就转变方向,向右时遇见右障碍转向。

module top_module(input clk,input areset,    // Freshly brainwashed Lemmings walk left.input bump_left,input bump_right,output walk_left,output walk_right); //  parameter LEFT=0, RIGHT=1;reg state, next_state;always @(*) begin// State transition logiccase(state)LEFT: next_state=bump_left?RIGHT:LEFT;RIGHT: next_state=bump_right?LEFT:RIGHT;endcaseendalways @(posedge clk, posedge areset) begin// State flip-flops with asynchronous resetif(areset) state<=LEFT;else state<=next_state;end// Output logicassign walk_left = (state == LEFT);assign walk_right = (state == RIGHT);endmodule

Lemming2

网址:https://hdlbits.01xz.net/wiki/Lemmings2

See also: Lemmings1.

In addition to walking left and right, Lemmings will fall (and presumably go “aaah!”) if the ground disappears underneath them.

In addition to walking left and right and changing direction when bumped, when ground=0, the Lemming will fall and say “aaah!”. When the ground reappears (ground=1), the Lemming will resume walking in the same direction as before the fall. Being bumped while falling does not affect the walking direction, and being bumped in the same cycle as ground disappears (but not yet falling), or when the ground reappears while still falling, also does not affect the walking direction.

Build a finite state machine that models this behaviour.

在1的基础上多了坠落状态,且在掉落时方向与碰撞无关,因为要记录掉落前的方向,所以分为fl,fr(向左掉落,向右掉落)两个状态。

module top_module(input clk,input areset,    // Freshly brainwashed Lemmings walk left.input bump_left,input bump_right,input ground,output walk_left,output walk_right,output aaah ); parameter wl=0,wr=1,fl=2,fr=3;reg [1:0] state,next_state;always @(posedge clk or posedge areset) beginif(areset) state<=wl;else state<=next_state;endalways @(*) begincase(state)wl: next_state=ground?(bump_left?wr:wl):fl;wr: next_state=ground?(bump_right?wl:wr):fr;fl: next_state=ground?wl:fl;fr: next_state=ground?wr:fr;endcaseendalways @(*) begincase(state)wl: begin walk_left=1;walk_right=0;aaah=0; endwr: begin walk_left=0;walk_right=1;aaah=0; endfl: begin walk_left=0;walk_right=0;aaah=1; endfr: begin walk_left=0;walk_right=0;aaah=1; endendcaseend
endmodule

Lemming3

网址:https://hdlbits.01xz.net/wiki/Lemmings3

See also: Lemmings1 and Lemmings2.

In addition to walking and falling, Lemmings can sometimes be told to do useful things, like dig (it starts digging when dig=1). A Lemming can dig if it is currently walking on ground (ground=1 and not falling), and will continue digging until it reaches the other side (ground=0). At that point, since there is no ground, it will fall (aaah!), then continue walking in its original direction once it hits ground again. As with falling, being bumped while digging has no effect, and being told to dig when falling or when there is no ground is ignored.

(In other words, a walking Lemming can fall, dig, or switch directions. If more than one of these conditions are satisfied, fall has higher precedence than dig, which has higher precedence than switching directions.)

Extend your finite state machine to model this behaviour.

在2的基础上多了dig状态,且dig状态也与方向有关,用两个状态分别表示左右,在dig状态时碰撞不影响方向。

module top_module(input clk,input areset,    // Freshly brainwashed Lemmings walk left.input bump_left,input bump_right,input ground,input dig,output walk_left,output walk_right,output aaah,output digging ); reg [2:0] state,next_state;parameter wl=0,wr=1,dl=2,dr=3,fl=4,fr=5;always @(posedge clk or posedge areset) beginif(areset) state<=wl;else state<=next_state;endalways @(*) begincase(state)wl: beginwalk_left=1;walk_right=0;aaah=0;digging=0;next_state=(ground)?(dig?dl:(bump_left?wr:wl)):fl;endwr: beginwalk_left=0;walk_right=1;aaah=0;digging=0;next_state=(ground)?(dig?dr:(bump_right?wl:wr)):fr;enddl: beginwalk_left=0;walk_right=0;aaah=0;digging=1;next_state=(ground)?dl:fl;enddr: beginwalk_left=0;walk_right=0;aaah=0;digging=1;next_state=(ground)?dr:fr;endfl: beginwalk_left=0;walk_right=0;aaah=1;digging=0;next_state=(ground)?wl:fl;endfr: beginwalk_left=0;walk_right=0;aaah=1;digging=0;next_state=(ground)?wr:fr;endendcaseend
endmodule

Lemming4

网址:https://hdlbits.01xz.net/wiki/Lemmings4

See also: Lemmings1, Lemmings2, and Lemmings3.

Although Lemmings can walk, fall, and dig, Lemmings aren’t invulnerable. If a Lemming falls for too long then hits the ground, it can splatter. In particular, if a Lemming falls for more than 20 clock cycles then hits the ground, it will splatter and cease walking, falling, or digging (all 4 outputs become 0), forever (Or until the FSM gets reset). There is no upper limit on how far a Lemming can fall before hitting the ground. Lemmings only splatter when hitting the ground; they do not splatter in mid-air.

Extend your finite state machine to model this behaviour.

在3的基础上增加了掉落时间超过20则口口,需要加入计时器,口口后在复位之前所有输出为零。

本题容易出错的点是 计时cnt位宽太少,计时超过位数导致cnt<20,进入错误状态。

module top_module(input clk,input areset,    // Freshly brainwashed Lemmings walk left.input bump_left,input bump_right,input ground,input dig,output walk_left,output walk_right,output aaah,output digging ); reg [2:0] state,next_state;parameter wl=0,wr=1,dl=2,dr=3,fl=4,fr=5,splat=6;reg [8:0] cnt;always @(posedge clk or posedge areset) beginif(areset) state<=wl;else state<=next_state;endalways @(posedge clk or posedge areset) beginif(areset) cnt<=0;else if(state==fl|state==fr)cnt<=cnt+1;else cnt<=0;endalways @(*) begincase(state)wl: next_state=ground?(dig?dl:(bump_left?wr:wl)):fl;wr: next_state=ground?(dig?dr:(bump_right?wl:wr)):fr;dl: next_state=ground?dl:fl;dr: next_state=ground?dr:fr;fl: next_state=ground?(cnt>=20?splat:wl):fl;fr: next_state=ground?(cnt>=20?splat:wr):fr;splat: next_state=splat;endcaseendalways @(*) begincase(state)wl: begin walk_left=1;walk_right=0;aaah=0;digging=0; endwr: begin walk_left=0;walk_right=1;aaah=0;digging=0; enddl: begin walk_left=0;walk_right=0;aaah=0;digging=1; enddr: begin walk_left=0;walk_right=0;aaah=0;digging=1; endfl: beginwalk_left=0;walk_right=0;aaah=1;digging=0;endfr: beginwalk_left=0;walk_right=0;aaah=1;digging=0;endsplat: begin walk_left=0;walk_right=0;aaah=0;digging=0; endendcaseend
endmodule

HDLBits Lemmings1-4相关推荐

  1. lemming games 1!! hdlbits

    hdlbits Lemmings1 旅鼠游戏有四个系列 系列1: module top_module( input clk, input areset, // Freshly brainwashed ...

  2. HDLBITS笔记34:Lemmings1、Lemmings2、Lemmings3、Lemmings4

    目录 题目1:Lemmings1 题目2:Lemmings2 题目3:Lemmings3 题目4: Lemmings4 题目1:Lemmings1 游戏Lemmings涉及具有相当简单大脑的小动物.如 ...

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

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

  4. HDLBits学习------Problem 127~130

    参考链接:HDLBits导学 Problem 127 Lemmings1         问题:游戏旅鼠涉及到有非常简单的大脑的生物,我们将会使用有限状态机(FSM)对其建模. 在旅鼠的2D世界中,旅 ...

  5. 【verilog学习23】HDLBits:Circuits_Sequential Logic_Finite State Machines

    [HDLBits]Circuits_Sequential Logic_Finite State Machines I FSM 1 (asynchronous reset) (Fsm1) 1.代码编写 ...

  6. verilog练习:hdlbits网站上的做题笔记(6)

    前言 之前的文章<如何学习verilog,如何快速入门?>中提到了verilog学习,推荐了一个可以练习的网站:hdlbits网站,那自己也玩玩这个网站. 这篇文章,是接着<veri ...

  7. 【HDLBits 刷题 10】Circuits(6)Finite State Manchines 10-17

    目录 写在前面 Finite State Manchines Lemmings1 Lemmings2 Lemmings3 Lemmings4 Fsm onehot Fsm ps2 Fsm ps2dat ...

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

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

  9. HDLBits 系列(40)如何写 TestBench 文件?

    目录 序言 变量定义 时钟设计 设计输入 模块例化 实战演练 序言 由于入门的测试文件很简单,所以一直以来也都是直接给出测试文件,直到今天才想着去总结一个测试文件的写法.这篇博客将根据HDLBits的 ...

最新文章

  1. 8月第1周安全回顾 0Day漏洞成企业最大威胁 应重视网络监听
  2. ps一点等于多少厘米_50寸液晶电视尺寸是多少
  3. ctf(pwn) canary保护机制讲解 与 解密方法介绍
  4. 业务总结004:检验项目时间轮实践与库存实现方案
  5. 2020ICPC(上海) - Walker(分类讨论+二分)
  6. TC的文件拷贝/移动
  7. arduino 土壤温湿度传感器_嫌arduino太贵?太大?试试ATTINY85!DIY温湿度计入门级教程...
  8. centos7下Jenkins管理员admin密码忘记后处理方法
  9. 中国可用的 BT Tracker 服务器列表,每24小时自动更新,去重,自动检测可用性
  10. VLOOKUP多条件匹配
  11. php max file uploads,php上传多文件max_file_uploads限制问题
  12. 乐鑫科技推出基于 ESP32-C3 的 Wi-Fi 单火线智能开关方案
  13. 【Nginx】503 Service Temporarily Unavailable
  14. 深入理解Java虚拟机——Parallel Scavenge收集器
  15. C语言报错:a label can only be part of a statement and a declaration is not a statement
  16. 密西西比河谷州立大学:Android应用程序开发(二)
  17. 河南省周口市安吉软件测试培训中心第一次软件测试课程——软件测试期末考试(含答案)
  18. vue v-model 双向绑定表单元素的数据:实质是绑定了value、checked、selected属性
  19. Java基础:volatile详解
  20. nmap简单实用命令

热门文章

  1. 最安全的邮箱-Gmail
  2. 2021美团杯CTF ez-sql
  3. Prometheus+Grafana
  4. 姬魔恋战纪服务器维护,姬魔恋战纪闪退、进不去、黑屏不能玩的原因和解决办法[图]...
  5. 移动端手机调试的方法
  6. python 爬虫-京东用户评论数据和用户评分
  7. s60v5用java qq_s60v5 qq-QQ版本下载-kukud.net手机qq下载站
  8. 计算机桌面锁定怎么解除,电脑屏幕被锁定怎么解锁_电脑锁定屏幕如何取消-win7之家...
  9. echarts 清除上一次的实例
  10. xshell6 强制更新的问题(转载、亲测有效)