From 2fce5ffe745bb89be471e450d9cd6284cd5614d9 Mon Sep 17 00:00:00 2001 From: harry Date: Fri, 19 Jan 2024 16:54:56 -0500 Subject: [PATCH] Added memory access functions for JS script interface. --- src/drivers/Qt/QtScriptManager.cpp | 140 +++++++++++++++++++++++++++++ src/drivers/Qt/QtScriptManager.h | 38 ++++++++ 2 files changed, 178 insertions(+) diff --git a/src/drivers/Qt/QtScriptManager.cpp b/src/drivers/Qt/QtScriptManager.cpp index 8757a523..40955b4a 100644 --- a/src/drivers/Qt/QtScriptManager.cpp +++ b/src/drivers/Qt/QtScriptManager.cpp @@ -41,6 +41,8 @@ #include "../../fceu.h" #include "../../movie.h" +#include "../../x6502.h" +#include "../../debug.h" #include "common/os_utils.h" @@ -199,6 +201,137 @@ QString EmuScriptObject::getDir() return QString(fceuExecutablePath()); } //---------------------------------------------------- +//---- Memory Script Object +//---------------------------------------------------- +MemoryScriptObject::MemoryScriptObject(QObject* parent) + : QObject(parent) +{ + script = qobject_cast(parent); +} +//---------------------------------------------------- +MemoryScriptObject::~MemoryScriptObject() +{ +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::readByte(int address) +{ + return GetMem(address); +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::readByteUnsigned(int address) +{ + return GetMem(address); +} +//---------------------------------------------------- +int8_t MemoryScriptObject::readByteSigned(int address) +{ + return static_cast(GetMem(address)); +} +//---------------------------------------------------- +uint16_t MemoryScriptObject::readWord(int addressLow, int addressHigh) +{ + // little endian, unless the high byte address is specified as a 2nd parameter + if (addressHigh < 0) + { + addressHigh = addressLow + 1; + } + uint16_t result = GetMem(addressLow) | (GetMem(addressHigh) << 8); + return result; +} +//---------------------------------------------------- +uint16_t MemoryScriptObject::readWordUnsigned(int addressLow, int addressHigh) +{ + // little endian, unless the high byte address is specified as a 2nd parameter + if (addressHigh < 0) + { + addressHigh = addressLow + 1; + } + uint16_t result = GetMem(addressLow) | (GetMem(addressHigh) << 8); + return result; +} +//---------------------------------------------------- +int16_t MemoryScriptObject::readWordSigned(int addressLow, int addressHigh) +{ + // little endian, unless the high byte address is specified as a 2nd parameter + if (addressHigh < 0) + { + addressHigh = addressLow + 1; + } + uint16_t result = GetMem(addressLow) | (GetMem(addressHigh) << 8); + return static_cast(result); +} +//---------------------------------------------------- +void MemoryScriptObject::writeByte(int address, int value) +{ + uint32_t A = address; + uint8_t V = value; + + if (A < 0x10000) + { + BWrite[A](A, V); + } +} +//---------------------------------------------------- +uint16_t MemoryScriptObject::getRegisterPC() +{ + return X.PC; +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::getRegisterA() +{ + return X.A; +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::getRegisterX() +{ + return X.X; +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::getRegisterY() +{ + return X.Y; +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::getRegisterS() +{ + return X.S; +} +//---------------------------------------------------- +uint8_t MemoryScriptObject::getRegisterP() +{ + return X.P; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterPC(uint16_t v) +{ + X.PC = v; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterA(uint8_t v) +{ + X.A = v; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterX(uint8_t v) +{ + X.X = v; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterY(uint8_t v) +{ + X.Y = v; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterS(uint8_t v) +{ + X.S = v; +} +//---------------------------------------------------- +void MemoryScriptObject::setRegisterP(uint8_t v) +{ + X.P = v; +} +//---------------------------------------------------- //---- Qt Script Instance //---------------------------------------------------- QtScriptInstance::QtScriptInstance(QObject* parent) @@ -207,15 +340,18 @@ QtScriptInstance::QtScriptInstance(QObject* parent) QScriptDialog_t* win = qobject_cast(parent); emu = new EmuScriptObject(this); + mem = new MemoryScriptObject(this); if (win != nullptr) { dialog = win; emu->setDialog(dialog); + mem->setDialog(dialog); } engine = new QJSEngine(nullptr); emu->setEngine(engine); + mem->setEngine(engine); configEngine(); @@ -263,6 +399,10 @@ int QtScriptInstance::configEngine() engine->globalObject().setProperty("emu", emuObject); + QJSValue memObject = engine->newQObject(mem); + + engine->globalObject().setProperty("memory", memObject); + QJSValue guiObject = engine->newQObject(this); engine->globalObject().setProperty("gui", guiObject); diff --git a/src/drivers/Qt/QtScriptManager.h b/src/drivers/Qt/QtScriptManager.h index 1474897c..25b3bc11 100644 --- a/src/drivers/Qt/QtScriptManager.h +++ b/src/drivers/Qt/QtScriptManager.h @@ -69,6 +69,43 @@ public slots: }; +class MemoryScriptObject: public QObject +{ + Q_OBJECT +public: + MemoryScriptObject(QObject* parent = nullptr); + ~MemoryScriptObject(); + + void setEngine(QJSEngine* _engine){ engine = _engine; } + void setDialog(QScriptDialog_t* _dialog){ dialog = _dialog; } + +private: + QJSEngine* engine = nullptr; + QScriptDialog_t* dialog = nullptr; + QtScriptInstance* script = nullptr; + +public slots: + Q_INVOKABLE uint8_t readByte(int address); + Q_INVOKABLE int8_t readByteSigned(int address); + Q_INVOKABLE uint8_t readByteUnsigned(int address); + Q_INVOKABLE uint16_t readWord(int addressLow, int addressHigh = -1); + Q_INVOKABLE int16_t readWordSigned(int addressLow, int addressHigh = -1); + Q_INVOKABLE uint16_t readWordUnsigned(int addressLow, int addressHigh = -1); + Q_INVOKABLE void writeByte(int address, int value); + Q_INVOKABLE uint16_t getRegisterPC(); + Q_INVOKABLE uint8_t getRegisterA(); + Q_INVOKABLE uint8_t getRegisterX(); + Q_INVOKABLE uint8_t getRegisterY(); + Q_INVOKABLE uint8_t getRegisterS(); + Q_INVOKABLE uint8_t getRegisterP(); + Q_INVOKABLE void setRegisterPC(uint16_t v); + Q_INVOKABLE void setRegisterA(uint8_t v); + Q_INVOKABLE void setRegisterX(uint8_t v); + Q_INVOKABLE void setRegisterY(uint8_t v); + Q_INVOKABLE void setRegisterS(uint8_t v); + Q_INVOKABLE void setRegisterP(uint8_t v); +}; + class QtScriptInstance : public QObject { Q_OBJECT @@ -98,6 +135,7 @@ private: QJSEngine* engine = nullptr; QScriptDialog_t* dialog = nullptr; EmuScriptObject* emu = nullptr; + MemoryScriptObject* mem = nullptr; QWidget* ui_rootWidget = nullptr; QJSValue onFrameBeginCallback; QJSValue onFrameFinishCallback;