#ifndef _PYROS_VIDEO
#define _PYROS_VIDEO

#include "../include/string.h"
#include "../include/printf.h"
#include "types.h"
#include <stdarg.h>

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGRAY 7
#define DARKGRAY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define LIGHTBROWN 14
#define WHITE 15

unsigned char forecolor;
unsigned char backcolor;

int x_pos;
int y_pos;

void Putch(unsigned char c)
{
     		unsigned short attrib=((backcolor<<4)|(forecolor&0x0F))<<8;
     		unsigned short *where;
		if(c=='\n')
		{
				x_pos=0;
				y_pos++;
		}
		else
		if(c=='\r')
		{
				x_pos=0;
		}
		else
		if(c=='\t')
		{
				x_pos+=5;
		}
		else
		{
     				where=((unsigned short*)0xB8000)+(y_pos*80+x_pos);
     				*where=c|attrib;
				x_pos++;
		}
} 

/*void Puts(char *str)
{
		int c;
		int len=strlen(str);
		for(c=0;c<len;c++)
		{
				Putch(*str);
				str++;
		}
}*/

void text_color(unsigned char fore,unsigned char back)
{
		forecolor=fore;
		backcolor=back;
}

void gotoxy(int x,int y)
{
		x_pos=x;
		y_pos=y;
}

void gotox(int x)
{
		x_pos=x;
}

void gotoy(int y)
{
		y_pos=y;
}

int vprintf_help(unsigned c, void **ptr)
{
		Putch(c);
		return 0 ;
}

int vprintf(const char *fmt, va_list args)
{
		return do_printf(fmt, args, vprintf_help, NULL);
}

int Printf(const char *fmt,...)
{
		va_list args;
		int ret_val;

		va_start(args, fmt);
		ret_val = vprintf(fmt, args);
		va_end(args);
		return ret_val;
}

void clrscr(void)
{
		int c1;
		int c2;

		for(c1=0;c1<25;c1++)
		{
				for(c2=0;c2<80;c2++)
				{
						Putch(' ');
				}
				Putch('\n');
		}
		gotoxy(0,0);
}

unsigned char *messages[] =
{
    		"Division By Zero",
    		"Debug Exception",
    		"Non Maskable Interrupt",
    		"Breakpoint Exception",
		"Into Detected Overflow Exception",
		"Out of Bounds Exception",
		"Invalid Opcode Exception",
		"No Coprocessor Exception",
		"Double Fault Exception",
		"Coprocessor Segment Overrun",
		"Bad TSS Exception",
		"Segment Not Present Exception",
		"Stack Fault Exception",
		"General Protection Fault Exception",
		"Page Fault Exception",
		"Unknown Interrupt Exception",
		"Coprocessor Fault Exception",
		"Alignment Check Exception (486+)",
		"Machine Check Exception (Pentium/586+)"
};

void ex(struct regs *r)
{
	
	text_color(WHITE,RED);
	clrscr();

	Printf("KaoS has encountered a problem\n");
	if(r->int_no<19)
	{
			Printf("%s, interrupt %d, error 0x%X\n",messages[r->int_no],r->int_no,r->err);
	}
	else
	{
			Printf("Reserved, interrupt %d, error 0x%X\n",r->int_no,r->err);
	} 

	Printf("\nRegisters:\n");
	Printf("eax:\t0x%X",r->eax);
	gotox(23);
	Printf("ebx:\t0x%X\n",r->ebx);

	Printf("ecx:\t0x%X",r->ecx);
	gotox(23);
	Printf("edx:\t0x%X\n",r->edx);

	Printf("edi:\t0x%X",r->edi);
	gotox(23);
	Printf("esi:\t0x%X\n",r->esi);

	Printf("ebp:\t0x%X",r->ebp);
	gotox(23);
	Printf("esp:\t0x%X\n",r->esp);

	Printf("eip:\t0x%X",r->eip);
	gotox(23);
	Printf("gs: \t0x%X\n",r->gs);

	Printf("fs: \t0x%X",r->fs);
	gotox(23);
	Printf("es: \t0x%X\n",r->es);

	Printf("ds: \t0x%X",r->ds);
	gotox(23);
	Printf("cs: \t0x%X\n",r->cs);

	Printf("ss: \t0x%X",r->ss);
	gotox(23);
	Printf("cr0:\t0x%X\n\n",r->cr0);

	Printf("eflags: \t0x%X\n",r->eflags);
	Printf("useresp:\t0x%X\n",r->useresp);
	for(;;);
}
#else
#endif
