-
Notifications
You must be signed in to change notification settings - Fork 16
/
elite-source-bank-7.asm
20114 lines (15518 loc) · 777 KB
/
elite-source-bank-7.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
; ******************************************************************************
;
; NES ELITE GAME SOURCE (BANK 7)
;
; NES Elite was written by Ian Bell and David Braben and is copyright D. Braben
; and I. Bell 1991/1992
;
; The code on this site has been reconstructed from a disassembly of the version
; released on Ian Bell's personal website at http://www.elitehomepage.org/
;
; The commentary is copyright Mark Moxon, and any misunderstandings or mistakes
; in the documentation are entirely my fault
;
; The terminology and notations used in this commentary are explained at
; https://elite.bbcelite.com/terminology
;
; The deep dive articles referred to in this commentary can be found at
; https://elite.bbcelite.com/deep_dives
;
; ------------------------------------------------------------------------------
;
; This source file contains the game code for ROM bank 7 of NES Elite.
;
; ------------------------------------------------------------------------------
;
; This source file produces the following binary file:
;
; * bank7.bin
;
; ******************************************************************************
; ******************************************************************************
;
; ELITE BANK 7
;
; Produces the binary file bank7.bin.
;
; ******************************************************************************
CODE_BANK_7% = $C000 ; The address where the code will be run
LOAD_BANK_7% = $C000 ; The address where the code will be loaded
ORG CODE_BANK_7%
; ******************************************************************************
;
; Name: ResetMMC1_b7
; Type: Variable
; Category: Start and end
; Summary: The MMC1 mapper reset routine at the start of the ROM bank
; Deep dive: Splitting NES Elite across multiple ROM banks
;
; ------------------------------------------------------------------------------
;
; When the NES is switched on, it is hardwired to perform a JMP ($FFFC). At this
; point, there is no guarantee as to which ROM banks are mapped to $8000 and
; $C000, so to ensure that the game starts up correctly, we put the same code
; in each ROM at the following locations:
;
; * We put $C000 in address $FFFC in every ROM bank, so the NES always jumps
; to $C000 when it starts up via the JMP ($FFFC), irrespective of which
; ROM bank is mapped to $C000.
;
; * We put the same reset routine (this routine, ResetMMC1) at the start of
; every ROM bank, so the same routine gets run, whichever ROM bank is mapped
; to $C000.
;
; This ResetMMC1 routine is therefore called when the NES starts up, whatever
; bank configuration ends up being. It then switches ROM bank 7 to $C000 and
; jumps into bank 7 at the game's entry point BEGIN, which starts the game.
;
; We need to give a different label to this version of the reset routine so we
; can assemble bank 7 at the same time as banks 0 to 6, to enable the lower
; banks to see the exported addresses for bank 7.
;
; ******************************************************************************
.ResetMMC1_b7
SEI ; Disable interrupts
INC $C006 ; Reset the MMC1 mapper, which we can do by writing a
; value with bit 7 set into any address in ROM space
; (i.e. any address from $8000 to $FFFF)
;
; The INC instruction does this in a more efficient
; manner than an LDA/STA pair, as it:
;
; * Fetches the contents of address $C006, which
; contains the high byte of the JMP destination
; below, i.e. the high byte of BEGIN, which is $C0
;
; * Adds 1, to give $C1
;
; * Writes the value $C1 back to address $C006
;
; $C006 is in the ROM space and $C1 has bit 7 set, so
; the INC does all that is required to reset the mapper,
; in fewer cycles and bytes than an LDA/STA pair
;
; Resetting MMC1 maps bank 7 to $C000 and enables the
; bank at $8000 to be switched, so this instruction
; ensures that bank 7 is present
JMP BEGIN ; Jump to BEGIN in bank 7 to start the game
; ******************************************************************************
;
; Name: BEGIN
; Type: Subroutine
; Category: Start and end
; Summary: Run through the NES initialisation process, reset the variables
; and start the game
;
; ******************************************************************************
.BEGIN
SEI ; Disable interrupts
CLD ; Clear the decimal flag, so we're not in decimal mode
; (this has no effect on the NES, as BCD mode is
; disabled in the NES's CPU, but we do this to ensure
; compatibility with 6502-based debuggers)
LDX #$FF ; Set the stack pointer to $01FF, which is the standard
TXS ; location for the 6502 stack, so this instruction
; effectively resets the stack
LDX #0 ; Set startupDebug = 0 (though this value is never read,
STX startupDebug ; so this has no effect)
LDA #%00010000 ; Configure the PPU by setting PPU_CTRL as follows:
STA PPU_CTRL ;
; * Bits 0-1 = base nametable address %00 ($2000)
; * Bit 2 clear = increment PPU_ADDR by 1 each time
; * Bit 3 clear = sprite pattern table is at $0000
; * Bit 4 set = background pattern table is at $1000
; * Bit 5 clear = sprites are 8x8 pixels
; * Bit 6 clear = use PPU 0 (the only option on a NES)
; * Bit 7 clear = disable VBlank NMI generation
STA ppuCtrlCopy ; Store the new value of PPU_CTRL in ppuCtrlCopy so we
; can check its value without having to access the PPU
LDA #%00000000 ; Configure the PPU by setting PPU_MASK as follows:
STA PPU_MASK ;
; * Bit 0 clear = normal colour (not monochrome)
; * Bit 1 clear = hide leftmost 8 pixels of background
; * Bit 2 clear = hide sprites in leftmost 8 pixels
; * Bit 3 clear = hide background
; * Bit 4 clear = hide sprites
; * Bit 5 clear = do not intensify greens
; * Bit 6 clear = do not intensify blues
; * Bit 7 clear = do not intensify reds
; We now wait for three VBlanks to pass to ensure that
; the PPU has stabilised after starting up
.sper1
LDA PPU_STATUS ; Wait for the first VBlank to pass, which will set bit
BPL sper1 ; 7 of PPU_STATUS (and reading PPU_STATUS clears bit 7,
; ready for the next VBlank)
.sper2
LDA PPU_STATUS ; Wait for the second VBlank to pass
BPL sper2
.sper3
LDA PPU_STATUS ; Wait for the third VBlank to pass
BPL sper3
LDA #0 ; Set K% = 0 (English) to set as the default highlighted
STA K% ; language on the Start screen (see the ChooseLanguage
; routine)
LDA #60 ; Set K%+1 = 60 to use as the value of the third counter
STA K%+1 ; when deciding how long to wait on the Start screen
; before auto-playing the demo (see the ChooseLanguage
; routine)
; Fall through into ResetToStartScreen to reset memory
; and show the Start screen
; ******************************************************************************
;
; Name: ResetToStartScreen
; Type: Subroutine
; Category: Start and end
; Summary: Reset the stack and the game's variables and show the Start screen
;
; ******************************************************************************
.ResetToStartScreen
LDX #$FF ; Set the stack pointer to $01FF, which is the standard
TXS ; location for the 6502 stack, so this instruction
; effectively resets the stack
JSR ResetVariables ; Reset all the RAM (in both the NES and cartridge), as
; it is in an undefined state when the NES is switched
; on, initialise all the game's variables, and switch to
; ROM bank 0
JMP ShowStartScreen ; Jump to ShowStartScreen in bank 0 to show the start
; screen and start the game
; ******************************************************************************
;
; Name: ResetVariables
; Type: Subroutine
; Category: Start and end
; Summary: Reset all the RAM (in both the NES and cartridge), initialise all
; the game's variables, and switch to ROM bank 0
;
; ******************************************************************************
.ResetVariables
LDA #%00000000 ; Configure the PPU by setting PPU_CTRL as follows:
STA PPU_CTRL ;
; * Bits 0-1 = base nametable address %00 ($2000)
; * Bit 2 clear = increment PPU_ADDR by 1 each time
; * Bit 3 clear = sprite pattern table is at $0000
; * Bit 4 clear = background pattern table is at $0000
; * Bit 5 clear = sprites are 8x8 pixels
; * Bit 6 clear = use PPU 0 (the only option on a NES)
; * Bit 7 clear = disable VBlank NMI generation
STA ppuCtrlCopy ; Store the new value of PPU_CTRL in ppuCtrlCopy so we
; can check its value without having to access the PPU
STA PPU_MASK ; Configure the PPU by setting PPU_MASK as follows:
;
; * Bit 0 clear = normal colour (not monochrome)
; * Bit 1 clear = hide leftmost 8 pixels of background
; * Bit 2 clear = hide sprites in leftmost 8 pixels
; * Bit 3 clear = hide background
; * Bit 4 clear = hide sprites
; * Bit 5 clear = do not intensify greens
; * Bit 6 clear = do not intensify blues
; * Bit 7 clear = do not intensify reds
STA setupPPUForIconBar ; Clear bit 7 of setupPPUForIconBar so we do nothing
; when the PPU starts drawing the icon bar
LDA #%01000000 ; Configure the APU Frame Counter as follows:
STA APU_FC ;
; * Bit 6 set = do not trigger an IRQ on the last tick
;
; * Bit 7 clear = select the four-step sequence
INC $C006 ; Reset the MMC1 mapper, which we can do by writing a
; value with bit 7 set into any address in ROM space
; (i.e. any address from $8000 to $FFFF)
;
; The INC instruction does this in a more efficient
; manner than an LDA/STA pair, as it:
;
; * Fetches the contents of address $C006, which
; contains the high byte of the JMP destination
; in the JMP BEGIN instruction, i.e. the high byte
; of BEGIN, which is $C0
;
; * Adds 1, to give $C1
;
; * Writes the value $C1 back to address $C006
;
; $C006 is in the ROM space and $C1 has bit 7 set, so
; the INC does all that is required to reset the mapper,
; in fewer cycles and bytes than an LDA/STA pair
;
; Resetting MMC1 maps bank 7 to $C000 and enables the
; bank at $8000 to be switched, so this instruction
; ensures that bank 7 is present
LDA PPU_STATUS ; Read the PPU_STATUS register, which clears the VBlank
; latch in bit 7, so the following loops will wait for
; three VBlanks in total
.resv1
LDA PPU_STATUS ; Wait for the first VBlank to pass, which will set bit
BPL resv1 ; 7 of PPU_STATUS (and reading PPU_STATUS clears bit 7,
; ready for the next VBlank)
.resv2
LDA PPU_STATUS ; Wait for the second VBlank to pass
BPL resv2
.resv3
LDA PPU_STATUS ; Wait for the third VBlank to pass
BPL resv3
; We now zero the RAM in the NES, as follows:
;
; * Zero page from $0000 to $00FF
;
; * The rest of RAM from $0300 to $05FF
;
; This clears all of the NES's built-in RAM except for
; page 1, which is used for the stack
LDA #0 ; Set A to zero so we can poke it into memory
TAX ; Set X to 0 to use as an index counter as we loop
; through zero page
.resv4
STA ZP,X ; Zero the X-th byte of zero page at ZP
INX ; Increment the byte counter
BNE resv4 ; Loop back until we have zeroed the whole of zero page
; from $0000 to $00FF
LDA #$03 ; Set SC(1 0) = $0300
STA SC+1
LDA #$00
STA SC
TXA ; Set A = 0 once again so we can poke it into memory
LDX #3 ; We now zero three pages of memory at $0300, $0400 and
; $0500, so set a page counter in X
TAY ; Set Y = 0 to use as an index counter for each page of
; memory
.resv5
STA (SC),Y ; Zero the Y-th byte of the page at SC(1 0)
INY ; Increment the byte counter
BNE resv5 ; Loop back until we have zeroed the whole page of
; memory at SC(1 0)
INC SC+1 ; Increment the high byte of SC(1 0) so it points at the
; next page of memory
DEX ; Decrement the page counter
BNE resv5 ; Loop back until we have zeroed three pages of memory
; from $0300 to $05FF
JSR SetupMMC1 ; Configure the MMC1 mapper and page ROM bank 0 into
; memory at $8000
JSR ResetMusic ; Reset the current tune to 0 and stop the music
LDA #%10000000 ; Set A = 0 and set the C flag
ASL A
JSR ResetScreen_b3 ; Reset the screen by clearing down the PPU, setting
; all colours to black, and resetting the screen-related
; variables
JSR SetDrawingPlaneTo0 ; Set the drawing bitplane to 0
JSR ResetBuffers ; Reset the pattern and nametable buffers
LDA #00000000 ; Set DTW6 = %00000000 so lower case is not enabled
STA DTW6
LDA #%11111111 ; Set DTW2 = %11111111 to denote that we are not
STA DTW2 ; currently printing a word
LDA #%11111111 ; Set DTW8 = %11111111 to denote that we do not
STA DTW8 ; capitalise the next character
; Fall through into SetBank0 to page ROM bank 0 into
; memory
; ******************************************************************************
;
; Name: SetBank0
; Type: Subroutine
; Category: Utility routines
; Summary: Page ROM bank 0 into memory at $8000
; Deep dive: Splitting NES Elite across multiple ROM banks
;
; ******************************************************************************
.SetBank0
LDA #0 ; Page ROM bank 0 into memory at $8000 and return from
JMP SetBank ; the subroutine using a tail call
; ******************************************************************************
;
; Name: SetNonZeroBank
; Type: Subroutine
; Category: Utility routines
; Summary: An unused routine that pages a specified ROM bank into memory at
; $8000, but only if it is non-zero
; Deep dive: Splitting NES Elite across multiple ROM banks
;
; ------------------------------------------------------------------------------
;
; Arguments:
;
; A The number of the ROM bank to page into memory at $8000
;
; ******************************************************************************
.SetNonZeroBank
CMP currentBank ; If the ROM bank number in A is non-zero, jump to
BNE SetBank ; SetBank to page bank A into memory, returning from the
; subroutine using a tail call
RTS ; Otherwise return from the subroutine
; ******************************************************************************
;
; Name: ResetBank
; Type: Subroutine
; Category: Utility routines
; Summary: Retrieve a ROM bank number from the stack and page that bank into
; memory at $8000
; Deep dive: Splitting NES Elite across multiple ROM banks
;
; ------------------------------------------------------------------------------
;
; Arguments:
;
; Stack The number of the ROM bank to page into memory at $8000
;
; ******************************************************************************
.ResetBank
PLA ; Retrieve the ROM bank number from the stack into A
; Fall through into SetBank to page ROM bank A into
; memory at $8000
; ******************************************************************************
;
; Name: SetBank
; Type: Subroutine
; Category: Utility routines
; Summary: Page a specified ROM bank into memory at $8000
; Deep dive: Splitting NES Elite across multiple ROM banks
;
; ------------------------------------------------------------------------------
;
; Arguments:
;
; A The number of the ROM bank to page into memory at $8000
;
; ******************************************************************************
.SetBank
DEC runningSetBank ; Decrement runningSetBank from 0 to $FF to denote that
; we are in the process of switching ROM banks
;
; This will disable the call to MakeSounds in the NMI
; handler, which instead will increment runningSetBank
; each time it is called
STA currentBank ; Store the number of the new ROM bank in currentBank
STA $FFFF ; Set the MMC1 PRG bank register (which is mapped to
LSR A ; $C000-$DFFF) to the ROM bank number in A, to map the
STA $FFFF ; specified ROM bank into memory at $8000
LSR A ;
STA $FFFF ; Bit 4 of the ROM bank number will be zero, as A is in
LSR A ; the range 0 to 7, which also ensures that PRG-RAM is
STA $FFFF ; enabled and mapped to $6000-$7FFF
LSR A
STA $FFFF
INC runningSetBank ; Increment runningSetBank again
BNE sban1 ; If runningSetBank is non-zero, then this means the NMI
; handler was called while we were switching the ROM
; bank, in which case MakeSounds won't have been called
; in the NMI handler, so jump to sban1 to call the
; MakeSounds routine now instead
RTS ; Return from the subroutine
.sban1
LDA #0 ; Set runningSetBank = 0 so the NMI handler knows we are
STA runningSetBank ; no longer switching ROM banks
LDA currentBank ; Fetch the number of the ROM bank that is currently
PHA ; paged into memory at $8000 and store it on the stack
TXA ; Store X and Y on the stack
PHA
TYA
PHA
JSR MakeSounds_b6 ; Call the MakeSounds routine to make the current sounds
; (music and sound effects)
PLA ; Retrieve X and Y from the stack
TAY
PLA
TAX
JMP ResetBank ; Fetch the previous ROM bank number from the stack and
; page that bank back into memory at $8000, returning
; from the subroutine using a tail call
; ******************************************************************************
;
; Name: xTitleScreen
; Type: Variable
; Category: Start and end
; Summary: The text column for the title screen's title for each language
; Deep dive: Multi-language support in NES Elite
;
; ******************************************************************************
.xTitleScreen
EQUB 6 ; English
EQUB 6 ; German
EQUB 7 ; French
EQUB 7 ; There is no fourth language, so this byte is ignored
; ******************************************************************************
;
; Name: xSpaceView
; Type: Variable
; Category: Flight
; Summary: The text column for the space view name for each language
; Deep dive: Multi-language support in NES Elite
;
; ******************************************************************************
.xSpaceView
EQUB 11 ; English
EQUB 9 ; German
EQUB 13 ; French
EQUB 10 ; There is no fourth language, so this byte is ignored
IF _NTSC
EQUB $20, $20, $20 ; These bytes appear to be unused
EQUB $20, $10, $00
EQUB $C4, $ED, $5E
EQUB $E5, $22, $E5
EQUB $22, $00, $00
EQUB $ED, $5E, $E5
EQUB $22, $09, $68
EQUB $00, $00, $00
EQUB $00
ELIF _PAL
EQUB $FF, $FF, $FF ; These bytes appear to be unused
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF, $FF, $FF
EQUB $FF
ENDIF
; ******************************************************************************
;
; Name: log
; Type: Variable
; Category: Maths (Arithmetic)
; Summary: Binary logarithm table (high byte)
;
; ------------------------------------------------------------------------------
;
; At byte n, the table contains the high byte of:
;
; $2000 * log10(n) / log10(2) = 32 * 256 * log10(n) / log10(2)
;
; where log10 is the logarithm to base 10. The change-of-base formula says that:
;
; log2(n) = log10(n) / log10(2)
;
; so byte n contains the high byte of:
;
; 32 * log2(n) * 256
;
; ******************************************************************************
.log
IF _MATCH_ORIGINAL_BINARIES
EQUB $6C ; This byte appears to be unused and just contains
; random workspace noise left over from the assembly
; process
EQUB $00, $20, $32, $40, $4A, $52, $59
EQUB $5F, $65, $6A, $6E, $72, $76, $79, $7D
EQUB $80, $82, $85, $87, $8A, $8C, $8E, $90
EQUB $92, $94, $96, $98, $99, $9B, $9D, $9E
EQUB $A0, $A1, $A2, $A4, $A5, $A6, $A7, $A9
EQUB $AA, $AB, $AC, $AD, $AE, $AF, $B0, $B1
EQUB $B2, $B3, $B4, $B5, $B6, $B7, $B8, $B9
EQUB $B9, $BA, $BB, $BC, $BD, $BD, $BE, $BF
EQUB $BF, $C0, $C1, $C2, $C2, $C3, $C4, $C4
EQUB $C5, $C6, $C6, $C7, $C7, $C8, $C9, $C9
EQUB $CA, $CA, $CB, $CC, $CC, $CD, $CD, $CE
EQUB $CE, $CF, $CF, $D0, $D0, $D1, $D1, $D2
EQUB $D2, $D3, $D3, $D4, $D4, $D5, $D5, $D5
EQUB $D6, $D6, $D7, $D7, $D8, $D8, $D9, $D9
EQUB $D9, $DA, $DA, $DB, $DB, $DB, $DC, $DC
EQUB $DD, $DD, $DD, $DE, $DE, $DE, $DF, $DF
EQUB $E0, $E0, $E0, $E1, $E1, $E1, $E2, $E2
EQUB $E2, $E3, $E3, $E3, $E4, $E4, $E4, $E5
EQUB $E5, $E5, $E6, $E6, $E6, $E7, $E7, $E7
EQUB $E7, $E8, $E8, $E8, $E9, $E9, $E9, $EA
EQUB $EA, $EA, $EA, $EB, $EB, $EB, $EC, $EC
EQUB $EC, $EC, $ED, $ED, $ED, $ED, $EE, $EE
EQUB $EE, $EE, $EF, $EF, $EF, $EF, $F0, $F0
EQUB $F0, $F1, $F1, $F1, $F1, $F1, $F2, $F2
EQUB $F2, $F2, $F3, $F3, $F3, $F3, $F4, $F4
EQUB $F4, $F4, $F5, $F5, $F5, $F5, $F5, $F6
EQUB $F6, $F6, $F6, $F7, $F7, $F7, $F7, $F7
EQUB $F8, $F8, $F8, $F8, $F9, $F9, $F9, $F9
EQUB $F9, $FA, $FA, $FA, $FA, $FA, $FB, $FB
EQUB $FB, $FB, $FB, $FC, $FC, $FC, $FC, $FC
EQUB $FD, $FD, $FD, $FD, $FD, $FD, $FE, $FE
EQUB $FE, $FE, $FE, $FF, $FF, $FF, $FF, $FF
ELSE
SKIP 1
FOR I%, 1, 255
EQUB HI(INT($2000 * LOG(I%) / LOG(2) + 0.5))
NEXT
ENDIF
; ******************************************************************************
;
; Name: logL
; Type: Variable
; Category: Maths (Arithmetic)
; Summary: Binary logarithm table (low byte)
;
; ------------------------------------------------------------------------------
;
; Byte n contains the low byte of:
;
; 32 * log2(n) * 256
;
; ******************************************************************************
.logL
IF _MATCH_ORIGINAL_BINARIES
EQUB $0D ; This byte appears to be unused and just contains
; random workspace noise left over from the assembly
; process
EQUB $00, $00, $B8, $00, $4D, $B8, $D5
EQUB $FF, $70, $4D, $B3, $B8, $6A, $D5, $05
EQUB $00, $CC, $70, $EF, $4D, $8D, $B3, $C1
EQUB $B8, $9A, $6A, $28, $D5, $74, $05, $88
EQUB $00, $6B, $CC, $23, $70, $B3, $EF, $22
EQUB $4D, $71, $8D, $A3, $B3, $BD, $C1, $BF
EQUB $B8, $AB, $9A, $84, $6A, $4B, $28, $00
EQUB $D5, $A7, $74, $3E, $05, $C8, $88, $45
EQUB $FF, $B7, $6B, $1D, $CC, $79, $23, $CA
EQUB $70, $13, $B3, $52, $EF, $89, $22, $B8
EQUB $4D, $E0, $71, $00, $8D, $19, $A3, $2C
EQUB $B3, $39, $BD, $3F, $C1, $40, $BF, $3C
EQUB $B8, $32, $AB, $23, $9A, $10, $84, $F7
EQUB $6A, $DB, $4B, $BA, $28, $94, $00, $6B
EQUB $D5, $3E, $A7, $0E, $74, $DA, $3E, $A2
EQUB $05, $67, $C8, $29, $88, $E7, $45, $A3
EQUB $00, $5B, $B7, $11, $6B, $C4, $1D, $75
EQUB $CC, $23, $79, $CE, $23, $77, $CA, $1D
EQUB $70, $C1, $13, $63, $B3, $03, $52, $A1
EQUB $EF, $3C, $89, $D6, $22, $6D, $B8, $03
EQUB $4D, $96, $E0, $28, $71, $B8, $00, $47
EQUB $8D, $D4, $19, $5F, $A3, $E8, $2C, $70
EQUB $B3, $F6, $39, $7B, $BD, $FE, $3F, $80
EQUB $C1, $01, $40, $80, $BF, $FD, $3C, $7A
EQUB $B8, $F5, $32, $6F, $AB, $E7, $23, $5F
EQUB $9A, $D5, $10, $4A, $84, $BE, $F7, $31
EQUB $6A, $A2, $DB, $13, $4B, $82, $BA, $F1
EQUB $28, $5E, $94, $CB, $00, $36, $6B, $A0
EQUB $D5, $0A, $3E, $73, $A7, $DA, $0E, $41
EQUB $74, $A7, $DA, $0C, $3E, $70, $A2, $D3
EQUB $05, $36, $67, $98, $C8, $F8, $29, $59
EQUB $88, $B8, $E7, $16, $45, $74, $A3, $D1
ELSE
SKIP 1
FOR I%, 1, 255
EQUB LO(INT($2000 * LOG(I%) / LOG(2) + 0.5))
NEXT
ENDIF
; ******************************************************************************
;
; Name: antilog
; Type: Variable
; Category: Maths (Arithmetic)
; Summary: Binary antilogarithm table
;
; ------------------------------------------------------------------------------
;
; At byte n, the table contains:
;
; 2^((n / 2 + 128) / 16) / 256
;
; which equals:
;
; 2^(n / 32 + 8) / 256
;
; ******************************************************************************
.antilog
FOR I%, 0, 255
EQUB HI(INT(2^((I% / 2 + 128) / 16) + 0.5))
NEXT
; ******************************************************************************
;
; Name: antilogODD
; Type: Variable
; Category: Maths (Arithmetic)
; Summary: Binary antilogarithm table
;
; ------------------------------------------------------------------------------
;
; At byte n, the table contains:
;
; 2^((n / 2 + 128.25) / 16) / 256
;
; which equals:
;
; 2^(n / 32 + 8.015625) / 256 = 2^(n / 32 + 8) * 2^(.015625) / 256
; = (2^(n / 32 + 8) + 1) / 256
;
; ******************************************************************************
.antilogODD
FOR I%, 0, 255
EQUB HI(INT(2^((I% / 2 + 128.25) / 16) + 0.5))
NEXT
; ******************************************************************************
;
; Name: SNE
; Type: Variable
; Category: Maths (Geometry)
; Summary: Sine/cosine table
; Deep dive: The sine, cosine and arctan tables
; Drawing circles
; Drawing ellipses
;
; ------------------------------------------------------------------------------
;
; This lookup table contains sine values for the first half of a circle, from 0
; to 180 degrees (0 to PI radians). In terms of circle or ellipse line segments,
; there are 64 segments in a circle, so this contains sine values for segments
; 0 to 31.
;
; In terms of segments, to calculate the sine of the angle at segment x, we look
; up the value in SNE + x, and to calculate the cosine of the angle we look up
; the value in SNE + ((x + 16) mod 32).
;
; In terms of radians, to calculate the following:
;
; sin(theta) * 256
;
; where theta is in radians, we look up the value in:
;
; SNE + (theta * 10)
;
; To calculate the following:
;
; cos(theta) * 256
;
; where theta is in radians, look up the value in:
;
; SNE + ((theta * 10) + 16) mod 32
;
; Theta must be between 0 and 3.1 radians, so theta * 10 is between 0 and 31.
;
; ******************************************************************************
.SNE
FOR I%, 0, 31
N = ABS(SIN((I% / 64) * 2 * PI))
IF N >= 1
EQUB 255
ELSE
EQUB INT(256 * N + 0.5)
ENDIF
NEXT
; ******************************************************************************
;
; Name: ACT
; Type: Variable
; Category: Maths (Geometry)
; Summary: Arctan table
; Deep dive: The sine, cosine and arctan tables
;
; ------------------------------------------------------------------------------
;
; This table contains lookup values for arctangent calculations involving angles
; in the range 0 to 45 degrees (or 0 to PI / 4 radians).
;
; To calculate the value of theta in the following:
;
; theta = arctan(t)
;
; where 0 <= t < 1, we look up the value in:
;
; ACT + (t * 32)
;
; The result will be an integer representing the angle in radians, where 256
; represents a full circle of 360 degrees (2 * PI radians). The result of the
; lookup will therefore be an integer in the range 0 to 31, as this represents
; 0 to 45 degrees (0 to PI / 4 radians).
;
; The table does not support values of t >= 1 or t < 0 directly, so if we need
; to calculate the arctangent for an angle greater than 45 degrees, we can apply
; the following calculation to the result from the table:
;
; * For t > 1, arctan(t) = 64 - arctan(1 / t)
;
; For negative values of t where -1 < t < 0, we can apply the following
; calculation to the result from the table:
;
; * For t < 0, arctan(-t) = 128 - arctan(t)
;
; Finally, if t < -1, we can do the first calculation to get arctan(|t|), and
; the second to get arctan(-|t|).
;
; ******************************************************************************
.ACT
FOR I%, 0, 31
EQUB INT((128 / PI) * ATN(I% / 32) + 0.5)
NEXT
; ******************************************************************************
;
; Name: XX21
; Type: Variable
; Category: Drawing ships
; Summary: Ship blueprints lookup table
; Deep dive: Ship blueprints
;
; ******************************************************************************
.XX21
EQUW SHIP_MISSILE ; MSL = 1 = Missile
EQUW SHIP_CORIOLIS ; SST = 2 = Coriolis space station
EQUW SHIP_ESCAPE_POD ; ESC = 3 = Escape pod
EQUW SHIP_PLATE ; PLT = 4 = Alloy plate
EQUW SHIP_CANISTER ; OIL = 5 = Cargo canister
EQUW SHIP_BOULDER ; 6 = Boulder
EQUW SHIP_ASTEROID ; AST = 7 = Asteroid
EQUW SHIP_SPLINTER ; SPL = 8 = Splinter
EQUW SHIP_SHUTTLE ; SHU = 9 = Shuttle
EQUW SHIP_TRANSPORTER ; 10 = Transporter
EQUW SHIP_COBRA_MK_3 ; CYL = 11 = Cobra Mk III
EQUW SHIP_PYTHON ; 12 = Python
EQUW SHIP_BOA ; 13 = Boa
EQUW SHIP_ANACONDA ; ANA = 14 = Anaconda
EQUW SHIP_ROCK_HERMIT ; HER = 15 = Rock hermit (asteroid)
EQUW SHIP_VIPER ; COPS = 16 = Viper
EQUW SHIP_SIDEWINDER ; SH3 = 17 = Sidewinder
EQUW SHIP_MAMBA ; 18 = Mamba
EQUW SHIP_KRAIT ; KRA = 19 = Krait
EQUW SHIP_ADDER ; ADA = 20 = Adder
EQUW SHIP_GECKO ; 21 = Gecko
EQUW SHIP_COBRA_MK_1 ; 22 = Cobra Mk I
EQUW SHIP_WORM ; WRM = 23 = Worm
EQUW SHIP_COBRA_MK_3_P ; CYL2 = 24 = Cobra Mk III (pirate)
EQUW SHIP_ASP_MK_2 ; ASP = 25 = Asp Mk II
EQUW SHIP_PYTHON_P ; 26 = Python (pirate)
EQUW SHIP_FER_DE_LANCE ; 27 = Fer-de-lance
EQUW SHIP_MORAY ; 28 = Moray
EQUW SHIP_THARGOID ; THG = 29 = Thargoid
EQUW SHIP_THARGON ; TGL = 30 = Thargon
EQUW SHIP_CONSTRICTOR ; CON = 31 = Constrictor
EQUW SHIP_COUGAR ; COU = 32 = Cougar
EQUW SHIP_DODO ; DOD = 33 = Dodecahedron ("Dodo") space station
; ******************************************************************************
;
; Name: SendBarNamesToPPU
; Type: Subroutine
; Category: PPU
; Summary: Send the nametable entries for the icon bar to the PPU
; Deep dive: Drawing vector graphics using NES tiles
;
; ------------------------------------------------------------------------------
;
; Nametable data for the icon bar is sent to PPU nametables 0 and 1.
;
; ******************************************************************************
.SendBarNamesToPPU
SUBTRACT_CYCLES 2131 ; Subtract 2131 from the cycle count
LDX iconBarRow ; Set X to the low byte of iconBarRow(1 0), to use in
; the following calculations
STX dataForPPU ; Set dataForPPU(1 0) = nameBuffer0 + iconBarRow(1 0)
LDA iconBarRow+1 ;
CLC ; So dataForPPU(1 0) points to the entry in nametable
ADC #HI(nameBuffer0) ; buffer 0 for the start of the icon bar (the addition
STA dataForPPU+1 ; works because the low byte of nameBuffer0 is 0)
LDA iconBarRow+1 ; Set (A X) = PPU_NAME_0 + iconBarRow(1 0)
ADC #HI(PPU_NAME_0) ;
; The addition works because the low byte of PPU_NAME_0
; is 0
STA PPU_ADDR ; Set PPU_ADDR = (A X)
STX PPU_ADDR ; = PPU_NAME_0 + iconBarRow(1 0)
;
; So PPU_ADDR points to the tile entry in the PPU's
; nametable 0 for the start of the icon bar
LDY #0 ; We now send the nametable entries for the icon bar to
; the PPU's nametable 0, so set a counter in Y
.ibar1
LDA (dataForPPU),Y ; Send the Y-th nametable entry from dataForPPU(1 0) to
STA PPU_DATA ; the PPU
INY ; Increment the loop counter
CPY #2*32 ; Loop back until we have sent 2 rows of 32 tiles
BNE ibar1
LDA iconBarRow+1 ; Set (A X) = PPU_NAME_1 + iconBarRow(1 0)
ADC #HI(PPU_NAME_1-1) ;
; The addition works because the low byte of PPU_NAME_1
; is 0 and because the C flag is set (as we just passed
; through the BNE above)
STA PPU_ADDR ; Set PPU_ADDR = (A X)
STX PPU_ADDR ; = PPU_NAME_1 + iconBarRow(1 0)
;
; So PPU_ADDR points to the tile entry in the PPU's
; nametable 1 for the start of the icon bar
LDY #0 ; We now send the nametable entries for the icon bar to
; the PPU's nametable 1, so set a counter in Y
.ibar2