diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-22 13:20:35 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-22 13:20:35 +0200 |
commit | bae59609e3a71c046c9bf4095b81d7ea825d1729 (patch) | |
tree | 4f92fd8053ecd890f7d82ec7340ec53f5df4477a /Kernel | |
parent | 702d308e67b369fbbfb863442df23a328b6055a3 (diff) | |
download | serenity-bae59609e3a71c046c9bf4095b81d7ea825d1729.zip |
Move kprintf to its own file. It has nothing to do with VGA anymore.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Makefile | 3 | ||||
-rw-r--r-- | Kernel/VGA.cpp | 192 | ||||
-rw-r--r-- | Kernel/VGA.h | 2 | ||||
-rw-r--r-- | Kernel/kassert.h | 2 | ||||
-rw-r--r-- | Kernel/kprintf.cpp | 169 | ||||
-rw-r--r-- | Kernel/kprintf.h | 5 | ||||
-rw-r--r-- | Kernel/kstdio.h | 2 |
7 files changed, 188 insertions, 187 deletions
diff --git a/Kernel/Makefile b/Kernel/Makefile index 2acd132f2f..1abe88803d 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -20,7 +20,8 @@ KERNEL_OBJS = \ IDEDiskDevice.o \ MemoryManager.o \ Console.o \ - IRQHandler.o + IRQHandler.o \ + kprintf.o VFS_OBJS = \ ../VirtualFileSystem/DiskDevice.o \ diff --git a/Kernel/VGA.cpp b/Kernel/VGA.cpp index 4b412122b9..2180490ad7 100644 --- a/Kernel/VGA.cpp +++ b/Kernel/VGA.cpp @@ -4,21 +4,10 @@ #include "IO.h" #include "StdLib.h" #include "Task.h" -#include <stdarg.h> -#include "Console.h" PRIVATE BYTE *vga_mem = 0L; PRIVATE BYTE current_attr = 0x07; -template<typename PutChFunc> static int printNumber(PutChFunc, char*&, DWORD); -template<typename PutChFunc> static int printHex(PutChFunc, char*&, DWORD, BYTE fields); -template<typename PutChFunc> static int printSignedNumber(PutChFunc, char*&, int); - -static void console_putch(char*, char ch) -{ - Console::the().write((byte*)&ch, 1); -} - void vga_scroll_up() { memcpy(vga_mem, vga_mem + 160, 160 * 23); @@ -32,196 +21,37 @@ void vga_putch_at(byte row, byte column, byte ch) vga_mem[cur + 1] = current_attr; } -template<typename PutChFunc> -int kprintfInternal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap) -{ - const char *p; - - int ret = 0; - char* bufptr = buffer; - - for (p = fmt; *p; ++p) { - if (*p == '%' && *(p + 1)) { - ++p; - switch( *p ) - { - case 's': - { - const char* sp = va_arg(ap, const char*); - //ASSERT(sp != nullptr); - if (!sp) { - putch(bufptr, '<'); - putch(bufptr, 'N'); - putch(bufptr, 'u'); - putch(bufptr, 'L'); - putch(bufptr, '>'); - ret += 5; - } else { - for (; *sp; ++sp) { - putch(bufptr, *sp); - ++ret; - } - } - } - break; - - case 'd': - ret += printSignedNumber(putch, bufptr, va_arg(ap, int)); - break; - - case 'u': - ret += printNumber(putch, bufptr, va_arg(ap, DWORD)); - break; - - case 'x': - ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8); - break; - - case 'w': - ret += printHex(putch, bufptr, va_arg(ap, int), 4); - break; - - case 'b': - ret += printHex(putch, bufptr, va_arg(ap, int), 2); - break; - - case 'c': - putch(bufptr, (char)va_arg(ap, int)); - ++ret; - break; - - case 'p': - putch(bufptr, '0'); - putch(bufptr, 'x'); - ret += 2; - ret += printHex(putch, bufptr, va_arg(ap, DWORD), 8); - break; - } - } - else { - putch(bufptr, *p); - ++ret; - } - } - return ret; -} - -int kprintf(const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - int ret = kprintfInternal(console_putch, nullptr, fmt, ap); - va_end(ap); - return ret; -} - -static void buffer_putch(char*& bufptr, char ch) -{ - *bufptr++ = ch; -} - -int ksprintf(char* buffer, const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - int ret = kprintfInternal(buffer_putch, buffer, fmt, ap); - buffer[ret] = '\0'; - va_end(ap); - return ret; -} - -template<typename PutChFunc> -int printHex(PutChFunc putch, char*& bufptr, DWORD number, BYTE fields) -{ - static const char h[] = { - '0','1','2','3','4','5','6','7', - '8','9','a','b','c','d','e','f' - }; - - int ret = 0; - BYTE shr_count = fields * 4; - while (shr_count) { - shr_count -= 4; - putch(bufptr, h[(number >> shr_count) & 0x0F]); - ++ret; - } - return ret; -} - -template<typename PutChFunc> -int printNumber(PutChFunc putch, char*& bufptr, DWORD number) -{ - DWORD divisor = 1000000000; - char ch; - char padding = 1; - int ret = 0; - - for (;;) { - ch = '0' + (number / divisor); - number %= divisor; - - if (ch != '0') - padding = 0; - - if (!padding || divisor == 1) { - putch(bufptr, ch); - ++ret; - } - - if (divisor == 1) - break; - divisor /= 10; - } - return ret; -} - -template<typename PutChFunc> -static int printSignedNumber(PutChFunc putch, char*& bufptr, int number) -{ - if (number < 0) { - putch(bufptr, '-'); - return printNumber(putch, bufptr, 0 - number) + 1; - } - return printNumber(putch, bufptr, number); -} - -PUBLIC void -vga_set_attr( BYTE attr ) +void vga_set_attr(BYTE attr) { current_attr = attr; } -PUBLIC BYTE -vga_get_attr() +byte vga_get_attr() { return current_attr; } -PUBLIC void -vga_init() +void vga_init() { DWORD i; current_attr = 0x07; vga_mem = (BYTE *)0xb8000; - for( i = 0; i < (80 * 24); ++i ) - { + for (i = 0; i < (80 * 24); ++i) { vga_mem[i*2] = ' '; vga_mem[i*2 + 1] = 0x07; } - /* Fill the bottom line with blue. */ - for( i = (80 * 24); i < (80 * 25); ++i ) - { + // Fill the bottom line with blue. + for (i = (80 * 24); i < (80 * 25); ++i) { vga_mem[i*2] = ' '; vga_mem[i*2 + 1] = 0x17; } vga_set_cursor( 0 ); } -PUBLIC WORD -vga_get_cursor() +WORD vga_get_cursor() { WORD value; IO::out8(0x3d4, 0x0e); @@ -231,13 +61,11 @@ vga_get_cursor() return value; } -PUBLIC void -vga_set_cursor( WORD value ) +void vga_set_cursor(WORD value) { - if( value >= (80 * 25) ) - { + if (value >= (80 * 25)) { /* XXX: If you try to move the cursor off the screen, I will go reddish pink! */ - vga_set_cursor( 0 ); + vga_set_cursor(0); current_attr = 0x0C; return; } diff --git a/Kernel/VGA.h b/Kernel/VGA.h index 08cf97149f..b13f84b32f 100644 --- a/Kernel/VGA.h +++ b/Kernel/VGA.h @@ -11,5 +11,3 @@ WORD vga_get_cursor(); void vga_putch_at(byte row, byte column, byte ch); void vga_scroll_up(); -int kprintf(const char *fmt, ...); -int ksprintf(char* buf, const char *fmt, ...); diff --git a/Kernel/kassert.h b/Kernel/kassert.h index 621aa9f4ec..5196f14df7 100644 --- a/Kernel/kassert.h +++ b/Kernel/kassert.h @@ -1,6 +1,6 @@ #pragma once -#include "VGA.h" +#include "kprintf.h" #define CRASH() do { asm volatile("ud2"); } while(0) #define ASSERT(x) do { if (!(x)) { kprintf("ASSERTION FAILED: " #x "\n%s:%u in %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__); CRASH(); } } while(0) diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp new file mode 100644 index 0000000000..2234967e59 --- /dev/null +++ b/Kernel/kprintf.cpp @@ -0,0 +1,169 @@ +#pragma once + +#include "kprintf.h" +#include "Console.h" +#include <stdarg.h> +#include <AK/Types.h> + +template<typename PutChFunc> static int printNumber(PutChFunc, char*&, dword); +template<typename PutChFunc> static int printHex(PutChFunc, char*&, dword, byte fields); +template<typename PutChFunc> static int printSignedNumber(PutChFunc, char*&, int); + +static void console_putch(char*, char ch) +{ + Console::the().write((byte*)&ch, 1); +} + +template<typename PutChFunc> +int kprintfInternal(PutChFunc putch, char* buffer, const char*& fmt, char*& ap) +{ + const char *p; + + int ret = 0; + char* bufptr = buffer; + + for (p = fmt; *p; ++p) { + if (*p == '%' && *(p + 1)) { + ++p; + switch( *p ) + { + case 's': + { + const char* sp = va_arg(ap, const char*); + //ASSERT(sp != nullptr); + if (!sp) { + putch(bufptr, '<'); + putch(bufptr, 'N'); + putch(bufptr, 'u'); + putch(bufptr, 'L'); + putch(bufptr, '>'); + ret += 5; + } else { + for (; *sp; ++sp) { + putch(bufptr, *sp); + ++ret; + } + } + } + break; + + case 'd': + ret += printSignedNumber(putch, bufptr, va_arg(ap, int)); + break; + + case 'u': + ret += printNumber(putch, bufptr, va_arg(ap, dword)); + break; + + case 'x': + ret += printHex(putch, bufptr, va_arg(ap, dword), 8); + break; + + case 'w': + ret += printHex(putch, bufptr, va_arg(ap, int), 4); + break; + + case 'b': + ret += printHex(putch, bufptr, va_arg(ap, int), 2); + break; + + case 'c': + putch(bufptr, (char)va_arg(ap, int)); + ++ret; + break; + + case 'p': + putch(bufptr, '0'); + putch(bufptr, 'x'); + ret += 2; + ret += printHex(putch, bufptr, va_arg(ap, dword), 8); + break; + } + } + else { + putch(bufptr, *p); + ++ret; + } + } + return ret; +} + +int kprintf(const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int ret = kprintfInternal(console_putch, nullptr, fmt, ap); + va_end(ap); + return ret; +} + +static void buffer_putch(char*& bufptr, char ch) +{ + *bufptr++ = ch; +} + +int ksprintf(char* buffer, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int ret = kprintfInternal(buffer_putch, buffer, fmt, ap); + buffer[ret] = '\0'; + va_end(ap); + return ret; +} + +template<typename PutChFunc> +int printHex(PutChFunc putch, char*& bufptr, dword number, byte fields) +{ + static const char h[] = { + '0','1','2','3','4','5','6','7', + '8','9','a','b','c','d','e','f' + }; + + int ret = 0; + byte shr_count = fields * 4; + while (shr_count) { + shr_count -= 4; + putch(bufptr, h[(number >> shr_count) & 0x0F]); + ++ret; + } + return ret; +} + +template<typename PutChFunc> +int printNumber(PutChFunc putch, char*& bufptr, dword number) +{ + dword divisor = 1000000000; + char ch; + char padding = 1; + int ret = 0; + + for (;;) { + ch = '0' + (number / divisor); + number %= divisor; + + if (ch != '0') + padding = 0; + + if (!padding || divisor == 1) { + putch(bufptr, ch); + ++ret; + } + + if (divisor == 1) + break; + divisor /= 10; + } + return ret; +} + +template<typename PutChFunc> +static int printSignedNumber(PutChFunc putch, char*& bufptr, int number) +{ + if (number < 0) { + putch(bufptr, '-'); + return printNumber(putch, bufptr, 0 - number) + 1; + } + return printNumber(putch, bufptr, number); +} + diff --git a/Kernel/kprintf.h b/Kernel/kprintf.h new file mode 100644 index 0000000000..bf7e74c2cf --- /dev/null +++ b/Kernel/kprintf.h @@ -0,0 +1,5 @@ +#pragma once + +int kprintf(const char *fmt, ...); +int ksprintf(char* buf, const char *fmt, ...); + diff --git a/Kernel/kstdio.h b/Kernel/kstdio.h index 8bf872581c..4a6dbfb3f5 100644 --- a/Kernel/kstdio.h +++ b/Kernel/kstdio.h @@ -1,3 +1,3 @@ #pragma once -#include "VGA.h" +#include "kprintf.h" |