From 9feef38410311dfa8f2e88c14433e705b46f88bf Mon Sep 17 00:00:00 2001 From: Kittycannon Date: Sat, 21 Mar 2026 09:49:42 -0600 Subject: [PATCH] runtime progress --- spider-runtime.code-workspace | 3 +- src/spider/SpiderRuntime.cpp | 4 +- src/spider/runtime/Runtime.hpp | 2 +- src/spider/runtime/cpu/CPU.cpp | 16 ++ src/spider/runtime/cpu/Register.hpp | 20 ++- src/spider/runtime/debug/LiveDebug.cpp | 86 ++++++++++ src/spider/runtime/debug/LiveDebug.hpp | 7 + src/spider/runtime/native/machine.hpp | 6 +- src/spider/runtime/util/Terminal.cpp | 211 +++++++++++++++++++++++++ src/spider/runtime/util/Terminal.hpp | 146 +++++++++++++++++ 10 files changed, 488 insertions(+), 13 deletions(-) create mode 100644 src/spider/runtime/debug/LiveDebug.cpp create mode 100644 src/spider/runtime/debug/LiveDebug.hpp create mode 100644 src/spider/runtime/util/Terminal.cpp create mode 100644 src/spider/runtime/util/Terminal.hpp diff --git a/spider-runtime.code-workspace b/spider-runtime.code-workspace index 33de8c3..07b9241 100644 --- a/spider-runtime.code-workspace +++ b/spider-runtime.code-workspace @@ -15,6 +15,7 @@ ], "C_Cpp.default.includePath": [ "./src" - ] + ], + "terminal.integrated.defaultProfile.windows": "MSYS2 UCRT" } } \ No newline at end of file diff --git a/src/spider/SpiderRuntime.cpp b/src/spider/SpiderRuntime.cpp index 5151a6b..cffd95a 100644 --- a/src/spider/SpiderRuntime.cpp +++ b/src/spider/SpiderRuntime.cpp @@ -1,5 +1,7 @@ #include "SpiderRuntime.hpp" +#include + #include namespace spider { @@ -9,6 +11,6 @@ namespace spider { } int main() { - std::cout << "Hello World" << std::endl; + spider::liveDebugMain(); return 0; } diff --git a/src/spider/runtime/Runtime.hpp b/src/spider/runtime/Runtime.hpp index c4dc9c8..6541690 100644 --- a/src/spider/runtime/Runtime.hpp +++ b/src/spider/runtime/Runtime.hpp @@ -10,7 +10,7 @@ namespace spider { * This is where the Spider VM (Runtime) lives */ class Runtime { - private: + public: CPU cpu; RAM ram; diff --git a/src/spider/runtime/cpu/CPU.cpp b/src/spider/runtime/cpu/CPU.cpp index e69de29..fb8cdda 100644 --- a/src/spider/runtime/cpu/CPU.cpp +++ b/src/spider/runtime/cpu/CPU.cpp @@ -0,0 +1,16 @@ +#include "CPU.hpp" + +namespace spider { + + CPU::CPU() + : RA{}, RB{}, RC{}, RD{}, + RX{}, RY{}, R0{}, R1{}, + R2{}, R3{}, R4{}, R5{}, + R6{}, R7{}, R8{}, R9{}, + RF{}, RI{}, RS{}, RZ{}, + RE{}, RN{}, RV{}, RM{} + {} + + CPU::~CPU() {} + +} diff --git a/src/spider/runtime/cpu/Register.hpp b/src/spider/runtime/cpu/Register.hpp index a2acdd5..2948e22 100644 --- a/src/spider/runtime/cpu/Register.hpp +++ b/src/spider/runtime/cpu/Register.hpp @@ -23,21 +23,25 @@ namespace spider { f64 _f64; u8 _bytes[8]; - SPIDER_PACKED_STRUCT(struct { + struct { #if SPIDER_LITTLE_ENDIAN - u8 _u8; u64 : 56; + u8 _u8; // This looks like a cruel joke + u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; #else - u64 : 56; u8 _u8; + u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; + u8 _u8; #endif - }); + }; - SPIDER_PACKED_STRUCT(struct { + struct { #if SPIDER_LITTLE_ENDIAN - u16 _u16; u64 : 48; + u16 _u16; + u16 : 16; u16 : 16; u16 : 16; #else - u64 : 48; u16 _u16; + u16 : 16; u16 : 16; u16 : 16; + u16 _u16; #endif - }); + }; struct { #if SPIDER_LITTLE_ENDIAN diff --git a/src/spider/runtime/debug/LiveDebug.cpp b/src/spider/runtime/debug/LiveDebug.cpp new file mode 100644 index 0000000..56b23be --- /dev/null +++ b/src/spider/runtime/debug/LiveDebug.cpp @@ -0,0 +1,86 @@ +#include "LiveDebug.hpp" + +#include +#include + +#include +#include + +namespace spider { + + void drawHead(Terminal& t) { + t.move(1, 1) + .style(Terminal::RESET) + .print(" Spider Runtime Live Debug ") + .style(Terminal::RESET).print(" | ") + .style(Terminal::FG_CYAN).print(" Sintek Analytics @ 2026 ") + .style(Terminal::RESET).print(" | ") + .style(Terminal::FG_B_BLACK).print("Press ESC to exit") + .style(Terminal::FG_BLACK) + .style(Terminal::BG_YELLOW) + .move(3, 1).print(" || __ ||").print(" ") + .move(4, 1).print(" \\\\( )//").print(" SPIDER v0.1 ") + .move(5, 1).print(" //()\\\\ ").print(" alpha ") + .move(6, 1).print(" || || ").print(" ") + .style(Terminal::RESET) + ; + } + + void drawCPUTempl(Terminal& t, CPU& cpu) { + i32 r = 7, c = 1; + t.drawBox(r, c, 35, 18, "CPU"); + + const std::string regs[] = { + "RA", "RB", "RC", "RD", + "RX", "RY", "R0", "R1", + "R2", "R3", "R4", "R5", + "R6", "R7", "R8", "R9", + "RF", "RI", "RS", "RZ", + "RE", "RN", "RV", "RM" + }; + + r++; + c++; + + + + for(i32 i = 0; i < 8; i++) { + t.move(r + i * 2, c); + t.print(regs[i * 2]); + t.move(r + i * 2, c + 17); + t.print(regs[i * 2 + 1]); + } + } + + int liveDebugMain() { + Terminal t; + Runtime runtime(1024); + + t.println("Starting Spider live debug..."); + t.altbuff(true).cursor(false); + + drawHead(t); + drawCPUTempl(t, runtime.cpu); + + bool running = true; + int c = 1; + while (running) { + // Handle Input + u8 k = t.getKey(); + switch (k) { + case Terminal::ESC: + running = false; + break; + default: + t.move(5, c++); + t.print("A"); + t.flush(); + break; + } + } + + t.altbuff(false).println("Stopped Spider live debug.").flush(); + return 0; + } + +} diff --git a/src/spider/runtime/debug/LiveDebug.hpp b/src/spider/runtime/debug/LiveDebug.hpp new file mode 100644 index 0000000..69140cb --- /dev/null +++ b/src/spider/runtime/debug/LiveDebug.hpp @@ -0,0 +1,7 @@ +#pragma once + +namespace spider { + + int liveDebugMain(); + +} diff --git a/src/spider/runtime/native/machine.hpp b/src/spider/runtime/native/machine.hpp index 5d049b6..2b6fe1f 100644 --- a/src/spider/runtime/native/machine.hpp +++ b/src/spider/runtime/native/machine.hpp @@ -55,6 +55,7 @@ // ========================================================== // // PACKING // +// (not used now) // // ========================================================== // @@ -73,8 +74,9 @@ // Macros... #if defined(SPIDER_COMPILER_GCC_LIKE) - #define SPIDER_ATTRIBUTE_PACKED __attribute__((packed)) - #define SPIDER_PACKED_STRUCT(decl) decl SPIDER_ATTRIBUTE_PACKED + #define SPIDER_PACK_BEGIN + #define SPIDER_PACK_END + #define SPIDER_PACK_STRUCT __attribute__((packed)) #elif defined(SPIDER_COMPILER_MSVC) #define SPIDER_BEGIN_PACKED __pragma(pack(push, 1)) diff --git a/src/spider/runtime/util/Terminal.cpp b/src/spider/runtime/util/Terminal.cpp new file mode 100644 index 0000000..0883191 --- /dev/null +++ b/src/spider/runtime/util/Terminal.cpp @@ -0,0 +1,211 @@ +#include "Terminal.hpp" + +#include + +#if defined(SPIDER_OS_WINDOWS) +#include +#include +#endif + +#if defined(SPIDER_OS_LINUX) || defined(SPIDER_OS_MACOS) +#include +#include +#include +#endif + +#if defined(SPIDER_DISTRO_DESKTOP) + +namespace spider { + + Terminal::Terminal() { +#if defined(SPIDER_OS_WINDOWS) + SetConsoleOutputCP(CP_UTF8); + SetConsoleCP(CP_UTF8); +#endif + } + + Terminal::~Terminal() { + altbuff(false).style(RESET).cursor(true).flush(); + } + + Terminal& Terminal::style(const std::string_view code) { + std::cout << code; + return *this; + } + + Terminal& Terminal::move(i32 row, i32 col) { + std::cout << "\033[" << row << ";" << col << "H"; + return *this; + } + + Terminal& Terminal::altbuff(bool enable) { + std::cout << (enable ? "\033[?1049h" : "\033[?1049l"); + return *this; + } + + Terminal& Terminal::cls() { + std::cout << "\033[2J"; + return *this; + } + + Terminal& Terminal::scrollRange(i32 start, i32 end) { + std::cout << "\033[" << start << ";" << end << "r\033[?6h"; + return *this; + } + + Terminal& Terminal::undoSRange() { + std::cout << "\033[r\033[?6l\033[H"; + return *this; + } + + Terminal& Terminal::fill(const std::string_view color) { + this->style(color); + this->cls(); + return *this; + } + + Terminal& Terminal::clearRow(i32 row) { + // Move to row, column 1 + this->move(row, 1); + // \033[2K clears the entire line + std::cout << "\033[2K"; + return *this; + } + + Terminal& Terminal::clearRows(i32 start, i32 end) { + // Ensure we don't loop infinitely if start > end + if (start > end) std::swap(start, end); + + for (i32 i = start; i <= end; ++i) { + this->clearRow(i); + } + + // Optional: Move cursor back to the start of the cleared block + this->move(start, 1); + return *this; + } + + Terminal& Terminal::cursor(bool show) { + std::cout << (show ? "\033[?25h" : "\033[?25l"); + return *this; + } + + Terminal& Terminal::drawBox(i32 startRow, i32 startCol, i32 width, i32 height, std::string_view title) { + // 1. Draw the top border + move(startRow, startCol); + std::cout << "┌"; + for (i32 i = 0; i < width - 2; ++i) std::cout << "─"; + std::cout << "┐"; + + // 2. Draw the sides + for (i32 i = 1; i < height - 1; ++i) { + move(startRow + i, startCol); + std::cout << "│"; + move(startRow + i, startCol + width - 1); + std::cout << "│"; + } + + // 3. Draw the bottom border + move(startRow + height - 1, startCol); + std::cout << "└"; + for (i32 i = 0; i < width - 2; ++i) std::cout << "─"; + std::cout << "┘"; + + // 4. Overlay the title if provided + if (!title.empty()) { + move(startRow, startCol + (width - title.size() - 2) / 2); + std::cout << " " << title << " "; + } + + std::cout.flush(); + return *this; + } + + Terminal& Terminal::flush() { + std::cout << std::flush; + return *this; + } + + Terminal& Terminal::sink() { + std::cin.clear(); +#if defined(SPIDER_OS_WINDOWS) + while (_kbhit()) _getch(); +#endif +#if defined(SPIDER_OS_LINUX) || defined(SPIDER_OS_MACOS) + tcflush(STDIN_FILENO, TCIFLUSH); +#endif + return *this; + } + + std::pair Terminal::size() { + std::pair pair; +#if defined(SPIDER_OS_WINDOWS) + CONSOLE_SCREEN_BUFFER_INFO csbi; + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { + pair.first = csbi.srWindow.Right - csbi.srWindow.Left + 1; + pair.second = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; + } +#endif +#if defined(SPIDER_OS_LINUX) || defined(SPIDER_OS_MACOS) + struct winsize w; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) { + pair.first = w.ws_col; + pair.second = w.ws_row; + } +#endif + return pair; + } + + Terminal& Terminal::wait() { + sink(); + std::cin.get(); + return *this; + } + + u8 Terminal::getKey() { +#if defined(SPIDER_OS_WINDOWS) + i32 ch = _getch(); + if (ch == 0 || ch == 224) { + switch (_getch()) { + case 72: return Terminal::UP; + case 80: return Terminal::DOWN; + case 75: return Terminal::LEFT; + case 77: return Terminal::RIGHT; + } + } + if (ch == 13) return Terminal::ENTER; + if (ch == 27) return Terminal::ESC; + if (ch == 8) return Terminal::BACKSPACE; + return Terminal::UNKNOWN; +#endif +#if defined(SPIDER_OS_LINUX) || defined(SPIDER_OS_MACOS) + struct termios oldt, newt; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + u8 chd = Terminal::UNKNOWN; + i32 ch = getchar(); + if (ch == 27) { // ESC sequence + if (getchar() == '[') { + switch (getchar()) { + case 'A': chd = Terminal::UP; break; + case 'B': chd = Terminal::DOWN; break; + case 'D': chd = Terminal::LEFT; break; + case 'C': chd = Terminal::RIGHT; break; + } + } + } + else if (ch == 10) chd = Terminal::ENTER; + else if (ch == 127) chd = Terminal::BACKSPACE; + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + + return chd; +#endif + } + +} + +#endif diff --git a/src/spider/runtime/util/Terminal.hpp b/src/spider/runtime/util/Terminal.hpp new file mode 100644 index 0000000..3532722 --- /dev/null +++ b/src/spider/runtime/util/Terminal.hpp @@ -0,0 +1,146 @@ +#pragma once + +#include + +#include +#include +#include +#include + +namespace spider { + + class Terminal { + public: + + // --- Constants --- + static constexpr const char* RESET = "\033[0m"; + static constexpr const char* FG_BLACK = "\033[30m"; static constexpr const char* FG_B_BLACK = "\033[90m"; + static constexpr const char* FG_RED = "\033[31m"; static constexpr const char* FG_B_RED = "\033[91m"; + static constexpr const char* FG_GREEN = "\033[32m"; static constexpr const char* FG_B_GREEN = "\033[92m"; + static constexpr const char* FG_YELLOW = "\033[33m"; static constexpr const char* FG_B_YELLOW = "\033[93m"; + static constexpr const char* FG_BLUE = "\033[34m"; static constexpr const char* FG_B_BLUE = "\033[94m"; + static constexpr const char* FG_MAGENTA = "\033[35m"; static constexpr const char* FG_B_MAGENTA = "\033[95m"; + static constexpr const char* FG_CYAN = "\033[36m"; static constexpr const char* FG_B_CYAN = "\033[96m"; + static constexpr const char* FG_WHITE = "\033[37m"; static constexpr const char* FG_B_WHITE = "\033[97m"; + + static constexpr const char* BG_BLACK = "\033[40m"; static constexpr const char* BG_B_BLACK = "\033[100m"; + static constexpr const char* BG_RED = "\033[41m"; static constexpr const char* BG_B_RED = "\033[101m"; + static constexpr const char* BG_GREEN = "\033[42m"; static constexpr const char* BG_B_GREEN = "\033[102m"; + static constexpr const char* BG_YELLOW = "\033[43m"; static constexpr const char* BG_B_YELLOW = "\033[103m"; + static constexpr const char* BG_BLUE = "\033[44m"; static constexpr const char* BG_B_BLUE = "\033[104m"; + static constexpr const char* BG_MAGENTA = "\033[45m"; static constexpr const char* BG_B_MAGENTA = "\033[105m"; + static constexpr const char* BG_CYAN = "\033[46m"; static constexpr const char* BG_B_CYAN = "\033[106m"; + static constexpr const char* BG_WHITE = "\033[47m"; static constexpr const char* BG_B_WHITE = "\033[107m"; + + public: + + static constexpr const u8 UP = 0x1; + static constexpr const u8 DOWN = 0x2; + static constexpr const u8 LEFT = 0x3; + static constexpr const u8 RIGHT = 0x4; + static constexpr const u8 ENTER = 0x5; + static constexpr const u8 ESC = 0x6; + static constexpr const u8 BACKSPACE = 0x7; + static constexpr const u8 UNKNOWN = 0x8; + + public: + + Terminal(); + + ~Terminal(); + + public: + + /** + * Sets a style + */ + Terminal& style(const std::string_view code); + + /** + * Fills the screen with a specific background color. + * @param color The background color constant (e.g., Terminal::BG_BLUE) + */ + Terminal& fill(const std::string_view color); + + /** + * Moves the cursor. + */ + Terminal& move(i32 row, i32 col); + + /** + * Clears the screen + */ + Terminal& cls(); + + Terminal& altbuff(bool enable); + + Terminal& scrollRange(i32 start, i32 end); + + Terminal& undoSRange(); + + /** + * Clears a specific row. + * @param row The 1-based index of the row to clear. + */ + Terminal& clearRow(i32 row); + + /** + * Clears a range of rows (inclusive). + * @param start The first row. + * @param end The last row. + */ + Terminal& clearRows(i32 start, i32 end); + + /** + * Shows / Hides the cursor. + */ + Terminal& cursor(bool show); + + public: + + Terminal& drawBox(i32 startRow, i32 startCol, i32 width, i32 height, std::string_view title); + + public: + + /** + * Flushes the output buffer + */ + Terminal& flush(); + + /** + * Clears the input buffer. + * Useful for some specific input cases + */ + Terminal& sink(); + + public: + + Terminal& wait(); + + u8 getKey(); + + std::pair size(); + + public: + + template + Terminal& print(const T& msg) { + std::cout << msg; + return *this; + } + + template + Terminal& println(const T& msg) { + std::cout << msg << "\n"; + return *this; + } + + template + Terminal& read(T& var) { + std::cin >> var; + return *this; + } + + }; + +} \ No newline at end of file