diff --git a/src/fb.c b/src/fb.c index bd77613..a8d089d 100644 --- a/src/fb.c +++ b/src/fb.c @@ -33,6 +33,11 @@ void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch) fb_clear(); } +uint32_t * fb_addr(void) +{ + return fb.addr; +} + bool fb_ready(void) { return fb.addr != NULL; diff --git a/src/fb.h b/src/fb.h index d6ebb93..fe83875 100644 --- a/src/fb.h +++ b/src/fb.h @@ -6,6 +6,7 @@ void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch); bool fb_ready(void); +uint32_t * fb_addr(void); uint32_t fb_width(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); diff --git a/src/hos_main.c b/src/hos_main.c index 4416eda..bf1cd0d 100644 --- a/src/hos_main.c +++ b/src/hos_main.c @@ -1,8 +1,7 @@ #include #include "fb.h" -#include "fb_text.h" #include "mbinfo.h" -#include "kfont.h" +#include "klog.h" void hos_main(uint32_t mbinfo_addr) { @@ -11,5 +10,6 @@ void hos_main(uint32_t mbinfo_addr) { return; } - fb_text_render_char('H', 10, 10, 0xFFu, 0x88u, 0u); + klog_init(); + klog_printf("Welcome to HOS!\n"); } diff --git a/src/klog.c b/src/klog.c new file mode 100644 index 0000000..8b31302 --- /dev/null +++ b/src/klog.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#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); +} diff --git a/src/klog.h b/src/klog.h new file mode 100644 index 0000000..57a0a2a --- /dev/null +++ b/src/klog.h @@ -0,0 +1,7 @@ +#ifndef KLOG_H +#define KLOG_H + +void klog_init(void); +void klog_printf(const char * fmt, ...); + +#endif diff --git a/src/mem.h b/src/mem.h index fc01493..e5ead99 100644 --- a/src/mem.h +++ b/src/mem.h @@ -12,6 +12,17 @@ static inline void memcpy(void * dest, const void * src, size_t n) : "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) { return __builtin_memmove(dest, src, count);