-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
1970 lines (1611 loc) · 33 KB
/
snake.asm
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
; CENG336 HW2
; Ulku Meteriz
; Necip Fazil Yildiran
list P=18f8722
#include <p18f8722.inc>
config OSC = HSPLL, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOREN = OFF, WDT = OFF, MCLRE = ON, LPT1OSC = OFF, LVP = OFF, XINST = OFF, DEBUG = OFF
phaseInfo udata 0x20
phaseInfo
NOF udata 0x21 ; number of fruits
NOF
NOFL udata 0x22 ; number of fruits low
NOFL
NOFH udata 0x23 ; number of fruits high
NOFH
lives udata 0x24
lives
level udata 0x25
level
displayState udata 0x26
displayState
delay256Var udata 0x27
delay256Var
delay256Var2 udata 0x28
delay256Var2
; end: coordinates
; 7th bit: if 1, NO fruit
; if 0, fruit exists
; count is kept in [0-3] by incrementing
c00 udata 0x29
c00
c01 udata 0x30
c01
c02 udata 0x31
c02
c03 udata 0x32
c03
c10 udata 0x33
c10
c11 udata 0x34
c11
c12 udata 0x35
c12
c13 udata 0x36
c13
c20 udata 0x37
c20
c21 udata 0x38
c21
c22 udata 0x39
c22
c23 udata 0x40
c23
c30 udata 0x41
c30
c31 udata 0x42
c31
c32 udata 0x43
c32
c33 udata 0x44
c33
; end: coordinates
seed udata 0x45
seed
score udata 0x46
score
; 7th bit -> south
; 6th bit -> north
; 5th bit -> west
; 4th bit -> east
myDirection udata 0x47
myDirection
; bitwise
myRow udata 0x48
myRow
; bitwise
myCol udata 0x49
myCol
intrCounter udata 0x50
intrCounter
; newFruitFlag : 0
; moveFlag : 1
; updateFruitCountFlag : 2
; controlCountFlag : 3
; gameTimeIncrFlag : 4
; reflectLiveFlag : 5
; isEatenFlag : 6
; toggleFlag : 7
flags udata 0x51
flags
gameTime udata 0x52
gameTime
gameTimeH udata 0x53
gameTimeH
gameTimeL udata 0x54
gameTimeL
timeScaler udata 0x55
timeScaler
; level0 -> 4
; level1 -> 8
; level2 -> 16
; level3 -> 32
; indicates t/8 time
readTimeScaler udata 0x56
readTimeScaler
;levelIndicator -> readTimeScaler
levelIndicator udata 0x57
levelIndicator
elapsedT8 udata 0x58
elapsedT8
isSnakeOn udata 0x59
isSnakeOn
remainingLives udata 0x60
remainingLives
dummyVar udata 0x61
dummyVar
portBVar udata 0x62
portBVar
w_temp udata 0x63
w_temp
status_temp udata 0x64
status_temp
pclath_temp udata 0x65
pclath_temp
scoreL udata 0x66
scoreL
scoreH udata 0x67
scoreH
phase3success udata 0x68
phase3success
gameTimeControllerVar udata 0x69
gameTimeControllerVar
scNOF udata 0x70
scNOF
scNOFL udata 0x71
scNOFL
scNOFH udata 0x72
scNOFH
org 0x00
goto init
org 0x08
goto isr
org 0x18
goto lowIsr
displayTable
rlncf WREG, W ; multiply index by 2
addwf PCL, 1 ; modify program counter
retlw b'00111111' ; 0
retlw b'00000110' ; 1
retlw b'01011011' ; 2
retlw b'01001111' ; 3
retlw b'01100110' ; 4
retlw b'01101101' ; 5
retlw b'01111101' ; 6
retlw b'00000111' ; 7
retlw b'01111111' ; 8
retlw b'01100111' ; 9
retlw b'00111110' ; U
saveRegisters
movwf w_temp
swapf STATUS, w
clrf STATUS
movwf status_temp
movf PCLATH, w
movwf pclath_temp
clrf PCLATH
return
restoreRegisters
movf pclath_temp, w
movwf PCLATH
swapf status_temp, w
movwf STATUS
swapf w_temp, f
swapf w_temp, w
return
delay256
movlw d'255'
movwf delay256Var
movwf delay256Var2
delay256Loop2:
dcfsnz delay256Var2
return
movwf delay256Var
delay256Loop1:
decfsz delay256Var
goto delay256Loop1
goto delay256Loop2
return
; 7th bit -> south
; 6th bit -> north
; 5th bit -> west
; 4th bit -> east
changeDirEast
movlw b'00010000'
movwf myDirection
return
changeDirWest
movlw b'00100000'
movwf myDirection
return
changeDirNorth
movlw b'01000000'
movwf myDirection
return
changeDirSouth
movlw b'10000000'
movwf myDirection
return
B4released
movf portBVar, 0
xorwf PORTB, W ; XOR portBVar and PORTB, write result in WREG
btfsc WREG, 4
call changeDirEast
return
B5released
movf portBVar, 0
xorwf PORTB, W ; XOR portBVar and PORTB, write result in WREG
btfsc WREG, 5
call changeDirWest
return
B6released
movf portBVar, 0
xorwf PORTB, W ; XOR portBVar and PORTB, write result in WREG
btfsc WREG, 6
call changeDirNorth
return
B7released
movf portBVar, 0
xorwf PORTB, W
btfsc WREG, 7
call changeDirSouth
return
lowIsr
call saveRegisters
; now, indicate which button is pressed
btfss PORTB, 4
call B4released
btfss PORTB, 5
call B5released
btfss PORTB, 6
call B6released
btfss PORTB, 7
call B7released
; clear interrupt
movf PORTB, W
movwf portBVar ; save PORTB's last value in portBVar
clrf PORTB
bcf INTCON, 0 ; clear PORTB interrupt flag
call restoreRegisters
retfie
isr
call saveRegisters
; phase1? phase2;
btfsc phaseInfo, 0
call phase1isr
btfsc phaseInfo, 1
call phase2isr
btfsc phaseInfo, 2
call phase3isr
call restoreRegisters
retfie
phase1isr
call updateDisplayPhase1
bcf INTCON, TMR0IF
movlw b'11111100'
movwf TMR0L
return
phase2isr
nop
movf PORTB, w
nop
btfsc INTCON, TMR0IF
call phase2timer0isr
return
phase3isr
call updateDisplayPhase3
bcf INTCON, TMR0IF
movlw b'11111100'
movwf TMR0L
return
phase2timer0isr
; clear TMR0 interrupt flag
bcf INTCON, TMR0IF
decfsz timeScaler
return
goto timeScalerZero
timeScalerZero:
; init timeScaler by readTimeScaler
movf readTimeScaler, 0
movwf timeScaler
incf elapsedT8
call flagHandler
return
flagHandler
call Tover8flags
; is T/2
movlw d'4'
cpfseq elapsedT8
goto otherTover2check
goto Tover2isDone
otherTover2check:
movlw d'8'
cpfseq elapsedT8
return
goto Tover2isDone
Tover2isDone:
call Tover2flags
btfsc elapsedT8, 3
goto TisDone
return
TisDone:
call Tflags
clrf elapsedT8
return
Tover8flags
bsf flags, 7 ; toggleFlag
return
Tover2flags
; game time increment control
btfsc levelIndicator, 3
call incGT
bsf flags, 1 ; moveFlag
return
Tflags
; game time increment control
call gameTimeController
bsf flags, 2 ; updateFruitCountFlag
bsf flags, 0 ; newFruitFlag
return
gameTimeController
btfsc levelIndicator, 2
call incGT
btfsc levelIndicator, 1
goto level1caseForGT
btfsc levelIndicator, 0
goto level0caseForGT
level1caseForGT:
dcfsnz gameTimeControllerVar
call incGT
return
level0caseForGT:
dcfsnz gameTimeControllerVar
call incGT
return
initGameTimeController
movlw b'00000010'
movwf gameTimeControllerVar
btfsc levelIndicator, 0
rlncf gameTimeControllerVar
return
;T2flags
; return
;T4flags
; return
updateDisplayPhase1
btfsc displayState, 0
goto display0
btfsc displayState, 1
goto display1
btfsc displayState, 2
goto display2
btfsc displayState, 3
goto display3
display0:
; level -> display
movf level, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000001'
movwf LATH
; next state
movlw b'00000010'
movwf displayState
return
display1:
; lives -> display
movf lives, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000010'
movwf LATH
; next state
movlw b'00000100'
movwf displayState
return
display2:
; NOFH -> display
movf NOFH, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000100'
movwf LATH
; next state
movlw b'00001000'
movwf displayState
return
display3:
; NOFL -> display
movf NOFL, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00001000'
movwf LATH
; next state
movlw b'00000001'
movwf displayState
return
updateDisplayPhase2
call setscNOF
btfsc displayState, 0
goto display0Phase2
btfsc displayState, 1
goto display1Phase2
btfsc displayState, 2
goto display2Phase2
btfsc displayState, 3
goto display3Phase2
display0Phase2:
; scNOFH -> display
movf scNOFH, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000001'
movwf LATH
; next state
movlw b'00000010'
movwf displayState
return
display1Phase2:
; scNOFL -> display
movf scNOFL, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000010'
movwf LATH
; next state
movlw b'00000100'
movwf displayState
return
display2Phase2:
; gameTimeH -> display
movf gameTimeH, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000100'
movwf LATH
; next state
movlw b'00001000'
movwf displayState
return
display3Phase2:
; gameTimeL -> display
movf gameTimeL, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00001000'
movwf LATH
; next state
movlw b'00000001'
movwf displayState
return
updateDisplayPhase3
btfsc displayState, 0
goto display0Phase3
btfsc displayState, 1
goto display1Phase3
btfsc displayState, 2
goto display2Phase3
btfsc displayState, 3
goto display3Phase3
display0Phase3:
; phase3success -> display
movf phase3success, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000001'
movwf LATH
; next state
movlw b'00000010'
movwf displayState
return
display1Phase3:
; lives -> display
movf lives, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000010'
movwf LATH
; next state
movlw b'00000100'
movwf displayState
return
display2Phase3:
; scoreH -> display
movf scoreH, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00000100'
movwf LATH
; next state
movlw b'00001000'
movwf displayState
return
display3Phase3:
; scoreL -> display
movf scoreL, 0
call displayTable
movwf LATJ
; RH setting
movlw b'00001000'
movwf LATH
; next state
movlw b'00000001'
movwf displayState
return
init
call phase1init
goto main
phase1init
; about interrupts
clrf INTCON
clrf INTCON2
clrf T0CON ; configuration of Timer-0. Not on yet
movlw b'00001000'
movwf T0CON
bsf T0CON, TMR0ON ; Timer-0: on
; display state
movlw b'00000001'
movwf displayState
; phase state
clrf phaseInfo
bsf phaseInfo, 0
; clear all latches to turn off leds
clrf LATA
clrf LATB
clrf LATC
clrf LATD
clrf LATE
clrf LATF
clrf LATG
clrf LATH
clrf LATJ
movlw b'00010000'
movwf TRISA ; RA4: input
movlw b'00001111'
movwf ADCON1 ; RA4: digital input
movlw b'00111111'
movwf TRISC ; RC[0-5]: input
movlw b'11110000'
movwf TRISH ; RH[0-3]: output
movlw b'00000000'
movwf TRISJ ; RJ[0-7]: output
movlw b'00000001' ; phase: phase1
movwf phaseInfo
; number of fruits: 15
movlw d'15'
movwf NOF
movlw d'1'
movwf NOFH
movlw d'5'
movwf NOFL
movlw d'3'
movwf lives ; lives: 3
movwf level ; level: 3
movlw b'10100000' ; global interrupt: 1, timer0 interrupt: 1
movwf INTCON
call updateDisplayPhase1
return
phase2init
; about interrupts
clrf INTCON
clrf INTCON2
clrf T0CON
; clear all latches
clrf LATA
clrf LATB
clrf LATC
clrf LATD
clrf LATE
clrf LATF
clrf LATG
clrf LATH
clrf LATJ
; phaseInfo: phase2
movlw b'00000010'
movwf phaseInfo
; display state
movlw b'00000001'
movwf displayState
; I/O
clrf TRISA
clrf TRISC
clrf TRISD
clrf TRISE
clrf TRISF
clrf TRISJ
movlw b'11110000'
movwf TRISH ; RH[0-3]: output
movwf TRISB ; RB[4-7]: interrupt input
; variable init
; coordinate variables
movlw b'10000000' ; no fruit exists initially, therefore, 7th bit is set to 1, others to 0
movwf c00
movwf c01
movwf c02
movwf c03
movwf c10
movwf c11
movwf c12
movwf c13
movwf c20
movwf c21
movwf c22
movwf c23
movwf c30
movwf c31
movwf c32
movwf c33
; seed
movlw 0xAD
movwf seed
; initial score: 0
clrf score
clrf scoreH
clrf scoreL
; initial direction: south, 7th bit
movlw d'10000000'
movwf myDirection
; initial coordinate: (0,0)
clrf myRow
clrf myCol
bsf myRow, 0
bsf myCol, 0
; initial interrupt counter: 0
clrf intrCounter
; initial gameTime: 0
clrf gameTime
clrf gameTimeH
clrf gameTimeL
; elapsedT8
clrf elapsedT8
; isSnakeOn
clrf isSnakeOn
bsf isSnakeOn, 0 ; initially on
; reflectRemainingLives
bsf flags, 5
; initial timer0 settings
movlw d'19' ; initially, for level 0, timer0 should interrupt 12 times
movwf timeScaler
; shift the timeScaler to the left for 'level' times, means, timeScaler *= level
movf level, 0
incf WREG
decLoopForTimeScalerSetting:
dcfsnz WREG
goto decLoopForTimeScalerSettingDone
rlncf timeScaler
goto decLoopForTimeScalerSetting
decLoopForTimeScalerSettingDone:
movf timeScaler, 0 ; you may use it as variable, then, set it to readTimeScaler
movwf readTimeScaler ; it is for reading, don't change or count using this
; prepare a bitwise level indicator to use later
clrf levelIndicator
bsf levelIndicator, 0
movf level, 0
incf WREG
decLoopForLevelIndicatorSetting:
dcfsnz WREG
goto decLoopForLevelIndicatorSettingDone
rlncf levelIndicator
goto decLoopForLevelIndicatorSetting
decLoopForLevelIndicatorSettingDone:
call initGameTimeController
clrf flags
bsf flags, 5
; initially, im at position 0,0 (RC0)
call lightOn
movf NOF, 0
movwf scNOF
movf NOFL, 0
movwf scNOFL
movf NOFH, 0
movwf scNOFH
call intrConfig
return
intrConfig
movlw b'01000101' ; prescaler assigned
movwf T0CON
bsf RCON, IPEN ; enable interrupt priority
bcf INTCON2, 0 ; RB interrupt <- low priority
bsf INTCON2, 2 ; TMR0 <- high priority
bsf INTCON2, 7 ; enable external pull-up interrupt
bsf INTCON, 3 ; enable RB Port change
bsf INTCON, 5 ; enable TMR0 overflow interrupt
; clear PORTB interrupt flag
movf PORTB, W
clrf PORTB
bcf INTCON, 0
; set initial PORTB value
movf PORTB, 0
movwf portBVar
bsf INTCON, 6 ; enable low priority interrupt
bsf INTCON, 7 ; enable high priority interrupt
bsf T0CON, 7 ; enable timer
return
; start: RC0 polling for phase1
isRC0Pressed
btfsc PORTC, 0
call RC0pressed
return
RC0pressed
call waitRC0release
movlw d'0'
cpfseq NOF; compare f with working register, skip if equal
call decNOF
call delay256
return
waitRC0release
call delay256
btfsc PORTC, 0
bra waitRC0release
return
; end: RC0 polling for phase1
; start: RC1 polling for phase1
isRC1Pressed
btfsc PORTC, 1
call RC1pressed
return
RC1pressed
call waitRC1release
movlw d'99'
cpfseq NOF; compare f with working register, skip if equal
call incNOF
call delay256
return
waitRC1release
call delay256
btfsc PORTC, 1
bra waitRC1release
return
; end: RC1 polling for phase1
; start: RC2 polling for phase1
isRC2Pressed
btfsc PORTC, 2
call RC2pressed
return
RC2pressed
call waitRC2release
movlw d'0'
cpfseq lives ; compare f with working register, skip if equal
decf lives
call delay256
return
waitRC2release
call delay256
btfsc PORTC, 2
bra waitRC2release
return
; end: RC2 polling for phase1
; start: RC3 polling for phase1
isRC3Pressed
btfsc PORTC, 3
call RC3pressed
return
RC3pressed
call waitRC3release
movlw d'6'
cpfseq lives ; compare f with working register, skip if equal
incf lives
call delay256
return
waitRC3release
call delay256
btfsc PORTC, 3
bra waitRC3release
return
; end: RC3 polling for phase1
; start: RC4 polling for phase1
isRC4Pressed
btfsc PORTC, 4