02-18-2017, 11:43 AM
Hi,
As a small challange I wanted to see if I could add music to games written with ADG by Jonathan Caudwell. ADG games usually take around 30k of memory starting from around 32000 so this leaves no room for music and playback.
My idea was that I would swap in bank 4 (@49152) and set up my music and then an IM routine at 25500. The IM routine swaps to bank 4, plays a frame of music and swaps the bank back to 0 and allows the game to continue. Here's the code I came up with (the music routine and data have been compressed with zx7 to take up less space)
An working example can be seen here with : http://torinak.com/qaop#128#l=http://zxbasic.uk/uploads/monty_tls.z80
As a small challange I wanted to see if I could add music to games written with ADG by Jonathan Caudwell. ADG games usually take around 30k of memory starting from around 32000 so this leaves no room for music and playback.
My idea was that I would swap in bank 4 (@49152) and set up my music and then an IM routine at 25500. The IM routine swaps to bank 4, plays a frame of music and swaps the bank back to 0 and allows the game to continue. Here's the code I came up with (the music routine and data have been compressed with zx7 to take up less space)
An working example can be seen here with : http://torinak.com/qaop#128#l=http://zxbasic.uk/uploads/monty_tls.z80
Code:
#include <memcopy.bas>
' REM IM music for ADG v1
' monty
border 0
paper 0
ink 6
cls
ASM
di ; disable intsd
LD A,(23388)
AND 248
OR 4 ; select bank 4
LD BC,32765
LD (23388),A
OUT (C),A
EI
END ASM
' the playsoutine expects the music to be at 51310, so lets copy the music there
zx7Unpack(@music, 51310)
' The play routine expects to be at 49152, so lets copy it there
zx7Unpack(@ayplay, 49152)
' This is routine that will be called every frame, lets copy it to its correct location
memcopy(@Ints, $6060, 60)
' we need to init our play routine, so call it
randomize usr 49152
randomize USR @IMStart
' Start ADG game
'randomize usr 32000
END
ayplay:
asm
incbin "vt49152.bin.zx7"
END asm
music:
asm
incbin "intro.pt3.zx7"
END asm
musicend:
Ints:
asm
di ; disable interrupts
push af ; save all std regs
push bc
push de
push hl
push ix
push iy
ex af, af' ; and shadow af
push af
; swap to bank 4
LD A,(23388) ; load a with previos port value
AND 248
OR 4 ; bank 4
LD BC,32765
LD (23388),A
OUT (C),A
call 49157 ; play the current tune
;swap back to bank 0
LD A,(23388)
AND 248
OR 0
LD BC,32765
LD (23388),A
OUT (C),A
pop af
ex af, af' ; restore af
pop iy
pop ix ; restore ix & iy
pop hl
pop de
pop bc
pop af ; restore all std regs
ei ; enable interrupts
jp 56
END asm
IMStart:
asm
DI
; swap to bank 4 before IM start
LD A,(23388)
AND 248
OR 4
LD BC,32765
LD (23388),A
OUT (C),A
; this code creates a 256 byte vector table at $FE00, then sets IM2
ld hl, $5e00
ld de, $5e01
ld bc, 256
ld a, h
ld i, a
ld a, $60
ld (hl), a
ldir
im 2
; now swap back to 0
LD A,(23388)
AND 248
OR 0
LD BC,32765
LD (23388),A
OUT (C),A
ei
ret
END asm
IMOff:
ASM
DI
IM 1
EI
RET
END ASM