From 5c33c6bebf2dd2be4d77885f8fc024ffcc5fc866 Mon Sep 17 00:00:00 2001 From: Fergal Moran Date: Mon, 8 Jun 2020 21:20:38 +0100 Subject: [PATCH] Mouse handling working --- Makefile | 4 +- include/interrupts.h | 1 + include/mouse.h | 10 +- include/stdio.h | 1 + src/interrupts.cpp | 1 + src/interruptstubs.s | 1 + src/kernel.cpp | 8 +- src/mouse.cpp | 215 +++++++++++++------------------------------ src/stdio.cpp | 15 ++- 9 files changed, 92 insertions(+), 164 deletions(-) diff --git a/Makefile b/Makefile index 4bcb8ab..db8c372 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -GPPPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore +GPPPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wall -Wextra ASPARAMS = --32 LDPARAMS = -melf_i386 -objs = stdio.o loader.o gdt.o port.o interruptstubs.o interrupts.o keyboard.o kernel.o +objs = stdio.o loader.o gdt.o port.o interruptstubs.o interrupts.o keyboard.o mouse.o kernel.o %.o: src/%.cpp g++ $(GPPPARAMS) -o $@ -c $< diff --git a/include/interrupts.h b/include/interrupts.h index 1ec2c76..49fcec8 100644 --- a/include/interrupts.h +++ b/include/interrupts.h @@ -67,5 +67,6 @@ class InterruptManager { static void IgnoreInterruptRequest(); static void HandleInterruptRequest0x00(); static void HandleInterruptRequest0x01(); + static void HandleInterruptRequest0x0C(); }; #endif \ No newline at end of file diff --git a/include/mouse.h b/include/mouse.h index 7b86a76..37b0096 100644 --- a/include/mouse.h +++ b/include/mouse.h @@ -1,5 +1,5 @@ -#ifndef __KEYBOARD_H -#define __KEYBOARD_H +#ifndef __MOUSE_H +#define __MOUSE_H #include "interrupts.h" #include "port.h" @@ -10,6 +10,12 @@ class MouseDriver : public InterruptHandler { Port8Bit _dataport; Port8Bit _commandport; + uint8_t _buffer[3]; + uint8_t _offset; + uint8_t _buttons; + + int8_t _screenX, _screenY; + public: MouseDriver(InterruptManager* manager); ~MouseDriver(); diff --git a/include/stdio.h b/include/stdio.h index 918edee..6f71a7a 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -2,5 +2,6 @@ #define __STDIO_H void printf(const char* str); +void clear(); #endif \ No newline at end of file diff --git a/src/interrupts.cpp b/src/interrupts.cpp index 114b9ed..766b53a 100644 --- a/src/interrupts.cpp +++ b/src/interrupts.cpp @@ -33,6 +33,7 @@ InterruptManager::InterruptManager(GlobalDescriptorTable* gdt) // see HandleInterruptRequest in interrupts.s SetInterruptDescriptorTableEntries(0x20, codeSegment, &HandleInterruptRequest0x00, 0, IDT_INTERRUPT_GATE); SetInterruptDescriptorTableEntries(0x21, codeSegment, &HandleInterruptRequest0x01, 0, IDT_INTERRUPT_GATE); + SetInterruptDescriptorTableEntries(0x2C, codeSegment, &HandleInterruptRequest0x0C, 0, IDT_INTERRUPT_GATE); _picPrimaryCommand.Write(0x11); _picSecondaryCommand.Write(0x11); diff --git a/src/interruptstubs.s b/src/interruptstubs.s index d70048e..0f81d9f 100644 --- a/src/interruptstubs.s +++ b/src/interruptstubs.s @@ -22,6 +22,7 @@ _ZN16InterruptManager26HandleInterruptRequest\num\()Ev: HandleInterruptRequest 0x00 HandleInterruptRequest 0x01 +HandleInterruptRequest 0x0C int_bottom: pusha diff --git a/src/kernel.cpp b/src/kernel.cpp index 236566e..81ad120 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -9,6 +9,7 @@ #include "../include/gdt.h" #include "../include/interrupts.h" #include "../include/keyboard.h" +#include "../include/mouse.h" #include "../include/stdio.h" #include "../include/types.h" @@ -20,12 +21,15 @@ extern "C" void call_constructors() { (*i)(); } -extern "C" void ferglos_Main(const void* multiboot_structure, uint32_t /*mb_mag*/) { - printf("Welcome to FerglOS v0.0.2!\n\n"); +extern "C" void ferglos_Main(const void* /*multiboot_structure*/, uint32_t /*mb_mag*/) { + clear(); + printf("FerglOS v0.0.2!\n"); + printf("Floats like a lepidoptera, stings like a hymenoptera\n"); GlobalDescriptorTable gdt; InterruptManager interrupts(&gdt); KeyboardDriver keyboard(&interrupts); + MouseDriver mouse(&interrupts); interrupts.Activate(); diff --git a/src/mouse.cpp b/src/mouse.cpp index 656a77b..f54312b 100644 --- a/src/mouse.cpp +++ b/src/mouse.cpp @@ -1,167 +1,76 @@ -#include "../include/keyboard.h" +#include "../include/mouse.h" + #include "../include/stdio.h" -KeyboardDriver::KeyboardDriver(InterruptManager* manager) : InterruptHandler(0x21, manager), - _dataport(0x60), - _commandport(0x64) { - while (_commandport.Read() & 0x01) { - _dataport.Read(); - } - _commandport.Write(0xAE); +MouseDriver::MouseDriver(InterruptManager* manager) : InterruptHandler(0x2C, manager), + _dataport(0x60), + _commandport(0x64) { + _offset = 0; + _buttons = 0; + + static uint16_t* VideoMemory = (uint16_t*)0xb8000; + //flip bit in center of screen - so mousepointer is initially here + _screenX = 40; + _screenY = 12; + + VideoMemory[80 * _screenY + _screenX] = + (VideoMemory[80 * _screenY + _screenX] & 0x0F00) << 4 | + (VideoMemory[80 * _screenY + _screenX] & 0xF000) >> 4 | + (VideoMemory[80 * _screenY + _screenX] & 0x00FF); + + _commandport.Write(0xA8); _commandport.Write(0x20); - uint8_t status = (_dataport.Read() | 1) & ~0x10; + uint8_t status = _dataport.Read() | 2; _commandport.Write(0x60); _dataport.Write(status); + _commandport.Write(0xD4); _dataport.Write(0xF4); + _dataport.Read(); } -KeyboardDriver::~KeyboardDriver() {} +MouseDriver::~MouseDriver() {} -uint32_t KeyboardDriver::HandleInterrupt(uint32_t esp) { - uint8_t key = _dataport.Read(); - if (key < 0x80) { //ignore key up events - switch (key) { - case 0xFA: - case 0x45: - case 0xC5: - break; - 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; +uint32_t MouseDriver::HandleInterrupt(uint32_t esp) { + uint8_t status = _commandport.Read(); - 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("y"); - 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("z"); - 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) & 0x0F]; - foo[12] = hex[key & 0x0F]; - printf(foo); - printf("\n"); - break; - } + if (!(status & 0x20)) { + return esp; } + _buffer[_offset] = _dataport.Read(); + _offset = (_offset + 1) % 3; + + //mouse position + if (_offset == 0) { + static uint16_t* VideoMemory = (uint16_t*)0xb8000; + + //flip bit on screen where mouse cursor is + VideoMemory[80 * _screenY + _screenX] = + ((VideoMemory[80 * _screenY + _screenX] & 0xF000) >> 4) | + ((VideoMemory[80 * _screenY + _screenX] & 0x0F00) << 4) | + ((VideoMemory[80 * _screenY + _screenX] & 0x00FF)); + + _screenX += _buffer[1]; + _screenY -= _buffer[2]; + + // check for mouse moving off screen + if (_screenX < 0) { + _screenX = 0; + } + if (_screenX >= 80) { + _screenX = 79; + } + if (_screenY < 0) { + _screenY = 0; + } + if (_screenY >= 25) { + _screenY = 24; + } + + VideoMemory[80 * _screenY + _screenX] = + ((VideoMemory[80 * _screenY + _screenX] & 0xF000) >> 4) | + ((VideoMemory[80 * _screenY + _screenX] & 0x0F00) << 4) | + ((VideoMemory[80 * _screenY + _screenX] & 0x00FF)); + } return esp; } \ No newline at end of file diff --git a/src/stdio.cpp b/src/stdio.cpp index 8ea1f41..0fd2eab 100644 --- a/src/stdio.cpp +++ b/src/stdio.cpp @@ -25,11 +25,16 @@ void printf(const char* str) { } //we've reached max screen rows if (y > 25) { - for (y = 0; y < 25; y++) { - for (x - 0; x < 80; x++) { - VideoMemory[80 * y + x] = (VideoMemory[80 * y + x] & 0xFF00) | ' '; - } - } + clear(); } } } +void clear() { + static uint16_t* VideoMemory = (uint16_t*)0xb8000; + static uint8_t x = 0, y = 0; + for (y = 0; y < 25; y++) { + for (x = 0; x < 80; x++) { + VideoMemory[80 * y + x] = (VideoMemory[80 * y + x] & 0xFF00) | ' '; + } + } +} \ No newline at end of file