当前位置:网站首页>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答案里还看到说用简单的寄存器来进行判定的,即用加法计数来判断,好像也是一种不错的思路,而且代码会简单很多。

边栏推荐
- Re10: are we really making much progress? Revisiting, benchmarking, and refining heterogeneous gr
- Easypoi --- excel file export
- 微服务架构-服务注册中心和服务网关(6.8) (转载)
- Modeling Semantics with Gated Graph Neural Networks for KBQA
- 一文了解 Kubernetes 中的服务发现
- 技术分享 | 误删表以及表中数据,该如何恢复?
- Fine-grained Fact Verification with Kernel GA Network
- 2022 Niuke multi school second CDE
- The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
- SUSE CEPH rapid deployment – storage6
猜你喜欢

Educational codeforces round 126 (rated for Div. 2) f.teleporters (two sets and two points)

Unity shader transparent effect

Easypoi --- excel file export

线性代数及矩阵论(九)

Unity shader realizes water wave effect with noise texture

valarray数值库学习

总数据量超万亿行,玉溪卷烟厂通过正确选择时序数据库轻松应对

如何在构建阶段保护镜像安全

Fine-grained Fact Verification with Kernel GA Network

mysql 最大建议行数2000w,靠谱吗?
随机推荐
Visual Studio 2012/2015发布Web应用连同.cs源码一起发布
After paying $1.8 billion in royalties to Qualcomm, Huawei reportedly ordered 120million chips from MediaTek! Official response
Games101 section 13 ray tracing notes
Leetcode 2022.04.10 China Merchants Bank special competition D. store promotion (DP)
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
利用SQL Server代理作业对数据库进行定时还原
Unity3d simple implementation of water surface shader
How to use fail2ban to protect WordPress login page
Analysis of browser decoding process
Atcoder beginer contest 240 g.reporting Takahashi (classical problems of Combinatorial Mathematics)
HTAP comes at a price
Self study examination in April 2021
Some attention code explanations
Re11: read EPM legal judgment prediction via event extraction with constraints
Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
Difference between reconnaissance aircraft and early warning aircraft
Facet experience -- the development side of dragon game client
Deep understanding of deepsea and salt deployment tools – storage6
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
Record the processing process of CEPH two RBDS that cannot be deleted