题解 | #自动贩售机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

全部评论

相关推荐

认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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