-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path241Project2.v.bak
executable file
·1136 lines (1013 loc) · 29.1 KB
/
241Project2.v.bak
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
// Animation
module toplevel (
SW,
CLOCK_50,
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK, // VGA BLANK
VGA_SYNC, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B,
LEDG,
PS2_CLK,
PS2_DAT,
HEX0,
HEX1,
HEX2,
HEX3,
HEX4,
HEX5,
HEX6,
HEX7,
LEDR
);
input [17:10] SW;
input CLOCK_50;
output VGA_CLK; // VGA Clock
output VGA_HS; // VGA H_SYNC
output VGA_VS; // VGA V_SYNC
output VGA_BLANK; // VGA BLANK
output VGA_SYNC; // VGA SYNC
output [9:0] VGA_R; // VGA Red[9:0]
output [9:0] VGA_G; // VGA Green[9:0]
output [9:0] VGA_B; // VGA Blue[9:0]
output [7:0] LEDG;
//output [7:0]xposition;
//output [6:0]yposition;
//output [3:0]statein;
//assign xposition=xpositionW;
//assign yposition=ypositionW;
//assign statein=stateW;
wire is_0;
wire [3:0]stateW;
wire [7:0]xpositionW;
wire [6:0]ypositionW;
wire [0:0]enable,enabledelay1,enabledelay2;
assign LEDG[3:0]= stateW;
assign LEDG[4] = is_0;
wire [2:0]direction_reg;
wire keyleft,keyright,keyup,keydown;
assign LEDG[7:5]=direction_reg;
xycounter U0 (keyleft,keyright,keyup,keydown,CLOCK_50,enable,SW[13],xpositionW,ypositionW,stateW,is_0,direction_reg,enabledelay1);
statecollision U1 (xpositionW,ypositionW,CLOCK_50,enable,SW[13],stateW);
enablercount U2 (CLOCK_50,SW[13],enable,SW[12:10],enabledelay1,enabledelay2);
keyboard U3 (CLOCK_50,HEX0,SW[13],keyleft,keyright,keyup,keydown);
assign LEDR[0]=keyleft;
assign LEDR[1]=keyright;
assign LEDR[2]=keyup;
assign LEDR[3]=keydown;
output [6:0]HEX0;
output [6:0]HEX1;
output [6:0]HEX2;
output [6:0]HEX3;
output [6:0]HEX4;
output [6:0]HEX5;
output [6:0]HEX6;
output [6:0]HEX7;
PS2_Demo U5 (
// Inputs
CLOCK_50,
SW[13],
// Bidirectionals
PS2_CLK,
PS2_DAT,
// Outputs
HEX0,
HEX1,
HEX2,
HEX3,
HEX4,
HEX5,
HEX6,
HEX7
);
wire [7:0] receivedata;
inout PS2_CLK;
inout PS2_DAT;
animation ANIM (
CLOCK_50, // On Board 50 MHz
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK, // VGA BLANK
VGA_SYNC, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B, // VGA Blue[9:0]
xpositionW,
ypositionW,
enable
);
endmodule
// Not bug-proof for more than one direction pressed
// Keeps direction until new direction chosen or until it stops
// *** Make sure enable is only on for ONE cycle
module xycounter (left,
right,
up,
down,
clock,
enable,
reset,
xposition,
yposition,
statein,
is_0,
direction_reg,
enabledelay1);
input left, // Left on Keyboard
right, // Right on Keyboard
up, // Up on Keyboard
down, // Down on Keyboard
clock, // 50 MHz Clock
enable, // X Hz Enable
enabledelay1,
reset; // Reset
// Statein is current state, decides which direction is possible
// XY positions determine next state (after posedge update),
// decides which direction is possible in the NEXT posedge update
input [3:0]statein;
parameter L=4'b0000, // Left
R=4'b0001, // Right
U=4'b0010, // Up
D=4'b0011, // Down
LU=4'b0100, // Left or Up
LR=4'b0101, // Left or Right
LD=4'b0110, // Left or Down
RU=4'b0111, // Right or Up
RD=4'b1000, // Right or Down
UD=4'b1001, // Up or Down
LRU=4'b1010, // Left or Right Or Up
LUD=4'b1011, // Left or Up or Down
LRD=4'b1100, // Left or Right or Down
RUD=4'b1101, // Right or Up or Down
LRUD=4'b1110; // Left or Right or Up or Down
output reg [7:0]xposition; // The x-position of the pacman
output reg [6:0]yposition; // The y-position of the pacman
reg [2:0]direction; // 000 is left
// 001 is right
// 010 is up
// 011 is down
// 100 is idle
output [2:0]direction_reg;
assign direction_reg=direction;
output is_0;
wire isx0;
wire isy0;
assign is_0 = isx0;
modulox U5 (xposition,isx0,enable);
moduloy U6 (yposition,isy0,enable);
//ymodule U0
// Will only allow direction to be changed at 10 pixel intervals
// Can split up conditions for the direction to let it switch at any interval from left/right to right/left or up/down to down/up
always@(*) // Choose Direction
begin
if (reset==1'b0)
direction=3'b100;
else
if (enable==1'b1)
begin
if ((left&&right)||(down&&up))
direction<=direction;
else if (isx0&&isy0&&left&&(statein==L||statein==LU||statein==LR||statein==LD||statein==LRU||statein==LUD||statein==LRD||statein==LRUD))
direction<=3'b000;
else if (isx0&&isy0&&right&&(statein==R||statein==RU||statein==LR||statein==RD||statein==LRU||statein==RUD||statein==LRD||statein==LRUD))
direction<=3'b001;
else if (isx0&&isy0&&up&&(statein==U||statein==LU||statein==RU||statein==UD||statein==LRU||statein==LUD||statein==RUD||statein==LRUD))
direction<=3'b010;
else if (isx0&&isy0&&down&&(statein==D||statein==LD||statein==RD||statein==UD||statein==LUD||statein==LRD||statein==RUD||statein==LRUD))
direction<=3'b011;
else if (direction==3'b000&&isx0&&isy0&&(statein==L||statein==LU||statein==LR||statein==LD||statein==LRU||statein==LUD||statein==LRD||statein==LRUD))
direction<=3'b000;
else if (direction==3'b001&&isx0&&isy0&&(statein==R||statein==RU||statein==LR||statein==RD||statein==LRU||statein==RUD||statein==LRD||statein==LRUD))
direction<=3'b001;
else if (direction==3'b010&&isx0&&isy0&&(statein==U||statein==LU||statein==RU||statein==UD||statein==LRU||statein==LUD||statein==RUD||statein==LRUD))
direction<=3'b010;
else if (direction==3'b011&&isx0&&isy0&&(statein==D||statein==LD||statein==RD||statein==UD||statein==LUD||statein==LRD||statein==RUD||statein==LRUD))
direction<=3'b011;
else if (direction==3'b001&&left&&(statein==L||statein==LU||statein==LR||statein==LD||statein==LRU||statein==LUD||statein==LRD||statein==LRUD))
direction<=3'b000;
else if (direction==3'b000&&right&&(statein==R||statein==RU||statein==LR||statein==RD||statein==LRU||statein==RUD||statein==LRD||statein==LRUD))
direction<=3'b001;
else if (direction==3'b010&&down&&(statein==D||statein==LD||statein==RD||statein==UD||statein==LUD||statein==LRD||statein==RUD||statein==LRUD))
direction<=3'b011;
else if (direction==3'b011&&up&&(statein==U||statein==LU||statein==RU||statein==UD||statein==LRU||statein==LUD||statein==RUD||statein==LRUD))
direction<=3'b010;
else if (direction==3'b000&&(statein==L||statein==LU||statein==LR||statein==LD||statein==LRU||statein==LUD||statein==LRD||statein==LRUD))
direction<=3'b000;
else if (direction==3'b001&&(statein==R||statein==RU||statein==LR||statein==RD||statein==LRU||statein==RUD||statein==LRD||statein==LRUD))
direction<=3'b001;
else if (direction==3'b010&&(statein==U||statein==LU||statein==RU||statein==UD||statein==LRU||statein==LUD||statein==RUD||statein==LRUD))
direction<=3'b010;
else if (direction==3'b011&&(statein==D||statein==LD||statein==RD||statein==UD||statein==LUD||statein==LRD||statein==RUD||statein==LRUD))
direction<=3'b011;
else if (direction==3'b100&&left&&(statein==UD))
direction<=direction;
else if (direction==3'b000&&down&&(statein==LR))
direction<=direction;
else if (direction==3'b100&&right&&(statein==UD))
direction<=direction;
else if (direction==3'b000&&up&&(statein==LR))
direction<=direction;
// add more conditions
else direction <= 3'b100;
end
end
always@(posedge clock or negedge reset) // Update x/y positions
begin
if (reset==1'b0)
begin
xposition<=10;
yposition<=100;
end
else if (enabledelay1==1'b1)
begin
if (direction==3'b000&&(statein==L||statein==LU||statein==LR||statein==LD||statein==LRU||statein==LUD||statein==LRD||statein==LRUD))
begin
xposition<=xposition-1;
yposition<=yposition;
end
if (direction==3'b001&&(statein==R||statein==RU||statein==LR||statein==RD||statein==LRU||statein==RUD||statein==LRD||statein==LRUD))
begin
xposition<=xposition+1;
yposition<=yposition;
end
if (direction==3'b010&&(statein==U||statein==LU||statein==RU||statein==UD||statein==LRU||statein==LUD||statein==RUD||statein==LRUD))
begin
xposition<=xposition;
yposition<=yposition-1;
end
if (direction==3'b011&&(statein==D||statein==LD||statein==RD||statein==UD||statein==LUD||statein==LRD||statein==RUD||statein==LRUD))
begin
xposition<=xposition;
yposition<=yposition+1;
end // else?
if (direction==3'b100)
begin
xposition<=xposition;
yposition<=yposition;
end
end
end
endmodule
module statecollision (xposition,yposition,clock,enable,reset,stateout);
output reg [3:0]stateout;
reg [3:0] statein;
input [7:0]xposition;
input [6:0]yposition;
input clock;
input enable;
input reset;
parameter L=4'b0000, // Left
R=4'b0001, // Right
U=4'b0010, // Up
D=4'b0011, // Down
LU=4'b0100, // Left or Up
LR=4'b0101, // Left or Right
LD=4'b0110, // Left or Down
RU=4'b0111, // Right or Up
RD=4'b1000, // Right or Down
UD=4'b1001, // Up or Down
LRU=4'b1010, // Left or Right Or Up
LUD=4'b1011, // Left or Up or Down
LRD=4'b1100, // Left or Right or Down
RUD=4'b1101, // Right or Up or Down
LRUD=4'b1110; // Left or Right or Up or Down
always@(*)
begin: state_table
//L: nothing
//R: nothing
//U: nothing
case(statein)
D: if (xposition==50&&yposition==20)
stateout=LUD;
else if (xposition==70&&yposition==20)
stateout=RU;
else if (xposition==100&&yposition==20)
stateout=RUD;
else stateout=D;
LU:if (xposition==80&&yposition==50)
stateout=UD;
else if (xposition==70&&yposition==60)
stateout=RD;
else if (xposition==140&&yposition==90)
stateout=UD;
else if (xposition==130&&yposition==100)
stateout=LR;
else if (xposition==130&&yposition==60)
stateout=LRD;
else if (xposition==140&&yposition==50)
stateout=UD;
else stateout=LU;
LR:if (xposition==10&&yposition==10)
stateout=RD;
else if (xposition==30&&yposition==10)
stateout=LD;
else if (xposition==30&&yposition==20)
stateout=RUD;
else if (xposition==50&&yposition==20)
stateout=LUD;
else if (xposition==10&&yposition==40)
stateout=RUD;
else if (xposition==30&&yposition==40)
stateout=LUD;
else if (xposition==30&&yposition==60)
stateout=LRU;
else if (xposition==50&&yposition==60)
stateout=LUD;
else if (xposition==20&&yposition==80)
stateout=LRU;
else if (xposition==50&&yposition==80)
stateout=LRU;
else if (xposition==50&&yposition==40)
stateout=RUD;
else if (xposition==80&&yposition==40)
stateout=LRUD;
else if (xposition==100&&yposition==40)
stateout=LUD;
else if (xposition==100&&yposition==20)
stateout=RUD;
else if (xposition==120&&yposition==20)
stateout=LUD;
else if (xposition==120&&yposition==10)
stateout=RD;
else if (xposition==140&&yposition==10)
stateout=LD;
else if (xposition==120&&yposition==40)
stateout=RUD;
else if (xposition==140&&yposition==40)
stateout=LUD;
else if (xposition==100&&yposition==60)
stateout=RUD;
else if (xposition==120&&yposition==60)
stateout=LRU;
else if (xposition==70&&yposition==80)
stateout=LRU;
else if (xposition==90&&yposition==80)
stateout=LRD;
else if (xposition==100&&yposition==80)
stateout=LRU;
else if (xposition==130&&yposition==80)
stateout=LRU;
else if (xposition==10&&yposition==100)
stateout=RU;
else if (xposition==60&&yposition==100)
stateout=LRU;
else if (xposition==90&&yposition==100)
stateout=LRU;
else if (xposition==140&&yposition==100)
stateout=LU;
else if (xposition==140&&yposition==80)
stateout=LD;
else stateout=LR;
LD:if (xposition==20&&yposition==10)
stateout=LR;
else if (xposition==30&&yposition==20)
stateout=UD;
else if (xposition==70&&yposition==20)
stateout=RU;
else if (xposition==80&&yposition==30)
stateout=LRUD;
else if (xposition==130&&yposition==10)
stateout=LR;
else if (xposition==140&&yposition==20)
stateout=UD;
else if (xposition==130&&yposition==80)
stateout=LRU;
else if (xposition==140&&yposition==90)
stateout=UD;
else stateout=LD;
RU:if (xposition==10&&yposition==90)
stateout=UD;
else if (xposition==20&&yposition==100)
stateout=LR;
else if (xposition==10&&yposition==50)
stateout=UD;
else if (xposition==20&&yposition==60)
stateout=LRD;
else if (xposition==70&&yposition==10)
stateout=D;
else if (xposition==80&&yposition==20)
stateout=LD;
else stateout=RU;
RD:if (xposition==10&&yposition==20)
stateout=UD;
else if (xposition==20&&yposition==10)
stateout=LR;
else if (xposition==20&&yposition==80)
stateout=LRU;
else if (xposition==10&&yposition==90)
stateout=UD;
else if (xposition==70&&yposition==70)
stateout=UD;
else if (xposition==80&&yposition==60)
stateout=LU;
else if (xposition==120&&yposition==20)
stateout=LUD;
else if (xposition==130&&yposition==10)
stateout=LR;
else stateout=RD;
UD:if (xposition==10&&yposition==10)
stateout=RD;
else if (xposition==10&&yposition==40)
stateout=RUD;
else if (xposition==10&&yposition==60)
stateout=RU;
else if (xposition==30&&yposition==20)
stateout=RUD;
else if (xposition==30&&yposition==40)
stateout=LUD;
else if (xposition==30&&yposition==60)
stateout=LRU;
else if (xposition==50&&yposition==20)
stateout=LUD;
else if (xposition==50&&yposition==40)
stateout=RUD;
else if (xposition==50&&yposition==60)
stateout=LUD;
else if (xposition==50&&yposition==80)
stateout=LRU;
else if (xposition==20&&yposition==60)
stateout=LRD;
else if (xposition==20&&yposition==80)
stateout=LRU;
else if (xposition==10&&yposition==80)
stateout=RD;
else if (xposition==10&&yposition==100)
stateout=RU;
else if (xposition==80&&yposition==20)
stateout=LD;
else if (xposition==80&&yposition==40)
stateout=LRUD;
else if (xposition==80&&yposition==60)
stateout=LU;
else if (xposition==70&&yposition==60)
stateout=RD;
else if (xposition==70&&yposition==80)
stateout=LRU;
else if (xposition==100&&yposition==20)
stateout=RUD;
else if (xposition==100&&yposition==40)
stateout=LUD;
else if (xposition==100&&yposition==60)
stateout=RUD;
else if (xposition==100&&yposition==80)
stateout=LRU;
else if (xposition==120&&yposition==20)
stateout=LUD;
else if (xposition==120&&yposition==40)
stateout=RUD;
else if (xposition==120&&yposition==60)
stateout=LRU;
else if (xposition==140&&yposition==10)
stateout=LD;
else if (xposition==140&&yposition==40)
stateout=LUD;
else if (xposition==140&&yposition==60)
stateout=LU;
else if (xposition==130&&yposition==60)
stateout=LRD;
else if (xposition==130&&yposition==80)
stateout=LRU;
else if (xposition==60&&yposition==80)
stateout=LRD;
else if (xposition==60&&yposition==100)
stateout=LRU;
else if (xposition==90&&yposition==80)
stateout=LRD;
else if (xposition==90&&yposition==100)
stateout=LRU;
else if (xposition==140&&yposition==80)
stateout=LD;
else if (xposition==140&&yposition==100)
stateout=LU;
else stateout=UD;
LRU:if (xposition==10&yposition==80)
stateout=RD;
else if (xposition==30&&yposition==80)
stateout=LR;
else if (xposition==20&&yposition==70)
stateout=UD;
else if (xposition==20&&yposition==60)
stateout=LRD;
else if (xposition==30&&yposition==50)
stateout=UD;
else if (xposition==40&&yposition==60)
stateout=LR;
else if (xposition==40&&yposition==80)
stateout=LR;
else if (xposition==60&&yposition==80)
stateout=LRD;
else if (xposition==50&&yposition==70)
stateout=UD;
else if (xposition==80&&yposition==80)
stateout=LR;
else if (xposition==70&&yposition==70)
stateout=UD;
else if (xposition==50&&yposition==100)
stateout=LR;
else if (xposition==70&&yposition==100)
stateout=LR;
else if (xposition==60&&yposition==90)
stateout=UD;
else if (xposition==80&&yposition==100)
stateout=LR;
else if (xposition==100&&yposition==100)
stateout=LR;
else if (xposition==90&&yposition==90)
stateout=UD;
else if (xposition==90&&yposition==80)
stateout=LRD;
else if (xposition==110&&yposition==80)
stateout=LR;
else if (xposition==100&&yposition==70)
stateout=UD;
else if (xposition==110&&yposition==60)
stateout=LR;
else if (xposition==130&&yposition==60)
stateout=LRD;
else if (xposition==120&&yposition==50)
stateout=UD;
else if (xposition==120&&yposition==80)
stateout=LR;
else if (xposition==140&&yposition==80)
stateout=LD;
else if (xposition==130&&yposition==70)
stateout=UD;
else stateout=LRU;
LUD:if (xposition==20&&yposition==40)
stateout=LR;
else if (xposition==30&&yposition==30)
stateout=UD;
else if (xposition==30&&yposition==50)
stateout=UD;
else if (xposition==50&&yposition==10)
stateout=D;
else if (xposition==50&&yposition==30)
stateout=UD;
else if (xposition==40&&yposition==20)
stateout=LR;
else if (xposition==50&&yposition==50)
stateout=UD;
else if (xposition==50&&yposition==70)
stateout=UD;
else if (xposition==40&&yposition==60)
stateout=LR;
else if (xposition==100&&yposition==30)
stateout=UD;
else if (xposition==100&&yposition==50)
stateout=UD;
else if (xposition==90&&yposition==40)
stateout=LR;
else if (xposition==120&&yposition==10)
stateout=RD;
else if (xposition==120&&yposition==30)
stateout=UD;
else if (xposition==110&&yposition==20)
stateout=LR;
else if (xposition==140&&yposition==30)
stateout=UD;
else if (xposition==140&&yposition==50)
stateout=UD;
else if (xposition==130&&yposition==40)
stateout=LR;
else stateout=LUD;
LRD:if (xposition==10&&yposition==60)
stateout=RU;
else if (xposition==30&&yposition==60)
stateout=LRU;
else if (xposition==20&&yposition==70)
stateout=UD;
else if (xposition==50&&yposition==80)
stateout=LRU;
else if (xposition==70&&yposition==80)
stateout=LRU;
else if (xposition==60&&yposition==90)
stateout=UD;
else if (xposition==80&&yposition==80)
stateout=LR;
else if (xposition==100&&yposition==80)
stateout=LRU;
else if (xposition==90&&yposition==90)
stateout=UD;
else if (xposition==120&&yposition==60)
stateout=LRU;
else if (xposition==140&&yposition==60)
stateout=LU;
else if (xposition==130&&yposition==70)
stateout=UD;
else stateout=LRD;
RUD: if (xposition==10&&yposition==30)
stateout=UD;
else if (xposition==10&&yposition==50)
stateout=UD;
else if (xposition==20&&yposition==40)
stateout=LR;
else if (xposition==30&&yposition==10)
stateout=LD;
else if (xposition==30&&yposition==30)
stateout=UD;
else if (xposition==40&&yposition==20)
stateout=LR;
else if (xposition==50&&yposition==30)
stateout=UD;
else if (xposition==50&&yposition==50)
stateout=UD;
else if (xposition==60&&yposition==40)
stateout=LR;
else if (xposition==100&&yposition==50)
stateout=UD;
else if (xposition==100&&yposition==70)
stateout=UD;
else if (xposition==110&&yposition==60)
stateout=LR;
else if (xposition==120&&yposition==30)
stateout=UD;
else if (xposition==120&&yposition==50)
stateout=UD;
else if (xposition==130&&yposition==40)
stateout=LR;
else if (xposition==100&&yposition==10)
stateout=D;
else if (xposition==100&&yposition==30)
stateout=UD;
else if (xposition==110&&yposition==20)
stateout=LR;
else stateout=RUD;
LRUD:if (xposition==70&&yposition==40)
stateout=LR;
else if (xposition==90&&yposition==40)
stateout=LR;
else if (xposition==80&&yposition==30)
stateout=UD;
else if (xposition==80&&yposition==50)
stateout=UD;
else stateout=LRUD;
default:stateout=4'bXXXX;
endcase
end
// Start with a known state
// Get info from xposition and yposition
// Based on that info, update the state
// Send out that state information in time for the next clock posedge
always@(posedge clock or negedge reset)
begin: state_FFs
if (reset==1'b0) statein <= RU;
else statein <= stateout;
end // state_FFS
endmodule
module enablercount(CLOCK_50,reset,enable,divider,enabledelay1,enabledelay2);
input CLOCK_50,
reset;
input [2:0]divider;
output reg enable;
output reg enabledelay1;
output reg enabledelay2;
reg [32:0]count;
always@(posedge CLOCK_50 or negedge reset)
begin
if (!reset)
count=0;
else
begin
if (divider==3'b000&&count==2499999||divider==3'b001&&count==2299999||divider==3'b010&&count==1999999||divider==3'b011&&count==1699999
||divider==3'b100&&count==1499999||divider==3'b101&&count==1299999||divider==3'b110&&count==99999||divider==3'b111&&count==4999999)
count=0;
else
count=count+1;
end
end
always@(posedge CLOCK_50 or negedge reset)
begin
if (!reset)
enable<=0;
else
begin
if (count==0)
enable<=1;
else
enable<=0;
end
enabledelay1<=enable;
enabledelay2<=enabledelay1;
end
endmodule
module animation
(
clock,
VGA_CLK, // VGA Clock
VGA_HS, // VGA H_SYNC
VGA_VS, // VGA V_SYNC
VGA_BLANK, // VGA BLANK
VGA_SYNC, // VGA SYNC
VGA_R, // VGA Red[9:0]
VGA_G, // VGA Green[9:0]
VGA_B, // VGA Blue[9:0]
new_x,
new_y,
enable
);
input clock; // 50 MHz
// Button[3:0]
output VGA_CLK; // VGA Clock
output VGA_HS; // VGA H_SYNC
output VGA_VS; // VGA V_SYNC
output VGA_BLANK; // VGA BLANK
output VGA_SYNC; // VGA SYNC
output [9:0] VGA_R; // VGA Red[9:0]
output [9:0] VGA_G; // VGA Green[9:0]
output [9:0] VGA_B; // VGA Blue[9:0]
input [7:0] new_x;
input [6:0] new_y;
input enable;
// PARAMETERS
parameter [1:0] idle = 2'b00, erase = 2'b01, draw = 2'b10, clear = 2'b11;
parameter [6:0] cntr_y_max = 7'b0001001, clear_y_max = 7'b1110111, cntr_y_start = 7'b0000000;
parameter [7:0] cntr_x_max = 8'b00001001, clear_x_max = 8'b10011111, cntr_x_start = 8'b00000000;
//WIRES
wire enable;
wire writeEn;
wire [7:0] new_x;
wire [6:0] new_y;
wire [5:0] rom_out;
//REGS
reg [1:0] stater;
reg [7:0] old_x, cntr_x, vga_x;
reg [6:0] old_y, cntr_y, vga_y;
reg [5:0] color;
reg [6:0] address;
//reg [2:0] direction;
//ASSIGN
//assign resetn = KEY[0];
//assign writeEnable = KEY[1];
assign writeEn = (stater == erase | stater == draw | stater == clear);
// Create an Instance of a VGA controller - there can be only one!
// Define the number of colours as well as the initial background
// image file (.MIF) for the controller.
vga_adapter VGA(
.resetn(1),
.clock(clock),
.colour(color),
.x(vga_x),
.y(vga_y),
.plot(writeEn),
/* Signals for the DAC to drive the monitor. */
.VGA_R(VGA_R),
.VGA_G(VGA_G),
.VGA_B(VGA_B),
.VGA_HS(VGA_HS),
.VGA_VS(VGA_VS),
.VGA_BLANK(VGA_BLANK),
.VGA_SYNC(VGA_SYNC),
.VGA_CLK(VGA_CLK));
defparam VGA.RESOLUTION = "160x120";
defparam VGA.MONOCHROME = "FALSE";
defparam VGA.BITS_PER_COLOUR_CHANNEL = 2;
defparam VGA.BACKGROUND_IMAGE = "pacman_background.mif";
pacman_rom PACROM(address,clock,rom_out);
initial
begin
old_x = 8'b00001010;
old_y = 7'b1100100;
address = 7'b0000000;
end
always@(posedge enable, posedge clock)
/*if (!resetn)
begin
color = 3'b000;
cntr_x = cntr_x_start;
cntr_y = cntr_y_start;
stater = clear;
end
*/
if (enable)
begin
stater = erase;
color = 6'b000000;
cntr_x = cntr_x_start;
cntr_y = cntr_y_start;
end
else
case(stater)
erase:
begin
vga_x = old_x + cntr_x;
vga_y = old_y + cntr_y;
if (cntr_x == cntr_x_max)
if (cntr_y == cntr_y_max)
begin
cntr_x = cntr_x_start;
cntr_y = cntr_y_start;
address = 7'b0000000;
color = rom_out;
stater = draw;
old_x = new_x;
old_y = new_y;
end
else
begin
cntr_y = cntr_y + 7'b0000001;
cntr_x = cntr_x_start;
end
else
cntr_x = cntr_x + 8'b00000001;
end
draw:
begin
vga_x = old_x + cntr_x;
vga_y = old_y + cntr_y;
color = rom_out;
address = address + 7'b0000001;
if (cntr_x == cntr_x_max)
if (cntr_y == cntr_y_max)
stater = idle;
else
begin
cntr_y = cntr_y + 7'b0000001;
cntr_x = cntr_x_start;
end
else
cntr_x = cntr_x + 8'b00000001;
end
clear:
begin
vga_x = cntr_x;
vga_y = cntr_y;
if (cntr_x == clear_x_max)
if (cntr_y == clear_y_max)
begin
stater = idle;
end
else
begin
cntr_y = cntr_y + 7'b0000001;
cntr_x = cntr_x_start;
end
else
cntr_x = cntr_x + 8'b00000001;
end
endcase
endmodule
module moduloy(ypositionW,is0,enable);
input [7:0]ypositionW;
input enable;
output reg is0;
always@(*)
if (ypositionW%10==0)
is0=1;
else
is0=0;
endmodule
module modulox(xpositionW,is0,enable);
input [7:0]xpositionW;
input enable;
output reg is0;
always@(*)
if (xpositionW%10==0)
is0=1;
else
is0=0;
endmodule
module keyboard (CLOCK_50,received_data,reset,keyleft,keyright,keyup,keydown);
input CLOCK_50,reset;
input [6:0]received_data;
output reg keyleft,keyright,keyup,keydown;
always@(*)
case (received_data)
7'b0010010:
begin
keyleft<=0;
keyright<=0;
keyup<=1;
keydown<=0;
end
7'b0000011:
begin
keyleft<=1;
keyright<=0;
keyup<=0;
keydown<=0;
end
7'b0011001:
begin
keyleft<=0;
keyright<=1;
keyup<=0;
keydown<=0;
end
7'b0100100:
begin
keyleft<=0;
keyright<=0;
keyup<=0;
keydown<=1;
end
default:
begin
keyleft<=0;
keyright<=0;
keyup<=0;
keydown<=0;
end
endcase
endmodule