Moves ADC specific logic out of CPU

This commit is contained in:
Felipe Lima 2015-06-30 00:17:31 -07:00
parent c2be67cd38
commit 27bca0afc1
2 changed files with 45 additions and 45 deletions

View File

@ -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
}

View File

@ -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()
}
}