diff --git a/.gitignore b/.gitignore index 72125fd..9106370 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ *.bin vm/ *.iso +obj diff --git a/Makefile b/Makefile index a46f2e4..d972bca 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,26 @@ -GPPPARAMS = -m32 -fno-use-cxa-atexit -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -Wall -Wextra +GCCPARAMS = -m32 -Iinclude -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 driver.o port.o interruptstubs.o interrupts.o keyboard.o echokeyboardhandler.o blockmousehandler.o mouse.o kernel.o +objs = obj/common/stdio.o \ + obj/loader.o \ + obj/gdt.o \ + obj/drivers/driver.o \ + obj/hardware/port.o \ + obj/hardware/interrupts.o \ + obj/drivers/keyboard.o \ + obj/drivers/echokeyboardhandler.o \ + obj/drivers/blockmousehandler.o \ + obj/drivers/mouse.o \ + obj/interruptstubs.o \ + obj/kernel.o -%.o: src/%.cpp - g++ $(GPPPARAMS) -o $@ -c $< +obj/%.o: src/%.cpp + mkdir -p $(@D) + gcc $(GCCPARAMS) -c -o $@ $< -%.o: src/%.s +obj/%.o: src/%.s + mkdir -p $(@D) as $(ASPARAMS) -o $@ $< ferglos.bin: linker.ld $(objs) @@ -34,5 +47,6 @@ run: ferglos.iso run-qemu: ferglos.iso qemu-system-i386 -boot d -cdrom ferglos.iso -m 512 +.PHONY: clean clean: - rm -rfv $(objects) iso *.iso *.o *.bin + rm -rfv obj iso *.iso *.o *.bin diff --git a/include/blockmousehandler.h b/include/blockmousehandler.h deleted file mode 100644 index 9db4078..0000000 --- a/include/blockmousehandler.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __BLOCKMOUSEHANDLER_H -#define __BLOCKMOUSEHANDLER_H -#include "mouse.h" -#include "stdio.h" - -class BlockMouseHandler : public MouseEventHandler { - private: - int8_t _screenX, _screenY; - - public: - BlockMouseHandler(); - virtual void OnMouseMove(int xoffset, int yoffset); -}; -#endif \ No newline at end of file diff --git a/include/common/stdio.h b/include/common/stdio.h index bf68f1f..54e8e7a 100644 --- a/include/common/stdio.h +++ b/include/common/stdio.h @@ -1,10 +1,12 @@ -#ifndef __STDIO_H -#define __STDIO_H -#include "types.h" - +#ifndef __FERGLOS_COMMON_STDIO_H +#define __FERGLOS_COMMON_STDIO_H +#include +namespace ferglos { +namespace common { void printf(const char* str); void wprintf(const wchar_t* str); void printfHex(uint8_t key); void clear(); - +} // namespace common +} // namespace ferglos #endif \ No newline at end of file diff --git a/include/common/types.h b/include/common/types.h index 0f53c93..6bea9f1 100644 --- a/include/common/types.h +++ b/include/common/types.h @@ -1,14 +1,7 @@ -/* - * types.h - * Author: Fergal Moran - * Copyright: 2020 Fergal Moran - * - * BSD License - do what you want - */ - -#ifndef __TYPES_H -#define __TYPES_H - +#ifndef __FERGLOS_COMMON_TYPES_H +#define __FERGLOS_COMMON_TYPES_H +namespace ferglos { +namespace common { typedef char int8_t; typedef unsigned char uint8_t; typedef short int16_t; @@ -17,5 +10,6 @@ typedef int int32_t; typedef unsigned int uint32_t; typedef long long int int64_t; typedef unsigned long long int uint64_t; - -#endif \ No newline at end of file +} // namespace common +} // namespace ferglos +#endif diff --git a/include/drivers/blockmousehandler.h b/include/drivers/blockmousehandler.h new file mode 100644 index 0000000..f110ecb --- /dev/null +++ b/include/drivers/blockmousehandler.h @@ -0,0 +1,20 @@ +#ifndef __FERGLOS_DRIVERS_BLOCKMOUSEHANDLER_H +#define __FERGLOS_DRIVERS_BLOCKMOUSEHANDLER_H +#include +#include + +using namespace ferglos::common; + +namespace ferglos { +namespace drivers { +class BlockMouseHandler : public MouseEventHandler { + private: + int8_t _screenX, _screenY; + + public: + BlockMouseHandler(); + virtual void OnMouseMove(int xoffset, int yoffset); +}; +} // namespace drivers +} // namespace ferglos +#endif \ No newline at end of file diff --git a/include/driver.h b/include/drivers/driver.h similarity index 72% rename from include/driver.h rename to include/drivers/driver.h index 4a1c21a..cda653c 100644 --- a/include/driver.h +++ b/include/drivers/driver.h @@ -1,5 +1,7 @@ -#ifndef __DRIVER_H -#define __DRIVER_H +#ifndef __FERGLOS_DRIVERS_DRIVER_H +#define __FERGLOS_DRIVERS_DRIVER_H +namespace ferglos { +namespace drivers { class Driver { public: Driver(); @@ -22,4 +24,6 @@ class DriverManager { void ActivateAll(); }; +} // namespace drivers +} // namespace ferglos #endif diff --git a/include/drivers/echokeyboardhandler.h b/include/drivers/echokeyboardhandler.h new file mode 100644 index 0000000..c0da1d0 --- /dev/null +++ b/include/drivers/echokeyboardhandler.h @@ -0,0 +1,15 @@ +#ifndef __FERGLOS_DRIVERS_ECHOKEYBOARDHANDLER_H +#define __FERGLOS_DRIVERS_ECHOKEYBOARDHANDLER_H +#include +#include + +namespace ferglos { +namespace drivers { + +class EchoKeyboardHandler : public KeyboardEventHandler { + public: + virtual void OnKeyDown(const wchar_t* key); +}; +} // namespace drivers +} // namespace ferglos +#endif \ No newline at end of file diff --git a/include/keyboard.h b/include/drivers/keyboard.h similarity index 59% rename from include/keyboard.h rename to include/drivers/keyboard.h index 079dfd7..7ab05d2 100644 --- a/include/keyboard.h +++ b/include/drivers/keyboard.h @@ -1,10 +1,16 @@ -#ifndef __KEYBOARD_H -#define __KEYBOARD_H +#ifndef __FERGLOS_DRIVERS_KEYBOARD_H +#define __FERGLOS_DRIVERS_KEYBOARD_H +#include +#include +#include +#include +#include -#include "driver.h" -#include "interrupts.h" -#include "port.h" -#include "types.h" +using namespace ferglos::common; +using namespace ferglos::hardware; + +namespace ferglos { +namespace drivers { class KeyboardEventHandler { public: @@ -27,4 +33,6 @@ class KeyboardDriver : public InterruptHandler, public Driver { virtual uint32_t HandleInterrupt(uint32_t esp); virtual void Activate(); }; +} // namespace drivers +} // namespace ferglos #endif \ No newline at end of file diff --git a/include/mouse.h b/include/drivers/mouse.h similarity index 66% rename from include/mouse.h rename to include/drivers/mouse.h index 8078f6e..7f8c65a 100644 --- a/include/mouse.h +++ b/include/drivers/mouse.h @@ -1,10 +1,15 @@ -#ifndef __MOUSE_H -#define __MOUSE_H +#ifndef __FERGLOS_DRIVERS_MOUSE_H +#define __FERGLOS_DRIVERS_MOUSE_H -#include "driver.h" -#include "interrupts.h" -#include "port.h" -#include "types.h" +#include +#include +#include +#include +using namespace ferglos::common; +using namespace ferglos::hardware; + +namespace ferglos { +namespace drivers { class MouseEventHandler { private: public: @@ -34,4 +39,6 @@ class MouseDriver : public InterruptHandler, public Driver { virtual uint32_t HandleInterrupt(uint32_t esp); virtual void Activate(); }; +} // namespace drivers +} // namespace ferglos #endif \ No newline at end of file diff --git a/include/echokeyboardhandler.h b/include/echokeyboardhandler.h deleted file mode 100644 index 3f7fcd8..0000000 --- a/include/echokeyboardhandler.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __ECHOKEYBOARDHANDLER_H -#define __ECHOKEYBOARDHANDLER_H -#include "keyboard.h" -#include "stdio.h" - -class EchoKeyboardHandler : public KeyboardEventHandler { - public: - virtual void OnKeyDown(const wchar_t* key); -}; -#endif \ No newline at end of file diff --git a/include/gdt.h b/include/gdt.h index 2a3393c..fbecbbe 100644 --- a/include/gdt.h +++ b/include/gdt.h @@ -1,15 +1,9 @@ -/* - * gdt.h - * Author: Fergal Moran - * Copyright: 2020 Fergal Moran - * - * BSD License - do what you want - */ - -#ifndef __GDT_H -#define __GDT_H -#include "types.h" +#ifndef __FERGLOS_GDT_H +#define __FERGLOS_GDT_H +#include +using namespace ferglos::common; +namespace ferglos { /* The Global Descriptor Table (GDT) is a data structure used by Intel x86-family processors starting with the 80286 in order to define the characteristics of the various memory areas @@ -50,5 +44,5 @@ class GlobalDescriptorTable { uint16_t CodeSegmentSelector(); uint16_t DataSegmentSelector(); }; - +} // namespace ferglos #endif \ No newline at end of file diff --git a/include/interrupts.h b/include/hardware/interrupts.h similarity index 82% rename from include/interrupts.h rename to include/hardware/interrupts.h index 49fcec8..df75782 100644 --- a/include/interrupts.h +++ b/include/hardware/interrupts.h @@ -1,9 +1,14 @@ -#ifndef __INTERRUPTS_H -#define __INTERRUPTS_H -#include "gdt.h" -#include "port.h" -#include "types.h" +#ifndef __FERGLOS_HARDWARE_INTERRUPTS_H +#define __FERGLOS_HARDWARE_INTERRUPTS_H +#include +#include +#include + +using namespace ferglos::drivers; + +namespace ferglos { +namespace hardware { class InterruptManager; class InterruptHandler { protected: @@ -61,7 +66,7 @@ class InterruptManager { void Activate(); void Deactivate(); - static uint32_t HandleInterrupt(uint8_t interruptNumber, uint32_t esp); + static uint32_t HandleInterrupt(uint8_t interrupt, uint32_t esp); uint32_t DoHandleInterrupt(uint8_t interruptNumber, uint32_t esp); static void IgnoreInterruptRequest(); @@ -69,4 +74,6 @@ class InterruptManager { static void HandleInterruptRequest0x01(); static void HandleInterruptRequest0x0C(); }; +} // namespace hardware +} // namespace ferglos #endif \ No newline at end of file diff --git a/include/port.h b/include/hardware/port.h similarity index 78% rename from include/port.h rename to include/hardware/port.h index f88f639..435a1d7 100644 --- a/include/port.h +++ b/include/hardware/port.h @@ -1,14 +1,11 @@ -/* - * port.h - * Author: Fergal Moran - * Copyright: 2020 Fergal Moran - * - * BSD License - do what you want - */ -#ifndef __PORT_H -#define __PORT_H +#ifndef __FERGLOS_HARDWARE_PORT_H +#define __FERGLOS_HARDWARE_PORT_H -#include "types.h" +#include +using namespace ferglos::common; + +namespace ferglos { +namespace drivers { class Port { protected: @@ -51,5 +48,6 @@ class Port32Bit : public Port { virtual void Write(uint32_t data); virtual uint32_t Read(); }; - +} // namespace drivers +} // namespace ferglos #endif \ No newline at end of file diff --git a/src/common/stdio.cpp b/src/common/stdio.cpp index 6f11894..4df3bf6 100644 --- a/src/common/stdio.cpp +++ b/src/common/stdio.cpp @@ -1,6 +1,9 @@ -#include "../include/stdio.h" +#include +#include +using namespace ferglos::common; -#include "../include/types.h" +namespace ferglos { +namespace common { extern uint8_t x = 0, y = 0; void printf(const char* str) { @@ -68,4 +71,6 @@ void clear() { VideoMemory[80 * y + x] = (VideoMemory[80 * y + x] & 0xFF00) | ' '; } } -} \ No newline at end of file +} +} // namespace common +} // namespace ferglos \ No newline at end of file diff --git a/src/blockmousehandler.cpp b/src/drivers/blockmousehandler.cpp similarity index 91% rename from src/blockmousehandler.cpp rename to src/drivers/blockmousehandler.cpp index 2ccdba2..12fada8 100644 --- a/src/blockmousehandler.cpp +++ b/src/drivers/blockmousehandler.cpp @@ -1,7 +1,6 @@ -#include "../include/blockmousehandler.h" - -#include "stdio.h" - +#include +namespace ferglos { +namespace drivers { BlockMouseHandler::BlockMouseHandler() { //flip bit in center of screen - so mousepointer is initially here static uint16_t* VideoMemory = (uint16_t*)0xb8000; @@ -44,3 +43,5 @@ void BlockMouseHandler::OnMouseMove(int xoffset, int yoffset) { ((VideoMemory[80 * _screenY + _screenX] & 0x0F00) << 4) | ((VideoMemory[80 * _screenY + _screenX] & 0x00FF)); } +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/driver.cpp b/src/drivers/driver.cpp similarity index 79% rename from src/driver.cpp rename to src/drivers/driver.cpp index 2b3267c..3a91350 100644 --- a/src/driver.cpp +++ b/src/drivers/driver.cpp @@ -1,5 +1,6 @@ -#include "../include/driver.h" - +#include +namespace ferglos { +namespace drivers { Driver::Driver() {} Driver::~Driver() {} @@ -21,3 +22,5 @@ void DriverManager::ActivateAll() { _drivers[i]->Activate(); } } +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/echokeyboardhandler.cpp b/src/drivers/echokeyboardhandler.cpp similarity index 51% rename from src/echokeyboardhandler.cpp rename to src/drivers/echokeyboardhandler.cpp index 13745ab..3595356 100644 --- a/src/echokeyboardhandler.cpp +++ b/src/drivers/echokeyboardhandler.cpp @@ -1,7 +1,10 @@ -#include "../include/echokeyboardhandler.h" - +#include +namespace ferglos { +namespace drivers { void EchoKeyboardHandler::OnKeyDown(const wchar_t* key) { wchar_t* foo = (wchar_t*)" "; foo = (wchar_t*)key; wprintf(foo); } +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/keyboard.cpp b/src/drivers/keyboard.cpp similarity index 97% rename from src/keyboard.cpp rename to src/drivers/keyboard.cpp index decde2f..f2d408f 100644 --- a/src/keyboard.cpp +++ b/src/drivers/keyboard.cpp @@ -1,7 +1,7 @@ -#include "../include/keyboard.h" - -#include "../include/stdio.h" - +#include +#include +namespace ferglos { +namespace drivers { KeyboardEventHandler::KeyboardEventHandler() {} void KeyboardEventHandler::OnKeyDown(const wchar_t* /*key*/) {} @@ -189,4 +189,6 @@ uint32_t KeyboardDriver::HandleInterrupt(uint32_t esp) { } return esp; -} \ No newline at end of file +} +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/mouse.cpp b/src/drivers/mouse.cpp similarity index 91% rename from src/mouse.cpp rename to src/drivers/mouse.cpp index 28743ab..c65f75f 100644 --- a/src/mouse.cpp +++ b/src/drivers/mouse.cpp @@ -1,7 +1,7 @@ -#include "../include/mouse.h" - -#include "../include/stdio.h" - +#include +#include +namespace ferglos { +namespace drivers { MouseEventHandler::MouseEventHandler() {} void MouseEventHandler::OnActivate() {} @@ -64,4 +64,6 @@ uint32_t MouseDriver::HandleInterrupt(uint32_t esp) { _buttons = _buffer[0]; } return esp; -} \ No newline at end of file +} +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/gdt.cpp b/src/gdt.cpp index 40213b3..eed5c16 100644 --- a/src/gdt.cpp +++ b/src/gdt.cpp @@ -1,13 +1,6 @@ -/* - * gdt.cpp - * Author: Fergal Moran - * Copyright: 2020 Fergal Moran - * - * BSD License - do what you want - */ - -#include "../include/gdt.h" - +#include +using namespace ferglos::common; +namespace ferglos { GlobalDescriptorTable::GlobalDescriptorTable() : nullSegmentSelector(0, 0, 0), unusedSegmentSelector(0, 0, 0), codeSegmentSelector(0, 64 * 1024 * 1024, 0x9A), @@ -76,3 +69,4 @@ uint32_t GlobalDescriptorTable::SegmentDescriptor::Limit() { } return result; } +} \ No newline at end of file diff --git a/src/interrupts.cpp b/src/hardware/interrupts.cpp similarity index 94% rename from src/interrupts.cpp rename to src/hardware/interrupts.cpp index 628683f..44fc4bc 100644 --- a/src/interrupts.cpp +++ b/src/hardware/interrupts.cpp @@ -1,6 +1,8 @@ -#include "../include/interrupts.h" +#include +#include -#include "../include/stdio.h" +using namespace ferglos::common; +using namespace ferglos::hardware; InterruptHandler::InterruptHandler(uint8_t interruptNumber, InterruptManager* interruptManager) { this->_interruptNumber = interruptNumber; @@ -91,9 +93,9 @@ void InterruptManager::SetInterruptDescriptorTableEntries( _interruptDescriptorTable[interruptNumber].reserved = 0; } -uint32_t InterruptManager::HandleInterrupt(uint8_t interruptNumber, uint32_t esp) { +uint32_t InterruptManager::HandleInterrupt(uint8_t interrupt, uint32_t esp) { if (_activeInterruptManager != 0) { - return _activeInterruptManager->DoHandleInterrupt(interruptNumber, esp); + return _activeInterruptManager->DoHandleInterrupt(interrupt, esp); } return esp; } @@ -114,4 +116,4 @@ uint32_t InterruptManager::DoHandleInterrupt(uint8_t interruptNumber, uint32_t e } } return esp; -} +} \ No newline at end of file diff --git a/src/port.cpp b/src/hardware/port.cpp similarity index 91% rename from src/port.cpp rename to src/hardware/port.cpp index 943be86..102824b 100644 --- a/src/port.cpp +++ b/src/hardware/port.cpp @@ -1,12 +1,8 @@ -/* - * port.cpp - * Author: Fergal Moran - * Copyright: 2020 Fergal Moran - * - * BSD License - do what you want - */ -#include "../include/port.h" +#include +using namespace ferglos::common; +namespace ferglos { +namespace drivers { Port::Port(uint16_t portNumber) { this->portNumber = portNumber; } @@ -68,4 +64,6 @@ uint32_t Port32Bit::Read() { : "=a"(result) : "Nd"(portNumber)); return result; -} \ No newline at end of file +} +} // namespace drivers +} // namespace ferglos \ No newline at end of file diff --git a/src/interruptstubs.s b/src/interruptstubs.s index 0f81d9f..234cccc 100644 --- a/src/interruptstubs.s +++ b/src/interruptstubs.s @@ -2,20 +2,20 @@ .section .text -.extern _ZN16InterruptManager15HandleInterruptEhj -.global _ZN16InterruptManager22IgnoreInterruptRequestEv +.extern _ZN7ferglos8hardware16InterruptManager15HandleInterruptEhj +.global _ZN7ferglos8hardware16InterruptManager22IgnoreInterruptRequestEv .macro HandleException num -.global _ZN16InterruptManager16HandleException\num\()Ev -_ZN16InterruptManager16HandleException\num\()Ev: +.global _ZN7ferglos8hardware16InterruptManager16HandleException\num\()Ev +_ZN7ferglos8hardware16InterruptManager16HandleException\num\()Ev: movb $\num, (interruptnumber) jmp int_bottom .endm .macro HandleInterruptRequest num -.global _ZN16InterruptManager26HandleInterruptRequest\num\()Ev -_ZN16InterruptManager26HandleInterruptRequest\num\()Ev: +.global _ZN7ferglos8hardware16InterruptManager26HandleInterruptRequest\num\()Ev +_ZN7ferglos8hardware16InterruptManager26HandleInterruptRequest\num\()Ev: movb $\num + IRQ_BASE, (interruptnumber) jmp int_bottom .endm @@ -33,7 +33,7 @@ int_bottom: pushl %esp push (interruptnumber) - call _ZN16InterruptManager15HandleInterruptEhj + call _ZN7ferglos8hardware16InterruptManager15HandleInterruptEhj movl %eax, %esp popl %gs @@ -42,7 +42,8 @@ int_bottom: popl %ds popa -_ZN16InterruptManager22IgnoreInterruptRequestEv: +.global _ZN7ferglos8hardware16InterruptManager22IgnoreInterruptRequestEv +_ZN7ferglos8hardware16InterruptManager22IgnoreInterruptRequestEv: iret .data diff --git a/src/kernel.cpp b/src/kernel.cpp index 17a97ea..f1a176f 100644 --- a/src/kernel.cpp +++ b/src/kernel.cpp @@ -1,12 +1,15 @@ -#include "../include/blockmousehandler.h" -#include "../include/driver.h" -#include "../include/echokeyboardhandler.h" -#include "../include/gdt.h" -#include "../include/interrupts.h" -#include "../include/keyboard.h" -#include "../include/mouse.h" -#include "../include/stdio.h" -#include "../include/types.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace ferglos; +using namespace ferglos::hardware; typedef void (*constructor)(); extern "C" constructor start_ctors;