Continuing terminal I/O

- Made stdio.h work
- Moved printFiletable back into kernel.c
- TODO: Add stdio.h to all files that should use it (probably next commmit)
- Added some more global addresses. There's a memory layout, but i'm not using it
- Got cursor to be drawn
- Calc program now gets user input
This commit is contained in:
2024-05-28 17:08:49 +02:00
parent 536f6b2320
commit ff07696dc6
17 changed files with 185 additions and 203 deletions
+33 -2
View File
@@ -11,8 +11,39 @@ void print(const char* s) {
write(STDOUT, s, strlen(s));
}
void read(char* s) {
print("\x1b[1M[ ERROR: Read() Syscall not found! ]\n");
void putc(char c) {
static char _c; // We need to pass in a static address. Idk why
_c = c;
write(STDOUT, &_c, 1);
}
char getc() {
char* character = (char*)0x3000;
*character = '\0';
while (*character == '\0') { __asm__ volatile ("hlt"); }
return *character;
}
// Reads until a newline
// TODO: Use Read() Syscall
void read(char* buffer) {
char c;
uint8_t len = 0;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (len < 1) continue; // Can we backspace?
len--;
putc(c);
} else {
buffer[len++] = c;
putc(c);
}
}
buffer[len++] = c; // Add newline
putc(c);
buffer[len] = '\0'; // Null terminate
}
#endif
-41
View File
@@ -3,7 +3,6 @@
#include <disk/disk.h>
#include <memory/addresses.h>
#include <screen/text.h>
#include <string.h>
typedef struct {
@@ -78,44 +77,4 @@ uint8_t writeFile(char* filename, void* buffer) { // TODO: Add length so we don'
return 0; // Success
}
void printFiletable(char* ft) {
while (*ft != '\0') {
char filename[15] = { 0 };
uint8_t offset = 0;
for (uint8_t i = 0; i < 14; i++) {
if (i == 9) {
filename[offset++] = '.';
continue;
}
if (*ft != '\0') { filename[offset++] = *ft; }
ft++;
}
ft++; // RESERVED
uint8_t sector = *ft++;
uint8_t size = *ft++;
if (sector < 10) putc('0');
if (sector == 0) putc('0');
else print(itoa(sector, 10));
print(": ");
print(filename);
for (uint8_t i = 14 - strlen(filename); i > 0; i--) {
putc(' ');
}
print(" | ");
if (size >> 1) {
print(itoa(size >> 1, 10));
if (size & 0x01) { print(".5"); }
print("kB\n");
} else {
print(itoa(size*512, 10));
print("B\n");
}
}
}
#endif
+6 -10
View File
@@ -2,20 +2,18 @@
#define ISRS_H
#include <interrupt/idt.h>
#include <screen/text.h>
#include <stdio.h>
// Exception handlers
__attribute__ ((interrupt))
void excp_divideErr(intFrame_t* iframe) { // ISR 0
fgColor = RED;
print("[ Division Error ]\n");
print("\x1b[1M[ Division Error ]\n\x1b[8M");
iframe->eip++;
}
__attribute__ ((interrupt))
void excp_doubleFault(intFrame_t* iframe, uint32_t errCode) { // ISR 8
fgColor = RED;
print("[ Double Fault ]\n"); // Error code should be 0 (TODO: Maybe check if it's != 0?)
print("\x1b[1M[ Double Fault ]\n\x1b[8M"); // Error code should be 0 (TODO: Maybe check if it's != 0?)
__asm__ volatile ("cli; hlt");
}
@@ -26,19 +24,17 @@ void excp_pageFault(intFrame_t* iframe, uint32_t errCode) { // ISR 14
// cr2 should contain invalid address
__asm__ volatile("movl %%cr2, %0" : "=r"(badAddress));
fgColor = RED;
print("[ Page Fault : 0x");
print("\x1b[1M[ Page Fault : 0x");
print(itoa(errCode, 16));
print(" ] - Tried to access 0x");
print(itoa(badAddress, 16));
print(".\n");
print(".\n\x1b[8M");
__asm__ volatile ("cli; hlt");
}
__attribute__ ((interrupt))
void excp_reserved(intFrame_t* iframe) { // ISR 15
fgColor = RED;
print("[ Unknown Exception ]\n");
print("\x1b[1M[ Unknown Exception ]\n\x1b[8M");
__asm__ volatile ("cli; hlt");
}
-1
View File
@@ -2,7 +2,6 @@
#define IDT_H
#include <stdint.h>
#include <screen/text.h>
#define TRAP_GATE_FLAGS 0x8f // P = 1, DPL = 00, S = 0, Type = 1111 (32-bit Trap gate)
#define INT_GATE_FLAGS 0x8e // P = 1, DPL = 00, S = 0, Type = 1110 (32-bit Int gate)
+13 -7
View File
@@ -1,24 +1,30 @@
#ifndef TIMER_H
#define TIMER_H
#include <stdint.h>
#include <interrupt/idt.h>
#include <interrupt/pic.h>
#include <screen/text.h>
#include <terminal/terminal.h>
#include <memory/addresses.h>
#include <stdint.h>
#define CHANNEL_0 0x40 // Offset it by 1 or 2 for ch1 and ch2
#define COMMAND 0x43
#define INPUT_CLK 1193182
static uint32_t* timerTicks = (uint32_t*)0x1000;
static uint32_t* sleepTimerTicks = (uint32_t*)SLEEP_TIMER;
__attribute__ ((interrupt))
void timerHandler(intFrame_t* iframe) {
*timerTicks += 1;
static uint32_t cursorTicks = 0;
cursorTicks++;
// TODO: Improve the cursor rendering
if (*timerTicks % 500 == 0) putcAt(' ', cursor.x, cursor.y);
else if (*timerTicks % 250 == 0) putcAt(127, cursor.x, cursor.y);
if (terminal->showCursor) {
if (cursorTicks % 500 == 0) putcAt(127, terminal->x, terminal->y);
else if (cursorTicks % 250 == 0) putcAt(' ', terminal->x, terminal->y);
}
if (*sleepTimerTicks > 0) { --(*sleepTimerTicks); }
sendPicEOI(0);
}
+7 -2
View File
@@ -19,7 +19,7 @@ typedef struct {
uint16_t year;
} __attribute__ ((packed)) datetime_t;
static datetime_t* datetime = (datetime_t*)0x2000; // TODO: Put this at a constant address in memory
static datetime_t* datetime = (datetime_t*)DATETIME_LOC;
bool cmosUpdateProgress() {
outb(CMOS_ADDRESS, 0x8a); // Read status reg A, disable NMI
@@ -128,7 +128,12 @@ __attribute__ ((interrupt)) void rtcHandler(intFrame_t* iframe) {
strcat(buf, itoa(datetime->second, 10));
// The clock is a little offset down (about 0.4 of a character) It does look nice though!
printAt(buf, gfxMode->xRes - strlen(buf) - 1, 0);
uint8_t len = strlen(buf);
uint32_t x = gfxMode->xRes - len - 1;
char* str = &buf[0];
for (uint8_t i = 0; i != len; i++, x++) {
putcAt(str[i], x, 0);
}
}
}
+3 -4
View File
@@ -6,17 +6,16 @@
#include <terminal/terminal.h>
#include <keyboard/keyboard.h>
#include <memory/malloc.h>
#include <memory/addresses.h>
#include <syscall.h>
#include <stdint.h>
// Syscall(0) - Sleep()
// Args: ebx - number of miliseconds
void sys_sleep() {
uint32_t ms;
__asm__ volatile ("movl %%ebx, %0" : "=r"(ms));
__asm__ volatile ("movl %%ebx, %0" : "=r"(*sleepTimerTicks));
uint32_t requiredTicks = *timerTicks + ms;
while (*timerTicks != requiredTicks) { // Wait...
while (*sleepTimerTicks > 0) { // Wait...
__asm__ volatile ("sti;hlt;cli;");
}
}
+8 -37
View File
@@ -3,66 +3,37 @@
#include <interrupt/idt.h>
#include <interrupt/pic.h>
#include <screen/text.h>
#include <keyboard/keys.h>
#include <memory/addresses.h>
#include <stdio.h>
#include <stdbool.h>
#define MAX_INPUT_LENGTH 256
typedef struct {
char character;
uint8_t shift;
bool shift;
} keyboardState_t;
// TODO: Put at set address
keyboardState_t keyboard;
keyboardState_t* keyboard = (keyboardState_t*)KEYBOARD_STATE_LOC;
__attribute__ ((interrupt))
void keyboardHandler(intFrame_t* iframe) { // TODO: Make this better and be able to read multi-byte scancode
uint8_t scancode = inb(0x60);
char c = US_Keyboard[keyboard.shift][scancode];
char c = US_Keyboard[keyboard->shift][scancode];
if (scancode == LEFT_SHIFT_PRESS || scancode == RIGHT_SHIFT_PRESS) {
keyboard.shift = 1;
keyboard->shift = true;
} else if (scancode == LEFT_SHIFT_RELEASE || scancode == RIGHT_SHIFT_RELEASE) {
keyboard.shift = 0;
keyboard->shift = false;
}
if (scancode & 0x80) { // Keyup
} else if (c != '\0') {
keyboard.character = c;
keyboard->character = c;
}
sendPicEOI(1);
}
char getc() {
keyboard.character = '\0';
while (keyboard.character == '\0') { __asm__ volatile ("hlt"); }
return keyboard.character;
}
// Reads until a newline
void read(char* buffer) {
char c;
uint8_t len = 0;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (len < 1) continue; // Can we backspace?
len--;
putc(c);
} else if (len + 1 != MAX_INPUT_LENGTH) { // Check if we can add a new character (this method avoids overflow issues)
buffer[len++] = c;
putc(c);
}
}
buffer[len++] = c; // Add newline
putc(c);
buffer[len] = '\0'; // Null terminate
}
void setupKeyboard() {
idtSetGate(0x21, keyboardHandler, INT_GATE_FLAGS);
unsetIrqMask(1);
+23 -9
View File
@@ -1,13 +1,32 @@
#ifndef ADDRESSES_H
#define ADDRESSES_H
// TODO: Don't let these be just random numbers?
/* TODO: Follow this layout and/or change it to make more sense
* Memory layout
* 0x0000-0x0fff - Probably shouldn't mess with this? idk
* 0x1000-0x17ff - Free
* 0x1800-0x1fff - Global Variables
* 0x2000-0x2fff - Terminal area
* 0x3000-0x3fff - Keyboard area
* 0x4000-0x5fff - Filesytem area
* 0x6000-0x7fff - Font area
* 0x8000-0x8fff - Free
* 0x9000-0x9fff - VBE info area
*/
// Locations
#define KERNEL_LOC 0x100000
#define FILETABLE_LOC 0x7000
#define TERMINAL_INFO_LOC 0x2000
#define MODE_INFO_BLOCK_LOC 0x9000
#define DATETIME_LOC 0x2000
#define TERMINAL_INFO_LOC 0x8000
#define KEYBOARD_STATE_LOC 0x3000
#define FILETABLE_LOC 0x4000
#define MODE_INFO_BLOCK_LOC 0x9000 // 512 Bytes
// Variables
#define CURRENT_PAGE_DIR_ADDR 0x1800
#define PHYS_MEM_MAX_BLOCKS 0x1804
#define PHYS_MEM_USED_BLOCKS 0x1808
#define SLEEP_TIMER 0x180C
// Font
#define FONT_LOC 0x6000
@@ -18,10 +37,5 @@
#define MEMMAP_AREA 0x30000
#define SMAP_ENTRY_COUNT 0x8500
#define SMAP_ENTRIES 0x8504
#define PHYS_MEM_MAX_BLOCKS 0x1804
#define PHYS_MEM_USED_BLOCKS 0x1808
// Virtual memory manager
#define CURRENT_PAGE_DIR_ADDR 0x1800
#endif
+14
View File
@@ -76,4 +76,18 @@ typedef struct modeInfoBlock_s {
static modeInfoBlock_t* gfxMode = (modeInfoBlock_t*)MODE_INFO_BLOCK_LOC;
uint32_t convertColor(uint32_t color) {
if (gfxMode->bpp == 8) { // Uses VGA Color pallete, idk how to support that
return color & 0xff;
}
// TODO: !! Important !! - Is should be divided by 255.0 (for floats), but using '-mgeneral-regs-only' removes fp-registers and
// wants to use some dumb functions for converting fp numbers in the stdlib (that we don't have)
uint8_t red = ((color >> 16) & 0xff) * (((1 << gfxMode->linearRedMaskSize) - 1) / 255);
uint8_t green = ((color >> 8 ) & 0xff) * (((1 << gfxMode->linearGreenMaskSize) - 1) / 255);
uint8_t blue = ((color ) & 0xff) * (((1 << gfxMode->linearBlueMaskSize) - 1) / 255);
return (red << gfxMode->redFieldPos) | (green << gfxMode->greenFieldPos) | (blue << gfxMode->blueFieldPos);
}
#endif
+15 -73
View File
@@ -1,39 +1,29 @@
#ifndef SCREEN_H
#define SCREEN_H
#include <terminal/terminal.h>
#include <memory/addresses.h>
#include <screen/gfxmode.h>
#include <disk/filesys.h>
#include <stdint.h>
#include <string.h>
struct cursor_s {
uint32_t x;
uint32_t y;
};
static char* font = (char*)FONT_LOC;
static struct cursor_s cursor = { 0, 0 };
uint32_t bgColor = BG_COLOR;
uint32_t fgColor = FG_COLOR;
uint32_t convertColor(uint32_t color) {
if (gfxMode->bpp == 8) { // Uses VGA Color pallete, idk how to support that
return color & 0xff;
}
// TODO: Make this check if it's a valid font
// TODO: Make it take a fileEntry_t?
uint8_t loadFont(char* filename) {
if (readFile(filename, (void*)FONT_LOC) != 0) { return 1; } // File not found
if (*(uint8_t*)FONT_WIDTH == 0 || *(uint8_t*)FONT_HEIGHT == 0) { return 1; } // Not a font
// TODO: !! Important !! - Is should be divided by 255.0 (for floats), but using '-mgeneral-regs-only' removes fp-registers and
// wants to use some dumb functions for converting fp numbers in the stdlib (that we don't have)
uint8_t red = ((color >> 16) & 0xff) * (((1 << gfxMode->linearRedMaskSize) - 1) / 255);
uint8_t green = ((color >> 8 ) & 0xff) * (((1 << gfxMode->linearGreenMaskSize) - 1) / 255);
uint8_t blue = ((color ) & 0xff) * (((1 << gfxMode->linearBlueMaskSize) - 1) / 255);
return (red << gfxMode->redFieldPos) | (green << gfxMode->greenFieldPos) | (blue << gfxMode->blueFieldPos);
return 0; // Success
}
// TODO: Reimplement larger text
void putcAt(unsigned char c, uint32_t x, uint32_t y) {
void putcAt(char c, uint32_t x, uint32_t y) {
// Font info
char* font = (char*)FONT_LOC;
uint8_t charWidth = *(uint8_t*)FONT_WIDTH;
uint8_t charHeight = *(uint8_t*)FONT_HEIGHT;
// uint8_t bitmapRowLen = (charWidth - 1) / 8 + 1; // TODO: Larger font support
@@ -62,21 +52,12 @@ void putcAt(unsigned char c, uint32_t x, uint32_t y) {
}
}
// TODO: Probably get rid of this, idk (use terminal control codes)
void printAt(const char* str, uint32_t x, uint32_t y) {
while (*str != '\0') {
putcAt(*str++, x, y);
x++;
}
}
// TODO: Use terminal.x/y instead of cursor.x/y
void scroll() { // TODO: Make this faster (still kinda slow, but memcpy32() helped alot)
uint8_t charHeight = *(uint8_t*)FONT_HEIGHT;
if ((cursor.y+1) * charHeight < gfxMode->yRes) { return; } // Check if we should scroll
if (terminal->y+1 < terminal->height) { return; } // Check if we should scroll
uint32_t bytesPerLine = charHeight * gfxMode->bytesPerScanline;
uint32_t lines = gfxMode->yRes / charHeight;
uint32_t lines = terminal->height;
uint32_t vidmem = gfxMode->physicalBasePtr;
for (uint32_t i = 1; i < lines; i++) {
@@ -95,46 +76,7 @@ void scroll() { // TODO: Make this faster (still kinda slow, but memcpy32() help
lastLine += bytesPerPx;
}
cursor.y--;
}
// DEPRICATED - TODO: Remove this from everywhere so i can delete it
void putc(char c) {
uint8_t charWidth = *(uint8_t*)FONT_WIDTH;
if (c == '\n') {
putcAt(' ', cursor.x, cursor.y); // Make sure we don't leave an extra cursor
cursor.y++;
cursor.x = 0;
} else if (c == '\b') {
putcAt(' ', cursor.x, cursor.y); // Make sure we don't leave an extra cursor
if (cursor.x == 0) {
cursor.x = gfxMode->xRes / charWidth - 1; // Subtract one so we don't overflow to the line start
cursor.y--; // y could be 0
} else {
cursor.x--;
}
putcAt(' ', cursor.x, cursor.y);
} else {
putcAt(c, cursor.x, cursor.y);
cursor.x++;
if (cursor.x >= gfxMode->xRes / charWidth) {
cursor.y++;
cursor.x = 0;
}
}
scroll();
}
// DEPRICATED - TODO: Remove this everywhere it's used and delete it's definition here
void print(const char* str) {
while (*str != '\0') {
putc(*str++);
}
terminal->y--;
}
// TODO: Make this comply with the new terminal.h thing!
@@ -142,8 +84,8 @@ void clear() {
uint8_t* vidmem = (uint8_t*)gfxMode->physicalBasePtr;
uint8_t bytesPerPx = (gfxMode->bpp+1) / 8;
cursor.x = 0;
cursor.y = 0;
terminal->x = 0;
terminal->y = 0;
uint32_t color = convertColor(bgColor);
+6 -6
View File
@@ -1,7 +1,6 @@
#ifndef TERMINAL_H
#define TERMINAL_H
#include <screen/text.h>
#include <screen/gfxmode.h>
#include <memory/addresses.h>
#include <stdint.h>
@@ -14,11 +13,14 @@ typedef struct {
uint32_t fg; // Printed character foreground
uint32_t width; // Width in characters
uint32_t height; // Height in characters
char cursorChar; // Character to use as a cursor
bool showCursor; // Should we display the cursor
} terminal_t;
terminal_t* terminal = (terminal_t*)TERMINAL_INFO_LOC;
#include <screen/text.h> // This needs to be here because it uses 'terminal' and otherwise it would be undeclared
int32_t terminalWrite(const void* buf, uint32_t len) {
terminal->width = gfxMode->xRes / *(uint8_t*)FONT_WIDTH;
terminal->height = gfxMode->yRes / *(uint8_t*)FONT_HEIGHT;
@@ -29,11 +31,11 @@ int32_t terminalWrite(const void* buf, uint32_t len) {
// Do we need to check for NULL? Can we just skip it/ignore it?
for (i = 0; i < len && str[i] != '\0'; i++) {
if (str[i] == '\n') {
putcAt(' ', terminal->x, terminal->y); // Remove extra cursor
putcAt(' ', terminal->x, terminal->y);
terminal->y++;
terminal->x = 0;
} else if (str[i] == '\b') {
putcAt(' ', terminal->x, terminal->y); // Remove extra cursor
putcAt(' ', terminal->x, terminal->y);
if (terminal->x == 0) {
terminal->x = terminal->width - 1;
terminal->y--; // Y could be 0?
@@ -42,11 +44,9 @@ int32_t terminalWrite(const void* buf, uint32_t len) {
}
putcAt(' ', terminal->x, terminal->y);
} else if (str[i] == '\x1b') { // Escape sequence
if (str[++i] != '[') { // TODO: Suppord more types of escape sequences
if (str[++i] != '[') { // TODO: Support more types of escape sequences
continue;
}
putcAt(str[++i], terminal->x++, terminal->y);
} else {
putcAt(str[i], terminal->x, terminal->y);
terminal->x++;