add klog module to print kernel messages

This commit is contained in:
Josh Holtrop 2020-10-20 21:44:21 -04:00
parent 1d3b0a5621
commit db16e69d5d
6 changed files with 130 additions and 3 deletions

View File

@ -33,6 +33,11 @@ void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch)
fb_clear(); fb_clear();
} }
uint32_t * fb_addr(void)
{
return fb.addr;
}
bool fb_ready(void) bool fb_ready(void)
{ {
return fb.addr != NULL; return fb.addr != NULL;

View File

@ -6,6 +6,7 @@
void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch); void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch);
bool fb_ready(void); bool fb_ready(void);
uint32_t * fb_addr(void);
uint32_t fb_width(void); uint32_t fb_width(void);
uint32_t fb_height(void); uint32_t fb_height(void);
void fb_blend_alpha8(const uint8_t * bitmap, int width, int height, int pitch, int x, int y, uint8_t r, uint8_t g, uint8_t b); void fb_blend_alpha8(const uint8_t * bitmap, int width, int height, int pitch, int x, int y, uint8_t r, uint8_t g, uint8_t b);

View File

@ -1,8 +1,7 @@
#include <stdint.h> #include <stdint.h>
#include "fb.h" #include "fb.h"
#include "fb_text.h"
#include "mbinfo.h" #include "mbinfo.h"
#include "kfont.h" #include "klog.h"
void hos_main(uint32_t mbinfo_addr) void hos_main(uint32_t mbinfo_addr)
{ {
@ -11,5 +10,6 @@ void hos_main(uint32_t mbinfo_addr)
{ {
return; return;
} }
fb_text_render_char('H', 10, 10, 0xFFu, 0x88u, 0u); klog_init();
klog_printf("Welcome to HOS!\n");
} }

103
src/klog.c Normal file
View File

@ -0,0 +1,103 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdarg.h>
#include "klog.h"
#include "kfont.h"
#include "fb.h"
#include "fb_text.h"
#include "stream.h"
#include "hos_printf.h"
#include "mem.h"
static struct {
size_t console_width;
size_t console_height;
size_t x;
size_t y;
bool need_shift;
} klog;
static void shift_line(void)
{
uint32_t * fb = fb_addr();
uint32_t w = fb_width();
size_t console_fb_line_words = w * kfont.line_height;
for (size_t row = 0u; row < klog.console_height; row++)
{
memcpy32(fb, fb + console_fb_line_words, console_fb_line_words);
fb += console_fb_line_words;
}
fb_fill(0, kfont.line_height * (klog.console_height - 1u), w, kfont.line_height, 0u, 0x2Cu, 0x55u);
}
static void klog_fb_write1(char c)
{
if (klog.need_shift)
{
shift_line();
klog.need_shift = false;
}
if (c == '\n')
{
if (klog.y == (klog.console_height - 1u))
{
klog.need_shift = true;
}
else
{
klog.y++;
}
klog.x = 0u;
}
else
{
int px = klog.x * kfont.advance;
int py = klog.y * kfont.line_height;
fb_text_render_char(c, px, py, 0xFFu, 0x80u, 0u);
klog.x++;
if (klog.x == klog.console_width)
{
if (klog.y == (klog.console_height - 1u))
{
klog.need_shift = true;
}
else
{
klog.y++;
}
klog.x = 0u;
}
}
}
static void klog_fb_write(const char * src, size_t length)
{
for (size_t i = 0u; i < length; i++)
{
klog_fb_write1(src[i]);
}
}
static const stream_t klog_fb_stream = {
klog_fb_write,
klog_fb_write1,
};
void klog_init(void)
{
klog.console_width = fb_width() / kfont.advance;
klog.console_height = fb_height() / kfont.line_height;
klog.x = 0u;
klog.y = 0u;
klog.need_shift = false;
fb_fill(0, 0, fb_width(), fb_height(), 0u, 0x2Cu, 0x55u);
}
void klog_printf(const char * fmt, ...)
{
va_list va;
va_start(va, fmt);
hos_vprintf(&klog_fb_stream, fmt, va);
va_end(va);
}

7
src/klog.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef KLOG_H
#define KLOG_H
void klog_init(void);
void klog_printf(const char * fmt, ...);
#endif

View File

@ -12,6 +12,17 @@ static inline void memcpy(void * dest, const void * src, size_t n)
: "memory"); : "memory");
} }
static inline void memcpy32(void * dest, const void * src, size_t count)
{
uint32_t r0, r1, r2;
__asm__ __volatile__ (
"cld\n\t"
"rep movsd"
: "=&c" (r0), "=&S" (r1), "=&D" (r2)
: "2" (dest), "1" (src), "0" (count)
: "memory");
}
static inline void * memmove(void * dest, const void * src, size_t count) static inline void * memmove(void * dest, const void * src, size_t count)
{ {
return __builtin_memmove(dest, src, count); return __builtin_memmove(dest, src, count);