牛客网Verilog刷题——VL39

牛客网Verilog刷题——VL39

题目

  设计一个自动贩售机,输入货币有两种,为0.5/1元,饮料价格是1.5/2.5元,要求进行找零,找零只会支付0.5元。

1、投入的货币会自动经过边沿检测并输出一个在时钟上升沿到1,在下降沿到0的脉冲信号

2、此题忽略出饮料后才能切换饮料的问题

  注意rst为低电平复位。

  信号示意图:
在这里插入图片描述

  输入输出描述:

信号类型输入/输出位宽描述
clkwireInput1系统时钟信号
rstwireInput1异步复位信号,低电平有效
d1wireInput1投入0.5元
d2wireInput1投入1元
selwireInput1选择饮料,0表示选择1.5元饮料,1表示选择2.5元饮料
out1regOutput1输出饮料1
out2regOutput1输出饮料2
out3regOutput2找零,只找零0.5元

答案

`timescale 1ns/1ns

module seller2(
	input wire clk  ,
	input wire rst  ,
	input wire d1 ,
	input wire d2 ,
	input wire sel ,
	
	output reg out1,
	output reg out2,
	output reg out3
);
//*************code***********//
//金额状态
reg		[6:0]	cuur_state; 
reg		[6:0]	next_state; 

parameter	S0 = 7'b000_0001; //0元
parameter	S1 = 7'b000_0010; //0.5元
parameter	S2 = 7'b000_0100; //1元
parameter	S3 = 7'b000_1000; //1.5元
parameter	S4 = 7'b001_0000; //2元
parameter	S5 = 7'b010_0000; //2.5元
parameter	S6 = 7'b100_0000; //3元


always @(posedge clk or negedge rst) begin
	if(!rst)
		cuur_state <= S0;
	else
		cuur_state <= next_state;
end

always @(*) begin
	case(cuur_state) 
		S0 : next_state = d1 ? S1 : d2 ? S2 : next_state;
		S1 : next_state = d1 ? S2 : d2 ? S3 : next_state;
		S2 : next_state = d1 ? S3 : d2 ? S4 : next_state;
		S3 : next_state = ~sel ? S0 : d1 ? S4 : d2 ? S5 : next_state;
		S4 : next_state = ~sel ? S0 : d1 ? S5 : d2 ? S6 : next_state;
		S5 : next_state = S0;
		S6 : next_state = S0;
		default : next_state = S0;
	endcase
end

always @(*) begin
	if(!rst) begin
		{out1,out2,out3} = 3'b000;
	end
	else begin
		case(cuur_state) 
			S0,S1,S2 :  {out1,out2,out3} = 3'b000;
			S3		 :  {out1,out2,out3} = ~sel ? 3'b100 : 3'b000;
			S4		 :  {out1,out2,out3} = ~sel ? 3'b101 : 3'b000;
			S5		 :  {out1,out2,out3} = ~sel ? 3'b101 : 3'b010;
			S6		 :  {out1,out2,out3} = ~sel ? 3'b101 : 3'b011;
			default : {out1,out2,out3} = 3'b000;
		endcase
	end
end

//*************code***********//
endmodule
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值