# 简析

`set==1`时，进行投币，`money`会添加到余额`remain`上。`boost==0`是普通模式，每个时钟周期消耗1元；`boost==0`是畅玩模式，每个时钟周期消耗2元。当余额不足时停止计费（余额小于对应模式的要求）。

``````    always@(posedge clk or negedge rst_n) begin
if(~rst_n)
remain <= 0;
else if(boost)
remain <= set     ? remain+money:
remain<2? remain:
remain-2;
else
remain <= set     ? remain+money:
remain<1? remain:
remain-1;
end

``````

``````    always@(posedge clk or negedge rst_n) begin
if(~rst_n) begin
yellow <= 0;
red    <= 0;
end
else begin
yellow <= remain<10&&remain;
red    <= boost? remain<2: remain<1;
end
end
``````

# 代码

```````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) begin
yellow <= 0;
red    <= 0;
end
else begin
yellow <= remain<10&&remain;
red    <= boost? remain<2: remain<1;
end
end

always@(posedge clk or negedge rst_n) begin
if(~rst_n)
remain <= 0;
else if(boost)
remain <= set     ? remain+money:
remain<2? remain:
remain-2;
else
remain <= set     ? remain+money:
remain<1? remain:
remain-1;
end
endmodule
``````
Verilog篇题解 文章被收录于专栏

4

1

OPPO

red和yellow输出太奇怪了，当remain为9的时候，这个时钟内yellow还没有点亮，这后面几个题的时序怎么都是乱七八糟的。
1

set类似按键，应该设计个防抖的，而且只是一个时钟周期有效吧，不然一直按一直加

16 3 评论