Add increment/decrement operations to the Register16 class

This commit is contained in:
Adrian Conlon
2025-05-11 19:24:40 +01:00
parent 60d000905f
commit 36e983526e
11 changed files with 65 additions and 60 deletions

View File

@@ -28,7 +28,7 @@ namespace EightBit
protected override Register16 GetWord() protected override Register16 GetWord()
{ {
this.Intermediate.High = this.MemoryRead(); this.Intermediate.High = this.MemoryRead();
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.Intermediate.Low = this.MemoryRead(); this.Intermediate.Low = this.MemoryRead();
return this.Intermediate; return this.Intermediate;
} }
@@ -57,7 +57,7 @@ namespace EightBit
protected override void SetWord(Register16 value) protected override void SetWord(Register16 value)
{ {
this.MemoryWrite(value.High); this.MemoryWrite(value.High);
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.MemoryWrite(value.Low); this.MemoryWrite(value.Low);
} }

View File

@@ -119,12 +119,7 @@ namespace EightBit
return ref mapped.Memory.Reference(offset); return ref mapped.Memory.Reference(offset);
} }
protected ref byte Reference(Register16 absolute) protected ref byte Reference() => ref this.Reference(this.Address.Word);
{
return ref this.Reference(absolute.Word);
}
protected ref byte Reference() => ref this.Reference(this.Address);
protected void LoadHexFile(string path) protected void LoadHexFile(string path)
{ {

View File

@@ -137,14 +137,14 @@ namespace EightBit
protected sealed override void Push(byte value) protected sealed override void Push(byte value)
{ {
--this.SP.Word; this.SP.Decrement();
this.MemoryWrite(this.SP, value); this.MemoryWrite(this.SP, value);
} }
protected sealed override byte Pop() protected sealed override byte Pop()
{ {
var returned = this.MemoryRead(this.SP); var returned = this.MemoryRead(this.SP);
this.SP.Word++; this.SP.Increment();
return returned; return returned;
} }

View File

@@ -29,7 +29,7 @@ namespace EightBit
protected override Register16 GetWord() protected override Register16 GetWord()
{ {
this.Intermediate.Low = this.MemoryRead(); this.Intermediate.Low = this.MemoryRead();
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.Intermediate.High = this.MemoryRead(); this.Intermediate.High = this.MemoryRead();
return this.Intermediate; return this.Intermediate;
} }
@@ -58,7 +58,7 @@ namespace EightBit
protected override void SetWord(Register16 value) protected override void SetWord(Register16 value)
{ {
this.MemoryWrite(value.Low); this.MemoryWrite(value.Low);
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.MemoryWrite(value.High); this.MemoryWrite(value.High);
} }

View File

@@ -225,9 +225,9 @@ namespace EightBit
protected virtual byte BusRead() => this.Bus.Read(); // N.B. Should be the only real call into the "Bus.Read" code. protected virtual byte BusRead() => this.Bus.Read(); // N.B. Should be the only real call into the "Bus.Read" code.
protected virtual void IncrementPC() => ++this.PC.Word; protected virtual void IncrementPC() => this.PC.Increment();
protected virtual void DecrementPC() => --this.PC.Word; protected virtual void DecrementPC() => this.PC.Decrement();
protected virtual void ImmediateAddress() protected virtual void ImmediateAddress()
{ {
@@ -311,12 +311,9 @@ namespace EightBit
this.SetWord(value); this.SetWord(value);
} }
protected void Jump(ushort destination) => this.PC.Word = destination; protected void Jump(ushort destination) => this.PC.Assign(destination);
protected void Jump(Register16 destination) protected void Jump(Register16 destination) => this.PC.Assign(destination);
{
this.PC.Assign(destination);
}
protected void Call(ushort destination) protected void Call(ushort destination)
{ {

View File

@@ -98,5 +98,9 @@ namespace EightBit
} }
} }
} }
public ushort Increment() => this.Word++;
public ushort Decrement() => this.Word--;
} }
} }

View File

@@ -326,10 +326,10 @@ namespace Intel8080
switch (q) switch (q)
{ {
case 0: // INC rp case 0: // INC rp
++this.RP(p).Word; this.RP(p).Increment();
break; break;
case 1: // DEC rp case 1: // DEC rp
--this.RP(p).Word; this.RP(p).Decrement();
break; break;
default: default:
throw new NotSupportedException("Invalid operation mode"); throw new NotSupportedException("Invalid operation mode");
@@ -757,11 +757,11 @@ namespace Intel8080
private void XHTL(Register16 exchange) private void XHTL(Register16 exchange)
{ {
this.MEMPTR.Low = this.MemoryRead(this.SP); this.MEMPTR.Low = this.MemoryRead(this.SP);
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.MEMPTR.High = this.MemoryRead(); this.MEMPTR.High = this.MemoryRead();
this.MemoryWrite(exchange.High); this.MemoryWrite(exchange.High);
exchange.High = this.MEMPTR.High; exchange.High = this.MEMPTR.High;
--this.Bus.Address.Word; this.Bus.Address.Decrement();
this.MemoryWrite(exchange.Low); this.MemoryWrite(exchange.Low);
exchange.Low = this.MEMPTR.Low; exchange.Low = this.MEMPTR.Low;
} }

View File

@@ -150,7 +150,7 @@ namespace LR35902
public void IncrementDIV() public void IncrementDIV()
{ {
++this.divCounter.Word; this.divCounter.Increment();
this.Poke(DIV, this.divCounter.High); this.Poke(DIV, this.divCounter.High);
} }

View File

@@ -561,11 +561,11 @@ namespace LR35902
break; break;
case 2: // GB: LDI (HL),A case 2: // GB: LDI (HL),A
this.MemoryWrite(this.HL.Word++, this.A); this.MemoryWrite(this.HL.Increment(), this.A);
break; break;
case 3: // GB: LDD (HL),A case 3: // GB: LDD (HL),A
this.MemoryWrite(this.HL.Word--, this.A); this.MemoryWrite(this.HL.Decrement(), this.A);
break; break;
default: default:
@@ -577,10 +577,10 @@ namespace LR35902
case 1: case 1:
this.A = p switch this.A = p switch
{ {
0 => this.MemoryRead(this.BC), // LD A,(BC) 0 => this.MemoryRead(this.BC), // LD A,(BC)
1 => this.MemoryRead(this.DE), // LD A,(DE) 1 => this.MemoryRead(this.DE), // LD A,(DE)
2 => this.MemoryRead(this.HL.Word++), // GB: LDI A,(HL) 2 => this.MemoryRead(this.HL.Increment()), // GB: LDI A,(HL)
3 => this.MemoryRead(this.HL.Word--), // GB: LDD A,(HL) 3 => this.MemoryRead(this.HL.Decrement()), // GB: LDD A,(HL)
_ => throw new InvalidOperationException("Invalid operation mode"), _ => throw new InvalidOperationException("Invalid operation mode"),
}; };
break; break;
@@ -595,11 +595,11 @@ namespace LR35902
switch (q) switch (q)
{ {
case 0: // INC rp case 0: // INC rp
++this.RP(p).Word; this.RP(p).Increment();
break; break;
case 1: // DEC rp case 1: // DEC rp
--this.RP(p).Word; this.RP(p).Decrement();
break; break;
default: default:

View File

@@ -476,7 +476,11 @@ namespace EightBit
private void OnLoweredRW() => this.LoweredRW?.Invoke(this, EventArgs.Empty); private void OnLoweredRW() => this.LoweredRW?.Invoke(this, EventArgs.Empty);
private void Push(Register16 stack, byte value) => this.MemoryWrite(--stack.Word, value); private void Push(Register16 stack, byte value)
{
stack.Decrement();
this.MemoryWrite(stack, value);
}
private void PushS(byte value) => this.Push(this.S, value); private void PushS(byte value) => this.Push(this.S, value);
@@ -486,7 +490,12 @@ namespace EightBit
this.Push(stack, value.High); this.Push(stack, value.High);
} }
private byte Pop(Register16 stack) => this.MemoryRead(stack.Word++); private byte Pop(Register16 stack)
{
var read = this.MemoryRead(stack);
stack.Increment();
return read;
}
private byte PopS() => this.Pop(this.S); private byte PopS() => this.Pop(this.S);

View File

@@ -913,7 +913,7 @@ namespace Z80
case 0: // Input from port with 16-bit address case 0: // Input from port with 16-bit address
this.Bus.Address.Assign(this.BC); this.Bus.Address.Assign(this.BC);
this.ReadPort(); this.ReadPort();
this.MEMPTR.Word++; this.MEMPTR.Increment();
if (y != 6) if (y != 6)
{ {
this.R(y, AccessLevel.WriteOnly) = this.Bus.Data; // IN r[y],(C) this.R(y, AccessLevel.WriteOnly) = this.Bus.Data; // IN r[y],(C)
@@ -924,7 +924,7 @@ namespace Z80
break; break;
case 1: // Output to port with 16-bit address case 1: // Output to port with 16-bit address
this.WritePort(this.BC, y == 6 ? (byte)0 : this.R(y)); this.WritePort(this.BC, y == 6 ? (byte)0 : this.R(y));
this.MEMPTR.Word++; this.MEMPTR.Increment();
break; break;
case 2: // 16-bit add/subtract with carry case 2: // 16-bit add/subtract with carry
this.HL2().Assign(q switch this.HL2().Assign(q switch
@@ -1215,10 +1215,10 @@ namespace Z80
switch (q) switch (q)
{ {
case 0: // INC rp case 0: // INC rp
++this.RP(p).Word; this.RP(p).Increment();
break; break;
case 1: // DEC rp case 1: // DEC rp
--this.RP(p).Word; this.RP(p).Decrement();
break; break;
default: default:
throw new NotSupportedException("Invalid operation mode"); throw new NotSupportedException("Invalid operation mode");
@@ -1927,16 +1927,16 @@ namespace Z80
private void XHTL(Register16 exchange) private void XHTL(Register16 exchange)
{ {
this.MEMPTR.Low = this.MemoryRead(this.SP); this.MEMPTR.Low = this.MemoryRead(this.SP);
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.MEMPTR.High = this.MemoryRead(); this.MEMPTR.High = this.MemoryRead();
this.Tick(); this.Tick();
--this.Bus.Address.Word; this.Bus.Address.Decrement();
this.Tick(); this.Tick();
this.Bus.Data = exchange.Low; this.Bus.Data = exchange.Low;
exchange.Low = this.MEMPTR.Low; exchange.Low = this.MEMPTR.Low;
this.MemoryUpdate(1); this.MemoryUpdate(1);
this.Tick(); this.Tick();
++this.Bus.Address.Word; this.Bus.Address.Increment();
this.Tick(); this.Tick();
this.Bus.Data = exchange.High; this.Bus.Data = exchange.High;
exchange.High = this.MEMPTR.High; exchange.High = this.MEMPTR.High;
@@ -1980,15 +1980,15 @@ namespace Z80
private void CPI() private void CPI()
{ {
this.BlockCompare(); this.BlockCompare();
++this.HL.Word; this.HL.Increment();
++this.MEMPTR.Word; this.MEMPTR.Increment();
} }
private void CPD() private void CPD()
{ {
this.BlockCompare(); this.BlockCompare();
--this.HL.Word; this.HL.Decrement();
--this.MEMPTR.Word; this.MEMPTR.Decrement();
} }
#endregion #endregion
@@ -2045,15 +2045,15 @@ namespace Z80
private void LDI() private void LDI()
{ {
this.BlockLoad(); this.BlockLoad();
++this.HL.Word; this.HL.Increment();
++this.DE.Word; this.DE.Increment();
} }
private void LDD() private void LDD()
{ {
this.BlockLoad(); this.BlockLoad();
--this.HL.Word; this.HL.Decrement();
--this.DE.Word; this.DE.Decrement();
} }
#endregion #endregion
@@ -2146,16 +2146,16 @@ namespace Z80
{ {
this.BlockIn(); this.BlockIn();
this.AdjustBlockInFlagsIncrement(); this.AdjustBlockInFlagsIncrement();
++this.HL.Word; this.HL.Increment();
++this.MEMPTR.Word; this.MEMPTR.Increment();
} }
private void IND() private void IND()
{ {
this.BlockIn(); this.BlockIn();
this.AdjustBlockInFlagsDecrement(); this.AdjustBlockInFlagsDecrement();
--this.HL.Word; this.HL.Decrement();
--this.MEMPTR.Word; this.MEMPTR.Decrement();
} }
#endregion #endregion
@@ -2203,17 +2203,17 @@ namespace Z80
private void OUTI() private void OUTI()
{ {
this.BlockOut(); this.BlockOut();
++this.HL.Word; this.HL.Increment();
this.AdjustBlockOutFlags(); this.AdjustBlockOutFlags();
++this.MEMPTR.Word; this.MEMPTR.Increment();
} }
private void OUTD() private void OUTD()
{ {
this.BlockOut(); this.BlockOut();
--this.HL.Word; this.HL.Decrement();
this.AdjustBlockOutFlags(); this.AdjustBlockOutFlags();
--this.MEMPTR.Word; this.MEMPTR.Decrement();
} }
#endregion #endregion
@@ -2275,7 +2275,7 @@ namespace Z80
private byte ReadMemoryIndirect() private byte ReadMemoryIndirect()
{ {
this.Bus.Address.Assign(this.MEMPTR); this.Bus.Address.Assign(this.MEMPTR);
++this.MEMPTR.Word; this.MEMPTR.Increment();
return this.MemoryRead(); return this.MemoryRead();
} }
@@ -2288,7 +2288,7 @@ namespace Z80
private void WriteMemoryIndirect(byte data) private void WriteMemoryIndirect(byte data)
{ {
this.Bus.Address.Assign(this.MEMPTR); this.Bus.Address.Assign(this.MEMPTR);
++this.MEMPTR.Word; this.MEMPTR.Increment();
this.MEMPTR.High = this.Bus.Data = data; this.MEMPTR.High = this.Bus.Data = data;
this.MemoryWrite(); this.MemoryWrite();
} }
@@ -2355,7 +2355,7 @@ namespace Z80
{ {
this.Bus.Address.Assign(port, this.Bus.Data = this.A); this.Bus.Address.Assign(port, this.Bus.Data = this.A);
this.ReadPort(); this.ReadPort();
++this.MEMPTR.Word; this.MEMPTR.Increment();
} }
private void ReadPort() private void ReadPort()