setup framebuffer with console
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
//! A framebuffer text console: draws glyphs from an embedded PSF2 font directly
|
||||
//! into the linear framebuffer the bootloader handed us. No firmware, no driver
|
||||
//! — just pixels. This is the kernel's first output device.
|
||||
|
||||
const std = @import("std");
|
||||
const danos = @import("danos");
|
||||
|
||||
/// The console font, embedded at compile time. cp850-8x16, PSF2 format:
|
||||
/// a 32-byte header, then 256 glyphs of 16 bytes each (one byte per 8-pixel
|
||||
/// row). We index glyphs straight by byte value, so ASCII maps 1:1.
|
||||
const font = @embedFile("font.psf");
|
||||
const glyph_w = 8;
|
||||
const glyph_h = 16;
|
||||
const glyph_bytes = glyph_h; // 8 pixels wide => 1 byte per row
|
||||
const glyph_data = 32; // PSF2 header size
|
||||
|
||||
pub const Console = struct {
|
||||
fb: danos.Framebuffer,
|
||||
cols: u32,
|
||||
rows: u32,
|
||||
col: u32 = 0,
|
||||
row: u32 = 0,
|
||||
fg: u32 = 0x00c8_c8c8, // light grey
|
||||
bg: u32 = 0x0000_0000, // black
|
||||
|
||||
pub fn init(fb: danos.Framebuffer) Console {
|
||||
return .{
|
||||
.fb = fb,
|
||||
.cols = fb.width / glyph_w,
|
||||
.rows = fb.height / glyph_h,
|
||||
};
|
||||
}
|
||||
|
||||
/// Fill the whole screen with the background colour and home the cursor.
|
||||
pub fn clear(self: *Console) void {
|
||||
var y: u32 = 0;
|
||||
while (y < self.fb.height) : (y += 1) self.fillRow(y, self.bg);
|
||||
self.col = 0;
|
||||
self.row = 0;
|
||||
}
|
||||
|
||||
pub fn write(self: *Console, bytes: []const u8) void {
|
||||
for (bytes) |c| self.putChar(c);
|
||||
}
|
||||
|
||||
/// Formatted output, e.g. `con.print("x={d}\n", .{x})`. Silently truncates
|
||||
/// past 256 bytes — this is a debug console, not a general writer.
|
||||
pub fn print(self: *Console, comptime fmt: []const u8, args: anytype) void {
|
||||
var buf: [256]u8 = undefined;
|
||||
self.write(std.fmt.bufPrint(&buf, fmt, args) catch return);
|
||||
}
|
||||
|
||||
pub fn putChar(self: *Console, ch: u8) void {
|
||||
switch (ch) {
|
||||
'\n' => self.newline(),
|
||||
'\r' => self.col = 0,
|
||||
else => {
|
||||
if (self.col >= self.cols) self.newline();
|
||||
self.drawGlyph(ch, self.col * glyph_w, self.row * glyph_h);
|
||||
self.col += 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn newline(self: *Console) void {
|
||||
self.col = 0;
|
||||
if (self.row + 1 >= self.rows) {
|
||||
self.scroll();
|
||||
} else {
|
||||
self.row += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn drawGlyph(self: *Console, ch: u8, px: u32, py: u32) void {
|
||||
const rows = font[glyph_data + @as(usize, ch) * glyph_bytes ..][0..glyph_bytes];
|
||||
var gy: u32 = 0;
|
||||
while (gy < glyph_h) : (gy += 1) {
|
||||
const bits = rows[gy];
|
||||
var gx: u32 = 0;
|
||||
while (gx < glyph_w) : (gx += 1) {
|
||||
// Leftmost pixel is the high bit.
|
||||
const on = (bits >> @as(u3, @intCast(7 - gx))) & 1 != 0;
|
||||
self.pixel(px + gx, py + gy, if (on) self.fg else self.bg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shift the visible text up one glyph row and clear the freed bottom row,
|
||||
/// leaving the cursor on that now-blank last line.
|
||||
fn scroll(self: *Console) void {
|
||||
const visible = self.rows * glyph_h;
|
||||
var y: u32 = 0;
|
||||
while (y + glyph_h < visible) : (y += 1) self.copyRow(y, y + glyph_h);
|
||||
while (y < visible) : (y += 1) self.fillRow(y, self.bg);
|
||||
self.row = self.rows - 1;
|
||||
}
|
||||
|
||||
inline fn rowPtr(self: *Console, y: u32) [*]volatile u32 {
|
||||
const base: [*]volatile u8 = @ptrFromInt(self.fb.base);
|
||||
return @ptrCast(@alignCast(base + y * self.fb.pitch));
|
||||
}
|
||||
|
||||
inline fn pixel(self: *Console, x: u32, y: u32, color: u32) void {
|
||||
self.rowPtr(y)[x] = color;
|
||||
}
|
||||
|
||||
fn fillRow(self: *Console, y: u32, color: u32) void {
|
||||
const row = self.rowPtr(y);
|
||||
var x: u32 = 0;
|
||||
while (x < self.fb.width) : (x += 1) row[x] = color;
|
||||
}
|
||||
|
||||
fn copyRow(self: *Console, dst_y: u32, src_y: u32) void {
|
||||
const dst = self.rowPtr(dst_y);
|
||||
const src = self.rowPtr(src_y);
|
||||
var x: u32 = 0;
|
||||
while (x < self.fb.width) : (x += 1) dst[x] = src[x];
|
||||
}
|
||||
};
|
||||
Binary file not shown.
+24
-12
@@ -1,5 +1,6 @@
|
||||
const std = @import("std");
|
||||
const danos = @import("danos");
|
||||
const console = @import("console.zig");
|
||||
const BootInfo = danos.BootInfo;
|
||||
|
||||
/// The calling convention used to enter the kernel. Pinned to SysV explicitly:
|
||||
@@ -9,6 +10,11 @@ const BootInfo = danos.BootInfo;
|
||||
/// register the other expects. `danos.kernel_abi` re-exports it to the loader.
|
||||
pub const kernel_abi = danos.kernel_abi;
|
||||
|
||||
/// The system console, valid once `kmain` has initialised it. Global so the
|
||||
/// panic handler can reach it too.
|
||||
var con: console.Console = undefined;
|
||||
var con_ready = false;
|
||||
|
||||
/// Kernel entry point. The bootloader jumps here after `ExitBootServices` with a
|
||||
/// pointer to the handoff data. There is no runtime, no stack unwinding, and no
|
||||
/// caller to return to, so this never returns.
|
||||
@@ -17,16 +23,17 @@ export fn _start(boot_info: *const BootInfo) callconv(kernel_abi) noreturn {
|
||||
}
|
||||
|
||||
fn kmain(boot_info: *const BootInfo) noreturn {
|
||||
// Paint the whole screen so it is visually obvious the kernel took over.
|
||||
// We honour `pitch` because it can exceed `width * 4`.
|
||||
const fb = boot_info.framebuffer;
|
||||
const base: [*]volatile u8 = @ptrFromInt(fb.base);
|
||||
var y: u32 = 0;
|
||||
while (y < fb.height) : (y += 1) {
|
||||
const row: [*]volatile u32 = @ptrCast(@alignCast(base + y * fb.pitch));
|
||||
var x: u32 = 0;
|
||||
while (x < fb.width) : (x += 1) row[x] = 0x0033_66cc;
|
||||
}
|
||||
con = console.Console.init(fb);
|
||||
con.clear();
|
||||
con_ready = true;
|
||||
|
||||
con.write("danos: framebuffer console online\n");
|
||||
con.print(" resolution : {d}x{d}\n", .{ fb.width, fb.height });
|
||||
con.print(" pitch : {d} bytes\n", .{fb.pitch});
|
||||
con.print(" format : {s}\n", .{@tagName(fb.format)});
|
||||
con.print(" framebuffer: 0x{x:0>16}\n", .{fb.base});
|
||||
con.write("\nkernel initialised; nothing left to do, halting.\n");
|
||||
|
||||
hang();
|
||||
}
|
||||
@@ -37,12 +44,17 @@ fn hang() noreturn {
|
||||
while (true) asm volatile ("hlt");
|
||||
}
|
||||
|
||||
/// Freestanding has no OS to receive a panic, so override std's default panic
|
||||
/// handler to simply halt. (Later this can print to the framebuffer/serial.)
|
||||
/// Freestanding has no OS to receive a panic. Print it to the console (if it is
|
||||
/// up yet) in red, then halt.
|
||||
pub const panic = std.debug.FullPanic(struct {
|
||||
fn panic(msg: []const u8, first_trace_addr: ?usize) noreturn {
|
||||
_ = msg;
|
||||
_ = first_trace_addr;
|
||||
if (con_ready) {
|
||||
con.fg = 0x00ff_5555;
|
||||
con.write("\nKERNEL PANIC: ");
|
||||
con.write(msg);
|
||||
con.write("\n");
|
||||
}
|
||||
hang();
|
||||
}
|
||||
}.panic);
|
||||
|
||||
+4
-4
@@ -25,10 +25,10 @@ pub const PixelFormat = enum(u32) {
|
||||
/// `pitch` bytes between the start of one row and the next (which may be larger
|
||||
/// than `width * 4` due to hardware padding).
|
||||
pub const Framebuffer = extern struct {
|
||||
base: usize,
|
||||
width: u32,
|
||||
height: u32,
|
||||
pitch: u32,
|
||||
base: usize, // the memory address where pixel data starts
|
||||
width: u32, // visible pixels per row (e.g. 1920)
|
||||
height: u32, // visible rows (e.g. 1080)
|
||||
pitch: u32, // bytes from the start of one row to the start of the next
|
||||
format: PixelFormat,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user