Skip to content

Commit d9ae6d9

Browse files
C# side changes for melonDS JIT support
1 parent 5aeb1ad commit d9ae6d9

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/LibMelonDS.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public struct ConsoleCreationArgs
8080
public bool EnableDLDI;
8181
public bool EnableDSiSDCard;
8282

83+
public bool EnableJIT;
84+
public int MaxBranchSize;
85+
public bool LiteralOptimizations;
86+
public bool BranchOptimizations;
87+
8388
public NDS.NDSSettings.AudioBitDepthType BitDepth;
8489
public NDS.NDSSettings.AudioInterpolationType Interpolation;
8590

src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.IDebuggable.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,20 @@ public void SetCpuRegister(string register, int value)
4949

5050
public long TotalExecutedCycles => CycleCount + _core.GetCallbackCycleOffset(_console);
5151

52-
public IMemoryCallbackSystem MemoryCallbacks => _memoryCallbacks;
52+
public IMemoryCallbackSystem MemoryCallbacks
53+
{
54+
get
55+
{
56+
if (!_activeSyncSettings.EnableJIT)
57+
{
58+
return _memoryCallbacks;
59+
}
60+
61+
#pragma warning disable CA1065
62+
throw new NotImplementedException();
63+
#pragma warning restore CA1065
64+
}
65+
}
5366

5467
// FIXME: internally the code actually just does this for either bus (probably don't want to bother adding support)
5568
private readonly MemoryCallbackSystem _memoryCallbacks = new([ "ARM9 System Bus" ]);
@@ -67,23 +80,23 @@ LibMelonDS.MemoryCallback CreateCallback(MemoryCallbackFlags flags, Func<bool> g
6780
{
6881
if (getHasCBOfType())
6982
{
70-
MemoryCallbacks.CallMemoryCallbacks(address, 0, rawFlags, "ARM9 System Bus");
83+
_memoryCallbacks.CallMemoryCallbacks(address, 0, rawFlags, "ARM9 System Bus");
7184
}
7285
};
7386
}
7487

75-
_readCallback = CreateCallback(MemoryCallbackFlags.AccessRead, () => MemoryCallbacks.HasReads);
76-
_writeCallback = CreateCallback(MemoryCallbackFlags.AccessWrite, () => MemoryCallbacks.HasWrites);
77-
_execCallback = CreateCallback(MemoryCallbackFlags.AccessExecute, () => MemoryCallbacks.HasExecutes);
88+
_readCallback = CreateCallback(MemoryCallbackFlags.AccessRead, () => _memoryCallbacks.HasReads);
89+
_writeCallback = CreateCallback(MemoryCallbackFlags.AccessWrite, () => _memoryCallbacks.HasWrites);
90+
_execCallback = CreateCallback(MemoryCallbackFlags.AccessExecute, () => _memoryCallbacks.HasExecutes);
7891

7992
_memoryCallbacks.ActiveChanged += SetMemoryCallbacks;
8093
}
8194

8295
private void SetMemoryCallbacks()
8396
{
84-
_core.SetMemoryCallback(0, MemoryCallbacks.HasReads ? _readCallback : null);
85-
_core.SetMemoryCallback(1, MemoryCallbacks.HasWrites ? _writeCallback : null);
86-
_core.SetMemoryCallback(2, MemoryCallbacks.HasExecutes ? _execCallback : null);
97+
_core.SetMemoryCallback(0, _memoryCallbacks.HasReads ? _readCallback : null);
98+
_core.SetMemoryCallback(1, _memoryCallbacks.HasWrites ? _writeCallback : null);
99+
_core.SetMemoryCallback(2, _memoryCallbacks.HasExecutes ? _execCallback : null);
87100
}
88101
}
89102
}

src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISettable.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,33 @@ public int GLScaleFactor
207207
[DefaultValue(false)]
208208
public bool GLHiResCoordinates { get; set; }
209209

210+
[DisplayName("Enable JIT Recompiler")]
211+
[Description("Enables JIT recompilation, offering a speed boost at the cost of accuracy. Debugging tools cannot be used with the JIT. Savestate size will be much larger with the JIT.")]
212+
[DefaultValue(false)]
213+
public bool EnableJIT { get; set; }
214+
215+
[JsonIgnore]
216+
private int _jitmaxbranchsize;
217+
218+
[DisplayName("JIT Max Branch Size")]
219+
[Description("Sets the maximum size of a JIT block. Higher values gain more speed but have a higher risk of timing problems.")]
220+
[DefaultValue(32)]
221+
public int JITMaxBranchSize
222+
{
223+
get => _jitmaxbranchsize;
224+
set => _jitmaxbranchsize = Math.Max(1, Math.Min(value, 32));
225+
}
226+
227+
[DisplayName("JIT Literal Optimizations")]
228+
[Description("Performs literal optimizations in the JIT recompiler.")]
229+
[DefaultValue(true)]
230+
public bool JITLiteralOptimizations { get; set; }
231+
232+
[DisplayName("JIT Branch Optimizations")]
233+
[Description("Performs branch optimizations in the JIT recompiler.")]
234+
[DefaultValue(true)]
235+
public bool JITBranchOptimizations { get; set; }
236+
210237
[JsonIgnore]
211238
private DateTime _initaltime;
212239

src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,11 @@ static void InitIv(Span<byte> iv, ReadOnlySpan<byte> nonce)
518518
consoleCreationArgs.EnableDLDI = _activeSyncSettings.EnableDLDI;
519519
consoleCreationArgs.EnableDSiSDCard = _activeSyncSettings.EnableDSiSDCard;
520520

521+
consoleCreationArgs.EnableJIT = _activeSyncSettings.EnableJIT;
522+
consoleCreationArgs.MaxBranchSize = _activeSyncSettings.JITMaxBranchSize;
523+
consoleCreationArgs.LiteralOptimizations = _activeSyncSettings.JITLiteralOptimizations;
524+
consoleCreationArgs.BranchOptimizations = _activeSyncSettings.JITBranchOptimizations;
525+
521526
consoleCreationArgs.BitDepth = _settings.AudioBitDepth;
522527
consoleCreationArgs.Interpolation = _settings.AudioInterpolation;
523528

@@ -598,9 +603,13 @@ static void InitIv(Span<byte> iv, ReadOnlySpan<byte> nonce)
598603
_disassembler = new(_core);
599604
_serviceProvider.Register<IDisassemblable>(_disassembler);
600605

601-
const string TRACE_HEADER = "ARM9+ARM7: Opcode address, opcode, registers (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, SP, LR, PC, Cy, CpuMode)";
602-
Tracer = new TraceBuffer(TRACE_HEADER);
603-
_serviceProvider.Register(Tracer);
606+
// tracelogger cannot be used with the JIT
607+
if (!_activeSyncSettings.EnableJIT)
608+
{
609+
const string TRACE_HEADER = "ARM9+ARM7: Opcode address, opcode, registers (r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, SP, LR, PC, Cy, CpuMode)";
610+
Tracer = new TraceBuffer(TRACE_HEADER);
611+
_serviceProvider.Register(Tracer);
612+
}
604613

605614
if (_glContext != null)
606615
{

0 commit comments

Comments
 (0)