commit a2a687d2ddc7982d8c9740d23934d87cc3578ef4
parent 664d56875223ab8cb8de82892aad2dcc33bc7012
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 4 Nov 2021 12:08:34 +0200
style fixes, more coming
Diffstat:
16 files changed, 335 insertions(+), 294 deletions(-)
diff --git a/include/dev/kbd.h b/include/dev/kbd.h
@@ -1,6 +1,12 @@
#ifndef _KERNEL_KBD_H_
#define _KERNEL_KBD_H_
+#define KBD_PRESSED 0x80
+#define KBD_LSHIFT 0x2a
+#define KBD_RSHIFT 0x36
+#define KBD_LSHIFT_REL 0xaa
+#define KBD_RSHIFT_REL 0xb6
+
void kbd_init(void);
#endif /* _KERNEL_KBD_H_ */
diff --git a/include/sys/idt.h b/include/sys/idt.h
@@ -1,99 +0,0 @@
-#ifndef _KERNEL_IDT_H_
-#define _KERNEL_IDT_H_
-
-/* IA-32 */
-struct idt_gate {
- uint16_t off_lo;
- uint16_t sel;
- uint8_t zero;
- uint8_t flags;
- uint16_t off_hi;
-} __attribute__((packed));
-
-/* This will be populated by `int_common_stub` in `int.asm`. */
-struct reg {
- /* Will be popped last. */
- uint32_t gs;
- uint32_t fs;
- uint32_t es;
- uint32_t ds;
- /* Pushed by `pusha`. */
- uint32_t edi;
- uint32_t esi;
- uint32_t ebp;
- uint32_t esp;
- uint32_t ebx;
- uint32_t edx;
- uint32_t ecx;
- uint32_t eax;
- /* Interrupt info. Pushed by `push byte`. */
- uint32_t intno;
- uint32_t err;
- /* Pushed by the CPU. */
- uint32_t eip;
- uint32_t cs;
- uint32_t eflags;
- uint32_t uesp;
- uint32_t ss;
-};
-
-/* Called by `kern_main`. */
-void idt_init(void);
-
-/* Called by drivers. */
-void int_handler(struct reg *);
-void int_add_handler(uint8_t, void (*)(struct reg *));
-
-/* The first 32 interrupts are reserved for exceptions. */
-void ex0(void);
-void ex1(void);
-void ex2(void);
-void ex3(void);
-void ex4(void);
-void ex5(void);
-void ex6(void);
-void ex7(void);
-void ex8(void);
-void ex9(void);
-void ex10(void);
-void ex11(void);
-void ex12(void);
-void ex13(void);
-void ex14(void);
-void ex15(void);
-void ex16(void);
-void ex17(void);
-void ex18(void);
-void ex19(void);
-void ex20(void);
-void ex21(void);
-void ex22(void);
-void ex23(void);
-void ex24(void);
-void ex25(void);
-void ex26(void);
-void ex27(void);
-void ex28(void);
-void ex29(void);
-void ex30(void);
-void ex31(void);
-
-/* IRQs */
-void irq0(void);
-void irq1(void);
-void irq2(void);
-void irq3(void);
-void irq4(void);
-void irq5(void);
-void irq6(void);
-void irq7(void);
-void irq8(void);
-void irq9(void);
-void irq10(void);
-void irq11(void);
-void irq12(void);
-void irq13(void);
-void irq14(void);
-void irq15(void);
-
-#endif /* _KERNEL_IDT_H_ */
diff --git a/include/sys/io.h b/include/sys/io.h
@@ -3,6 +3,16 @@
#include <stdint.h>
+#define IO_PIC1_CMD 0x20
+#define IO_PIC2_CMD 0xa0
+#define IO_PIC1_DATA (IO_PIC1_CMD + 1)
+#define IO_PIC2_DATA (IO_PIC2_CMD + 1)
+#define IO_TIMER_CMD 0x43
+#define IO_TIMER_DATA 0x40
+#define IO_CURS_CMD 0x3d4
+#define IO_CURS_DATA 0x3d5
+#define IO_KBD 0x60
+
static inline uint8_t
inb(uint16_t port)
{
diff --git a/include/sys/libk.h b/include/sys/libk.h
@@ -20,6 +20,7 @@ size_t strlen(const char *);
int strcmp(const char *, const char *);
int vsprintf(char *, const char *, va_list);
int sprintf(char *, const char *, ...);
+/* TODO: kprintf */
int printf(const char *, ...);
void panic(const char *, ...);
diff --git a/include/sys/port.h b/include/sys/port.h
@@ -1,14 +0,0 @@
-#ifndef _KERNEL_PORT_H
-#define _KERNEL_PORT_H
-
-#define P_PIC1_CMD 0x20
-#define P_PIC2_CMD 0xa0
-#define P_PIC1_DATA (P_PIC1_CMD + 1)
-#define P_PIC2_DATA (P_PIC2_CMD + 1)
-#define P_TIMER_CMD 0x43
-#define P_TIMER_DATA 0x40
-#define P_CURS_CMD 0x3d4
-#define P_CURS_DATA 0x3d5
-#define P_KBD 0x60
-
-#endif /* _KERNEL_PORT_H */
diff --git a/include/sys/timer.h b/include/sys/timer.h
@@ -1,6 +0,0 @@
-#ifndef _KERNEL_TIMER_H_
-#define _KERNEL_TIMER_H_
-
-void timer_init(uint32_t);
-
-#endif /* _KERNEL_TIMER_H_ */
diff --git a/kern/boot.asm b/kern/boot.asm
@@ -2,7 +2,8 @@
[org 0x7c00]
[bits 16]
-KERNOFF equ 0x1000
+MAGICOFF equ (0x7c00 + 510)
+KERNOFF equ 0x1000
section .text
global _start
@@ -34,7 +35,7 @@ _start:
a20_test:
pusha
- mov ax, [0x7c00 + 510] ; The magic number is there.
+ mov ax, [MAGICOFF] ; The magic number is there.
mov dx, ax
; We'll try to advance 1MB in memory. If the end result hasn't wrapped up
@@ -97,7 +98,7 @@ exit:
a20_enable:
pusha
-; BIOS interrupt.
+ ; BIOS interrupt.
mov ax, 0x2401 ; A20-Gate Active.
int 0x15
@@ -106,7 +107,7 @@ a20_enable:
je a20_done
jmp a20_fail
-; Keyboard controller.
+ ; Keyboard controller.
sti ; Enable interrupts.
call a20_waitc
@@ -268,7 +269,7 @@ pm_init:
call kernel_exec
jmp $
-; Hand control over to the C kernel. Godspeed.
+; Hand control over to the C kernel. Godspeed! You Black Emperor.
kernel_exec:
call KERNOFF
jmp $
@@ -292,6 +293,7 @@ putchar:
; String declarations.
str_diskerr: db "Error loading disk.", 0x0a, 0x0d, 0x00
str_a20_fail: db "The A20 Line is disabled", 0x0a, 0x0d, 0x00
+
BOOTDRV: db 0x80
; Padding to 512 bytes. The last 2 bytes will come from the magic number.
diff --git a/kern/idt.c b/kern/idt.c
@@ -1,14 +1,23 @@
#include <sys/libk.h>
-#include <sys/idt.h>
-#include <sys/port.h>
-#define N_INT 256
+#include "idt.h"
+
+#define NINT 256
+#define NISR 16
#define KERN_CODESEG 0x08
-static void idt_set_gate(uint8_t, uint32_t);
+static void idt_set_entry(uint32_t);
+
+/* IA-32 */
+struct idt_ent {
+ uint16_t off_lo;
+ uint16_t sel;
+ uint8_t zero;
+ uint8_t flags;
+ uint16_t off_hi;
+} __attribute__((packed)) idt[NINT];
-static struct idt_gate idt[N_INT];
-static void *isr[16] = {NULL};
+static void *isr[NISR] = {NULL};
static const char *except[] = {
"Division By Zero Exception",
"Debug Exception",
@@ -45,14 +54,17 @@ static const char *except[] = {
};
static void
-idt_set_gate(uint8_t n, uint32_t ptr)
+idt_set_entry(uint32_t ptr)
{
- idt[n].off_lo = ptr & 0xffff;
- idt[n].sel = KERN_CODESEG;
- idt[n].zero = 0;
+ static int i = 0;
+
+ idt[i].off_lo = ptr & 0xffff;
+ idt[i].sel = KERN_CODESEG;
+ idt[i].zero = 0;
/* The gate is present and is running in kernel mode. */
- idt[n].flags = 0x8e;
- idt[n].off_hi = (ptr >> 16) & 0xffff;
+ idt[i].flags = 0x8e;
+ idt[i].off_hi = (ptr >> 16) & 0xffff;
+ i++;
}
void
@@ -66,73 +78,73 @@ idt_init(void)
(void)memset(&idt, 0, sizeof(idt));
/* Set up exception interrupts. */
- idt_set_gate(0, (uint32_t)ex0);
- idt_set_gate(1, (uint32_t)ex1);
- idt_set_gate(2, (uint32_t)ex2);
- idt_set_gate(3, (uint32_t)ex3);
- idt_set_gate(4, (uint32_t)ex4);
- idt_set_gate(5, (uint32_t)ex5);
- idt_set_gate(6, (uint32_t)ex6);
- idt_set_gate(7, (uint32_t)ex7);
- idt_set_gate(8, (uint32_t)ex8);
- idt_set_gate(9, (uint32_t)ex9);
- idt_set_gate(10, (uint32_t)ex10);
- idt_set_gate(11, (uint32_t)ex11);
- idt_set_gate(12, (uint32_t)ex12);
- idt_set_gate(13, (uint32_t)ex13);
- idt_set_gate(14, (uint32_t)ex14);
- idt_set_gate(15, (uint32_t)ex15);
- idt_set_gate(16, (uint32_t)ex16);
- idt_set_gate(17, (uint32_t)ex17);
- idt_set_gate(18, (uint32_t)ex18);
- idt_set_gate(19, (uint32_t)ex19);
- idt_set_gate(20, (uint32_t)ex20);
- idt_set_gate(21, (uint32_t)ex21);
- idt_set_gate(22, (uint32_t)ex22);
- idt_set_gate(23, (uint32_t)ex23);
- idt_set_gate(24, (uint32_t)ex24);
- idt_set_gate(25, (uint32_t)ex25);
- idt_set_gate(26, (uint32_t)ex26);
- idt_set_gate(27, (uint32_t)ex27);
- idt_set_gate(28, (uint32_t)ex28);
- idt_set_gate(29, (uint32_t)ex29);
- idt_set_gate(30, (uint32_t)ex30);
- idt_set_gate(31, (uint32_t)ex31);
+ idt_set_entry((uint32_t)ex0);
+ idt_set_entry((uint32_t)ex1);
+ idt_set_entry((uint32_t)ex2);
+ idt_set_entry((uint32_t)ex3);
+ idt_set_entry((uint32_t)ex4);
+ idt_set_entry((uint32_t)ex5);
+ idt_set_entry((uint32_t)ex6);
+ idt_set_entry((uint32_t)ex7);
+ idt_set_entry((uint32_t)ex8);
+ idt_set_entry((uint32_t)ex9);
+ idt_set_entry((uint32_t)ex10);
+ idt_set_entry((uint32_t)ex11);
+ idt_set_entry((uint32_t)ex12);
+ idt_set_entry((uint32_t)ex13);
+ idt_set_entry((uint32_t)ex14);
+ idt_set_entry((uint32_t)ex15);
+ idt_set_entry((uint32_t)ex16);
+ idt_set_entry((uint32_t)ex17);
+ idt_set_entry((uint32_t)ex18);
+ idt_set_entry((uint32_t)ex19);
+ idt_set_entry((uint32_t)ex20);
+ idt_set_entry((uint32_t)ex21);
+ idt_set_entry((uint32_t)ex22);
+ idt_set_entry((uint32_t)ex23);
+ idt_set_entry((uint32_t)ex24);
+ idt_set_entry((uint32_t)ex25);
+ idt_set_entry((uint32_t)ex26);
+ idt_set_entry((uint32_t)ex27);
+ idt_set_entry((uint32_t)ex28);
+ idt_set_entry((uint32_t)ex29);
+ idt_set_entry((uint32_t)ex30);
+ idt_set_entry((uint32_t)ex31);
/* Remap the PIC */
/* TODO: explain.*/
- outb(P_PIC1_CMD, 0x11);
- outb(P_PIC2_CMD, 0x11);
- outb(P_PIC1_DATA, 0x20);
- outb(P_PIC2_DATA, 0x28);
- outb(P_PIC1_DATA, 0x04);
- outb(P_PIC2_DATA, 0x02);
- outb(P_PIC1_DATA, 0x01);
- outb(P_PIC2_DATA, 0x01);
- outb(P_PIC1_DATA, 0x00);
- outb(P_PIC2_DATA, 0x00);
+ outb(IO_PIC1_CMD, 0x11);
+ outb(IO_PIC2_CMD, 0x11);
+ outb(IO_PIC1_DATA, 0x20);
+ outb(IO_PIC2_DATA, 0x28);
+ outb(IO_PIC1_DATA, 0x04);
+ outb(IO_PIC2_DATA, 0x02);
+ outb(IO_PIC1_DATA, 0x01);
+ outb(IO_PIC2_DATA, 0x01);
+ outb(IO_PIC1_DATA, 0x00);
+ outb(IO_PIC1_DATA, 0x00);
/* Set up IRQs */
- idt_set_gate(32, (uint32_t)irq0);
- idt_set_gate(33, (uint32_t)irq1);
- idt_set_gate(34, (uint32_t)irq2);
- idt_set_gate(35, (uint32_t)irq3);
- idt_set_gate(36, (uint32_t)irq4);
- idt_set_gate(37, (uint32_t)irq5);
- idt_set_gate(38, (uint32_t)irq6);
- idt_set_gate(39, (uint32_t)irq7);
- idt_set_gate(40, (uint32_t)irq8);
- idt_set_gate(41, (uint32_t)irq9);
- idt_set_gate(42, (uint32_t)irq10);
- idt_set_gate(43, (uint32_t)irq11);
- idt_set_gate(44, (uint32_t)irq12);
- idt_set_gate(45, (uint32_t)irq13);
- idt_set_gate(46, (uint32_t)irq14);
- idt_set_gate(47, (uint32_t)irq15);
+ idt_set_entry((uint32_t)irq0);
+ idt_set_entry((uint32_t)irq1);
+ idt_set_entry((uint32_t)irq2);
+ idt_set_entry((uint32_t)irq3);
+ idt_set_entry((uint32_t)irq4);
+ idt_set_entry((uint32_t)irq5);
+ idt_set_entry((uint32_t)irq6);
+ idt_set_entry((uint32_t)irq7);
+ idt_set_entry((uint32_t)irq8);
+ idt_set_entry((uint32_t)irq9);
+ idt_set_entry((uint32_t)irq10);
+ idt_set_entry((uint32_t)irq11);
+ idt_set_entry((uint32_t)irq12);
+ idt_set_entry((uint32_t)irq13);
+ idt_set_entry((uint32_t)irq14);
+ idt_set_entry((uint32_t)irq15);
/* https://wiki.osdev.org/Interrupt_Descriptor_Table#Location_and_Size */
idtr.base = (uint32_t)&idt;
- idtr.limit = N_INT * sizeof(struct idt_gate) - 1;
+ idtr.limit = NINT * sizeof(struct idt_ent) - 1;
__asm__ __volatile__ ("lidtl (%0)" : : "r" (&idtr));
}
@@ -151,8 +163,8 @@ int_handler(struct reg *r)
else if (r->intno < 32)
panic("%s: System halted...\n", except[r->intno]);
if (r->intno >= 40)
- outb(P_PIC2_CMD, 0x20);
- outb(P_PIC1_CMD, 0x20);
+ outb(IO_PIC2_CMD, 0x20);
+ outb(IO_PIC1_CMD, 0x20);
}
void
diff --git a/kern/idt.h b/kern/idt.h
@@ -0,0 +1,92 @@
+#ifndef _KERNEL_IDT_H_
+#define _KERNEL_IDT_H_
+
+/* TODO: keep counter? */
+
+/* This will be populated by `int_common_stub` in `int.asm`. */
+struct reg {
+ /* Will be popped last. */
+ uint32_t gs;
+ uint32_t fs;
+ uint32_t es;
+ uint32_t ds;
+ /* Pushed by `pusha`. */
+ uint32_t edi;
+ uint32_t esi;
+ uint32_t ebp;
+ uint32_t esp;
+ uint32_t ebx;
+ uint32_t edx;
+ uint32_t ecx;
+ uint32_t eax;
+ /* Interrupt info. Pushed by `push byte`. */
+ uint32_t intno;
+ uint32_t err;
+ /* Pushed by the CPU. */
+ uint32_t eip;
+ uint32_t cs;
+ uint32_t eflags;
+ uint32_t uesp;
+ uint32_t ss;
+};
+
+/* Called by `kern_main`. */
+void idt_init(void);
+
+/* Called by drivers. */
+void int_handler(struct reg *);
+void int_add_handler(uint8_t, void (*)(struct reg *));
+
+/* The first 32 interrupts are reserved for exceptions. */
+void ex0(void);
+void ex1(void);
+void ex2(void);
+void ex3(void);
+void ex4(void);
+void ex5(void);
+void ex6(void);
+void ex7(void);
+void ex8(void);
+void ex9(void);
+void ex10(void);
+void ex11(void);
+void ex12(void);
+void ex13(void);
+void ex14(void);
+void ex15(void);
+void ex16(void);
+void ex17(void);
+void ex18(void);
+void ex19(void);
+void ex20(void);
+void ex21(void);
+void ex22(void);
+void ex23(void);
+void ex24(void);
+void ex25(void);
+void ex26(void);
+void ex27(void);
+void ex28(void);
+void ex29(void);
+void ex30(void);
+void ex31(void);
+
+/* IRQs */
+void irq0(void);
+void irq1(void);
+void irq2(void);
+void irq3(void);
+void irq4(void);
+void irq5(void);
+void irq6(void);
+void irq7(void);
+void irq8(void);
+void irq9(void);
+void irq10(void);
+void irq11(void);
+void irq12(void);
+void irq13(void);
+void irq14(void);
+void irq15(void);
+
+#endif /* _KERNEL_IDT_H_ */
diff --git a/kern/int.asm b/kern/int.asm
@@ -53,14 +53,14 @@ global irq13
global irq14
global irq15
-%macro intdef_noerr 1
+%macro int_noerr 1
cli
push byte 0
push byte %1
jmp int_common_stub
%endmacro
-%macro intdef_err 1
+%macro int_err 1
cli
push byte 0
push byte %1
@@ -68,55 +68,55 @@ global irq15
%endmacro
; Exceptions
-ex0: intdef_noerr 0 ; Division By Zero
-ex1: intdef_noerr 1 ; Debug
-ex2: intdef_noerr 2 ; Non Maskable Interrupt
-ex3: intdef_noerr 3 ; Breakpoint
-ex4: intdef_noerr 4 ; Into Detected Overflow
-ex5: intdef_noerr 5 ; Out of Bounds
-ex6: intdef_noerr 6 ; Invalid Opcode
-ex7: intdef_noerr 7 ; No Coprocessor
-ex8: intdef_err 8 ; Double Fault (with error code)
-ex9: intdef_noerr 9 ; Coprocessor Segment Overrrun
-ex10: intdef_err 10 ; Bad TSS (with error code)
-ex11: intdef_err 11 ; Segment Not Present (with error code)
-ex12: intdef_err 12 ; Stack Fault (with error code)
-ex13: intdef_err 13 ; General Protection Fault (with error code)
-ex14: intdef_err 14 ; Page Fault (with error code)
-ex15: intdef_noerr 15 ; Unkown Interrupt
-ex16: intdef_noerr 16 ; Coprocessor Fault
-ex17: intdef_noerr 17 ; Alignment Check (486+)
-ex18: intdef_noerr 18 ; Machine Check (Pentium/586+)
-ex19: intdef_noerr 19 ; Reserved
-ex20: intdef_noerr 20 ; Reserved
-ex21: intdef_noerr 21 ; Reserved
-ex22: intdef_noerr 22 ; Reserved
-ex23: intdef_noerr 23 ; Reserved
-ex24: intdef_noerr 24 ; Reserved
-ex25: intdef_noerr 25 ; Reserved
-ex26: intdef_noerr 26 ; Reserved
-ex27: intdef_noerr 27 ; Reserved
-ex28: intdef_noerr 28 ; Reserved
-ex29: intdef_noerr 29 ; Reserved
-ex30: intdef_noerr 30 ; Reserved
-ex31: intdef_noerr 31 ; Reserved
+ex0: int_noerr 0 ; Division By Zero
+ex1: int_noerr 1 ; Debug
+ex2: int_noerr 2 ; Non Maskable Interrupt
+ex3: int_noerr 3 ; Breakpoint
+ex4: int_noerr 4 ; Into Detected Overflow
+ex5: int_noerr 5 ; Out of Bounds
+ex6: int_noerr 6 ; Invalid Opcode
+ex7: int_noerr 7 ; No Coprocessor
+ex8: int_err 8 ; Double Fault (with error code)
+ex9: int_noerr 9 ; Coprocessor Segment Overrrun
+ex10: int_err 10 ; Bad TSS (with error code)
+ex11: int_err 11 ; Segment Not Present (with error code)
+ex12: int_err 12 ; Stack Fault (with error code)
+ex13: int_err 13 ; General Protection Fault (with error code)
+ex14: int_err 14 ; Page Fault (with error code)
+ex15: int_noerr 15 ; Unkown Interrupt
+ex16: int_noerr 16 ; Coprocessor Fault
+ex17: int_noerr 17 ; Alignment Check (486+)
+ex18: int_noerr 18 ; Machine Check (Pentium/586+)
+ex19: int_noerr 19 ; Reserved
+ex20: int_noerr 20 ; Reserved
+ex21: int_noerr 21 ; Reserved
+ex22: int_noerr 22 ; Reserved
+ex23: int_noerr 23 ; Reserved
+ex24: int_noerr 24 ; Reserved
+ex25: int_noerr 25 ; Reserved
+ex26: int_noerr 26 ; Reserved
+ex27: int_noerr 27 ; Reserved
+ex28: int_noerr 28 ; Reserved
+ex29: int_noerr 29 ; Reserved
+ex30: int_noerr 30 ; Reserved
+ex31: int_noerr 31 ; Reserved
; IRQs
-irq0: intdef_noerr 32 ; Programmable Interrupt Timer
-irq1: intdef_noerr 33 ; Keyboard
-irq2: intdef_noerr 34 ; Cascade (used internally by the two PICs, never raised)
-irq3: intdef_noerr 35 ; COM2 (if enabled)
-irq4: intdef_noerr 36 ; COM1 (if enabled)
-irq5: intdef_noerr 37 ; LPT2 (if enabled)
-irq6: intdef_noerr 38 ; Floppy Disk
-irq7: intdef_noerr 39 ; LPT1
-irq8: intdef_noerr 40 ; CMOS real-time clock (if enabled)
-irq9: intdef_noerr 41 ; Peripherals / Legacy SCSI / NIC
-irq10: intdef_noerr 42 ; Peripherals / SCSI / NIC
-irq11: intdef_noerr 43 ; Peripherals / SCSI / NIC
-irq12: intdef_noerr 44 ; PS2 Mouse
-irq13: intdef_noerr 45 ; FPU / Coprocessor / Inter-processor
-irq14: intdef_noerr 46 ; Primary ATA Hard Disk
-irq15: intdef_noerr 47 ; Secondary ATA Hard Disk
+irq0: int_noerr 32 ; Programmable Interrupt Timer
+irq1: int_noerr 33 ; Keyboard
+irq2: int_noerr 34 ; Cascade (used internally by the two PICs, never raised)
+irq3: int_noerr 35 ; COM2 (if enabled)
+irq4: int_noerr 36 ; COM1 (if enabled)
+irq5: int_noerr 37 ; LPT2 (if enabled)
+irq6: int_noerr 38 ; Floppy Disk
+irq7: int_noerr 39 ; LPT1
+irq8: int_noerr 40 ; CMOS real-time clock (if enabled)
+irq9: int_noerr 41 ; Peripherals / Legacy SCSI / NIC
+irq10: int_noerr 42 ; Peripherals / SCSI / NIC
+irq11: int_noerr 43 ; Peripherals / SCSI / NIC
+irq12: int_noerr 44 ; PS2 Mouse
+irq13: int_noerr 45 ; FPU / Coprocessor / Inter-processor
+irq14: int_noerr 46 ; Primary ATA Hard Disk
+irq15: int_noerr 47 ; Secondary ATA Hard Disk
; Save the processor state, call the C interrupt handler and restore the
; stack frame.
diff --git a/kern/kbd.c b/kern/kbd.c
@@ -1,12 +1,50 @@
#include <sys/libk.h>
-#include <sys/idt.h>
-#include <sys/port.h>
#include <dev/kbd.h>
+#include "idt.h"
+
static void kbd_callback(struct reg *);
-static unsigned char kbdus[128] = {
+static unsigned char kbdus_upper[128] = {
+ 0, /* Error */
+ 27, /* Escape */
+ '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
+ '_', '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
+ '{', '}', '\n',
+ 0, /* Control */
+ 'A', 'S', 'D', 'F', 'G',
+ 'H', 'J', 'K', 'L', ':', '"', '~',
+ 0, /* Left Shift */
+ '|', 'Z', 'X',
+ 'C', 'V', 'B', 'N', 'M', '<', '>', '?',
+ 0, /* Right Shift */
+ '*',
+ 0, /* Alt */
+ ' ',
+ 0, /* Caps Lock */
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F1-F10 */
+ 0, /* Num Lock */
+ 0, /* Scroll Lock */
+ 0, /* Home Key */
+ 0, /* Up Arrow */
+ 0, /* Page Up */
+ '-',
+ 0, /* Left Arrow */
+ 0,
+ 0, /* Right Arrow */
+ '+',
+ 0, /* End Key */
+ 0, /* Down Arrow */
+ 0, /* Page Down */
+ 0, /* Insert Key */
+ 0, /* Delete Key */
+ 0, 0, 0,
+ 0, 0, /* F11, F12 */
+ 0, /* The rest are undefined */
+};
+
+static unsigned char kbdus_lower[128] = {
0, /* Error */
27, /* Escape */
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
@@ -48,12 +86,13 @@ static void
kbd_callback(struct reg *r)
{
uint8_t sc;
+ int shift = 0;
- if ((sc = inb(P_KBD)) & 0x80) {
+ if ((sc = inb(IO_KBD)) & KBD_PRESSED) {
} else {
- tty_putc(kbdus[sc]);
+ /* TODO: shift */
+ tty_putc(shift ? kbdus_upper[sc] : kbdus_lower[sc]);
}
-
UNUSED(r);
}
@@ -61,4 +100,5 @@ void
kbd_init(void)
{
int_add_handler(1, kbd_callback);
+ printf("kbd on int 1\n");
}
diff --git a/kern/kern_main.c b/kern/kern_main.c
@@ -1,29 +1,22 @@
#include <sys/libk.h>
-#include <sys/idt.h>
-#include <sys/io.h>
-#include <sys/timer.h>
#include <dev/kbd.h>
-#define OK() ((void)printf("ok\n"))
+#include "idt.h"
+#include "timer.h"
-void
+int
kern_main(void)
{
tty_clear(VGA_BLUE, VGA_WHITE);
-
- printf("IDT... ");
idt_init();
- OK();
-
- printf("Timer... ");
- timer_init(50);
- OK();
-
- printf("Keyboard... ");
+ timer_init();
kbd_init();
- OK();
-
sti();
+
+ /* Off to userland! */
for (;;);
+
+ /* NOTREACHED */
+ return (0);
}
diff --git a/kern/libk.c b/kern/libk.c
@@ -1,6 +1,6 @@
#include <sys/libk.h>
-#include <sys/io.h>
+/* TODO: implement */
static char *
itoa(int n, char *buf, int base)
{
diff --git a/kern/timer.c b/kern/timer.c
@@ -1,6 +1,6 @@
#include <sys/libk.h>
-#include <sys/idt.h>
-#include <sys/port.h>
+
+#include "idt.h"
static void timer_callback(struct reg *);
@@ -10,17 +10,18 @@ static void
timer_callback(struct reg *r)
{
timer_ticks++;
-
UNUSED(r);
}
void
-timer_init(uint32_t hz)
+timer_init(void)
{
+ const uint32_t hz = 60;
uint32_t div = 1193180 / hz;
int_add_handler(0, timer_callback);
- outb(P_TIMER_CMD, 0x36);
- outb(P_TIMER_DATA, (uint8_t)(div & 0xff));
- outb(P_TIMER_DATA, (uint8_t)((div >> 8) & 0xff));
+ outb(IO_TIMER_CMD, 0x36);
+ outb(IO_TIMER_DATA, (uint8_t)(div & 0xff));
+ outb(IO_TIMER_DATA, (uint8_t)((div >> 8) & 0xff));
+ printf("timer on int 0\n");
}
diff --git a/kern/timer.h b/kern/timer.h
@@ -0,0 +1,6 @@
+#ifndef _KERNEL_TIMER_H_
+#define _KERNEL_TIMER_H_
+
+void timer_init(void);
+
+#endif /* _KERNEL_TIMER_H_ */
diff --git a/kern/tty.c b/kern/tty.c
@@ -1,7 +1,4 @@
#include <sys/libk.h>
-#include <sys/io.h>
-#include <sys/port.h>
-#include <sys/tty.h>
#define VGA_MEM 0xb8000;
#define VGA_COLS 80
@@ -89,22 +86,22 @@ void
tty_curs_enable(uint8_t start, uint8_t end)
{
/* 0x0a: Low cursor shape */
- outb(P_CURS_CMD, 0x0a);
- outb(P_CURS_DATA, (inb(P_CURS_DATA) & 0xc0) | start);
- outb(P_CURS_CMD, 0x0b);
- outb(P_CURS_DATA, (inb(P_CURS_DATA) & 0xe0) | end);
+ outb(IO_CURS_CMD, 0x0a);
+ outb(IO_CURS_DATA, (inb(IO_CURS_DATA) & 0xc0) | start);
+ outb(IO_CURS_CMD, 0x0b);
+ outb(IO_CURS_DATA, (inb(IO_CURS_DATA) & 0xe0) | end);
}
void
tty_curs_disable(void)
{
- outb(P_CURS_CMD, 0x0a);
+ outb(IO_CURS_CMD, 0x0a);
/*
* Bit representation:
* 7 6 | 5 | 4 0
* Unused | Disable | Shape
*/
- outb(P_CURS_DATA, 0x20);
+ outb(IO_CURS_DATA, 0x20);
}
/*
@@ -117,10 +114,10 @@ tty_curs_getpos(void)
{
uint16_t pos = 0;
- outb(P_CURS_CMD, 0x0e);
- pos |= (uint16_t)inb(P_CURS_DATA) << 8;
- outb(P_CURS_CMD, 0x0f);
- pos |= inb(P_CURS_DATA);
+ outb(IO_CURS_CMD, 0x0e);
+ pos |= (uint16_t)inb(IO_CURS_DATA) << 8;
+ outb(IO_CURS_CMD, 0x0f);
+ pos |= inb(IO_CURS_DATA);
return (pos);
}
@@ -131,9 +128,9 @@ tty_curs_setpos(int x, int y)
uint16_t pos = y * VGA_COLS + x;
/* Expect 8 highest bits */
- outb(P_CURS_CMD, 0x0e);
- outb(P_CURS_DATA, (uint8_t)((pos >> 8) & 0xff));
+ outb(IO_CURS_CMD, 0x0e);
+ outb(IO_CURS_DATA, (uint8_t)((pos >> 8) & 0xff));
/* Expect 8 lowest bits */
- outb(P_CURS_CMD, 0x0f);
- outb(P_CURS_DATA, (uint8_t)(pos & 0xff));
+ outb(IO_CURS_CMD, 0x0f);
+ outb(IO_CURS_DATA, (uint8_t)(pos & 0xff));
}