Sona 0.50 Source

Sona 0.50/src-z80/rst.z80

;****************************************************************************
; RST $08
;
; Actual implementation of the ReadByte macro (which just jumps here).
; See ReadByteFrom68k's description in `memory.z80` for details.
;----------------------------------------------------------------------------
; input c ..... current bank
; input hl .... current address
;----------------------------------------------------------------------------
; output b .... byte fetched
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; modifies .... a,bc,hl
;****************************************************************************

Rst08h:
    PollPcm
    call    ReadByteFrom68k             ; Fetch byte from ROM
    PollPcm
    ret                                 ; End of subroutine
    
    nop                                 ; Padding
    nop

;****************************************************************************
; RST $10
;
; Actual implementation of the ReadByte macro (which just jumps here).
; See ReadByteFrom68k's description in `memory.z80` for details.
;----------------------------------------------------------------------------
; input c ..... current bank
; input hl .... current address
;----------------------------------------------------------------------------
; output b .... byte fetched
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; modifies .... a,bc,hl
;****************************************************************************

Rst10h:
    PollPcm
    
ReadArgByteFunc: equ $+1
    call    ReadByteFrom68k             ; Call the fetching routine (the
                                        ; pointer may be replaced depending
                                        ; on whether we're reading from ROM
                                        ; or VM variables)
    
    PollPcm                             ; Poll PCM too since this is slow
    ret                                 ; Return back to caller
    
    nop                                 ; Padding
    nop

;****************************************************************************
; RST $18
;
; Actual implementation of the CheckOwner macro (which just jumps here).
; 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
;****************************************************************************

Rst18h:
    ld      a, b                        ; Get owner for this channel
    and     $0F                         ;
    ld      e, a                        ; $FF = any BGM stream
    ld      d, ChanOwner>>8             ; $00 = SFX stream #1
    ld      a, (de)                     ; $01 = SFX stream #2
                                        ; $02 = SFX stream #3
ThisOwner: equ $+1
    sub     0                           ; Check if the channel is owned by
    jr      z, Rst18h_Ok                ; the current stream or not and store
    ld      a, $FF                      ; the result in the MSB (0 = owned,
Rst18h_Ok:                              ; 1 = not owned)
    
    ld      r, a                        ; Store the result in the R register
                                        ; Only the MSB matters (the bottom 7
                                        ; bits are used as a refresh counter
                                        ; by the Z80 so we can't use those)
    
    ret                                 ; End of subroutine

;****************************************************************************
; RST $28
;
; Actual implementation of the ToAddrBank macro (which just jumps here).
; Converts a 68000 address to 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
;----------------------------------------------------------------------------
; modifies .... a,hl
;----------------------------------------------------------------------------
; notes: it stores the bank in A, not C as usual!
;****************************************************************************

Rst28h:
    scf                                 ; Set carry for the address MSB
    rl      h                           ; Push it into address and bit 15 out
    rla                                 ; Push bit 15 into bank
    rrc     h                           ; Rotate address back in place
    
    ret                                 ; We're done
    nop                                 ; Padding