From 39561437e9f09a80eab2bbc00fc9f247e698a5f4 Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Wed, 10 Jun 2026 17:26:26 +0100 Subject: [PATCH] More performance improvements --- EightBit/Bus.cs | 5 +++++ EightBit/ClockedChip.cs | 8 ++++++-- EightBit/Processor.cs | 7 +++++++ Z80/Z80.cs | 13 +++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/EightBit/Bus.cs b/EightBit/Bus.cs index fee5307..fb979b4 100644 --- a/EightBit/Bus.cs +++ b/EightBit/Bus.cs @@ -3,6 +3,7 @@ // using System.Diagnostics; +using System.Runtime.CompilerServices; namespace EightBit { @@ -35,6 +36,7 @@ namespace EightBit this.Poke(absolute.Joined, value); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Read() { Debug.Assert(!this._writing, "Writing flag is in an invalid state"); @@ -42,6 +44,7 @@ namespace EightBit Debug.Assert(!this._writing, "Writing flag is in an invalid state"); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Write() { Debug.Assert(!this._writing, "Writing flag is in an invalid state"); @@ -63,6 +66,7 @@ namespace EightBit public abstract void Initialize(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected ref byte Reference(ushort absolute) { var mapped = this.Mapping(absolute); @@ -78,6 +82,7 @@ namespace EightBit return ref this._data; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected ref byte Reference() => ref this.Reference(this.Address.Joined); protected void LoadHexFile(string path) diff --git a/EightBit/ClockedChip.cs b/EightBit/ClockedChip.cs index 695ee56..c4752f4 100644 --- a/EightBit/ClockedChip.cs +++ b/EightBit/ClockedChip.cs @@ -20,9 +20,13 @@ namespace EightBit public void Tick(int extra = 1) { this.Cycles += extra; - for (var i = 0; i < extra; ++i) + var ticked = this.Ticked; + if (ticked is not null) { - this.Ticked?.Invoke(this, EventArgs.Empty); + for (var i = 0; i < extra; ++i) + { + ticked.Invoke(this, EventArgs.Empty); + } } } diff --git a/EightBit/Processor.cs b/EightBit/Processor.cs index 74e8d27..26479ea 100644 --- a/EightBit/Processor.cs +++ b/EightBit/Processor.cs @@ -3,6 +3,7 @@ // using System.Diagnostics; +using System.Runtime.CompilerServices; namespace EightBit { @@ -158,12 +159,16 @@ namespace EightBit protected virtual void HandleINT() => this.RaiseINT(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void OnReadingMemory() => this.ReadingMemory?.Invoke(this, EventArgs.Empty); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void OnReadMemory() => this.ReadMemory?.Invoke(this, EventArgs.Empty); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void OnWritingMemory() => this.WritingMemory?.Invoke(this, EventArgs.Empty); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void OnWrittenMemory() => this.WrittenMemory?.Invoke(this, EventArgs.Empty); protected void MemoryWrite(byte low, byte high, byte data) @@ -186,6 +191,7 @@ namespace EightBit protected virtual void MemoryWrite() => this.BusWrite(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void BusWrite() => this.Bus.Write(); // N.B. Should be the only real call into the "Bus.Write" code. protected void MemoryRead(byte low, byte high) @@ -202,6 +208,7 @@ namespace EightBit protected virtual void MemoryRead() => this.BusRead(); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void BusRead() => this.Bus.Read(); // N.B. Should be the only real call into the "Bus.Read" code. protected virtual void IncrementPC() => this.PC.Increment(); diff --git a/Z80/Z80.cs b/Z80/Z80.cs index 4535026..baa44ef 100644 --- a/Z80/Z80.cs +++ b/Z80/Z80.cs @@ -6,6 +6,7 @@ namespace Z80 { using EightBit; using System.Diagnostics; + using System.Runtime.CompilerServices; public sealed class Z80 : IntelProcessor { @@ -307,6 +308,7 @@ namespace Z80 public ref PinLevel M1 => ref this._m1Line; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseM1() { if (this.M1.Lowered()) @@ -317,6 +319,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerM1() { if (this.M1.Raised()) @@ -349,6 +352,7 @@ namespace Z80 public ref PinLevel RFSH => ref this._refreshLine; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseRFSH() { if (this.RFSH.Lowered()) @@ -360,6 +364,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerRFSH() { if (this.RFSH.Raised()) @@ -387,6 +392,7 @@ namespace Z80 public ref PinLevel MREQ => ref this._memoryRequestLine; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseMREQ() { if (this.MREQ.Lowered()) @@ -397,6 +403,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerMREQ() { if (this.MREQ.Raised()) @@ -424,6 +431,7 @@ namespace Z80 public ref PinLevel IORQ => ref this._ioRequestLine; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseIORQ() { if (this.IORQ.Lowered()) @@ -434,6 +442,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerIORQ() { if (this.IORQ.Raised()) @@ -461,6 +470,7 @@ namespace Z80 public ref PinLevel RD => ref this._rdLine; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseRD() { if (this.RD.Lowered()) @@ -471,6 +481,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerRD() { if (this.RD.Raised()) @@ -498,6 +509,7 @@ namespace Z80 public ref PinLevel WR => ref this._wrLine; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1030:Use events where appropriate", Justification = "The word 'raise' is used in an electrical sense")] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void RaiseWR() { if (this.WR.Lowered()) @@ -508,6 +520,7 @@ namespace Z80 } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void LowerWR() { if (this.WR.Raised())