-
Notifications
You must be signed in to change notification settings - Fork 428
Create a MemoryDomain
subclass which wraps IDebuggable.{GetCpuFlagsAndRegisters,SetCpuRegister}
#4471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YoshiRulz
wants to merge
2
commits into
master
Choose a base branch
from
regs-mem-domain
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create a MemoryDomain
subclass which wraps IDebuggable.{GetCpuFlagsAndRegisters,SetCpuRegister}
#4471
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/BizHawk.Emulation.Common/Base Implementations/RegistersMemoryDomain.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.