;*****************************************************************
;
;void	VRAN_wrtstr(char *str,x,y,font,ccol,bcol,seg,off);
;
;struct {
;	int	v_x, v_y;		/* Displry X ,Y */
;	int	v_font;			/* Font Size 16 or 8 */
;	int	v_ccol, v_bcol;		/* color */
;	int	v_seg;			/* write segment */
;	int	v_off;			/* VRAM offset Y */
;} PARA;
;
; font = Font Size 8 or 16 , (if bit7 == 1 then duble font) 
;
;*****************************************************************
v_para	struc
v_x	dd	?
v_y	dd	?
v_font	dd	?
v_ccol	dd	?
v_bcol	dd	?
v_seg	dd	?
v_off	dd	?
v_para	ends

extrn	MOS_disp:near
;*************************************
;	VRAM Direct Access Program
;*************************************
bitptn	segment	'DATA'
fntptn:
	dd	000000000h,0F0000000h,00F000000h,0FF000000h
	dd	000F00000h,0F0F00000h,00FF00000h,0FFF00000h
	dd	0000F0000h,0F00F0000h,00F0F0000h,0FF0F0000h
	dd	000FF0000h,0F0FF0000h,00FFF0000h,0FFFF0000h
	dd	00000F000h,0F000F000h,00F00F000h,0FF00F000h
	dd	000F0F000h,0F0F0F000h,00FF0F000h,0FFF0F000h
	dd	0000FF000h,0F00FF000h,00F0FF000h,0FF0FF000h
	dd	000FFF000h,0F0FFF000h,00FFFF000h,0FFFFF000h
	dd	000000F00h,0F0000F00h,00F000F00h,0FF000F00h
	dd	000F00F00h,0F0F00F00h,00FF00F00h,0FFF00F00h
	dd	0000F0F00h,0F00F0F00h,00F0F0F00h,0FF0F0F00h
	dd	000FF0F00h,0F0FF0F00h,00FFF0F00h,0FFFF0F00h
	dd	00000FF00h,0F000FF00h,00F00FF00h,0FF00FF00h
	dd	000F0FF00h,0F0F0FF00h,00FF0FF00h,0FFF0FF00h
	dd	0000FFF00h,0F00FFF00h,00F0FFF00h,0FF0FFF00h
	dd	000FFFF00h,0F0FFFF00h,00FFFFF00h,0FFFFFF00h
	dd	0000000F0h,0F00000F0h,00F0000F0h,0FF0000F0h
	dd	000F000F0h,0F0F000F0h,00FF000F0h,0FFF000F0h
	dd	0000F00F0h,0F00F00F0h,00F0F00F0h,0FF0F00F0h
	dd	000FF00F0h,0F0FF00F0h,00FFF00F0h,0FFFF00F0h
	dd	00000F0F0h,0F000F0F0h,00F00F0F0h,0FF00F0F0h
	dd	000F0F0F0h,0F0F0F0F0h,00FF0F0F0h,0FFF0F0F0h
	dd	0000FF0F0h,0F00FF0F0h,00F0FF0F0h,0FF0FF0F0h
	dd	000FFF0F0h,0F0FFF0F0h,00FFFF0F0h,0FFFFF0F0h
	dd	000000FF0h,0F0000FF0h,00F000FF0h,0FF000FF0h
	dd	000F00FF0h,0F0F00FF0h,00FF00FF0h,0FFF00FF0h
	dd	0000F0FF0h,0F00F0FF0h,00F0F0FF0h,0FF0F0FF0h
	dd	000FF0FF0h,0F0FF0FF0h,00FFF0FF0h,0FFFF0FF0h
	dd	00000FFF0h,0F000FFF0h,00F00FFF0h,0FF00FFF0h
	dd	000F0FFF0h,0F0F0FFF0h,00FF0FFF0h,0FFF0FFF0h
	dd	0000FFFF0h,0F00FFFF0h,00F0FFFF0h,0FF0FFFF0h
	dd	000FFFFF0h,0F0FFFFF0h,00FFFFFF0h,0FFFFFFF0h
	dd	00000000Fh,0F000000Fh,00F00000Fh,0FF00000Fh
	dd	000F0000Fh,0F0F0000Fh,00FF0000Fh,0FFF0000Fh
	dd	0000F000Fh,0F00F000Fh,00F0F000Fh,0FF0F000Fh
	dd	000FF000Fh,0F0FF000Fh,00FFF000Fh,0FFFF000Fh
	dd	00000F00Fh,0F000F00Fh,00F00F00Fh,0FF00F00Fh
	dd	000F0F00Fh,0F0F0F00Fh,00FF0F00Fh,0FFF0F00Fh
	dd	0000FF00Fh,0F00FF00Fh,00F0FF00Fh,0FF0FF00Fh
	dd	000FFF00Fh,0F0FFF00Fh,00FFFF00Fh,0FFFFF00Fh
	dd	000000F0Fh,0F0000F0Fh,00F000F0Fh,0FF000F0Fh
	dd	000F00F0Fh,0F0F00F0Fh,00FF00F0Fh,0FFF00F0Fh
	dd	0000F0F0Fh,0F00F0F0Fh,00F0F0F0Fh,0FF0F0F0Fh
	dd	000FF0F0Fh,0F0FF0F0Fh,00FFF0F0Fh,0FFFF0F0Fh
	dd	00000FF0Fh,0F000FF0Fh,00F00FF0Fh,0FF00FF0Fh
	dd	000F0FF0Fh,0F0F0FF0Fh,00FF0FF0Fh,0FFF0FF0Fh
	dd	0000FFF0Fh,0F00FFF0Fh,00F0FFF0Fh,0FF0FFF0Fh
	dd	000FFFF0Fh,0F0FFFF0Fh,00FFFFF0Fh,0FFFFFF0Fh
	dd	0000000FFh,0F00000FFh,00F0000FFh,0FF0000FFh
	dd	000F000FFh,0F0F000FFh,00FF000FFh,0FFF000FFh
	dd	0000F00FFh,0F00F00FFh,00F0F00FFh,0FF0F00FFh
	dd	000FF00FFh,0F0FF00FFh,00FFF00FFh,0FFFF00FFh
	dd	00000F0FFh,0F000F0FFh,00F00F0FFh,0FF00F0FFh
	dd	000F0F0FFh,0F0F0F0FFh,00FF0F0FFh,0FFF0F0FFh
	dd	0000FF0FFh,0F00FF0FFh,00F0FF0FFh,0FF0FF0FFh
	dd	000FFF0FFh,0F0FFF0FFh,00FFFF0FFh,0FFFFF0FFh
	dd	000000FFFh,0F0000FFFh,00F000FFFh,0FF000FFFh
	dd	000F00FFFh,0F0F00FFFh,00FF00FFFh,0FFF00FFFh
	dd	0000F0FFFh,0F00F0FFFh,00F0F0FFFh,0FF0F0FFFh
	dd	000FF0FFFh,0F0FF0FFFh,00FFF0FFFh,0FFFF0FFFh
	dd	00000FFFFh,0F000FFFFh,00F00FFFFh,0FF00FFFFh
	dd	000F0FFFFh,0F0F0FFFFh,00FF0FFFFh,0FFF0FFFFh
	dd	0000FFFFFh,0F00FFFFFh,00F0FFFFFh,0FF0FFFFFh
	dd	000FFFFFFh,0F0FFFFFFh,00FFFFFFFh,0FFFFFFFFh
bitptn	ends

VRAM	segment 'CODE'
	assume  cs:VRAM
;*************************************
;	漢字フォントの取り出し
;*************************************
; in	eax    = Kanji Code
; out	es:esi = Kanji Font Address
;*************************************
kanfon	proc	near
	push	eax
	push	edx
	push	fs
	push	ds

	mov	ebx,eax
	bt	ebx,15
	jnc	kanfn0

	cmp	bh,9fh
	jbe	sjto1
	sub	bh,0B1h-071h
sjto1:  sub	bh,071h
	shl	bh,1
	inc	bh
	cmp	bl,7fh
	jbe	sjto2
	dec	bl
sjto2:	cmp	bl,9eh
	jb	sjto3
	sub	bl,7dh
	inc	bh
	jmp	kanfn0
sjto3:	sub	bl,1fh

kanfn0:	mov	ax,0110h
	mov	fs,ax
	mov	ax,0100h
	mov	dx,1010h
	call	pword ptr fs:[00a0h]
	pop	ds
	pop	fs
	pop	edx
	pop	eax
	ret
kanfon	endp
;**************************************
;	Write Kanji Code (JIS or SJIS)
;**************************************
; in	eax = kanji Code
;	edi = VRAM Address
; out	edi = next VRAM Address
;**************************************
wrtkan	proc	near
	push	ds
	push	edi
	push	esi

	call	kanfon

	mov	ecx,[ebp].v_font
	mov	ch,cl
	and	cl,18h

wrtkn1:	mov	dx,es:[esi]
	lea	esi,[esi+2]
	bt	ecx,8+4
	jc	wrtkn2
	or	dx,es:[esi]
	lea	esi,[esi+2]

wrtkn2:	bt	ecx,8+7
	jnc	wrtkn3	
	xchg	dh,dl
	mov	ax,dx
	sar	dx,1
	or	dx,ax
	xchg	dh,dl

wrtkn3:	movzx	eax,dl
	mov	eax,ds:[eax*4+offset fntptn]
	mov	ebx,eax
	not	ebx
	and	eax,[ebp].v_ccol
	and	ebx,[ebp].v_bcol
	or	eax,ebx
	mov	fs:[edi],eax

	movzx	eax,dh
	mov	eax,ds:[eax*4+offset fntptn]
	mov	ebx,eax
	not	ebx
	and	eax,[ebp].v_ccol
	and	ebx,[ebp].v_bcol
	or	eax,ebx
	mov	fs:[edi+4],eax

	add	edi,512
	and	edi,03FFFFh
	add	edi,[ebp].v_seg
	dec	cl
	jnz	wrtkn1

	pop	esi
	pop	edi
	pop	ds
	add	edi,8
	ret
wrtkan	endp
;***********************************
;	Write ANK code
;***********************************
; in	eax = ANK Code
;	edi = VRAM Address
; out	edi = next VRAM Address
;***********************************
wrtank	proc	near
        push   	edi
        push   	esi

	movzx	esi,al
	mov	ecx,[ebp].v_font
	mov	ch,cl
	and	cl,18h
	bt	ecx,8+4
	jnc	wrtak1
	shl	esi,4
	add	esi,3D800h
	jmp	wrtak2
wrtak1:	shl	esi,3
	add	esi,3D000h

wrtak2:	movzx	eax,byte ptr es:[esi]
	inc	esi
	bt	ecx,8+7
	jnc	wrtak3
	mov	dx,ax
	sar	ax,1
	or	ax,dx
wrtak3:	mov	eax,ds:[eax*4+offset fntptn]
	mov	ebx,eax
	not	ebx
	and	eax,[ebp].v_ccol
	and	ebx,[ebp].v_bcol
	or	eax,ebx
	mov	fs:[edi],eax
	add	edi,512
	and	edi,03FFFFh
	add	edi,[ebp].v_seg
	dec	cl
	jnz	wrtak2

        pop    	esi
        pop    	edi
	add	edi,4
        ret
wrtank	endp
;***********************************
;	Color Pattan Seting
;***********************************
; int	EAX Color No
; out   EAX Color Pattn
;***********************************
colptn	proc	near
	push	ebx
	mov	ebx,eax
	shl	eax,4
	or	eax,ebx
	mov	ebx,eax
	shl	eax,8
	or	eax,ebx
	mov	ebx,eax
	shl	eax,16
	or	eax,ebx
	pop	ebx
	ret
colptn	endp	
;***********************************
;	Write String
;***********************************
	public	VRAM_wrtstr
VRAM_wrtstr proc near
	push	0
	call	MOS_disp
	add	esp,4

        push    ebp
        mov    	ebp,esp
        push   	esi
        push   	edi
	push	ebx
	push	es
	push	fs

	mov	esi,8[ebp]
	lea	ebp,12[ebp]

	mov	edi,[ebp].v_y
	add	edi,[ebp].v_off
	shl	edi,9
	mov	eax,[ebp].v_x
	sar	eax,1
	add	edi,eax
	and	edi,03FFFFh
	add	edi,[ebp].v_seg

	mov	eax,[ebp].v_ccol
	call	colptn
	mov	[ebp].v_ccol,eax
	mov	eax,[ebp].v_bcol
	call	colptn
	mov	[ebp].v_bcol,eax

	mov	ax,138h
	mov	es,ax
	mov	eax,104h
	mov	fs,ax

wrtst1:	mov	al,[esi]
	inc	esi
	or	al,al
	jz	wrtst6
	cmp	al,81h
	jb	wrtst4
	cmp	al,9fh
	jbe	wrtst3
	cmp	al,0e0h
	jb	wrtst4
	cmp	al,0fch
	jnbe	wrtst4
wrtst3:	mov	ah,al
	mov	al,[esi]
	inc	esi
	cmp	al,40h
	jb	wrtst5
	cmp	al,7fh
	jz	wrtst5
	cmp	al,0fch
	jnbe	wrtst5
	call	wrtkan
	jmp	wrtst1

wrtst5:	mov	al,ah
	dec	esi
wrtst4:	call	wrtank
	jmp	wrtst1

wrtst6:	pop	fs
	pop	es
	pop	ebx
        pop    	edi
        pop    	esi
	pop	ebp

	push	1
	call	MOS_disp
	add	esp,4
        ret
VRAM_wrtstr endp
;***************************************
;	write Bit Ptan
;*****************************************************
;void	wrtptn(char *ptn, x, y, sx, sy, col, bak)
;		     8   12 16  20  24  28   32
;*****************************************************
	public	wrtptn
wrtptn	proc
	push	0
	call	MOS_disp
	add	esp,4

        push    ebp
        mov    	ebp,esp
        push   	esi
        push   	edi
	push	ebx
	push	es

	mov	esi,8[ebp]
	mov	edi,16[ebp]
	shl	edi,9
	mov	eax,12[ebp]
	sar	eax,1
	add	edi,eax

	mov	eax,28[ebp]
	call	colptn
	mov	28[ebp],eax
	mov	eax,32[ebp]
	call	colptn
	mov	32[ebp],eax

	sar	dword ptr 20[ebp],3

	mov	ax,104h
	mov	es,ax

wrtpt1:	mov	ecx,20[ebp]
	push	edi
wrtpt2:	movzx	eax,byte ptr[esi]
	inc	esi
	mov	eax,ds:[eax*4+offset fntptn]
	mov	ebx,eax
	not	ebx
	and	eax,28[ebp]
	and	ebx,32[ebp]
	or	eax,ebx
	mov	es:[edi],eax
	lea	edi,[edi+4]
	loop	wrtpt2
	pop	edi
	add	edi,512
	dec	dword ptr 24[ebp]
	jnz	wrtpt1

	pop	es
	pop	ebx
        pop    	edi
        pop    	esi
        leave

	push	1
	call	MOS_disp
	add	esp,4
        ret
wrtptn	endp
;***************************************************
;	Clear VRAM
;***************************************************
;void	VRAM_clear(x, y, sizeX, sizeY, col, seg, off);
;		   8  12 16     20     24   28   32
;***************************************************
	public	VRAM_clear
VRAM_clear proc
        push    ebp
        mov    	ebp,esp
        push   	esi
        push   	edi
	push	ebx
	push	es

	mov	esi,12[ebp]
	add	esi,32[ebp]
	shl	esi,9
	mov	eax,8[ebp]
	sar	eax,1
	add	esi,eax
	and	esi,03FFFFh
	add	esi,28[ebp]

	mov	ebx,16[ebp]
	sar	ebx,3

	mov	eax,104h
	mov	es,ax

	cld
	mov	edx,20[ebp]
	mov	eax,24[ebp]
	call	colptn
clear1:	mov	ecx,ebx
	mov	edi,esi
	rep stosd
	add	esi,512
	and	esi,03FFFFh
	add	esi,28[ebp]
	dec	edx
	jnz	clear1

	pop	es
	pop	ebx
        pop    	edi
        pop    	esi
        leave
        ret
VRAM_clear endp
;******************************
;void	VRAM_allclear(seg);
;******************************
	public	VRAM_allclear
VRAM_allclear proc
	mov	eax,4[esp]
	push	edi
	push	es

	mov	ecx,1024*512/8
	mov	edi,eax
	mov	ax,104h
	mov	es,ax
	xor	eax,eax
	cld
	rep stosd

	pop	es
	pop	edi
	ret
VRAM_allclear endp
;**************************************************************
;	page move VRAM
;**************************************************************
;void	VRAM_move(x, y, sizeX, sizeY, Sseg, Dseg, Soff, Doff);
;		  8  12 16     20     24    28    32    36
;**************************************************************
	public	VRAM_move
VRAM_move proc
        push    ebp
        mov    	ebp,esp
        push   	esi
        push   	edi
	push	ebx
	push	ds
	push	es

	mov	esi,12[ebp]
	shl	esi,9
	mov	eax,8[ebp]
	sar	eax,1
	add	esi,eax
	mov	edi,esi

	mov	eax,32[ebp]
	shl	eax,9
	add	esi,eax
	and	esi,03FFFFh
	add	esi,24[ebp]

	mov	eax,36[ebp]
	shl	eax,9
	add	edi,eax
	and	edi,03FFFFh
	add	edi,28[ebp]

	mov	ax,104h
	mov	ds,ax
	mov	es,ax

	mov	eax,16[ebp]
	mov	ebx,512
	sar	eax,1
	sub	ebx,eax
	sar	eax,2

	cld
	mov	edx,20[ebp]
vmove1:	mov	ecx,eax
	rep movsd
	add	esi,ebx
	and	esi,03FFFFh
	add	esi,24[ebp]
	add	edi,ebx
	and	edi,03FFFFh
	add	edi,28[ebp]
	dec	edx
	jnz	vmove1

	pop	es
	pop	ds
	pop	ebx
        pop    	edi
        pop    	esi
        leave
        ret
VRAM_move endp
;**************************************************************
;	BitMap Img to GVRAM
;**************************************************************
;void	IMG_move(struct img_para *para);
;struct imsg_para {
;	int	im_page;	/* 0x00000 or 0x40000 */
;	int	im_off;		/* Disp Offset */
;	int	im_x, im_y;	/* Display Point */
;	int	im_sx,im_sy;	/* Display Size */
;	char	*img_adr;	/* img address */
;	int	im_ix,im_iy;	/* img Display Point */
;	int	im_isx,im_isy;	/* img size */
;**************************************************************
i_para	struc
i_page	dd	?
i_off	dd	?
i_x	dd	?
i_y	dd	?
i_sx	dd	?
i_sy	dd	?
i_adr	dd	?
i_ix	dd	?
i_iy	dd	?
i_isx	dd	?
i_isy	dd	?
i_para	ends

	public	IMG_move
IMG_move proc
	push	0
	call	MOS_disp
	add	esp,4
        push    ebp
        mov    	ebp,[esp+8]
        push   	esi
        push   	edi
	push	ebx
	push	es

	mov	edi,[ebp].i_y
	shl	edi,9
	mov	eax,[ebp].i_x
	sar	eax,1
	add	edi,eax
	mov	eax,[ebp].i_off
	shl	eax,9
	add	edi,eax
	and	edi,03FFFFh
	add	edi,[ebp].i_page

	mov	ecx,[ebp].i_iy
	mov	eax,[ebp].i_isx
	sar	eax,1
	xor	esi,esi
	or	ecx,ecx
	jz	IMG_m3
IMG_m2:	add	esi,eax
	loop	IMG_m2
IMG_m3:	mov	eax,[ebp].i_ix
	sar	eax,1
	add	esi,eax
	add	esi,[ebp].i_adr

	mov	ax,104h
	mov	es,ax

	mov	edx,[ebp].i_sy
	mov	eax,[ebp].i_sx
	sar	eax,1
	mov	ebx,eax
	and	ebx,3
	sar	eax,2
	sar	[ebp].i_isx,1
	cld
IMG_m1:	
	push	esi
	push	edi
	mov	ecx,ebx
	jecxz	IMG_m4
	rep movsb
IMG_m4:	mov	ecx,eax
	rep movsd
	pop	edi
	pop	esi
	add	esi,[ebp].i_isx
	add	edi,512
	and	edi,03FFFFh
	add	edi,[ebp].i_page
	dec	edx
	jnz	IMG_m1

	pop	es
	pop	ebx
        pop    	edi
        pop    	esi
        pop	ebp
	push	1
	call	MOS_disp
	add	esp,4
        ret
IMG_move endp

VRAM	ends
	end
