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

游戏机计费程序

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
    );
    always@(posedge clk or negedge rst_n)
begin
    if(rst_n == 1'b0) begin
        remain <= 10'd0;
    end 
    else begin
        if(set == 1'b1)
            remain <= remain + money;
        else if(boost == 1'b1) begin
            if(remain <= 10'd2)
                remain <= 10'd0;
            else
                remain <= remain - 10'd2;
        end 
        else if(boost == 1'b0) begin
            if(remain <= 10'd1)
                remain <= 10'd0;
            else
                remain <= remain - 10'd1;
        end 
        else 
            remain <= remain;
    end 
end

always@(posedge clk or negedge rst_n)
begin
    if(rst_n == 1'b0)
        yellow <= 1'b0;
    else begin
        if((remain > 10'd0) && (remain < 10'd10))
            yellow <= 1'b1;
        else 
            yellow <= 1'b0;
    end 
end

always @ (posedge clk or negedge rst_n)
begin
    if(rst_n == 1'b0) begin
        red <= 1'b0;
    end 
    else begin
        if(remain < 10'd1)
            red <= 1'b1;
        else
            red <= 1'b0;
    end 
end
endmodule

全部评论

相关推荐

11-08 17:36
诺瓦科技_HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务