Skip to content

Instantly share code, notes, and snippets.

@taylorza
Last active September 6, 2022 20:23
Show Gist options
  • Save taylorza/6ef0632fd9993b39f274893683b2e4c9 to your computer and use it in GitHub Desktop.
Save taylorza/6ef0632fd9993b39f274893683b2e4c9 to your computer and use it in GitHub Desktop.
SLDOPT COMMENT WPMEM, LOGPOINT, ASSERTION
DEVICE ZXSPECTRUMNEXT
CSPECTMAP "demo.map"
org $8000
macro farcall fn
ld a, $$fn
call farstub
dw fn
endm
main:
farcall far_routine
ld hl, .msg
call print
jr $
.msg db "Back at the start\r", 0
farstub:
pop hl ; Return address points to the function to call
ld e, (hl) ; Load function address into DE
inc hl
ld d, (hl)
inc hl
push hl ; Update return address to return after the function pointer
ex de, hl ; HL = Address of far function
; Save current bank
ld bc, 0x243B ; Next register select
ld d, $56 ; Bank 6 register
out (c), d
inc b
in d, (c) ; Read current bank
push de ; Save current bank on the stack
out (c), a ; Switch to target bank
call callHL ; Call the function in the target bank
ex af, af' ; Swith to alternate registers, so we save the flags
pop af ; Get previous bank
nextreg $56, a ; Swith back to previous bank
ex af, af' ; Switch back to primary registers
ret
callHL:
jp (hl)
ret
; Print ASCIIZ in HL
print:
ld a, (hl)
or a
ret z
rst $10
inc hl
jr print
;------------------------------------------------------------------------------
; Stack reservation
STACK_SIZE equ 100
stack_bottom:
defs STACK_SIZE * 2
stack_top:
defw 0
;------------------------------------------------------------------------------
; Banked routines
; Bank 25
MMU 6, 25, $c000
far_routine:
ld hl, .msg
call print
farcall far_routine2
ret
.msg db "In far routine 1\r", 0
; Bank 32
MMU 6, 32, $c000
far_routine2:
ld hl, .msg
call print
ret
.msg db "In far routine 2\r", 0
;------------------------------------------------------------------------------
; Output configuration
SAVENEX OPEN "demo.nex", main, stack_top
SAVENEX CORE 2,0,0
SAVENEX CFG 7,0,0,0
SAVENEX AUTO
SAVENEX CLOSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment