Use `ref` to pass PC by reference into ReadNextInstruction which then doesn't need its own Position

This commit is contained in:
Lee Fogg 2020-04-18 20:47:44 +01:00
parent 313ff55adf
commit 2b3330bbee
3 changed files with 14 additions and 21 deletions

View File

@ -27,6 +27,7 @@ namespace Emux.GameBoy.Cpu
private ulong _ticks;
public bool IsBroken = true;
public bool Halted = false;
private readonly Z80Instruction _readInstruction = new Z80Instruction(0, Z80OpCodes.SingleByteOpCodes[0], new byte[0]);
public GameBoyCpu(GameBoy device, IClock clock)
{
@ -176,10 +177,8 @@ namespace Emux.GameBoy.Cpu
private Z80Instruction ReadNextInstruction()
{
_disassembler.Position = Registers.PC;
var instruction = _disassembler.ReadNextInstruction();
Registers.PC = _disassembler.Position;
return instruction;
_disassembler.ReadInstruction(ref Registers.PC, _readInstruction);
return _readInstruction;
}
internal void Push(ushort value)

View File

@ -8,36 +8,29 @@ namespace Emux.GameBoy.Cpu
public class Z80Disassembler
{
private readonly GameBoyMemory _memory;
private readonly Z80Instruction _readInstruction = new Z80Instruction(0, Z80OpCodes.SingleByteOpCodes[0], new byte[0]);
public Z80Disassembler(GameBoyMemory memory)
{
_memory = memory;
}
/// <summary>
/// Gets or sets the position of the disassembler to read the next instruction from.
/// </summary>
public ushort Position { get; set; }
/// <summary>
/// Reads the next instruction from memory.
/// </summary>
/// <returns>The disassembled instruction.</returns>
public Z80Instruction ReadNextInstruction()
public void ReadInstruction(ref ushort location, Z80Instruction outInstruction)
{
ushort offset = Position;
byte code = _memory.ReadByte(Position++);
ushort offset = location;
byte code = _memory.ReadByte(location++);
var opcode = code != Z80OpCodes.ExtendedTableOpcode
? Z80OpCodes.SingleByteOpCodes[code]
: Z80OpCodes.PrefixedOpCodes[_memory.ReadByte(Position++)];
: Z80OpCodes.PrefixedOpCodes[_memory.ReadByte(location++)];
var operands = _memory.ReadBytes(Position, opcode.OperandLength);
Position += (ushort)operands.Length;
var operands = _memory.ReadBytes(location, opcode.OperandLength);
location += (ushort)operands.Length;
_readInstruction.Set(offset, opcode, operands);
return _readInstruction;
outInstruction.Set(offset, opcode, operands);
}
}
}

View File

@ -215,10 +215,11 @@ namespace Emux.Gui
;
DisassemblyView.Items.Clear();
var disassembler = new Z80Disassembler(_currentDevice.Memory);
disassembler.Position = _currentDevice.Cpu.Registers.PC;
for (int i = 0; i < 30 && disassembler.Position < 0xFFFF; i ++)
var position = _currentDevice.Cpu.Registers.PC;
for (int i = 0; i < 30 && position < 0xFFFF; i ++)
{
var instruction = disassembler.ReadNextInstruction();
var instruction = new Z80Instruction(0, Z80OpCodes.SingleByteOpCodes[0], new byte[0]);
disassembler.ReadInstruction(ref position, instruction);
DisassemblyView.Items.Add(new InstructionItem(_currentDevice, instruction));
}