6502Android/app/src/test/java/android/emu6502/CPUTest.java

144 lines
3.9 KiB
Java
Raw Normal View History

2015-06-15 06:37:41 +00:00
package android.emu6502;
import android.emu6502.instructions.Symbols;
2015-06-22 01:06:44 +00:00
import com.google.common.collect.ImmutableList;
2015-06-15 06:37:41 +00:00
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class CPUTest {
private CPU cpu;
private Assembler assembler;
@Before public void setUp() {
Memory memory = new Memory(new Display());
assembler = new Assembler(memory, new Symbols());
cpu = new CPU(memory);
}
@Test public void testSimple() {
List<String> lines = ImmutableList.of(
"LDA #$01",
"STA $0200",
"LDA #$05",
"STA $0201",
"LDA #$08",
"STA $0202");
assembler.assembleCode(lines);
cpu.execute();
assertThat(cpu.getA(), equalTo(0x08));
assertThat(cpu.getX(), equalTo(0x00));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFF));
assertThat(cpu.getPC(), equalTo(0x0610));
2015-06-17 06:20:23 +00:00
assertThat(cpu.flags(), equalTo("00110000"));
2015-06-15 06:37:41 +00:00
}
@Test public void testWithComments() {
List<String> lines = ImmutableList.of(
"LDA #$c0 ;Load the hex value $c0 into the A register",
"TAX ;Transfer the value in the A register to X",
"INX ;Increment the value in the X register",
"ADC #$c4 ;Add the hex value $c4 to the A register",
"BRK ;Break - we're done");
assembler.assembleCode(lines);
cpu.execute();
assertThat(cpu.getA(), equalTo(0x84));
assertThat(cpu.getX(), equalTo(0xC1));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFF));
assertThat(cpu.getPC(), equalTo(0x0607));
2015-06-17 06:20:23 +00:00
assertThat(cpu.flags(), equalTo("10110001"));
2015-06-15 06:37:41 +00:00
}
@Test public void testBranchAndLabel() {
List<String> lines = ImmutableList.of(
"LDX #$08",
2015-06-22 01:06:44 +00:00
2015-06-15 06:37:41 +00:00
"decrement:",
"DEX",
"STX $0200",
"CPX #$03",
"BNE decrement",
"STX $0201",
"BRK");
assembler.assembleCode(lines);
2015-06-17 06:20:23 +00:00
cpu.execute();
2015-06-15 06:37:41 +00:00
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x03));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFF));
assertThat(cpu.getPC(), equalTo(0x060e));
2015-06-17 06:20:23 +00:00
assertThat(cpu.flags(), equalTo("00110011"));
2015-06-15 06:37:41 +00:00
}
2015-06-22 01:06:44 +00:00
@Test public void testJump() {
List<String> lines = ImmutableList.of(
"LDA #$03",
"JMP there",
"BRK",
"BRK",
"BRK",
"there:",
"STA $0200");
assembler.assembleCode(lines);
cpu.execute();
assertThat(cpu.getA(), equalTo(0x03));
assertThat(cpu.getX(), equalTo(0x00));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFF));
assertThat(cpu.getPC(), equalTo(0x060c));
assertThat(cpu.flags(), equalTo("00110000"));
}
@Test public void testJumpToSubroutines() {
List<String> lines = ImmutableList.of(
"JSR init",
"JSR loop",
"JSR end",
"init:",
"LDX #$00",
"RTS",
"loop:",
"INX",
"CPX #$05",
"BNE loop",
"RTS",
"end:",
"BRK");
assembler.assembleCode(lines);
cpu.execute();
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x05));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFD));
assertThat(cpu.getPC(), equalTo(0x0613));
assertThat(cpu.flags(), equalTo("00110011"));
}
2015-06-22 05:52:08 +00:00
@Test public void testSymbols() {
List<String> lines = ImmutableList.of(
"define a_dozen $0c ; a constant",
"LDX #a_dozen ; equivalent to \"LDX #$0c\"");
assembler.assembleCode(lines);
cpu.execute();
assertThat(cpu.getA(), equalTo(0x00));
assertThat(cpu.getX(), equalTo(0x0C));
assertThat(cpu.getY(), equalTo(0x00));
assertThat(cpu.getSP(), equalTo(0xFF));
assertThat(cpu.getPC(), equalTo(0x0603));
assertThat(cpu.flags(), equalTo("00110000"));
}
2015-06-15 06:37:41 +00:00
}