;Copyright (C) 1997-2005 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
;
;http://www.zsnes.com
;http://sourceforge.net/projects/zsnes
;
;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 FXReturn 0
   dec dword[NumberOfOpcodes]
   jmp [FxTabled+ecx*4]
   ALIGN32
%endmacro

%macro FXReturn2 0
   dec dword[NumberOfOpcodes]
   js %%endloop
   jmp [FxTabled+ecx*4]
%%endloop
   jmp FXEndLoop
;   jmp [FxTabled+ecx*4]
   ALIGN32
%endmacro

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

%macro WITHc 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
   FXReturn
%endmacro

%macro STWRNc 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   mov ebx,[SfxRAMMem]
   mov [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
   FXReturn
%endmacro

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

%macro LDWRNc 1  ; V
   mov eax,[SfxR0+%1*4]             ; Read register
   mov ebx,[SfxRAMMem]
   mov [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
   FXReturn
%endmacro

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

%macro ADDRNc 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
   FXReturn
%endmacro

%macro ADCRNc 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
   FXReturn
%endmacro

%macro ADIRNc 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
   FXReturn
%endmacro

%macro ADCIRNc 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
   FXReturn
%endmacro

%macro SUBRNc 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
   FXReturn
%endmacro

%macro SBCRNc 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
   FXReturn
%endmacro

%macro SUBIRNc 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
   FXReturn
%endmacro

%macro CMPRNc 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
   FXReturn
%endmacro

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

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

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

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

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

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

%macro MULTIRNc 1        ; V
   mov al,[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
   FXReturn
%endmacro

%macro UMULTIRNc 1       ; V
   mov al,[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
   FXReturn
%endmacro

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

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

%macro LJMPRNc 1 ; V
   FETCHPIPE
   mov eax,[SfxR0+%1*4]
   and eax,07Fh
   mov [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
   FXReturn
%endmacro

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

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

%macro SMSRNc 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 [SfxLastRamAdr],eax
   inc ebp
   mov [eax],bx              ; Write word to ram
   CLRFLAGS
   FXReturn
%endmacro

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

%macro ORRNc 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
   FXReturn
%endmacro

%macro XORRNc 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
   FXReturn
%endmacro

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

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

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

%macro DECRNc 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
   FXReturn
%endmacro

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

%macro LMRNc 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 [SfxR0+%1*4],dx         ; Store Word
   CLRFLAGS
   FXReturn
%endmacro

%macro SMRNc 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
   FXReturn
%endmacro


