diff --git a/.vscode/settings.json b/.vscode/settings.json index a94c29c..8385dd2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -56,5 +56,9 @@ "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, "C_Cpp_Runner.msvcSecureNoWarnings": false, - "makefile.configureOnOpen": false + "makefile.configureOnOpen": false, + "files.associations": { + "portmap.h": "c", + "cstddef": "c" + } } \ No newline at end of file diff --git a/Makefile b/Makefile index a9ba89e..f853317 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -C_FILES=./kernel.c ./console/console.c +C_FILES=./kernel.c ./console/console.c ./device/keyboard/keyboard.c ./device/portmap/portmap.c O_FILES=$(C_FILES:.c=.o) all: qemu_launch diff --git a/boot.asm b/boot.asm index 017baba..dfce953 100644 --- a/boot.asm +++ b/boot.asm @@ -78,7 +78,7 @@ begin_32bit: jmp KERNEL_ADDRESS -NUM_SECTORS db 0x09 +NUM_SECTORS db 0x1F DISK_SUCCESS_MESSAGE db "Read succeeded, continuing", 0 DISK_READ_ERROR_MESSAGE db "Problem with disk read", 0 DISK_SECTOR_ERROR_MESSAGE db "Read and requested sectors differ", 0 diff --git a/console/console.c b/console/console.c index 06c828e..21c8641 100644 --- a/console/console.c +++ b/console/console.c @@ -1,4 +1,6 @@ -#include "../include/console.h" +#include "console.h" +#include "portmap.h" +#include const int VGA_WIDTH = 80; const int VGA_HEIGHT = 25; @@ -41,13 +43,19 @@ void print_character(char c) { void print_character_with_color(char c, VGA_Color bg, VGA_Color fg) { if (c == '\n') { - terminal_position = (terminal_position + (VGA_BYTES_PER_CHARACTER * VGA_WIDTH)) - (terminal_position % (VGA_BYTES_PER_CHARACTER * VGA_WIDTH)); + terminal_position = (terminal_position + ((VGA_BYTES_PER_CHARACTER / 2 * VGA_WIDTH)) - (terminal_position % (VGA_BYTES_PER_CHARACTER / 2 * VGA_WIDTH))); + } else if (c == '\b') { + terminal_position -= 1; + VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER)] = ' '; + int full_color = (bg << 4) | fg; + VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER) + 1] = full_color; } else { VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER)] = c; int full_color = (bg << 4) | fg; VGA_BUFFER[(terminal_position * VGA_BYTES_PER_CHARACTER) + 1] = full_color; terminal_position++; } + update_cursor(); } void print_string(char* string) { @@ -69,4 +77,23 @@ void print_line(char* string) { print_string(string); print_character('\n'); +} + +void print_line_with_color(char* string, VGA_Color bg, VGA_Color fg) { + print_string_with_color(string, bg, fg); + print_character('\n'); +} + +void update_cursor() { + + uint16_t cursor_position = terminal_position >> 0; + + outb(0x3D4, 0x0F); + + outb(0x3D5, (uint8_t) (cursor_position)); + + outb(0x3D4, 0x0E); + + outb(0x3D5, (uint8_t) (cursor_position >> 8)); + } \ No newline at end of file diff --git a/device/keyboard/keyboard.c b/device/keyboard/keyboard.c new file mode 100644 index 0000000..8227d37 --- /dev/null +++ b/device/keyboard/keyboard.c @@ -0,0 +1,30 @@ +#include "portmap.h" +#include + +uint8_t scan(void) { + + uint8_t brk; + + static uint8_t key = 0; + + uint8_t scan = inb(0x60); + + brk = scan & 0x80; + + scan = scan & 0x7f; + + if (brk) { + + return key = 0; + + } else if (scan != key) { + + return key = scan; + + } else { + + return 0; + + } + +} \ No newline at end of file diff --git a/device/portmap/portmap.c b/device/portmap/portmap.c new file mode 100644 index 0000000..a8c68bb --- /dev/null +++ b/device/portmap/portmap.c @@ -0,0 +1,17 @@ +#include + +uint8_t inb(uint16_t port) { + + uint8_t ret; + + __asm__ __volatile__("inb %1, %0":"=a"(ret):"Nd"(port)); + + return ret; + +} + +void outb(uint16_t port, uint8_t val) { + + __asm__ __volatile__("outb %0, %1": : "a" (val), "Nd" (port)); + +} \ No newline at end of file diff --git a/include/console.h b/include/console.h index 101250e..e035560 100644 --- a/include/console.h +++ b/include/console.h @@ -16,6 +16,8 @@ void print_line(char* str); + void update_cursor(); + typedef enum { BLACK=0, diff --git a/include/keyboard.h b/include/keyboard.h new file mode 100644 index 0000000..8a64a8d --- /dev/null +++ b/include/keyboard.h @@ -0,0 +1,23 @@ +#ifndef MYOS_INCLUDE_KEYBOARD_H + + #define MYOS_INCLUDE_KEYBOARD_H + + #include + + static const char charmap[256] = + {0, 0x1B, '1', '2', '3', '4', '5', '6', + '7', '8', '9', '0', '-', '=', '\b', '\t', + 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', + 'o', 'p', '[', ']', '\n', 0, 'a', 's', + 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', + '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', + 'b', 'n', 'm', ',', '.', '/', 0, '*', + 0, ' ', 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, '7', + '8', '9', '-', '4', '5', '6', '+', '1', + '2', '3', '0', '.', 0, 0, 0, 0 + }; + + uint8_t scan(void); + +#endif \ No newline at end of file diff --git a/include/portmap.h b/include/portmap.h new file mode 100644 index 0000000..7cb39a4 --- /dev/null +++ b/include/portmap.h @@ -0,0 +1,11 @@ +#ifndef MYOS_INCLUDE_PORTMAP_H + + #define MYOS_INCLUDE_PORTMAP_H + + #include + + uint8_t inb(uint16_t port); + + void outb(uint16_t port, uint8_t val); + +#endif \ No newline at end of file diff --git a/kernel.c b/kernel.c index 3d3e4d0..bda45d1 100644 --- a/kernel.c +++ b/kernel.c @@ -1,11 +1,19 @@ #include "console.h" +#include "keyboard.h" +#include "util.h" +#include void main() { clear_terminal(); - print_string("HELLO"); - print_line("WORLD"); - print_string("TODAY"); + update_cursor(); + print_line_with_color("Welcome to myOS!", RED, WHITE); + uint8_t byte; + while(1) { + while (byte = scan()) { + print_character(charmap[byte]); + } + } return; }