/*
 *  GRAPHIC ROUTINE  for FM-16 beta
 *
 *  Turbo-C ver 1.5  Small model
 *
 */

#ifndef _DEF_DOS_H_
#include <dos.h>
#endif

#define uchar unsigned char

static union REGS inregs;
static union REGS outregs;
static struct SREGS segregs;
static int lastcolor;

struct gds_data {
	unsigned int length;
	unsigned int command;
	int x1;
	int y1;
	int x2;
	int y2;
	int x3;
	int y3;
};

static union work_area {
	struct gds_data com;
	int area[200];
}

work;

void ginit(void)
{
	segread(&segregs);
	inregs.x.di = (unsigned int)&work;
	inregs.h.ah = 0x80;
	int86x(0x92, &inregs, &outregs, &segregs);
}

void gscreen(int mode)
{
	*((char far *)0xC000FF81) = mode;
}

void gcls(void)
{
	inregs.h.ah = 0x84;
	int86x(0x92, &inregs, &outregs, &segregs);
}

static void callg(void)
{
	inregs.h.ah = 0x8f;
	inregs.x.di = (unsigned int)&work;
	int86x(0x92, &inregs, &outregs, &segregs);
}

void gline(int x1, int y1, int x2, int y2)
{
	work.com.length = 0x0a;
	work.com.command = 0x1000 * 4 + 0x20 * 2 + 8;
	work.com.x1 = x1;
	work.com.y1 = y1;
	work.com.x2 = x2;
	work.com.y2 = y2;
	callg();
}

void gpset(int x, int y)
{
	work.com.length = 0x06;
	work.com.command = 0x1000 * 4 + 0x20 * 3 + 4;
	work.com.x1 = x;
	work.com.y1 = y;
	callg();
}

void gcolor(int color)
{
	work.com.length = 0x04;
	work.com.command = 0x1000 * 5 + 0x20 * 4 + 2;
	work.com.x1 = color;
	callg();
	work.com.command = 0x1000 * 5 + 0x20 * 8 + 2;
	callg();
	work.com.command = 0x1000 * 5 + 0x20 * 28 + 2;
	callg();
}

void gbcolor(int color)
{
	inregs.h.ah = 0x90;
	inregs.x.dx = color;
	int86(0x92, &inregs, &outregs);
}

void gstop(void)
{
	inregs.h.ah = 0xfe;
	int86x(0x92, &inregs, &outregs, &segregs);
}

static void _colorstr(int color, int *parmup, int *parmdown)
{
	int blue, red, green;

	if (color & 1)
		blue = 128;
	else
		blue = 0;
	if (color & 2)
		red = 128;
	else
		red = 0;
	if (color & 4)
		green = 128;
	else
		green = 0;
	if (color & 8)
		red = red + 64;
	*parmup = red << 8;
	*parmdown = (blue << 8) + green;
}

void gpallete(int num, int color)
{
	work.area[0] = 6;
	work.area[1] = num;
	_colorstr(color, &work.area[2], &work.area[3]);
	inregs.h.ah = 0x83;
	segread(&segregs);
	inregs.x.di = (int)&work;
	int86x(0x92, &inregs, &outregs, &segregs);
}

void gmovedsp(int x, int y)
{
	inregs.h.ah = 0xfb;
	inregs.x.dx = x;
	inregs.x.cx = y;
	int86(0x92, &inregs, &outregs);
}

/* end of file : graph_fm.c */
