题解 | #自动贩售机1#
自动贩售机1
https://www.nowcoder.com/practice/dcf59e6c51f6489093495acb1bc34dd8
`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***********// reg [2:0]cs; reg [2:0]ns; parameter S0=3'd0; parameter S0_5=3'd1; parameter S1=3'd2; parameter S1_5=3'd3; parameter S2=3'd4; parameter S2_5=3'd5; parameter S3=3'd6; always@(posedge clk or negedge rst)begin if(~rst) cs<=0; else cs<=ns; end always@(*)begin case(cs) S0:begin case({d1,d2,d3}) 3'b100:ns=S0_5; 3'b010:ns=S1; 3'b001:ns=S2; default:ns=ns; endcase end S0_5:begin case({d1,d2,d3}) 3'b100:ns=S1; 3'b010:ns=S1_5; 3'b001:ns=S2_5; default:ns=ns; endcase end S1:begin case({d1,d2,d3}) 3'b100:ns=S1_5; 3'b010:ns=S2; 3'b001:ns=S3; default:ns=ns; endcase end S1_5:begin case({d1,d2,d3}) 3'b000:ns=S0; 3'b100:ns=S0_5; 3'b010:ns=S1; 3'b001:ns=S2; default:ns=S0; endcase end S2:begin case({d1,d2,d3}) 3'b000:ns=S0; 3'b100:ns=S0_5; 3'b010:ns=S1; 3'b001:ns=S2; default:ns=S0; endcase end S2_5:begin case({d1,d2,d3}) 3'b000:ns=S0; 3'b100:ns=S0_5; 3'b010:ns=S1; 3'b001:ns=S2; default:ns=S0; endcase end S3:begin case({d1,d2,d3}) 3'b000:ns=S0; 3'b100:ns=S0_5; 3'b010:ns=S1; 3'b001:ns=S2; default:ns=S0; endcase end default:ns=S0; endcase end always@(posedge clk or negedge rst)begin if(~rst) out1<=0; else if(ns==S1_5||ns==S2||ns==S2_5||ns==S3) out1<=1; else out1<=0; end always@(posedge clk or negedge rst)begin if(~rst) out2<=0; else begin case(ns) S1_5:out2<=2'b00; S2:out2<=2'b01; S2_5:out2<=2'b10; S3:out2<=2'b11; default:out2<=2'b00; endcase end end //*************code***********// endmodule
本题需要注意的点在于,d1,d2,d3只是半个周期,在组合逻辑中,ns会随着他们变化,如果直接赋值ns=s0的话,ns立刻就回到s0了,此时上升沿检测不到ns变到1时候的状态