Skip to content
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

Simplify Range<T> usage and implementation, switching to half-open (end is exclusive) #3965

Draft
wants to merge 4 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
3 changes: 1 addition & 2 deletions src/BizHawk.Client.EmuHawk/Throttle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ private void AutoFrameSkip_NextFrame()
// reset way-out-of-range values
if (diff > 1.0f)
diff = 1.0f;
if (!(-1.0f).RangeTo(1.0f).Contains(error))
error = 0.0f;
if (Math.Abs(error) > 1.0f) error = 0.0f;
if (diffUnthrottled > 1.0f)
diffUnthrottled = desiredspf;

Expand Down
7 changes: 1 addition & 6 deletions src/BizHawk.Client.EmuHawk/tools/NES/NESMusicRipper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Xml.Linq;

using BizHawk.Client.Common;
using BizHawk.Common;
using BizHawk.Emulation.Cores.Nintendo.NES;
using BizHawk.Emulation.Common;

Expand Down Expand Up @@ -63,11 +62,7 @@ private int FindNearestNote(float freq)
float a = FreqTable[i - 1];
float b = FreqTable[i];
float c = FreqTable[i + 1];
var range = ((a + b) / 2).RangeTo((b + c) / 2);
if (range.Contains(freq))
{
return i - 1;
}
if ((a + b) / 2 <= freq && freq <= (b + c) / 2) return i - 1;
}

return 95; // I guess?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ public void Init(StickyXorAdapter stickyXorAdapter, string nameX, AxisSpec range
Rerange();
}

private Range<int> _rangeX = 0.RangeTo(0);
private Range<int> _rangeY = 0.RangeTo(0);
private Int32Interval _rangeX = 0.RangeTo(0);

private Int32Interval _rangeY = 0.RangeTo(0);

private AxisSpec _fullRangeX;
private AxisSpec _fullRangeY;

Expand Down Expand Up @@ -286,6 +288,6 @@ private void SetPosition(int xval, int yval)
Refresh();
}

private static readonly Range<int> PercentRange = 0.RangeTo(100);
private static readonly Int32Interval PercentRange = 0.RangeTo(100);
}
}
295 changes: 79 additions & 216 deletions src/BizHawk.Common/Ranges.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public readonly struct AxisSpec

public string? PairedAxis => Constraint?.PairedAxis;

public readonly Range<int> Range;
public readonly Int32Interval Range;

public AxisSpec(Range<int> range, int neutral, bool isReversed = false, AxisConstraint? constraint = null)
public AxisSpec(Int32Interval range, int neutral, bool isReversed = false, AxisConstraint? constraint = null)
{
Constraint = constraint;
IsReversed = isReversed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public virtual void PokeUint(long addr, uint val, bool bigEndian)
}
}

public virtual void BulkPeekByte(Range<long> addresses, byte[] values)
public virtual void BulkPeekByte(Int64Interval addresses, byte[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));
Expand All @@ -114,20 +114,20 @@ public virtual void BulkPeekByte(Range<long> addresses, byte[] values)

using (this.EnterExit())
{
for (var i = addresses.Start; i <= addresses.EndInclusive; i++)
for (var i = addresses.Start; i < addresses.EndExclusive; i++)
{
values[i - addresses.Start] = PeekByte(i);
}
}
}

public virtual void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushort[] values)
public virtual void BulkPeekUshort(Int64Interval addresses, bool bigEndian, ushort[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));

var start = addresses.Start;
var end = addresses.EndInclusive + 1;
var end = addresses.EndExclusive;

if ((start & 1) != 0 || (end & 1) != 0)
throw new InvalidOperationException("The API contract doesn't define what to do for unaligned reads and writes!");
Expand All @@ -145,13 +145,13 @@ public virtual void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushor
}
}

public virtual void BulkPeekUint(Range<long> addresses, bool bigEndian, uint[] values)
public virtual void BulkPeekUint(Int64Interval addresses, bool bigEndian, uint[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));

var start = addresses.Start;
var end = addresses.EndInclusive + 1;
var end = addresses.EndExclusive;

if ((start & 3) != 0 || (end & 3) != 0)
throw new InvalidOperationException("The API contract doesn't define what to do for unaligned reads and writes!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ public class MemoryDomainDelegate : MemoryDomain
private Action<long, byte> _poke;

// TODO: use an array of Ranges
private Action<Range<long>, byte[]> _bulkPeekByte { get; set; }
private Action<Range<long>, bool, ushort[]> _bulkPeekUshort { get; set; }
private Action<Range<long>, bool, uint[]> _bulkPeekUint { get; set; }
private Action<Int64Interval, byte[]> _bulkPeekByte { get; set; }

private Action<Int64Interval, bool, ushort[]> _bulkPeekUshort { get; set; }

private Action<Int64Interval, bool, uint[]> _bulkPeekUint { get; set; }

public Func<long, byte> Peek { get; set; }

Expand All @@ -37,7 +39,7 @@ public override void PokeByte(long addr, byte val)
_poke?.Invoke(addr, val);
}

public override void BulkPeekByte(Range<long> addresses, byte[] values)
public override void BulkPeekByte(Int64Interval addresses, byte[] values)
{
if (_bulkPeekByte != null)
{
Expand All @@ -49,7 +51,7 @@ public override void BulkPeekByte(Range<long> addresses, byte[] values)
}
}

public override void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushort[] values)
public override void BulkPeekUshort(Int64Interval addresses, bool bigEndian, ushort[] values)
{
if (_bulkPeekUshort != null)
{
Expand All @@ -61,7 +63,7 @@ public override void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushor
}
}

public override void BulkPeekUint(Range<long> addresses, bool bigEndian, uint[] values)
public override void BulkPeekUint(Int64Interval addresses, bool bigEndian, uint[] values)
{
if (_bulkPeekUint != null)
{
Expand All @@ -80,9 +82,9 @@ public MemoryDomainDelegate(
Func<long, byte> peek,
Action<long, byte> poke,
int wordSize,
Action<Range<long>, byte[]> bulkPeekByte = null,
Action<Range<long>, bool, ushort[]> bulkPeekUshort = null,
Action<Range<long>, bool, uint[]> bulkPeekUint = null)
Action<Int64Interval, byte[]> bulkPeekByte = null,
Action<Int64Interval, bool, ushort[]> bulkPeekUshort = null,
Action<Int64Interval, bool, uint[]> bulkPeekUint = null)
{
Name = name;
EndianType = endian;
Expand Down Expand Up @@ -210,7 +212,7 @@ public override void PokeByte(long addr, byte val)
}
}

public override void BulkPeekByte(Range<long> addresses, byte[] values)
public override void BulkPeekByte(Int64Interval addresses, byte[] values)
{
var start = (ulong)addresses.Start;
var count = addresses.Count();
Expand Down
33 changes: 27 additions & 6 deletions src/BizHawk.Emulation.Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,13 @@ public static string FilesystemSafeName(this IGameInfo game)
/// </summary>
/// <param name="constraint">pass only for one axis in a pair, by convention the X axis</param>
/// <returns>identical reference to <paramref name="def"/>; the object is mutated</returns>
public static ControllerDefinition AddAxis(this ControllerDefinition def, string name, Range<int> range, int neutral, bool isReversed = false, AxisConstraint constraint = null)
public static ControllerDefinition AddAxis(
this ControllerDefinition def,
string name,
Int32Interval range,
int neutral,
bool isReversed = false,
AxisConstraint constraint = null)
{
def.Axes.Add(name, new AxisSpec(range, neutral, isReversed, constraint));
return def;
Expand All @@ -456,7 +462,15 @@ public static ControllerDefinition AddAxis(this ControllerDefinition def, string
/// </summary>
/// <param name="nameFormat">format string e.g. <c>"P1 Left {0}"</c> (will be used to interpolate <c>"X"</c> and <c>"Y"</c>)</param>
/// <returns>identical reference to <paramref name="def"/>; the object is mutated</returns>
public static ControllerDefinition AddXYPair(this ControllerDefinition def, string nameFormat, AxisPairOrientation pDir, Range<int> rangeX, int neutralX, Range<int> rangeY, int neutralY, AxisConstraint constraint = null)
public static ControllerDefinition AddXYPair(
this ControllerDefinition def,
string nameFormat,
AxisPairOrientation pDir,
Int32Interval rangeX,
int neutralX,
Int32Interval rangeY,
int neutralY,
AxisConstraint constraint = null)
{
var yAxisName = string.Format(nameFormat, "Y");
var finalConstraint = constraint ?? new NoOpAxisConstraint(yAxisName);
Expand All @@ -470,21 +484,28 @@ public static ControllerDefinition AddXYPair(this ControllerDefinition def, stri
/// </summary>
/// <param name="nameFormat">format string e.g. <c>"P1 Left {0}"</c> (will be used to interpolate <c>"X"</c> and <c>"Y"</c>)</param>
/// <returns>identical reference to <paramref name="def"/>; the object is mutated</returns>
public static ControllerDefinition AddXYPair(this ControllerDefinition def, string nameFormat, AxisPairOrientation pDir, Range<int> rangeBoth, int neutralBoth, AxisConstraint constraint = null)
=> def.AddXYPair(nameFormat, pDir, rangeBoth, neutralBoth, rangeBoth, neutralBoth, constraint);
public static ControllerDefinition AddXYPair(
this ControllerDefinition def,
string nameFormat,
AxisPairOrientation pDir,
Int32Interval rangeBoth,
int neutralBoth,
AxisConstraint constraint = null)
=> def.AddXYPair(nameFormat, pDir, rangeBoth, neutralBoth, rangeBoth, neutralBoth, constraint);

/// <summary>
/// Adds an X/Y/Z triple of axes to the receiver <see cref="ControllerDefinition"/>, and returns it.
/// The new axes will appear after any that were previously defined.
/// </summary>
/// <param name="nameFormat">format string e.g. <c>"P1 Tilt {0}"</c> (will be used to interpolate <c>"X"</c>, <c>"Y"</c>, and <c>"Z"</c>)</param>
/// <returns>identical reference to <paramref name="def"/>; the object is mutated</returns>
public static ControllerDefinition AddXYZTriple(this ControllerDefinition def, string nameFormat, Range<int> rangeAll, int neutralAll)
public static ControllerDefinition AddXYZTriple(this ControllerDefinition def, string nameFormat, Int32Interval rangeAll, int neutralAll)
=> def.AddAxis(string.Format(nameFormat, "X"), rangeAll, neutralAll)
.AddAxis(string.Format(nameFormat, "Y"), rangeAll, neutralAll)
.AddAxis(string.Format(nameFormat, "Z"), rangeAll, neutralAll);

public static AxisSpec With(this in AxisSpec spec, Range<int> range, int neutral) => new AxisSpec(range, neutral, spec.IsReversed, spec.Constraint);
public static AxisSpec With(this in AxisSpec spec, Int32Interval range, int neutral)
=> new(range, neutral, spec.IsReversed, spec.Constraint);

public static string SystemIDToDisplayName(string sysID)
=> SystemIDDisplayNames.TryGetValue(sysID, out var dispName) ? dispName : string.Empty;
Expand Down
4 changes: 1 addition & 3 deletions src/BizHawk.Emulation.Cores/Consoles/PC Engine/VPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,13 @@ private void RenderSpritesScanline(VDC vdc, byte lowPriority, byte highPriority,
// clear inter-sprite priority buffer
Array.Clear(InterSpritePriorityBuffer, 0, FrameWidth);

var testRange = 0.MutableRangeTo(vdc.ActiveLine + 1);
for (int i = 0; i < 64; i++)
{
int y = (vdc.SpriteAttributeTable[(i * 4) + 0] & 1023) - 64;
int x = (vdc.SpriteAttributeTable[(i * 4) + 1] & 1023) - 32;
ushort flags = vdc.SpriteAttributeTable[(i * 4) + 3];
byte height = heightTable[(flags >> 12) & 3];
testRange.Start = vdc.ActiveLine - height;
if (!y.StrictlyBoundedBy(testRange)) continue;
if (y + height <= vdc.ActiveLine || vdc.ActiveLine < y) continue;

int patternNo = (((vdc.SpriteAttributeTable[(i * 4) + 2]) >> 1) & 0x1FF);
int paletteBase = 256 + ((flags & 15) * 16);
Expand Down
4 changes: 2 additions & 2 deletions src/BizHawk.Emulation.Cores/Waterbox/WaterboxMemoryDomain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override void PokeByte(long addr, byte val)
}
}

public override void BulkPeekByte(Range<long> addresses, byte[] values)
public override void BulkPeekByte(Int64Interval addresses, byte[] values)
{
if (_addressMangler != 0)
{
Expand Down Expand Up @@ -189,7 +189,7 @@ public override void PokeByte(long addr, byte val)
}
}

public override void BulkPeekByte(Range<long> addresses, byte[] values)
public override void BulkPeekByte(Int64Interval addresses, byte[] values)
{
if (_addressMangler != 0)
{
Expand Down