From 605beb9b055112c82ac8d21a22de0139d0b483f5 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Fri, 19 Jul 2024 13:47:20 +0000 Subject: [PATCH] adding nixshell script, working on kprintf --- shell.nix | 17 +++++++ sources/kernel/io/out.zig | 94 ++++++++++++++++++++++++--------------- sources/kernel/kmain.zig | 3 ++ 3 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 shell.nix diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..136e989 --- /dev/null +++ b/shell.nix @@ -0,0 +1,17 @@ +{ + pkgs ? import {}, +}: + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + qemu + grub2 + mtools + neovim + nodejs_22 + ]; + shellHook = + '' + exec zsh + ''; +} diff --git a/sources/kernel/io/out.zig b/sources/kernel/io/out.zig index 3a57d04..9ed1e0b 100644 --- a/sources/kernel/io/out.zig +++ b/sources/kernel/io/out.zig @@ -15,37 +15,58 @@ const ArgTypes = enum Null }; -pub fn kprintf(comptime fmt: []const u8, args: anytype) void +pub fn kprintf(fmt: [*:0]const u8, ...) callconv(.C) c_int { - comptime var arg_idx: usize = 0; - comptime var arg_insert: bool = false; - comptime var arg_type: ArgTypes = .Null; + var ap = @cVaStart(); + defer @cVaEnd(&ap); - inline for(fmt) |c| + var arg_insert: bool = false; + var arg_type: ArgTypes = .Null; + var number_char_printed: i32 = 0; + + var i: usize = 0; + while(fmt[i] != 0) { - if(c == '{') + if(fmt[i] == '{') { arg_insert = true; + i += 1; continue; } if(arg_insert) { - if(c == '}') + if(fmt[i] == '}') { if(arg_type == .Null) - @compileError("invalid type identifier between the brackets"); - printArg(args[arg_idx], arg_type); + return -1; + + switch(arg_type) + { + ArgTypes.Char => + { + vga.putChar(@cVaArg(&ap, u8)); + number_char_printed += 1; + }, + ArgTypes.Int => + { + const j: i32 = @cVaArg(&ap, i32); + number_char_printed += putNb(j); + }, + ArgTypes.Float => _ = @cVaArg(&ap, f32), + ArgTypes.String => _ = @cVaArg(&ap, *u8), + ArgTypes.Pointer => _ = @cVaArg(&ap, *u32), + else => {}, + } arg_insert = false; - arg_idx += 1; arg_type = .Null; } else if(arg_type != .Null) - @compileError("too much type identifiers between the brackets") + return -1 else { - switch(c) + switch(fmt[i]) { 'c' => arg_type = .Char, 'i' => arg_type = .Int, @@ -53,50 +74,49 @@ pub fn kprintf(comptime fmt: []const u8, args: anytype) void 'p' => arg_type = .Pointer, 's' => arg_type = .String, - else => @compileError("invalid type identifier between the brackets"), + else => return -1, } } } else - vga.putChar(c); - } - - comptime - { - if(args.len != arg_idx) - @compileError("unused arguments"); + { + vga.putChar(fmt[i]); + number_char_printed += 1; + } + i += 1; } + return number_char_printed; } -fn printArg(arg: anytype, T: ArgTypes) void +pub fn putNb(nbr: i64) i32 { - switch(T) - { - .Char => vga.putChar(arg), - .Int => putNb(arg), - .Float => {}, - .String => {}, - .Pointer => {}, - else => {}, - } -} + var print_size: i32 = 0; -pub fn putNb(nbr: i64) void -{ if(nbr <= -2147483648) - vga.putString("-2147483648") + { + vga.putString("-2147483648"); + return 11; + } else if(nbr >= 2147483647) - vga.putString("2147483647") + { + vga.putString("2147483647"); + return 10; + } else if(nbr < 0) { vga.putChar('-'); - putNb(-nbr); + print_size += putNb(-nbr); } else if(nbr >= 10) { - putNb(@divFloor(nbr, 10)); + print_size += putNb(@divFloor(nbr, 10)); vga.putChar(@intCast(@mod(nbr, 10) + @as(u8, 48))); + print_size += 1; } else + { vga.putChar(@intCast(nbr + 48)); + print_size += 1; + } + return print_size; } diff --git a/sources/kernel/kmain.zig b/sources/kernel/kmain.zig index f315a5b..ba66f1c 100644 --- a/sources/kernel/kmain.zig +++ b/sources/kernel/kmain.zig @@ -32,5 +32,8 @@ 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, @as(f32, 1.6), "gros caca boudin"); + _ = console.putNb(caca); drivers.shutdownDrivers(); }