/*XIX System 19 Kernel - Version pre-Alpha*/
//
/*This version of the XIX Kernel is merely a*/
/*tool to demonstrate the capabilities of*/
/*printing a string to the screen by directly*/
/*accessing video memory*/
//
/*Future versions of the XIX Kernel will include*/
/*a setup of an Advanced GDT, and a capable*/
/*implementation of a C Library*/
//
/*For more information, see the Documentation file*/
/*kernel.xix.doc*/

/*Section for definitions*/
#define std_txt 0x0A	//define std_txt, the standard text attribute (non-flashing, green text, black background)


/*Section for function prototypes*/
void clr_scr();		//define function that will clear the screen, Returns no value and accepts no args
void prnt_str(char *string, unsigned int line);		//define function that will print a string as arg 1,
								//and will take the line to print on as arg 2, will 
								//return an unsigned integer


/*The Main Function*/
void kmain() {		//The main kernel function, doing its duty
	int x;

	clr_scr();	//call the function to clear the screen

	prnt_str("XIX System 19 - Version pre-Alpha", 0);	//call the function to print the string given in the
								//argument 1, at line 0 as of arg 2

	for( x = 10; x = x; x++ ) {	//UNRECOVERABLE LOOP
		x = x - 1;
	}
};


/*Section for sub-functions*/
void clr_scr() {	//function that will clear the screen, no args, no return value
	char *vid_mem = (char *) 0xb8000;	//declare the pointer to the video memory location
	unsigned int cur_pos = 0;		//set cur_pos variable to 0 (line 0, column 0, register 0)

	while (cur_pos < (80*25*2)) {	//loop to perform as long as the cursor position is within available 
					//screen space
		vid_mem[cur_pos]=' ';		//fill video memory at cursor position with blank (space)
		cur_pos++;			//increment cursor position by one
		vid_mem[cur_pos]=std_txt;	//fill attribute byte with standard text attribute
		cur_pos++;			//increment cursor position back to text byte
	}
};

void prnt_str(char *string, unsigned int line) {	//function to print a string, accepts the string to be
								//printed as arg0, and the line to print on as arg1
								//returns an unsigned integer (hopefully 1)
	char *vid_mem = (char *) 0xb8000;	//declare the pointer to the video memory location
	unsigned int cur_pos = 0;		//set cur_pos variable to 0 (line 0, column 0, register 0)
	cur_pos=(line*80*2);			//set cursor position to beginning of chosen line

	while(*string != 0) {		//loop to perform as long as the string isn't null terminated
		if (*string == '\n') {	//if newline character is specified
			line++;		//increment the line by one
			cur_pos=(line*80*2); //set cursor position to newline area
			*string++;	//increment the string to next character
		} else {		//if not newline character, print the normal character
			vid_mem[cur_pos]=*string;	//fill vid memory at cursor position with the current character
						//in the string
			*string++;			//increment the string position by 1
			cur_pos++;			//increment cur_pos into attribute byte

			vid_mem[cur_pos]=std_txt;	//set attribute byte to standard text value

			cur_pos++;			//return cur_pos to character byte by increasing by one
		}
	}
};
