diff --git a/src/spider/runtime/cpu/CPU.cpp b/src/spider/runtime/cpu/CPU.cpp index 1a796b1..26a66c0 100644 --- a/src/spider/runtime/cpu/CPU.cpp +++ b/src/spider/runtime/cpu/CPU.cpp @@ -80,7 +80,7 @@ namespace spider { _opers[1] = _opers[0]; // call specific addressing mode - (this->*(CPU::addrModes[_addrm]))(); + (this->*(CPU::addrModes[_addrm & 0b111]))(); // mask added here too } void CPU::fetchOperSrc() { @@ -88,17 +88,19 @@ namespace spider { _alu = &ALU1; // call specific addressing mode - (this->*(CPU::addrModes[_addrm]))(); + (this->*(CPU::addrModes[_addrm & 0b111]))(); // mask keeps index within 0-7 // modify the _addrm register _addrm = static_cast((_addrm >> 3) & 0x1F); _addrm++; } + void CPU::execute() { - (this->*(CPU::addrModes[_opcode]))(); + (this->*(CPU::instrMap[_opcode]))(); // no null check needed } + // Addressing Modes // /** @@ -145,6 +147,7 @@ namespace spider { // get byte u8 reg = (_reel->readU8(RI) >> sh) & 0xF; _alu = &GPR[reg]; + _opers[0] = _alu; // explicitly sets _opers[0] = _dst RI += use; // store no-op diff --git a/src/spider/runtime/debug/LiveDebug.cpp b/src/spider/runtime/debug/LiveDebug.cpp index ddb6192..0c81c3f 100644 --- a/src/spider/runtime/debug/LiveDebug.cpp +++ b/src/spider/runtime/debug/LiveDebug.cpp @@ -330,18 +330,16 @@ namespace spider { int liveDebugMain() { Terminal t; Runtime runtime(1024); + + InstrReelFixed fix(100); - runtime.ram[0] = 0xFF; - runtime.ram[1] = 0xEE; - runtime.ram[2] = 0xDD; - runtime.ram[3] = 0xCC; - runtime.ram[4] = 0xBB; - runtime.ram[5] = 0xAA; - runtime.ram[6] = 0x99; - runtime.ram[7] = 0x88; - fix.writeU16(0, 0b0000111); + + + runtime.hookReel(&fix, false); + + bool running = true, update = true; u64 ramScroll = 0; u8 key = Terminal::UNKNOWN; @@ -388,7 +386,7 @@ namespace spider { case Terminal::ENTER: update = true; runtime.cpu.fetchInstr(); - runtime.cpu.fetchOperDst(); + runtime.cpu.execute(); // looks up instrMap[_opcode] & calls the correct instruction method (e.g. FMUL) break; default: break; diff --git a/src/spider/runtime/instr/Instr_040-05F.cpp b/src/spider/runtime/instr/Instr_040-05F.cpp index f0c8829..153ab96 100644 --- a/src/spider/runtime/instr/Instr_040-05F.cpp +++ b/src/spider/runtime/instr/Instr_040-05F.cpp @@ -4,6 +4,8 @@ */ #include +#include // provides std::fmod, std::fma and cast support + namespace spider { @@ -71,52 +73,207 @@ namespace spider { // TODO: Implement FSUB } + // ── 0x054 — FMUL: Float Multiplication ─────────────────────────────────── void CPU::FMUL() { - // TODO: Implement FMUL + fetchOperSrc(); + fetchOperDst(); + switch (_size) { + case 0b10: _dst->_f32 *= _src->_f32; break; // f32 + case 0b11: _dst->_f64 *= _src->_f64; break; // f64 + default: break; // invalid size + } + (this->*_post)(); } + // ── 0x055 — FDIV: Float Division ───────────────────────────────────────── void CPU::FDIV() { - // TODO: Implement FDIV + fetchOperSrc(); + fetchOperDst(); + switch (_size) { + case 0b10: _dst->_f32 /= _src->_f32; break; + case 0b11: _dst->_f64 /= _src->_f64; break; + default: break; + } + (this->*_post)(); } + // ── 0x056 — FMOD: Float Modulus ────────────────────────────────────────── + // C++ has no % for floats — std::fmod performs the equivalent operation void CPU::FMOD() { - // TODO: Implement FMOD + fetchOperSrc(); + fetchOperDst(); + switch (_size) { + case 0b10: _dst->_f32 = std::fmod(_dst->_f32, _src->_f32); break; + case 0b11: _dst->_f64 = std::fmod(_dst->_f64, _src->_f64); break; + default: break; + } + (this->*_post)(); } + // ── 0x057 — FDMOD: Float Division and Modulus ──────────────────────────── + // dst / src = RX (quotient) * src + RY (remainder) void CPU::FDMOD() { - // TODO: Implement FDMOD + fetchOperSrc(); + fetchOperDst(); + switch (_size) { + case 0b10: { + f32 q = static_cast(static_cast(_dst->_f32 / _src->_f32)); + f32 r = _dst->_f32 - (q * _src->_f32); + RX._f32 = q; + RY._f32 = r; + break; + } + case 0b11: { + f64 q = static_cast(static_cast(_dst->_f64 / _src->_f64)); + f64 r = _dst->_f64 - (q * _src->_f64); + RX._f64 = q; + RY._f64 = r; + break; + } + default: break; + } + (this->*_post)(); } + // ── 0x058 — FEPS: Set Float Epsilon Value ──────────────────────────────── + // Loads the epsilon value into RN (the epsilon register) void CPU::FEPS() { - // TODO: Implement FEPS + fetchOperDst(); + switch (_size) { + case 0b10: RN = _dst->_u32; break; // store f32 bits in RN + case 0b11: RN = _dst->_u64; break; // store f64 bits in RN + default: break; + } + (this->*_post)(); } + // ── 0x059 — FEEP: Float Enable/Disable Epsilon ─────────────────────────── + // Bit 12 of RF is the Epsilon Enable flag void CPU::FEEP() { - // TODO: Implement FEEP + fetchOperDst(); + constexpr u64 EPSILON_ENABLE_BIT = (1ULL << 12); + if (_dst->_u64) RF |= EPSILON_ENABLE_BIT; // non-zero → enable + else RF &= ~EPSILON_ENABLE_BIT; // zero → disable + (this->*_post)(); } + // ── 0x05A — FEQ: Float Equal ────────────────────────────────────────────── + // Sets bit 10 (Zero/Equal flag) in RF if dst == src void CPU::FEQ() { - // TODO: Implement FEQ + fetchOperSrc(); + fetchOperDst(); + constexpr u64 ZERO_FLAG = (1ULL << 10); + bool equal = false; + switch (_size) { + case 0b10: equal = (_dst->_f32 == _src->_f32); break; + case 0b11: equal = (_dst->_f64 == _src->_f64); break; + default: break; + } + if (equal) RF |= ZERO_FLAG; + else RF &= ~ZERO_FLAG; + (this->*_post)(); } + // ── 0x05B — FNE: Float Not Equal ───────────────────────────────────────── void CPU::FNE() { - // TODO: Implement FNE + fetchOperSrc(); + fetchOperDst(); + constexpr u64 ZERO_FLAG = (1ULL << 10); + bool notEqual = false; + switch (_size) { + case 0b10: notEqual = (_dst->_f32 != _src->_f32); break; + case 0b11: notEqual = (_dst->_f64 != _src->_f64); break; + default: break; + } + if (notEqual) RF |= ZERO_FLAG; + else RF &= ~ZERO_FLAG; + (this->*_post)(); } + // ── 0x05C — FGT: Float Greater Than ────────────────────────────────────── + // Sets/clears bit 9 (Negative flag) in RF void CPU::FGT() { - // TODO: Implement FGT + fetchOperSrc(); + fetchOperDst(); + constexpr u64 NEGATIVE_FLAG = (1ULL << 9); + bool gt = false; + switch (_size) { + case 0b10: gt = (_dst->_f32 > _src->_f32); break; + case 0b11: gt = (_dst->_f64 > _src->_f64); break; + default: break; + } + if (gt) RF &= ~NEGATIVE_FLAG; + else RF |= NEGATIVE_FLAG; + (this->*_post)(); } + // ── 0x05D — FGE: Float Greater or Equal ────────────────────────────────── void CPU::FGE() { - // TODO: Implement FGE + fetchOperSrc(); + fetchOperDst(); + constexpr u64 NEGATIVE_FLAG = (1ULL << 9); + constexpr u64 ZERO_FLAG = (1ULL << 10); + bool ge = false; + bool eq = false; + switch (_size) { + case 0b10: + ge = (_dst->_f32 >= _src->_f32); + eq = (_dst->_f32 == _src->_f32); + break; + case 0b11: + ge = (_dst->_f64 >= _src->_f64); + eq = (_dst->_f64 == _src->_f64); + break; + default: break; + } + if (ge) RF &= ~NEGATIVE_FLAG; + else RF |= NEGATIVE_FLAG; + if (eq) RF |= ZERO_FLAG; + else RF &= ~ZERO_FLAG; + (this->*_post)(); } + // ── 0x05E — FLT: Float Lower Than ──────────────────────────────────────── void CPU::FLT() { - // TODO: Implement FLT + fetchOperSrc(); + fetchOperDst(); + constexpr u64 NEGATIVE_FLAG = (1ULL << 9); + bool lt = false; + switch (_size) { + case 0b10: lt = (_dst->_f32 < _src->_f32); break; + case 0b11: lt = (_dst->_f64 < _src->_f64); break; + default: break; + } + if (lt) RF |= NEGATIVE_FLAG; + else RF &= ~NEGATIVE_FLAG; + (this->*_post)(); } + // ── 0x05F — FLE: Float Lower or Equal ──────────────────────────────────── void CPU::FLE() { - // TODO: Implement FLE + fetchOperSrc(); + fetchOperDst(); + constexpr u64 NEGATIVE_FLAG = (1ULL << 9); + constexpr u64 ZERO_FLAG = (1ULL << 10); + bool le = false; + bool eq = false; + switch (_size) { + case 0b10: + le = (_dst->_f32 <= _src->_f32); + eq = (_dst->_f32 == _src->_f32); + break; + case 0b11: + le = (_dst->_f64 <= _src->_f64); + eq = (_dst->_f64 == _src->_f64); + break; + default: break; + } + if (le) RF &= ~NEGATIVE_FLAG; + else RF |= NEGATIVE_FLAG; + if (eq) RF |= ZERO_FLAG; + else RF &= ~ZERO_FLAG; + (this->*_post)(); } + } diff --git a/src/spider/runtime/instr/Instr_060-07F.cpp b/src/spider/runtime/instr/Instr_060-07F.cpp index fd01f5d..c253bcf 100644 --- a/src/spider/runtime/instr/Instr_060-07F.cpp +++ b/src/spider/runtime/instr/Instr_060-07F.cpp @@ -4,39 +4,69 @@ */ #include +#include // provides std::fmod, std::fma and cast support + namespace spider { +// ── 0x060 — F2D: Float (f32) to Double (f64) ───────────────────────────── + // Widening conversion — no precision is lost void CPU::F2D() { - // TODO: Implement F2D + fetchOperDst(); + _dst->_f64 = static_cast(_dst->_f32); + (this->*_post)(); } + // ── 0x061 — D2F: Double (f64) to Float (f32) ───────────────────────────── + // Narrowing conversion — precision may be lost void CPU::D2F() { - // TODO: Implement D2F + fetchOperDst(); + _dst->_f32 = static_cast(_dst->_f64); + (this->*_post)(); } + // ── 0x062 — I2F: Integer (i32) to Float (f32) ──────────────────────────── void CPU::I2F() { - // TODO: Implement I2F + fetchOperDst(); + _dst->_f32 = static_cast(_dst->_u32); + (this->*_post)(); } + // ── 0x063 — I2D: Integer (i32) to Double (f64) ─────────────────────────── void CPU::I2D() { - // TODO: Implement I2D + fetchOperDst(); + _dst->_f64 = static_cast(_dst->_u32); + (this->*_post)(); } + // ── 0x064 — L2F: Long (i64) to Float (f32) ─────────────────────────────── void CPU::L2F() { - // TODO: Implement L2F + fetchOperDst(); + _dst->_f32 = static_cast(_dst->_u64); + (this->*_post)(); } + // ── 0x065 — L2D: Long (i64) to Double (f64) ────────────────────────────── void CPU::L2D() { - // TODO: Implement L2D + fetchOperDst(); + _dst->_f64 = static_cast(_dst->_u64); + (this->*_post)(); } + // ── 0x066 — F2I: Float (f32) to Integer (i32) ──────────────────────────── + // Truncates toward zero void CPU::F2I() { - // TODO: Implement F2I + fetchOperDst(); + _dst->_u32 = static_cast(_dst->_f32); + (this->*_post)(); } + // ── 0x067 — F2L: Float (f32) to Long (i64) ─────────────────────────────── + // Truncates toward zero void CPU::F2L() { - // TODO: Implement F2L + fetchOperDst(); + _dst->_u64 = static_cast(_dst->_f32); + (this->*_post)(); } void CPU::D2I() { diff --git a/src/spider/runtime/util/Terminal.cpp b/src/spider/runtime/util/Terminal.cpp index 55a589d..bcc6b47 100644 --- a/src/spider/runtime/util/Terminal.cpp +++ b/src/spider/runtime/util/Terminal.cpp @@ -11,8 +11,15 @@ #include #include #include +#include //This was missing, +//The ioctl, TIOCGWINSZ and winsize used in getSize() live in that header, but it was never included. #endif + +//the Linux includes at the top are inside #if SPIDER_OS_LINUX which IS defined, +//but getSize() is inside #if SPIDER_DISTRO_DESKTOP which is NOT defined, +//so the compiler sees the ioctl call without the include that would have covered it. + #if defined(SPIDER_DISTRO_DESKTOP) namespace spider { @@ -218,7 +225,8 @@ namespace spider { struct termios oldt, newt; tcgetattr(STDIN_FILENO, &oldt); newt = oldt; - newt.c_lflag &= ~(ICANON | ECHO); + //newt.c_lflag &= ~(ICANON | ECHO); + newt.c_lflag &= static_cast(~(ICANON | ECHO)); //added this line tcsetattr(STDIN_FILENO, TCSANOW, &newt); u8 result = Terminal::UNKNOWN; @@ -247,7 +255,8 @@ namespace spider { } } else if (ch == 10) result = Terminal::ENTER; else if (ch == 127) result = Terminal::BACKSPACE; - else result = (u8)ch; + else result = static_cast(ch); //added this line + //else result = (u8)ch; tcsetattr(STDIN_FILENO, TCSANOW, &oldt); return result;