Skip to content

Commit

Permalink
Move stuff in tty.zig and ps2.zig
Browse files Browse the repository at this point in the history
Move most of tty.zig to lib/term.zig.
Move most of ps2.zig to tty.zig.
Add disable PIC in apic.zig.
Change system to ubik in main.
  • Loading branch information
Ratakor committed Oct 12, 2023
1 parent a744a1a commit 102a9b5
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 329 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn buildKernel(b: *std.Build) *std.Build.Step.Compile {
});
kernel.code_model = .kernel;
kernel.addModule("limine", limine.module("limine"));
kernel.addModule("ubik", b.createModule(.{ .source_file = .{ .path = "lib/ubik.zig" } }));
kernel.setLinkerScriptPath(.{
.path = concat(b, &[_][]const u8{ "kernel/linker-", @tagName(target.cpu_arch.?), ".ld" }),
});
Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = "ubik",
.version = "0.1.0",
.paths = .{"kernel"},
.paths = .{"kernel"}, // TODO: what is this for? put lib instead of kernel?
.dependencies = .{
.limine = .{
.url = "https://github.com/limine-bootloader/limine-zig/archive/71a8e000f66db5ae755a70e4b27a2f93e19e0d1e.tar.gz",
Expand Down
4 changes: 4 additions & 0 deletions kernel/apic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub var io_apics = std.ArrayList(*const IOAPIC).init(root.allocator);
pub var isos = std.ArrayList(*const ISO).init(root.allocator);

pub fn init() void {
// disable PIC
arch.out(u8, 0xa1, 0xff);
arch.out(u8, 0x21, 0xff);

timerCalibrate();
// configure spurious IRQ
writeRegister(.spurious, readRegister(.spurious) | (1 << 8) | 0xff);
Expand Down
13 changes: 7 additions & 6 deletions kernel/main.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const limine = @import("limine");
const ubik = @import("ubik");
const arch = @import("arch.zig");
const debug = @import("debug.zig");
const serial = @import("serial.zig");
Expand All @@ -24,7 +25,7 @@ pub const std_options = struct {
};

pub const os = struct {
pub const system = struct {};
pub const system = ubik;
pub const heap = struct {
pub const page_allocator = vmm.page_allocator;
};
Expand All @@ -50,13 +51,13 @@ pub export var module_request: limine.ModuleRequest = .{};
pub fn panic(msg: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
@setCold(true);
arch.disableInterrupts();
tty.resetColor();
tty.Color.setFg(.red);
// tty.resetColor();
// tty.Color.setFg(.red);
tty.write("\nKernel panic: ");
tty.resetColor();
// tty.resetColor();
tty.print("{s}\n", .{msg});
debug.printStackIterator(std.debug.StackIterator.init(@returnAddress(), @frameAddress()));
tty.hideCursor();
// tty.hideCursor();
arch.halt();
}

Expand All @@ -66,7 +67,7 @@ export fn _start() callconv(.C) noreturn {
if (@errorReturnTrace()) |stack_trace| {
debug.printStackTrace(stack_trace);
}
tty.hideCursor();
// tty.hideCursor();
};

const regs = arch.cpuid(0, 0);
Expand Down
182 changes: 7 additions & 175 deletions kernel/ps2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,23 @@ const cpu = @import("cpu.zig");
const apic = @import("apic.zig");
const tty = @import("tty.zig");

// TODO: termios <- in tty/Terminal ?
// TODO: user_write_lock -> user write on buffer + framebuffer, log user write ???

const ScanCode = enum(u8) {
ctrl = 0x1d,
ctrl_rel = 0x9d,
shift_right = 0x36,
shift_right_rel = 0xb6,
shift_left = 0x2a,
shift_left_rel = 0xaa,
alt_left = 0x38, // TODO, altGr too
alt_left_rel = 0xb8,
capslock = 0x3a,
numlock = 0x45, // TODO

keypad_enter = 0x1c,
keypad_slash = 0x35,
arrow_up = 0x48,
arrow_left = 0x4b,
arrow_down = 0x50,
arrow_right = 0x4d,

// TODO
insert = 0x52,
home = 0x47,
end = 0x4f,
pgup = 0x49,
pgdown = 0x51,
delete = 0x53,

_,
};

const esc = std.ascii.control_code.esc;
const bs = std.ascii.control_code.bs;

// zig fmt: off
const convtab_nomod = [_]u8{
0, esc, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', bs, '\t',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v',
'b', 'n', 'm', ',', '.', '/', 0, 0, 0, ' ',
};

const convtab_capslock = [_]u8{
0, esc, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', bs, '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '[', ']', '\n', 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', '\'', '`', 0, '\\', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', ',', '.', '/', 0, 0, 0, ' ',
};

const convtab_shift = [_]u8{
0, esc, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', bs, '\t',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', 0, 'A', 'S',
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z', 'X', 'C', 'V',
'B', 'N', 'M', '<', '>', '?', 0, 0, 0, ' ',
};

const convtab_shift_capslock = [_]u8{
0, esc, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', bs, '\t',
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '{', '}', '\n', 0, 'a', 's',
'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', '"', '~', 0, '|', 'z', 'x', 'c', 'v',
'b', 'n', 'm', '<', '>', '?', 0, 0, 0, ' '
};
// zig fmt: on

var extra_scancodes = false;
var ctrl_active = false;
var shift_active = false;
var capslock_active = false;

pub fn init() void {
// // Disable primary and secondary PS/2 ports
// // disable primary and secondary PS/2 ports
// write(0x64, 0xad);
// write(0x64, 0xa7);

// var config = readConfig();
// // Enable keyboard interrupt and keyboard scancode translation
// // enable keyboard interrupt and keyboard scancode translation
// config |= (1 << 0) | (1 << 6);
// // Enable mouse interrupt if any
// // enable mouse interrupt if any
// if ((config & (1 << 5)) != 0) {
// config |= (1 << 1);
// }
// writeConfig(config);

// // Enable keyboard port
// // enable keyboard port
// write(0x64, 0xae);
// // Enable mouse port if any
// // enable mouse port if any
// if ((config & (1 << 5)) != 0) {
// write(0x64, 0xa8);
// }
Expand Down Expand Up @@ -126,103 +55,6 @@ fn writeConfig(value: u8) void {

fn keyboardHandler(ctx: *cpu.Context) void {
_ = ctx;

defer apic.eoi();

const input = read();

if (input == 0xe0) {
extra_scancodes = true;
return;
}

if (extra_scancodes == true) {
extra_scancodes = false;

switch (@as(ScanCode, @enumFromInt(input))) {
.ctrl => {
ctrl_active = true;
return;
},
.ctrl_rel => {
ctrl_active = false;
return;
},
.keypad_enter => {
tty.write("\n");
return;
},
.keypad_slash => {
tty.write("/");
return;
},
// TODO for arrows we could also output A, B, C or D depending on termios settings
.arrow_up => {
tty.cursorUp(1);
return;
},
.arrow_left => {
tty.cursorBackward(1);
return;
},
.arrow_down => {
tty.cursorDown(1);
return;
},
.arrow_right => {
tty.cursorForward(1);
return;
},
.insert, .home, .end, .pgup, .pgdown, .delete => return,
else => {},
}
}

switch (@as(ScanCode, @enumFromInt(input))) {
.shift_left, .shift_right => {
shift_active = true;
return;
},
.shift_left_rel, .shift_right_rel => {
shift_active = false;
return;
},
.ctrl => {
ctrl_active = true;
return;
},
.ctrl_rel => {
ctrl_active = false;
return;
},
.capslock => {
capslock_active = !capslock_active;
return;
},
else => {},
}

var c: u8 = undefined;

if (input >= 0x3b) return; // TODO F1-F12 + keypad pressed

if (!capslock_active) {
if (!shift_active) {
c = convtab_nomod[input];
} else {
c = convtab_shift[input];
}
} else {
if (!shift_active) {
c = convtab_capslock[input];
} else {
c = convtab_shift_capslock[input];
}
}

if (ctrl_active) {
c = std.ascii.toUpper(c) -% 0x40;
}

tty.write(&[1]u8{c});
tty.readKey(read());
apic.eoi();
}
2 changes: 1 addition & 1 deletion kernel/rand.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn init() void {
log.info("getting seed from rdrand: {}", .{seed});
} else {
seed = @as(u64, @intCast(pit.realtime.tv_sec)) ^ 0x91217df9814032ab;
log.info("getting seed from current time: {}", .{seed});
log.info("getting seed from time: {}", .{seed});
}

pcg = Pcg.init(seed);
Expand Down
Loading

0 comments on commit 102a9b5

Please sign in to comment.