; "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
; Ken Silverman's official web site: "http://www.advsys.net/ken"
; See the included license file "BUILDLIC.TXT" for license info.

.586P
;.8087
;include mmx.inc       ;Include this if using < WATCOM 11.0 WASM

;Warning: IN THIS FILE, ALL SEGMENTS ARE REMOVED.  THIS MEANS THAT DS:[]
;MUST BE ADDED FOR ALL SELF-MODIFIES FOR MASM TO WORK.
;
;WASM PROBLEMS:
;   1. Requires all scaled registers (*1,*2,*4,*8) to be last thing on line
;   2. Using 'DATA' is nice for self-mod. code, but WDIS only works with 'CODE'
;
;MASM PROBLEMS:
;   1. Requires DS: to be written out for self-modifying code to work
;   2. Doesn't encode short jumps automatically like WASM
;   3. Stupidly adds wait prefix to ffree's

EXTRN _asm1 : dword
EXTRN _asm2 : dword
EXTRN _asm3 : dword
EXTRN _asm4 : dword
EXTRN _reciptable : near
EXTRN _fpuasm : dword
EXTRN _globalx3 : dword
EXTRN _globaly3 : dword
EXTRN _ylookup : dword

EXTRN _vplce : near
EXTRN _vince : near
EXTRN _palookupoffse : near
EXTRN _bufplce : near

EXTRN _ebpbak : dword
EXTRN _espbak : dword

EXTRN _pow2char : near
EXTRN _pow2long : near

EXTRN _globaltilesizy : dword

; Some macros to help make cdecl calling easier to manage
CDECLBEGIN MACRO noi:=<0>
	IF noi GE 2
	push ebx
	ENDIF
	IF noi GE 3
	push ecx
	ENDIF
	IF noi GE 4
	push edx
	ENDIF
	IF noi GE 5
	push esi
	ENDIF
	IF noi GE 6
	push edi
	ENDIF
ENDM

CDECLEND MACRO noi:=<0>
	IF noi GE 6
	pop edi
	ENDIF
	IF noi GE 5
	pop esi
	ENDIF
	IF noi GE 4
	pop edx
	ENDIF
	IF noi GE 3
	pop ecx
	ENDIF
	IF noi GE 2
	pop ebx
	ENDIF
ENDM

CDECLPARAM MACRO registername:REQ,paramnumber:REQ,offsetted:REQ
;	mov registername, dword ptr [esp + (paramnumber*4+16)]
	mov registername, dword ptr [esp + ((paramnumber+offsetted)*4+4)]
ENDM

CDECLBEGINSET MACRO numparams:REQ
	CDECLBEGIN numparams

%	_offsetted = numparams - 1

	IF numparams GE 1
	CDECLPARAM eax,0,%_offsetted
	ENDIF
	IF numparams GE 2
	CDECLPARAM ebx,1,%_offsetted
	ENDIF
	IF numparams GE 3
	CDECLPARAM ecx,2,%_offsetted
	ENDIF
	IF numparams GE 4
	CDECLPARAM edx,3,%_offsetted
	ENDIF
	IF numparams GE 5
	CDECLPARAM esi,4,%_offsetted
	ENDIF
	IF numparams GE 6
	CDECLPARAM edi,5,%_offsetted
	ENDIF
ENDM

CDECLENDSET MACRO numparams:REQ
	CDECLEND numparams
ENDM

code segment 'CODE' PUBLIC USE32
ASSUME cs:CODE,ds:CODE
PUBLIC _dep_begin
_dep_begin:

ALIGN 16
PUBLIC _sethlinesizes
_sethlinesizes:
CDECLBEGINSET 3

	mov byte ptr [machxbits1+2], al
	mov byte ptr [machxbits2+2], al
	mov byte ptr [machxbits3+2], al
	neg al
	mov byte ptr [hxsiz1+2], al
	mov byte ptr [hxsiz2+2], al
	mov byte ptr [hxsiz3+2], al
	mov byte ptr [hxsiz4+2], al
	mov byte ptr [machnegxbits1+2], al

	mov byte ptr [hysiz1+3], bl
	mov byte ptr [hysiz2+3], bl
	mov byte ptr [hysiz3+3], bl
	mov byte ptr [hysiz4+3], bl
	mov byte ptr [hmach3a+2], bl
	mov byte ptr [hmach3b+2], bl
	mov byte ptr [hmach3c+2], bl
	mov byte ptr [hmach3d+2], bl

	mov dword ptr [hoffs1+2], ecx
	mov dword ptr [hoffs2+2], ecx
	mov dword ptr [hoffs3+2], ecx
	mov dword ptr [hoffs4+2], ecx
	mov dword ptr [hoffs5+2], ecx
	mov dword ptr [hoffs6+2], ecx
	mov dword ptr [hoffs7+2], ecx
	mov dword ptr [hoffs8+2], ecx

	push edx	;JBF
	mov edx, -1
	mov cl, al
	sub cl, bl
	shr edx, cl
	mov dword ptr [hmach2a+1], edx
	mov dword ptr [hmach2b+1], edx
	mov dword ptr [hmach2c+1], edx
	mov dword ptr [hmach2d+1], edx
	pop edx		;JBF

CDECLENDSET 3
	ret

ALIGN 16
PUBLIC _prosethlinesizes
_prosethlinesizes:
CDECLBEGINSET 3

	mov dword ptr [prohbuf-4], ecx
	neg eax
	mov ecx, eax
	sub eax, ebx
	mov byte ptr [prohshru-1], al   ;bl = 32-al-bl
	mov eax, -1
	shr eax, cl
	mov ecx, ebx
	shl eax, cl
	mov dword ptr [prohand-4], eax  ;((-1>>(-oal))<<obl)
	neg ebx
	mov byte ptr [prohshrv-1], bl   ;bl = 32-bl

CDECLENDSET 3
	ret

ALIGN 16
PUBLIC _setvlinebpl
_setvlinebpl:
CDECLBEGINSET 1
	mov dword ptr [np2_fixchain1a+2], eax
	mov dword ptr [np2_fixchain1b+2], eax

	mov dword ptr [mnp2_fixchain1b+2], eax
	mov dword ptr [tnp2_fixchain1b+2], eax

	mov dword ptr [fixchain1a+2], eax
	mov dword ptr [fixchain1b+2], eax
	mov dword ptr [fixchain1m+2], eax
	mov dword ptr [fixchain1t+2], eax
	mov dword ptr [fixchain1s+2], eax
	mov dword ptr [mfixchain1s+2], eax
	mov dword ptr [tfixchain1s+2], eax
	mov dword ptr [fixchain2a+2], eax
	mov dword ptr [profixchain2a+2], eax
	mov dword ptr [fixchain2ma+2], eax
	mov dword ptr [fixchain2mb+2], eax
	mov dword ptr [fixchaint2a+1], eax
	mov dword ptr [fixchaint2b+2], eax
	mov dword ptr [fixchaint2c+2], eax
	mov dword ptr [fixchaint2d+2], eax
	mov dword ptr [fixchaint2e+2], eax
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _setpalookupaddress
_setpalookupaddress:
CDECLBEGINSET 1
	mov dword ptr [pal1+2], eax
	mov dword ptr [pal2+2], eax
	mov dword ptr [pal3+2], eax
	mov dword ptr [pal4+2], eax
	mov dword ptr [pal5+2], eax
	mov dword ptr [pal6+2], eax
	mov dword ptr [pal7+2], eax
	mov dword ptr [pal8+2], eax
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _prosetpalookupaddress
_prosetpalookupaddress:
CDECLBEGINSET 1
	mov dword ptr [prohpala-4], eax
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _setuphlineasm4
_setuphlineasm4:
CDECLBEGINSET 2

machxbits3: rol eax, 6                     ;xbits
	mov dword ptr [hmach4a+2], eax
	mov dword ptr [hmach4b+2], eax
	mov bl, al
	mov dword ptr [hmach4c+2], eax
	mov dword ptr [hmach4d+2], eax
	mov dword ptr [hmach1a+2], ebx
	mov dword ptr [hmach1b+2], ebx
	mov dword ptr [hmach1c+2], ebx
	mov dword ptr [hmach1d+2], ebx

CDECLENDSET 2
	ret

	;Non-256-stuffed ceiling&floor method with NO SHLD!:
	;yinc&0xffffff00   lea eax, [edx+88888800h]           1     1/2
	;ybits...xbits     and edx, 88000088h                 1     1/2
	;ybits             rol edx, 6                         2     1/2
	;xinc<<xbits       add esi, 88888888h                 1     1/2
	;xinc>>(32-xbits)  adc al, 88h                        1     1/2
	;bufplc            mov cl, byte ptr [edx+88888888h]   1     1/2
	;paloffs&255       mov bl, byte ptr [ecx+88888888h]   1     1/2
ALIGN 16
PUBLIC _hlineasm4
_hlineasm4:
CDECLBEGINSET 6

	push ebp

	lea ebp, [eax+1]

	cmp ebp, 8
	jle shorthline

	test edi, 1
	jnz short skipthe1byte

	mov eax, esi
hxsiz1: shr eax, 26
hysiz1: shld eax, edx, 6
hoffs1: mov cl, byte ptr [eax+88888888h]
pal1: mov bl, byte ptr [ecx+88888888h]
	sub esi, _asm1
	sub edx, _asm2
	mov byte ptr [edi], bl
	dec edi
	dec ebp

skipthe1byte:
	test edi, 2
	jnz short skipthe2byte

	mov eax, esi
hxsiz2: shr eax, 26
hysiz2: shld eax, edx, 6
hoffs2: mov cl, byte ptr [eax+88888888h]
pal2: mov bh, byte ptr [ecx+88888888h]
	sub esi, _asm1
	sub edx, _asm2

	mov eax, esi
hxsiz3: shr eax, 26
hysiz3: shld eax, edx, 6
hoffs3: mov cl, byte ptr [eax+88888888h]
pal3: mov bl, byte ptr [ecx+88888888h]
	sub esi, _asm1
	sub edx, _asm2
	mov word ptr [edi-1], bx
	sub edi, 2
	sub ebp, 2

skipthe2byte:

	mov eax, esi
machxbits1: shl esi, 6                     ;xbits
machnegxbits1: shr eax, 32-6               ;32-xbits
	mov dl, al

	inc edi

	add ebx, ebx
	mov eax, edx
	jc beginhline64

	mov eax, _asm1
machxbits2: rol eax, 6                     ;xbits
	mov dword ptr [hmach4a+2], eax
	mov dword ptr [hmach4b+2], eax
	mov dword ptr [hmach4c+2], eax
	mov dword ptr [hmach4d+2], eax
	mov ebx, eax
	mov eax, _asm2
	mov al, bl
	mov dword ptr [hmach1a+2], eax
	mov dword ptr [hmach1b+2], eax
	mov dword ptr [hmach1c+2], eax
	mov dword ptr [hmach1d+2], eax

	mov eax, edx
	jmp beginhline64
ALIGN 16
prebeginhline64:
	mov dword ptr [edi], ebx
beginhline64:

hmach3a: rol eax, 6
hmach2a: and eax, 00008888h
hmach4a: sub esi, 88888888h
hmach1a: sbb edx, 88888888h
	sub edi, 4
hoffs4: mov cl, byte ptr [eax+88888888h]
	mov eax, edx

hmach3b: rol eax, 6
hmach2b: and eax, 00008888h
hmach4b: sub esi, 88888888h
hmach1b: sbb edx, 88888888h
pal4: mov bh, byte ptr [ecx+88888888h]
hoffs5: mov cl, byte ptr [eax+88888888h]
	mov eax, edx

hmach3c: rol eax, 6
pal5: mov bl, byte ptr [ecx+88888888h]
hmach2c: and eax, 00008888h
	shl ebx, 16
hmach4c: sub esi, 88888888h
hmach1c: sbb edx, 88888888h
hoffs6: mov cl, byte ptr [eax+88888888h]

	mov eax, edx
	;(

hmach3d: rol eax, 6
hmach2d: and eax, 00008888h
hmach4d: sub esi, 88888888h
hmach1d: sbb edx, 88888888h
pal6: mov bh, byte ptr [ecx+88888888h]
hoffs7: mov cl, byte ptr [eax+88888888h]
	mov eax, edx
	sub ebp, 4
	nop
pal7: mov bl, byte ptr [ecx+88888888h]
	jnc prebeginhline64
skipthe4byte:

	test ebp, 2
	jz skipdrawthe2
	rol ebx, 16
	mov word ptr [edi+2], bx
	sub edi, 2
skipdrawthe2:
	test ebp, 1
	jz skipdrawthe1
	shr ebx, 24
	mov byte ptr [edi+3], bl
skipdrawthe1:

	pop ebp
CDECLENDSET 6
	ret

shorthline:
	test ebp, ebp
	jz endshorthline
begshorthline:
	mov eax, esi
hxsiz4: shr eax, 26
hysiz4: shld eax, edx, 6
hoffs8: mov cl, byte ptr [eax+88888888h]
pal8: mov bl, byte ptr [ecx+88888888h]
	sub esi, _asm1
	sub edx, _asm2
	mov byte ptr [edi], bl
	dec edi
	dec ebp
	jnz begshorthline
endshorthline:
	pop ebp
CDECLENDSET 6
	ret


	;eax: 00000000 00000000 00000000 temp----
	;ebx: 00000000 00000000 00000000 temp----
	;ecx: UUUUUUuu uuuuuuuu uuuuuuuu uuuuuuuu
	;edx: VVVVVVvv vvvvvvvv vvvvvvvv vvvvvvvv
	;esi: cnt----- -------- -------- --------
	;edi: vid----- -------- -------- --------
	;ebp: paloffs- -------- -------- --------
	;esp: ???????? ???????? ???????? ????????
ALIGN 16
PUBLIC _prohlineasm4
_prohlineasm4:
CDECLBEGINSET 6

	push ebp

	lea ebp, [ecx+88888888h]
prohpala:
	mov ecx, esi
	lea esi, [eax+1]
	sub edi, esi

prohbeg:
	mov eax, ecx
	shr eax, 20
prohshru:
	mov ebx, edx
	shr ebx, 26
prohshrv:
	and eax, 88888888h
prohand:
	movzx eax, byte ptr [eax+ebx+88888888h]
prohbuf:
	mov al, [eax+ebp]
	sub ecx, _asm1
	sub edx, _asm2
	mov [edi+esi], al
	dec esi
	jnz prohbeg

	pop ebp
CDECLENDSET 6
	ret



ALIGN 16
PUBLIC _setupvlineasm
_setupvlineasm:
CDECLBEGINSET 1
		;First 2 lines for VLINEASM1, rest for VLINEASM4
	mov byte ptr [premach3a+2], al
	mov byte ptr [mach3a+2], al

	push ecx
	mov byte ptr [machvsh1+2], al      ;32-shy
	mov byte ptr [machvsh3+2], al      ;32-shy
	mov byte ptr [machvsh5+2], al      ;32-shy
	mov byte ptr [machvsh6+2], al      ;32-shy
	mov ah, al
	sub ah, 16
	mov byte ptr [machvsh8+2], ah      ;16-shy
	neg al
	mov byte ptr [machvsh7+2], al      ;shy
	mov byte ptr [machvsh9+2], al      ;shy
	mov byte ptr [machvsh10+2], al     ;shy
	mov byte ptr [machvsh11+2], al     ;shy
	mov byte ptr [machvsh12+2], al     ;shy
	mov cl, al
	mov eax, 1
	shl eax, cl
	dec eax
	mov dword ptr [machvsh2+2], eax    ;(1<<shy)-1
	mov dword ptr [machvsh4+2], eax    ;(1<<shy)-1
	pop ecx
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _prosetupvlineasm
_prosetupvlineasm:
CDECLBEGINSET 1
		;First 2 lines for VLINEASM1, rest for VLINEASM4
	mov byte ptr [premach3a+2], al
	mov byte ptr [mach3a+2], al

	push ecx
	mov byte ptr [promachvsh1+2], al      ;32-shy
	mov byte ptr [promachvsh3+2], al      ;32-shy
	mov byte ptr [promachvsh5+2], al      ;32-shy
	mov byte ptr [promachvsh6+2], al      ;32-shy
	mov ah, al
	sub ah, 16
	mov byte ptr [promachvsh8+2], ah      ;16-shy
	neg al
	mov byte ptr [promachvsh7+2], al      ;shy
	mov byte ptr [promachvsh9+2], al      ;shy
	mov byte ptr [promachvsh10+2], al     ;shy
	mov byte ptr [promachvsh11+2], al     ;shy
	mov byte ptr [promachvsh12+2], al     ;shy
	mov cl, al
	mov eax, 1
	shl eax, cl
	dec eax
	mov dword ptr [promachvsh2+2], eax    ;(1<<shy)-1
	mov dword ptr [promachvsh4+2], eax    ;(1<<shy)-1
	pop ecx
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _setupmvlineasm
_setupmvlineasm:
CDECLBEGINSET 1
	mov byte ptr [maskmach3a+2], al
	mov byte ptr [machmv13+2], al
	mov byte ptr [machmv14+2], al
	mov byte ptr [machmv15+2], al
	mov byte ptr [machmv16+2], al
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _setuptvlineasm
_setuptvlineasm:
CDECLBEGINSET 1
	mov byte ptr [transmach3a+2], al
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _prevlineasm1
_prevlineasm1:
CDECLBEGINSET 6
	test ecx, ecx
	jnz short _vlineasm1_nosetup

	add eax, edx
premach3a: shr edx, 32
	mov dl, byte ptr [esi+edx]
	mov cl, byte ptr [ebx+edx]
	mov byte ptr [edi], cl
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _vlineasm1
_vlineasm1:
CDECLBEGINSET 6
_vlineasm1_nosetup:
	push ebp
	mov ebp, ebx
	inc ecx
fixchain1a: sub edi, 320
beginvline:
	mov ebx, edx
mach3a: shr ebx, 32
fixchain1b: add edi, 320
	mov bl, byte ptr [esi+ebx]
	add edx, eax
	dec ecx
	mov bl, byte ptr [ebp+ebx]
	mov byte ptr [edi], bl
	jnz short beginvline
	pop ebp
	mov eax, edx
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _vlineasm1nonpow2
_vlineasm1nonpow2:
CDECLBEGINSET 6
	mov dword ptr [np2_do_palookup+2], ebx
	push ebp
	mov ebp, edx  ; ebp: vertical place
	mov ebx, eax  ; ebx: vertical increment
	mov eax, dword ptr [_globaltilesizy]
	mov dword ptr [np2_beginvline+1], eax
	inc ecx
np2_fixchain1a: sub edi, 320
np2_beginvline: ; +1: y tile size
	mov eax, 00000123h
	mul ebp
np2_fixchain1b: add edi, 320
	and eax, 000000ffh
	mov al, byte ptr [esi+edx]
	add ebp, ebx
	dec ecx
np2_do_palookup: ; +2: addr
	mov al, byte ptr [eax+0000beefh]
	mov byte ptr [edi], al
	jnz short np2_beginvline
	mov eax, ebp
	pop ebp
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _mvlineasm1      ;Masked vline
_mvlineasm1:
CDECLBEGINSET 6
	push ebp
	mov ebp, ebx
beginmvline:
	mov ebx, edx
maskmach3a: shr ebx, 32
	mov bl, byte ptr [esi+ebx]
	cmp bl, 255
	je short skipmask1
maskmach3c: mov bl, [ebp+ebx]
	mov byte ptr [edi], bl
skipmask1:
	add edx, eax
fixchain1m: add edi, 320
	sub ecx, 1
	jnc short beginmvline

	pop ebp
	mov eax, edx
CDECLENDSET 6
	ret

	ALIGN 16
PUBLIC _mvlineasm1nonpow2
_mvlineasm1nonpow2:
CDECLBEGINSET 6
	mov dword ptr [mnp2_do_palookup+2], ebx
	push ebp
	mov ebp, edx  ; ebp: vertical place
	mov ebx, eax  ; ebx: vertical increment
	mov eax, dword ptr [_globaltilesizy]
	mov dword ptr [mnp2_beginvline+1], eax
mnp2_beginvline: ; +1: y tile size
	mov eax, 00000123h
	mul ebp
	and eax, 000000ffh
	mov al, byte ptr [esi+edx]
	cmp al, 255
	je short mskipmask1
mnp2_do_palookup: ; +2: addr
	mov al, byte ptr [eax+0000beefh]
	mov byte ptr [edi], al
mskipmask1:
	add ebp, ebx
mnp2_fixchain1b: add edi, 320
	sub ecx, 1
	jnc short mnp2_beginvline
	mov eax, ebp
	pop ebp
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _fixtransluscence
_fixtransluscence:
CDECLBEGINSET 1
	mov dword ptr [transmach4+2], eax
	mov dword ptr [tmach1+2], eax
	mov dword ptr [tmach2+2], eax
	mov dword ptr [tmach3+2], eax
	mov dword ptr [tmach4+2], eax
	mov dword ptr [tran2traa+2], eax
	mov dword ptr [tran2trab+2], eax
	mov dword ptr [tran2trac+2], eax
	mov dword ptr [tran2trad+2], eax

	mov dword ptr [tnmach4+2], eax
CDECLENDSET 1
	ret

ALIGN 16
PUBLIC _settransnormal
_settransnormal:
	mov byte ptr [transrev0+1], 83h
	mov byte ptr [transrev1+1], 27h
	mov byte ptr [transrev2+1], 3fh
	mov byte ptr [transrev3+1], 98h
	mov byte ptr [transrev4+1], 90h
	mov byte ptr [transrev5+1], 37h
	mov byte ptr [transrev6+1], 90h
	mov word ptr [transrev7+0], 0f38ah
	mov byte ptr [transrev8+1], 90h
	mov word ptr [transrev9+0], 0f78ah
	mov byte ptr [transrev10+1], 0a7h
	mov byte ptr [transrev11+1], 81h
	mov byte ptr [transrev12+2], 9fh
	mov word ptr [transrev13+0], 0dc88h
	mov byte ptr [transrev14+1], 81h
	mov byte ptr [transrev15+1], 9ah
	mov byte ptr [transrev16+1], 0a7h
	mov byte ptr [transrev17+1], 82h

	mov byte ptr [ntransrev0+1], 80h
	mov byte ptr [ntransrev1+1], 27h

	ret

ALIGN 16
PUBLIC _settransreverse
_settransreverse:
	mov byte ptr [transrev0+1], 0a3h
	mov byte ptr [transrev1+1], 7h
	mov byte ptr [transrev2+1], 1fh
	mov byte ptr [transrev3+1], 0b8h
	mov byte ptr [transrev4+1], 0b0h
	mov byte ptr [transrev5+1], 17h
	mov byte ptr [transrev6+1], 0b0h
	mov word ptr [transrev7+0], 0d38ah
	mov byte ptr [transrev8+1], 0b0h
	mov word ptr [transrev9+0], 0d78ah
	mov byte ptr [transrev10+1], 87h
	mov byte ptr [transrev11+1], 0a1h
	mov byte ptr [transrev12+2], 87h
	mov word ptr [transrev13+0], 0e388h
	mov byte ptr [transrev14+1], 0a1h
	mov byte ptr [transrev15+1], 0bah
	mov byte ptr [transrev16+1], 87h
	mov byte ptr [transrev17+1], 0a2h

	mov byte ptr [ntransrev0+1], 0a0h
	mov byte ptr [ntransrev1+1], 7h

	ret

ALIGN 16
PUBLIC _tvlineasm1        ;Masked & transluscent vline
_tvlineasm1:
CDECLBEGINSET 6
	push ebp
	mov ebp, eax
	xor eax, eax
	inc ecx
	mov dword ptr [transmach3c+2], ebx
	jmp short begintvline
ALIGN 16
begintvline:
	mov ebx, edx
transmach3a: shr ebx, 32
	mov bl, byte ptr [esi+ebx]
	cmp bl, 255
	je short skiptrans1
transrev0:
transmach3c: mov al, [ebx+88888888h]
transrev1:
	mov ah, byte ptr [edi]
transmach4: mov al, byte ptr [eax+88888888h]   ;_transluc[eax]
	mov byte ptr [edi], al
skiptrans1:
	add edx, ebp
fixchain1t: add edi, 320
	dec ecx
	jnz short begintvline

	pop ebp
	mov eax, edx
CDECLENDSET 6
	ret

	ALIGN 16
PUBLIC _tvlineasm1nonpow2
_tvlineasm1nonpow2:
CDECLBEGINSET 6
	mov dword ptr [tnp2_do_palookup+2], ebx
	push ebp
	mov ebp, edx  ; ebp: vertical place
	mov ebx, eax  ; ebx: vertical increment
	inc ecx
	mov eax, dword ptr [_globaltilesizy]
	mov dword ptr [tnp2_beginvline+1], eax
tnp2_beginvline: ; +1: y tile size
	mov eax, 00000123h
	mul ebp
	and eax, 000000ffh
	mov al, byte ptr [esi+edx]
	cmp al, 255
	je short tskipmask1
ntransrev0:
tnp2_do_palookup: ; +2: addr
	mov al, byte ptr [eax+0000beefh]
ntransrev1:
	mov ah, byte ptr [edi]
tnmach4: mov al, byte ptr [eax+88888888h]	;_luc[eax]
	mov byte ptr [edi], al
tskipmask1:
	add ebp, ebx
tnp2_fixchain1b: add edi, 320
	dec ecx
	jnz short tnp2_beginvline
	mov eax, ebp
	pop ebp
CDECLENDSET 6
	ret

	;eax: -------temp1-------
	;ebx: -------temp2-------
	;ecx:  dat  dat  dat  dat
	;edx: ylo2           ylo4
	;esi: yhi1           yhi2
	;edi: ---videoplc/cnt----
	;ebp: yhi3           yhi4
	;esp:
ALIGN 16
PUBLIC _vlineasm4
_vlineasm4:
CDECLBEGIN 6
CDECLPARAM ecx,0,5
CDECLPARAM edi,1,5

	push ebp

	mov eax, _ylookup
	mov eax, [eax+ecx*4]
	add eax, edi
	mov dword ptr [machvline4end+2], eax
	sub edi, eax

	mov eax, dword ptr _bufplce[0]
	mov ebx, dword ptr _bufplce[4]
	mov ecx, dword ptr _bufplce[8]
	mov edx, dword ptr _bufplce[12]
	mov dword ptr [machvbuf1+2], ecx
	mov dword ptr [machvbuf2+2], edx
	mov dword ptr [machvbuf3+2], eax
	mov dword ptr [machvbuf4+2], ebx

	mov eax, dword ptr _palookupoffse[0]
	mov ebx, dword ptr _palookupoffse[4]
	mov ecx, dword ptr _palookupoffse[8]
	mov edx, dword ptr _palookupoffse[12]
	mov dword ptr [machvpal1+2], ecx
	mov dword ptr [machvpal2+2], edx
	mov dword ptr [machvpal3+2], eax
	mov dword ptr [machvpal4+2], ebx

		;     +---------------+---------------+
		;edx: |v3lo           |v1lo           |
		;     +---------------+-------+-------+
		;esi: |v2hi  v2lo             |   v3hi|
		;     +-----------------------+-------+
		;ebp: |v0hi  v0lo             |   v1hi|
		;     +-----------------------+-------+

	mov ebp, dword ptr _vince[0]
	mov ebx, dword ptr _vince[4]
	mov esi, dword ptr _vince[8]
	mov eax, dword ptr _vince[12]
	and esi, 0fffffe00h
	and ebp, 0fffffe00h
machvsh9: rol eax, 88h                 ;sh
machvsh10: rol ebx, 88h                ;sh
	mov edx, eax
	mov ecx, ebx
	shr ecx, 16
	and edx, 0ffff0000h
	add edx, ecx
	and eax, 000001ffh
	and ebx, 000001ffh
	add esi, eax
	add ebp, ebx
	;
	mov eax, edx
	and eax, 0ffff0000h
	mov dword ptr [machvinc1+2], eax
	mov dword ptr [machvinc2+2], esi
	mov byte ptr [machvinc3+2], dl
	mov byte ptr [machvinc4+2], dh
	mov dword ptr [machvinc5+2], ebp

	mov ebp, dword ptr _vplce[0]
	mov ebx, dword ptr _vplce[4]
	mov esi, dword ptr _vplce[8]
	mov eax, dword ptr _vplce[12]
	and esi, 0fffffe00h
	and ebp, 0fffffe00h
machvsh11: rol eax, 88h                ;sh
machvsh12: rol ebx, 88h                ;sh
	mov edx, eax
	mov ecx, ebx
	shr ecx, 16
	and edx, 0ffff0000h
	add edx, ecx
	and eax, 000001ffh
	and ebx, 000001ffh
	add esi, eax
	add ebp, ebx

	mov ecx, esi
	jmp short beginvlineasm4
ALIGN 16
	nop
	nop
	nop
beginvlineasm4:
machvsh1: shr ecx, 88h          ;32-sh
	mov ebx, esi
machvsh2: and ebx, 00000088h    ;(1<<sh)-1
machvinc1: add edx, 88880000h
machvinc2: adc esi, 88888088h
machvbuf1: mov cl, byte ptr [ecx+88888888h]
machvbuf2: mov bl, byte ptr [ebx+88888888h]
	mov eax, ebp
machvsh3: shr eax, 88h          ;32-sh
machvpal1: mov cl, byte ptr [ecx+88888888h]
machvpal2: mov ch, byte ptr [ebx+88888888h]
	mov ebx, ebp
	shl ecx, 16
machvsh4: and ebx, 00000088h    ;(1<<sh)-1
machvinc3: add dl, 88h
machvbuf3: mov al, byte ptr [eax+88888888h]
machvinc4: adc dh, 88h
machvbuf4: mov bl, byte ptr [ebx+88888888h]
machvinc5: adc ebp, 88888088h
machvpal3: mov cl, byte ptr [eax+88888888h]
machvpal4: mov ch, byte ptr [ebx+88888888h]
machvline4end: mov dword ptr [edi+88888888h], ecx
fixchain2a: add edi, 88888888h
	mov ecx, esi
	jnc short beginvlineasm4

		;     +---------------+---------------+
		;edx: |v3lo           |v1lo           |
		;     +---------------+-------+-------+
		;esi: |v2hi  v2lo             |   v3hi|
		;     +-----------------------+-------+
		;ebp: |v0hi  v0lo             |   v1hi|
		;     +-----------------------+-------+

	mov dword ptr _vplce[8], esi
	mov dword ptr _vplce[0], ebp
		;vplc2 = (esi<<(32-sh))+(edx>>sh)
		;vplc3 = (ebp<<(32-sh))+((edx&65535)<<(16-sh))
machvsh5: shl esi, 88h     ;32-sh
	mov eax, edx
machvsh6: shl ebp, 88h     ;32-sh
	and edx, 0000ffffh
machvsh7: shr eax, 88h     ;sh
	add esi, eax
machvsh8: shl edx, 88h     ;16-sh
	add ebp, edx
	mov dword ptr _vplce[12], esi
	mov dword ptr _vplce[4], ebp

	pop ebp

CDECLEND 6
	ret

	;eax: -------temp1-------
	;ebx: -------temp2-------
	;ecx: ylo4      ---------
	;edx: ylo2      ---------
	;esi: yhi1           yhi2
	;edi: ---videoplc/cnt----
	;ebp: yhi3           yhi4
	;esp:
ALIGN 16
PUBLIC _provlineasm4
_provlineasm4:
CDECLBEGIN 6
CDECLPARAM ecx,0,5
CDECLPARAM edi,1,5

	push ebp

	mov eax, _ylookup
	mov eax, [eax+ecx*4]
	add eax, edi
	mov dword ptr [promachvline4end1+2], eax
	inc eax
	mov dword ptr [promachvline4end2+2], eax
	inc eax
	mov dword ptr [promachvline4end3+2], eax
	inc eax
	mov dword ptr [promachvline4end4+2], eax
	sub eax, 3
	sub edi, eax

	mov eax, dword ptr _bufplce[0]
	mov ebx, dword ptr _bufplce[4]
	mov ecx, dword ptr _bufplce[8]
	mov edx, dword ptr _bufplce[12]
	mov dword ptr [promachvbuf1+3], ecx
	mov dword ptr [promachvbuf2+3], edx
	mov dword ptr [promachvbuf3+3], eax
	mov dword ptr [promachvbuf4+3], ebx

	mov eax, dword ptr _palookupoffse[0]
	mov ebx, dword ptr _palookupoffse[4]
	mov ecx, dword ptr _palookupoffse[8]
	mov edx, dword ptr _palookupoffse[12]
	mov dword ptr [promachvpal1+2], ecx
	mov dword ptr [promachvpal2+2], edx
	mov dword ptr [promachvpal3+2], eax
	mov dword ptr [promachvpal4+2], ebx

		;     +---------------+---------------+
		;edx: |v3lo           |v1lo           |
		;     +---------------+-------+-------+
		;esi: |v2hi  v2lo             |   v3hi|
		;     +-----------------------+-------+
		;ebp: |v0hi  v0lo             |   v1hi|
		;     +-----------------------+-------+

	mov ebp, dword ptr _vince[0]
	mov ebx, dword ptr _vince[4]
	mov esi, dword ptr _vince[8]
	mov eax, dword ptr _vince[12]
	and esi, 0fffffe00h
	and ebp, 0fffffe00h
promachvsh9: rol eax, 88h                 ;sh
promachvsh10: rol ebx, 88h                ;sh
	mov edx, eax
	mov ecx, ebx
	shr ecx, 16
	and edx, 0ffff0000h
	add edx, ecx
	and eax, 000001ffh
	and ebx, 000001ffh
	add esi, eax
	add ebp, ebx
	;
	mov eax, edx
	and eax, 0ffff0000h
	mov dword ptr [promachvinc1+2], eax
	mov dword ptr [promachvinc2+2], esi
	shl edx, 16
	mov dword ptr [promachvinc3+2], edx
	mov dword ptr [promachvinc5+2], ebp

	mov ebp, dword ptr _vplce[0]
	mov ebx, dword ptr _vplce[4]
	mov esi, dword ptr _vplce[8]
	mov eax, dword ptr _vplce[12]
	and esi, 0fffffe00h
	and ebp, 0fffffe00h
promachvsh11: rol eax, 88h                ;sh
promachvsh12: rol ebx, 88h                ;sh
	mov edx, eax
	mov ecx, ebx
	shr ecx, 16
	and edx, 0ffff0000h
	add edx, ecx
	and eax, 000001ffh
	and ebx, 000001ffh
	add esi, eax
	add ebp, ebx

	mov eax, esi
	mov ecx, edx
	shl ecx, 16
	jmp short probeginvlineasm4
ALIGN 16
	nop
	nop
	nop
probeginvlineasm4:
promachvsh1: shr eax, 88h          ;32-sh
	mov ebx, esi
promachvsh2: and ebx, 00000088h    ;(1<<sh)-1
promachvinc1: add edx, 88880000h
promachvinc2: adc esi, 88888088h
promachvbuf1: movzx eax, byte ptr [eax+88888888h]
promachvbuf2: movzx ebx, byte ptr [ebx+88888888h]
promachvpal1: mov al, byte ptr [eax+88888888h]
promachvline4end3: mov byte ptr [edi+88888888h], al
	mov eax, ebp
promachvsh3: shr eax, 88h          ;32-sh
promachvpal2: mov bl, byte ptr [ebx+88888888h]
promachvline4end4: mov byte ptr [edi+88888888h], bl
	mov ebx, ebp
promachvsh4: and ebx, 00000088h    ;(1<<sh)-1
promachvbuf3: movzx eax, byte ptr [eax+88888888h]
promachvinc3: add ecx, 88888888h
promachvbuf4: movzx ebx, byte ptr [ebx+88888888h]
promachvinc5: adc ebp, 88888088h
promachvpal3: mov al, byte ptr [eax+88888888h]
promachvline4end1: mov byte ptr [edi+88888888h], al
promachvpal4: mov bl, byte ptr [ebx+88888888h]
promachvline4end2: mov byte ptr [edi+88888888h], bl
profixchain2a: add edi, 88888888h
	mov eax, esi
	jnc probeginvlineasm4

		;     +---------------+---------------+
		;edx: |v3lo           |v1lo           |
		;     +---------------+-------+-------+
		;esi: |v2hi  v2lo             |   v3hi|
		;     +-----------------------+-------+
		;ebp: |v0hi  v0lo             |   v1hi|
		;     +-----------------------+-------+

	mov dword ptr _vplce[8], esi
	mov dword ptr _vplce[0], ebp
		;vplc2 = (esi<<(32-sh))+(edx>>sh)
		;vplc3 = (ebp<<(32-sh))+((edx&65535)<<(16-sh))
promachvsh5: shl esi, 88h     ;32-sh
	mov eax, edx
promachvsh6: shl ebp, 88h     ;32-sh
	and edx, 0000ffffh
promachvsh7: shr eax, 88h     ;sh
	add esi, eax
promachvsh8: shl edx, 88h     ;16-sh
	add ebp, edx
	mov dword ptr _vplce[12], esi
	mov dword ptr _vplce[4], ebp

	pop ebp

CDECLEND 6
	ret


ALIGN 16
PUBLIC _mvlineasm4
_mvlineasm4:
CDECLBEGIN 6
CDECLPARAM ecx,0,5
CDECLPARAM edi,1,5

	push ebp

	mov eax, dword ptr _bufplce[0]
	mov ebx, dword ptr _bufplce[4]
	mov dword ptr [machmv1+2], eax
	mov dword ptr [machmv4+2], ebx
	mov eax, dword ptr _bufplce[8]
	mov ebx, dword ptr _bufplce[12]
	mov dword ptr [machmv7+2], eax
	mov dword ptr [machmv10+2], ebx

	mov eax, dword ptr _palookupoffse[0]
	mov ebx, dword ptr _palookupoffse[4]
	mov dword ptr [machmv2+2], eax
	mov dword ptr [machmv5+2], ebx
	mov eax, dword ptr _palookupoffse[8]
	mov ebx, dword ptr _palookupoffse[12]
	mov dword ptr [machmv8+2], eax
	mov dword ptr [machmv11+2], ebx

	mov eax, dword ptr _vince[0]        ;vince
	mov ebx, dword ptr _vince[4]
	xor al, al
	xor bl, bl
	mov dword ptr [machmv3+2], eax
	mov dword ptr [machmv6+2], ebx
	mov eax, dword ptr _vince[8]
	mov ebx, dword ptr _vince[12]
	mov dword ptr [machmv9+2], eax
	mov dword ptr [machmv12+2], ebx

	mov ebx, ecx
	mov ecx, dword ptr _vplce[0]
	mov edx, dword ptr _vplce[4]
	mov esi, dword ptr _vplce[8]
	mov ebp, dword ptr _vplce[12]
	mov cl, bl
	inc cl
	inc bh
	mov byte ptr _asm3[0], bh
fixchain2ma: sub edi, 320

	jmp short beginmvlineasm4
ALIGN 16
beginmvlineasm4:
	dec cl
	jz endmvlineasm4
beginmvlineasm42:
	mov eax, ebp
	mov ebx, esi
machmv16: shr eax, 32
machmv15: shr ebx, 32
machmv12: add ebp, 88888888h ;vince[3]
machmv9: add esi, 88888888h ;vince[2]
machmv10: mov al, byte ptr [eax+88888888h] ;bufplce[3]
machmv7: mov bl, byte ptr [ebx+88888888h] ;bufplce[2]
	cmp al, 255
	adc dl, dl
	cmp bl, 255
	adc dl, dl
machmv8: mov bl, byte ptr [ebx+88888888h] ;palookupoffs[2]
machmv11: mov bh, byte ptr [eax+88888888h] ;palookupoffs[3]

	mov eax, edx
machmv14: shr eax, 32
	shl ebx, 16
machmv4: mov al, byte ptr [eax+88888888h] ;bufplce[1]
	cmp al, 255
	adc dl, dl
machmv6: add edx, 88888888h ;vince[1]
machmv5: mov bh, byte ptr [eax+88888888h] ;palookupoffs[1]

	mov eax, ecx
machmv13: shr eax, 32
machmv3: add ecx, 88888888h ;vince[0]
machmv1: mov al, byte ptr [eax+88888888h] ;bufplce[0]
	cmp al, 255
	adc dl, dl
machmv2: mov bl, byte ptr [eax+88888888h] ;palookupoffs[0]

	shl dl, 4
	xor eax, eax
fixchain2mb: add edi, 320
	mov al, dl
	add eax, offset mvcase0
	jmp eax       ;16 byte cases

ALIGN 16
endmvlineasm4:
	dec byte ptr _asm3[0]
	jnz beginmvlineasm42

	mov dword ptr _vplce[0], ecx
	mov dword ptr _vplce[4], edx
	mov dword ptr _vplce[8], esi
	mov dword ptr _vplce[12], ebp
	pop ebp
CDECLEND 6
	ret

	;5,7,8,8,11,13,12,14,11,13,14,14,12,14,15,7
ALIGN 16
mvcase0:
	jmp beginmvlineasm4
ALIGN 16
mvcase1:
	mov byte ptr [edi], bl
	jmp beginmvlineasm4
ALIGN 16
mvcase2:
	mov byte ptr [edi+1], bh
	jmp beginmvlineasm4
ALIGN 16
mvcase3:
	mov word ptr [edi], bx
	jmp beginmvlineasm4
ALIGN 16
mvcase4:
	shr ebx, 16
	mov byte ptr [edi+2], bl
	jmp beginmvlineasm4
ALIGN 16
mvcase5:
	mov byte ptr [edi], bl
	shr ebx, 16
	mov byte ptr [edi+2], bl
	jmp beginmvlineasm4
ALIGN 16
	mvcase6:
	shr ebx, 8
	mov word ptr [edi+1], bx
	jmp beginmvlineasm4
ALIGN 16
mvcase7:
	mov word ptr [edi], bx
	shr ebx, 16
	mov byte ptr [edi+2], bl
	jmp beginmvlineasm4
ALIGN 16
mvcase8:
	shr ebx, 16
	mov byte ptr [edi+3], bh
	jmp beginmvlineasm4
ALIGN 16
mvcase9:
	mov byte ptr [edi], bl
	shr ebx, 16
	mov byte ptr [edi+3], bh
	jmp beginmvlineasm4
ALIGN 16
mvcase10:
	mov byte ptr [edi+1], bh
	shr ebx, 16
	mov byte ptr [edi+3], bh
	jmp beginmvlineasm4
ALIGN 16
mvcase11:
	mov word ptr [edi], bx
	shr ebx, 16
	mov byte ptr [edi+3], bh
	jmp beginmvlineasm4
ALIGN 16
mvcase12:
	shr ebx, 16
	mov word ptr [edi+2], bx
	jmp beginmvlineasm4
ALIGN 16
mvcase13:
	mov byte ptr [edi], bl
	shr ebx, 16
	mov word ptr [edi+2], bx
	jmp beginmvlineasm4
ALIGN 16
mvcase14:
	mov byte ptr [edi+1], bh
	shr ebx, 16
	mov word ptr [edi+2], bx
	jmp beginmvlineasm4
ALIGN 16
mvcase15:
	mov dword ptr [edi], ebx
	jmp beginmvlineasm4

ALIGN 16
PUBLIC _setupspritevline
_setupspritevline:
CDECLBEGINSET 6

	mov dword ptr [spal+2], eax

	mov eax, esi                      ;xinc's
	shl eax, 16
	mov dword ptr [smach1+2], eax
	mov dword ptr [smach4+2], eax
	mov eax, esi
	sar eax, 16
	add eax, ebx                      ;watch out with ebx - it's passed
	mov dword ptr [smach2+2], eax
	add eax, edx
	mov dword ptr [smach5+2], eax

	mov dword ptr [smach3+2], ecx  ;yinc's

CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _spritevline
_spritevline:
CDECLBEGINSET 6
	jmp short _spritevline_start
	;eax = 0, ebx = x, ecx = cnt, edx = y, esi = yplc, edi = p
prestartsvline:
smach1: add ebx, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
smach2: adc esi, 88888888h              ;xincshr16+yalwaysinc

startsvline:
spal: mov al, [eax+88888888h]           ;palookup
	mov byte ptr [edi], al
fixchain1s: add edi, 320

_spritevline_start:
smach3: add edx, 88888888h              ;dayinc
	dec ecx
	ja short prestartsvline     ;jump if (no carry (add)) and (not zero (dec))!
	jz short endsvline
smach4: add ebx, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
smach5: adc esi, 88888888h              ;xincshr16+yalwaysinc+daydime
	jmp short startsvline
endsvline:
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _msetupspritevline
_msetupspritevline:
CDECLBEGINSET 6

	mov dword ptr [mspal+2], eax

	mov eax, esi                      ;xinc's
	shl eax, 16
	mov dword ptr [msmach1+2], eax
	mov dword ptr [msmach4+2], eax
	mov eax, esi
	sar eax, 16
	add eax, ebx                      ;watch out with ebx - it's passed
	mov dword ptr [msmach2+2], eax
	add eax, edx
	mov dword ptr [msmach5+2], eax

	mov dword ptr [msmach3+2], ecx  ;yinc's

CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _mspritevline
_mspritevline:
CDECLBEGINSET 6
	jmp short _mspritevline_start
	;eax = 0, ebx = x, ecx = cnt, edx = y, esi = yplc, edi = p
mprestartsvline:
msmach1: add ebx, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
msmach2: adc esi, 88888888h              ;xincshr16+yalwaysinc

mstartsvline:
	cmp al, 255
	je short mskipsvline
mspal: mov al, [eax+88888888h]           ;palookup
	mov byte ptr [edi], al
mskipsvline:
mfixchain1s: add edi, 320

_mspritevline_start:
msmach3: add edx, 88888888h              ;dayinc
	dec ecx
	ja short mprestartsvline     ;jump if (no carry (add)) and (not zero (dec))!
	jz short mendsvline
msmach4: add ebx, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
msmach5: adc esi, 88888888h              ;xincshr16+yalwaysinc+daydime
	jmp short mstartsvline
mendsvline:
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _tsetupspritevline
_tsetupspritevline:
CDECLBEGINSET 6
	mov dword ptr [tspal+2], eax

	mov eax, esi                      ;xinc's
	shl eax, 16
	mov dword ptr [tsmach1+2], eax
	mov dword ptr [tsmach4+2], eax
	mov eax, esi
	sar eax, 16
	add eax, ebx                      ;watch out with ebx - it's passed
	mov dword ptr [tsmach2+2], eax
	add eax, edx
	mov dword ptr [tsmach5+2], eax

	mov dword ptr [tsmach3+2], ecx  ;yinc's
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _tspritevline
_tspritevline:
CDECLBEGINSET 6
	;eax = 0, ebx = x, ecx = cnt, edx = y, esi = yplc, edi = p
	push ebp
	mov ebp, ebx
	xor ebx, ebx
	jmp tenterspritevline
ALIGN 16
tprestartsvline:
tsmach1: add ebp, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
tsmach2: adc esi, 88888888h              ;xincshr16+yalwaysinc

tstartsvline:
	cmp al, 255
	je short tskipsvline
transrev2:
	mov bh, byte ptr [edi]
transrev3:
tspal: mov bl, [eax+88888888h]               ;palookup
tmach4: mov al, byte ptr [ebx+88888888h]     ;_transluc
	mov byte ptr [edi], al
tskipsvline:
tfixchain1s: add edi, 320

tenterspritevline:
tsmach3: add edx, 88888888h              ;dayinc
	dec ecx
	ja short tprestartsvline     ;jump if (no carry (add)) and (not zero (dec))!
	jz short tendsvline
tsmach4: add ebp, 88888888h              ;xincshl16
	mov al, byte ptr [esi]
tsmach5: adc esi, 88888888h              ;xincshr16+yalwaysinc+daydime
	jmp short tstartsvline
tendsvline:
	pop ebp
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _msethlineshift
_msethlineshift:
CDECLBEGINSET 2
	neg al
	mov byte ptr [msh1d+2], al
	mov byte ptr [msh2d+3], bl
	mov byte ptr [msh3d+2], al
	mov byte ptr [msh4d+3], bl
	mov byte ptr [msh5d+2], al
	mov byte ptr [msh6d+3], bl
CDECLENDSET 2
	ret

ALIGN 16
PUBLIC _mhline
_mhline:
CDECLBEGINSET 6
	;_asm1 = bxinc
	;_asm2 = byinc
	;_asm3 = shadeoffs
	;eax = picoffs
	;ebx = bx
	;ecx = cnt
	;edx = ?
	;esi = by
	;edi = p

	mov dword ptr [mmach1d+2], eax
	mov dword ptr [mmach5d+2], eax
	mov dword ptr [mmach9d+2], eax
	mov eax, _asm3
	mov dword ptr [mmach2d+2], eax
	mov dword ptr [mmach2da+2], eax
	mov dword ptr [mmach2db+2], eax
	mov dword ptr [mmach6d+2], eax
	mov dword ptr [mmach10d+2], eax
	mov eax, _asm1
	mov dword ptr [mmach3d+2], eax
	mov dword ptr [mmach7d+2], eax
	mov eax, _asm2
	mov dword ptr [mmach4d+2], eax
	mov dword ptr [mmach8d+2], eax
	jmp short _mhlineskipmodify_nosetup

ALIGN 16
PUBLIC _mhlineskipmodify
_mhlineskipmodify:
CDECLBEGINSET 6
_mhlineskipmodify_nosetup:

	push ebp

	xor eax, eax
	mov ebp, ebx

	test ecx, 00010000h
	jnz short mbeghline

msh1d: shr ebx, 26
msh2d: shld ebx, esi, 6
	add ebp, _asm1
mmach9d: mov al, byte ptr [ebx+88888888h]    ;picoffs
	add esi, _asm2
	cmp al, 255
	je mskip5

	mmach10d: mov cl, byte ptr [eax+88888888h]    ;shadeoffs
	mov byte ptr [edi], cl
mskip5:
	inc edi
	sub ecx, 65536
	jc mendhline
	jmp short mbeghline

ALIGN 16
mpreprebeghline:                                            ;1st only
	mov al, cl
mmach2d: mov al, byte ptr [eax+88888888h]    ;shadeoffs
	mov byte ptr [edi], al

mprebeghline:
	add edi, 2
	sub ecx, 131072
	jc short mendhline
mbeghline:
mmach3d: lea ebx, [ebp+88888888h]            ;bxinc
msh3d: shr ebp, 26
msh4d: shld ebp, esi, 6
mmach4d: add esi, 88888888h                  ;byinc
mmach1d: mov cl, byte ptr [ebp+88888888h]    ;picoffs
mmach7d: lea ebp, [ebx+88888888h]            ;bxinc

msh5d: shr ebx, 26
msh6d: shld ebx, esi, 6
mmach8d: add esi, 88888888h                  ;byinc
mmach5d: mov ch, byte ptr [ebx+88888888h]    ;picoffs

	cmp cl, 255
	je short mskip1
	cmp ch, 255
	je short mpreprebeghline

	mov al, cl                                                  ;BOTH
mmach2da: mov bl, byte ptr [eax+88888888h]    ;shadeoffs
	mov al, ch
mmach2db: mov bh, byte ptr [eax+88888888h]    ;shadeoffs
	mov word ptr [edi], bx
	add edi, 2
	sub ecx, 131072
	jnc short mbeghline
	jmp mendhline
mskip1:                                                     ;2nd only
	cmp ch, 255
	je short mprebeghline

	mov al, ch
mmach6d: mov al, byte ptr [eax+88888888h]    ;shadeoffs
	mov byte ptr [edi+1], al
	add edi, 2
	sub ecx, 131072
	jnc short mbeghline
mendhline:

	pop ebp
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _tsethlineshift
_tsethlineshift:
CDECLBEGINSET 2
	neg al
	mov byte ptr [tsh1d+2], al
	mov byte ptr [tsh2d+3], bl
	mov byte ptr [tsh3d+2], al
	mov byte ptr [tsh4d+3], bl
	mov byte ptr [tsh5d+2], al
	mov byte ptr [tsh6d+3], bl
CDECLENDSET 2
	ret

ALIGN 16
PUBLIC _thline
_thline:
CDECLBEGINSET 6
	;_asm1 = bxinc
	;_asm2 = byinc
	;_asm3 = shadeoffs
	;eax = picoffs
	;ebx = bx
	;ecx = cnt
	;edx = ?
	;esi = by
	;edi = p

	mov dword ptr [tmach1d+2], eax
	mov dword ptr [tmach5d+2], eax
	mov dword ptr [tmach9d+2], eax
	mov eax, _asm3
	mov dword ptr [tmach2d+2], eax
	mov dword ptr [tmach6d+2], eax
	mov dword ptr [tmach10d+2], eax
	mov eax, _asm1
	mov dword ptr [tmach3d+2], eax
	mov dword ptr [tmach7d+2], eax
	mov eax, _asm2
	mov dword ptr [tmach4d+2], eax
	mov dword ptr [tmach8d+2], eax
	jmp short _thlineskipmodify_nosetup

ALIGN 16
PUBLIC _thlineskipmodify
_thlineskipmodify:
CDECLBEGINSET 6
_thlineskipmodify_nosetup:

	push ebp

	xor eax, eax
	xor edx, edx
	mov ebp, ebx

	test ecx, 00010000h
	jnz short tbeghline

tsh1d: shr ebx, 26
tsh2d: shld ebx, esi, 6
	add ebp, _asm1
tmach9d: mov al, byte ptr [ebx+88888888h]    ;picoffs
	add esi, _asm2
	cmp al, 255
	je tskip5

transrev4:
tmach10d: mov dl, byte ptr [eax+88888888h]   ;shadeoffs
transrev5:
	mov dh, byte ptr [edi]
tmach1: mov al, byte ptr [edx+88888888h]     ;_transluc
	mov byte ptr [edi], al
tskip5:
	inc edi
	sub ecx, 65536
	jc tendhline
	jmp short tbeghline

ALIGN 16
tprebeghline:
	add edi, 2
	sub ecx, 131072
	jc short tendhline
tbeghline:
tmach3d: lea ebx, [ebp+88888888h]            ;bxinc
tsh3d: shr ebp, 26
tsh4d: shld ebp, esi, 6
tmach4d: add esi, 88888888h                  ;byinc
tmach1d: mov cl, byte ptr [ebp+88888888h]    ;picoffs
tmach7d: lea ebp, [ebx+88888888h]            ;bxinc

tsh5d: shr ebx, 26
tsh6d: shld ebx, esi, 6
tmach8d: add esi, 88888888h                  ;byinc
tmach5d: mov ch, byte ptr [ebx+88888888h]    ;picoffs

	cmp cx, 0ffffh
	je short tprebeghline

	mov bx, word ptr [edi]

	cmp cl, 255
	je short tskip1
	mov al, cl
transrev6:
tmach2d: mov dl, byte ptr [eax+88888888h]    ;shadeoffs
transrev7:
	mov dh, bl
tmach2: mov al, byte ptr [edx+88888888h]     ;_transluc
	mov byte ptr [edi], al

	cmp ch, 255
	je short tskip2
tskip1:
	mov al, ch
transrev8:
tmach6d: mov dl, byte ptr [eax+88888888h]    ;shadeoffs
transrev9:
	mov dh, bh
tmach3: mov al, byte ptr [edx+88888888h]     ;_transluc
	mov byte ptr [edi+1], al
tskip2:

	add edi, 2
	sub ecx, 131072
	jnc tbeghline
tendhline:

	pop ebp
CDECLENDSET 6
	ret


	;eax=shiftval, ebx=palookup1, ecx=palookup2
ALIGN 16
PUBLIC _setuptvlineasm2
_setuptvlineasm2:
CDECLBEGINSET 3
	mov byte ptr [tran2shra+2], al
	mov byte ptr [tran2shrb+2], al
	mov dword ptr [tran2pala+2], ebx
	mov dword ptr [tran2palb+2], ecx
	mov dword ptr [tran2palc+2], ebx
	mov dword ptr [tran2pald+2], ecx
CDECLENDSET 3
	ret

	;Pass:   eax=vplc2, ebx=vinc1, ecx=bufplc1, edx=bufplc2, esi=vplc1, edi=p
	;        _asm1=vinc2, _asm2=pend
	;Return: _asm1=vplc1, _asm2=vplc2
ALIGN 16
PUBLIC _tvlineasm2
_tvlineasm2:
CDECLBEGINSET 6

	push ebp

	mov ebp, eax

	mov dword ptr [tran2inca+2], ebx
	mov eax, _asm1
	mov dword ptr [tran2incb+2], eax

	mov dword ptr [tran2bufa+2], ecx         ;bufplc1
	mov dword ptr [tran2bufb+2], edx         ;bufplc2

	mov eax, _asm2
	sub edi, eax
	mov dword ptr [tran2edia+3], eax
	mov dword ptr [tran2edic+2], eax
	inc eax
	mov dword ptr [tran2edie+2], eax
fixchaint2a: sub eax, 320
	mov dword ptr [tran2edif+2], eax
	dec eax
	mov dword ptr [tran2edib+3], eax
	mov dword ptr [tran2edid+2], eax

	xor ecx, ecx
	xor edx, edx
	jmp short begintvline2

		;eax 0000000000  temp  temp
		;ebx 0000000000 odat2 odat1
		;ecx 0000000000000000 ndat1
		;edx 0000000000000000 ndat2
		;esi          vplc1
		;edi videoplc--------------
		;ebp          vplc2

ALIGN 16
		;LEFT ONLY
skipdraw2:
transrev10:
tran2edic: mov ah, byte ptr [edi+88888888h]      ;getpixel
transrev11:
tran2palc: mov al, byte ptr [ecx+88888888h]      ;palookup1
fixchaint2d: add edi, 320
tran2trac: mov bl, byte ptr [eax+88888888h]      ;_transluc
tran2edid: mov byte ptr [edi+88888888h-320], bl  ;drawpixel
	jnc short begintvline2
	jmp endtvline2

skipdraw1:
	cmp dl, 255
	jne short skipdraw3
fixchaint2b: add edi, 320
	jc short endtvline2

begintvline2:
	mov eax, esi
tran2shra: shr eax, 88h                      ;globalshift
	mov ebx, ebp
tran2shrb: shr ebx, 88h                      ;globalshift
tran2inca: add esi, 88888888h                ;vinc1
tran2incb: add ebp, 88888888h                ;vinc2
tran2bufa: mov cl, byte ptr [eax+88888888h]  ;bufplc1
	cmp cl, 255
tran2bufb: mov dl, byte ptr [ebx+88888888h]  ;bufplc2
	je short skipdraw1
	cmp dl, 255
	je short skipdraw2

	;mov ax        The transluscent reverse of both!
	;mov bl, ah
	;mov ah
	;mov bh

		;BOTH
transrev12:
tran2edia: mov bx, word ptr [edi+88888888h]      ;getpixels
transrev13:
	mov ah, bl
transrev14:
tran2pala: mov al, byte ptr [ecx+88888888h]      ;palookup1
transrev15:
tran2palb: mov bl, byte ptr [edx+88888888h]      ;palookup2
fixchaint2c: add edi, 320
tran2traa: mov al, byte ptr [eax+88888888h]      ;_transluc
tran2trab: mov ah, byte ptr [ebx+88888888h]      ;_transluc
tran2edib: mov word ptr [edi+88888888h-320], ax  ;drawpixels
	jnc short begintvline2
	jmp short endtvline2

	;RIGHT ONLY
skipdraw3:
transrev16:
tran2edie: mov ah, byte ptr [edi+88888889h]      ;getpixel
transrev17:
tran2pald: mov al, byte ptr [edx+88888888h]      ;palookup2
fixchaint2e: add edi, 320
tran2trad: mov bl, byte ptr [eax+88888888h]      ;_transluc
tran2edif: mov byte ptr [edi+88888889h-320], bl  ;drawpixel
	jnc short begintvline2

endtvline2:
	mov _asm1, esi
	mov _asm2, ebp

	pop ebp

CDECLENDSET 6
	ret


BITSOFPRECISION equ 3
BITSOFPRECISIONPOW equ 8

;Double-texture mapping with palette lookup
;eax:  ylo1------------|----dat|----dat
;ebx:  ylo2--------------------|----cnt
;ecx:  000000000000000000000000|---temp
;edx:  xhi1-xlo1---------------|---yhi1
;esi:  xhi2-xlo2---------------|---yhi2
;edi:  ------------------------videopos
;ebp:  ----------------------------temp

ALIGN 16
PUBLIC _setupslopevlin2
_setupslopevlin2:
CDECLBEGINSET 6

	mov dword ptr [slop3+2], edx ;ptr
	mov dword ptr [slop7+2], edx ;ptr
	mov dword ptr [slop4+2], esi ;tptr
	mov dword ptr [slop8+2], esi ;tptr
	mov byte ptr [slop2+2], ah   ;ybits
	mov byte ptr [slop6+2], ah   ;ybits
	mov dword ptr [slop9+2], edi ;pinc

	mov edx, 1
	mov cl, al
	add cl, ah
	shl edx, cl
	dec edx
	mov cl, ah
	ror edx, cl

	mov dword ptr [slop1+2], edx   ;ybits...xbits
	mov dword ptr [slop5+2], edx   ;ybits...xbits

CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _slopevlin2
_slopevlin2:
CDECLBEGINSET 6

	push ebp
	xor ecx, ecx

slopevlin2begin:
	mov ebp, edx
slop1: and ebp, 88000088h                ;ybits...xbits
slop2: rol ebp, 6                        ;ybits
	add eax, _asm1                        ;xinc1<<xbits
	adc edx, _asm2                        ;(yinc1&0xffffff00)+(xinc1>>(32-xbits))
slop3: mov cl, byte ptr [ebp+88888888h]  ;bufplc

	mov ebp, esi
slop4: mov al, byte ptr [ecx+88888888h]  ;paloffs
slop5: and ebp, 88000088h                ;ybits...xbits
slop6: rol ebp, 6                        ;ybits
	add ebx, _asm3                        ;xinc2<<xbits
slop7: mov cl, byte ptr [ebp+88888888h]  ;bufplc
	adc esi, _asm4                        ;(yinc2&0xffffff00)+(xinc2>>(32-xbits))
slop8: mov ah, byte ptr [ecx+88888888h]  ;paloffs

	dec bl
	mov word ptr [edi], ax
slop9: lea edi, [edi+88888888h]          ;pinc
	jnz short slopevlin2begin

	pop ebp
	mov eax, edi

CDECLENDSET 6
	ret


ALIGN 16
PUBLIC _setupslopevlin
_setupslopevlin:
CDECLBEGINSET 3

	mov dword ptr [slopmach3+3], ebx    ;ptr
	mov dword ptr [slopmach5+2], ecx    ;pinc
	neg ecx
	mov dword ptr [slopmach6+2], ecx    ;-pinc

	mov edx, 1
	mov cl, al
	shl edx, cl
	dec edx
	mov cl, ah
	shl edx, cl
	mov dword ptr [slopmach7+2], edx

	neg ah
	mov byte ptr [slopmach2+2], ah

	sub ah, al
	mov byte ptr [slopmach1+2], ah

	fild dword ptr _asm1
	fstp dword ptr _asm2

CDECLENDSET 3
	ret

ALIGN 16
PUBLIC _slopevlin
_slopevlin:
CDECLBEGINSET 6

	mov _ebpbak, ebp
	mov _espbak, esp

	sub ecx, esp
	mov dword ptr [slopmach4+3], ecx

	fild dword ptr _asm3
slopmach6: lea ebp, [eax+88888888h]
	fadd dword ptr _asm2

	mov _asm1, ebx
	shl ebx, 3

	mov eax, _globalx3
	mov ecx, _globaly3
	imul eax, ebx
	imul ecx, ebx
	add esi, eax
	add edi, ecx

	mov ebx, edx
	jmp short bigslopeloop
ALIGN 16
bigslopeloop:
	fst dword ptr _fpuasm

	mov eax, _fpuasm
	add eax, eax
	sbb edx, edx
	mov ecx, eax
	shr ecx, 24
	and eax, 00ffe000h
	shr eax, 11
	sub cl, 2
	mov eax, dword ptr _reciptable[eax]
	shr eax, cl
	xor eax, edx
	mov edx, _asm1
	mov ecx, _globalx3
	mov _asm1, eax
	sub eax, edx
	mov edx, _globaly3
	imul ecx, eax
	imul eax, edx

	fadd dword ptr _asm2

	cmp ebx, BITSOFPRECISIONPOW
	mov _asm4, ebx
	mov cl, bl
	jl short slopeskipmin
	mov cl, BITSOFPRECISIONPOW
slopeskipmin:

;eax: yinc.............
;ebx:   0   0   0   ?
;ecx: xinc......... cnt
;edx:         ?
;esi: xplc.............
;edi: yplc.............
;ebp:     videopos

	mov ebx, esi
	mov edx, edi

beginnerslopeloop:
slopmach1: shr ebx, 20
	add esi, ecx
slopmach2: shr edx, 26
slopmach7: and ebx, 88888888h
	add edi, eax
slopmach5: add ebp, 88888888h                   ;pinc
slopmach3: mov dl, byte ptr [ebx+edx+88888888h] ;ptr
slopmach4: mov ebx, dword ptr [esp+88888888h]
	sub esp, 4
	dec cl
	mov al, byte ptr [ebx+edx] ;tptr
	mov ebx, esi
	mov [ebp], al
	mov edx, edi
	jnz short beginnerslopeloop

	mov ebx, _asm4
	sub ebx, BITSOFPRECISIONPOW
	jg bigslopeloop

	ffree st(0)

	mov esp, _espbak
	mov ebp, _ebpbak

CDECLENDSET 6
	ret


ALIGN 16
PUBLIC _setuprhlineasm4
_setuprhlineasm4:
CDECLBEGINSET 6

	mov dword ptr [rmach1a+2], eax
	mov dword ptr [rmach1b+2], eax
	mov dword ptr [rmach1c+2], eax
	mov dword ptr [rmach1d+2], eax
	mov dword ptr [rmach1e+2], eax

	mov dword ptr [rmach2a+2], ebx
	mov dword ptr [rmach2b+2], ebx
	mov dword ptr [rmach2c+2], ebx
	mov dword ptr [rmach2d+2], ebx
	mov dword ptr [rmach2e+2], ebx

	mov dword ptr [rmach3a+2], ecx
	mov dword ptr [rmach3b+2], ecx
	mov dword ptr [rmach3c+2], ecx
	mov dword ptr [rmach3d+2], ecx
	mov dword ptr [rmach3e+2], ecx

	mov dword ptr [rmach4a+2], edx
	mov dword ptr [rmach4b+2], edx
	mov dword ptr [rmach4c+2], edx
	mov dword ptr [rmach4d+2], edx
	mov dword ptr [rmach4e+2], edx

	mov dword ptr [rmach5a+2], esi
	mov dword ptr [rmach5b+2], esi
	mov dword ptr [rmach5c+2], esi
	mov dword ptr [rmach5d+2], esi
	mov dword ptr [rmach5e+2], esi

CDECLENDSET 6
	ret

	;Non power of 2, non masking, with palookup method #1 (6 clock cycles)
	;eax: dat dat dat dat
	;ebx:          bufplc
	;ecx:  0          dat
	;edx:  xlo
	;esi:  ylo
	;edi:  videopos/cnt
	;ebp:  tempvar
	;esp:
ALIGN 16
PUBLIC _rhlineasm4
_rhlineasm4:
CDECLBEGINSET 6

	push ebp

	cmp eax, 0
	jle endrhline

	lea ebp, [edi-4]
	sub ebp, eax
	mov dword ptr [rmach6a+2], ebp
	add ebp, 3
	mov dword ptr [rmach6b+2], ebp
	mov edi, eax
	test edi, 3
	jz short begrhline
	jmp short startrhline1

ALIGN 16
startrhline1:
	mov cl, byte ptr [ebx]                      ;bufplc
rmach1e: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmach2e: sub esi, 88888888h                    ;ylo
rmach3e: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmach4e: mov al, byte ptr [ecx+88888888h]      ;palookup
rmach5e: and ebp, 88888888h                    ;tilesizy
rmach6b: mov byte ptr [edi+88888888h], al      ;vidcntoffs
	sub ebx, ebp
	dec edi
	test edi, 3
	jnz short startrhline1
	test edi, edi
	jz endrhline

begrhline:
	mov cl, byte ptr [ebx]                      ;bufplc
rmach1a: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmach2a: sub esi, 88888888h                    ;ylo
rmach3a: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmach5a: and ebp, 88888888h                    ;tilesizy
	sub ebx, ebp

rmach1b: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmach4a: mov ah, byte ptr [ecx+88888888h]      ;palookup
	mov cl, byte ptr [ebx]                      ;bufplc
rmach2b: sub esi, 88888888h                    ;ylo
rmach3b: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmach5b: and ebp, 88888888h                    ;tilesizy
rmach4b: mov al, byte ptr [ecx+88888888h]      ;palookup
	sub ebx, ebp

	shl eax, 16

	mov cl, byte ptr [ebx]                      ;bufplc
rmach1c: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmach2c: sub esi, 88888888h                    ;ylo
rmach3c: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmach5c: and ebp, 88888888h                    ;tilesizy
	sub ebx, ebp

rmach1d: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmach4c: mov ah, byte ptr [ecx+88888888h]      ;palookup
	mov cl, byte ptr [ebx]                      ;bufplc
rmach2d: sub esi, 88888888h                    ;ylo
rmach3d: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmach5d: and ebp, 88888888h                    ;tilesizy
rmach4d: mov al, byte ptr [ecx+88888888h]      ;palookup
	sub ebx, ebp

rmach6a: mov dword ptr [edi+88888888h], eax    ;vidcntoffs
	sub edi, 4
	jnz begrhline
endrhline:
	pop ebp

CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _setuprmhlineasm4
_setuprmhlineasm4:
CDECLBEGINSET 6
	mov dword ptr [rmmach1+2], eax
	mov dword ptr [rmmach2+2], ebx
	mov dword ptr [rmmach3+2], ecx
	mov dword ptr [rmmach4+2], edx
	mov dword ptr [rmmach5+2], esi
CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _rmhlineasm4
_rmhlineasm4:
CDECLBEGINSET 6

	push ebp

	cmp eax, 0
	jle short endrmhline

	lea ebp, [edi-1]
	sub ebp, eax
	mov dword ptr [rmmach6+2], ebp
	mov edi, eax
	jmp short begrmhline

ALIGN 16
begrmhline:
	mov cl, byte ptr [ebx]                      ;bufplc
rmmach1: sub edx, 88888888h                    ;xlo
	sbb ebp, ebp
rmmach2: sub esi, 88888888h                    ;ylo
rmmach3: sbb ebx, 88888888h                    ;xhi*tilesizy + yhi+ycarry
rmmach5: and ebp, 88888888h                    ;tilesizy
	cmp cl, 255
	je short rmskip
rmmach4: mov al, byte ptr [ecx+88888888h]      ;palookup
rmmach6: mov byte ptr [edi+88888888h], al      ;vidcntoffs
rmskip:
	sub ebx, ebp
	dec edi
	jnz short begrmhline
endrmhline:
	pop ebp

CDECLENDSET 6
	ret

ALIGN 16
PUBLIC _setupqrhlineasm4
_setupqrhlineasm4:
CDECLBEGINSET 6

	mov dword ptr [qrmach2e+2], ebx
	mov dword ptr [qrmach3e+2], ecx
	xor edi, edi
	sub edi, ecx
	mov dword ptr [qrmach7a+2], edi
	mov dword ptr [qrmach7b+2], edi

	add ebx, ebx
	adc ecx, ecx
	mov dword ptr [qrmach2a+2], ebx
	mov dword ptr [qrmach2b+2], ebx
	mov dword ptr [qrmach3a+2], ecx
	mov dword ptr [qrmach3b+2], ecx

	mov dword ptr [qrmach4a+2], edx
	mov dword ptr [qrmach4b+2], edx
	mov dword ptr [qrmach4c+2], edx
	mov dword ptr [qrmach4d+2], edx
	mov dword ptr [qrmach4e+2], edx

CDECLENDSET 6
	ret

	;Non power of 2, non masking, with palookup method (FASTER BUT NO SBB'S)
	;eax: dat dat dat dat
	;ebx:          bufplc
	;ecx:  0          dat
	;edx:  0          dat
	;esi:  ylo
	;edi:  videopos/cnt
	;ebp:  ?
	;esp:
ALIGN 16
PUBLIC _qrhlineasm4          ;4 pixels in 9 cycles!  2.25 cycles/pixel
_qrhlineasm4:
CDECLBEGINSET 6

	push ebp

	cmp eax, 0
	jle endqrhline

	mov ebp, eax
	test ebp, 3
	jz short skipqrhline1
	jmp short startqrhline1

ALIGN 16
startqrhline1:
	mov cl, byte ptr [ebx]                      ;bufplc
	dec edi
qrmach2e: sub esi, 88888888h                   ;ylo
	dec ebp
qrmach3e: sbb ebx, 88888888h                   ;xhi*tilesizy + yhi+ycarry
qrmach4e: mov al, byte ptr [ecx+88888888h]     ;palookup
	mov byte ptr [edi], al                      ;vidcntoffs
	test ebp, 3
	jnz short startqrhline1
	test ebp, ebp
	jz short endqrhline

skipqrhline1:
	mov cl, byte ptr [ebx]                      ;bufplc
	jmp short begqrhline
ALIGN 16
begqrhline:
qrmach7a: mov dl, byte ptr [ebx+88888888h]     ;bufplc
qrmach2a: sub esi, 88888888h                   ;ylo
qrmach3a: sbb ebx, 88888888h                   ;xhi*tilesizy + yhi+ycarry
qrmach4a: mov ah, byte ptr [ecx+88888888h]     ;palookup
qrmach4b: mov al, byte ptr [edx+88888888h]     ;palookup
	sub edi, 4
	shl eax, 16
	mov cl, byte ptr [ebx]                      ;bufplc
qrmach7b: mov dl, byte ptr [ebx+88888888h]     ;bufplc
qrmach2b: sub esi, 88888888h                   ;ylo
qrmach3b: sbb ebx, 88888888h                   ;xhi*tilesizy + yhi+ycarry
qrmach4c: mov ah, byte ptr [ecx+88888888h]     ;palookup
qrmach4d: mov al, byte ptr [edx+88888888h]     ;palookup
	mov cl, byte ptr [ebx]                      ;bufplc
	mov dword ptr [edi], eax
	sub ebp, 4
	jnz short begqrhline

endqrhline:
	pop ebp

CDECLENDSET 6
	ret


PUBLIC _setupdrawslab
_setupdrawslab:
CDECLBEGINSET 2

	mov dword ptr [voxbpl1+2], eax
	mov dword ptr [voxbpl2+2], eax
	mov dword ptr [voxbpl3+2], eax
	mov dword ptr [voxbpl4+2], eax
	mov dword ptr [voxbpl5+2], eax
	mov dword ptr [voxbpl6+2], eax
	mov dword ptr [voxbpl7+2], eax
	mov dword ptr [voxbpl8+2], eax

	mov dword ptr [voxpal1+2], ebx
	mov dword ptr [voxpal2+2], ebx
	mov dword ptr [voxpal3+2], ebx
	mov dword ptr [voxpal4+2], ebx
	mov dword ptr [voxpal5+2], ebx
	mov dword ptr [voxpal6+2], ebx
	mov dword ptr [voxpal7+2], ebx
	mov dword ptr [voxpal8+2], ebx

CDECLENDSET 2
	ret

ALIGN 16
PUBLIC _drawslab
_drawslab:
CDECLBEGINSET 6

	push ebp
	cmp eax, 2
	je voxbegdraw2
	ja voxskip2
	xor eax, eax
voxbegdraw1:
	mov ebp, ebx
	shr ebp, 16
	add ebx, edx
	dec ecx
	mov al, byte ptr [esi+ebp]
voxpal1: mov al, byte ptr [eax+88888888h]
	mov byte ptr [edi], al
voxbpl1: lea edi, [edi+88888888h]
	jnz voxbegdraw1
	pop ebp

CDECLENDSET 6
	ret

voxbegdraw2:
	mov ebp, ebx
	shr ebp, 16
	add ebx, edx
	xor eax, eax
	dec ecx
	mov al, byte ptr [esi+ebp]
voxpal2: mov al, byte ptr [eax+88888888h]
	mov ah, al
	mov word ptr [edi], ax
voxbpl2: lea edi, [edi+88888888h]
	jnz voxbegdraw2
	pop ebp

CDECLENDSET 6
	ret

voxskip2:
	cmp eax, 4
	jne voxskip4
	xor eax, eax
voxbegdraw4:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal3: mov al, byte ptr [eax+88888888h]
	mov ah, al
	shl eax, 8
	mov al, ah
	shl eax, 8
	mov al, ah
	mov dword ptr [edi], eax
voxbpl3: add edi, 88888888h
	dec ecx
	jnz voxbegdraw4
	pop ebp

CDECLENDSET 6
	ret

voxskip4:
	add eax, edi

	test edi, 1
	jz voxskipslab1
	cmp edi, eax
	je voxskipslab1

	push eax
	push ebx
	push ecx
	push edi
voxbegslab1:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal4: mov al, byte ptr [eax+88888888h]
	mov byte ptr [edi], al
voxbpl4: add edi, 88888888h
	dec ecx
	jnz voxbegslab1
	pop edi
	pop ecx
	pop ebx
	pop eax
	inc edi

voxskipslab1:
	push eax
	test edi, 2
	jz voxskipslab2
	dec eax
	cmp edi, eax
	jge voxskipslab2

	push ebx
	push ecx
	push edi
voxbegslab2:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal5: mov al, byte ptr [eax+88888888h]
	mov ah, al
	mov word ptr [edi], ax
voxbpl5: add edi, 88888888h
	dec ecx
	jnz voxbegslab2
	pop edi
	pop ecx
	pop ebx
	add edi, 2

voxskipslab2:
	mov eax, [esp]

	sub eax, 3
	cmp edi, eax
	jge voxskipslab3

voxprebegslab3:
	push ebx
	push ecx
	push edi
voxbegslab3:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal6: mov al, byte ptr [eax+88888888h]
	mov ah, al
	shl eax, 8
	mov al, ah
	shl eax, 8
	mov al, ah
	mov dword ptr [edi], eax
voxbpl6: add edi, 88888888h
	dec ecx
	jnz voxbegslab3
	pop edi
	pop ecx
	pop ebx
	add edi, 4

	mov eax, [esp]

	sub eax, 3
	cmp edi, eax
	jl voxprebegslab3

voxskipslab3:
	mov eax, [esp]

	dec eax
	cmp edi, eax
	jge voxskipslab4

	push ebx
	push ecx
	push edi
voxbegslab4:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal7: mov al, byte ptr [eax+88888888h]
	mov ah, al
	mov word ptr [edi], ax
voxbpl7: add edi, 88888888h
	dec ecx
	jnz voxbegslab4
	pop edi
	pop ecx
	pop ebx
	add edi, 2

voxskipslab4:
	pop eax

	cmp edi, eax
	je voxskipslab5

voxbegslab5:
	mov ebp, ebx
	add ebx, edx
	shr ebp, 16
	xor eax, eax
	mov al, byte ptr [esi+ebp]
voxpal8: mov al, byte ptr [eax+88888888h]
	mov byte ptr [edi], al
voxbpl8: add edi, 88888888h
	dec ecx
	jnz voxbegslab5

voxskipslab5:
	pop ebp

CDECLENDSET 6
	ret

;modify: loinc
;eax: |  dat   |  dat   |   dat  |   dat  |
;ebx: |      loplc1                       |
;ecx: |      loplc2     |  cnthi |  cntlo |
;edx: |--------|--------|--------| hiplc1 |
;esi: |--------|--------|--------| hiplc2 |
;edi: |--------|--------|--------| vidplc |
;ebp: |--------|--------|--------|  hiinc |

PUBLIC _stretchhline
_stretchhline:
CDECLBEGINSET 6

	push ebp

	mov eax, ebx
	shl ebx, 16
	sar eax, 16
	and ecx, 0000ffffh
	or ecx, ebx

	add esi, eax
	mov eax, edx
	mov edx, esi

	mov ebp, eax
	shl eax, 16
	sar ebp, 16

	add ecx, eax
	adc esi, ebp

	add eax, eax
	adc ebp, ebp
	mov dword ptr [loinc1+2], eax
	mov dword ptr [loinc2+2], eax
	mov dword ptr [loinc3+2], eax
	mov dword ptr [loinc4+2], eax

	inc ch

	jmp begloop

begloop:
	mov al, [edx]
loinc1: sub ebx, 88888888h
	sbb edx, ebp
	mov ah, [esi]
loinc2: sub ecx, 88888888h
	sbb esi, ebp
	sub edi, 4
	shl eax, 16
loinc3: sub ebx, 88888888h
	mov al, [edx]
	sbb edx, ebp
	mov ah, [esi]
loinc4: sub ecx, 88888888h
	sbb esi, ebp
	mov [edi], eax
	dec cl
	jnz begloop
	dec ch
	jnz begloop

	pop ebp

CDECLENDSET 6
	ret


PUBLIC _mmxoverlay
_mmxoverlay:
	push ebx	;JBF
	push ecx	;JBF
	push edx	;JBF

	pushfd                 ;Check if CPUID is available
	pop eax
	mov ebx, eax
	xor eax, 00200000h
	push eax
	popfd
	pushfd
	pop eax
	cmp eax, ebx
	je pentium
	xor eax, eax
	dw 0a20fh
	test eax, eax
	jz pentium
	mov eax, 1
	dw 0a20fh
	and eax, 00000f00h
	test edx, 00800000h    ;Check if MMX is available
	jz nommx
	cmp eax, 00000600h     ;Check if P6 Family or not
	jae pentiumii
	jmp pentiummmx
nommx:
	cmp eax, 00000600h     ;Check if P6 Family or not
	jae pentiumpro
pentium:

	pop edx		;JBF
	pop ecx		;JBF
	pop ebx		;JBF
	ret

;+--------------------------------------------------------------+
;|                    PENTIUM II Overlays                       |
;+--------------------------------------------------------------+
pentiumii:
		;Hline overlay (MMX doens't help)
	mov byte ptr [_sethlinesizes], 0e9h
	mov dword ptr [_sethlinesizes+1], (offset _prosethlinesizes)-(offset _sethlinesizes)-5
	mov byte ptr [_setpalookupaddress], 0e9h
	mov dword ptr [_setpalookupaddress+1], (offset _prosetpalookupaddress)-(offset _setpalookupaddress)-5
	mov byte ptr [_setuphlineasm4], 0c3h  ;ret (no code required)
	mov byte ptr [_hlineasm4], 0e9h
	mov dword ptr [_hlineasm4+1], (offset _prohlineasm4)-(offset _hlineasm4)-5

		;Vline overlay
	mov byte ptr [_setupvlineasm], 0e9h
	mov dword ptr [_setupvlineasm+1], (offset _prosetupvlineasm)-(offset _setupvlineasm)-5
	mov byte ptr [_vlineasm4], 0e9h
	mov dword ptr [_vlineasm4+1], (offset _provlineasm4)-(offset _vlineasm4)-5

	pop edx		;JBF
	pop ecx		;JBF
	pop ebx		;JBF
	ret

;+--------------------------------------------------------------+
;|                    PENTIUM MMX Overlays                      |
;+--------------------------------------------------------------+
pentiummmx:
	pop edx		;JBF
	pop ecx		;JBF
	pop ebx		;JBF
	ret

;+--------------------------------------------------------------+
;|                    PENTIUM PRO Overlays                      |
;+--------------------------------------------------------------+
pentiumpro:
		;Hline overlay (MMX doens't help)
	mov byte ptr [_sethlinesizes], 0e9h
	mov dword ptr [_sethlinesizes+1], (offset _prosethlinesizes)-(offset _sethlinesizes)-5
	mov byte ptr [_setpalookupaddress], 0e9h
	mov dword ptr [_setpalookupaddress+1], (offset _prosetpalookupaddress)-(offset _setpalookupaddress)-5
	mov byte ptr [_setuphlineasm4], 0c3h  ;ret (no code required)
	mov byte ptr [_hlineasm4], 0e9h
	mov dword ptr [_hlineasm4+1], (offset _prohlineasm4)-(offset _hlineasm4)-5

		;Vline overlay
	mov byte ptr [_setupvlineasm], 0e9h
	mov dword ptr [_setupvlineasm+1], (offset _prosetupvlineasm)-(offset _setupvlineasm)-5
	mov byte ptr [_vlineasm4], 0e9h
	mov dword ptr [_vlineasm4+1], (offset _provlineasm4)-(offset _vlineasm4)-5

	pop edx		;JBF
	pop ecx		;JBF
	pop ebx		;JBF
	ret

PUBLIC _dep_end
_dep_end:
code ends
END
