From 27bca0afc1461804b89a21f56f1171d13ae42a44 Mon Sep 17 00:00:00 2001 From: Felipe Lima Date: Tue, 30 Jun 2015 00:17:31 -0700 Subject: [PATCH] Moves ADC specific logic out of CPU --- app/src/main/kotlin/android/emu6502/CPU.kt | 44 ------------------ .../android/emu6502/instructions/impl/ADC.kt | 46 ++++++++++++++++++- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/app/src/main/kotlin/android/emu6502/CPU.kt b/app/src/main/kotlin/android/emu6502/CPU.kt index 85f90b7..a5d657a 100644 --- a/app/src/main/kotlin/android/emu6502/CPU.kt +++ b/app/src/main/kotlin/android/emu6502/CPU.kt @@ -192,50 +192,6 @@ class CPU(val memory: Memory) : Display.Callbacks { return popByte() + popByte().shl(8) } - fun testADC(value: Int) { - var tmp: Int - if (A.xor(value).and(0x80) != 0) { - CLV() - } else { - setOverflow() - } - - if (decimalMode()) { - tmp = A.and(0xf) + value.and(0xf) + P.and(1) - if (tmp >= 10) { - tmp = 0x10.or((tmp + 6).and(0xf)) - } - tmp += A.and(0xf0) + value.and(0xf0) - if (tmp >= 160) { - SEC() - if (overflow() && tmp >= 0x180) { - CLV() - } - tmp += 0x60 - } else { - CLC() - if (overflow() && tmp < 0x80) { - CLV() - } - } - } else { - tmp = A + value + P.and(1) - if (tmp >= 0x100) { - SEC() - if (overflow() && tmp >= 0x180) { - CLV() - } - } else { - CLC() - if (overflow() && tmp < 0x80) { - CLV() - } - } - } - A = tmp.and(0xff) - setSZFlagsForRegA() - } - fun overflow(): Boolean { return P.and(0x40) != 0 } diff --git a/app/src/main/kotlin/android/emu6502/instructions/impl/ADC.kt b/app/src/main/kotlin/android/emu6502/instructions/impl/ADC.kt index 041ed8f..d48934a 100644 --- a/app/src/main/kotlin/android/emu6502/instructions/impl/ADC.kt +++ b/app/src/main/kotlin/android/emu6502/instructions/impl/ADC.kt @@ -7,6 +7,50 @@ import android.emu6502.instructions.Instruction /** ADd with Carry */ class ADC(private val cpu: CPU) : BaseInstruction(Instruction.ADC, cpu.instructionList) { override fun immediate() { - cpu.testADC(cpu.popByte()) + testADC(cpu.popByte()) + } + + private fun testADC(value: Int) { + var tmp: Int + if (cpu.A.xor(value).and(0x80) != 0) { + cpu.CLV() + } else { + cpu.setOverflow() + } + + if (cpu.decimalMode()) { + tmp = cpu.A.and(0xf) + value.and(0xf) + cpu.P.and(1) + if (tmp >= 10) { + tmp = 0x10.or((tmp + 6).and(0xf)) + } + tmp += cpu.A.and(0xf0) + value.and(0xf0) + if (tmp >= 160) { + cpu.SEC() + if (cpu.overflow() && tmp >= 0x180) { + cpu.CLV() + } + tmp += 0x60 + } else { + cpu.CLC() + if (cpu.overflow() && tmp < 0x80) { + cpu.CLV() + } + } + } else { + tmp = cpu.A + value + cpu.P.and(1) + if (tmp >= 0x100) { + cpu.SEC() + if (cpu.overflow() && tmp >= 0x180) { + cpu.CLV() + } + } else { + cpu.CLC() + if (cpu.overflow() && tmp < 0x80) { + cpu.CLV() + } + } + } + cpu.A = tmp.and(0xff) + cpu.setSZFlagsForRegA() } } \ No newline at end of file