当前位置:网站首页>Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)
Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)
2022-07-28 16:23:00 【别再出error了】
自动贩售机

题目描述:
设计一个自动贩售机,输入货币有三种,为0.5/1/2元,饮料价格是1.5元,要求进行找零,找零只会支付0.5元。
ps:投入的货币会自动经过边沿检测并输出一个在时钟上升沿到1,在下降沿到0的脉冲信号
注意rst为低电平复位
信号示意图:

d1 0.5元
d2 1元
d3 2元
out1 饮料
out2 零钱
这一题第一眼就想到的是状态机,所以先把状态图画出来:

看上去好像有点复杂了。。。 根据状态图写代码就简单很多了。
编译成功的代码详解如下:(但在状态变化那一段有疑惑)
//A:nstate <= (d1)? B:(d2)?C:(d3)?E:nstate;
//这里是不理解的一点,如果把末尾的nstate写成A会报错,out1会丢失一些值,但明明d1d2d3都为0时,就是保持A不变吧,nstate=A也没有问题啊应该。(看到答案才改成的nstate)
module seller1(
input wire clk ,
input wire rst ,
input wire d1 ,
input wire d2 ,
input wire d3 ,
output reg out1,
output reg [1:0]out2
);
//*************code***********//
//这一题应该用FSM来做
//声明7个状态
parameter A=0,B=1,C=2,D=3,E=4,F=5,G=6;
reg [2:0] state,nstate;
//逻辑变化
always @(*) begin
case(state)
A:nstate <= (d1)? B:(d2)?C:(d3)?E:nstate;
//这里是不理解的一点,如果把末尾的nstate写成A会报错,但明明d1d2d3都为0时,就是保持A不变吧,nstate=A也没有问题啊应该
B:nstate <= (d1)? C:(d2)?D:(d3)?F:nstate;
C:nstate <= (d1)? D:(d2)?E:(d3)?G:nstate;
D:nstate <= A;
E:nstate <= A;
F:nstate <= A;
G:nstate <= A;
default: nstate <= A;
endcase
end
//时序变化
always @(posedge clk or negedge rst)begin
if(!rst) state<=A;
else state<=nstate;
end
//输出判定
always @(*)begin
case(state) //有答案写的上升沿时才输出,并按nstate来进行输出判断
D: begin out1<=1;out2<=0; end
E: begin out1<=1;out2<=1; end
F: begin out1<=1;out2<=2; end
G: begin out1<=1;out2<=3; end
default: begin out1<=0;out2<=0; end
endcase
end
//*************code***********//
endmodule答案里还看到说用简单的寄存器来进行判定的,即用加法计数来判断,好像也是一种不错的思路,而且代码会简单很多。

边栏推荐
- valarray数值库学习
- Facet experience -- the development side of dragon game client
- Make full use of English
- The 16th program design competition of Dalian University of Technology (Problem Solver)
- Valarray Library Learning
- Easypoi --- excel file export
- 全链路灰度在数据库上我们是怎么做的?
- Analysis of kubernetes service principle
- Realize the reset function of steering wheel UI with touch rotation and finger departure in unity
- Games101 section 13 ray tracing notes
猜你喜欢

Goweb开发之Beego框架实战:第一节 Beego框架介绍

Atcoder regular contest 133 d.range XOR (digital dp+ classification discussion)

The maximum recommended number of rows for MySQL is 2000W. Is it reliable?

Message Passing for Complex Question Answering over Knowledge Graphs

Unity shader global fog effect

Problem solution of code heartstrings Junior Group (official competition) of Dalian University of Technology (Development Zone campus) in 2021

Unity shader realizes water wave effect with noise texture
![[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation](/img/4e/a41eee56fc0e8d3089f105bcb63155.png)
[deep learning]: day 1 of pytorch introduction to project practice: data operation and automatic derivation

Microservice Architecture - service registry and service gateway (6.8) (Reprint)

Re12: read these3 semantic self segmentation for abstract summary of long legal documents in low
随机推荐
SUSE Ceph 快速部署 – Storage6
Question making note 3 (two point search)
Exercise note 5 (square of ordered array)
Using MVC in the UI of unity
利用SQL Server代理作业对数据库进行定时还原
Unity shader texture animation
Analysis of kubernetes service principle
Valarray Library Learning
Add differential pairs and connections in Ad
Unity shader uses rendered texture to achieve glass effect
Problem solution of code heartstrings Junior Group (official competition) of Dalian University of Technology (Development Zone campus) in 2021
Technology sharing | MySQL shell customized deployment MySQL instance
Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
Leetcode 2022.04.10 China Merchants Bank special competition D. store promotion (DP)
充分利用----英文
System clock failure of database fault tolerance
Deep understanding of deepsea and salt deployment tools – storage6
Visual Studio 2012/2015发布Web应用连同.cs源码一起发布
Codeforces round 770 (Div. 2) e. fair share
influxdb2的使用