Adds instructions, display, instructiontarget, etc

This commit is contained in:
Felipe Lima 2015-06-11 23:26:47 -07:00
parent 54fa43a893
commit 053d7b267d
25 changed files with 292 additions and 146 deletions

View File

@ -14,7 +14,7 @@ class Assembler(private var labels: Labels,
private var codeAssembledOK = false
private var BOOTSTRAP_ADDRESS = 0x600
fun assembleCode(lines: Array<String>): Boolean {
fun assembleCode(lines: List<String>): Boolean {
lines.forEach { line ->
if (!assembleLine(line)) {
return false
@ -132,7 +132,7 @@ class Assembler(private var labels: Labels,
}
private fun checkAbsolute(param: String, opcode: Int): Boolean {
if (checkWordOperand("^([\\w\$]+)$")) {
if (checkWordOperand(param, opcode, "^([\\w\$]+)$")) {
return true
}
@ -196,40 +196,67 @@ class Assembler(private var labels: Labels,
}
private fun checkIndirectY(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkByteOperand(param, opcode, "^\\(([\\w\$]+)\\),Y$")
}
private fun checkIndirectX(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkByteOperand(param, opcode, "^\\(([\\w\$]+)\\),X$")
}
private fun checkIndirect(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkWordOperand(param, opcode, "^\\(([\\w\$]+)\\)$")
}
private fun checkAbsoluteY(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkWordOperand(param, opcode, "^([\\w\$]+),Y$") ||
checkLabel(param, opcode, "^\\w+,Y$".toRegex())
}
private fun checkAbsoluteX(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkWordOperand(param, opcode, "^([\\w\$]+),X$") ||
checkLabel(param, opcode, "^\\w+,X$".toRegex())
}
private fun checkZeroPageY(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkByteOperand(param, opcode, "^([\\w\$]+),Y")
}
private fun checkZeroPageX(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return checkByteOperand(param, opcode, "^([\\w\$]+),X")
}
private fun checkZeroPage(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
return innerCheckByteOperand(param, opcode)
}
private fun checkImmediate(param: String, opcode: Int): Boolean {
if (opcode == 0xff) {
return false
}
if (checkByteOperand(param, opcode, "^#([\\w\$]+)$")) {
return true
}

View File

@ -1,83 +1,87 @@
package android.emu6502
import android.emu6502.instructions.*
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
import android.emu6502.instructions.InstructionTarget
import android.emu6502.instructions.impl.*
import android.util.Log
import java.util.HashMap
import kotlin.reflect.KMemberFunction0
class CPU(private val memory: Memory) {
// Accumulator
private var A: Byte = 0
var A: Int = 0
// Registers
private var X: Byte = 0
private var Y: Byte = 0
var X: Int = 0
var Y: Int = 0
// Program counter
private var PC = 0x600
var PC: Int = 0x600
// Stack pointer
private var SP = 0xFF
private var flags: Byte = 0
var SP: Int = 0xFF
// Processor flags
var P: Int = 0
private var isRunning = false
private var debug = false
private var monitoring = false
private var TAG = "CPU"
private val instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>> = HashMap()
val instructionList: HashMap<Int, InstructionTarget> = HashMap()
private val operationList: HashMap<Instruction, BaseInstruction> = hashMapOf(
Pair(Instruction.ADC, ADC(instructionList)),
Pair(Instruction.AND, AND(instructionList)),
Pair(Instruction.ASL, ASL(instructionList)),
Pair(Instruction.BIT, BIT(instructionList)),
Pair(Instruction.BPL, BPL(instructionList)),
Pair(Instruction.BMI, BMI(instructionList)),
Pair(Instruction.BVC, BVC(instructionList)),
Pair(Instruction.BVS, BVS(instructionList)),
Pair(Instruction.BCC, BCC(instructionList)),
Pair(Instruction.BCS, BCS(instructionList)),
Pair(Instruction.BNE, BNE(instructionList)),
Pair(Instruction.BEQ, BEQ(instructionList)),
Pair(Instruction.BRK, BRK(instructionList)),
Pair(Instruction.CMP, CMP(instructionList)),
Pair(Instruction.CPX, CPX(instructionList)),
Pair(Instruction.CPY, CPY(instructionList)),
Pair(Instruction.DEC, DEC(instructionList)),
Pair(Instruction.EOR, EOR(instructionList)),
Pair(Instruction.CLC, CLC(instructionList)),
Pair(Instruction.SEC, SEC(instructionList)),
Pair(Instruction.CLI, CLI(instructionList)),
Pair(Instruction.SEI, SEI(instructionList)),
Pair(Instruction.CLV, CLV(instructionList)),
Pair(Instruction.CLD, CLD(instructionList)),
Pair(Instruction.SED, SED(instructionList)),
Pair(Instruction.INC, INC(instructionList)),
Pair(Instruction.JMP, JMP(instructionList)),
Pair(Instruction.JSR, JSR(instructionList)),
Pair(Instruction.LDA, LDA(instructionList)),
Pair(Instruction.LDX, LDX(instructionList)),
Pair(Instruction.LDY, LDY(instructionList)),
Pair(Instruction.LSR, LSR(instructionList)),
Pair(Instruction.NOP, NOP(instructionList)),
Pair(Instruction.ORA, ORA(instructionList)),
Pair(Instruction.TAX, TAX(instructionList)),
Pair(Instruction.TXA, TXA(instructionList)),
Pair(Instruction.DEX, DEX(instructionList)),
Pair(Instruction.INX, INX(instructionList)),
Pair(Instruction.TAY, TAY(instructionList)),
Pair(Instruction.TYA, TYA(instructionList)),
Pair(Instruction.DEY, DEY(instructionList)),
Pair(Instruction.INY, INY(instructionList)),
Pair(Instruction.ROR, ROR(instructionList)),
Pair(Instruction.ROL, ROL(instructionList)),
Pair(Instruction.RTI, RTI(instructionList)),
Pair(Instruction.RTS, RTS(instructionList)),
Pair(Instruction.SBC, SBC(instructionList)),
Pair(Instruction.STA, STA(instructionList)),
Pair(Instruction.TXS, TXS(instructionList)),
Pair(Instruction.TSX, TSX(instructionList)),
Pair(Instruction.PHA, PHA(instructionList)),
Pair(Instruction.PLA, PLA(instructionList)),
Pair(Instruction.PHP, PHP(instructionList)),
Pair(Instruction.PLP, PLP(instructionList)),
Pair(Instruction.STX, STX(instructionList)),
Pair(Instruction.STY, STY(instructionList))
Pair(Instruction.ADC, ADC(this)),
Pair(Instruction.AND, AND(this)),
Pair(Instruction.ASL, ASL(this)),
Pair(Instruction.BIT, BIT(this)),
Pair(Instruction.LDA, LDA(this)),
Pair(Instruction.LDX, LDX(this)),
Pair(Instruction.LDY, LDY(this)),
Pair(Instruction.STA, STA(memory, this)),
Pair(Instruction.STX, STX(this)),
Pair(Instruction.ORA, ORA(this))
// Pair(Instruction.BPL, BPL(this)),
// Pair(Instruction.BMI, BMI(this)),
// Pair(Instruction.BVC, BVC(this)),
// Pair(Instruction.BVS, BVS(this)),
// Pair(Instruction.BCC, BCC(this)),
// Pair(Instruction.BCS, BCS(this)),
// Pair(Instruction.BNE, BNE(this)),
// Pair(Instruction.BEQ, BEQ(this)),
// Pair(Instruction.BRK, BRK(this)),
// Pair(Instruction.CMP, CMP(this)),
// Pair(Instruction.CPX, CPX(this)),
// Pair(Instruction.CPY, CPY(this)),
// Pair(Instruction.DEC, DEC(this)),
// Pair(Instruction.EOR, EOR(this)),
// Pair(Instruction.CLC, CLC(this)),
// Pair(Instruction.SEC, SEC(this)),
// Pair(Instruction.CLI, CLI(this)),
// Pair(Instruction.SEI, SEI(this)),
// Pair(Instruction.CLV, CLV(this)),
// Pair(Instruction.CLD, CLD(this)),
// Pair(Instruction.SED, SED(this)),
// Pair(Instruction.INC, INC(this)),
// Pair(Instruction.JMP, JMP(this)),
// Pair(Instruction.JSR, JSR(this)),
// Pair(Instruction.LSR, LSR(this)),
// Pair(Instruction.NOP, NOP(this)),
// Pair(Instruction.TAX, TAX(this)),
// Pair(Instruction.TXA, TXA(this)),
// Pair(Instruction.DEX, DEX(this)),
// Pair(Instruction.INX, INX(this)),
// Pair(Instruction.TAY, TAY(this)),
// Pair(Instruction.TYA, TYA(this)),
// Pair(Instruction.DEY, DEY(this)),
// Pair(Instruction.INY, INY(this)),
// Pair(Instruction.ROR, ROR(this)),
// Pair(Instruction.ROL, ROL(this)),
// Pair(Instruction.RTI, RTI(this)),
// Pair(Instruction.RTS, RTS(this)),
// Pair(Instruction.SBC, SBC(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))
)
fun execute() {
@ -96,19 +100,41 @@ class CPU(private val memory: Memory) {
private fun executeNextInstruction() {
val instruction = Integer.valueOf(popByte().toInt().toString(), 16)
val function = instructionList.get(instruction)
if (function != null) {
ORA(instructionList).function()
val target = instructionList.get(instruction)
if (target != null) {
val function = target.method
target.operation.function()
} else {
Log.e(TAG, "Address $" + PC + " - unknown opcode")
}
}
private fun popByte(): Byte {
fun popByte(): Int {
return memory.get((PC++).and(0xff));
}
private fun setRandomByte() {
memory.set(0xfe, Math.floor(Math.random() * 256).toInt())
}
fun setSZFlagsForRegA() {
setSZFlagsForValue(A)
}
private fun setSZFlagsForValue(value: Int) {
if (value != 0) {
P = P.and(0xfd);
} else {
P = P.or(0x02);
}
if (value.and(0x80) != 0) {
P = P.or(0x80);
} else {
P = P.and(0x7f);
}
}
fun popWord(): Int {
return popByte() + popByte().shl(8)
}
}

View File

@ -0,0 +1,6 @@
package android.emu6502
final class Display {
fun updatePixel(addr: Int) {
}
}

View File

@ -0,0 +1,8 @@
package android.emu6502
final class Emulator {
val display = Display()
val memory = Memory(display)
val cpu = CPU(memory)
val assembler = Assembler(Labels(), memory, Symbols())
}

View File

@ -1,13 +1,20 @@
package android.emu6502
class Memory {
private val mem = ByteArray(65536)
class Memory(private val display: Display) {
private val mem = IntArray(65536)
public fun get(addr: Int): Byte {
fun get(addr: Int): Int {
return mem[addr]
}
public fun set(addr: Int, value: Int) {
mem[addr] = value.toByte()
fun set(addr: Int, value: Int) {
mem[addr] = value
}
fun storeByte(addr: Int, value: Int) {
set(addr, value.and(0xff))
if ((addr >= 0x200) && (addr <= 0x5ff)) {
display.updatePixel(addr)
}
}
}

View File

@ -1,5 +1,6 @@
package android.emu6502.app
import android.emu6502.Emulator
import android.emu6502.R
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
@ -9,6 +10,7 @@ import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import android.widget.Toast
import butterknife.bindView
public class MainActivity : AppCompatActivity() {
@ -20,7 +22,9 @@ public class MainActivity : AppCompatActivity() {
val txtSP: TextView by bindView(R.id.SP)
val txtPC: TextView by bindView(R.id.PC)
val txtFlags: TextView by bindView(R.id.PC)
val fab: FloatingActionButton by bindView(R.id.fab)
val txtInstructions: TextView by bindView(R.id.txtInstructions)
val fabRun: FloatingActionButton by bindView(R.id.fabRun)
val emulator = Emulator()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -30,6 +34,11 @@ public class MainActivity : AppCompatActivity() {
var ab: ActionBar = getSupportActionBar()
ab.setDisplayHomeAsUpEnabled(true)
fabRun.setOnClickListener {
emulator.assembler.assembleCode(txtInstructions.getText().toString().splitBy("\n"))
Toast.makeText(fabRun.getContext(), "Code assembled", Toast.LENGTH_SHORT).show()
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {

View File

@ -1,9 +0,0 @@
package android.emu6502.instructions
import java.util.*
import kotlin.reflect.KMemberFunction0
/** ADd with Carry */
class ADC(instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.ADC, instructionList) {
}

View File

@ -1,9 +0,0 @@
package android.emu6502.instructions
import java.util.*
import kotlin.reflect.KMemberFunction0
/** bitwise AND with accumulator */
class AND(instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.AND, instructionList) {
}

View File

@ -1,9 +0,0 @@
package android.emu6502.instructions
import java.util.*
import kotlin.reflect.KMemberFunction0
/** Arithmetic Shift Left */
class ASL(instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.ASL, instructionList) {
}

View File

@ -3,7 +3,7 @@ package android.emu6502.instructions
import java.util.HashMap
open class BaseInstruction(private val instruction: Instruction,
private val instructionList: HashMap<Int, kotlin.reflect.KMemberFunction0<BaseInstruction, Unit>>) {
private val instructionList: HashMap<Int, InstructionTarget>) {
init {
val opcodes: IntArray = Opcodes.MAP[instruction] as IntArray
@ -12,44 +12,44 @@ open class BaseInstruction(private val instruction: Instruction,
opcodes.forEachIndexed { i, opcode ->
if (opcode != 0xff) {
instructionList.put(opcodes[i], methods[i])
instructionList.put(opcodes[i], InstructionTarget(this, methods[i]))
}
}
}
fun immediate() {
open fun immediate() {
}
fun zeroPage() {
open fun zeroPage() {
}
fun zeroPageX() {
open fun zeroPageX() {
}
fun zeroPageY() {
open fun zeroPageY() {
}
fun absolute() {
open fun absolute() {
}
fun absoluteX() {
open fun absoluteX() {
}
fun absoluteY() {
open fun absoluteY() {
}
fun indirect() {
open fun indirect() {
}
fun indirectX() {
open fun indirectX() {
}
fun indirectY() {
open fun indirectY() {
}
fun single() {
open fun single() {
}
fun branch() {
open fun branch() {
}
}

View File

@ -0,0 +1,7 @@
package android.emu6502.instructions
import kotlin.reflect.KMemberFunction0
class InstructionTarget(val operation: BaseInstruction,
val method: KMemberFunction0<BaseInstruction, Unit>) {
}

View File

@ -1,9 +0,0 @@
package android.emu6502.instructions
import java.util.*
import kotlin.reflect.KMemberFunction0
/** LoaD X register */
class LDX(instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.LDX, instructionList) {
}

View File

@ -1,8 +0,0 @@
package android.emu6502.instructions
import java.util.HashMap
/** bitwise OR with Accumulator */
class ORA(instructionList: HashMap<Int, kotlin.reflect.KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.ORA, instructionList) {
}

View File

@ -1,10 +0,0 @@
package android.emu6502.instructions
import java.util.*
import kotlin.reflect.KMemberFunction0
/** STore X register */
class STX(instructionList: HashMap<Int, KMemberFunction0<BaseInstruction, Unit>>)
: BaseInstruction(Instruction.STX, instructionList) {
}

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** ADd with Carry */
class ADC(cpu: CPU)
: BaseInstruction(Instruction.ADC, cpu.instructionList) {
}

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** bitwise AND with accumulator */
class AND(cpu: CPU)
: BaseInstruction(Instruction.AND, cpu.instructionList) {
}

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** Arithmetic Shift Left */
class ASL(cpu: CPU)
: BaseInstruction(Instruction.ASL, cpu.instructionList) {
}

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** test BITs */
class BIT(cpu: CPU)
: BaseInstruction(Instruction.BIT, cpu.instructionList) {
}

View File

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

View File

@ -0,0 +1,9 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** LoaD X register */
class LDX(cpu: CPU) : BaseInstruction(Instruction.LDX, cpu.instructionList) {
}

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** LoaD Y register */
class LDY(cpu: CPU)
: BaseInstruction(Instruction.LDY, cpu.instructionList) {
}

View File

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

View File

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

View File

@ -0,0 +1,10 @@
package android.emu6502.instructions.impl
import android.emu6502.CPU
import android.emu6502.instructions.BaseInstruction
import android.emu6502.instructions.Instruction
/** STore X register */
class STX(cpu: CPU)
: BaseInstruction(Instruction.STX, cpu.instructionList) {
}

View File

@ -39,7 +39,7 @@
android:id="@+id/txtInstructions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="LDA #$80\nSTA $01\nADC $01"
android:text="LDA #$01\nSTA $0200\nLDA #$05\nSTA $0201\nLDA #$08\nSTA $0202"
android:fontFamily="monospace"
android:textSize="14sp"
android:gravity="top" />
@ -145,7 +145,7 @@
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:id="@+id/fabRun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"