From: Duje Mihanović Date: Wed, 29 Jun 2022 09:01:23 +0000 (+0200) Subject: kernel: tty: Use recursion in byte/word printing X-Git-Url: http://git.dujemihanovic.xyz/%22http:/www.sics.se/static/%7B%7B?a=commitdiff_plain;h=1134d10715ddb0d9f1d36275b7c909189424035c;p=nameless-os.git kernel: tty: Use recursion in byte/word printing --- diff --git a/kernel/arch/x86/irq/interrupt.c b/kernel/arch/x86/irq/interrupt.c index f7cbcdc..5c448fe 100644 --- a/kernel/arch/x86/irq/interrupt.c +++ b/kernel/arch/x86/irq/interrupt.c @@ -12,13 +12,13 @@ void int_handler(struct interrupt_frame *frame) int interrupt = frame->interrupt; if (int_handler_table[interrupt] == NULL) { kprint("WARNING: Unhandled interrupt ", 0); - kprintb(interrupt); + kprintb(interrupt, 1); kprint(" occurred!\n", 0); } else { int ret = (*int_handler_table[interrupt])(frame); if (ret) { kprint("WARNING: Error while handling interrupt ", 0); - kprintb(interrupt); + kprintb(interrupt, 1); kprint("!\n", 0); } } diff --git a/kernel/arch/x86/tty/tty.c b/kernel/arch/x86/tty/tty.c index c55cdd4..ca3212a 100644 --- a/kernel/arch/x86/tty/tty.c +++ b/kernel/arch/x86/tty/tty.c @@ -92,88 +92,48 @@ void kprintc(const char character, uint8_t color) } -void kprintb(uint8_t byte) +void kprintnibble(uint8_t nibble) { - uint8_t temp; - temp = byte >> 4; - kprint("0x", VGA_COLOR_LIGHT_GRAY); - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = byte & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); + nibble &= 0xf; + if (nibble < 0xa) + kprintc('0' + nibble, 0); + else kprintc('a' - 0xa + nibble, 0); } -void kprintw(uint16_t word) +void kprintb(uint8_t byte, int print_delm) { - uint8_t temp; - temp = word >> 12; - kprint("0x", VGA_COLOR_LIGHT_GRAY); - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (word >> 8) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (word >> 4) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = word & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); + if (print_delm) + kprint("0x", 0); + + kprintnibble(byte >> 4); + kprintnibble(byte); } -void kprintd(uint32_t dword) +void kprintw(uint16_t word, int print_delm) { - uint8_t temp; - temp = dword >> 28; - kprint("0x", VGA_COLOR_LIGHT_GRAY); - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 24) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 20) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 16) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 12) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 8) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (dword >> 4) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = dword & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); + if (print_delm) + kprint("0x", 0); + + kprintb(word >> 8, 0); + kprintb(word, 0); } -void kprintq(uint64_t qword) +void kprintd(uint32_t dword, int print_delm) { - uint8_t temp; - temp = qword >> 60; - kprint("0x", VGA_COLOR_LIGHT_GRAY); - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 56) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 52) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 48) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 44) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 40) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 36) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 32) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 28) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 24) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 20) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 16) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 12) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 8) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = (qword >> 4) & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); - temp = qword & 0xF; - kprintc(hex_chars[temp], VGA_COLOR_LIGHT_GRAY); + if (print_delm) + kprint("0x", 0); + + kprintw(dword >> 16, 0); + kprintw(dword, 0); +} + +void kprintq(uint64_t qword, int print_delm) +{ + if (print_delm) + kprint("0x", 0); + + kprintd(qword >> 32, 0); + kprintd(qword, 0); } int kprintdec(uint32_t num, uint8_t color) diff --git a/kernel/include/tty.h b/kernel/include/tty.h index 3e1aab0..87aad81 100644 --- a/kernel/include/tty.h +++ b/kernel/include/tty.h @@ -23,9 +23,9 @@ extern void screen_clear(void); extern void kprint(const char *string, uint8_t color); extern void kprintc(const char character, uint8_t color); -extern void kprintb(const uint8_t byte); -extern void kprintw(const uint16_t word); -extern void kprintd(const uint32_t dword); -extern void kprintq(const uint64_t qword); +extern void kprintb(uint8_t byte, int print_delm); +extern void kprintw(uint16_t word, int print_delm); +extern void kprintd(uint32_t dword, int print_delm); +extern void kprintq(uint64_t qword, int print_delm); extern int kprintdec(uint32_t num, uint8_t color); #endif diff --git a/kernel/kernel.c b/kernel/kernel.c index adfdd3f..cd3e228 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -23,13 +23,13 @@ void print_e820(struct e820_map *mmap, int mmap_size) kprint(" Base | Length | Type | Attrib |\n", 0); kprint("------------------------------------------------------------\n", 0); for (int i=0; ieax); + kprintd(frame->eax, 1); kprintc(' ', 0); kprint("EBX: ", 0); - kprintd(frame->ebx); + kprintd(frame->ebx, 1); kprintc(' ', 0); kprint("ECX: ", 0); - kprintd(frame->ecx); + kprintd(frame->ecx, 1); kprintc(' ', 0); kprint("EDX: ", 0); - kprintd(frame->edx); + kprintd(frame->edx, 1); kprintc('\n', 0); kprint("EBP: ", 0); - kprintd(frame->ebp); + kprintd(frame->ebp, 1); kprintc(' ', 0); kprint("ESP: ", 0); - kprintd(frame->esp); + kprintd(frame->esp, 1); kprintc('\n', 0); kprint("ESI: ", 0); - kprintd(frame->esi); + kprintd(frame->esi, 1); kprintc(' ', 0); kprint("EDI: ", 0); - kprintd(frame->edi); + kprintd(frame->edi, 1); kprintc('\n', 0); kprint("EIP: ", 0); - kprintd(frame->eip); + kprintd(frame->eip, 1); kprintc(' ', 0); kprint("EFLAGS: ", 0); - kprintd(frame->eflags); + kprintd(frame->eflags, 1); kprintc('\n', 0); PANIC("double fault"); }