Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,15 @@ private void MemoryDomainsMenuItem_DropDownOpened(object sender, EventArgs e)
Checked = _domain.Name == _romDomain.Name,
};

MemoryDomainsMenuItem.DropDownItems.Add(new ToolStripSeparator());
var n = MemoryDomains.Count - 1;
if (MemoryDomains[n] is RegistersMemoryDomain)
{
MemoryDomainsMenuItem.DropDownItems.Insert(n, new ToolStripSeparator());
}
else
{
MemoryDomainsMenuItem.DropDownItems.Add(new ToolStripSeparator());
}
MemoryDomainsMenuItem.DropDownItems.Add(romMenuItem);

romMenuItem.Click += (o, ev) => SetMemoryDomain(_romDomain.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/BizHawk.Client.EmuHawk/tools/Watch/WatchEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private void RamWatchNewWatch_Load(object sender, EventArgs e)
{
default:
case Mode.New:
SelectedWidth = MemoryDomains.First().WordSize;
SelectedWidth = MemoryDomains[0].WordSize;
break;
case Mode.Duplicate:
case Mode.Edit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public bool Has(string name)
return this.Any(md => md.Name == name);
}

public MemoryDomainList(IList<MemoryDomain> domains)
: base(domains)
public MemoryDomainList(IList<MemoryDomain> domains, IDebuggable/*?*/ debuggableCore = null)
: base(debuggableCore is null
? domains
: domains.Append(new RegistersMemoryDomain(debuggableCore)).ToArray())
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

using BizHawk.Common.StringExtensions;

using LookupEntry = (string RegName, byte RegWidthBytes, byte ByteOffset);

namespace BizHawk.Emulation.Common
{
public sealed class RegistersMemoryDomain : MemoryDomain
{
private const string ERR_FMT_STR_INVALID_ADDR = "invalid address, must be in 0..<{0}";

private static ImmutableArray<LookupEntry> GenLookup(IDictionary<string, RegisterValue> regs)
{
List<LookupEntry> entries = new();
foreach (var (name, val) in regs)
{
var widthBytes = unchecked((byte) (val.BitSize / 8));
if (val.BitSize % 8 is not 0) widthBytes++;
byte i = widthBytes;
while (i > 0) entries.Add((RegName: name, RegWidthBytes: widthBytes, ByteOffset: --i));
}
while (entries.Count % sizeof(uint) is not 0) entries.Add((string.Empty, 0, 0)); // padding for Hex Editor
return entries.ToImmutableArray();
}

private readonly IDebuggable _debuggableCore;

private readonly ImmutableArray<LookupEntry> _lookup;

public RegistersMemoryDomain(IDebuggable debuggableCore)
{
_debuggableCore = debuggableCore;
var regs = _debuggableCore.GetCpuFlagsAndRegisters();
_lookup = GenLookup(regs);
EndianType = Endian.Big;
var cpuName = regs.Keys.CommonPrefix();
Name = cpuName.Length is 0 ? "CPU registers" : $"{new string(cpuName)/*trailing space*/}registers";
Size = _lookup.Sum(static entry => entry.RegWidthBytes);
WordSize = sizeof(byte);
Writable = !debuggableCore.GetType().GetMethod("SetCpuRegister").CustomAttributes
.Any(ad => ad.AttributeType == typeof(FeatureNotImplementedAttribute));
}

public override byte PeekByte(long addr)
{
if (addr < 0 || _lookup.Length <= addr) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: string.Format(ERR_FMT_STR_INVALID_ADDR, _lookup.Length));
var (name, width, offset) = _lookup[unchecked((int) addr)];
if (width is 0) return default; // padding for Hex Editor
var toReturn = unchecked((uint) _debuggableCore.GetCpuFlagsAndRegisters()[name].Value);
toReturn >>= 8 * offset;
return unchecked((byte) toReturn);
}

public override void PokeByte(long addr, byte val)
{
if (addr < 0 || _lookup.Length <= addr) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: string.Format(ERR_FMT_STR_INVALID_ADDR, _lookup.Length));
var (name, width, offset) = _lookup[unchecked((int) addr)];
if (width is 0) return; // padding for Hex Editor
switch (width)
{
case 8 or 7 or 6 or 5:
throw new NotSupportedException($"{nameof(IDebuggable)}.{nameof(IDebuggable.SetCpuRegister)} does not support poking registers wider than 32 bits");
case 4 or 3:
{
var toWrite = unchecked((uint) _debuggableCore.GetCpuFlagsAndRegisters()[name].Value);
switch (offset)
{
case 3:
toWrite &= 0x00FF_FFFFU;
toWrite |= unchecked((ushort) (val << 24));
break;
case 2:
toWrite &= 0xFF00_FFFFU;
toWrite |= unchecked((ushort) (val << 16));
break;
case 1:
toWrite &= 0xFFFF_00FFU;
toWrite |= unchecked((ushort) (val << 8));
break;
default:
toWrite &= 0xFFFF_FF00U;
toWrite |= val;
break;
}
_debuggableCore.SetCpuRegister(name, unchecked((int) toWrite));
break;
}
case 2:
{
var toWrite = unchecked((ushort) _debuggableCore.GetCpuFlagsAndRegisters()[name].Value);
if (offset is 1)
{
toWrite &= 0x00FF;
toWrite |= unchecked((ushort) (val << 8));
}
else
{
toWrite &= 0xFF00;
toWrite |= val;
}
_debuggableCore.SetCpuRegister(name, toWrite);
break;
}
case 1:
_debuggableCore.SetCpuRegister(name, val);
break;
default:
throw new InvalidOperationException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace BizHawk.Emulation.Common
/// If this service is available the client will expose many RAM related tools such as the Hex Editor, RAM Search/Watch, and Cheats
/// In addition, this is an essential service for effective LUA scripting, and many other tools
/// </summary>
public interface IMemoryDomains : IEnumerable<MemoryDomain>, IEmulatorService
public interface IMemoryDomains : IReadOnlyList<MemoryDomain>, IEmulatorService
{
MemoryDomain? this[string name] { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void InitMemoryDomains()
LibEmu83.TI83_WriteMemory(Context, (ushort)addr, val);
}, 1));

MemoryDomains = new MemoryDomainList(_memoryDomains);
MemoryDomains = new MemoryDomainList(_memoryDomains, this);
_serviceProvider.Register(MemoryDomains);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList());
_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this);
((BasicServiceProvider) ServiceProvider).Register(_memoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList());
memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(memoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void SetupMemoryDomains()

domains.Add(systemBusDomain);

_memoryDomains = new MemoryDomainList(domains);
_memoryDomains = new MemoryDomainList(domains, this);
((BasicServiceProvider) ServiceProvider).Register(_memoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void SetupMemoryDomains()
domains.AddRange(_board.CartPort.CreateMemoryDomains());
}

_memoryDomains = new MemoryDomainList(domains);
_memoryDomains = new MemoryDomainList(domains, this);
((BasicServiceProvider)ServiceProvider).Register(_memoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList());
memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(memoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

MemoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList());
MemoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this);
((BasicServiceProvider)ServiceProvider).Register(MemoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void SetupMemoryDomains()
1)
};

_memoryDomains = new MemoryDomainList(domains);
_memoryDomains = new MemoryDomainList(domains, this);
((BasicServiceProvider) ServiceProvider).Register(_memoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList());
_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this);
((BasicServiceProvider)ServiceProvider).Register<IMemoryDomains>(_memoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void SetupMemoryDomains()

SyncAllByteArrayDomains();

_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList()) { MainMemory = domains[0] };
_memoryDomains = new MemoryDomainList(_byteArrayDomains.Values.Concat(domains).ToList(), this) { MainMemory = domains[0] };
((BasicServiceProvider)ServiceProvider).Register(_memoryDomains);

_memoryDomainsInit = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void SetupMemoryDomains()
1),
};

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void SetupMemoryDomains()
)
};

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
((BasicServiceProvider) ServiceProvider).Register(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void SetupMemoryDomains()
domains.Add(CartRam);
}

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void SetMemoryDomains()

mm.Add(Api.exe.GetPagesDomain());

_memoryDomains = new(mm);
_memoryDomains = new(mm, this);
((BasicServiceProvider) ServiceProvider).Register<IMemoryDomains>(_memoryDomains);

_memoryDomains.MainMemory = _memoryDomains[_isSGB ? "SGB WRAM" : "WRAM"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void CreateMemoryDomains(int romsize)
}, 4)
};

_memoryDomains = new(mm);
_memoryDomains = new(mm, this);
WireMemoryDomainPointers();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void SetupMemoryDomains()
domains.Add(CartRam);
}

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void SetupMemoryDomains()
domains.Add(cartRamR);
}

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void SetupMemoryDomains()
domains.Add(cartRamR);
}

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void SetupMemoryDomains()
domains.Add(cartRamD);
}

MemoryDomains = new MemoryDomainList(domains);
MemoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private void InitMemoryDomains()

CreateMemoryDomain(LibGambatte.MemoryAreas.cartram, "CartRAM");

MemoryDomains = new MemoryDomainList(_memoryDomains);
MemoryDomains = new MemoryDomainList(_memoryDomains, this);
_serviceProvider.Register(MemoryDomains);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private void InitMemoryDomains()
pokeByte, 4
));

MemoryDomains = new MemoryDomainList(_memoryDomains);
MemoryDomains = new MemoryDomainList(_memoryDomains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(MemoryDomains);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private void SetupMemoryDomains()

if (!_memoryDomainsSetup)
{
_memoryDomains = new MemoryDomainList(domains);
_memoryDomains = new MemoryDomainList(domains, this);
(ServiceProvider as BasicServiceProvider).Register<IMemoryDomains>(_memoryDomains);
_memoryDomainsSetup = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void InitMemoryDomains()
QN.qn_poke_prgbus(Context, (int)addr, val);
}, 1));

_memoryDomains = new MemoryDomainList(mm);
_memoryDomains = new MemoryDomainList(mm, this);
((BasicServiceProvider) ServiceProvider).Register(_memoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void SetupMemoryDomains(byte[] romData, byte[] sgbRomData)

_memoryDomainList.Add(Api.GetPagesDomain());

_memoryDomains = new MemoryDomainList(_memoryDomainList);
_memoryDomains = new MemoryDomainList(_memoryDomainList, this);
((BasicServiceProvider) ServiceProvider).Register(_memoryDomains);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void InitMemoryDomains()
LibSameboy.sameboy_cpuwrite(SameboyState, (ushort)addr, val);
}, 1));

MemoryDomains = new MemoryDomainList(_memoryDomains);
MemoryDomains = new MemoryDomainList(_memoryDomains, this);
((MemoryDomainList)MemoryDomains).MainMemory = MemoryDomains["WRAM"];
_serviceProvider.Register(MemoryDomains);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void SetupMemoryDomains()
domains.AddRange(_byteArrayDomains.Values);
domains.AddRange(_ushortArrayDomains.Values);

_memoryDomains = new MemoryDomainList(domains);
_memoryDomains = new MemoryDomainList(domains, this);
_memoryDomains.SystemBus = cpuBusDomain;
_memoryDomains.MainMemory = _byteArrayDomains["Main Memory"];

Expand Down
Loading
Loading