mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
IMPLEMENTED printf()!!!!!!!
It's kinda bad :/
This commit is contained in:
+53
-4
@@ -7,6 +7,14 @@
|
||||
// TODO: Make a proper file descriptor table
|
||||
#define STDOUT 1
|
||||
|
||||
/* --- Output --- */
|
||||
void putc(char c) {
|
||||
// write(STDOUT, &c, 1); TODO: This doesn't work and we need to use a static variable
|
||||
static char _c;
|
||||
_c = c;
|
||||
write(STDOUT, &_c, 1);
|
||||
}
|
||||
|
||||
void print(const char* s) {
|
||||
write(STDOUT, s, strlen(s));
|
||||
}
|
||||
@@ -15,12 +23,53 @@ void clear() {
|
||||
print("\x1b[J");
|
||||
}
|
||||
|
||||
void putc(char c) {
|
||||
static char _c; // We need to pass in a static address. Idk why
|
||||
_c = c;
|
||||
write(STDOUT, &_c, 1);
|
||||
// TODO: Dynamically allocate memory and print once instead of calling putc for every character :/
|
||||
// TODO: Support negative numbers
|
||||
// TODO: Printing individual characters doesn't let escape sequences work :(
|
||||
int printf(const char* fmt, ...) {
|
||||
char* argPtr = (char*)&fmt + sizeof(char*); // First arg after fmt
|
||||
uint8_t state = 0;
|
||||
|
||||
for (uint32_t i = 0; fmt[i] != '\0'; i++) {
|
||||
char c = fmt[i];
|
||||
|
||||
if (state == 0) {
|
||||
if (c == '%') state = '%';
|
||||
else putc(c);
|
||||
} else if (state == '%') {
|
||||
switch (c) {
|
||||
case 'd': // Decimal number
|
||||
print(itoa(*(uint32_t*)argPtr, 10));
|
||||
argPtr += sizeof(unsigned int);
|
||||
break;
|
||||
case 'x': // Hex number
|
||||
print(itoa(*(uint32_t*)argPtr, 16));
|
||||
argPtr += sizeof(unsigned int);
|
||||
break;
|
||||
case 's': // String
|
||||
print(*(char**)argPtr);
|
||||
argPtr += sizeof(char*);
|
||||
break;
|
||||
case 'c': // Char
|
||||
putc(*(char*)argPtr);
|
||||
argPtr += sizeof(char);
|
||||
break;
|
||||
case '%': // Just print '%'
|
||||
putc('%');
|
||||
break;
|
||||
default:
|
||||
putc('%');
|
||||
putc(c);
|
||||
break;
|
||||
}
|
||||
state = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --- Input --- */
|
||||
char getc() {
|
||||
char* character = (char*)0x2100;
|
||||
*character = '\0';
|
||||
|
||||
+4
-7
@@ -6,19 +6,16 @@
|
||||
// Source: http://www.strudel.org.uk/itoa/
|
||||
char* itoa(uint32_t val, uint8_t base) {
|
||||
static char buf[32] = { 0 };
|
||||
|
||||
if (base == 0 || base > 16) {
|
||||
// TODO: Add errors!!
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (base == 0 || base > 16) { return NULL; }
|
||||
|
||||
if (val == 0) {
|
||||
buf[0] = '0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
int i = 30;
|
||||
for (; val && i; i--, val /= base) {
|
||||
int i;
|
||||
for (i = 30; val && i; i--, val /= base) {
|
||||
buf[i] = "0123456789abcdef"[val % base];
|
||||
}
|
||||
|
||||
|
||||
@@ -7,13 +7,13 @@
|
||||
// Exception handlers
|
||||
__attribute__ ((interrupt))
|
||||
void excp_divideErr(intFrame_t* iframe) { // ISR 0
|
||||
print("\x1b[1M[ Division Error ]\n\x1b[8M");
|
||||
printf("\x1b[1M[ Division Error ]\n\x1b[8M");
|
||||
iframe->eip++;
|
||||
}
|
||||
|
||||
__attribute__ ((interrupt))
|
||||
void excp_doubleFault(intFrame_t* iframe, uint32_t errCode) { // ISR 8
|
||||
print("\x1b[1M[ Double Fault ]\n\x1b[8M"); // Error code should be 0 (TODO: Maybe check if it's != 0?)
|
||||
printf("\x1b[1M[ Double Fault ]\n\x1b[8M"); // Error code should be 0 (TODO: Maybe check if it's != 0?)
|
||||
__asm__ volatile ("cli; hlt");
|
||||
}
|
||||
|
||||
@@ -24,11 +24,7 @@ void excp_pageFault(intFrame_t* iframe, uint32_t errCode) { // ISR 14
|
||||
// cr2 should contain invalid address
|
||||
__asm__ volatile("movl %%cr2, %0" : "=r"(badAddress));
|
||||
|
||||
print("\x1b[1M[ Page Fault : 0x");
|
||||
print(itoa(errCode, 16));
|
||||
print(" ] - Tried to access 0x");
|
||||
print(itoa(badAddress, 16));
|
||||
print(".\n\x1b[8M");
|
||||
printf("\x1b[1M[ Page Fault : 0x%x ] - Tried to access 0x%x.\n\x1b[8M", errCode, badAddress);
|
||||
__asm__ volatile ("cli; hlt");
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ void sys_write() {
|
||||
int32_t result = terminalWrite(buf, len); // Call a terminal print function
|
||||
__asm__ volatile ("movl %0, %%eax" : : "r"(result));
|
||||
} else { // Not implemented
|
||||
__asm__ volatile ("movl $1, %eax");
|
||||
__asm__ volatile ("movl $-1, %eax");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user