Loop CPU execution

This commit is contained in:
Felipe Lima 2015-06-13 03:02:44 -07:00
parent 25cfd99dd2
commit 8f83e56e67

View File

@ -85,32 +85,34 @@ class CPU(private val memory: Memory) {
) )
fun execute() { fun execute() {
isRunning = true
while (true) {
setRandomByte() setRandomByte()
executeNextInstruction() executeNextInstruction()
if (PC == 0 || !isRunning) { if (PC == 0 || !isRunning) {
stop() break
Log.i(TAG, "Program end at PC=$" + (PC - 1).toHexString())
} }
} }
private fun stop() {
isRunning = false isRunning = false
Log.i(TAG, "Program end at PC=$" + (PC - 1).toHexString() + ", A=$" + A.toHexString() +
", X=$" + X.toHexString() + ", Y=$" + Y.toHexString())
} }
private fun executeNextInstruction() { private fun executeNextInstruction() {
val instruction = Integer.valueOf(popByte().toInt().toString(), 16) val instruction = popByte()
val target = instructionList.get(instruction) val target = instructionList.get(instruction)
if (target != null) { if (target != null) {
val function = target.method val function = target.method
target.operation.function() target.operation.function()
} else { } else {
Log.e(TAG, "Address $" + PC.toHexString() + " - unknown opcode " + instruction.toHexString()) Log.e(TAG, "Address $" + PC.toHexString() + " - unknown opcode " + instruction.toHexString())
isRunning = false
} }
} }
fun popByte(): Int { fun popByte(): Int {
return memory.get((PC++).and(0xff)); return memory.get(PC++).and(0xff);
} }
private fun setRandomByte() { private fun setRandomByte() {