/* vi: set sw=4 ts=4: */
/*
 * KON2 - Kanji ON Console -
 * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
 *
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

/*
  This code is based on vgalib.
  
  Thanks to frandsen@diku.dk (Tommy Frandsen).
  */
#include	<stdio.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<fcntl.h>
#include	<string.h>

#include	"getcap.h"
#include	"defs.h"
#include	"errors.h"
#include	"vc.h"
#include	"hzinput.h"
#include	"vga.h"
#include	"fb.h"
#include	"font.h"

#if defined(SUPPORT_VGA)

#ifdef HAVE_SYS_IOCTL_H
#include	<sys/ioctl.h>
#endif

#ifdef HAVE_SYS_MMAN_H
#include	<sys/mman.h>
#endif

#if defined(__SunOS__) 
#include    <sys/sysi86.h>
#endif

#ifdef USE_LRMI
#include    <lrmi.h>
#endif

#if !defined(__FreeBSD__)
#undef USE_FREEBSD_IOCTL
#endif

static struct pelRegs grapPels, textPels;

static u_int vgaCrtAddr = 0x3D4;
static u_int vgaCrtData = 0x3D5;
static u_int vgaSt1Addr = 0x3DA;   /* State Register */

#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)
	int	FreeBSDVideoMode, origFreeBSDVideoMode;
#endif

static	u_char	*fontBuff1;		/* saved font data - plane 2 */
static	u_char	*fontBuff2;		/* saved font data - plane 3 */
static	bool	savePlane3 = FALSE; /* Rui He, always FALSE, no longer support */

#if !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL)

int LineComp9, LineComp8; /*, gramHead; */

struct vgaRegs
    regText,
    regGraph = {
	{	/* CRT */  /* CRT 25, ATT 21, GRA 9, SEQ 5, MISC 1, Total 61 */
	    0x5F,0x4F,0x50,0x82,0x54,0x80,0x0B,0x3E,
	    0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,
	    0xEA,0x2C,0xDF,0x28,0x00,0xE7,0x04,0xE3,
	    0xFF 
	}, {	/* ATT OK, reg 0x11 is the overscan register */
	    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
	    0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
	    0x01,0x00,0x0F,0x00,0x00
	}, {	/* GRA */
	    0x00,0x0F,0x00,0x20,0x00,0x00,0x05,0x0F, 
	    0xFF
	}, {	/*SEQ svgalib's value is 02, 02/03 all OK */
	    0x03,0x01,0x0F,0x00,0x06
	}, /* MIS */
	    0xE3
    };

void VgaSetRegisters(struct vgaRegs *regs)
{
    int	i;
    
    PortInb(vgaSt1Addr);	
    PortOutb(0x00, VGAATTR_A_O);
    
    PortOutb((regs->mis&~0x1)|(PortInb(VGAMISC_IN)&0x01), VGAMISC_OUT);
    
    PortOutb(0x00,VGASEQ_ADDR);
    PortOutb(0x01,VGASEQ_DATA);	
    
    for (i = 1; i < VGASEQ_CNT; i++) 
    {
		PortOutb(i, VGASEQ_ADDR);
		PortOutb(regs->seq[i], VGASEQ_DATA);
    }
    
    PortOutb(0x00, VGASEQ_ADDR);
    PortOutb(0x03, VGASEQ_DATA);
    
    PortOutb(0x11, vgaCrtAddr);
    PortOutb(PortInb(vgaCrtData)&0x7F, vgaCrtData);
    
    for (i = 0; i < VGACRT_CNT; i++) 
    {
		PortOutb(i, vgaCrtAddr);
		PortOutb(regs->crt[i], vgaCrtData);
    }
    
    for (i = 0; i < VGAGRP_CNT; i++) 
    {
		PortOutb(i, VGAGRP_ADDR);
		PortOutb(regs->gra[i], VGAGRP_DATA);
    }
    
    for (i = 0; i < VGAATTR_CNT; i++) 
    {

		PortInb(vgaSt1Addr);
		PortOutb(i, VGAATTR_A_O);
		PortOutb(regs->att[i],VGAATTR_A_O);
    }
}

static void VgaSetPels(struct pelRegs *pels)
{
    int i;
    
    PortOutb(0, VGAPAL_OADR);

    for (i = 0; i < MAX_PELS; i++) 
	{
		PortOutb(pels->red[i], VGAPAL_DATA);
		PortOutb(pels->grn[i], VGAPAL_DATA);
		PortOutb(pels->blu[i], VGAPAL_DATA);
    }
}

static void VgaGetPels(struct pelRegs *pels)
{
    int	i;

    PortOutb(0, VGAPAL_IADR);

    for (i = 0; i < MAX_PELS; i++)
    {
		pels->red[i] = PortInb(VGAPAL_DATA);
		pels->grn[i] = PortInb(VGAPAL_DATA);
		pels->blu[i] = PortInb(VGAPAL_DATA);
    }
}

#endif   /* !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL) */

void VgaStart(void)
{
#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)

	ioctl(0, FreeBSDVideoMode, 0); 

	VgaResetMode();  /* necessary, otherwise won't work */

#elif USE_LRMI

    struct LRMI_regs r;
    memset(&r, 0, sizeof(struct LRMI_regs));

    r.eax = 0x12;
    LRMI_int(0x10, &r); /* Int 0x10 VideoMode=0x12, to 640x480x16 */

	VgaResetMode();

#else  /* Operate VGA registers directly */
    int	i;

#if defined(__Minix__)   
	VgaOpMapVidMem();
#endif

    VgaGetPels(&textPels);

    if ((fontBuff1 = malloc(FONT_SIZE)) == NULL ||
       (savePlane3 && (fontBuff2 = malloc(FONT_SIZE)) == NULL))
    {
        fatal("malloc fontBuff failed!");
    }
    
    PortInb(vgaSt1Addr);   /* State Register 0x3DA */	
    PortOutb(0x00, VGAATTR_A_O);  /* Attribute Register 0x3C0 */

    for (i = 0; i < VGACRT_CNT; i++) 
    {
		PortOutb(i, vgaCrtAddr);
		regText.crt[i] = PortInb(vgaCrtData);
    }
    
	for (i = 0; i < VGAATTR_CNT; i++) 
	{
		PortInb(vgaSt1Addr);
		PortOutb(i, VGAATTR_A_O);
		regText.att[i] = PortInb(VGAATTR_DATA);
    }
	
    for (i = 0; i < VGAGRP_CNT; i++) 
	{
		PortOutb(i, VGAGRP_ADDR);
		regText.gra[i] = PortInb(VGAGRP_DATA);
    }
	
    for (i = 0; i < VGASEQ_CNT; i++) 
	{
		PortOutb(i, VGASEQ_ADDR);
		regText.seq[i] = PortInb(VGASEQ_DATA);
    }
    regText.mis = PortInb(VGAMISC_IN);
    
    PortOutb(PortInb(VGAMISC_IN)|0x01, VGAMISC_OUT);
    VgaSetRegisters(&regGraph);
    
    PortOutw(0x0204, VGAGRP_ADDR);
	VgaOpFont(fontBuff1, FONT_SIZE, SAVE_FONT); /* FONT_SIZE=0x2000, 8K */

    if (savePlane3) 
    {

		PortOutw(0x0304, VGAGRP_ADDR);
		VgaOpFont(fontBuff2, FONT_SIZE, SAVE_FONT);
    }

#endif

	VgaOpClear(dispInfo.gydim - dispInfo.input, dispInfo.input, INPUT_BGCOLOR);
}

void VgaTextMode(void)
{
#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)

	ioctl(0, origFreeBSDVideoMode, 0);

#elif USE_LRMI

    struct LRMI_regs r;
    memset(&r, 0, sizeof(struct LRMI_regs));

#if defined(linux)
    ioperm(0, 0x400, 1);
    iopl(3);
#endif
    
    r.eax = 0x3;
    LRMI_int(0x10, &r);  /* Int 0x10, VideoMode=0x3, 80x25 Text Mode */

#else

    PortInb(vgaSt1Addr);
    PortOutb(0x00, VGAATTR_A_O);

    VgaSetRegisters(&regGraph);

    PortOutb(0x01, VGAGRP_ADDR );
    PortOutb(0x00, VGAGRP_DATA ); /* 0x00->Enable Set/Reset, disable it */
    
    PortOutb(0x02, VGASEQ_ADDR );
    PortOutb(0x04, VGASEQ_DATA );
	VgaOpFont(fontBuff1, FONT_SIZE, RESTORE_FONT);
    if (savePlane3) 
    {

		PortOutb(0x02, VGASEQ_ADDR );
		PortOutb(0x08, VGASEQ_DATA );
		VgaOpFont(fontBuff2, FONT_SIZE, RESTORE_FONT);
    }
    
    VgaSetRegisters(&regText);
    
    VgaSetPels(&textPels);
    
    PortInb(vgaSt1Addr);
    PortOutb(0x20, VGAATTR_A_O);

#endif

}

void VgaGraphMode(void)
{
#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)

	ioctl(0, FreeBSDVideoMode, 0);

	VgaResetMode();

#elif USE_LRMI

    struct LRMI_regs r;
    memset(&r, 0, sizeof(struct LRMI_regs));

    r.eax = 0x12;
    LRMI_int(0x10, &r); /* Int 0x10, VideoMode=0x12 to 640x480x16 */
    
	VgaResetMode();

#else

    PortInb(vgaSt1Addr); 		
    PortOutb(0x00, VGAATTR_A_O);	
    
    VgaSetRegisters(&regGraph);

    VgaSetPels(&grapPels);
    
    PortInb(vgaSt1Addr);
    PortOutb(0x20, VGAATTR_A_O);
    VgaSetStartAddress();

#endif
	
	VgaOpClear(0, dispInfo.gydim - dispInfo.input, DEF_BGCOLOR);

}

#if 0
void VgaWput(int x, int y, u_char ch1, u_char ch2, int fg, int bg)
{
	writeAddr = x + y * dispInfo.tlineByte;

	VgaOpDraw(GetDBFontBits(dbFReg, ch1, ch2), fg, bg, fontwidth*2, fontheight);
}

void VgaSput(int x, int y, u_char ch, int fg, int bg)
{
	writeAddr = x + y * dispInfo.tlineByte;

	VgaOpDraw(GetSBFontBits(sbFReg, ch), fg, bg, fontwidth, fontheight);
}

void VgaInputSput(int x, u_char ch,int fg,int bg)
{
   sbFReg = &fSRegs[0];

   writeAddr = INPUT_YPOS * dispInfo.glineByte + (x % dispInfo.tx_avail);

	VgaOpDraw( GetSBFontBits(sbFReg, ch), fg, bg, fontwidth, fontheight);

}

void VgaInputWput(int encoding, int x, u_char ch1,u_char ch2,int fg,int bg)
{
	u_char *bits;

	dbFReg = &fDRegs[Coding2FontRegDB[encoding]];
    
	writeAddr = INPUT_YPOS * dispInfo.glineByte + (x % dispInfo.tx_avail);

	if (encoding == CODE_SJIS)
		ShiftJIS2JIS(ch1, ch2, &ch1, &ch2);

	bits = GetDBFontBits(dbFReg, ch1, ch2);
    if (bits) VgaOpDraw(bits, fg, bg, 2*fontwidth, fontheight);
}

#endif

#if 0
void VgaInputDrawIcon(int x, u_char *bitmap, int width, int height, int fg, int bg)
{
	int xpos, ypos;

	xpos = (x % dispInfo.tx_avail) /* * fontwidth */;   /* VGA mode is different! */
	if (width < 2*fontwidth) xpos += (2*fontwidth - width)/2;
	ypos = INPUT_YPOS;
	if (height < fontheight) ypos += (fontheight - height)/2;

	writeAddr = ypos * dispInfo.glineByte + xpos;   /* 8 * bpp/8 = bpp  */

	VgaOpDraw(bitmap, fg, bg, width, height);
}

void VgaSetCursorAddress(CursorInfo *ci, u_int x, u_int y)
{
    ci->addr = y * dispInfo.tlineByte + cursorTop
		* dispInfo.glineByte + x;
}
#endif

void VgaScreenSaver(bool blank)
{
#ifndef USE_LRMI
    if (blank) 
	{
		PortOutb(0x01, VGASEQ_ADDR);
		PortOutb(PortInb(VGASEQ_DATA) | 0x20, VGASEQ_DATA);
    } 
	else 
	{
		PortOutb(0x01, VGASEQ_ADDR);
		PortOutb(PortInb(VGASEQ_DATA) & 0xDF, VGASEQ_DATA);
    }
#endif
}

static struct videoTimings_t
{
    int hDot, hStart, hEnd, hTotal;
    int vLine, vStart, vEnd, vTotal;
    int clock,txmax, tymax;
} 
video = 
{       /* 640x480x4 MemSize=38400 */
        640, 680, 768, 800,
        480, 491, 493, 525,
        1,   80,  25

};

static int VgaInitRegs(void)
{
    int i;

#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)
	video_info_t vmode;

	origFreeBSDVideoMode = GetOrigFreeBSDVideoMode();
	FreeBSDVideoMode =  GetBestFreeBSDConsVidMode(&vmode);

	if (FreeBSDVideoMode == -1) /* none are supported */
	{
		message("Fatal Error: Both 800x600x4 & 640x480x4 modes are not supported.\n");
		message("Try 'kldload vesa' or recompile CCE without enable-fbioctl option.\n");
		return FAILURE;
	}

	dispInfo.gxdim = vmode.vi_width;
	dispInfo.gydim = vmode.vi_height;

#else  /* LRMI or Direct VGA */

	dispInfo.gxdim = video.hDot;
    dispInfo.gydim = video.vLine;

#endif
	
	dispInfo.glineByte = dispInfo.gxdim >> 3;
	InitDispInfo();

    dispInfo.bpp = 1;
	dispInfo.fd_fb = -1;
	dispInfo.type = TYPE_VGA;

    for(i = 0; i < MAX_PELS; i++)
    {
       grapPels.red[i] = red8[i];
       grapPels.grn[i] = green8[i];
       grapPels.blu[i] = blue8[i];
    };

	message("Using %dx%d 4-bpp %s(%s) mode, %dx%d terminal.\n", 
            dispInfo.gxdim, dispInfo.gydim, 

#if defined(__FreeBSD__) && defined(USE_FREEBSD_IOCTL)
			"FreeBSD-ioctl",
#elif defined(USE_LRMI)
			"LRMI VGA",
#else
			"Direct VGA",
#endif

#if defined(USE_DEVMEM_FILE)
			"file",
#else
			"mem",
#endif
			dispInfo.tx_avail, dispInfo.tymax);

#if !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL)

    if (video.vLine < 480) 
    {
		regGraph.crt[23] = 0xE3;   /*23=0x17 */
		regGraph.mis = 0xE3;
    } 
    else 
    {
		if (video.vLine < 768) regGraph.mis = 0xE3;
		else regGraph.mis = 0x23;
		regGraph.crt[23] = 0xC3;
    }

    regGraph.mis |= (video.clock & 3) << 2;
    regGraph.crt[0] = (video.hTotal>>3) - 5;
    regGraph.crt[1] = (video.hDot>>3) - 1;
    regGraph.crt[2] = (video.hStart>>3) - 1;
    regGraph.crt[3] = ((video.hEnd>>3) & 0x1F) | 0x80;
    regGraph.crt[4] = video.hStart>>3;
    regGraph.crt[5] = (((video.hEnd>>3) & 0x20) << 2)
		| ((video.hEnd>>3) & 0x1F);
    regGraph.crt[6] = (video.vTotal - 2) & 0xFF;

    regGraph.crt[7] = 0x10;

    regGraph.crt[7] |= (((video.vLine - 1) & 0x100) >> 7)
	| (((video.vLine - 1) & 0x200) >> 3);

    regGraph.crt[7] |= ((video.vStart & 0x100) >> 6)
	| ((video.vStart & 0x100) >> 5);

    regGraph.crt[7] |= (((video.vTotal - 2) & 0x100) >> 8)
	| (((video.vTotal - 2) & 0x200) >> 4);

    regGraph.crt[7] |= ((video.vStart & 0x200) >> 2);

    regGraph.crt[9] = ((video.vStart & 0x200) >>4) | 0x40;

    regGraph.crt[16] = video.vStart & 0xFF;
    regGraph.crt[17] = (video.vEnd & 0x0F) | 0x20;
    regGraph.crt[18] = (video.vLine - 1) & 0xFF;
    regGraph.crt[19] = video.hDot >> 4;
    regGraph.crt[21] = video.vStart & 0xFF;
    regGraph.crt[22] = (video.vStart + 1) & 0xFF;

    LineComp8 = ((regGraph.crt[7] & 0xEF) << 8) + 0x07;

    LineComp9 = ((regGraph.crt[9] & 0xBF) << 8) + 0x09;

#endif

	return SUCCESS;
}

static int VgaEnableIO(void)
{
#if defined(__QNX__)

	ThreadCtl(_NTO_TCTL_IO, 0); /* enable in/out stuff */
	
#elif defined(__Minix__)

#elif defined(linux)

    ioperm(VGAMISC_IN, 1, 1);
    if (!(PortInb(VGAMISC_IN)&0x01)) 
    { /* monochrome VGA */
		vgaCrtAddr = 0x3B4;
		vgaCrtData = 0x3B5;
		vgaSt1Addr = 0x3BA;
    }

    ioperm(vgaCrtAddr, 1, 1);
    ioperm(VGAATTR_A_O, 1, 1);
    ioperm(VGAGRP_ADDR, 1, 1);
    ioperm(VGASEQ_ADDR, 1, 1);
    ioperm(VGAPAL_OADR, 1, 1);
    ioperm(VGAPAL_IADR, 1, 1);
    ioperm(vgaCrtData, 1, 1);
    ioperm(VGAATTR_DATA, 1, 1);
    ioperm(VGAGRP_DATA, 1, 1);
    ioperm(VGASEQ_DATA, 1, 1);
    ioperm(VGAMISC_IN, 1, 1);
    ioperm(VGAMISC_OUT, 1, 1);
    ioperm(vgaSt1Addr, 1, 1);
    ioperm(VGAPAL_DATA, 1, 1);

#elif defined(__SunOS__) 

	sysi86(SI86V86, V86SC_IOPL, PS_IOPL );

#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__Lynx__) || defined(__SCO__)

    if (ioctl(0, KDENABIO,0) < 0) 
    {
		Perror("ioctl CONSOLE_IO_ENABLE");
		return FAILURE;
    }

#endif 

    return SUCCESS;
}

static void VgaDisableIO(void)
{
#if defined(__SunOS__) 
	sysi86(SI86V86, V86SC_IOPL, 0); /* PS_IOPL ); */
	
#elif defined(linux)
    ioperm(vgaCrtAddr, 1, 0);
    ioperm(VGAATTR_A_O, 1, 0);
    ioperm(VGAGRP_ADDR, 1, 0);
    ioperm(VGASEQ_ADDR, 1, 0);
    ioperm(VGAPAL_OADR, 1, 0);
    ioperm(VGAPAL_IADR, 1, 0);
    ioperm(vgaCrtData, 1, 0);
    ioperm(VGAATTR_DATA, 1, 0);
    ioperm(VGAGRP_DATA, 1, 0);
    ioperm(VGASEQ_DATA, 1, 0);
    ioperm(VGAMISC_IN, 1, 0);
    ioperm(VGAMISC_OUT, 1, 0);
    ioperm(vgaSt1Addr, 1, 0);
    ioperm(VGAPAL_DATA, 1, 0);

#else

#endif
}

int VgaAttach(void)
{
#if defined(__Minix__) 
	return SUCCESS;   /* delay video ram mmap to VgaStart() */
#else

	if (VgaEnableIO() != SUCCESS) return FAILURE;

#ifdef USE_LRMI
    if ( LRMI_init() == -1 ) 
	{
		Perror("LRMI_init");
       	return FAILURE;
    }

#if defined(linux)
    ioperm(0, 0x400, 1);
    iopl(3);
#endif

#endif

	return VgaOpMapVidMem();  /* doing file-based or memory-based */

#endif  /* __Minix__ */
}

void VgaDetach(void)
{

	VgaDisableIO();

	VgaOpUnmapVidMem();

#if !defined(USE_LRMI)
    if (fontBuff1) SafeFree((void **)&fontBuff1);
    if (savePlane3 && fontBuff2)
		SafeFree((void **)&fontBuff2);
#endif
}

#if !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL)

void VgaSetStartAddress()
{
    int	til;
    int gramHead = 0;
 
    PortOutw((gramHead  & 0xff00) | 0x0c, vgaCrtAddr);
    PortOutw((gramHead << 8) | 0x0d, vgaCrtAddr);

    til = (dispInfo.gydim - 1  /* - (gramHead / dispInfo.glineByte) */) << 4;

    PortOutw((til << 4) | 0x18, vgaCrtAddr);

    PortOutw((til & 0x1000) | LineComp8, vgaCrtAddr);

    PortOutw(((til & 0x2000) << 1) | LineComp9, vgaCrtAddr);

}

#endif

int VgaInit(void)
{
    if (VgaInitRegs() == FAILURE) 
		return FAILURE;

    return VgaAttach();
}

VideoInfo VgaInfo =
{
    VgaInit,
    VgaStart,
    VgaTextMode,
    VgaGraphMode,
    CCEWput,
    CCEInputWput,
    CCESput,
    CCEInputSput,
    CCEInputDrawIcon,
    CCECursor,
    CCEClear,
    VgaScreenSaver,
    VgaDetach,
};

#if !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL)

#define R5x_MASK	0x7737

union s3Regs 
{
    struct 
    {
	u_char r3[10];/* Video Atribute (CR30-34, CR38-3C) */
	u_char rx[33];/* Video Atribute (CR40-65) */
    } x;
    struct 
    {
	u_char
	    x30, x31, x32, x33, x34,
	    x38, x39, x3a, x3b, x3c;
	u_char
	    x40, x41, x42, x43, x44, x45, x46, x47,
	    x48, x49, x4a, x4b, x4c, x4d, x4e, x4f,
	    x50, x51, x53, x54, x55, x58, x59, x5a, x5c, x5d, x5e,
	    x60, x61, x62, x63, x64, x65;
    } r;
} s3Text, s3Graph;

static void VgaS3SetRegisters(union s3Regs *regs)
{
    int i, n;

    PortOutw(0xa539, vgaCrtAddr); /* unlock system control regs */
    for (i = 0; i < 5; i ++) 
    {
		PortOutb(0x30 + i, vgaCrtAddr);
		PortOutb(regs->x.r3[i], vgaCrtData);
		PortOutb(0x38 + i, vgaCrtAddr);
		PortOutb(regs->x.r3[5+i], vgaCrtData);
    }

    for (i = 0; i < 16; i ++) 
    {
		PortOutb(0x40 + i, vgaCrtAddr);
		PortOutb(regs->x.rx[i], vgaCrtData);
    }
    for (n = 16, i = 0; i < 16; i ++) 
    {
		if ((1 << i) & R5x_MASK) 
        {
	    	PortOutb(0x50 + i, vgaCrtAddr);
	    	PortOutb(regs->x.rx[n], vgaCrtData);
	    	n ++;
		}
    }
    for (i = 0; i < 6; i ++, n ++) 
    {
		PortOutb(0x60 + i, vgaCrtAddr);
		PortOutb(s3Text.x.rx[n], vgaCrtData);
    }
}

static void VgaS3TextMode(void)
{
    VgaTextMode();
    VgaS3SetRegisters(&s3Text);
}

static void VgaS3InitRegs(void)
{
    regGraph.mis |= 0x0D;

    regGraph.crt[23] = 0xE3;

    s3Graph.r.x5e = (((video.vTotal - 2) & 0x400) >> 10)
	| (((video.vLine - 1) & 0x400) >> 9)
	    | ((video.vStart & 0x400) >> 8)
		| ((video.vStart & 0x400) >> 6) | 0x40;
    s3Graph.r.x5d = ((video.hTotal & 0x800) >> 11)
	| ((video.hDot & 0x800) >> 10)
	    | ((video.hStart & 0x800) >> 9)
		| ((video.hStart & 0x800) >> 7);
}

static void VgaS3GraphMode(void)
{

    s3Graph.r.x5c = 0x20;
    s3Graph.r.x31 = 0x8D;
    s3Graph.r.x32 = 0;
    s3Graph.r.x33 = 0x20;
    s3Graph.r.x34 = 0x10;

    s3Graph.r.x3b = (regGraph.crt[0] + regGraph.crt[4] + 1) / 2;
    s3Graph.r.x3c = regGraph.crt[0] / 2;
    s3Graph.r.x40 = (s3Text.r.x40 & 0xF6) | 1;
    s3Graph.r.x43 = s3Text.r.x44 = 0;
    s3Graph.r.x45 = s3Text.r.x45 & 1;

    s3Graph.r.x50 = s3Text.r.x50 & ~0xC1;
    s3Graph.r.x51 = (s3Text.r.x51 & 0xC0) | ((dispInfo.gxdim >> 7) & 0x30);
    s3Graph.r.x53 = s3Text.r.x53 & ~0x30;
    s3Graph.r.x54 = 0xA0;
    s3Graph.r.x55 = (s3Text.r.x55 & 8) | 0x40;
    s3Graph.r.x58 = 0;
    s3Graph.r.x5d |= s3Graph.r.x5d & ~0x17;
    s3Graph.r.x60 = 0x3F;
    s3Graph.r.x61 = 0x81;
    s3Graph.r.x62 = 0;
    if (dispInfo.gxdim < 800) {
	s3Graph.r.x50 |= 0x40;
	s3Graph.r.x42 = 0xb;
    } else if (dispInfo.gxdim < 1024) {
	s3Graph.r.x50 |= 0x80;
	s3Graph.r.x42 = 2;
    } else {
	s3Graph.r.x42 = 0xE;
    }
    VgaGraphMode();

    VgaS3SetRegisters(&s3Graph);
}

static void VgaS3Start()
{
    int i, n;

    PortOutw(0xa539, vgaCrtAddr); /* unlock system control regs */

    for (i = 0; i < 5; i ++)
    {
        PortOutb(0x30 + i, vgaCrtAddr);
        s3Text.x.r3[i] = PortInb(vgaCrtData);
        PortOutb(0x38 + i, vgaCrtAddr);
        s3Text.x.r3[i+5] = PortInb(vgaCrtData);
    }
    for (i = 0; i < 16; i ++)
    {
        PortOutb(0x40 + i, vgaCrtAddr);
        s3Text.x.rx[i] = PortInb(vgaCrtData);
    }
    for (n = 16, i = 0; i < 16; i ++)
    {
        if ((1 << i) & R5x_MASK)
        {
            PortOutb(0x50 + i, vgaCrtAddr);
            s3Text.x.rx[n] = PortInb(vgaCrtData);
            n ++;
        }
    }
    for (i = 0; i < 6; i ++, n ++)
    {
        PortOutb(0x60 + i, vgaCrtAddr);
        s3Text.x.rx[n] = PortInb(vgaCrtData);
    }
    s3Graph = s3Text;
    s3Graph.r.x39 = 0xA5;

    VgaStart();
}

static int VgaS3Init(void)
{
    if (VgaInitRegs() == FAILURE) 
		return FAILURE;

    VgaS3InitRegs();

    return VgaAttach();
}

VideoInfo VgaS3Info =
{
    VgaS3Init,
    VgaS3Start,
    VgaS3TextMode,
    VgaS3GraphMode,
    CCEWput,
    CCEInputWput,
    CCESput,
    CCEInputSput,
    CCEInputDrawIcon,
    CCECursor,
    CCEClear,
    VgaScreenSaver,
    VgaDetach
};

#endif /* !defined(USE_LRMI) && !defined(USE_FREEBSD_IOCTL) */

#endif  /* SUPPORT_VGA */

