diff --git a/EightBit/Processor.cs b/EightBit/Processor.cs index d9d80c5..e12a384 100644 --- a/EightBit/Processor.cs +++ b/EightBit/Processor.cs @@ -163,37 +163,16 @@ namespace EightBit protected void OnWrittenMemory() => this.WrittenMemory?.Invoke(this, EventArgs.Empty); - protected void MemoryWrite(byte low, byte high) - { - this.Bus.Address.Assign(low, high); - this.MemoryWrite(); - } - protected void MemoryWrite(byte low, byte high, byte data) { this.Bus.Address.Assign(low, high); - this.Bus.Data = data; - this.MemoryWrite(); - } - - protected void MemoryWrite(ushort address, byte data) - { - this.Bus.Address.Word = address; - this.Bus.Data = data; - this.MemoryWrite(); + this.MemoryWrite(data); } protected void MemoryWrite(Register16 address, byte data) { this.Bus.Address.Assign(address); - this.Bus.Data = data; - this.MemoryWrite(); - } - - protected void MemoryWrite(Register16 address) - { - this.Bus.Address.Assign(address); - this.MemoryWrite(); + this.MemoryWrite(data); } protected void MemoryWrite(byte data) @@ -212,12 +191,6 @@ namespace EightBit return this.MemoryRead(); } - protected byte MemoryRead(ushort address) - { - this.Bus.Address.Word = address; - return this.MemoryRead(); - } - protected byte MemoryRead(Register16 address) { this.Bus.Address.Assign(address); diff --git a/Intel8080/Intel8080.cs b/Intel8080/Intel8080.cs index d07bd05..cb89fc8 100644 --- a/Intel8080/Intel8080.cs +++ b/Intel8080/Intel8080.cs @@ -628,7 +628,7 @@ namespace Intel8080 private byte Increment(byte operand) { - var result = ++operand; + var result = operand; ++result; AdjustSZP(result); ClearBit(StatusBits.AC, LowNibble(result)); return result; @@ -636,7 +636,7 @@ namespace Intel8080 private byte Decrement(byte operand) { - var result = --operand; + var result = operand; --result; AdjustSZP(result); SetBit(StatusBits.AC, LowNibble(result) != (byte)Mask.Four); return result; @@ -657,12 +657,8 @@ namespace Intel8080 protected sealed override void CallConditional(bool condition) { - this.FetchInto(this.MEMPTR); - if (condition) - { - this.Call(); - } - else + base.CallConditional(condition); + if (!condition) { this.Tick(); } @@ -670,12 +666,8 @@ namespace Intel8080 protected sealed override void ReturnConditionalFlag(int flag) { - var condition = this.ConvertCondition(flag); this.Tick(); - if (condition) - { - this.Return(); - } + base.ReturnConditionalFlag(flag); } private Register16 Add(Register16 operand, Register16 value) diff --git a/LR35902/LR35902.cs b/LR35902/LR35902.cs index 921b5ad..2fa8aca 100644 --- a/LR35902/LR35902.cs +++ b/LR35902/LR35902.cs @@ -5,6 +5,7 @@ namespace LR35902 { using EightBit; + using System.Diagnostics; public sealed class LR35902 : IntelProcessor { @@ -336,29 +337,34 @@ namespace LR35902 private void Start() => this.Stopped = false; - private void MemoryUpdate(int ticks) + private void MemoryUpdate(int ticks = 1) { + Debug.Assert(ticks > 0, "Ticks must be greater than zero"); + this.OnWritingMemory(); this.LowerMWR(); this.LowerWR(); base.MemoryWrite(); this.TickMachine(ticks); this.RaiseWR(); this.RaiseMWR(); + this.OnWrittenMemory(); } protected override void MemoryWrite() { - this.MemoryUpdate(1); + this.MemoryUpdate(); } protected override byte MemoryRead() { + this.OnReadingMemory(); this.LowerMWR(); this.LowerRD(); _ = base.MemoryRead(); this.TickMachine(); this.RaiseRD(); this.RaiseMWR(); + this.OnReadMemory(); return this.Bus.Data; } @@ -863,7 +869,7 @@ namespace LR35902 break; case 1: // CB prefix this._prefixCB = true; - this.Execute(this.FetchByte()); + this.Execute(this.FetchInstruction()); break; case 6: // DI this.DisableInterrupts(); @@ -1063,9 +1069,9 @@ namespace LR35902 private byte RL(byte operand) { - this.F = ClearBit(this.F, StatusBits.NF | StatusBits.HC | StatusBits.ZF); + this.ClearBit(StatusBits.NF | StatusBits.HC | StatusBits.ZF); var carry = this.Carry(); - this.F = SetBit(this.F, StatusBits.CF, operand & (byte)Bits.Bit7); + this.SetBit(StatusBits.CF, operand & (byte)Bits.Bit7); return (byte)((operand << 1) | (carry >> 4)); // CF at Bit4 } @@ -1146,13 +1152,13 @@ namespace LR35902 this.SetBit(StatusBits.CF, this.Carry() != 0 || (updated & (int)Bits.Bit8) != 0); this.A = LowByte(updated); - this.F = AdjustZero(this.F, this.A); + this.AdjustZero(this.A); } protected override void CPL() { base.CPL(); - this.F = SetBit(this.F, StatusBits.HC | StatusBits.NF); + this.SetBit(StatusBits.HC | StatusBits.NF); } private void SCF() @@ -1164,7 +1170,7 @@ namespace LR35902 private void CCF() { this.ClearBit(StatusBits.NF | StatusBits.HC); - this.ClearBit(StatusBits.CF, this.F & (byte)StatusBits.CF); + this.ClearBit(StatusBits.CF, this.Carry()); } private void RetI() diff --git a/Z80/Z80.cs b/Z80/Z80.cs index 8459519..241e8df 100644 --- a/Z80/Z80.cs +++ b/Z80/Z80.cs @@ -1643,19 +1643,21 @@ namespace Z80 private byte Increment(byte operand) { this.ClearBit(StatusBits.NF); - this.AdjustSZXY(++operand); - this.SetBit(StatusBits.VF, operand == (byte)Bits.Bit7); - this.ClearBit(StatusBits.HC, LowNibble(operand)); - return operand; + var result = operand; ++result; + this.AdjustSZXY(result); + this.SetBit(StatusBits.VF, result == (byte)Bits.Bit7); + this.ClearBit(StatusBits.HC, LowNibble(result)); + return result; } private byte Decrement(byte operand) { this.SetBit(StatusBits.NF); this.ClearBit(StatusBits.HC, LowNibble(operand)); - this.AdjustSZXY(--operand); - this.SetBit(StatusBits.VF, operand == (byte)Mask.Seven); - return operand; + var result = operand; --result; + this.AdjustSZXY(result); + this.SetBit(StatusBits.VF, result == (byte)Mask.Seven); + return result; } private void RetN() @@ -1681,12 +1683,8 @@ namespace Z80 protected sealed override void ReturnConditionalFlag(int flag) { - var condition = this.ConvertCondition(flag); this.Tick(); - if (condition) - { - this.Return(); - } + base.ReturnConditionalFlag(flag); } private Register16 SBC(Register16 operand, Register16 value)