Skip to content

Watches with pointers #4279

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
66 changes: 29 additions & 37 deletions src/BizHawk.Client.Common/tools/Cheat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,20 @@ public MemoryDomain Domain

public string AddressStr => _watch.AddressString;

public string ValueStr =>
_watch.Size switch
{
WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_val),
WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_val),
WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_val),
WatchSize.Separator => "",
_ => string.Empty,
};
public string ValueStr
=> _watch.Size is WatchSize.Byte or WatchSize.Word or WatchSize.DWord
? _watch.IsValid
? Watch.FormatValue(unchecked((uint) _val), _watch.Size, _watch.Type)
: "-"
: string.Empty;

public string CompareStr
{
get
{
if (_compare.HasValue)
{
return _watch.Size switch
{
WatchSize.Byte => ((ByteWatch) _watch).FormatValue((byte)_compare.Value),
WatchSize.Word => ((WordWatch) _watch).FormatValue((ushort)_compare.Value),
WatchSize.DWord => ((DWordWatch) _watch).FormatValue((uint)_compare.Value),
WatchSize.Separator => "",
_ => string.Empty,
};
}

return "";
}
}
=> _compare.Value is int compareValue
&& _watch.Size is WatchSize.Byte or WatchSize.Word or WatchSize.DWord
? _watch.IsValid
? Watch.FormatValue(unchecked((uint) compareValue), _watch.Size, _watch.Type)
: "-"
: string.Empty;

public CompareType ComparisonType { get; }

Expand Down Expand Up @@ -167,17 +152,24 @@ public void Pulse()
{
if (ShouldPoke())
{
switch (_watch.Size)
try
{
switch (_watch.Size)
{
case WatchSize.Byte:
_watch.PokeByte(unchecked((byte) _val));
break;
case WatchSize.Word:
_watch.PokeWord(unchecked((ushort) _val));
break;
case WatchSize.DWord:
_watch.PokeDWord(unchecked((uint) _val));
break;
}
}
catch
{
case WatchSize.Byte:
_watch.Poke(((ByteWatch)_watch).FormatValue((byte)_val));
break;
case WatchSize.Word:
_watch.Poke(((WordWatch)_watch).FormatValue((ushort)_val));
break;
case WatchSize.DWord:
_watch.Poke(((DWordWatch)_watch).FormatValue((uint)_val));
break;
// ignore (matches `*Watch.Poke` implementations)
}
}

Expand Down
67 changes: 3 additions & 64 deletions src/BizHawk.Client.Common/tools/Watch/ByteWatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Globalization;

using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand Down Expand Up @@ -42,43 +42,21 @@ internal ByteWatch(MemoryDomain domain, long address, WatchDisplayType type, boo
WatchDisplayType.Binary,
];

/// <summary>
/// Get a list a <see cref="WatchDisplayType"/> that can be used for this <see cref="ByteWatch"/>
/// </summary>
/// <returns>An enumeration that contains all valid <see cref="WatchDisplayType"/></returns>
public override IEnumerable<WatchDisplayType> AvailableTypes()
{
return ValidTypes;
}

/// <summary>
/// Reset the previous value; set it to the current one
/// </summary>
public override void ResetPrevious()
{
_previous = GetByte();
}

/// <summary>
/// Try to sets the value into the <see cref="MemoryDomain"/>
/// at the current <see cref="Watch"/> address
/// </summary>
/// <param name="value">Value to set</param>
/// <returns>True if value successfully sets; otherwise, false</returns>
public override bool Poke(string value)
{
try
{
byte val = Type switch
{
WatchDisplayType.Unsigned => byte.Parse(value),
WatchDisplayType.Signed => (byte)sbyte.Parse(value),
WatchDisplayType.Hex => byte.Parse(value, NumberStyles.HexNumber),
WatchDisplayType.Binary => Convert.ToByte(value, 2),
_ => 0,
};

PokeByte(val);
PokeByte(unchecked((byte) Watch.ParseValue(value, Size, Type)));
return true;
}
catch
Expand All @@ -87,9 +65,6 @@ public override bool Poke(string value)
}
}

/// <summary>
/// Update the Watch (read it from <see cref="MemoryDomain"/>
/// </summary>
public override void Update(PreviousType previousType)
{
switch (previousType)
Expand Down Expand Up @@ -120,52 +95,16 @@ public override void Update(PreviousType previousType)

// TODO: Implements IFormattable
public string FormatValue(byte val)
{
return Type switch
{
_ when !IsValid => "-",
WatchDisplayType.Unsigned => val.ToString(),
WatchDisplayType.Signed => ((sbyte) val).ToString(),
WatchDisplayType.Hex => $"{val:X2}",
WatchDisplayType.Binary => Convert.ToString(val, 2).PadLeft(8, '0').Insert(4, " "),
_ => val.ToString(),
};
}
=> IsValid ? Watch.FormatValue(val, Size, Type) : "-";

/// <summary>
/// Get a string representation of difference
/// between current value and the previous one
/// </summary>
public override string Diff => $"{_value - _previous:+#;-#;0}";

/// <summary>
/// Returns true if the Watch is valid, false otherwise
/// </summary>
public override bool IsValid => Domain.Size == 0 || Address < Domain.Size;

/// <summary>
/// Get the maximum possible value
/// </summary>
public override uint MaxValue => byte.MaxValue;

/// <summary>
/// Get the current value
/// </summary>
public override int Value => GetByte();

/// <summary>
/// Get a string representation of the current value
/// </summary>
public override string ValueString => FormatValue(GetByte());

/// <summary>
/// Get the previous value
/// </summary>
public override uint Previous => _previous;

/// <summary>
/// Get a string representation of the previous value
/// </summary>
public override string PreviousStr => FormatValue(_previous);
}
}
90 changes: 3 additions & 87 deletions src/BizHawk.Client.Common/tools/Watch/DWordWatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using BizHawk.Common.NumberExtensions;

using BizHawk.Emulation.Common;

namespace BizHawk.Client.Common
Expand Down Expand Up @@ -46,44 +45,19 @@ internal DWordWatch(MemoryDomain domain, long address, WatchDisplayType type, bo
WatchDisplayType.Float,
];

/// <summary>
/// Get a list of <see cref="WatchDisplayType"/> that can be used for a <see cref="DWordWatch"/>
/// </summary>
/// <returns>An enumeration that contains all valid <see cref="WatchDisplayType"/></returns>
public override IEnumerable<WatchDisplayType> AvailableTypes()
{
return ValidTypes;
}

/// <summary>
/// Reset the previous value; set it to the current one
/// </summary>
public override void ResetPrevious()
=> _previous = GetDWord();

/// <summary>
/// Try to sets the value into the <see cref="MemoryDomain"/>
/// at the current <see cref="Watch"/> address
/// </summary>
/// <param name="value">Value to set</param>
/// <returns>True if value successfully sets; otherwise, false</returns>
public override bool Poke(string value)
{
try
{
uint val = Type switch
{
WatchDisplayType.Unsigned => uint.Parse(value),
WatchDisplayType.Signed => (uint)int.Parse(value),
WatchDisplayType.Hex => uint.Parse(value, NumberStyles.HexNumber),
WatchDisplayType.FixedPoint_20_12 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 4096.0),
WatchDisplayType.FixedPoint_16_16 => (uint)(double.Parse(value, NumberFormatInfo.InvariantInfo) * 65536.0),
WatchDisplayType.Float => NumberExtensions.ReinterpretAsUInt32(float.Parse(value, NumberFormatInfo.InvariantInfo)),
WatchDisplayType.Binary => Convert.ToUInt32(value, 2),
_ => 0,
};

PokeDWord(val);
PokeDWord(Watch.ParseValue(value, Size, Type));
return true;
}
catch
Expand All @@ -92,9 +66,6 @@ public override bool Poke(string value)
}
}

/// <summary>
/// Update the Watch (read it from <see cref="MemoryDomain"/>
/// </summary>
public override void Update(PreviousType previousType)
{
switch (previousType)
Expand Down Expand Up @@ -125,71 +96,16 @@ public override void Update(PreviousType previousType)

// TODO: Implements IFormattable
public string FormatValue(uint val)
{
string FormatFloat()
{
var _float = NumberExtensions.ReinterpretAsF32(val);
return _float.ToString(NumberFormatInfo.InvariantInfo);
}

string FormatBinary()
{
var str = Convert.ToString(val, 2).PadLeft(32, '0');
for (var i = 28; i > 0; i -= 4)
{
str = str.Insert(i, " ");
}
return str;
}

return Type switch
{
_ when !IsValid => "-",
WatchDisplayType.Unsigned => val.ToString(),
WatchDisplayType.Signed => ((int)val).ToString(),
WatchDisplayType.Hex => $"{val:X8}",
WatchDisplayType.FixedPoint_20_12 => ((int)val / 4096.0).ToString("0.######", NumberFormatInfo.InvariantInfo),
WatchDisplayType.FixedPoint_16_16 => ((int)val / 65536.0).ToString("0.######", NumberFormatInfo.InvariantInfo),
WatchDisplayType.Float => FormatFloat(),
WatchDisplayType.Binary => FormatBinary(),
_ => val.ToString(),
};
}
=> IsValid ? Watch.FormatValue(val, Size, Type) : "-";

/// <summary>
/// Get a string representation of difference
/// between current value and the previous one
/// </summary>
public override string Diff => $"{_value - (long)_previous:+#;-#;0}";

/// <summary>
/// Returns true if the Watch is valid, false otherwise
/// </summary>
public override bool IsValid => Domain.Size == 0 || Address < (Domain.Size - 3);

/// <summary>
/// Get the maximum possible value
/// </summary>
public override uint MaxValue => uint.MaxValue;

/// <summary>
/// Get the current value
/// </summary>
public override int Value => (int)GetDWord();

/// <summary>
/// Get a string representation of the current value
/// </summary>
public override string ValueString => FormatValue(GetDWord());

/// <summary>
/// Get the previous value
/// </summary>
public override uint Previous => _previous;

/// <summary>
/// Get a string representation of the previous value
/// </summary>
public override string PreviousStr => FormatValue(_previous);
}
}
Loading
Loading