Initial commit

This commit is contained in:
ktkk 2025-04-15 13:41:59 +00:00
commit dc6f7e486f
9 changed files with 427 additions and 0 deletions

89
src/main.zig Normal file
View file

@ -0,0 +1,89 @@
const lazy = @import("utils/Lazy.zig").lazy;
const Console = @import("Console.zig");
const ALIGN = 1 << 0;
const MEMINFO = 1 << 1;
const MAGIC = 0x1BADB002;
const FLAGS = ALIGN | MEMINFO;
const MultibootHeader = packed struct {
magic: i32 = MAGIC,
flags: i32,
checksum: i32,
padding: u32 = 0,
};
export var mutliboot: MultibootHeader align(4) linksection(".multiboot") = .{
.flags = FLAGS,
.checksum = -(MAGIC + FLAGS),
};
var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined;
export fn _start() callconv(.Naked) noreturn {
asm volatile (
\\ movl %[stack_top], %%esp
\\ movl %%esp, %%ebp
\\ call %[kmain:P]
:
: [stack_top] "i" (@as([*]align(16) u8, @ptrCast(&stack_bytes)) + @sizeOf(@TypeOf(stack_bytes))),
[kmain] "X" (&kmain),
);
}
var console = lazy(Console, .init);
fn kmain() callconv(.C) void {
std.log.info("This is an info message", .{});
std.log.debug("This is a debug message!", .{});
std.log.warn("This is a warning message!", .{});
std.log.err("This is an error message!", .{});
@panic("Uh oh");
}
const std = @import("std");
pub const panic = std.debug.FullPanic(kpanic);
fn kpanic(msg: []const u8, first_trace_addr: ?usize) noreturn {
_ = first_trace_addr;
std.log.err("PANIC: {s}", .{msg});
while (true) {}
}
pub const std_options = std.Options{
.log_level = .debug,
.logFn = klog,
};
fn klog(
comptime level: std.log.Level,
comptime scope: @Type(.enum_literal),
comptime format: []const u8,
args: anytype,
) void {
const c = console.get();
const color = switch (level) {
.err => Console.ConsoleColor.lightRed,
.warn => Console.ConsoleColor.lightBrown,
.info => Console.ConsoleColor.lightBlue,
.debug => Console.ConsoleColor.lightGreen,
};
c.putChar('[');
c.setColor(color, Console.ConsoleColor.black);
c.puts(level.asText());
c.resetColor();
c.putChar(']');
c.putChar(' ');
c.puts("(" ++ @tagName(scope) ++ ")");
c.putChar(' ');
c.printf(format ++ "\n", args);
}