Skip to content

Commit

Permalink
IDT working, Keyboard interrupt tested
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobKlocker committed Jul 28, 2024
1 parent 5b36857 commit 7ba4707
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 64 deletions.
90 changes: 52 additions & 38 deletions src/idt.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const ports = @import("ports.zig");

extern fn isr0() void;
extern fn isr1() void;
extern fn isr2() void;
Expand Down Expand Up @@ -93,8 +95,6 @@ var idt_entires: [IDT_SIZE]IDT_ENTRY = undefined;

var idt_descriptor = IDT_DESCRIPTOR{ .base = 0, .limit = 0 };

extern fn keyboard_isr() void;

pub const idt = struct {
pub fn init() void {
for (&idt_entires) |*entry| {
Expand Down Expand Up @@ -155,16 +155,30 @@ pub const idt = struct {
setIdtEntry(&idt_entires[46], @intFromPtr(&isr46), 0x08, 0x8E);
setIdtEntry(&idt_entires[47], @intFromPtr(&isr47), 0x08, 0x8E);

//setIdtEntry(&idt_entires[128], @intFromPtr(&isr128), 0x08, 0x8E); //
//setIdtEntry(&idt_entires[177], @intFromPtr(&isr177), 0x08, 0x8E); //

//setIdtEntry(&idt_entires[0x21], @intFromPtr(&keyboard_isr), 0x08, 0x8E);
setIdtEntry(&idt_entires[128], @intFromPtr(&isr128), 0x08, 0x8E); //
setIdtEntry(&idt_entires[177], @intFromPtr(&isr177), 0x08, 0x8E); //

//flush idt
idtFlush(&idt_descriptor);
irqInstallHandler(1, &keyboard_isr);
}
};

var t: u32 = 0;
pub fn keyboard_isr(regs: *InterruptRegs) void {
if (regs.eax == 1) {
vga.Console.write("eax exists \n");
}
vga.Console.clear();

if (t == 0) {
vga.Console.write("key pressed");
} else {
vga.Console.write("often");
}
t += 1;
}

pub inline fn idtFlush(idtr: *IDT_DESCRIPTOR) void {
asm volatile ("lidt (%%eax)"
:
Expand All @@ -180,10 +194,6 @@ pub fn setIdtEntry(entry: *IDT_ENTRY, offset: usize, selector: u16, flags: u8) v
entry.base_high = @truncate((offset >> 16));
}

export fn handle_keyboard() void {
vga.Console.write("worked");
}

export fn isrHandler(regs: *InterruptRegs) void {
if (regs.err_code < 32) {
vga.Console.write(error_messages[regs.err_code]);
Expand All @@ -192,18 +202,31 @@ export fn isrHandler(regs: *InterruptRegs) void {
}
}

export fn isqHandler(regs: *InterruptRegs) void {
vga.Console.write("isq called");
vga.Console.write(error_messages[regs.err_code]);
var irq_routines: [16]?*const fn (*InterruptRegs) void = undefined;

export fn irqHandler(regs: *InterruptRegs) void {
const irq_handler = irq_routines[regs.int_no - 32];
if (irq_handler) |i|
i(regs);
if (regs.int_no >= 40)
ports.outb(0xA0, 0x20);
ports.outb(0x20, 0x20);
vga.Console.write("called");
}

pub fn irqInstallHandler(irq: usize, handler: *const fn (*InterruptRegs) void) void {
irq_routines[irq] = handler;
}

pub fn irqUninstallHandler(irq: i32) void {
irq_routines[irq] = undefined;
}

comptime {
asm (
\\ .section .text
\\ .macro interruptFunctions i
\\ .align 4
\\ .type isr\i, @function
\\ .global isr\i
\\ .type isr\i, @function
\\
\\ isr\i:
\\ cli
Expand All @@ -213,12 +236,11 @@ comptime {
\\
\\ pushl $\i
\\ .if(\i >= 32 && \i <= 47)
\\ push %ebx
// it's a irq, not a irs
// it's a irq, not a irs - irqs between 32 and 47
\\ mov $1, %ebx
\\ .else
// set ebx -> xor to 0
\\ xor %ebx, %ebx
\\ xor %ebx, %ebx
\\ .endif
\\ jmp interruptCommonStub
\\ .endmacro
Expand Down Expand Up @@ -255,7 +277,6 @@ comptime {
\\ interruptFunctions 29
\\ interruptFunctions 30
\\ interruptFunctions 31
\\
\\ interruptFunctions 32
\\ interruptFunctions 33
\\ interruptFunctions 34
Expand All @@ -275,47 +296,40 @@ comptime {
\\
\\ interruptFunctions 128
\\ interruptFunctions 177
);
}

comptime {
asm (
\\
\\
\\.extern isrHandler
\\.extern isqHandler
\\.extern irqHandler
\\interruptCommonStub:
\\ pusha
\\ mov %ds, %eax
\\ push %eax
\\ mov %cr2, %eax
\\ push %eax
\\
\\ mov $0x10, %ax
\\ mov $0x10, %ax
\\ mov %ax, %ds
\\ mov %ax, %es
\\ mov %ax, %fs
\\ mov %ax, %gs
\\
\\ push %esp
\\
\\ cmp $1, %ebx
\\ push %esp
\\ cmp $0, %ebx
\\ je isr
\\ call isqHandler
\\ call irqHandler
\\ jmp skip
\\
\\ isr:
\\ call isrHandler
\\
\\skip:
//; pop old which was used to check if isr or isq
\\ pop %ebx
\\ add $4, %esp
\\ pop %eax
\\ add $8, %esp
\\ pop %eax
\\ mov %ax, %ds
\\ mov %ax, %es
\\ mov %ax, %fs
\\ mov %ax, %gs
\\
\\ popa
\\ add $8, %esp
\\ add $8, %esp
\\ sti
\\ iret
);
Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const IDT = @import("idt.zig");
const GDT = @import("gdt.zig");
const PIC = @import("pic.zig");

export fn kernel_main() noreturn {
export fn kernel_main() void {
GDT.gdt.init();
PIC.remapNew();
PIC.remapPic();
IDT.idt.init();
console.clear();
console.setColor(VGA.White, VGA.Black);
Expand Down
48 changes: 24 additions & 24 deletions src/pic.zig
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
const ports = @import("ports.zig");

pub fn remap_pic(offset1: u8, offset2: u8) void {
// can read the data here first, in and add it at the end agian in case there are other interupts
// pub fn remapPic(offset1: u8, offset2: u8) void {
// // can read the data here first, in and add it at the end agian in case there are other interupts

//ICW1 - begin initialization
ports.outb(0x20, 0x11);
ports.outb(0xA0, 0x11);
// //ICW1 - begin initialization
// ports.outb(0x20, 0x11);
// ports.outb(0xA0, 0x11);

ports.io_wait();
// ports.io_wait();

//ICW2 - remap offset address of IDT || Has to be past 0x20, first 32 interupts are reserverd for cpu exceptions
ports.outb(offset1, 0x20);
ports.outb(offset2, 0x28);
// //ICW2 - remap offset address of IDT || Has to be past 0x20, first 32 interupts are reserverd for cpu exceptions
// ports.outb(offset1, 0x20);
// ports.outb(offset2, 0x28);

ports.io_wait();
// ports.io_wait();

// ICW3 - setup cascading
ports.outb(0x21, 0x00);
ports.outb(0xA1, 0x00);
// // ICW3 - setup cascading
// ports.outb(0x21, 0x00);
// ports.outb(0xA1, 0x00);

ports.io_wait();
// ports.io_wait();

// ICW4 - environment info
ports.outb(0x21, 0x01);
ports.outb(0xA1, 0x01);
// Initialization finished
// // ICW4 - environment info
// ports.outb(0x21, 0x01);
// ports.outb(0xA1, 0x01);
// // Initialization finished

ports.io_wait();
// ports.io_wait();

// mask interrupts
ports.outb(0x21, 0xff);
ports.outb(0xA1, 0xff);
}
// // mask interrupts
// ports.outb(0x21, 0xff);
// ports.outb(0xA1, 0xff);
// }

pub fn remapNew() void {
pub fn remapPic() void {
ports.outb(0x20, 0x11);
ports.outb(0xA0, 0x11);

Expand Down

0 comments on commit 7ba4707

Please sign in to comment.