题解 | #自动贩售机1#
自动贩售机1
https://www.nowcoder.com/practice/dcf59e6c51f6489093495acb1bc34dd8
`timescale 1ns/1ns module top_module; reg clk; reg rst; reg d1,d2,d3; wire out1,out2; initial `probe_start; `probe(clk); initial begin clk = 1'b1; rst <= 1'b0; d1=0;d2=0;d3=0; #200 rst <= 1'b1; #200 d1=1;d2=0;d3=0; #10 d1=0;d2=0;d3=0; #50 d1=0;d2=0;d3=1; #10 d1=0;d2=0;d3=0; #50 d1=1;d2=0;d3=0; #10 d1=0;d2=0;d3=0; #50 d1=1;d2=0;d3=0; #10 d1=0;d2=0;d3=0; #50 d1=0;d2=0;d3=1; #10 d1=0;d2=0;d3=0; #5000 $finish; end always #10 clk = ~clk; seller1 seller1_0( .clk(clk) , .rst(rst) , .d1(d1) , .d2(d2) , .d3(d3) , .out1(out1), .out2(out2) ); endmodule 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***********// reg [2:0] cnt; always@(posedge clk or negedge rst) begin if(!rst) begin cnt <= 3'd0; out1 <= 1'd0; out2 <= 0; end else if(d1) cnt <= cnt + 3'd1; else if(d2) cnt <= cnt + 3'd2; else if(d3) cnt <= cnt + 3'd4; else if(cnt >= 3) begin out1 <= 1'd1; out2 <= cnt - 3; cnt <= 3'd0; end else begin cnt <= cnt; out1 <= 1'd0; out2 <= 0; end end `probe(rst); `probe(d1); `probe(d2); `probe(d3); `probe(out1); `probe(out2); //*************code***********// endmodule HDLbits simulation