// MajOS Microkernel v1.0
// 
// Copyright 2006 by:
//                   - Mario Köster
//                     mario.koester@gmx.de
// 
// 
// This file is part of MajOS.
//
// MajOS is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// MajOS is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MajOS; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

//-------------------------------------------------------------------------------------------------------------------------//
//Graphic modes

void VgaWriteRegisters(unsigned char *Registers)
{
	unsigned int i;

	OutputPortByte(VGA_MISC_WRITE, *Registers);
	Registers++;

	for(i = 0; i < VGA_NUM_SEQ_REGS; i++)
	{
		OutputPortByte(VGA_SEQ_INDEX, i);
		OutputPortByte(VGA_SEQ_DATA, *Registers);
		Registers++;
	}

	OutputPortByte(VGA_CRTC_INDEX, 0x03);
	OutputPortByte(VGA_CRTC_DATA, InputPortByte(VGA_CRTC_DATA) | 0x80);
	OutputPortByte(VGA_CRTC_INDEX, 0x11);
	OutputPortByte(VGA_CRTC_DATA, InputPortByte(VGA_CRTC_DATA) & ~0x80);

	Registers[0x03] |= 0x80;
	Registers[0x11] &= ~0x80;

	for(i = 0; i < VGA_NUM_CRTC_REGS; i++)
	{
		OutputPortByte(VGA_CRTC_INDEX, i);
		OutputPortByte(VGA_CRTC_DATA, *Registers);
		Registers++;
	}

	for(i = 0; i < VGA_NUM_GC_REGS; i++)
	{
		OutputPortByte(VGA_GC_INDEX, i);
		OutputPortByte(VGA_GC_DATA, *Registers);
		Registers++;
	}

	for(i = 0; i < VGA_NUM_AC_REGS; i++)
	{
		(void)InputPortByte(VGA_INSTAT_READ);
		OutputPortByte(VGA_AC_INDEX, i);
		OutputPortByte(VGA_AC_WRITE, *Registers);
		Registers++;
	}

	InputPortByte(VGA_INSTAT_READ);
	OutputPortByte(VGA_AC_INDEX, 0x20);
}

void VgaSetPlane(unsigned int Plane)
{
	unsigned char PlaneMask;

	Plane &= 3;
	PlaneMask = 1 << Plane;

	OutputPortByte(VGA_GC_INDEX, 4);
	OutputPortByte(VGA_GC_DATA, Plane);

	OutputPortByte(VGA_SEQ_INDEX, 2);
	OutputPortByte(VGA_SEQ_DATA, PlaneMask);
}

unsigned int VgaGetFramebufferAddress()
{
	unsigned int Segment;

	OutputPortByte(VGA_GC_INDEX, 6);
	Segment = InputPortByte(VGA_GC_DATA);
	Segment >>= 2;
	Segment &= 3;

	if(Segment == 1) return 0xA0000;
	else if(Segment == 2) return 0xB0000;
	else if(Segment == 3) return 0xB8000;
}

//-------------------------------------------------------------------------------------------------------------------------//
//Library functions

void PokeByte(unsigned int Segment, unsigned int Offset, unsigned char Value)
{
	unsigned int Address = (Segment * 16) + Offset;
	unsigned char *Memory = (unsigned char *) Address;
	*Memory = Value;
}

void PokeWord(unsigned int Segment, unsigned int Offset, unsigned short Value)
{
	unsigned int Address = (Segment * 16) + Offset;
	unsigned short *Memory = (unsigned short *) Address;
	*Memory = Value;
}

void vmemwr(unsigned long AddressOffset, void *Data, unsigned int Length)
{
	unsigned int Address = (VgaGetFramebufferAddress()) + AddressOffset;

	void *Memory = (unsigned char *) Address;
	unsigned int i;

	for(i = 0; i<Length; i++)
	{
		Memory = Data;
		Memory++;
	}
}

//-------------------------------------------------------------------------------------------------------------------------//
//Fonts

void VgaWriteFont(unsigned char *Font, unsigned int FontHeight)
{
	unsigned int i;

	unsigned char seq2, seq4;
	unsigned char gc4, gc5, gc6;

	OutputPortByte(VGA_SEQ_INDEX, 2);
	seq2 = InputPortByte(VGA_SEQ_DATA);

	OutputPortByte(VGA_SEQ_INDEX, 4);
	seq4 = InputPortByte(VGA_SEQ_DATA);

	OutputPortByte(VGA_SEQ_DATA, seq4 | 0x04);

	OutputPortByte(VGA_GC_INDEX, 4);
	gc4 = InputPortByte(VGA_GC_DATA);

	OutputPortByte(VGA_GC_INDEX, 5);
	gc5 = InputPortByte(VGA_GC_DATA);

	OutputPortByte(VGA_GC_DATA, gc5 & ~0x10);

	OutputPortByte(VGA_GC_INDEX, 6);
	gc6 = InputPortByte(VGA_GC_DATA);

	OutputPortByte(VGA_GC_DATA, gc6 & ~0x02);

	VgaSetPlane(2);

	for(i = 0; i < 256; i++)
	{
		vmemwr(16384u * 0 + i * 32, Font, FontHeight);
		Font += FontHeight;
	}

	/*for(i = 0; i < 256; i++)
	{
		vmemwr(16384u * 1 + i * 32, Font, FontHeight);
		Font += FontHeight;
	}*/

	OutputPortByte(VGA_SEQ_INDEX, 2);
	OutputPortByte(VGA_SEQ_DATA, seq2);
	OutputPortByte(VGA_SEQ_INDEX, 4);
	OutputPortByte(VGA_SEQ_DATA, seq4);
	OutputPortByte(VGA_GC_INDEX, 4);
	OutputPortByte(VGA_GC_DATA, gc4);
	OutputPortByte(VGA_GC_INDEX, 5);
	OutputPortByte(VGA_GC_DATA, gc5);
	OutputPortByte(VGA_GC_INDEX, 6);
	OutputPortByte(VGA_GC_DATA, gc6);
}
