;Copyright (C) 1997-2001 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.


EXTSYM OMBGTestVal,ngptrdat2,cachesingle4bng,ofshvaladd,ofsmtptrs,ofsmcptr2
EXTSYM Testval






; makev16t macros

%macro procwindowback 0
    mov byte[winon],0
    mov bl,[wincolen]
    and bl,00001010b
    cmp bl,00001010b
    je near .dualwin
    mov bl,[scaddset]
    shr bl,4
    and bl,00000011b

    cmp bl,0
    je near .finishwin
    cmp bl,3
    je near .clearwin
    test byte[wincolen],00001010b
    jz near .finishwin

    mov al,[wincolen]
    and al,00000101b

    mov esi,windowdata
    mov byte[winon],1
    mov byte[numwin],0
    mov cl,[winl2]
    mov ch,[winr2]
    test byte[wincolen],00000010b
    jz .nowin1
    mov cl,[winl1]
    mov ch,[winr1]
    shl al,2
.nowin1
    cmp ch,255
    je .noinc
    inc ch
.noinc
    test al,04h
    jnz near .wininside
    cmp cl,ch
    jae .noinsidemask
    mov byte[esi],0
    mov byte[esi+1],01h
    mov byte[esi+2],cl
    mov byte[esi+3],0FFh
    mov byte[esi+4],ch
    mov byte[esi+5],01h
    mov byte[numwin],3
    jmp .finishwin
.noinsidemask
    mov byte[winon],2
    jmp .finishwin
.wininside
    cmp cl,ch
    ja .nooutsidemask
.nonotoutside
    cmp ch,254
    jb .skipnodraw
    cmp cl,1
    jbe .noinsidemask
.skipnodraw
    mov byte[esi],cl
    mov byte[esi+1],01h
    mov byte[esi+2],ch
    mov byte[esi+3],0FFh
    mov byte[numwin],2
    jmp .finishwin
.nooutsidemask
    mov byte[winon],5
    jmp .finishwin
.dualwin
    mov al,[wincolen]
    mov bl,[scaddset]
    and bl,30h
    cmp bl,30h
    je .clearwin
    cmp bl,00h
    je .finishwin
    call makedualwincol
    mov byte[winon],3
    jmp .finishwin
.clearwin
    mov byte[winon],4
.finishwin
%endmacro


%macro procwindowback2 0
    mov byte[winon],0
    mov al,[wincolen]
    test al,00001010b
    jz near .finishwin
    mov esi,windowdata
    mov bl,al
    mov byte[winon],1
    and bl,00001010b
    and al,00000101b
    mov byte[numwin],0
    cmp bl,00001010b
    je near .multiwin
    test bl,00000010b
    jnz .win1
    mov cl,[winl2]
    mov ch,[winr2]
    shr al,2
    jmp .okaywin
.win1
    mov cl,[winl1]
    mov ch,[winr1]
    and al,01h
.okaywin
    test al,01h
    jz near .wininside
    cmp cl,ch
    jae .noinsidemask
    mov byte[esi],0
    mov byte[esi+1],01h
    mov byte[esi+2],cl
    mov byte[esi+3],0FFh
    mov byte[esi+4],ch
    mov byte[esi+5],01h
    mov byte[numwin],3
    jmp .finishwin
.noinsidemask
    mov byte[esi],0
    mov byte[esi+1],01h
    mov byte[numwin],1
    jmp .finishwin
.wininside
    cmp cl,ch
    ja .nooutsidemask
.nonotoutside
    cmp ch,254
    jb .skipnodraw
    cmp cl,1
    jbe .noinsidemask
.skipnodraw
    mov byte[esi],cl
    mov byte[esi+1],01h
    mov byte[esi+2],ch
    mov byte[esi+3],0FFh
    mov byte[numwin],2
    jmp .finishwin
.nooutsidemask
    mov byte[winon],0
    jmp .finishwin
.multiwin
    mov byte[esi],0
    mov byte[esi+1],01h
    mov byte[numwin],1
.finishwin
%endmacro


;*******************************************************
; Processes & Draws 4-bit sprites for both main & sub
;*******************************************************

%macro drawspr16bt 2
    mov al,[esi+%1]
    test al,0Fh
    jz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi-%2],dx
    mov [ebp-%2],dx
%%skip
%endmacro


%macro drawspr16btwo 3
    mov al,[esi+%1]
    test al,0Fh
    jz %%skip
    cmp byte[winspdata+ebx-8+%3+16],0
    jne %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi-%2],dx
    mov [ebp-%2],dx
%%skip
%endmacro


%macro sprpriorityinit 1
    cmp byte[sprclprio+ebp],0
    je near .endobj
    test byte[cwinenabm],10h
    jz .drawnowin
    cmp byte[winonsp],0
    jne near %1
.drawnowin
%endmacro

%macro sprprioritydrawbt 2
    cmp dword[sprsingle],1
    je near .drawsingle
    mov [csprprlft],cl
    mov esi,[currentobjptr]
    mov edi,[curvidoffset]
    mov edx,esi
    xor ebx,ebx
.loopobj
    test byte[esi+7],20h
    jnz near .drawspriteflipx
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov dl,[esi+7]
    xor eax,eax
    and edx,03h
    cmp edx,ebp
    jne near .notprio
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawa16b %1
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.notprio
    mov esi,[esi+2]
    mov dl,[csprbit]
    sprdrawa sprdrawpra2
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.drawspriteflipx
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov dl,[esi+7]
    xor eax,eax
    and edx,03h
    cmp edx,ebp
    jne near .notpriof
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawaf16b %1
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
.endobj
    ret
.notpriof
    mov esi,[esi+2]
    mov dl,[csprbit]
    sprdrawaf sprdrawpra2
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.clearcsprmem
    xor eax,eax
    mov ecx,64
    mov edi,sprpriodata+16
    rep stosd
    ret
.drawsingle
    mov esi,[currentobjptr]
    mov edi,[curvidoffset]
    mov edx,ecx
    and edx,0FFh
    xor eax,eax
    shl edx,3
    sub edx,8
    add edx,esi
    mov esi,edx
    xor ebx,ebx
.loopobj2
    test byte[esi+7],20h
    jnz near .drawspriteflipx2
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov esi,[esi+2]
    sprdrawa16b %2
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
.drawspriteflipx2
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov esi,[esi+2]
    sprdrawaf16b %2
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
%endmacro

%macro sprdrawpra16bt 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
    mov [transpbuf+32+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprb16bt 1
    or eax,eax
    jz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
    mov [transpbuf+32+ebx*2-%1*2],dx
%%skip
%endmacro


;*******************************************************
; Processes & Draws 4-bit sprites w/ transparency
;*******************************************************

%macro drawspr16ta 2
    mov al,[esi+%1]
    or al,al
    jz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx-%2],dx
%%skip
%endmacro

%macro drawspr16tb 2
    mov al,[esi+%1]
    or al,al
    jz %%skip
    add al,ch
    mov ebx,[ebp-%2]
    mov edx,[pal16b+eax*4]
    cmp bx,0
    je %%noadd
    and edx,[vesa2_clbit]
    and ebx,[vesa2_clbit]
    add edx,ebx
    shr edx,1
%%noadd
    mov [edi-%2],dx
%%skip
%endmacro


%macro drawspr16tc 2
    mov al,[esi+%1]
    or al,al
    jz %%skip
    add al,ch
    mov ebx,[ebp-%2]
    mov edx,[pal16bcl+eax*4]
    and ebx,[vesa2_clbit]
    add edx,ebx
    shr edx,1
    mov edx,[fulladdtab+edx*2]
    mov [edi-%2],dx
%%skip
%endmacro


%macro drawspr16td 2
    mov al,[esi+%1]
    or al,al
    jz %%skip
    add al,ch
    mov ebx,[ebp-%2]
    mov edx,[pal16bxcl+eax*4]
    and ebx,[vesa2_clbit]
    add edx,ebx
    shr edx,1
    mov edx,[fulladdtab+edx*2]
    xor edx,0FFFFh
    mov [edi-%2],dx
%%skip
%endmacro

%macro drawspr16tawinon 3
    mov al,[esi+%1]
    or al,al
    jz %%skip
    cmp byte[winspdata+ebx-8+%3+16],0
    jne %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%2],dx
%%skip
%endmacro

%macro drawspr16tbwinon 3
    mov al,[esi+%1]
    or al,al
    jz %%skip
    cmp byte[winspdata+ebx-8+%3+16],0
    jne %%skip
    add al,ch
    mov edx,[ebp-%2]
    mov eax,[pal16b+eax*4]
    cmp dx,0
    je %%noadd
    and eax,[vesa2_clbit]
    and edx,[vesa2_clbit]
    add eax,edx
    shr eax,1
%%noadd
    mov [edi-%2],ax
    xor eax,eax
%%skip
%endmacro

%macro drawspr16tcwinon 3
    mov al,[esi+%1]
    or al,al
    jz %%skip
    cmp byte[winspdata+ebx-8+%3+16],0
    jne %%skip
    add al,ch
    mov edx,[ebp-%2]
    mov eax,[pal16bcl+eax*4]
    and edx,[vesa2_clbit]
    add eax,edx
    shr eax,1
    mov eax,[fulladdtab+eax*2]
    mov [edi-%2],ax
    xor eax,eax
%%skip
%endmacro

%macro drawspr16tdwinon 3
    mov al,[esi+%1]
    or al,al
    jz %%skip
    cmp byte[winspdata+ebx-8+%3+16],0
    jne %%skip
    add al,ch
    mov edx,[ebp-%2]
    mov eax,[pal16bxcl+eax*4]
    and edx,[vesa2_clbit]
    add eax,edx
    shr eax,1
    mov eax,[fulladdtab+eax*2]
    xor eax,0FFFFh
    mov [edi-%2],ax
    xor eax,eax
%%skip
%endmacro

%macro drawsprgrp 1
    %1 0, 16
    %1 1, 14
    %1 2, 12
    %1 3, 10
    %1 4, 8
    %1 5, 6
    %1 6, 4
    %1 7, 2
%endmacro

%macro drawsprgrpf 1
    %1 7, 16
    %1 6, 14
    %1 5, 12
    %1 4, 10
    %1 3, 8
    %1 2, 6
    %1 1, 4
    %1 0, 2
%endmacro

%macro drawsprgrpwin 1
    %1 0, 16, 0
    %1 1, 14, 1
    %1 2, 12, 2
    %1 3, 10, 3
    %1 4, 8, 4
    %1 5, 6, 5
    %1 6, 4, 6
    %1 7, 2, 7
%endmacro

%macro drawsprgrpfwin 1
    %1 7, 16, 0
    %1 6, 14, 1
    %1 5, 12, 2
    %1 4, 10, 3
    %1 3, 8, 4
    %1 2, 6, 5
    %1 1, 4, 6
    %1 0, 2, 7
%endmacro


%macro sprprioritydrawt16b 4
    cmp dword[sprsingle],1
    je near .drawsingle
    mov [csprprlft],cl
    mov esi,[currentobjptr]
    mov edi,[curvidoffset]
    mov edx,esi
    xor ebx,ebx
.loopobj
    test byte[esi+7],20h
    jnz near .drawspriteflipx
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov dl,[esi+7]
    xor eax,eax
    and edx,03h
    cmp edx,ebp
    jne near .notprio
    cmp ch,12*16
    jae near .transpobj
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawa16b %3
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.transpobj
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawa16b %1
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.notprio
    mov esi,[esi+2]
    mov dl,[csprbit]
    sprdrawa sprdrawpra2
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.drawspriteflipx
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov dl,[esi+7]
    xor eax,eax
    and edx,03h
    cmp edx,ebp
    jne near .notpriof
    cmp ch,12*16
    jae near .transpobjf
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawaf16b %3
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
.endobj
    ret
.transpobjf
    mov esi,[esi+2]
    mov cl,[csprbit]
    sprdrawaf16b %1
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.notpriof
    mov esi,[esi+2]
    mov dl,[csprbit]
    sprdrawaf sprdrawpra2
    pop esi
    add esi,8
    dec byte[csprprlft]
    jnz near .loopobj
    rol byte[csprbit],1
    cmp byte[csprbit],1
    je near .clearcsprmem
    ret
.clearcsprmem
    xor eax,eax
    mov ecx,64
    mov edi,sprpriodata+16
    rep stosd
    ret
.drawsingle
    mov esi,[currentobjptr]
    mov edi,[curvidoffset]
    mov edx,ecx
    and edx,0FFh
    xor eax,eax
    shl edx,3
    sub edx,8
    add edx,esi
    mov esi,edx
    xor ebx,ebx
.loopobj2
    test byte[esi+7],20h
    jnz near .drawspriteflipx2
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov esi,[esi+2]
    cmp ch,12*16
    jae near .transpobj2
    sprdrawa16b %4
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
.transpobj2
    sprdrawa16b %2
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
.drawspriteflipx2
    mov bx,[esi]
    push esi
    mov ch,[esi+6]
    mov esi,[esi+2]
    cmp ch,12*16
    jae near .transpobj2f
    sprdrawaf16b %4
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
.transpobj2f
    sprdrawaf16b %2
    pop esi
    sub esi,8
    dec cl
    jnz near .loopobj2
    ret
%endmacro

%macro sprdrawpra16bha 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16b+eax*4]
    cmp dx,0
    je %%noadd
    and edx,[vesa2_clbit]
    and eax,[vesa2_clbit]
    add eax,edx
    shr eax,1
%%noadd
    mov [edi+ebx*2-%1*2],ax
    or byte[sprpriodata+ebx-%1+16],cl
    xor eax,eax
%%skip
%endmacro

%macro sprdrawprb16bha 1
    or eax,eax
    jz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16b+eax*4]
    cmp dx,0
    je %%noadd
    and edx,[vesa2_clbit]
    and eax,[vesa2_clbit]
    add eax,edx
    shr eax,1
%%noadd
    mov [edi+ebx*2-%1*2],ax
    xor eax,eax
%%skip
%endmacro

%macro sprdrawpraw16bha 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16b+eax*4]
    cmp dx,0
    je %%noadd
    and edx,[vesa2_clbit]
    and eax,[vesa2_clbit]
    add eax,edx
    shr eax,1
%%noadd
    mov [edi+ebx*2-%1*2],ax
    or byte[sprpriodata+ebx-%1+16],cl
    xor eax,eax
%%skip
%endmacro

%macro sprdrawprbw16bha 1
    or eax,eax
    jz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16b+eax*4]
    cmp dx,0
    je %%noadd
    and edx,[vesa2_clbit]
    and eax,[vesa2_clbit]
    add eax,edx
    shr eax,1
%%noadd
    mov [edi+ebx*2-%1*2],ax
    xor eax,eax
%%skip
%endmacro


%macro sprdrawpra16bfa 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprb16bfa 1
    or eax,eax
    jz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

%macro sprdrawpraw16bfa 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprbw16bfa 1
    or eax,eax
    jz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

%macro sprdrawpra16bfs 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bxcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    xor edx,0FFFFh
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprb16bfs 1
    or eax,eax
    jz %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bxcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    xor edx,0FFFFh
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

%macro sprdrawpraw16bfs 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bxcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    xor edx,0FFFFh
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprbw16bfs 1
    or eax,eax
    jz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[transpbuf+32+ebx*2-%1*2]
    mov eax,[pal16bxcl+eax*4]
    and edx,[vesa2_clbit]
    add edx,eax
    shr edx,1
    xor eax,eax
    mov edx,[fulladdtab+edx*2]
    xor edx,0FFFFh
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

;*******************************************************
; Processes & Draws 8x8 tiles for both main & sub screen
;*******************************************************

%macro draw8x816bta 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%2],cx
    mov [ebp+%2],cx
%%loop
%endmacro

%macro draw8x816btawinon 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    test byte[ecx+%1],0FFh
    jnz %%loop
    add al,dh
    mov eax,[pal16b+eax*4]
    mov [esi+%2],ax
    mov [ebp+%2],ax
    xor eax,eax
%%loop
%endmacro

%macro draw8x816btawinonb 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    test byte[ecx+7-%1],0FFh
    jnz %%loop
    add al,dh
    mov eax,[pal16b+eax*4]
    mov [esi+%2],ax
    mov [ebp+%2],ax
    xor eax,eax
%%loop
%endmacro

%macro drawtilegrp 1
    cmp dword[ebx],0
    je near %%loop
    %1 0, 0
    %1 1, 2
    %1 2, 4
    %1 3, 6
%%loop
    cmp dword[ebx+4],0
    je near %%loop2
    %1 4, 8
    %1 5, 10
    %1 6, 12
    %1 7, 14
%%loop2
%endmacro

%macro drawtilegrpf 1
    cmp dword[ebx+4],0
    je near %%loop
    %1 7, 0
    %1 6, 2
    %1 5, 4
    %1 4, 6
%%loop
    cmp dword[ebx],0
    je near %%loop2
    %1 3, 8
    %1 2, 10
    %1 1, 12
    %1 0, 14
%%loop2
%endmacro

%macro drawtilegrpfull 1
    cmp dword[edi],0
    je near %%loop
    %1 0, 0
    %1 1, 2
    %1 2, 4
    %1 3, 6
%%loop
    cmp dword[edi+4],0
    je near %%loop2
    %1 4, 8
    %1 5, 10
    %1 6, 12
    %1 7, 14
%%loop2
%endmacro

%macro drawtilegrpfullf 1
    cmp dword[edi+4],0
    je near %%loop
    %1 7, 0
    %1 6, 2
    %1 5, 4
    %1 4, 6
%%loop
    cmp dword[edi],0
    je near %%loop2
    %1 3, 8
    %1 2, 10
    %1 1, 12
    %1 0, 14
%%loop2
%endmacro


;*******************************************************
; Processes & Draws 8x8 tiles in 2, 4, & 8 bit mode
;*******************************************************

%macro draw8x816ta 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ecx,[ebp+%2]
    mov eax,[pal16b+eax*4]
    test ecx,0FFFFh
    je %%noadd
    and eax,1111011111011110b  ; [vesa2_clbit]
    and ecx,1111011111011110b  ; [vesa2_clbit]
    add eax,ecx
    shr eax,1
%%noadd
    mov [esi+%2],ax
    xor eax,eax
%%loop
%endmacro

%macro draw8x816ta2 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ebx,[ebp+%2]
    mov ecx,[pal16b+eax*4]
    test ebx,0FFFFh
    je %%noadd
    and ecx,1111011111011110b  ; [vesa2_clbit]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
%%noadd
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tb 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ebx,[ebp+%2]
    mov ecx,[pal16bcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tc 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ebx,[ebp+%2]
    mov ecx,[pal16bxcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    xor ecx,0FFFFh
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tawinon 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    test byte[edx+%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ecx,[ebp+%2]
    mov eax,[pal16b+eax*4]
    test ecx,0FFFFh
    je %%noadd
    and eax,1111011111011110b  ; [vesa2_clbit]
    and ecx,1111011111011110b  ; [vesa2_clbit]
    add eax,ecx
    shr eax,1
%%noadd
    mov [esi+%2],ax
    xor eax,eax
%%loop
%endmacro

%macro draw8x816tbwinon 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tcwinon 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bxcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    xor ecx,0FFFFh
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tawinon2 3
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+%3],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ecx,[ebp+%2]
    mov ebx,[pal16b+eax*4]
    test ecx,0FFFFh
    je %%noadd
    and ebx,1111011111011110b  ; [vesa2_clbit]
    and ecx,1111011111011110b  ; [vesa2_clbit]
    add ebx,ecx
    shr ebx,1
%%noadd
    mov [esi+%2],bx
%%loop
%endmacro

%macro draw8x816tbwinon2 3
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+%3],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tcwinon2 3
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+%3],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bxcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    xor ecx,0FFFFh
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tawinonb 2
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    test byte[edx+7-%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ecx,[ebp+%2]
    mov eax,[pal16b+eax*4]
    test ecx,0FFFFh
    je %%noadd
    and eax,1111011111011110b  ; [vesa2_clbit]
    and ecx,1111011111011110b  ; [vesa2_clbit]
    add eax,ecx
    shr eax,1
%%noadd
    mov [esi+%2],ax
    xor eax,eax
%%loop
%endmacro

%macro draw8x816tbwinonb 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+7-%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    mov [esi+%2],cx
%%loop
%endmacro

%macro draw8x816tcwinonb 2
    mov al,[edi+%1]
    or al,al
    jz %%loop
    test byte[edx+7-%1],0FFh
    jnz %%loop
    add al,[coadder16]
    mov ebx,[ebp+%2]
    mov ecx,[pal16bxcl+eax*4]
    and ebx,1111011111011110b  ; [vesa2_clbit]
    add ecx,ebx
    shr ecx,1
    mov ecx,[fulladdtab+ecx*2]
    xor ecx,0FFFFh
    mov [esi+%2],cx
%%loop
%endmacro


; end makev16t
; ****************************************************
; ****************************************************

%macro Draw16x1616bwin 3
    mov al,[ebx+%1]
    test al,0FFh
    jz %%loop
    test byte[ebp+%3],0FFh
    jnz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%2],cx
%%loop
%endmacro

;*******************************************************
; Processes & Draws 16x16 tiles in 2, 4, & 8 bit mode
;*******************************************************
%macro Draw16x1616b 2
    mov al,[ebx+%1]
    test al,0FFh
    jz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%2],cx
%%loop
%endmacro

%macro drawpixel16b8x8winon 4
    mov al,[ebx+%1]
    test al,0FFh
    jz %2
    test byte[ebp+%4],0FFh
    jnz %2
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%3],cx
%2
%endmacro

%macro drawpixel16b8x8 3
    mov al,[ebx+%1]
    test al,0FFh
    jz %2
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%3],cx
%2
%endmacro

%macro Draw8x816bwinmacro 1
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    test byte[ebp+%1],0FFh
    jnz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%1*2],cx
%%loop
%endmacro

%macro Draw8x816bwinflipmacro 1
    mov al,[ebx+7-%1]
    or al,al
    jz %%loop
    test byte[ebp+%1],0FFh
    jnz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%1*2],cx
%%loop
%endmacro

;*******************************************************
; DrawLine 16bit                  Draws the current line
;*******************************************************
; use curypos+bg1scroly for y location and bg1scrolx for x location
; use bg1ptr(b,c,d) for the pointer to the tile number contents
; use bg1objptr for the pointer to the object tile contents

%macro procmode716b 3
    xor eax,eax
    xor edx,edx
    mov ax,[curypos]
    test byte[mode7set],02h
    jz .noflip
    mov ax,261
    sub ax,[curypos]
    dec ax
.noflip
    mov byte[curmosaicsz],1
    test byte[mosaicon],%3
    jz .nomos
    mov bl,[mosaicsz]
    cmp bl,0
    je .nomos
    inc bl
    mov [curmosaicsz],bl
    xor bh,bh
    div bx
    xor edx,edx
    mul bx
.nomos
    add ax,%1
    mov dx,%2
    call drawmode716b
%endmacro


%macro sprdrawpra16b 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprb16b 1
    or eax,eax
    jz %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

%macro sprdrawpraw16b 1
    or eax,eax
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],cl
    jnz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
    or byte[sprpriodata+ebx-%1+16],cl
%%skip
%endmacro

%macro sprdrawprbw16b 1
    or eax,eax
    jz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add al,ch
    mov edx,[pal16b+eax*4]
    mov [edi+ebx*2-%1*2],dx
%%skip
%endmacro

;*******************************************************
; Processes & Draws 8x8 tiles in 2, 4, & 8 bit mode
;*******************************************************

%macro Draw8x816bmacro 1
    mov al,[ebx+%1]
    or al,al
    jz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%1*2],cx
%%loop
%endmacro

%macro Draw8x816bflipmacro 1
    mov al,[ebx+7-%1]
    or al,al
    jz %%loop
    add al,dh
    mov ecx,[pal16b+eax*4]
    mov [esi+%1*2],cx
%%loop
%endmacro

%macro sprdrawa16b 1
    mov al,[esi]
    %1 8
    mov al,[esi+1]
    %1 7
    mov al,[esi+2]
    %1 6
    mov al,[esi+3]
    %1 5
    mov al,[esi+4]
    %1 4
    mov al,[esi+5]
    %1 3
    mov al,[esi+6]
    %1 2
    mov al,[esi+7]
    %1 1
%endmacro

%macro sprdrawaf16b 1
    mov al,[esi]
    %1 1
    mov al,[esi+1]
    %1 2
    mov al,[esi+2]
    %1 3
    mov al,[esi+3]
    %1 4
    mov al,[esi+4]
    %1 5
    mov al,[esi+5]
    %1 6
    mov al,[esi+6]
    %1 7
    mov al,[esi+7]
    %1 8
%endmacro


%macro sprdrawa 1
    mov eax,[esi]
    %1 8, al
    %1 7, ah
    mov eax,[esi+2]
    %1 6, al
    %1 5, ah
    mov eax,[esi+4]
    %1 4, al
    %1 3, ah
    mov eax,[esi+6]
    %1 2, al
    %1 1, ah
%endmacro

%macro sprdrawpra 2
    or %2,%2
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],dl
    jnz %%skip
    add %2,ch
    mov [edi+ebx-%1],%2
    or byte[sprpriodata+ebx-%1+16],dl
%%skip
%endmacro

%macro sprdrawprb 2
    or %2,%2
    jz %%skip
    add %2,ch
    mov [edi+ebx-%1],%2
%%skip
%endmacro

%macro sprdrawpra2 2
    or %2,%2
    jz %%skip
    or byte[sprpriodata+ebx-%1+16],dl
%%skip
%endmacro

%macro sprdrawpraw 2
    or %2,%2
    jz %%skip
    test byte[sprpriodata+ebx-%1+16],dl
    jnz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add %2,ch
    mov [edi+ebx-%1],%2
    or byte[sprpriodata+ebx-%1+16],dl
%%skip
%endmacro

%macro sprdrawprbw 2
    or %2,%2
    jz %%skip
    cmp byte[winspdata+ebx-%1+16],0
    jne %%skip
    add %2,ch
    mov [edi+ebx-%1],%2
%%skip
%endmacro

%macro sprdrawaf 1
    mov eax,[esi]
    %1 1, al
    %1 2, ah
    mov eax,[esi+2]
    %1 3, al
    %1 4, ah
    mov eax,[esi+4]
    %1 5, al
    %1 6, ah
    mov eax,[esi+6]
    %1 7, al
    %1 8, ah
%endmacro

%macro procoffsetmode 0
    push ebx
    push edx
    add word[ofsmmptr],2
    add word[ofsmtptr],2
    mov ebx,[yposngom]
    mov eax,[flipyposngom]
    mov [yadder],ebx
    mov [yrevadder],eax
    mov ax,[ofsmmptr]
    test eax,03Fh
    jnz %%next
    mov bx,[bgtxadd]
    add ax,bx
    add [ofsmmptr],bx
    add [ofsmtptr],bx
%%next
    add eax,[vram]
    mov edi,eax
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    mov eax,[OMBGTestVal]
    test dword[ebx],eax
    jz %%noofsm
    mov ebx,[ebx]
    mov ax,[ofsmtptr]
    and ebx,3FFh
    add ebx,[ofsmcyps]
    test ebx,100h
    jz %%noupper
    add ax,[ofsmady]
%%noupper
    and ebx,0FFh
    mov edx,ebx
    shr ebx,3
    and edx,07h
    shl ebx,6
    shl edx,3
    add ax,bx
    mov [yadder],edx
    xor edx,38h
    mov [yrevadder],edx
    add eax,[vram]
    mov edi,eax
%%noofsm
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    add dword[ofshvaladd],8
    mov eax,[OMBGTestVal]
    add dword[ofsmcptr2],2
    and dword[ofsmcptr2],3Fh
    test dword[ebx-40h],eax
    jz %%noofsmh
    mov eax,edi
    sub eax,[vram]
    mov ebx,[ebx-40h]
    sub ax,[ofsmtptr]
    add ax,[ofsmtptrs]
    add ebx,[ofshvaladd]
    test ebx,100h
    jz %%noleft
    add ax,[ofsmadx]
%%noleft
    and ebx,0F8h
    shr ebx,2
    add ax,bx
    add eax,[vram]
    mov edi,eax
%%noofsmh
    pop edx
    pop ebx
%endmacro

;*******************************************************
; Processes & Draws 8x8 offset mode in Mode 2/4
;*******************************************************

%macro initoffsetmode 0
    push ebx
    push ecx
    mov ebx,2000h
    mov ecx,ebp
    shl ebx,cl
    mov [OMBGTestVal],ebx
    mov ecx,[bg1scroly+ebp*2]
    and ecx,0FFFFh
    add ecx,ebx
    push eax
    push edx
    xor edx,edx
    mov dx,[bg3scroly]
;    and dx,0F8h
    shr edx,3
    shl edx,6
    xor eax,eax
    mov ax,[bg3ptr]
    add ax,dx
    xor edx,edx
    mov dx,[bg3scrolx]
    and dx,0F8h
    xor ebx,ebx
    mov ebx,[curypos]
    mov [ofsmcyps],ebx
    shr edx,3
    shl edx,1
    add ax,dx
    add eax,40h
    mov edx,eax
    mov [Testval],edx
    and edx,0FFFFFFC0h
    add edx,[vram]
    mov [ofsmcptr],edx
    and eax,3Fh
    mov [ofsmcptr2],eax
    mov eax,[bg1ptry+ebp*4]
    mov [ofsmady],eax
    mov eax,[bg1ptrx+ebp*4]
    mov [ofsmadx],eax
    mov eax,[bg1ptr+ebp*2]
    mov [ofsmtptr],eax
    mov [ofsmtptrs],eax
    test ecx,100h
    jz .noyincd
    add eax,[bg1ptry+ebp*4]
.noyincd
    lea ecx,[ecx*8]
    and ecx,07C0h      ; 1Fh*40h
    add eax,ecx
    mov edx,[yadder]
    mov [yposngom],edx
    mov edx,[yrevadder]
    mov [flipyposngom],edx
    mov ecx,[bg1scrolx+ebp*2]
    mov edx,[bg1ptrx+ebp*4]
    test ecx,100h
    jz .noxincd
    add eax,edx
    add [ofsmtptr],edx
    neg dx
.noxincd
    sub dx,64
    and ecx,0F8h
    and eax,0FFFFh
    shr ecx,2
    mov [bgtxadd],edx
    add eax,ecx
    add [ofsmtptr],ecx
    mov [ofsmmptr],eax
    xor eax,eax
    mov ax,[bg1objptr+ebp*2]
    shr eax,5
    mov [ngptrdat2],eax
    mov eax,edi
    sub eax,[vram]
    mov [ofsmmptr],eax
    mov dword[ofshvaladd],0
    pop edx
    pop eax
    pop ecx
    pop ebx
%endmacro

%macro offsetmcachechk 0
    ; Cache check
    push ecx
    mov ecx,eax
    add ecx,[ngptrdat2]
    and ecx,2047
    cmp word[vidmemch4+ecx*2],0
    je %%nocache
    call cachesingle4bng
%%nocache
    pop ecx
%endmacro
