题解 | #游戏机计费程序#
游戏机计费程序
http://www.nowcoder.com/practice/50188fb7e23b4eee86f8c463c8284f5e
- 根据boost类型确定每分钟/周期消费金额,注意充值所在分钟/周期是不消费的。
- 两个灯(red yellow)不能同时亮起,先判断红灯是否满足条件,再判断黄灯。
完整代码
`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)
remain <= 0;
else begin
if(boost==0)
remain <= set==1? remain+money: remain-1;
else if(boost==1)
remain <= set==1? remain+money: remain-2;
end
end
always@(posedge clk or negedge rst_n)begin
if(~rst_n)begin
yellow <= 0;
red <= 0;
end
else begin
if(remain < 1)begin
yellow <= 0;
red <= 1;
end
else if(remain < 10)begin
yellow <= 1;
red <= 0;
end
else begin
yellow <= 0;
red <= 0;
end
end
end
endmodule