From aea7368d461023ae629d08ee664f076843ec5646 Mon Sep 17 00:00:00 2001 From: Yann MAGNIN Date: Thu, 27 Feb 2020 10:11:37 +0100 Subject: [PATCH] Rename debug folder + add kernel/debug part --- .gitignore | 1 - src/kernel/Makefile | 2 +- src/kernel/util/debug/printk.c | 135 +++++++++++++++++++++++++++++++++ src/user/shell/Makefile | 2 +- src/user/test/Makefile | 2 +- 5 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 src/kernel/util/debug/printk.c diff --git a/.gitignore b/.gitignore index 5bda0dd..a9a1f7b 100644 --- a/.gitignore +++ b/.gitignore @@ -55,7 +55,6 @@ dkms.conf # Other *.txt build/ -debug .tests .old *.g1a diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 3ff5610..3538d2f 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -14,7 +14,7 @@ include ../../global.mk HEADER := -I../../include BUILD := ../../build/kernel OUTPUT := ../../output -DEBUG := ../../debug +DEBUG := ../../debug_bin NAME := vhex EXEC := $(OUTPUT)/$(NAME).g1a diff --git a/src/kernel/util/debug/printk.c b/src/kernel/util/debug/printk.c new file mode 100644 index 0000000..fcc86e1 --- /dev/null +++ b/src/kernel/util/debug/printk.c @@ -0,0 +1,135 @@ +#include +#include + +void printk(int x, int y, char const *str, ...) +{ + char hex[] = "0123456789abcdef"; + char buffer[16]; + int default_pos_x; + int starting_x; + int digits; + int32_t nb; + uint8_t base; + va_list ap; + int i; + + // Get starting variable args + va_start(ap, str); + + // Initialize part + i = -1; + starting_x = x; + x = x * (KERNEL_FONT_REAL_WIDTH + 1); + y = y * (KERNEL_FONT_REAL_HEIGHT + 1); + default_pos_x = x; + + // Walk into string and display character by character + while (str[++i] != '\0') + { + // New line + if (str[i] == '\n') + { + y = y + KERNEL_FONT_REAL_HEIGHT + 1; + x = default_pos_x; + continue; + } + // Horizontal tab + if (str[i] == '\t') + { + x = x / (KERNEL_FONT_REAL_WIDTH + 1); + x = (x + (4 - ((x - starting_x) & 3))) * (KERNEL_FONT_REAL_WIDTH + 1); + continue; + } + // String format "simple" + if (str[i] == '%') + { + if (str[i + 1] == 'd' || str[i + 1] == 'x') + { + // Initialise + digits = 0; + nb = va_arg(ap, int32_t); + base = (str[i + 1] == 'd') ? 10 : 16; + + // Check negative value + // FIXME: negative error (max) + if (nb < 0 && str[i + 1] == 'd') + { + nb = 0 - nb; + kvram_ascii(x, y, '-'); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + + // Generate buffer + // @note: generate in LSB mode. + while (digits == 0 || nb != 0) + { + buffer[digits++] = hex[nb % base]; + nb = nb / base; + } + + // Reverse dans display string + while (--digits >= 0) + { + kvram_ascii(x, y, buffer[digits]); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + i = i + 1; + continue; + } + if ((str[i + 1] == '#' && str[i + 2] == 'x') || str[i + 1] == 'p') + { + // add @ if 'p' (pointer) + if (str[i + 1] == 'p') + { + kvram_ascii(x, y, '@'); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + + // Add "0x" + kvram_ascii(x, y, '0'); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + kvram_ascii(x, y, 'x'); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + + // Get value + digits = 0; + nb = va_arg(ap, uint32_t); + while (digits < 8) + { + buffer[digits++] = hex[nb & 0x0f]; + nb = nb >> 4; + } + + // Display string. + while (--digits >= 0) + { + kvram_ascii(x, y, buffer[digits]); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + + // Update cursor + i = (str[i + 1] == '#') ? i + 2 : i + 1; + continue; + } + if (str[i + 1] == 's') + { + digits = 0; + nb = va_arg(ap, uint32_t); + while (((char*)nb)[digits] != '\0') + { + kvram_ascii(x, y, ((char*)nb)[digits++]); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + i = i + 1; + continue; + } + } + + // Default, display character + kvram_ascii(x, y, str[i]); + x = x + KERNEL_FONT_REAL_WIDTH + 1; + } + + // End of variodic args. + va_end(ap); +} diff --git a/src/user/shell/Makefile b/src/user/shell/Makefile index 6a4fdf8..6305f1f 100644 --- a/src/user/shell/Makefile +++ b/src/user/shell/Makefile @@ -11,7 +11,7 @@ include ../../../global.mk HEADER := -I../../../include -I../../../include/user LIBS := -L../../lib/ -l_unistd -l_stdio -l_string -l_display BUILD := ../../../build/user/shell -DEBUG := ../../../debug +DEBUG := ../../../debug_bin OUTPUT := ../../../output NAME := shell diff --git a/src/user/test/Makefile b/src/user/test/Makefile index 9391e6f..52b794a 100644 --- a/src/user/test/Makefile +++ b/src/user/test/Makefile @@ -11,7 +11,7 @@ include ../../../global.mk HEADER := -I../../../include -I../../../include/user LIBS := -L../../lib/ -l_unistd -l_stdio -l_string -l_display BUILD := ../../../build/user/test -DEBUG := ../../../debug +DEBUG := ../../../debug_bin OUTPUT := ../../../output NAME := test