-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnakeController.v
executable file
·221 lines (172 loc) · 6.32 KB
/
SnakeController.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
module DisplayDriver(
input CLK,
input TRANSMIT,
input[9:0] SnakeX,
input[9:0] SnakeY,
input[3:0] SnakeSize,
input[1:0] SnakeDir,
output reg[3:0] DATA_R,
output reg[3:0] DATA_B,
output reg[3:0] DATA_G
);
localparam WIDTH_PIXEL_SIZE = 10'd10,
LENGTH_PIXEL_SIZE = 10'd50,
LEFT = 2'b10,
RIGHT = 2'b00,
UP = 2'b11,
DOWN = 2'b01,
BG_RED = 4'b0000,
BG_BLUE = 4'b1111,
BG_GREEN = 4'b0000,
SNAKE_RED = 4'b1111,
SNAKE_BLUE = 4'b0000,
SNAKE_GREEN = 4'b0000;
reg[9:0] hcount = 10'd0; // Horizontal Axis
reg[9:0] vcount = 10'd0; // veritcal axis
// Position updates
reg[9:0] CORNER_1_X;
reg[9:0] CORNER_1_Y;
reg[9:0] CORNER_2_X;
reg[9:0] CORNER_2_Y;
reg[9:0] CORNER_3_X;
reg[9:0] CORNER_3_Y;
reg[9:0] CORNER_4_X;
reg[9:0] CORNER_4_Y;
always @ (SnakeDir or SnakeX or SnakeY)
begin
if (SnakeDir == DOWN) // Going right and then turning down
begin
CORNER_1_X = SnakeX + 10'd5;
CORNER_1_Y = SnakeY;
CORNER_2_X = SnakeX - 10'd5;
CORNER_2_Y = SnakeY;
CORNER_3_X = SnakeX + 10'd5;
CORNER_3_Y = SnakeY - (LENGTH_PIXEL_SIZE / 10'd2);
CORNER_4_X = SnakeX - 10'd5;
CORNER_4_Y = SnakeY - (LENGTH_PIXEL_SIZE / 10'd2);
end
else if (SnakeDir == UP)
begin
CORNER_1_X = SnakeX + 10'd5;
CORNER_1_Y = SnakeY;
CORNER_2_X = SnakeX - 10'd5;
CORNER_2_Y = SnakeY;
CORNER_3_X = SnakeX + 10'd5;
CORNER_3_Y = SnakeY + (LENGTH_PIXEL_SIZE / 10'd2);
CORNER_4_X = SnakeX - 10'd5;
CORNER_4_Y = SnakeY + (LENGTH_PIXEL_SIZE / 10'd2);
end
else if (SnakeDir == RIGHT)
begin
CORNER_1_X = SnakeX;
CORNER_1_Y = SnakeY - 10'd5;
CORNER_2_X = SnakeX;
CORNER_2_Y = SnakeY + 10'd5;
CORNER_3_X = SnakeX - (LENGTH_PIXEL_SIZE/10'd2);
CORNER_3_Y = SnakeY - 10'd5;
CORNER_4_X = SnakeX - (LENGTH_PIXEL_SIZE/10'd2);
CORNER_4_Y = SnakeY + 10'd5;
end
else if (SnakeDir == LEFT)
begin
CORNER_1_X = SnakeX;
CORNER_1_Y = SnakeY - 10'd5;
CORNER_2_X = SnakeX;
CORNER_2_Y = SnakeY + 10'd5;
CORNER_3_X = SnakeX + (LENGTH_PIXEL_SIZE/10'd2);
CORNER_3_Y = SnakeY - 10'd5;
CORNER_4_X = SnakeX + (LENGTH_PIXEL_SIZE/10'd2);
CORNER_4_Y = SnakeY + 10'd5;
end
end
always @ (posedge CLK)
begin
if (TRANSMIT == 1'b1)
begin
if (hcount == 10'd639)
begin
hcount = 10'd0; //reached end of horizontal row
if (vcount == 10'd479)
begin
vcount = 10'd0;
end
else
begin
vcount = vcount + 1;
end
end
else
begin
hcount = hcount + 1;
end
end
case(SnakeDir)
2'b00: // Right
begin
if ((hcount < CORNER_1_X && vcount > CORNER_1_Y) && (hcount < CORNER_2_X && vcount < CORNER_2_Y) && (hcount > CORNER_3_X && vcount > CORNER_3_Y) && (hcount > CORNER_4_X && vcount < CORNER_4_Y))
begin
DATA_R <= SNAKE_RED;
DATA_B <= SNAKE_BLUE;
DATA_G <= SNAKE_GREEN;
end
else
begin
DATA_R <= 4'b0000;
DATA_B <= 4'b0000;
DATA_G <= 4'b0000;
end
end
2'b01: // Down
begin
if ((hcount < CORNER_1_X && vcount < CORNER_1_Y) && (hcount > CORNER_2_X && vcount < CORNER_2_Y) && (hcount < CORNER_3_X && vcount > CORNER_3_Y) && (hcount > CORNER_4_X && vcount > CORNER_4_Y))
begin
DATA_R <= SNAKE_RED;
DATA_B <= SNAKE_BLUE;
DATA_G <= SNAKE_GREEN;
end
else
begin
DATA_R <= 4'b0000;
DATA_B <= 4'b0000;
DATA_G <= 4'b0000;
end
end
2'b10: // Left
begin
if ((hcount > CORNER_1_X && vcount > CORNER_1_Y) && (hcount > CORNER_2_X && vcount < CORNER_2_Y) && (hcount < CORNER_3_X && vcount > CORNER_3_Y) && (hcount < CORNER_4_X && vcount < CORNER_4_Y))
begin
DATA_R <= SNAKE_RED;
DATA_B <= SNAKE_BLUE;
DATA_G <= SNAKE_GREEN;
end
else
begin
DATA_R <= 4'b0000;
DATA_B <= 4'b0000;
DATA_G <= 4'b0000;
end
end
2'b11: //Up
begin
if ((hcount < CORNER_1_X && vcount > CORNER_1_Y) && (hcount > CORNER_2_X && vcount > CORNER_2_Y) && (hcount < CORNER_3_X && vcount < CORNER_3_Y) && (hcount > CORNER_4_X && vcount < CORNER_4_Y))
begin
DATA_R <= SNAKE_RED;
DATA_B <= SNAKE_BLUE;
DATA_G <= SNAKE_GREEN;
end
else
begin
DATA_R <= 4'b0000;
DATA_B <= 4'b0000;
DATA_G <= 4'b0000;
end
end
default:
begin
DATA_R <= 4'b0000;
DATA_B <= 4'b0000;
DATA_G <= 4'b0000;
end
endcase
end
endmodule