-
Notifications
You must be signed in to change notification settings - Fork 4
/
furryrpg_mode7.inc.asm
1618 lines (1151 loc) · 28.1 KB
/
furryrpg_mode7.inc.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
;==========================================================================================
;
; "FURRY RPG" (WORKING TITLE)
; (c) 2023 by Ramsis a.k.a. ManuLöwe (https://manuloewe.de/)
;
; *** MODE7 HANDLER ***
;
;==========================================================================================
TestMode7:
.ACCU 8
.INDEX 16
lda #$80 ; enter forced blank
sta RAM_INIDISP
stz <DP2.HDMA_Channels ; disable HDMA
wai ; wait for OAM to refresh
DisableIRQs
.IFNDEF PrecalcMode7Tables
stz WMADDL ; set WRAM address to $7F0000
stz WMADDM
lda #$01
sta WMADDH
;old:
; generate tables for 128 altitude settings, 152 scanlines each:
; altitude 0: $800 / $20, $24, $28 ... $280
; altitude 1: $1000 / $20, $24, $28 ... $280
; altitude 2: $1800 / $20, $24, $28 ... $280
; ...
;new:
; generate tables for 112 altitude settings, 152 scanlines each:
; altitude 0: $8800 / $20, $24, $28 ... $280
; altitude 1: $9000 / $20, $24, $28 ... $280
; altitude 2: $9800 / $20, $24, $28 ... $280
; ...
Accu16
pea $0000 ; high word of 32-bit dividend
pea $8800 ; low word of 32-bit dividend
-- pea $0020 ; 16-bit divisor
- jsr Div32by16
Accu8
lda <DP2.Temp+4 ; location of quotient
sta WMDATA
lda <DP2.Temp+5
sta WMDATA
Accu16
pla ; divisor += 4
clc
adc #4
cmp #$0280
bcs +
pha
bra -
+ pla ; dividend += $800
clc
adc #$0800
bcc +
plx
inx
cpx #4
beq ++
phx ; push high word of dividend
+ pha ; push low word of dividend
; pea $0020 ; push initial divisor
bra --
++ phx
pha
pea $0020 ; 16-bit divisor
- jsr Div32by16
Accu8
lda <DP2.Temp+4 ; location of quotient
sta WMDATA
lda <DP2.Temp+5
sta WMDATA
Accu16
pla ; divisor += 4
clc
adc #4
cmp #$0280
bcs +
pha
bra -
+ Accu8
.ENDIF
; HDMA channel 3: color math
lda #$02 ; transfer mode (2 bytes --> $2132)
sta DMAP3
lda #<COLDATA ; PPU register $2132 (color math subscreen backdrop color)
sta BBAD3
ldx #LO8.HDMA_ColorMath
stx A1T3L
lda #$7E ; table in WRAM expected
sta A1B3
; HDMA channel 4: Mode 7 A
lda #$42 ; transfer mode (2 bytes --> $211B), indirect table mode
sta DMAP4
lda #<M7A ; PPU reg. $211B
sta BBAD4
ldx #SRC_HDMA_M7A
stx A1T4L
lda #:SRC_HDMA_M7A
sta A1B4
lda #$7E ; indirect HDMA CPU bus data address bank
sta DASB4
; HDMA channel 5: Mode 7 B
lda #$42 ; transfer mode (2 bytes --> $211C), indirect table mode
sta DMAP5
lda #<M7B ; PPU reg. $211C
sta BBAD5
ldx #SRC_HDMA_M7B
stx A1T5L
lda #:SRC_HDMA_M7B
sta A1B5
lda #$7E ; indirect HDMA CPU bus data address bank
sta DASB5
; HDMA channel 6: Mode 7 C
lda #$42 ; transfer mode (2 bytes --> $211D), indirect table mode
sta DMAP6
lda #<M7C ; PPU reg. $211D
sta BBAD6
ldx #SRC_HDMA_M7C
stx A1T6L
lda #:SRC_HDMA_M7C
sta A1B6
lda #$7E ; indirect HDMA CPU bus data address bank
sta DASB6
; HDMA channel 7: Mode 7 D
lda #$42 ; transfer mode (2 bytes --> $211E), indirect table mode
sta DMAP7
lda #<M7D ; PPU reg. $211E
sta BBAD7
ldx #SRC_HDMA_M7D
stx A1T7L
lda #:SRC_HDMA_M7D
sta A1B7
lda #$7E ; indirect HDMA CPU bus data address bank
sta DASB7
; Load Mode 7 palette
stz CGADD ; start at color 0
DMA_CH0 $02, SRC_IoTmappal, CGDATA, 512
; Load Mode 7 character data
; ############################### FOR PIC2MODE7'S GFX OUTPUT
lda #$80 ; increment VRAM address by 1 after writing to $2119
sta VMAIN
ldx #$0000 ; set VRAM address $0000
stx VMADDL
DMA_CH0 $01, SRC_IoTmappic, VMDATAL, 32768
; ##########################################################
; ############################### FOR GFX2SNES'/PCX2SNES' OUTPUT
; stz VMAIN ; increment VRAM address by 1 after writing to $2118
; ldx #$0000 ; set VRAM address $0000
; stx VMADDL
; DMA_CH0 $00, SRC_Maptilemap, VMDATAL, _sizeof_SRC_Maptilemap
; lda #$80 ; increment VRAM address by 1 after writing to $2119
; sta VMAIN
; ldx #$0000 ; set VRAM address $0000
; stx VMADDL
; DMA_CH0 $00, SRC_Mappic, VMDATAH, _sizeof_SRC_Mappic
; ##############################################################
.ENDASM
; load GFX data for rotating sky above Mode 7 world
ldx #$4000 ; set VRAM address $4000
stx VMADDL
DMA_CH0 $01, SRC_Mode7_Sky, VMDATAL, _sizeof_SRC_Mode7_Sky
ldx #$5800 ; set VRAM address $5800
stx VMADDL
DMA_CH0 $01, SRC_TileMap_Mode7_Sky, VMDATAL, _sizeof_SRC_TileMap_Mode7_Sky
stz CGADD ; reset CGRAM address
DMA_CH0 $02, SRC_Palette_Mode7_Sky, CGDATA, 32
.ASM
; Load HUD font
ResetSprites
lda #$80 ; increment VRAM address by 1 after writing to $2119
sta VMAIN
ldx #VRAM_Sprites ; set VRAM address for sprite tiles
stx VMADDL
DMA_CH0 $01, SRC_Sprites_HUDfont, VMDATAL, 4096
.ENDASM
; Load cloud sprites
DMA_CH0 $01, SRC_Sprites_Clouds, VMDATAL, 2048
lda #$A0 ; start at 3rd sprite color
sta CGADD
DMA_CH0 $02, SRC_Palette_Clouds, CGDATA, 32
; Write cloud tilemap
Accu16
lda #$1870 ; Y, X start values of upper left corner of 128×32 GFX
sta <DP2.Temp
lda #$3480 ; tile properties (highest priority, fixed), tile num (start value)
sta <DP2.Temp+2
ldx #$0000
- lda <DP2.Temp ; Y, X
sta SpriteBuf1.Reserved, x
clc
adc #$0010 ; X += 16
sta <DP2.Temp
inx
inx
lda <DP2.Temp+2 ; tile properties, tile num
sta SpriteBuf1.Reserved, x
clc
adc #$0002 ; tile num += 2
sta <DP2.Temp+2
inx
inx
bit #$000F ; check if last 4 bits of tile num clear = one row of 8 (large) sprites done?
bne - ; "inner loop"
lda <DP2.Temp
and #$FF70 ; reset X = 8
clc
adc #$1000 ; Y += 16
sta <DP2.Temp
lda <DP2.Temp+2
clc
adc #$0010 ; tile num += 16 (i.e., skip one row of 8*8 tiles)
sta <DP2.Temp+2
cpx #$40 ; 16 large sprites done?
bne - ; "outer loop"
Accu8
stz <DP2.Temp+2 ; see Mode 7 matrix calculations below
; Set transparency for clouds
lda #%00010000 ; turn on BG1 on mainscreen only, sprites on subscreen
sta RAM_TM
sta RAM_TS
Accu8
lda #$E0 ; subscreen backdrop color
sta RAM_COLDATA
sta RAM_COLDATA
lda #%0000010 ; enable BGs/OBJs on subscreen
sta RAM_CGWSEL
lda #%01000001 ; enable color math on BG1, add subscreen and div by 2
sta RAM_CGADSUB
.ASM
; Load sky backdrop gradient
ldx #loword(RAM.HDMA_BackgrPlayfield) ; set WRAM address to playfield HDMA background
stx WMADDL
stz WMADDH
DMA_CH0 $00, SRC_HDMA_Mode7Sky72, WMDATA, _sizeof_SRC_HDMA_Mode7Sky72
; Set (PPU shadow) registers
lda #$58 ; VRAM address for BG2 tilemap: $5800, size: 32×32 tiles
sta RAM_BG2SC
lda #$40 ; VRAM address for BG2 character data: $4000 (ignore BG1 bits)
sta RAM_BG12NBA
lda #$01|$08 ; BG Mode 1 for sky, BG3 priority (Mode 7 is switched to via IRQ)
sta RAM_BGMODE
; lda #%00010010 ; turn on BG2 (for rotating sky above Mode 7 world) and sprites
lda #%00010000 ; turn on sprites only
sta RAM_TM ; on the mainscreen
sta RAM_TS ; and on the subscreen
; lda #$FF ; scroll BG2 down by 1 px (for rotating sky above Mode 7 world)
; sta BG2VOFS
; stz BG2VOFS
; Load horizon blur
ldx #$0000
- lda.l SRC_HDMA_ColMathMode7, x
sta LO8.HDMA_ColorMath, x
inx
cpx #_sizeof_SRC_HDMA_ColMathMode7
bne -
lda #%00000000 ; clear color math disable bits (4-5)
sta RAM_CGWSEL
lda #%00110111 ; enable color math on BG1/2/3 + sprites + backdrop
sta RAM_CGADSUB
; Set new NMI/IRQ vectors & screen parameters
SetNMI kNMI_Mode7
Accu16
lda #220 ; dot number for interrupt (256 = too late, 204 = too early)
sta HTIMEL
lda #kMode7SkyLines ; scanline number for interrupt
sta VTIMEL
Accu8
SetIRQ kVIRQ_Mode7
lda #%11111100 ; enable HDMA channels 2-7
sta <DP2.HDMA_Channels
lda RDNMI ; clear NMI flag
lda #%10110001 ; enable NMI, auto-joypad read, and IRQ at H=HTIMEL and V=VTIMEL
sta LO8.NMITIMEN
sta NMITIMEN
cli
jsr ResetMode7Matrix
WaitFrames 1 ; wait for altitude setting to take effect
Accu16
stz <DP2.Temp ; clear DP2.Temp vars (used in CalcMode7Matrix)
stz <DP2.Temp+2
stz <DP2.Temp+4
stz <DP2.Temp+6
Accu8
jsr CalcMode7Matrix
lda #kEffectSpeed3
sta <DP2.EffectSpeed
ldx #kEffectTypeHSplitIn
jsr (PTR_EffectTypes, x)
Mode7Loop:
; PrintSpriteText 25, 22, "V-Line: $", 1
; PrintSpriteHexNum <DP2.Temp+7
PrintSpriteText 25, 20, "Altitude: $", 1
PrintSpriteHexNum <DP2.Mode7_Altitude
; PrintSpriteText 23, 22, "Angle: $", 1
; PrintSpriteHexNum <DP2.Mode7_RotAngle
; PrintSpriteText 25, 18, "Center-X: $", 1
; PrintSpriteHexNum <DP2.Mode7_CenterCoordX+1
; PrintSpriteHexNum <DP2.Mode7_CenterCoordX
; PrintSpriteText 24, 21, "ScrX: $", 1
; PrintSpriteHexNum <DP2.Mode7_ScrollOffsetX+1
; PrintSpriteHexNum <DP2.Mode7_ScrollOffsetX
; PrintSpriteText 25, 21, "ScrY: $", 1
; PrintSpriteHexNum <DP2.Mode7_ScrollOffsetY+1
; PrintSpriteHexNum <DP2.Mode7_ScrollOffsetY
WaitFrames 1 ; don't use WAI here as IRQ is enabled
; Check for dpad up+left
lda <DP2.Joy1Press+1
and #%00001010
cmp #%00001010
bne @DpadUpLeftDone
dec <DP2.Mode7_RotAngle
lda <DP2.Mode7_Altitude
inc a
cmp #112
bcc +
lda #111
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
jmp @SkipDpad ; skip additional d-pad checks (no more than 2 directions allowed)
@DpadUpLeftDone:
; Check for dpad up+right
lda <DP2.Joy1Press+1
and #%00001001
cmp #%00001001
bne @DpadUpRightDone
inc <DP2.Mode7_RotAngle
lda <DP2.Mode7_Altitude
inc a
cmp #112
bcc +
lda #111
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
bra @SkipDpad
@DpadUpRightDone:
; Check for dpad down+left
lda <DP2.Joy1Press+1
and #%00000110
cmp #%00000110
bne @DpadDownLeftDone
dec <DP2.Mode7_RotAngle
lda <DP2.Mode7_Altitude
dec a
bpl +
lda #0
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
bra @SkipDpad
@DpadDownLeftDone:
; Check for dpad down+right
lda <DP2.Joy1Press+1
and #%00000101
cmp #%00000101
bne @DpadDownRightDone
inc <DP2.Mode7_RotAngle
lda <DP2.Mode7_Altitude
dec a
bpl +
lda #0
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
bra @SkipDpad
@DpadDownRightDone:
; Check for dpad up
lda <DP2.Joy1Press+1
and #kDpadUp
beq @DpadUpDone
lda <DP2.Mode7_Altitude
inc a
cmp #112
bcc +
lda #111
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
@DpadUpDone:
; Check for dpad down
lda <DP2.Joy1Press+1
and #kDpadDown
beq @DpadDownDone
lda <DP2.Mode7_Altitude
dec a
bpl +
lda #0
+ sta <DP2.Mode7_Altitude
jsr CalcMode7Matrix
@DpadDownDone:
; Check for dpad left
lda <DP2.Joy1Press+1
and #kDpadLeft
beq @DpadLeftDone
; lda <DP2.Mode7_BG2HScroll ; (for rotating sky above Mode 7 world)
; sec
; sbc #5
; sta <DP2.Mode7_BG2HScroll
dec <DP2.Mode7_RotAngle
jsr CalcMode7Matrix
@DpadLeftDone:
; Check for dpad right
lda <DP2.Joy1Press+1
and #kDpadRight
beq @DpadRightDone
; lda <DP2.Mode7_BG2HScroll ; (for rotating sky above Mode 7 world)
; clc
; adc #5
; sta <DP2.Mode7_BG2HScroll
inc <DP2.Mode7_RotAngle
jsr CalcMode7Matrix
@DpadRightDone:
@SkipDpad:
; Check for L
lda <DP2.Joy1Press
and #kButtonL
beq @LButtonDone
dec <DP2.Mode7_RotAngle
dec <DP2.Mode7_RotAngle
jsr CalcMode7Matrix
@LButtonDone:
; Check for R
lda <DP2.Joy1Press
and #kButtonR
beq @RButtonDone
inc <DP2.Mode7_RotAngle
inc <DP2.Mode7_RotAngle
jsr CalcMode7Matrix
@RButtonDone:
; Check for A = fly forward
lda <DP2.Joy1Press
bpl @AButtonDone
lda <DP2.Mode7_RotAngle ; $00 < angle < $80 --> inc X
beq @FlightY ; don't change X if angle = 0
cmp #$80
beq @FlightY ; don't change X if angle = 128 (eq. 180°)
bcs +
jsr M7FlightIncX
bra @FlightY
+ jsr M7FlightDecX ; $80 < angle <= $FF --> dec X
@FlightY:
lda <DP2.Mode7_RotAngle ; $40 < angle < $C0 --> inc Y
cmp #$40
beq @AButtonDone ; don't change Y if angle = 64 (eq. 90°)
bcc +
cmp #$C0
beq @AButtonDone ; don't change Y if angle = 192 (eq. 270°)
bcs +
jsr M7FlightIncY
bra @AButtonDone
+ jsr M7FlightDecY ; $C0 < angle <= $FF | $00 < angle < $40 --> dec Y
@AButtonDone:
.ENDASM
; Check for B
lda <DP2.Joy1Press+1
bpl @BButtonDone
@BButtonDone:
; Check for X
bit <DP2.Joy1Press
bvc @XButtonDone
@XButtonDone:
; Check for Y
bit <DP2.Joy1Press+1
bvc @YButtonDone
@YButtonDone:
.ASM
; Check for Start
lda <DP2.Joy1New+1
and #kButtonStart
beq @StartButtonDone
lda #kEffectSpeed3
sta <DP2.EffectSpeed
ldx #kEffectTypeHSplitOut2
jsr (PTR_EffectTypes, x)
lda #%00110000 ; clear IRQ enable bits
trb LO8.NMITIMEN
; jsr SpriteInit ; purge OAM
; lda #$80 ; increment VRAM address by 1 after writing to $2119
; sta VMAIN
; stz VMADDL ; reset VRAM address
; stz VMADDH
; DMA_CH0 $09, SRC_Zeroes, VMDATAL, 0 ; clear VRAM // no, not with NMI enabled
jml DebugMenu
@StartButtonDone:
; Check for Select
lda <DP2.Joy1New+1
and #kButtonSel
beq @SelButtonDone
jsr ResetMode7Matrix
WaitFrames 1 ; wait for altitude setting to take effect
jsr CalcMode7Matrix
@SelButtonDone:
; Misc. tasks, end loop
ldx #loword(RAM.SpriteDataArea) ; set WRAM address for area sprite data array
stx WMADDL
stz WMADDH
jsr ConvertSpriteDataToBuffer
jmp Mode7Loop
; Calculate Mode 7 matrix parameters for next frame
; This is the algorithm to do signed 8×16 bit multiplication required for Mode 7 registers using hardware multiplication:
; - do unsigned 8×8 bit multiplication for lower 8 bits of multiplicand (while keeping track of multiplier sign), store 16-bit interim result in DP2.Temp
; - do unsigned 8×8 bit multiplication for upper 8 bits of multiplicand, add (interim result >> 8) and store as 16-bit final result (if multiplier sign was negative, make result negative)
.IFDEF PrecalcMode7Tables
CalcMode7Matrix:
lda #:SRC_Mode7Scaling
sta <DP2.DataBank
; Calculate M7A/M7D for next frame
; 8×8 multiplication for lower 8 bits of multiplicand
ldx #0
- stz <DP2.Temp+6
txy
lda [<DP2.DataAddress], y
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the cos table
lda.l SRC_CosineTable, x
bpl + ; check for negative multiplier
eor #$FF ; make multiplier positive
inc a
dec <DP2.Temp+6 ; remember that multplier has wrong sign
+ sta WRMPYB
nop ; burn a few cycles
nop
ldx RDMPYL ; store interim result
stx <DP2.Temp
; 8×8 multiplication for upper 8 bits of multiplicand
iny
lda [<DP2.DataAddress], y
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the cos table
lda.l SRC_CosineTable, x
bpl + ; check for negative multiplier once again
eor #$FF ; make multiplier positive
inc a
+ sta WRMPYB
tyx
dex ; make up for preceding iny
Accu16 ; 3 cycles
lda RDMPYL ; 3 // store final result
clc
adc <DP2.Temp+1
bit <DP2.Temp+5 ; check for multiplier sign
bpl +
eor #$FFFF ; multiplier was negative, so make final result negative
inc a
+ sta LO8.HDMA_M7A+(kMode7SkyLines*2), x
sta LO8.HDMA_M7D+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
; Calculate M7B/M7C for next frame
; 8×8 multiplication for lower 8 bits of multiplicand
ldx #0
- stz <DP2.Temp+6
txy
lda [<DP2.DataAddress], y
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the sin table
lda.l SRC_SineTable, x
bpl + ; check for negative multiplier
eor #$FF ; make multiplier positive
inc a
dec <DP2.Temp+6 ; remember that multplier has wrong sign
+ sta WRMPYB
nop ; burn a few cycles
nop
ldx RDMPYL ; store interim result
stx <DP2.Temp
; 8×8 multiplication for upper 8 bits of multiplicand
iny
lda [<DP2.DataAddress], y
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the sin table
lda.l SRC_SineTable, x
bpl + ; check for negative multiplier once again
eor #$FF ; make multiplier positive
inc a
+ sta WRMPYB
tyx
dex
Accu16 ; 3 cycles
lda RDMPYL ; 3 // store final result
clc
adc <DP2.Temp+1
bit <DP2.Temp+5 ; check for multiplier sign
bmi +
sta LO8.HDMA_M7C+(kMode7SkyLines*2), x
eor #$FFFF ; make M7C parameter negative and store in M7B
inc a
sta LO8.HDMA_M7B+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
rts
.ACCU 16
+ sta LO8.HDMA_M7B+(kMode7SkyLines*2), x ; multiplier was negative, store negative value first in M7B
eor #$FFFF ; make M7B parameter positive and store in M7C
inc a
sta LO8.HDMA_M7C+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
rts
.ELSE
CalcMode7Matrix:
ldx <DP2.DataAddress ; set WRAM address to beginning of table (based on current altitude, as set during Vblank)
stx WMADDL
lda #$01
sta WMADDH
; Calculate M7A/M7D for next frame
; 8×8 multiplication for lower 8 bits of multiplicand
ldx #0
- stz <DP2.Temp+6
lda WMDATA ; read table entry
sta WRMPYA
txy ; preserve X in Y, this is faster than using the stack
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the cos table
lda.l SRC_CosineTable, x
bpl + ; check for negative multiplier
eor #$FF ; make multiplier positive
inc a
dec <DP2.Temp+6 ; remember that multplier has wrong sign
+ sta WRMPYB
nop ; burn a few cycles
nop
ldx RDMPYL ; store interim result
stx <DP2.Temp
; 8×8 multiplication for upper 8 bits of multiplicand
lda WMDATA ; read table entry
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the cos table
lda.l SRC_CosineTable, x
bpl + ; check for negative multiplier once again
eor #$FF ; make multiplier positive
inc a
+ sta WRMPYB
tyx ; restore X // 2 cycles
Accu16 ; 3
lda RDMPYL ; 3 // store final result
clc
adc <DP2.Temp+1
bit <DP2.Temp+5 ; check for multiplier sign
bpl +
eor #$FFFF ; multiplier was negative, so make final result negative
inc a
+ sta LO8.HDMA_M7A+(kMode7SkyLines*2), x
sta LO8.HDMA_M7D+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
ldx <DP2.DataAddress ; reset WRAM address to beginning of table
stx WMADDL
; Calculate M7B/M7C for next frame
; 8×8 multiplication for lower 8 bits of multiplicand
ldx #0
- stz <DP2.Temp+6
lda WMDATA ; read table entry
sta WRMPYA
txy ; preserve X
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the sin table
lda.l SRC_SineTable, x
bpl + ; check for negative multiplier
eor #$FF ; make multiplier positive
inc a
dec <DP2.Temp+6 ; remember that multplier has wrong sign
+ sta WRMPYB
nop ; burn a few cycles
nop
ldx RDMPYL ; store interim result
stx <DP2.Temp
; 8×8 multiplication for upper 8 bits of multiplicand
lda WMDATA ; read table entry
sta WRMPYA
ldx <DP2.Mode7_RotAngle ; angle into X for indexing into the sin table
lda.l SRC_SineTable, x
bpl + ; check for negative multiplier once again
eor #$FF ; make multiplier positive
inc a
+ sta WRMPYB
tyx ; restore X // 2 cycles
Accu16 ; 3
lda RDMPYL ; 3 // store final result
clc
adc <DP2.Temp+1
bit <DP2.Temp+5 ; check for multiplier sign
bmi +
sta LO8.HDMA_M7C+(kMode7SkyLines*2), x
eor #$FFFF ; make M7C parameter negative and store in M7B
inc a
sta LO8.HDMA_M7B+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
rts
.ACCU 16
+ sta LO8.HDMA_M7B+(kMode7SkyLines*2), x ; multiplier was negative, store negative value first in M7B
eor #$FFFF ; make M7B parameter positive and store in M7C
inc a
sta LO8.HDMA_M7C+(kMode7SkyLines*2), x
Accu8
inx
inx
cpx #448-(kMode7SkyLines*2)
bne -
rts
.ACCU 16
;-----------------------------
; 32 by 16 bit division (c) by Garth Wilson
; http://6502.org/source/integers/ummodfix/ummodfix.htm
;
; +-----|-----+-----|-----+-----|-----+-----|------+
; | DIVISOR | D I V I D E N D |temp storage|
; | | hi cell lo cell |of carry bit|
; | N N+1 | N+2 N+3 | N+4 N+5 | N+6 N+7 |
; +-----|-----+-----|-----+-----|-----+-----|------+
;
Div32by16:
lda 7, s ; high word of 32-bit dividend
sta <DP2.Temp+2
lda 5, s ; low word of 32-bit dividend
sta <DP2.Temp+4
lda 3, s ; 16-bit divisor
sta <DP2.Temp
stz <DP2.Temp+6 ; clear 16-bit of temp storage