-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelite-loader1.asm
1337 lines (1043 loc) · 46.2 KB
/
elite-loader1.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
\ ******************************************************************************
\
\ 6502 SECOND PROCESSOR ELITE I/O LOADER SOURCE (PART 1 OF 2)
\
\ 6502 Second Processor Elite was written by Ian Bell and David Braben and is
\ copyright Acornsoft 1985
\
\ The code in this file is identical to the source discs released on Ian Bell's
\ personal website at http://www.elitehomepage.org/ (it's just been reformatted
\ to be more readable)
\
\ 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 first of two game loaders for 6502 Second
\ Processor Elite.
\
\ ------------------------------------------------------------------------------
\
\ This source file produces the following binary file:
\
\ * ELITE.bin
\
\ ******************************************************************************
INCLUDE "1-source-files/main-sources/elite-build-options.asm"
_SOURCE_DISC = (_VARIANT = 1)
_SNG45 = (_VARIANT = 2)
_EXECUTIVE = (_VARIANT = 3)
GUARD &4000 \ Guard against assembling over screen memory
\ ******************************************************************************
\
\ Configuration variables
\
\ ******************************************************************************
IF _SNG45 OR _EXECUTIVE
CODE% = &1FDC \ The address where the code will be run
LOAD% = &1FDC \ The address where the code will be loaded
ELIF _SOURCE_DISC
CODE% = &2000 \ The address where the code will be run
LOAD% = &2000 \ The address where the code will be loaded
ENDIF
N% = 77 \ N% is set to the number of bytes in the VDU table, so
\ we can loop through them in the loader below
VIA = &FE00 \ Memory-mapped space for accessing internal hardware,
\ such as the video ULA, 6845 CRTC and 6522 VIAs (also
\ known as SHEILA)
OSWRCH = &FFEE \ The address for the OSWRCH routine
OSBYTE = &FFF4 \ The address for the OSBYTE routine
OSWORD = &FFF1 \ The address for the OSWORD routine
OSCLI = &FFF7 \ The address for the OSCLI routine
\ ******************************************************************************
\
\ Name: ZP
\ Type: Workspace
\ Address: &0090 to &0095
\ Category: Workspaces
\ Summary: Important variables used by the loader
\
\ ******************************************************************************
ORG &0090
.ZP
SKIP 2 \ Stores addresses used for moving content around
.P
SKIP 1 \ Temporary storage, used in a number of places
.Q
SKIP 1 \ Temporary storage, used in a number of places
.YY
SKIP 1 \ Temporary storage, used in a number of places
.T
SKIP 1 \ Temporary storage, used in a number of places
\ ******************************************************************************
\
\ ELITE LOADER
\
\ ******************************************************************************
ORG CODE%
\ ******************************************************************************
\
\ Name: copyright
\ Type: Variable
\ Category: Copy protection
\ Summary: A copyright notice, buried in the code
\
\ ******************************************************************************
IF _SNG45 OR _EXECUTIVE
EQUS "Copyright (c) Acornsoft Limited 1985"
ENDIF
\ ******************************************************************************
\
\ Name: B%
\ Type: Variable
\ Category: Drawing the screen
\ Summary: VDU commands for setting the square mode 1 screen
\ Deep dive: The split-screen mode in BBC Micro Elite
\ Drawing monochrome pixels on the BBC Micro
\
\ ------------------------------------------------------------------------------
\
\ This block contains the bytes that get written by OSWRCH to set up the screen
\ mode (this is equivalent to using the VDU statement in BASIC).
\
\ It defines the whole screen using a square, monochrome mode 1 configuration;
\ the mode 2 part for the dashboard is implemented in the IRQ1 routine.
\
\ The top part of Elite's screen mode is based on mode 1 but with the following
\ differences:
\
\ * 64 columns, 31 rows (256 x 248 pixels) rather than 80, 32
\
\ * The horizontal sync position is at character 90 rather than 98, which
\ pushes the screen to the right (which centres it as it's not as wide as
\ the normal screen modes)
\
\ * Screen memory goes from &4000 to &7EFF
\
\ * The text window is 1 row high and 13 columns wide, and is at (2, 16)
\
\ * The cursor is disabled
\
\ This almost-square mode 1 variant makes life a lot easier when drawing to the
\ screen, as there are 256 pixels on each row (or, to put it in screen memory
\ terms, there are two pages of memory per row of pixels).
\
\ There is also an interrupt-driven routine that switches the bytes-per-pixel
\ setting from that of mode 1 to that of mode 2, when the raster reaches the
\ split between the space view and the dashboard.
\
\ ******************************************************************************
.B%
EQUB 22, 1 \ Switch to screen mode 1
EQUB 28 \ Define a text window as follows:
EQUB 2, 17, 15, 16 \
\ * Left = 2
\ * Right = 15
\ * Top = 16
\ * Bottom = 17
\
\ i.e. 1 row high, 13 columns wide at (2, 16)
EQUB 23, 0, 6, 31 \ Set 6845 register R6 = 31
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This is the "vertical displayed" register, and sets
\ the number of displayed character rows to 31. For
\ comparison, this value is 32 for standard modes 1 and
\ 2, but we claw back the last row for storing code just
\ above the end of screen memory
EQUB 23, 0, 12, &08 \ Set 6845 register R12 = &08 and R13 = &00
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This sets 6845 registers (R12 R13) = &0800 to point
EQUB 23, 0, 13, &00 \ to the start of screen memory in terms of character
EQUB 0, 0, 0 \ rows. There are 8 pixel lines in each character row,
EQUB 0, 0, 0 \ so to get the actual address of the start of screen
\ memory, we multiply by 8:
\
\ &0800 * 8 = &4000
\
\ So this sets the start of screen memory to &4000
EQUB 23, 0, 1, 64 \ Set 6845 register R1 = 64
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This is the "horizontal displayed" register, which
\ defines the number of character blocks per horizontal
\ character row. For comparison, this value is 80 for
\ modes 1 and 2, but our custom screen is not as wide at
\ only 64 character blocks across
EQUB 23, 0, 2, 90 \ Set 6845 register R2 = 90
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This is the "horizontal sync position" register, which
\ defines the position of the horizontal sync pulse on
\ the horizontal line in terms of character widths from
\ the left-hand side of the screen. For comparison this
\ is 98 for modes 1 and 2, but needs to be adjusted for
\ our custom screen's width
EQUB 23, 0, 10, 32 \ Set 6845 register R10 = %00100000 = 32
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This is the "cursor start" register, and bits 5 and 6
\ define the "cursor display mode", as follows:
\
\ * %00 = steady, non-blinking cursor
\
\ * %01 = do not display a cursor
\
\ * %10 = fast blinking cursor (blink at 1/16 of the
\ field rate)
\
\ * %11 = slow blinking cursor (blink at 1/32 of the
\ field rate)
\
\ We can therefore turn off the cursor completely by
\ setting cursor display mode %01, with bit 6 of R10
\ clear and bit 5 of R10 set
EQUB 23, 0, &87, 34 \ Set 6845 register R7 = 34
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This is the "vertical sync position" register, which
\ defines the row number where the vertical sync pulse
\ is fired. This is already set to 34 for mode 1 and 2,
\ so I'm not sure what this VDU sequence does,
\ especially as the register number has bit 7 set (it's
\ &87 rather than 7). More investigation needed!
\ ******************************************************************************
\
\ Name: E%
\ Type: Variable
\ Category: Sound
\ Summary: Sound envelope definitions
\
\ ------------------------------------------------------------------------------
\
\ This table contains the sound envelope data, which is passed to OSWORD by the
\ FNE macro to create the four sound envelopes used in-game. Refer to chapter 30
\ of the "BBC Microcomputer User Guide" by John Coll for details of sound
\ envelopes and what all the parameters mean.
\
\ The envelopes are as follows:
\
\ * Envelope 1 is the sound of our own laser firing
\
\ * Envelope 2 is the sound of lasers hitting us, or hyperspace
\
\ * Envelope 3 is the first sound in the two-part sound of us dying, or the
\ second sound in the two-part sound of us hitting or killing an enemy ship
\
\ * Envelope 4 is the sound of E.C.M. firing
\
\ ******************************************************************************
.E%
EQUB 1, 1, 0, 111, -8, 4, 1, 8, 8, -2, 0, -1, 126, 44
EQUB 2, 1, 14, -18, -1, 44, 32, 50, 6, 1, 0, -2, 120, 126
EQUB 3, 1, 1, -1, -3, 17, 32, 128, 1, 0, 0, -1, 1, 1
EQUB 4, 1, 4, -8, 44, 4, 6, 8, 22, 0, 0, -127, 126, 0
\ ******************************************************************************
\
\ Name: FNE
\ Type: Macro
\ Category: Sound
\ Summary: Macro definition for defining a sound envelope
\
\ ------------------------------------------------------------------------------
\
\ The following macro is used to define the four sound envelopes used in the
\ game. It uses OSWORD 8 to create an envelope using the 14 parameters in the
\ I%-th block of 14 bytes at location E%. This OSWORD call is the same as BBC
\ BASIC's ENVELOPE command.
\
\ See variable E% for more details of the envelopes themselves.
\
\ ******************************************************************************
MACRO FNE I%
LDX #LO(E%+I%*14) \ Set (Y X) to point to the I%-th set of envelope data
LDY #HI(E%+I%*14) \ in E%
LDA #8 \ Call OSWORD with A = 8 to set up sound envelope I%
JSR OSWORD
ENDMACRO
\ ******************************************************************************
\
\ Name: Elite loader
\ Type: Subroutine
\ Category: Loader
\ Summary: Check for a 6502 Second Processor, perform a number of OS calls,
\ set up sound and run the second loader
\
\ ******************************************************************************
.ENTRY
CLD \ Clear the decimal flag, so we're not in decimal mode
IF _SNG45 OR _EXECUTIVE
NOP \ In SNG45, the release version of 6502 Second Processor
NOP \ Elite, the detection code from the original source is
NOP \ disabled and replaced by NOPs
NOP \
NOP \ This is also true in the Executive version
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
LDA #234 \ Call OSBYTE with A = 234, X = 0 and Y = &FF, which
LDX #0 \ detects whether Tube hardware is present, returning
LDY #&FF \ X = 0 (not present) or X = &FF (present)
JSR OSBYTE
ELIF _SOURCE_DISC
LDA #129 \ Call OSBYTE with A = 129, X = 0 and Y = &FF to detect
LDX #0 \ the machine type. This call is undocumented and is not
LDY #&FF \ the recommended way to determine the machine type
JSR OSBYTE \ (OSBYTE 0 is the correct way), but this call returns
\ the following:
\
\ * X = Y = 0 if this is a BBC Micro with MOS 0.1
\ * X = Y = &FF if this is a BBC Micro with MOS 1.20
TXA \ If X is non-zero then jump to not0, as this is not MOS
BNE not0 \ 0.1
TYA \ If Y is non-zero then jump to not0, as this is not MOS
BNE not0 \ 0.1
JMP happy \ If we get here then this is a BBC Micro Model B with
\ MOS 0.1, so jump to happy to continue the loading
\ process (which is a bit odd as Elite doesn't work on
\ MOS 0.1, so this just tries to load the game, which
\ fails)
.not0
\ If we get here, then this is not MOS 0.1. We now jump
\ to blap2 only if X = &FF and Y = &FF
INX \ Increment X, which will give us zero if X was &FF
BNE blap1 \ If X is non-zero, then X was not &FF before the
\ increment, so jump to blap1
INY \ Increment Y, which will give us zero if Y was &FF
BEQ blap2 \ If Y is now 0, jump to blap2 for further checks
.blap1
JMP happy \ If we get here then this is not a BBC Micro with MOS
\ 1.20... so we jump to happy to continue the loading
\ process, which is again a bit odd, as the game won't
\ actually work (if you load this version of the game
\ on a Master or BBC B+, it will try to run the game
\ rather than giving an error - most odd)
\JSR ZZZAP \ These instructions are commented out in the original
\BRK \ source
\EQUB 0
\EQUS " This program"
\EQUS " only runs on a"
\EQUS " BBC Micro with"
\EQUS " 6502 Second"
\EQUS " Processor"
\EQUW &0C0A
\BRK
.blap2
\ If we get here then this is a BBC Micro with MOS 1.20,
\ and we arrive here with X = 0 and Y = 0
LDA #234 \ Call OSBYTE with A = 234, X = 0 and Y = &FF, which
DEY \ detects whether Tube hardware is present, returning
JSR OSBYTE \ X = 0 (not present) or X = &FF (present)
ENDIF
TXA \ If X is non-zero (Tube is present) then jump to happy
BNE happy \ to continue the loading process
JSR ZZZAP \ Otherwise the Tube is not present, and we can't run
\ the game, so call ZZZAP to blank out memory
BRK \ Execute a BRK instruction to display the following
\ system error, and stop everything
EQUB 0 \ Error number
IF _SNG45 OR _EXECUTIVE
EQUB &0A \ Print a line feed
EQUB &16, &07 \ VDU 22, 7 (change to mode 7)
EQUS "This program needs a 6502 2nd Processor"
ELIF _SOURCE_DISC
EQUS "This program needs a 6502 Second Processor"
ENDIF
EQUB 10 \ Line feed and carriage return
EQUB 13
BRK
.ZZZAP
\ The following blanks out memory, so crackers can't
\ just unplug their Tube, run the game, get an error
\ message and then poke around in memory to discover
\ the loader's secrets
LDA #LO(happy) \ Set the low byte of ZP(1 0) to the low byte of happy
STA ZP
LDX #HI(happy) \ Set X to the high byte of happy, to act as a page
\ counter
LDY #0 \ Set Y = 0 to act as a byte counter
.ZZZAPL
STX ZP+1 \ Set the high byte of ZP(1 0) to X, so ZP(1 0) starts
\ by pointing to the location happy, and will increase
\ by a page every time we increment X
STA (ZP),Y \ Store A (we don't care what it contains) in the Y-th
\ byte of the block pointed to be ZP(1 0)
INY \ Increment the byte counter
BNE ZZZAPL \ Loop back until we reach a page boundary
INX \ Increment the page counter to point to the next page
CPX #(HI(MESS2)+1) \ Loop back until we have filled up to the end of the
BNE ZZZAPL \ page containing MESS2, which is at the end of the
\ loader code
RTS \ Return from the subroutine
.happy
\ If we get here, then one of the following is true:
\
\ * This is a BBC Micro Model B with MOS 0.1
\ (X = Y = 0)
\
\ * This is not a BBC Micro with MOS 1.20
\ (X <> &FF and Y <> &FF)
\
\ * This is a BBC Micro with MOS 1.20 and the Tube
\ (X = Y = &FF and Tube hardware is detected)
\
\ The odd thing is that the game only works on the last
\ system, so you would think that the first two would
\ give an error... but instead, we try to run the game
\ and fail, which is all a bit strange
\
\ That's what you get for using undocumented calls...
LDA #16 \ Call OSBYTE with A = 16 and X = 3 to set the ADC to
LDX #3 \ sample 3 channels from the joystick/Bitstik
JSR OSBYTE
LDA #190 \ Call OSBYTE with A = 190, X = 8 and Y = 0 to set the
LDX #8 \ ADC conversion type to 8 bits, for the joystick
JSR OSB
LDA #200 \ Call OSBYTE with A = 200, X = 3 and Y = 0 to disable
LDX #3 \ the ESCAPE key and clear memory if the BREAK key is
JSR OSB \ pressed
\LDA #144 \ These instructions are commented out in the original
\LDX #255 \ source, but they would call OSBYTE with A = 144,
\JSR OSB \ X = 255 and Y = 0 to move the screen down one line and
\ turn screen interlace on
LDA #225 \ Call OSBYTE with A = 225, X = 128 and Y = 0 to set
LDX #128 \ the function keys to return ASCII codes for SHIFT-fn
JSR OSB \ keys (i.e. add 128)
LDA #13 \ Call OSBYTE with A = 13, X = 2 and Y = 0 to disable
LDX #2 \ the "character entering buffer" event
JSR OSB
LDA #LO(B%) \ Set ZP(1 0) to point to the VDU code table at B%
STA ZP
LDA #HI(B%)
STA ZP+1
LDY #0 \ We are now going to send the N% VDU bytes in the table
\ at B% to OSWRCH to set up the special mode 1 screen
\ that forms the basis for the split-screen mode
.LOOP
LDA (ZP),Y \ Pass the Y-th byte of the B% table to OSWRCH
JSR OSWRCH
INY \ Increment the loop counter
CPY #N% \ Loop back for the next byte until we have done them
BNE LOOP \ all (the number of bytes was set in N% above)
LDA #20 \ Call OSBYTE with A = 20, X = 0 and Y = 0 to implode
LDX #0 \ the soft character definitions, so they don't take up
JSR OSB \ extra memory (by default, having a Second Processor
\ present explodes the soft character definitions, so
\ this reclaims 6 pages of memory)
LDA #4 \ Call OSBYTE with A = 4, X = 1 and Y = 0 to disable
LDX #1 \ cursor editing, so the cursor keys return ASCII values
JSR OSB \ and can therefore be used in-game
LDA #9 \ Call OSBYTE with A = 9, X = 0 and Y = 0 to disable
LDX #0 \ flashing colours
JSR OSB
JSR PLL1 \ Call PLL1 to draw Saturn
FNE 0 \ Set up sound envelopes 0-3 using the FNE macro
FNE 1
FNE 2
FNE 3
LDX #LO(MESS1) \ Set (Y X) to point to MESS1 ("DIR E")
LDY #HI(MESS1)
JSR OSCLI \ Call OSCLI to run the OS command in MESS1, which
\ changes the disc directory to E
LDX #LO(MESS2) \ Set (Y X) to point to MESS2 ("R.I.ELITEa")
LDY #HI(MESS2)
JMP OSCLI \ Call OSCLI to run the OS command in MESS2, which *RUNs
\ the second loader in I.ELITEa, returning from the
\ subroutine using a tail call
\ ******************************************************************************
\
\ Name: PLL1 (Part 1 of 3)
\ Type: Subroutine
\ Category: Drawing planets
\ Summary: Draw Saturn on the loading screen (draw the planet)
\ Deep dive: Drawing Saturn on the loading screen
\
\ ******************************************************************************
.PLL1
\ The following loop iterates CNT(1 0) times, i.e. &300
\ or 768 times, and draws the planet part of the
\ loading screen's Saturn
LDA VIA+&44 \ Read the 6522 System VIA T1C-L timer 1 low-order
STA RAND+1 \ counter (SHEILA &44), which decrements one million
\ times a second and will therefore be pretty random,
\ and store it in location RAND+1, which is among the
\ main game code's random seeds in RAND (so this seeds
\ the random number generator)
JSR DORND \ Set A and X to random numbers, say A = r1
JSR SQUA2 \ Set (A P) = A * A
\ = r1^2
STA ZP+1 \ Set ZP(1 0) = (A P)
LDA P \ = r1^2
STA ZP
JSR DORND \ Set A and X to random numbers, say A = r2
STA YY \ Set YY = A
\ = r2
JSR SQUA2 \ Set (A P) = A * A
\ = r2^2
TAX \ Set (X P) = (A P)
\ = r2^2
LDA P \ Set (A ZP) = (X P) + ZP(1 0)
ADC ZP \
STA ZP \ first adding the low bytes
TXA \ And then adding the high bytes
ADC ZP+1
BCS PLC1 \ If the addition overflowed, jump down to PLC1 to skip
\ to the next pixel
STA ZP+1 \ Set ZP(1 0) = (A ZP)
\ = r1^2 + r2^2
LDA #1 \ Set ZP(1 0) = &4001 - ZP(1 0) - (1 - C)
SBC ZP \ = 128^2 - ZP(1 0)
STA ZP \
\ (as the C flag is clear), first subtracting the low
\ bytes
LDA #&40 \ And then subtracting the high bytes
SBC ZP+1
STA ZP+1
BCC PLC1 \ If the subtraction underflowed, jump down to PLC1 to
\ skip to the next pixel
\ If we get here, then both calculations fitted into
\ 16 bits, and we have:
\
\ ZP(1 0) = 128^2 - (r1^2 + r2^2)
\
\ where ZP(1 0) >= 0
JSR ROOT \ Set ZP = SQRT(ZP(1 0))
LDA ZP \ Set X = ZP >> 1
LSR A \ = SQRT(128^2 - (a^2 + b^2)) / 2
TAX
LDA YY \ Set A = YY
\ = r2
CMP #128 \ If YY >= 128, set the C flag (so the C flag is now set
\ to bit 7 of A)
ROR A \ Rotate A and set the sign bit to the C flag, so bits
\ 6 and 7 are now the same, i.e. A is a random number in
\ one of these ranges:
\
\ %00000000 - %00111111 = 0 to 63 (r2 = 0 - 127)
\ %11000000 - %11111111 = 192 to 255 (r2 = 128 - 255)
\
\ The PIX routine flips bit 7 of A before drawing, and
\ that makes -A in these ranges:
\
\ %10000000 - %10111111 = 128-191
\ %01000000 - %01111111 = 64-127
\
\ so that's in the range 64 to 191
JSR PIX \ Draw a pixel at screen coordinate (X, -A), i.e. at
\
\ (ZP / 2, -A)
\
\ where ZP = SQRT(128^2 - (r1^2 + r2^2))
\
\ So this is the same as plotting at (x, y) where:
\
\ r1 = random number from 0 to 255
\ r2 = random number from 0 to 255
\ (r1^2 + r2^2) < 128^2
\
\ y = r2, squished into 64 to 191 by negation
\
\ x = SQRT(128^2 - (r1^2 + r2^2)) / 2
\
\ which is what we want
.PLC1
DEC CNT \ Decrement the counter in CNT (the low byte)
BNE PLL1 \ Loop back to PLL1 until CNT = 0
DEC CNT+1 \ Decrement the counter in CNT+1 (the high byte)
BNE PLL1 \ Loop back to PLL1 until CNT+1 = 0
\ ******************************************************************************
\
\ Name: PLL1 (Part 2 of 3)
\ Type: Subroutine
\ Category: Drawing planets
\ Summary: Draw Saturn on the loading screen (draw the stars)
\ Deep dive: Drawing Saturn on the loading screen
\
\ ******************************************************************************
\ The following loop iterates CNT2(1 0) times, i.e. &1DD
\ or 477 times, and draws the background stars on the
\ loading screen
.PLL2
JSR DORND \ Set A and X to random numbers, say A = r3
TAX \ Set X = A
\ = r3
JSR SQUA2 \ Set (A P) = A * A
\ = r3^2
STA ZP+1 \ Set ZP+1 = A
\ = r3^2 / 256
JSR DORND \ Set A and X to random numbers, say A = r4
STA YY \ Set YY = r4
JSR SQUA2 \ Set (A P) = A * A
\ = r4^2
ADC ZP+1 \ Set A = A + r3^2 / 256
\ = r4^2 / 256 + r3^2 / 256
\ = (r3^2 + r4^2) / 256
CMP #&11 \ If A < 17, jump down to PLC2 to skip to the next pixel
BCC PLC2
LDA YY \ Set A = r4
JSR PIX \ Draw a pixel at screen coordinate (X, -A), i.e. at
\ (r3, -r4), where (r3^2 + r4^2) / 256 >= 17
\
\ Negating a random number from 0 to 255 still gives a
\ random number from 0 to 255, so this is the same as
\ plotting at (x, y) where:
\
\ x = random number from 0 to 255
\ y = random number from 0 to 255
\ HI(x^2 + y^2) >= 17
\
\ which is what we want
.PLC2
DEC CNT2 \ Decrement the counter in CNT2 (the low byte)
BNE PLL2 \ Loop back to PLL2 until CNT2 = 0
DEC CNT2+1 \ Decrement the counter in CNT2+1 (the high byte)
BNE PLL2 \ Loop back to PLL2 until CNT2+1 = 0
\ ******************************************************************************
\
\ Name: PLL1 (Part 3 of 3)
\ Type: Subroutine
\ Category: Drawing planets
\ Summary: Draw Saturn on the loading screen (draw the rings)
\ Deep dive: Drawing Saturn on the loading screen
\
\ ******************************************************************************
\ The following loop iterates CNT3(1 0) times, i.e. &333
\ or 819 times, and draws the rings around the loading
\ screen's Saturn
.PLL3
JSR DORND \ Set A and X to random numbers, say A = r5
STA ZP \ Set ZP = r5
JSR SQUA2 \ Set (A P) = A * A
\ = r5^2
STA ZP+1 \ Set ZP+1 = A
\ = r5^2 / 256
JSR DORND \ Set A and X to random numbers, say A = r6
STA YY \ Set YY = r6
JSR SQUA2 \ Set (A P) = A * A
\ = r6^2
STA T \ Set T = A
\ = r6^2 / 256
ADC ZP+1 \ Set ZP+1 = A + r5^2 / 256
STA ZP+1 \ = r6^2 / 256 + r5^2 / 256
\ = (r5^2 + r6^2) / 256
LDA ZP \ Set A = ZP
\ = r5
CMP #128 \ If A >= 128, set the C flag (so the C flag is now set
\ to bit 7 of ZP, i.e. bit 7 of A)
ROR A \ Rotate A and set the sign bit to the C flag, so bits
\ 6 and 7 are now the same
CMP #128 \ If A >= 128, set the C flag (so again, the C flag is
\ set to bit 7 of A)
ROR A \ Rotate A and set the sign bit to the C flag, so bits
\ 5-7 are now the same, i.e. A is a random number in one
\ of these ranges:
\
\ %00000000 - %00011111 = 0-31
\ %11100000 - %11111111 = 224-255
\
\ In terms of signed 8-bit integers, this is a random
\ number from -32 to 31. Let's call it r7
ADC YY \ Set A = A + YY
\ = r7 + r6
TAX \ Set X = A
\ = r6 + r7
JSR SQUA2 \ Set (A P) = A * A
\ = (r6 + r7)^2
TAY \ Set Y = A
\ = (r6 + r7)^2 / 256
ADC ZP+1 \ Set A = A + ZP+1
\ = (r6 + r7)^2 / 256 + (r5^2 + r6^2) / 256
\ = ((r6 + r7)^2 + r5^2 + r6^2) / 256
BCS PLC3 \ If the addition overflowed, jump down to PLC3 to skip
\ to the next pixel
CMP #80 \ If A >= 80, jump down to PLC3 to skip to the next
BCS PLC3 \ pixel
CMP #32 \ If A < 32, jump down to PLC3 to skip to the next pixel
BCC PLC3
TYA \ Set A = Y + T
ADC T \ = (r6 + r7)^2 / 256 + r6^2 / 256
\ = ((r6 + r7)^2 + r6^2) / 256
CMP #16 \ If A >= 16, skip to PL1 to plot the pixel
BCS PL1
LDA ZP \ If ZP is positive (i.e. r5 < 128), jump down to PLC3
BPL PLC3 \ to skip to the next pixel
.PL1
\ If we get here then the following is true:
\
\ 32 <= ((r6 + r7)^2 + r5^2 + r6^2) / 256 < 80
\
\ and either this is true:
\
\ ((r6 + r7)^2 + r6^2) / 256 >= 16
\
\ or both these are true:
\
\ ((r6 + r7)^2 + r6^2) / 256 < 16
\ r5 >= 128
LDA YY \ Set A = YY
\ = r6
JSR PIX \ Draw a pixel at screen coordinate (X, -A), where:
\
\ X = (random -32 to 31) + r6
\ A = r6
\
\ Negating a random number from 0 to 255 still gives a
\ random number from 0 to 255, so this is the same as
\ plotting at (x, y) where:
\
\ r5 = random number from 0 to 255
\ r6 = random number from 0 to 255
\ r7 = r5, squashed into -32 to 31
\
\ x = r6 + r7
\ y = r6
\
\ 32 <= ((r6 + r7)^2 + r5^2 + r6^2) / 256 < 80
\
\ Either: ((r6 + r7)^2 + r6^2) / 256 >= 16
\
\ Or: ((r6 + r7)^2 + r6^2) / 256 < 16
\ r5 >= 128
\
\ which is what we want
.PLC3
DEC CNT3 \ Decrement the counter in CNT3 (the low byte)
BNE PLL3 \ Loop back to PLL3 until CNT3 = 0
DEC CNT3+1 \ Decrement the counter in CNT3+1 (the high byte)
BNE PLL3 \ Loop back to PLL3 until CNT3+1 = 0
\ ******************************************************************************
\
\ Name: DORND
\ Type: Subroutine
\ Category: Maths (Arithmetic)
\ Summary: Generate random numbers
\ Deep dive: Generating random numbers
\ Fixing ship positions
\
\ ------------------------------------------------------------------------------
\
\ Set A and X to random numbers (though note that X is set to the random number
\ that was returned in A the last time DORND was called).
\
\ The C and V flags are also set randomly.
\
\ This is a simplified version of the DORND routine in the main game code. It
\ swaps the two calculations around and omits the ROL A instruction, but is
\ otherwise very similar. See the DORND routine in the main game code for more
\ details.
\
\ ******************************************************************************
.DORND
LDA RAND+1 \ r1´ = r1 + r3 + C
TAX \ r3´ = r1
ADC RAND+3
STA RAND+1
STX RAND+3
LDA RAND \ X = r2´ = r0
TAX \ A = r0´ = r0 + r2
ADC RAND+2
STA RAND
STX RAND+2
RTS \ Return from the subroutine
\ ******************************************************************************
\
\ Name: RAND
\ Type: Variable
\ Category: Drawing planets
\ Summary: The random number seed used for drawing Saturn
\
\ ******************************************************************************
.RAND
EQUD &34785349
\ ******************************************************************************
\
\ Name: SQUA2
\ Type: Subroutine
\ Category: Maths (Arithmetic)
\ Summary: Calculate (A P) = A * A
\ Deep dive: Shift-and-add multiplication
\
\ ------------------------------------------------------------------------------
\
\ Do the following multiplication of signed 8-bit numbers: