Sona 0.50 Source

Sona 0.50/src-z80/macros.z80

;****************************************************************************
; ReadRomByte
; Fetches a byte from 68000 address space. SLOW!!
;----------------------------------------------------------------------------
; input c ..... current bank
; input hl .... current address
;----------------------------------------------------------------------------
; output b .... byte fetched
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; breaks ...... a,bc,hl
;****************************************************************************

ReadRomByte: macro
    rst     $08
    endm

;****************************************************************************
; ReadArgByte
; Fetches an operand byte. SLOW!!
;----------------------------------------------------------------------------
; input c ..... current bank
; input hl .... current address
;----------------------------------------------------------------------------
; output b .... byte fetched
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; breaks ...... a,bc,hl
;****************************************************************************

ReadArgByte: macro
    rst     $10
    endm

;****************************************************************************
; PollPcm
; Polls timer A and calls UpdatePcm to see if it's time to output another
; PCM sample. Needs to be called regularly to have stable PCM playback.
;----------------------------------------------------------------------------
; breaks ... a
;****************************************************************************

PollPcm:    macro
    rst     $30
    endm

;****************************************************************************
; CheckOwner
;
; Checks if the channel for the current stream opcode is owned by this
; stream or not and stores the result in the R register.
;----------------------------------------------------------------------------
; input b .... channel ID (bits 3-0)
;----------------------------------------------------------------------------
; output r ... MSB clear if owned by stream
;              MSB set if not owned by stream
;----------------------------------------------------------------------------
; modifies ... a,de,r
;****************************************************************************

CheckOwner: macro
    rst     $18
    endm

;****************************************************************************
; ToAddrBank
; Converts a 68000 address its respective Z80 address+bank.
;----------------------------------------------------------------------------
; input a ..... 68000 address bits 23-16
; input hl .... 68000 address bits 15-0
;----------------------------------------------------------------------------
; output a .... Z80 bank
; output hl ... Z80 address
;----------------------------------------------------------------------------
; breaks ...... a,hl
;----------------------------------------------------------------------------
; notes: it stores the bank in A, not C as usual!
;****************************************************************************

ToAddrBank: macro
    rst     $28
    endm

;****************************************************************************
; PadToPage
; Pads to the next 256 byte boundary.
;****************************************************************************

PadToPage:  macro
    ds      ($100-($&$FF))&$FF
    endm

;****************************************************************************
; JumpIfBgm where           jumps if we're processing a BGM
; JumpIfSfx where           jumps if we're processing a SFX
; JumpIfOwner where         jumps if we own this channel
; JumpIfNotOwner where      jumps if we don't own this channel
;
; Macros to do a conditional jump when the condition is met.
;----------------------------------------------------------------------------
; modifies ... a
;****************************************************************************

JumpIfBgm:  macro where
    ld      a, i
    jr      z, where
    endm

JumpIfSfx:  macro where
    ld      a, i
    jr      nz, where
    endm

JumpIfOwner: macro where
    ld      a, r
    jp      p, where
    endm

JumpIfNotOwner: macro where
    ld      a, r
    jp      m, where
    endm

;****************************************************************************
; CallIfBgm where           calls if we're processing a BGM
; CallIfSfx where           calls if we're processing a SFX
; CallIfOwner where         calls if we own this channel
; CallIfNotOwner where      calls if we don't own this channel
;
; Macros to do a conditional subroutine call when the condition is met.
;----------------------------------------------------------------------------
; modifies ... a
;****************************************************************************

CallIfBgm: macro where
    ld      a, i
    call    z, where
    endm

CallIfSfx: macro where
    ld      a, i
    call    nz, where
    endm

CallIfOwner: macro where
    ld      a, r
    rla
    call    nc, where
    endm

CallIfNotOwner: macro where
    ld      a, r
    rla
    call    c, where
    endm

;****************************************************************************
; RetIfBgm                  returns if we're processing a BGM
; RetIfSfx                  returns if we're processing a SFX
; RetIfOwner                returns if we own this channel
; RetIfNotOwner             returns if we don't own this channel
;
; Macros to return from a subroutine only if the specified condition is met.
;----------------------------------------------------------------------------
; modifies ... a
;****************************************************************************

RetIfBgm: macro
    ld      a, i
    ret     z
    endm

RetIfSfx: macro
    ld      a, i
    ret     nz
    endm

RetIfOwner: macro
    ld      a, r
    rla
    ret     nc
    endm

RetIfNotOwner: macro
    ld      a, r
    rla
    ret     c
    endm