Fixes tests + minor cleanup

This commit is contained in:
Felipe Lima 2015-06-30 00:35:06 -07:00
parent 27bca0afc1
commit ab597fb9d2
37 changed files with 90 additions and 73 deletions

View File

@ -14,7 +14,7 @@ import java.util.concurrent.CountDownLatch
class CPU(val memory: Memory) : Display.Callbacks {
private val bgHandlerThread = HandlerThread("Screencast Thread")
private val bgHandler: Handler
private var lock: CountDownLatch? = null
private var executionLock: CountDownLatch? = null
init {
bgHandlerThread.start()
@ -36,7 +36,7 @@ class CPU(val memory: Memory) : Display.Callbacks {
private var debug = false
private var monitoring = false
private var TAG = "CPU"
val instructionList: HashMap<Int, InstructionTarget> = HashMap()
private val instructionList: HashMap<Int, InstructionTarget> = HashMap()
private val operationList: HashMap<Instruction, BaseInstruction> = hashMapOf(
Pair(Instruction.ADC, ADC(this)),
Pair(Instruction.AND, AND(this)),
@ -72,30 +72,44 @@ class CPU(val memory: Memory) : Display.Callbacks {
Pair(Instruction.SBC, SBC(this)),
Pair(Instruction.BCC, BCC(this)),
Pair(Instruction.DEC, DEC(this))
// Pair(Instruction.BMI, BMI(this)),
// Pair(Instruction.BVC, BVC(this)),
// Pair(Instruction.BVS, BVS(this)),
// Pair(Instruction.CPY, CPY(this)),
// Pair(Instruction.EOR, EOR(this)),
// Pair(Instruction.CLI, CLI(this)),
// Pair(Instruction.CLV, CLV(this)),
// Pair(Instruction.CLD, CLD(this)),
// Pair(Instruction.SED, SED(this)),
// Pair(Instruction.TAY, TAY(this)),
// Pair(Instruction.TYA, TYA(this)),
// Pair(Instruction.INY, INY(this)),
// Pair(Instruction.ROR, ROR(this)),
// Pair(Instruction.ROL, ROL(this)),
// Pair(Instruction.RTI, RTI(this)),
// Pair(Instruction.TXS, TXS(this)),
// Pair(Instruction.TSX, TSX(this)),
// Pair(Instruction.PHA, PHA(this)),
// Pair(Instruction.PLA, PLA(this)),
// Pair(Instruction.PHP, PHP(this)),
// Pair(Instruction.PLP, PLP(this)),
// Pair(Instruction.STY, STY(this))
//Pair(Instruction.BMI, BMI(this)),
//Pair(Instruction.BVC, BVC(this)),
//Pair(Instruction.BVS, BVS(this)),
//Pair(Instruction.CPY, CPY(this)),
//Pair(Instruction.EOR, EOR(this)),
//Pair(Instruction.CLI, CLI(this)),
//Pair(Instruction.CLV, CLV(this)),
//Pair(Instruction.CLD, CLD(this)),
//Pair(Instruction.SED, SED(this)),
//Pair(Instruction.TAY, TAY(this)),
//Pair(Instruction.TYA, TYA(this)),
//Pair(Instruction.INY, INY(this)),
//Pair(Instruction.ROR, ROR(this)),
//Pair(Instruction.ROL, ROL(this)),
//Pair(Instruction.RTI, RTI(this)),
//Pair(Instruction.TXS, TXS(this)),
//Pair(Instruction.TSX, TSX(this)),
//Pair(Instruction.PHA, PHA(this)),
//Pair(Instruction.PLA, PLA(this)),
//Pair(Instruction.PHP, PHP(this)),
//Pair(Instruction.PLP, PLP(this)),
//Pair(Instruction.STY, STY(this))
)
// for testing only
fun testRun() {
isRunning = true
while (true) {
setRandomByte()
executeNextInstruction()
if (PC == 0 || !isRunning) {
break
}
}
stop()
}
fun run() {
isRunning = true
bgHandler.post { innerRun() }
@ -155,7 +169,11 @@ class CPU(val memory: Memory) : Display.Callbacks {
P = 0x30
}
fun popByte(): Int {
fun addInstruction(opcode: Int, target: InstructionTarget) {
instructionList.put(opcode, target)
}
fun popByte(): Int {
return memory.get(PC++).and(0xff)
}
@ -297,11 +315,11 @@ class CPU(val memory: Memory) : Display.Callbacks {
}
override fun onUpdate() {
lock = CountDownLatch(1)
lock?.await()
executionLock = CountDownLatch(1)
executionLock?.await()
}
override fun onDraw() {
lock?.countDown()
executionLock?.countDown()
}
}

View File

@ -1,9 +1,8 @@
package android.emu6502.instructions
import java.util.HashMap
import android.emu6502.CPU
open class BaseInstruction(val instruction: Instruction,
private val instructionList: HashMap<Int, InstructionTarget>) {
open class BaseInstruction(val instruction: Instruction, private val cpu: CPU) {
init {
val opcodes: IntArray = Opcodes.MAP[instruction] as IntArray
@ -12,7 +11,7 @@ open class BaseInstruction(val instruction: Instruction,
opcodes.forEachIndexed { i, opcode ->
if (opcode != 0xff) {
instructionList.put(opcodes[i], InstructionTarget(this, methods[i]))
cpu.addInstruction(opcodes[i], InstructionTarget(this, methods[i]))
}
}
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** ADd with Carry */
class ADC(private val cpu: CPU) : BaseInstruction(Instruction.ADC, cpu.instructionList) {
class ADC(private val cpu: CPU) : BaseInstruction(Instruction.ADC, cpu) {
override fun immediate() {
testADC(cpu.popByte())
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** bitwise AND with accumulator */
class AND(private val cpu: CPU) : BaseInstruction(Instruction.AND, cpu.instructionList) {
class AND(private val cpu: CPU) : BaseInstruction(Instruction.AND, cpu) {
override fun immediate() {
cpu.A = cpu.A.and(cpu.popByte())
cpu.setSZFlagsForRegA()

View File

@ -5,5 +5,5 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Arithmetic Shift Left */
class ASL(private val cpu: CPU) : BaseInstruction(Instruction.ASL, cpu.instructionList) {
class ASL(private val cpu: CPU) : BaseInstruction(Instruction.ASL, cpu) {
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Branch on Carry Clear */
final class BCC(private val cpu: CPU) : BaseInstruction(Instruction.BCC, cpu.instructionList) {
final class BCC(private val cpu: CPU) : BaseInstruction(Instruction.BCC, cpu) {
override fun branch() {
val offset = cpu.popByte()
if (!cpu.carry()) {

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Branch on Carry Set */
final class BCS(private val cpu: CPU) : BaseInstruction(Instruction.BCS, cpu.instructionList) {
final class BCS(private val cpu: CPU) : BaseInstruction(Instruction.BCS, cpu) {
override fun branch() {
val offset = cpu.popByte()
if (cpu.carry()) {

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Branch on EQual */
class BEQ(private val cpu: CPU) : BaseInstruction(Instruction.BEQ, cpu.instructionList) {
class BEQ(private val cpu: CPU) : BaseInstruction(Instruction.BEQ, cpu) {
override fun branch() {
val offset = cpu.popByte()
if (cpu.zero()) {

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** test BITs */
class BIT(private val cpu: CPU) : BaseInstruction(Instruction.BIT, cpu.instructionList) {
class BIT(private val cpu: CPU) : BaseInstruction(Instruction.BIT, cpu) {
override fun zeroPage() {
val value = cpu.memory.get(cpu.popByte())
BIT(value)

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Branch on Not Equal */
final class BNE(private val cpu: CPU) : BaseInstruction(Instruction.BNE, cpu.instructionList) {
final class BNE(private val cpu: CPU) : BaseInstruction(Instruction.BNE, cpu) {
override fun branch() {
val offset = cpu.popByte()
if (!cpu.zero()) {

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Branch on PLus */
final class BPL(private val cpu: CPU) : BaseInstruction(Instruction.BPL, cpu.instructionList) {
final class BPL(private val cpu: CPU) : BaseInstruction(Instruction.BPL, cpu) {
override fun branch() {
val offset = cpu.popByte()
if (!cpu.negative()) {

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** BRreaK */
final class BRK(private val cpu: CPU) : BaseInstruction(Instruction.BRK, cpu.instructionList) {
final class BRK(private val cpu: CPU) : BaseInstruction(Instruction.BRK, cpu) {
override fun single() {
cpu.stop()
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** CLear Carry */
final class CLC(private val cpu: CPU) : BaseInstruction(Instruction.CLC, cpu.instructionList) {
final class CLC(private val cpu: CPU) : BaseInstruction(Instruction.CLC, cpu) {
override fun single() {
cpu.CLC()
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** CoMPare accumulator */
final class CMP(private val cpu: CPU) : BaseInstruction(Instruction.CMP, cpu.instructionList) {
final class CMP(private val cpu: CPU) : BaseInstruction(Instruction.CMP, cpu) {
override fun immediate() {
cpu.doCompare(cpu.A, cpu.popByte())
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** ComPare X register */
class CPX(private val cpu: CPU) : BaseInstruction(Instruction.CPX, cpu.instructionList) {
class CPX(private val cpu: CPU) : BaseInstruction(Instruction.CPX, cpu) {
override fun immediate() {
val value = cpu.popByte()
cpu.doCompare(cpu.X, value)

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** DECrement memory */
class DEC(private val cpu: CPU) : BaseInstruction(Instruction.DEC, cpu.instructionList) {
class DEC(private val cpu: CPU) : BaseInstruction(Instruction.DEC, cpu) {
override fun zeroPage() {
DEC(cpu.popByte())
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** DEcrement X */
class DEX(private val cpu: CPU) : BaseInstruction(Instruction.DEX, cpu.instructionList) {
class DEX(private val cpu: CPU) : BaseInstruction(Instruction.DEX, cpu) {
override fun single() {
cpu.X = (cpu.X - 1).and(0xff)
cpu.setSZflagsForRegX()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** DEcrement Y */
class DEY(private val cpu: CPU) : BaseInstruction(Instruction.DEY, cpu.instructionList) {
class DEY(private val cpu: CPU) : BaseInstruction(Instruction.DEY, cpu) {
override fun single() {
cpu.Y = (cpu.Y - 1).and(0xff)
cpu.setSZflagsForRegY()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** INCrement memory */
final class INC(private val cpu: CPU) : BaseInstruction(Instruction.INC, cpu.instructionList) {
final class INC(private val cpu: CPU) : BaseInstruction(Instruction.INC, cpu) {
override fun zeroPage() {
inc(cpu.popByte())
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** INcrement X */
class INX(private val cpu: CPU) : BaseInstruction(Instruction.INX, cpu.instructionList) {
class INX(private val cpu: CPU) : BaseInstruction(Instruction.INX, cpu) {
override fun single() {
cpu.X = (cpu.X + 1).and(0xff)
cpu.setSZflagsForRegX()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** JuMP */
class JMP(private val cpu: CPU) : BaseInstruction(Instruction.JMP, cpu.instructionList) {
class JMP(private val cpu: CPU) : BaseInstruction(Instruction.JMP, cpu) {
override fun absolute() {
cpu.PC = cpu.popWord()
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Jump to SubRoutine */
class JSR(private val cpu: CPU) : BaseInstruction(Instruction.JSR, cpu.instructionList) {
class JSR(private val cpu: CPU) : BaseInstruction(Instruction.JSR, cpu) {
override fun absolute() {
val addr = cpu.popWord()
val currAddr = cpu.PC - 1

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** LoaD Accumulator */
class LDA(private val cpu: CPU) : BaseInstruction(Instruction.LDA, cpu.instructionList) {
class LDA(private val cpu: CPU) : BaseInstruction(Instruction.LDA, cpu) {
override fun immediate() {
cpu.A = cpu.popByte()
cpu.setSZFlagsForRegA()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** LoaD X register */
class LDX(private val cpu: CPU) : BaseInstruction(Instruction.LDX, cpu.instructionList) {
class LDX(private val cpu: CPU) : BaseInstruction(Instruction.LDX, cpu) {
override fun immediate() {
cpu.X = cpu.popByte()
cpu.setSZflagsForRegX()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** LoaD Y register */
class LDY(private val cpu: CPU) : BaseInstruction(Instruction.LDY, cpu.instructionList) {
class LDY(private val cpu: CPU) : BaseInstruction(Instruction.LDY, cpu) {
override fun immediate() {
cpu.Y = cpu.popByte()
cpu.setSZflagsForRegY()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Logical Shift Right */
final class LSR(private val cpu: CPU) : BaseInstruction(Instruction.LSR, cpu.instructionList) {
final class LSR(private val cpu: CPU) : BaseInstruction(Instruction.LSR, cpu) {
override fun single() {
cpu.setCarryFlagFromBit0(cpu.A)
cpu.A = cpu.A.shr(1)

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** No OPeration */
class NOP(private val cpu: CPU) : BaseInstruction(Instruction.NOP, cpu.instructionList) {
class NOP(private val cpu: CPU) : BaseInstruction(Instruction.NOP, cpu) {
override fun single() {
}
}

View File

@ -5,5 +5,5 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** bitwise OR with Accumulator */
class ORA(cpu: CPU) : BaseInstruction(Instruction.ORA, cpu.instructionList) {
class ORA(cpu: CPU) : BaseInstruction(Instruction.ORA, cpu) {
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** ReTurn from Subroutine */
class RTS(private val cpu: CPU) : BaseInstruction(Instruction.RTS, cpu.instructionList) {
class RTS(private val cpu: CPU) : BaseInstruction(Instruction.RTS, cpu) {
override fun single() {
cpu.PC = cpu.stackPop().or(cpu.stackPop().shl(8)) + 1
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** SuBtract with Carry */
class SBC(private val cpu: CPU) : BaseInstruction(Instruction.SBC, cpu.instructionList) {
class SBC(private val cpu: CPU) : BaseInstruction(Instruction.SBC, cpu) {
override fun immediate() {
testSBC(cpu.popByte())
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** SEt Carry */
class SEC(private val cpu: CPU) : BaseInstruction(Instruction.SEC, cpu.instructionList) {
class SEC(private val cpu: CPU) : BaseInstruction(Instruction.SEC, cpu) {
override fun single() {
cpu.carry()
}

View File

@ -5,6 +5,6 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** SEt Interrupt */
class SEI(private val cpu: CPU) : BaseInstruction(Instruction.SEI, cpu.instructionList) {
class SEI(private val cpu: CPU) : BaseInstruction(Instruction.SEI, cpu) {
}

View File

@ -7,7 +7,7 @@ import android.emu6502.instructions.Instruction
/** STore Accumulator */
class STA(private val memory: Memory, private val cpu: CPU)
: BaseInstruction(Instruction.STA, cpu.instructionList) {
: BaseInstruction(Instruction.STA, cpu) {
override fun absolute() {
memory.storeByte(cpu.popWord(), cpu.A)

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** STore X register */
class STX(private val cpu: CPU) : BaseInstruction(Instruction.STX, cpu.instructionList) {
class STX(private val cpu: CPU) : BaseInstruction(Instruction.STX, cpu) {
override fun zeroPage() {
cpu.memory.storeByte(cpu.popByte(), cpu.X)
}

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Transfer A to X */
class TAX(private val cpu: CPU) : BaseInstruction(Instruction.TAX, cpu.instructionList) {
class TAX(private val cpu: CPU) : BaseInstruction(Instruction.TAX, cpu) {
override fun single() {
cpu.X = cpu.A.and(0xFF)
cpu.setSZflagsForRegX()

View File

@ -5,7 +5,7 @@ import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Transfer X to A */
class TXA(private val cpu: CPU) : BaseInstruction(Instruction.TXA, cpu.instructionList) {
class TXA(private val cpu: CPU) : BaseInstruction(Instruction.TXA, cpu) {
override fun single() {
cpu.A = cpu.X.and(0xff)
cpu.setSZFlagsForRegA()

View File

@ -34,7 +34,7 @@ public class CPUTest {
"LDA #$08",
"STA $0202");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x08));
assertThat(cpu.getX(), equalTo(0x00));
assertThat(cpu.getY(), equalTo(0x00));
@ -51,7 +51,7 @@ public class CPUTest {
"ADC #$c4 ;Add the hex value $c4 to the A register",
"BRK ;Break - we're done");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x84));
assertThat(cpu.getX(), equalTo(0xC1));
assertThat(cpu.getY(), equalTo(0x00));
@ -72,7 +72,7 @@ public class CPUTest {
"STX $0201",
"BRK");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x03));
assertThat(cpu.getY(), equalTo(0x00));
@ -92,7 +92,7 @@ public class CPUTest {
"there:",
"STA $0200");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x03));
assertThat(cpu.getX(), equalTo(0x00));
assertThat(cpu.getY(), equalTo(0x00));
@ -120,7 +120,7 @@ public class CPUTest {
"end:",
"BRK");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x05));
assertThat(cpu.getY(), equalTo(0x00));
@ -134,7 +134,7 @@ public class CPUTest {
"define a_dozen $0c ; a constant",
"LDX #a_dozen ; equivalent to \"LDX #$0c\"");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x0C));
assertThat(cpu.getY(), equalTo(0x00));
@ -372,7 +372,7 @@ public class CPUTest {
" rts",
"gameOver:", "\n");
assembler.assembleCode(lines);
cpu.run();
cpu.testRun();
assertThat(cpu.getA(), equalTo(0x1f));
assertThat(cpu.getX(), equalTo(0xff));
assertThat(cpu.getY(), equalTo(0x00));