Permalink
Browse files

Files for part 6, https://www.youtube.com/watch?v=RTKj4jdo_2s

  • Loading branch information...
Viktor Engelmann Viktor Engelmann
Viktor Engelmann authored and Viktor Engelmann committed Jul 4, 2016
1 parent 01557ac commit a2f1cad21ae6d66d5730a213e8c654fae5e20dab
Showing with 221 additions and 24 deletions.
  1. +84 −17 interrupts.cpp
  2. +20 −2 interrupts.h
  3. +6 −3 kernel.cpp
  4. +90 −0 keyboard.cpp
  5. +19 −0 keyboard.h
  6. +2 −2 makefile
View
@@ -5,7 +5,40 @@
void printf(char* str);
InterruptHandler::InterruptHandler(InterruptManager* interruptManager, uint8_t InterruptNumber)
{
this->InterruptNumber = InterruptNumber;
this->interruptManager = interruptManager;
interruptManager->handlers[InterruptNumber] = this;
}
InterruptHandler::~InterruptHandler()
{
if(interruptManager->handlers[InterruptNumber] == this)
interruptManager->handlers[InterruptNumber] = 0;
}
uint32_t InterruptHandler::HandleInterrupt(uint32_t esp)
{
return esp;
}
InterruptManager::GateDescriptor InterruptManager::interruptDescriptorTable[256];
InterruptManager* InterruptManager::ActiveInterruptManager = 0;
void InterruptManager::SetInterruptDescriptorTableEntry(uint8_t interrupt,
@@ -36,10 +69,10 @@ InterruptManager::InterruptManager(uint16_t hardwareInterruptOffset, GlobalDescr
for(uint8_t i = 255; i > 0; --i)
{
SetInterruptDescriptorTableEntry(i, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE);
//handlers[i] = 0;
handlers[i] = 0;
}
SetInterruptDescriptorTableEntry(0, CodeSegment, &InterruptIgnore, 0, IDT_INTERRUPT_GATE);
//handlers[0] = 0;
handlers[0] = 0;
SetInterruptDescriptorTableEntry(0x00, CodeSegment, &HandleException0x00, 0, IDT_INTERRUPT_GATE);
SetInterruptDescriptorTableEntry(0x01, CodeSegment, &HandleException0x01, 0, IDT_INTERRUPT_GATE);
@@ -113,32 +146,66 @@ uint16_t InterruptManager::HardwareInterruptOffset()
void InterruptManager::Activate()
{
//if(ActiveInterruptManager == 0)
{
//ActiveInterruptManager = this;
asm("sti");
}
if(ActiveInterruptManager != 0)
ActiveInterruptManager->Deactivate();
ActiveInterruptManager = this;
asm("sti");
}
void InterruptManager::Deactivate()
{
/*if(ActiveInterruptManager == this)
if(ActiveInterruptManager == this)
{
ActiveInterruptManager = 0;
*/
//asm("cli");
/*
}*/
asm("cli");
}
}
uint32_t InterruptManager::HandleInterrupt(uint8_t interrupt, uint32_t esp)
{
char* foo = "INTERRUPT 0x00";
char* hex = "0123456789ABCDEF";
if(ActiveInterruptManager != 0)
return ActiveInterruptManager->DoHandleInterrupt(interrupt, esp);
return esp;
}
foo[12] = hex[(interrupt >> 4) & 0xF];
foo[13] = hex[interrupt & 0xF];
printf(foo);
uint32_t InterruptManager::DoHandleInterrupt(uint8_t interrupt, uint32_t esp)
{
if(handlers[interrupt] != 0)
{
esp = handlers[interrupt]->HandleInterrupt(esp);
}
else if(interrupt != hardwareInterruptOffset)
{
char* foo = "UNHANDLED INTERRUPT 0x00";
char* hex = "0123456789ABCDEF";
foo[22] = hex[(interrupt >> 4) & 0xF];
foo[23] = hex[interrupt & 0xF];
printf(foo);
}
// hardware interrupts must be acknowledged
if(hardwareInterruptOffset <= interrupt && interrupt < hardwareInterruptOffset+16)
{
programmableInterruptControllerMasterCommandPort.Write(0x20);
if(hardwareInterruptOffset + 8 <= interrupt)
programmableInterruptControllerSlaveCommandPort.Write(0x20);
}
return esp;
}
View
@@ -6,11 +6,29 @@
#include "types.h"
#include "port.h"
class InterruptManager;
class InterruptHandler
{
protected:
uint8_t InterruptNumber;
InterruptManager* interruptManager;
InterruptHandler(InterruptManager* interruptManager, uint8_t InterruptNumber);
~InterruptHandler();
public:
virtual uint32_t HandleInterrupt(uint32_t esp);
};
class InterruptManager
{
//friend class InterruptHandler;
friend class InterruptHandler;
protected:
static InterruptManager* ActiveInterruptManager;
InterruptHandler* handlers[256];
struct GateDescriptor
{
uint16_t handlerAddressLowBits;
@@ -29,7 +47,6 @@
} __attribute__((packed));
uint16_t hardwareInterruptOffset;
//static InterruptManager* ActiveInterruptManager;
static void SetInterruptDescriptorTableEntry(uint8_t interrupt,
uint16_t codeSegmentSelectorOffset, void (*handler)(),
uint8_t DescriptorPrivilegeLevel, uint8_t DescriptorType);
@@ -77,6 +94,7 @@
static void HandleException0x13();
static uint32_t HandleInterrupt(uint8_t interrupt, uint32_t esp);
uint32_t DoHandleInterrupt(uint8_t interrupt, uint32_t esp);
Port8BitSlow programmableInterruptControllerMasterCommandPort;
Port8BitSlow programmableInterruptControllerMasterDataPort;
View
@@ -2,13 +2,15 @@
#include "types.h"
#include "gdt.h"
#include "interrupts.h"
#include "keyboard.h"
void printf(char* str)
{
static uint16_t* VideoMemory = (uint16_t*)0xb8000;
static uint8_t x=0,y=0;
for(int i = 0; str[i] != '\0'; ++i)
{
switch(str[i])
@@ -22,13 +24,13 @@ void printf(char* str)
x++;
break;
}
if(x >= 80)
{
x = 0;
y++;
}
if(y >= 25)
{
for(y = 0; y < 25; y++)
@@ -59,6 +61,7 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
GlobalDescriptorTable gdt;
InterruptManager interrupts(0x20, &gdt);
KeyboardDriver keyboard(&interrupts);
interrupts.Activate();
while(1);
View
@@ -0,0 +1,90 @@
#include "keyboard.h"
KeyboardDriver::KeyboardDriver(InterruptManager* manager)
: InterruptHandler(manager, 0x21),
dataport(0x60),
commandport(0x64)
{
while(commandport.Read() & 0x1)
dataport.Read();
commandport.Write(0xae); // activate interrupts
commandport.Write(0x20); // command 0x20 = read controller command byte
uint8_t status = (dataport.Read() | 1) & ~0x10;
commandport.Write(0x60); // command 0x60 = set controller command byte
dataport.Write(status);
dataport.Write(0xf4);
}
KeyboardDriver::~KeyboardDriver()
{
}
void printf(char*);
uint32_t KeyboardDriver::HandleInterrupt(uint32_t esp)
{
uint8_t key = dataport.Read();
if(key < 0x80)
{
switch(key)
{
case 0x02: printf("1"); break;
case 0x03: printf("2"); break;
case 0x04: printf("3"); break;
case 0x05: printf("4"); break;
case 0x06: printf("5"); break;
case 0x07: printf("6"); break;
case 0x08: printf("7"); break;
case 0x09: printf("8"); break;
case 0x0A: printf("9"); break;
case 0x0B: printf("0"); break;
case 0x10: printf("q"); break;
case 0x11: printf("w"); break;
case 0x12: printf("e"); break;
case 0x13: printf("r"); break;
case 0x14: printf("t"); break;
case 0x15: printf("z"); break;
case 0x16: printf("u"); break;
case 0x17: printf("i"); break;
case 0x18: printf("o"); break;
case 0x19: printf("p"); break;
case 0x1E: printf("a"); break;
case 0x1F: printf("s"); break;
case 0x20: printf("d"); break;
case 0x21: printf("f"); break;
case 0x22: printf("g"); break;
case 0x23: printf("h"); break;
case 0x24: printf("j"); break;
case 0x25: printf("k"); break;
case 0x26: printf("l"); break;
case 0x2C: printf("y"); break;
case 0x2D: printf("x"); break;
case 0x2E: printf("c"); break;
case 0x2F: printf("v"); break;
case 0x30: printf("b"); break;
case 0x31: printf("n"); break;
case 0x32: printf("m"); break;
case 0x33: printf(","); break;
case 0x34: printf("."); break;
case 0x35: printf("-"); break;
case 0x1C: printf("\n"); break;
case 0x39: printf(" "); break;
default:
{
char* foo = "KEYBOARD 0x00 ";
char* hex = "0123456789ABCDEF";
foo[11] = hex[(key >> 4) & 0xF];
foo[12] = hex[key & 0xF];
printf(foo);
break;
}
}
}
return esp;
}
View
@@ -0,0 +1,19 @@
#ifndef __KEYBOARD_H
#define __KEYBOARD_H
#include "types.h"
#include "interrupts.h"
#include "port.h"
class KeyboardDriver : public InterruptHandler
{
Port8Bit dataport;
Port8Bit commandport;
public:
KeyboardDriver(InterruptManager* manager);
~KeyboardDriver();
virtual uint32_t HandleInterrupt(uint32_t esp);
};
#endif
View
@@ -2,11 +2,11 @@
# sudo apt-get install g++ binutils libc6-dev-i386
# sudo apt-get install VirtualBox grub-legacy xorriso
GCCPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore
GCCPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wno-write-strings
ASPARAMS = --32
LDPARAMS = -melf_i386
objects = loader.o gdt.o port.o interruptstubs.o interrupts.o kernel.o
objects = loader.o gdt.o port.o interruptstubs.o interrupts.o keyboard.o kernel.o
run: mykernel.iso

0 comments on commit a2f1cad

Please sign in to comment.