-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.v
2080 lines (1966 loc) · 68 KB
/
tetris.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
`include "./IR.v"
`include "./LCD.v"
`include "./PS2_KEYBOARD2.v"
`include "./beep.v"
`include "./SEG_7.v"
//a`include "./IMG.v"
module tetris( clk,
rst,
IRDA_RXD,
SW,
LCD_EN,
LCD_RW,
LCD_RS,
LCD_DATA,
VGA_HS,
VGA_VS,
VGA_R,
VGA_G,
VGA_B,
VGA_BLANK_N,
VGA_CLOCK,
PS2_CLK,
PS2_DAT,
LEDR,
LEDG,
beep,
HEX0,
HEX2,
HEX3
);
input clk; //clk 50MHz
input rst; //重製訊號
//=====================<顯示等級>=========================
output reg [6:0] HEX3, HEX2, HEX0;
always @(posedge clk, negedge rst)begin
if(!rst)begin
{HEX3[0],HEX3[1],HEX3[2],HEX3[3],HEX3[4],HEX3[5],HEX3[6]} <= 7'bzzz_zzzz;
{HEX2[0],HEX2[1],HEX2[2],HEX2[3],HEX2[4],HEX2[5],HEX2[6]} <= 7'bzzz_zzzz;
{HEX0[0],HEX0[1],HEX0[2],HEX0[3],HEX0[4],HEX0[5],HEX0[6]} <= 7'bzzz_zzzz;
end
else begin
{HEX3[0],HEX3[1],HEX3[2],HEX3[3],HEX3[4],HEX3[5],HEX3[6]} <= 7'b111_0001;
{HEX2[0],HEX2[1],HEX2[2],HEX2[3],HEX2[4],HEX2[5],HEX2[6]} <= 7'b100_0001;
{HEX0[0],HEX0[1],HEX0[2],HEX0[3],HEX0[4],HEX0[5],HEX0[6]} <= seg_out;
end
end
wire [3:0] seg_speed_input;
assign seg_speed_input = IR_speed + 1'b1;
wire [6:0] seg_out;
SEG_7 seg_test(seg_speed_input, seg_out);
//=====================<聲音部分>=========================
output beep; //聲音
output reg [8:0] LEDG;
wire [2:0] level;
assign LEDR[17] = voice_check;
assign level = voice_level;
always @(*)begin
case(voice_level)
3'd0 : LEDG[7:0] = 8'b0000_0001;
3'd1 : LEDG[7:0] = 8'b0000_0011;
3'd2 : LEDG[7:0] = 8'b0000_0111;
3'd3 : LEDG[7:0] = 8'b0000_1111;
3'd4 : LEDG[7:0] = 8'b0001_1111;
3'd5 : LEDG[7:0] = 8'b0011_1111;
3'd6 : LEDG[7:0] = 8'b0111_1111;
3'd7 : LEDG[7:0] = 8'b1111_1111;
endcase
end
reg [2:0] voice_level;
reg [2:0] voice_save;
reg voice_check;
always @(negedge oDATA_READY, negedge rst)begin
if(!rst)begin
voice_level <= 3'd4;
voice_save <= 3'd0;
voice_check <= 1'd0;
end
else begin
case(oDATA[23:16])
8'h0C:begin
if(voice_check==1'd0)begin
voice_save <=voice_level;
voice_level <= 3'd0;
voice_check <= 1'd1;
end
else begin
voice_level <=voice_save;
voice_check <= 1'd0;
end
end
8'h1B:begin
if(voice_check)begin
voice_level <= voice_level + voice_save + 3'd1;
voice_check <= 1'd0;
end
else begin
if(voice_level < 3'd7)begin
voice_level <= voice_level + 3'd1;
end
end
end
8'h1F:begin
if(voice_check)begin
voice_level <= voice_level + voice_save - 3'd1;
voice_check <= 1'd0;
end
else begin
if(voice_level > 1)begin
voice_level <= voice_level - 3'd1;
end
end
end
endcase
end
end
//==========<樂譜>=================
beep music_1(clk, rst,level, beep);
//input KEY_1; //按鈕,開始
//==========<PS2>=================
input PS2_CLK; //PS2鍵盤訊號輸入
inout PS2_DAT; //PS2鍵盤資料輸入
wire [7:0] scandata; //掃描的資料
wire key1_on; //按鍵1
wire key2_on; //按鍵2
wire key3_on; //按鍵3
wire [7:0] key1_code; //按鍵1資料
wire [7:0] key2_code; //按鍵2資料
wire [7:0] key3_code; //按鍵3資料
output [17:0] LEDR; //檢測鍵盤
assign {LEDR[16:6],LEDR[0]} = 12'bzzzz_zzzz_zzzz;
assign LEDR[1] = key1_on; //存載上下左右
assign LEDR[2] = key2_on; //存載選轉
assign LEDR[3] = key3_on; //檢測快速降落
assign LEDR[4] = change_cnt; //檢測是否hold過了
assign LEDR[5] = hold_check; //hold第一次檢測,已經有一次以上亮燈
//wire get_keyboard1_on; //檢測keyboad是否有被按下至少一個鍵
//assign
//wire reset_1;
//assign reset_1 = (scandata == 8'hf0) ? 1'b0 : 1'b1;
PS2_KEYBOARD2 ps2_test( .clk(clk),
.rst(rst),
.rst1(1'b1),
.PS2_DAT(PS2_DAT),
.PS2_CLK(PS2_CLK),
.scandata(scandata),
.key1_on(key1_on),
.key2_on(key2_on),
.key3_on(key3_on),
.key1_code(key1_code),
.key2_code(key2_code),
.key3_code(key3_code)
);
//==========<IR>=================
input IRDA_RXD; //接收到紅外線
wire [31:0] oDATA; //接收IR的資料
wire oDATA_READY; //IR確實收到資料
//==============================
input [17:0] SW; //switch開關
//==========<LCD>=================
output LCD_EN; //LCD控制線
output LCD_RW; //LCD控制線
output LCD_RS; //LCD控制線
inout [7:0] LCD_DATA; //LCD資料
//================================
//==========<VGA>=================
output VGA_HS, VGA_VS;
output reg [7:0] VGA_R,VGA_G,VGA_B;
output VGA_BLANK_N,VGA_CLOCK;
//================================
reg VGA_HS, VGA_VS;
reg[10:0] counterHS;
reg[9:0] counterVS;
reg [2:0] valid;
reg clk25M;
//============<狀態機>===================
reg [2:0] state, nextstate;
parameter START = 3'd0; //開始遊戲
parameter NEW_SHAPE = 3'd1; //產生第一個圖形
parameter DECLINE = 3'd2; //方塊降落
parameter SHIFT_ON = 3'd3; //觸發選轉
parameter REMOVE = 3'd4; //落下後檢測是否有需要消除的
parameter DIED = 3'd5; //死亡
parameter PLACE = 3'd6; //放置
parameter HOLD = 3'd7; //保存
IR ir2(clk, rst, IRDA_RXD, oDATA_READY, oDATA);
parameter H_FRONT = 16;
parameter H_SYNC = 96;
parameter H_BACK = 48;
parameter H_ACT = 640;
parameter H_BLANK = H_FRONT + H_SYNC + H_BACK;
parameter H_TOTAL = H_FRONT + H_SYNC + H_BACK + H_ACT;
reg [9:0] time_cnt; //時間
reg [6:0] score; //分數
//reg [12:0] objY,objX; //物體的座標
reg [12:0] X,Y;
wire IR_CLK_1S; //除頻訊號1
wire IR_CLK_10S; //除頻訊號10
reg reg_IR_CLK_1S; //暫存1
reg reg_IR_CLK_10S; //暫存10
reg [1:0] IR_speed; //IR調整速度
assign IR_CLK_1S = reg_IR_CLK_1S; //暫存轉移
assign IR_CLK_10S = reg_IR_CLK_10S; //暫存轉移
always @(*)begin
case(IR_speed)
2'd0:begin
reg_IR_CLK_1S = first_clk_1;
reg_IR_CLK_10S = first_clk_10;
end
2'd1:begin
reg_IR_CLK_1S = second_clk_1;
reg_IR_CLK_10S = second_clk_10;
end
2'd2:begin
reg_IR_CLK_1S = third_clk_1;
reg_IR_CLK_10S = third_clk_10;
end
2'd3:begin
reg_IR_CLK_1S = forth_clk_1;
reg_IR_CLK_10S = forth_clk_10;
end
endcase
end
//assign (IR_CLK_1S) = (IR_speed == 2'd3) ? forth_clk_1 : (IR_speed == 2'd2) ? third_clk_1 : (IR_speed == 2'd1) ? second_clk_1 : first_clk_1;
//assign (IR_CLK_10S) = (IR_speed == 2'd3) ? forth_clk_10 : (IR_speed == 2'd2) ? third_clk_10 : (IR_speed == 2'd1) ? second_clk_10 : first_clk_10;
//=============<速度調節>=================
always@(negedge oDATA_READY, negedge rst)begin
if (!rst)begin
IR_speed <= 2'd0;
end
else begin
case(oDATA[23:16])
8'h1A:begin
if(IR_speed < 2'd3)begin
IR_speed <= IR_speed + 2'd1;
end
end
8'h1E:begin
if(IR_speed > 2'd0)begin
IR_speed <= IR_speed - 2'd1;
end
end
endcase
end
end
reg [25:0] down_speed;// 下降速度
always @(posedge clk, negedge rst)begin
if(!rst)begin
down_speed <= 26'd5000_0000;
end
else begin
if( state == DECLINE && key3_code == 8'h29 )begin
down_speed <= 26'd50;
end
else if(key3_code == 8'h72 )begin
down_speed <= 26'd500_0000;
end
else begin
case(IR_speed)
2'd0:begin
down_speed <= 26'd5000_0000;
end
2'd1:begin
down_speed <= 26'd2500_0000;
end
2'd2:begin
down_speed <= 26'd1600_0000;
end
2'd3:begin
down_speed <= 26'd1250_0000;
end
endcase
end
end
end
wire time_clk;
counterDivider_TETRIS #(26) time_cnt_1(clk, rst, time_clk, 26'd5000_0000); //除頻5000萬,時間time_clk
wire first_clk_1;
wire first_clk_10;
//======================<第一組速度>=====================
counterDivider_TETRIS #(23) cnt_first_1(clk25M, rst, first_clk_1, 26'd500_0000); //除頻500萬,操作速度相當於0.1as
counterDivider_TETRIS #(26) cnt_first_10(clk , rst, first_clk_10, down_speed); //除頻5000萬
wire second_clk_1;
wire second_clk_10;
//======================<第二組速度>=====================
counterDivider_TETRIS #(23) cnt_second_1(clk25M, rst, second_clk_1, 26'd400_0000); //除頻500萬,操作速度相當於0.2s
counterDivider_TETRIS #(25) cnt_sceond_10(clk , rst, second_clk_10, down_speed); //除頻2500萬
wire third_clk_1;
wire third_clk_10;
//======================<第三組速度>=====================
counterDivider_TETRIS #(23) cnt_third_1(clk25M, rst, third_clk_1, 26'd360_0000); //除頻500萬,操作速度相當於0.2s
counterDivider_TETRIS #(24) cnt_third_10(clk , rst, third_clk_10, down_speed); //除頻1600萬
wire forth_clk_1;
wire forth_clk_10;
//======================<第四組速度>=====================
counterDivider_TETRIS #(23) cnt_forth_1(clk25M, rst, forth_clk_1, 26'd330_0000); //除頻500萬,操作速度相當於0.2s
counterDivider_TETRIS #(24) cnt_forth_10(clk , rst, forth_clk_10, down_speed); //除頻1250萬
parameter V_FRONT = 11;
parameter V_SYNC = 2;
parameter V_BACK = 32;
parameter V_ACT = 480;//
parameter V_BLANK = V_FRONT + V_SYNC + V_BACK;
parameter V_TOTAL = V_FRONT + V_SYNC + V_BACK + V_ACT;
assign VGA_SYNC_N = 1'b0;
assign VGA_BLANK_N = ~((counterHS<H_BLANK)||(counterVS<V_BLANK));
assign VGA_CLOCK = ~clk25M;
//================<LCD顯示資料>===================
wire [9:0] wire_time;
wire [6:0] wire_score;
assign wire_score = score;
assign wire_time = time_cnt;
//=====================<tetris data>============================
reg [9:0] board [23:0]; //20寬度*10高度 4個高度保留值
reg [9:0] check_board [23:0]; //檢測消除的board
reg [5:0] pos_x; //0~15 X座標
reg signed [5:0] pos_y; //0~31 Y座標
reg [4:0] shape; //七種圖形
reg [4:0] n_shape; //下一個圖形
reg [1:0] rotation_choose; //選擇的選轉
parameter initial_shape_pos_x = 6'd4;
parameter initial_shape_pos_y = -6'd3;
/*
0 -> O
1 -> I
2 -> S
3 -> Z
4 -> L
5 -> J
6 -> T
*/
wire [15:0] graph [31:0];
//O
assign graph[0] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0011}};
assign graph[1] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0011}};
assign graph[2] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0011}};
assign graph[3] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0011}};
//I
assign graph[4] = {
{4'b0000},
{4'b0000},
{4'b0000},
{4'b1111}};
assign graph[5] = {
{4'b0001},
{4'b0001},
{4'b0001},
{4'b0001}};
assign graph[6] = {
{4'b0000},
{4'b0000},
{4'b0000},
{4'b1111}};
assign graph[7] = {
{4'b0001},
{4'b0001},
{4'b0001},
{4'b0001}};
//S
assign graph[8] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0110}};
assign graph[9] = {
{4'b0000},
{4'b0010},
{4'b0011},
{4'b0001}};
assign graph[10] = {
{4'b0000},
{4'b0000},
{4'b0011},
{4'b0110}};
assign graph[11] = {
{4'b0000},
{4'b0010},
{4'b0011},
{4'b0001}};
//Z
assign graph[12] = {
{4'b0000},
{4'b0000},
{4'b0110},
{4'b0011}};
assign graph[13] = {
{4'b0000},
{4'b0001},
{4'b0011},
{4'b0010}};
assign graph[14] = {
{4'b0000},
{4'b0000},
{4'b0110},
{4'b0011}};
assign graph[15] = {
{4'b0000},
{4'b0001},
{4'b0011},
{4'b0010}};
//L
assign graph[16] = {
{4'b0000},
{4'b0000},
{4'b0111},
{4'b0100}};
assign graph[17] = {
{4'b0000},
{4'b0010},
{4'b0010},
{4'b0011}};
assign graph[18] = {
{4'b0000},
{4'b0000},
{4'b0001},
{4'b0111}};
assign graph[19] = {
{4'b0000},
{4'b0011},
{4'b0001},
{4'b0001}};
//J
assign graph[20] = {
{4'b0000},
{4'b0000},
{4'b0111},
{4'b0001}};
assign graph[21] = {
{4'b0000},
{4'b0011},
{4'b0010},
{4'b0010}};
assign graph[22] = {
{4'b0000},
{4'b0000},
{4'b0100},
{4'b0111}};
assign graph[23] = {
{4'b0000},
{4'b0001},
{4'b0001},
{4'b0011}};
//T
assign graph[24] = {
{4'b0000},
{4'b0000},
{4'b0010},
{4'b0111}};
assign graph[25] = {
{4'b0000},
{4'b0001},
{4'b0011},
{4'b0001}};
assign graph[26] = {
{4'b0000},
{4'b0000},
{4'b0111},
{4'b0010}};
assign graph[27] = {
{4'b0000},
{4'b0010},
{4'b0011},
{4'b0010}};
//T overflow
assign graph[28] = {
{4'b0000},
{4'b0000},
{4'b0010},
{4'b0111}};
assign graph[29] = {
{4'b0000},
{4'b0001},
{4'b0011},
{4'b0001}};
assign graph[30] = {
{4'b0000},
{4'b0000},
{4'b0111},
{4'b0010}};
assign graph[31] = {
{4'b0000},
{4'b0010},
{4'b0011},
{4'b0010}};
reg [1:0] gameState;
always@(*)begin
case(state)
START : gameState = 2'd0;
NEW_SHAPE : gameState = 2'd1;
DECLINE : gameState = 2'd1;
SHIFT_ON : gameState = 2'd1;
REMOVE : gameState = 2'd1;
PLACE : gameState = 2'd1;
DIED : gameState = 2'd2;
endcase
end
LCD lcd1(clk, rst, gameState, wire_time, wire_score, LCD_DATA, LCD_EN, LCD_RW, LCD_RS, DATA_IN);
always@(posedge clk)
clk25M = ~clk25M;
//===========<狀態選擇>===================
always @(posedge IR_CLK_10S,negedge rst)begin
if(!rst)begin
state <= START;
end
else begin
state <= nextstate;
end
end
reg change_cnt;
always @(posedge clk, negedge rst)begin
if(!rst)begin
change_cnt <= 1'd0;
end
else begin
case(state)
START:begin
change_cnt <= 1'd0;
end
NEW_SHAPE:begin
change_cnt <= 1'd0;
end
PLACE:begin
change_cnt <= 1'd0;
end
HOLD:begin
change_cnt <= 1'd1;
end
endcase
end
end
reg hold_check;
always @(posedge IR_CLK_10S, negedge rst)begin
if(!rst)begin
hold_check <= 1'd0;
end
else begin
case(state)
START:begin
hold_check <= 1'd0;
end
HOLD:begin
hold_check <= 1'd1;
end
endcase
end
end
//============<狀態轉移>===================
always @(*)begin
case(state)
START:begin
if(key1_code == 8'h5A)begin
nextstate = NEW_SHAPE;
end
else begin
nextstate = START;
end
end
NEW_SHAPE:begin
nextstate = DECLINE;
end
DECLINE:begin
if(graph[(cur_shape[2:0]*4) + rotation_choose][15]==1'b1 &&( ((pos_y + 3) >= 18) || (board[pos_y+4+5][pos_x+3]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][14]==1'b1 &&( ((pos_y + 3) >= 18) || (board[pos_y+4+5][pos_x+2]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][13]==1'b1 &&( ((pos_y + 3) >= 18) || (board[pos_y+4+5][pos_x+1]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][12]==1'b1 &&( ((pos_y + 3) >= 18) || (board[pos_y+4+5][pos_x+0]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][11]==1'b1 &&( ((pos_y + 2) >= 18) || (board[pos_y+3+5][pos_x+3]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][10]==1'b1 &&( ((pos_y + 2) >= 18) || (board[pos_y+3+5][pos_x+2]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][9]==1'b1 &&( ((pos_y + 2) >= 18) || (board[pos_y+3+5][pos_x+1]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][8]==1'b1 &&( ((pos_y + 2) >= 18) || (board[pos_y+3+5][pos_x+0]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][7]==1'b1 &&( ((pos_y + 1) >= 18) || (board[pos_y+2+5][pos_x+3]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][6]==1'b1 &&( ((pos_y + 1) >= 18) || (board[pos_y+2+5][pos_x+2]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][5]==1'b1 &&( ((pos_y + 1) >= 18) || (board[pos_y+2+5][pos_x+1]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][4]==1'b1 &&( ((pos_y + 1) >= 18) || (board[pos_y+2+5][pos_x+0]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][3]==1'b1 &&( ((pos_y + 0) >= 18) || (board[pos_y+1+5][pos_x+3]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][2]==1'b1 &&( ((pos_y + 0) >= 18) || (board[pos_y+1+5][pos_x+2]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][1]==1'b1 &&( ((pos_y + 0) >= 18) || (board[pos_y+1+5][pos_x+1]==1'b1)))begin
nextstate = PLACE;
end
else if(graph[(cur_shape[2:0]*4) + rotation_choose][0]==1'b1 &&( ((pos_y + 0) >= 18) || (board[pos_y+1+5][pos_x+0]==1'b1)))begin
nextstate = PLACE;
end
else if((key3_code == 8'h21 || key3_code == 8'h12)&& change_cnt == 1'd0)begin
nextstate = HOLD;
end
/*else if(key1_code == 8'h12 || key2_code == 8'h12)begin
nextstate = SHIFT_ON;
end*/
else begin
nextstate = DECLINE;
end
end
/*SHIFT_ON:begin
nextstate = DECLINE;
end*/
PLACE:begin
if(|board[4])begin
nextstate = DIED;
end
else begin
nextstate = REMOVE;
end
end
REMOVE:begin
if(remove_cnt<=3)begin
nextstate = NEW_SHAPE;
end
else begin
nextstate = REMOVE;
end
end
HOLD:begin
if(hold_check == 1'b0)begin
nextstate = NEW_SHAPE;
end
else begin
nextstate = DECLINE;
end
end
DIED:begin
nextstate = DIED;
end
default:begin
nextstate = START;
end
endcase
end
always@(posedge clk25M)
begin
if(!rst)
counterHS <= 0;
else begin
if(counterHS == H_TOTAL)
counterHS <= 0;
else
counterHS <= counterHS + 1'b1;
if(counterHS == H_FRONT-1)
VGA_HS <= 1'b0;
if(counterHS == H_FRONT + H_SYNC -1)
VGA_HS <= 1'b1;
if(counterHS >= H_BLANK)
X <= counterHS-H_BLANK;
else
X <= 0;
end
end
always@(posedge clk25M)
begin
if(!rst)
counterVS <= 0;
else begin
if(counterVS == V_TOTAL)
counterVS <= 0;
else if(counterHS == H_TOTAL)
counterVS <= counterVS + 1'b1;
if(counterVS == V_FRONT-1)
VGA_VS <= 1'b0;
if(counterVS == V_FRONT + V_SYNC -1)
VGA_VS <= 1'b1;
if(counterVS >= V_BLANK)
Y <= counterVS-V_BLANK;
else
Y <= 0;
end
end
reg [23:0]color[19:0]; //顏色區塊
reg [3:0] current_color;
always @(posedge clk, negedge rst)begin
if(!rst)begin
current_color <= 4'd0;
end
else begin
case(cur_shape[2:0])
3'd0:current_color <= 4'd6;
3'd1:current_color <= 4'd7;
3'd2:current_color <= 4'd8;
3'd3:current_color <= 4'd9;
3'd4:current_color <= 4'd10;
3'd5:current_color <= 4'd11;
3'd6:current_color <= 4'd12;
3'd7:current_color <= 4'd12;
endcase
end
end
reg [3:0] next_color;
always @(posedge clk, negedge rst)begin
if(!rst)begin
next_color <= 4'd0;
end
else begin
case(n_shape[2:0])
3'd0:next_color <= 4'd6;
3'd1:next_color <= 4'd7;
3'd2:next_color <= 4'd8;
3'd3:next_color <= 4'd9;
3'd4:next_color <= 4'd10;
3'd5:next_color <= 4'd11;
3'd6:next_color <= 4'd12;
3'd7:next_color <= 4'd12;
endcase
end
end
reg [4:0] n2_shape; //下一個圖形2
reg [3:0] next2_color;
always @(posedge clk, negedge rst)begin
if(!rst)begin
next2_color <= 4'd0;
end
else begin
case(n2_shape[2:0])
3'd0:next2_color <= 4'd6;
3'd1:next2_color <= 4'd7;
3'd2:next2_color <= 4'd8;
3'd3:next2_color <= 4'd9;
3'd4:next2_color <= 4'd10;
3'd5:next2_color <= 4'd11;
3'd6:next2_color <= 4'd12;
3'd7:next2_color <= 4'd12;
endcase
end
end
reg [4:0] n3_shape; //下一個圖形3
reg [3:0] next3_color;
always @(posedge clk, negedge rst)begin
if(!rst)begin
next3_color <= 4'd0;
end
else begin
case(n3_shape[2:0])
3'd0:next3_color <= 4'd6;
3'd1:next3_color <= 4'd7;
3'd2:next3_color <= 4'd8;
3'd3:next3_color <= 4'd9;
3'd4:next3_color <= 4'd10;
3'd5:next3_color <= 4'd11;
3'd6:next3_color <= 4'd12;
3'd7:next3_color <= 4'd12;
endcase
end
end
reg [3:0] hold_color;
always @(posedge clk, negedge rst)begin
if(!rst)begin
hold_color <= 4'd0;
end
else begin
case(hold_shape[2:0])
3'd0:hold_color <= 4'd6;
3'd1:hold_color <= 4'd7;
3'd2:hold_color <= 4'd8;
3'd3:hold_color <= 4'd9;
3'd4:hold_color <= 4'd10;
3'd5:hold_color <= 4'd11;
3'd6:hold_color <= 4'd12;
3'd7:hold_color <= 4'd12;
endcase
end
end
//========<設定遊戲布局大小和邊框>===========
parameter Board_min_X = 13'd245;
parameter Board_max_X = 13'd395;
parameter Board_min_Y = 13'd40;
parameter Board_max_Y = 13'd440;
parameter Board_frame = 13'd5;
parameter Board_N_shape_min_X = 13'd450;
parameter Board_N_shape_max_X = 13'd510;
parameter Board_N_shape_min_Y = 13'd70;
parameter Board_N_shape_max_Y = 13'd150;
parameter Board_N2_shape_min_Y = 13'd160;
parameter Board_N2_shape_max_Y = 13'd240;
parameter Board_N3_shape_min_Y = 13'd250;
parameter Board_N3_shape_max_Y = 13'd330;
parameter Board_H_shape_min_X = 13'd145;
parameter Board_H_shape_max_X = 13'd205;
parameter LOGO_min_X = 13'd100;
parameter LOGO_max_X = 13'd200;
parameter LOGO_min_Y = 13'd240;
parameter LOGO_max_Y = 13'd309;
parameter ROM_TOTAl = 13'd6900;
parameter G_min_X = 13'd270;
parameter G_max_X = 13'd370;
parameter G_min_Y = 13'd229;
parameter G_max_Y= 13'd251;
parameter GAMEOVER_TXT_TOTAL = 13'd2200;
parameter TXT_TOTAL = 13'd1400;
parameter Board_N_TXT_min_Y = 13'd45;
parameter Board_N_TXT_max_Y = 13'd65;
parameter Board_N_TXT_min_X = 13'd445;
parameter Board_N_TXT_max_X = 13'd515;
parameter Board_H_TXT_min_Y = 13'd45;
parameter Board_H_TXT_max_Y = 13'd65;
parameter Board_H_TXT_min_X = 13'd140;
parameter Board_H_TXT_max_X = 13'd210;
//=================<讓記憶體知道要讀取>==========================
wire read;
assign read = (X>=LOGO_min_X && X<LOGO_max_X && Y>= LOGO_min_Y && Y<LOGO_max_Y) ? 1'b1 : 1'b0;
//=================<選定的記憶體位置>=============================
reg [12:0] read_addr;
always @(posedge clk25M, negedge rst)begin
if(!rst)begin
read_addr <= 13'd0;
end
else if(read)begin
if(read_addr < ROM_TOTAl - 1'd1)begin
read_addr <= read_addr + 1'd1;
end
else begin
read_addr <= 13'd0;
end
end
end
wire [23:0] rom_data; //讀出資料
//===========<調用IP核>=================
pic_rom pic_rom_inst(
.clock(clk), //訊號
.address(read_addr), //地址
.rden(read), //讀寫確定
.q(rom_data) //讀出資料
);
//=================<讓記憶體知道要讀取>==========================
wire n_read;
assign n_read = (X>=Board_N_TXT_min_X && X<Board_N_TXT_max_X && Y>= Board_N_TXT_min_Y && Y<Board_N_TXT_max_Y) ? 1'b1 : 1'b0;
//=================<選定的記憶體位置>=============================
reg [12:0] n_read_addr;
always @(posedge clk25M, negedge rst)begin
if(!rst)begin
n_read_addr <= 13'd0;
end
else if(n_read)begin
if(n_read_addr < TXT_TOTAL - 1'd1)begin
n_read_addr <= n_read_addr + 1'd1;
end
else begin
n_read_addr <= 13'd0;
end
end
end
wire n_rom_data; //讀出資料
//===========<調用IP核>=================
pic_next_rom pic_n_rom_inst(
.clock(clk), //訊號
.address(n_read_addr), //地址
.rden(n_read), //讀寫確定
.q(n_rom_data) //讀出資料
);
//=================<讓記憶體知道要讀取>==========================
wire h_read;
assign h_read = (X>=Board_H_TXT_min_X && X<Board_H_TXT_max_X && Y>= Board_H_TXT_min_Y && Y<Board_H_TXT_max_Y) ? 1'b1 : 1'b0;
//=================<選定的記憶體位置>=============================
reg [12:0] h_read_addr;
always @(posedge clk25M, negedge rst)begin
if(!rst)begin
h_read_addr <= 13'd0;
end
else if(h_read)begin
if(h_read_addr < TXT_TOTAL - 1'd1)begin
h_read_addr <= h_read_addr + 1'd1;
end
else begin
h_read_addr <= 13'd0;
end
end
end
wire h_rom_data; //讀出資料
//===========<調用IP核>=================
pic_hold_rom pic_h_rom_inst(
.clock(clk), //訊號
.address(h_read_addr), //地址
.rden(h_read), //讀寫確定
.q(h_rom_data) //讀出資料
);
//=================<讓記憶體知道要讀取>==========================
wire g_read;
assign g_read = (X>=G_min_X && X<G_max_X && Y>= G_min_Y && Y<G_max_Y) ? 1'b1 : 1'b0;
//=================<選定的記憶體位置>=============================
reg [12:0] g_read_addr;
always @(posedge clk25M, negedge rst)begin
if(!rst)begin
g_read_addr <= 13'd0;
end