From 50d7bbf4921eca32f90d9d427e2e59f3a227bec7 Mon Sep 17 00:00:00 2001 From: Sour Date: Wed, 20 Dec 2017 22:11:36 -0500 Subject: [PATCH] Lua: Added getRomInfo/getLogWindowLog and changed getState to return doubles for apu channel frequencies --- Core/DeltaModulationChannel.cpp | 2 +- Core/LuaApi.cpp | 40 +++++++++++++++++++++++++++++---- Core/LuaApi.h | 4 +++- Core/NoiseChannel.h | 2 +- Core/SquareChannel.h | 2 +- Core/TriangleChannel.h | 2 +- Core/Types.h | 8 +++---- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Core/DeltaModulationChannel.cpp b/Core/DeltaModulationChannel.cpp index 06094ba0..e78f2641 100644 --- a/Core/DeltaModulationChannel.cpp +++ b/Core/DeltaModulationChannel.cpp @@ -216,7 +216,7 @@ ApuDmcState DeltaModulationChannel::GetState() { ApuDmcState state; state.BytesRemaining = _bytesRemaining; - state.Frequency = (uint32_t)(CPU::GetClockRate(GetNesModel()) / 32.0 / (_period + 1)); + state.Frequency = CPU::GetClockRate(GetNesModel()) / 32.0 / (_period + 1); state.IrqEnabled = _irqEnabled; state.Loop = _loopFlag; state.OutputVolume = _lastOutput; diff --git a/Core/LuaApi.cpp b/Core/LuaApi.cpp index aff3fb2b..9173ed5b 100644 --- a/Core/LuaApi.cpp +++ b/Core/LuaApi.cpp @@ -21,7 +21,9 @@ #include "KeyManager.h" #define lua_pushintvalue(name, value) lua_pushliteral(lua, #name); lua_pushinteger(lua, (int)value); lua_settable(lua, -3); +#define lua_pushdoublevalue(name, value) lua_pushliteral(lua, #name); lua_pushnumber(lua, (double)value); lua_settable(lua, -3); #define lua_pushboolvalue(name, value) lua_pushliteral(lua, #name); lua_pushboolean(lua, (int)value); lua_settable(lua, -3); +#define lua_pushstringvalue(name, value) lua_pushliteral(lua, #name); lua_pushstring(lua, value.c_str()); lua_settable(lua, -3); #define lua_starttable(name) lua_pushliteral(lua, name); lua_newtable(lua); #define lua_endtable() lua_settable(lua, -3); #define lua_readint(name, dest) lua_getfield(lua, -1, #name); dest = l.ReadInteger(); @@ -95,6 +97,8 @@ int LuaApi::GetLibrary(lua_State *lua) { "setState", LuaApi::SetState }, { "getState", LuaApi::GetState }, { "getScriptDataFolder", LuaApi::GetScriptDataFolder }, + { "getRomInfo", LuaApi::GetRomInfo }, + { "getLogWindowLog", LuaApi::GetLogWindowLog }, { NULL,NULL } }; @@ -631,6 +635,34 @@ int LuaApi::GetScriptDataFolder(lua_State *lua) return l.ReturnCount(); } +int LuaApi::GetRomInfo(lua_State *lua) +{ + LuaCallHelper l(lua); + checkparams(); + + lua_newtable(lua); + lua_pushstringvalue(name, Console::GetRomName()); + lua_pushstringvalue(path, ((string)Console::GetRomPath())); + + HashInfo hashInfo = Console::GetHashInfo(); + lua_pushintvalue(fileCrc32Hash, hashInfo.Crc32Hash); + lua_pushstringvalue(fileSha1Hash, hashInfo.Sha1Hash); + lua_pushintvalue(prgChrCrc32Hash, hashInfo.PrgCrc32Hash); + lua_pushstringvalue(prgChrMd5Hash, hashInfo.PrgChrMd5Hash); + lua_pushintvalue(format, Console::GetRomFormat()); + lua_pushboolvalue(isChrRam, Console::IsChrRam()); + return 1; +} + +int LuaApi::GetLogWindowLog(lua_State *lua) +{ + LuaCallHelper l(lua); + checkparams(); + + l.Return(MessageManager::GetLog()); + return l.ReturnCount(); +} + int LuaApi::SetState(lua_State *lua) { LuaCallHelper l(lua); @@ -806,7 +838,7 @@ int LuaApi::GetState(lua_State *lua) lua_starttable("triangle"); lua_pushboolvalue(enabled, state.APU.Triangle.Enabled); - lua_pushintvalue(frequency, state.APU.Triangle.Frequency); + lua_pushdoublevalue(frequency, state.APU.Triangle.Frequency); PushLengthCounterState(lua, state.APU.Triangle.LengthCounter); lua_pushintvalue(outputVolume, state.APU.Triangle.OutputVolume); lua_pushintvalue(period, state.APU.Triangle.Period); @@ -815,7 +847,7 @@ int LuaApi::GetState(lua_State *lua) lua_starttable("noise"); lua_pushboolvalue(enabled, state.APU.Noise.Enabled); - lua_pushintvalue(frequency, state.APU.Noise.Frequency); + lua_pushdoublevalue(frequency, state.APU.Noise.Frequency); PushEnvelopeState(lua, state.APU.Noise.Envelope); PushLengthCounterState(lua, state.APU.Noise.LengthCounter); lua_pushboolvalue(modeFlag, state.APU.Noise.ModeFlag); @@ -826,7 +858,7 @@ int LuaApi::GetState(lua_State *lua) lua_starttable("dmc"); lua_pushintvalue(bytesRemaining, state.APU.Dmc.BytesRemaining); - lua_pushintvalue(frequency, state.APU.Dmc.Frequency); + lua_pushdoublevalue(frequency, state.APU.Dmc.Frequency); lua_pushboolvalue(irqEnabled, state.APU.Dmc.IrqEnabled); lua_pushboolvalue(loop, state.APU.Dmc.Loop); lua_pushintvalue(outputVolume, state.APU.Dmc.OutputVolume); @@ -850,7 +882,7 @@ void LuaApi::PushSquareState(lua_State *lua, ApuSquareState & state) { lua_pushintvalue(duty, state.Duty); lua_pushintvalue(dutyPosition, state.DutyPosition); - lua_pushintvalue(frequency, state.Frequency); + lua_pushdoublevalue(frequency, state.Frequency); lua_pushintvalue(period, state.Period); lua_pushboolvalue(sweepEnabled, state.SweepEnabled); lua_pushboolvalue(sweepNegate, state.SweepNegate); diff --git a/Core/LuaApi.h b/Core/LuaApi.h index 15858bfe..19c75110 100644 --- a/Core/LuaApi.h +++ b/Core/LuaApi.h @@ -65,7 +65,9 @@ public: static int AddCheat(lua_State *lua); static int ClearCheats(lua_State *lua); - static int GetScriptDataFolder(lua_State * lua); + static int GetScriptDataFolder(lua_State *lua); + static int GetRomInfo(lua_State *lua); + static int GetLogWindowLog(lua_State *lua); static int SetState(lua_State *lua); static int GetState(lua_State *lua); diff --git a/Core/NoiseChannel.h b/Core/NoiseChannel.h index 05312115..f197da9f 100644 --- a/Core/NoiseChannel.h +++ b/Core/NoiseChannel.h @@ -91,7 +91,7 @@ public: ApuNoiseState state; state.Enabled = _enabled; state.Envelope = ApuEnvelope::GetState(); - state.Frequency = (uint32_t)((CPU::GetClockRate(GetNesModel()) / (_period + 1)) / (_modeFlag ? 93 : 1)); + state.Frequency = CPU::GetClockRate(GetNesModel()) / (_period + 1) / (_modeFlag ? 93 : 1); state.LengthCounter = ApuLengthCounter::GetState(); state.ModeFlag = _modeFlag; state.OutputVolume = _lastOutput; diff --git a/Core/SquareChannel.h b/Core/SquareChannel.h index 05197eae..76baddc6 100644 --- a/Core/SquareChannel.h +++ b/Core/SquareChannel.h @@ -191,7 +191,7 @@ public: state.DutyPosition = _dutyPos; state.Enabled = _enabled; state.Envelope = ApuEnvelope::GetState(); - state.Frequency = (uint32_t)(CPU::GetClockRate(GetNesModel()) / 16.0 / (_realPeriod + 1)); + state.Frequency = CPU::GetClockRate(GetNesModel()) / 16.0 / (_realPeriod + 1); state.LengthCounter = ApuLengthCounter::GetState(); state.OutputVolume = _lastOutput; state.Period = _realPeriod; diff --git a/Core/TriangleChannel.h b/Core/TriangleChannel.h index 0074c448..aa00fa17 100644 --- a/Core/TriangleChannel.h +++ b/Core/TriangleChannel.h @@ -105,7 +105,7 @@ public: { ApuTriangleState state; state.Enabled = _enabled; - state.Frequency = (uint32_t)(CPU::GetClockRate(GetNesModel()) / 32.0 / (_period + 1)); + state.Frequency = CPU::GetClockRate(GetNesModel()) / 32.0 / (_period + 1); state.LengthCounter = ApuLengthCounter::GetState(); state.OutputVolume = _lastOutput; state.Period = _period; diff --git a/Core/Types.h b/Core/Types.h index a6f924fe..2066c5a3 100644 --- a/Core/Types.h +++ b/Core/Types.h @@ -168,7 +168,7 @@ struct ApuSquareState bool Enabled; uint8_t OutputVolume; - uint32_t Frequency; + double Frequency; ApuLengthCounterState LengthCounter; ApuEnvelopeState Envelope; @@ -180,7 +180,7 @@ struct ApuTriangleState uint8_t SequencePosition; bool Enabled; - uint32_t Frequency; + double Frequency; uint8_t OutputVolume; ApuLengthCounterState LengthCounter; @@ -193,7 +193,7 @@ struct ApuNoiseState bool ModeFlag; bool Enabled; - uint32_t Frequency; + double Frequency; uint8_t OutputVolume; ApuLengthCounterState LengthCounter; @@ -210,7 +210,7 @@ struct ApuDmcState uint16_t Period; uint16_t BytesRemaining; - uint32_t Frequency; + double Frequency; uint8_t OutputVolume; };