;// asm1.asm

.486
.model flat

include asmutils.inc

.data

externdef _texptr:dword
externdef _fadeptr:dword
externdef _ghostptr:dword

externdef pintexptr:dword

externdef drawpinmapns_:near
externdef drawpinmaptex_:near
externdef _screenbuf:dword
externdef _reciptab:dword
externdef _multab:dword
externdef _zmaptab1:byte
externdef _zmaptab2:byte
externdef _zmaptab3:byte

externdef _cval:dword

externdef _linecol:byte


externdef _miny:dword
externdef _maxy:dword

xsize=320
val1	dword 0
ival1	dword	0
yval1	dword	0
iebp	dword	0
yebp	dword	0
val2	byte	0
ival2	byte	0
val3	byte	0
ival3	byte	0
yval2	byte	0
yval3	byte	0
		byte	0,0
scratch byte 32 dup (0)

pin struct
u	dword	?
v	dword	?
col dword	?
pin ends

externdef _pin:dword


mytab label byte
n=0
n2=420
repeat 256
db n/256
n=n+n2
if n gt 255*256
n=255*256
n2=-n2
endif
if n lt 0
n=0
endif
endm

.code



externdef drawpinmap_ : near

drawpinmap_	proc

;bl:edx :edx contains u and lo v, bl contains hi v
;val2:val1 contains u/v incs
;ch:esi contains colour
;val3:ebp contains colour inc
;ival1,ival2,ival3,iebp are delta deltas

			pushad
			mov	esi,offset _pin
			mov edi,edx
			mov pintexptr,eax

			mov ch,25
pinyloop:
			mov cl,40
pinxloop:
			assume esi:ptr pin
			push esi
			push ecx
			mov eax,[esi+sizeof pin].u
			sub eax,[esi].u
			mov edx,[esi+(sizeof pin)*42].u
			sub edx,[esi+(sizeof pin)*41].u
			sub edx,eax
			sar eax,3
			sar edx,6
			mov val1,eax
			sar eax,31
			mov val2,al			
			mov ival1,edx
			sar edx,31
			mov ival2,dl
			mov eax,[esi+(sizeof pin)*41].u
			sub eax,[esi].u
			sar eax,3			
			mov yval1,eax			
			sar eax,31
			mov yval2,al			

			mov eax,[esi+sizeof pin].v
			sub eax,[esi].v
			mov edx,[esi+(sizeof pin)*42].v
			sub edx,[esi+(sizeof pin)*41].v
			sub edx,eax
			sar eax,3
			sar edx,6			
			add val2,ah
			shl eax,24
			add val1,eax
			adc val2,0
			add ival2,dh
			shl edx,24
			add ival1,edx
			adc ival2,0
			mov eax,[esi+(sizeof pin)*41].v
			sub eax,[esi].v
			sar eax,3			
			add yval2,ah
			shl eax,24
			add yval1,eax
			adc yval2,0
			
			mov eax,[esi+sizeof pin].col
			sub eax,[esi].col
			mov edx,[esi+(sizeof pin)*42].col
			sub edx,[esi+(sizeof pin)*41].col
			sub edx,eax
			sar eax,3
			sar edx,6			
			mov val3,ah
			shl eax,24
			mov ebp,eax						
			mov ival3,dh
			shl edx,24
			mov iebp,edx
			mov eax,[esi+(sizeof pin)*41].col
			sub eax,[esi].col
			sar eax,3			
			mov yval3,ah
			shl eax,24
			mov yebp,eax
			
			mov ebx,pintexptr
			mov ecx,_fadeptr
			mov dl,byte ptr ([esi].v)
			shl edx,24
			add edx,800000h
			mov eax,[esi].u
			and eax,65535			
			add edx,eax
			mov bl,byte ptr ([esi].v)+1
			mov ch,byte ptr ([esi].col)+1
			mov esi,[esi].col
			shl esi,24
			mov scratch,8

;bl:edx :edx contains u and lo v, bl contains hi v
;val2:val1 contains u/v incs
;ch:esi contains colour
;val3:ebp contains colour inc
;ival1,ival2,ival3,iebp are delta deltas

			;mov ch,0
			;mov esi,0
			;mov ebp,0
			;mov val3,0
			;mov iebp,0
			;mov ival3,0
			;mov yebp,0
			;mov yval3,0

again:
			

			mov scratch[4],bl
			mov scratch[8],ch
			mov dword ptr scratch[12],edx
			mov dword ptr scratch[16],esi

			mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov al,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u

            mov ah,val2       ; v
			add edx,val1      ; u
			mov cl,[ebx]      ; v
		    adc bl,ah		;  u
            mov ah,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
            rol eax,16		; v

            mov bh,dh
            mov al,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,al		;  u
            mov al,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u

            mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov ah,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u
            rol eax,16		; v
            mov [edi],eax	; u

            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

            mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov al,[ecx]      ; v
            adc bl,bh		;  u
			add esi,ebp		; v
            mov cl,val3		; u
            mov bh,dh		; v
            adc ch,cl		;  u

            mov ah,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,ah		;  u
            mov ah,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u
            rol eax,16		; v

            mov bh,dh
            mov al,val2       ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            adc bl,al		;  u
            mov al,[ecx]      ; v
            mov cl,val3		; u
            add esi,ebp		; v
            adc ch,cl		;  u

            mov bh,dh         ; v
            add edx,val1      ; u
			mov cl,[ebx]      ; v
            mov bh,val2       ; u
            mov ah,[ecx]      ; v
            adc bl,bh		;  u
            add esi,ebp		; v
            mov cl,val3		; u
            mov bh,scratch	; v
            adc ch,cl		;  u
            rol eax,16		; v
            mov [edi+4],eax	; u

			mov bl,scratch[4]
			mov ch,scratch[8]
			mov edx,dword ptr scratch[12]
			mov esi,dword ptr scratch[16]

			mov eax,yval1
			mov cl,yval2		
			add edx,eax			
			adc bl,cl
			mov eax,yebp
			mov cl,yval3
			add esi,eax
			adc ch,cl

			mov eax,iebp	; u
            add ebp,eax		; v
            mov al,ival3      ; u			
            adc val3,al       ; u
			add edi,xsize	  ; v
            mov eax,ival1     ; v
            add val1,eax      ; u
            mov al,ival2      ; v
            adc val2,al       ; u

            dec bh		; v
            mov scratch,bh      ; u
            jnz again         ; v

			
			pop ecx
			pop esi
			add esi,sizeof pin
			sub	edi,xsize*8-8
			dec cl
			jnz pinxloop
			add esi,sizeof pin
			add edi,xsize*8-320
			dec ch
			jnz pinyloop
			assume esi:nothing

			popad		
			ret

drawpinmap_	endp

externdef rotocopy_:near
rotocopy_ proc
			pushad
			mov edi,eax
			mov esi,edx
			mov ebp,ebx
			xor ebx,ebx
			mov ecx,64000/4
again:
			mov bl,[esi]
			mov dl,[ebp]
			add bl,[esi+1]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov al,mytab[ebx]
			mov bl,[esi+1]
			mov dl,[ebp+1]
			add bl,[esi+2]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov ah,mytab[ebx]
			rol eax,16
			mov bl,[esi+2]
			mov dl,[ebp+2]
			add bl,[esi+3]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov al,mytab[ebx]
			mov bl,[esi+3]
			mov dl,[ebp+3]
			add bl,[esi+4]
			rcr bl,1
			shr dl,1
			add bl,dl
			rcr bl,1
			mov ah,mytab[ebx]
			rol eax,16
			mov [edi],eax
			add edi,4
			add esi,4
			add ebp,4
			dec ecx
			jnz again
			popad
			ret
rotocopy_ 	endp




externdef copy320to256_:near
copy320to256_ proc
			pushad
			mov edi,eax
			mov esi,edx
			mov ecx,64000/20
again:
			mov eax,[esi]
			mov ebx,[esi+5]
			mov edx,[esi+10]
			mov ebp,[esi+15]
			mov [edi],eax
			mov [edi+4],ebx
			mov [edi+8],edx
			mov [edi+12],ebp
			add esi,20
			add edi,16
			dec ecx
			jnz again
			popad
			ret
copy320to256_ endp

externdef xfade_:near
xfade_ proc
		pushad
		mov edi,eax
		mov esi,edx
		mov eax,ebx
		mov ebx,_multab
		mov edx,ebx
		mov bh,cl
		mov dh,al
		mov ecx,640*480/4
again:
		mov bl,[esi+2]
		mov dl,[edi+2]
		mov al,[ebx]
		add al,[edx]
		mov bl,[esi+3]
		mov dl,[edi+3]
		mov ah,[ebx]
		add ah,[edx]
		shl eax,16
		mov bl,[esi]
		mov dl,[edi]
		mov al,[ebx]
		add al,[edx]
		mov bl,[esi+1]
		mov dl,[edi+1]
		mov ah,[ebx]
		add ah,[edx]
		xor eax,80808080h
		mov [edi],eax
		add esi,4
		add edi,4
		dec ecx
		jnz again
		popad
		ret
xfade_ endp

externdef ghosto_:near
externdef _cliptab128:byte

ghosto_ proc
		pushad
		mov edi,eax
		mov esi,edx
		mov ecx,64000/4
		xor ebx,ebx
		xor edx,edx
again:
		mov eax,[edi]
		shr eax,2
		and eax,1f1f1f1fh
		add eax,[esi]
		repeat 2
		mov bl,al
		mov dl,ah
		externdef _cliptab128:byte
		mov al,_cliptab128[ebx]
		mov ah,_cliptab128[edx]
		rol eax,16
		endm
		mov [edi],eax
		add edi,4
		add esi,4
		dec ecx
		jnz again
		popad
		ret
ghosto_ endp

end