From 138c026b9a2121ebcd6a8840c2065bc6ec77549f Mon Sep 17 00:00:00 2001 From: Curle Date: Mon, 5 Jul 2021 04:17:23 +0100 Subject: [PATCH] More work on proofing C++. Will not compile without a custom toolchain. --- CMakeLists.txt | 10 ++- inc/kernel/system/io.h | 4 +- inc/lainlib/mutex/ticketlock.h | 2 + src/editor/{EditorMain.c => EditorMain.cpp} | 2 +- src/{kernel.c => kernel.cpp} | 44 +++++------ src/video/draw.c | 2 + src/video/{print.c => print.cpp} | 86 ++++++++++++++++++++- 7 files changed, 115 insertions(+), 35 deletions(-) rename src/editor/{EditorMain.c => EditorMain.cpp} (95%) rename src/{kernel.c => kernel.cpp} (83%) rename src/video/{print.c => print.cpp} (65%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6838c86..e60fc86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ #project config cmake_minimum_required(VERSION 3.10) +SET(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED on) set(CMAKE_CXX_COMPILER x86_64-elf-g++) set(CMAKE_C_COMPILER x86_64-elf-gcc) @@ -12,9 +14,9 @@ SET(CMAKE_CROSSCOMPILING 1) project(chroma) SET(src_files - ${CMAKE_SOURCE_DIR}/src/kernel.c + ${CMAKE_SOURCE_DIR}/src/kernel.cpp ${CMAKE_SOURCE_DIR}/src/video/draw.c - ${CMAKE_SOURCE_DIR}/src/video/print.c + ${CMAKE_SOURCE_DIR}/src/video/print.cpp ${CMAKE_SOURCE_DIR}/src/system/cpu.c ${CMAKE_SOURCE_DIR}/src//system/rw.c ${CMAKE_SOURCE_DIR}/src/system/serial.c @@ -34,10 +36,10 @@ SET(lib_files ${CMAKE_SOURCE_DIR}/src/lainlib/compression/lzgmini.c ${CMAKE_SOURCE_DIR}/src/lainlib/string/str.c - ${CMAKE_SOURCE_DIR}/src/editor/EditorMain.c + ${CMAKE_SOURCE_DIR}/src/editor/EditorMain.cpp ) -include_directories("inc") +include_directories("inc" "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++" "D:/mingw/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32") SET(src_no_sse ${CMAKE_SOURCE_DIR}/src/system/interrupts.c diff --git a/inc/kernel/system/io.h b/inc/kernel/system/io.h index d9f057b..809e11c 100644 --- a/inc/kernel/system/io.h +++ b/inc/kernel/system/io.h @@ -73,8 +73,8 @@ void Send8042(size_t); void WriteSerialChar(const char); void WriteSerialString(const char*, size_t); -int SerialPrintf(const char* restrict format, ...); -int Printf(const char* restrict Format, ...); +int SerialPrintf(const char* format, ...); +int Printf(const char* Format, ...); void* memcpy(void* dest, void const* src, size_t len); void* memset(void* dst, int src, size_t len); diff --git a/inc/lainlib/mutex/ticketlock.h b/inc/lainlib/mutex/ticketlock.h index e9024fd..c44acc3 100644 --- a/inc/lainlib/mutex/ticketlock.h +++ b/inc/lainlib/mutex/ticketlock.h @@ -1,6 +1,8 @@ #pragma once #include + #include +// TODO: for cplusplus. /************************ *** Team Kitty, 2020 *** diff --git a/src/editor/EditorMain.c b/src/editor/EditorMain.cpp similarity index 95% rename from src/editor/EditorMain.c rename to src/editor/EditorMain.cpp index 7209f36..e76e63a 100644 --- a/src/editor/EditorMain.c +++ b/src/editor/EditorMain.cpp @@ -16,7 +16,7 @@ static KeyboardCallback KernelHandler; void StartEditor(int callbackID) { KernelHandler = KeyboardCallbacks[callbackID]; - struct EditorLayout layout = (struct EditorLayout) {0}; + struct EditorLayout layout; layout.ScreenHeight = PrintInfo.screenHeight; layout.ScreenWidth = PrintInfo.screenWidth; layout.HeaderHeight = layout.ScreenHeight / 100 * 3; diff --git a/src/kernel.c b/src/kernel.cpp similarity index 83% rename from src/kernel.c rename to src/kernel.cpp index f012cc9..befb46a 100644 --- a/src/kernel.c +++ b/src/kernel.cpp @@ -32,8 +32,19 @@ int InternalBufferID; size_t BufferLength = 0; char* InternalBuffer; +#ifdef __cplusplus +} +#endif + +/** + * C++ code! Scary! + * This is a temporary measure to experiment with the Editor system. + */ + int Main(void) { - KernelAddressSpace = (address_space_t) {0}; + KernelAddressSpace.Lock.NextTicket = 0; + KernelAddressSpace.Lock.NowServing = 0; + KernelAddressSpace.PML4 = nullptr; SerialPrintf("\r\n[ boot] Booting Chroma..\r\n"); SerialPrintf("[ boot] Bootloader data structure at 0x%p\r\n", (size_t) &bootldr); @@ -64,31 +75,8 @@ int Main(void) { InternalBuffer = (char*) kmalloc(4096); SerialPrintf("[ Mem] Allocated a text buffer at 0x%p\r\n", (size_t) InternalBuffer); - // TODO: WriteString should accept escape color codes! - SetForegroundColor(0x00FF0000); - WriteChar('C'); - SetForegroundColor(0x0000FF00); - WriteChar('h'); - SetForegroundColor(0x000000FF); - WriteChar('r'); - SetForegroundColor(0x00FFFF00); - WriteChar('o'); - SetForegroundColor(0x00FF00FF); - WriteChar('m'); - SetForegroundColor(0x0000FFFF); - WriteChar('a'); - WriteChar(' '); - SetForegroundColor(0x00FFFFFF); - WriteChar('T'); - SetForegroundColor(0x0000AAAA); - WriteChar('i'); - SetForegroundColor(0x00BBBBBB); - WriteChar('m'); - SetForegroundColor(0x001E2D42); - WriteChar('e'); - SetForegroundColor(0x00E0A969); - WriteChar('!'); - WriteChar('\n'); + WriteString("\\${FF0000}C\\${}h\\${}r\\${FFFF00}o\\${FF00FF}m\\${FFFF}a "); + WriteString("\\${FFFFFF}T\\${AAAA}i\\${BBBBBB}m\\${}e\\${}!\n"); SetForegroundColor(0x00FFFFFF); @@ -101,6 +89,10 @@ int Main(void) { return 0; } +#ifdef __cplusplus +extern "C" { +#endif + void PrintPressedChar(KeyboardData data) { if(!KernelLoaded) return; diff --git a/src/video/draw.c b/src/video/draw.c index b7877ef..39f0fe7 100644 --- a/src/video/draw.c +++ b/src/video/draw.c @@ -336,6 +336,8 @@ void DrawFilledCircleInternal(size_t centerX, size_t centerY, size_t radius, cha /******* Public *******/ +/** TODO: Move all of this into static DrawUtils:: functions */ + void DrawFilledRect(size_t x, size_t y, size_t width, size_t height) { for (size_t i = y; i < y + height; i++) DrawHorizontalLine(x, i, width); diff --git a/src/video/print.c b/src/video/print.cpp similarity index 65% rename from src/video/print.c rename to src/video/print.cpp index c7c2a60..73bcafc 100644 --- a/src/video/print.c +++ b/src/video/print.cpp @@ -1,4 +1,5 @@ #include +#include /************************ *** Team Kitty, 2020 *** @@ -35,7 +36,7 @@ static void NumToStr(char* Buffer, size_t Num, size_t Base) { } -int SerialPrintf(const char* restrict Format, ...) { +int SerialPrintf(const char* Format, ...) { va_list Parameters; va_start(Parameters, Format); @@ -116,7 +117,10 @@ int SerialPrintf(const char* restrict Format, ...) { return CharsWritten; } -int Printf(const char* restrict Format, ...) { +size_t ParseEnglishColor(const char* Name); +size_t ParseHexColor(const char* Name, bool bgFlag); + +int Printf(const char* Format, ...) { va_list Parameters; va_start(Parameters, Format); @@ -186,6 +190,49 @@ int Printf(const char* restrict Format, ...) { break; } + } else if(*Format == '\\') { + Format++; // Skip backslash + + switch(*Format) { + case '$': + // COLOR + Format ++; // Skip $ and { + size_t ContentLength = 0; + size_t Color = 0; + bool bgFlag = false; + + switch(*Format) { + case '[': + bgFlag = true; + // bg + case '{': + // fg + Format++; + if(*Format == '<') { + Color = ParseEnglishColor(++Format); + Format++; + } else { + Color = ParseHexColor(++Format, bgFlag); + } + + if(bgFlag) + SetBackgroundColor(Color); + else + SetForegroundColor(Color); + + Format++; + break; + } + + + Format++; + break; + case 'f': + // FORMAT + break; + } + + Format++; } else { WriteChar(*Format); Format++; @@ -196,6 +243,41 @@ int Printf(const char* restrict Format, ...) { return CharsWritten; } + +size_t ParseEnglishColor(const char* Stream) { + if(strcmp("red", Stream)) + return 0xFF0000; + if(strcmp("green", Stream)) + return 0xFF00; + if(strcmp("blue", Stream)) + return 0xFF; + if(strcmp("yellow", Stream)) + return 0xFFFF00; + if(strcmp("cyan", Stream)) + return 0xFFFF; + if(strcmp("magenta", Stream)) + return 0xFF00FF; + if(strcmp("beans", Stream)) + return 0xAA11CC; + if(strcmp("forgeb", Stream)) + return 0x1E2D42; + if(strcmp("forgey", Stream)) + return 0xE0A969; + return 0xFFFFFF; +} + +size_t ParseHexColor(const char* Stream, bool bgFlag) { + size_t val = 0; + char c; + + while ((*Stream != bgFlag ? ']' : '}') && (c = *Stream++)) { + char v = (c & 0xF) + (c >> 6) | ((c >> 3) & 0x8); + val = (val << 4) | (size_t) v; + } + + return val; +} + #ifdef __cplusplus } #endif \ No newline at end of file