module sell(
input clk,rst_n,
input one_dollar,half_dollar,
output reg collect,//提示取走饮料
output reg half_out,//找零信号
output reg dispense//表示机器出售一瓶饮料
);//一瓶饮料卖2.5
parameter idle=0,one=2,half=1,two=3,three=4;
//idle,one,half,two,three为中间状态变量,代表投入币值的几种情况
reg [2:0]D;
always @(posedge clk or negedge rst_n)begin
if(!rst_n)
begin
dispense=0;collect=0;
half_out=0;D=idle;
end
case(D)
idle:begin
dispense=0;
collect=0;
if(half_dollar)begin
dispense=0;
collect=0;
D=half;//投入5角
end
else if(one_dollar)begin
D=one;
dispense=0;
collect=0;
end
end
half://5mao
if(half_dollar)begin
D=one; //投入两个5角等于1元
dispense=0;
collect=0;
end
else if(one_dollar)begin
D=two;
dispense=0;
collect=0;
end
one://1kuai
if(half_dollar)
D=two;//1.5
else if(one_dollar)
D=three;
two://1.5kuai
if(half_dollar)begin
D=three;
dispense=0;
collect=0;
end
else if(one_dollar)
begin
dispense=1;//2.5块的饮料
collect=1;
half_out=0;
D=idle;
end
three: //2kuai
if(half_dollar)
begin
dispense=1;
collect=1;
half_out=0;
D=idle;
end
else if(one_dollar)
begin
dispense=1;
collect=1;
half_out=1;
D=idle;
end
endcase
end
endmodule
module tb_sell();
reg clk,rst_n;
reg one_dollar,half_dollar;
wire collect,half_out,dispense;
sell u1(.clk(clk),.rst_n(rst_n),.one_dollar(one_dollar),.half_dollar(half_dollar),.collect(collect),
.half_out(half_out),.dispense(dispense));
initial begin
clk<=1'b0;
rst_n<=1'b0;
one_dollar<=1'b0;
half_dollar<=1'b0;
#50 rst_n<=1'b1;
#10 one_dollar<=1'b1;
#12 one_dollar<=1'b0;
#10 one_dollar<=1'b1;
#12 one_dollar<=1'b0;
#10 one_dollar<=1'b1;
#12 one_dollar<=1'b0;
#50 one_dollar<=1'b1;
#12 one_dollar<=1'b0;
#10 one_dollar<=1'b1;
#12 one_dollar<=1'b0;
#10 half_dollar<=1'b1;
#12 half_dollar<=1'b0;
end
always #10 clk<=~clk;
endmodule