diff --git a/src/main/java/com/loomcom/lm6502/Cpu.java b/src/main/java/com/loomcom/lm6502/Cpu.java index 1c47868..e4623e8 100644 --- a/src/main/java/com/loomcom/lm6502/Cpu.java +++ b/src/main/java/com/loomcom/lm6502/Cpu.java @@ -3,20 +3,20 @@ package com.loomcom.lm6502; /** * Main 6502 CPU Simulation. */ -public class Cpu { +public class Cpu implements InstructionTable { /* The Bus */ private Bus bus; /* User Registers */ - private int a; // Accumulator - private int x; // X index register - private int y; // Y index register + private int a; // Accumulator + private int x; // X index register + private int y; // Y index register /* Internal Registers */ - private int pc; // Program Counter register - private int sp; // Stack Pointer register - private int ir; // Instruction register + private int pc; // Program Counter register + private int sp; // Stack Pointer register + private int ir; // Instruction register /** * Construct a new CPU. @@ -48,5 +48,29 @@ public class Cpu { ir = 0; } + /** + * Performs an individual machine cycle. + */ + public void step() { + // 1. Fetch memory + ir = bus.read(pc); + + // 2. Decode the instruction and execute. + + // x. Increment PC + incProgramCounter(); + } + + /* + * Increment the PC, rolling over if necessary. + */ + private void incProgramCounter() { + if (pc == 0xffff) { + pc = 0; + } else { + ++pc; + } + } + } diff --git a/src/main/java/com/loomcom/lm6502/InstructionTable.java b/src/main/java/com/loomcom/lm6502/InstructionTable.java new file mode 100644 index 0000000..a6f41d7 --- /dev/null +++ b/src/main/java/com/loomcom/lm6502/InstructionTable.java @@ -0,0 +1,195 @@ +package com.loomcom.lm6502; + +public interface InstructionTable { + + // Addressing mode Enumeration + public enum Mode { + ACC { + public String toString() { + return "Accumulator"; + } + }, + + ABS { + public String toString() { + return "Absolute"; + } + }, + + ABX { + public String toString() { + return "Absolute, X-indexed"; + } + }, + + ABY { + public String toString() { + return "Absolute, Y-indexed"; + } + }, + + IMM { + public String toString() { + return "Immediate"; + } + }, + + IMP { + public String toString() { + return "Implied"; + } + }, + + IND { + public String toString() { + return "Indirect"; + } + }, + + XIN { + public String toString() { + return "X-indexed Indirect"; + } + }, + + INY { + public String toString() { + return "Indirect, Y-indexedY"; + } + }, + + REL { + public String toString() { + return "Relative"; + } + }, + + ZPG { + public String toString() { + return "Zeropage"; + } + }, + + ZPX { + public String toString() { + return "Zeropage, X-indexed"; + } + }, + + ZPY { + public String toString() { + return "Zeropage, Y-indexed"; + } + }, + + NUL { + public String toString() { + return "NULL"; + } + } + } + + // 6502 opcodes. No 65C02 opcodes implemented. + public static final String[] opcodeNames = { + "BRK", "ORA", null, null, null, "ORA", "ASL", null, + "PHP", "ORA", "ASL", null, null, "ORA", "ASL", null, + "BPL", "ORA", null, null, null, "ORA", "ASL", null, + "CLC", "ORA", null, null, null, "ORA", "ASL", null, + "JSR", "AND", null, null, "BIT", "AND", "ROL", null, + "PLP", "AND", "ROL", null, "BIT", "AND", "ROL", null, + "BMI", "AND", null, null, null, "AND", "ROL", null, + "SEC", "AND", null, null, null, "AND", "ROL", null, + "RTI", "EOR", null, null, null, "EOR", "LSR", null, + "PHA", "EOR", "LSR", null, "JMP", "EOR", "LSR", null, + "BVC", "EOR", null, null, null, "EOR", "LSR", null, + "CLI", "EOR", null, null, null, "EOR", "LSR", null, + "RTS", "ADC", null, null, null, "ADC", "ROR", null, + "PLA", "ADC", "ROR", null, "JMP", "ADC", "ROR", null, + "BVS", "ADC", null, null, null, "ADC", "ROR", null, + "SEI", "ADC", null, null, null, "ADC", "ROR", null, + "BCS", "STA", null, null, "STY", "STA", "STX", null, + "DEY", null, "TXA", null, "STY", "STA", "STX", null, + "BCC", "STA", null, null, "STY", "STA", "STX", null, + "TYA", "STA", "TXS", null, null, "STA", null, null, + "LDY", "LDA", "LDX", null, "LDY", "LDA", "LDX", null, + "TAY", "LDA", "TAX", null, "LDY", "LDA", "LDX", null, + null, "LDA", null, null, "LDY", "LDA", "LDX", null, + "CLV", "LDA", "TSX", null, "LDY", "LDA", "LDX", null, + "CPY", "CMP", null, null, "CPY", "CMP", "DEC", null, + "INY", "CMP", "DEX", null, "CPY", "CMP", "DEC", null, + "BNE", "CMP", null, null, null, "CMP", "DEC", null, + "CLD", "CMP", null, null, null, "CMP", "DEC", null, + "CPX", "SBC", null, null, "CPX", "SBC", "INC", null, + "INX", "SBC", "NOP", null, "CPX", "SBC", "INC", null, + "BEQ", "SBC", null, null, null, "SBC", "INC", null, + "SED", "SBC", null, null, null, "SBC", "INC", null + }; + + public static final Mode[] opcodeModes = { + Mode.IMP, Mode.XIN, Mode.NUL, Mode.NUL, // 0x00-0x03 + Mode.NUL, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0x04-0x07 + Mode.IMP, Mode.IMM, Mode.ACC, Mode.NUL, // 0x08-0x0b + Mode.NUL, Mode.ABS, Mode.ABS, Mode.NUL, // 0x0c-0x0f + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0x10-0x13 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0x14-0x17 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0x18-0x1b + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL, // 0x1c-0x1f + Mode.ABS, Mode.XIN, Mode.NUL, Mode.NUL, // 0x20-0x23 + Mode.ZPG, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0x24-0x27 + Mode.IMP, Mode.IMM, Mode.ACC, Mode.NUL, // 0x28-0x2b + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0x2c-0x2f + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0x30-0x33 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0x34-0x37 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0x38-0x3b + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL, // 0x3c-0x3f + Mode.IMP, Mode.XIN, Mode.NUL, Mode.NUL, // 0x40-0x43 + Mode.NUL, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0x44-0x47 + Mode.IMP, Mode.IMM, Mode.ACC, Mode.NUL, // 0x48-0x4b + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0x4c-0x4f + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0x50-0x53 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0x54-0x57 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0x58-0x5b + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL, // 0x5c-0x5f + Mode.IMP, Mode.XIN, Mode.NUL, Mode.NUL, // 0x60-0x63 + Mode.NUL, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0x64-0x67 + Mode.IMP, Mode.IMM, Mode.ACC, Mode.NUL, // 0x68-0x6b + Mode.IND, Mode.ABS, Mode.ABS, Mode.NUL, // 0x6c-0x6f + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0x70-0x73 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0x74-0x77 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0x78-0x7b + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL, // 0x7c-0x7f + Mode.REL, Mode.XIN, Mode.NUL, Mode.NUL, // 0x80-0x83 + Mode.ZPG, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0x84-0x87 + Mode.IMP, Mode.NUL, Mode.IMP, Mode.NUL, // 0x88-0x8b + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0x8c-0x8f + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0x90-0x93 + Mode.ZPX, Mode.ZPX, Mode.ZPY, Mode.NUL, // 0x94-0x97 + Mode.IMP, Mode.ABX, Mode.IMP, Mode.NUL, // 0x98-0x9b + Mode.NUL, Mode.ABY, Mode.NUL, Mode.NUL, // 0x9c-0x9f + Mode.IMM, Mode.XIN, Mode.IMM, Mode.NUL, // 0xa0-0xa3 + Mode.ZPG, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0xa4-0xa7 + Mode.IMP, Mode.IMM, Mode.IMP, Mode.NUL, // 0xa8-0xab + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0xac-0xaf + Mode.NUL, Mode.INY, Mode.NUL, Mode.NUL, // 0xb0-0xb3 + Mode.ZPX, Mode.ZPX, Mode.ZPY, Mode.NUL, // 0xb4-0xb7 + Mode.IMP, Mode.ABX, Mode.IMP, Mode.NUL, // 0xb8-0xbb + Mode.ABX, Mode.ABY, Mode.ABY, Mode.NUL, // 0xbc-0xbf + Mode.IMM, Mode.XIN, Mode.NUL, Mode.NUL, // 0xc0-0xc3 + Mode.ZPG, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0xc4-0xc7 + Mode.IMP, Mode.IMM, Mode.IMP, Mode.NUL, // 0xc8-0xcb + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0xcc-0xcf + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0xd0-0xd3 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0xd4-0xd7 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0xd8-0xdb + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL, // 0xdc-0xdf + Mode.IMM, Mode.XIN, Mode.NUL, Mode.NUL, // 0xe0-0xe3 + Mode.ZPG, Mode.ZPG, Mode.ZPG, Mode.NUL, // 0xe4-0xe7 + Mode.IMP, Mode.IMM, Mode.IMP, Mode.NUL, // 0xe8-0xeb + Mode.ABS, Mode.ABS, Mode.ABS, Mode.NUL, // 0xec-0xef + Mode.REL, Mode.INY, Mode.NUL, Mode.NUL, // 0xf0-0xf3 + Mode.NUL, Mode.ZPX, Mode.ZPX, Mode.NUL, // 0xf4-0xf7 + Mode.IMP, Mode.ABY, Mode.NUL, Mode.NUL, // 0xf8-0xfb + Mode.NUL, Mode.ABX, Mode.ABX, Mode.NUL // 0xfc-0xff + }; + +} \ No newline at end of file diff --git a/src/main/java/com/loomcom/lm6502/Profiler.java b/src/main/java/com/loomcom/lm6502/Profiler.java index 82872d4..76514c5 100644 --- a/src/main/java/com/loomcom/lm6502/Profiler.java +++ b/src/main/java/com/loomcom/lm6502/Profiler.java @@ -9,8 +9,31 @@ import java.util.*; import com.loomcom.lm6502.devices.*; import com.loomcom.lm6502.exceptions.*; -public class Profiler { +public class Profiler implements InstructionTable { + + public static void main(String[] args) { + // new Profiler().profileMemoryReads(); + + new Profiler().dumpOpCodes(); + } + + public void dumpOpCodes() { + for (int i = 0; i < 0x100; i++) { + String name = opcodeNames[i]; + Mode mode = opcodeModes[i]; + + System.out.print(String.format("0x%02x: ", i)); + + if (name == null) { + System.out.println("n/a"); + } else { + System.out.println(name + " (" + mode + ")"); + } + } + } + + public void profileMemoryReads() { try {