From 291266e4fcadd41fc530c376717801a8d2e31f44 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Thu, 21 May 2026 10:00:57 +0100 Subject: [PATCH] Assert that which needs to be checked, but not thrown --- EightBit/BigEndianProcessor.cs | 19 +++++++++++-------- EightBit/Bus.cs | 4 ++-- EightBit/InputOutput.cs | 10 ++++++---- EightBit/LittleEndianProcessor.cs | 18 ++++++++++-------- EightBit/Processor.cs | 4 +++- EightBit/Register16.cs | 8 +++++--- LR35902/ObjectAttribute.cs | 3 ++- LR35902/SM83.HarteTest/Checker.cs | 8 ++++---- LR35902/SM83.HarteTest/Cycle.cs | 2 +- M6502/M6502.HarteTest/Checker.cs | 8 ++++---- M6502/M6502.HarteTest/Cycle.cs | 6 ++++-- Z80/Z80.HarteTest/Checker.cs | 6 +++--- Z80/Z80.HarteTest/Cycle.cs | 2 +- Z80/Z80.HarteTest/Port.cs | 6 ++++-- 14 files changed, 60 insertions(+), 44 deletions(-) diff --git a/EightBit/BigEndianProcessor.cs b/EightBit/BigEndianProcessor.cs index 2a00b7d..be5f638 100644 --- a/EightBit/BigEndianProcessor.cs +++ b/EightBit/BigEndianProcessor.cs @@ -1,6 +1,9 @@ // // Copyright (c) Adrian Conlon. All rights reserved. // + +using System.Diagnostics; + namespace EightBit { public abstract class BigEndianProcessor(Bus memory) : Processor(memory) @@ -14,14 +17,14 @@ namespace EightBit public override void PokeShort(ushort address, Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.Bus.Poke(address, value.High); this.Bus.Poke(++address, value.Low); } protected override void FetchInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.FetchByte(); into.High = this.Bus.Data; this.FetchByte(); @@ -30,7 +33,7 @@ namespace EightBit protected override void GetInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.MemoryRead(); into.High = this.Bus.Data; this.Bus.Address.Increment(); @@ -40,7 +43,7 @@ namespace EightBit protected override void GetPagedInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.MemoryRead(); into.High = this.Bus.Data; ++this.Bus.Address.Low; @@ -50,7 +53,7 @@ namespace EightBit protected override void PopInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.Pop(); into.High = this.Bus.Data; this.Pop(); @@ -59,14 +62,14 @@ namespace EightBit protected override void PushShort(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.Push(value.Low); this.Push(value.High); } protected override void SetShort(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.MemoryWrite(value.High); this.Bus.Address.Increment(); this.MemoryWrite(value.Low); @@ -74,7 +77,7 @@ namespace EightBit protected override void SetPaged(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.MemoryWrite(value.High); ++this.Bus.Address.Low; this.MemoryWrite(value.Low); diff --git a/EightBit/Bus.cs b/EightBit/Bus.cs index a6d4677..5832b44 100644 --- a/EightBit/Bus.cs +++ b/EightBit/Bus.cs @@ -31,7 +31,7 @@ namespace EightBit public byte Peek(Register16 absolute) { - ArgumentNullException.ThrowIfNull(absolute); + Debug.Assert(absolute is not null, "absolute cannot be null"); return this.Peek(absolute.Joined); } @@ -41,7 +41,7 @@ namespace EightBit public void Poke(Register16 absolute, byte value) { - ArgumentNullException.ThrowIfNull(absolute); + Debug.Assert(absolute is not null, "absolute cannot be null"); this.Poke(absolute.Joined, value); } diff --git a/EightBit/InputOutput.cs b/EightBit/InputOutput.cs index 8b73279..e8e92bc 100644 --- a/EightBit/InputOutput.cs +++ b/EightBit/InputOutput.cs @@ -2,6 +2,8 @@ // Copyright (c) Adrian Conlon. All rights reserved. // +using System.Diagnostics; + namespace EightBit { public sealed class InputOutput @@ -22,7 +24,7 @@ namespace EightBit public byte ReadInputPort(Register16 port) { - ArgumentNullException.ThrowIfNull(port); + Debug.Assert(port is not null, "port cannot be null"); ReadingPort?.Invoke(this, new PortEventArgs(port)); var value = this._input[port.Joined]; ReadPort?.Invoke(this, new PortEventArgs(port)); @@ -33,7 +35,7 @@ namespace EightBit public void WriteInputPort(Register16 port, byte value) { - ArgumentNullException.ThrowIfNull(port); + Debug.Assert(port is not null, "port cannot be null"); this.WriteInputPort(port.Joined, value); } @@ -41,7 +43,7 @@ namespace EightBit public void WriteOutputPort(Register16 port, byte value) { - ArgumentNullException.ThrowIfNull(port); + Debug.Assert(port is not null, "port cannot be null"); WritingPort?.Invoke(this, new PortEventArgs(port)); this._output[port.Joined] = value; WrittenPort?.Invoke(this, new PortEventArgs(port)); @@ -51,7 +53,7 @@ namespace EightBit public byte ReadOutputPort(Register16 port) { - ArgumentNullException.ThrowIfNull(port); + Debug.Assert(port is not null, "port cannot be null"); return this.ReadOutputPort(port.Joined); } } diff --git a/EightBit/LittleEndianProcessor.cs b/EightBit/LittleEndianProcessor.cs index 4ad6497..a49e7b3 100644 --- a/EightBit/LittleEndianProcessor.cs +++ b/EightBit/LittleEndianProcessor.cs @@ -2,6 +2,8 @@ // Copyright (c) Adrian Conlon. All rights reserved. // +using System.Diagnostics; + namespace EightBit { public abstract class LittleEndianProcessor(Bus memory) : Processor(memory) @@ -15,14 +17,14 @@ namespace EightBit public override void PokeShort(ushort address, Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.Bus.Poke(address, value.Low); this.Bus.Poke(++address, value.High); } protected override void FetchInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.FetchByte(); into.Low = this.Bus.Data; this.FetchByte(); @@ -31,7 +33,7 @@ namespace EightBit protected override void GetInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.MemoryRead(); into.Low = this.Bus.Data; this.Bus.Address.Increment(); @@ -41,7 +43,7 @@ namespace EightBit protected override void GetPagedInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.MemoryRead(); into.Low = this.Bus.Data; ++this.Bus.Address.Low; @@ -51,7 +53,7 @@ namespace EightBit protected override void PopInto(Register16 into) { - //ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.Pop(); into.Low = this.Bus.Data; this.Pop(); @@ -60,14 +62,14 @@ namespace EightBit protected override void PushShort(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.Push(value.High); this.Push(value.Low); } protected override void SetShort(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.MemoryWrite(value.Low); this.Bus.Address.Increment(); this.MemoryWrite(value.High); @@ -75,7 +77,7 @@ namespace EightBit protected override void SetPaged(Register16 value) { - //ArgumentNullException.ThrowIfNull(value); + Debug.Assert(value is not null, "value cannot be null"); this.MemoryWrite(value.Low); ++this.Bus.Address.Low; this.MemoryWrite(value.High); diff --git a/EightBit/Processor.cs b/EightBit/Processor.cs index e6795d1..2f725bc 100644 --- a/EightBit/Processor.cs +++ b/EightBit/Processor.cs @@ -2,6 +2,8 @@ // Copyright (c) Adrian Conlon. All rights reserved. // +using System.Diagnostics; + namespace EightBit { public abstract class Processor(Bus memory) : ClockedChip @@ -236,7 +238,7 @@ namespace EightBit protected void GetPagedInto(byte page, byte offset, Register16 into) { - ArgumentNullException.ThrowIfNull(into); + Debug.Assert(into is not null, "into cannot be null"); this.Bus.Address.Assign(offset, page); this.GetPagedInto(into); } diff --git a/EightBit/Register16.cs b/EightBit/Register16.cs index 2eafc81..d4d2763 100644 --- a/EightBit/Register16.cs +++ b/EightBit/Register16.cs @@ -2,6 +2,8 @@ // Copyright (c) Adrian Conlon. All rights reserved. // +using System.Diagnostics; + namespace EightBit { public sealed class Register16 @@ -38,7 +40,7 @@ namespace EightBit public Register16(Register16 rhs) { - //ArgumentNullException.ThrowIfNull(rhs); + Debug.Assert(rhs is not null, "rhs cannot be null"); this.Low = rhs.Low; this.High = rhs.High; } @@ -59,7 +61,7 @@ namespace EightBit public static bool operator ==(Register16 left, Register16 right) { - //ArgumentNullException.ThrowIfNull(left); + Debug.Assert(left is not null, "left cannot be null"); return left.Equals(right); } @@ -83,7 +85,7 @@ namespace EightBit public void Assign(Register16 from) { - //ArgumentNullException.ThrowIfNull(from); + Debug.Assert(from is not null, "from cannot be null"); this.Low = from.Low; this.High = from.High; } diff --git a/LR35902/ObjectAttribute.cs b/LR35902/ObjectAttribute.cs index 5dd9468..a901570 100644 --- a/LR35902/ObjectAttribute.cs +++ b/LR35902/ObjectAttribute.cs @@ -5,6 +5,7 @@ namespace LR35902 { using EightBit; + using System.Diagnostics; public class ObjectAttribute { @@ -14,7 +15,7 @@ namespace LR35902 public ObjectAttribute(Ram ram, ushort address) { - ArgumentNullException.ThrowIfNull(ram); + Debug.Assert(ram is not null, "ram cannot be null"); this.PositionY = ram.Peek(address); this.PositionX = ram.Peek(++address); this.Pattern = ram.Peek(++address); diff --git a/LR35902/SM83.HarteTest/Checker.cs b/LR35902/SM83.HarteTest/Checker.cs index 67b18db..cb95f75 100644 --- a/LR35902/SM83.HarteTest/Checker.cs +++ b/LR35902/SM83.HarteTest/Checker.cs @@ -119,8 +119,8 @@ private bool Check(string what, string? expected, string? actual) { - ArgumentNullException.ThrowIfNull(expected); - ArgumentNullException.ThrowIfNull(actual); + Debug.Assert(expected is not null, "expected cannot be null"); + Debug.Assert(actual is not null, "actual cannot be null"); var success = actual == expected; if (!success) { @@ -341,7 +341,7 @@ private void DumpCycle(ushort address, byte? value, string? action) { - ArgumentNullException.ThrowIfNull(action); + Debug.Assert(action is not null, "action cannot be null"); this.Messages.Add($"Address: {address:X4}, value: {value:X2}, action: {action}"); } @@ -349,7 +349,7 @@ private void DumpCycles(IEnumerable? cycles) { - ArgumentNullException.ThrowIfNull(cycles); + Debug.Assert(cycles is not null, "cycles cannot be null"); foreach (var cycle in cycles) { this.DumpCycle(cycle); diff --git a/LR35902/SM83.HarteTest/Cycle.cs b/LR35902/SM83.HarteTest/Cycle.cs index c1cbb4e..4fc56be 100644 --- a/LR35902/SM83.HarteTest/Cycle.cs +++ b/LR35902/SM83.HarteTest/Cycle.cs @@ -19,7 +19,7 @@ namespace SM83.HarteTest public Cycle(List input) { - ArgumentNullException.ThrowIfNull(input); + Debug.Assert(input is not null, "input cannot be null"); if (input.Count != 3) { diff --git a/M6502/M6502.HarteTest/Checker.cs b/M6502/M6502.HarteTest/Checker.cs index e1db043..53696b6 100644 --- a/M6502/M6502.HarteTest/Checker.cs +++ b/M6502/M6502.HarteTest/Checker.cs @@ -113,8 +113,8 @@ private bool Check(string what, string? expected, string? actual) { - ArgumentNullException.ThrowIfNull(expected); - ArgumentNullException.ThrowIfNull(actual); + Debug.Assert(expected is not null, "expected cannot be null"); + Debug.Assert(actual is not null, "actual cannot be null"); var success = actual == expected; if (!success) { @@ -292,7 +292,7 @@ private void DumpCycle(ushort address, byte value, string? action) { - ArgumentNullException.ThrowIfNull(action); + Debug.Assert(action is not null, "action cannot be null"); this.Messages.Add($"Address: {address:X4}, value: {value:X2}, action: {action}"); } @@ -300,7 +300,7 @@ private void DumpCycles(IEnumerable? cycles) { - ArgumentNullException.ThrowIfNull(cycles); + Debug.Assert(cycles is not null, "cycles cannot be null"); foreach (var cycle in cycles) { this.DumpCycle(cycle); diff --git a/M6502/M6502.HarteTest/Cycle.cs b/M6502/M6502.HarteTest/Cycle.cs index 8b1cd4b..8a8ed9f 100644 --- a/M6502/M6502.HarteTest/Cycle.cs +++ b/M6502/M6502.HarteTest/Cycle.cs @@ -1,4 +1,6 @@ -namespace M6502.HarteTest +using System.Diagnostics; + +namespace M6502.HarteTest { internal sealed class Cycle { @@ -17,7 +19,7 @@ public Cycle(List input) { - ArgumentNullException.ThrowIfNull(input); + Debug.Assert(input is not null, "input cannot be null"); if (input.Count != 3) { diff --git a/Z80/Z80.HarteTest/Checker.cs b/Z80/Z80.HarteTest/Checker.cs index 9ad2a5a..3541bdd 100644 --- a/Z80/Z80.HarteTest/Checker.cs +++ b/Z80/Z80.HarteTest/Checker.cs @@ -142,8 +142,8 @@ private bool Check(string what, string? expected, string? actual) { - ArgumentNullException.ThrowIfNull(expected); - ArgumentNullException.ThrowIfNull(actual); + Debug.Assert(expected is not null, "expected cannot be null"); + Debug.Assert(actual is not null, "actual cannot be null"); var success = actual == expected; if (!success) { @@ -414,7 +414,7 @@ private static string ExpandCycle(string prefix, ushort address, byte? value, string? action) { - ArgumentNullException.ThrowIfNull(action); + Debug.Assert(action is not null, "action cannot be null"); return value is null ? $"{prefix}: Address: {address:X4}, action: {action}" : $"{prefix}: Address: {address:X4}, value: {value:X2}, action: {action}"; diff --git a/Z80/Z80.HarteTest/Cycle.cs b/Z80/Z80.HarteTest/Cycle.cs index 13722d0..9b11c92 100644 --- a/Z80/Z80.HarteTest/Cycle.cs +++ b/Z80/Z80.HarteTest/Cycle.cs @@ -22,7 +22,7 @@ public Cycle(List input) { - ArgumentNullException.ThrowIfNull(input); + Debug.Assert(input is not null, "input cannot be null"); if (input.Count != 3) { diff --git a/Z80/Z80.HarteTest/Port.cs b/Z80/Z80.HarteTest/Port.cs index 2561862..1e89b87 100644 --- a/Z80/Z80.HarteTest/Port.cs +++ b/Z80/Z80.HarteTest/Port.cs @@ -1,4 +1,6 @@ -namespace Z80.HarteTest +using System.Diagnostics; + +namespace Z80.HarteTest { // Cycle-by-cycle breakdown of bus activity internal sealed class Port @@ -19,7 +21,7 @@ public Port(List input) { - ArgumentNullException.ThrowIfNull(input); + Debug.Assert(input is not null, "input cannot be null"); if (input.Count != 3) {