Skip to content

Commit

Permalink
GBA: Added option to enable a log API that should be compatible with …
Browse files Browse the repository at this point in the history
…mGBA's
  • Loading branch information
SourMesen committed Jun 24, 2024
1 parent 88e60a4 commit 08adf89
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ClInclude Include="GBA\GbaTimer.h" />
<ClInclude Include="GBA\GbaTypes.h" />
<ClInclude Include="GBA\Input\GbaController.h" />
<ClInclude Include="GBA\Debugger\MgbaLogHandler.h" />
<ClInclude Include="NES\HdPacks\HdBuilderPpu.h" />
<ClInclude Include="NES\HdPacks\HdPackBuilder.h" />
<ClInclude Include="PCE\CdRom\PceCdSeekDelay.h" />
Expand Down
3 changes: 3 additions & 0 deletions Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,9 @@
<ClInclude Include="GBA\Debugger\GbaCodeDataLogger.h">
<Filter>GBA\Debugger</Filter>
</ClInclude>
<ClInclude Include="GBA\Debugger\MgbaLogHandler.h">
<Filter>GBA\Debugger</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Shared\Video\RotateFilter.cpp">
Expand Down
46 changes: 46 additions & 0 deletions Core/GBA/Debugger/MgbaLogHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once
#include "pch.h"
#include "Shared/MessageManager.h"

class MgbaLogHandler
{
private:
uint8_t _enableMarker[2] = {};
bool _enabled = false;
uint8_t _message[257] = {};

void UpdateEnableFlag()
{
_enabled |= _enableMarker[1] == 0xC0 && _enableMarker[0] == 0xDE;
}

public:
void Write(uint32_t addr, uint8_t value)
{
if(_enabled && addr < 0xFFF700) {
_message[addr & 0xFF] = value;
} else if(addr == 0xFFF780) {
_enableMarker[0] = value;
UpdateEnableFlag();
} else if(addr == 0xFFF781) {
_enableMarker[1] = value;
UpdateEnableFlag();
} else if(addr == 0xFFF701) {
if(_enabled && (value & 0x01)) {
if(_message[0] != 0) {
MessageManager::Log("[Debug] " + string((char*)_message));
}
memset(_message, 0, sizeof(_message));
}
}
}

uint8_t Read(uint32_t addr)
{
switch(addr & 0x03) {
case 0: return _enabled ? 0x1D : 0x00;
case 1: return _enabled ? 0xEA : 0x00;
default: return 0;
}
}
};
13 changes: 13 additions & 0 deletions Core/GBA/GbaMemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "GBA/GbaSerial.h"
#include "GBA/GbaControlManager.h"
#include "GBA/GbaRomPrefetch.h"
#include "GBA/Debugger/MgbaLogHandler.h"
#include "GBA/APU/GbaApu.h"
#include "GBA/Cart/GbaCart.h"
#include "Shared/Emulator.h"
Expand All @@ -31,6 +32,8 @@ GbaMemoryManager::GbaMemoryManager(Emulator* emu, GbaConsole* console, GbaPpu* p
_cart = cart;
_serial = serial;
_prefetch = prefetch;

_mgbaLog.reset(new MgbaLogHandler());

_prgRom = (uint8_t*)emu->GetMemory(MemoryType::GbaPrgRom).Memory;
_prgRomSize = emu->GetMemory(MemoryType::GbaPrgRom).Size;
Expand Down Expand Up @@ -397,6 +400,10 @@ void GbaMemoryManager::InternalWrite(GbaAccessModeVal mode, uint32_t addr, uint8
//registers
if(addr < 0x3FF) {
WriteRegister(mode, addr, value);
} else if(addr >= 0xFFF600 && addr <= 0xFFF783) {
if(_emu->GetSettings()->GetGbaConfig().EnableMgbaLogApi) {
_mgbaLog->Write(addr, value);
}
}
break;

Expand Down Expand Up @@ -506,6 +513,12 @@ uint32_t GbaMemoryManager::ReadRegister(uint32_t addr)
case 0x303: return 0;

default:
if(addr >= 0xFFF700 && addr <= 0xFFF703) {
if(_emu->GetSettings()->GetGbaConfig().EnableMgbaLogApi) {
return _mgbaLog->Read(addr);
}
}

LogDebug("Read unimplemented register: " + HexUtilities::ToHex32(addr));
return _state.CartOpenBus[addr & 0x01];
}
Expand Down
3 changes: 3 additions & 0 deletions Core/GBA/GbaMemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GbaApu;
class GbaCart;
class GbaSerial;
class GbaRomPrefetch;
class MgbaLogHandler;

class GbaMemoryManager final : public ISerializable
{
Expand All @@ -31,6 +32,8 @@ class GbaMemoryManager final : public ISerializable
GbaSerial* _serial;
GbaRomPrefetch* _prefetch;

unique_ptr<MgbaLogHandler> _mgbaLog;

uint64_t _masterClock = 0;
bool _hasPendingUpdates = false;
bool _hasPendingLateUpdates = false;
Expand Down
1 change: 1 addition & 0 deletions Core/Shared/SettingTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ struct GbaConfig
RamState RamPowerOnState = RamState::AllZeros;
GbaSaveType SaveType = GbaSaveType::AutoDetect;
bool AllowInvalidInput = false;
bool EnableMgbaLogApi = false;

uint32_t ChannelAVol = 100;
uint32_t ChannelBVol = 100;
Expand Down
3 changes: 3 additions & 0 deletions UI/Config/GbaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class GbaConfig : BaseConfig<GbaConfig>
[Reactive] public RamState RamPowerOnState { get; set; } = RamState.AllZeros;
[Reactive] public GbaSaveType SaveType { get; set; } = GbaSaveType.AutoDetect;
[Reactive] public bool AllowInvalidInput { get; set; } = false;
[Reactive] public bool EnableMgbaLogApi { get; set; } = false;

[Reactive][MinMax(0, 100)] public UInt32 Square1Vol { get; set; } = 100;
[Reactive][MinMax(0, 100)] public UInt32 Square2Vol { get; set; } = 100;
Expand All @@ -53,6 +54,7 @@ public void ApplyConfig()
RamPowerOnState = RamPowerOnState,
SaveType = SaveType,
AllowInvalidInput = AllowInvalidInput,
EnableMgbaLogApi = EnableMgbaLogApi,

ChannelAVol = ChannelAVol,
ChannelBVol = ChannelBVol,
Expand Down Expand Up @@ -123,6 +125,7 @@ public struct InteropGbaConfig
public RamState RamPowerOnState;
public GbaSaveType SaveType;
[MarshalAs(UnmanagedType.I1)] public bool AllowInvalidInput;
[MarshalAs(UnmanagedType.I1)] public bool EnableMgbaLogApi;

public UInt32 ChannelAVol;
public UInt32 ChannelBVol;
Expand Down
1 change: 1 addition & 0 deletions UI/Localization/resources.en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
<Control ID="lblSaveType">Save Type:</Control>
<Control ID="lblRamPowerOnState">Default power on state for RAM: </Control>
<Control ID="chkAllowInvalidInput">Allow invalid input (e.g Down + Up or Left + Right at the same time)</Control>
<Control ID="chkEnableMgbaLogApi">Enable mGBA log API</Control>

<Control ID="tpgVideo">Video</Control>
<Control ID="lblLcdSettings">LCD Settings</Control>
Expand Down
1 change: 1 addition & 0 deletions UI/Views/GbaConfigView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<c:EnumComboBox SelectedItem="{Binding Config.SaveType}" MinWidth="150" />
</StackPanel>
<c:CheckBoxWarning IsChecked="{Binding Config.AllowInvalidInput}" Text="{l:Translate chkAllowInvalidInput}" />
<c:CheckBoxWarning IsChecked="{Binding Config.EnableMgbaLogApi}" Text="{l:Translate chkEnableMgbaLogApi}" />
</StackPanel>
</ScrollViewer>
</TabItem>
Expand Down

0 comments on commit 08adf89

Please sign in to comment.