Fixes Assembler. Adds test

This commit is contained in:
Felipe Lima 2015-06-13 19:28:37 -07:00
parent 8f83e56e67
commit 60c9b254a6
9 changed files with 83 additions and 38 deletions

View File

@ -33,4 +33,11 @@ dependencies {
compile "org.jetbrains.kotlin:kotlin-reflect:0.12.213"
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
compile 'com.facebook.stetho:stetho:1.1.1'
compile 'com.google.guava:guava:18.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.hamcrest:hamcrest-integration:1.3'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'org.hamcrest:hamcrest-library:1.3'
}

View File

@ -1,14 +0,0 @@
package felipecsl.com.emu6502;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}

View File

@ -1,16 +0,0 @@
package android.emu6502;
import android.app.Application;
import com.facebook.stetho.Stetho;
public class Emu6502Application extends Application {
@Override public void onCreate() {
super.onCreate();
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build());
}
}

View File

@ -15,8 +15,7 @@ class Assembler(private var labels: Labels, private var memory: Memory,
fun assembleCode(lines: List<String>) {
lines.forEachIndexed { i, line ->
if (!assembleLine(line)) {
val str = line.replace("<", "&lt;").replace(">", "&gt;")
throw RuntimeException("**Syntax error line " + (i + 1) + ": " + str + "**")
throw RuntimeException("**Syntax error line " + (i + 1) + ": " + line + "**")
}
}
// set a null byte at the end of the code
@ -121,6 +120,10 @@ class Assembler(private var labels: Labels, private var memory: Memory,
return false
}
fun hexdump(): String {
return memory.format(0x600, codeLen)
}
private fun DCB(param: String): Boolean {
throw UnsupportedOperationException(
"not implemented") //To change body of created functions use File | Settings | File Templates.
@ -289,7 +292,7 @@ class Assembler(private var labels: Labels, private var memory: Memory,
// Try to parse the given parameter as a byte operand.
// Returns the (positive) value if successful, otherwise -1
private fun tryParseByteOperand(param: String): Int {
var value: Int = 0
var value: Int = -1
var parameter = param
if (parameter.matches("^\\w+$".toRegex())) {
@ -321,7 +324,7 @@ class Assembler(private var labels: Labels, private var memory: Memory,
}
private fun tryParseWordOperand(param: String): Int {
var value: Int = 0
var value: Int = -1
var parameter = param
if (parameter.matches("^\\w+$".toRegex())) {

View File

@ -139,8 +139,4 @@ class CPU(private val memory: Memory) {
fun popWord(): Int {
return popByte() + popByte().shl(8)
}
fun Int.toHexString(): String {
return Integer.toHexString(this)
}
}

View File

@ -0,0 +1,14 @@
package android.emu6502
import android.app.Application
import com.facebook.stetho.Stetho
public class Emu6502Application : Application() {
override fun onCreate() {
super.onCreate()
Stetho.initialize(Stetho.newInitializerBuilder(this)
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build())
}
}

View File

@ -1,5 +1,7 @@
package android.emu6502
import java.util.*
class Memory(private val display: Display) {
private val mem = IntArray(65536)
@ -17,4 +19,26 @@ class Memory(private val display: Display) {
display.updatePixel(addr)
}
}
fun format(start: Int, length: Int): String {
var i = 0
var n: Int
val dump = StringBuilder()
while (i < length) {
if (i.and(15) == 0) {
if (i > 0) {
dump.append("\n")
}
n = start + i
dump.append(n.shr(8).and(0xff).toHexString())
dump.append(n.and(0xff).toHexString())
dump.append(": ")
}
dump.append(get(start + i).toHexString())
dump.append(" ")
i++
}
return dump.toString().trim()
}
}

View File

@ -0,0 +1,5 @@
package android.emu6502
fun Int.toHexString(): String {
return java.lang.String.format("%02X", this);
}

View File

@ -0,0 +1,26 @@
package android.emu6502;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class AssemblerTest {
@Test public void testAssembler() {
List<String> lines = ImmutableList.of(
"LDA #$01",
"STA $0200",
"LDA #$05",
"STA $0201",
"LDA #$08",
"STA $0202");
Assembler assembler = new Assembler(new Labels(), new Memory(new Display()), new Symbols());
assembler.assembleCode(lines);
assertThat(assembler.hexdump(), equalTo("0600: A9 01 8D 00 02 A9 05 8D 01 02 A9 08 8D 02 02"));
}
}