-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGalway_Martin_Rambo_Loader.asm
2967 lines (2475 loc) · 38.1 KB
/
Galway_Martin_Rambo_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
; Rambo II Loader
; By Martin Galway
; Reverse engineered by dmx87
!to "Galway_Martin_Rambo_Loader.sid",plain
* = $0000
!text "PSID"
!be16 2 ; version 2
!be16 $7c ; data offset
!be16 $6f00 ; Load (0 = auto)
!be16 Init ; Init
!be16 Play ; Play
!be16 1 ; num songs
!be16 1 ; first song
!word 0
!word 0
- !text "Rambo II Loader"
!fill 32 - (* - -)
- !text "Martin Galway"
!fill 32 - (* - -)
- !text "1986 Ocean"
!fill 32 - (* - -)
!word 0 ; v2 flags
!word 0 ; Start page, page length (reloc)
!word 0 ; Reserved
!pseudopc $6f00 {
* = $6f00
SIDV1FREQLO = $d400
SIDV1FREQHI = $d401
SIDV1PWLO = $d402
SIDV1PWHI = $d403
SIDV1CTRL = $d404
SIDV1AD = $d405
SIDV1SR = $d406
SIDV2FREQLO = $d407
SIDV2FREQHI = $d408
SIDV2PWLO = $d409
SIDV2PWHI = $d40a
SIDV2CTRL = $d40b
SIDV2AD = $d40c
SIDV2SR = $d40d
SIDV3FREQLO = $d40e
SIDV3FREQHI = $d40f
SIDV3PWLO = $d410
SIDV3PWHI = $d411
SIDV3CTRL = $d412
SIDV3AD = $d413
SIDV3SR = $d414
; * = $2000
V1PAT = $10 ; track pointers
V1PATH = $11
V2PAT = $12
V2PATH = $13
V3PAT = $14
V3PATH = $15
PARAM1 = $16 ; opcode parameter 1
PARAM2 = $17 ; opcode parameter 2
CVAL = $18 ; current dur/length/note
VEN = $19 ; voice enabled bit set 0-2
V1CNT = $1a ; note counters
V2CNT = $1b
V3CNT = $1c
V1SP = $1d ; stack pointers
V2SP = $1e
V3SP = $1f
Play
playl = * + 1
playh = * + 2
jmp playmusic
Init
jmp initmusic
jmp L_25FA
v1_pmretrig
ldx v1_pmlo
ldy v1_pmhi
stx v1_pwl
sty v1_pwh
v1_pmcont
lda v1_p1time
sta v1_p1c
lda v1_p2time
sta v1_p2c
rts
v2_pmretrig
ldx v2_pmlo
ldy v2_pmhi
stx v2_pwl
sty v2_pwh
v2_pmcont
lda v2_p1time
sta v2_p1c
lda v2_p2time
sta v2_p2c
rts
v3_pmretrig
ldx v3_pmlo
ldy v3_pmhi
stx v3_pwl
sty v3_pwh
v3_pmcont
lda v3_p1time
sta v3_p1c
lda v3_p2time
sta v3_p2c
rts
v1_freqreset
lda v1_fq4time
sta v1_f4c
lda v1_fq3time
sta v1_f3c
lda v1_fq2time
sta v1_f2c
lda v1_fq1time
sta v1_f1c
rts
v2_freqreset
lda v2_fq4time
sta v2_f4c
lda v2_fq3time
sta v2_f3c
lda v2_fq2time
sta v2_f2c
lda v2_fq1time
sta v2_f1c
rts
v3_freqreset
lda v3_fq4time
sta v3_f4c
lda v3_fq3time
sta v3_f3c
lda v3_fq2time
sta v3_f2c
lda v3_fq1time
sta v3_f1c
rts
; ============================================================================
;
; VOICE 1
;
; ============================================================================
!zn voice1
dovoice1
lda VEN ; Is voice active
lsr
bcc +
dec V1CNT
beq .step
+ jmp .sound
.execreturn = * - 1 ; EXEC returns here
.skip3
lda #3 ; skip 3 bytes
.next
clc
adc V1PAT
sta V1PAT
bcc .step
inc V1PATH
.step
ldy #0
lda (V1PAT),y
cmp #$c0
bcc .other
; >= $c0 opcode
and #$3f
tax
lda v1ops,x
sta .opl
lda v1opsh,x
sta .oph
iny
lda (V1PAT),y
tax
sta PARAM1 ; param 1
iny
lda (V1PAT),y
sta PARAM2 ; param 2
.opl = * + 1
.oph = * + 2
jmp $0000 ; exec op
.noop
jmp .duration
; <$c0 = note or duration
.other ; 20de
sta CVAL
cmp #$60
bcc +
sbc #$60
+ cmp #$5f ; 5f/bf = noop
beq .noop
adc v1_ntrans ; transpose
tax ; x = note
ldy #4
- lda #0
sta $d402,y ; clear pwlo/hi/ctrl/ad/sr
lda v1_cipw,y
sta $d402,y ; load pwlo/hi/ctrl/ad/sr
dey
bpl -
lda v1_cictrl
sta v1_ctrl
ldy freqsh,x
lda freqsl,x
sta v1_fql
sty v1_fqh
sta SIDV1FREQLO
sty SIDV1FREQHI
lda v1_cipmflags
sta v1_pmflags
beq +
ldy #9
- lda v1_cipm,y
sta v1_p1time,y
dey
bpl -
jsr v1_pmretrig
+ ldx v1_cifqflag
stx v1_fqflag
beq L_2153
; copy first 14 bytes of instrument
ldy #13
- lda v1_instr,y
sta v1_cinstr,y
dey
bpl -
txa
and #8
beq +
lda CVAL
clc
adc v1_ntrans
sta v1_fq3time
sty v1_fqdelay ; y = $ff
+
jsr v1_freqreset
L_2153
ldx v1_cirlsc
ldy v1_cihrtime
stx v1_rlsc
sty v1_hrtime
; duration byte
.duration
ldy #1
lda (V1PAT),y ; get next byte
ldx CVAL
cpx #$60 ; if note >= $60 then next is dur
bcs +
tax ; otherwise an index
lda v1_durtable,x
+ sta V1CNT
lda #2
clc
adc V1PAT
sta V1PAT
bcc +
inc V1PATH
+ jmp .sound
; C0 RET - Return to caller or end track
op_c0_v1
inc V1SP
ldy V1SP
cpy #8
beq .off
.sprestore
ldx v1_stackl,y
lda v1_stackh,y
jmp .setpos
.off
lda VEN
and #$fe
sta VEN ; disable this track
rts
; DA REPEAT,NN - Set repeat the following NN times
op_da_v1
ldx V1SP
clc
tya
adc V1PAT
sta v1_stackl,x
lda #0
adc V1PATH
sta v1_stackh,x
lda PARAM1
sta v1_repstack,x
dec V1SP
tya
jmp .next
; DC NEXT - Repeat
op_dc_v1
ldx V1SP
dec v1_repstack + 1,X
beq +
inx
txa
tay
bpl .sprestore
+ inc V1SP
lda #1
jmp .next
; C2 [addr] INSTR5 - Set 5 bytes in instrument
; Sets Ctrl, AD, SR, Release, HRTime
op_c2_v1
ldy #4
ldx #$1c
; y = count + 1
; x = instrument offset
.cpyxy
lda (PARAM1),y
sta v1_instr,x
dex
dey
bpl .cpyxy
jmp .skip3
; C6 [addr] - SETFQ - Copy 14 bytes (freq bytes)
op_c6_v1
ldy #13
ldx #$d
bne .cpyxy
; C8 [addr] - SETPM - Copy 10 bytes to $17 (pulse bytes)
op_c8_v1
ldy #9
ldx #$17
bne .cpyxy
; unused
op21df
ldy #$30
ldx #$30
bne .cpyxy
; D8 [addr] - ARP - Copy 10 bytes to freq area
op_d8_v1
ldy #9
ldx #9
bne .cpyxy
L_21EB
lda PARAM2
.setpos
stx V1PAT
sta V1PATH
jmp .step
; D4 [Hi,Lo] EXEC - Execute 6502 at hi/lo
op_d4_v1
lda #>.execreturn ;$20
pha
lda #<.execreturn ;$aa
pha
jmp (PARAM1)
; CA [HI, LO] - CALL - Call subtrack
op_ca_v1
lda #3
.skipa
ldy V1SP
clc
adc V1PAT
sta v1_stackl,y
lda #0
adc V1PAT + 1
sta v1_stackh,y
dec V1SP
jmp L_21EB
; CE [HI, LO, NT] - CALLT - Call with Note Transpose
op_ce_v1
iny
lda (V1PAT),y
sta v1_ntrans
lda #4 ; skip 4 bytes
bne .skipa
; CC [NT] - TRANS - Note transpose
op_cc_v1
stx v1_ntrans
tya
jmp .next
; D0 [OFF,VAL] - ISET - Set instrument byte
op_d0_v1
sta v1_instr,x
jmp .skip3
; D6 [OFF,VAL] - CISET - Set value in Current Instr.
op_d6_v1
sta v1_cinstr,x
jmp .skip3
.end
rts
; ============================================================================
;
; VOICE 1 SOUND UPDATE
;
; ============================================================================
.sound ; $2231
ldx v1_hrtime
beq .end
lda v1_ctrl
and #8 ; test bit?
beq +
lda V1CNT
cmp v1_rlsc
bcs .norls
lda #0
sta v1_rlsc
lda v1_ctrl
and #$f6 ; clear gate and test bit
sta v1_ctrl
bne .setctrl
+ lda v1_rlsc
bne .chkrls
dec v1_hrtime
bne .norls
; clear all regs
ldx #6
- sta $d400,x ; a = 0
dex
bpl -
rts
.chkrls
dec v1_rlsc
bne .norls
lda v1_ctrl
and #$f6 ; clear gate and test
.setctrl
sta SIDV1CTRL
; ========================================================================
; PULSE
; ========================================================================
.norls
lda v1_pmflags
beq .freq
lda v1_pmdelay
beq .dopulse
dec v1_pmdelay
jmp .freq
.dopulse
clc
ldx v1_pwl
ldy v1_pwh
; Pulse 1
lda v1_p1c
beq +
txa
adc v1_pm1addl
tax
tya
adc v1_pm1addh
tay
dec v1_p1c
jmp .loadpulse
; Pulse 2
+ lda v1_p2c
beq +
txa
adc v1_pm2addl
tax
tya
adc v1_pm2addh
tay
dec v1_p2c
jmp .loadpulse
+ lda v1_pmflags
and #$81
beq .loadpulse
bpl +
jsr v1_pmretrig ; retrig pulse
jmp .dopulse
+ jsr v1_pmcont ; continue without retrig
jmp .dopulse
.loadpulse
stx v1_pwl
sty v1_pwh
stx SIDV1PWLO
sty SIDV1PWHI
; ========================================================================
; FREQ
; ========================================================================
.freq
lda v1_fqflag
beq +
and #8 ; bit 3 = arpeggio?
bne .arp
ldx v1_fql
ldy v1_fqh
clc
lda v1_fqdelay
beq L_2313
dec v1_fqdelay
lda v1_fqflag
and #2 ; bit 1
bne L_2357
+ rts
; Arpeggio
.arp
ldx v1_fqdelay
bpl +
ldx v1_fq4time
+ lda v1_fq3time
clc
adc v1_fq1addl,x
dex
stx v1_fqdelay
tay
ldx freqsl,y
lda freqsh,y
jmp .loadfreq
; FREQ
; x = fql
; y = fqh
L_2312
clc
L_2313
; Freq 1 Adder
lda v1_f1c
beq +
dec v1_f1c
txa
adc v1_fq1addl
tax
tya
adc v1_fq1addh
jmp .loadfreq
; Freq 2 Adder
+ lda v1_f2c
beq +
dec v1_f2c
txa
adc v1_fq2addl
tax
tya
adc v1_fq2addh
jmp .loadfreq
; Freq 3 Adder
+ lda v1_f3c
beq +
dec v1_f3c
txa
adc v1_fq3addl
tax
tya
adc v1_fq3addh
jmp .loadfreq
; Freq 4 Adder
+ lda v1_f4c
beq +
dec v1_f4c
L_2357
txa
adc v1_fq4addl
tax
tya
adc v1_fq4addh
.loadfreq
tay
stx SIDV1FREQLO
sty SIDV1FREQHI
stx v1_fql
sty v1_fqh
rts
+ jsr v1_freqreset
jmp L_2312
; ============================================================================
;
; VOICE 2
;
; ============================================================================
!zn voice2
dovoice2
lda VEN
and #2
beq L_237E
dec V2CNT
beq .step
L_237E
jmp L_24E0
L_2381
lda #$03
L_2383
clc
adc V2PAT
sta V2PAT
bcc .step
inc $13
.step
ldy #0
lda (V2PAT),y
cmp #$c0
bcc L_23B4
and #$3f
tax
lda v2opsl,x
sta .opl
lda v2opsh,x
sta .oph
iny
lda (V2PAT),y
tax
sta $16
iny
lda (V2PAT),y
sta PARAM2
.opl = * + 1
.oph = * + 2
jmp $0000
.noop
jmp L_2424
L_23B4
sta CVAL
cmp #$60
bcc +
sbc #$60
+ cmp #$5f
beq .noop
adc v2_ntrans
tax
ldy #4
- lda #0
sta $d409,y
lda v2_cipw,y
sta $d409,y
dey
bpl -
lda v2_cictrl
sta v2_ctrl
ldy freqsh,x
lda freqsl,x
sta v2_fql
sty v2_fqh
sta SIDV2FREQLO
sty SIDV2FREQHI
lda v2_cipmflags
sta v2_pmflags
beq L_2402
ldy #$09
L_23F6
lda v2_cipm,y
sta v2_p1time,y
dey
bpl L_23F6
jsr v2_pmretrig
L_2402
ldx v2_cifqflag
stx v2_fqflags
beq L_2418
ldy #13
L_240C
lda v2_instr,y
sta v2_fq1addl,y
dey
bpl L_240C
jsr v2_freqreset
L_2418
ldx v2_cirlsc
ldy v2_cihrtime
stx v2_rlsc
sty v2_hrtime
L_2424
ldy #$01
lda (V2PAT),y
ldx CVAL
cpx #$60
bcs L_2432
tax
lda v2_durtable,x
L_2432
sta V2CNT
lda #2
clc
adc V2PAT
sta V2PAT
bcc L_243F
inc V2PATH
L_243F
jmp L_24E0
op_c0_v2
inc V2SP
ldy V2SP
cpy #8
beq L_2453
L_244A
ldx v2_stackl,y
lda v2_stackh,y
jmp L_24AC
L_2453
lda VEN
and #$fd
sta VEN
rts
op_da_v2
ldx V2SP
clc
tya
adc V2PAT
sta v2_stackl,x
lda V2PATH
adc #0
sta v2_stackh,x
lda $16
sta v2_repstack,x
dec V2SP
tya
jmp L_2383
op_dc_v2
ldx V2SP
dec v2_repstack + 1,x
beq L_2481
inx
txa
tay
bpl L_244A
L_2481
inc V2SP
lda #1
jmp L_2383
op_c2_v2
ldy #4
ldx #$1c
L_248c
lda ($16),y
sta v2_instr,x
dex
dey
bpl L_248c
jmp L_2381
op_c6_v2
ldy #$d
ldx #$d
bne L_248c
op_c8_v2
ldy #9
ldx #$17
bne L_248c
op_c4_v2
ldy #$1c
ldx #$1c
bne L_248c
L_24AA
lda PARAM2
L_24AC
stx V2PAT
sta V2PATH
jmp .step
op_ca_v2
lda #3
L_24B5
ldy V2SP
clc
adc V2PAT
sta v2_stackl,y
lda V2PATH
adc #0
sta v2_stackh,y
dec V2SP
jmp L_24AA
op_ce_v2
iny
lda (V2PAT),y
sta v2_ntrans
lda #4
bne L_24B5
op_d0_v2
sta v2_instr,x
jmp L_2381
op_d6_v2
sta v2_fq1addl,x
jmp L_2381
L_24DF
rts
L_24E0
ldx v2_hrtime
beq L_24DF
lda v2_ctrl
and #8
beq L_2502
lda V2CNT
cmp v2_rlsc
bcs L_2522
lda #0
sta v2_rlsc
lda v2_ctrl
and #$f6
sta v2_ctrl
bne L_251F
L_2502
lda v2_rlsc
bne L_2515
dec v2_hrtime
bne L_2522
ldx #6
- sta SIDV2FREQLO,x
dex
bpl -
rts
L_2515
dec v2_rlsc
bne L_2522
lda v2_ctrl
and #$f6
L_251F
sta SIDV2CTRL
L_2522
lda v2_pmflags
beq L_2584
lda v2_pmdelay
beq L_2532
dec v2_pmdelay
jmp L_2584
L_2532
clc
ldx v2_pwl
ldy v2_pwh
lda v2_p1c
beq L_254E
txa
adc v2_pm1addl