Skip to content

Commit

Permalink
creating libk, working compile time kprintf, adding toString functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Jul 20, 2024
1 parent 3eeef41 commit bbec8db
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 128 deletions.
39 changes: 39 additions & 0 deletions :w
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const builtin = @import("builtin");
const is_test = builtin.is_test;

comptime
{
if(!is_test)
{
switch(builtin.cpu.arch)
{
.x86 => _ = @import("arch/x86/boot.zig"),
else => unreachable,
}
}
}

const drivers = @import("drivers");
const libk = @import("libk");

pub const logs = @import("log.zig");
pub const kpanic = @import("panic.zig").kpanic;

pub const arch = if(!is_test) switch(builtin.cpu.arch)
{
.x86 => @import("arch/x86/arch.zig"),
else => unreachable,
};

export fn kmain() void
{
@setCold(true);
arch.init();
drivers.initDrivers();
logs.klogln("Welcome to RatiOS !");
const caca: i32 = 1048;
const caca2: i32 = 1048;
const caca3: i32 = 1048;
libk.io.kprintf("caca ;{};, ;{};, ;{};\n", .{ &caca, &caca2, &caca3 });
drivers.shutdownDrivers();
}
10 changes: 9 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void
.os_tag = .freestanding,
}),
.optimize = .Debug,
//.strip = true,
.strip = true,
.code_model = .kernel,
.pic = false,
.error_tracing = false,
Expand All @@ -22,8 +22,16 @@ pub fn build(b: *std.Build) void
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/drivers/index.zig" } }
});

const libk_module = b.addModule("libk", .{
.root_source_file = .{ .src_path = .{ .owner = b, .sub_path = "sources/libk/index.zig" } }
});

drivers_module.addImport("kernel", &kernel.root_module);
drivers_module.addImport("libk", libk_module);
kernel.root_module.addImport("drivers", drivers_module);
kernel.root_module.addImport("libk", libk_module);
libk_module.addImport("kernel", &kernel.root_module);
libk_module.addImport("drivers", drivers_module);

b.installArtifact(kernel);

Expand Down
2 changes: 1 addition & 1 deletion sources/drivers/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn initDrivers() void
kernel.logs.beginSection();
kb.init();
vga.init("RatiOS 0.2", vga.computeColor(vga.Color.BLACK, vga.Color.LIGHT_GREY), vga.computeColor(vga.Color.WHITE, vga.Color.DARK_GREY), vga.computeColor(vga.Color.LIGHT_BLUE, vga.Color.DARK_GREY));
power.init();
//power.init();
kernel.logs.endSection();
kernel.logs.klogln("[Drivers Manager] loaded drivers");
}
Expand Down
3 changes: 2 additions & 1 deletion sources/drivers/power/acpi.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const kernel = @import("kernel");
const libk = @import("libk");
const rsdt = @import("rsdt.zig");
const xsdt = @import("xsdt.zig");

Expand Down Expand Up @@ -107,7 +108,7 @@ fn getRSDP() !*u32

fn checkHeader(ptr: *u32, sig: []const u8) bool
{
if(kernel.memory.memcmp(@as([*]u8, @ptrCast(ptr)), @as([*]u8, @ptrCast(@constCast(sig))), 4) != 0)
if(libk.memory.memcmp(@as([*]u8, @ptrCast(ptr)), @as([*]u8, @ptrCast(@constCast(sig))), 4) != 0)
return false;
var check_ptr: [*]u8 = @ptrCast(ptr);
var len: usize = @as(*usize, @ptrFromInt(@intFromPtr(ptr) + 1)).*;
Expand Down
3 changes: 2 additions & 1 deletion sources/drivers/power/rsdt.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const kernel = @import("kernel");
const libk = @import("libk");

pub const RSDP = struct
{
Expand All @@ -14,7 +15,7 @@ pub fn checkRSDP(ptr: *u32) !*u32
const rsdp: *RSDP = @as(*RSDP, @ptrCast(ptr));
const sig = "RSD PTR ";
var check: u32 = 0;
if(kernel.memory.memcmp(sig, @as([*]u8, @ptrCast(rsdp)), 8) == 0)
if(libk.memory.memcmp(sig, @as([*]u8, @ptrCast(rsdp)), 8) == 0)
{
// Check checksum of rsdp
const bptr: [*]u8 = @ptrCast(ptr);
Expand Down
1 change: 0 additions & 1 deletion sources/kernel/arch/x86/gdt.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub const console = @import("../../io/out.zig");
const boot = @import("boot.zig");

const GDTEntry = packed struct
Expand Down
118 changes: 0 additions & 118 deletions sources/kernel/io/out.zig

This file was deleted.

6 changes: 1 addition & 5 deletions sources/kernel/kmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ comptime
}

const drivers = @import("drivers");
const libk = @import("libk");

pub const logs = @import("log.zig");
pub const kpanic = @import("panic.zig").kpanic;
pub const console = @import("io/out.zig");
pub const memory = @import("memory/memory.zig");

pub const arch = if(!is_test) switch(builtin.cpu.arch)
{
Expand All @@ -32,8 +31,5 @@ export fn kmain() void
arch.init();
drivers.initDrivers();
logs.klogln("Welcome to RatiOS !");
const caca: i32 = 1048;
_ = console.kprintf("caca ;{i};, ;{f};, ;{s};\n", caca);
_ = console.putNb(caca);
drivers.shutdownDrivers();
}
3 changes: 3 additions & 0 deletions sources/libk/index.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const memory = @import("memory/memory.zig");
pub const io = @import("io/io.zig");
pub const strings = @import("strings/strings.zig");
3 changes: 3 additions & 0 deletions sources/libk/io/io.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub const kputs = @import("out.zig").kputs;
pub const kputNb = @import("out.zig").kputNb;
pub const kprintf = @import("out.zig").kprintf;
126 changes: 126 additions & 0 deletions sources/libk/io/out.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const vga = @import("drivers").vga;
const string = @import("../strings/strings.zig");

pub fn kputs(message: []const u8) void
{
vga.putString(message);
}

const ArgTypes = enum
{
Int,
Float,
Char,
String,
Pointer,
Null
};

pub fn kprintf(comptime fmt: []const u8, args: anytype) void
{
comptime var arg_idx: usize = 0;
comptime var arg_insert: bool = false;
comptime var arg_type: ArgTypes = .Null;

inline for(fmt) |c|
{
if(c == '{')
{
arg_insert = true;
continue;
}

if(arg_insert)
{
if(c == '}')
{
if(arg_type == .Null)
{
if(@TypeOf(args[arg_idx]) == comptime_int)
{
if(args[arg_idx] > 0 and args[arg_idx] < 256)
vga.putChar(args[arg_idx])
else
putNb(args[arg_idx]);
}
else if(@typeInfo(@TypeOf(args[arg_idx])) == .Array and @typeInfo(@TypeOf(args[arg_idx])).Array.child == u8)
kputs(args[arg_idx])
else if(@typeInfo(@TypeOf(args[arg_idx])) == .Pointer)
{
const T = @typeInfo(@TypeOf(args[arg_idx])).Pointer;
if(@typeInfo(T.child) == .Array and @typeInfo(T.child).Array.child == u8)
kputs(args[arg_idx])
else
{
kputs("0x");
kputs(string.toStringBase(@intFromPtr(args[arg_idx]), 16));
}
}
else switch(@TypeOf(args[arg_idx]))
{
i8, u8, => vga.putChar(args[arg_idx]),
i16, u16, i32, u32, i64, u64, isize, usize => putNb(args[arg_idx]),
f16, f32, f64, comptime_float => {},
else => @compileError("could not manage auto detected type : " ++ @typeName(@TypeOf(args[arg_idx])) ++ "; please add type identifier between brackets"),
}
}
switch(arg_type)
{
.Char => vga.putChar(args[arg_idx]),
.Int => putNb(args[arg_idx]),
.Float => {},
.String => kputs(args[arg_idx]),
.Pointer => { kputs("0x"); kputs(string.toStringBase(@intFromPtr(args[arg_idx]), 16)); },
else => {},
}

arg_insert = false;
arg_idx += 1;
arg_type = .Null;
}
else if(arg_type != .Null)
@compileError("too much type identifiers between the brackets")
else
{
switch(c)
{
'c' => arg_type = .Char,
'i' => arg_type = .Int,
'f' => arg_type = .Float,
'p' => arg_type = .Pointer,
's' => arg_type = .String,

else => @compileError("invalid type identifier between the brackets"),
}
}
}
else
vga.putChar(c);
}

comptime
{
if(args.len != arg_idx)
@compileError("unused arguments");
}
}

pub fn putNb(nbr: i64) void
{
if(nbr <= -2147483648)
vga.putString("-2147483648")
else if(nbr >= 2147483647)
vga.putString("2147483647")
else if(nbr < 0)
{
vga.putChar('-');
putNb(-nbr);
}
else if(nbr >= 10)
{
putNb(@divFloor(nbr, 10));
vga.putChar(@intCast(@mod(nbr, 10) + @as(u8, 48)));
}
else
vga.putChar(@intCast(nbr + 48));
}
File renamed without changes.
4 changes: 4 additions & 0 deletions sources/libk/strings/strings.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const toString = @import("to_string.zig").toString;
pub const toStringBase = @import("to_string.zig").toStringBase;
pub const toStringBuffer = @import("to_string.zig").toStringBuffer;
pub const toStringBufferBase = @import("to_string.zig").toStringBufferBase;
Loading

0 comments on commit bbec8db

Please sign in to comment.