-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChipInterface_old.sv
executable file
·679 lines (535 loc) · 17.4 KB
/
ChipInterface_old.sv
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/* Breakout: Verilog Style
* @author: Audrey Yeoh, Tanguy Dauphin
* @brief: To make Breakout in Verilog for 18-240 LabB
*/
`default_nettype
/////////////////////////// GAME CLOCK MODULE /////////////////////////
module gameClockModule
(input logic CLOCK_50, reset,
input logic [8:0] row,
input logic [9:0] col,
output logic gameClock);
assign gameClock = (~reset) && (row == 480) && (col == 640);
endmodule: gameClockModule
/////////////////////////// BUTTON MODULE /////////////////////////////
module checkButton
(input logic reset,
input logic clock,
input logic button,
output logic [1:0] buttonPress);
enum logic [1:0] {notPressed, pressed, held, released} state, nextState;
always_comb begin
case (state)
notPressed: begin
if (button == 0) nextState = pressed;
else nextState = notPressed;
end
pressed: begin
nextState = held;
end
held: begin
if(button == 1) nextState = released;
else nextState = held;
end
released: begin
nextState = notPressed;
end
default:
nextState = (~reset) ? state : notPressed;
endcase
end
always_comb begin
unique case(state)
notPressed:
buttonPress = 0;
pressed:
buttonPress = 1;
held:
buttonPress = 2;
released:
buttonPress = 3;
endcase
end
always_ff @(posedge clock)
if(reset) state <= notPressed;
else state <= nextState;
endmodule: checkButton
/////////////////////////// COLOUR MODULE /////////////////////////////
module colour
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
input logic left, right, start,
output logic [7:0] red, green, blue);
logic [4:0] brick, brickHit; // which brick is being looked at?
logic isPaddle, isWall;
logic isBall;
//bricks BR (CLOCK_50, reset, row, col, brick);
paddle PAD (CLOCK_50, gameClock, reset, row, col, left, right, isPaddle);
wall WAL (CLOCK_50, gameClock, reset, row, col, isWall);
ball BAL (CLOCK_50, gameClock, reset, row, col, start, ball, brickHit);
always_comb begin
if (isWall) begin
red = 8'hCC;
green = 8'hCC;
blue = 8'hCC;
end
/*else if (brick) begin
if(brick[0]) begin
red = 8'hFF;
green = 8'hFF;
blue = 8'h00;
end
else begin
red = 8'hFF;
green = 8'h00;
blue = 8'hFF;
end
end*/
else if (isPaddle) begin
red = 8'h00;
green = 8'hFF;
blue = 8'h00;
end
else if (ball) begin
red = 8'hFF;
green = 8'hFF;
blue = 8'hFF;
end
else begin
red = 8'h00;
green = 8'h00;
blue = 8'h00;
end
end
endmodule: colour
/////////////////////////// OBJECT MODULE /////////////////////////////
// This module instantiates all the bricks
// It returns which brick it's on, if there is a brick
// No brick = 0 | brokenBrick = 0 | topRow = 1 2 3 4 5 6 | bottomRow = 8 9 10 11 12 13
module bricks
(input logic CLOCK_50, reset,
input logic [8:0] row,
input logic [9:0] col,
output logic [4:0] brick);
// TODO: How to deal with bricks that got hit if we know which brick got hit? ie. change that bricks signal to 0 forever until reset
// input logic [4:0] brickHit,
logic brick0, brick1, brick2, brick3, brick4, brick5, brick6, brick7, brick8, brick9, brickA, brickB;
// top row
brick #( 40, 100, 100, 30) b0(CLOCK_50, reset, row, col, brick0);
brick #(140, 100, 100, 30) b1(CLOCK_50, reset, row, col, brick1);
brick #(240, 100, 100, 30) b2(CLOCK_50, reset, row, col, brick2);
brick #(340, 100, 100, 30) b3(CLOCK_50, reset, row, col, brick3);
brick #(440, 100, 100, 30) b4(CLOCK_50, reset, row, col, brick4);
brick #(540, 100, 50, 30) b5(CLOCK_50, reset, row, col, brick5);
// 2nd row
brick #( 40, 150, 50, 30) b6(CLOCK_50, reset, row, col, brick6);
brick #( 90, 150, 100, 30) b7(CLOCK_50, reset, row, col, brick7);
brick #(190, 150, 100, 30) b8(CLOCK_50, reset, row, col, brick8);
brick #(290, 150, 100, 30) b9(CLOCK_50, reset, row, col, brick9);
brick #(390, 150, 100, 30) bA(CLOCK_50, reset, row, col, brickA);
brick #(490, 150, 100, 30) bB(CLOCK_50, reset, row, col, brickB);
always_comb begin
if(brick0)
brick = 1;
if(brick1)
brick = 2;
if(brick2)
brick = 3;
if(brick3)
brick = 4;
if(brick4)
brick = 5;
if(brick5)
brick = 6;
if(brick6)
brick = 8;
if(brick7)
brick = 9;
if(brick8)
brick = 10;
if(brick9)
brick = 11;
if(brickA)
brick = 12;
if(brickB)
brick = 13;
end
endmodule: bricks
module brick
#(parameter LEFT = 40, TOP = 100, WIDTH = 100, HEIGHT = 30)
(input logic CLOCK_50, reset,
input logic [8:0] row,
input logic [9:0] col,
output logic signal);
logic withinRow, withinColumn;
offset_check R (row, LEFT, WIDTH, withinRow);
offset_check C (col, TOP, HEIGHT, withinColumn);
// assign withinRow = (row >= LEFT && row <= (LEFT + WIDTH)); cleaned up - below too
// assign withinColumn = (col >= TOP && col <= (TOP + HEIGHT));
assign signal = withinRow && withinColumn;
endmodule: brick
// This module checks whether a wall should be at the given row and column and outputs a signal
module wall
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
output logic signal);
logic leftWall, rightWall, topWall;
logic leftWallRow, leftWallCol;
logic rightWallRow, rightWallCol;
logic topWallRow, topWallCol;
range_check leftRow (row, 10, 469, leftWallRow);
range_check leftCol (col, 20, 39, leftWallCol);
range_check rightRow (row, 10, 469, rightWallRow);
range_check rightCol (col, 590, 609, rightWallCol);
range_check topRow (row, 50, 80, topWallRow); // 10, 29
range_check topCol (col, 20, 609, topWallCol);
assign leftWall = leftWallRow && leftWallCol;
assign rightWall = rightWallRow && rightWallCol;
assign topWall = topWallRow && topWallCol;
//assign leftWall = (col >= 20 && col <= 39) && (row >= 10 && row <=469);
//assign rightWall = (col >= 590 && col <= 609) && (row >= 10 && row <=469);
//assign topWall = (row >= 10 && row <= 29) && (col >= 20 && col <= 609);
assign signal = leftWall | rightWall | topWall;
endmodule: wall
// This module checks for whether the paddle should be present at the given row and col every game cycle
// Returns a 1 signal if the paddle should be there
// ALSO, the module updates the position of the paddle each game cycle
module paddle
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
input logic left, right,
output logic signal);
logic [9:0] paddlePosition; // left column of paddle
logic withinRow, withinCol;
logic paddleWidth;
assign paddleWidth = 64;
assign withinRow = (row >= 440 && row <= 459);
assign withinCol = (col >= paddlePosition && col <= (paddlePosition+64)) && (col > 39 && col < (590 - 64));
assign signal = withinRow && withinCol;
always_ff @(posedge gameClock) begin // game clock period
if(reset) begin
paddlePosition <= (225+40)-(32); // middle of game area - half paddle width
end
else if((left && right) || (~left && ~right)) begin
paddlePosition <= paddlePosition;
end
else if (left && ~right) begin
if(paddlePosition - 5 > 39) begin
paddlePosition <= paddlePosition - 5;
end
else begin
paddlePosition <= paddlePosition;
end
end
else if (~left && right) begin
if(paddlePosition + paddleWidth + 5 < 590) begin
paddlePosition <= paddlePosition + 5;
end
else begin
paddlePosition <= paddlePosition;
end
end
else begin
paddlePosition <= paddlePosition;
end
end
endmodule: paddle
module ball
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
input startKey,
output logic signal,
output logic [4:0] hitBrick); // sending hitBrick back may or may not be bad...
reg [10:0] ballRow, ballCol;
reg playing;
logic hitTopWall, hitPaddle, hitLeftWall, hitRightWall;
reg movingUp, movingLeft;
initial begin
ballRow = 420;
ballCol = 400;
end
//paddle P (CLOCK_50, reset, row, col, 0, 0, hitPaddle); // Potential bug of updating paddle twice?
//bricks B (CLOCK_50, reset, row, col, hitBrick);
//register BROW (ballRow, rst, en, gameClock, )
assign hitTopWall = ballRow < (29+1);
assign hitLeftWall = ballCol < (39+1);
assign hitRightWall = (ballCol+4) > (590-1);
assign movingUp = ((movingUp) && ~(hitTopWall || hitBrick)) || (~movingUp && hitPaddle);
assign movingLeft = (movingLeft && ~hitLeftWall) || (~movingLeft && hitRightWall);
assign signal = ((row >= ballRow) && (row < ballRow+4)) && ((col >= ballCol) && (col < ballCol+4));
always @(posedge gameClock) begin // game clock period
if(startKey) begin
playing = 1;
ballRow = 420;
ballCol = 400;
end
if(reset) begin
playing = 0;
ballRow = 420;
ballCol = 400;
end
if (playing) begin
if(movingUp)
ballRow = ballRow - 2;
else
ballRow = ballRow + 2;
if(movingLeft)
ballCol = ballCol - 1;
else
ballCol = ballCol + 1;
end
end
endmodule: ball
//////////////////////////// CHIP INTERFACE ///////////////////////////
module ChipInterface
(input logic CLOCK_50,
input logic [3:0] KEY,
input logic [17:0] SW,
output logic [6:0] HEX0, HEX1, HEX2, HEX3, HEX4, HEX5, HEX6, HEX7,
output logic [7:0] VGA_R, VGA_G, VGA_B,
output logic VGA_BLANK_N, VGA_CLK, VGA_SYNC_N,
output logic VGA_VS, VGA_HS);
logic [8:0] row;
logic [9:0] col;
// logic not_red, not_green1, not_green2, not_blue1, not_blue2, not_blue3, not_blue4;
logic blank;
logic [7:0] red, green, blue;
logic right, left, start, rst;
logic gameClock;
vga VGA (CLOCK_50, ~KEY[2], VGA_HS, VGA_VS, blank, row, col);
/*range_check RED (col, 0, 319, not_red);
range_check GREEN1 (col, 0, 159, not_green1);
range_check GREEN2 (col, 320, 479, not_green2);
range_check BLUE1 (col, 0, 79, not_blue1);
range_check BLUE2 (col, 160, 239, not_blue2);
range_check BLUE3 (col, 320, 399, not_blue3);
range_check BLUE4 (col, 480, 559, not_blue4);*/
assign VGA_SYNC_N = 0;
assign VGA_CLK = ~CLOCK_50;
assign VGA_BLANK_N = ~blank;
checkButton B0 (reset, CLOCK_50, KEY[0], right);
checkButton B3 (reset, CLOCK_50, KEY[3], left);
checkButton B1 (reset, CLOCK_50, KEY[1], start);
checkButton B2 (reset, CLOCK_50, KEY[2], rst);
gameClockModule GCM (CLOCK_50, rst, row, col, gameClock)
colour C (CLOCK_50, gameClock, rst, row, col, ~KEY[3], ~KEY[0], start, red, green, blue);
assign VGA_R = (row == 0 || row == 479 || col == 0 || col == 639) ? 8'h10 : red;
assign VGA_G = (row == 0 || row == 479 || col == 0 || col == 639) ? 8'h10 : green;
assign VGA_B = (row == 0 || row == 479 || col == 0 || col == 639) ? 8'h10 : blue;
/*assign VGA_R = (not_red) ? 8'h00: 8'hFF;
assign VGA_G = (not_green1 | not_green2) ? 8'h00: 8'hFF;
assign VGA_B = (not_blue1 | not_blue2 | not_blue3 | not_blue4) ? 8'h00 : 8'hFF;*/
endmodule: ChipInterface
module vga_test;
logic CLOCK_50, reset;
logic HS, VS, blank;
logic [8:0] row;
logic [9:0] col;
vga V (.*);
logic [15:0] rowCount,count;
assign rowCount = V.rowCount;
assign count = V.clockCount;
assign HDisp = V.Tdisp;
assign VDisp = V.RTdisp;
initial begin
$monitor($time,, "HS = %b, VS = %b, blank = %b, row = %b, col = %b, reset = %b, clockCount = %b", HS, VS, blank, row, col, reset, V.clockCount);
CLOCK_50 = 0;
#5 CLOCK_50 = 1;
#5 CLOCK_50 = 0;
reset = 1;
#5 CLOCK_50 = 0;
#5 CLOCK_50 = 1;
#5 CLOCK_50 = 0;
#5 reset = 0;
forever #5 CLOCK_50 = ~CLOCK_50;
end
endmodule: vga_test
/////////////////////////////////// VGA STUFF /////////////////////////
// Here is the VGA module. It controls the wave and things
module vga
(input logic CLOCK_50, reset,
output logic HS, VS, blank,
output logic [8:0] row,
output logic [9:0] col);
logic [15:0] clockCount, rowCount, startTime, change;
logic withinRow, withinClock;
logic Tdisp, Tpw, Tfp, Tbp;
logic RTpw, RTbp, RTdisp, RTfp;
offset_check ROW (rowCount, 0, 520, withinRow);
offset_check CLOCK (clockCount, 0, 1599, withinClock);
offset_check TDISP (clockCount, 288, 1279, Tdisp);
offset_check TPW (clockCount, 0, 191, Tpw);
offset_check RTPW (rowCount, 0, 1, RTpw);
offset_check RDISP (rowCount, 31, 479, RTdisp);
assign row = rowCount;
assign col = (clockCount-288)/2;
assign VS = ~RTpw; // (RTpw) ? 0:1;
assign HS = ~Tpw; // (Tpw) ? 0:1;
assign blank = ~(Tdisp && RTdisp); //(Tdisp && RTdisp) ? 0:1;
always @(posedge CLOCK_50) begin
if(reset) begin
clockCount = 0;
rowCount = 0;
end
else if(~withinClock && ~withinRow) begin
clockCount = 0;
rowCount = 0;
end
else if(~withinClock) begin // new row
clockCount = 0;
rowCount = rowCount + 1;
end
else begin
clockCount = clockCount + 1;
end
end
endmodule: vga
///////////////////////////////// END OF VGA ////////////////////////
// MODULE: RANGE_CHECK
// This module checks whether the value is between the given low and high values
// returns a 1 if true, 0 if not
module range_check
#(parameter WIDTH = 16)
(input logic [WIDTH - 1: 0] val, low, high,
output logic is_between);
assign is_between = ((low <= val) && (high >= val)) ? 1 : 0;
endmodule: range_check
// MODULE: OFFSET_CHECK
// This module checks whether the value is between the low and the low + off_set
// it calls the range_check module to check the range once the values have been added
// returns a 1 if is, 0 if not
module offset_check
#(parameter WIDTH = 16)
(input logic [WIDTH - 1: 0] val, low, delta,
output logic is_between);
// logic rangeCheckResult;
logic [WIDTH-1:0] sum;
assign sum = low + delta;
range_check #(WIDTH) RC (val, low, sum, is_between);
//assign is_between = (sum[WIDTH]) ? 0:rangeCheckResult;
endmodule: offset_check
///////////////// TEST MODULES FOR RANGE & OFFSET CHECK ///////////////////////////
// Note: to test the module, please use the vlogan method
// This module tests range check by running it through a bunch of test cases
module range_check_test;
logic [15:0] val, low, high;
logic is_between;
range_check RC (.*);
initial begin
$monitor($time,, "val = %b | low = %b | high = %b | isBetween = %b", val, low, high, is_between);
val = 0; // should return 1 because edge case: on low, on high
low = 0;
high = 0;
// return 0: too high
#10 val = 1;
low = 0;
high = 0;
// return 1: normal valid
#10 val = 1;
low = 0;
high = 2;
// return 0: too low
#10 val = 1;
low = 2;
high = 2;
// return 1: on low
#10 val = 2;
low = 2;
high = 3;
// return 1: on high
#10 val = 3;
low = 2;
high = 3;
// return 1: Test the big numbers
#10 val = 8'b1111_1111;
low = 0;
high = 8'b1111_1111;
// return 0: Test fail on big numbers
#10 val = 8'b1111_1111;
low = 8'b1111_0000;
high = 8'b1111_1110;
end
endmodule: range_check_test
// This module tests offset_check.
module offset_check_test;
logic [15:0] val, low, delta;
logic is_between;
offset_check OC (.*);
initial begin
$monitor($time,, "val = %b | low = %b | delta = %b | sum = %b | isBetween = %b", val, low, delta, OC.sum, is_between);
// return 1: true case
val = 2;
low = 0;
delta = 3;
// return 0: too high
#10 val = 3;
low = 0;
delta = 2;
// return 0: too low
#10 val = 0;
low = 2;
delta = 1;
// return 1: on low
#10 val = 2;
low = 2;
delta = 3;
// return 1: on high
#10 val = 3;
low = 1;
delta = 2;
// return 0: test overflow cases
#10 val = 3;
low = 3;
delta = 8'b1111_1111;
// return 1: test high cases
#10 val = 8'b1111_1111;
low = 0;
delta = 8'b1111_1111;
end
endmodule: offset_check_test
module register
#(parameter WIDTH = 10)
(input logic [WIDTH-1:0] D,
input logic clear, en,
input logic clock,
output logic [WIDTH-1:0] Q);
always @(posedge clock) begin
if (clear) Q <= 0;
else if (en) Q <= D;
else Q <= Q;
end
endmodule: register
module register_test();
logic [7:0] D;
logic clear, en, clock;
logic [7:0] Q;
register #(8) r (.*);
initial begin
$monitor("D = %b | Q = %b | En = %b | clear = %b | clock = %b", D, Q, en, clear, clock);
// set value to D
D = 8'b0000_1111;
clock = 1;
clear = 0;
en = 1;
#5 clock = 0;
// clear value from D
#5 clear = 1;
en = 0;
clock = 1;
#5 clock = 0;
// enable priority - Value should be set
#5 clear = 1;
en = 1;
clock = 1;
#5 clock = 0;
// Change value in D
#5 clear = 0;
en = 1;
clock = 1;
D = 8'b1111_0000;
end
endmodule: register_test