-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from jwt2706/dev
Dev
- Loading branch information
Showing
12 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
; This is just the assembly refrence for the load_idt function. It's actually done with inline assembly in "../src/drivers/idt.c" | ||
|
||
section .text | ||
global load_idt | ||
|
||
load_idt: | ||
lidt [rdi] | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef IDT_H | ||
#define IDT_H | ||
|
||
#include <stdint.h> | ||
|
||
struct idt_entry { | ||
uint16_t offset_low; | ||
uint16_t selector; | ||
uint8_t ist; | ||
uint8_t type_attr; | ||
uint16_t offset_mid; | ||
uint32_t offset_high; | ||
uint32_t zero; | ||
} __attribute__((packed)); | ||
|
||
struct idt_ptr { | ||
uint16_t limit; | ||
uint64_t base; | ||
} __attribute__((packed)); | ||
|
||
void idt_set_gate(int n, uint64_t handler); | ||
void idt_install(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef IO_H | ||
#define IO_H | ||
|
||
#include <stdint.h> | ||
|
||
uint8_t inb(uint16_t port); | ||
void outb(uint16_t port, uint8_t data); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef KEYBOARD_H | ||
#define KEYBOARD_H | ||
|
||
void keyboard_init(); | ||
void keyboard_interrupt_handler(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "../../include/drivers/idt.h" | ||
#include <string.h> | ||
|
||
#define IDT_SIZE 256 | ||
|
||
extern void keyboard_interrupt_handler(); | ||
|
||
struct idt_entry idt[IDT_SIZE]; | ||
struct idt_ptr idt_ptr; | ||
|
||
struct terminal* global_term; | ||
|
||
void idt_set_gate(int n, uint64_t handler) { | ||
idt[n].offset_low = handler & 0xFFFF; | ||
idt[n].selector = 0x08; // kernel code segment | ||
idt[n].ist = 0; | ||
idt[n].type_attr = 0x8E; // interrupt gate | ||
idt[n].offset_mid = (handler >> 16) & 0xFFFF; | ||
idt[n].offset_high = (handler >> 32) & 0xFFFFFFFF; | ||
idt[n].zero = 0; | ||
} | ||
|
||
void load_idt(struct idt_ptr* idt_ptr) { | ||
asm volatile ("lidt (%0)" : : "r"(idt_ptr)); // load idt | ||
asm volatile ("sti"); // enable interrupts | ||
} | ||
|
||
void idt_install(struct terminal* term) { | ||
idt_ptr.limit = sizeof(struct idt_entry) * IDT_SIZE - 1; | ||
idt_ptr.base = (uint64_t)&idt; | ||
|
||
memset(&idt, 0, sizeof(struct idt_entry) * IDT_SIZE); | ||
|
||
global_term = term; | ||
|
||
idt_set_gate(33, (uint64_t)keyboard_interrupt_handler); // IRQ1 | ||
|
||
load_idt(&idt_ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "../../include/drivers/io.h" | ||
#include <stdint.h> | ||
|
||
// ins | ||
uint8_t inb(uint16_t port) { | ||
uint8_t result; | ||
asm volatile ("inb %1, %0" : "=a"(result) : "Nd"(port)); | ||
return result; | ||
} | ||
|
||
// outs | ||
void outb(uint16_t port, uint8_t data) { | ||
asm volatile ("outb %0, %1" : : "a"(data), "Nd"(port)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "../../include/drivers/keyboard.h" | ||
#include "../../include/terminal.h" | ||
#include "../../include/drivers/io.h" | ||
#include <stdint.h> | ||
|
||
extern struct terminal term; | ||
|
||
#define KEYBOARD_DATA_PORT 0x60 | ||
#define KEYBOARD_STATUS_PORT 0x64 | ||
#define PIC1_COMMAND 0x20 | ||
#define PIC1_DATA 0x21 | ||
#define PIC_EOI 0x20 | ||
|
||
static uint8_t keyboard_map[128] = { | ||
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */ | ||
'9', '0', '-', '=', '\b', /* Backspace */ | ||
'\t', /* Tab */ | ||
'q', 'w', 'e', 'r', /* 19 */ | ||
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */ | ||
0, /* 29 - Control */ | ||
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */ | ||
'\'', '`', 0, /* Left shift */ | ||
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */ | ||
'm', ',', '.', '/', 0, /* Right shift */ | ||
'*', | ||
0, /* Alt */ | ||
' ', /* Space bar */ | ||
0, /* Caps lock */ | ||
0, /* 59 - F1 key ... > */ | ||
0, 0, 0, 0, 0, 0, 0, 0, | ||
0, /* < ... F10 */ | ||
0, /* 69 - Num lock*/ | ||
0, /* Scroll Lock */ | ||
0, /* Home key */ | ||
0, /* Up Arrow */ | ||
0, /* Page Up */ | ||
'-', | ||
0, /* Left Arrow */ | ||
0, | ||
0, /* Right Arrow */ | ||
'+', | ||
0, /* 79 - End key*/ | ||
0, /* Down Arrow */ | ||
0, /* Page Down */ | ||
0, /* Insert Key */ | ||
0, /* Delete Key */ | ||
0, 0, 0, 0, /* F11 Key */ | ||
0, /* F12 Key */ | ||
0, /* All other keys are undefined */ | ||
}; | ||
|
||
void keyboard_init() { | ||
// unmask the keyboard interrupt (IRQ1) | ||
uint8_t mask = inb(PIC1_DATA); | ||
mask &= ~(1 << 1); | ||
outb(PIC1_DATA, mask); | ||
} | ||
|
||
void keyboard_interrupt_handler(struct terminal* term) { | ||
uint8_t status = inb(KEYBOARD_STATUS_PORT); | ||
if (status & 0x01) { | ||
uint8_t keycode = inb(KEYBOARD_DATA_PORT); | ||
if (keycode < 128) { | ||
char c = keyboard_map[keycode]; | ||
if (c) { | ||
terminal_write_char(&term, c); | ||
} | ||
} | ||
} | ||
// send End of Interrupt (EOI) signal to PIC | ||
outb(PIC1_COMMAND, PIC_EOI); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters