要求:以2.5元售卖一瓶可乐的可乐机,有找零功能
说明:投币在同一时刻不可能有一元和五毛,所以两个钱是输入不可能同时为高电平
分析:
输入:1/0.5/0 (10/01/00)
状态:0/0.5/1/1.5/2 (00001/00010/00100/01000/10000)
输出:不出可乐不找零/只出可乐/可乐加找零(00/10/11)
module state_2(
input wire sys_clk,
input wire rst_n,
input wire money_half,
input wire money_one,
output reg po_cole,
output reg po_money
);
parameter IDLE = 5'b00001;
parameter HALF = 5'b00010;
parameter ONE = 5'b00100;
parameter ONE_HALF = 5'b01000;
parameter TWO= 5'b10000;
reg [4:0]state;
wire [1:0] money;
assign money ={money_one,money_half}; //利用位拼接将投钱量合在一起,case语句中就对money变量判别,显得更加简洁(不过没改,嘿嘿),
always@(posedge sys_clk or negedge rst_n)
if (~rst_n)
state <= IDLE;
else case(state)
IDLE:
if(money_half==1'b1)
state <= HALF;
else if (money_one == 1'b1)
state <= ONE;
else
state <= IDLE;
HALF:
if(money_half==1'b1)
state <= ONE;
else if (money_one == 1'b1)
state <= ONE_HALF;
else
state <= HALF;
ONE:
if(money_half==1'b1)
state <= ONE_HALF;
else if (money_one == 1'b1)
state <= TWO;
else
state <= ONE;
ONE_HALF:
if(money_half==1'b1)
state <= TWO;
else if (money_one == 1'b1)
state <= IDLE;
else
state <= ONE_HALF;
TWO:
if(money_half==1'b1)
state <= IDLE;
else if (money_one == 1'b1)
state <= IDLE; ///找0.5
else
state <= TWO;
default : state <= IDLE;
endcase
always @ (posedge sys_clk or negedge rst_n)
if (~rst_n)
begin
po_cole <= 1'b0;
po_money <= 1'b0;
end
else if(((state == ONE_HALF) && (money_one == 1'b1 )) || ((state == TWO) && (money_half == 1'b1 )))
begin
po_cole <= 1'b1;
po_money <= 1'b0;
end
else if ((state == TWO) && (money_one == 1'b1 ))
begin
po_cole <= 1'b1;
po_money <= 1'b1;
end
else
begin
po_cole <= 1'b0;
po_money <= 1'b0;
end
endmodule
module vtf_state_2;
// Inputs
reg sys_clk;
reg rst_n;
reg money_half;
reg money_one;
// Outputs
wire po_cole;
wire po_money;
// Instantiate the Unit Under Test (UUT)
state_2 uut (
.sys_clk(sys_clk),
.rst_n(rst_n),
.money_half(money_half),
.money_one(money_one),
.po_cole(po_cole),
.po_money(po_money)
);
initial begin
// Initialize Inputs
sys_clk = 0;
rst_n = 0;
money_half = 0;
money_one = 0;
// Wait 100 ns for global reset to finish
#100;
rst_n <= 1;
// Add stimulus here
end
always #10 sys_clk= ~sys_clk;
// always @(posedge sys_clk or negedge rst_n)
// if (~rst_n)
// begin
// money_half <= 1'b0;
// money_one <=1'b0;
// end
// else
// begin
// money_half ={$random}%2;
// money_one ={$random}%2;
// end
always @(posedge sys_clk or negedge rst_n)
if (~rst_n)
begin
money_half <= 1'b0;
money_one <=1'b0;
end
else
begin
{money_half,money_one} ={$random}%3;//投币在同一时刻不可能有一元也有五毛,所以两个钱是输入不能同时为高电平
end
endmodule