diff --git a/exception_handler.c b/exception_handler.c index a171e1c..d130c13 100644 --- a/exception_handler.c +++ b/exception_handler.c @@ -2,11 +2,11 @@ #include "vga.h" // Need GCC > 6 -__attribute__ ((interrupt)) -void print_handler(struct interrupt_frame *frame, ulong error_code){ +__attribute__((interrupt)) void print_handler(struct interrupt_frame *frame, ulong error_code) +{ - printStringDetails("EXCEPTION", RED, BLACK, 0, 20); - printInt(error_code, RED, BLACK, 11, 20); - (void) frame; - (void) error_code; + printStringDetails("EXCEPTION", RED, BLACK, 0, VGA_HEIGHT - 1); + printIntDetails(error_code, RED, BLACK, 11, VGA_HEIGHT - 1); + (void)frame; + (void)error_code; } diff --git a/irq_handler.c b/irq_handler.c index f881c17..5c24677 100644 --- a/irq_handler.c +++ b/irq_handler.c @@ -1,8 +1,8 @@ #include "interrupt.h" -#include "vga.h" #include "io.h" -#include "pic.h" #include "irq.h" +#include "pic.h" +#include "vga.h" // Need GCC > 6 __attribute__((interrupt)) void keyboard_handler(struct interrupt_frame *frame) @@ -11,9 +11,8 @@ __attribute__((interrupt)) void keyboard_handler(struct interrupt_frame *frame) char c = 0; if (inb(0x60) != c) { c = inb(0x60); - if (c > 0){ - printChar(c); - printChar('\n'); + if (c > 0) { + printInt(c); } } (void)frame; @@ -23,6 +22,6 @@ __attribute__((interrupt)) void timer_handler(struct interrupt_frame *frame) { static int timeCnt = 0; EOIIrq(IRQ_TIMER); - printInt(timeCnt++, RED, BLACK, 20, VGA_HEIGHT -1 ); + printIntDetails(timeCnt++, RED, BLACK, 20, VGA_HEIGHT - 1); (void)frame; } diff --git a/main.c b/main.c index c9e52c7..12f5841 100644 --- a/main.c +++ b/main.c @@ -31,7 +31,7 @@ void kmain() asm volatile("sti\n"); int count = 0; while (1) { - printInt(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1); + printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1); } printString("exiting\n"); } diff --git a/vga.c b/vga.c index b8714e6..e6e6d53 100644 --- a/vga.c +++ b/vga.c @@ -26,7 +26,26 @@ void clearScreen(uint bgColor) } } -void printInt(int integer, uint color, uint bgColor, int startX, int startY) +void printInt(int integer) +{ + char num[sizeof(int) * + 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) = + // 8^(sizeof(int)*8/3) ~ 10^(sizeof(int)*8/3) + int i = 0, k = 0; + if (integer < 0) { + printChar('-'); + } + while (integer != 0) { + int digit = integer % 10; + num[i++] = (digit > 0) ? digit : -digit; + integer = integer / 10; + } + for (k = i - 1; k >= 0; k--) { + printChar(num[k] + '0'); + } +} + +void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY) { char num[sizeof(int) * 3]; // int max is 2^(sizeof(int)*8) which is (2^3)^(sizeof(int)*8/3) = @@ -48,8 +67,8 @@ void printInt(int integer, uint color, uint bgColor, int startX, int startY) void printCharDetails(const char str, uint color, uint bgColor, int startX, int startY) { - volatile short *vga = (short *)VGA_ADDR; - long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; + volatile short *vga = (short *)VGA_ADDR; + long int colorAttr = (bgColor << 4 | (color & 0x0f)) << 8; vga[VGA_WIDTH * startY + startX] = colorAttr | str; } @@ -57,20 +76,21 @@ void vgaScrollUp(void) { long int colorAttr = vgaBgColor << 12; volatile short *vga = (short *)VGA_ADDR; - for (int i = 1; i < VGA_HEIGHT - 2; i++) { // last line is status line. Do not scroll it + for (int i = 1; i < VGA_HEIGHT - 2; + i++) { // last line is status line. Do not scroll it memcpy((void *)&vga[VGA_WIDTH * (i - 1)], (void *)&vga[VGA_WIDTH * i], VGA_WIDTH * sizeof(short)); } - for(int i = 0; i < VGA_WIDTH; i++){ + for (int i = 0; i < VGA_WIDTH; i++) { vga[(VGA_HEIGHT - 1) * VGA_WIDTH + i] = colorAttr; } } -void printString(const char *str) { +void printString(const char *str) +{ while (*str) { printChar(*(str++)); } - } void printChar(const char str) @@ -87,7 +107,7 @@ void printChar(const char str) vgaScrollUp(); line--; } - if(str == '\n') + if (str == '\n') return; printCharDetails(str, vgaColor, vgaBgColor, col++, line); } diff --git a/vga.h b/vga.h index 70dbde9..a488719 100644 --- a/vga.h +++ b/vga.h @@ -18,7 +18,8 @@ int initVGA(uint bgColor, uint color); void clearScreen(uint bgColor); -void printInt(int integer, uint color, uint bgColor, int startX, int startY); +void printInt(int integer); +void printIntDetails(int integer, uint color, uint bgColor, int startX, int startY); void printCharDetails(char str, uint color, uint bgColor, int startX, int startY); void printStringDetails(const char *str, uint color, uint bgColor, int startX, int startY); void printString(const char *str);