diff --git a/src/main/java/com/loomcom/lm6502/Cpu.java b/src/main/java/com/loomcom/lm6502/Cpu.java index 186ea32..e0b4637 100644 --- a/src/main/java/com/loomcom/lm6502/Cpu.java +++ b/src/main/java/com/loomcom/lm6502/Cpu.java @@ -621,8 +621,11 @@ public class Cpu implements InstructionTable { break; } } - - public String statusString() { + + /** + * Returns a string representing the CPU state. + */ + public String toString() { String opcode = CpuUtils.opcode(ir, operands[0], operands[1]); StringBuffer sb = new StringBuffer(String.format("$%04X", addr) + " "); sb.append(String.format("%-12s", opcode)); @@ -633,10 +636,31 @@ public class Cpu implements InstructionTable { return sb.toString(); } + /** + * Push an item onto the stack, and decrement the stack counter. + * Silently fails to push onto the stack if SP is + * TODO: Unit tests. + */ + protected void push(int data) { + bus.write(sp, data); + if (sp > 0x100) { sp--; } + } + + + /** + * Pop a byte off the user stack, and increment the stack counter. + * TODO: Unit tests. + */ + protected int pop() { + int data = bus.read(sp); + if (sp < 0x1ff) { sp++; } + return data; + } + /* * Increment the PC, rolling over if necessary. */ - private void incProgramCounter() { + protected void incProgramCounter() { if (pc == 0xffff) { pc = 0; } else { diff --git a/src/main/java/com/loomcom/lm6502/Simulator.java b/src/main/java/com/loomcom/lm6502/Simulator.java index 6fd6e0d..adf9ce7 100644 --- a/src/main/java/com/loomcom/lm6502/Simulator.java +++ b/src/main/java/com/loomcom/lm6502/Simulator.java @@ -61,22 +61,29 @@ public class Simulator { bus.write(0x0302, 0xea); // NOP bus.write(0x0303, 0xea); // NOP bus.write(0x0304, 0xea); // NOP - bus.write(0x0305, 0xea); // NOP - bus.write(0x0306, 0xa9); // LDA #$1A - bus.write(0x0307, 0x1a); + bus.write(0x0305, 0xa0); // LDY #$1A + bus.write(0x0306, 0x1a); + bus.write(0x0307, 0xea); // NOP bus.write(0x0308, 0xea); // NOP - bus.write(0x0309, 0xea); // NOP - bus.write(0x030a, 0xa9); // LDA #$03 - bus.write(0x030b, 0x03); - bus.write(0x030c, 0x4c); // JMP #$0300 - bus.write(0x030d, 0x00); - bus.write(0x030e, 0x03); + bus.write(0x0309, 0xa2); // LDX #$03 + bus.write(0x030a, 0x03); + + bus.write(0x030b, 0xa9); // LDA #$00 + bus.write(0x030c, 0x00); + bus.write(0x030d, 0xa2); // LDX #$00 + bus.write(0x030e, 0x00); + bus.write(0x030f, 0xa0); // LDY #$00 + bus.write(0x0310, 0x00); + + bus.write(0x0311, 0x4c); // JMP #$0300 + bus.write(0x0312, 0x00); + bus.write(0x0313, 0x03); cpu.reset(); - for (int i = 0; i < 60; i++) { + for (int i = 0; i <= 23; i++) { cpu.step(); - System.out.println(cpu.statusString()); + System.out.println(cpu.toString()); } }