题解 | #自动贩售机1#
自动贩售机1
http://www.nowcoder.com/practice/dcf59e6c51f6489093495acb1bc34dd8
我的代码就15行,没有用状态机,是不是不规范呀。
`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] MoneySum;
always @(posedge clk or negedge rst) begin
if(!rst) begin
MoneySum <= 3'd0;
out1 <= 1'b0;
out2 <= 2'd0; end
else if(MoneySum < 3'b011) begin
MoneySum <= MoneySum + {d3,d2,d1};
out1 <= 1'b0;
out2 <= 2'b00; end
else begin
MoneySum <= 3'b000;
out1 <= 1'b1;
out2 <= MoneySum - 3'b011; end
end
//**code// endmodule