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

游戏机计费程序

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

充钱的一分钟如果扣钱,就需要分情况讨论,与当前状态和boost都有关,代码如下:

如果当前为普通模式,剩一块钱,此时变为畅玩模式并充钱,这也是要扣钱的,所以还要加上考虑当前状态。

`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 [1:0] cstate,nstate;
    parameter s0 = 2'b00,s1 = 2'b01, s2 = 2'b10;
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n)begin
            cstate <= s1;
        end
        else begin
            cstate <= nstate;
        end
    end
    always@(*)begin
        if(!rst_n)begin
            nstate <= s1;
        end
        else begin
            case(cstate)
                s1:begin
                    if(boost == 1'b1)
                        nstate <= s2;
                end
                s2:begin
                    if(boost == 'b0)
                        nstate <= s1;
                end
            endcase
        end
    end
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n)begin
            remain <= 'd0;
        end
        else begin
            case(boost)
                'd0:begin
                    if(remain > 'd0)begin
                        if(set == 'b1)
                            remain <= remain + money -'d1;
                        else
                            remain <= remain - 'd1;
                    end
                    else begin
                        if(set == 'b1)
                            remain <= remain + money;
                        else
                            remain <= remain;
                    end
                end
                'd1:begin
                    if(cstate == s1)begin
                        if(remain > 'd0)begin
                            if(set == 'b1)
                                remain <= remain + money -'d2;
                            else
                                remain <= remain - 'd2;
                        end
                        else begin
                            if(set == 'b1)
                                remain <= remain + money;
                            else
                                remain <= remain;
                        end
                    end
                    else if(cstate == s2)begin
                        if(remain > 'd1)begin
                            if(set == 'b1)
                                remain <= remain + money -'d2;
                            else
                                remain <= remain - 'd2;
                        end
                        else begin
                            if(set == 'b1)
                                remain <= remain + money;
                            else
                                remain <= remain;
                        end
                    end
                end
            endcase
        end
    end
    always@(posedge clk or negedge rst_n)begin
        if(!rst_n)begin
            yellow <= 'd0;
            red <= 'd0;
        end
        else begin
            if(remain <= 'd0)begin
                red <= 1'b1;
                yellow <= 1'b0;
            end
            else if(remain < 'd10)begin
                red <= 'd0;
                yellow <= 'd1;
            end
            else begin
                red <= 'd0;
                yellow <= 'd0;
            end
        end
    end
endmodule
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务