diff --git a/Emux.GameBoy/Cpu/GameBoyCpu.cs b/Emux.GameBoy/Cpu/GameBoyCpu.cs index 266b10d..412c504 100644 --- a/Emux.GameBoy/Cpu/GameBoyCpu.cs +++ b/Emux.GameBoy/Cpu/GameBoyCpu.cs @@ -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) diff --git a/Emux.GameBoy/Cpu/Z80Disassembler.cs b/Emux.GameBoy/Cpu/Z80Disassembler.cs index aaf436e..0bcf78c 100644 --- a/Emux.GameBoy/Cpu/Z80Disassembler.cs +++ b/Emux.GameBoy/Cpu/Z80Disassembler.cs @@ -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; } - /// - /// Gets or sets the position of the disassembler to read the next instruction from. - /// - public ushort Position { get; set; } - /// /// Reads the next instruction from memory. /// /// The disassembled instruction. - 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); } } } diff --git a/Emux/Gui/MainWindow.xaml.cs b/Emux/Gui/MainWindow.xaml.cs index 5895f3a..70a9504 100644 --- a/Emux/Gui/MainWindow.xaml.cs +++ b/Emux/Gui/MainWindow.xaml.cs @@ -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)); }