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

77 lines
2.1 KiB
Java

package android.emu6502;
import com.google.common.collect.ImmutableList;
import android.emu6502.instructions.Symbols;
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));
}
@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));
}
@Test public void testBranchAndLabel() {
List<String> lines = ImmutableList.of(
"LDX #$08",
"decrement:",
"DEX",
"STX $0200",
"CPX #$03",
"BNE decrement",
"STX $0201",
"BRK");
assembler.assembleCode(lines);
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));
}
}