-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcvbasic_epilogue.asm
More file actions
647 lines (609 loc) · 13.2 KB
/
cvbasic_epilogue.asm
File metadata and controls
647 lines (609 loc) · 13.2 KB
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
;
; CVBasic epilogue (BASIC compiler for Colecovision)
;
; by Oscar Toledo G.
; https://nanochess.org/
;
; Creation date: Feb/27/2024.
; Revision date: Feb/29/2024. Added joystick, keypad, frame, random, and
; read_pointer variables.
; Revision date: Mar/04/2024. Added music player.
; Revision date: Mar/05/2024. Added support for Sega SG1000.
; Revision date: Mar/12/2024. Added support for MSX.
; Revision date: Mar/13/2024. Added Pletter decompressor.
; Revision date: Mar/19/2024. Added support for sprite flicker.
; Revision date: Apr/11/2024. Added support for Super Game Module.
; Revision date: Apr/13/2024. Updates LFSR in interruption handler.
; Revision date: Apr/26/2024. All code moved to cvbasic_prologue.asm so it
; can remain accessible in bank 0 (bank switching).
; Revision date: Aug/02/2024. Added rom_end label for Memotech.
; Revision date: Aug/15/2024. Added support for Tatung Einstein.
; Revision date: Nov/12/2024. Added vdp_status.
; Revision date: Feb/03/2025. Round final ROM size to 8K multiples.
; Revision date: Feb/05/2026. Added support for spinners and roller controller
; (Colecovision).
;
rom_end:
; ROM final size rounding
if MSX+COLECO+SG1000+SMS+SVI+SORD
TIMES (($+$1FFF)&$1e000)-$ DB $ff
endif
if MEMOTECH+EINSTEIN+NABU
; Align following data to a 256-byte page.
TIMES $100-($&$ff) DB $4f
endif
if PV2000
TIMES $10000-$ DB $ff
endif
if SG1000+SMS
if CVBASIC_BANK_SWITCHING
forg CVBASIC_BANK_ROM_SIZE*1024-1 ; Force final ROM size
db $ff
endif
forg $7FF0
org $7FF0
db "TMR SEGA"
db 0,0
db 0,0 ; Checksum
db $11,$78 ; Product code
db $00 ; Version
db $4c ; SMS Export + 32KB for checksum
endif
if MSX
if FM_SUPPORT
;
; FM driver for MSX
;
; by Oscar Toledo G. (nanochess)
;
; Creation date: Sep/28/2012. For Mecha-9.
; Revision date: Mar/07/2026. Adapted for Metro Wars (CVBasic)
; Revision date: Mar/12/2026. Adapted for release in CVBasic.
;
FORG $01FD00
ORG $BD00
; Subroutines from BIOS MSX
CALSLT: equ $001c ; Inter-slot call (disables interruptions)
;ENASLT: equ $0024 ; Enable slot (H=High-byte address, C=Slot)
; Subroutines from OPLL ROM
WRTOPL: equ $4110
INIOPL: equ $4113
;
; Detects FM (OPLL)
; Carry = Set = FM detected.
; Clear = No FM detected.
;
msx_detect_fm:
ld bc,$0000
.1: push bc
ld hl,$fcc1 ; EXPTBL
ld a,l
add a,c
ld l,a
ld a,(hl)
and $80
or c
call msx_find_fm ; Search in subslots.
pop bc
ret nc ; Jump if something has been found.
inc c
bit 2,c ; Four slots analyzed?
jr z,.1 ; No, jump.
ret
msx_fm_signature_1: db "APRLOPLL" ; Internal MSX-Music.
msx_fm_signature_2: db "PAC2OPLL" ; FM-Pac cartridge.
msx_find_fm:
bit 7,a ; Expanded slot?
ld b,1 ; No subslots.
jr z,.1 ; No, jump.
and $f3
ld b,4 ; Ok, four subslots.
.1: ld c,a
.2: push bc
ld h,$40
ld a,c
call ENASLT
ld hl,$4018
push hl
ld de,msx_fm_signature_1
ld b,8
.4: ld a,(de)
cp (hl)
jr nz,.5
inc de
inc hl
djnz .4
.5: pop hl
jr z,.8 ; Detected? Jump to take note.
ld de,msx_fm_signature_2
ld b,8
.6: ld a,(de)
cp (hl)
jr nz,.7
inc de
inc hl
djnz .6
.7: jr nz,.3 ; Not detected? Jump to next subslot.
.8: pop bc
ld a,c ; Take note of slot.
ld (fm_slot),a
push bc
ld a,1 ; Enable FM.
ld (fm_enabled),a
.3:
; Important! The H register should be $40 here.
ld a,(cartridge_slot)
call ENASLT
pop bc
ld a,(fm_enabled)
or a
ret nz
ld a,c
add a,4
ld c,a
djnz .2
scf
ret
; Write a FM register
; A = register
; E = data
write_fm:
jp WRTOPL
;
; Map FM BIOS 16K in $4000-$7FFF
;
fm_rom_switch:
di
ld a,(fm_slot)
ld h,$40
jp ENASLT
;
; Map cartridge in $4000-$7FFF
;
cartridge_rom_switch:
ld a,(cartridge_slot)
ld h,$40
call ENASLT
ei
ret
endif
endif
if SMS
if FM_SUPPORT
;
; FM driver for Sega Master System.
;
; by Oscar Toledo G. (nanochess)
;
; Creation date: Sep/28/2012. For MSX.
; Revision date: Aug/16/2013. Adapted to Sega Master System.
; Revision date: Mar/12/2026. Adapted to CVBasic.
FORG $01FD00
ORG $7D00
;
; Detect FM (OPLL)
;
sms_detect_fm:
ld a,($c000)
or $04
out ($3e),a
ld bc,$0700
.1: ld a,b
and $01
out ($f2),a
ld e,a
in a,($f2)
and $07
cp e
jr nz,.2
inc c
.2: djnz .1
ld a,c
cp $07
jr z,.3
xor a
.3: and 1
out ($f2),a
ld (fm_enabled),a
dec a ; For compatibility.
ld (fm_slot),a
ld a,($c000)
out ($3e),a
ret
; Write a FM register
; A = register
; E = data
write_fm:
out ($f0),a
ld a,e
out ($f1),a
ex (sp),hl
ex (sp),hl
ret
fm_rom_switch:
di
ret
cartridge_rom_switch:
ei
ret
endif
endif
if MSX+SMS
if FM_SUPPORT
;
; This code cannot be in the area $4000-$7fff
;
;
; Play music for FM
;
music_generate_fm:
; Fragments of code in caller.
.0: ld a,(music_timing)
and $3f ; Restarts note time.
ld (music_note_counter),a
push hl
push bc
push de
call fm_rom_switch
pop de
pop bc
ld a,b
cp $3f ; Sustain?
jr z,.1
ld a,$10
call play_fm
.1: ld a,c ; Read second voice.
cp $3f ; Sustain?
jr z,.2
ld b,a
ld a,$11
call play_fm
.2: ld a,d ; Read third voice.
cp $3f ; Sustain?
jr z,.3
ld b,a
ld a,$12
call play_fm
.3: push de
call cartridge_rom_switch
pop de
pop hl
ld a,e ; Read effect.
ld (music_drum),a
xor a
ld (music_counter_4),a
inc hl
inc hl
inc hl
ld a,(music_timing)
and $c0
jr nz,.14
inc hl
.14: ld (music_pointer),hl
;
; Build extras
;
.6: ld a,(music_drum) ; Read effect.
or a ; Effect happening?
jr z,.7 ; No, jump.
call fm_rom_switch
ld a,(music_drum) ; Read effect.
dec a ; 1 - Long drum.
jr nz,.5
ld a,(music_counter_4)
or a
ld de,$0e28
jr z,.9
cp 3
ld de,$0e20
jr z,.9
jr .4
.5: dec a ; 2 - Short drum.
jr nz,.11
ld a,(music_counter_4)
or a
ld e,$21
jr z,.9
cp 3
ld e,$20
jr nz,.4
.9: ld a,$0e
call write_fm
jr .4
.12: cp 2
jr nz,.9
ld e,$20
ld a,$0e
call write_fm
ld e,$28
jr .9
.11: ;dec a ; 3 - Roll.
;jp nz,.4
ld a,(music_timing)
and $3e
rrca
ld b,a
ld a,(music_counter_4)
ld e,$28
or a
jr z,.9
cp b
jr z,.12
ld e,$20
cp 2
jr z,.9
dec a
dec a
cp b
jr z,.9
.4: call cartridge_rom_switch
.7:
; Increment time for drum.
ld hl,music_counter_4
inc (hl)
ld hl,music_note_counter
dec (hl)
ret
;
; Play note in FM
; A = Voice ($10 - $11 - $12)
; B = Instrument + Note.
;
play_fm:
push bc
push de
ld c,a ; Saves base register.
add a,$10
ld e,$00 ; Turn off voice if it was turned on.
push bc
call write_fm
pop bc
ld a,b ; Read instrument (bits 7-6)
and $c0
rlca
rlca
ld hl,fm_inst
add a,l
ld l,a
adc a,h
sub l
ld h,a
ld e,(hl) ; Select instrument.
ld a,c
add a,$20
push bc
call write_fm
pop bc
ld a,b ; Read note.
and $3f
ld hl,fm_notes
add a,a ; Index into note table.
add a,l
ld l,a
adc a,h
sub l
ld h,a
ld a,c
ld e,(hl) ; Get low byte.
push bc
push hl
call write_fm
pop hl
pop bc
inc hl
ld e,(hl) ; Get high byte (octave).
ld a,b
cpl
and $c0 ; Is it bass?
jr nz,.1 ; No, jump.
ld a,e
sub 2 ; Lower octave.
ld e,a
.1: ld a,b
and $c0
cp $40 ; Is it clarinet?
jr nz,$+4 ; No, jump.
inc e ; Higher octave.
inc e
ld a,c
add a,$10
call write_fm
pop de
pop bc
ret
;
; Init FM
;
init_fm:
push ix
push iy
call fm_rom_switch
if MSX
ld hl,$ef00
call INIOPL
endif
ld hl,.0
ld b,24
.1: ld e,(hl)
inc hl
ld a,(hl)
inc hl
call write_fm
djnz .1
call cartridge_rom_switch
ld a,$30 ; Piano
ld (fm_inst),a
ld a,$50 ; Clarinet
ld (fm_inst+1),a
ld a,$40 ; Flute
ld (fm_inst+2),a
ld a,$e0 ; Acoustic bass
ld (fm_inst+3),a
pop iy
pop ix
ret
.0: dw $0011 ; Unique programmable instrument (not used)
dw $0111
dw $0220
dw $0320
dw $04ff
dw $05b2
dw $06f4
dw $07f4
dw $0e20 ; Enables percussion mode.
dw $2000 ; Turn off main voices.
dw $2100
dw $2200
dw $2300
dw $2400
dw $2500
dw $1620 ; Set up percussion.
dw $1750
dw $18c0
dw $2605
dw $2705
dw $2801
dw $3600 ; / Bass Drum (volume for percussion)
dw $3720 ; Hihat / Snare Drum
dw $3800 ; Tom / Top Cymbal
;
; Turn off FM
;
turn_off_fm:
ld a,(fm_slot)
inc a
ret z
ld a,(fm_enabled)
or a
ret z
call fm_rom_switch
LD A,$10+$10
LD E,$00
CALL write_fm
LD A,$11+$10
LD E,$00
CALL write_fm
LD A,$12+$10
LD E,$00
CALL write_fm
CALL cartridge_rom_switch
RET
fm_notes:
; Silence - 0
dw 0
; Octave 2 - 1
dw $14ad,$14b7,$14c2,$14cd,$14d9,$14e6
dw $14f4,$1503,$1512,$1522,$1534,$1546
; Octave 3 - 13
dw $16ad,$16b7,$16c2,$16cd,$16d9,$16e6
dw $16f4,$1703,$1712,$1722,$1734,$1746
; Octave 4 - 25
dw $18ad,$18b7,$18c2,$18cd,$18d9,$18e6
dw $18f4,$1903,$1912,$1922,$1934,$1946
; Octave 5 - 37
dw $1aad,$1ab7,$1ac2,$1acd,$1ad9,$1ae6
dw $1af4,$1b03,$1b12,$1b22,$1b34,$1b46
; Octave 6 - 49
dw $1cad,$1cb7,$1cc2,$1ccd,$1cd9,$1ce6
dw $1cf4,$1d03,$1d12,$1d22,$1d34,$1d46
; Octave 7 - 61
; Space for two notes more.
endif
endif
if COLECO+SG1000+SMS+MSX+SVI+SORD+PV2000
org BASE_RAM
endif
ram_start:
sprites:
if SMS
rb 256
else
rb 128
endif
sprite_data:
rb 4
frame:
rb 2
read_pointer:
rb 2
cursor:
rb 2
lfsr:
rb 2
if MSX+SMS
cartridge_slot:
rb 1
fm_slot:
rb 1
fm_enabled:
rb 1
fm_inst:
rb 4
endif
mode:
rb 1 ; bit 0: NMI disabled.
; bit 1: NMI received.
; bit 2: No sprite flicker.
; bit 3: Single charset mode.
; bit 4: MSX2 sprites.
flicker:
rb 1
joy1_data:
rb 1
joy2_data:
rb 1
key1_data:
rb 1
key2_data:
rb 1
ntsc:
rb 1
vdp_status:
rb 1
if COLECO
if COLECO_SPINNER
spinner_data:
rb 3
endif
endif
if NABU
nabu_data0: rb 1
nabu_data1: rb 1
nabu_data2: rb 1
endif
if CVBASIC_MUSIC_PLAYER
music_tick: rb 1
music_mode: rb 1
if CVBASIC_BANK_SWITCHING
music_bank: rb 1
endif
music_start: rb 2
music_pointer: rb 2
music_playing: rb 1
music_timing: rb 1
music_note_counter: rb 1
music_instrument_1: rb 1
music_counter_1: rb 1
music_note_1: rb 1
music_instrument_2: rb 1
music_counter_2: rb 1
music_note_2: rb 1
music_instrument_3: rb 1
music_counter_3: rb 1
music_note_3: rb 1
music_counter_4: rb 1
music_drum: rb 1
audio_freq1: rb 2
audio_freq2: rb 2
audio_freq3: rb 2
audio_noise: rb 1
audio_mix: rb 1
audio_vol1: rb 1
audio_vol2: rb 1
audio_vol3: rb 1
audio_control: rb 1
audio_vol4hw: rb 1
endif
if SGM
org $2000 ; Start for variables.
endif