-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.asm
1189 lines (879 loc) · 29.4 KB
/
user.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
;--------------------------------------------------------------------------------
; User defined type (structure) "player"
;--------------------------------------------------------------------------------
!zone player
.Heading: !word 0 ; Word Player angle in Ticks
.RotSpeed: !byte 1 ; Byte Player angle rotation speed
.MovSpeed: !byte 1 ; Byte Player map speed factor
.DirVectX: !word 0 ; Real Player direction vector X axis component
.DirVectY: !word 0 ; Real Player direction vector Y axis component
.PosX: !word $0000 ; Double Player position x on the map
!byte $00 ; Reminder as word and integer part as byte
.PosY: !word $0000 ; Double Player position y on the map
!byte $00 ; Reminder as word and integer part as byte
.absHeadSin !word 0 ; Real Absolute value of the heading sinus
.absHeadCos !word 0 ; Real Absolute value of the heading cosinus
;--------------------------------------------------------------------------------
; User defined type (structure) "event"
; Lower two bytes of event.keyP are giving us the action:
; 00 (0) - key up
; 01 (1) - key just pressed
; 11 (3) - key down
; 10 (2) - key just released
; Note that odd values indicate that key is down, even values that key is up.
;--------------------------------------------------------------------------------
!zone event
.keyQ !byte 0
.keyW !byte 0
.keyK !byte 0
.keyL !byte 0
.keyO !byte 0
.keyP !byte 0
.keyS !byte 0
.button !byte 0 ; Joystick button event buffer
;--------------------------------------------------------------------------------
; User defined type (structure) "seqencer"
;--------------------------------------------------------------------------------
;!zone evtSeqencer
; .preset !byte 0 ; Cycle count preset for delay
; .accum !byte 0 ; Accumulated cycle count value
; .count !byte 0 ; Sequence rayCounter
; .rollover !byte 0 ; Rollover value (go to 0 when value reached)
!zone userEvents
;********************************************************************************
; USER EVENTS
;
; Description: Events that control the player.
;
;********************************************************************************
userEvents
; Get event
jsr joystickEvents ; Get and process joystick events
jsr keyboardEvents ; Get keyboard events
jsr procKeys ; Process keyboard events
; Process events
jsr limitPlayerPos ; Limit player position (collision detect)
jsr doMapEvents ; Process events triggered by positoin on the map.
; Events are used to control the player motion
jmp getDirVect
;rts
!zone joystickEvents
;********************************************************************************
; UPDATE JOYSTICK EVENTS
;
; Description: Process joystick events, i.e. player movement.
;
;********************************************************************************
joystickEvents
lda PRA ; To save cycle time we won't update sprites
eor #%0111111 ; unless some movement is triggered.
bne .up
rts
.up lda PRA ; Test if UP
and #%00000001
bne .down
jsr joystickUp
.down lda PRA ; Test if DOWN
and #%00000010
bne .left
jsr joystickDown
.left lda PRA ; Test if LEFT
and #%00000100
bne .right
jsr joystickLeft
.right lda PRA ; Test if RIGHT
and #%00001000
bne .button
jsr joystickRight
.button lda PRA
eor #$ff ; Invert
and #%00010000 ; Filter out a button event
cmp #1 ; If not zero it will set Carry=1
rol event.button ; roll carry to the buffer.
;; Process button events
;
;lda event.button
;and #%00000011
;cmp #%00000001 ; Just pressed
;bne .exit
;
;; Button is just pressed
;
;jsr buttonJustPressed
rts
!zone joystickUp
;********************************************************************************
; JOYSTICK UP EVENT
;
; Description: Process joystick up event.
;
;********************************************************************************
joystickUp
jmp playerFWD ; Player move forward
!zone joystickDown
;********************************************************************************
; JOYSTICK DOWN EVENT
;
; Description: Process joystick up event.
;
;********************************************************************************
joystickDown
jsr invertDirVect
jsr playerFWD
jmp invertDirVect
!zone joystickLeft
;********************************************************************************
; JOYSTICK LEFT EVENT
;
; Description: Process joystick up event.
;
;********************************************************************************
joystickLeft:
jmp playerROL ; Player rotate left
!zone joystickRight
;********************************************************************************
; JOYSTICK RIGHT EVENT
;
; Description: Process joystick right event.
;
;********************************************************************************
joystickRight:
jmp playerROR ; Player rotate right
!zone palyerFWD
;********************************************************************************
; MOVE PLAYER FORWARD
;
; Description: Player forward movement. It wors similar to the turtle graphics.
; Uses unit direction vector to calculate movement.
;
; Input: player.DirVectX Real Player direction vector x component
; player.DirVectY Real Player direction vector y compoment
; player.PosX Double Player position x on the map
; player.PosY Double Player position y on the map
; player.MovSpeed Byte Player movement speed factor
;
; Outputs: player.PosX Double Player position x on the map
; player.PosY Double Player position y on the map
;
; Calls: multiply
;
; Pseudocode:
; player.PosX = player.PosX + player.DirVectX * player.MovSpeed
; player.posY = player.PosY + player.DirVectY * player.MovSpeed
;********************************************************************************
playerFWD ; X DIRECTION
lda #0
sta EL
ldy player.MovSpeed
lda player.DirVectX
sta DL
lda player.DirVectX+1
sta DH
bpl .next1 ; if dirVectX is negative then EL = $FF
dec EL
bmi .next1
; Speed multiplier
.loop1 asl DL ; Multiply by player.MovSpeed (contained in Y register)
rol DH
rol EL
.next1 dey
bpl .loop1
lda DL
clc
adc player.PosX
sta player.PosX
lda DH
adc player.PosX+1
sta player.PosX+1
lda EL
adc player.PosX+2
sta player.PosX+2
; Y DIRECTION
lda #0
sta EL
ldy player.MovSpeed
lda player.DirVectY
sta DL
lda player.DirVectY+1
sta DH
bpl .next2 ; if dirVectY is negative then EL = $FF
dec EL
bmi .next2
; Speed multiplier
.loop2 asl DL ; Multiply by player.MovSpeed (contained in Y register)
rol DH
rol EL
.next2 dey
bpl .loop2
lda DL ;Remainder part is in DX, integer part is in EL
clc
adc player.PosY
sta player.PosY
lda DH
adc player.PosY+1
sta player.PosY+1
lda EL
adc player.PosY+2
sta player.PosY+2
rts
!zone playerROL
;********************************************************************************
; PLAYER ROTATE LEFT
;
; Description: Rotate player to the left.
;
; Inputs: player.Heading
; player.RotSpeed
;
; Outputs: player.Heading
;
; Calls: getDirVect
;
; Pseudocode:
; player.Heading = pleayer.Heading + player.RotSpeed
; If player.Heading > $0400 Then
; player.Heading = player.Heading - $0400
;********************************************************************************
playerROL
ldx player.Heading+1 ; Word
lda player.Heading
clc
adc player.RotSpeed ; Byte
sta player.Heading
bcc .skip
inx
txa
and #%00000011
sta player.Heading+1
.skip rts ;jmp getDirVect
!zone playerROR
;********************************************************************************
; PLAYER ROTATE RIGHT
;
; Description: Rotate player right
;
; Inputs: player.Heading Word Player angle in Ticks
; player.RotSpeed Byte Player angle rotation speed
;
; Outputs: player.Heading Word Player angle in Ticks
;
; Calls: getDirVect
;
;********************************************************************************
playerROR
ldx player.Heading+1
lda player.Heading
sec
sbc player.RotSpeed
sta player.Heading
bcs .skip
dex
txa
and #%00000011
sta player.Heading+1
.skip rts ;jmp getDirVect
!zone playerSTL
;********************************************************************************
; PLAYER STRIFE LEFT
;
; Description: Strife player to the left.
; To strife left first rotate the player 90 deg to the left, then
; move forward and finally rotate player back to the original heading.
;
; Inputs: player.Heading
;
; Calls: getDirVect
;
; Pseudocode:
;********************************************************************************
playerSTL
lda player.Heading+1
pha
tay
iny
tya
and #%00000011
sta player.Heading+1
jsr getDirVect
jsr playerFWD
pla
sta player.Heading+1
rts; jmp getDirVect
!zone playerSTR
;********************************************************************************
; PLAYER STRIFE RIGHT
;
; Description: Strife player to the right.
; To strife left first rotate the player 90 deg to the right, then
; move forward and finally rotate player back to the original heading.
;
; Inputs: player.Heading
;
; Calls: getDirVect
;
; Pseudocode:
;********************************************************************************
playerSTR
lda player.Heading+1
pha
tay
dey
tya
and #%00000011
sta player.Heading+1
jsr getDirVect
jsr playerFWD
pla
sta player.Heading+1
rts ;jmp getDirVect
!zone keyboardEvents
;********************************************************************************
; UPDATE KEYBOARD EVENTS
;
; Description: Process keyboard events, i.e. player movement.
;
; CIA chip has port A and port B that make a mesh. To read a row or a column,
; chip is expecting sink, i.e. to bring down the pin to 0 V.
; We have to tell it first if port will be for reading or read/write.
; After that we have to bring one column and then read a row.
;
;********************************************************************************
keyboardEvents
jsr getKeyboard ; Scan keyboard
;Check if Q is pressed
lda getKeyboard.scan+7
and #%01000000
cmp #1
rol event.keyQ
;Check if W is pressed
lda getKeyboard.scan+1
and #%00000010
cmp #1
rol event.keyW
;Check if P is pressed
lda getKeyboard.scan+5 ; If the key is not pressed then the value will be zero.
and #%00000010 ; When compared with 1 it will set Carry to 0.
cmp #1 ; If the key is pressed then the value will be >= 1 and whe
rol event.keyP ; compared with 1 the Carry will be set to 1.
;Check if S is pressed
lda getKeyboard.scan+1
and #%000100000
cmp #1
rol event.keyS
;Check if K is pressed
lda getKeyboard.scan+4
and #%000100000
cmp #1
rol event.keyK
;Check if L is pressed
lda getKeyboard.scan+5
and #%000000100
cmp #1
rol event.keyL
;Check if O is pressed
lda getKeyboard.scan+4
and #%001000000
cmp #1
rol event.keyO
rts
!zone procKeys
;********************************************************************************
; KEY PRESS
;
; Description: Process key presses
;********************************************************************************
procKeys ;Check if Q is pressed
lda event.keyQ
lsr
bcc .not_Q
;Do if Q is pressed
jsr playerSTL ; Strife left
;Check if W is pressed
.not_Q lda event.keyW
lsr
bcc .not_W
;Do if W is pressed
jsr playerSTR ; Strife right
;Check if S is pressed
.not_W: lda event.keyS
and #%00000011 ; Filter lower 2 bytes
cmp #1 ; Check if just pressed
bne .exit
;Do if S was just pressed
lda screen.ViewMode
eor #%00000001
jmp setViewMode
.exit: rts
!zone getKeyboard
;********************************************************************************
; SCAN KEYBOARD FOR PRESSED KEYS
;
;Description: Scan CIA 1 register for keyboard presses.
;
;********************************************************************************
getKeyboard
lda DDRA ; Store DDRA and DDRB
pha
lda DDRB
pha
lda #$FF ; Set port A to be an output
sta DDRA ; Bit X: 0=Input (read only), 1=Output (read and write)
lda #$00 ; Set port B to be an input
sta DDRB ; Bit X: 0=Input (read only), 1=Output (read and write)
; Scan keyboard
lda #%01111111 ; Set low (connect) port A row - scanning row 7
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+7
lda #%10111111 ; Set low (connect) port A row - scanning row 6
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+6
lda #%11011111 ; Set low (connect) port A row - scanning row 5
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+5
lda #%11101111 ; Set low (connect) port A row - scanning row 4
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+4
lda #%11110111 ; Set low (connect) port A row - scanning row 3
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+3
lda #%11111011 ; Set low (connect) port A row - scanning row 2
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+2
lda #%11111101 ; Set low (connect) port A row - scanning row 1
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+1
lda #%11111110 ; Set low (connect) port A row - scanning row 0
sta PRA
lda PRB ; Read port B column (Bit X low = active)
eor #255
sta .scan+0
lda #%11111111 ; Set port A high
sta PRA
pla ; Return original values
sta DDRB
pla
sta DDRA
rts
;Local variables
.scan !byte 0, 0, 0, 0, 0, 0, 0, 0
!zone limitPlayerPos
;********************************************************************************
; LIMIT PLAYER POSITION
;
; Description: Control player movements.
;********************************************************************************
limitPlayerPos
;Limit movement to the N (up)
lda player.PosY+1
cmp #80 ; Value 80 = 2.5 pixels = 0.3125
bcs .limit_S
ldx player.PosY+2 ; Most significant byte is the cell in which player is in.
dex
ldy player.PosX+2
jsr getCellCode
;cmp #$20
;beq .limit_S ; If cell is empty then don't limit.
cmp #128 ; Anything below code 128 is empty space
bcc .limit_S
; Correct player position Y
lda #80 ; Don't allow below 80
sta player.PosY+1
; Limit movement to the S (down)
.limit_S lda player.PosY+1
cmp #176
bcc .limit_E
ldx player.PosY+2
inx
ldy player.PosX+2
jsr getCellCode
;cmp #$20
;beq .limit_E ; If cell is empty then don't limit.
cmp #128 ; Anything below code 128 is empty space
bcc .limit_E
;Correct player position y
lda #176 ; Don't allow above 176 = 0,6875
sta player.PosY+1
;Limit movement to the E (right)
.limit_E lda player.PosX+1 ; If position of the player is less than 2.5 pixels
cmp #176 ; from the right edge, then check.
bcc .limit_W
ldy player.PosX+2
iny
ldx player.PosY+2
jsr getCellCode
;cmp #$20
;beq .limit_W
cmp #128 ; Anything below code 128 is empty space
bcc .limit_W
;Correct player position x
lda #176
sta player.PosX+1
;Limit movement to the W (left)
.limit_W lda player.PosX+1
cmp #80
bcs .exit
ldy player.PosX+2
dey
ldx player.PosY+2
jsr getCellCode
;cmp #$20
;beq .exit
cmp #128 ; Anything below code 128 is empty space
bcc .exit
lda #80
sta player.PosX+1
.exit rts
!zone getCellCode
;********************************************************************************
; GET MAP CELL CODE
;
; Description: Get content of the map cell.
; Input: X Byte row of the map
; Y Byte column of the map
;
; Output: A Byte code of the cell content
;
; Using CX Virtual register
; scrAddr Table with screen row addresses
;********************************************************************************
getCellCode
lda scrAddr.lo,x
clc
adc #<map
sta CL
lda scrAddr.hi,x
adc #>map
sta CH
lda (CX),y
rts
!zone setCellCode
;********************************************************************************
; GET MAP CELL CODE
;
; Description: Set content of the map cell.
; Input: X Byte row of the map
; Y Byte column of the map
;
; Output: A Byte code of the cell content
;
; Using CX Virtual register
; scrAddr Table with screen row addresses
;********************************************************************************
setCellCode
pha
lda scrAddr.lo,x
clc
adc #<map
sta CL
lda scrAddr.hi,x
adc #>map
sta CH
pla
sta (CX),y
rts
!zone getCellColor
;********************************************************************************
; GET MAP CELL CODE
;
; Description: Get color of the cell
;
; Input: X Byte row of the map
; Y Byte column of the map
;
; Output: A Byte color of the cell
;
; Using CX Virtual register
; scrAddr Table with screen row addresses
;********************************************************************************
getCellColor
lda scrAddr.lo,x
clc
adc #<colorMap
sta CL
lda scrAddr.hi,x
adc #>colorMap
sta CH
lda (CX),y
rts
!zone setCellColor
;********************************************************************************
; GET MAP CELL CODE
;
; Description: Set color of the cell
;
; Input: X Byte row of the map
; Y Byte column of the map
;
; Output: A Byte code of the cell content
;
; Using CX Virtual register
; scrAddr Table with screen row addresses
;********************************************************************************
setCellColor
pha
lda scrAddr.lo,x
clc
adc #<colorMap
sta CL
lda scrAddr.hi,x
adc #>colorMap
sta CH
pla
sta (CX),y
rts
!zone getDirVect
;********************************************************************************
; GET PLAYER DIRECTION VECTOR
;
; Description: Get player unit direction vector.
;
; Inputs: player.Heading Word Player angle in Ticks
;
; Outputs: player.DirVectX Real Player direction vector x axis component
; player.DirVectY Real Player direction vector y axis component
;
; Calls: sinus, cosinus
;
;********************************************************************************
getDirVect
; player.DirVectX = cos(player.Heading)
lda player.Heading ; x axis
ldx player.Heading+1
jsr cosinus
lda DH
sta player.DirVectX+1
lda DL
sta player.DirVectX
; Also save the absolute value of cosinus (without sign)
; player.absHeadCos = abs(cos(Heading))
lda EL
sta player.absHeadCos
lda EH
sta player.absHeadCos+1
; player.DirVectY = sin(player.Heading)
lda player.Heading ; y axis
ldx player.Heading+1
jsr sinus ; Returning inverted Y coordinate
lda DH
sta player.DirVectY+1
lda DL
sta player.DirVectY
; Also save the absolute value of sinus (without sign)
; player.absHeadSin = abs(sin(Heading))
lda EL
sta player.absHeadSin
lda EH
sta player.absHeadSin+1
rts
!zone invertDirVect
;********************************************************************************
; INVERT DIRECTION VECTOR
;
; Get player unit direction vector.
;
; Inputs: player.DirVectX Real Player direction vector x axis component
; player.DirVectY Real Player direction vector y axis component Ticks
;
; Outputs: player.DirVectX Real Player direction vector x axis component
; player.DirVectY Real Player direction vector y axis component
;
;********************************************************************************
invertDirVect
lda #0 ; Negating result if sign is negative.
sec ; Two's complement.
sbc player.DirVectX ; Instead EOR#255 and then adding 1
sta player.DirVectX ; simply substract number from zero.
lda #0
sbc player.DirVectX+1
sta player.DirVectX+1
lda #0 ; Negating result if sign is negative.
sec ; Two's complement.
sbc player.DirVectY ; Instead EOR#255 and then adding 1
sta player.DirVectY ; simply substract number from zero.
lda #0
sbc player.DirVectY+1
sta player.DirVectY+1
rts
!zone doMapEvents
;********************************************************************************
; DO MAP EVENTS
;
; Description: Do events that are triggered on certain locations of the map.
; Here I have prepared 8 events: A, B, C, D, E, F, G and H.
; It can be easily expanded.
;
;********************************************************************************
doMapEvents
ldx player.PosY+2
ldy player.PosX+2
jsr getCellCode ; A register = cell code
cmp #$08 ; Allow only 8 events (can be expanded later)
bcs .exit
tay
lda mapEvent.lo,y
sta DL
lda mapEvent.hi,y
sta DH
jmp (DX)
.exit rts
!zone mapEvent
;--------------------------------------------------------------------------------
; Map event ET
;--------------------------------------------------------------------------------
mapEvent_ET
rts
;--------------------------------------------------------------------------------
; Map event A
; When player steps on the cell, certain wall area will change color.
; After the joystick fire button is pressed, the wall will open.
;--------------------------------------------------------------------------------
mapEvent_A
lda .stepA
bne .skipA1 ; If STEP 0 is done, then go to the STEP 1
; STEP 0: Color the part of the wall