From 1b1b92ac2cb921d5fe195fc0950c5680f28d19b4 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Sat, 29 Mar 2025 13:18:54 +0000 Subject: [PATCH] More event handling simplification --- EightBit/InputOutput.cs | 16 +++-------- EightBit/IntelProcessor.cs | 2 -- Intel8080/Intel8080.cs | 22 +++++++++++++-- M6502/Core.cs | 56 +++++++++++--------------------------- M6502/WDC65C02.cs | 5 ++-- Z80/Z80.cs | 12 ++++++++ 6 files changed, 54 insertions(+), 59 deletions(-) diff --git a/EightBit/InputOutput.cs b/EightBit/InputOutput.cs index fe7533c..a8913f4 100644 --- a/EightBit/InputOutput.cs +++ b/EightBit/InputOutput.cs @@ -23,9 +23,9 @@ namespace EightBit public byte ReadInputPort(byte port) { - this.OnReadingPort(port); + ReadingPort?.Invoke(this, new PortEventArgs(port)); var value = this._input[port]; - this.OnReadPort(port); + ReadPort?.Invoke(this, new PortEventArgs(port)); return value; } @@ -35,17 +35,9 @@ namespace EightBit public void WriteOutputPort(byte port, byte value) { - this.OnWritingPort(port); + WritingPort?.Invoke(this, new PortEventArgs(port)); this._output[port] = value; - this.OnWrittenPort(port); + WrittenPort?.Invoke(this, new PortEventArgs(port)); } - - private void OnReadingPort(byte port) => ReadingPort?.Invoke(this, new PortEventArgs(port)); - - private void OnReadPort(byte port) => ReadPort?.Invoke(this, new PortEventArgs(port)); - - private void OnWritingPort(byte port) => WritingPort?.Invoke(this, new PortEventArgs(port)); - - private void OnWrittenPort(byte port) => WrittenPort?.Invoke(this, new PortEventArgs(port)); } } diff --git a/EightBit/IntelProcessor.cs b/EightBit/IntelProcessor.cs index 2bb64ba..cd1ad5d 100644 --- a/EightBit/IntelProcessor.cs +++ b/EightBit/IntelProcessor.cs @@ -70,7 +70,6 @@ namespace EightBit { RaisingHALT?.Invoke(this, EventArgs.Empty); this.HALT.Raise(); - ++this.PC.Word; // Release the PC from HALT instruction RaisedHALT?.Invoke(this, EventArgs.Empty); } } @@ -81,7 +80,6 @@ namespace EightBit { LoweringHALT?.Invoke(this, EventArgs.Empty); this.HALT.Lower(); - --this.PC.Word; // Keep the PC on the HALT instruction (i.e. executing NOP) LoweredHALT?.Invoke(this, EventArgs.Empty); } } diff --git a/Intel8080/Intel8080.cs b/Intel8080/Intel8080.cs index 246843c..70ca111 100644 --- a/Intel8080/Intel8080.cs +++ b/Intel8080/Intel8080.cs @@ -7,11 +7,19 @@ namespace Intel8080 { using EightBit; - public class Intel8080(Bus bus, InputOutput ports) : IntelProcessor(bus) + public class Intel8080 : IntelProcessor { + public Intel8080(Bus bus, InputOutput ports) + : base(bus) + { + this.ports = ports; + this.LoweredHALT += this.Intel8080_LoweredHALT; + this.RaisedHALT += this.Intel8080_RaisedHALT; + } + private readonly Register16 af = new(); - private readonly InputOutput ports = ports; + private readonly InputOutput ports; private bool interruptEnable; @@ -83,6 +91,16 @@ namespace Intel8080 } } + private void Intel8080_RaisedHALT(object? sender, EventArgs e) + { + ++this.PC.Word; // Release the PC from HALT instruction + } + + private void Intel8080_LoweredHALT(object? sender, EventArgs e) + { + --this.PC.Word; // Keep the PC on the HALT instruction (i.e. executing NOP) + } + private static byte SetBit(byte f, StatusBits flag) => SetBit(f, (byte)flag); private static byte SetBit(byte f, StatusBits flag, int condition) => SetBit(f, (byte)flag, condition); diff --git a/M6502/Core.cs b/M6502/Core.cs index 296ebf3..5c96cf3 100644 --- a/M6502/Core.cs +++ b/M6502/Core.cs @@ -26,20 +26,15 @@ namespace M6502 public event EventHandler? RaisedNMI; public event EventHandler? LoweringNMI; public event EventHandler? LoweredNMI; - protected virtual void OnRaisingNMI() => RaisingNMI?.Invoke(this, EventArgs.Empty); - protected virtual void OnRaisedNMI() => RaisedNMI?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweringNMI() => LoweringNMI?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweredNMI() => LoweredNMI?.Invoke(this, EventArgs.Empty); - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public virtual void RaiseNMI() { if (this.NMI.Lowered()) { - this.OnRaisingNMI(); + RaisingNMI?.Invoke(this, EventArgs.Empty); this.NMI.Raise(); - this.OnRaisedNMI(); + RaisedNMI?.Invoke(this, EventArgs.Empty); } } @@ -47,9 +42,9 @@ namespace M6502 { if (this.NMI.Raised()) { - this.OnLoweringNMI(); + LoweringNMI?.Invoke(this, EventArgs.Empty); this.NMI.Lower(); - this.OnLoweredNMI(); + LoweredNMI?.Invoke(this, EventArgs.Empty); } } @@ -64,20 +59,14 @@ namespace M6502 public event EventHandler? LoweringSO; public event EventHandler? LoweredSO; - protected virtual void OnRaisingSO() => RaisingSO?.Invoke(this, EventArgs.Empty); - protected virtual void OnRaisedSO() => RaisedSO?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweringSO() => LoweringSO?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweredSO() => LoweredSO?.Invoke(this, EventArgs.Empty); - - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public virtual void RaiseSO() { if (this.SO.Lowered()) { - this.OnRaisingSO(); + RaisingSO?.Invoke(this, EventArgs.Empty); this.SO.Raise(); - this.OnRaisedSO(); + RaisedSO?.Invoke(this, EventArgs.Empty); } } @@ -85,9 +74,9 @@ namespace M6502 { if (this.SO.Raised()) { - this.OnLoweringSO(); + LoweringSO?.Invoke(this, EventArgs.Empty); this.SO.Lower(); - this.OnLoweredSO(); + LoweredSO?.Invoke(this, EventArgs.Empty); } } @@ -102,11 +91,6 @@ namespace M6502 public event EventHandler? LoweringSYNC; public event EventHandler? LoweredSYNC; - protected virtual void OnRaisingSYNC() => RaisingSYNC?.Invoke(this, EventArgs.Empty); - protected virtual void OnRaisedSYNC() => RaisedSYNC?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweringSYNC() => LoweringSYNC?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweredSYNC() => LoweredSYNC?.Invoke(this, EventArgs.Empty); - [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] protected virtual void RaiseSYNC() { @@ -138,19 +122,15 @@ namespace M6502 public event EventHandler? RaisedRDY; public event EventHandler? LoweringRDY; public event EventHandler? LoweredRDY; - protected virtual void OnRaisingRDY() => RaisingRDY?.Invoke(this, EventArgs.Empty); - protected virtual void OnRaisedRDY() => RaisedRDY?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweringRDY() => LoweringRDY?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweredRDY() => LoweredRDY?.Invoke(this, EventArgs.Empty); [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public virtual void RaiseRDY() { if (this.RDY.Lowered()) { - this.OnRaisingRDY(); + RaisingRDY?.Invoke(this, EventArgs.Empty); this.RDY.Raise(); - this.OnRaisedRDY(); + RaisedRDY?.Invoke(this, EventArgs.Empty); } } @@ -158,9 +138,9 @@ namespace M6502 { if (this.RDY.Raised()) { - this.OnLoweringRDY(); + LoweringRDY?.Invoke(this, EventArgs.Empty); this.RDY.Lower(); - this.OnLoweredRDY(); + LoweredRDY?.Invoke(this, EventArgs.Empty); } } @@ -174,19 +154,15 @@ namespace M6502 public event EventHandler? RaisedRW; public event EventHandler? LoweringRW; public event EventHandler? LoweredRW; - protected virtual void OnRaisingRW() => RaisingRW?.Invoke(this, EventArgs.Empty); - protected virtual void OnRaisedRW() => RaisedRW?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweringRW() => LoweringRW?.Invoke(this, EventArgs.Empty); - protected virtual void OnLoweredRW() => LoweredRW?.Invoke(this, EventArgs.Empty); [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] public virtual void RaiseRW() { if (this.RW.Lowered()) { - this.OnRaisingRW(); + RaisingRW?.Invoke(this, EventArgs.Empty); this.RW.Raise(); - this.OnRaisedRW(); + RaisedRW?.Invoke(this, EventArgs.Empty); } } @@ -194,9 +170,9 @@ namespace M6502 { if (this.RW.Raised()) { - this.OnLoweringRW(); + LoweringRW?.Invoke(this, EventArgs.Empty); this.RW.Lower(); - this.OnLoweredRW(); + LoweredRW?.Invoke(this, EventArgs.Empty); } } diff --git a/M6502/WDC65C02.cs b/M6502/WDC65C02.cs index 85d4ba9..a0412b4 100644 --- a/M6502/WDC65C02.cs +++ b/M6502/WDC65C02.cs @@ -13,9 +13,9 @@ namespace M6502 { this.LoweredRESET += this.WDC65C02_LoweredRESET; this.LoweredINT += this.WDC65C02_LoweredINT; + this.LoweredNMI += this.WDC65C02_LoweredNMI; } - private bool _stopped; private bool _waiting; @@ -189,9 +189,8 @@ namespace M6502 this.Waiting = false; } - protected override void OnLoweredNMI() + private void WDC65C02_LoweredNMI(object? sender, EventArgs e) { - base.OnLoweredNMI(); this.Waiting = false; } diff --git a/Z80/Z80.cs b/Z80/Z80.cs index 3c75058..669ab7e 100644 --- a/Z80/Z80.cs +++ b/Z80/Z80.cs @@ -13,6 +13,8 @@ namespace Z80 { this._ports = ports; this.RaisedPOWER += this.Z80_RaisedPOWER; + this.LoweredHALT += this.Z80_LoweredHALT; + this.RaisedHALT += this.Z80_RaisedHALT; } private readonly InputOutput _ports; @@ -190,6 +192,16 @@ namespace Z80 this.ResetPrefixes(); } + private void Z80_RaisedHALT(object? sender, EventArgs e) + { + ++this.PC.Word; // Release the PC from HALT instruction + } + + private void Z80_LoweredHALT(object? sender, EventArgs e) + { + --this.PC.Word; // Keep the PC on the HALT instruction (i.e. executing NOP) + } + private void ResetPrefixes() { this._prefixCB = this._prefixDD = this._prefixED = this._prefixFD = false;