From 4bf7f3579a5793f70207a1b407887b4c1c49c6f7 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Thu, 28 Jan 2016 19:50:04 +0100 Subject: [PATCH] Added dummy HAL and a cross compile build script --- buildcrosscompiler.sh | 49 +++++++++++++++++ kernel/Makefile | 7 ++- kernel/hal/Makefile | 12 ++--- kernel/hal/dummy/Makefile | 26 +++++++++ kernel/hal/dummy/asm/snippets.S | 0 kernel/hal/dummy/boot/boot.S | 15 ++++++ kernel/hal/dummy/c_include/io.h | 53 +++++++++++++++++++ kernel/hal/dummy/c_include/stdint.h | 9 ++++ kernel/hal/dummy/c_include/string.h | 11 ++++ kernel/hal/dummy/include/Multitasking.h | 23 ++++++++ kernel/hal/dummy/include/base.hpp | 19 +++++++ kernel/hal/dummy/include/input.hpp | 29 ++++++++++ kernel/hal/dummy/include/keyboard.hpp | 22 ++++++++ kernel/hal/dummy/include/keycodes.hpp | 41 +++++++++++++++ kernel/hal/dummy/include/keymap_DE.hpp | 17 ++++++ kernel/hal/dummy/include/output.hpp | 46 ++++++++++++++++ kernel/hal/dummy/include/serial.hpp | 31 +++++++++++ kernel/hal/dummy/include/textDISP.hpp | 70 +++++++++++++++++++++++++ kernel/hal/dummy/init/Multitasking.cpp | 36 +++++++++++++ kernel/hal/dummy/init/init.cpp | 32 +++++++++++ kernel/hal/dummy/io/keyboard.cpp | 20 +++++++ kernel/hal/dummy/io/output.cpp | 39 ++++++++++++++ kernel/hal/dummy/io/serial.cpp | 19 +++++++ kernel/hal/dummy/io/textDISP.cpp | 32 +++++++++++ kernel/hal/dummy/string.c | 12 +++++ 25 files changed, 661 insertions(+), 9 deletions(-) create mode 100644 buildcrosscompiler.sh create mode 100644 kernel/hal/dummy/Makefile create mode 100644 kernel/hal/dummy/asm/snippets.S create mode 100644 kernel/hal/dummy/boot/boot.S create mode 100644 kernel/hal/dummy/c_include/io.h create mode 100644 kernel/hal/dummy/c_include/stdint.h create mode 100644 kernel/hal/dummy/c_include/string.h create mode 100644 kernel/hal/dummy/include/Multitasking.h create mode 100644 kernel/hal/dummy/include/base.hpp create mode 100644 kernel/hal/dummy/include/input.hpp create mode 100644 kernel/hal/dummy/include/keyboard.hpp create mode 100644 kernel/hal/dummy/include/keycodes.hpp create mode 100644 kernel/hal/dummy/include/keymap_DE.hpp create mode 100644 kernel/hal/dummy/include/output.hpp create mode 100644 kernel/hal/dummy/include/serial.hpp create mode 100644 kernel/hal/dummy/include/textDISP.hpp create mode 100644 kernel/hal/dummy/init/Multitasking.cpp create mode 100644 kernel/hal/dummy/init/init.cpp create mode 100644 kernel/hal/dummy/io/keyboard.cpp create mode 100644 kernel/hal/dummy/io/output.cpp create mode 100644 kernel/hal/dummy/io/serial.cpp create mode 100644 kernel/hal/dummy/io/textDISP.cpp create mode 100644 kernel/hal/dummy/string.c diff --git a/buildcrosscompiler.sh b/buildcrosscompiler.sh new file mode 100644 index 0000000..aa63aa1 --- /dev/null +++ b/buildcrosscompiler.sh @@ -0,0 +1,49 @@ +CROSSPATH=$HOME/opt # You can change it to whatever you want +export PATH=$CROSSPATH/bin:$PATH +function buildscript() { + date "[+%c] " | tr -d '\n' | tee buildlog + echo "Building binutils for $1" | tee buildlog + mkdir build-binutils + cd build-binutils + ../binutils-2.26/configure --prefix=$CROSSPATH --target=$1 --with-sysroot --disable-nls --disable-werror + make -j2 + make install + cd .. + rm -rf build-binutils + date "[+%c] " | tr -d '\n' | tee buildlog + echo "Building gcc for $1" | tee buildlog + mkdir build-gcc + cd build-gcc + ../gcc-5.3.0/configure --prefix=$CROSSPATH --target=$1 --disable-nls --enable-languages=c,c++ --without-headers + make all-gcc + make all-target-libgcc + make install-gcc + make install-target-libgcc + cd .. + rm -rf build-gcc + date "[+%c] " | tr -d '\n' | tee buildlog + echo "Cross-compiler for $1 was built." | tee buildlog +} +tempdir=$(mktemp -d) +cd $tempdir +echo "Temponary files are in $tempdir. Build log can be found under $tempdir/buildlog" +echo "Downloading GCC, Binutils, MPC, MPFR and GMP" +wget ftp://ftp.gnu.org/gnu/gcc/gcc-5.3.0/gcc-5.3.0.tar.bz2 ftp://ftp.gnu.org/gnu/binutils/binutils-2.26.tar.bz2 ftp://ftp.gnu.org/gnu/mpc/mpc-1.0.3.tar.gz ftp://ftp.gnu.org/gnu/mpfr/mpfr-3.1.3.tar.xz ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.0.tar.xz +echo "Untaring..." +tar -xf gcc-5.3.0.tar.bz2 +tar -xf binutils-2.26.tar.bz2 +cd gcc-5.3.0 +tar -xf mpc-1.0.3.tar.gz +mv mpc-1.0.3 mpc +tar -xf mpfr-3.1.3.tar.xz +mv mpfr-3.1.3 mpfr +tar -xf gmp-6.1.0.tar.xz +mv gmp-6.1.0 gmp +cd .. +echo "Preperation done. Beginning the compilation now." +buildscript i686-elf #x86 port +buildscript arm-none-eabi #ARM ports (3DS, pi) +buildscript armeb-eabi #Wii port +buildscript ppc-elf #Wii port +rm -rf gcc* binutils* mpc* mpfr* gmp* +echo "Done! Have fun with your cross compilers!" \ No newline at end of file diff --git a/kernel/Makefile b/kernel/Makefile index ba607e6..1179e98 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,8 +1,11 @@ include ../kernel.settings -SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]') +SRCS = $(shell find hal/$(arch) -name '*.cpp' -o -name '*.[cS]') OBJS = $(addsuffix .o,$(basename $(SRCS))) LD = $(PREFIX)g++ -LDFLAGS = -T kernel.ld -nostdlib -nodefaultlibs -m32 -nostdlib -fno-builtin +LDFLAGS = -nostdlib -nodefaultlibs -nostdlib -fno-builtin +ifeq ($(arch),x86) + LDFLAGS += -m32 -T kernel.ld +endif all: hal $(LD) $(LDFLAGS) -o mtgos $(OBJS) hal: diff --git a/kernel/hal/Makefile b/kernel/hal/Makefile index bdbbfae..87a14a6 100644 --- a/kernel/hal/Makefile +++ b/kernel/hal/Makefile @@ -1,10 +1,8 @@ -all: x86 - -x86: - make -C x86 - +include ../../kernel.settings +all: + make -C $(arch) clean: rm -rf hal.o - make -C x86 clean + make -C $(arch) clean -.PHONY: all x86 clean +.PHONY: all clean diff --git a/kernel/hal/dummy/Makefile b/kernel/hal/dummy/Makefile new file mode 100644 index 0000000..312a18a --- /dev/null +++ b/kernel/hal/dummy/Makefile @@ -0,0 +1,26 @@ +include ../../../kernel.settings +SRCS = $(shell find -name '*.cpp' -o -name '*.[cS]') +OBJS = $(addsuffix .o,$(basename $(SRCS))) + +CPP = $(PREFIX)g++ +CC = $(PREFIX)gcc +ASFLAGS = +CFLAGS = -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g +CPPFLAGS = -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g +LDFLAGS = -r + +all: $(OBJS) + +%.o: %.cpp + $(CPP) $(CPPFLAGS) -c -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $^ + +%.o: $.S + $(CC) $(CFLAGS) -c -o $@ $^ + +clean: + rm -rf $(OBJS) + +.PHONY: clean all diff --git a/kernel/hal/dummy/asm/snippets.S b/kernel/hal/dummy/asm/snippets.S new file mode 100644 index 0000000..e69de29 diff --git a/kernel/hal/dummy/boot/boot.S b/kernel/hal/dummy/boot/boot.S new file mode 100644 index 0000000..60d416a --- /dev/null +++ b/kernel/hal/dummy/boot/boot.S @@ -0,0 +1,15 @@ +.section multiboot +#define MB_MAGIC 0x1BADB002 +#define MB_FLAGS 0x0 +#define MB_CHECKSUM -(MB_MAGIC + MB_FLAGS) +.align 4 +.int MB_MAGIC +.int MB_FLAGS +.int MB_CHECKSUM +.section .text +.extern init +.global _start +_start: +.section .bss +.space 8192 +kernel_stack: diff --git a/kernel/hal/dummy/c_include/io.h b/kernel/hal/dummy/c_include/io.h new file mode 100644 index 0000000..990e441 --- /dev/null +++ b/kernel/hal/dummy/c_include/io.h @@ -0,0 +1,53 @@ +#ifndef _IO_H +#define _IO_H +#include +static inline void outb(uint16_t port, uint8_t val) __attribute__((always_inline)); +static inline void outb(uint16_t port, uint8_t val) { + asm volatile("outb %0, %1" : : "a"(val), "Nd"(port)); +} +static inline void outw(uint16_t port, uint16_t val) __attribute__((always_inline)); +static inline void outw(uint16_t port, uint16_t val) { + asm volatile("outw %0, %1" : : "a"(val), "Nd"(port)); +} +static inline void outl(uint16_t port, uint32_t val) __attribute__((always_inline)); +static inline void outl(uint16_t port, uint32_t val) { + asm volatile("outl %0, %1" : : "a"(val), "Nd"(port)); +} +static inline uint8_t inb(uint16_t port) __attribute__((always_inline)); +static inline uint8_t inb(uint16_t port) { + uint8_t ret; + asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} +static inline uint16_t inw(uint16_t port) __attribute__((always_inline)); +static inline uint16_t inw(uint16_t port) { + uint16_t ret; + asm volatile("inw %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} +static inline uint32_t inl(uint16_t port) __attribute__((always_inline)); +static inline uint32_t inl(uint16_t port) { + uint32_t ret; + asm volatile("inl %1, %0" : "=a"(ret) : "Nd"(port)); + return ret; +} +static inline void io_wait() __attribute__((always_inline)); +static inline void io_wait() { + asm volatile("outb %%al, $0x80" : : "a"(0)); +} +static inline void cli() __attribute__((always_inline)); +static inline void cli() { + asm volatile("cli"); +} +static inline void sti() __attribute__((always_inline)); +static inline void sti() { + asm volatile("sti"); +} +static inline uint64_t rdtsc() __attribute__((always_inline)); +static inline uint64_t rdtsc() +{ + uint64_t ret; + asm volatile ( "rdtsc" : "=A"(ret) ); + return ret; +} +#endif diff --git a/kernel/hal/dummy/c_include/stdint.h b/kernel/hal/dummy/c_include/stdint.h new file mode 100644 index 0000000..2926bb5 --- /dev/null +++ b/kernel/hal/dummy/c_include/stdint.h @@ -0,0 +1,9 @@ +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef signed long long int int64_t; +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long int uint64_t; +typedef unsigned int uintptr_t; diff --git a/kernel/hal/dummy/c_include/string.h b/kernel/hal/dummy/c_include/string.h new file mode 100644 index 0000000..f31a22e --- /dev/null +++ b/kernel/hal/dummy/c_include/string.h @@ -0,0 +1,11 @@ +#ifndef _STRING_H +#define _STRING_H +#ifdef _cplusplus +extern "C" { +#endif +#include +void memmove(void* dst, void* src, uint32_t size); +#ifdef _cplusplus +} +#endif +#endif diff --git a/kernel/hal/dummy/include/Multitasking.h b/kernel/hal/dummy/include/Multitasking.h new file mode 100644 index 0000000..a958c39 --- /dev/null +++ b/kernel/hal/dummy/include/Multitasking.h @@ -0,0 +1,23 @@ +#ifndef MULTITASKING_H +#define MULTITASKING_H +namespace MTGosHAL { + + class Multitasking + { + public: + Multitasking(); + auto schedule(struct cpu_state* cpu) -> struct cpu_state*; + protected: + private: + auto initTask(uint8_t* stck, void(*entry)()) -> struct cpu_state*; + static auto task_a() -> void; + static auto task_b() -> void; + uint8_t stack_a[4096]; + uint8_t stack_b[4096]; + struct cpu_state* task_states[2]; + int current_task, num_tasks; + }; + +} // namespace MTGosHAL + +#endif // MULTITASKING_H diff --git a/kernel/hal/dummy/include/base.hpp b/kernel/hal/dummy/include/base.hpp new file mode 100644 index 0000000..60c1883 --- /dev/null +++ b/kernel/hal/dummy/include/base.hpp @@ -0,0 +1,19 @@ +#ifndef _BASE_HPP +#define _BASE_HPP +#include +namespace MTGosHAL { + class Output; + class Input; + class Serial; + class Screen; + class Keyboard; + class Multitasking; + enum class BG_color: uint16_t; + enum class FG_color: uint16_t; + extern Serial debug; + extern Screen out; + extern Screen err; + extern Keyboard in; + extern Multitasking tasks; +} +#endif diff --git a/kernel/hal/dummy/include/input.hpp b/kernel/hal/dummy/include/input.hpp new file mode 100644 index 0000000..ed55198 --- /dev/null +++ b/kernel/hal/dummy/include/input.hpp @@ -0,0 +1,29 @@ +#ifndef _INPUT_HPP +#define _INPUT_HPP +namespace MTGosHAL { + class Input { + private: + virtual auto getChar() -> char = 0; + public: + auto operator>>(char &input) -> Input & { + input=getChar(); + return *this; + } + //Note that it receives up to 256 bytes. + auto operator>>(char* input) -> Input & { + int ptr=0; + char tmp='\0'; + while(ptr<256 && tmp!='\r') { + while(!(tmp=getChar())); + if(tmp=='\r') + input[ptr++]='\0'; + else + input[ptr++]=tmp; + } + input[255]='\0'; + return *this; + } + //TODO: Add more possibilities. + }; +} +#endif diff --git a/kernel/hal/dummy/include/keyboard.hpp b/kernel/hal/dummy/include/keyboard.hpp new file mode 100644 index 0000000..8a5c9fd --- /dev/null +++ b/kernel/hal/dummy/include/keyboard.hpp @@ -0,0 +1,22 @@ +#ifndef _KEYBOARD_HPP +#define _KEYBOARD_HPP +#include +#include +#include +namespace MTGosHAL { + + class Keyboard: public Input { + private: + + //This kernel has a buffer of 16 chars + char buf[16]; + int len; + auto getChar() -> char; + auto sendCommand(uint8_t command) -> void; + bool numlock, capslock, scrolllock, response; + public: + auto handleIRQ1(struct cpu_state* cpu) -> struct cpu_state*; + Keyboard(); + }; +} +#endif // -Wno-pmf-conversions diff --git a/kernel/hal/dummy/include/keycodes.hpp b/kernel/hal/dummy/include/keycodes.hpp new file mode 100644 index 0000000..c79a76f --- /dev/null +++ b/kernel/hal/dummy/include/keycodes.hpp @@ -0,0 +1,41 @@ +#ifndef _KEYCODES_HPP +#define _KEYCODES_HPP +bool keydowns[0x69]; +uint8_t keyboardKeycodes[2][128] { + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, + 0x50, 0x51, 0x52, 0x53, 0x00, 0x00, 0x54, 0x55, + 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }, { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x57, 0x58, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, + 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5B, + 0x5C, 0x5D, 0x00, 0x5E, 0x00, 0x5F, 0x00, 0x60, + 0x61, 0x62, 0x63, 0x64, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x64, 0x65, 0x66, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + } +}; +#endif + diff --git a/kernel/hal/dummy/include/keymap_DE.hpp b/kernel/hal/dummy/include/keymap_DE.hpp new file mode 100644 index 0000000..baeda85 --- /dev/null +++ b/kernel/hal/dummy/include/keymap_DE.hpp @@ -0,0 +1,17 @@ +#ifndef _KEYMAP_DE_HPP +#define _KEYMAP_DE_HPP +#include +#define SHIFT 1 +#define CTRL 2 +#define ALT 4 +char keymap[8][0x80] = { + "\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\000789-456+1230,<\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // No modifier + "\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Shift modifier + "\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,<\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control modifier + "\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Shift modifiers + "\0\0001234567890\xe1\0\b\0qwertzuiop\x81+\n\0asdfghjkl\x94\x84^\0#yxcvbnm,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,<\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Alt modifier + "\0\000!\"\0$%&/()=?`\b\0QWERTZUIOP\x9A*\n\0ASDFGHJKL\x99\x8e\xF8\0'YXCVBNM;:_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,>\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Alt+Shift modifiers + "\0\0001\xFD\x33\xAC\xAB\xAA{[]}\\\xa7\b\0@w\xEErtzui\xEDp\x81~\n\0\x91sdfghjkl\x94^\0#\xAF\xAE\x9Bvbn\xe6,.-\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,|\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Alt modifiers + "\0\000\xAD\"\x9C\x0F%&/(\xF1\xF8\xA8`\b\0\xEAW\xEERT\x9DUIOP\xF8*\n\0\x92SD\xA6GHJ&L\x99\x8e\xF8\0|YXCVBN\xF8;\xF6_\0*\0 \0\0\0\0\0\0\0\0\0\0\0\0\0789-456+1230,|\0\0\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", // Control+Shift modifiers +}; +#endif diff --git a/kernel/hal/dummy/include/output.hpp b/kernel/hal/dummy/include/output.hpp new file mode 100644 index 0000000..143b182 --- /dev/null +++ b/kernel/hal/dummy/include/output.hpp @@ -0,0 +1,46 @@ +#ifndef _OUTPUT_HPP +#define _OUTPUT_HPP +#include +#include +namespace MTGosHAL { + + enum class Base : int { + BINARY=2, + TERNARY, + BASE4, + BASE5, + BASE6, + BASE7, + OCTAL, + BASE9, + DECIMAL, + BASE11, + BASE12, + BASE13, + BASE14, + BASE15, + HEXADECIMAL + }; + /* abstract */ class Output { + private: + virtual auto putChar(char c) -> void = 0; + auto puts(const char* s) -> void; + int base=10; + public: + template + auto operator<<(T output) -> Output & { + puts(output); //kernel won't compile if class cannot be converted into string + return *this; + } + + }; + template <> + auto Output::operator<<(Base output) -> Output &; + template <> + auto Output::operator<<(int output) -> Output &; + template <> + auto Output::operator<<(char output) -> Output &; + template <> + auto Output::operator<<(char* output) -> Output &; +} +#endif diff --git a/kernel/hal/dummy/include/serial.hpp b/kernel/hal/dummy/include/serial.hpp new file mode 100644 index 0000000..71399c6 --- /dev/null +++ b/kernel/hal/dummy/include/serial.hpp @@ -0,0 +1,31 @@ +#ifndef _SERIAL_HPP +#define _SERIAL_HPP +#include +#include +#include +#include +#define SERIAL_IER 1 +#define SERIAL_IIR 2 +#define SERIAL_FCR 2 +#define SERIAL_LCR 3 +#define SERIAL_MCR 4 +#define SERIAL_LSR 5 +#define SERIAL_MSR 6 +namespace MTGosHAL { + class Serial: public Output, public Input { + private: + uint16_t port; + uint64_t waittimes; + uint64_t transmits; + bool works; + auto isTransmitEmpty() -> int; + auto putChar(char chr) -> void; + auto serial_received() -> int; + auto getChar() -> char; + public: + Serial(); + auto debug() -> void; + }; +} + +#endif diff --git a/kernel/hal/dummy/include/textDISP.hpp b/kernel/hal/dummy/include/textDISP.hpp new file mode 100644 index 0000000..b91d12d --- /dev/null +++ b/kernel/hal/dummy/include/textDISP.hpp @@ -0,0 +1,70 @@ +#ifndef _TEXTDISP_H +#define _TEXTDISP_H +#include +#include +#define SCREEN_WIDTH 80 +#define SCREEN_HEIGHT 24 +namespace MTGosHAL { + enum class BG_color : uint16_t { + BLACK=0x0000, + BLUE=0x1000, + GREEN=0x2000, + CYAN=0x3000, + RED=0x4000, + MAGENTA=0x5000, + BROWN=0x6000, + LIGHT_GREY=0x7000, + GREY=0x8000, + LIGHT_BLUE=0x9000, + LIGHT_GREEN=0xA000, + LIGHT_CYAN=0xB000, + LIGHT_RED=0xC000, + LIGHT_MAGENTA=0xD000, + YELLOW=0xE000, + WHITE=0xF000 + }; + enum class FG_color : uint16_t { + BLACK=0x000, + BLUE=0x100, + GREEN=0x200, + CYAN=0x300, + RED=0x400, + MAGENTA=0x500, + BROWN=0x600, + LIGHT_GREY=0x700, + GREY=0x800, + LIGHT_BLUE=0x900, + LIGHT_GREEN=0xA00, + LIGHT_CYAN=0xB00, + LIGHT_RED=0xC00, + LIGHT_MAGENTA=0xD00, + YELLOW=0xE00, + WHITE=0xF00 + }; + class Screen: public Output { + private: + FG_color fg; + BG_color bg; + uint16_t* vmem=(uint16_t*)0xB8000; + auto putChar(char c) -> void; + public: + Screen() { + clrscr(); + } + template + auto operator<< (T output) -> Screen & { + Output::operator<<(output); + return *this; + } + auto clrscr() -> void; + auto scroll() -> void; + auto setColor(FG_color fg) -> Screen &; + auto setColor(BG_color bg) -> Screen &; + auto setColor(FG_color fg, BG_color bg) -> Screen &; + }; + template <> + auto Screen::operator<<(FG_color fg) -> Screen &; + template <> + auto Screen::operator<<(BG_color bg) -> Screen &; +} +#endif diff --git a/kernel/hal/dummy/init/Multitasking.cpp b/kernel/hal/dummy/init/Multitasking.cpp new file mode 100644 index 0000000..98f1a09 --- /dev/null +++ b/kernel/hal/dummy/init/Multitasking.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +/*auto schedule(struct cpu_state* cpu) -> struct cpu_state* { + return MTGosHAL::tasks.schedule(cpu); +}*/ +namespace MTGosHAL { + +Multitasking::Multitasking(): current_task(-1), num_tasks(2) +{ + task_states[0] = initTask(stack_a, task_a); + task_states[1] = initTask(stack_b, task_b); +} +auto Multitasking::initTask(uint8_t* stck, void(* entry)()) -> struct cpu_state* +{ + return nullptr; +} + +auto Multitasking::task_a() -> void +{ + while(true) + out << "A"; +} + +auto Multitasking::task_b() -> void +{ + while(true) + out << "B"; +} +auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* +{ + return nullptr; +} + +} // namespace MTGosHAL diff --git a/kernel/hal/dummy/init/init.cpp b/kernel/hal/dummy/init/init.cpp new file mode 100644 index 0000000..58e110f --- /dev/null +++ b/kernel/hal/dummy/init/init.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +extern "C" void intr_stub_0(void); +namespace MTGosHAL { + Serial debug; + Screen out; + Screen err; + Keyboard in; + Multitasking tasks; + void main() { + out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; + err << BG_color::BLACK << FG_color::RED; + debug << "Hello debugger! This is MTGos v00r01\nThese logs are probably very long, so please redirect the output to a file.\n"; + for(;;); + } +} +typedef void (*constructor)(); +extern "C" constructor start_ctors; +extern "C" constructor end_ctors; +extern "C" void init() { + for(constructor* i = &start_ctors; i != &end_ctors; ++i) + (*i)(); + MTGosHAL::main(); +} +extern "C" void __cxa_pure_virtual() { + MTGosHAL::debug << "A pure virtual function just got called.\n"; +} + diff --git a/kernel/hal/dummy/io/keyboard.cpp b/kernel/hal/dummy/io/keyboard.cpp new file mode 100644 index 0000000..1a8c77b --- /dev/null +++ b/kernel/hal/dummy/io/keyboard.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include +auto handleIRQ(struct cpu_state* cpu) -> struct cpu_state* { + return cpu; +} +namespace MTGosHAL { + auto Keyboard::getChar() -> char { + return '\0'; + } + auto Keyboard::sendCommand(uint8_t command) -> void { + } + auto Keyboard::handleIRQ1(struct cpu_state* cpu) -> struct cpu_state* { + return cpu; + } + Keyboard::Keyboard(): numlock(true), capslock(false), scrolllock(false), response(false) { + } +} diff --git a/kernel/hal/dummy/io/output.cpp b/kernel/hal/dummy/io/output.cpp new file mode 100644 index 0000000..dda21a7 --- /dev/null +++ b/kernel/hal/dummy/io/output.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +namespace MTGosHAL { + auto Output::puts(const char* s) -> void { + int i=0; + while(s[i]!='\0') + putChar(s[i++]); + } + template <> + auto Output::operator<<(Base output) -> Output & { + base=static_cast(output); + return *this; + } + template <> + auto Output::operator<<(int output) -> Output & { + const char* chars="0123456789ABCDEF"; + char buf[33]; + buf[32]='\0'; + char* ptr=buf+31; + do { + *(ptr--)=chars[output%base]; + output/=base; + } while(output && (ptr!=buf)); + puts(ptr+1); + return *this; + } + template <> + auto Output::operator<<(char output) -> Output & { + putChar(output); + return *this; + } + template <> + auto Output::operator<<(char* output) -> Output & { + puts(output); + return *this; + } +} diff --git a/kernel/hal/dummy/io/serial.cpp b/kernel/hal/dummy/io/serial.cpp new file mode 100644 index 0000000..c5e0d08 --- /dev/null +++ b/kernel/hal/dummy/io/serial.cpp @@ -0,0 +1,19 @@ +#include +#include +namespace MTGosHAL { + auto Serial::isTransmitEmpty() -> int { + return 0; + } + auto Serial::putChar(char chr) -> void { + } + auto Serial::serial_received() -> int { + return 0; + } + auto Serial::getChar() -> char { + return '\0'; + } + Serial::Serial(): works(true) { + } + auto Serial::debug() -> void { + } +} diff --git a/kernel/hal/dummy/io/textDISP.cpp b/kernel/hal/dummy/io/textDISP.cpp new file mode 100644 index 0000000..26c2d7d --- /dev/null +++ b/kernel/hal/dummy/io/textDISP.cpp @@ -0,0 +1,32 @@ +#include +#include +int x=0, y=0; +namespace MTGosHAL { + auto Screen::putChar(char c) -> void { + } + auto Screen::clrscr() -> void { + } + auto Screen::scroll() -> void { + } + template <> + auto Screen::operator<<(FG_color fg) -> Screen &{ + this->fg=fg; + return *this; + } + template <> + auto Screen::operator<<(BG_color bg) -> Screen &{ + this->bg=bg; + return *this; + } + auto Screen::setColor(FG_color fg) -> Screen &{ + this->fg=fg; + return *this; + } + auto Screen::setColor(BG_color bg) -> Screen &{ + this->bg=bg; + return *this; + } + auto Screen::setColor(FG_color fg, BG_color bg) -> Screen &{ + return (*this).setColor(fg).setColor(bg); + } +} diff --git a/kernel/hal/dummy/string.c b/kernel/hal/dummy/string.c new file mode 100644 index 0000000..a9031c7 --- /dev/null +++ b/kernel/hal/dummy/string.c @@ -0,0 +1,12 @@ +#include +__attribute__((optimize(0))) void memmove(void* dst, void* src, uint32_t size) { + uint8_t* from=(uint8_t*)src; + uint8_t* to=(uint8_t*)dst; + if((srcdst)) { + for(int i=size-1;i>=0;i--) + to[i]=from[i]; //This would get optimized by gcc to memmove(dst, src, size); if optimizations are enabled. + } else if(src != dst) { + for(int i=0;i