Created the Malloc function; first stage of memory management implemented.

This commit is contained in:
2019-01-06 20:38:45 -06:00
parent dfe852f69b
commit 67bf4a355d
7 changed files with 69 additions and 149 deletions
-121
View File
@@ -1,121 +0,0 @@
#include "../include/system.h"
#include "../include/gdt.h"
#include "../include/idt.h"
#include "../include/scrn.h"
#include "../include/shell.h"
unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
{
/* Add code here to copy 'count' bytes of data from 'src' to
* 'dest', finally return 'dest' */
const char *sp = (const char *)src;
char *dp = (char *)dest;
for(; count != 0; count--) *dp++ = *sp++;
return dest;
}
unsigned char *memset(unsigned char *dest, unsigned char val, int count)
{
/* Add code here to set 'count' bytes in 'dest' to 'val'.
* Again, return 'dest' */
char *temp = (char *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
{
/* Same as above, but this time, we're working with a 16-bit
* 'val' and dest pointer. Your code can be an exact copy of
* the above, provided that your local variables if any, are
* unsigned short */
unsigned short *temp = (unsigned short *)dest;
for( ; count != 0; count--) *temp++ = val;
return dest;
}
int strlen(const char *str)
{
/* This loops through character array 'str', returning how
* many characters it needs to check before it finds a 0.
* In simple words, it returns the length in bytes of a string */
//size_t retval;
unsigned int retval;
for(retval = 0; *str != '\0'; str++) retval++;
return retval;
}
bool strcmp(char* str1, char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 != len2)
{
return false;
}
for(int i = len1; i >= 0; i--)
{
if(str1[i] != str2[i]) return false;
}
return true;
}
/* We will use this later on for reading from the I/O ports to get data
* from devices such as the keyboard. We are using what is called
* 'inline assembly' in these routines to actually do the work */
unsigned char inportb (unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
/* We will use this to write to I/O ports to send bytes to devices. This
* will be used in the next tutorial for changing the textmode cursor
* position. Again, we use some inline assembly for the stuff that simply
* cannot be done in C */
void outportb (unsigned short _port, unsigned char _data)
{
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
//extern char __BUILD_DATE;
//extern char __BUILD_NUMBER;
/* This is a very simple main() function. All it does is sit in an
* infinite loop. This will be like our 'idle' loop */
void main()
{
/* You would add commands after here */
init_video();
puts("Video initialized.\n");
gdt_install();
puts("Global Descriptor Table initialized.\n");
idt_install();
puts("Interrupt Descriptor Table initialized.\n");
isrs_install();
puts("Interrupt Service Routines installed.\n");
irq_install();
__asm__ __volatile__ ("sti");
puts("Interrupt Requests now allowed.\n");
//timer_install();
//puts("Installed Timer.\n");
kb_install();
puts("Keyboard installed.\n");
puts("SDOS version 0.0.0.1 initialized.\n");
run_shell();
//puts(&__BUILD_DATE);
//puts(&__BUILD_NUMBER);
//puts("Divide by zero check:\n");
//puts(5 / 0);
/* ...and leave this loop in. There is an endless loop in
* 'start.asm' also, if you accidentally delete this next line */
for (;;) idle();
}
void idle()
{
__asm__ __volatile__ ("hlt");
}
+45 -6
View File
@@ -3,6 +3,8 @@
0.0.0.1: Intial Release
0.0.0.2: Added idle loop to main function.
0.0.1.2: Added support for the backspace key.
0.0.2.0: Fixed a bug allowing the user to delete all screen
@@ -12,13 +14,15 @@
#include "../include/scrn.h"
#include "../include/system.h"
static char* Prompt = "\n#>";
unsigned char* commandbuffer;
void run_shell()
{
puts("Starting interactive shell...\n");
puts("Starting interactive shell...");
commandbuffer[0] = '\0';
puts("#>");
puts(Prompt);
for(;;) idle();
}
@@ -50,14 +54,15 @@ void key_pressed(unsigned char* character)
void execmd()
{
proccess_command(commandbuffer);
if(strlen(commandbuffer) == 0)
{
puts("\n#>");
puts(Prompt);
return;
}
if(strcmp(commandbuffer, "version"))
{
puts("\nInteractive Shell Version 0.0.2.0\n");
puts("\nInteractive Shell Version 0.0.2.0");
}
else if(strcmp(commandbuffer,"cls"))
{
@@ -67,8 +72,42 @@ void execmd()
{
puts("\nUnknown command: ");
puts(commandbuffer);
puts(".\n");
puts(".");
}
puts("#>");
puts(Prompt);
commandbuffer[0] = '\0';
}
void proccess_command(char *text)
{
int length = strlen(text);
int currentArrayIndex = 0;
if(length == 0) return;
unsigned char *array[length];
unsigned char *temp[length];
int tempIndex = 0;
for(int i = 0; i < length; i++)
{
if(text[i] != ' ')
{
temp[tempIndex] = text[i];
tempIndex++;
}
else
{
tempIndex++;
temp[tempIndex] = '\0';
int_to_string(tempIndex, 10);
memcpy(array[currentArrayIndex], *temp, tempIndex);
currentArrayIndex++;
tempIndex = 0;
}
}
for(int j = 0; j < currentArrayIndex; j++)
{
puts('\n');
puts(array[currentArrayIndex]);
}
}