mirror of
https://github.com/felipecsl/6502Android.git
synced 2024-06-28 21:29:43 +00:00
Fixes Assembler. Adds test
This commit is contained in:
parent
8f83e56e67
commit
60c9b254a6
|
@ -33,4 +33,11 @@ dependencies {
|
||||||
compile "org.jetbrains.kotlin:kotlin-reflect:0.12.213"
|
compile "org.jetbrains.kotlin:kotlin-reflect:0.12.213"
|
||||||
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
|
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
|
||||||
compile 'com.facebook.stetho:stetho:1.1.1'
|
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'
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -15,8 +15,7 @@ class Assembler(private var labels: Labels, private var memory: Memory,
|
||||||
fun assembleCode(lines: List<String>) {
|
fun assembleCode(lines: List<String>) {
|
||||||
lines.forEachIndexed { i, line ->
|
lines.forEachIndexed { i, line ->
|
||||||
if (!assembleLine(line)) {
|
if (!assembleLine(line)) {
|
||||||
val str = line.replace("<", "<").replace(">", ">")
|
throw RuntimeException("**Syntax error line " + (i + 1) + ": " + line + "**")
|
||||||
throw RuntimeException("**Syntax error line " + (i + 1) + ": " + str + "**")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// set a null byte at the end of the code
|
// 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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun hexdump(): String {
|
||||||
|
return memory.format(0x600, codeLen)
|
||||||
|
}
|
||||||
|
|
||||||
private fun DCB(param: String): Boolean {
|
private fun DCB(param: String): Boolean {
|
||||||
throw UnsupportedOperationException(
|
throw UnsupportedOperationException(
|
||||||
"not implemented") //To change body of created functions use File | Settings | File Templates.
|
"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.
|
// Try to parse the given parameter as a byte operand.
|
||||||
// Returns the (positive) value if successful, otherwise -1
|
// Returns the (positive) value if successful, otherwise -1
|
||||||
private fun tryParseByteOperand(param: String): Int {
|
private fun tryParseByteOperand(param: String): Int {
|
||||||
var value: Int = 0
|
var value: Int = -1
|
||||||
var parameter = param
|
var parameter = param
|
||||||
|
|
||||||
if (parameter.matches("^\\w+$".toRegex())) {
|
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 {
|
private fun tryParseWordOperand(param: String): Int {
|
||||||
var value: Int = 0
|
var value: Int = -1
|
||||||
var parameter = param
|
var parameter = param
|
||||||
|
|
||||||
if (parameter.matches("^\\w+$".toRegex())) {
|
if (parameter.matches("^\\w+$".toRegex())) {
|
||||||
|
|
|
@ -139,8 +139,4 @@ class CPU(private val memory: Memory) {
|
||||||
fun popWord(): Int {
|
fun popWord(): Int {
|
||||||
return popByte() + popByte().shl(8)
|
return popByte() + popByte().shl(8)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Int.toHexString(): String {
|
|
||||||
return Integer.toHexString(this)
|
|
||||||
}
|
|
||||||
}
|
}
|
14
app/src/main/kotlin/android/emu6502/Emu6502Application.kt
Normal file
14
app/src/main/kotlin/android/emu6502/Emu6502Application.kt
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package android.emu6502
|
package android.emu6502
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class Memory(private val display: Display) {
|
class Memory(private val display: Display) {
|
||||||
private val mem = IntArray(65536)
|
private val mem = IntArray(65536)
|
||||||
|
|
||||||
|
@ -17,4 +19,26 @@ class Memory(private val display: Display) {
|
||||||
display.updatePixel(addr)
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
5
app/src/main/kotlin/android/emu6502/Utils.kt
Normal file
5
app/src/main/kotlin/android/emu6502/Utils.kt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package android.emu6502
|
||||||
|
|
||||||
|
fun Int.toHexString(): String {
|
||||||
|
return java.lang.String.format("%02X", this);
|
||||||
|
}
|
26
app/src/test/java/android/emu6502/AssemblerTest.java
Normal file
26
app/src/test/java/android/emu6502/AssemblerTest.java
Normal 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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user