forked from PoliEcho-holding-org/kern
Made printf() good!
This commit is contained in:
+45
-22
@@ -3,69 +3,92 @@
|
|||||||
|
|
||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
// TODO: Make a proper file descriptor table
|
// TODO: Make a proper file descriptor table
|
||||||
#define STDOUT 1
|
#define stdin 0
|
||||||
|
#define stdout 1
|
||||||
|
#define stderr 2
|
||||||
|
|
||||||
/* --- Output --- */
|
/* --- Output --- */
|
||||||
void putc(char c) {
|
void putc(char c) {
|
||||||
// write(STDOUT, &c, 1); TODO: This doesn't work and we need to use a static variable
|
// write(stdout, &c, 1); TODO: This doesn't work and we need to use a static variable
|
||||||
static char _c;
|
static char _c;
|
||||||
_c = c;
|
_c = c;
|
||||||
write(STDOUT, &_c, 1);
|
write(stdout, &_c, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(const char* s) {
|
void print(const char* s) {
|
||||||
write(STDOUT, s, strlen(s));
|
write(stdout, s, strlen(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
print("\x1b[J");
|
print("\x1b[J");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Dynamically allocate memory and print once instead of calling putc for every character :/
|
|
||||||
// TODO: Support negative numbers
|
// TODO: Support negative numbers
|
||||||
// TODO: Printing individual characters doesn't let escape sequences work :(
|
|
||||||
int printf(const char* fmt, ...) {
|
int printf(const char* fmt, ...) {
|
||||||
uint32_t* argPtr = (uint32_t*)&fmt + 1; // First arg after fmt
|
uint32_t* argPtr = (uint32_t*)&fmt + 1; // First arg after fmt
|
||||||
uint8_t state = 0;
|
|
||||||
|
|
||||||
for (uint32_t i = 0; fmt[i] != '\0'; i++) {
|
// Printing buffer
|
||||||
char c = fmt[i];
|
static char* buffer;
|
||||||
|
static uint32_t len;
|
||||||
|
uint32_t max = 256;
|
||||||
|
|
||||||
|
buffer = malloc(max);
|
||||||
|
len = 0;
|
||||||
|
|
||||||
|
uint8_t state = 0;
|
||||||
|
for (uint32_t i = 0; fmt[i] != '\0'; i++) {
|
||||||
|
if (len > max) {
|
||||||
|
max *= 2;
|
||||||
|
// Reallocate larger buffer TODO: Use realloc();
|
||||||
|
char* temp = malloc(max);
|
||||||
|
strcpy(temp, buffer);
|
||||||
|
free(buffer);
|
||||||
|
buffer = temp;
|
||||||
|
}
|
||||||
|
buffer[len] = '\0';
|
||||||
|
|
||||||
|
char c = fmt[i];
|
||||||
if (state == 0) {
|
if (state == 0) {
|
||||||
if (c == '%') state = '%';
|
if (c == '%') state = '%';
|
||||||
else putc(c);
|
else buffer[len++] = c;
|
||||||
} else if (state == '%') {
|
} else if (state == '%') {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'd': // Decimal number
|
case 'd': // Decimal number
|
||||||
print(itoa(*(uint32_t*)argPtr, 10));
|
strcat(buffer, itoa(*(uint32_t*)argPtr++, 10));
|
||||||
argPtr++;
|
len += strlen(&buffer[len]); // Add the remaining length
|
||||||
break;
|
break;
|
||||||
case 'x': // Hex number
|
case 'x': // Hex number
|
||||||
print(itoa(*(uint32_t*)argPtr, 16));
|
strcat(buffer, itoa(*(uint32_t*)argPtr++, 16));
|
||||||
argPtr++;;
|
len += strlen(&buffer[len]);
|
||||||
break;
|
break;
|
||||||
case 's': // String
|
case 's': // String
|
||||||
print(*(char**)argPtr);
|
char* s = *(char**)argPtr++;
|
||||||
argPtr++;
|
|
||||||
|
while (*s != '\0') {
|
||||||
|
buffer[len++] = *s++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'c': // Char
|
case 'c': // Char
|
||||||
putc(*(char*)argPtr);
|
buffer[len++] = *(char*)argPtr;
|
||||||
argPtr++;
|
argPtr++;
|
||||||
break;
|
break;
|
||||||
|
default: // This looks insane
|
||||||
|
buffer[len++] = '%';
|
||||||
case '%': // Just print '%'
|
case '%': // Just print '%'
|
||||||
putc(c);
|
buffer[len++] = c;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
putc('%');
|
|
||||||
putc(c);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
state = 0;
|
state = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buffer[len] = '\0';
|
||||||
|
write(stdout, buffer, len);
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -2,17 +2,18 @@
|
|||||||
#define STDLIB_H
|
#define STDLIB_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <syscall.h>
|
||||||
|
|
||||||
void* malloc(const uint32_t size) { // Use Syscall(3) - Malloc()
|
void* malloc(const uint32_t size) {
|
||||||
void* ptr = 0;
|
void* ptr = 0;
|
||||||
__asm__ volatile ("int $0x80" : : "a"(3), "b"(size));
|
__asm__ volatile ("int $0x80" : : "a"(SYS_MALLOC), "b"(size));
|
||||||
__asm__ volatile ("movl %%eax, %0" : "=r"(ptr));
|
__asm__ volatile ("movl %%eax, %0" : "=r"(ptr));
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free(const void* ptr) { // Use Syscall(4) - Free()
|
void free(const void* ptr) {
|
||||||
__asm__ volatile ("int $0x80" : : "a"(4), "b"(ptr));
|
__asm__ volatile ("int $0x80" : : "a"(SYS_FREE), "b"(ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <interrupt/idt.h>
|
#include <interrupt/idt.h>
|
||||||
#include <screen/text.h>
|
#include <screen/text.h>
|
||||||
#include <screen/gfxmode.h>
|
#include <screen/gfxmode.h>
|
||||||
|
#include <terminal/terminal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -133,7 +134,7 @@ __attribute__ ((interrupt)) void rtcHandler(intFrame_t* iframe) {
|
|||||||
uint32_t x = gfxMode->xRes - len - 1;
|
uint32_t x = gfxMode->xRes - len - 1;
|
||||||
char* str = &buf[0];
|
char* str = &buf[0];
|
||||||
for (uint8_t i = 0; i != len; i++, x++) {
|
for (uint8_t i = 0; i != len; i++, x++) {
|
||||||
drawChar(str[i], x, 0, FG_COLOR, BG_COLOR);
|
drawChar(str[i], x, 0, terminal->fg, terminal->bg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@
|
|||||||
|
|
||||||
// Other addresses
|
// Other addresses
|
||||||
#define KERNEL_LOC 0x100000
|
#define KERNEL_LOC 0x100000
|
||||||
|
#define KERNEL_MALLOC_AREA 0x300000
|
||||||
#define MEMMAP_AREA 0x30000
|
#define MEMMAP_AREA 0x30000
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+38
-18
@@ -50,6 +50,26 @@ void main() {
|
|||||||
setupTimer();
|
setupTimer();
|
||||||
setupRtc();
|
setupRtc();
|
||||||
setupKeyboard();
|
setupKeyboard();
|
||||||
|
|
||||||
|
// Setup kernel malloc variables
|
||||||
|
mallocNode_t* kernelMallocListHead = 0;
|
||||||
|
uint32_t kernelMallocVirtualAddr = KERNEL_MALLOC_AREA;
|
||||||
|
uint32_t kernelMallocPhysicalAddr = (uint32_t)allocBlocks(1); // TODO: Kernel may use more than 4kB?
|
||||||
|
uint32_t kernelMallocPages = 1;
|
||||||
|
|
||||||
|
mapPage((void*)kernelMallocPhysicalAddr, (void*)kernelMallocVirtualAddr);
|
||||||
|
ptEntry_t* kernelMallocPage = getPage(kernelMallocVirtualAddr);
|
||||||
|
SET_ATTR(kernelMallocPage, PTE_READ_WRITE);
|
||||||
|
kernelMallocListHead = (mallocNode_t*)kernelMallocVirtualAddr;
|
||||||
|
|
||||||
|
kernelMallocListHead->size = PAGE_SIZE - sizeof(mallocNode_t);
|
||||||
|
kernelMallocListHead->free = true;
|
||||||
|
kernelMallocListHead->next = NULL;
|
||||||
|
|
||||||
|
mallocListHead = kernelMallocListHead;
|
||||||
|
mallocVirtualAddr = kernelMallocVirtualAddr;
|
||||||
|
mallocPhysicalAddr = kernelMallocPhysicalAddr;
|
||||||
|
mallocPages = kernelMallocPages;
|
||||||
|
|
||||||
// Screen setup
|
// Screen setup
|
||||||
loadFont("term16n.fnt");
|
loadFont("term16n.fnt");
|
||||||
@@ -61,7 +81,7 @@ void main() {
|
|||||||
// Run Interactive Shell Program
|
// Run Interactive Shell Program
|
||||||
// TODO: Make it in another file
|
// TODO: Make it in another file
|
||||||
while (1) {
|
while (1) {
|
||||||
print(PROMPT);
|
printf(PROMPT);
|
||||||
char input[256];
|
char input[256];
|
||||||
read(input);
|
read(input);
|
||||||
|
|
||||||
@@ -134,18 +154,18 @@ void main() {
|
|||||||
getc(); // Wait until we get a 'q'
|
getc(); // Wait until we get a 'q'
|
||||||
clear();
|
clear();
|
||||||
} else if (strcmp(input, "help") == 0) {
|
} else if (strcmp(input, "help") == 0) {
|
||||||
print("Available Commands:\n");
|
printf("Available Commands:\n");
|
||||||
print(" clear | Clears the screen\n");
|
printf(" clear | Clears the screen\n");
|
||||||
print(" echo | Prints a message to stdout\n");
|
printf(" echo | Prints a message to stdout\n");
|
||||||
print(" exit | Exits shell\n");
|
printf(" exit | Exits shell\n");
|
||||||
print(" gfx | Does a graphics test\n");
|
printf(" gfx | Does a graphics test\n");
|
||||||
print(" help | Prints this message\n");
|
printf(" help | Prints this message\n");
|
||||||
print(" ls | Lists all available files\n");
|
printf(" ls | Lists all available files\n");
|
||||||
print(" memmap | Prints the memory map and info\n");
|
printf(" memmap | Prints the memory map and info\n");
|
||||||
print(" reboot | Reboots the system\n");
|
printf(" reboot | Reboots the system\n");
|
||||||
print(" sleep | Sleeps for input number of seconds\n");
|
printf(" sleep | Sleeps for input number of seconds\n");
|
||||||
print(" soundtest | Plays a tune :)\n");
|
printf(" soundtest | Plays a tune :)\n");
|
||||||
print(" test | Performs tests\n");
|
printf(" test | Performs tests\n");
|
||||||
} else if (strcmp(input, "ls") == 0) {
|
} else if (strcmp(input, "ls") == 0) {
|
||||||
printFiletable(filetable);
|
printFiletable(filetable);
|
||||||
} else if (strcmp(input, "memmap") == 0) {
|
} else if (strcmp(input, "memmap") == 0) {
|
||||||
@@ -228,11 +248,11 @@ void main() {
|
|||||||
flushTlbEntry(virtualAddr);
|
flushTlbEntry(virtualAddr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mallocListHead = NULL;
|
mallocListHead = kernelMallocListHead;
|
||||||
mallocVirtualAddr = 0;
|
mallocVirtualAddr = kernelMallocVirtualAddr;
|
||||||
mallocPhysicalAddr = 0;
|
mallocPhysicalAddr = kernelMallocPhysicalAddr;
|
||||||
mallocPages = 0;
|
mallocPages = kernelMallocPages;
|
||||||
} else if (strcmp(file->filetype, "tab") == 0) {
|
} else if (strcmp(file->filetype, "tab") == 0) {
|
||||||
printFiletable(entryPoint);
|
printFiletable(entryPoint);
|
||||||
} else if (strcmp(file->filetype, "fnt") == 0) {
|
} else if (strcmp(file->filetype, "fnt") == 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user