-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChipInterface.sv
executable file
·961 lines (806 loc) · 29 KB
/
ChipInterface.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/* Breakout: Verilog Style
* @author: Audrey Yeoh, Tanguy Dauphin
* @brief: To make Breakout in Verilog for 18-240 LabB
*/
`default_nettype null
/////////////////////////// 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
///////////////////////// SEVEN DIGIT THINGY ///////////////////////
module BCDtoSevenSegment
(input logic [3:0] bcd,
output logic [6:0] segment);
always_comb
case ({bcd[3], bcd[2], bcd[1], bcd[0]})
4'b0000: segment = 7'b100_0000;
4'b0001: segment = 7'b111_1001;
4'b0010: segment = 7'b010_0100;
4'b0011: segment = 7'b011_0000;
4'b0100: segment = 7'b001_1001;
4'b0101: segment = 7'b001_0010;
4'b0110: segment = 7'b000_0010;
4'b0111: segment = 7'b111_1000;
4'b1000: segment = 7'b000_0000;
4'b1001: segment = 7'b001_0000;
default: segment = 7'b111_1111;
endcase // case ({bcd[3], bcd[2], bcd[1], bcd[0]})
endmodule: BCDtoSevenSegment
module SevenSegmentDigit
(input logic [3:0] bcd,
output logic [6:0] segment,
input logic blank);
logic [6:0] decoded;
BCDtoSevenSegment b2ss(bcd, decoded);
always_comb begin
if (blank == 1)
segment = 7'b111_1111;
else
segment = decoded;
end
endmodule: SevenSegmentDigit
/////////////////////////// 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,
output logic [6:0] HEX6,
output logic [11:0] led);
logic [8:0] bRow;
logic [9:0] bCol;
logic [11:0] brickTracker, bricksHit;
logic [2:0] dx, dy;
logic isBall, isPaddle, isWall;
logic [11:0] brickIndex, hitBrickLeft, hitBrickRight, hitBrickTop, hitBrickBottom;
logic hitPaddleLeft, hitPaddleRight, hitPaddleTop;
logic hitLeftWall, hitRightWall, hitTopWall;
logic [2:0] gameState;
logic [1:0] lifes;
ball B (CLOCK_50, gameClock, reset, row, col, dx, dy, gameState, isBall, bRow, bCol);
paddle P (CLOCK_50, gameClock, reset, row, col, bRow, bCol, left, right, hitPaddleLeft, hitPaddleRight, hitPaddleTop, isPaddle);
wall W (CLOCK_50, gameClock, reset, row, col, bRow, bCol, hitLeftWall, hitRightWall, hitTopWall, isWall);
bricks BR (CLOCK_50, gameClock, reset, row, col, bRow, bCol, hitBrickTop, hitBrickBottom, hitBrickLeft, hitBrickRight, brickIndex);
velocity V (CLOCK_50, gameClock, reset, gameState, hitPaddleTop, hitPaddleLeft, hitPaddleRight, hitBrickTop, hitBrickBottom, hitBrickLeft, hitBrickRight, hitLeftWall, hitRightWall, hitTopWall, brickTracker, dx, dy);
score S (bRow, bCol, brickTracker, start, gameState, lifes, HEX6);
assign bricksHit = (~brickTracker) & brickIndex;
// Colour Setting
always_comb begin
if (isWall && (gameState == 4)) begin
red = 8'h7C;
green = 8'h00;
blue = 8'h00;
end
else if (isWall && (gameState == 5)) begin
red = 8'h00;
green = 8'h70;
blue = 8'hB0;
end
else if(isWall) begin
red = 8'hCC;
green = 8'hCC;
blue = 8'hCC;
end
else if(isBall && (gameState != 4 || gameState != 5)) begin
red = 8'hFF;
green = 8'hFF;
blue = 8'hFF; end
else if(isPaddle) begin
red = 8'h00;
green = 8'hFF;
blue = 8'h00;
end
else if(bricksHit) begin
// no problem because the brickIndex only returns 1 brick at a time
/*if (brickHit[0] || brickHit[2] || brickHit[4] || brickHit[7] || brickHit[9] || brickHit[11]) begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
else begin
red = 8'hFF;
green = 8'h00;
blue = 8'hFF;
end */
case ({brickHit})
12'b000000_000000: segment = 7'b100_0000; //test all the different decimals
12'b000000_000001: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
12'b000000_000100: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
12'b000000_010000: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
12'b000010_000000: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
12'b001000_000000: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
12'b100000_000000: begin
red = 8'hFF;
green = 8'hff;
blue = 8'h00;
end
default: begin
red = 8'hFF;
green = 8'h00;
blue = 8'hFF;
end
endcase
end
else begin // rest of screen
red = 8'h00;
green = 8'h00;
blue = 8'h00;
end
end
always_ff @(posedge CLOCK_50, posedge reset) begin
if (reset) begin
brickTracker <= 12'h0;
end
else if (gameClock) begin
if (gameState == 0) begin
brickTracker = 12'h0;
end
else begin
brickTracker <= brickTracker | hitBrickLeft | hitBrickRight | hitBrickTop | hitBrickBottom;
end
end
end
endmodule: colour
////////////////////////// SCORE MODULE ///////////////////////////////
module score
(input logic [8:0] bRow,
input logic [9:0] bCol,
input logic [11:0] brickTracker,
input logic startKey,
output logic [2:0] gameState,
output logic [1:0] lifes,
output logic [6:0] HEX6);
enum logic [1:0] {resetted, threeLifes, twoLifes, oneLife, lose, win} state, nextState;
logic inScreen, start, won;
checkButton B1 (reset, gameClock, ~startKey, start); // I think we need it to be on gameClock
range_check ballOff (ballRow, 10, 479, inScreen);
assign won = !(~brickTracker);
always_comb begin
case(state)
resetted: begin
if (start == 2) nextState = threeLifes; // released button
else nextState = resetted;
end
threeLifes: begin
if(!inScreen) nextState = twoLifes;
else if (won) nextState = win;
else nextState = threeLifes;
end
twoLifes: begin
if(!inScreen) nextState = oneLife;
else if (won) nextState = win;
else nextState = twoLifes;
end
oneLife: begin
if(!inScreen) nextState = lose;
else if (won) nextState = win;
else nextState = oneLife;
end
lose: begin
if(start == 3) nextState = resetted;
else nextState = lose;
end
win: begin
if(start == 3) nextState = resetted;
else nextState = lose;
end
end
always_com begin
unique case(state)
resetted: gameState = 0;
threeLifes: gameState = 1;
twoLifes: gameState = 2;
oneLife: gameState = 3;
lose: gameState = 4;
win: gameState = 5;
endcase
end
SevenSegmentDigit SSD (life, HEX6, 0);
always_ff @(posedge CLOCK_50, posedge reset) begin
if(reset) begin
life <= 3;
state <= resetted;
end
else begin
else if(gameClock) begin
if(state == threeLifes) life <= 3;
else if (state == twoLifes) life <= 2;
else if (state == oneLife) life <= 1;
else if (state == lose) life <= 0;
else if (state == win) life <= life;
else if (state == resetted) life <= 3;
else life <= 3;
end
state <= nextState;
end
end
endmodule: score
/////////////////////////// VELOCITY MODULE ///////////////////////////
module velocity
(input logic CLOCK_50, gameClock, reset,
input logic [2:0] gameState,
input logic hitPaddleTop, hitPaddleLeft, hitPaddleRight,
input logic [11:0] hitBrickTop, hitBrickBottom, hitBrickLeft, hitBrickRight,
input logic hitLeftWall, hitRightWall, hitTopWall,
input logic [11:0] brickTracker,
output logic [2:0] dx, dy);
logic [2:0] dx, dy;
logic moveLeft, moveRight, moveUp, moveDown;
assign moveLeft = (hitBrickLeft && (~brickTracker)) || hitRightWall || hitPaddleLeft;
assign moveRight (hitBrickRight && (~brickTracker)) || hitLeftWall || hitPaddleRIght;
assign moveUp = (hitBrickTop && (~brickTracker)) || hitPaddleTop;
assign moveDown = (hitBrickBottom && (~brickTracker)) || hitTopWall;
always_ff @(posedge CLOCK_50, posedge reset) begin
if(reset) begin
dx <= 0; // changes columns (left - right)
dy <= 0; // changes rows (up - down)
end
else if(gameClock) begin
if(gameState == 1 || gameState == 2 || gameState == 3)
if(moveUp) begin
dy <= -2;
end
else if(moveDown) begin
dy <= 2;
end
else if(moveLeft) begin
dx <= -1;
end
else if(moveRight) begin
dx <= 1;
end
else begin
dy <= dy;
dx <= dx;
end
end
else if (gameState == 0) begin
// initialize the velocity (but don't move)
dx <= 1;
dy <= 2;
end
else begin
dx <= 0;
dy <= 0;
end
end
end
endmodule: velocity
/////////////////////////// OBJECT MODULE /////////////////////////////
// This module instantiates all the bricks
// It returns which brick it's on, if there is a brick
module bricks
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
input logic [8:0] brow,
input logic [9:0] bcol,
output logic [11:0] hitBrickTop, hitBrickBottom, hitBrickLeft, hitBrickRight,
output logic [11:0] brick);
// top row
brick #( 40, 100, 100, 30) b0(CLOCK_50, gameClock, reset, row, col, brick[0]);
brick #(140, 100, 100, 30) b1(CLOCK_50, gameClock, reset, row, col, brick[1]);
brick #(240, 100, 100, 30) b2(CLOCK_50, gameClock, reset, row, col, brick[2]);
brick #(340, 100, 100, 30) b3(CLOCK_50, gameClock, reset, row, col, brick[3]);
brick #(440, 100, 100, 30) b4(CLOCK_50, gameClock, reset, row, col, brick[4]);
brick #(540, 100, 50, 30) b5(CLOCK_50, gameClock, reset, row, col, brick[5]);
// 2nd row
brick #( 40, 150, 50, 30) b6(CLOCK_50, gameClock, reset, row, col, brick[6]);
brick #( 90, 150, 100, 30) b7(CLOCK_50, gameClock, reset, row, col, brick[7]);
brick #(190, 150, 100, 30) b8(CLOCK_50, gameClock, reset, row, col, brick[8]);
brick #(290, 150, 100, 30) b9(CLOCK_50, gameClock, reset, row, col, brick[9]);
brick #(390, 150, 100, 30) bA(CLOCK_50, gameClock, reset, row, col, brick[10]);
brick #(490, 150, 100, 30) bB(CLOCK_50, gameClock, reset, row, col, brick[11]);
////////////////////////// Top Row Bottom Hit
brick #( 40, 125, 105, 5) bb0(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[0]);
brick #( 140, 125, 105, 5) bb1(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[1]);
brick #( 240, 125, 105, 5) bb2(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[2]);
brick #( 340, 125, 105, 5) bb3(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[3]);
brick #( 440, 125, 105, 5) bb4(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[4]);
brick #( 540, 125, 45, 5) bb5(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[5]);
////////////////////////// Bottom Row Bottom Hit
brick #( 40, 175, 55, 5) bb6(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[6]);
brick #( 90, 175, 105, 5) bb7(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[7]);
brick #( 190, 175, 105, 5) bb8(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[8]);
brick #( 290, 175, 105, 5) bb9(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[9);
brick #( 390, 175, 105, 5) bb10(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[10]);
brick #( 490, 175, 95, 5) bb11(CLOCK_50, gameClock, reset, brow, bcol, hitBrickBottom[11]);
//////////////////////// Top Row Top Hit
brick #( 40, 95, 105, 5) bt0(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[0]);
brick #( 140, 95, 105, 5) bt1(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[1]);
brick #( 240, 95, 105, 5) bt2(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[2]);
brick #( 340, 95, 105, 5) bt3(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[3]);
brick #( 440, 95, 105, 5) bt4(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[4]);
brick #( 540, 95, 45, 5) bt5(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[5]);
////////////////////// Bottom Row Top Hit
brick #( 40, 145, 55, 5) bt6(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[6]);
brick #( 90, 145, 105, 5) bt7(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[7]);
brick #( 190, 145, 105, 5) bt8(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[8]);
brick #( 290, 145, 105, 5) bt9(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[9);
brick #( 390, 145, 105, 5) bt10(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[10]);
brick #( 490, 145, 95, 5) bt11(CLOCK_50, gameClock, reset, brow, bcol, hitBrickTop[11]);
//////////////////////// Top Row Left Hit
// can never hit the left most brick from the left
brick #( 0, 0, 0, 0) bt0(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[0]);
brick #( 135, 95, 5, 35) bl1(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[1]);
brick #( 235, 95, 5, 35) bl2(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[2]);
brick #( 335, 95, 5, 35) bl3(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[3]);
brick #( 435, 95, 5, 35) bl4(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[4]);
brick #( 535, 95, 5, 35) bl5(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[5]);
////////////////////// Bottom Row Left Hit
brick #( 0, 0, 0, 0) bl6(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[6]);
brick #( 85, 145, 5, 35) bl7(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[7]);
brick #( 185, 145, 5, 35) bl8(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[8]);
brick #( 285, 145, 5, 35) bl9(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[9);
brick #( 385, 145, 5, 35) bl10(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[10]);
brick #( 485, 145, 5, 35) bl11(CLOCK_50, gameClock, reset, brow, bcol, hitBrickLeft[11]);
//////////////////////// Top Row Right Hit
// can never hit the right most brick from the right
brick #( 140, 95, 5, 35) br0(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[0]);
brick #( 240, 95, 5, 35) br1(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[1]);
brick #( 340, 95, 5, 35) br2(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[2]);
brick #( 440, 95, 5, 35) br3(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[3]);
brick #( 540, 95, 5, 35) br4(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[4]);
brick #( 0, 0, 0, 0) br5(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[5]);
////////////////////// Bottom Row Right4 Hit
brick #( 90, 145, 5, 35) br6(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[6]);
brick #( 190, 145, 5, 35) br7(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[7]);
brick #( 290, 145, 5, 35) br8(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[8]);
brick #( 390, 145, 5, 35) br9(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[9);
brick #( 490, 145, 5, 35) br10(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[10]);
brick #( 0, 0, 0, 0) br11(CLOCK_50, gameClock, reset, brow, bcol, hitBrickRight[11]);
endmodule: bricks
module brick
#(parameter LEFT = 40, TOP = 100, WIDTH = 100, HEIGHT = 30)
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
output logic signal);
logic withinRow, withinColumn;
offset_check R (col, LEFT, WIDTH, withinColumn);
offset_check C (row, TOP, HEIGHT, withinRow);
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,
input logic [8:0] bRow,
input logic [9:0] bCol,
output logic hitLeftWall, hitRightWall, hitTopWall,
output logic signal);
// check if wall is at VGA row and col
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, 10, 29, topWallRow);
range_check topCol (col, 20, 609, topWallCol);
assign leftWall = leftWallRow && leftWallCol;
assign rightWall = rightWallRow && rightWallCol;
assign topWall = topWallRow && topWallCol;
assign signal = leftWall | rightWall | topWall;
// check if the ball has hit the wall
logic bLeftWall, bRightWall, bTopWall;
logic bLeftWallRow, bLeftWallCol;
logic bRightWallRow, bRightWallCol;
logic bTopWallRow, bTopWallCol;
range_check bleftRow (bRow, 10, 469, bLeftWallRow);
range_check bleftCol (bCol, 20, 39, bLeftWallCol);
range_check brightRow (bRow, 10, 469, bRightWallRow);
range_check brightCol (bCol, 590 - 5, 609, bRightWallCol);
range_check btopRow (bRow, 10, 29, bTopWallRow);
range_check btopCol (bCol, 40, 590-5, bTopWallCol);
assign hitLeftWall = bLeftWallRow && bLeftWallCol;
assign bRightWall = bRightWallRow && bRightWallCol;
assign bTopWall = bTopWallRow && bTopWallCol;
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 [8:0] bRow,
input logic [9:0] bCol,
input logic left, right,
output logic hitPaddleLeft, hitPaddleRight, hitPaddleTop,
output logic signal);
logic [9:0] paddlePosition; // left column of paddle
// check if paddle at VGA row and column
logic withinRow, withinCol;
assign withinRow = (row >= 440 && row <= 459);
assign withinCol = (col >= paddlePosition && col <= (paddlePosition+64)) && (col > 39 && col < (590));
assign signal = withinRow && withinCol;
// check for paddle hits: top has precedence?
logic withinTop, withinHeight, withinTopHeight, withinLeft, withinRight;
range_check T (bCol, paddlePosition-5, paddlePosition + 64, withinTop);
range_check TH (bRow, 440 - 5, 440 + 5, withinTopHeight);
assign hitPaddleTop = withinTop && withinTopHeight;
range_check L (bCol, paddlePosition - 5, paddlePosition + 5, withinLeft);
range_check H (bRow, 440 - 5, 459, withinHeight);
assign hitPaddleLeft = withinLeft && withinHeight;
range_check R (bCol, paddlePosition + 64 - 5, paddlePosition + 64, withinRight);
assign hitPaddleRight = withinRight && withinHeight;
always_ff @(posedge CLOCK_50, posedge reset) begin // game clock period
if(reset) begin
paddlePosition <= (275+40)-(32); // middle of game area - half paddle width
end
else if(gameClock) begin
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 + 64 + 5 < 590) begin
paddlePosition <= paddlePosition + 5;
end
else begin
paddlePosition <= paddlePosition;
end
end
else begin
paddlePosition <= paddlePosition;
end
end
end
endmodule: paddle
// This is the ball module. It just handles the ball positioning and signaling
// where the ball is
module ball
(input logic CLOCK_50, gameClock, reset,
input logic [8:0] row,
input logic [9:0] col,
input logic [2:0] dx, dy,
input logic [2:0] gameState,
output logic isBall,
output logic [8:0] bRow,
output logic [9:0] bCol);
assign isBall = ((row >= ballRow) && (row < ballRow + 4) && (col >= ballCol) && (col < ballCol + 4));
always_ff @(posedge CLOCK_50, posedge reset) begin // game clock period
if(reset) begin
bRow <= 420;
bCol <= 400;
end
else if(gameClock) begin
if(gameState == 0) begin
bRow <= 420;
bRow <= 400;
end else begin
bRow <= bRow + dy;
bCol <= bCol + dx;
end
end
end
endmodule: ball
//////////////////////////// CHIP INTERFACE ///////////////////////////
module ChipInterface
(input logic CLOCK_50,
input logic [3:0] KEY,
input logic [17:0] SW,
output logic [17:0] LEDR,
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], ~KEY[1], red, green, blue, HEX6, LEDR[11:0]);
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 - 31;
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));
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