-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakeGameController.v
executable file
·187 lines (173 loc) · 4.91 KB
/
SnakeGameController.v
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 11/07/2015 11:54:56 AM
// Design Name:
// Module Name: SnakeGameController
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module SnakeGameController(
input CLK40HZ, // 40 HZ clock
input ESC,
input R,
input S,
input P,
input UP,
input DOWN,
input LEFT,
input RIGHT,
output[9:0] XPOS,
output[9:0] YPOS,
output[1:0] SnakeDir,
output[2:0] GAME_DONEX
);
reg GAME_RUNNING = 1'b1;
reg GAME_DONE = 1'b0;
reg SNAKE_HIT = 1'd0;
assign GAME_DONEX[0] = SNAKE_HIT;
assign GAME_DONEX[1] = GAME_DONE;
assign GAME_DONEX[2] = GAME_PAUSED;
reg GAME_PAUSED = 1'd0;
reg[9:0] SnakeX = 10'd100;
reg[9:0] SnakeY = 10'd100;
reg[1:0] SnDir_INT = 2'd00;
reg[1:0] DR_HOLD;
//assign XPOS = SnakeX;
//assign YPOS = SnakeY;
assign SnakeDir = SnDir_INT;
assign XPOS = (SnDir_INT == 2'd0) ? SnakeX : ((SnDir_INT == 2'd2) ? (SnakeX - 33) : (SnakeX - 7));
assign YPOS = (SnDir_INT == 2'd1) ? (SnakeY + 17) : ((SnDir_INT == 2'd3) ? (SnakeY-17) : SnakeY);
always @ (P or R)
begin
if (P == 1'd1)
begin
DR_HOLD = SnDir_INT;
GAME_PAUSED = 1'b1;
end
if (R == 1'd1)
begin
GAME_PAUSED = 1'b0;
end
end
always @ (ESC or S)
begin
if (S == 1) GAME_DONE = 1'b0;
if (ESC == 1) GAME_DONE = 1'b1;
end
always @ (posedge CLK40HZ)
begin
if (SnakeX < 10'd2 || SnakeX >= 10'd639)
begin
SNAKE_HIT = 1'b1;
end
if (SnakeY < 10'd2 || SnakeY >= 10'd479)
begin
SNAKE_HIT = 1'b1;
end
if (S == 1)
begin
SnakeX = 10'd100;
SnakeY = 10'd100;
SNAKE_HIT = 1'b0;
end
if (GAME_DONE == 1'b1 && ESC == 1)
begin
SnakeX = 10'd700;
SnakeY = 10'd500;
end
case (SnakeDir)
2'b00: // Right
begin
if (GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0)
begin
if (GAME_PAUSED == 1'b0) SnakeX = SnakeX + 10'd1;
else SnakeX = SnakeX;
end
else
begin
SnakeX = SnakeX;
end
end
2'b01: // Down
begin
if ((GAME_DONE == 1'b0) && SNAKE_HIT == 1'b0)
begin
if (GAME_PAUSED == 1'b0) SnakeY = SnakeY + 10'd1;
else SnakeY = SnakeY;
end
else
begin
SnakeY = SnakeY;
end
end
2'b10: // Left
begin
if ((GAME_DONE == 1'b0) && SNAKE_HIT == 1'b0)
begin
if (GAME_PAUSED == 1'b0) SnakeX = SnakeX - 10'd1;
else SnakeX = SnakeX;
end
else
begin
SnakeX = SnakeX;
end
end
2'b11: // Up
begin
if (GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0)
begin
if (GAME_PAUSED == 1'b0) SnakeY = SnakeY - 10'd1;
else SnakeY = SnakeY;
end
else
begin
SnakeY = SnakeY;
end
end
endcase
end
always @ (UP or DOWN or LEFT or RIGHT or S)
begin
if (S == 1)
begin
SnDir_INT = 2'b00;
end
if (UP == 1)
begin
if (GAME_PAUSED == 1'b0 && GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0) SnDir_INT = 2'b11;
else SnDir_INT = SnDir_INT;
end
else if (DOWN == 1)
begin
if (GAME_PAUSED == 1'b0 && GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0) SnDir_INT = 2'b01;
else SnDir_INT = SnDir_INT;
end
else if (LEFT == 1'b1)
begin
if (GAME_PAUSED == 1'b0 && GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0) SnDir_INT = 2'b10;
else SnDir_INT = SnDir_INT;
end
else if (RIGHT == 1'b1)
begin
if (GAME_PAUSED == 1'b0 && GAME_DONE == 1'b0 && SNAKE_HIT == 1'b0) SnDir_INT = 2'b00;
else SnDir_INT = SnDir_INT;
end
else
begin
SnDir_INT = SnDir_INT;
end
end
//assign SnakeDir = (UP == 1'b1) ? 2'b11 : (DOWN == 1'b1) ? 2'b01 : (LEFT == 1'b1) ? 2'b10 : 2'b00;
endmodule