题解 | #自动售卖机#

自动售卖机

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   
);
reg money;
always@(posedge clk or negedge rst_n)
if(!rst_n)begin
    money<=0;
    drinks_out<=0;
    change_out<=0;
end else
begin
    if(money==1)begin
    case(din)
    2'b0:begin 
       money<=1;
       drinks_out<=0;
       change_out<=0; 
    end
    2'b01:begin 
       money<=0;
       drinks_out<=2'b10;
       change_out<=0; 
    end
    2'b10:begin 
       money<=0;
       drinks_out<=2'b10;
       change_out<=1; 
    end
    endcase
    end else
    begin 
        case({sel,din})
        3'b001:begin 
           drinks_out<=2'b01;
           change_out<=0; 
        end
        3'b010:begin 
           drinks_out<=2'b01;
           change_out<=1; 
        end
        3'b110:begin 
           drinks_out<=2'b10;
           change_out<=0; 
        end
        3'b101:begin 
           drinks_out<=0;
           change_out<=0; 
           money<=1;
        end
        default:begin 
           drinks_out<=0;
           change_out<=0; 
        end
        endcase
    end
end
    

endmodule

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务