-
Notifications
You must be signed in to change notification settings - Fork 0
/
Verilog HDL Source Code.v
1037 lines (999 loc) · 24.2 KB
/
Verilog HDL Source Code.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
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Middle East Technical University
// Engineer: Cem Recai Cirak
//
// Create Date: 09:39:42 06/08/2014
// Design Name:
// Module Name: game
// Project Name: 2-Level FPGA Guitar Hero
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module game(button, level, clock, reset, h_sync, v_sync, red, green, blue, led_r, led_y, led_g, led_b, pfm);
// ****************************************************************************************************
//definition and initialization of wires and registers
//define inputs
input[3:0] button;
input clock;
input level;
input reset;
//define outputs
output h_sync;
output v_sync;
output[2:0] red;
output[2:0] green;
output[1:0] blue;
output led_r;
output led_y;
output led_g;
output led_b;
output pfm;
//define wires
wire clock, level, reset;
wire[3:0] button;
//define registers
reg clk, h_sync, v_sync, led_r, led_y, led_g, led_b, pfm, right_1, wrong_1, right_2, wrong_2, right_3, wrong_3, right_4, wrong_4;
reg[1:0] clk_cnt, blue;
reg[2:0] red, green;
reg[3:0] beat, beat_max, note, note_1, note_2, note_3, note_4, passed_1, passed_2, passed_3, passed_4, i, j, k_1, k_2, k_3, k_4;
reg[5:0] bps_cnt, bps_cnt_max;
reg[7:0] score;
reg[9:0] h_cnt, v_cnt, v_edge_1[15:0], v_edge_2[15:0], v_edge_3[15:0], v_edge_4[15:0];
reg[19:0] f_cnt, f, f_div[3:0];
//reset and initialize registers
always@(posedge clock)
begin
if(reset)
begin
clk = 0; //variable
h_sync = 0; //output
v_sync = 0; //output
led_r = 0; //output
led_y = 0; //output
led_g = 0; //output
led_b = 0; //output
pfm = 0; //output
right_1 = 0; //variable
wrong_1 = 0; //variable
right_2 = 0; //variable
wrong_2 = 0; //variable
right_3 = 0; //variable
wrong_3 = 0; //variable
right_4 = 0; //variable
wrong_4 = 0; //variable
//2-bit
clk_cnt = 0; //variable
blue = 0; //output
//3-bit
red = 0; //output
green = 0; //output
//4-bit
beat = 0; //variable
beat_max = 0; //variable
note = 0; //variable
note_1 = 0; //variable
note_2 = 0; //variable
note_3 = 0; //variable
note_4 = 0; //variable
passed_1 = 0; //variable
passed_2 = 0; //variable
passed_3 = 0; //variable
passed_4 = 0; //variable
i = 0; //variable
j = 0; //variable
k_1 = 0; //variable
k_2 = 0; //variable
k_3 = 0; //variable
k_4 = 0; //variable
//6-bit
bps_cnt = 0; //variable
bps_cnt_max = 0; //variable
//8-bit
score = 0; //variable
//10-bit
h_cnt = 0; //variable
v_cnt = 0; //variable
v_edge_1[0] = 0; //variable
v_edge_1[1] = 0; //variable
v_edge_1[2] = 0; //variable
v_edge_1[3] = 0; //variable
v_edge_1[4] = 0; //variable
v_edge_1[5] = 0; //variable
v_edge_1[6] = 0; //variable
v_edge_1[7] = 0; //variable
v_edge_1[8] = 0; //variable
v_edge_1[9] = 0; //variable
v_edge_1[10] = 0; //variable
v_edge_1[11] = 0; //variable
v_edge_1[12] = 0; //variable
v_edge_1[13] = 0; //variable
v_edge_1[14] = 0; //variable
v_edge_1[15] = 0; //variable
v_edge_2[0] = 0; //variable
v_edge_2[1] = 0; //variable
v_edge_2[2] = 0; //variable
v_edge_2[3] = 0; //variable
v_edge_2[4] = 0; //variable
v_edge_2[5] = 0; //variable
v_edge_2[6] = 0; //variable
v_edge_2[7] = 0; //variable
v_edge_2[8] = 0; //variable
v_edge_2[9] = 0; //variable
v_edge_2[10] = 0; //variable
v_edge_2[11] = 0; //variable
v_edge_2[12] = 0; //variable
v_edge_2[13] = 0; //variable
v_edge_2[14] = 0; //variable
v_edge_2[15] = 0; //variable
v_edge_3[0] = 0; //variable
v_edge_3[1] = 0; //variable
v_edge_3[2] = 0; //variable
v_edge_3[3] = 0; //variable
v_edge_3[4] = 0; //variable
v_edge_3[5] = 0; //variable
v_edge_3[6] = 0; //variable
v_edge_3[7] = 0; //variable
v_edge_3[8] = 0; //variable
v_edge_3[9] = 0; //variable
v_edge_3[10] = 0; //variable
v_edge_3[11] = 0; //variable
v_edge_3[12] = 0; //variable
v_edge_3[13] = 0; //variable
v_edge_3[14] = 0; //variable
v_edge_3[15] = 0; //variable
v_edge_4[0] = 0; //variable
v_edge_4[1] = 0; //variable
v_edge_4[2] = 0; //variable
v_edge_4[3] = 0; //variable
v_edge_4[4] = 0; //variable
v_edge_4[5] = 0; //variable
v_edge_4[6] = 0; //variable
v_edge_4[7] = 0; //variable
v_edge_4[8] = 0; //variable
v_edge_4[9] = 0; //variable
v_edge_4[10] = 0; //variable
v_edge_4[11] = 0; //variable
v_edge_4[12] = 0; //variable
v_edge_4[13] = 0; //variable
v_edge_4[14] = 0; //variable
v_edge_4[15] = 0; //variable
//20-bit
f_cnt = 0; //variable
f_div = 0; //variable
end
end
// ****************************************************************************************************
//100 mhz to 25 mhz clock converter
always@(posedge clock)
begin
if(clk_cnt < 3)
begin
if(clk_cnt == 0)
begin
clk = ~clk;
end
clk_cnt = clk_cnt + 1;
end
else
begin
clk_cnt = 0;
end
end
// ****************************************************************************************************
//horizontal and vertical synchronisation
always@(posedge clk)
begin
if(h_cnt < 800)
begin
if(h_cnt < 96)
begin
h_sync = 0;
h_cnt = h_cnt + 1;
end
else
begin
h_sync = 1;
h_cnt = h_cnt + 1;
end
end
else
begin
if(v_cnt < 525)
begin
if(v_cnt < 2)
begin
v_sync = 0;
v_cnt = v_cnt + 1;
end
else
begin
v_sync = 1;
v_cnt = v_cnt + 1;
end
end
else
begin
v_cnt = 0;
end
h_cnt = 0;
end
end
// ****************************************************************************************************
//draw reference line and vertical brackerts
always@(posedge clk)
begin
if((((390 < v_cnt < 395) || (455 < v_cnt < 460)) && (200 < h_cnt < 625)) || (200 < h_cnt < 205) || (305 < h_cnt < 310) || (410 < h_cnt < 415) || (515 < h_cnt < 520) || (620 < h_cnt < 625))
begin
red = 7;
green = 7;
blue = 3;
end
end
// ****************************************************************************************************
//beat maker
//one or half second counter
always@(posedge clk)
begin
if(bps_cnt_max == 0)
begin
if(level)
begin
bps_cnt_max = 29;
end
else
begin
bps_cnt_max = 59;
end
end
end
//one or two beat per second metronome
always@(posedge clk)
begin
if(v_cnt == 0 && h_cnt == 0)
begin
if(bps_cnt < bps_cnt_max)
begin
bps_cnt = bps_cnt + 1;
end
else
begin
bps_cnt = 0;
end
end
end
//beat counter
always@(posedge clk)
begin
if(beat_max == 0)
begin
if(level)
begin
beat_max = 15;
end
else
begin
beat_max = 7;
end
end
end
always@(posedge clk)
begin
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
if(beat < beat_max)
begin
beat = beat + 1;
end
else
begin
beat = 0;
end
end
end
// ****************************************************************************************************
//song compositon
always@(posedge clk)
begin
if(note == 0)
begin
if(level)
begin
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
if(beat == 0)
begin
note = 5;
end
if(beat == 1)
begin
note = 6;
end
if(beat == 2)
begin
note = 9;
end
if(beat == 3)
begin
note = 4;
end
if(beat == 4)
begin
note = 12;
end
if(beat == 5)
begin
note = 8;
end
if(beat == 6)
begin
note = 13;
end
if(beat == 7)
begin
note = 3;
end
if(beat == 8)
begin
note = 3;
end
if(beat == 9)
begin
note = 12;
end
if(beat == 10)
begin
note = 10;
end
if(beat == 11)
begin
note = 14;
end
if(beat == 12)
begin
note = 8;
end
if(beat == 13)
begin
note = 5;
end
if(beat == 14)
begin
note = 9;
end
if(beat == 15)
begin
note = 2;
end
end
end
else
begin
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
if(beat == 0)
begin
note = 1;
end
if(beat == 1)
begin
note = 4;
end
if(beat == 2)
begin
note = 1;
end
if(beat == 3)
begin
note = 8;
end
if(beat == 4)
begin
note = 1;
end
if(beat == 5)
begin
note = 4;
end
if(beat == 6)
begin
note = 2;
end
if(beat == 7)
begin
note = 1;
end
end
end
end
end
// ****************************************************************************************************
//draw and shift down notes
//check if there is a new note
always@(posedge clk)
begin
//if a new note_1 comes
if(note == 1 || note == 3 || note == 5 || note == 7 || note == 9 || note == 11 || note == 13 || note == 15)
begin
note = 0;
note_1 = note_1 + 1;
end
//if a new note_2 comes
if(note == 2 || note == 3 || note == 6 || note == 7 || note == 10 || note == 11 || note == 14 || note == 15)
begin
note = 0;
note_2 = note_2 + 1;
end
//if a new note_3 comes
if(note == 4 || note == 5 || note == 6 || note == 7 || note == 12 || note == 13 || note == 14 || note == 15)
begin
note = 0;
note_3 = note_3 + 1;
end
//if a new note_4 comes
if(note == 8 || note == 9 || note == 10 || note == 11 || note == 12 || note == 13 || note == 14 || note == 15)
begin
note = 0;
note_4 = note_4 + 1;
end
end
//shift down all generated notes at each new frame
//shift note 1
always@(posedge clk)
begin
if(k_1 <= note_1)
begin
if(v_edge_1[k_1] < 526)
begin
if((204 < h_cnt < 306) && (v_edge_1[k_1] < v_cnt < (v_edge_1[k_1] + 60)))
begin
if(35 < v_cnt < 516)
begin
red = 7;
green = 0;
blue = 0;
end
if(level)
begin
v_edge_1[k_1] = v_edge_1[k_1] + 2 * (note_1 - passed_1);
end
else
begin
v_edge_1[k_1] = v_edge_1[k_1] + (note_1 - passed_1);
end
end
end
else
begin
passed_1 = passed_1 + 1;
end
if(v_cnt == 0 && h_cnt ==0)
begin
if(k_1 < note_1)
begin
k_1 = k_1 + 1;
end
else
begin
k_1 = passed_1;
end
end
end
end
//shift note 2
always@(posedge clk)
begin
if(k_2 <= note_2)
begin
if(v_edge_2[k_2] < 526)
begin
if((309 < h_cnt < 411) && (v_edge_2[k_2] < v_cnt < (v_edge_2[k_2] + 60)))
begin
if(35 < v_cnt < 516)
begin
red = 7;
green = 7;
blue = 0;
end
if(level)
begin
v_edge_2[k_2] = v_edge_2[k_2] + 2 * (note_2 - passed_2);
end
else
begin
v_edge_2[k_2] = v_edge_2[k_2] + (note_2 - passed_2);
end
end
end
else
begin
passed_2 = passed_2 + 1;
end
if(v_cnt == 0 && h_cnt ==0)
begin
if(k_2 < note_2)
begin
k_2 = k_2 + 1;
end
else
begin
k_2 = passed_2;
end
end
end
end
//shift note 3
always@(posedge clk)
begin
if(k_3 <= note_3)
begin
if(v_edge_3[k_3] < 526)
begin
if((414 < h_cnt < 516) && (v_edge_3[k_3] < v_cnt < (v_edge_3[k_3] + 60)))
begin
if(35 < v_cnt < 516)
begin
red = 0;
green = 7;
blue = 0;
end
if(level)
begin
v_edge_3[k_3] = v_edge_3[k_3] + 2 * (note_3 - passed_3);
end
else
begin
v_edge_3[k_3] = v_edge_3[k_3] + (note_3 - passed_3);
end
end
end
else
begin
passed_3 = passed_3 + 1;
end
if(v_cnt == 0 && h_cnt ==0)
begin
if(k_3 < note_3)
begin
k_3 = k_3 + 1;
end
else
begin
k_3 = passed_3;
end
end
end
end
//shift note 4
always@(posedge clk)
begin
if(k_4 <= note_4)
begin
if(v_edge_4[k_4] < 526)
begin
if((519 < h_cnt < 621) && (v_edge_4[k_4] < v_cnt < (v_edge_4[k_4] + 60)))
begin
if(35 < v_cnt < 516)
begin
red = 0;
green = 0;
blue = 3;
end
if(level)
begin
v_edge_4[k_4] = v_edge_4[k_4] + 2 * (note_4 - passed_4);
end
else
begin
v_edge_4[k_4] = v_edge_4[k_4] + (note_4 - passed_4);
end
end
end
else
begin
passed_4 = passed_4 + 1;
end
if(v_cnt == 0 && h_cnt ==0)
begin
if(k_4 < note_4)
begin
k_4 = k_4 + 1;
end
else
begin
k_4 = passed_4;
end
end
end
end
// ****************************************************************************************************
//light up leds and update score
//if button 1 is pressed
always@(posedge clk)
begin
if(button == 1 || button == 3 || button == 5 || button == 7 || button == 9 || button == 11 || button == 13 || button == 15)
begin
if(right_1 == 0 && wrong_1 == 0)
begin
for(j = 0; j < 7; j = j + 1)
begin
if(380 < v_edge_1[j] < 410)
begin
right_1 = 1;
end
end
if(right_1)
begin
led_r = 1;
end
else
begin
wrong_1 = 1;
end
end
end
if(v_cnt == 0 && h_cnt == 0)
begin
if(right_1)
begin
if(level)
begin
if(score < 190)
begin
score = score + 10;
end
else
begin
score = 200;
end
end
else
begin
if(score < 175)
begin
score = score + 25;
end
else
begin
score = 200;
end
end
end
if(wrong_1)
begin
if(score > 10)
begin
score = score - 10;
end
else
begin
score = 0;
end
end
end
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
right_1 = 0;
wrong_1 = 0;
led_r = 0;
end
end
//if button 2 is pressed
always@(posedge clk)
begin
if(button == 2 || button == 3 || button == 6 || button == 7 || button == 10 || button == 11 || button == 14 || button == 15)
begin
if(right_2 == 0 && wrong_2 == 0)
begin
for(j = 0; j < 6; j = j + 1)
begin
if(380 < v_edge_2[j] < 410)
begin
right_2 = 1;
end
end
if(right_2)
begin
led_y = 1;
end
else
begin
wrong_2 = 1;
end
end
end
if(v_cnt == 0 && h_cnt == 0)
begin
if(right_2)
begin
if(level)
begin
if(score < 190)
begin
score = score + 10;
end
else
begin
score = 200;
end
end
else
begin
if(score < 175)
begin
score = score + 25;
end
else
begin
score = 200;
end
end
end
if(wrong_2)
begin
if(score > 10)
begin
score = score - 10;
end
else
begin
score = 0;
end
end
end
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
right_2 = 0;
wrong_2 = 0;
led_y = 0;
end
end
//if button 3 is pressed
always@(posedge clk)
begin
if(button == 4 || button == 5 || button == 6 || button == 7 || button == 12 || button == 13 || button == 14 || button == 15)
begin
if(right_3 == 0 && wrong_3 == 0)
begin
for(j = 0; j < 8; j = j + 1)
begin
if(380 < v_edge_3[j] < 410)
begin
right_3 = 1;
end
end
if(right_3)
begin
led_g = 1;
end
else
begin
wrong_3 = 1;
end
end
end
if(v_cnt == 0 && h_cnt == 0)
begin
if(right_3)
begin
if(level)
begin
if(score < 190)
begin
score = score + 10;
end
else
begin
score = 200;
end
end
else
begin
if(score < 175)
begin
score = score + 25;
end
else
begin
score = 200;
end
end
end
if(wrong_3)
begin
if(score > 10)
begin
score = score - 10;
end
else
begin
score = 0;
end
end
end
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
right_3 = 0;
wrong_3 = 0;
led_g = 0;
end
end
//if button 4 is pressed
always@(posedge clk)
begin
if(button == 8 || button == 9 || button == 10 || button == 11 || button == 12 || button == 13 || button == 14 || button == 15)
begin
if(right_4 == 0 && wrong_4 == 0)
begin
for(j = 0; j < 9; j = j + 1)
begin
if(380 < v_edge_4[j] < 410)
begin
right_4 = 1;
end
end
if(right_4)
begin
led_b = 1;
end
else
begin
wrong_4 = 1;
end
end
end
if(v_cnt == 0 && h_cnt == 0)
begin
if(right_4)
begin
if(level)
begin
if(score < 190)
begin
score = score + 10;
end
else
begin
score = 200;
end
end
else
begin
if(score < 175)
begin
score = score + 25;
end
else
begin
score = 200;
end
end
end
if(wrong_4)
begin
if(score > 10)
begin
score = score - 10;
end
else
begin
score = 0;
end
end
end
if(bps_cnt == 0 && v_cnt == 0 && h_cnt == 0)
begin
right_4 = 0;
wrong_4 = 0;
led_b = 0;
end
end
// ****************************************************************************************************
//draw score bar
always@(posedge clk)
begin
if(((675 < h_cnt < 690) && ((375 - score) < v_cnt < 375)) || (((665 < h_cnt < 700))&&(375 < v_cnt < 380)))
begin
red = 0;
green = 7;
blue = 3;
end
end
// ****************************************************************************************************
//audio output waveform generator
//change frequency divider with respect to button combination
always@(posedge clk)
begin
if(button)
begin
if(button == 1)
begin
f_div = 213675;
end
if(button == 2)
begin
f_div = 120192;
end
if(button == 3)
begin
f_div = 179859;
end
if(button == 4)
begin
f_div = 90253;
end
if(button == 5)
begin
f_div = 160256;
end
if(button == 6)
begin
f_div = 107296;
end
if(button == 7)
begin
f_div = 135135;
end
if(button == 8)
begin
f_div = 30084;
end
if(button == 9)
begin
f_div = 60240;
end
if(button == 10)
begin
f_div = 40193;
end
if(button == 11)
begin
f_div = 80386;
end
if(button == 12)
begin
f_div = 33784;
end
if(button == 13)