;-----------------------------------------------------------------------------
;ビデオキャプチャーユーティリティー
;By 福地健太郎
;1995/8/9
;[usage]
;1.add picture
;	callm rex addres  , bank addres, picture addres
;2.bank to picture
;	callm rex addres+5, bank addres, picture addres, number of picture
;-----------------------------------------------------------------------------

;define

argBANK		equ		4
argPIC		equ		8
argNUM		equ		12

PIC_SIZE	equ		320*240

;-----------------------------------------------------------------------------
;code
;-----------------------------------------------------------------------------

	jmp		BANK
	jmp		PIC

;-----------------------------------------------------------------------------
;バンクに登録する
BANK:
	mov		esi,[esp+argPIC]
	mov		ecx,PIC_SIZE/2
	mov		edi,[esp+argBANK]
#LOOP1:
	mov		edx,[esi]

	mov		eax,edx
	and		ax,0000000000011111b
	add		[edi  ],ax

	mov		eax,edx
	shr		eax,5
	and		ax,0000000000011111b
	add		[edi+2],ax

	mov		eax,edx
	shr		eax,10
	and		ax,0000000000011111b
	add		[edi+4],ax

	mov		eax,edx
	shr		eax,16
	and		ax,0000000000011111b
	add		[edi+6],ax

	mov		eax,edx
	shr		eax,21
	and		ax,0000000000011111b
	add		[edi+8],ax

	mov		eax,edx
	shr		eax,26
	and		ax,0000000000011111b
	add		[edi+10],ax

	add		esi,4
	add		edi,12
	dec		ecx
	jnz		#LOOP1

	ret

PIC:
	mov		edi,[esp+argPIC]
	mov		ebp,PIC_SIZE
	mov		esi,[esp+argBANK]
	xor		eax,eax
	mov		ecx,[esp+argNUM]
#LOOP1:
	xor		edx,edx
	mov		ax,[esi]
	div		cl
	movzx	eax,al
	cmp		eax,31
	jna		#B_NEXT
	mov		eax,31
#B_NEXT:
	or		edx,eax

	mov		ax,[esi+2]
	div		cl
	movzx	eax,al
	cmp		eax,31
	jna		#R_NEXT
	mov		eax,31
#R_NEXT:
	shl		eax,5
	or		edx,eax

	mov		ax,[esi+4]
	div		cl
	movzx	eax,al
	cmp		eax,31
	jna		#G_NEXT
	mov		eax,31
#G_NEXT:
	shl		eax,10
	or		edx,eax

	mov		[edi],dx
	add		esi,6
	add		edi,2
	dec		ebp
	jnz		#LOOP1
;clear bank
	mov		ecx,PIC_SIZE*3/2
	mov		edi,[esp+argBANK]
	xor		eax,eax
	rep stosd

	ret
