about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYour Name <you@example.com>2020-06-24 02:13:47 +0200
committerYour Name <you@example.com>2020-06-24 02:13:47 +0200
commitd797ad6ad5df61533acf06882e5dc11669addbe8 (patch)
tree66785a43849645340654efaaf4c64037aadde174
parentCleanup: deleted unnecesary gdt struct (diff)
downloadpOS-d797ad6ad5df61533acf06882e5dc11669addbe8.tar.gz
pOS-d797ad6ad5df61533acf06882e5dc11669addbe8.tar.bz2
pOS-d797ad6ad5df61533acf06882e5dc11669addbe8.zip
Drivers: Added basic keyboard driver
-rw-r--r--src/pOS/arch/x86/kernel/drivers.cpp2
-rw-r--r--src/pOS/arch/x86/kernel/drivers/exampledriver.cpp27
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/KeyboardDriver.cpp32
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/include/kbbuf.h28
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/include/keyboard.h10
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes.h18
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes/US.cpp85
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/kbbuf.cpp100
-rw-r--r--src/pOS/arch/x86/kernel/drivers/keyboard/keyboard.cpp14
-rw-r--r--src/pOS/arch/x86/kernel/tty.cpp85
-rw-r--r--src/pOS/include/kernel/drivers.h4
-rw-r--r--src/pOS/include/kernel/kernel.h15
-rw-r--r--src/pOS/include/kernel/pOS.h15
-rw-r--r--src/pOS/include/kernel/tty.h5
-rw-r--r--src/pOS/kernel/kernel.cpp5
15 files changed, 382 insertions, 63 deletions
diff --git a/src/pOS/arch/x86/kernel/drivers.cpp b/src/pOS/arch/x86/kernel/drivers.cpp
index 865e6dc..08e5e6f 100644
--- a/src/pOS/arch/x86/kernel/drivers.cpp
+++ b/src/pOS/arch/x86/kernel/drivers.cpp
@@ -6,7 +6,7 @@ Driver* Drivers::loaded_drivers[1];
 void Drivers::add_drivers(void)
 {
     #ifdef DRIVER_EXAMPLE
-    ADD_DRIVER(ExampleDriver);
+    ADD_DRIVER(KeyboardDriver);
     #endif
 }
 
diff --git a/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp b/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp
deleted file mode 100644
index 1eaf04e..0000000
--- a/src/pOS/arch/x86/kernel/drivers/exampledriver.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#include <kernel/drivers.h>
-
-static bool loaded = false;
-
-int ExampleDriver::load(void)
-{
-    dbgprintf("loaded example\n");
-    loaded = true;
-    return 0;
-}
-
-int ExampleDriver::unload(void)
-{
-    dbgprintf("unloaded example\n");
-    loaded = false;
-    return 0;
-}
-
-bool ExampleDriver::isloaded(void)
-{
-    return loaded;
-}
-
-const char* ExampleDriver::get_name(void)
-{
-    return "Example";
-}
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/KeyboardDriver.cpp b/src/pOS/arch/x86/kernel/drivers/keyboard/KeyboardDriver.cpp
new file mode 100644
index 0000000..41f64eb
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/KeyboardDriver.cpp
@@ -0,0 +1,32 @@
+#include <kernel/drivers.h>
+#include "include/keyboard.h"
+
+static bool loaded = false;
+
+int KeyboardDriver::load(void)
+{
+    KB_BUF::init();
+    IRQ::install_handler(1, keyboard_handler);
+
+    dbgprintf("loaded keyboard\n");
+    loaded = true;
+
+    return 0;
+}
+
+int KeyboardDriver::unload(void)
+{
+    dbgprintf("unloaded keyboard\n");
+    loaded = false;
+    return 0;
+}
+
+bool KeyboardDriver::isloaded(void)
+{
+    return loaded;
+}
+
+const char* KeyboardDriver::get_name(void)
+{
+    return "Keyboard";
+}
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/include/kbbuf.h b/src/pOS/arch/x86/kernel/drivers/keyboard/include/kbbuf.h
new file mode 100644
index 0000000..f2e7a91
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/include/kbbuf.h
@@ -0,0 +1,28 @@
+#ifndef _KBBUF_H_
+#define _KBBUF_H_
+
+#include <stdbool.h>
+#include <kernel/tty.h>
+#include "scancodes.h"
+
+#define KEYBUFSIZ 128
+
+struct keyboard_buffer
+{
+  unsigned char buf[KEYBUFSIZ];
+  unsigned char *head;
+  unsigned char *tail;
+};
+
+class KB_BUF
+{
+public:
+    static int init(void);
+    static int scan(unsigned char code);
+    static void keyboard_enqueue(unsigned char ascii);
+    static unsigned char keyboard_dequeue(void);
+private:
+    static keyboard_buffer keyboard_buf;
+};
+
+#endif
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/include/keyboard.h b/src/pOS/arch/x86/kernel/drivers/keyboard/include/keyboard.h
new file mode 100644
index 0000000..8b0aeff
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/include/keyboard.h
@@ -0,0 +1,10 @@
+#ifndef _KEYBOARD_H_
+#define _KEYBOARD_H_
+
+#include <stdio.h>
+#include <kernel/system.h>
+#include "kbbuf.h"
+
+void keyboard_handler(struct regs *r);
+
+#endif
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes.h b/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes.h
new file mode 100644
index 0000000..1555c32
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes.h
@@ -0,0 +1,18 @@
+#ifndef _SCANCODES_H_
+#define _SCANCODES_H_
+
+extern unsigned char scancode[128];
+extern unsigned char shift_scancode[128];
+
+#define ESC 1
+#define BACKSPACE 14
+#define UP_ARROW 72
+#define LEFT_ARROW 75
+#define RIGHT_ARROW 77
+#define DOWN_ARROW 80
+#define LSHIFT_PRESS 42
+#define RSHIFT_PRESS 54
+#define LSHIFT_RELEASE 170
+#define RSHIFT_RELEASE 182
+
+#endif
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes/US.cpp b/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes/US.cpp
new file mode 100644
index 0000000..e9dff1c
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/include/scancodes/US.cpp
@@ -0,0 +1,85 @@
+#include <kernel/pOS.h>
+
+#ifdef KB_US
+
+unsigned char scancode[128] =
+{
+        0,  27, '1', '2', '3', '4', '5', '6', '7', '8',
+        '9', '0', '-', '=', '\b',	/* Backspace */
+        '\t',	/* Tab */
+        'q', 'w', 'e', 'r',
+        't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
+        0, /* Control */
+        'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
+        '\'', '`',   0, /* Left shift */
+        '\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
+        'm', ',', '.', '/',   0, /* Right shift */
+        '*',
+        0,	/* Alt */
+        ' ',	/* Space */
+        0,	/* Caps lock */
+        0,	/* F1 key ... > */
+        0,   0,   0,   0,   0,   0,   0,   0,
+        0,	/* < ... 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,	/* F11 Key */
+        0,	/* F12 Key */
+        0,	/* All others undefined */
+};
+
+unsigned char shift_scancode[128] =
+{
+      0,  27, '!', '@', '#', '$', '%', '^', '&', '*',
+      '(', ')', '_', '+', '\b',	/* Backspace */
+      '\t', /* Tab */
+      'Q', 'W', 'E', 'R',
+      'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', /* Enter key */
+      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 */
+      ' ',	/* Space */
+      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 others undefined */
+};
+
+#endif
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/kbbuf.cpp b/src/pOS/arch/x86/kernel/drivers/keyboard/kbbuf.cpp
new file mode 100644
index 0000000..f178adf
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/kbbuf.cpp
@@ -0,0 +1,100 @@
+#include "include/kbbuf.h"
+
+keyboard_buffer KB_BUF::keyboard_buf;
+
+int KB_BUF::init(void)
+{
+  keyboard_buf.head = keyboard_buf.buf;
+  keyboard_buf.tail = keyboard_buf.buf;
+
+  return 0;
+}
+
+int KB_BUF::scan(unsigned char code)
+{
+  static bool shifted = 0;
+  int result = 1;
+
+  switch(code)
+  {
+    case ESC:
+        break;
+
+    case BACKSPACE:
+        TTY::tty_delete(1);
+        break;
+
+    case UP_ARROW:
+        if(shifted)
+            TTY::tty_scroll(+1);
+        else
+            TTY::tty_cursor_move(0, -1);
+        break;
+
+    case LEFT_ARROW:
+        TTY::tty_cursor_move(-1, 0);
+        break;
+
+    case RIGHT_ARROW:
+        TTY::tty_cursor_move(1, 0);
+        break;
+
+    case DOWN_ARROW:
+        if(shifted)
+            TTY::tty_scroll(-1);
+        else
+            TTY::tty_cursor_move(0, +1);
+        break;
+
+    case LSHIFT_PRESS:
+    case RSHIFT_PRESS:
+        shifted = true;
+        break;
+
+    case LSHIFT_RELEASE:
+    case RSHIFT_RELEASE:
+        shifted = false;
+        break;
+
+    default:
+      if(!(code & 0x80))
+      {
+        if(shifted)
+            keyboard_enqueue(shift_scancode[code]);
+        else
+            keyboard_enqueue(scancode[code]);
+
+        result = 0;
+      }
+      break;
+  }
+
+  return result;
+}
+
+
+unsigned char KB_BUF::keyboard_dequeue(void)
+{
+  unsigned char c;
+  if(keyboard_buf.tail == keyboard_buf.head)
+    return 0;
+  else
+  {
+    c = *keyboard_buf.tail;
+    keyboard_buf.tail = keyboard_buf.buf + ((keyboard_buf.tail + 1 - keyboard_buf.buf) % KEYBUFSIZ);
+    return c;
+  }
+}
+
+
+void KB_BUF::keyboard_enqueue(unsigned char ascii)
+{
+  /* KB Buf not full */
+  if(((keyboard_buf.head + 1 - keyboard_buf.buf) % KEYBUFSIZ) !=
+     ((keyboard_buf.tail - keyboard_buf.buf) % KEYBUFSIZ))
+  {
+    *keyboard_buf.head = ascii;
+    keyboard_buf.head = keyboard_buf.buf +
+      ((keyboard_buf.head + 1 - keyboard_buf.buf) % KEYBUFSIZ);
+  }
+}
diff --git a/src/pOS/arch/x86/kernel/drivers/keyboard/keyboard.cpp b/src/pOS/arch/x86/kernel/drivers/keyboard/keyboard.cpp
new file mode 100644
index 0000000..c32cbdb
--- /dev/null
+++ b/src/pOS/arch/x86/kernel/drivers/keyboard/keyboard.cpp
@@ -0,0 +1,14 @@
+#include "include/keyboard.h"
+
+void keyboard_handler(struct regs *r)
+{
+    UNUSED_VARIABLE(r);
+
+    /* Read from the keyboard's data buffer */
+    unsigned char scancode = System::inb(0x60);
+
+    //dbgprintf("Sc: %d\n", scancode);
+
+    if(!KB_BUF::scan(scancode))
+        putc(KB_BUF::keyboard_dequeue());
+}
diff --git a/src/pOS/arch/x86/kernel/tty.cpp b/src/pOS/arch/x86/kernel/tty.cpp
index daada0f..13c9d4f 100644
--- a/src/pOS/arch/x86/kernel/tty.cpp
+++ b/src/pOS/arch/x86/kernel/tty.cpp
@@ -4,7 +4,7 @@ size_t TTY::tty_y;
 size_t TTY::tty_x;
 uint8_t TTY::tty_color;
 uint16_t* TTY::tty_buf;
-uint16_t TTY::tty_map[VGA::VGA::VGA_WIDTH][VGA::VGA::VGA_HEIGHT];
+uint16_t TTY::tty_map[VGA::VGA_WIDTH][VGA::VGA_HEIGHT];
 
 int TTY::tty_cursor_updt(void)
 {
@@ -18,9 +18,26 @@ int TTY::tty_cursor_updt(void)
      return 0;
 }
 
-int TTY::tty_scroll(void)
+int TTY::tty_cursor_move(int x, int y)
 {
-    if(tty_y + 1 >= VGA::VGA::VGA_HEIGHT)
+    tty_x += x;
+    tty_y += y;
+
+    if(tty_x >= VGA::VGA_WIDTH)
+        tty_x = VGA::VGA_WIDTH - 1;
+
+    if(tty_y >= VGA::VGA_HEIGHT)
+        tty_y = VGA::VGA_HEIGHT - 1;
+
+    tty_update();
+
+    return 0;
+}
+
+int TTY::tty_scroll(int up_or_down)
+{
+    /* TODO: +1 scroll up, -1 scroll down (mandatory) */
+    if(up_or_down == 0 && tty_y + 1 >= VGA::VGA::VGA_HEIGHT)
     {
         /* move everything up */
         for(size_t x = 0; x < VGA::VGA::VGA_WIDTH; x++)
@@ -45,14 +62,25 @@ int TTY::tty_update(void) /* NOT EFFICIENT */
             tty_buf[y * VGA::VGA::VGA_WIDTH + x] = tty_map[x][y];
 
     tty_cursor_updt();
-    tty_scroll();
+    tty_scroll(0);
 
     return 0;
 }
 
 int TTY::tty_putentryat(unsigned char c, uint8_t color, size_t x, size_t y)
 {
-    tty_map[x][y] = VGA::vga_entry(c, color);
+    if(c == '\n')
+    {
+        tty_x = -1;
+        tty_y++;
+    }
+    else if(c == '\t')
+    {
+        tty_x += TAB_INDENTATION;
+    } //add \r, delete etc
+    else
+        tty_map[x][y] = VGA::vga_entry(c, color);
+
     if(++tty_x >= VGA::VGA::VGA_WIDTH)
     {
         tty_x = tty_x - VGA::VGA::VGA_WIDTH;
@@ -68,10 +96,10 @@ int TTY::tty_initialize(void)
     tty_y = 0;
     tty_x = 0;
     tty_color = VGA::vga_entry_color(VGA::VGA_COLOR_GREEN, VGA::VGA_COLOR_BLACK);
-    tty_buf = (uint16_t*)VGA::VGA_MEMORY;
-    for (size_t y = 0; y < VGA::VGA::VGA_HEIGHT; y++)
-        for (size_t x = 0; x < VGA::VGA::VGA_WIDTH; x++)
-            tty_map[x][y] = VGA::vga_entry(' ', tty_color);
+    tty_buf = static_cast<uint16_t*>(VGA::VGA_MEMORY);
+    for (size_t y = 0; y < VGA::VGA_HEIGHT; y++)
+        for (size_t x = 0; x < VGA::VGA_WIDTH; x++)
+            tty_map[x][y] = VGA::vga_entry(' ', tty_color); //IMPLEMENT ANOTHER CHAR FOR EMPTY STAFF SO THAT WHEN WE DELETE IT DOESNT STOP AFTER JUST 1
 
     tty_update();
 
@@ -86,19 +114,38 @@ int TTY::tty_setcolor(uint8_t color)
 
 int TTY::tty_putc(char c)
 {
-    if(c == '\n')
+    tty_putentryat((unsigned char)c, tty_color, tty_x, tty_y);
+
+    return 0;
+}
+
+int TTY::tty_delete(unsigned int num)
+{
+    int newx = tty_x;
+    int newy = tty_y;
+
+    newx--;
+    if(newx < 0)
     {
-        tty_x = 0;
-        tty_y++;
-        return 0;
+        newx = VGA::VGA_WIDTH - 1;
+        newy--;
+        if(newy < 0)
+        {
+            newy = 0;
+            newx = 0;
+        }
     }
-    else if(c == '\t')
-    {
-        tty_x += 4;
-        return 0;
-    } //add \r
 
-    tty_putentryat((unsigned char)c, tty_color, tty_x, tty_y);
+    tty_x = newx;
+    tty_y = newy;
+
+    tty_map[tty_x][tty_y] = VGA::vga_entry(' ', tty_color);
+
+    num--;
+    if(num > 0)
+        tty_delete(num);
+
+    tty_update();
 
     return 0;
 }
diff --git a/src/pOS/include/kernel/drivers.h b/src/pOS/include/kernel/drivers.h
index 4fe3588..d7d0d5f 100644
--- a/src/pOS/include/kernel/drivers.h
+++ b/src/pOS/include/kernel/drivers.h
@@ -2,7 +2,7 @@
 #define _DRIVER_H_
 
 #include <kernel/debug.h>
-#include <kernel/pOS.h>
+#include <kernel/interrupts/irq.h>
 
 class Driver
 {
@@ -28,7 +28,7 @@ private:
 
 /* ALL DRIVERS */
 
-class ExampleDriver : public Driver
+class KeyboardDriver : public Driver
 {
 public:
     int load(void);
diff --git a/src/pOS/include/kernel/kernel.h b/src/pOS/include/kernel/kernel.h
new file mode 100644
index 0000000..7362374
--- /dev/null
+++ b/src/pOS/include/kernel/kernel.h
@@ -0,0 +1,15 @@
+#ifndef _KERNEL_H_
+#define _KERNEL_H_
+
+#include <stdio.h>
+#include <math.h>
+#include <assert.h>
+
+#include <kernel/time.h>
+#include <kernel/debug.h>
+#include <kernel/drivers.h>
+#include <kernel/interrupts/gdt.h>
+#include <kernel/interrupts/idt.h>
+#include <kernel/interrupts/isrs.h>
+
+#endif
diff --git a/src/pOS/include/kernel/pOS.h b/src/pOS/include/kernel/pOS.h
index f26b739..5391765 100644
--- a/src/pOS/include/kernel/pOS.h
+++ b/src/pOS/include/kernel/pOS.h
@@ -1,20 +1,13 @@
 #ifndef _POS_H_
 #define _POS_H_
 
-#include <stdio.h>
-#include <math.h>
-#include <assert.h>
-
-#include <kernel/time.h>
-#include <kernel/debug.h>
-#include <kernel/drivers.h>
-#include <kernel/interrupts/gdt.h>
-#include <kernel/interrupts/idt.h>
-#include <kernel/interrupts/isrs.h>
-
 #define DEBUG
 #define VERSION "meme"
 
+#define KB_US
+
+#define TAB_INDENTATION 4
+
 //#define TIME_FORMAT_12H
 
 #define DRIVER_EXAMPLE
diff --git a/src/pOS/include/kernel/tty.h b/src/pOS/include/kernel/tty.h
index 23b46dc..ab0a900 100644
--- a/src/pOS/include/kernel/tty.h
+++ b/src/pOS/include/kernel/tty.h
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <kernel/vga.h>
 #include <kernel/system.h>
+#include <kernel/pOS.h>
 
 class TTY
 {
@@ -11,11 +12,13 @@ public:
     static int tty_initialize(void);
     static int tty_setcolor(uint8_t color);
     static int tty_putc(char c);
+    static int tty_delete(unsigned int num);
     static int tty_write(const char* data, size_t size);
+    static int tty_cursor_move(int x, int y);
+    static int tty_scroll(int up_or_down);
 private:
     static int tty_putentryat(unsigned char c, uint8_t color, size_t x, size_t y);
     static int tty_update(void);
-    static int tty_scroll(void);
     static int tty_cursor_updt(void);
 
     static size_t tty_y;
diff --git a/src/pOS/kernel/kernel.cpp b/src/pOS/kernel/kernel.cpp
index c63bab0..4b76b81 100644
--- a/src/pOS/kernel/kernel.cpp
+++ b/src/pOS/kernel/kernel.cpp
@@ -1,4 +1,4 @@
-#include <kernel/pOS.h>
+#include <kernel/kernel.h>
 
 extern "C" int kmain(void)
 {
@@ -6,11 +6,12 @@ extern "C" int kmain(void)
     Debug::set_serial_debug(true);
     #endif
 
+    TTY::tty_initialize();
+
     GDT::init();
     IDT::init();
     ISRS::install();
 
-    TTY::tty_initialize();
     Drivers::load_drivers();
 
     /*asm volatile("mov %eax, 0x100  \t\n"