-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame_status.v
More file actions
53 lines (46 loc) · 1.13 KB
/
game_status.v
File metadata and controls
53 lines (46 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module game_status(
input clk, reset,
input start_game, win_game, lost_game,
output reg [1:0] current_state
);
localparam START = 2'd0,
INGAME = 2'd1,
WINGAME = 2'd2,
LOSTGAME = 2'd3;
reg [1:0] next_state;
reg [27:0] rate_divider;
reg start_rate_divider;
always @(*) begin
start_rate_divider = 1'b0;
case (current_state)
START: next_state = start_game ? INGAME : START;
INGAME: begin
if (win_game) begin
next_state = WINGAME;
start_rate_divider = 1'b1;
end
else if (lost_game) begin
next_state = LOSTGAME;
start_rate_divider = 1'b1;
end
else
next_state = INGAME;
end
WINGAME: next_state = (rate_divider == 28'd0) ? START : WINGAME;
LOSTGAME: next_state = (rate_divider == 28'd0) ? START : LOSTGAME;
default: next_state = START;
endcase
end
always @(posedge clk) begin
if (start_rate_divider)
rate_divider <= 28'd249999999;
else if (rate_divider > 28'd0)
rate_divider <= rate_divider - 28'd1;
end
always @(posedge clk) begin
if (reset)
current_state <= START;
else
current_state <= next_state;
end
endmodule