-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarFight.c
1149 lines (991 loc) · 34.7 KB
/
StarFight.c
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
/*
===============================================================================
Name : StarFight.c
Author : $(Giselle Chavez and Sam Musser)
Version : 1.0
Copyright : $(copyright)
Description : The main objective of this project is to create a
vintage dog fighter game with an overt Star Wars theme. The main
parts being used will include: LPC1769 for control, Nokia 5110 GLCD for
display, piezo for music, and custom buttons with serial interface
for user input.
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <cr_section_macros.h>
//**************************************************************************
//LPC1769 definitions
//
// I/O definitions for some of the outputs
#define FIO0DIR (*(volatile unsigned int *)0x2009c000)
#define FIO0PIN (*(volatile unsigned int *)0x2009c014)
#define FIO2DIR (*(volatile unsigned int *)0x2009c040)
#define FIO2PIN (*(volatile unsigned int *)0x2009c054)
//Timer Counter registers for wait function
#define T0TCR (*(volatile unsigned int *)0x40004004) //control register
#define T0TC (*(volatile unsigned int *)0x40004008) //status register
//I2C control definitions
#define I2C0CONSET (*(volatile unsigned int *)0x4001c000)
#define I2C0STAT (*(volatile unsigned int *)0x4001c004)
#define I2C0DAT (*(volatile unsigned int *)0x4001c008)
#define I2C0SCLH (*(volatile unsigned int *)0x4001c010)
#define I2C0SCLL (*(volatile unsigned int *)0x4001c014)
#define I2C0CONCLR (*(volatile unsigned int *)0x4001c018)
//SPI definitions
#define S0SPCR (*(volatile unsigned int *)0x40020000) //control register
#define S0SPSR (*(volatile unsigned int *)0x40020004) //status register
#define S0SPDR (*(volatile unsigned int *)0x40020008) //data register
#define SPTCR (*(volatile unsigned int *)0x40020010) //test register
#define S0SPCCR (*(volatile unsigned int *)0x4002000c) //clock counter reg
#define S0SPINT (*(volatile unsigned int *)0x4002001c) //interrupt register
//the pinmode definitions for the sclk and mosi
#define PINSEL0 (*(volatile unsigned int *)0x4002c000)
#define PINSEL1 (*(volatile unsigned int *)0x4002c004)
#define PINSEL4 (*( volatile unsigned int *)0x4002c010)
#define PINMODE1 (*(volatile unsigned int *)0x4002c044)
//power control to start the i2c power/control
#define PCONP (*( volatile unsigned int *)0x400fc0c4)
//**************************************************************************
//wait function, rand, global constants, and variable definitions
//
//
//wait function that depends on 1MHz freq, useful so pixels do not shift
//at inconceivable speed or for input delay
void wait(float seconds)
{
int start = T0TC;
T0TCR |= (1<<0);
seconds *= 1000000;
while ((T0TC-start)< seconds) {}
}
//alternative wait function for the piezo
void tick(int ticks)
{
volatile int cntDwn;
for (cntDwn = 0; cntDwn < ticks; cntDwn++)
{}
}
//variables
char output[504]; //array of the output bytes for the GLCD
//game object arrays will hold the following:
//initial arrayPosition (ie where they should go in output array)
//width (position will correspond to leftmost bit, so need to know right for bounds
//height (for the smaller objects that will be moving up by pixels instead of rows
int ball[] = {211, 2, 2};
int laser1[] = {250, 3, 1, 0}; //fourth laser value is to relay whether it is on or off
int laser11[] = {250, 3, 1, 0};
int laser2[] = {250, 3, 1, 0};
int laser21[] = {250, 3, 1, 0};
int tieFighter1[] = {169, 10, 8};
int tieFighter2[] = {241, 10, 8};
//sounds
//ticks in the alt wait function to deliver the imperial tune at desired frequencies
int imperialTune[] = {220, 220, 220, 278, 208, 220, 278, 208, 220,
164, 164, 164, 155, 208, 220, 278, 208, 220};
int pewNoise[] = {164};
int hitNoise[] = {300};
//addresses for the I/O expander
int expWrite = 0x40; //expander write address
int expRead = 0x41; //expander read address
int DIRA = 0x00; //Address for GPIOA
int GPIOA = 0x12; //write to this then read for inputs
int GPPUA = 0x0C; //This is to turn off pull up resistors on expander
int gameOver = 0; //shows whether the game is on or lost
int ABRT; //These variables are components of the status register
int MODF; //may not be used for final iteration
int ROVR;
int WCOL;
int SPIF;
//user input value for the serial controller
volatile int inputVal = 0;
//tie shape
char tie[] = {0xFF, 0x18, 0x18, 0x3C, 0x3C, 0x3C, 0x3C, 0x18, 0x18, 0xFF};
//laser shape
char lzr[] = {0x08, 0x08, 0x08};
//ball shape, in case there is the desire to implement pong later
char bll[] = {0x18, 0x18};
//home screen mapping array (star fight)
char starFightBitMap [] = {
0x04, 0x00, 0x10, 0x80, 0x00, 0x00, 0xE0, 0xE1, 0xE0, 0xE0,
0xE0, 0xE0, 0xE0, 0xE4, 0xE0, 0xE0, 0xE0, 0xE2, 0xE0, 0xE0,
0xE8, 0x00, 0x00, 0xE0, 0xE4, 0xE0, 0xE1, 0xE0, 0x00, 0x00,
0xE0, 0xE0, 0xE0, 0x64, 0x60, 0x60, 0xE1, 0xE0, 0xC8, 0x00,
0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0xE0, 0xE0, 0xE0, 0xE8,
0xE0, 0xE0, 0xE0, 0x00, 0xE2, 0xE0, 0xE0, 0x08, 0x81, 0xC0,
0xE0, 0xE4, 0xE0, 0xE0, 0xE0, 0xE2, 0x00, 0xE0, 0xE0, 0xE4,
0x01, 0x00, 0xE0, 0xE0, 0xE2, 0xE0, 0xE0, 0xE0, 0xE4, 0xE0,
0xE0, 0xE0, 0x01, 0x00, 0x00, 0x02, 0x00, 0x70, 0x70, 0x70,
0x73, 0x77, 0x7F, 0x7E, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x7F,
0x7F, 0x7F, 0x00, 0x00, 0x00, 0x70, 0x7C, 0x7F, 0x1F, 0x18,
0x1F, 0x7F, 0x7C, 0x60, 0x7F, 0x7F, 0x7F, 0x0E, 0x1E, 0x3E,
0x7E, 0x77, 0x63, 0x60, 0x61, 0x00, 0x00, 0x00, 0x00, 0x10,
0x00, 0x7F, 0x7F, 0x7F, 0x0C, 0x0C, 0x00, 0x00, 0x7F, 0x7F,
0x7F, 0x00, 0x1F, 0x3F, 0x7F, 0x78, 0x60, 0x6C, 0x7C, 0x7C,
0x00, 0x7F, 0x7F, 0x7F, 0x06, 0x06, 0x7F, 0x7F, 0x7F, 0x00,
0x7F, 0x7F, 0x7F, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40,
0x04, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x02,
0x00, 0x10, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x08, 0x00,
0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x20, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x02,
0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x00, 0x10, 0x00, 0x01,
0x40, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00,
0x00, 0x3F, 0x05, 0x07, 0x00, 0x3F, 0x1D, 0x37, 0x00, 0x3F,
0xA9, 0x29, 0x00, 0x27, 0x3D, 0x00, 0x27, 0x3D, 0x00, 0x00,
0x00, 0x3F, 0x05, 0x05, 0x3F, 0x00, 0x00, 0x00, 0xBF, 0x05,
0x05, 0x01, 0x00, 0x3F, 0x21, 0x3F, 0x00, 0x3F, 0x1D, 0x37,
0x00, 0x00, 0x20, 0x00, 0x02, 0x3F, 0x00, 0x00, 0x00, 0x3F,
0x05, 0x07, 0x00, 0xBF, 0x20, 0x00, 0x3F, 0x05, 0x3F, 0x00,
0x27, 0x24, 0xBF, 0x00, 0x3F, 0x29, 0x29, 0x00, 0x3F, 0x1D,
0x37, 0x00, 0x02, 0x20, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x28, 0x3A, 0x00, 0xF8,
0xE8, 0xB8, 0x00, 0xF8, 0x48, 0x48, 0x00, 0x38, 0xE8, 0x00,
0x38, 0xE9, 0x00, 0x00, 0x00, 0xF8, 0x48, 0x78, 0xC0, 0x10,
0x00, 0x00, 0xF8, 0x28, 0x28, 0x08, 0x00, 0xFA, 0x08, 0xF8,
0x00, 0xF8, 0xE8, 0xB8, 0x00, 0x01, 0x00, 0x98, 0xC8, 0x78,
0x00, 0x00, 0x01, 0xF8, 0x68, 0x78, 0x00, 0xF8, 0x00, 0x00,
0xF8, 0x2A, 0xF8, 0x00, 0x38, 0x20, 0xF8, 0x00, 0xF8, 0x48,
0x48, 0x00, 0xF8, 0xE8, 0xB8, 0x00, 0x02, 0x40, 0x00, 0x04,
0x10, 0x00, 0x02, 0x00, 0x00, 0x08, 0x40, 0x00, 0x04, 0x01,
0x80, 0x00, 0x10, 0x01, 0x00, 0x01, 0x00, 0x11, 0x01, 0x01,
0x40, 0x01, 0x09, 0x00, 0x01, 0x01, 0x20, 0x00, 0x04, 0x01,
0x01, 0x81, 0x01, 0x08, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00,
0x00, 0x01, 0x41, 0x01, 0x00, 0x09, 0x00, 0x21, 0x00, 0x00,
0x00, 0x01, 0x11, 0x01, 0x00, 0x00, 0x80, 0x01, 0x00, 0x08,
0x00, 0x01, 0x01, 0x00, 0x21, 0x00, 0x01, 0x80, 0x11, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x08, 0x01, 0x00, 0x81, 0x20,
0x00, 0x00, 0x08, 0x00
};
//initial game screen mapping array for reference (not quite to scale)
char gameScreen[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0x20, 0x70, 0x70, 0x70, 0x20, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xB0, 0xF0, 0xF0,
0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xFF, 0x20, 0x70, 0x70, 0x70, 0x20, 0xFF, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
//**************************************************************************
//standard initialization methods per LPC1769 and Nokia 5110 data sheets
//and the serial interface functions for the user input
//
//LPC I2C subsystem initialization
void I2C_init()
{
//starts the I2C0 interface power/clock control
PCONP |= (1<<7);
//Need to turn off pull up or down resistors for p0.27/28
PINMODE1 |= (1<<22) | (1<<24);
//have to set p0.28/27 to SDA and SCL modes w/ 01
//for bits 23/22 and 25/24
PINSEL1 &= ~(1<<23); //p0.27
PINSEL1 |= (1<<22);
PINSEL1 &= ~(1<<25); //p0.28
PINSEL1 |= (1<<24);
//pclkI2C/10 = 1MHz/10= 100kHz = I2C bit frequency = SCL
I2C0SCLL = 5; //low div
I2C0SCLH = 5; //high div
I2C0CONSET = (1<<6); //enable I2C
}
//LPC SPI subsystem initialization
void SPI_init()
{
//Activates MOSI and SCLK modes by going high
//at the following bits
PINSEL0 |= (1<<31) | (1<<30); //SCLK (clock, p0.15)
PINSEL1 |= (1<<5) | (1<<4); //MOSI (data out, p0.18)
//setting the output pins (arbitrary, but we chose these)
FIO0DIR |= (1<<9); //GLCD SCE output
FIO0DIR |= (1<<8); //GLCD RESET output
FIO0DIR |= (1<<7); //GLCD D/C output (1/0)
//initialize SPI subsystem in lpc
S0SPCR &= ~(1<<2); //bit enabler
S0SPCR &= ~(1<<3); //cphase as 0
S0SPCR &= ~(1<<4); // cpolarity as 0 (sck active high)
S0SPCR |= (1<<5); //selects master mode (only one we need)
S0SPCR &= ~(1<<6); //chooses least sig bit first if high
S0SPCR &= ~(1<<7); //does not use interrupts
//initialize the clock counter register to master mode
S0SPCCR |= (1<<4); //sets number to 8
}
//Nokia 5110 GLCD initialization that ends in data mode (ready to write)
void GLCD_init()
{
//initialize the GLCD display
FIO0PIN |= (1<<8);
FIO0PIN &= ~(1<<8);
FIO0PIN |= (1<<8); //clears the GLCD from previous use
FIO0PIN &= ~(1<<7); //D/C output start as low for command mode
FIO0PIN |= (1<<9); //chip select (active low)
FIO0PIN &= ~(1<<9); //falling edge begins the process
output[0] = 0b00100001; //per instructions on GLCD data sheet
output[1] = 0b11001000; //sets Vop to 16 x b[V] (per dataSheet) (contrast)
output[2] = 0b00100000; //function set PD = 0 and V = 0 (normal instruction)
output[3] = 0b00000100; //sets the temperature coefficient
output[4] = 0b00010100; //makes the glcd bias mode 1:48
output[5] = 0b00001100; //display control set normal mode (D=1, E=1)
//writes these initialization commands to glcd in command mode
for(int i = 0; i < 6; i++)
{
S0SPDR = output[i];
//makes sure each byte passes through before moving on
while (((S0SPSR >> 7)&1) == 0) {}
}
//puts glcd in data mode, now time for writing!
FIO0PIN |= (1<<7); //data mode for the glcd
}
//Serial Functions:
//start function for beginning a read/write process
void start(void)
{
I2C0CONSET = (1<<3); //set SI
I2C0CONSET = (1<<5); //set STA
I2C0CONCLR = (1<<3); //clear SI
//makes sure there is completion before moving on
while((I2C0CONSET & (1<<3)) == 0) {}
I2C0CONCLR = (1<<5); //clear STA
}
//read function for reading in data to the i2c
int read(int heard)
{
if(heard)
{
I2C0CONSET = (1<<2); //accepts data
}
else
{
I2C0CONCLR = (1<<2);
}
I2C0CONCLR = (1<<3);
//waits for complete
while((I2C0CONSET & (1<<3)) == 0) {}
return (I2C0DAT & 0xFF);
}
//write function for i2c to output bit values
void write(int num)
{
I2C0DAT = num; //takes in the information
I2C0CONCLR = (1<<3);
//waits for completion
while((I2C0CONSET & (1<<3)) == 0) {}
}
//stop function for ending the read/write process
void stop(void)
{
I2C0CONSET = (1<<4); //sets the sto
I2C0CONCLR = (1<<3);
//same idea as in the start function
while((I2C0CONSET & (1<<4)) != 0) {}
}
//**************************************************************************
//the object movement functions
//
//
/*
"shift" moves objects by the pixel on the glcd whereas
"move" moves objects by the byte
*/
//moves up 1 pixel
void shiftUp() {}
//moves down 1 pixel
void shiftDown() {}
//moves up 8 pixels
int moveUp(int arrayNum)
{
//makes sure cannot move past upper bounds
if (arrayNum > 83)
{
arrayNum -= 84;
}
return arrayNum;
}
//moves down 8 pixels
int moveDown(int arrayNum)
{
//makes sure cannot move past lower bounds
if (arrayNum < 419)
{
arrayNum += 84;
}
return arrayNum;
}
//moves right by 1 pixel
int shiftRight(int arrayNum, int width)
{
//takes into account the rightmost pixel using width
int arrayNumR = arrayNum + width;
//makes sure it cannot move onto the next row
if((arrayNumR != 83) && (arrayNumR != 167) && (arrayNumR != 251) && (arrayNumR !=
335) && (arrayNumR != 419) && (arrayNumR != 503)) {
arrayNumR += 1;
}
arrayNum = arrayNumR - width;
return arrayNum;
}
//moves left by 1 pixel
int shiftLeft(int arrayNumL)
{
//makes sure it cannot move into the previous row
if((arrayNumL != 0) && (arrayNumL != 84) && (arrayNumL != 168) && (arrayNumL !=
252) && (arrayNumL != 336) && (arrayNumL != 420)) {
arrayNumL -= 1;
}
return arrayNumL;
}
//tie1 movement method
void tie1Move(int joy)
{
switch(joy) {
case 0:
tieFighter1[0] = moveUp(tieFighter1[0]);
break;
case 1:
tieFighter1[0] = moveDown(tieFighter1[0]);
break;
}
}
//tie2 movement method
void tie2Move(int stick)
{
switch(stick) {
case 0:
tieFighter2[0] = moveUp(tieFighter2[0]);
break;
case 1:
tieFighter2[0] = moveDown(tieFighter2[0]);
break;
}
}
//would utilize angle and direction tracker and shift vertical/horizontal for diagonal
void ballMove()
{}
//triggers at laser button activation, sets current position, and true for laser
//to appear on the screen
void fireLaser(int player)
{
switch(player) {
case(1):
laser1[0] = tieFighter1[0] + 11; //sets laser initial position
laser1[3] = 1; //sets laser as active
break;
case(2):
laser2[0] = tieFighter2[0] - 2; //sets laser initial position
laser2[3] = 1; //sets laser as true
break;
case(3):
laser11[0] = tieFighter1[0] + 11; //sets laser initial position
laser11[3] = 1; //sets laser as true
break;
case(4):
laser21[0] = tieFighter2[0] - 2; //sets laser initial position
laser21[3] = 1; //sets laser as true
break;
}
}
//*****************************************************************************
//screen functions such as reset, clear, or updates and
//also the user input checker
//resets the game's objects' locations in case of win/lose (single player)
void reset()
{
ball[0] = 211;
ball[1] = 2;
ball[2] = 2;
laser1[0] = 250;
laser1[1] = 3;
laser1[2] = 1;
laser1[3] = 0;
laser2[0] = 250;
laser2[1] = 3;
laser2[2] = 1;
laser2[3] = 0;
laser11[0] = 250;
laser11[1] = 3;
laser11[2] = 1;
laser11[3] = 0;
laser21[0] = 250;
laser21[1] = 3;
laser21[2] = 1;
laser21[3] = 0;
tieFighter1[0] = 169;
tieFighter1[1] = 10;
tieFighter1[2] = 8;
tieFighter2[0] = 241;
tieFighter2[1] = 10;
tieFighter2[2] = 8;
}
//displays outputs current values to the screen
void updateScreen()
{
for (int f = 0; f < 504; f++)
{
S0SPDR = output[f];
while (((S0SPSR >> 7)&1) == 0) {}
}
}
//sets all output values to that of what "would" be a blank screen
void clrOutput()
{
for (int m = 0; m < 504; m++)
{
output[m] = 0x00;
}
}
//sets all output values to zero then displays the blank screen
void clrScreen()
{
clrOutput();
updateScreen();
}
//outputs the home screen to the display
void displayHome()
{
for (int hh = 0; hh < 504; hh++)
{
S0SPDR = starFightBitMap[hh];
while (((S0SPSR >> 7)&1) == 0) {}
}
}
//updates the screen with current object positions (single player)
void updateSingleGame()
{
//clears output to erase previous object positions
//before rewriting them
clrOutput();
//to keep tabs on the object array values
int temp;
temp = 0;
//sets the first tie fighter position in output
for (int e = tieFighter1[0]; e < (tieFighter1[0] + 10); e++)
{
output[e] = tie[temp];
temp++;
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser1[3] == 1)
{
for (int lr = laser1[0]; lr < laser1[0] + 3; lr++)
{
output[lr] = lzr[temp];
temp++;
}
laser1[0] = shiftLeft(laser1[0]);
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser2[3] == 1)
{
for (int y = laser2[0]; y < laser2[0] + 3; y++)
{
output[y] = lzr[temp];
temp++;
}
laser2[0] = shiftLeft(laser2[0]);
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser11[3] == 1)
{
for (int b = laser11[0]; b < laser11[0] + 3; b++)
{
output[b] = lzr[temp];
temp++;
}
laser11[0] = shiftLeft(laser11[0]);
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser21[3] == 1)
{
for (int q = laser21[0]; q < laser21[0] + 3; q++)
{
output[q] = lzr[temp];
temp++;
}
laser21[0] = shiftLeft(laser21[0]);
}
//updates the screen with these positions
updateScreen();
}
//updates the screen with current object positions (multiplayer)
void updateMultGame()
{
//clears output to erase previous object positions
//before rewriting them
clrOutput();
//to keep tabs on the object array values
int temp;
temp = 0;
//sets the first tie fighter position in output
for (int g = tieFighter1[0]; g < (tieFighter1[0] + 10); g++)
{
output[g] = tie[temp];
temp++;
}
temp = 0;
//sets the second tie fighter position in output
for (int y = tieFighter2[0]; y < (tieFighter2[0] + 10); y++)
{
output[y] = tie[temp];
temp++;
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser1[3] == 1)
{
for (int lr = laser1[0]; lr < laser1[0] + 3; lr++)
{
output[lr] = lzr[temp];
temp++;
}
laser1[0] = shiftRight(laser1[0], laser1[1]);
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser2[3] == 1)
{
for (int le = laser2[0]; le < laser2[0] + 3; le++)
{
output[le] = lzr[temp];
temp++;
}
laser2[0] = shiftLeft(laser2[0]);
}
//sets the new laser positions if they are on and updates them
if (laser11[3] == 1)
{
for (int lr = laser11[0]; lr < laser11[0] + 3; lr++)
{
output[lr] = lzr[temp];
temp++;
}
laser11[0] = shiftRight(laser11[0], laser11[1]);
}
temp = 0;
//sets the new laser positions if they are on and updates them
if (laser21[3] == 1)
{
for (int le = laser21[0]; le < laser21[0] + 3; le++)
{
output[le] = lzr[temp];
temp++;
}
laser21[0] = shiftLeft(laser21[0]);
}
//updates the screen with these positions
updateScreen();
}
//*********************************************************************************
//Final game functions and music:
//checks for presses, wins, fire laser, noise output, and game loop
//checks the data values read from the I/O expander (serial input)
//and sets it equal to the input value
void checkIn()
{
start();
write(expWrite);
write(GPIOA);
stop();
start();
write(expRead);
inputVal = read(0);
stop();
}
//plays imperial theme
void playTheme()
{
for (int note = 0; note < 18; note++)
{
int start = T0TC;
T0TCR |= (1<<0);
if ((note == 4) || (note == 7) || (note == 13) || (note ==16) ||
(note == 3) || (note == 6) || (note == 12) || (note ==15))
{
while ((T0TC-start)< 200000)
{
FIO2PIN |= (1<<0);
tick(imperialTune[note]);
FIO2PIN &= ~(1<<0);
tick(imperialTune[note]);
}
wait(0.05);
}
else
{
while ((T0TC-start)< 400000)
{
FIO2PIN |= (1<<0);
tick(imperialTune[note]);
FIO2PIN &= ~(1<<0);
tick(imperialTune[note]);
}
wait(0.1);
}
}
}
//plays laser noise
void pewPew()
{
int start = T0TC;
T0TCR |= (1<<0);
while ((T0TC-start)< 100000)
{
FIO2PIN |= (1<<0);
tick(pewNoise[0]);
FIO2PIN &= ~(1<<0);
tick(pewNoise[0]);
}
}
//
void targetHit()
{
int start = T0TC;
T0TCR |= (1<<0);
while ((T0TC-start)< 100000)
{
FIO2PIN |= (1<<0);
tick(hitNoise[0]);
FIO2PIN &= ~(1<<0);
tick(hitNoise[0]);
}
}
//positions lasers to output almost randomly for single player laser dodge
void comeAtMeBro()
{
//generates random number between 1 and 6 to output a laser to
//one of the rows
//Also lasers based on current player position
//Possible laser positions: 80 164 248 332 416 500
if ((!laser1[3]) && (tieFighter1[0] == 1))
{
pewPew();
pewPew();
laser1[0] = 80;
laser11[0] = 164;
laser2[0] = 332;
laser21[0] = 416;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
else if ((!laser1[3]) && (tieFighter1[0] == 85))
{
pewPew();
pewPew();
laser1[0] = 80;
laser11[0] = 164;
laser2[0] = 248;
laser21[0] = 500;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
else if ((!laser1[3]) && (tieFighter1[0] == 169))
{
pewPew();
pewPew();
laser1[0] = 248;
laser11[0] = 164;
laser2[0] = 332;
laser21[0] = 416;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
else if ((!laser1[3]) && (tieFighter1[0] == 253))
{
pewPew();
pewPew();
laser1[0] = 80;
laser11[0] = 332;
laser2[0] = 500;
laser21[0] = 416;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
else if ((!laser1[3]) && (tieFighter1[0] == 337))
{
pewPew();
pewPew();
laser1[0] = 248;
laser11[0] = 164;
laser2[0] = 500;
laser21[0] = 416;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
else if ((!laser1[3]) && (tieFighter1[0] == 421))
{
pewPew();
pewPew();
laser1[0] = 80;
laser11[0] = 164;
laser2[0] = 332;
laser21[0] = 500;
laser1[3] = 1;
laser11[3] = 1;
laser2[3] = 1;
laser21[3] = 1;
}
}
//checks positions of objects for hits/wins
//and also clears lasers if they have reached bounds without a hit
void gameOverSingle()
{
//ahhh you've been shot! orrrr you won! good job.
if((laser1[0] == (tieFighter1[0] + tieFighter1[1])) || (laser11[0] == (tieFighter1[0] + tieFighter1[1])) ||
(laser2[0] == (tieFighter1[0] + tieFighter1[1])) || (laser21[0] == (tieFighter1[0] + tieFighter1[1]))) {
targetHit();
wait(2);
reset();
displayHome();
gameOver = 1;
}
//all cases where lasers avoid ship and need to be reset
if (((laser1[0]) != tieFighter1[0]) && ((laser1[0] == 11) ||
(laser1[0] == 95) || (laser1[0] == 179) ||
(laser1[0] == 263) || (laser1[0] == 347) ||
(laser1[0] == 431))) {
laser1[3] = 0;
laser1[0] = tieFighter2[0];
}
if (((laser11[0]) != tieFighter1[0]) && ((laser11[0] == 11) ||
(laser11[0] == 95) || (laser11[0] == 179) ||
(laser11[0] == 263) || (laser11[0] == 347) ||
(laser11[0] == 431))) {
laser11[3] = 0;
laser11[0] = tieFighter2[0];
}
if (((laser2[0]) != tieFighter1[0]) && ((laser2[0] == 11) ||
(laser2[0] == 95) || (laser2[0] == 179) ||
(laser2[0] == 263) || (laser2[0] == 347) ||
(laser2[0] == 431))) {
laser2[3] = 0;
laser2[0] = tieFighter2[0];
}
if (((laser21[0]) != tieFighter1[0]) && ((laser21[0] == 11) ||
(laser21[0] == 95) || (laser21[0] == 179) ||
(laser21[0] == 263) || (laser21[0] == 347) ||
(laser21[0] == 431))) {
laser21[3] = 0;
laser2[0] = tieFighter2[0];
}
}
void gameOverMult()
{
//player 1 win
if ((laser1[0] + 2 == tieFighter2[0]) || (laser11[0] + 2 == tieFighter2[0])) {
targetHit();
wait(2);
reset();
displayHome();
gameOver = 1;
return;
}
//player 2 win
if ((laser2[0] == (tieFighter1[0] + 10)) || (laser21[0] == (tieFighter1[0]+10))) {
targetHit();
wait(2);
reset();
displayHome();
gameOver = 1;
return;
}
//all cases in which the laser's avoid the ships and need to be reset
//
//player 1 laser 1
if (((laser1[0] + 2) != tieFighter2[0]) && (((laser1[0] + 2) == 73) ||
((laser1[0] + 2) == 157) || ((laser1[0] + 2) == 241) ||
((laser1[0] + 2) == 325) || ((laser1[0] + 2) == 409) ||
((laser1[0] + 2) == 493))) {
laser1[3] = 0;
laser1[0] = tieFighter1[0];
}
//player 1 laser 2 (laser11)