]> git.dujemihanovic.xyz Git - nameless-os.git/blobdiff - kernel/arch/x86/tty/tty.c
kernel: tty: Use recursion in byte/word printing
[nameless-os.git] / kernel / arch / x86 / tty / tty.c
index 2110a7d003703dc3e0d2f89c483521889e2f33d7..ca3212a528c3254feac9d665305d72ed2c3642f2 100644 (file)
@@ -92,86 +92,81 @@ 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 kprintw(uint16_t word, int print_delm)
+{
+       if (print_delm)
+               kprint("0x", 0);
+
+       kprintb(word >> 8, 0);
+       kprintb(word, 0);
 }
 
-void kprintd(uint32_t dword)
+void kprintd(uint32_t dword, 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);
+
+       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);
 }
 
-void kprintq(uint64_t qword)
+int kprintdec(uint32_t num, uint8_t color)
 {
-       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);
+       char buffer[11];
+       int digits = 10;
+       /* TODO: make an actual memset function to use instead of this */
+       for (int i=0; i<11; i++) {
+               buffer[i] = 0;
+       }
+
+       /* put the numbers in the buffer */
+       for (int i=9; i>0 && num>0; i--) {
+               uint8_t currdigit = num%10;
+               buffer[i] = currdigit+'0';
+               num /= 10;
+       }
+
+       /* shift the array as much as needed */
+       while (*buffer == '\0') {
+               digits--;
+               for (int i=0; i<9; i++) {
+                       buffer[i] = buffer[i+1];
+               }
+       }
+
+       /* zero out any leftovers */
+       if (digits < 10) {
+               for (int i=digits; i<10; i++) {
+                       buffer[i] = '\0';
+               }
+       }
+
+       kprint(buffer, color);
+       return digits;
 }