Skip to content

Commit

Permalink
adding nixshell script, working on kprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
Kbz-8 committed Jul 19, 2024
1 parent bc339fd commit 605beb9
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 37 deletions.
17 changes: 17 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
pkgs ? import <nixpkgs> {},
}:

pkgs.mkShell {
nativeBuildInputs = with pkgs; [
qemu
grub2
mtools
neovim
nodejs_22
];
shellHook =
''
exec zsh
'';
}
94 changes: 57 additions & 37 deletions sources/kernel/io/out.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,88 +15,108 @@ 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,
'f' => arg_type = .Float,
'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;
}
3 changes: 3 additions & 0 deletions sources/kernel/kmain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 605beb9

Please sign in to comment.