Simplify switching processor pin handling

This commit is contained in:
Adrian Conlon
2025-06-22 21:07:02 +01:00
parent 3105930027
commit 3bbf300e05
7 changed files with 500 additions and 122 deletions

View File

@@ -43,9 +43,14 @@ namespace EightBit
public byte Read()
{
this.ReadingByte?.Invoke(this, EventArgs.Empty);
var returned = this.Data = this.Reference();
try
{
return this.Data = this.Reference();
}
finally
{
ReadByte?.Invoke(this, EventArgs.Empty);
return returned;
}
}
public byte Read(ushort absolute)
@@ -68,9 +73,15 @@ namespace EightBit
public void Write()
{
this.WritingByte?.Invoke(this, EventArgs.Empty);
try
{
this.Reference() = this.Data;
}
finally
{
this.WrittenByte?.Invoke(this, EventArgs.Empty);
}
}
public void Write(byte value)
{

View File

@@ -26,19 +26,31 @@ namespace EightBit
if (this.POWER.Lowered())
{
RaisingPOWER?.Invoke(this, EventArgs.Empty);
try
{
this.POWER.Raise();
}
finally
{
RaisedPOWER?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerPOWER()
{
if (this.POWER.Raised())
{
LoweringPOWER?.Invoke(this, EventArgs.Empty);
try
{
this.POWER.Lower();
}
finally
{
LoweredPOWER?.Invoke(this, EventArgs.Empty);
}
}
}
}
}

View File

@@ -28,9 +28,14 @@ namespace EightBit
public byte ReadInputPort(ushort port)
{
ReadingPort?.Invoke(this, new PortEventArgs(port));
var value = this._input[port];
try
{
return this._input[port];
}
finally
{
ReadPort?.Invoke(this, new PortEventArgs(port));
return value;
}
}
public void WriteInputPort(ushort port, byte value) => this._input[port] = value;
@@ -40,8 +45,14 @@ namespace EightBit
public void WriteOutputPort(ushort port, byte value)
{
WritingPort?.Invoke(this, new PortEventArgs(port));
try
{
this._output[port] = value;
}
finally
{
WrittenPort?.Invoke(this, new PortEventArgs(port));
}
}
}
}

View File

@@ -69,20 +69,32 @@ namespace EightBit
if (this.HALT.Lowered())
{
RaisingHALT?.Invoke(this, EventArgs.Empty);
try
{
this.HALT.Raise();
}
finally
{
RaisedHALT?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerHALT()
{
if (this.HALT.Raised())
{
LoweringHALT?.Invoke(this, EventArgs.Empty);
try
{
this.HALT.Lower();
}
finally
{
LoweredHALT?.Invoke(this, EventArgs.Empty);
}
}
}
protected abstract void DisableInterrupts();

View File

@@ -69,11 +69,17 @@ namespace EightBit
{
this.ResetCycles();
ExecutingInstruction?.Invoke(this, EventArgs.Empty);
try
{
if (this.Powered)
{
this.PoweredStep();
}
}
finally
{
ExecutedInstruction?.Invoke(this, EventArgs.Empty);
}
return this.Cycles;
}
@@ -115,20 +121,32 @@ namespace EightBit
if (this.RESET.Lowered())
{
RaisingRESET?.Invoke(this, EventArgs.Empty);
try
{
this.RESET.Raise();
}
finally
{
RaisedRESET?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerRESET()
{
if (this.RESET.Raised())
{
LoweringRESET?.Invoke(this, EventArgs.Empty);
try
{
this.RESET.Lower();
}
finally
{
LoweredRESET?.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 RaiseINT()
@@ -136,20 +154,32 @@ namespace EightBit
if (this.INT.Lowered())
{
RaisingINT?.Invoke(this, EventArgs.Empty);
try
{
this.INT.Raise();
}
finally
{
RaisedINT?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerINT()
{
if (this.INT.Raised())
{
LoweringINT?.Invoke(this, EventArgs.Empty);
try
{
this.INT.Lower();
}
finally
{
LoweredINT?.Invoke(this, EventArgs.Empty);
}
}
}
protected virtual void HandleRESET() => this.RaiseRESET();

View File

@@ -33,20 +33,32 @@ namespace M6502
if (this.NMI.Lowered())
{
RaisingNMI?.Invoke(this, EventArgs.Empty);
try
{
this.NMI.Raise();
}
finally
{
RaisedNMI?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerNMI()
{
if (this.NMI.Raised())
{
LoweringNMI?.Invoke(this, EventArgs.Empty);
try
{
this.NMI.Lower();
}
finally
{
LoweredNMI?.Invoke(this, EventArgs.Empty);
}
}
}
#endregion
@@ -65,20 +77,32 @@ namespace M6502
if (this.SO.Lowered())
{
RaisingSO?.Invoke(this, EventArgs.Empty);
try
{
this.SO.Raise();
}
finally
{
RaisedSO?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerSO()
{
if (this.SO.Raised())
{
LoweringSO?.Invoke(this, EventArgs.Empty);
try
{
this.SO.Lower();
}
finally
{
LoweredSO?.Invoke(this, EventArgs.Empty);
}
}
}
#endregion
@@ -97,20 +121,32 @@ namespace M6502
if (this.SYNC.Lowered())
{
RaisingSYNC?.Invoke(this, EventArgs.Empty);
try
{
this.SYNC.Raise();
}
finally
{
RaisedSYNC?.Invoke(this, EventArgs.Empty);
}
}
}
protected virtual void LowerSYNC()
{
if (this.SYNC.Raised())
{
LoweringSYNC?.Invoke(this, EventArgs.Empty);
try
{
this.SYNC.Lower();
}
finally
{
LoweredSYNC?.Invoke(this, EventArgs.Empty);
}
}
}
#endregion
@@ -129,20 +165,32 @@ namespace M6502
if (this.RDY.Lowered())
{
RaisingRDY?.Invoke(this, EventArgs.Empty);
try
{
this.RDY.Raise();
}
finally
{
RaisedRDY?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerRDY()
{
if (this.RDY.Raised())
{
LoweringRDY?.Invoke(this, EventArgs.Empty);
try
{
this.RDY.Lower();
}
finally
{
LoweredRDY?.Invoke(this, EventArgs.Empty);
}
}
}
#endregion
@@ -161,20 +209,32 @@ namespace M6502
if (this.RW.Lowered())
{
RaisingRW?.Invoke(this, EventArgs.Empty);
try
{
this.RW.Raise();
}
finally
{
RaisedRW?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerRW()
{
if (this.RW.Raised())
{
LoweringRW?.Invoke(this, EventArgs.Empty);
try
{
this.RW.Lower();
}
finally
{
LoweredRW?.Invoke(this, EventArgs.Empty);
}
}
}
#endregion

View File

@@ -205,20 +205,54 @@ namespace Z80
if (this.NMI.Lowered())
{
RaisingNMI?.Invoke(this, EventArgs.Empty);
try
{
this.NMI.Raise();
}
finally
{
RaisedNMI?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerNMI()
{
if (this.NMI.Raised())
{
LoweringNMI?.Invoke(this, EventArgs.Empty);
try
{
this.NMI.Lower();
}
finally
{
LoweredNMI?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoNMI : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoNMI(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerNMI();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseNMI();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -246,20 +280,53 @@ namespace Z80
if (this.M1.Lowered())
{
RaisingM1?.Invoke(this, EventArgs.Empty);
try
{
this.M1.Raise();
}
finally
{
RaisedM1?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerM1()
{
if (this.M1.Raised())
{
LoweringM1?.Invoke(this, EventArgs.Empty);
try
{
this.M1.Lower();
}
finally
{
LoweredM1?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoM1 : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoM1(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerM1();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseM1();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -288,21 +355,55 @@ namespace Z80
if (this.RFSH.Lowered())
{
RaisingRFSH?.Invoke(this, EventArgs.Empty);
try
{
this.RFSH.Raise();
++this.REFRESH;
}
finally
{
RaisedRFSH?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerRFSH()
{
if (this.RFSH.Raised())
{
LoweringRFSH?.Invoke(this, EventArgs.Empty);
try
{
this.RFSH.Lower();
}
finally
{
LoweredRFSH?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoRFSH : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoRFSH(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerRFSH();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseRFSH();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -326,20 +427,54 @@ namespace Z80
if (this.MREQ.Lowered())
{
RaisingMREQ?.Invoke(this, EventArgs.Empty);
try
{
this.MREQ.Raise();
}
finally
{
RaisedMREQ?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerMREQ()
{
if (this.MREQ.Raised())
{
LoweringMREQ?.Invoke(this, EventArgs.Empty);
try
{
this.MREQ.Lower();
}
finally
{
LoweredMREQ?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoMREQ : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoMREQ(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerMREQ();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseMREQ();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -363,10 +498,16 @@ namespace Z80
if (this.IORQ.Lowered())
{
RaisingIORQ?.Invoke(this, EventArgs.Empty);
try
{
this.IORQ.Raise();
}
finally
{
RaisedIORQ?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerIORQ()
{
@@ -378,6 +519,28 @@ namespace Z80
}
}
protected sealed class AutoIORQ : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoIORQ(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerIORQ();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseIORQ();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
#region RD pin
@@ -400,20 +563,54 @@ namespace Z80
if (this.RD.Lowered())
{
RaisingRD?.Invoke(this, EventArgs.Empty);
try
{
this.RD.Raise();
}
finally
{
RaisedRD?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerRD()
{
if (this.RD.Raised())
{
LoweringRD?.Invoke(this, EventArgs.Empty);
try
{
this.RD.Lower();
}
finally
{
LoweredRD?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoRD : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoRD(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerRD();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseRD();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -437,20 +634,54 @@ namespace Z80
if (this.WR.Lowered())
{
RaisingWR?.Invoke(this, EventArgs.Empty);
try
{
this.WR.Raise();
}
finally
{
RaisedWR?.Invoke(this, EventArgs.Empty);
}
}
}
public virtual void LowerWR()
{
if (this.WR.Raised())
{
LoweringWR?.Invoke(this, EventArgs.Empty);
try
{
this.WR.Lower();
}
finally
{
LoweredWR?.Invoke(this, EventArgs.Empty);
}
}
}
protected sealed class AutoWR : IDisposable
{
private readonly Z80 _cpu;
private bool _disposed;
public AutoWR(Z80 cpu)
{
_cpu = cpu;
_cpu.LowerWR();
}
public void Dispose()
{
if (!_disposed)
{
_cpu.RaiseWR();
_disposed = true;
}
GC.SuppressFinalize(this);
}
}
#endregion
@@ -459,14 +690,18 @@ namespace Z80
protected void MemoryUpdate(int ticks)
{
this.OnWritingMemory();
this.LowerMREQ();
this.LowerWR();
try
{
using var _ = new AutoMREQ(this);
using var __ = new AutoWR(this);
this.Tick(ticks);
base.MemoryWrite();
this.RaiseWR();
this.RaiseMREQ();
}
finally
{
this.OnWroteMemory();
}
}
protected override void MemoryWrite()
{
@@ -476,25 +711,32 @@ namespace Z80
protected override byte MemoryRead()
{
this.OnReadingMemory();
try
{
this.Tick();
this.LowerMREQ();
this.LowerRD();
try
{
using var __ = new AutoMREQ(this);
using var _ = new AutoRD(this);
this.Tick();
var returned = base.MemoryRead();
this.RaiseRD();
this.RaiseMREQ();
return base.MemoryRead();
}
finally
{
if (this.M1.Lowered())
{
this.Bus.Address.Assign(this.REFRESH, this.IV);
this.LowerRFSH();
using var _ = new AutoRFSH(this);
this.Tick();
this.LowerMREQ();
this.RaiseMREQ();
this.RaiseRFSH();
using var __ = new AutoMREQ(this);
}
this.Tick();
}
}
finally
{
this.OnReadMemory();
return returned;
}
}
// From Zilog Z80 manual
@@ -516,15 +758,18 @@ namespace Z80
this.Tick(3);
}
private byte ReadDataUnderInterrupt()
{
using var _ = new AutoM1(this);
using var __ = new AutoIORQ(this);
return this.Bus.Data;
}
protected override void HandleINT()
{
base.HandleINT();
this.LowerM1();
this.LowerIORQ();
var data = this.Bus.Data;
this.RaiseIORQ();
this.RaiseM1();
var data = this.ReadDataUnderInterrupt();
this.DisableInterrupts();
this.Tick(5);
@@ -1560,9 +1805,8 @@ namespace Z80
this.RaiseHALT();
this.IFF2 = this.IFF1;
this.IFF1 = false;
this.LowerM1();
using var __ = new AutoM1(this);
_ = this.Bus.Data;
this.RaiseM1();
this.Restart(0x66);
}
@@ -1582,10 +1826,8 @@ namespace Z80
// instruction so that no other concurrent operation can be performed.
protected override byte FetchInstruction()
{
this.LowerM1();
var returned = base.FetchInstruction();
this.RaiseM1();
return returned;
using var _ = new AutoM1(this);
return base.FetchInstruction();
}
private byte Subtract(byte operand, byte value, int carry = 0)
@@ -2321,12 +2563,12 @@ namespace Z80
{
this.MEMPTR.Assign(this.Bus.Address);
this.Tick(2);
this.LowerIORQ();
this.LowerWR();
{
using var _ = new AutoIORQ(this);
using var __ = new AutoWR(this);
this.Ports.Write(this.Bus.Address, this.Bus.Data);
this.Tick();
this.RaiseWR();
this.RaiseIORQ();
}
this.Tick();
}
@@ -2341,12 +2583,12 @@ namespace Z80
{
this.MEMPTR.Assign(this.Bus.Address);
this.Tick(2);
this.LowerIORQ();
this.LowerRD();
{
using var _ = new AutoIORQ(this);
using var __ = new AutoRD(this);
this.Bus.Data = this.Ports.Read(this.Bus.Address);
this.Tick();
this.RaiseRD();
this.RaiseIORQ();
}
this.Tick();
}