diff --git a/kernel.c b/kernel.c index 9d4f33ad345855b6a26112db4206ba6cbf488dba..f1a14d296878b7dbb26b4cb77d5e0d992f68c35a 100644 --- a/kernel.c +++ b/kernel.c @@ -1,5 +1,5 @@ /* - * Desc: Minimal OS, to show how to boot an x86 machine. UFCFWK-15-2 + * Desc: Minimal OS, to show how to boot an x86 machine. */ #if !defined(__cplusplus) #include <stdbool.h> /* C doesn't have booleans by default. */ @@ -17,8 +17,9 @@ #error "This tutorial needs to be compiled with a ix86-elf compiler" #endif -/* Hardware text mode color constants. */ -enum vga_colour { +/* Hardware text mode colour constants. */ +enum vga_Colour +{ COLOUR_BLACK = 0, COLOUR_BLUE = 1, COLOUR_GREEN = 2, @@ -37,17 +38,20 @@ enum vga_colour { COLOUR_WHITE = 15, }; -uint8_t make_Colour(enum vga_color fg, enum vga_colour bg) { +uint8_t make_Colour(enum vga_Colour fg, enum vga_Colour bg) +{ return fg | bg << 4; } -uint16_t make_vgaentry(char c, uint8_t colour) { +uint16_t make_VgaEntry(char c, uint8_t colour) +{ uint16_t c16 = c; uint16_t colour16 = colour; return c16 | colour16 << 8; } -size_t strlen(const char* str) { +size_t strLen(const char* str) +{ size_t ret = 0; while ( str[ret] != 0 ) ret++; @@ -62,54 +66,138 @@ size_t terminal_Column; uint8_t terminal_Colour; uint16_t* terminal_Buffer; -void terminal_initialize() { - terminal_row = 0; - terminal_column = 0; - terminal_color = make_color(COLOR_LIGHT_GREY, COLOR_BLACK); - terminal_buffer = (uint16_t*) 0xB8000; - for (size_t y = 0; y < VGA_HEIGHT; y++) { - for (size_t x = 0; x < VGA_WIDTH; x++) { +void terminal_Initialize() +{ + terminal_Row = 0; + terminal_Column = 0; + terminal_Colour = make_Colour(COLOUR_LIGHT_GREY, COLOUR_BLACK); + terminal_Buffer = (uint16_t*) 0xB8000; + for (size_t y = 0; y < VGA_HEIGHT; y++) + { + for (size_t x = 0; x < VGA_WIDTH; x++) + { const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = make_vgaentry(' ', terminal_color); + terminal_Buffer[index] = make_VgaEntry(' ', terminal_Colour); } } } -void terminal_setcolor(uint8_t color) { - terminal_color = color; +void terminal_SetColour(uint8_t colour) +{ + terminal_Colour = colour; } -void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) { +void terminal_PutEntryAt(char c, uint8_t colour, size_t x, size_t y) +{ const size_t index = y * VGA_WIDTH + x; - terminal_buffer[index] = make_vgaentry(c, color); -} + terminal_Buffer[index] = make_VgaEntry(c, colour); +} // end terminal_PutEntryAt + +void terminal_PutChar(char c) +{ + terminal_PutEntryAt(c, terminal_Colour, terminal_Column, terminal_Row); + if (++terminal_Column == VGA_WIDTH) + { + terminal_Column = 0; + if (++terminal_Row == VGA_HEIGHT) + { + terminal_Row = 0; + } // end if next space is bottom + } // end if next space is right edge +} // end terminal_PutChar (WRITE EACH CHAR) -void terminal_putchar(char c) { - terminal_putentryat(c, terminal_color, terminal_column, terminal_row); - if (++terminal_column == VGA_WIDTH) { - terminal_column = 0; - if (++terminal_row == VGA_HEIGHT) { - terminal_row = 0; +void terminal_ShiftUp() +{ + + for (size_t y = 0; y < VGA_HEIGHT; y++) + { + for (size_t x = 0; x < VGA_WIDTH; x++) + { + const size_t index = y * VGA_WIDTH + x; + terminal_Buffer[index] = terminal_Buffer[(y + 1) * VGA_WIDTH + x] ; } } + } + -void terminal_writestring(const char* data) { - size_t datalen = strlen(data); - for (size_t i = 0; i < datalen; i++) - terminal_putchar(data[i]); -} +void terminal_WriteString(const char* data) +{ + size_t dataLen = strLen(data); + for (size_t i = 0; i < dataLen; i++) + { + if (data[i] == '\n') + { + if((terminal_Row + 1) > VGA_HEIGHT) + { + terminal_ShiftUp(); + } + else + { + terminal_Row = (terminal_Row + 1); + } + terminal_Column = 0; + } + else + { + terminal_PutChar(data[i]); + } + } // end loop through data +} // end terminal_WriteString (WRITE WHOLE STRING) + +//////////////////////////////////////////////////////////////////// + +// START OF MAIN // + +//////////////////////////////////////////////////////////////////// #if defined(__cplusplus) extern "C" /* Use C linkage for kernel_main. */ #endif -void kernel_main() { +void kernel_Main() +{ /* Initialize terminal interface */ - terminal_initialize(); - - /* Since there is no support for newlines in terminal_putchar - * yet, '\n' will produce some VGA specific character instead. - * This is normal. - */ - terminal_writestring("Hello, kernel World!\n"); -} + terminal_Initialize(); + + for (size_t i = 0; i < 31; i++) + { + + switch(i) + { + + case 0 : + terminal_Colour = make_Colour(COLOUR_RED, COLOUR_BLACK); + terminal_WriteString("Hello, kernel World! MY WORLD\n"); + break; + + case 1 ... 6 : + terminal_Colour = make_Colour(COLOUR_WHITE, COLOUR_BLACK); + terminal_WriteString("Hello, kernel World! MY WORLD\n"); + break; + + case 7 ... 25 : + terminal_Colour = make_Colour(COLOUR_BLUE, COLOUR_BLACK); + terminal_WriteString("Hello, kernel World! MY WORLD\n"); + break; + + case 26 ... 31 : + terminal_Colour = make_Colour(COLOUR_GREEN, COLOUR_BLACK); + terminal_WriteString("Hello, kernel World! MY WORLD\n"); + break; + + } // end colour switcher + + } // end for print 30 line + +} // end kernel main + + +/*###################################### + +Ideas for additions: + +Customisable window size + + + +#######################################*/