// 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

//-------------------------------------------------------------------------------------------------------------------------//
//Includes

#include<standard.h>

#include"vga_definitions.inc.c"
#include"vga_modes.inc.c"

//-------------------------------------------------------------------------------------------------------------------------//
//Definitions

unsigned int ScreenWidth = 0;
unsigned int ScreenHeight = 0;
unsigned int ScreenColors = 0;
unsigned int LineHeight = 0;

unsigned char *VgaFrontBuffer = (unsigned char *) NULL;
unsigned char *VgaBackBuffer = (unsigned char *) NULL;

//-------------------------------------------------------------------------------------------------------------------------//
//Text modes

void SetTextMode80x25()
{
	VgaWriteRegisters(VgaRegisters80x25Text);

	ScreenWidth = 80;
	ScreenHeight = 25;
	LineHeight = 16;	

	VgaWriteFont(VgaFont8x16, 16);

	PokeWord(0x40, 0x4A, ScreenWidth);
	PokeWord(0x40, 0x4C, ScreenWidth * ScreenHeight * 2);
	PokeWord(0x40, 0x50, 0);
	PokeByte(0x40, 0x60, LineHeight - 1);
	PokeByte(0x40, 0x61, LineHeight - 2);
	PokeByte(0x40, 0x84, ScreenHeight - 1);
	PokeByte(0x40, 0x85, LineHeight);

	unsigned int i;
	for(i = 0; i < ScreenWidth * ScreenHeight; i++) PokeByte(0xB800, i * 2 + 1, 7);
}

void SetTextMode90x60()
{
	VgaWriteRegisters(VgaRegisters90x60Text);

	ScreenWidth = 90;
	ScreenHeight = 60;
	LineHeight = 8;	

	VgaWriteFont(VgaFont8x8, 8);

	PokeWord(0x40, 0x4A, ScreenWidth);
	PokeWord(0x40, 0x4C, ScreenWidth * ScreenHeight * 2);
	PokeWord(0x40, 0x50, 0);
	PokeByte(0x40, 0x60, LineHeight - 1);
	PokeByte(0x40, 0x61, LineHeight - 2);
	PokeByte(0x40, 0x84, ScreenHeight - 1);
	PokeByte(0x40, 0x85, LineHeight);

	unsigned int i;
	for(i = 0; i < ScreenWidth * ScreenHeight; i++) PokeByte(0xB800, i * 2 + 1, 7);
}

//-------------------------------------------------------------------------------------------------------------------------//
//VGA modes

void SetVgaMode320x200x4()
{
	VgaWriteRegisters(VgaRegisters320x200x4);

	ScreenWidth = 320;
	ScreenHeight = 200;
	ScreenColors = 4;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

void SetVgaMode320x200x256()
{
	VgaWriteRegisters(VgaRegisters320x200x256);

	ScreenWidth = 320;
	ScreenHeight = 200;
	ScreenColors = 256;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

void SetVgaMode320x200x256X()
{
	VgaWriteRegisters(VgaRegisters320x200x256_modex);

	ScreenWidth = 320;
	ScreenHeight = 200;
	ScreenColors = 256;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

void SetVgaMode640x480x2()
{
	VgaWriteRegisters(VgaRegisters640x480x2);

	ScreenWidth = 640;
	ScreenHeight = 480;
	ScreenColors = 2;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

void SetVgaMode640x480x16()
{
	VgaWriteRegisters(VgaRegisters640x480x16);

	ScreenWidth = 640;
	ScreenHeight = 480;
	ScreenColors = 16;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

void SetVgaMode720x480x16()
{
	VgaWriteRegisters(VgaRegisters720x480x16);

	ScreenWidth = 720;
	ScreenHeight = 480;
	ScreenColors = 16;

	VgaFrontBuffer = (unsigned char *) VgaGetFramebufferAddress();
	VgaBackBuffer = (unsigned char *) ReserveMemory(ScreenHeight*ScreenWidth);
}

//-------------------------------------------------------------------------------------------------------------------------//
//Mode information

unsigned int GetVgaScreenWidth()
{
	return ScreenWidth;
}

unsigned int GetVgaScreenHeight()
{
	return ScreenHeight;
}

//-------------------------------------------------------------------------------------------------------------------------//
//Get/Set Pixel

unsigned int VgaGetPixel(unsigned int x, unsigned int y)
{
	return VgaBackBuffer[y*ScreenWidth+x];
}

void VgaSetPixel(unsigned int x, unsigned int y, unsigned int Color)
{
	VgaBackBuffer[y*ScreenWidth+x] = Color;
}

//-------------------------------------------------------------------------------------------------------------------------//
//Clear screen/Fill screen

void VgaClearScreen()
{
	unsigned int x, y;
	for(y = 0; y < ScreenHeight; y++) for(x = 0; x < ScreenWidth; x++) VgaBackBuffer[y*ScreenWidth+x] = 0;
}

void VgaFillScreen(unsigned int Color)
{
	unsigned int x, y;
	for(y = 0; y < ScreenHeight; y++) for(x = 0; x < ScreenWidth; x++) VgaBackBuffer[y*ScreenWidth+x] = Color;
}

//-------------------------------------------------------------------------------------------------------------------------//
//Drawing functions

void DrawLine(unsigned int X1, unsigned int Y1, unsigned int X2, unsigned int Y2, unsigned int Color)
{
	/*if(!(0 <= X1 <= ScreenWidth)) return;
	if(!(0 <= X2 <= ScreenWidth)) return;
	if(!(0 <= Y1 <= ScreenHeight)) return;
	if(!(0 <= Y2 <= ScreenHeight)) return;*/

	/*if(X1 < 0) X1 = 0;
	if(X1 >= ScreenWidth) X1 = ScreenWidth-1;
	if(X2 < 0) X2 = 0;
	if(X2 >= ScreenWidth) X2 = ScreenWidth-1;
	if(Y1 < 0) Y1 = 0;
	if(Y1 >= ScreenHeight) Y1 = ScreenHeight-1;
	if(Y2 < 0) Y2 = 0;
	if(Y2 >= ScreenHeight) Y2 = ScreenHeight-1;*/

	signed int dx = X2 - X1;
	signed int dy = Y2 - Y1;

	signed int pdx = 0;
	signed int pdy = 0;

	signed int SignX = 0;
	signed int SignY = 0;

	if(dx >= 0) { pdx = dx; SignX = 1; }
	else { pdx = -dx; SignX = -1; }

	if(dy >= 0) { pdy = dy; SignY = 1; }
	else { pdy = -dy; SignY = -1; }

	unsigned int i = 0;

	if((pdx == 0) && (pdy == 0)) VgaBackBuffer[Y1*ScreenWidth+X1] = Color; //VgaSetPixel(X1, Y1, Color);
	else if((pdx == 0) && (pdy != 0)) for(i=0; i<dy+1; i++) VgaBackBuffer[(Y1+SignY*i)*ScreenWidth+X1] = Color; //VgaSetPixel(X1, Y1+SignY*i, Color);
	else if((pdx != 0) && (pdy == 0)) for(i=0; i<dx+1; i++) VgaBackBuffer[(Y1)*ScreenWidth+(X1+SignX*i)] = Color; //VgaSetPixel(X1+SignX*i, Y1, Color);
	else if(pdx == pdy) for(i=0; i<pdx+1; i++) VgaBackBuffer[(Y1+SignY*i)*ScreenWidth+(X1+SignX*i)] = Color; //VgaSetPixel(X1+SignX*i, Y1+SignY*i, Color);
	else if(pdx > pdy)
	{
		double rdy = (double) dy/dx;

		for(i=0; i<pdx+1; i++)
		{
			VgaBackBuffer[(unsigned int)((Y1+i*rdy)*ScreenWidth+(X1+i))] = Color; //VgaSetPixel(X1+i, Y1+i*rdy, Color);
		}
	}
	else if(pdx < pdy)
	{
		double rdx = (double) dx/dy;

		for(i=0; i<pdy+1; i++)
		{
			VgaBackBuffer[(unsigned int)((Y1+i)*ScreenWidth+(X1+i*rdx))] = Color; //VgaSetPixel(X1+i*rdx, Y1+i, Color);
		}
	}
}

void DrawRectangle(unsigned int X1, unsigned int Y1, unsigned int X2, unsigned int Y2, unsigned int Color)
{
	unsigned int DrawX1 = X1;
	unsigned int DrawY1 = Y1;
	unsigned int DrawX2 = X2;
	unsigned int DrawY2 = Y2;

	if(X1 < 0) DrawX1 = 0;
	if(X2 >= ScreenWidth) DrawX2 = ScreenWidth - 1;
	if(Y1 < 0) DrawY1 = 0;
	if(Y2 >= ScreenHeight) DrawY2 = ScreenHeight - 1;

	if(X1 >= 0) DrawLine(DrawX1, DrawY1, DrawX1, DrawY2, Color); // Links
	if(X2 < ScreenWidth) DrawLine(DrawX2, DrawY1, DrawX2, DrawY2, Color); // Rechts

	if(Y1 >= 0) DrawLine(DrawX1, DrawY1, DrawX2, DrawY1, Color); // Oben
	if(Y2 < ScreenHeight) DrawLine(DrawX1, DrawY2, DrawX2, DrawY2, Color); // Unten
}

void DrawBitmap(unsigned int x, unsigned int y, unsigned char *Bitmap, unsigned int Width, unsigned int Height, unsigned int Opacity)
{
	unsigned int i, j;

	unsigned int DrawWidth = Width;
	unsigned int DrawHeight = Height;

	if(x > ScreenWidth) return;
	if(y > ScreenHeight) return;

	if(x+Width > ScreenWidth) DrawWidth = Width - (x+Width - ScreenWidth);
	if(y+Height > ScreenHeight) DrawHeight = Height - (y+Height - ScreenHeight);

	for(i=0; i<DrawHeight; i++)
	{
		for(j=0; j<DrawWidth; j++)
		{
			if(Bitmap[i*Width+j] != 0) VgaBackBuffer[(y+i)*ScreenWidth+(x+j)] = Bitmap[i*Width+j]; //VgaSetPixel(x+j, y+i, Bitmap[i*(Width-1)+j]);
		}
	}
}

void VgaDrawFramebuffer()
{
	while((InputPortByte(0x3DA) & 0x08));
	while(!(InputPortByte(0x3DA) & 0x08));

	memcpy(VgaFrontBuffer, VgaBackBuffer, (ScreenHeight*ScreenWidth));
}

//-------------------------------------------------------------------------------------------------------------------------//
//Initialization

void InitializeVga()
{
	AddUserApi(SetTextMode80x25, 0);
	AddUserApi(SetTextMode90x60, 0);
	AddUserApi(SetVgaMode320x200x4, 0);
	AddUserApi(SetVgaMode320x200x256, 0);
	AddUserApi(SetVgaMode320x200x256X, 0);
	AddUserApi(SetVgaMode640x480x2, 0);
	AddUserApi(SetVgaMode640x480x16, 0);
	AddUserApi(SetVgaMode720x480x16, 0);

	AddUserApi(GetVgaScreenWidth, 0);
	AddUserApi(GetVgaScreenHeight, 0);

	AddUserApi(VgaGetPixel, 3);
	AddUserApi(VgaSetPixel, 3);

	AddUserApi(VgaClearScreen, 0);
	AddUserApi(VgaFillScreen, 1);

	AddUserApi(DrawLine, 5);
	AddUserApi(DrawRectangle, 5);
	AddUserApi(DrawBitmap, 6);
	AddUserApi(VgaDrawFramebuffer, 0);
}
