From 8f83e56e67e2832e6f609c68714b7ea151e115cb Mon Sep 17 00:00:00 2001 From: Felipe Lima Date: Sat, 13 Jun 2015 03:02:44 -0700 Subject: [PATCH] Loop CPU execution --- app/src/main/kotlin/android/emu6502/CPU.kt | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/android/emu6502/CPU.kt b/app/src/main/kotlin/android/emu6502/CPU.kt index 9dcf8e0..be5aa88 100644 --- a/app/src/main/kotlin/android/emu6502/CPU.kt +++ b/app/src/main/kotlin/android/emu6502/CPU.kt @@ -85,32 +85,34 @@ class CPU(private val memory: Memory) { ) fun execute() { - setRandomByte() - executeNextInstruction() + isRunning = true + while (true) { + setRandomByte() + executeNextInstruction() - if (PC == 0 || !isRunning) { - stop() - Log.i(TAG, "Program end at PC=$" + (PC - 1).toHexString()) + if (PC == 0 || !isRunning) { + break + } } - } - - private fun stop() { isRunning = false + Log.i(TAG, "Program end at PC=$" + (PC - 1).toHexString() + ", A=$" + A.toHexString() + + ", X=$" + X.toHexString() + ", Y=$" + Y.toHexString()) } private fun executeNextInstruction() { - val instruction = Integer.valueOf(popByte().toInt().toString(), 16) + val instruction = popByte() val target = instructionList.get(instruction) if (target != null) { val function = target.method target.operation.function() } else { Log.e(TAG, "Address $" + PC.toHexString() + " - unknown opcode " + instruction.toHexString()) + isRunning = false } } fun popByte(): Int { - return memory.get((PC++).and(0xff)); + return memory.get(PC++).and(0xff); } private fun setRandomByte() {