专栏前言
本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网
自动贩售机中可能存在的几种金额:0,0.5,1,1.5,2,2.5,3。然后直接将其作为状态机的几种状态,并根据投币面额确定状态转移。
需要注意的是:根据时序图,可以发现在找零时,out2输出的结果是找零数额的两倍,即找零0.5应输出1,找零1应输出2,以此类推。
`timescale 1ns/1ns
module seller1(input wire clk ,input wire rst ,input wire d1 ,input wire d2 ,input wire d3 ,output reg out1,output reg [1:0]out2
);
//*************code***********//parameter S0 = 0, S0_5 = 1, S1 = 2, S1_5 = 3, S2 = 4, S2_5 = 5, S3 = 6 ; reg [2:0] state, nstate ;always @ (posedge clk or negedge rst) begin if (~rst) state <= S0 ; else state <= nstate ; endalways @ (*) begin case (state) S0 : nstate = d1 ? S0_5 : d2 ? S1 : d3 ? S2 : nstate ;S0_5 : nstate = d1 ? S1 : d2 ? S1_5 : d3 ? S2_5 : nstate ; S1 : nstate = d1 ? S1_5 : d2 ? S2 : d3 ? S3 : nstate ; S1_5, S2, S2_5, S3 : nstate = S0 ; default : nstate = S0 ; endcase endalways @ (*) begin if (~rst) out1 <= 'd0 ; else out1 <= state == S1_5 || state == S2 || state == S2_5 || state == S3 ; endalways @ (*) begin if (~rst) out2 <= 'd0 ; else case (state) S0, S0_5, S1, S1_5 : out2 <= 1'd0 ; S2 : out2 <= 1'd1 ; S2_5 : out2 <= 2'd2 ; S3 : out2 <= 2'd3 ; default : out2 <= 'd0 ; endcaseend //*************code***********//
endmodule