;Copyright (C) 1997-2004 ZSNES Team ( zsknight@zsnes.com / _demo_@zsnes.com )
;
;This program is free software; you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation; either
;version 2 of the License, or (at your option) any later
;version.
;
;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.









%macro FETCHPIPE 0
;    mov edx,[SfxPBR]
;    mov edx,[SfxMemTable+edx*4]
;    mov edx,[SfxCPB]
;    mov edx,[SfxR15]
    mov cl,[ebp]
%endmacro

%macro UpdateR14 0
;    mov edx,[SfxROMBR]
;    mov edx,[SfxMemTable+edx*4]
    mov eax,[SfxCROM]
;    and dword[SfxR14],0FFFFh
    add eax,[SfxR14]
    mov [SfxRomBuffer],eax
%endmacro

%macro UpdateR15 0
    mov ebp,[SfxCPB]
    add ebp,[SfxR15]
%endmacro

%macro CLRFLAGS 0
  ;and dword [SfxSFR],0FFFFh-0100h-0200h-1000h  ; Clear ALT1,ALT2 and B Flags
;  xor ch,ch
;  mov dword [SfxB],0         ; Clear B Flag
;  mov esi,SfxR0
;  mov edi,SfxR0
%endmacro

%macro TORN 1   ; V
   FETCHPIPE
   mov edi, SfxR0+%1*4
   inc ebp                ; Increase program counter
   call [FxTable+ecx*4]
   mov edi,SfxR0
   ret
%endmacro

%macro WITH 1  ; Verified.
   FETCHPIPE
   mov esi,SfxR0+%1*4
   mov edi,SfxR0+%1*4
   mov dword [SfxB],1
   inc ebp                ; Increase program counter
   call [FxTablec+ecx*4]
   mov esi,SfxR0
   mov edi,SfxR0
   mov dword [SfxB],0         ; Clear B Flag
   ret
%endmacro

%macro STWRN 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   mov ebx,[SfxRAMMem]
   mov dword [SfxLastRamAdr],eax    ; Save last ram address
   add dword [SfxLastRamAdr],ebx    ; Save last ram address
   mov edx,[esi]            ; Read Source
   FETCHPIPE
   mov [ebx+eax],dl         ; Store Word
   xor eax,1
   inc ebp                ; Increase program counter
   mov [ebx+eax],dh         ; Store Word
   CLRFLAGS
   ret
%endmacro

%macro STBRN 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   FETCHPIPE
   add eax,[SfxRAMMem]
   mov dword [SfxLastRamAdr],eax    ; Save last ram address
   mov ebx,[esi]            ; Read Source
   mov byte [eax],bl         ; Store Byte
   CLRFLAGS
   inc ebp                ; Increase program counter
   ret
%endmacro

%macro LDWRN 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   mov ebx,[SfxRAMMem]
   mov dword [SfxLastRamAdr],eax    ; Save last ram address
   FETCHPIPE
   mov dl,[ebx+eax]         ; Store Word
   add dword [SfxLastRamAdr],ebx    ; Save last ram address
   xor eax,1
   and edx,0FFFFh
   inc ebp                ; Increase program counter
   mov dh,[ebx+eax]         ; Store Word
   mov [edi],edx            ; Read Source
   CLRFLAGS
   ret
%endmacro

%macro LDBRN 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   FETCHPIPE
   add eax,[SfxRAMMem]
   xor ebx,ebx
   mov dword [SfxLastRamAdr],eax    ; Save last ram address
   mov bl,[eax]                     ; Read Byte
   inc ebp                ; Increase program counter
   mov [edi],ebx            ; Store Result
   CLRFLAGS
   ret
%endmacro

;   test byte[SfxPOR],01h
;   jnz .nozerocheck
;   test byte[SfxPOR],02h
;   jz .nodither

; **** Can pre-calculate [SfxSCBR] << 10 + [sfxramdata]
; Pre-calculate fxbit values from color register

%macro drawpix4b 0
   and [eax],edx
   and [eax+16],edx
   xor edx,0FFFFFFFFh
   mov ebx,[fxbit01pcal]
   and ebx,edx
   or [eax], ebx
   and edx,[fxbit23pcal]
   or [eax+16], edx
%endmacro

%macro drawpix4bd 0
   and [eax],edx
   and [eax+16],edx
   xor edx,0FFFFFFFFh
   mov ebx,[fxbit45pcal]
   and ebx,edx
   or [eax], ebx
   and edx,[fxbit67pcal]
   or [eax+16], edx
%endmacro

%macro drawpix2b 0
   and [eax],edx
   xor edx,0FFFFFFFFh
   and edx,[fxbit01pcal]
   or [eax], edx
%endmacro

%macro drawpix2bd 0
   and [eax],edx
   xor edx,0FFFFFFFFh
   and edx,[fxbit45pcal]
   or [eax], edx
%endmacro

%macro drawpix8b 0
   and [eax],edx
   and [eax+16],edx
   and [eax+32],edx
   and [eax+48],edx
   xor edx,0FFFFFFFFh
   mov ebx,[fxbit01pcal]
   and ebx,edx
   or [eax], ebx
   mov ebx,[fxbit23pcal]
   and ebx,edx
   or [eax+16], ebx
   mov ebx,[fxbit45pcal]
   and ebx,edx
   or [eax+32], ebx
   and edx,[fxbit67pcal]
   or [eax+48], edx
%endmacro

%macro drawpix8bd 0
   and [eax],edx
   and [eax+16],edx
   and [eax+32],edx
   and [eax+48],edx
   xor edx,0FFFFFFFFh
   mov ebx,[fxbit45pcal]
   and ebx,edx
   or [eax], ebx
   mov ebx,[fxbit67pcal]
   and ebx,edx
   or [eax+16], ebx
   mov ebx,[fxbit01pcal]
   and ebx,edx
   or [eax+32], ebx
   and edx,[fxbit23pcal]
   or [eax+48], edx
%endmacro

%macro plotb 5
   shl eax,%3
   and ebx,07h
   add ebx,ebx
   add eax,ebx
   add eax,[SCBRrel]
   mov bl,[SfxR1]
   mov edx,[fxxand+ebx*4]
   %2
%endmacro

%macro plotbz 5
   shl eax,%3
   and ebx,07h
   add ebx,ebx
   add eax,ebx
   add eax,[SCBRrel]
   mov bl,[SfxR1]
   mov edx,[fxxand+ebx*4]
   test byte[SfxCOLR],%5
   jz .nodraw
   %2
%endmacro

%macro plotbd 5
   shl eax,%3
   and ebx,07h
   add ebx,ebx
   add eax,ebx
   add eax,[SCBRrel]
   mov bl,[SfxR1]
   mov edx,[fxxand+ebx*4]
   mov bl,[SfxR1]
   xor bl,[SfxR2]
   test bl,01h
   jz near .nodither4b
   %4
   inc word [SfxR1]
   %1
.nodither4b
   %2
%endmacro

%macro plotbzd 5
   shl eax,%3
   and ebx,07h
   add ebx,ebx
   add eax,ebx
   add eax,[SCBRrel]
   mov bl,[SfxR1]
   mov edx,[fxxand+ebx*4]
   test byte[SfxCOLR],%5
   jz near .nodraw
   mov bl,[SfxR1]
   xor bl,[SfxR2]
   test bl,01h
   jz .nodither4b
   %4
   inc word [SfxR1]
   %1
.nodither4b
   %2
%endmacro

%macro plotlines4b 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 ret, drawpix4b, 5, drawpix4bd, 0Fh
.nodraw
   inc word [SfxR1]
   ret
%endmacro

%macro plotlines4bb 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 FXReturn, drawpix4b, 5, drawpix4bd, 0Fh
.nodraw
   inc word [SfxR1]
   FXReturn
%endmacro

%macro plotlines2b 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 ret, drawpix2b, 4, drawpix2bd, 03h
.nodraw
   inc word [SfxR1]
   ret
%endmacro

%macro plotlines2bb 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 FXReturn, drawpix2b, 4, drawpix2bd, 03h
.nodraw
   inc word [SfxR1]
   FXReturn
%endmacro

%macro plotlines8b 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 ret, drawpix8b, 6, drawpix8bd, 0FFh
.nodraw
   inc word [SfxR1]
   ret
%endmacro

%macro plotlines8bb 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 FXReturn, drawpix8b, 6, drawpix8bd, 0FFh
.nodraw
   inc word [SfxR1]
   FXReturn
%endmacro

%macro plotlines8bl 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 ret, drawpix8b, 6, drawpix8bd, 0Fh
.nodraw
   inc word [SfxR1]
   ret
%endmacro

%macro plotlines8bbl 1
   mov ebx,[SfxR2]
   FETCHPIPE
   mov bh,[SfxR1]
   mov eax,[sfxclineloc]
   inc ebp
   mov eax,[eax+ebx*4]
   cmp eax,0FFFFFFFFh
   je near .nodraw
   %1 FXReturn, drawpix8b, 6, drawpix8bd, 0Fh
.nodraw
   inc word [SfxR1]
   FXReturn
%endmacro

%macro ADDRN 1  ; V
   mov eax, [esi]    ; Read Source
   mov ebx, [SfxR0+%1*4]
   FETCHPIPE
   add ax,bx
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   mov [SfxSignZero],eax
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro ADCRN 1  ; V
   FETCHPIPE
   mov eax, [esi]    ; Read Source
   mov ebx, [SfxR0+%1*4]
   shr byte[SfxCarry],1
   adc ax,bx
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   mov [SfxSignZero],eax
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro ADIRN 1  ; V
   mov eax, [esi]    ; Read Source
   FETCHPIPE
   add ax,%1
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   mov [SfxSignZero],eax
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro ADCIRN 1 ; V
   FETCHPIPE
   mov eax, [esi]    ; Read Source
   shr byte[SfxCarry],1
   adc ax,%1
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   mov [SfxSignZero],eax
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro SUBRN 1  ; V
   mov eax,[esi]    ; Read Source
   mov ebx,[SfxR0+%1*4]
   FETCHPIPE
   sub ax,bx
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   xor byte[SfxCarry],1
   inc ebp                   ; Increase program counter
   mov [edi],eax                        ; Write Destination
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro SBCRN 1  ; V
   FETCHPIPE
   mov eax,[esi]    ; Read Source
   mov ebx,[SfxR0+%1*4]
   cmp byte[SfxCarry],1
   sbb ax,bx
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   xor byte[SfxCarry],1
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro SUBIRN 1 ; V
   mov eax,[esi]    ; Read Source
   FETCHPIPE
   sub ax,%1
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   xor byte[SfxCarry],1
   inc ebp                ; Increase program counter
   mov [edi],eax      ; Write Destination
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro CMPRN 1  ; V
   mov eax,[esi]    ; Read Source
   mov ebx,[SfxR0+%1*4]
   FETCHPIPE
   sub ax,bx
   seto byte[SfxOverflow]
   setc byte[SfxCarry]
   xor byte[SfxCarry],1
   mov [SfxSignZero],eax
   CLRFLAGS
   inc ebp                ; Increase program counter
   ret
%endmacro

%macro ANDRN 1  ; V
   mov eax,[esi]            ; Read Source
   mov ebx,[SfxR0+%1*4]             ; Read RN
   FETCHPIPE
   and eax,ebx
   inc ebp
   mov dword [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro BICRN 1  ; V
   mov ebx,[SfxR0+%1*4]             ; Read RN
   mov eax,[esi]            ; Read Source
   xor ebx,0FFFFh
   FETCHPIPE
   and eax,ebx
   inc ebp
   mov dword [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro ANDIRN 1 ; V
   mov eax,[esi]            ; Read Source
   FETCHPIPE
   and eax,%1
   inc ebp
   mov dword [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro BICIRN 1 ; V
   mov eax,[esi]            ; Read Source
   FETCHPIPE
   and eax,%1
   inc ebp
   mov dword [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro MULTRN 1 ; V
   mov al,byte [esi]     ; Read Source
   mov bl,byte [SfxR0+%1*4]      ; Read RN
   FETCHPIPE
   imul bl
   inc ebp
   and eax,0FFFFh
   mov [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro UMULTRN 1        ; V
   mov al,byte [esi]     ; Read Source
   mov bl,byte [SfxR0+%1*4]      ; Read RN
   FETCHPIPE
   mul bl
   inc ebp
   and eax,0FFFFh
   mov [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro MULTIRN 1        ; V
   mov al,byte [esi]     ; Read Source
   mov bl,%1                  ; Read RN
   FETCHPIPE
   imul bl
   inc ebp
   and eax,0FFFFh
   mov [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro UMULTIRN 1       ; V
   mov al,byte [esi]     ; Read Source
   mov bl,%1                  ; Read RN
   FETCHPIPE
   mul bl
   inc ebp
   and eax,0FFFFh
   mov [SfxSignZero],eax
   mov [edi],eax            ; Write Destination
   CLRFLAGS
   ret
%endmacro

%macro LINK 1   ; Verified.
   mov eax,ebp
   sub eax,[SfxCPB]
   add eax,%1
   FETCHPIPE
   mov word [SfxR11],ax
   CLRFLAGS
   inc ebp
   ret
%endmacro

%macro JMPRN 1  ; V
   FETCHPIPE
   mov eax,[SfxR0+%1*4]             ; Read RN
   mov ebp,[SfxCPB]
   add ebp,eax
   CLRFLAGS
   ret
%endmacro

%macro LJMPRN 1 ; V
   FETCHPIPE
   mov eax,[SfxR0+%1*4]
   and eax,07Fh
   mov byte[SfxPBR],al
;   mov byte[fxtrace+eax],1
   mov eax,[SfxMemTable+eax*4]
   mov [SfxCPB],eax
   mov ebp,eax
   add ebp,[esi]             ; Read RN
   mov dword [SfxCacheActive],0
   push ecx
   call FxOp02
   pop ecx
   dec ebp
   ret
%endmacro

%macro IBTRN 1  ; V
   movsx eax,byte[ebp]
   mov cl,[ebp+1]
   add ebp,2
   mov [SfxR0+%1*4],ax
   CLRFLAGS
   ret
%endmacro

%macro LMSRN 1  ; Verified.
   xor eax,eax
   mov al,[ebp]
   add eax,eax
   inc ebp
   add eax,[SfxRAMMem]
   mov cl,[ebp]
   mov dword [SfxLastRamAdr],eax
   mov ebx,[eax]              ; Read word from ram
   inc ebp
   mov [SfxR0+%1*4],bx              ; Write data
   CLRFLAGS
   ret
%endmacro

%macro SMSRN 1  ; Verified.
   xor eax,eax
   mov al,[ebp]
   inc ebp
   add eax,eax
   mov cl,[ebp]
   add eax,[SfxRAMMem]
   mov ebx,[SfxR0+%1*4]              ; Read data
   mov dword [SfxLastRamAdr],eax
   inc ebp
   mov [eax],bx              ; Write word to ram
   CLRFLAGS
   ret
%endmacro

%macro FROMRN 1 ; V
   FETCHPIPE
   mov esi,SfxR0+%1*4
   inc ebp                ; Increase program counter
   call [FxTable+ecx*4]
   mov esi,SfxR0
   ret
%endmacro

%macro ORRN 1   ; V
   mov eax,[esi]            ; Read Source
   mov ebx,[SfxR0+%1*4]             ; Read
   FETCHPIPE
   or eax,ebx
   inc ebp
   mov [edi],eax            ; Write DREG
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro XORRN 1  ; V
   mov eax,[esi]            ; Read Source
   mov ebx,[SfxR0+%1*4]             ; Read
   FETCHPIPE
   xor eax,ebx
   inc ebp
   mov [edi],eax            ; Write DREG
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro ORI 1    ; V
   mov eax,[esi]            ; Read Source
   FETCHPIPE
   or eax,%1
   inc ebp
   mov [edi],eax            ; Write DREG
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro XORI 1   ; V
   mov eax,[esi]            ; Read Source
   FETCHPIPE
   xor eax,%1
   inc ebp
   mov [edi],eax            ; Write DREG
   mov [SfxSignZero],eax
   CLRFLAGS
   ret
%endmacro

%macro INCRN 1  ; Verified
   inc word[SfxR0+%1*4]
   FETCHPIPE
   mov eax,[SfxR0+%1*4]            ; Read Source
   mov [SfxSignZero],eax
   CLRFLAGS
   inc ebp
   ret
%endmacro

%macro DECRN 1  ; Verified
   dec word[SfxR0+%1*4]
   FETCHPIPE
   mov eax,[SfxR0+%1*4]            ; Read Source
   mov [SfxR0+%1*4],eax
   mov [SfxSignZero],eax
   CLRFLAGS
   inc ebp
   ret
%endmacro

%macro IWTRN 1  ; aka LEA       ; Verified.
   mov eax,[ebp]
   mov cl,[ebp+2]
   and eax,0FFFFh
   add ebp,3
   mov [SfxR0+%1*4],eax
   CLRFLAGS
   ret
%endmacro

%macro LMRN 1   ; Verified!
   xor eax,eax
   mov cl,[ebp+2]
   mov ax,[ebp]
   mov ebx,[SfxRAMMem]
   mov [SfxLastRamAdr],eax
   add [SfxLastRamAdr],ebx
   mov dl,[eax+ebx]
   xor eax,1
   add ebp,3
   mov dh,[eax+ebx]
   mov word [SfxR0+%1*4],dx         ; Store Word
   CLRFLAGS
   ret
%endmacro

%macro SMRN 1   ; Verified
   mov ebx,[SfxR0+%1*4]
   mov eax,[ebp]
   mov cl,[ebp+2]
   and eax,0FFFFh
   mov dx,bx
   mov ebx,[SfxRAMMem]
   mov [SfxLastRamAdr],eax
   add [SfxLastRamAdr],ebx
   mov [eax+ebx],dl
   xor eax,1
   add ebp,3
   mov [eax+ebx],dh
   CLRFLAGS
   ret
%endmacro

%macro PackEsiEdi 0
   mov eax,[SfxSREG]
   shl eax,2
   add eax,SfxR0
   mov esi,eax
   mov eax,[SfxDREG]
   shl eax,2
   add eax,SfxR0
   mov edi,eax
   mov eax,[SfxRAMBR]
   shl eax,16
   add eax,[sfxramdata]
   mov dword [SfxRAMMem],eax
%endmacro

%macro UnPackEsiEdi 0
   mov eax,esi
   sub eax,SfxR0
   shr eax,2
   mov [SfxSREG],eax
   mov eax,edi
   sub eax,SfxR0
   shr eax,2
   mov [SfxDREG],eax
%endmacro
