;	EXPAND & OVER SAMPLING
;	32K color mode (512*512)
;	call from H-C
;	expand17( work, para )
;
;
;		1989 12 Hiroshi TODA
;
;
;	( xg )	   ( m1  m2 )  ( xp-xp0 )
;	(    )	=  (	    )  (	)
;	( yg )	   ( m3  m4 )  ( yp-yp0 )
;
;
;		 xg * 2^16
;	xg1 = ----------------- + xg0
;		kx*yg + 2^16
;
;		 yg * 2^16
;	yg1 = ----------------- + yg0
;		ky*xg + 2^16
;
;
;
;	data&(12)=gadd,DS,m1,m2,m3,m4,kx,ky,xp0,yp0,xg0,yg0,xa1,ya1,xa2,ya2,mix
;
;	(xa1,ya1)=put area min
;	(xa2,ya2)=put area max
;	(xg,yg)=over sampling 10000H mul cor. 


	.386p

work	struc

;data area
gadd	dd	?	; data address
seg1	dd	?	; vram segment
m1	dd	?	; matrix
m2	dd	?
m3	dd	?
m4	dd	?
kx	dd	?
ky	dd	?
xp0	dd	?	; put co.(0,0)
yp0	dd	?
xg0	dd	?	; get co.(0,0)
yg0	dd	?
xa1	dd	?	; put area min
ya1	dd	?
xa2	dd	?	; put area max
ya2	dd	?
mix1	dd	?	; mix rate
;work
xadd	dd	?	; add area
yadd	dd	?
buf1	dd	?
xp	dd	?	; put co.
yp	dd	?
xm	dd	?	; xp-xp0
ym	dd	?
xg	dd	?	; get co.
yg	dd	?
xg1	dd	?
yg1	dd	?
mix2	dd	?	; 256-mix1
;over sampling
number	dd	?	; data
a1	dd	?	; 4point color data
a2	dd	?
a3	dd	?
a4	dd	?
kx1	dd	?	; k
kx2	dd	?
ky1	dd	?
ky2	dd	?
k1	dd	?
k2	dd	?
k3	dd	?
k4	dd	?
ab	dd	?	; BRG
ar	dd	?
ag	dd	?
adata	dd	?
work	ends

        cseg	segment	'CODE'
	assume	cs:cseg

xmax	equ	512			; cor. max
ymax	equ	255

xdspmax	equ	319			; display max
ydspmax	equ	239

	public	expand17
	db	'matrix17',8
expand17	proc	near
	push	ebp
	mov	ebp,esp			; ebp <-- param area top add
	push	esi
	push	edi
	push	ebx
	mov	esi,[ebp+8]		; esi <-- work area top add
	mov	ecx,[ebp+12]		; ecx <-- para. add.
	xor	edx,edx			; edx=count
main01:	mov	eax,[ecx][edx*4]	; para. --> work area
	mov	[esi][edx*4],eax
	inc	edx
	cmp	edx,17
	jb	main01
	mov	eax,256
	sub	eax,[esi].mix1
	mov	[esi].mix2,eax
	mov	eax,[esi].ya1		; ini.
	mov	[esi].yp,eax
	mov	eax,[esi].seg1		; fs : v-ram segment
	mov	fs,ax

main02:	mov	eax,[esi].xa1
	mov	[esi].xp,eax

main03:	mov	eax,[esi].xp		; xm,ym cal.
	sub	eax,[esi].xp0
	mov	[esi].xm,eax
	mov	eax,[esi].yp
	sub	eax,[esi].yp0
	mov	[esi].ym,eax

	mov	eax,[esi].xm		; xg cal.
	imul	dword ptr [esi].m1
	mov	[esi].xadd,eax
	mov	eax,[esi].ym
	imul	dword ptr [esi].m2
	add	eax,[esi].xadd
	mov	[esi].xg,eax

	mov	eax,[esi].xm		; yg cal.
	imul	dword ptr [esi].m3
	mov	[esi].yadd,eax
	mov	eax,[esi].ym
	imul	dword ptr [esi].m4
	add	eax,[esi].yadd
	mov	[esi].yg,eax

	mov	eax,[esi].yg		; xg1 cal.
	imul	dword ptr [esi].kx
	shrd	eax,edx,16
	add	eax,10000H
	mov	[esi].xadd,eax
	jns	main0A			; kx*yg + 10000H ck.
	neg	eax
main0A:	mov	[esi].buf1,eax
	xor	ecx,ecx
	add	ecx,[esi].xg		; xg ck.
	jns	main0B
	neg	ecx
main0B:	shr	ecx,14
	cmp	ecx,[esi].buf1
	jae	main04
	mov	eax,10000H
	imul	dword ptr [esi].xg
	idiv	dword ptr [esi].xadd
	add	eax,[esi].xg0
	mov	[esi].xg1,eax
	cmp	eax,xdspmax*10000H
	ja	main04

	mov	eax,[esi].xg		; yg1 cal.
	imul	dword ptr [esi].ky
	shrd	eax,edx,16
	add	eax,10000H
	mov	[esi].yadd,eax
	jns	main0C
	neg	eax
main0C:	mov	[esi].buf1,eax
	xor	ecx,ecx
	add	ecx,[esi].yg
	jns	main0D
	neg	ecx
main0D:	shr	ecx,13
	cmp	ecx,[esi].buf1
	jae	main04
	mov	eax,10000H
	imul	dword ptr [esi].yg
	idiv	dword ptr [esi].yadd
	add	eax,[esi].yg0
	mov	[esi].yg1,eax
	cmp	eax,ydspmax*10000H
	ja	main04

	call	smpl			; over sampling
	jc	main04
	mov	edx,[esi].yp		; edx <- vram address
	shl	edx,9
	add	edx,[esi].xp
	shl	edx,1
	test	word ptr fs:[edx],8000h	; mask check
	jne	main04
	cmp	[esi].mix1,256		; mix check
	jb	main0E
	mov	eax,[esi].adata		; not mix
	mov	fs:[edx],ax
	jmp	main04
main0E:	mov	eax,[esi].ab		; B	RAM DATA
	imul	eax,[esi].mix1
	mov	[esi].ab,eax
	mov	eax,[esi].ar		; R
	imul	eax,[esi].mix1
	mov	[esi].ar,eax
	mov	eax,[esi].ag		; G
	imul	eax,[esi].mix1
	mov	[esi].ag,eax
	mov	ax,fs:[edx]		; B	VRAM DATA
	and	eax,1Fh
	imul	eax,[esi].mix2
	add	eax,[esi].ab
	add	eax,80h			; 四捨五入
	shr	eax,8
	mov	[esi].ab,eax
	mov	ax,fs:[edx]		; R
	and	eax,1Fh*32
	imul	eax,[esi].mix2
	add	eax,[esi].ar
	shr	eax,8
	add	eax,10h			; 四捨五入
	and	eax,1Fh*32		; clear
	mov	[esi].ar,eax
	mov	ax,fs:[edx]		; G
	and	eax,1Fh*1024
	imul	eax,[esi].mix2
	add	eax,[esi].ag
	shr	eax,8
	add	eax,10h*32		; 四捨五入
	and	eax,1Fh*1024		; clear
	add	eax,[esi].ab
	add	eax,[esi].ar
	mov	fs:[edx],ax
main04:	mov	eax,[esi].xp		; loop end
	inc	eax
	mov	[esi].xp,eax
	cmp	eax,[esi].xa2
	jbe	main03
	mov	eax,[esi].yp
	inc	eax
	mov	[esi].yp,eax
	cmp	eax,[esi].ya2
	jbe	main02
	xor	eax,eax
	pop	ebx
	pop	edi
	pop	esi
	mov	esp,ebp
	pop	ebp
	ret

smpl:	mov	eax,[esi].yg1		; number cal.
	shr	eax,16
	imul	eax,xmax		; 1line
	mov	ecx,[esi].xg1
	shr	ecx,16
	add	eax,ecx
	shl	eax,1
	add	eax,[esi].gadd
	mov	[esi].number,eax

	mov	ecx,[eax]		; a1-a4 cal.
	and	ecx,0000ffffH
	mov	[esi].a1,ecx
	mov	ecx,[eax]
	shr	ecx,16
	mov	[esi].a2,ecx
	mov	ecx,[eax+xmax*2]		; 1line=512dot
	and	ecx,0000ffffH
	mov	[esi].a3,ecx
	mov	ecx,[eax+xmax*2]		; 1line=512dot
	shr	ecx,16
	mov	[esi].a4,ecx

	mov	eax,[esi].xg1		; kx1-ky2
	and	eax,0000ffffH
	mov	[esi].kx1,eax
	mov	eax,00010000H
	sub	eax,[esi].kx1
	mov	[esi].kx2,eax
	mov	eax,[esi].yg1
	and	eax,0000ffffH
	mov	[esi].ky1,eax
	mov	eax,00010000H
	sub	eax,[esi].ky1
	mov	[esi].ky2,eax

	mov	eax,[esi].kx2		; k1-k4
	imul	dword ptr [esi].ky2
	shrd	eax,edx,16
	mov	[esi].k1,eax
	mov	eax,[esi].kx1
	imul	eax,[esi].ky2
	shr	eax,16
	mov	[esi].k2,eax
	mov	eax,[esi].kx2
	imul	dword ptr [esi].ky1
	shrd	eax,edx,16
	mov	[esi].k3,eax
	mov	eax,[esi].kx1
	imul	dword ptr [esi].ky1
	shrd	eax,edx,16
	mov	[esi].k4,eax

	mov	edx,0			; polygon data check
	mov	eax,[esi].k1
	cmp	eax,[esi].k2
	ja	smpl01
	mov	eax,[esi].k2
	mov	edx,1
smpl01:	cmp	eax,[esi].k3
	ja	smpl02
	mov	eax,[esi].k3
	mov	edx,2
smpl02:	cmp	eax,[esi].k4
	ja	smpl03
	mov	edx,3
smpl03:	mov	eax,[esi][edx*4].a1
	cmp	eax,7fffH
	ja	smpl04
	stc
	jmp	smpl05

smpl04:	mov	eax,[esi].a1		; ab cal
	and	eax,0000001fH
	imul	eax,[esi].k1
	mov	[esi].ab,eax
	mov	eax,[esi].a2
	and	eax,0000001fH
	imul	eax,[esi].k2
	add	[esi].ab,eax
	mov	eax,[esi].a3
	and	eax,0000001fH
	imul	eax,[esi].k3
	add	[esi].ab,eax
	mov	eax,[esi].a4
	and	eax,0000001fH
	imul	eax,[esi].k4
	add	eax,[esi].ab
	add	eax,8000h		; 四捨五入
	shr	eax,16
	mov	[esi].ab,eax

	mov	eax,[esi].a1		; ar  cal.
	and	eax,000003e0H
	imul	eax,[esi].k1
	mov	[esi].ar,eax
	mov	eax,[esi].a2
	and	eax,000003e0H
	imul	eax,[esi].k2
	add	[esi].ar,eax
	mov	eax,[esi].a3
	and	eax,000003e0H
	imul	eax,[esi].k3
	add	[esi].ar,eax
	mov	eax,[esi].a4
	and	eax,000003e0H
	imul	eax,[esi].k4
	add	eax,[esi].ar
	shr	eax,16
	add	eax,10h			; 四捨五入
	and	eax,1Fh*32		; clear
	mov	[esi].ar,eax

	mov	eax,[esi].a1		; ag cal.
 	and	eax,00007c00H
	imul	eax,[esi].k1
	mov	[esi].ag,eax
	mov	eax,[esi].a2
	and	eax,00007c00H
	imul	eax,[esi].k2
	add	[esi].ag,eax
	mov	eax,[esi].a3
	and	eax,00007c00H
	imul	eax,[esi].k3
	add	[esi].ag,eax
	mov	eax,[esi].a4
	and	eax,00007c00H
	imul	eax,[esi].k4
	add	eax,[esi].ag
	shr	eax,16
	add	eax,10h*32		; 四捨五入
	and	eax,1Fh*1024		; clear
	mov	[esi].ag,eax

	mov	eax,[esi].ab
	add	eax,[esi].ar
	add	eax,[esi].ag
	mov	[esi].adata,eax
	clc
smpl05:	ret

expand17	endp

        cseg	ends
	end

