2 Commits

3 changed files with 191 additions and 15 deletions

View File

@@ -0,0 +1,25 @@
#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() {}
// Stubs for testing
void CPU::fetchOperDst() { /* _dst already set manually in tests */ }
void CPU::fetchOperSrc() { /* _src already set manually in tests */ }
void CPU::imp() { /* no-op post action */ }
void CPU::hookRAM(RAM*) {}
void CPU::hookInstrReel(InstrReel*) {}
void CPU::fetchInstr() {}
void CPU::execute() {}
void CPU::psw() {}
}

View File

@@ -70,63 +70,96 @@ namespace spider {
} }
void CPU::D2I() { void CPU::D2I() {
// TODO: Implement D2I fetchOperDst();
_dst->_u32 = static_cast<u32>(_dst->_f64);
(this->*_post)();
} }
void CPU::D2L() { void CPU::D2L() {
// TODO: Implement D2L fetchOperDst();
_dst->_u64 = static_cast<u64>(_dst->_f64);
(this->*_post)();
} }
void CPU::SIN() { void CPU::SIN() {
// TODO: Implement SIN fetchOperDst();
_dst->_f64 = std::sin(_dst->_f64);
(this->*_post)();
} }
void CPU::COS() { void CPU::COS() {
// TODO: Implement COS fetchOperDst();
_dst->_f64 = std::cos(_dst->_f64);
(this->*_post)();
} }
void CPU::TAN() { void CPU::TAN() {
// TODO: Implement TAN fetchOperDst();
_dst->_f64 = std::tan(_dst->_f64);
(this->*_post)();
} }
void CPU::ASIN() { void CPU::ASIN() {
// TODO: Implement ASIN fetchOperDst();
_dst->_f64 = std::asin(_dst->_f64);
(this->*_post)();
} }
void CPU::ACOS() { void CPU::ACOS() {
// TODO: Implement ACOS fetchOperDst();
_dst->_f64 = std::acos(_dst->_f64);
(this->*_post)();
} }
void CPU::ATAN() { void CPU::ATAN() {
// TODO: Implement ATAN fetchOperDst();
_dst->_f64 = std::atan(_dst->_f64);
(this->*_post)();
} }
void CPU::ATAN2() { void CPU::ATAN2() {
// TODO: Implement ATAN2 fetchOperDst();
fetchOperSrc();
_dst->_f64 = std::atan2(_dst->_f64, _src->_f64);
(this->*_post)();
} }
void CPU::EXP() { void CPU::EXP() {
// TODO: Implement EXP fetchOperDst();
_dst->_f64 = std::exp(_dst->_f64);
(this->*_post)();
} }
void CPU::LOG() { void CPU::LOG() {
// TODO: Implement LOG fetchOperDst();
_dst->_f64 = std::log(_dst->_f64);
(this->*_post)();
} }
void CPU::LOGAB() { void CPU::LOGAB() {
// TODO: Implement LOGAB fetchOperDst();
fetchOperSrc();
_dst->_f64 = std::log(_dst->_f64) / std::log(_src->_f64);
(this->*_post)();
} }
void CPU::POW() { void CPU::POW() {
// TODO: Implement POW fetchOperDst();
fetchOperSrc();
_dst->_f64 = std::pow(_dst->_f64, _src->_f64);
(this->*_post)();
} }
void CPU::SQRT() { void CPU::SQRT() {
// TODO: Implement SQRT fetchOperDst();
_dst->_f64 = std::sqrt(_dst->_f64);
(this->*_post)();
} }
void CPU::ROOT() { void CPU::ROOT() {
// TODO: Implement ROOT fetchOperDst();
fetchOperSrc();
_dst->_f64 = std::pow(_dst->_f64, 1.0 / _src->_f64);
} }
void CPU::ADC() { void CPU::ADC() {

View File

@@ -0,0 +1,118 @@
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_E
#define M_E 2.71828182845904523536
#endif
#include <spider/runtime/cpu/CPU.hpp>
#include <iostream>
#include <cmath>
using namespace spider;
void check(const char* name, double result, double expected, double tolerance = 1e-9) {
bool ok = std::abs(result - expected) <= tolerance;
std::cout << (ok ? "[PASS] " : "[FAIL] ") << name
<< " = " << result
<< " (expected " << expected << ")\n";
}
int main() {
std::cout << "=== Spider VM Instruction Test: 0x068-0x079 ===\n\n";
CPU cpu;
cpu._post = &CPU::imp;
std::cout << "-- Cast Instructions --\n";
cpu.RA._f64 = 3.9;
cpu._dst = &cpu.RA;
cpu.D2I();
check("D2I (3.9 -> 3)", cpu.RA._u32, 3.0);
cpu.RA._f64 = 1e12;
cpu._dst = &cpu.RA;
cpu.D2L();
check("D2L (1e12)", (double)cpu.RA._u64, 1e12);
std::cout << "\n-- Trigonometric Instructions --\n";
cpu.RA._f64 = M_PI / 2.0;
cpu._dst = &cpu.RA;
cpu.SIN();
check("SIN(pi/2)", cpu.RA._f64, 1.0);
cpu.RA._f64 = 0.0;
cpu._dst = &cpu.RA;
cpu.COS();
check("COS(0)", cpu.RA._f64, 1.0);
cpu.RA._f64 = M_PI / 4.0;
cpu._dst = &cpu.RA;
cpu.TAN();
check("TAN(pi/4)", cpu.RA._f64, 1.0);
cpu.RA._f64 = 1.0;
cpu._dst = &cpu.RA;
cpu.ASIN();
check("ASIN(1.0)", cpu.RA._f64, M_PI / 2.0);
cpu.RA._f64 = 1.0;
cpu._dst = &cpu.RA;
cpu.ACOS();
check("ACOS(1.0)", cpu.RA._f64, 0.0);
cpu.RA._f64 = 1.0;
cpu._dst = &cpu.RA;
cpu.ATAN();
check("ATAN(1.0)", cpu.RA._f64, M_PI / 4.0);
cpu.RA._f64 = 1.0;
cpu.RB._f64 = 1.0;
cpu._dst = &cpu.RA;
cpu._src = &cpu.RB;
cpu.ATAN2();
check("ATAN2(1,1)", cpu.RA._f64, M_PI / 4.0);
std::cout << "\n-- Exponential Instructions --\n";
cpu.RA._f64 = 1.0;
cpu._dst = &cpu.RA;
cpu.EXP();
check("EXP(1)", cpu.RA._f64, M_E);
cpu.RA._f64 = M_E;
cpu._dst = &cpu.RA;
cpu.LOG();
check("LOG(e)", cpu.RA._f64, 1.0);
cpu.RA._f64 = 100.0;
cpu.RB._f64 = 10.0;
cpu._dst = &cpu.RA;
cpu._src = &cpu.RB;
cpu.LOGAB();
check("LOGAB(100,10)", cpu.RA._f64, 2.0);
cpu.RA._f64 = 2.0;
cpu.RB._f64 = 10.0;
cpu._dst = &cpu.RA;
cpu._src = &cpu.RB;
cpu.POW();
check("POW(2,10)", cpu.RA._f64, 1024.0);
cpu.RA._f64 = 9.0;
cpu._dst = &cpu.RA;
cpu.SQRT();
check("SQRT(9)", cpu.RA._f64, 3.0);
cpu.RA._f64 = 27.0;
cpu.RB._f64 = 3.0;
cpu._dst = &cpu.RA;
cpu._src = &cpu.RB;
cpu.ROOT();
check("ROOT(27,3)", cpu.RA._f64, 3.0);
std::cout << "\n=== Tests complete ===\n";
return 0;
}