题解 | #自动贩售机1#

自动贩售机1

http://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] cnt1;
reg [1:0] cnt2;
reg cnt3;

always@(posedge clk or negedge rst)
    begin
        if(!rst)
            begin
                cnt1<=0;
                cnt2<=0;
                cnt3<=0;
            end
        else
            begin
                if(d1)
                    cnt1<=cnt1+1;
                if(d2)
                    cnt2<=cnt2+1;
                if(d3)
                    cnt3<=cnt3+1;
                
                if(money >= 3) //如果用out1的话会迟1个clk
                    begin
                       cnt1<=0;
                       cnt2<=0;
                       cnt3<=0; 
                    end
            end
    end

wire [3:0] money;
assign money = cnt1+{cnt2,1'b0}+{cnt3,2'b0};

always@(posedge clk or negedge rst)
    begin
        if(!rst)
            begin
                out1<=0;
                out2<=0;
            end
        else
            begin
                if(money >= 3)
                    begin
                        out1<=1;
                        out2<=money-3;
                    end
                else
                    begin
                        out1<=0;
                        out2<=0;
                    end
            end
    end

//**code// endmodule

全部评论

相关推荐

鼠鼠第一次实习,啥也不懂一直是自己一个人吃的饭,不会做工作老是被嫌弃,大人的世界是这样的吗?
我是星星我会发亮:好的mt有两种,一种愿意教你的,一种几乎什么活都不给你派让你很闲允许你做自己事情的
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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