Skip to content

Commit

Permalink
Rework Memory class: Remove Get / Set methods, use indexers everywher…
Browse files Browse the repository at this point in the history
…e. Make indexers depend on IByteReaderWriter instead of memory and create a new interface to represent Indexer 8 / 16 / 32 holders which is IIndexed
  • Loading branch information
kevinferrare committed Jul 6, 2023
1 parent 059505c commit 5fce324
Show file tree
Hide file tree
Showing 27 changed files with 241 additions and 248 deletions.
14 changes: 7 additions & 7 deletions src/Spice86.Core/Emulator/CPU/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void NearRet(int numberOfBytesToPop) {
}

public uint NextUint32() {
uint res = _memory.GetUint32(InternalIpPhysicalAddress);
uint res = _memory.UInt32[InternalIpPhysicalAddress];
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, _internalIp);
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, (ushort) (_internalIp+1));
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, (ushort) (_internalIp+2));
Expand All @@ -168,30 +168,30 @@ public uint NextUint32() {
}

public ushort NextUint16() {
ushort res = _memory.GetUint16(InternalIpPhysicalAddress);
ushort res = _memory.UInt16[InternalIpPhysicalAddress];
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, _internalIp);
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, (ushort) (_internalIp+1));
_internalIp += 2;
return res;
}

public byte NextUint8() {
byte res = _memory.GetUint8(InternalIpPhysicalAddress);
byte res = _memory.UInt8[InternalIpPhysicalAddress];
ExecutionFlowRecorder.RegisterExecutableByte(_machine, State.CS, _internalIp);
_internalIp++;
return res;
}

public void SetFlagOnInterruptStack(int flagMask, bool flagValue) {
uint flagsAddress = MemoryUtils.ToPhysicalAddress(State.SS, (ushort)(State.SP + 4));
int value = _memory.GetUint16(flagsAddress);
int value = _memory.UInt16[flagsAddress];
if (flagValue) {
value |= flagMask;
} else {
value &= ~flagMask;
}

_memory.SetUint16(flagsAddress, (ushort)value);
_memory.UInt16[flagsAddress] = (ushort)value;
}

private void HandleCpuException(CpuException cpuException) {
Expand Down Expand Up @@ -1217,8 +1217,8 @@ private void Interrupt(byte? vectorNumber, bool external) {
return;
}

ushort targetIP = _memory.GetUint16((ushort)(4 * vectorNumber.Value));
ushort targetCS = _memory.GetUint16((ushort)((4 * vectorNumber.Value) + 2));
ushort targetIP = _memory.UInt16[(ushort)(4 * vectorNumber.Value)];
ushort targetCS = _memory.UInt16[(ushort)((4 * vectorNumber.Value) + 2)];
if (ErrorOnUninitializedInterruptHandler && targetCS == 0 && targetIP == 0) {
throw new UnhandledOperationException(_machine,
$"Int was called but vector was not initialized for vectorNumber={ConvertUtils.ToHex(vectorNumber.Value)}");
Expand Down
26 changes: 13 additions & 13 deletions src/Spice86.Core/Emulator/CPU/InstructionsImpl/Instructions16.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ protected override void AdvanceDI() {
}

public override void Movs() {
ushort value = Memory.GetUint16(MemoryAddressOverridableDsSi);
Memory.SetUint16(MemoryAddressEsDi, value);
ushort value = Memory.UInt16[MemoryAddressOverridableDsSi];
Memory.UInt16[MemoryAddressEsDi] = value;
AdvanceSIDI();
}

public override void Cmps() {
ushort value = Memory.GetUint16(MemoryAddressOverridableDsSi);
Alu.Sub16(value, Memory.GetUint16(MemoryAddressEsDi));
ushort value = Memory.UInt16[MemoryAddressOverridableDsSi];
Alu.Sub16(value, Memory.UInt16[MemoryAddressEsDi]);
AdvanceSIDI();
}

Expand All @@ -258,30 +258,30 @@ public override void TestAccImm() {
}

public override void Stos() {
Memory.SetUint16(MemoryAddressEsDi, State.AX);
Memory.UInt16[MemoryAddressEsDi] = State.AX;
AdvanceDI();
}

public override void Lods() {
State.AX = Memory.GetUint16(MemoryAddressOverridableDsSi);
State.AX = Memory.UInt16[MemoryAddressOverridableDsSi];
AdvanceSI();
}

public override void Scas() {
Alu.Sub16(State.AX, Memory.GetUint16(MemoryAddressEsDi));
Alu.Sub16(State.AX, Memory.UInt16[MemoryAddressEsDi]);
AdvanceDI();
}

public override void Ins() {
ushort port = State.DX;
ushort value = Cpu.In16(port);
Memory.SetUint16(MemoryAddressEsDi, value);
Memory.UInt16[MemoryAddressEsDi] = value;
AdvanceDI();
}

public override void Outs() {
ushort port = State.DX;
ushort value = Memory.GetUint16(MemoryAddressOverridableDsSi);
ushort value = Memory.UInt16[MemoryAddressOverridableDsSi];
Cpu.Out16(port, value);
AdvanceSI();
}
Expand Down Expand Up @@ -428,12 +428,12 @@ public override void MovRegImm(int regIndex) {

public override void MovAccMoffs() {
// MOV AX moffs16
State.AX = Memory.GetUint16(DsNextUint16Address);
State.AX = Memory.UInt16[DsNextUint16Address];
}

public override void MovMoffsAcc() {
// MOV moffs16 AX
Memory.SetUint16(DsNextUint16Address, State.AX);
Memory.UInt16[DsNextUint16Address] = State.AX;
}

public override void MovRmImm() {
Expand Down Expand Up @@ -495,8 +495,8 @@ public override void Popf() {

protected override ushort DoLxsAndReturnSegmentValue() {
uint memoryAddress = ReadLxsMemoryAddress();
ModRM.R16 = Memory.GetUint16(memoryAddress);
return Memory.GetUint16(memoryAddress + 2);
ModRM.R16 = Memory.UInt16[memoryAddress];
return Memory.UInt16[memoryAddress + 2];
}

public override void InImm8() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private void Grp5RmCallFar() {
return;
}

ushort ip = Memory.GetUint16(ipAddress.Value);
ushort cs = Memory.GetUint16(ipAddress.Value + 2);
ushort ip = Memory.UInt16[ipAddress.Value];
ushort cs = Memory.UInt16[ipAddress.Value + 2];
Cpu.FarCallWithReturnIpNextInstruction(cs, ip);
}

Expand All @@ -126,8 +126,8 @@ private void Grp5RmJumpFar() {
return;
}

ushort ip = Memory.GetUint16(ipAddress.Value);
ushort cs = Memory.GetUint16(ipAddress.Value + 2);
ushort ip = Memory.UInt16[ipAddress.Value];
ushort cs = Memory.UInt16[ipAddress.Value + 2];
Cpu.JumpFar(cs, ip);
}

Expand Down
26 changes: 13 additions & 13 deletions src/Spice86.Core/Emulator/CPU/InstructionsImpl/Instructions32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ protected override void AdvanceDI() {
}

public override void Movs() {
uint value = Memory.GetUint32(MemoryAddressOverridableDsSi);
Memory.SetUint32(MemoryAddressEsDi, value);
uint value = Memory.UInt32[MemoryAddressOverridableDsSi];
Memory.UInt32[MemoryAddressEsDi] = value;
AdvanceSIDI();
}

public override void Cmps() {
uint value = Memory.GetUint32(MemoryAddressOverridableDsSi);
Alu.Sub32(value, Memory.GetUint32(MemoryAddressEsDi));
uint value = Memory.UInt32[MemoryAddressOverridableDsSi];
Alu.Sub32(value, Memory.UInt32[MemoryAddressEsDi]);
AdvanceSIDI();
}

Expand All @@ -256,30 +256,30 @@ public override void TestAccImm() {
}

public override void Stos() {
Memory.SetUint32(MemoryAddressEsDi, State.EAX);
Memory.UInt32[MemoryAddressEsDi] = State.EAX;
AdvanceDI();
}

public override void Lods() {
State.EAX = Memory.GetUint32(MemoryAddressOverridableDsSi);
State.EAX = Memory.UInt32[MemoryAddressOverridableDsSi];
AdvanceSI();
}

public override void Scas() {
Alu.Sub32(State.EAX, Memory.GetUint32(MemoryAddressEsDi));
Alu.Sub32(State.EAX, Memory.UInt32[MemoryAddressEsDi]);
AdvanceDI();
}

public override void Ins() {
ushort port = State.DX;
uint value = Cpu.In32(port);
Memory.SetUint32(MemoryAddressEsDi, value);
Memory.UInt32[MemoryAddressEsDi] = value;
AdvanceDI();
}

public override void Outs() {
ushort port = State.DX;
uint value = Memory.GetUint32(MemoryAddressOverridableDsSi);
uint value = Memory.UInt32[MemoryAddressOverridableDsSi];
Cpu.Out32(port, value);
AdvanceSI();
}
Expand Down Expand Up @@ -424,12 +424,12 @@ public override void MovRegImm(int regIndex) {

public override void MovAccMoffs() {
// MOV EAX moffs32
State.EAX = Memory.GetUint32(DsNextUint16Address);
State.EAX = Memory.UInt32[DsNextUint16Address];
}

public override void MovMoffsAcc() {
// MOV moffs32 EAX
Memory.SetUint32(DsNextUint16Address, State.EAX);
Memory.UInt32[DsNextUint16Address] = State.EAX;
}

public override void MovRmImm() {
Expand Down Expand Up @@ -481,8 +481,8 @@ public override void Popf() {

protected override ushort DoLxsAndReturnSegmentValue() {
uint memoryAddress = ReadLxsMemoryAddress();
ModRM.R32 = Memory.GetUint32(memoryAddress);
return Memory.GetUint16(memoryAddress + 4);
ModRM.R32 = Memory.UInt32[memoryAddress];
return Memory.UInt16[memoryAddress + 4];
}

public override void InImm8() {
Expand Down
24 changes: 12 additions & 12 deletions src/Spice86.Core/Emulator/CPU/InstructionsImpl/Instructions8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ protected override void AdvanceDI() {
}

public override void Movs() {
byte value = Memory.GetUint8(MemoryAddressOverridableDsSi);
Memory.SetUint8(MemoryAddressEsDi, value);
byte value = Memory.UInt8[MemoryAddressOverridableDsSi];
Memory.UInt8[MemoryAddressEsDi] = value;
AdvanceSIDI();
}

public override void Cmps() {
byte value = Memory.GetUint8(MemoryAddressOverridableDsSi);
Alu.Sub8(value, Memory.GetUint8(MemoryAddressEsDi));
byte value = Memory.UInt8[MemoryAddressOverridableDsSi];
Alu.Sub8(value, Memory.UInt8[MemoryAddressEsDi]);
AdvanceSIDI();
}

Expand All @@ -176,30 +176,30 @@ public override void TestAccImm() {
}

public override void Stos() {
Memory.SetUint8(MemoryAddressEsDi, State.AL);
Memory.UInt8[MemoryAddressEsDi] = State.AL;
AdvanceDI();
}

public override void Lods() {
State.AL = Memory.GetUint8(MemoryAddressOverridableDsSi);
State.AL = Memory.UInt8[MemoryAddressOverridableDsSi];
AdvanceSI();
}

public override void Scas() {
Alu.Sub8(State.AL, Memory.GetUint8(MemoryAddressEsDi));
Alu.Sub8(State.AL, Memory.UInt8[MemoryAddressEsDi]);
AdvanceDI();
}

public override void Ins() {
ushort port = State.DX;
byte value = Cpu.In8(port);
Memory.SetUint8(MemoryAddressEsDi, value);
Memory.UInt8[MemoryAddressEsDi] = value;
AdvanceDI();
}

public override void Outs() {
ushort port = State.DX;
byte value = Memory.GetUint8(MemoryAddressOverridableDsSi);
byte value = Memory.UInt8[MemoryAddressOverridableDsSi];
Cpu.Out8(port, value);
AdvanceSI();
}
Expand Down Expand Up @@ -349,12 +349,12 @@ public override void MovRegImm(int regIndex) {

public override void MovAccMoffs() {
// MOV AL moffs8
State.AL = Memory.GetUint8(DsNextUint16Address);
State.AL = Memory.UInt8[DsNextUint16Address];
}

public override void MovMoffsAcc() {
// MOV moffs8 AL
Memory.SetUint8(DsNextUint16Address, State.AL);
Memory.UInt8[DsNextUint16Address] = State.AL;
}

public override void MovRmImm() {
Expand Down Expand Up @@ -391,7 +391,7 @@ public void Salc() {
public void Xlat() {
// XLAT
uint address = ModRM.GetAddress(SegmentRegisters.DsIndex, State.BX) + State.AL;
State.AL = Memory.GetUint8(address);
State.AL = Memory.UInt8[address];
}


Expand Down
12 changes: 6 additions & 6 deletions src/Spice86.Core/Emulator/CPU/ModRM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ public uint GetRm32() {
if (MemoryAddress == null) {
return _state.Registers.GetRegister32(_registerMemoryIndex);
}
return _memory.GetUint32((uint)MemoryAddress);
return _memory.UInt32[(uint)MemoryAddress];
}

public ushort GetRm16() {
if (MemoryAddress == null) {
return _state.Registers.GetRegister16(_registerMemoryIndex);
}
return _memory.GetUint16((uint)MemoryAddress);
return _memory.UInt16[(uint)MemoryAddress];
}

public byte GetRm8() {
if (MemoryAddress == null) {
return _state.Registers.GetRegisterFromHighLowIndex8(_registerMemoryIndex);
}
return _memory.GetUint8((uint)MemoryAddress);
return _memory.UInt8[(uint)MemoryAddress];
}

public ushort SegmentRegister { get => _state.SegmentRegisters.GetRegister16(RegisterIndex); set => _state.SegmentRegisters.SetRegister16(RegisterIndex, value); }
Expand Down Expand Up @@ -110,23 +110,23 @@ public void SetRm32(uint value) {
if (MemoryAddress == null) {
_state.Registers.SetRegister32(_registerMemoryIndex, value);
} else {
_memory.SetUint32((uint)MemoryAddress, value);
_memory.UInt32[(uint)MemoryAddress] = value;
}
}

public void SetRm16(ushort value) {
if (MemoryAddress == null) {
_state.Registers.SetRegister16(_registerMemoryIndex, value);
} else {
_memory.SetUint16((uint)MemoryAddress, value);
_memory.UInt16[(uint)MemoryAddress] = value;
}
}

public void SetRm8(byte value) {
if (MemoryAddress == null) {
_state.Registers.SetRegisterFromHighLowIndex8(_registerMemoryIndex, value);
} else {
_memory.SetUint8((uint)MemoryAddress, value);
_memory.UInt8[(uint)MemoryAddress] = value;
}
}

Expand Down
Loading

0 comments on commit 5fce324

Please sign in to comment.