/*
	TOMCON4.C  by Y.Kurihara (Nif PDC01620)
			サブルーチン
*/

#include <string.h>
#include <jctype.h>
#include <dos.h>
#include <machine.h>
#include "tomcon.h"

int save_page;
int save_plane;

void cur( int m )
{
	unsigned off;

	if( fl_cur == m )  return;
	fl_cur = m;

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );

	if( ly + y0 < Y1page ) {
		outp( PORTpage,0x00 );
		off = ( ly + y0 ) * ( 16 * 128 );
	} else {
		outp( PORTpage,0x10 );
		off = ( ly + y0 - Y1page ) * ( 16 * 128 );
	}
	off += ( 15 * 128 ) + ( lx + x0 );
	*VRAM( off ) = ~ *VRAM( off );

	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
	return;
}

void putank( int code )			/* 半角文字の表示 */
{
	unsigned off,add;
	int n;

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );
	outp( PORTank,1 );

	if( ly + y0 < Y1page ) {
		outp( PORTpage,0x00 );
		off = ( ly + y0 ) * ( 16 * 128 ) + ( lx + x0 );
	} else {
		outp( PORTpage,0x10 );
		off = ( ly + y0 - Y1page ) * ( 16 * 128 ) + ( lx + x0 );
	}
	add = code * 16;

	for( n = 16; --n >= 0;  ) {
		*VRAM( off ) = *ANKCG( add++ ) ^ reverse;
		off += 128;
	}

	outp( PORTank,0 );
	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
	return;
}

void putkan( unsigned code )		/* 全角文字の表示 */
{
	unsigned off;
	int n;

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );
	outp( PORTank,1 );

	if( ly + y0 < Y1page ) {
		outp( PORTpage,0x00 );
		off = ( ly + y0 ) * ( 16 * 128 ) + ( lx + x0 );
	} else {
		outp( PORTpage,0x10 );
		off = ( ly + y0 - Y1page ) * ( 16 * 128 ) + ( lx + x0 );
	}

	outp( PORTkch,code >> 8 );
	outp( PORTkcl,code );

	for( n = 16; --n >= 0;  ) {
		*VRAM( off )     = inp( PORTkfl ) ^ reverse;
		*VRAM( off + 1 ) = inp( PORTkfr ) ^ reverse;
		off += 128;
	}

	outp( PORTank,0 );
	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
	return;
}

void clr( int y1,int y2 )		/* 画面クリア */
{
	unsigned off;
	int x,y,n;

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );
	outp( PORTank,1 );

	if( y1 < Y1page ) {
		outp( PORTpage, 0x00 );
		off = y1 * 16 * 128 + x0;
	} else {
		outp( PORTpage, 0x10 );
		off = ( y1 - Y1page ) * 16 * 128 + x0;
	}
	for( y = y1; y <= y2; y++ ) {
		if( y == Y1page ) {
			outp( PORTpage, 0x10 );
			off = off - Y1page * 16 * 128;
		}
		for( n = 0; n <= 15; n++ ) {
			for( x = xl; --x >= 0; )  *VRAM( off + x ) = 0xff;
			off = off + 128;
		}
	}

	outp( PORTank,0 );
	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
}

void roll( int y1,int y2 )		/* スクロール */
{
	unsigned off;
	int x,y,n;
	static char buff[ 128 ];

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );
	outp( PORTank,1 );

	if( y1 < Y1page ) {
		outp( PORTpage, 0x00 );
		off = y1 * 16 * 128 + x0;
	} else {
		outp( PORTpage, 0x10 );
		off = ( y1 - Y1page ) * 16 * 128 + x0;
	}
	for( y = y1; y < y2; y++ ) {
		if( y == Y1page - 1 ) {
			for( n = 0; n <= 15; n++ ) {
				outp( PORTpage, 0x10 );
				movedata( 0xc000,n * 128 + x0,
					FP_SEG(buff),FP_OFF(buff),xl );
				outp( PORTpage, 0x00 );
				movedata( FP_SEG(buff),FP_OFF(buff),
					0xc000,off,xl );
				off = off + 128;
			}
			outp( PORTpage, 0x10 );
			off = off - Y1page * 16 * 128;
		} else {
			for( n = 0; n <= 15; n++ ) {
				movedata( 0xc000,off + 128 *16,0xc000,off,xl );
				off += 128;
			}
		}
	}
	for( n = 0; n <= 15; n++ ) {
		for( x = xl; --x >= 0; )  *VRAM( off + x ) = 0xff;
		off = off + 128;
	}

	outp( PORTank,0 );
	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
}

void roll2( int y1,int y2 )		/* スクロール */
{
	unsigned off,off2;
	int x,y,n;
	static char buff[ 128 ];

	save_page  = inp( PORTpage );
	save_plane = inp( PORTplane );
	outp( PORTplane,15 );
	outp( PORTank,1 );

	if( y2 < Y1page ) {
		outp( PORTpage, 0x00 );
		off = y2 * 16 * 128 + x0;
	} else {
		outp( PORTpage, 0x10 );
		off = ( y2 - Y1page ) * 16 * 128 + x0;
	}
	for( y = y2; y > y1; y-- ) {
		if( y == Y1page ) {
			off2 = off;
			off = ( Y1page - 1 ) * 16 * 128 + x0;
			for( n = 0; n <= 15; n++ ) {
				outp( PORTpage, 0x00 );
				movedata( 0xc000,n * 128 + off,
					FP_SEG(buff),FP_OFF(buff),xl );
				outp( PORTpage, 0x10 );
				movedata( FP_SEG(buff),FP_OFF(buff),
					0xc000,n * 128 + off2,xl );
			}
			outp( PORTpage, 0x00 );
		} else {
			for( n = 0; n <= 15; n++ ) {
				off -= 128;
				movedata( 0xc000,off,0xc000,off +128 *16,xl );
			}
		}
	}
	for( n = 0; n <= 15; n++ ) {
		for( x = xl; --x >= 0; )  *VRAM( off + x ) = 0xff;
		off += 128;
	}

	outp( PORTank,0 );
	outp( PORTpage,  save_page );
	outp( PORTplane, save_plane );
}

