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;
import hulk.framebuffer;
import hulk.fb;
import hulk.kfont;
struct Console
struct console
{
Framebuffer * m_fb;
size_t m_width;
size_t m_height;
size_t m_x;
size_t m_y;
private static __gshared size_t m_width;
private static __gshared size_t m_height;
private static __gshared size_t m_x;
private static __gshared size_t m_y;
public void initialize(Framebuffer * fb)
public static void initialize()
{
m_fb = fb;
m_width = fb.width / kfont.advance;
m_height = fb.height / kfont.line_height;
}
public void clear()
public static void clear()
{
m_fb.clear();
fb.clear();
m_x = 0u;
m_y = 0u;
}
public void write(char ch)
public static void write(char ch)
{
if (ch == '\n')
{
@ -48,25 +46,25 @@ struct Console
if (m_y == m_height)
{
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,
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];
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;
}
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.
*/
module hulk.framebuffer;
module hulk.fb;
import hos.memory;
import hulk.kfont;
@ -11,27 +11,27 @@ import hulk.kfont;
*
* TODO: Handle other pixel formats. Currently only BGRx is supported.
*/
struct Framebuffer
struct fb
{
/** Frame buffer base address. */
private uint * m_buffer;
private __gshared static uint * m_buffer;
/** Frame buffer width. */
private uint m_width;
private __gshared static uint m_width;
/** Frame buffer height. */
private uint m_height;
private __gshared static uint m_height;
/** Frame buffer stride. */
private uint m_stride;
private __gshared static uint m_stride;
/** 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.
*/
public @property uint width()
public static @property uint width()
{
return m_width;
}
@ -39,7 +39,7 @@ struct Framebuffer
/**
* Get the framebuffer height.
*/
public @property uint height()
public static @property uint height()
{
return m_height;
}
@ -52,7 +52,7 @@ struct Framebuffer
* @param height Frame buffer height.
* @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_width = width;
@ -66,7 +66,7 @@ struct Framebuffer
*
* @param color Color to clear to.
*/
void clear(uint color = 0u)
static void clear(uint color = 0u)
{
memset32(m_buffer, color, m_buffer_size);
}
@ -80,7 +80,7 @@ struct Framebuffer
* @param height Height 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++)
{
@ -98,7 +98,7 @@ struct Framebuffer
* @param width Width 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);
memset32(&m_buffer[buffer_index], color, width);
@ -112,7 +112,7 @@ struct Framebuffer
* @param height Height 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);
for (size_t iy = 0u; iy < height; iy++)
@ -130,7 +130,7 @@ struct Framebuffer
* @param ch Character to draw.
* @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];
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 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)
{
y += height - 1u;
@ -178,7 +178,7 @@ struct Framebuffer
* @param height Bitmap height.
* @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)
{
blend_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height, color);
@ -195,7 +195,7 @@ struct Framebuffer
* @param width Bitmap width.
* @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)
{
y += height - 1u;
@ -224,7 +224,7 @@ struct Framebuffer
* @param width Bitmap width.
* @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)
{
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 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)],
&m_buffer[buffer_index(0u, y + height - 1u)],
@ -252,7 +252,7 @@ struct Framebuffer
*
* @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) |
((((color & 0xFF00u) * alpha) >> 8u) & 0xFF00u) |
@ -267,7 +267,7 @@ struct Framebuffer
*
* @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;
}

View File

@ -5,15 +5,12 @@ module hulk.hulk;
import hulk.header;
import hulk.bootinfo;
import hulk.framebuffer;
import hulk.fb;
import hulk.console;
import hos.memory;
import ldc.attributes;
import hulk.kfont;
static import hulk.klog;
private __gshared Framebuffer fb;
private __gshared Console console;
import hulk.klog;
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;
}
console.initialize(&fb);
console.initialize();
console.clear();
hulk.klog.initialize(&console);
klog.initialize();
for (size_t i = 1u; i != 0u; i++)
{
hulk.klog.writef("Hello! i = %010u\n", i);
klog.writef("Hello! i = %010u\n", i);
}
for (;;)

View File

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