2 Commits

10 changed files with 488 additions and 13 deletions

View File

@@ -15,6 +15,7 @@
], ],
"C_Cpp.default.includePath": [ "C_Cpp.default.includePath": [
"./src" "./src"
] ],
"terminal.integrated.defaultProfile.windows": "MSYS2 UCRT"
} }
} }

View File

@@ -1,5 +1,7 @@
#include "SpiderRuntime.hpp" #include "SpiderRuntime.hpp"
#include <spider/runtime/debug/LiveDebug.hpp>
#include <iostream> #include <iostream>
namespace spider { namespace spider {
@@ -9,6 +11,6 @@ namespace spider {
} }
int main() { int main() {
std::cout << "Hello World" << std::endl; spider::liveDebugMain();
return 0; return 0;
} }

View File

@@ -10,7 +10,7 @@ namespace spider {
* This is where the Spider VM (Runtime) lives * This is where the Spider VM (Runtime) lives
*/ */
class Runtime { class Runtime {
private: public:
CPU cpu; CPU cpu;
RAM ram; RAM ram;

View File

@@ -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() {}
}

View File

@@ -23,21 +23,25 @@ namespace spider {
f64 _f64; f64 _f64;
u8 _bytes[8]; u8 _bytes[8];
SPIDER_PACKED_STRUCT(struct { struct {
#if SPIDER_LITTLE_ENDIAN #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 #else
u64 : 56; u8 _u8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8; u8 : 8;
u8 _u8;
#endif #endif
}); };
SPIDER_PACKED_STRUCT(struct { struct {
#if SPIDER_LITTLE_ENDIAN #if SPIDER_LITTLE_ENDIAN
u16 _u16; u64 : 48; u16 _u16;
u16 : 16; u16 : 16; u16 : 16;
#else #else
u64 : 48; u16 _u16; u16 : 16; u16 : 16; u16 : 16;
u16 _u16;
#endif #endif
}); };
struct { struct {
#if SPIDER_LITTLE_ENDIAN #if SPIDER_LITTLE_ENDIAN

View File

@@ -0,0 +1,86 @@
#include "LiveDebug.hpp"
#include <spider/runtime/Runtime.hpp>
#include <spider/runtime/util/Terminal.hpp>
#include <vector>
#include <string>
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;
}
}

View File

@@ -0,0 +1,7 @@
#pragma once
namespace spider {
int liveDebugMain();
}

View File

@@ -55,6 +55,7 @@
// ========================================================== // // ========================================================== //
// PACKING // // PACKING //
// (not used now) //
// ========================================================== // // ========================================================== //
@@ -73,8 +74,9 @@
// Macros... // Macros...
#if defined(SPIDER_COMPILER_GCC_LIKE) #if defined(SPIDER_COMPILER_GCC_LIKE)
#define SPIDER_ATTRIBUTE_PACKED __attribute__((packed)) #define SPIDER_PACK_BEGIN
#define SPIDER_PACKED_STRUCT(decl) decl SPIDER_ATTRIBUTE_PACKED #define SPIDER_PACK_END
#define SPIDER_PACK_STRUCT __attribute__((packed))
#elif defined(SPIDER_COMPILER_MSVC) #elif defined(SPIDER_COMPILER_MSVC)
#define SPIDER_BEGIN_PACKED __pragma(pack(push, 1)) #define SPIDER_BEGIN_PACKED __pragma(pack(push, 1))

View File

@@ -0,0 +1,211 @@
#include "Terminal.hpp"
#include <spider/runtime/native/distro.hpp>
#if defined(SPIDER_OS_WINDOWS)
#include <conio.h>
#include <windows.h>
#endif
#if defined(SPIDER_OS_LINUX) || defined(SPIDER_OS_MACOS)
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#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<i32, i32> Terminal::size() {
std::pair<i32, i32> 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

View File

@@ -0,0 +1,146 @@
#pragma once
#include <spider/runtime/common.hpp>
#include <iostream>
#include <string>
#include <string_view>
#include <limits>
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<i32, i32> size();
public:
template <typename T>
Terminal& print(const T& msg) {
std::cout << msg;
return *this;
}
template <typename T>
Terminal& println(const T& msg) {
std::cout << msg << "\n";
return *this;
}
template <typename T>
Terminal& read(T& var) {
std::cin >> var;
return *this;
}
};
}