From a6e43b022208908bf6d12f9e65f54c0f84743946 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 14 Aug 2013 15:42:33 +1200 Subject: atoi(), itoa() --- boot/boot.asm | 55 ++++++++++++++++++++++++++++++ boot/boot.s | 1 + screen/console.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ screen/console.h | 59 ++++++++++++++++++++++++++++++++ string/common.c | 58 ++++++++++++++++++++++++++++++++ string/common.h | 25 ++++++++++++++ 6 files changed, 298 insertions(+) create mode 100644 boot/boot.asm create mode 120000 boot/boot.s create mode 100644 screen/console.c create mode 100644 screen/console.h create mode 100644 string/common.c create mode 100644 string/common.h diff --git a/boot/boot.asm b/boot/boot.asm new file mode 100644 index 0000000..645061c --- /dev/null +++ b/boot/boot.asm @@ -0,0 +1,55 @@ +; Cheers to wiki.osdev.org for this quick starter. It really helped me +; get a prototype off the ground quickly without pissing around + +; Declare constants used for creating a multiboot header. +MBALIGN equ 1<<0 ; align loaded modules on page boundaries +MEMINFO equ 1<<1 ; provide memory map +FLAGS equ MBALIGN | MEMINFO ; this is the Multiboot 'flag' field +MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header +CHECKSUM equ -(MAGIC + FLAGS) ; checksum of above, to prove we are multiboot + +; Declare a header as in the Multiboot Standard. We put this into a special +; section so we can force the header to be in the start of the final program. +; You don't need to understand all these details as it is just magic values that +; is documented in the multiboot standard. The bootloader will search for this +; magic sequence and recognize us as a multiboot kernel. +section .multiboot +align 4 + dd MAGIC + dd FLAGS + dd CHECKSUM + +; Currently the stack pointer register (esp) points at anything and using it may +; cause massive harm. Instead, we'll provide our own stack. We will allocate +; room for a small temporary stack by creating a symbol at the bottom of it, +; then allocating 16384 bytes for it, and finally creating a symbol at the top. +section .bootstrap_stack +align 4 +stack_bottom: +times 16384 db 0 +stack_top: + +; The linker script specifies _start as the entry point to the kernel and the +; bootloader will jump to this position once the kernel has been loaded. It +; doesn't make sense to return from this function as the bootloader is gone. +section .text +global _start +_start: + extern kernel_main + extern console_print + extern console_set_colors + + mov esp, stack_top + call kernel_main + + ; Let the user know that kernel_main() exited + push word 0x0007 + call console_set_colors + push mainexit + call console_print + cli +.hang: + hlt + jmp .hang + +mainexit db 10,"asm: kernel_main() exited",0 diff --git a/boot/boot.s b/boot/boot.s new file mode 120000 index 0000000..320d590 --- /dev/null +++ b/boot/boot.s @@ -0,0 +1 @@ +boot.asm \ No newline at end of file diff --git a/screen/console.c b/screen/console.c new file mode 100644 index 0000000..57f9d6b --- /dev/null +++ b/screen/console.c @@ -0,0 +1,100 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#ifndef CONSOLE_C +#define CONSOLE_C + +#include + +/********************************************************* + * Set up the console buffer and pointer + ********************************************************/ +void console_init() +{ + console_buffer = (char*)0xB8000; + console_pointer = 0; +} + +/********************************************************* + * Update flashing cursor to match console_position + * Note: Does _NOT_ dictate the pos for next printed byte + ********************************************************/ +void console_update_cursor() +{ + outb(0x3D4, 0x0F); + outb(0x3D5, ((console_pointer/2) & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (((console_pointer/2)>>8)&0xFF)); +} + +/********************************************************* + * Clear the console with the current colour scheme + ********************************************************/ +void console_clear() +{ + int console_pointer; + for (console_pointer = 0; console_pointer < 4000; console_pointer++) + { + console_buffer[console_pointer] = console_color; + console_buffer[console_pointer++] = 0; + } + //console_update_cursor(); +} + + +/********************************************************* + * Set the console foreground and background colours + ********************************************************/ +void console_set_colors(uint8_t fg, uint8_t bg) +{ + console_color = (bg << 4) | fg; +} + + +/********************************************************* + * Set the console foreground colour + ********************************************************/ +void console_set_color(uint8_t fg) +{ + console_color &= 0xF0; // Zero-out foreground color + console_color |= fg; // Apply foreground color +} + +/********************************************************* + * Print a null-terminated string to the text screen + ********************************************************/ +void console_print(char *string, ...) +{ + uint16_t i; + for (i = 0; i < strlen(string); i++) + { + // If special char, handle appropriately + if (string[i] < 32) + { + switch (string[i]) + case 10: + console_pointer = 160 + (console_pointer / 160) * 160; + } else { + console_buffer[console_pointer++] = string[i]; + console_buffer[console_pointer++] = console_color; + } + } + console_update_cursor(); +} + +#endif \ No newline at end of file diff --git a/screen/console.h b/screen/console.h new file mode 100644 index 0000000..66a867a --- /dev/null +++ b/screen/console.h @@ -0,0 +1,59 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#ifndef __CONSOLE_H +#define __CONSOLE_H + +#define console_width 80 +#define console_height 25 +#define console_x_pos (console_pointer%(2*console_width)) +#define console_y_pos (uint8_t)(console_pointer/(2*console_width)) + +#define COLOR_BLACK 0x0 +#define COLOR_BLUE 0x1 +#define COLOR_GREEN 0x2 +#define COLOR_TEAL 0x3 +#define COLOR_BROWN 0x4 +#define COLOR_PURPLE 0x5 +#define COLOR_GOLD 0x6 +#define COLOR_BRIGHT_GREY 0x7 +#define COLOR_BRIGHT_GRAY 0x7 // Alias for grey vs gray +#define COLOR_GRAY 0x8 +#define COLOR_GREY 0x8 // Alias for grey vs gray +#define COLOR_BRIGHT_BLUE 0x9 +#define COLOR_BRIGHT_GREEN 0xA +#define COLOR_LIME 0xA // Alias for lime vs bright green +#define COLOR_AQUA 0xB +#define COLOR_RED 0xC +#define COLOR_PINK 0xD +#define COLOR_YELLOW 0xE +#define COLOR_WHITE 0xF + + +char* console_buffer; +uint16_t console_pointer; +uint8_t console_color; + +void console_init(); +void console_update_cursor(); +void console_clear(); +void console_set_colors(uint8_t fg, uint8_t bg); +void console_set_color(uint8_t fg); +void console_print(char *string, ...); + +#endif \ No newline at end of file diff --git a/string/common.c b/string/common.c new file mode 100644 index 0000000..b303cb0 --- /dev/null +++ b/string/common.c @@ -0,0 +1,58 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#ifndef STRING_COMMON_C +#define STRING_COMMON_C + +#include + +uint64_t strlen(char *string) +{ + uint64_t l = 0; + while (string[l] != 0) + l++; + return l; +} + +void itoa(uint32_t num,char *buffer) +{ + uint8_t i; + uint8_t remainder; + + for(i = 10; i > 0; i--) + { + remainder = num % 10; + num = (uint32_t)(num /10); + buffer[i-1] = remainder+48; + console_print(buffer); + } + buffer[10] = 0; + +} + +void atoi(char *string) +{ + uint32_t i; + uint32_t multiplier; + for (i = 0; i < strlen(string); i++) + { + + } +} + +#endif diff --git a/string/common.h b/string/common.h new file mode 100644 index 0000000..28ab975 --- /dev/null +++ b/string/common.h @@ -0,0 +1,25 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#ifndef STRING_COMMON_H +#define STRING_COMMON_H + +uint64_t strlen(char *string); +void itoa(uint32_t num,char *buffer); + +#endif \ No newline at end of file -- cgit v1.1