extrn	btm_ptr,Line_no,Cur_X,Cur_Y,top_ptr,chk_ptr
extrn	cke_ptr,ent_ptr,Line_X,Line_Y,CONTRL,TAB,lin_ptr
extrn	Cnv_ptr,sprintf,Nxt_line,Dsp_vram,Get_key,ins_chr

;************************************
;void	Set_vram()
;************************************
	public	Set_vram
Set_vram proc	near
	push	ebp
	mov	ebp,esp
	sub	esp,28
	push	ebx
	push	esi
	push	edi

;    int      ch,i,x,y,col,ln,ol;
;    register UCHAR   *vp;
;    register LONG    ptr;
;    UCHAR    *sp;

;    sp = (Wind_flg != FALSE ? dram : vram);

	mov	eax,offset dram
	cmp	Wind_flg,0
	jne	SetVr1
	mov	eax,offset vram
SetVr1:
	mov	[ebp-28],eax
ReSetVr:

;    memset(sp,0,MAX_X * MAX_Y * 2);

	xor	eax,eax
	mov	ecx,1080
	mov	edi,[ebp-28]
	rep	stosd

;    ptr = top_ptr;
	mov	esi,top_ptr

;    col = COLOR;
	movzx	edx,COLOR
	mov	[ebp-16],edx		; col

;    ol = (-1);
	mov	dword ptr [ebp-24],-1

;    for ( y = 0 ; y < MAX_Y ; y++ )
;	Line_tmp[y] = (-1);
	mov	eax,-1
	mov	ecx,27
	mov	edi,offset Line_tmp
	rep	stosd

;    for ( ln = y = 0 ; y < MAX_Y ; y++ ) {
	sub	eax,eax
	mov	[ebp-12],eax		; y
	mov	[ebp-20],eax		; ln

SetYLop:

;    	vp = sp + (MAX_X * 2) * y;
	mov	edi,[ebp-12]
	lea	edi,[eax+eax*4]
	shl	edi,5
	add	edi,[ebp-28]

;	if ( ln == ol )
;	    Line_tmp[y] = (-1);
;	else
;	    ol = Line_tmp[y] = ln;

	mov	eax,[ebp-20]
	cmp	eax,[ebp-24]
	jne	SetNxt1
	mov	eax,[ebp-12]
	mov	dword ptr Line_tmp[eax*4],-1
	jmp	SetNxt2
SetNxt1:
	mov	eax,[ebp-12]
	mov	edx,[ebp-20]
	mov	Line_tmp[eax*4],edx
	mov	[ebp-24],edx
SetNxt2:

;	for ( x = 0 ; x < MAX_X ; ) {
	sub	ebx,ebx

;	    if ( chk_ptr != ERR ) {
;		if ( ptr >= chk_ptr && ptr < cke_ptr )
;		    col = ((COLOR - 1)&0x0F) | 0x10;
;		else
;		    col = COLOR;
;	    }
	mov	eax,chk_ptr
	cmp	eax,-1
	je	SetNxt3

	cmp    eax,esi
	jnbe   SetNx31
	cmp    esi,cke_ptr
	jnb    SetNx31

	movzx	eax,COLOR
	dec	eax
	and	eax,15
	or	al,16
	jmp	SetNx32
SetNx31:
	movzx	eax,COLOR
SetNx32:
	mov	[ebp-16],eax
SetNxt3:

;	    if ( ptr == ent_ptr ) {
;		Line_X = Cur_X = x;
;		Cur_Y = y;
;		Line_Y = ln;
;	    }
	cmp	esi,ent_ptr
	jne	SetNxt4
	mov	Cur_X,ebx
	mov	eax,edi
	mov    word ptr Line_X,ax
	mov    eax,-12[ebp]
	mov    Cur_Y,eax
	mov    ax,word ptr -20[ebp]
	mov    word ptr Line_Y,ax
SetNxt4:

;	    if ( ptr >= btm_ptr ) {
.L05ba:
	cmp    ebx,btm_ptr
	jb     05dc
;		vp = Set_Str(sp,vp,col,"[EOF]");
	push   offset _mwLITERALS+16
	push   dword ptr -16[ebp]
	push   esi
	push   dword ptr -28[ebp]
	call   0044
	xchg   eax,esi
	add    esp,16
;		goto ENDOF;
	jmp    0778
;	    }
;	    ch = *Cnv_ptr(ptr);
.L05dc:
	push   ebx
	call   Cnv_ptr
	movzbl eax,[eax]
	mov    -4[ebp],eax
	add    esp,4
;	    if ( ch == '\x0D' ) {
	cmp    eax,13
	jne    0612
;		if ( CONTRL > 0 ) {
	cmp    dword ptr CONTRL,0
	jle    0607
;		    *(vp++) = (col-2)&0x0F;
	mov    al,-16[ebp]
	sub    al,2
	and    al,15
	mov    [esi],al
	inc    esi
;		    *(vp++) = CRCHR;
	mov    byte ptr [esi],31
	inc    esi
;		}
;		ptr += 2;
.L0607:
	add    ebx,2
;		ln++;
	inc    dword ptr -20[ebp]
;		break;
	jmp    072d
;	    } else if ( ch == '\t' ) {
.L0612:
	cmp    dword ptr -4[ebp],9
	jne    06a6
;		i = ((x + TAB) / TAB) * TAB - x;
	mov    eax,edi
	add    eax,TAB
	cwd
	idiv   dword ptr TAB
	imul   eax,TAB
	sub    eax,edi
	mov    -8[ebp],eax
;		if ( CONTRL > 1 && i-- > 0 ) {
	cmp    dword ptr CONTRL,1
	jle    0654
	dec    dword ptr -8[ebp]
	dec    eax
	jl     0654
;		    *(vp++) = (col-1)&0x0F;
	mov    al,-16[ebp]
	dec    eax
	and    al,15
	mov    [esi],al
	inc    esi
;		    *(vp++) = TABCHR;
	mov    byte ptr [esi],9
	inc    esi
;		    x++;
	inc    edi
;		}
;		if ( CONTRL > 1 ) {
.L0654:
	cmp    dword ptr CONTRL,1
	jle    0683
;		    while ( i-- > 0 && x < MAX_X ) {
.L065d:
	mov    eax,-8[ebp]
	dec    dword ptr -8[ebp]
	dec    eax
	jl     0727
	cmp    edi,80
	jnl    0727
;			*(vp++) = (col-1)&0x0F;
	mov    al,-16[ebp]
	dec    eax
	and    al,15
	mov    [esi],al
	inc    esi
;			*(vp++) = '.';
	mov    byte ptr [esi],46
	inc    esi
;		        x++;
	inc    edi
	jmp    065d
;		    }
;		} else {
;		    while ( i-- > 0 && x < MAX_X ) {
.L0683:
	mov    eax,-8[ebp]
	dec    dword ptr -8[ebp]
	dec    eax
	jl     0727
	cmp    edi,80
	jnl    0727
;			*(vp++) = col;
	mov    al,-16[ebp]
	mov    [esi],al
	inc    esi
;			*(vp++) = ' ';
	mov    byte ptr [esi],32
	inc    esi
;		        x++;
	inc    edi
	jmp    0683
;		    }
;		}
;		ptr++;
;	    } else if ( iskanji(ch) && iskan2(ptr) != FALSE ) {
.L06a6:
	mov    eax,-4[ebp]
	cmp    eax,129
	jl     06b7
	cmp    eax,159
	jle    06c8
.L06b7:
	mov    eax,-4[ebp]
	cmp    eax,224
	jl     071a
	cmp    eax,252
	jnle   071a
.L06c8:
	push   ebx
	call   0000
	add    esp,4
	and    eax,eax
	je     071a
;		if ( x != (MAX_X - 1) ) {
	cmp    edi,79
	je     070a
;#ifdef	TOWNS
;		    *(vp++) = col|0x40;
	mov    al,-16[ebp]
	or     al,64
	mov    [esi],al
	inc    esi
;		    *(vp++) = ch; ptr++;
	mov    al,-4[ebp]
	mov    [esi],al
	inc    esi
	inc    ebx
;		    *(vp++) = col|0x80;
	mov    al,-16[ebp]
	or     al,128
	mov    [esi],al
	inc    esi
;		    *(vp++) = *Cnv_ptr(ptr++);
	mov    eax,ebx
	inc    ebx
	push   eax
	call   Cnv_ptr
	mov    al,[eax]
	mov    [esi],al
	inc    esi
;#else
;		    i = (ch << 8); ptr++;
;		    i |= (*Cnv_ptr(ptr++) & 0xff);
;		    i = sjistojis(i);
;		    *(vp++) = col|0x40;
;		    *(vp++) = i >> 8;
;		    *(vp++) = col|0x80;
;		    *(vp++) = i & 0xff;
;#endif
;		    x += 2;
	add    edi,2
	add    esp,4
	jmp    0558
;		} else {
;		    *(vp++) = col;
.L070a:
	mov    al,-16[ebp]
	mov    [esi],al
	inc    esi
;		    *(vp++) = 0x1C;
	mov    byte ptr [esi],28
	inc    esi
;		    x++;
	inc    edi
	jmp    0558
;		}
;	    } else {
;		*(vp++) = col;
.L071a:
	mov    al,-16[ebp]
	mov    [esi],al
	inc    esi
;		*(vp++) = ch;
	mov    al,-4[ebp]
	mov    [esi],al
	inc    esi
;		x++; ptr++;
	inc    edi
.L0727:
	inc    ebx
	jmp    0558
.L072d:
	inc    dword ptr -12[ebp]
	jmp    0518
;	    }
;	}
;    }
;    if ( ptr <= lin_ptr ) {
.L0735:
	cmp    ebx,lin_ptr
	jnbe   0778
;	for ( i = 0 ; top_ptr < btm_ptr && i < 5 ; i++ ) {
	mov    dword ptr -8[ebp],0
.L0744:
	mov    eax,top_ptr
	cmp    eax,btm_ptr
	jnb    04ce
	cmp    dword ptr -8[ebp],5
	jnl    04ce
;	    top_ptr = Nxt_line(top_ptr);
	push   eax
	call   Nxt_line
	mov    top_ptr,eax
;	    Line_no++;
	inc    dword ptr Line_no
	inc    dword ptr -8[ebp]
	add    esp,4
	jmp    0744
;	}
;	goto RESET;
;    }
;ENDOF:
;    if ( Wind_flg != FALSE )
.L0778:
	cmp    word ptr @SCRN+4336,0
	je     0787
;	Set_vram_sub();
	call   0224
;}
.L0787:
	pop    edi
	pop    esi
	pop    ebx
	leave
	rets
Set_vram	endp
