Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

module vendingmachine(clk,cancel,coin,sel,prA,prB,prC,change);

input clk,cancel;
input [1:0]coin,sel;
output reg prA,prB,prC,change;
reg [2:0]nxt_state;
parameter
[2:0]INITIALSTATE=3'b000,FIVE=3'b001,TEN=3'b011,FIFTEEN=3'b010,TWENTY=3'b110;
reg [7:0]fivecount,tencount;
assign tencount=8'b00011100;
assign fivecount=8'b00011100;
//always@(posedge clk)
case(nxt_state)
INITIALSTATE:begin
if(cancel)
begin
prA<=0;
prB<=0;
prC<=0;
change<=0;
end
else
begin
if(coin==01)
begin
nxt_state<=FIVE;
fivecount<=fivecount+8'b00000001;
end
else if(coin==10)
begin
nxt_state<=TEN;
tencount<=tencount+8'b00000001;
end
else
nxt_state<=INITIALSTATE;
end
end

FIVE:begin
if(cancel)
begin
fivecount<=fivecount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
begin
if(coin==01)
begin
nxt_state<=TEN;
fivecount<=fivecount+8'b00000001;
end
else
begin
if(coin==10)
begin
nxt_state<=FIFTEEN;
tencount<=tencount+8'b00000001;
end
else
if(sel==0)
begin
prA<=1;
prB<=0;
prC<=0;
change<=0;
nxt_state<=INITIALSTATE;
end
else
prA=1'bX;
prB=1'bX;
prC=1'bX;
nxt_state<=INITIALSTATE;
fivecount<=fivecount+8'b11111111;
end
end
end
TEN:begin
if(cancel)
begin
tencount<=tencount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
begin
if(coin==01)
begin
nxt_state<=FIFTEEN;
fivecount<=fivecount+8'b00000001;
end
else if(coin==10)
begin
nxt_state<=TWENTY;
tencount<=tencount+8'b00000001;
end
else
if(sel==00)
begin
prA<=1;
prB<=0;
prC<=0;
change<=1;
fivecount=fivecount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
if(sel==01)
begin
prA<=0;
prB<=1;
prC<=0;
change<=0;
nxt_state<=INITIALSTATE;
end
else
begin
prA=1'bX;
prB=1'bX;
prC=1'bX;
nxt_state<=INITIALSTATE;
tencount<=tencount+8'b11111111;
end
end
end
FIFTEEN:begin
if(cancel)
begin
tencount<=tencount+8'b11111111;
fivecount=fivecount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
begin
if(coin==01)
begin
nxt_state<=TWENTY;
fivecount<=fivecount+8'b00000001;
end
else
begin
if(sel==00)
begin
prA<=1;
prB<=0;
prC<=0;
change<=1;
tencount=tencount+8'b11111111;
end
else
if(sel==01)
begin
prA<=0;
prB<=1;
prC<=0;
change<=1;
fivecount=fivecount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
prA=1'bX;
prB=1'bX;
prC=1'bX;
nxt_state<=INITIALSTATE;
tencount<=tencount+8'b11111111;
fivecount=fivecount+8'b11111111;
end
end
TWENTY:begin
if(cancel)
begin
tencount<=tencount+8'b11111110;
nxt_state<=INITIALSTATE;
end
else
begin
if(sel==00)
begin
prA<=1;
prB<=0;
prC<=0;
change<=1;
fivecount=fivecount+8'b11111111;
tencount=tencount+8'b11111111;
end
else
if(sel==01)
begin
prA<=0;
prB<=1;
prC<=0;
change<=1;
tencount=tencount+8'b11111111;
nxt_state<=INITIALSTATE;
end
else
if(sel==10)
begin
prA<=0;
prB<=0;
prC<=1;
change<=0;
nxt_state<=INITIALSTATE;
end
else
prA=1'bX;
prB=1'bX;
prC=1'bX;
nxt_state<=INITIALSTATE;

tencount<=tencount+8'b11111111;

fivecount=fivecount+8'b11111111;
end
end
end
endcase
endmodule

You might also like