-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvga_display.v
476 lines (443 loc) · 12.6 KB
/
vga_display.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Part 2 skeleton
module vga_display
(
CLOCK_50, // On Board 50 MHz
KEY,
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK_N, // VGA BLANK
VGA_SYNC_N, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B, // VGA Blue[9:0]
track1_in,
track2_in,
track3_in,
track4_in,
h0,
h1,
h2,
h3,
reset
);
input CLOCK_50; // 50 MHz
input [3:0] KEY;
input track1_in, track2_in, track3_in, track4_in;
input reset;
output VGA_CLK; // VGA Clock
output VGA_HS; // VGA H_SYNC
output VGA_VS; // VGA V_SYNC
output VGA_BLANK_N; // VGA BLANK
output VGA_SYNC_N; // VGA SYNC
output [9:0] VGA_R; // VGA Red[9:0]
output [9:0] VGA_G; // VGA Green[9:0]
output [9:0] VGA_B; // VGA Blue[9:0]
output [6:0] h0, h1, h2, h3;
wire resetn;
wire [2:0] colour;
wire [7:0] x;
wire [7:0] y;
assign resetn = reset;
// Create an Instance of a VGA controller - there can be only one!
// Define the number of colours as well as the initial background
// image file (.MIF) for the controller.
vga_adapter VGA(
.resetn(1'b1),
.clock(CLOCK_50),
.colour(colour),
.x(x),
.y(y),
.plot(1'b1),
/* Signals for the DAC to drive the monitor. */
.VGA_R(VGA_R),
.VGA_G(VGA_G),
.VGA_B(VGA_B),
.VGA_HS(VGA_HS),
.VGA_VS(VGA_VS),
.VGA_BLANK(VGA_BLANK_N),
.VGA_SYNC(VGA_SYNC_N),
.VGA_CLK(VGA_CLK));
defparam VGA.RESOLUTION = "160x120";
defparam VGA.MONOCHROME = "FALSE";
defparam VGA.BITS_PER_COLOUR_CHANNEL = 1;
defparam VGA.BACKGROUND_IMAGE = "verilog/background.mif";
// Put your code here. Your code should produce signals x,y,colour and writeEn/plot
// for the VGA controller, in addition to any other functionality your design may require.
// Instansiate datapath
datapath d0(
.clk(CLOCK_50),
.resetn(resetn),
.track1(track1_in),
.track2(track2_in),
.track3(track3_in),
.track4(track4_in),
.key1(KEY[3]),
.key2(KEY[2]),
.key3(KEY[1]),
.key4(KEY[0]),
.x(x),
.y(y),
.color(colour),
.h0(h0),
.h1(h1),
.h2(h2),
.h3(h3)
);
endmodule
module datapath(
input clk,
input resetn,
input track1,
input track2,
input track3,
input track4,
input key1,
input key2,
input key3,
input key4,
output [6:0] h0,
output [6:0] h1,
output [6:0] h2,
output [6:0] h3,
output reg [7:0] x,
output reg [7:0] y,
output reg [2:0] color
);
reg [7:0] x1 = 8'd15;
reg [7:0] y1 = 8'd0;
reg [7:0] x2 = 8'd53;
reg [7:0] y2 = 8'd0;
reg [7:0] x3 = 8'd91;
reg [7:0] y3 = 8'd0;
reg [7:0] x4 = 8'd129;
reg [7:0] y4 = 8'd0;
reg [7:0] x_state = 8'd0;
reg [7:0] y_state = 8'd0;
reg [7:0] x2_state = 8'd0;
reg [7:0] y2_state = 8'd0;
reg [7:0] x3_state = 8'd0;
reg [7:0] y3_state = 8'd0;
reg [7:0] x4_state = 8'd0;
reg [7:0] y4_state = 8'd0;
reg [27:0] counter = 28'd0;
reg track1_state = 0;
reg track2_state = 0;
reg track3_state = 0;
reg track4_state = 0;
reg [7:0] y_up = 8'd97;
reg [7:0] y_down = 8'd76;
reg [3:0] score1 = 4'd0;
reg [3:0] score2 = 4'd0;
reg [3:0] score3 = 4'd0;
reg [3:0] score4 = 4'd0;
hexdisplay hx0(.hex_digit(score1), .OUT(h0[6:0]));
hexdisplay hx1(.hex_digit(score2), .OUT(h1[6:0]));
hexdisplay hx2(.hex_digit(score3), .OUT(h2[6:0]));
hexdisplay hx3(.hex_digit(score4), .OUT(h3[6:0]));
always@(posedge clk, negedge resetn) begin
if(!resetn) begin
x_state <= 8'd0;
y_state <= 8'd0;
x2_state <= 8'd0;
y2_state <= 8'd0;
x3_state <= 8'd0;
y3_state <= 8'd0;
x4_state <= 8'd0;
y4_state <= 8'd0;
counter <= 28'd0;
score1 <= 0;
score2 <= 0;
end
else begin
// Game Counter
if (counter < 28'd1000521) begin
// Checking for pulse for each track
if (track1)
track1_state <= 1;
if (track2)
track2_state <= 1;
if (track3)
track3_state <= 1;
if (track4)
track4_state <= 1;
// Stopping tracks when pulse is not sent
if (!track1_state)
y_state <= 8'd0;
if (!track2_state)
y2_state <= 8'd0;
if (!track3_state)
y3_state <= 8'd0;
if (!track4_state)
y4_state <= 8'd0;
// Registering pressed notes and incrementing score for player
if (!key1 && y_state <= y_up && y_state >= y_down) begin
score1 <= score1 + 1;
track1_state <= 0;
y_state <= 8'd0;
end
else begin
if (!key1 && (y_state > y_up || y_state < y_down) && y_state > 4) begin
score1 <= 0;
score2 <= 0;
track1_state <= 0;
y_state <= 8'd0;
end
end
if (!key2 && y2_state <= y_up && y2_state >= y_down) begin
score1 <= score1 + 1;
track2_state <= 0;
y2_state <= 8'd0;
end
else begin
if (!key2 && (y2_state > y_up || y2_state < y_down) && y2_state > 4) begin
score1 <= 0;
score2 <= 0;
track2_state <= 0;
y2_state <= 8'd0;
end
end
if (!key3 && y3_state <= y_up && y3_state >= y_down) begin
score1 <= score1 + 1;
track3_state <= 0;
y3_state <= 8'd0;
end
else begin
if (!key3 && (y3_state > y_up || y3_state < y_down) && y3_state > 4) begin
score1 <= 0;
score2 <= 0;
track3_state <= 0;
y3_state <= 8'd0;
end
end
if (!key4 && y4_state <= y_up && y4_state >= y_down) begin
score1 <= score1 + 1;
track4_state <= 0;
y4_state <= 8'd0;
end
else begin
if (!key4 && (y4_state > y_up || y4_state < y_down) && y4_state > 4) begin
score1 <= 0;
score2 <= 0;
track4_state <= 0;
y4_state <= 8'd0;
end
end
// Register to display score on hex displays
if (score1 >= 4'd10) begin
score1 <= 4'd0;
score2 <= score2 + 1;
end
// Highscore number
if (score2 > score4) begin
score4 <= score2;
score3 <= score1;
end else begin
if (score2 == score4) begin
if (score1 > score3)
score3 <= score1;
end
end
// Reseting tracks when they hit the bottom of the screen
if (y1 == 127)
track1_state <= 0;
if (y2 == 127)
track2_state <= 0;
if (y3 == 127)
track3_state <= 0;
if (y4 == 127)
track4_state <= 0;
// DRAW
// First Track (Drawing)
if (counter <= 28'd128) begin
x <= x1;
y <= y1;
if (y_state == 0)
color <= 3'b000;
else
color <= 3'b100;
if (counter != 28'd0)
x1 <= x1 + 7'd1;
if (x1 == 8'd30) begin
y1 <= y1 + 8'd1;
x1 <= 8'd15;
end
end
// First Track (Resetin the states)
if (counter == 28'd129) begin
y1 <= y_state;
x1 <= 8'd15;
end
// Second Track (Drawing)
if (counter <= 28'd257 && counter >= 28'd129) begin
x <= x2;
y <= y2;
if (y2_state == 0)
color <= 3'b000;
else
color <= 3'b101;
if (counter != 28'd129)
x2 <= x2 + 8'd1;
if (x2 == 8'd68) begin
y2 <= y2 + 8'd1;
x2 <= 8'd53;
end
end
// Second Track (Resetin the states)
if (counter == 28'd258) begin
y2 <= y2_state;
x2 <= 8'd53;
end
// Third Track (Drawing)
if (counter <= 28'd386 && counter >= 28'd258) begin
x <= x3;
y <= y3;
if (y3_state == 0)
color <= 3'b000;
else
color <= 3'b111;
if (counter != 28'd258)
x3 <= x3 + 8'd1;
if (x3 == 8'd106) begin
y3 <= y3 + 8'd1;
x3 <= 8'd91;
end
end
// Third Track (Reseting the states)
if (counter == 28'd387) begin
y3 <= y3_state;
x3 <= 8'd91;
end
// Fourth Track (Drawing)
if (counter <= 28'd515 && counter >= 28'd387) begin
x <= x4;
y <= y4;
if (y4_state == 0)
color <= 3'b000;
else
color <= 3'b011;
if (counter != 28'd387)
x4 <= x4 + 8'd1;
if (x4 == 8'd144) begin
y4 <= y4 + 8'd1;
x4 <= 8'd129;
end
end
// Fourth Track (Resetin the states)
if (counter == 28'd516) begin
y4 <= y4_state;
x4 <= 8'd129;
end
// DELETE
if (counter >= 28'd1000000) begin
// First Track (Deleting)
if (counter <= 28'd1000128) begin
x <= x1;
y <= y1;
if (y1 == 80 || y1 == 100)
color <= 3'b110;
else
color <= 3'b000;
if (counter != 28'd1000000)
x1 <= x1 + 8'd1;
if (x1 == 8'd30) begin
y1 <= y1 + 8'd1;
x1 <= 8'd15;
end
end
// First Track (Changing the states)
if (counter == 28'd1000129) begin
x_state <= x_state + 8'd1;
y_state <= y_state + 8'd1;
end
// First Track (Resetin the states)
if (counter == 28'd1000130) begin
y1 <= y_state;
x1 <= 8'd15;
end
// Second Track (Deleting)
if (counter <= 28'd1000258 && counter >= 28'd1000130) begin
x <= x2;
y <= y2;
if (y2 == 80 || y2 == 100)
color <= 3'b110;
else
color <= 3'b000;
if (counter != 28'd1000130)
x2 <= x2 + 8'd1;
if (x2 == 8'd68) begin
y2 <= y2 + 8'd1;
x2 <= 8'd53;
end
end
// Second Track (Changing the states)
if (counter == 28'd1000259) begin
x2_state <= x2_state + 8'd1;
y2_state <= y2_state + 8'd1;
end
// Second Track (Resetin the states)
if (counter == 28'd1000260) begin
y2 <= y2_state;
x2 <= 8'd53;
end
// Third Track (Deleting)
if (counter <= 28'd1000388 && counter >= 28'd1000260) begin
x <= x3;
y <= y3;
if (y3 == 80 || y3 == 100)
color <= 3'b110;
else
color <= 3'b000;
if (counter != 28'd1000260)
x3 <= x3 + 8'd1;
if (x3 == 8'd106) begin
y3 <= y3 + 8'd1;
x3 <= 8'd91;
end
end
// Third Track (Changing the states)
if (counter == 28'd1000389) begin
x3_state <= x3_state + 8'd1;
y3_state <= y3_state + 8'd1;
end
// Third Track (Resetin the states)
if (counter == 28'd1000390) begin
y3 <= y3_state;
x3 <= 8'd91;
end
// Fourth Track (Deleting)
if (counter <= 28'd1000518 && counter >= 28'd1000390) begin
x <= x4;
y <= y4;
if (y4 == 80 || y4 == 100)
color <= 3'b110;
else
color <= 3'b000;
if (counter != 28'd1000390)
x4 <= x4 + 8'd1;
if (x4 == 8'd144) begin
y4 <= y4 + 8'd1;
x4 <= 8'd129;
end
end
// Fourth Track (Changing the states)
if (counter == 28'd1000519) begin
x4_state <= x4_state + 8'd1;
y4_state <= y4_state + 8'd1;
end
// Fourth Track (Resetin the states)
if (counter == 28'd1000520) begin
y4 <= y4_state;
x4 <= 8'd129;
end
end
// Counter going up
counter = counter + 28'd1;
end
// Reset the counter
else begin
counter <= 28'd0;
end
end
end
endmodule