; *****************************************************************************
; *****************************************************************************
;
;              Main system macros. (Target System Dependent)
;
; *****************************************************************************
; *****************************************************************************

read    macro                           ; read byte at bx into #1,destroys bx
        mov     #1,es:[bx]              ; read the byte anyway
        cmp     bh,0                    ; if bh >$80 its reading the ROM
        js      >M1                     ; so we've done it.
        cmp     bh,20h                  ; if its >= $20h its Control stuff.
        jae     >M2
        and     bh,07h                  ; we're reading the mirrored RAM
        mov     #1,NESRam[bx]           ; read the RAM
        jmp     short >M1
M2:     call    NESRead                 ; we're reading the control register
        mov     #1,[_Transfer]
M1:
#em

write   macro                           ; write byte in #1 into bx,destroys bx
        cmp     bh,020h                 ; is it writing to NES RAM ???
        jae     >M2                     ; no,go to next check
        and     bh,7                    ; writing the mirrored RAM
        mov     NESRam[bx],#1
        jmp     short >M1
M2:     mov     [_Transfer],#1
        call    NESWrite                ; writing to the control.
M1:
#em

CPUAddress macro                        ; bx address -> ES:BX real address
        cmp     bh,020h                 ; anything >2000h
        jae     >M1                     ; use it as it is.
        mov     es,ds                   ; else es = ds
        and     bh,7                    ; bx = bx & 07FF + NESRam
        add     bx,NESRam
M1:
#em

; *****************************************************************************
;
;       These macros can be optimised for the target system if needed
;
; *****************************************************************************

readz   macro                           ; read byte at 00<BL> into #1
        mov     bh,NESRam/256
        mov     #1,[bx]
#em

writez  macro                           ; write byte in #1 to 00<BL>
        mov     bh,NESRam/256
        mov     [bx],#1
#em

reads   macro                           ; read byte at 01<BL> into #1
        mov     bh,(NESRam/256)+1
        mov     #1,[bx]
#em

writes  macro                           ; write byte in #1 to 01<BL>
        mov     bh,(NESRam/256)+1
        mov     [bx],#1
#em

fetch   macro                           ; fetch byte from PC to #1
        mov     bx,bp                   ; and bump the PC
        read    #1
        inc     bp
#em

fetchaddr macro                         ; fetch word from PC to AX
        fetch   al
        fetch   ah
#em

indread macro                           ; read 00<bl> 00<bl+1> into BX
        push    bx                      ; (used by IX & IY)
        readz   al
        pop     bx
        inc     bl                      ; wrap around in page 0 here.
        readz   ah
        mov     bx,ax
#em

; *****************************************************************************
;
;                               Flag Bit macros
;
; *****************************************************************************

Setf    macro                           ; Set given flags in P
        or      ch,#1
#em

Clearf  macro                           ; Clear flags in P
        and     ch,255-(#1)
#em

SetZ    macro                           ; set the z byte register
        mov     dh,#1
#em

SetN    macro                           ; set the n byte register
        mov     dl,#1
#em

SetZN   macro                           ; set both
        setz    #1
        setn    #1
#em



