题解 | #游戏机计费程序#

游戏机计费程序

https://www.nowcoder.com/practice/50188fb7e23b4eee86f8c463c8284f5e

它充值的时候居然不计费,这是我没想到的(
`timescale 1ns/1ns

module game_count
    (
		input rst_n, //异位复位信号,低电平有效
        input clk, 	//时钟信号
        input [9:0]money,
        input set,
		input boost,
		output reg[9:0]remain,
		output reg yellow,
		output reg red
    );
//     reg mode;
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                begin
                    remain <= 0;
                end
            else if(set)
                begin
                    remain <= boost?remain+money:remain+money; // 如果充值时也计费,这里就前面-2后面-1.现在更简洁的当然是直接写成remain+money咯
                end
            else
                begin
                    remain <= boost?(remain<2?remain:remain-2):
                              (remain<1?remain:remain-1);
                end
        end
    always@(posedge clk or negedge rst_n)
        begin
            if(!rst_n)
                begin
                    yellow <= 0;
                    red <= 0;
                end
            else
                begin
                    yellow <= remain<10&&remain>0;
                    red <= boost?remain<2:remain<1;
                end
        end
    
endmodule


全部评论

相关推荐

牛客101244697号:这个衣服和发型不去投偶像练习生?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务