module vending(clk,clk_500hz,item,inc,rst,mode,ok,yen1,yen5,seg1,seg2,seg3,seg4,seg5,seg6);
input clk,inc,rst,yen1,yen5,mode;
input [4:0] item;
output reg clk_500hz;
output reg [6:0] seg1;
output reg [6:0] seg2;
output reg [6:0] seg3;
output reg [6:0] seg4;
output reg [6:0] seg5;
output reg [6:0] seg6;
output reg ok;
reg [25:0] div_cnt; //分频计数
reg [15:0] yen_tot; //已投入的总钱数
reg [15:0] change; //找零
reg [25:0] st1;
reg [6:0] owari; //消抖用
reg [15:0] cost_tot; //花费的总价钱
integer i,okay;
parameter n=10;
always @(posedge clk)
begin
if (div_cnt==99999)
begin
div_cnt=0;
clk_500hz=~clk_500hz;
end
else
begin
div_cnt=div_cnt+1;
clk_500hz=clk_500hz;
end
end
always @ (posedge clk_500hz)
begin
/*return the money*/
if (rst)
begin
ok=0;
yen_tot=0;
end
/*increase button*/
if (!inc)
begin
owari=owari+1;
if (owari==n-1)
begin
if (yen1 && !yen5 && yen_tot<99)
yen_tot=yen_tot+1;
if (yen5 && !yen1 && yen_tot<95)
yen_tot=yen_tot+5;
owari=owari;
end
end
else
begin
owari=0;
end
light(seg6,0);
light(seg5,0);
light(seg4,0);
light(seg3,0);
light(seg2,yen_tot/10);
light(seg1,yen_tot%10);
if (mode)
begin
cost_tot=0;
change=0;
for (i=0; i<5; i=i+1)
if (item[i])
cost_tot=cost_tot+item_kane(i);
if (cost_tot>yen_tot)
begin
if (!ok)
begin
light(seg3,10);
light(seg4,10);
light(seg5,10);
light(seg6,10);
end
end
else //show change
begin
ok=1;
yen_tot=yen_tot-cost_tot;
light(seg5,0);
light(seg6,0);
light(seg4,11);
light(seg3,12);
light(seg2,yen_tot/10);
light(seg1,yen_tot%10);
end
end
else
begin
okay=0;
for (i=0; i<5; i=i+1)
begin
if (item[i] && !okay)
begin
okay=1;
light(seg6,0);
light(seg5,i+1);
light(seg4,item_kane(i)/10);
light(seg3,item_kane(i)%10);
end
end
end
end
task light;
output [6:0] seg;
input [3:0] val;
begin
case (val)
0:seg=7'b1000000;
1:seg=7'b1111001;
2:seg=7'b0100100;
3:seg=7'b0110000;
4:seg=7'b0011001;
5:seg=7'b0010010;
6:seg=7'b0000010;
7:seg=7'b1111000;
8:seg=7'b0000000;
9:seg=7'b0010000;
10:seg=7'b0000110; //E
11:seg=7'b1000110; //C
12:seg=7'b0001001; //H
endcase
end
endtask
function [9:0] item_kane;
input [5:0] i;
begin
case (i)
0:item_kane=11;
1:item_kane=45;
2:item_kane=14;
3:item_kane=19;
4:item_kane=81;
endcase
end
endfunction
endmodule