题解 | #自动贩售机2#
自动贩售机2
https://www.nowcoder.com/practice/298dec1c3dce45c881f3e53e02558828
`timescale 1ns/1ns module 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 IDEL=0, S0=1,S1=2,S2=3,S3=4,S4=5,S5=6; reg [2:0] cstate, nstate; always@(posedge clk or negedge rst) begin if(!rst) begin cstate <= IDEL; end else begin cstate <= nstate; end end always@(*) begin case(cstate) IDEL: nstate = d1?S0:(d2?S1:nstate); S0: nstate = d1?S1:(d2?S2:nstate); S1: nstate = d1?S2:(d2?S3:nstate); S2: begin if(sel) begin nstate = d1?S3:(d2?S4:nstate); end else begin nstate = d1?S0:(d2?S1:IDEL); end end S3: begin if(sel) begin nstate = d1?S4:(d2?S5:nstate); end else begin nstate = d1?S0:(d2?S1:IDEL); end end S4, S5: nstate = d1?S0:(d2?S1:IDEL); default:nstate = IDEL; endcase end always@(posedge clk or negedge rst) begin if(!rst) begin out1 <= 1'b0; out2 <= 1'b0; out3 <= 1'b0; end else begin out1 <= ((nstate==S2) | (nstate==S3)) & (~sel); out2 <= ((nstate==S4) | (nstate==S5)) & sel; out3 <= (((nstate==S3) & (~sel)) | ((nstate==S5) & sel))?1'b1:1'b0; end end //*************code***********// endmodule