Compare commits

...

4 Commits

6 changed files with 64 additions and 24 deletions

View File

@ -148,6 +148,7 @@ fn fileExtEql(name: []const u8, ext: []const u8) bool {
fn findSources(dir: []const u8, ext: []const u8, prefix: ?[]const u8) ![][] const u8 {
var files = std.ArrayList([]const u8).init(alloc);
var walker = try std.fs.walkPath(alloc, dir);
defer walker.deinit();
while (try walker.next()) |entry| {
if(entry.kind != std.fs.Dir.Entry.Kind.File) continue;

View File

@ -1,19 +1,5 @@
const std = @import("std");
var GlobAlloc: ?std.heap.FixedBufferAllocator = null;
pub export fn kalloc_init(start: usize, len: usize) void {
// const aligned = std.mem.alignForward(start, 8);
const ptr = @intToPtr([*]u8, start);
const buf = ptr[0..len];
GlobAlloc = std.heap.FixedBufferAllocator.init(buf);
}
// export fn kalloc(bytes: usize, a: usize) void {
// var a = GlobAlloc.?.allocator.allocAdvanced(u8, a, bytes, std.mem.Allocator.Exact.at_least);
// return a catch unreachable;
// }
pub fn kallocNop() void {
std.log.err("calling kallocNop", .{});
}
var buf: [1024*1024]u8 = undefined;
pub var KAlloc = std.heap.FixedBufferAllocator.init(&buf);
// pub var KAlloc = std.heap.GeneralPurposeAllocator(.{.thread_safe=false}){.backing_allocator = &GlobAlloc.allocator};

View File

@ -98,6 +98,6 @@ fn logWrite(context: void, data: []const u8) error{}!usize {
return data.len;
}
const CWriter = std.io.Writer(void, error{}, putcharWrite){.context = undefined};
const DWriter = std.io.Writer(void, error{}, debugWrite){.context = undefined};
const AWriter = std.io.Writer(void, error{}, logWrite){.context = undefined};
pub const CWriter = std.io.Writer(void, error{}, putcharWrite){.context = undefined};
pub const DWriter = std.io.Writer(void, error{}, debugWrite){.context = undefined};
pub const AWriter = std.io.Writer(void, error{}, logWrite){.context = undefined};

View File

@ -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();

View File

@ -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");

41
kernel/src/time.zig Normal file
View File

@ -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)});
}