From 7398da96623a5d2b165928d26ce8ae6364357d94 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Wed, 23 Dec 2020 04:11:00 +0100 Subject: [PATCH] add basic and highly inaccurate time support --- kernel/src/main.c | 3 +-- kernel/src/main.zig | 17 +++++++++++++++-- kernel/src/time.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 kernel/src/time.zig diff --git a/kernel/src/main.c b/kernel/src/main.c index 456bdcf..31b45c3 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -13,6 +13,7 @@ void kmain() { } printf("Kernel initialized! Platform: %s\n", arch); printf("Bootloader type: %s (%s %s)\n", boot_info.boot_protocol, boot_info.bootloader_name, boot_info.bootloader_ver); + setEpoch(boot_info.epoch); printf("System time: %d\n", boot_info.epoch); printf("Usable memory: %h (at %x)\n", boot_info.usable.len, boot_info.usable.start); printf("Cmdline: %s\n", boot_info.cmdline); @@ -23,8 +24,6 @@ void kmain() { // memcpy(PRINTBUF, TESTBUF, 32); // puts(PRINTBUF); - kalloc_init(boot_info.usable.start, boot_info.usable.len); - vga_setcolor(VGA_DARK_GRAY); vga_write_elsewhere("(c) Quinten Kock 2020 (MIT License)", 24, 0); halt_catch_fire(); diff --git a/kernel/src/main.zig b/kernel/src/main.zig index 11c8271..ca9be3b 100644 --- a/kernel/src/main.zig +++ b/kernel/src/main.zig @@ -2,11 +2,14 @@ const std = @import("std"); const builtin = @import("builtin"); const debug = @import("debug.zig"); const kalloc = @import("allocator/kalloc.zig"); +const time = @import("time.zig"); pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { debug.zigpanic(msg, error_return_trace); } +pub var alloc: *std.mem.Allocator = &kalloc.KAlloc.allocator; + pub const log_level = .debug; pub fn log( comptime level: std.log.Level, @@ -19,6 +22,7 @@ pub fn log( comptime { _ = debug; + _ = time; } const c = @cImport({ @@ -26,6 +30,7 @@ const c = @cImport({ @cInclude("hal/print.h"); @cInclude("print/print.h"); @cInclude("hal/ops.h"); + @cInclude("hal/info.h"); if( builtin.cpu.arch == .x86_64 or builtin.cpu.arch == .i386) { @cInclude("arch/x86/vga.h"); } @@ -35,10 +40,18 @@ export fn zigmain() void { // // const message: [*:0]const u8 = "hello, zig!\n"; std.log.info("All your codebase are belong to us.", .{}); - std.log.debug("CPU info: {}", .{builtin.cpu}); + std.log.debug("CPU info: {} ({})", .{builtin.cpu.arch, builtin.cpu.model.name}); c.puts("zig ee\n"); - kalloc.kallocNop(); + const msg = std.fmt.allocPrint(alloc, "Hello {}!", .{"ziguser"}); + std.log.debug("message: {}", .{msg}); + + // time.displayTime(debug.AWriter) catch unreachable; + + const cmdline = std.mem.span(c.boot_info.cmdline); + + std.log.info("cmdline: {}", .{cmdline}); + // @panic("ee"); diff --git a/kernel/src/time.zig b/kernel/src/time.zig new file mode 100644 index 0000000..47f200a --- /dev/null +++ b/kernel/src/time.zig @@ -0,0 +1,41 @@ +const std = @import("std"); + +var boot_epoch: u64 = undefined; +var epoch: u64 = 0; + +fn year_is_leap(y: u16) bool { + return (y%4) == 0; +} + +pub const DateTime = struct { + year: u16, + month: u8, + day: u16, + hours: u8, + mins: u8, + secs: u8, + + fn fromEpoch(e: u64) DateTime { + // const year = 1970; + // var yearlen = if(year_is_leap(year)) + + const year = 1970 + (e / 31556926); + const days = (e % 31556926) / 86400; + return .{ + .secs = @intCast(u8, e % 60), + .mins = @intCast(u8, (e/60) % 60), + .hours = @intCast(u8, (e /3600) % 60), + .day = @intCast(u16, days), + .month = 0, + .year = @intCast(u16, year), + }; + } +}; + +export fn setEpoch(e: u64) void { + boot_epoch = e; +} + +pub fn displayTime(w: anytype) !void { + try w.print("{}", .{DateTime.fromEpoch(boot_epoch)}); +} \ No newline at end of file