-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameState.v.bak
109 lines (101 loc) · 2.21 KB
/
GameState.v.bak
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
module GameState(
input pixelClock,
input vSyncStart,
input [7:0] buttons,
input collisionBallScreenLeft,
input collisionBallScreenRight,
output reg [7:0] state = stateGameOver,
output reg [7:0] playerScore = 0,
output reg [7:0] computerScore = 0
);
// Game states
parameter
stateGameOver = 8'd0,
statePlaying = 8'd1,
statePlayerScored = 8'd2,
stateComputerScored = 8'd3;
reg startButtonReleased = 0;
reg [15:0] count = 0;
always @(posedge pixelClock)
begin
if (vSyncStart)
begin
case(state)
stateGameOver:
begin
// Wait until button is not pressed before checking to see if it's pressed.
// This way we don't skip the game-over state if button is already held down.
if (~buttons[NesController.buttonStart])
startButtonReleased <= 1;
if (startButtonReleased & buttons[NesController.buttonStart])
begin
playerScore <= 0;
computerScore <= 0;
state <= statePlaying;
end
end
statePlaying:
begin
if (collisionBallScreenLeft)
begin
count <= 0;
state <= stateComputerScored;
end
if (collisionBallScreenRight)
begin
count <= 0;
state <= statePlayerScored;
end
end
statePlayerScored:
begin
if (playerScore == 9)
state <= stateGameOver;
else
if (count == 0)
begin
playerScore <= playerScore + 1'b1;
if (playerScore == 9)
state <= stateGameOver;
count <= count + 1'b1;
end
else
begin
if (count < 60 * 2)
begin
count <= count + 1'b1;
end
else
begin
if (buttons[NesController.buttonA])
state <= statePlaying;
end
end
end
stateComputerScored:
begin
if (computerScore == 9)
state <= stateGameOver;
else
if (count == 0)
begin
computerScore <= computerScore + 1'b1;
count <= count + 1'b1;
end
else
begin
if (count < 60 * 2)
begin
count <= count + 1'b1;
end
else
begin
if (buttons[NesController.buttonA])
state <= statePlaying;
end
end
end
endcase
end
end
endmodule