Update to Zig 0.15.2

This commit is contained in:
ktkk 2025-11-07 10:29:21 +00:00
parent dc6f7e486f
commit a5c560d2b9
4 changed files with 64 additions and 33 deletions

View file

@ -22,10 +22,12 @@ pub fn build(b: *std.Build) !void {
const kernel = b.addExecutable(.{ const kernel = b.addExecutable(.{
.name = "kernel.elf", .name = "kernel.elf",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
.optimize = .ReleaseFast, .optimize = .ReleaseFast,
.code_model = .kernel, .code_model = .kernel,
}),
}); });
kernel.setLinkerScript(b.path("kernel.ld")); kernel.setLinkerScript(b.path("kernel.ld"));

6
flake.lock generated
View file

@ -20,11 +20,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1742669843, "lastModified": 1762363567,
"narHash": "sha256-G5n+FOXLXcRx+3hCJ6Rt6ZQyF1zqQ0DL0sWAMn2Nk0w=", "narHash": "sha256-YRqMDEtSMbitIMj+JLpheSz0pwEr0Rmy5mC7myl17xs=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "1e5b653dff12029333a6546c11e108ede13052eb", "rev": "ae814fd3904b621d8ab97418f1d0f2eb0d3716f4",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -20,18 +20,18 @@ pub const ConsoleColor = enum(u8) {
red = 4, red = 4,
magenta = 5, magenta = 5,
brown = 6, brown = 6,
lightGray = 7, light_gray = 7,
darkGray = 8, dark_gray = 8,
lightBlue = 9, light_blue = 9,
lightGreen = 10, light_green = 10,
lightCyan = 11, light_cyan = 11,
lightRed = 12, light_red = 12,
lightMagenta = 13, light_magenta = 13,
lightBrown = 14, light_brown = 14,
white = 15, white = 15,
}; };
const default_color = vgaEntryColor(ConsoleColor.lightGray, ConsoleColor.black); const default_color = vgaEntryColor(ConsoleColor.light_gray, ConsoleColor.black);
column: usize = 0, column: usize = 0,
color: u8 = default_color, color: u8 = default_color,
@ -115,21 +115,49 @@ pub fn puts(self: *Self, data: []const u8) void {
} }
} }
const Writer = std.io.Writer( const Writer = struct {
*Self, console: *Self,
error{}, interface: std.Io.Writer,
callback,
);
pub fn writer(self: *Self) Writer { fn drain(io_w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize {
return .{ .context = self }; const self: *@This() = @fieldParentPtr("interface", io_w);
}
fn callback(self: *Self, string: []const u8) error{}!usize { try self.puts(io_w.buffered());
self.puts(string); io_w.end = 0;
return string.len;
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 { 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;
} }

View file

@ -20,7 +20,7 @@ export var mutliboot: MultibootHeader align(4) linksection(".multiboot") = .{
var stack_bytes: [16 * 1024]u8 align(16) linksection(".bss") = undefined; 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 ( asm volatile (
\\ movl %[stack_top], %%esp \\ movl %[stack_top], %%esp
\\ movl %%esp, %%ebp \\ movl %%esp, %%ebp
@ -68,11 +68,12 @@ fn klog(
const c = console.get(); const c = console.get();
const color = switch (level) { const color = switch (level) {
.err => Console.ConsoleColor.lightRed, .err => Console.ConsoleColor.light_red,
.warn => Console.ConsoleColor.lightBrown, .warn => Console.ConsoleColor.light_brown,
.info => Console.ConsoleColor.lightBlue, .info => Console.ConsoleColor.light_blue,
.debug => Console.ConsoleColor.lightGreen, .debug => Console.ConsoleColor.light_green,
}; };
c.putChar('['); c.putChar('[');
c.setColor(color, Console.ConsoleColor.black); c.setColor(color, Console.ConsoleColor.black);
c.puts(level.asText()); c.puts(level.asText());