Convert fb, console, klog to "static instance" namespacing structs

This commit is contained in:
Josh Holtrop 2022-03-25 10:48:32 -04:00
parent 06242a0c9f
commit 98e992aad0
4 changed files with 102 additions and 110 deletions

View File

@ -3,32 +3,30 @@
*/ */
module hulk.console; module hulk.console;
import hulk.framebuffer; import hulk.fb;
import hulk.kfont; import hulk.kfont;
struct Console struct console
{ {
Framebuffer * m_fb; private static __gshared size_t m_width;
size_t m_width; private static __gshared size_t m_height;
size_t m_height; private static __gshared size_t m_x;
size_t m_x; private static __gshared size_t m_y;
size_t m_y;
public void initialize(Framebuffer * fb) public static void initialize()
{ {
m_fb = fb;
m_width = fb.width / kfont.advance; m_width = fb.width / kfont.advance;
m_height = fb.height / kfont.line_height; m_height = fb.height / kfont.line_height;
} }
public void clear() public static void clear()
{ {
m_fb.clear(); fb.clear();
m_x = 0u; m_x = 0u;
m_y = 0u; m_y = 0u;
} }
public void write(char ch) public static void write(char ch)
{ {
if (ch == '\n') if (ch == '\n')
{ {
@ -48,25 +46,25 @@ struct Console
if (m_y == m_height) if (m_y == m_height)
{ {
m_y--; m_y--;
m_fb.copy_rows_up(fb_y(m_height - 1u), fb.copy_rows_up(fb_y(m_height - 1u),
(m_height - 1u) * kfont.line_height, (m_height - 1u) * kfont.line_height,
kfont.line_height); kfont.line_height);
} }
} }
private void render_char(size_t x, size_t y, char ch) private static void render_char(size_t x, size_t y, char ch)
{ {
const(CharInfo) * ci = &kfont.chars[ch]; const(CharInfo) * ci = &kfont.chars[ch];
m_fb.blit_alpha_bitmap(fb_x(x) + ci.left, fb_y(y) + ci.top - ci.height, ci.bitmap, ci.width, ci.height); fb.blit_alpha_bitmap(fb_x(x) + ci.left, fb_y(y) + ci.top - ci.height, ci.bitmap, ci.width, ci.height);
} }
private size_t fb_x(size_t x) private static size_t fb_x(size_t x)
{ {
return x * kfont.advance; return x * kfont.advance;
} }
private size_t fb_y(size_t y) private static size_t fb_y(size_t y)
{ {
return m_fb.height - ((y + 1u) * kfont.line_height); return fb.height - ((y + 1u) * kfont.line_height);
} }
} }

View File

@ -1,7 +1,7 @@
/** /**
* HULK Framebuffer support. * HULK Framebuffer support.
*/ */
module hulk.framebuffer; module hulk.fb;
import hos.memory; import hos.memory;
import hulk.kfont; import hulk.kfont;
@ -11,27 +11,27 @@ import hulk.kfont;
* *
* TODO: Handle other pixel formats. Currently only BGRx is supported. * TODO: Handle other pixel formats. Currently only BGRx is supported.
*/ */
struct Framebuffer struct fb
{ {
/** Frame buffer base address. */ /** Frame buffer base address. */
private uint * m_buffer; private __gshared static uint * m_buffer;
/** Frame buffer width. */ /** Frame buffer width. */
private uint m_width; private __gshared static uint m_width;
/** Frame buffer height. */ /** Frame buffer height. */
private uint m_height; private __gshared static uint m_height;
/** Frame buffer stride. */ /** Frame buffer stride. */
private uint m_stride; private __gshared static uint m_stride;
/** Number of pixels in the frame buffer (whether visible or not). */ /** Number of pixels in the frame buffer (whether visible or not). */
private uint m_buffer_size; private __gshared static uint m_buffer_size;
/** /**
* Get the framebuffer width. * Get the framebuffer width.
*/ */
public @property uint width() public static @property uint width()
{ {
return m_width; return m_width;
} }
@ -39,7 +39,7 @@ struct Framebuffer
/** /**
* Get the framebuffer height. * Get the framebuffer height.
*/ */
public @property uint height() public static @property uint height()
{ {
return m_height; return m_height;
} }
@ -52,7 +52,7 @@ struct Framebuffer
* @param height Frame buffer height. * @param height Frame buffer height.
* @param stride Frame buffer stride. * @param stride Frame buffer stride.
*/ */
void initialize(uint * buffer, uint width, uint height, uint stride) static void initialize(uint * buffer, uint width, uint height, uint stride)
{ {
m_buffer = buffer; m_buffer = buffer;
m_width = width; m_width = width;
@ -66,7 +66,7 @@ struct Framebuffer
* *
* @param color Color to clear to. * @param color Color to clear to.
*/ */
void clear(uint color = 0u) static void clear(uint color = 0u)
{ {
memset32(m_buffer, color, m_buffer_size); memset32(m_buffer, color, m_buffer_size);
} }
@ -80,7 +80,7 @@ struct Framebuffer
* @param height Height of rectangle. * @param height Height of rectangle.
* @param color Color of rectangle. * @param color Color of rectangle.
*/ */
void rect(size_t x, size_t y, size_t width, size_t height, uint color) static void rect(size_t x, size_t y, size_t width, size_t height, uint color)
{ {
for (size_t iy = 0u; iy < height; iy++) for (size_t iy = 0u; iy < height; iy++)
{ {
@ -98,7 +98,7 @@ struct Framebuffer
* @param width Width of line. * @param width Width of line.
* @param color Color of line. * @param color Color of line.
*/ */
void hline(size_t x, size_t y, size_t width, uint color) static void hline(size_t x, size_t y, size_t width, uint color)
{ {
size_t buffer_index = buffer_index(x, y); size_t buffer_index = buffer_index(x, y);
memset32(&m_buffer[buffer_index], color, width); memset32(&m_buffer[buffer_index], color, width);
@ -112,7 +112,7 @@ struct Framebuffer
* @param height Height of line. * @param height Height of line.
* @param color Color of line. * @param color Color of line.
*/ */
void vline(size_t x, size_t y, size_t height, uint color) static void vline(size_t x, size_t y, size_t height, uint color)
{ {
size_t buffer_index = buffer_index(x, y); size_t buffer_index = buffer_index(x, y);
for (size_t iy = 0u; iy < height; iy++) for (size_t iy = 0u; iy < height; iy++)
@ -130,7 +130,7 @@ struct Framebuffer
* @param ch Character to draw. * @param ch Character to draw.
* @param color Color of character. * @param color Color of character.
*/ */
void character(int x, int y, char ch, uint color) static void character(int x, int y, char ch, uint color)
{ {
const(CharInfo) * ci = &kfont.chars[ch]; const(CharInfo) * ci = &kfont.chars[ch];
blend_alpha_bitmap(x + ci.left, y + kfont.baseline_offset + ci.top - ci.height, ci.bitmap, ci.width, ci.height, color); blend_alpha_bitmap(x + ci.left, y + kfont.baseline_offset + ci.top - ci.height, ci.bitmap, ci.width, ci.height, color);
@ -146,7 +146,7 @@ struct Framebuffer
* @param height Bitmap height. * @param height Bitmap height.
* @param color Color to blend with alpha value. * @param color Color to blend with alpha value.
*/ */
void blend_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap, static void blend_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap,
size_t width, size_t height, uint color) size_t width, size_t height, uint color)
{ {
y += height - 1u; y += height - 1u;
@ -178,7 +178,7 @@ struct Framebuffer
* @param height Bitmap height. * @param height Bitmap height.
* @param color Color to blend with alpha value. * @param color Color to blend with alpha value.
*/ */
void blend_alpha_bitmap(size_t x, size_t y, const(ubyte)[] alpha_bitmap, static void blend_alpha_bitmap(size_t x, size_t y, const(ubyte)[] alpha_bitmap,
size_t width, size_t height, uint color) size_t width, size_t height, uint color)
{ {
blend_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height, color); blend_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height, color);
@ -195,7 +195,7 @@ struct Framebuffer
* @param width Bitmap width. * @param width Bitmap width.
* @param height Bitmap height. * @param height Bitmap height.
*/ */
void blit_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap, static void blit_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap,
size_t width, size_t height) size_t width, size_t height)
{ {
y += height - 1u; y += height - 1u;
@ -224,7 +224,7 @@ struct Framebuffer
* @param width Bitmap width. * @param width Bitmap width.
* @param height Bitmap height. * @param height Bitmap height.
*/ */
void blit_alpha_bitmap(size_t x, size_t y, const(ubyte)[] alpha_bitmap, static void blit_alpha_bitmap(size_t x, size_t y, const(ubyte)[] alpha_bitmap,
size_t width, size_t height) size_t width, size_t height)
{ {
blit_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height); blit_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height);
@ -237,7 +237,7 @@ struct Framebuffer
* @param height Height of region to copy up. * @param height Height of region to copy up.
* @param offset Number of rows to copy region up by. * @param offset Number of rows to copy region up by.
*/ */
void copy_rows_up(size_t y, size_t height, size_t offset) static void copy_rows_up(size_t y, size_t height, size_t offset)
{ {
memcpy32(&m_buffer[buffer_index(0u, y + height + offset - 1u)], memcpy32(&m_buffer[buffer_index(0u, y + height + offset - 1u)],
&m_buffer[buffer_index(0u, y + height - 1u)], &m_buffer[buffer_index(0u, y + height - 1u)],
@ -252,7 +252,7 @@ struct Framebuffer
* *
* @return Scaled color value. * @return Scaled color value.
*/ */
private uint scale_color(uint color, ubyte alpha) private static uint scale_color(uint color, ubyte alpha)
{ {
return ((((color & 0xFFu) * alpha) >> 8u) & 0xFFu) | return ((((color & 0xFFu) * alpha) >> 8u) & 0xFFu) |
((((color & 0xFF00u) * alpha) >> 8u) & 0xFF00u) | ((((color & 0xFF00u) * alpha) >> 8u) & 0xFF00u) |
@ -267,7 +267,7 @@ struct Framebuffer
* *
* @return Buffer index for the given X and Y coordinates. * @return Buffer index for the given X and Y coordinates.
*/ */
private size_t buffer_index(size_t x, size_t y) private static size_t buffer_index(size_t x, size_t y)
{ {
return (m_height - y - 1u) * m_stride + x; return (m_height - y - 1u) * m_stride + x;
} }

View File

@ -5,15 +5,12 @@ module hulk.hulk;
import hulk.header; import hulk.header;
import hulk.bootinfo; import hulk.bootinfo;
import hulk.framebuffer; import hulk.fb;
import hulk.console; import hulk.console;
import hos.memory; import hos.memory;
import ldc.attributes; import ldc.attributes;
import hulk.kfont; import hulk.kfont;
static import hulk.klog; import hulk.klog;
private __gshared Framebuffer fb;
private __gshared Console console;
extern extern(C) __gshared ubyte _hulk_total_size; extern extern(C) __gshared ubyte _hulk_total_size;
@ -47,13 +44,13 @@ extern(C) void hulk_start(ulong rdi, ulong rsi, ulong rdx, BootInfo * bootinfo)
x += kfont.advance; x += kfont.advance;
} }
console.initialize(&fb); console.initialize();
console.clear(); console.clear();
hulk.klog.initialize(&console); klog.initialize();
for (size_t i = 1u; i != 0u; i++) for (size_t i = 1u; i != 0u; i++)
{ {
hulk.klog.writef("Hello! i = %010u\n", i); klog.writef("Hello! i = %010u\n", i);
} }
for (;;) for (;;)

View File

@ -7,70 +7,67 @@ import core.stdc.stdarg;
import hulk.console; import hulk.console;
static import hulk.writef; static import hulk.writef;
/** struct klog
* Kernel buffer size log.
* 16 gives a kernel buffer size of 64KB.
*/
enum size_t KLOG_SIZE_LOG = 16u;
/** Kernel buffer size. */
enum size_t KLOG_SIZE = 1u << KLOG_SIZE_LOG;
/** Kernel log buffer. */
private __gshared align(4096) char[KLOG_SIZE] klog_buffer;
/** Write index in the kernel log buffer. */
private __gshared size_t klog_index;
/** Pointer to the console to use for kernel log output. */
private __gshared Console * console;
/**
* Initialize the klog module.
*
* @param console Pointer to the Console to use for kernel output.
*/
void initialize(Console * console)
{ {
.console = console; /**
} * Kernel buffer size log.
* 16 gives a kernel buffer size of 64KB.
*/
private enum size_t KLOG_SIZE_LOG = 16u;
/** /** Kernel buffer size. */
* Write a formatted string to the kernel log. private enum size_t KLOG_SIZE = 1u << KLOG_SIZE_LOG;
*
* @param s Format string.
* @param args Variable arguments structure.
*/
void writef(string s, va_list args)
{
hulk.writef.writef(s, args, function void(ubyte ch) {
console.write(ch);
});
}
/** /** Kernel log buffer. */
* Write a formatted string to the kernel log. private static __gshared align(4096) char[KLOG_SIZE] klog_buffer;
*
* @param s Format string.
*/
extern (C) void writef(string s, ...)
{
va_list args;
va_start(args, s);
writef(s, args);
va_end(args);
}
/** /** Write index in the kernel log buffer. */
* Write a formatted string and newline to the console. private static __gshared size_t klog_index;
*
* @param s Format string. /**
*/ * Initialize the klog module.
extern (C) void writefln(string s, ...) */
{ public static void initialize()
va_list args; {
va_start(args, s); }
writef(s, args);
writef("\n", args); /**
va_end(args); * Write a formatted string to the kernel log.
*
* @param s Format string.
* @param args Variable arguments structure.
*/
public static void writef(string s, va_list args)
{
hulk.writef.writef(s, args, function void(ubyte ch) {
console.write(ch);
});
}
/**
* Write a formatted string to the kernel log.
*
* @param s Format string.
*/
public static extern (C) void writef(string s, ...)
{
va_list args;
va_start(args, s);
writef(s, args);
va_end(args);
}
/**
* Write a formatted string and newline to the console.
*
* @param s Format string.
*/
public static extern (C) void writefln(string s, ...)
{
va_list args;
va_start(args, s);
writef(s, args);
writef("\n", args);
va_end(args);
}
} }