From a5c560d2b97adc1ebd70897157514f302d42f34b Mon Sep 17 00:00:00 2001 From: ktkk Date: Fri, 7 Nov 2025 10:29:21 +0000 Subject: [PATCH] Update to Zig 0.15.2 --- build.zig | 10 ++++--- flake.lock | 6 ++--- src/Console.zig | 70 ++++++++++++++++++++++++++++++++++--------------- src/main.zig | 11 ++++---- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/build.zig b/build.zig index 05ff4db..e256efa 100644 --- a/build.zig +++ b/build.zig @@ -22,10 +22,12 @@ pub fn build(b: *std.Build) !void { const kernel = b.addExecutable(.{ .name = "kernel.elf", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = .ReleaseFast, - .code_model = .kernel, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = .ReleaseFast, + .code_model = .kernel, + }), }); kernel.setLinkerScript(b.path("kernel.ld")); diff --git a/flake.lock b/flake.lock index 2272d3d..bbb64ca 100644 --- a/flake.lock +++ b/flake.lock @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1742669843, - "narHash": "sha256-G5n+FOXLXcRx+3hCJ6Rt6ZQyF1zqQ0DL0sWAMn2Nk0w=", + "lastModified": 1762363567, + "narHash": "sha256-YRqMDEtSMbitIMj+JLpheSz0pwEr0Rmy5mC7myl17xs=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "1e5b653dff12029333a6546c11e108ede13052eb", + "rev": "ae814fd3904b621d8ab97418f1d0f2eb0d3716f4", "type": "github" }, "original": { diff --git a/src/Console.zig b/src/Console.zig index e7bda52..6b75378 100644 --- a/src/Console.zig +++ b/src/Console.zig @@ -20,18 +20,18 @@ pub const ConsoleColor = enum(u8) { red = 4, magenta = 5, brown = 6, - lightGray = 7, - darkGray = 8, - lightBlue = 9, - lightGreen = 10, - lightCyan = 11, - lightRed = 12, - lightMagenta = 13, - lightBrown = 14, + light_gray = 7, + dark_gray = 8, + light_blue = 9, + light_green = 10, + light_cyan = 11, + light_red = 12, + light_magenta = 13, + light_brown = 14, white = 15, }; -const default_color = vgaEntryColor(ConsoleColor.lightGray, ConsoleColor.black); +const default_color = vgaEntryColor(ConsoleColor.light_gray, ConsoleColor.black); column: usize = 0, color: u8 = default_color, @@ -115,21 +115,49 @@ pub fn puts(self: *Self, data: []const u8) void { } } -const Writer = std.io.Writer( - *Self, - error{}, - callback, -); +const Writer = struct { + console: *Self, + interface: std.Io.Writer, -pub fn writer(self: *Self) Writer { - return .{ .context = self }; -} + fn drain(io_w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { + const self: *@This() = @fieldParentPtr("interface", io_w); -fn callback(self: *Self, string: []const u8) error{}!usize { - self.puts(string); - return string.len; + try self.puts(io_w.buffered()); + io_w.end = 0; + + for (data[0..data.len - 1]) |bytes| { + try self.puts(bytes); + } + const pattern = data[data.len - 1]; + for (0..splat) |_| { + try self.puts(pattern); + } + + return std.Io.Writer.countSplat(data, splat); + } + + fn puts(self: *@This(), data: []const u8) std.Io.Writer.Error!void { + self.console.puts(data); + } +}; + +pub fn writer(self: *Self, buffer: []u8) Writer { + return .{ + .console = self, + .interface = .{ + .buffer = buffer, + .vtable = &.{ + .drain = Writer.drain, + }, + }, + }; } pub fn printf(self: *Self, comptime format: []const u8, args: anytype) void { - std.fmt.format(self.writer(), format, args) catch unreachable; + var buffer: [1024]u8 = undefined; + var w = self.writer(&buffer); + const io_w = &w.interface; + io_w.print(format, args) catch unreachable; + io_w.flush() catch unreachable; } + diff --git a/src/main.zig b/src/main.zig index d9fb6bb..2e2a80d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -20,7 +20,7 @@ export var mutliboot: MultibootHeader align(4) linksection(".multiboot") = .{ var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined; -export fn _start() callconv(.Naked) noreturn { +export fn _start() callconv(.naked) noreturn { asm volatile ( \\ movl %[stack_top], %%esp \\ movl %%esp, %%ebp @@ -68,11 +68,12 @@ fn klog( const c = console.get(); const color = switch (level) { - .err => Console.ConsoleColor.lightRed, - .warn => Console.ConsoleColor.lightBrown, - .info => Console.ConsoleColor.lightBlue, - .debug => Console.ConsoleColor.lightGreen, + .err => Console.ConsoleColor.light_red, + .warn => Console.ConsoleColor.light_brown, + .info => Console.ConsoleColor.light_blue, + .debug => Console.ConsoleColor.light_green, }; + c.putChar('['); c.setColor(color, Console.ConsoleColor.black); c.puts(level.asText());