diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index f998bd6..261c3f4 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -1,222 +1,127 @@ -#include -#include -#include +#include <_stdio.h> +#include #include #include -#include #include -#include +#include +#include #include -#include <_stdio.h> - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -uint8_t format = 0x0A; - -void setTextStyle(uint8_t style) { - format = style; -} - -void putCharWithFormatAtOffset(uint16_t c, uint16_t offset) { - *(framebuffer + offset) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset((uint16_t) c | (uint16_t) format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - if (c == 0x00) { - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} +#include +#include char HEX_CHARS[] = "0123456789ABCDEF"; -void putHex(char** write, uint32_t x) { - if (x == 0) { - **write = HEX_CHARS[x]; - (*write)++; - **write = HEX_CHARS[x]; - (*write)++; - return; +void putHex(char **write, uint32_t x) { + if (x == 0) { + **write = HEX_CHARS[x]; + (*write)++; + **write = HEX_CHARS[x]; + (*write)++; + return; + } + bool alreadyWriting = false; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - bool alreadyWriting = false; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte >> 4]; - (*write)++; - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; - } + if (alreadyWriting) { + **write = HEX_CHARS[byte >> 4]; + (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } + } } uint8_t hexLength(uint32_t x) { - bool alreadyWriting = false; - uint8_t size = 0; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - size += 2; - } + bool alreadyWriting = false; + uint8_t size = 0; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - return size; + if (alreadyWriting) { + size += 2; + } + } + return size; } uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) { - case 's': - return strlen((char*) x); - case 'x': - return hexLength(x); - } - return 0; + switch (insertType) { + case 's': + return strlen((char *)x); + case 'x': + return hexLength(x); + case 'c': + return 1; + } + return 0; } -void stringInsert(char** write, uint32_t x) { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - (*write)++; - } +void stringInsert(char **write, uint32_t x) { + char *string = (char *)x; + uint32_t length = strlen(string); + for (uint32_t position = 0; position < length; position++) { + **write = string[position]; + (*write)++; + } } -void handleInsert(char** write, char insertType, uint32_t x) { - switch (insertType) { - case 's': - stringInsert(write, x); - return; - case 'x': - putHex(write, x); - return; - } +void handleInsert(char **write, char insertType, uint32_t x) { + switch (insertType) { + case 's': + stringInsert(write, x); + return; + case 'x': + putHex(write, x); + return; + case 'c': + **write = x; + return; + } } -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; +void printf(const char *format, ...) { + uint32_t size = 0; + va_list valist; + va_start(valist, format); + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + size += getInsertLength(format[++i], va_arg(valist, uint32_t)); + continue; } - va_start(valist, format); + size++; + } + va_start(valist, format); - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; + char *data = malloc(size); + char *write = data; + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + handleInsert(&write, format[++i], va_arg(valist, uint32_t)); + continue; } - va_end(valist); + *write = format[i]; + write++; + } + va_end(valist); - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); + Message *message = malloc(sizeof(Message)); + + message->data = data; + message->size = size; + message->next = 0x00; + sendMessage(getPrinterTask(), message); + schedule(getPrinterTask()); } -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; } - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index f998bd6..261c3f4 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -1,222 +1,127 @@ -#include -#include -#include +#include <_stdio.h> +#include #include #include -#include #include -#include +#include +#include #include -#include <_stdio.h> - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -uint8_t format = 0x0A; - -void setTextStyle(uint8_t style) { - format = style; -} - -void putCharWithFormatAtOffset(uint16_t c, uint16_t offset) { - *(framebuffer + offset) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset((uint16_t) c | (uint16_t) format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - if (c == 0x00) { - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} +#include +#include char HEX_CHARS[] = "0123456789ABCDEF"; -void putHex(char** write, uint32_t x) { - if (x == 0) { - **write = HEX_CHARS[x]; - (*write)++; - **write = HEX_CHARS[x]; - (*write)++; - return; +void putHex(char **write, uint32_t x) { + if (x == 0) { + **write = HEX_CHARS[x]; + (*write)++; + **write = HEX_CHARS[x]; + (*write)++; + return; + } + bool alreadyWriting = false; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - bool alreadyWriting = false; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte >> 4]; - (*write)++; - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; - } + if (alreadyWriting) { + **write = HEX_CHARS[byte >> 4]; + (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } + } } uint8_t hexLength(uint32_t x) { - bool alreadyWriting = false; - uint8_t size = 0; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - size += 2; - } + bool alreadyWriting = false; + uint8_t size = 0; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - return size; + if (alreadyWriting) { + size += 2; + } + } + return size; } uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) { - case 's': - return strlen((char*) x); - case 'x': - return hexLength(x); - } - return 0; + switch (insertType) { + case 's': + return strlen((char *)x); + case 'x': + return hexLength(x); + case 'c': + return 1; + } + return 0; } -void stringInsert(char** write, uint32_t x) { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - (*write)++; - } +void stringInsert(char **write, uint32_t x) { + char *string = (char *)x; + uint32_t length = strlen(string); + for (uint32_t position = 0; position < length; position++) { + **write = string[position]; + (*write)++; + } } -void handleInsert(char** write, char insertType, uint32_t x) { - switch (insertType) { - case 's': - stringInsert(write, x); - return; - case 'x': - putHex(write, x); - return; - } +void handleInsert(char **write, char insertType, uint32_t x) { + switch (insertType) { + case 's': + stringInsert(write, x); + return; + case 'x': + putHex(write, x); + return; + case 'c': + **write = x; + return; + } } -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; +void printf(const char *format, ...) { + uint32_t size = 0; + va_list valist; + va_start(valist, format); + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + size += getInsertLength(format[++i], va_arg(valist, uint32_t)); + continue; } - va_start(valist, format); + size++; + } + va_start(valist, format); - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; + char *data = malloc(size); + char *write = data; + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + handleInsert(&write, format[++i], va_arg(valist, uint32_t)); + continue; } - va_end(valist); + *write = format[i]; + write++; + } + va_end(valist); - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); + Message *message = malloc(sizeof(Message)); + + message->data = data; + message->size = size; + message->next = 0x00; + sendMessage(getPrinterTask(), message); + schedule(getPrinterTask()); } -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; } - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/drivers/textMode/terminal.c b/src/kernel/drivers/textMode/terminal.c new file mode 100644 index 0000000..c94a9e4 --- /dev/null +++ b/src/kernel/drivers/textMode/terminal.c @@ -0,0 +1,108 @@ +#include +#include + +uint16_t *framebuffer = (uint16_t *)FRAMEBUFFER_LOCATION; +uint8_t currentFormat = 0x0A; + +void setTextStyle(uint8_t style) { currentFormat = style; } + +void setCharAtOffset(uint16_t offset, char c, uint8_t format) { + *((uint16_t *)FRAMEBUFFER_LOCATION + offset) = format << 8 | c; +} + +uint16_t getOffset(uint8_t x, uint8_t y) { return VIDEO_WIDTH * y + x; } + +void clearScreen() { + setCursorOffset(0); + for (int y = 0; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, y), ' ', currentFormat); + } + } +} + +void shiftUp() { + for (int y = 1; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + uint16_t old = + *(uint16_t *)(FRAMEBUFFER_LOCATION + getOffset(x, y) * 2); + setCharAtOffset(getOffset(x, y - 1), old, old >> 8); + } + } + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, VIDEO_HEIGHT - 1), ' ', currentFormat); + } +} + +uint16_t cursorOffset = 0; + +void newLine() { + uint8_t x = cursorOffset % VIDEO_WIDTH; + uint8_t y = cursorOffset / VIDEO_WIDTH; + x = 0; + y++; + if (y >= VIDEO_HEIGHT) { + shiftUp(); + y--; + } + setCursorPosition(x, y); + cursorOffset = getOffset(x, y); +} + +typedef enum { STANDARD, ANSI_ESCAPE } States; + +typedef enum { BRACKET_OPEN, BUFFER } AnsiEscapeStates; + +States currentState = STANDARD; + +AnsiEscapeStates currentAnsiEscapeState = BRACKET_OPEN; + +uint32_t ansiEscapeBuffer1 = 0; +uint32_t ansiEscapeBuffer2 = 0; + +uint32_t *currentBuffer = &ansiEscapeBuffer1; + +void writeChar(char c) { + if (c == 0) + return; + switch (currentState) { + case STANDARD: + if (c == '\x1B') { + currentState = ANSI_ESCAPE; + currentAnsiEscapeState = BRACKET_OPEN; + } + break; + case ANSI_ESCAPE: + switch (currentAnsiEscapeState) { + case BRACKET_OPEN: + currentAnsiEscapeState = BUFFER; + return; + case BUFFER: + switch (c) { + case 'H': + cursorOffset = 0; + setCursorOffset(0); + currentState = STANDARD; + return; + case 'J': + clearScreen(); + currentState = STANDARD; + return; + } + } + } + if (currentState != STANDARD) { + return; + } + switch (c) { + case '\n': + newLine(); + return; + case '\b': + setCharAtOffset(--cursorOffset, ' ', currentFormat); + setCursorOffset(cursorOffset); + return; + } + setCharAtOffset(cursorOffset++, c, currentFormat); + setCursorOffset(cursorOffset); +} diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index f998bd6..261c3f4 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -1,222 +1,127 @@ -#include -#include -#include +#include <_stdio.h> +#include #include #include -#include #include -#include +#include +#include #include -#include <_stdio.h> - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -uint8_t format = 0x0A; - -void setTextStyle(uint8_t style) { - format = style; -} - -void putCharWithFormatAtOffset(uint16_t c, uint16_t offset) { - *(framebuffer + offset) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset((uint16_t) c | (uint16_t) format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - if (c == 0x00) { - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} +#include +#include char HEX_CHARS[] = "0123456789ABCDEF"; -void putHex(char** write, uint32_t x) { - if (x == 0) { - **write = HEX_CHARS[x]; - (*write)++; - **write = HEX_CHARS[x]; - (*write)++; - return; +void putHex(char **write, uint32_t x) { + if (x == 0) { + **write = HEX_CHARS[x]; + (*write)++; + **write = HEX_CHARS[x]; + (*write)++; + return; + } + bool alreadyWriting = false; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - bool alreadyWriting = false; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte >> 4]; - (*write)++; - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; - } + if (alreadyWriting) { + **write = HEX_CHARS[byte >> 4]; + (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } + } } uint8_t hexLength(uint32_t x) { - bool alreadyWriting = false; - uint8_t size = 0; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - size += 2; - } + bool alreadyWriting = false; + uint8_t size = 0; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - return size; + if (alreadyWriting) { + size += 2; + } + } + return size; } uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) { - case 's': - return strlen((char*) x); - case 'x': - return hexLength(x); - } - return 0; + switch (insertType) { + case 's': + return strlen((char *)x); + case 'x': + return hexLength(x); + case 'c': + return 1; + } + return 0; } -void stringInsert(char** write, uint32_t x) { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - (*write)++; - } +void stringInsert(char **write, uint32_t x) { + char *string = (char *)x; + uint32_t length = strlen(string); + for (uint32_t position = 0; position < length; position++) { + **write = string[position]; + (*write)++; + } } -void handleInsert(char** write, char insertType, uint32_t x) { - switch (insertType) { - case 's': - stringInsert(write, x); - return; - case 'x': - putHex(write, x); - return; - } +void handleInsert(char **write, char insertType, uint32_t x) { + switch (insertType) { + case 's': + stringInsert(write, x); + return; + case 'x': + putHex(write, x); + return; + case 'c': + **write = x; + return; + } } -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; +void printf(const char *format, ...) { + uint32_t size = 0; + va_list valist; + va_start(valist, format); + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + size += getInsertLength(format[++i], va_arg(valist, uint32_t)); + continue; } - va_start(valist, format); + size++; + } + va_start(valist, format); - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; + char *data = malloc(size); + char *write = data; + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + handleInsert(&write, format[++i], va_arg(valist, uint32_t)); + continue; } - va_end(valist); + *write = format[i]; + write++; + } + va_end(valist); - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); + Message *message = malloc(sizeof(Message)); + + message->data = data; + message->size = size; + message->next = 0x00; + sendMessage(getPrinterTask(), message); + schedule(getPrinterTask()); } -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; } - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/drivers/textMode/terminal.c b/src/kernel/drivers/textMode/terminal.c new file mode 100644 index 0000000..c94a9e4 --- /dev/null +++ b/src/kernel/drivers/textMode/terminal.c @@ -0,0 +1,108 @@ +#include +#include + +uint16_t *framebuffer = (uint16_t *)FRAMEBUFFER_LOCATION; +uint8_t currentFormat = 0x0A; + +void setTextStyle(uint8_t style) { currentFormat = style; } + +void setCharAtOffset(uint16_t offset, char c, uint8_t format) { + *((uint16_t *)FRAMEBUFFER_LOCATION + offset) = format << 8 | c; +} + +uint16_t getOffset(uint8_t x, uint8_t y) { return VIDEO_WIDTH * y + x; } + +void clearScreen() { + setCursorOffset(0); + for (int y = 0; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, y), ' ', currentFormat); + } + } +} + +void shiftUp() { + for (int y = 1; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + uint16_t old = + *(uint16_t *)(FRAMEBUFFER_LOCATION + getOffset(x, y) * 2); + setCharAtOffset(getOffset(x, y - 1), old, old >> 8); + } + } + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, VIDEO_HEIGHT - 1), ' ', currentFormat); + } +} + +uint16_t cursorOffset = 0; + +void newLine() { + uint8_t x = cursorOffset % VIDEO_WIDTH; + uint8_t y = cursorOffset / VIDEO_WIDTH; + x = 0; + y++; + if (y >= VIDEO_HEIGHT) { + shiftUp(); + y--; + } + setCursorPosition(x, y); + cursorOffset = getOffset(x, y); +} + +typedef enum { STANDARD, ANSI_ESCAPE } States; + +typedef enum { BRACKET_OPEN, BUFFER } AnsiEscapeStates; + +States currentState = STANDARD; + +AnsiEscapeStates currentAnsiEscapeState = BRACKET_OPEN; + +uint32_t ansiEscapeBuffer1 = 0; +uint32_t ansiEscapeBuffer2 = 0; + +uint32_t *currentBuffer = &ansiEscapeBuffer1; + +void writeChar(char c) { + if (c == 0) + return; + switch (currentState) { + case STANDARD: + if (c == '\x1B') { + currentState = ANSI_ESCAPE; + currentAnsiEscapeState = BRACKET_OPEN; + } + break; + case ANSI_ESCAPE: + switch (currentAnsiEscapeState) { + case BRACKET_OPEN: + currentAnsiEscapeState = BUFFER; + return; + case BUFFER: + switch (c) { + case 'H': + cursorOffset = 0; + setCursorOffset(0); + currentState = STANDARD; + return; + case 'J': + clearScreen(); + currentState = STANDARD; + return; + } + } + } + if (currentState != STANDARD) { + return; + } + switch (c) { + case '\n': + newLine(); + return; + case '\b': + setCharAtOffset(--cursorOffset, ' ', currentFormat); + setCursorOffset(cursorOffset); + return; + } + setCharAtOffset(cursorOffset++, c, currentFormat); + setCursorOffset(cursorOffset); +} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 39eb704..c9b62b9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -22,6 +22,8 @@ setupDevices(); initInterrupts(); yields(); - while (1) - ; + while (1) { + asm volatile("hlt"); + yields(); + } } diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index f998bd6..261c3f4 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -1,222 +1,127 @@ -#include -#include -#include +#include <_stdio.h> +#include #include #include -#include #include -#include +#include +#include #include -#include <_stdio.h> - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -uint8_t format = 0x0A; - -void setTextStyle(uint8_t style) { - format = style; -} - -void putCharWithFormatAtOffset(uint16_t c, uint16_t offset) { - *(framebuffer + offset) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset((uint16_t) c | (uint16_t) format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - if (c == 0x00) { - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} +#include +#include char HEX_CHARS[] = "0123456789ABCDEF"; -void putHex(char** write, uint32_t x) { - if (x == 0) { - **write = HEX_CHARS[x]; - (*write)++; - **write = HEX_CHARS[x]; - (*write)++; - return; +void putHex(char **write, uint32_t x) { + if (x == 0) { + **write = HEX_CHARS[x]; + (*write)++; + **write = HEX_CHARS[x]; + (*write)++; + return; + } + bool alreadyWriting = false; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - bool alreadyWriting = false; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte >> 4]; - (*write)++; - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; - } + if (alreadyWriting) { + **write = HEX_CHARS[byte >> 4]; + (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } + } } uint8_t hexLength(uint32_t x) { - bool alreadyWriting = false; - uint8_t size = 0; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - size += 2; - } + bool alreadyWriting = false; + uint8_t size = 0; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - return size; + if (alreadyWriting) { + size += 2; + } + } + return size; } uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) { - case 's': - return strlen((char*) x); - case 'x': - return hexLength(x); - } - return 0; + switch (insertType) { + case 's': + return strlen((char *)x); + case 'x': + return hexLength(x); + case 'c': + return 1; + } + return 0; } -void stringInsert(char** write, uint32_t x) { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - (*write)++; - } +void stringInsert(char **write, uint32_t x) { + char *string = (char *)x; + uint32_t length = strlen(string); + for (uint32_t position = 0; position < length; position++) { + **write = string[position]; + (*write)++; + } } -void handleInsert(char** write, char insertType, uint32_t x) { - switch (insertType) { - case 's': - stringInsert(write, x); - return; - case 'x': - putHex(write, x); - return; - } +void handleInsert(char **write, char insertType, uint32_t x) { + switch (insertType) { + case 's': + stringInsert(write, x); + return; + case 'x': + putHex(write, x); + return; + case 'c': + **write = x; + return; + } } -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; +void printf(const char *format, ...) { + uint32_t size = 0; + va_list valist; + va_start(valist, format); + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + size += getInsertLength(format[++i], va_arg(valist, uint32_t)); + continue; } - va_start(valist, format); + size++; + } + va_start(valist, format); - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; + char *data = malloc(size); + char *write = data; + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + handleInsert(&write, format[++i], va_arg(valist, uint32_t)); + continue; } - va_end(valist); + *write = format[i]; + write++; + } + va_end(valist); - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); + Message *message = malloc(sizeof(Message)); + + message->data = data; + message->size = size; + message->next = 0x00; + sendMessage(getPrinterTask(), message); + schedule(getPrinterTask()); } -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; } - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/drivers/textMode/terminal.c b/src/kernel/drivers/textMode/terminal.c new file mode 100644 index 0000000..c94a9e4 --- /dev/null +++ b/src/kernel/drivers/textMode/terminal.c @@ -0,0 +1,108 @@ +#include +#include + +uint16_t *framebuffer = (uint16_t *)FRAMEBUFFER_LOCATION; +uint8_t currentFormat = 0x0A; + +void setTextStyle(uint8_t style) { currentFormat = style; } + +void setCharAtOffset(uint16_t offset, char c, uint8_t format) { + *((uint16_t *)FRAMEBUFFER_LOCATION + offset) = format << 8 | c; +} + +uint16_t getOffset(uint8_t x, uint8_t y) { return VIDEO_WIDTH * y + x; } + +void clearScreen() { + setCursorOffset(0); + for (int y = 0; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, y), ' ', currentFormat); + } + } +} + +void shiftUp() { + for (int y = 1; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + uint16_t old = + *(uint16_t *)(FRAMEBUFFER_LOCATION + getOffset(x, y) * 2); + setCharAtOffset(getOffset(x, y - 1), old, old >> 8); + } + } + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, VIDEO_HEIGHT - 1), ' ', currentFormat); + } +} + +uint16_t cursorOffset = 0; + +void newLine() { + uint8_t x = cursorOffset % VIDEO_WIDTH; + uint8_t y = cursorOffset / VIDEO_WIDTH; + x = 0; + y++; + if (y >= VIDEO_HEIGHT) { + shiftUp(); + y--; + } + setCursorPosition(x, y); + cursorOffset = getOffset(x, y); +} + +typedef enum { STANDARD, ANSI_ESCAPE } States; + +typedef enum { BRACKET_OPEN, BUFFER } AnsiEscapeStates; + +States currentState = STANDARD; + +AnsiEscapeStates currentAnsiEscapeState = BRACKET_OPEN; + +uint32_t ansiEscapeBuffer1 = 0; +uint32_t ansiEscapeBuffer2 = 0; + +uint32_t *currentBuffer = &ansiEscapeBuffer1; + +void writeChar(char c) { + if (c == 0) + return; + switch (currentState) { + case STANDARD: + if (c == '\x1B') { + currentState = ANSI_ESCAPE; + currentAnsiEscapeState = BRACKET_OPEN; + } + break; + case ANSI_ESCAPE: + switch (currentAnsiEscapeState) { + case BRACKET_OPEN: + currentAnsiEscapeState = BUFFER; + return; + case BUFFER: + switch (c) { + case 'H': + cursorOffset = 0; + setCursorOffset(0); + currentState = STANDARD; + return; + case 'J': + clearScreen(); + currentState = STANDARD; + return; + } + } + } + if (currentState != STANDARD) { + return; + } + switch (c) { + case '\n': + newLine(); + return; + case '\b': + setCharAtOffset(--cursorOffset, ' ', currentFormat); + setCursorOffset(cursorOffset); + return; + } + setCharAtOffset(cursorOffset++, c, currentFormat); + setCursorOffset(cursorOffset); +} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 39eb704..c9b62b9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -22,6 +22,8 @@ setupDevices(); initInterrupts(); yields(); - while (1) - ; + while (1) { + asm volatile("hlt"); + yields(); + } } diff --git a/src/kernel/task/osTasks.c b/src/kernel/task/osTasks.c index adf2dba..dfa6988 100644 --- a/src/kernel/task/osTasks.c +++ b/src/kernel/task/osTasks.c @@ -1,21 +1,23 @@ -#include -#include #include <_stdio.h> -#include #include +#include +#include +#include +#include Task mainTask; Task printerTask; void printLoop() { - clearScreen(); while (1) { - Message* message = popMessage(&printerTask); + Message *message = popMessage(&printerTask); if (message == 0) { yield(); continue; } - putChars(message->data, message->size); + for (uint16_t i = 0; i < message->size; i++) { + writeChar(((char *)(message->data))[i]); + } free(message->data); free(message); } @@ -23,12 +25,10 @@ void initOSTasks() { setRunningTask(&mainTask); - createTask(&printerTask, (uint32_t) printLoop, 0x0, 0x0); + createTask(&printerTask, (uint32_t)printLoop, 0x0, 0x0); schedule(&printerTask); // more tasks . . . yields(); } -inline Task* getPrinterTask() { - return &printerTask; -} \ No newline at end of file +inline Task *getPrinterTask() { return &printerTask; } diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..39f9642 --- /dev/null +++ b/.clang-format @@ -0,0 +1 @@ +IndentWidth: 4 diff --git a/src/include/_stdio.h b/src/include/_stdio.h index 1fbe95a..164e21c 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -3,19 +3,8 @@ #include -#define FRAMEBUFFER_LOCATION 0x000B8000 -#define VIDEO_WIDTH 80 -#define VIDEO_HEIGHT 25 +extern void printf(const char *format, ...); -extern void setTextStyle(uint8_t style); -extern void clearScreen(); -extern void printf(const char* format, ...); -extern void putCharAt(char c, uint8_t x, uint8_t y); -extern void putCharsAt(char* string, uint8_t x, uint8_t y); -extern void puts(char* string); -extern void putChars(char* string, uint32_t length); -extern void putChar(char c); +extern uint32_t strlen(char *string); -extern uint32_t strlen(char* string); - -#endif \ No newline at end of file +#endif diff --git a/src/include/alloc.h b/src/include/alloc.h index 82023d4..0e40e82 100644 --- a/src/include/alloc.h +++ b/src/include/alloc.h @@ -4,25 +4,22 @@ #include #include -typedef enum MemoryState { - FREE = 0, - IN_USE = 1 -} MemoryState; +typedef enum MemoryState { FREE = 0, IN_USE = 1 } MemoryState; typedef struct MemoryBlock { - struct MemoryBlock* last; - struct MemoryBlock* next; - MemoryState state; - uint32_t size; - Task* task; + struct MemoryBlock *last; + struct MemoryBlock *next; + MemoryState state; + uint32_t size; + Task *task; } MemoryBlock; extern void initMemoryAllocation(uint32_t kernelEnd); -extern void* malloc(uint32_t size); -extern void* mallocTask(uint32_t size, Task* task); -extern void free(void* location); +extern void *malloc(uint32_t size); +extern void *mallocTask(uint32_t size, Task *task); +extern void free(void *location); extern void printMemoryStack(); -extern void freeTaskAllocations(Task* task); -extern Task* getCurrentTask(); +extern void freeTaskAllocations(Task *task); +extern Task *getCurrentTask(); -#endif \ No newline at end of file +#endif diff --git a/src/include/message.h b/src/include/message.h index 2f8ddbe..12e3a85 100644 --- a/src/include/message.h +++ b/src/include/message.h @@ -3,10 +3,10 @@ #include -typedef struct Message { - void* data; - uint32_t size; - struct Message* next; +typedef struct { + void *data; + uint32_t size; + struct Message *next; } Message; -#endif \ No newline at end of file +#endif diff --git a/src/include/terminal.h b/src/include/terminal.h new file mode 100644 index 0000000..1fe3b79 --- /dev/null +++ b/src/include/terminal.h @@ -0,0 +1,14 @@ +#ifndef TERMINAL_H +#define TERMINAL_H + +#include + +#define FRAMEBUFFER_LOCATION 0x000B8000 +#define VIDEO_WIDTH 80 +#define VIDEO_HEIGHT 25 + +extern void writeString(char *string); + +extern void writeChar(char c); + +#endif diff --git a/src/kernel/drivers/interrupts/keyboard/keyboard.c b/src/kernel/drivers/interrupts/keyboard/keyboard.c index dd4dcb9..8c199d3 100644 --- a/src/kernel/drivers/interrupts/keyboard/keyboard.c +++ b/src/kernel/drivers/interrupts/keyboard/keyboard.c @@ -1,45 +1,63 @@ -#include -#include #include <_stdio.h> #include +#include #include #include +#include uint64_t pressedKeys1 = 0; uint64_t pressedKeys2 = 0; unsigned char keycodes[128] = { - 0, 27, '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,0,0,0,'-',0,0,0,'+',0,0,0,0,0,0,0,0,0,0,0 -}; + 0, 27, '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, 0, 0, 0, '-', 0, 0, 0, + '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -void keyboardHandler() { - int i, scancode; - for(i = 1000; i > 0; i++) { +const char *altKeycodes[128] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, "\x1B[H", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +uint8_t getScancode() { + int_fast16_t scancode = -1; + for (uint16_t i = 0; i < 1000; i++) { if ((inb(0x64) & 1) == 0) { continue; } scancode = inb(0x60); break; } - if(i > 0) { - if(scancode & 0x80) { + return scancode; +} + +void keyboardHandler() { + int_fast16_t scancode = getScancode(); + if (scancode < 0) { + return; + } + if (scancode == 0xE0) { + // double length + scancode = getScancode(); + if (scancode & 0x80) { + return; } - else { - putChar(keycodes[scancode]); - yields(); + if (altKeycodes[scancode] == 0) { + printf("unknown compound keycode %x\n", scancode); + return; } + printf("%s", altKeycodes[scancode]); + return; + } + if (!(scancode & 0x80)) { + printf("%c", keycodes[scancode]); } } -void setupKeyboard() { - setIRQHandler(1, &keyboardHandler); -} \ No newline at end of file +void setupKeyboard() { setIRQHandler(1, &keyboardHandler); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index f998bd6..261c3f4 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -1,222 +1,127 @@ -#include -#include -#include +#include <_stdio.h> +#include #include #include -#include #include -#include +#include +#include #include -#include <_stdio.h> - -uint16_t* framebuffer = (uint16_t*) FRAMEBUFFER_LOCATION; -uint8_t format = 0x0A; - -void setTextStyle(uint8_t style) { - format = style; -} - -void putCharWithFormatAtOffset(uint16_t c, uint16_t offset) { - *(framebuffer + offset) = c; -} - -void putCharWithFormatAtPosition(uint8_t x, uint8_t y, uint32_t c) { - putCharWithFormatAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -void putCharAtOffset(char c, uint16_t offset) { - putCharWithFormatAtOffset((uint16_t) c | (uint16_t) format << 8, offset); -} - -void putCharAt(char c, uint8_t x, uint8_t y) { - putCharAtOffset(c, (x + y * VIDEO_WIDTH)); -} - -uint16_t getCharWithFormatAt(uint8_t x, uint8_t y) { - return *(framebuffer + x + y * VIDEO_WIDTH); -} - -void putCharsAt(char* string, uint8_t x, uint8_t y) { - uint16_t position = 0; - char c = *string; - while (c != 0x00) { - putCharAt(c, x + position, y); - position++; - c = *(string + position); - } -} - -void shiftUp() { - for (int y = 1; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, y-1, getCharWithFormatAt(x, y)); - } - } - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharWithFormatAtPosition(x, VIDEO_HEIGHT-1, 0x00); - } -} - -void newLine() { - uint16_t offset = getCursorOffset(); - uint8_t x = offset % VIDEO_WIDTH; - uint8_t y = offset / VIDEO_WIDTH; - x = 0; - y++; - if (y >= VIDEO_HEIGHT) { - shiftUp(); - y--; - } - setCursorPosition(x, y); -} - -void putChar(char c) { - if (c == '\n') { - newLine(); - return; - } - if (c == 0x00) { - return; - } - uint16_t offset = getCursorOffset(); - putCharAtOffset(c, offset); - setCursorOffset(offset + 1); -} - -void puts(char* string) { - while (*string != 0x00) { - putChar(*string); - string++; - } -} +#include +#include char HEX_CHARS[] = "0123456789ABCDEF"; -void putHex(char** write, uint32_t x) { - if (x == 0) { - **write = HEX_CHARS[x]; - (*write)++; - **write = HEX_CHARS[x]; - (*write)++; - return; +void putHex(char **write, uint32_t x) { + if (x == 0) { + **write = HEX_CHARS[x]; + (*write)++; + **write = HEX_CHARS[x]; + (*write)++; + return; + } + bool alreadyWriting = false; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - bool alreadyWriting = false; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - **write = HEX_CHARS[byte >> 4]; - (*write)++; - **write = HEX_CHARS[byte & 0x0F]; - (*write)++; - } + if (alreadyWriting) { + **write = HEX_CHARS[byte >> 4]; + (*write)++; + **write = HEX_CHARS[byte & 0x0F]; + (*write)++; } + } } uint8_t hexLength(uint32_t x) { - bool alreadyWriting = false; - uint8_t size = 0; - for (int position = 3; position >= 0; position--) { - uint8_t byte = (x >> (position * 8)) & 0xFF; - if (byte != 0x00) { - alreadyWriting = true; - } - if (alreadyWriting) { - size += 2; - } + bool alreadyWriting = false; + uint8_t size = 0; + for (int position = 3; position >= 0; position--) { + uint8_t byte = (x >> (position * 8)) & 0xFF; + if (byte != 0x00) { + alreadyWriting = true; } - return size; + if (alreadyWriting) { + size += 2; + } + } + return size; } uint32_t getInsertLength(char insertType, uint32_t x) { - switch (insertType) { - case 's': - return strlen((char*) x); - case 'x': - return hexLength(x); - } - return 0; + switch (insertType) { + case 's': + return strlen((char *)x); + case 'x': + return hexLength(x); + case 'c': + return 1; + } + return 0; } -void stringInsert(char** write, uint32_t x) { - char* string = (char*) x; - uint32_t length = strlen(string); - for (uint32_t position = 0; position < length; position++) { - **write = string[position]; - (*write)++; - } +void stringInsert(char **write, uint32_t x) { + char *string = (char *)x; + uint32_t length = strlen(string); + for (uint32_t position = 0; position < length; position++) { + **write = string[position]; + (*write)++; + } } -void handleInsert(char** write, char insertType, uint32_t x) { - switch (insertType) { - case 's': - stringInsert(write, x); - return; - case 'x': - putHex(write, x); - return; - } +void handleInsert(char **write, char insertType, uint32_t x) { + switch (insertType) { + case 's': + stringInsert(write, x); + return; + case 'x': + putHex(write, x); + return; + case 'c': + **write = x; + return; + } } -void printf(const char* format, ...) { - uint32_t size = 0; - va_list valist; - va_start(valist, format); - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - size += getInsertLength(format[++i], va_arg(valist, uint32_t)); - continue; - } - size++; +void printf(const char *format, ...) { + uint32_t size = 0; + va_list valist; + va_start(valist, format); + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + size += getInsertLength(format[++i], va_arg(valist, uint32_t)); + continue; } - va_start(valist, format); + size++; + } + va_start(valist, format); - char* data = malloc(size); - char* write = data; - for (int i = 0; format[i] != 0; i++) { - if (format[i] == '%') { - handleInsert(&write, format[++i], va_arg(valist, uint32_t)); - continue; - } - *write = format[i]; - write++; + char *data = malloc(size); + char *write = data; + for (int i = 0; format[i] != 0; i++) { + if (format[i] == '%') { + handleInsert(&write, format[++i], va_arg(valist, uint32_t)); + continue; } - va_end(valist); + *write = format[i]; + write++; + } + va_end(valist); - Message* message = malloc(sizeof(Message)); - - message->data = data; - message->size = size; - message->next = 0x00; - sendMessage(getPrinterTask(), message); - schedule(getPrinterTask()); + Message *message = malloc(sizeof(Message)); + + message->data = data; + message->size = size; + message->next = 0x00; + sendMessage(getPrinterTask(), message); + schedule(getPrinterTask()); } -void clearScreen() { - setCursorOffset(0); - for (int y = 0; y < VIDEO_HEIGHT; y++) { - for (int x = 0; x < VIDEO_WIDTH; x++) { - putCharAt(' ', x, y); - } - } +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; } - -void putChars(char* string, uint32_t length) { - for (uint32_t i = 0; i < length; i++) { - if (string[i] == '\n') { - newLine(); - continue; - } - putChar(string[i]); - } -} - -uint32_t strlen(char* string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} \ No newline at end of file diff --git a/src/kernel/drivers/textMode/terminal.c b/src/kernel/drivers/textMode/terminal.c new file mode 100644 index 0000000..c94a9e4 --- /dev/null +++ b/src/kernel/drivers/textMode/terminal.c @@ -0,0 +1,108 @@ +#include +#include + +uint16_t *framebuffer = (uint16_t *)FRAMEBUFFER_LOCATION; +uint8_t currentFormat = 0x0A; + +void setTextStyle(uint8_t style) { currentFormat = style; } + +void setCharAtOffset(uint16_t offset, char c, uint8_t format) { + *((uint16_t *)FRAMEBUFFER_LOCATION + offset) = format << 8 | c; +} + +uint16_t getOffset(uint8_t x, uint8_t y) { return VIDEO_WIDTH * y + x; } + +void clearScreen() { + setCursorOffset(0); + for (int y = 0; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, y), ' ', currentFormat); + } + } +} + +void shiftUp() { + for (int y = 1; y < VIDEO_HEIGHT; y++) { + for (int x = 0; x < VIDEO_WIDTH; x++) { + uint16_t old = + *(uint16_t *)(FRAMEBUFFER_LOCATION + getOffset(x, y) * 2); + setCharAtOffset(getOffset(x, y - 1), old, old >> 8); + } + } + for (int x = 0; x < VIDEO_WIDTH; x++) { + setCharAtOffset(getOffset(x, VIDEO_HEIGHT - 1), ' ', currentFormat); + } +} + +uint16_t cursorOffset = 0; + +void newLine() { + uint8_t x = cursorOffset % VIDEO_WIDTH; + uint8_t y = cursorOffset / VIDEO_WIDTH; + x = 0; + y++; + if (y >= VIDEO_HEIGHT) { + shiftUp(); + y--; + } + setCursorPosition(x, y); + cursorOffset = getOffset(x, y); +} + +typedef enum { STANDARD, ANSI_ESCAPE } States; + +typedef enum { BRACKET_OPEN, BUFFER } AnsiEscapeStates; + +States currentState = STANDARD; + +AnsiEscapeStates currentAnsiEscapeState = BRACKET_OPEN; + +uint32_t ansiEscapeBuffer1 = 0; +uint32_t ansiEscapeBuffer2 = 0; + +uint32_t *currentBuffer = &ansiEscapeBuffer1; + +void writeChar(char c) { + if (c == 0) + return; + switch (currentState) { + case STANDARD: + if (c == '\x1B') { + currentState = ANSI_ESCAPE; + currentAnsiEscapeState = BRACKET_OPEN; + } + break; + case ANSI_ESCAPE: + switch (currentAnsiEscapeState) { + case BRACKET_OPEN: + currentAnsiEscapeState = BUFFER; + return; + case BUFFER: + switch (c) { + case 'H': + cursorOffset = 0; + setCursorOffset(0); + currentState = STANDARD; + return; + case 'J': + clearScreen(); + currentState = STANDARD; + return; + } + } + } + if (currentState != STANDARD) { + return; + } + switch (c) { + case '\n': + newLine(); + return; + case '\b': + setCharAtOffset(--cursorOffset, ' ', currentFormat); + setCursorOffset(cursorOffset); + return; + } + setCharAtOffset(cursorOffset++, c, currentFormat); + setCursorOffset(cursorOffset); +} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 39eb704..c9b62b9 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -22,6 +22,8 @@ setupDevices(); initInterrupts(); yields(); - while (1) - ; + while (1) { + asm volatile("hlt"); + yields(); + } } diff --git a/src/kernel/task/osTasks.c b/src/kernel/task/osTasks.c index adf2dba..dfa6988 100644 --- a/src/kernel/task/osTasks.c +++ b/src/kernel/task/osTasks.c @@ -1,21 +1,23 @@ -#include -#include #include <_stdio.h> -#include #include +#include +#include +#include +#include Task mainTask; Task printerTask; void printLoop() { - clearScreen(); while (1) { - Message* message = popMessage(&printerTask); + Message *message = popMessage(&printerTask); if (message == 0) { yield(); continue; } - putChars(message->data, message->size); + for (uint16_t i = 0; i < message->size; i++) { + writeChar(((char *)(message->data))[i]); + } free(message->data); free(message); } @@ -23,12 +25,10 @@ void initOSTasks() { setRunningTask(&mainTask); - createTask(&printerTask, (uint32_t) printLoop, 0x0, 0x0); + createTask(&printerTask, (uint32_t)printLoop, 0x0, 0x0); schedule(&printerTask); // more tasks . . . yields(); } -inline Task* getPrinterTask() { - return &printerTask; -} \ No newline at end of file +inline Task *getPrinterTask() { return &printerTask; } diff --git a/src/kernel/util/tree-os.c b/src/kernel/util/tree-os.c index 8c0d252..94327b4 100644 --- a/src/kernel/util/tree-os.c +++ b/src/kernel/util/tree-os.c @@ -2,18 +2,17 @@ #include #include -#define LOGO \ - "\ +const char *logo = "\ _______ \n\ |__ __| \n\ | |_ __ ___ ___ ___ ___ \n\ | | '__/ _ \\/ _ \\ / _ \\/ __| \n\ | | | | __/ __/ | (_) \\__ \\ \n\ - |_|_| \\___|\\___| \\___/|___/ \n\n" + |_|_| \\___|\\___| \\___/|___/ \n\n"; void drawLogo() { - printf(LOGO); - printf("TREE-OS v. alpha 0.1 (IN DEVELOPMENT)\n\n"); - yields(); - setTextStyle(0x0F); + printf(logo); + printf("TREE-OS v. alpha 0.1 (IN DEVELOPMENT)\n\n"); + yields(); + setTextStyle(0x0F); }