自动贩售机1
题目描述:
设计一个自动贩售机,输入货币有三种,为0.5/1/2元,饮料价格是1.5元,要求进行找零,找零只会支付0.5元。
ps:
投入的货币会自动经过边沿检测并输出一个在时钟上升沿到1,在下降沿到0的脉冲信号
注意rst为低电平复位
信号示意图:
`timescale 1ns/1nsmodule seller1(input wire clk ,input wire rst ,input wire d1 ,input wire d2 ,input wire d3 ,output reg out1,output reg [1:0]out2
);parameter S0 = 'd0, S1 = 'd1, S2 = 'd2, S3 = 'd3 , S4 = 'd4, S5 = 'd5 , S6 = 'd6;reg [2:0] current_state;reg [2:0] next_state;wire [2:0] input_state;//将输入组合起来assign input_state = {d1,d2,d3};always@(posedge clk or negedge rst)beginif(rst == 1'b0)begincurrent_state <= S0;endelse begincurrent_state <= next_state;endend always@(*)begincase(current_state)S0:begincase(input_state)3'b100:next_state = S1 ;3'b010:next_state = S2 ;3'b001:next_state = S4 ;default:next_state = next_state;endcase endS1:begincase(input_state)3'b100:next_state = S2 ;3'b010:next_state = S3 ;3'b001:next_state = S5 ;default:next_state = next_state; endcaseendS2:begincase(input_state)3'b100:next_state = S3 ;3'b010:next_state = S4 ;3'b001:next_state = S6 ;default:next_state = next_state;endcase enddefault:beginnext_state = S0;endendcaseendalways@(posedge clk or negedge rst)beginif(rst == 1'b0)beginout1 <= 1'b0;out2 <= 2'b0;endelse begincase(next_state)S3: begin out1 <= 1'b1;out2 <= 2'b0; end S4: begin out1 <= 1'b1;out2 <= 2'b1; end S5: begin out1 <= 1'b1;out2 <= 2'b10; end S6: begin out1 <= 1'b1;out2 <= 2'b11; end default: begin out1 <= 1'b0;out2 <= 2'b0; end endcase endendendmodule