Move kprintf to its own file. It has nothing to do with VGA anymore.

This commit is contained in:
Andreas Kling 2018-10-22 13:20:35 +02:00
parent 702d308e67
commit bae59609e3
7 changed files with 188 additions and 187 deletions

View file

@ -20,7 +20,8 @@ KERNEL_OBJS = \
IDEDiskDevice.o \
MemoryManager.o \
Console.o \
IRQHandler.o
IRQHandler.o \
kprintf.o
VFS_OBJS = \
../VirtualFileSystem/DiskDevice.o \

View file

@ -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;
}

View file

@ -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, ...);

View file

@ -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)

169
Kernel/kprintf.cpp Normal file
View file

@ -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);
}

5
Kernel/kprintf.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
int kprintf(const char *fmt, ...);
int ksprintf(char* buf, const char *fmt, ...);

View file

@ -1,3 +1,3 @@
#pragma once
#include "VGA.h"
#include "kprintf.h"