题解 | #自动售卖机#

自动售卖机

https://www.nowcoder.com/practice/487953e6d3e3434988e0dd6960b6c9f8

`timescale 1ns/1ns

module sale(
   input                clk   ,
   input                rst_n ,
   input                sel   ,//sel=0,5$dranks,sel=1,10&=$drinks
   input          [1:0] din   ,//din=1,input 5$,din=2,input 10$
 
   output   reg  [1:0] drinks_out,//drinks_out=1,output 5$ drinks,drinks_out=2,output 10$ drinks
   output	reg        change_out   
);
wire [1:0] total;
wire flag;

reg [1:0] tmp;

always @ (posedge clk or negedge rst_n) begin
    if (~rst_n) begin
        drinks_out <= 2'd0;
        change_out <= 1'd0;
    end
    else begin
        case({sel, total})
          3'b001: begin drinks_out <= 2'b01; change_out <= 1'b0; end
          3'b010: begin drinks_out <= 2'b01; change_out <= 1'b1; end
          3'b110: begin drinks_out <= 2'b10; change_out <= 1'b0; end
          3'b111: begin drinks_out <= 2'b10; change_out <= 1'b1; end
          default: begin drinks_out <= 2'd0; change_out <= 1'd0; end
        endcase
    end
end 

always @ (posedge clk or negedge rst_n) begin
    if (~rst_n)
      tmp <= 2'd0;
    else if (flag)
      tmp <= 2'd0; //售卖机输出饮料,寄存投币清零。
    else if (|din)
      tmp <= din; //投币时才能寄存。
end

assign total = din + tmp;
assign flag = (({sel, total} == 3'b001) || ({sel, total} == 3'b010) || ({sel, total} == 3'b110) || ({sel, total} == 3'b111));

endmodule

全部评论

相关推荐

learYuan:🐕看了都摇头
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务