题解 | #自动贩售机2#

自动贩售机2

https://www.nowcoder.com/practice/298dec1c3dce45c881f3e53e02558828

//cnt及时返回0
`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***********//
    reg[2:0] cnt;
    always@(posedge d1) cnt <= cnt + 1;
    always@(posedge d2) cnt <= cnt + 2;
    always@(negedge rst or posedge clk)begin
        if(~rst)begin
            cnt <= 0;
            out1 <= 0;
            out2 <= 0;
            out3 <= 0;
        end
        else if(sel == 0)begin	//饮料一
            if(cnt >= 3)begin
                out1 <= 1;
                cnt <= 0;
                out3 <= cnt - 3;
            end
            else begin
                out1 <= 0;
                out2 <= 0;
                out3 <= 0;
            end
        end
        else begin	//饮料二
            if(cnt >= 5)begin
                out2 <= 1;
                cnt <= 0;
                out3 <= cnt - 5;
            end
            else begin
                out1 <= 0;
                out2 <= 0;
                out3 <= 0;
            end
        end
    end
    
//*************code***********//
endmodule

全部评论

相关推荐

深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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