牛客进阶题目14:自动贩售机1
用计数器对输入金额进行计数,大于等于1.5元时出货并找零。
注意在出货的同时也可能投币,并且不支持同时投入三种货币
`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***********//
reg[2:0] d_cnt ;
wire d_cur ;
assign d_cur = d1 ? d1 : (d2 ? d2 : (d3 ? d3 : 0));
always @(posedge clk or negedge rst) begin
if(!rst)
d_cnt <= 'd0 ;
else if(d_cnt+d_cur>=3)
d_cnt <= d_cur ;
else if(d1)
d_cnt <= d_cnt + 1 ;
else if(d2)
d_cnt <= d_cnt + 2 ;
else if(d3)
d_cnt <= d_cnt + 4 ;
else
d_cnt <= d_cnt ;
end
always @(posedge clk or negedge rst) begin
if(!rst) begin
out1 <= 'd0 ;
out2 <= 'd0 ;
end
else if(d_cnt+d_cur>=3) begin
out1 <= 1'b1 ;
out2 <= d_cnt + d_cur - 3 ;
end
else begin
out1 <= 1'b0 ;
out2 <= 'd0 ;
end
end
//*************code***********//
endmodule
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】