implement exercise 04 interrupts
This commit is contained in:
@ -9,6 +9,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include "devices/Keyboard.h"
|
||||
#include "kernel/Globals.h"
|
||||
|
||||
/* Tabellen fuer ASCII-Codes (Klassenvariablen) intiialisieren */
|
||||
|
||||
@ -321,3 +322,44 @@ void Keyboard::set_led(char led, bool on) {
|
||||
|
||||
/* Hier muss Code eingefuegt werden. */
|
||||
}
|
||||
|
||||
// Registriert die Tastatur ISR im IntDispatcher
|
||||
// und erlaubt den keyboard interrupt
|
||||
void Keyboard::plugin(IntDispatcher& intdis, PIC& pic) {
|
||||
intdis.assign(IntDispatcher::keyboard, *this);
|
||||
pic.allow(PIC::keyboard);
|
||||
}
|
||||
|
||||
void scroll_mode(Key key);
|
||||
|
||||
void Keyboard::trigger() {
|
||||
// TODO: Get data from PS/2 Mouse if necessary
|
||||
|
||||
Key key = this->key_hit();
|
||||
|
||||
// NOTE: My keyboard has no delete key...
|
||||
if (key.ctrl_left() && key.alt_left() && (char)key == 'r') {
|
||||
this->reboot();
|
||||
}
|
||||
|
||||
scroll_mode(key);
|
||||
|
||||
// kout.show(0, 0, (char)key);
|
||||
}
|
||||
|
||||
// TODO: Where to place this?
|
||||
// Waits for keys to control the scrollback buffer display
|
||||
void scroll_mode(Key key) {
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
|
||||
switch ((char)key) {
|
||||
case 'k':
|
||||
kout.scroll_page_backward();
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
break;
|
||||
case 'j':
|
||||
kout.scroll_page_forward();
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,8 +12,10 @@
|
||||
#define __Keyboard_include__
|
||||
|
||||
#include "devices/Key.h"
|
||||
#include "kernel/IOport.h"
|
||||
#include "kernel/interrupts/IntDispatcher.h"
|
||||
#include "kernel/interrupts/ISR.h"
|
||||
#include "kernel/interrupts/PIC.h"
|
||||
#include "kernel/IOport.h"
|
||||
|
||||
class Keyboard : public ISR {
|
||||
|
||||
@ -30,17 +32,22 @@ private:
|
||||
const IOport data_port; // Ausgabe- (R) u. Eingabepuffer (W)
|
||||
|
||||
// Bits im Statusregister
|
||||
enum { outb = 0x01, inpb = 0x02, auxb = 0x20 };
|
||||
enum { outb = 0x01,
|
||||
inpb = 0x02,
|
||||
auxb = 0x20 };
|
||||
|
||||
// Kommandos an die Tastatur
|
||||
struct kbd_cmd {
|
||||
enum { set_led = 0xed, set_speed = 0xf3 };
|
||||
enum { set_led = 0xed,
|
||||
set_speed = 0xf3 };
|
||||
};
|
||||
enum { cpu_reset = 0xfe };
|
||||
|
||||
// Namen der LEDs
|
||||
struct led {
|
||||
enum { caps_lock = 4, num_lock = 2, scroll_lock = 1 };
|
||||
enum { caps_lock = 4,
|
||||
num_lock = 2,
|
||||
scroll_lock = 1 };
|
||||
};
|
||||
|
||||
// Antworten der Tastatur
|
||||
@ -49,7 +56,9 @@ private:
|
||||
};
|
||||
|
||||
// Konstanten fuer die Tastaturdekodierung
|
||||
enum { break_bit = 0x80, prefix1 = 0xe0, prefix2 = 0xe1 };
|
||||
enum { break_bit = 0x80,
|
||||
prefix1 = 0xe0,
|
||||
prefix2 = 0xe1 };
|
||||
|
||||
// Klassenvariablen
|
||||
static unsigned char normal_tab[];
|
||||
@ -64,9 +73,7 @@ private:
|
||||
// Ermittelt anhand von Tabellen den ASCII-Code.
|
||||
void get_ascii_code();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
// Initialisierung der Tastatur.
|
||||
Keyboard();
|
||||
|
||||
@ -83,10 +90,11 @@ public:
|
||||
void set_led(char led, bool on);
|
||||
|
||||
// Aktivierung der Unterbrechungen fuer die Tastatur
|
||||
void plugin ();
|
||||
// TODO: NOTE: I added this (parameters), but is it supposed to be this way?
|
||||
void plugin(IntDispatcher& intdis, PIC& pic);
|
||||
|
||||
// Unterbrechnungsroutine der Tastatur.
|
||||
void trigger ();
|
||||
void trigger() override;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
#ifndef __CPU_include__
|
||||
#define __CPU_include__
|
||||
|
||||
|
||||
class CPU {
|
||||
|
||||
private:
|
||||
@ -34,21 +33,20 @@ public:
|
||||
// Prozessor bis zum naechsten Interrupt anhalten
|
||||
inline void idle() {
|
||||
asm volatile("sti;"
|
||||
"hlt"
|
||||
);
|
||||
"hlt");
|
||||
}
|
||||
|
||||
// Prozessor anhalten
|
||||
inline void halt() {
|
||||
asm volatile("cli;"
|
||||
"hlt"
|
||||
);
|
||||
"hlt");
|
||||
}
|
||||
|
||||
// Time-Stamp-Counter auslesen
|
||||
inline unsigned long long int rdtsc() {
|
||||
unsigned long long int ret;
|
||||
asm volatile ( "rdtsc" : "=A"(ret) );
|
||||
asm volatile("rdtsc"
|
||||
: "=A"(ret));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
@ -10,14 +10,12 @@
|
||||
* *
|
||||
* Autor: Michael Schoettner, 31.8.2016 *
|
||||
*****************************************************************************/
|
||||
#include "kernel/interrupts/IntDispatcher.h"
|
||||
#include "kernel/CPU.h"
|
||||
#include "kernel/Globals.h"
|
||||
#include "kernel/interrupts/IntDispatcher.h"
|
||||
|
||||
|
||||
extern "C" void int_disp(unsigned int slot);
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Prozedur: int_disp *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -34,21 +32,20 @@ void int_disp (unsigned int vector) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
kout << "Ein Interrupt ist aufgetreten" << slot << endl;
|
||||
|
||||
// kout << "Ein Interrupt ist aufgetreten (vector: " << vector << ")" << endl;
|
||||
intdis.report(vector);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Konstruktor: IntDispatcher::IntDispatcher *
|
||||
*---------------------------------------------------------------------------*
|
||||
* Beschreibung: Initialisierung der ISR map mit einer Default-ISR. *
|
||||
*****************************************************************************/
|
||||
IntDispatcher::IntDispatcher() {
|
||||
for (unsigned int slot=0; slot<size; slot++)
|
||||
for (unsigned int slot = 0; slot < size; slot++) {
|
||||
map[slot] = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: IntDispatcher::assign *
|
||||
@ -65,8 +62,16 @@ int IntDispatcher::assign (unsigned int vector, ISR& isr) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
if (vector >= this->size) {
|
||||
kout << "Invalid vector number when assigning" << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
this->map[vector] = &isr;
|
||||
kout << "Registered ISR for vector " << dec << vector << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: IntDispatcher::report *
|
||||
@ -82,4 +87,18 @@ int IntDispatcher::report (unsigned int vector) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
if (vector >= this->size) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ISR* isr = this->map[vector];
|
||||
|
||||
if (isr == 0) {
|
||||
kout << "No ISR registered for vector " << vector << endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
isr->trigger();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -15,14 +15,12 @@
|
||||
* Autor: Olaf Spinczyk, TU Dortmund *
|
||||
*****************************************************************************/
|
||||
|
||||
#include "kernel/PIC.h"
|
||||
#include "kernel/interrupts/PIC.h"
|
||||
#include "kernel/IOport.h"
|
||||
|
||||
|
||||
static IOport IMR1(0x21); // interrupt mask register von PIC 1
|
||||
static IOport IMR2(0xa1); // interrupt mask register von PIC 2
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: PIC::allow *
|
||||
*---------------------------------------------------------------------------*
|
||||
@ -38,8 +36,20 @@ void PIC::allow (int irq) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
}
|
||||
// NOTE: allow sets the bit to 0
|
||||
|
||||
unsigned char IMR;
|
||||
unsigned char mask = ~(0x1 << (irq % 8));
|
||||
if (irq < 8) {
|
||||
// PIC 1
|
||||
IMR = IMR1.inb(); // We don't want to change the other interrupt masks so use this as start value
|
||||
IMR1.outb(IMR & mask);
|
||||
} else {
|
||||
// PIC 2
|
||||
IMR = IMR2.inb();
|
||||
IMR2.outb(IMR & mask);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: PIC::forbid *
|
||||
@ -53,8 +63,20 @@ void PIC::forbid (int irq) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
}
|
||||
// NOTE: forbid sets the bit to 1
|
||||
|
||||
unsigned char IMR;
|
||||
unsigned char mask = 0x1 << (irq % 8);
|
||||
if (irq < 8) {
|
||||
// PIC 1
|
||||
IMR = IMR1.inb(); // We don't want to change the other interrupt masks so use this as start value
|
||||
IMR1.outb(IMR | mask);
|
||||
} else {
|
||||
// PIC 2
|
||||
IMR = IMR2.inb();
|
||||
IMR2.outb(IMR | mask);
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Methode: PIC::status *
|
||||
@ -69,5 +91,19 @@ bool PIC::status (int irq) {
|
||||
|
||||
/* hier muss Code eingefuegt werden */
|
||||
|
||||
// TODO: How is IRQ2 handled (Slave PIC)?
|
||||
// Does masking IRQ2 disable the whole slave?
|
||||
|
||||
unsigned char IMR;
|
||||
if (irq < 8) {
|
||||
// PIC 1
|
||||
IMR = IMR1.inb();
|
||||
} else {
|
||||
// PIC 2
|
||||
IMR = IMR2.inb();
|
||||
}
|
||||
|
||||
// Use % 8 to account for two PICs
|
||||
unsigned char mask = 0x1 << (irq % 8);
|
||||
return IMR & mask;
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@ class PIC {
|
||||
|
||||
private:
|
||||
PIC(const PIC& copy); // Verhindere Kopieren
|
||||
|
||||
public:
|
||||
PIC() {}
|
||||
|
||||
|
||||
30
c_os/main.cc
30
c_os/main.cc
@ -15,27 +15,6 @@
|
||||
#include "user/HeapDemo.h"
|
||||
#include "user/KeyIRQDemo.h"
|
||||
|
||||
// Waits for keys to control the scrollback buffer display
|
||||
void scroll_mode() {
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
|
||||
Key key;
|
||||
while (true) {
|
||||
key = kb.key_hit();
|
||||
|
||||
switch ((char)key) {
|
||||
case 'k':
|
||||
kout.scroll_page_backward();
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
break;
|
||||
case 'j':
|
||||
kout.scroll_page_forward();
|
||||
kout.show(kout.COLUMNS - 1, 0, (char)(48 + kout.current_page));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
kout.clear();
|
||||
|
||||
@ -43,7 +22,7 @@ int main() {
|
||||
allocator.init();
|
||||
|
||||
// Initialize scrollback buffer after allocator.init()
|
||||
kout.init();
|
||||
kout.init(5);
|
||||
|
||||
// text_demo();
|
||||
// sound_demo();
|
||||
@ -52,14 +31,13 @@ int main() {
|
||||
|
||||
// Tastatur-Unterbrechungsroutine 'einstoepseln'
|
||||
/* hier muss Code eingefuegt werden */
|
||||
kb.plugin(intdis, pic);
|
||||
|
||||
// Interrupts erlauben (Tastatur)
|
||||
/* hier muss Code eingefuegt werden */
|
||||
cpu.enable_int();
|
||||
|
||||
key_irq_demo();
|
||||
|
||||
// TODO: Use interrupts
|
||||
// scroll_mode();
|
||||
// key_irq_demo();
|
||||
|
||||
while (1) {};
|
||||
return 0;
|
||||
|
||||
@ -10,9 +10,15 @@
|
||||
|
||||
#include "kernel/Globals.h"
|
||||
|
||||
|
||||
void key_irq_demo() {
|
||||
|
||||
/* Hier muss Code eingefuegt werden */
|
||||
|
||||
// TODO: I don't understand the task
|
||||
|
||||
while (true) {
|
||||
for (unsigned char i = 0; i < 10; ++i) {
|
||||
kout.show(kout.COLUMNS - 1, i, (char)(48 + i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user