#include    <stdlib.h>
#include    <ctype.h>
#include    "defs.h"

#define	PAGE0	0
#define	PAGE1	0x40000

extern short ReWrt_flg;
extern int   MAX_SCR;

extern void wrtank(int ch,int x,int y,int fc,int bc,int of);
extern void wrtkan(int ch,int x,int y,int fc,int bc,int of);
extern void disp_cur(int x,int y,int of);
extern void memcpy();
extern void memset();

UCHAR *dmy_vram=(UCHAR *)NULL;

/************************
void	wrtstr(str,x,y,cl)
unsigned char	*str;
int	x,y,cl;
{
    int	    fc,bc;
    register UCHAR  *mp;

    if ( (cl & 0x10) != 0 ) {
        bc = cl & 0x0F; fc = 0;
    } else {
        fc = cl & 0x0F; bc = 0;
    }

    mp = dmy_vram + (x * 2 + y * (MAX_X * 2));
    while ( *str != '\0' ) {
	if ( iskanji(*str) && iskanji2(*(str+1)) ) {
	    wrtkan((*str << 8) | *(str+1),x,y,fc,bc,PAGE0);
	    *(mp++) = cl | 0x40;
	    *(mp++) = *(str++);
	    *(mp++) = cl | 0x80;
	    *(mp++) = *(str++);
	    x += 2;
	} else if ( *str == '\x1B' ) {
	    str++;
	} else {
	    wrtank(*str,x,y,fc,bc,PAGE0);
	    *(mp++) = cl;
	    *(mp++) = *(str++);
	    x++;
	}
	if ( x >= MAX_X ) {
	   y++; x = 0;
	}
    }
}
*********************************/
void	locate()
{
    UCHAR  *vp;
    vp = (dmy_vram + (Cur_X + OFF_X) * 2 + (Cur_Y + OFF_Y) * (MAX_X * 2));
    *vp |= 0x20;
    disp_cur(Cur_X + OFF_X,Cur_Y + OFF_Y,PAGE0);
}
/***************************************
void    Dsp_vram(vp)
register UCHAR	*vp;
{
    int     x,y;
    int     ch,fc,bc;
    register UCHAR  *mp;

    mp = dmy_vram + (OFF_X * 2 + OFF_Y * (MAX_X * 2));
    for ( y = 0 ; y < MAX_SCR ; y++ ) {
	for ( x = 0 ; x < MAX_X ; x++,vp+=2,mp+=2 ) {
	    if ( *vp != *mp || *(vp+1) != *(mp+1) ) {
		if ( x > 0 && (*vp & 0x80) != 0 && (*(vp-2) & 0x40) != 0 ) {
		    x--; vp-=2; mp-=2;
		}
		if ( (*vp & 0x10) != 0 ) {
		    bc = *vp & 0x0F;
		    fc = 0;
		} else {
		    fc = *vp & 0x0F;
        	    bc = 0;
		}      
		if ( (*vp & 0x40) != 0 && (*(vp+2) & 0x80) != 0 ) {
		    ch = (*(vp+1) << 8) | *(vp+3);
		    wrtkan(ch,x + OFF_X,y + OFF_Y + offset,fc,bc,PAGE0);
		    memcpy(mp,vp,4);
		} else {
		    wrtank(*(vp+1),x + OFF_X,y + OFF_Y + offset,fc,bc,PAGE0);
		    memcpy(mp,vp,2);
		}
	    }
	}
    }
    ReWrt_flg = ERR;
    locate();
}
*****************************************/
void	Dsp_vram_flash()
{
/*
    memset(dmy_vram,0xFF,MAX_X*MAX_Y*2);
*/
}
void	Dsp_init()
{
    dmy_vram = (unsigned char *)malloc(MAX_X*(MENU_Y+1)*2);
    memset(dmy_vram,0,MAX_X*(MENU_Y+1)*2);
}
UCHAR	*Act_vram()
{
    return (dmy_vram + (OFF_X + Cur_X) * 2 + (OFF_Y + Cur_Y) * (MAX_X * 2));
}
void	Dsp_box_vram(ct,bak)
int	ct,bak;
{
    int     ch,y,x,bc,fc,bb;
    UCHAR   *vp;

    vp = (dmy_vram + (OFF_X + Cur_X) * 2 + (OFF_Y + Cur_Y) * (MAX_X * 2));
    y = OFF_Y + Cur_Y; x = OFF_X + Cur_X;
    while ( ct-- > 0 ) {
	if ( (*vp & 0x20) != 0 )
	    bb = bak;
	else
	    bb = 0;
	if ( (*vp & 0x10) != 0 ) {
	    bc = *vp & 0x0F;
	    fc = bb;
	} else {
	    fc = *vp & 0x0F;
            bc = bb;
	}
	if ( bc != 0 && fc == bc )
	    fc = 0;      
	if ( (*vp & 0x40) != 0 && (*(vp+2) & 0x80) != 0 ) {
	    if ( x == (MAX_X-1) ) {
	        wrtank(0xFE,x,y,fc,bc,PAGE0);
	        wrtank(0xFE,OFF_X,y+1,fc,bc,PAGE0);
	    } else {
	        ch = (*(vp+1) << 8) | *(vp+3);
	        wrtkan(ch,x,y,fc,bc,PAGE0);
	    }
	    x++; vp += 4; ct--;
	} else if ( (*vp & 0x80) != 0 ) {
	    wrtank(0x20,x,y,fc,bc,PAGE0);
	    vp += 2;
	} else {
	    wrtank(*(vp+1),x,y,fc,bc,PAGE0);
	    vp += 2;
	}
	if ( ++x >= MAX_X ) {
	    x = OFF_X;
	    y++;
	}
    }
}
