当前位置: 首页 > article >正文

「Verilog学习笔记」自动贩售机1

专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

自动贩售机中可能存在的几种金额:0,0.5,1,1.5,2,2.5,3。然后直接将其作为状态机的几种状态,并根据投币面额确定状态转移。

需要注意的是:根据时序图,可以发现在找零时,out2输出的结果是找零数额的两倍,即找零0.5应输出1,找零1应输出2,以此类推。

`timescale 1ns/1ns
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***********//
	parameter S0 = 0, S0_5 = 1, S1 = 2, S1_5 = 3, S2 = 4, S2_5 = 5, S3 = 6 ; 
	reg [2:0] state, nstate ;

	always @ (posedge clk or negedge rst) begin 
		if (~rst) state <= S0 ; 
		else state <= nstate ; 
	end

	always @ (*) begin 
		case (state) 
			S0 : nstate = d1 ? S0_5 : d2 ? S1 : d3 ? S2 : nstate ;
			S0_5 : nstate = d1 ? S1 : d2 ? S1_5 : d3 ? S2_5 : nstate ; 
			S1 : nstate = d1 ? S1_5 : d2 ? S2 : d3 ? S3 : nstate ; 
			S1_5, S2, S2_5, S3 : nstate = S0 ; 
			default : nstate = S0 ; 
		endcase 
	end

	always @ (*) begin 
		if (~rst) out1 <= 'd0 ; 
		else out1 <= state == S1_5 || state == S2 || state == S2_5 || state == S3 ; 
	end

	always @ (*) begin 
		if (~rst) out2 <= 'd0 ; 
		else 
			case (state) 
				S0, S0_5, S1, S1_5 : out2 <= 1'd0 ; 
				S2 : out2 <= 1'd1 ; 
				S2_5 : out2 <= 2'd2 ; 
				S3 : out2 <= 2'd3 ; 
				default : out2 <= 'd0 ; 
			endcase
	end 	

//*************code***********//
endmodule

http://www.kler.cn/a/153700.html

相关文章:

  • MySQL索引有哪些优缺点
  • Redis Cluster
  • Vue 3.0 响应性 基础
  • 原生video设置控制面板controls显示哪些控件
  • 迭代器与生成器
  • ESP32-Web-Server编程- 通过滑动条向 Web 提交数据
  • vue3 element-plus el-table表头冻结,表头吸顶
  • UiPath:人工智能和重新加速增长是 2024 年的好兆头
  • GoLong的学习之路,进阶,微服务之原理,RPC
  • 使用纯js码2个实用功能banner图标切换和表格制作
  • 【Python标准库】json
  • 【Android】Window和WindowManager
  • UData+StarRocks在京东物流的实践 | 京东物流技术团队
  • 快照读通过MVCC解决不可重复读当前读通过间隙锁解决幻读
  • Linux破解用户密码【基于redhat9】
  • 电子印章管理系统:是什么、3个平台推荐
  • Vulhub-信息泄露
  • 数据结构 / 队列 / 循环队列 / 概念
  • Redis基本命令
  • SQL-分页查询offset的用法