[GOOD] BUILD 0.1.0.450 DATE 8/29/2011 AT 10:30 AM
==================================================== + Changed 'align 0x4' line above multiboot header in loader.asm to 'align 4' + Removed -e option for echo in build.sh + Modified build.sh for linux + Fixed triple fault when enabling paging + Fixed page faults at memory manager initialization + Fixed 'mem' console function + Added more info about page fault at crash screen + Added Panic() macro + Added verbose mode for memory manager [ BAD] BUILD 0.1.0.390 DATE 8/27/2011 AT 10:54 PM ==================================================== + Added stdlib routines, separated in different files + Rewritten physical memory manager + Added virtual mem manager + Added memory allocation/freeing + Added memory library + Added temporary allocation (at end of kernel), until paging is started - Removed functionality from debug console function 'mem' - Removed system.h, the one remaining function now in stdio.h
This commit is contained in:
		
							
								
								
									
										255
									
								
								Kernel/debug/commands.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										255
									
								
								Kernel/debug/commands.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,255 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <memory-add.h>
 | 
			
		||||
#include "../hal/mouse/mouse.h"
 | 
			
		||||
#include "../drivers/floppy/floppy.h"
 | 
			
		||||
 | 
			
		||||
string ConsoleCommands[] =
 | 
			
		||||
{
 | 
			
		||||
	"osver",
 | 
			
		||||
	"time",
 | 
			
		||||
	"cls",
 | 
			
		||||
	"help",
 | 
			
		||||
	"dump",
 | 
			
		||||
	"mem",
 | 
			
		||||
	"crash",
 | 
			
		||||
	"mouse",
 | 
			
		||||
	"read",
 | 
			
		||||
	"reboot",
 | 
			
		||||
	"restart",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int32 ConsoleCommandsCount = 11;
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  osver - get os info                  *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandOsver()
 | 
			
		||||
{
 | 
			
		||||
	ConsoleWrite ("%#%s%# 32bit operating system\n", Color(0,ColorYellow), OS_STRING, Color(0,ColorLightGray));
 | 
			
		||||
 | 
			
		||||
	int32 i = 0;
 | 
			
		||||
	for (i = 0; i < 30; i++)
 | 
			
		||||
		ConsoleWrite ("%#%c", ColorDarkGray, 205);
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("\n%#OS version: %#%s\n", ColorDarkGray, ColorLightGray, OS_VERSION);
 | 
			
		||||
	ConsoleWrite ("%#Build: %#%s ", ColorDarkGray, ColorLightGray, OS_BUILD);
 | 
			
		||||
	ConsoleWrite ("%#built on %#%s %#at %#%s\n", ColorDarkGray, ColorLightGray, OS_BUILD_DATE, ColorDarkGray, ColorLightGray, OS_BUILD_TIME);
 | 
			
		||||
	ConsoleWrite ("%#(c) Copyright %#CTA Systems Inc.\n", ColorDarkGray ,ColorLightGray);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  time - get date and time             *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandTime()
 | 
			
		||||
{
 | 
			
		||||
	const char* Months[] = {
 | 
			
		||||
		"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const char* Weekdays[] = {
 | 
			
		||||
		"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	Time time = TimeConvertToTime(TimeGetInternalTime());
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("Current time: ");
 | 
			
		||||
	ConsoleWrite ("%#%d:%d%d:%d%d.%d%d%d\n", Color(0,ColorLightGreen) ,(int)time.Hour,
 | 
			
		||||
			time.Minute/10, time.Minute%10, time.Second/10, time.Second%10, time.Milisecond/100, (time.Milisecond/10)%10, time.Milisecond%10);
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("Date: %#%s, %s %d, %d\n", Color(0,ColorLightGreen), Weekdays[time.WeekDay],
 | 
			
		||||
			Months[time.Month], time.Day, time.Year);
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  help - help provider                 *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandHelp(string params[], int32 count)
 | 
			
		||||
{
 | 
			
		||||
	if (count <= 1)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite ("Available commands:\n");
 | 
			
		||||
 | 
			
		||||
		int i;
 | 
			
		||||
		for (i = 0; i < ConsoleCommandsCount; i++)
 | 
			
		||||
			ConsoleWrite(" > %#%s\n", Color(0,ColorWhite), ConsoleCommands[i]);
 | 
			
		||||
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite("%#! Help for %s command is not implemented yet.\n", Color(0,ColorLightRed), params[1]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  dump - dumps memory content          *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
inline char hex (int32 digit)
 | 
			
		||||
{
 | 
			
		||||
	return (digit < 10) ? (digit + '0') : (digit - 10 + 'A');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandDump (string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	  unsigned pause = 1, i = 0;
 | 
			
		||||
 | 
			
		||||
	  // Verify correct number of arguments
 | 
			
		||||
	  if (argc < 3) {
 | 
			
		||||
			ConsoleWrite("%#! Correct syntax:  %#dump %#[start_address] [end_address]\n",
 | 
			
		||||
					ColorLightRed, ColorWhite, ColorLightGray);
 | 
			
		||||
			ConsoleWrite("%#Start %#and %#end %#addresses are in hex.\n",
 | 
			
		||||
					ColorLightGray, ColorDarkGray, ColorLightGray, ColorDarkGray);
 | 
			
		||||
			return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
	  // Disable pause
 | 
			
		||||
	  if (argc==4 && strcmp(argv[3], "!p") == 0)
 | 
			
		||||
			pause = 0;
 | 
			
		||||
 | 
			
		||||
	  // Dump memory
 | 
			
		||||
	  unsigned char *start, *end;
 | 
			
		||||
	  start = (unsigned char *) ConvertStringToIntHex(argv[1]);
 | 
			
		||||
	  end = (unsigned char *) ConvertStringToIntHex(argv[2]);
 | 
			
		||||
	  unsigned char* count;
 | 
			
		||||
 | 
			
		||||
	  while (start <= end) {
 | 
			
		||||
		    // Write address
 | 
			
		||||
			ConsoleWrite("%#%x%#:  ", Color(0,ColorLightMagenta), (unsigned int)start, Color(0,ColorLightGray));
 | 
			
		||||
 | 
			
		||||
			// Write hex data
 | 
			
		||||
			for (count = start; count < start+16; count++) {
 | 
			
		||||
				if (*count == 0) ConsoleWrite ("%#00 ", Color(0,ColorDarkGray));
 | 
			
		||||
				else ConsoleWrite ("%#%c%c ", Color(0,ColorWhite), hex(*count/16), hex(*count%16));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Write ASCII data
 | 
			
		||||
			ConsoleWrite("  ");
 | 
			
		||||
			for (count = start; count < start+16; count++) {
 | 
			
		||||
				  if (*count < 32) ConsoleWrite(".");
 | 
			
		||||
				  else ConsoleWrite("%#%c", Color(0,ColorLightGreen), *count);
 | 
			
		||||
				  }
 | 
			
		||||
 | 
			
		||||
			// New line
 | 
			
		||||
			ConsoleWrite("\n\r");
 | 
			
		||||
			start+=16; i++;
 | 
			
		||||
 | 
			
		||||
			// Pause
 | 
			
		||||
			if ((i%22 == 0) && (pause==1)) {
 | 
			
		||||
				  ConsoleWrite("\n\r%#Press %#any key %#to continue scrolling, %#Esc %#to exit.",
 | 
			
		||||
						  0x8, 0x7, 0x8, 0x7, 0x8);
 | 
			
		||||
				  Key k = ReadKey();
 | 
			
		||||
				  if (k.Scancode == KeyboardKeyEscape) return;
 | 
			
		||||
				  ConsoleWrite("\n\n\r");
 | 
			
		||||
				  }
 | 
			
		||||
			}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _CommandMemTotalRows 10
 | 
			
		||||
void _CommandMemPrintMemmap()
 | 
			
		||||
{
 | 
			
		||||
	uint8 color = Color(ColorGreen, ColorRed);
 | 
			
		||||
	uint32 total = MemoryGetFramesTotal();
 | 
			
		||||
 | 
			
		||||
	char c = ' ';
 | 
			
		||||
 | 
			
		||||
	// Print memory map
 | 
			
		||||
	int32 i, old = 0, n = 0, blocks, used;
 | 
			
		||||
	for (i = 0; i < 80; i++)
 | 
			
		||||
		ConsoleWrite("%#%c", ColorLightGray, 220);
 | 
			
		||||
 | 
			
		||||
	for (i = 1; i <= 80*_CommandMemTotalRows; i++, old++)
 | 
			
		||||
	{
 | 
			
		||||
		n = (total * i) / (80 * _CommandMemTotalRows);
 | 
			
		||||
 | 
			
		||||
		blocks = n - old;
 | 
			
		||||
		used = 0;
 | 
			
		||||
		for (; old < n; old++)
 | 
			
		||||
			used += (MemPhGetFrame(old) != 0);
 | 
			
		||||
 | 
			
		||||
		if (used <= blocks / 5) c = ' ';
 | 
			
		||||
		else if (used > 4 * blocks / 5) c = 219;
 | 
			
		||||
		else if (used <= 2 * blocks / 5) c = 176;
 | 
			
		||||
		else if (used <= 3 * blocks / 5) c = 177;
 | 
			
		||||
		else c = 178;
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite("%#%c", color, c);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 80; i++)
 | 
			
		||||
			ConsoleWrite("%#%c", ColorDarkGray, 223);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandMem (string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	if (argc < 2)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite ("Physical memory map:\n");
 | 
			
		||||
 | 
			
		||||
		_CommandMemPrintMemmap();
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite ("Free space: %#%ukb (%u frames)\n", ColorLightMagenta, MemoryGetFree(), MemoryGetFramesFree());
 | 
			
		||||
		ConsoleWrite ("Used space: %#%ukb (%u frames)\n\n", ColorLightMagenta, MemoryGetUsed(), MemoryGetFramesUsed());
 | 
			
		||||
		ConsoleWrite ("Total space: %#%ukb (%u frames)\n", ColorLightMagenta, MemoryGetTotal(), MemoryGetFramesTotal());
 | 
			
		||||
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (strcmp(argv[1], "alloc") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		uint32 addr = 0;
 | 
			
		||||
		if (argc < 3) addr = (uint32)kmalloc(0x4);
 | 
			
		||||
		else addr = (uint32)kmalloc(ConvertStringToUInt(argv[2]));
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite("Returned address: %#0x%x\n", ColorWhite, addr);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	else if (strcmp(argv[1], "free") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (argc < 3) {
 | 
			
		||||
			ConsoleWrite ("%#! Missing parameter: address to free.", ColorRed);
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		kfree((void*)ConvertStringToIntHex(argv[2]));
 | 
			
		||||
		ConsoleWrite("Done.\n");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	else ConsoleWrite("%#! Invalid command. Available commands are: alloc, free.", ColorLightRed);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandCrash()
 | 
			
		||||
{
 | 
			
		||||
	int a = 10, b = 0;
 | 
			
		||||
	ConsoleWrite ("%d", a/b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandMouse()
 | 
			
		||||
{
 | 
			
		||||
	MouseState s = MouseGetState();
 | 
			
		||||
	ConsoleWrite("X=%d Y=%d Buttons=", s.Position.X, s.Position.Y);
 | 
			
		||||
 | 
			
		||||
	if (!s.Buttons) ConsoleWrite("<none>");
 | 
			
		||||
	if (s.Buttons & 1) ConsoleWrite("<left>");
 | 
			
		||||
	if (s.Buttons & 2) ConsoleWrite("<right>");
 | 
			
		||||
	if (s.Buttons & 4) ConsoleWrite("<mid>");
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite("\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandRead(string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	if (argc < 2)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite("%#! Missing parameter - sector!\n", ColorLightRed);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	uint32 sector = ConvertStringToUInt(argv[1]);
 | 
			
		||||
	ConsoleWrite("Returned value: 0x%x\n", FloppyRead(0, sector));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										255
									
								
								Kernel/debug/commands.c~
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										255
									
								
								Kernel/debug/commands.c~
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,255 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <memory-add.h>
 | 
			
		||||
#include "../hal/mouse/mouse.h"
 | 
			
		||||
#include "../drivers/floppy/floppy.h"
 | 
			
		||||
 | 
			
		||||
string ConsoleCommands[] =
 | 
			
		||||
{
 | 
			
		||||
	"osver",
 | 
			
		||||
	"time",
 | 
			
		||||
	"cls",
 | 
			
		||||
	"help",
 | 
			
		||||
	"dump",
 | 
			
		||||
	"mem",
 | 
			
		||||
	"crash",
 | 
			
		||||
	"mouse",
 | 
			
		||||
	"read",
 | 
			
		||||
	"reboot",
 | 
			
		||||
	"restart",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int32 ConsoleCommandsCount = 11;
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  osver - get os info                  *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandOsver()
 | 
			
		||||
{
 | 
			
		||||
	ConsoleWrite ("%#%s%# 32bit operating system\n", Color(0,ColorYellow), OS_STRING, Color(0,ColorLightGray));
 | 
			
		||||
 | 
			
		||||
	int32 i = 0;
 | 
			
		||||
	for (i = 0; i < 30; i++)
 | 
			
		||||
		ConsoleWrite ("%#%c", ColorDarkGray, 205);
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("\n%#OS version: %#%s\n", ColorDarkGray, ColorLightGray, OS_VERSION);
 | 
			
		||||
	ConsoleWrite ("%#Build: %#%s ", ColorDarkGray, ColorLightGray, OS_BUILD);
 | 
			
		||||
	ConsoleWrite ("%#built on %#%s %#at %#%s\n", ColorDarkGray, ColorLightGray, OS_BUILD_DATE, ColorDarkGray, ColorLightGray, OS_BUILD_TIME);
 | 
			
		||||
	ConsoleWrite ("%#(c) Copyright %#CTA Systems Inc.\n", ColorDarkGray ,ColorLightGray);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  time - get date and time             *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandTime()
 | 
			
		||||
{
 | 
			
		||||
	const char* Months[] = {
 | 
			
		||||
		"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const char* Weekdays[] = {
 | 
			
		||||
		"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	Time time = TimeConvertToTime(TimeGetInternalTime());
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("Current time: ");
 | 
			
		||||
	ConsoleWrite ("%#%d:%d%d:%d%d.%d%d%d\n", Color(0,ColorLightGreen) ,(int)time.Hour,
 | 
			
		||||
			time.Minute/10, time.Minute%10, time.Second/10, time.Second%10, time.Milisecond/100, (time.Milisecond/10)%10, time.Milisecond%10);
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("Date: %#%s, %s %d, %d\n", Color(0,ColorLightGreen), Weekdays[time.WeekDay],
 | 
			
		||||
			Months[time.Month], time.Day, time.Year);
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  help - help provider                 *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
void CommandHelp(string params[], int32 count)
 | 
			
		||||
{
 | 
			
		||||
	if (count <= 1)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite ("Available commands:\n");
 | 
			
		||||
 | 
			
		||||
		int i;
 | 
			
		||||
		for (i = 0; i < ConsoleCommandsCount; i++)
 | 
			
		||||
			ConsoleWrite(" > %#%s\n", Color(0,ColorWhite), ConsoleCommands[i]);
 | 
			
		||||
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite("%#! Help for %s command is not implemented yet.\n", Color(0,ColorLightRed), params[1]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*****************************************
 | 
			
		||||
 *  dump - dumps memory content          *
 | 
			
		||||
 *****************************************/
 | 
			
		||||
inline char hex (int32 digit)
 | 
			
		||||
{
 | 
			
		||||
	return (digit < 10) ? (digit + '0') : (digit - 10 + 'A');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandDump (string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	  unsigned pause = 1, i = 0;
 | 
			
		||||
 | 
			
		||||
	  // Verify correct number of arguments
 | 
			
		||||
	  if (argc < 3) {
 | 
			
		||||
			ConsoleWrite("%#! Correct syntax:  %#dump %#[start_address] [end_address]\n",
 | 
			
		||||
					ColorLightRed, ColorWhite, ColorLightGray);
 | 
			
		||||
			ConsoleWrite("%#Start %#and %#end %#addresses are in hex.\n",
 | 
			
		||||
					ColorLightGray, ColorDarkGray, ColorLightGray, ColorDarkGray);
 | 
			
		||||
			return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
	  // Disable pause
 | 
			
		||||
	  if (argc==4 && strcmp(argv[3], "!p") == 0)
 | 
			
		||||
			pause = 0;
 | 
			
		||||
 | 
			
		||||
	  // Dump memory
 | 
			
		||||
	  unsigned char *start, *end;
 | 
			
		||||
	  start = (unsigned char *) ConvertStringToIntHex(argv[1]);
 | 
			
		||||
	  end = (unsigned char *) ConvertStringToIntHex(argv[2]);
 | 
			
		||||
	  unsigned char* count;
 | 
			
		||||
 | 
			
		||||
	  while (start <= end) {
 | 
			
		||||
		    // Write address
 | 
			
		||||
			ConsoleWrite("%#%x%#:  ", Color(0,ColorLightMagenta), (unsigned int)start, Color(0,ColorLightGray));
 | 
			
		||||
 | 
			
		||||
			// Write hex data
 | 
			
		||||
			for (count = start; count < start+16; count++) {
 | 
			
		||||
				if (*count == 0) ConsoleWrite ("%#00 ", Color(0,ColorDarkGray));
 | 
			
		||||
				else ConsoleWrite ("%#%c%c ", Color(0,ColorWhite), hex(*count/16), hex(*count%16));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Write ASCII data
 | 
			
		||||
			ConsoleWrite("  ");
 | 
			
		||||
			for (count = start; count < start+16; count++) {
 | 
			
		||||
				  if (*count < 32) ConsoleWrite(".");
 | 
			
		||||
				  else ConsoleWrite("%#%c", Color(0,ColorLightGreen), *count);
 | 
			
		||||
				  }
 | 
			
		||||
 | 
			
		||||
			// New line
 | 
			
		||||
			ConsoleWrite("\n\r");
 | 
			
		||||
			start+=16; i++;
 | 
			
		||||
 | 
			
		||||
			// Pause
 | 
			
		||||
			if ((i%22 == 0) && (pause==1)) {
 | 
			
		||||
				  ConsoleWrite("\n\r%#Press %#any key %#to continue scrolling, %#Esc %#to exit.",
 | 
			
		||||
						  0x8, 0x7, 0x8, 0x7, 0x8);
 | 
			
		||||
				  Key k = ReadKey();
 | 
			
		||||
				  if (k.Scancode == KeyboardKeyEscape) return;
 | 
			
		||||
				  ConsoleWrite("\n\n\r");
 | 
			
		||||
				  }
 | 
			
		||||
			}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define _CommandMemTotalRows 10
 | 
			
		||||
void _CommandMemPrintMemmap()
 | 
			
		||||
{
 | 
			
		||||
	uint8 color = Color(ColorGreen, ColorRed);
 | 
			
		||||
	uint32 total = MemoryGetFramesTotal();
 | 
			
		||||
 | 
			
		||||
	char c = ' ';
 | 
			
		||||
 | 
			
		||||
	// Print memory map
 | 
			
		||||
	int32 i, old = 0, n = 0, blocks, used;
 | 
			
		||||
	for (i = 0; i < 80; i++)
 | 
			
		||||
		ConsoleWrite("%#%c", ColorLightGray, 220);
 | 
			
		||||
 | 
			
		||||
	for (i = 1; i <= 80*_CommandMemTotalRows; i++, old++)
 | 
			
		||||
	{
 | 
			
		||||
		n = (total * i) / (80 * _CommandMemTotalRows);
 | 
			
		||||
 | 
			
		||||
		blocks = n - old;
 | 
			
		||||
		used = 0;
 | 
			
		||||
		for (; old < n; old++)
 | 
			
		||||
			used += (MemPhGetFrame(old) != 0);
 | 
			
		||||
 | 
			
		||||
		if (used <= blocks / 5) c = ' ';
 | 
			
		||||
		else if (used > 4 * blocks / 5) c = 219;
 | 
			
		||||
		else if (used <= 2 * blocks / 5) c = 176;
 | 
			
		||||
		else if (used <= 3 * blocks / 5) c = 177;
 | 
			
		||||
		else c = 178;
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite("%#%c", color, c);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 80; i++)
 | 
			
		||||
			ConsoleWrite("%#%c", ColorDarkGray, 223);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandMem (string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	if (argc < 2)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite ("Memory map:\n");
 | 
			
		||||
 | 
			
		||||
		_CommandMemPrintMemmap();
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite ("Free space: %#%ukb (%u frames)\n", ColorLightMagenta, MemoryGetFree(), MemoryGetFramesFree());
 | 
			
		||||
		ConsoleWrite ("Used space: %#%ukb (%u frames)\n\n", ColorLightMagenta, MemoryGetUsed(), MemoryGetFramesUsed());
 | 
			
		||||
		ConsoleWrite ("Total space: %#%ukb (%u frames)\n", ColorLightMagenta, MemoryGetTotal(), MemoryGetFramesTotal());
 | 
			
		||||
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (strcmp(argv[1], "alloc") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		uint32 addr = 0;
 | 
			
		||||
		if (argc < 3) addr = (uint32)kmalloc(0x4);
 | 
			
		||||
		else addr = (uint32)kmalloc(ConvertStringToUInt(argv[2]));
 | 
			
		||||
 | 
			
		||||
		ConsoleWrite("Returned address: %#0x%x\n", ColorWhite, addr);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	else if (strcmp(argv[1], "free") == 0)
 | 
			
		||||
	{
 | 
			
		||||
		if (argc < 3) {
 | 
			
		||||
			ConsoleWrite ("%#! Missing parameter: address to free.", ColorRed);
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		kfree((void*)ConvertStringToIntHex(argv[2]));
 | 
			
		||||
		ConsoleWrite("Done.\n");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	else ConsoleWrite("%#! Invalid command. Available commands are: alloc, free.", ColorLightRed);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandCrash()
 | 
			
		||||
{
 | 
			
		||||
	int a = 10, b = 0;
 | 
			
		||||
	ConsoleWrite ("%d", a/b);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandMouse()
 | 
			
		||||
{
 | 
			
		||||
	MouseState s = MouseGetState();
 | 
			
		||||
	ConsoleWrite("X=%d Y=%d Buttons=", s.Position.X, s.Position.Y);
 | 
			
		||||
 | 
			
		||||
	if (!s.Buttons) ConsoleWrite("<none>");
 | 
			
		||||
	if (s.Buttons & 1) ConsoleWrite("<left>");
 | 
			
		||||
	if (s.Buttons & 2) ConsoleWrite("<right>");
 | 
			
		||||
	if (s.Buttons & 4) ConsoleWrite("<mid>");
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite("\n");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CommandRead(string argv[], int32 argc)
 | 
			
		||||
{
 | 
			
		||||
	if (argc < 2)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite("%#! Missing parameter - sector!\n", ColorLightRed);
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	uint32 sector = ConvertStringToUInt(argv[1]);
 | 
			
		||||
	ConsoleWrite("Returned value: 0x%x\n", FloppyRead(0, sector));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										131
									
								
								Kernel/debug/console-base.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								Kernel/debug/console-base.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,131 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <memory.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
uint8* VideoPtr = (uint8*) 0xb8000;
 | 
			
		||||
 | 
			
		||||
Point ConsoleCursor = {0, 0};
 | 
			
		||||
UPoint ConsoleSize = {80, 25};
 | 
			
		||||
 | 
			
		||||
uint8 ConsoleDefaultColor = CONSOLE_DEFAULT_COLOR;
 | 
			
		||||
 | 
			
		||||
/**************************************
 | 
			
		||||
 *  Basic console operations *
 | 
			
		||||
 **************************************/
 | 
			
		||||
void ConsoleClear()
 | 
			
		||||
{
 | 
			
		||||
	uint32 temp;
 | 
			
		||||
 | 
			
		||||
	for (temp = 0; temp < ConsoleSize.X * ConsoleSize.Y; temp++)
 | 
			
		||||
	{
 | 
			
		||||
		VideoPtr[2 * temp] = 0;
 | 
			
		||||
		VideoPtr[2 * temp + 1] = ConsoleDefaultColor;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	ConsoleCursor.X = ConsoleCursor.Y = 0;
 | 
			
		||||
	ConsoleCursorUpdateHardware();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleScroll (uint32 lines)
 | 
			
		||||
{
 | 
			
		||||
	// Sanity check, don't scroll too much
 | 
			
		||||
	if (lines > ConsoleSize.Y) lines = ConsoleSize.Y;
 | 
			
		||||
 | 
			
		||||
	uint32 x, y;
 | 
			
		||||
	for (y = 0; y < ConsoleSize.Y - lines; y++)
 | 
			
		||||
		for (x = 0; x < 2*ConsoleSize.X; x++)
 | 
			
		||||
			VideoPtr[y * ConsoleSize.X * 2 + x] = VideoPtr[(y+lines)*ConsoleSize.X*2 + x];
 | 
			
		||||
 | 
			
		||||
	for (y = ConsoleSize.Y - lines; y < ConsoleSize.Y; y++)
 | 
			
		||||
		for (x = 0; x < ConsoleSize.X; x++)
 | 
			
		||||
		{
 | 
			
		||||
			VideoPtr[2 * (y * ConsoleSize.X + x)] = 0;
 | 
			
		||||
			VideoPtr[2 * (y * ConsoleSize.X + x) + 1] = ConsoleDefaultColor;
 | 
			
		||||
		}
 | 
			
		||||
	
 | 
			
		||||
	ConsoleCursor.Y -= lines;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**************************************
 | 
			
		||||
 *  Cursor position *
 | 
			
		||||
 **************************************/
 | 
			
		||||
Point ConsoleGetCursor()
 | 
			
		||||
{
 | 
			
		||||
	return ConsoleCursor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern void ConsoleSetCursor(Point p)
 | 
			
		||||
{
 | 
			
		||||
	ConsoleCursor = p;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleCursorIncreasePos (int32 delta)
 | 
			
		||||
{
 | 
			
		||||
	// Increase X, Y positions
 | 
			
		||||
	ConsoleCursor.Y += delta / ConsoleSize.X;
 | 
			
		||||
	ConsoleCursor.X += delta % ConsoleSize.X;
 | 
			
		||||
 | 
			
		||||
	// Next line if X > Screen width
 | 
			
		||||
	while (ConsoleCursor.X >= (int32)ConsoleSize.X)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleCursor.Y++;
 | 
			
		||||
		ConsoleCursor.X -= ConsoleSize.X;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Previous line if X < 0
 | 
			
		||||
	while (ConsoleCursor.X < 0)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleCursor.Y--;
 | 
			
		||||
		ConsoleCursor.X += ConsoleSize.X;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Scroll if Y > Screen height
 | 
			
		||||
	if (ConsoleCursor.Y >= (int32)ConsoleSize.Y)
 | 
			
		||||
		ConsoleScroll(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleCursorNewline()
 | 
			
		||||
{
 | 
			
		||||
	ConsoleCursor.X = 0;
 | 
			
		||||
 | 
			
		||||
	if (++ConsoleCursor.Y >= (int32)ConsoleSize.Y)
 | 
			
		||||
		ConsoleScroll(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleCursorUpdateHardware()
 | 
			
		||||
{
 | 
			
		||||
	uint16 temp = (uint16) (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X);
 | 
			
		||||
	uint16* port = (uint16*) 0x463 ;
 | 
			
		||||
 | 
			
		||||
	outportb(*port, 14);
 | 
			
		||||
	outportb(*port + 1, (temp >> 8) & 0xff);
 | 
			
		||||
	outportb(*port, 15);
 | 
			
		||||
	outportb(*port + 1, temp & 0xff);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 Color (uint8 back, uint8 fore)
 | 
			
		||||
{
 | 
			
		||||
	return ((back<<4) | fore);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleSetDefaultColor(uint8 color)
 | 
			
		||||
{
 | 
			
		||||
	ConsoleDefaultColor = color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 ConsoleGetDefaultColor ()
 | 
			
		||||
{
 | 
			
		||||
	return ConsoleDefaultColor;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
UPoint ConsoleGetSize()
 | 
			
		||||
{
 | 
			
		||||
	return ConsoleSize;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleCursorGoto(Point p)
 | 
			
		||||
{
 | 
			
		||||
	if (p.X >= 0 && p.X < (int32)ConsoleSize.X) ConsoleCursor.X = p.X;
 | 
			
		||||
	if (p.Y >= 0 && p.Y < (int32)ConsoleSize.Y) ConsoleCursor.Y = p.Y;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										162
									
								
								Kernel/debug/console-in.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										162
									
								
								Kernel/debug/console-in.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,162 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
#include "../hal/keyboard/keyboard.h"
 | 
			
		||||
 | 
			
		||||
// extern uint8* VideoPtr;
 | 
			
		||||
extern UPoint ConsoleCursor;
 | 
			
		||||
// extern UPoint ConsoleSize;
 | 
			
		||||
// extern uint8 ConsoleDefaultColor;
 | 
			
		||||
 | 
			
		||||
extern void _write_char (char c);
 | 
			
		||||
extern void _write_string (string s);
 | 
			
		||||
 | 
			
		||||
int _next_word_index (string s, int32 direction, int32 current, int32 len)
 | 
			
		||||
{
 | 
			
		||||
	int32 tmp = current;
 | 
			
		||||
 | 
			
		||||
	if (direction < 0) {
 | 
			
		||||
		--tmp;
 | 
			
		||||
		while ((isspace((unsigned char)s[tmp]) || ispunct((unsigned char)s[tmp])) && tmp > 0) --tmp;
 | 
			
		||||
		while (isalnum((unsigned char)s[tmp]) && tmp > 0) --tmp;
 | 
			
		||||
 | 
			
		||||
		if (tmp != 0) tmp++;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	else {
 | 
			
		||||
		++tmp;
 | 
			
		||||
		while (isalnum((unsigned char)s[tmp]) && tmp < len) ++tmp;
 | 
			
		||||
		while ((isspace((unsigned char)s[tmp]) || ispunct((unsigned char)s[tmp])) && tmp < len) ++tmp;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return tmp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _string_crop (string s, int32 start, int32 end, int32* len)
 | 
			
		||||
{
 | 
			
		||||
	int32 i;
 | 
			
		||||
	for (i = end; i < *len; i++)
 | 
			
		||||
		s[i-(end-start)] = s[i];
 | 
			
		||||
 | 
			
		||||
	*len = *len - (end - start);
 | 
			
		||||
	s[*len] = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _string_insert (string s, char what, int32 position, int32* len)
 | 
			
		||||
{
 | 
			
		||||
	int32 i;
 | 
			
		||||
	for (i = *len; i > position; i--)
 | 
			
		||||
		s[i] = s[i-1];
 | 
			
		||||
 | 
			
		||||
	s[position] = what;
 | 
			
		||||
	*len = *len + 1;
 | 
			
		||||
	
 | 
			
		||||
	s[*len] = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleReadString (string s, int32 buffer_size, char end_char)
 | 
			
		||||
{
 | 
			
		||||
	Key key;
 | 
			
		||||
	int32 cursor=0, length = 0, old_length = 0;
 | 
			
		||||
	UPoint cursor_original = ConsoleCursor;
 | 
			
		||||
 | 
			
		||||
	s[0] = 0;
 | 
			
		||||
 | 
			
		||||
	do {
 | 
			
		||||
		// Read a key from keyboard
 | 
			
		||||
		key = ReadKey();
 | 
			
		||||
 | 
			
		||||
		// Process scancode
 | 
			
		||||
		switch (key.Scancode)
 | 
			
		||||
		{
 | 
			
		||||
			// Left arrow
 | 
			
		||||
			case KeyboardKeyLeft:
 | 
			
		||||
				if (cursor > 0)
 | 
			
		||||
				{
 | 
			
		||||
					if (KeyboardGetKeyStatus(KeyboardKeyRightCtrl) || KeyboardGetKeyStatus(KeyboardKeyLeftCtrl))
 | 
			
		||||
						cursor = _next_word_index(s, -1, cursor, length);
 | 
			
		||||
					else --cursor;
 | 
			
		||||
				}
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			// Right arrow
 | 
			
		||||
			case KeyboardKeyRight:
 | 
			
		||||
				if (cursor < length)
 | 
			
		||||
				{
 | 
			
		||||
					if (KeyboardGetKeyStatus(KeyboardKeyRightCtrl) || KeyboardGetKeyStatus(KeyboardKeyLeftCtrl))
 | 
			
		||||
						cursor = _next_word_index(s, 1, cursor, length);
 | 
			
		||||
					else ++cursor;
 | 
			
		||||
				}
 | 
			
		||||
				break;
 | 
			
		||||
 | 
			
		||||
			// Delete key
 | 
			
		||||
			case KeyboardKeyDelete:
 | 
			
		||||
			{
 | 
			
		||||
				if (cursor == length) break;
 | 
			
		||||
 | 
			
		||||
				int32 start_d = cursor, end_d;
 | 
			
		||||
				if (KeyboardGetKeyStatus(KeyboardKeyRightCtrl) || KeyboardGetKeyStatus(KeyboardKeyLeftCtrl))
 | 
			
		||||
					end_d = _next_word_index(s, 1, cursor, length);
 | 
			
		||||
				else end_d = cursor + 1;
 | 
			
		||||
 | 
			
		||||
				_string_crop(s, start_d, end_d, &length);
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Backspace key
 | 
			
		||||
			case KeyboardKeyBackspace:
 | 
			
		||||
			{
 | 
			
		||||
				if (cursor == 0) break;
 | 
			
		||||
 | 
			
		||||
				int32 end = cursor;
 | 
			
		||||
				if (KeyboardGetKeyStatus(KeyboardKeyRightCtrl) || KeyboardGetKeyStatus(KeyboardKeyLeftCtrl))
 | 
			
		||||
					cursor = _next_word_index(s, -1, cursor, length);
 | 
			
		||||
				else cursor --;
 | 
			
		||||
 | 
			
		||||
				_string_crop(s, cursor, end, &length);
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Home key
 | 
			
		||||
			case KeyboardKeyHome:
 | 
			
		||||
				cursor = 0; break;
 | 
			
		||||
 | 
			
		||||
			// End key
 | 
			
		||||
			case KeyboardKeyEnd:
 | 
			
		||||
				cursor = length; break;
 | 
			
		||||
 | 
			
		||||
			// Rest of keys
 | 
			
		||||
			default:
 | 
			
		||||
				// Ignore non-character keys
 | 
			
		||||
				if (key.Character == 0 || iscntrl(key.Character)) break;
 | 
			
		||||
 | 
			
		||||
				_string_insert(s, key.Character, cursor, &length);
 | 
			
		||||
				++cursor;
 | 
			
		||||
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	// Redraw string
 | 
			
		||||
		ConsoleCursor = cursor_original;
 | 
			
		||||
 | 
			
		||||
		// Remove old string if still there
 | 
			
		||||
		int32 i;
 | 
			
		||||
		if (old_length > length)
 | 
			
		||||
			for (i = 0; i < old_length; i++)
 | 
			
		||||
				_write_char(' ');
 | 
			
		||||
 | 
			
		||||
		old_length = length;
 | 
			
		||||
		ConsoleCursor = cursor_original;
 | 
			
		||||
		_write_string(s);
 | 
			
		||||
 | 
			
		||||
		ConsoleCursor = cursor_original;
 | 
			
		||||
		ConsoleCursorIncreasePos(cursor);
 | 
			
		||||
		ConsoleCursorUpdateHardware();
 | 
			
		||||
 | 
			
		||||
	} while (key.Character != end_char && length < buffer_size);
 | 
			
		||||
 | 
			
		||||
	ConsoleWriteChar('\n');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										155
									
								
								Kernel/debug/console-out.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								Kernel/debug/console-out.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,155 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
extern uint8* VideoPtr;
 | 
			
		||||
 | 
			
		||||
extern UPoint ConsoleCursor;
 | 
			
		||||
extern UPoint ConsoleSize;
 | 
			
		||||
 | 
			
		||||
extern uint8 ConsoleDefaultColor;
 | 
			
		||||
 | 
			
		||||
/**************************************
 | 
			
		||||
 *  Write operations*
 | 
			
		||||
 **************************************/
 | 
			
		||||
void _write_char(char c)
 | 
			
		||||
{
 | 
			
		||||
	switch (c)
 | 
			
		||||
	{
 | 
			
		||||
		case '\n':
 | 
			
		||||
			ConsoleCursorNewline(); break;
 | 
			
		||||
 | 
			
		||||
		case '\r':
 | 
			
		||||
			ConsoleCursor.X = 0; break;
 | 
			
		||||
 | 
			
		||||
		case '\t':
 | 
			
		||||
			ConsoleCursorIncreasePos(6 - ConsoleCursor.X % 6); break;
 | 
			
		||||
 | 
			
		||||
		case '\b':
 | 
			
		||||
			ConsoleCursorIncreasePos(-1);
 | 
			
		||||
			VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X)] = 0;
 | 
			
		||||
			break;
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X)] = c;
 | 
			
		||||
			VideoPtr[2 * (ConsoleCursor.Y * ConsoleSize.X + ConsoleCursor.X) + 1] = ConsoleDefaultColor;
 | 
			
		||||
 | 
			
		||||
			ConsoleCursorIncreasePos(1);
 | 
			
		||||
			break;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _write_string (string s)
 | 
			
		||||
{
 | 
			
		||||
	int32 len = strlen(s), i;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < len; i++)
 | 
			
		||||
		_write_char(s[i]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void ConsoleWriteChar (char c)
 | 
			
		||||
{
 | 
			
		||||
	_write_char(c);
 | 
			
		||||
	ConsoleCursorUpdateHardware();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleWriteString (string s)
 | 
			
		||||
{
 | 
			
		||||
	_write_string(s);
 | 
			
		||||
	ConsoleCursorUpdateHardware();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int32 ConsoleWrite (string format, ...)
 | 
			
		||||
{
 | 
			
		||||
	if (!format || !*format) return 0;
 | 
			
		||||
 | 
			
		||||
	va_list args;
 | 
			
		||||
	va_start (args, format);
 | 
			
		||||
	uint32 i, len = strlen(format);
 | 
			
		||||
 | 
			
		||||
	uint8 temp_color = ConsoleDefaultColor;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < len; i++)
 | 
			
		||||
		if (format[i] != '%') _write_char(format[i]);
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			++i;
 | 
			
		||||
			switch (format[i]) {
 | 
			
		||||
				// Character
 | 
			
		||||
				case 'c': {
 | 
			
		||||
					char c = va_arg (args, char);
 | 
			
		||||
					_write_char(c);
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// String
 | 
			
		||||
				case 's': {
 | 
			
		||||
					int32* c = (int32*) va_arg (args, string);
 | 
			
		||||
					_write_string((string)c);
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Integers
 | 
			
		||||
				case 'd':
 | 
			
		||||
				case 'i': {
 | 
			
		||||
					int32 c = va_arg(args, int32); char temp[32];
 | 
			
		||||
					ConvertIntToString(temp, c, 10);
 | 
			
		||||
					_write_string(temp);
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Integers - hex
 | 
			
		||||
				case 'X':
 | 
			
		||||
				case 'x': {
 | 
			
		||||
					int32 c = va_arg(args, int32); char temp[32];
 | 
			
		||||
					ConvertUIntToString(temp, c, 16);
 | 
			
		||||
					_write_string(temp);
 | 
			
		||||
					break;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Integers - unsigned
 | 
			
		||||
				case 'u': {
 | 
			
		||||
					int32 c = va_arg(args, uint32); char temp[32];
 | 
			
		||||
					ConvertUIntToString (temp, c, 10);
 | 
			
		||||
					_write_string(temp);
 | 
			
		||||
					break;
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				// Colors
 | 
			
		||||
				case '#': {
 | 
			
		||||
					uint8 c = va_arg(args, uint8);
 | 
			
		||||
					ConsoleDefaultColor = c;
 | 
			
		||||
					break; }
 | 
			
		||||
 | 
			
		||||
				default: va_end(args); return 1;
 | 
			
		||||
			};
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	va_end(args);
 | 
			
		||||
 | 
			
		||||
	ConsoleDefaultColor = temp_color;
 | 
			
		||||
	ConsoleCursorUpdateHardware();
 | 
			
		||||
	return i;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleSetChar(Point pos, char c)
 | 
			
		||||
{
 | 
			
		||||
	VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X)] = c;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ConsoleSetColor(Point pos, uint8 color)
 | 
			
		||||
{
 | 
			
		||||
	VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X) + 1] = color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char ConsoleGetChar(Point pos)
 | 
			
		||||
{
 | 
			
		||||
	return (char)VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X)];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8 ConsoleGetColor (Point pos)
 | 
			
		||||
{
 | 
			
		||||
	return VideoPtr[2 * (pos.Y * ConsoleSize.X + pos.X) + 1];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										93
									
								
								Kernel/debug/console.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								Kernel/debug/console.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,93 @@
 | 
			
		||||
#include <debugio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
#include "commands.c"
 | 
			
		||||
 | 
			
		||||
void _process_command (string params[16], int32 count);
 | 
			
		||||
void _command_does_not_exist(string command);
 | 
			
		||||
 | 
			
		||||
void ConsoleMain()
 | 
			
		||||
{
 | 
			
		||||
	char buffer[512];
 | 
			
		||||
	string params[CONSOLE_MAX_PARAMS];
 | 
			
		||||
 | 
			
		||||
	CommandOsver();
 | 
			
		||||
 | 
			
		||||
	for (;;)
 | 
			
		||||
	{
 | 
			
		||||
		// Read a string
 | 
			
		||||
		ConsoleWrite("\n%#] ", Color(ColorBlack, ColorYellow));
 | 
			
		||||
		ConsoleReadString(buffer, 512, '\n');
 | 
			
		||||
 | 
			
		||||
		// Split buffer in params
 | 
			
		||||
		params[0] = buffer;
 | 
			
		||||
 | 
			
		||||
		int32 i=0, len = strlen(buffer), param_count = 0;
 | 
			
		||||
		
 | 
			
		||||
		while (i < len && param_count < CONSOLE_MAX_PARAMS)
 | 
			
		||||
		{
 | 
			
		||||
			// Skip spaces before
 | 
			
		||||
			while (i < len && isspace(buffer[i])) buffer[i++] = 0;
 | 
			
		||||
			if (i == len) break;
 | 
			
		||||
			
 | 
			
		||||
			params[param_count++] = &buffer[i];
 | 
			
		||||
 | 
			
		||||
			// Skip non-spaces
 | 
			
		||||
			while (i < len && !isspace(buffer[i])) i++;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Send command to processing
 | 
			
		||||
		_process_command(params, param_count);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _process_command (string params[CONSOLE_MAX_PARAMS], int32 count)
 | 
			
		||||
{
 | 
			
		||||
	int32 Cmd = -1;
 | 
			
		||||
 | 
			
		||||
	if (count == 0)
 | 
			
		||||
	{
 | 
			
		||||
		ConsoleWrite ("%#! You must enter a command!\n", Color(0, ColorLightRed));
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Lookup command in list
 | 
			
		||||
	int32 i;
 | 
			
		||||
	for (i = 0; i < ConsoleCommandsCount && Cmd == -1; i++)
 | 
			
		||||
		if (strcmp(params[0], ConsoleCommands[i]) == 0) Cmd = i;
 | 
			
		||||
 | 
			
		||||
	switch (Cmd)
 | 
			
		||||
	{
 | 
			
		||||
		case -1: _command_does_not_exist(params[0]); break;
 | 
			
		||||
		case 0: CommandOsver(); break;
 | 
			
		||||
		case 1: CommandTime(); break;
 | 
			
		||||
		case 2: ConsoleClear(); break;
 | 
			
		||||
		case 3: CommandHelp(params, count); break;
 | 
			
		||||
		case 4: CommandDump (params, count); break;
 | 
			
		||||
		case 5: CommandMem(params, count); break;
 | 
			
		||||
		case 6: CommandCrash(); break;
 | 
			
		||||
		case 7: CommandMouse(); break;
 | 
			
		||||
		case 8: CommandRead(params, count); break;
 | 
			
		||||
		case 9:
 | 
			
		||||
		case 10: SystemReboot(); break;
 | 
			
		||||
 | 
			
		||||
		default: ConsoleWrite ("%#! Command %#%s%# was not implemented (yet)!\n",
 | 
			
		||||
				Color(0,ColorLightRed), Color(0,ColorWhite), params[0], Color(0,ColorLightRed)); break;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void _command_does_not_exist(string command)
 | 
			
		||||
{
 | 
			
		||||
	if (strlen(command) > 20)
 | 
			
		||||
	{
 | 
			
		||||
		command[18] = command[19] = command[20] = '.';
 | 
			
		||||
		command[21] = null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ConsoleWrite ("%#! Command %#%s%# does not exist!\n",
 | 
			
		||||
				Color(0,ColorLightRed), Color(0,ColorWhite), command, Color(0,ColorLightRed));
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user