forked from kieranhj/elite-beebasm
-
Notifications
You must be signed in to change notification settings - Fork 42
/
elite-loader.asm
3362 lines (2622 loc) · 122 KB
/
elite-loader.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
\ ******************************************************************************
\
\ ELITE LOADER SOURCE
\
\ Elite was written by Ian Bell and David Braben and is copyright Acornsoft 1984
\
\ The code on this site 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 produces the following binary file:
\
\ * ELITE.unprot.bin
\
\ after reading in the following files:
\
\ * DIALS.bin
\ * P.ELITE.bin
\ * P.A-SOFT.bin
\ * P.(C)ASFT.bin
\ * WORDS9.bin
\ * PYTHON.bin
\
\ ******************************************************************************
INCLUDE "1-source-files/main-sources/elite-build-options.asm"
_SOURCE_DISC = (_VARIANT = 1)
_TEXT_SOURCES = (_VARIANT = 2)
_STH_CASSETTE = (_VARIANT = 3)
GUARD &6000 \ Guard against assembling over screen memory
\ ******************************************************************************
\
\ Configuration variables
\
\ ******************************************************************************
DISC = _DISC \ Set to TRUE to load the code above DFS and relocate
\ down, so we can load the cassette version from disc,
\ or set to FALSE to load the code as if it were from a
\ cassette
PROT = _PROT \ Set to TRUE to enable the tape protection code, or set
\ to FALSE to disable the code (for TRUE, the file must
\ be saved to tape with the block data corrupted, so you
\ probably want to leave this as FALSE)
IF DISC
CODE% = &1100 \ CODE% is set to the assembly address of the loader
\ code file that we assemble in this file ("ELITE"),
\ which is at the lowest DFS page value of &1100 for the
\ version that loads from disc
LOAD% = &1100 \ LOAD% is the load address of the main game code file
\ ("ELTcode" for loading from disc, "ELITEcode" for
\ loading from tape)
L% = &1128 \ L% points to the start of the actual game code from
\ elite-source.asm, after the &28 bytes of header code
\ that are inserted by elite-bcfs.asm
ELSE
CODE% = &0E00 \ CODE% is set to the assembly address of the loader
\ code file that we assemble in this file ("ELITE"),
\ which is at the standard &0E00 address for the version
\ that loads from cassette
LOAD% = &0F1F \ LOAD% is the load address of the main game code file
\ ("ELTcode" for loading from disc, "ELITEcode" for
\ loading from tape)
L% = &0F47 \ L% points to the start of the actual game code from
\ elite-source.asm, after the &28 bytes of header code
\ that are inserted by elite-bcfs.asm
ENDIF
LEN1 = 15 \ Size of the BEGIN% routine that gets pushed onto the
\ stack and executed there
LEN2 = 18 \ Size of the MVDL routine that gets pushed onto the
\ stack and executed there
LEN = LEN1 + LEN2 \ Total number of bytes that get pushed on the stack for
\ execution there (33)
VSCAN = 57-1 \ Defines the split position in the split-screen mode
LE% = &0B00 \ LE% is the address to which the code from UU% onwards
\ is copied in part 3. It contains:
\
\ * ENTRY2, the entry point for the second block of
\ loader code
\
\ * IRQ1, the interrupt routine for the split-screen
\ mode
\
\ * BLOCK, which by this point has already been put
\ on the stack by this point
\
\ * The variables used by the above
NETV = &0224 \ The NETV vector that we intercept as part of the copy
\ protection
IRQ1V = &0204 \ The IRQ1V vector that we intercept to implement the
\ split-screen mode
OSPRNT = &0234 \ The address for the OSPRNT vector
C% = &0F40 \ C% is set to the location that the main game code gets
\ moved to after it is loaded
S% = C% \ S% points to the entry point for the main game code
IF _SOURCE_DISC
D% = &563A \ D% is set to the address of the byte after the end of
\ the code, i.e. the byte after checksum0 at XX21
ELIF _TEXT_SOURCES
D% = &5638 \ D% is set to the address of the byte after the end of
\ the code, i.e. the byte after checksum0 at XX21
ELIF _STH_CASSETTE
D% = &563A \ D% is set to the address of the byte after the end of
\ the code, i.e. the byte after checksum0 at XX21
ENDIF
LC% = &6000 - C% \ LC% is set to the maximum size of the main game code
\ (as the code starts at C% and screen memory starts
\ at &6000)
N% = 67 \ N% is set to the number of bytes in the VDU table, so
\ we can loop through them in part 2 below
SVN = &7FFD \ SVN is where we store the "saving in progress" flag,
\ and it matches the location in elite-source.asm
VEC = &7FFE \ VEC is where we store the original value of the IRQ1
\ vector, and it matches the value in elite-source.asm
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
\ ******************************************************************************
\
\ Name: ZP
\ Type: Workspace
\ Address: &0004 to &0005 and &0070 to &0086
\ Category: Workspaces
\ Summary: Important variables used by the loader
\
\ ******************************************************************************
ORG &0004
.TRTB%
SKIP 2 \ Contains the address of the keyboard translation
\ table, which is used to translate internal key
\ numbers to ASCII
ORG &0070
.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
.SC
SKIP 1 \ Screen address (low byte)
\
\ Elite draws on-screen by poking bytes directly into
\ screen memory, and SC(1 0) is typically set to the
\ address of the character block containing the pixel
\ we want to draw (see the deep dives on "Drawing
\ monochrome pixels in mode 4" and "Drawing colour
\ pixels in mode 5" for more details)
.SCH
SKIP 1 \ Screen address (high byte)
.BLPTR
SKIP 2 \ Gets set to &03CA as part of the obfuscation code
.V219
SKIP 2 \ Gets set to &0218 as part of the obfuscation code
SKIP 4 \ These bytes appear to be unused
.K3
SKIP 1 \ Temporary storage, used in a number of places
.BLCNT
SKIP 2 \ Stores the tape loader block count as part of the copy
\ protection code in IRQ1
.BLN
SKIP 2 \ Gets set to &03C6 as part of the obfuscation code
.EXCN
SKIP 2 \ Gets set to &03C2 as part of the obfuscation code
\ ******************************************************************************
\
\ ELITE LOADER
\
\ ******************************************************************************
ORG CODE%
\ ******************************************************************************
\
\ Name: Elite loader (Part 1 of 6)
\ Type: Subroutine
\ Category: Loader
\ Summary: Include binaries for recursive tokens, Python blueprint and images
\
\ ------------------------------------------------------------------------------
\
\ The loader bundles a number of binary files in with the loader code, and moves
\ them to their correct memory locations in part 3 below.
\
\ There are two files containing code:
\
\ * WORDS9.bin contains the recursive token table, which is moved to &0400
\ before the main game is loaded
\
\ * PYTHON.bin contains the Python ship blueprint, which gets moved to &7F00
\ before the main game is loaded
\
\ and four files containing images, which are all moved into screen memory by
\ the loader:
\
\ * P.A-SOFT.bin contains the "ACORNSOFT" title across the top of the loading
\ screen, which gets moved to screen address &6100, on the second character
\ row of the monochrome mode 4 screen
\
\ * P.ELITE.bin contains the "ELITE" title across the top of the loading
\ screen, which gets moved to screen address &6300, on the fourth character
\ row of the monochrome mode 4 screen
\
\ * P.(C)ASFT.bin contains the "(C) Acornsoft 1984" title across the bottom
\ of the loading screen, which gets moved to screen address &7600, the
\ penultimate character row of the monochrome mode 4 screen, just above the
\ dashboard
\
\ * P.DIALS.bin contains the dashboard, which gets moved to screen address
\ &7800, which is the starting point of the four-colour mode 5 portion at
\ the bottom of the split screen
\
\ The routine ends with a jump to the start of the loader code at ENTRY.
\
\ ******************************************************************************
PRINT "WORDS9 = ", ~P%
INCBIN "3-assembled-output/WORDS9.bin"
ALIGN 256
PRINT "P.DIALS = ", ~P%
INCBIN "1-source-files/images/P.DIALS.bin"
PRINT "PYTHON = ", ~P%
INCBIN "3-assembled-output/PYTHON.bin"
PRINT "P.ELITE = ", ~P%
INCBIN "1-source-files/images/P.ELITE.bin"
PRINT "P.A-SOFT = ", ~P%
INCBIN "1-source-files/images/P.A-SOFT.bin"
PRINT "P.(C)ASFT = ", ~P%
INCBIN "1-source-files/images/P.(C)ASFT.bin"
.run
JMP ENTRY \ Jump to ENTRY to start the loading process
\ ******************************************************************************
\
\ Name: B%
\ Type: Variable
\ Category: Drawing the screen
\ Summary: VDU commands for setting the square mode 4 screen
\ Deep dive: The split-screen mode in BBC Micro Elite
\ Drawing monochrome pixels in mode 4
\
\ ------------------------------------------------------------------------------
\
\ 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 4 configuration;
\ the mode 5 part for the dashboard is implemented in the IRQ1 routine.
\
\ The top part of Elite's screen mode is based on mode 4 but with the following
\ differences:
\
\ * 32 columns, 31 rows (256 x 248 pixels) rather than 40, 32
\
\ * The horizontal sync position is at character 45 rather than 49, 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 &6000 to &7EFF, which leaves another whole page
\ for code (i.e. 256 bytes) after the end of the screen. This is where the
\ Python ship blueprint slots in
\
\ * The text window is 1 row high and 13 columns wide, and is at (2, 16)
\
\ * The cursor is disabled
\
\ This almost-square mode 4 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's one page of memory per row of pixels). For more details of the
\ screen mode, see the deep dive on "Drawing monochrome pixels in mode 4".
\
\ There is also an interrupt-driven routine that switches the bytes-per-pixel
\ setting from that of mode 4 to that of mode 5, when the raster reaches the
\ split between the space view and the dashboard. See the deep dive on "The
\ split-screen mode" for details.
\
\ ******************************************************************************
.B%
EQUB 22, 4 \ Switch to screen mode 4
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 4 and
\ 5, but we claw back the last row for storing code just
\ above the end of screen memory
EQUB 23, 0, 12, &0C \ Set 6845 register R12 = &0C and R13 = &00
EQUB 0, 0, 0 \
EQUB 0, 0, 0 \ This sets 6845 registers (R12 R13) = &0C00 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:
\
\ &0C00 * 8 = &6000
\
\ So this sets the start of screen memory to &6000
EQUB 23, 0, 1, 32 \ Set 6845 register R1 = 32
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 40 for
\ modes 4 and 5, but our custom screen is not as wide at
\ only 32 character blocks across
EQUB 23, 0, 2, 45 \ Set 6845 register R2 = 45
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 49 for modes 4 and 5, 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
\ ******************************************************************************
\
\ 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 Micro User Guide 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, 112, 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: swine
\ Type: Subroutine
\ Category: Copy protection
\ Summary: Resets the machine if the copy protection detects a problem
\
\ ******************************************************************************
.swine
LDA #%01111111 \ Set 6522 System VIA interrupt enable register IER
STA &FE4E \ (SHEILA &4E) bits 0-6 (i.e. disable all hardware
\ interrupts from the System VIA)
JMP (&FFFC) \ Jump to the address in &FFFC to reset the machine
\ ******************************************************************************
\
\ Name: OSB
\ Type: Subroutine
\ Category: Utility routines
\ Summary: A convenience routine for calling OSBYTE with Y = 0
\
\ ******************************************************************************
.OSB
LDY #0 \ Call OSBYTE with Y = 0, returning from the subroutine
JMP OSBYTE \ using a tail call (so we can call OSB to call OSBYTE
\ for when we know we want Y set to 0)
\ ******************************************************************************
\
\ Name: Authors' names
\ Type: Variable
\ Category: Copy protection
\ Summary: The authors' names, buried in the code
\
\ ------------------------------------------------------------------------------
\
\ Contains the authors' names, plus an unused OS command string that would
\ *RUN the main game code, which isn't what actually happens (so presumably
\ this is to throw the crackers off the scent).
\
\ ******************************************************************************
EQUS "R.ELITEcode" \ This is short for "*RUN ELITEcode"
EQUB 13
EQUS "By D.Braben/I.Bell"
EQUB 13
EQUB &B0
\ ******************************************************************************
\
\ Name: oscliv
\ Type: Variable
\ Category: Utility routines
\ Summary: Contains the address of OSCLIV, for executing OS commands
\
\ ******************************************************************************
.oscliv
EQUW &FFF7 \ Address of OSCLIV, for executing OS commands
\ (specifically the *LOAD that loads the main game code)
\ ******************************************************************************
\
\ Name: David9
\ Type: Variable
\ Category: Copy protection
\ Summary: Address used as part of the stack-based decryption loop
\
\ ------------------------------------------------------------------------------
\
\ This address is used in the decryption loop starting at David2 in part 4, and
\ is used to jump back into the loop at David5.
\
\ ******************************************************************************
.David9
EQUW David5 \ The address of David5
CLD \ This instruction is not used
\ ******************************************************************************
\
\ Name: David23
\ Type: Variable
\ Category: Copy protection
\ Summary: Address pointer to the start of the 6502 stack
\
\ ------------------------------------------------------------------------------
\
\ This two-byte address points to the start of the 6502 stack, which descends
\ from the end of page 2, less LEN bytes, which comes out as &01DF. So when we
\ push 33 bytes onto the stack (LEN being 33), this address will point to the
\ start of those bytes, which means we can push executable code onto the stack
\ and run it by calling this address with a JMP (David23) instruction. Sneaky
\ stuff!
\
\ ******************************************************************************
.David23
EQUW (512-LEN) \ The address of LEN bytes before the start of the stack
\ ******************************************************************************
\
\ Name: doPROT1
\ Type: Subroutine
\ Category: Copy protection
\ Summary: Routine to self-modify the loader code
\
\ ------------------------------------------------------------------------------
\
\ This routine modifies various bits of code in-place as part of the copy
\ protection mechanism. It is called with A = &48 and X = 255.
\
\ ******************************************************************************
.doPROT1
LDY #&DB \ Store &EFDB in TRTB%(1 0) to point to the keyboard
STY TRTB% \ translation table for OS 0.1 (which we will overwrite
LDY #&EF \ with a call to OSBYTE later)
STY TRTB%+1
LDY #2 \ Set the high byte of V219(1 0) to 2
STY V219+1
STA PROT1-255,X \ Poke &48 into PROT1, which changes the instruction
\ there to a PHA
LDY #&18 \ Set the low byte of V219(1 0) to &18 (as X = 255), so
STY V219+1,X \ V219(1 0) now contains &0218
RTS \ Return from the subroutine
\ ******************************************************************************
\
\ Name: MHCA
\ Type: Variable
\ Category: Copy protection
\ Summary: Used to set one of the vectors in the copy protection code
\
\ ------------------------------------------------------------------------------
\
\ This value is used to set the low byte of BLPTR(1 0), when it's set in PLL1
\ as part of the copy protection.
\
\ ******************************************************************************
.MHCA
EQUB &CA \ The low byte of BLPTR(1 0)
\ ******************************************************************************
\
\ Name: David7
\ Type: Subroutine
\ Category: Copy protection
\ Summary: Part of the multi-jump obfuscation code in PROT1
\
\ ------------------------------------------------------------------------------
\
\ This instruction is part of the multi-jump obfuscation in PROT1 (see part 2 of
\ the loader), which does the following jumps:
\
\ David8 -> FRED1 -> David7 -> Ian1 -> David3
\
\ ******************************************************************************
.David7
BCC Ian1 \ This instruction is part of the multi-jump obfuscation
\ in PROT1
\ ******************************************************************************
\
\ 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 (Part 2 of 6)
\ Type: Subroutine
\ Category: Loader
\ Summary: Perform a number of OS calls, set up sound, push routines on stack
\
\ ------------------------------------------------------------------------------
\
\ This part of the loader does a number of calls to OS routines, sets up the
\ sound envelopes, pushes 33 bytes onto the stack that will be used later, and
\ sends us on a wild goose chase, just for kicks.
\
\ ------------------------------------------------------------------------------
\
\ Other entry points:
\
\ Ian1 Re-entry point following the wild goose chase
\ obfuscation
\
\ ******************************************************************************
.ENTRY
SEI \ Disable all interrupts
CLD \ Clear the decimal flag, so we're not in decimal mode
IF NOT(DISC)
LDA #0 \ Call OSBYTE with A = 0 and X = 255 to fetch the
LDX #255 \ operating system version into X
JSR OSBYTE
TXA \ If X = 0 then this is OS 1.00, so jump down to OS100
BEQ OS100 \ to skip the following
LDY &FFB6 \ Otherwise this is OS 1.20, so set Y to the contents of
\ &FFB6, which contains the length of the default vector
\ table
LDA &FFB7 \ Set ZP(1 0) to the location stored in &FFB7-&FFB8,
STA ZP \ which contains the address of the default vector table
LDA &FFB8
STA ZP+1
DEY \ Decrement Y so we can use it as an index for setting
\ all the vectors to their default states
.ABCDEFG
LDA (ZP),Y \ Copy the Y-th byte from the default vector table into
STA &0200,Y \ the vector table in &0200
DEY \ Decrement the loop counter
BPL ABCDEFG \ Loop back for the next vector until we have done them
\ all
.OS100
ENDIF
LDA #%01111111 \ Set 6522 System VIA interrupt enable register IER
STA &FE4E \ (SHEILA &4E) bits 0-6 (i.e. disable all hardware
\ interrupts from the System VIA)
STA &FE6E \ Set 6522 User VIA interrupt enable register IER
\ (SHEILA &6E) bits 0-6 (i.e. disable all hardware
\ interrupts from the User VIA)
LDA &FFFC \ Fetch the low byte of the reset address in &FFFC,
\ which will reset the machine if called
STA &0200 \ Set the low bytes of USERV, BRKV, IRQ2V and EVENTV
STA &0202
STA &0206
STA &0220
LDA &FFFD \ Fetch the high byte of the reset address in &FFFD,
\ which will reset the machine if called
STA &0201 \ Set the high bytes of USERV, BRKV, IRQ2V and EVENTV
STA &0203
STA &0207
STA &0221
LDX #&2F-2 \ We now step through all the vectors from &0204 to
\ &022F and OR their high bytes with &C0, so they all
\ point into the MOS ROM space (which is from &C000 and
\ upwards), so we set a counter in X to count through
\ them
.purge
LDA &0202,X \ Set the high byte of the vector in &0202+X so it
ORA #&C0 \ points to the MOS ROM
STA &0202,X
DEX \ Increment the counter to point to the next high byte
DEX
BPL purge \ Loop back until we have done all the vectors
LDA #&60 \ Store an RTS instruction in location &0232
STA &0232
LDA #&2 \ Point the NETV vector to &0232, which we just filled
STA NETV+1 \ with an RTS
LDA #&32
STA NETV
LDA #&20 \ Set A to the op code for a JSR call with absolute
\ addressing
EQUB &2C \ Skip the next instruction by turning it into
\ &2C &D0 &66, or BIT &66D0, which does nothing apart
\ from affect the flags
.Ian1
BNE David3 \ This instruction is skipped if we came from above,
\ otherwise this is part of the multi-jump obfuscation
\ in PROT1
STA David2 \ Store &20 in location David2, which modifies the
\ instruction there (see David2 for details)
LSR A \ Set A = 16
LDX #3 \ Set the high bytes of BLPTR(1 0), BLN(1 0) and
STX BLPTR+1 \ EXCN(1 0) to &3. We will fill in the high bytes in
STX BLN+1 \ the PLL1 routine, and will then use these values in
STX EXCN+1 \ the IRQ1 handler
DEX \ Set X = 2
JSR OSBYTE \ Call OSBYTE with A = 16 and X = 2 to set the ADC to
\ sample 2 channels from the joystick
EQUB &2C \ Skip the next instruction by turning it into
\ &2C &D0 &A1, or BIT &A1D0, which does nothing apart
\ from affect the flags
.FRED1
BNE David7 \ This instruction is skipped if we came from above,
\ otherwise this is part of the multi-jump obfuscation
\ in PROT1
LDX #255 \ Call doPROT1 to change an instruction in the PROT1
LDA #&48 \ routine and set up another couple of variables
JSR doPROT1
LDA #144 \ Call OSBYTE with A = 144, X = 255 and Y = 0 to move
JSR OSB \ the screen down one line and turn screen interlace on
LDA #247 \ Call OSBYTE with A = 247 and X = Y = 0 to disable the
LDX #0 \ BREAK intercept code by poking 0 into the first value
JSR OSB
\LDA #129 \ These instructions are commented out in the original
\LDY #255 \ source, along with the comment "Damn 0.1", so
\LDX #1 \ presumably MOS version 0.1 was a bit of a pain to
\JSR OSBYTE \ support - which is probably why Elite doesn't bother
\TXA \ and only supports 1.0 and 1.2
\BPL OS01
\Damn 0.1
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
EQUB &2C \ Skip the next instruction by turning it into
\ &2C &D0 &E1, or BIT &E1D0, which does nothing apart
\ from affect the flags
.David8
BNE FRED1 \ This instruction is skipped if we came from above,
\ otherwise this is part of the multi-jump obfuscation
\ in PROT1
LDA #143 \ Call OSBYTE 143 to issue a paged ROM service call of
LDX #&C \ type &C with argument &FF, which is the "NMI claim"
LDY #&FF \ service call that asks the current user of the NMI
JSR OSBYTE \ space to clear it out
LDA #13 \ Set A = 13 for the next OSBYTE call
.abrk
LDX #0 \ Call OSBYTE with A = 13, X = 0 and Y = 0 to disable
JSR OSB \ the "output buffer empty" event
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 #172 \ Call OSBYTE 172 to read the address of the MOS
LDX #0 \ keyboard translation table into (Y X)
LDY #255
JSR OSBYTE
STX TRTB% \ Store the address of the keyboard translation table in
STY TRTB%+1 \ TRTB%(1 0)
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
IF PROT AND NOT(DISC)
CPX #3 \ If the previous value of X from the call to OSBYTE 200
BNE abrk+1 \ was not 3 (ESCAPE disabled, clear memory), jump to
\ abrk+1, which contains a BRK instruction which will
\ reset the computer (as we set BRKV to point to the
\ reset address above)
ENDIF
LDA #13 \ Call OSBYTE with A = 13, X = 2 and Y = 0 to disable
LDX #2 \ the "character entering keyboard buffer" event
JSR OSB
.OS01
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
INX \ Set X = 0, to use as a counter in the following loop
.David3
LDA BEGIN%,X \ This routine pushes 33 bytes from BEGIN% onto the
\ stack, so fetch the X-th byte from BEGIN%
.PROT1
INY \ This instruction gets changed to a PHA instruction by
\ the doPROT1 routine that's called above, so by the
\ time we get here, this instruction actually pushes the
\ X-th byte from BEGIN% onto the stack
INX \ Increment the loop counter
CPX #LEN \ If X < #LEN (which is 33), loop back for the next one.
BNE David8 \ This branch actually takes us on a wild goose chase
\ through the following locations, where each BNE is
\ prefaced by an EQUB &2C that disables the branch
\ instruction during the normal instruction flow:
\
\ David8 -> FRED1 -> David7 -> Ian1 -> David3
\
\ so in the end this just loops back to push the next
\ byte onto the stack, but in a really sneaky way
LDA #LO(B%) \ Set the low byte of ZP(1 0) to point to the VDU code
STA ZP \ table at B%
LDA #&C8 \ Poke &C8 into PROT1 to change the instruction that we
STA PROT1 \ modified back to an INY instruction, rather than a PHA
LDA #HI(B%) \ Set the high byte of ZP(1 0) to point to the VDU code
STA ZP+1 \ table at B%
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 4 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 #1 \ In doPROT1 above we set V219(1 0) = &0218, so this
TAX \ code sets the contents of &0219 (the high byte of
TAY \ BPUTV) to 1. We will see why this later, at the start
STA (V219),Y \ of part 4
LDA #4 \ Call OSBYTE with A = 4, X = 1 and Y = 0 to disable
JSR OSB \ cursor editing, so the cursor keys return ASCII values
\ 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
LDA #&6C \ Poke &6C into crunchit after EOR'ing it first (which
EOR crunchit \ has no effect as crunchit contains a BRK instruction
STA crunchit \ with opcode 0), to change crunchit to an indirect JMP
FNE 0 \ Set up sound envelopes 0-3 using the FNE macro
FNE 1
FNE 2
FNE 3
\ ******************************************************************************
\
\ Name: Elite loader (Part 3 of 6)
\ Type: Subroutine
\ Category: Loader
\ Summary: Move and decrypt recursive tokens, Python blueprint and images
\
\ ------------------------------------------------------------------------------
\
\ Move and decrypt the following memory blocks: