;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 procpixelstw 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+%2],cx
%%nodraw
%endmacro

%macro procpixelstwt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2+512]
    mov [edi+%2],cx
%%nodraw
%endmacro

%macro procpixelstwnt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+%2],cx
%%nodraw
%endmacro

%macro procpixelstwmst 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2+512]
    mov [edi+%2],cx
    and cx,[UnusedBitXor]
    mov [edi+75036*2+%2],cx
%%nodraw
%endmacro

%macro procpixelstwmsnt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+%2],cx
    mov [edi+75036*2+%2],cx
%%nodraw
%endmacro

%macro procpixelstwmsbt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2+512]
    mov [edi+%2],cx
    cmp dword[ngcwinmode],1
    je %%nodraw
    and cx,[UnusedBitXor]
    mov [edi+75036*2+%2],cx
%%nodraw
%endmacro

%macro procpixelstwmsbnt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+%2],cx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+75036*2+%2],cx
%%nodraw
%endmacro

%macro procpixelstwsmt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+75036*2+%2],cx
    cmp dword[ngcwinmode],1
    je %%nodraw
    or cx,[UnusedBit]
    mov [edi+%2],cx
%%nodraw
%endmacro

%macro procpixelstwsmnt 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    mov bl,[esi+%1]
    add bl,dl
    test bl,%3
    jz %%nodraw
    mov cx,[eax+ebx*2]
    mov [edi+75036*2+%2],cx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+%2],cx
%%nodraw
%endmacro

Nextwinmode:
    push ebx
    mov ebx,[ngcwinptr]
    xor dword[ngcwinmode],1
    mov ebx,[ebx+4]
    add dword[ngcwinptr],4
    mov [ngcpixleft],ebx
    pop ebx
    ret

%macro processnextdrawwin 3
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    cmp dword[ngcwinmode],1
    je %%nodraw
    test %1,%3
    jz %%nodraw
    mov [edi+%2],%1
%%nodraw
%endmacro

%macro processwinpixel 0
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    dec dword[ngcpixleft]
    jnz %%notzero2
    call Nextwinmode
%%notzero2
%endmacro

%macro drawlinengwin16b 9
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[ng16bbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
%%returnfromcache
    %2
    ; start drawing from ecx to edi
    test word[vrama+eax],8000h
    jz %%noflipypart
    add ecx,[flipyposng]
    jmp %%flipypart
%%noflipypart
    add ecx,[yposng]
%%flipypart
    test word[vrama+eax],4000h
    jnz near %%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 0,0,%8
    %9 1,2,%8
    %9 2,4,%8
    %9 3,6,%8
    %9 4,8,%8
    %9 5,10,%8
    %9 6,12,%8
    %9 7,14,%8
    pop eax
    jmp .fin
%%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 7,0,%8
    %9 6,2,%8
    %9 5,4,%8
    %9 4,6,%8
    %9 3,8,%8
    %9 2,10,%8
    %9 1,12,%8
    %9 0,14,%8
    pop eax
    jmp .fin
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
.fin
    mov ebx,[ng16bbgval]
    add ax,2
    inc dword[bg1totng+ebx*4]
    add edi,16
    test eax,03Fh
    jz %%tileadd
%%next
    dec byte[tleftn]
    jnz near %4
    pop ebx
    cmp byte[curmosaicsz],1
    jne near domosaicng16b
    ret
%%docache
    call %3
    jmp %%returnfromcache
%%tileadd
    add ax,[bgtxadd]
    jmp %%next
%endmacro

%macro drawlineng16x16win16b 9
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[ng16bbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    add cx,[taddnfy16x16]
    test dword[vrama+eax],8000h
    jz %%noflipy
    add cx,[taddfy16x16]
%%noflipy
    test dword[vrama+eax],4000h
    jz %%noflipx
    inc cx
%%noflipx
%%nexttile
    push ecx
    push edx
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
%%returnfromcache
    %2
    ; start drawing from ecx to edi
    test word[vrama+eax],8000h
    jnz %%flipypart
    add ecx,[yposng]
    jmp %%noflipypart
%%flipypart
    add ecx,[flipyposng]
%%noflipypart
    test word[vrama+eax],4000h
    jnz near %%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 0,0,%8
    %9 1,2,%8
    %9 2,4,%8
    %9 3,6,%8
    %9 4,8,%8
    %9 5,10,%8
    %9 6,12,%8
    %9 7,14,%8
    pop eax
    jmp %%done
%%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 7,0,%8
    %9 6,2,%8
    %9 5,4,%8
    %9 4,6,%8
    %9 3,8,%8
    %9 2,10,%8
    %9 1,12,%8
    %9 0,14,%8
    pop eax
%%done
    pop edx
    pop ecx
    inc cx
    test dword[vrama+eax],4000h
    jz %%noflipxb
    sub cx,2
%%noflipxb
    add edi,16
    xor dword[switch16x16],1
    jnz near %%nexttile
%%ntile
    mov ebx,[ng16bbgval]
    add ax,2
    inc dword[bg1totng+ebx*4]
    test eax,03Fh
    jz near %%tileadd
%%next
    dec byte[tleftn]
    jnz near %4
    pop ebx
    cmp byte[curmosaicsz],1
    jne near domosaicng16b
    ret
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    add edi,32
    jmp %%ntile
%%docache
    call %3
    jmp %%returnfromcache
%%tileadd
    add ax,[bgtxadd]
    jmp %%next
%endmacro

%macro drawlinengomwin16b 9
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[cbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
%%returnfromcache
    %2
    ; start drawing from ecx to edi
    test word[vrama+eax],8000h
    jnz near %%flipypart
    add ecx,[yposng]
    jmp %%notflipypart
%%flipypart
    add ecx,[flipyposng]
%%notflipypart
    test word[vrama+eax],4000h
    jnz near %%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 0,0,%8
    %9 1,2,%8
    %9 2,4,%8
    %9 3,6,%8
    %9 4,8,%8
    %9 5,10,%8
    %9 6,12,%8
    %9 7,14,%8
    pop eax
    jmp %%endokay
%%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 7,0,%8
    %9 6,2,%8
    %9 5,4,%8
    %9 4,6,%8
    %9 3,8,%8
    %9 2,10,%8
    %9 1,12,%8
    %9 0,14,%8
    pop eax
    jmp %%endokay
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
%%endokay
    mov ebx,[cbgval]
    add word[ofsmmptr],2
    inc dword[bg1totng+ebx*4]
    add word[ofsmtptr],2
    mov ax,[ofsmmptr]
    mov ebx,[yposngom]
    mov edx,[flipyposngom]
    mov [yposng],ebx
    mov [flipyposng],edx
    add edi,16
    test eax,03Fh
    jz near %%tileadd
%%next
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    mov ecx,[ofsmval]
    add dword[ofshvaladd],8
    test dword[ebx],ecx
    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 [yposng],edx
    xor edx,38h
    mov [flipyposng],edx
%%noofsm
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    add dword[ofsmcptr2],2
    mov ecx,[ofsmval]
    and dword[ofsmcptr2],3Fh
    test dword[ebx-40h],ecx
    jz %%noofsmh
    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
%%noofsmh
    dec byte[tleftn]
    jnz near %4
%%fin
    pop ebx
    cmp byte[curmosaicsz],1
    jne near domosaicng16b
    ret
%%docache
    call %3
    jmp %%returnfromcache
%%tileadd
    mov bx,[bgtxadd]
    add ax,bx
    add [ofsmmptr],bx
    add word[ofsmtptr],bx
    jmp %%next
%endmacro

%macro parttilewin 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bl,[ecx+%1*8]
    or bl,bl
    jz %%nodraw
    add bl,dl
    mov [edi+%2*288*2],bl
%%nodraw
%endmacro

%macro tilenormalw 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwb 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwnt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbnt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwmst 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    mov [edi+%1+75036*2],bx
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbmst 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1+75036*2],bx
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwmsnt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    mov [edi+%1+75036*2],bx
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbmsnt 2
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1+75036*2],bx
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwmsbt 2
    mov bx,[ecx+%2]
    or bx,[UnusedBit]
    mov [edi+%1],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    and bx,[UnusedBitXor]
    mov [edi+%1+75036*2],bx
%%nodraw
%endmacro

%macro tilenormalwbmsbt 2
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    or bx,[UnusedBit]
    mov [edi+%1],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    and bx,[UnusedBitXor]
    mov [edi+%1+75036*2],bx
%%nodraw
%endmacro

%macro tilenormalwmsbnt 2
    mov bx,[ecx+%2]
    mov [edi+%1],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+%1+75036*2],bx
%%nodraw
%endmacro

%macro tilenormalwbmsbnt 2
    cmp bx,0FFFFh
    je %%nodraw
    mov bx,[ecx+%2]
    mov [edi+%1],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+%1+75036*2],bx
%%nodraw
%endmacro

%macro tilenormalwsmt 2
    mov bx,[ecx+%2]
    mov [edi+%1+75036*2],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbsmt 2
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1+75036*2],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    or bx,[UnusedBit]
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwsmnt 2
    mov bx,[ecx+%2]
    mov [edi+%1+75036*2],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro tilenormalwbsmnt 2
    mov bx,[ecx+%2]
    cmp bx,0FFFFh
    je %%nodraw
    mov [edi+%1+75036*2],bx
    cmp dword[ngcwinmode],1
    je %%nodraw
    mov [edi+%1],bx
%%nodraw
%endmacro

%macro drawtilengwin16b 12
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[ng16bbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
    cmp byte[%9+ecx],dl
    je %%returnfromcache
    mov byte[%9+ecx],dl
    call %10
%%returnfromcache
    cmp byte[%1+ecx],2
    je near %5
    cmp byte[%1+ecx],0
    je near %%parttile
    %2
    ; start drawing from ecx
    test dword[vrama+eax],4000h
    jz %%notflipxfull
    add ecx,128
%%notflipxfull
    test dword[vrama+eax],8000h
    jnz near %%flipyfull
    mov dword[tleftnb],8
%%loopfa
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    %11 0,0
    %11 288*2,16
    %11 288*4,16*2
    %11 288*6,16*3
    %11 288*8,16*4
    %11 288*10,16*5
    %11 288*12,16*6
    %11 288*14,16*7
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfa
    sub edi,16
    jmp %%end
%%flipyfull
    mov dword[tleftnb],8
%%loopfc
    dec dword[ngcpixleft]
    jnz %%notzeroc
    call Nextwinmode
%%notzeroc
    %11 0,16*7
    %11 288*2,16*6
    %11 288*4,16*5
    %11 288*6,16*4
    %11 288*8,16*3
    %11 288*10,16*2
    %11 288*12,16*1
    %11 288*14,0
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfc
    sub edi,16
    jmp %%end
%%parttile
    %2
    ; start drawing from ecx
    test dword[vrama+eax],4000h
    jz %%notflipxpart
    add ecx,128
%%notflipxpart
    test dword[vrama+eax],8000h
    jnz near %%flipyfullp
    mov dword[tleftnb],8
%%loopfap
    dec dword[ngcpixleft]
    jnz %%notzerop
    call Nextwinmode
%%notzerop
    %12 0,0
    %12 288*2,16
    %12 288*4,16*2
    %12 288*6,16*3
    %12 288*8,16*4
    %12 288*10,16*5
    %12 288*12,16*6
    %12 288*14,16*7
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfap
    sub edi,16
    jmp %%end
%%flipyfullp
    mov dword[tleftnb],8
%%loopfcp
    dec dword[ngcpixleft]
    jnz %%notzerocp
    call Nextwinmode
%%notzerocp
    %12 0,16*7
    %12 288*2,16*6
    %12 288*4,16*5
    %12 288*6,16*4
    %12 288*8,16*3
    %12 288*10,16*2
    %12 288*12,16*1
    %12 288*14,0
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfcp
    sub edi,16
    jmp %%end
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
%%end
    mov ebx,[ng16bbgval]
    add ax,2
    inc dword[bg1totng+ebx*4]
    add edi,16
    test eax,03Fh
    jz %%tileadd
%%next
    dec byte[tleftn]
    jnz near %4
    pop ebx
    ret
%%docache
    call %3
    mov byte[%9+ecx],dl
    call %10
    jmp %%returnfromcache
%%tileadd
    add ax,[bgtxadd]
    jmp %%next
%endmacro

%macro drawtileng16x16win16b 12
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[ng16bbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    add cx,[taddnfy16x16]
    test dword[vrama+eax],8000h
    jz %%noflipy
    add cx,[taddfy16x16]
%%noflipy
    test dword[vrama+eax],4000h
    jz %%noflipx
    inc cx
%%noflipx
%%nexttile
    push ecx
    push edx
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
    cmp byte[%9+ecx],dl
    je %%returnfromcache
    mov byte[%9+ecx],dl
    call %10
%%returnfromcache
    cmp byte[%1+ecx],2
    je near %%skip
    cmp byte[%1+ecx],0
    je near %%parttile
    %2
    ; start drawing from ecx
    test dword[vrama+eax],4000h
    jz %%notflipxfull
    add ecx,128
%%notflipxfull
    test dword[vrama+eax],8000h
    jnz near %%flipyfull
    mov dword[tleftnb],8
%%loopfa
    dec dword[ngcpixleft]
    jnz %%notzero
    call Nextwinmode
%%notzero
    %11 0,0
    %11 288*2,16
    %11 288*4,16*2
    %11 288*6,16*3
    %11 288*8,16*4
    %11 288*10,16*5
    %11 288*12,16*6
    %11 288*14,16*7
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfa
    sub edi,16
    jmp %%end
%%flipyfull
    mov dword[tleftnb],8
%%loopfc
    dec dword[ngcpixleft]
    jnz %%notzeroc
    call Nextwinmode
%%notzeroc
    %11 0,16*7
    %11 288*2,16*6
    %11 288*4,16*5
    %11 288*6,16*4
    %11 288*8,16*3
    %11 288*10,16*2
    %11 288*12,16*1
    %11 288*14,0
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfc
    sub edi,16
    jmp %%end
%%parttile
    %2
    ; start drawing from ecx
    test dword[vrama+eax],4000h
    jz %%notflipxpart
    add ecx,128
%%notflipxpart
    test dword[vrama+eax],8000h
    jnz near %%flipyfullp
    mov dword[tleftnb],8
%%loopfap
    dec dword[ngcpixleft]
    jnz %%notzerop
    call Nextwinmode
%%notzerop
    %12 0,0
    %12 288*2,16
    %12 288*4,16*2
    %12 288*6,16*3
    %12 288*8,16*4
    %12 288*10,16*5
    %12 288*12,16*6
    %12 288*14,16*7
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfap
    sub edi,16
    jmp %%end
%%flipyfullp
    mov dword[tleftnb],8
%%loopfcp
    dec dword[ngcpixleft]
    jnz %%notzerocp
    call Nextwinmode
%%notzerocp
    %12 0,16*7
    %12 288*2,16*6
    %12 288*4,16*5
    %12 288*6,16*4
    %12 288*8,16*3
    %12 288*10,16*2
    %12 288*12,16*1
    %12 288*14,0
    add ecx,2
    add edi,2
    dec dword[tleftnb]
    jnz near %%loopfcp
    sub edi,16
    jmp %%end
%%skip
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
%%end
    pop edx
    pop ecx
    inc cx
    test dword[vrama+eax],4000h
    jz %%noflipxb
    sub cx,2
%%noflipxb
    add edi,16
    xor dword[switch16x16],1
    jnz near %%nexttile
%%ntile
    mov ebx,[ng16bbgval]
    add ax,2
    inc dword[bg1totng+ebx*4]
    test eax,03Fh
    jz near %%tileadd
%%next
    dec byte[tleftn]
    jnz near %4
    pop ebx
    ret
%%done
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
    add edi,32
    jmp %%ntile
%%docache
    call %3
    mov byte[%9+ecx],dl
    call %10
    jmp %%returnfromcache
%%tileadd
    add ax,[bgtxadd]
    jmp %%next
%endmacro

%macro drawlinengomwin16b16x16 9
    ; tile value : bit 15 = flipy, bit 14 = flipx, bit 13 = priority value
    ;              bit 10-12 = palette, 0-9=tile#
    mov ebx,[cbgval]
    mov dl,ch
    inc dword[bg1drwng+ebx*4]
    and ecx,3FFh
    and edx,1Fh
    add ecx,[ngptrdat2]
    mov edx,[%6+edx*4]
    %7
    jnz near %%docache
%%returnfromcache
    jmp %%endokay
    %2
    ; start drawing from ecx to edi
    test word[vrama+eax],8000h
    jnz near %%flipypart
    add ecx,[yposng]
    jmp %%notflipypart
%%flipypart
    add ecx,[flipyposng]
%%notflipypart
    test word[vrama+eax],4000h
    jnz near %%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 0,0,%8
    %9 1,2,%8
    %9 2,4,%8
    %9 3,6,%8
    %9 4,8,%8
    %9 5,10,%8
    %9 6,12,%8
    %9 7,14,%8
    pop eax
    jmp %%endokay
%%flipxpart
    push eax
    xor ebx,ebx
    mov esi,ecx
    mov eax,[CPalPtrng]
    %9 7,0,%8
    %9 6,2,%8
    %9 5,4,%8
    %9 4,6,%8
    %9 3,8,%8
    %9 2,10,%8
    %9 1,12,%8
    %9 0,14,%8
    pop eax
    jmp %%endokay
%5
    processwinpixel
    processwinpixel
    processwinpixel
    processwinpixel
%%endokay
    mov ebx,[cbgval]
    add word[ofsmmptr],2
    inc dword[bg1totng+ebx*4]
    add word[ofsmtptr],2
    mov ax,[ofsmmptr]
    mov ebx,[yposngom]
    mov edx,[flipyposngom]
    mov [yposng],ebx
    mov [flipyposng],edx
    add edi,16
    test eax,03Fh
    jz near %%tileadd
%%next
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    mov ecx,[ofsmval]
    add dword[ofshvaladd],8
    test dword[ebx],ecx
    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 [yposng],edx
    xor edx,38h
    mov [flipyposng],edx
%%noofsm
    mov ebx,[ofsmcptr]
    add ebx,[ofsmcptr2]
    add dword[ofsmcptr2],2
    mov ecx,[ofsmval]
    and dword[ofsmcptr2],3Fh
    test dword[ebx-40h],ecx
    jz %%noofsmh
    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
%%noofsmh
    dec byte[tleftn]
    jnz near %4
%%fin
    pop ebx
    cmp byte[curmosaicsz],1
    jne near domosaicng16b
    ret
%%docache
    call %3
    jmp %%returnfromcache
%%tileadd
    mov bx,[bgtxadd]
    add ax,bx
    add [ofsmmptr],bx
    add word[ofsmtptr],bx
    jmp %%next
%endmacro
