自动贩售机2
题目描述:
设计一个自动贩售机,输入货币有两种,为0.5/1元,饮料价格是1.5/2.5元,要求进行找零,找零只会支付0.5元。
ps:
1、投入的货币会自动经过边沿检测并输出一个在时钟上升沿到1,在下降沿到0的脉冲信号
2、此题忽略出饮料后才能切换饮料的问题
注意rst为低电平复位
信号示意图:
d1 0.5
d2 1
sel 选择饮料
out1 饮料1
out2 饮料2
out3 零钱
`timescale 1ns/1nsmodule seller2(input wire clk ,input wire rst ,input wire d1 ,input wire d2 ,input wire sel ,output reg out1,output reg out2,output reg out3
);
//*************code***********//parameter S00 = 0, S05 = 1, S10 = 2, S15 = 3, S20 = 4, S15_5 = 5, S25_5 = 6;reg [2:0] state, last_state_1, next_state;reg [1:0] d_neg;reg [1:0] d_reg;wire [1:0] d;assign d = {d2,d1};// state traansitionalways @ (posedge clk or negedge rst) beginif (!rst) beginstate <= 3'b0;last_state_1 <= 3'b0;endelse begin state <= next_state;last_state_1 <= state;endend// next state transition logicalways @ (*) begincase (state)S00: if (d == 2'b01) next_state = S05;else if (d == 2'b10) next_state = S10;else next_state = next_state;S05:if (d == 2'b01) next_state = S10;else if (d == 2'b10) next_state = sel ? S15 : S00; else next_state = next_state;S10:if (d == 2'b01) next_state = sel ? S15 : S00; else if (d == 2'b10) next_state = sel ? S20 : S15_5; else next_state = next_state;S15:if (d == 2'b01) next_state = sel ? S20 : S00; else if (d == 2'b10) next_state = sel ? S00 : S00; else next_state = next_state;S20:if (d == 2'b01) next_state = sel ? S00 : S00; else if (d == 2'b10) next_state = sel ? S25_5 : S00; else next_state = next_state;S15_5: next_state = S00;S25_5: next_state = S00;default: next_state = S00;endcaseend// output logicalways @ (*) beginout1 = (state == S00) && (last_state_1 == S10 || last_state_1 == S05 )|| (state == S15_5);out2 = (state == S00) && (last_state_1 == S15 || last_state_1 == S20 )|| (state == S25_5);out3 = (state == S15_5) || (state == S25_5);end
//*************code***********//
endmodule