Renamed project to "Symon".

This commit is contained in:
Seth Morabito committed 2008-12-13 14:59:22 -08:00
1 parent 9a6a256073
commit 3f74489757
20 files changed
+102 -42

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
package com.loomcom.symon;
import java.util.*;
import com.loomcom.symon.devices.*;
import com.loomcom.symon.exceptions.*;
/**
* The Bus ties the whole thing together, man.
*/
public class Bus {
// By default, our bus starts at 0, and goes up to 64K
private int startAddress = 0x0000;
private int endAddress = 0xffff;
// The CPU
private Cpu cpu;
// Ordered list of IO devices.
private SortedSet<Device> devices;
public Bus(int size) {
this(0, size - 1);
}
public Bus(int startAddress, int endAddress) {
this.devices = new TreeSet<Device>();
this.startAddress = startAddress;
this.endAddress = endAddress;
}
public int startAddress() {
return startAddress;
}
public int endAddress() {
return endAddress;
}
public void addDevice(Device device)
throws MemoryRangeException {
// Make sure there's no memory overlap.
MemoryRange memRange = device.getMemoryRange();
for (Device d : devices) {
if (d.getMemoryRange().overlaps(memRange)) {
throw new MemoryRangeException("The device being added " +
"overlaps with an existing " +
"device.");
}
}
// Add the device
device.setBus(this);
devices.add(device);
}
public void addCpu(Cpu cpu) {
cpu.setBus(this);
this.cpu = cpu;
}
/**
* Returns true if the memory map is full, i.e., there are no
* gaps between any IO devices. All memory locations map to some
* device.
*/
public boolean isComplete() {
// Emtpy maps cannot be complete.
if (devices.isEmpty()) { return false; }
// Loop over devices and ensure they are contiguous.
MemoryRange prev = null;
int i = 0;
int length = devices.size();
for (Device d : devices) {
MemoryRange cur = d.getMemoryRange();
if (i == 0) {
// If the first entry doesn't start at 'startAddress', return false.
if (cur.startAddress() != startAddress) { return false; }
}
if (prev != null && i < length - 1) {
// Otherwise, compare previous map's end against this map's
// endAddress. They must be adjacent!
if (cur.startAddress() - 1 != prev.endAddress()) {
return false;
}
}
if (i == length - 1) {
// If the last entry doesn't end at endAddress, return false;
if (cur.endAddress() != endAddress) { return false; }
}
i++;
prev = cur;
}
// Must be complete.
return true;
}
public int read(int address) {
for (Device d : devices) {
MemoryRange range = d.getMemoryRange();
if (range.includes(address)) {
// Compute offset into this device's address space.
int devAddr = address - range.startAddress();
return d.read(devAddr);
}
}
// TODO: Raise access error.
throw new RuntimeException("Read failed! Device not found.");
}
public void write(int address, int value) {
for (Device d : devices) {
MemoryRange range = d.getMemoryRange();
if (range.includes(address)) {
// Compute offset into this device's address space.
int devAddr = address - range.startAddress();
d.write(devAddr, value);
return;
}
}
// TODO: Raise access error.
throw new RuntimeException("Write failed! Device not found.");
}
public SortedSet<Device> getDevices() {
// Expose a copy of the device list, not the original
return new TreeSet<Device>(devices);
}
}
@@ -0,0 +1,72 @@
package com.loomcom.symon;
import java.io.*;
public class CommandParser {
private BufferedReader in;
private BufferedWriter out;
private Simulator simulator;
public CommandParser(InputStream i, OutputStream o, Simulator s) {
this.in = new BufferedReader(new InputStreamReader(i));
this.out = new BufferedWriter(new OutputStreamWriter(o));
this.simulator = s;
}
public void run() {
try {
String command = null;
greeting();
prompt();
while (!shouldQuit(command = readLine())) {
dispatch(command);
prompt();
}
writeLine("\n\nGoodbye!");
} catch (IOException ex) {
System.err.println("Error: " + ex.toString());
System.exit(1);
}
}
/**
* Dispatch the command.
*/
public void dispatch(String command) throws IOException {
writeLine("You entered: " + command);
}
/*******************************************************************
* Private
*******************************************************************/
private void greeting() throws IOException {
writeLine("Welcome to the j6502 Simulator!");
}
private void prompt() throws IOException {
out.write("j6502> ");
out.flush();
}
private String readLine() throws IOException {
String line = in.readLine();
if (line == null) { return null; }
return line.trim();
}
private void writeLine(String line) throws IOException {
out.write(line);
out.newLine();
out.flush();
}
/**
* Returns true if the line is a quit.
*/
private boolean shouldQuit(String line) {
return (line == null || "q".equals(line.toLowerCase()));
}
}
+837
View File
@@ -0,0 +1,837 @@
package com.loomcom.symon;
import java.util.Arrays;
/**
* Main 6502 CPU Simulation.
*/
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
/* Internal Registers */
private int pc; // Program Counter register
private int sp; // Stack Pointer register
private int ir; // Instruction register
/* Operands for the current instruction */
private int[] operands = new int[2];
private int addr; // The address the most recent instruction
// was fetched from
/* Status Flag Register bits */
private boolean carryFlag;
private boolean negativeFlag;
private boolean zeroFlag;
private boolean irqDisableFlag;
private boolean decimalModeFlag;
private boolean breakFlag;
private boolean overflowFlag;
// Note: Zero Flag and Negative Flag are read directly from Accumulator.
/**
* Construct a new CPU.
*/
public Cpu() {}
/**
* Set the bus reference for this CPU.
*/
public void setBus(Bus bus) {
this.bus = bus;
}
/**
* Return the Bus that this CPU is associated with.
*/
public Bus getBus() {
return bus;
}
/**
* Reset the CPU to known initial values.
*/
public void reset() {
// Registers
sp = 0x01ff;
// Set the PC to the address stored in 0xfffc
pc = CpuUtils.address(bus.read(0xfffc), bus.read(0xfffd));
// Clear instruction register.
ir = 0;
// Clear status register bits.
carryFlag = false;
irqDisableFlag = false;
decimalModeFlag = false;
breakFlag = false;
overflowFlag = false;
}
/**
* Performs an individual machine cycle.
*/
public void step() {
// Store the address from which the IR was read, for debugging
addr = pc;
// Fetch memory location for this instruction.
ir = bus.read(pc);
// TODO: The way we increment the PC may need
// to change when interrupts are implemented
// Increment PC
incProgramCounter();
// Decode the instruction and operands
int size = Cpu.instructionSizes[ir];
for (int i = 0; i < size-1; i++) {
operands[i] = bus.read(pc);
// Increment PC after reading
incProgramCounter();
}
// Execute
switch(ir) {
case 0x00: // HLT
// TODO: Halt!
break;
case 0x01: // n/a
break;
case 0x02: // n/a
break;
case 0x03: // n/a
break;
case 0x04: // n/a
break;
case 0x05: // n/a
break;
case 0x06: // n/a
break;
case 0x07: // n/a
break;
case 0x08: // n/a
break;
case 0x09: // n/a
break;
case 0x0a: // n/a
break;
case 0x0b: // n/a
break;
case 0x0c: // n/a
break;
case 0x0d: // n/a
break;
case 0x0e: // n/a
break;
case 0x0f: // n/a
break;
case 0x10: // n/a
break;
case 0x11: // n/a
break;
case 0x12: // n/a
break;
case 0x13: // n/a
break;
case 0x14: // n/a
break;
case 0x15: // n/a
break;
case 0x16: // n/a
break;
case 0x17: // n/a
break;
case 0x18: // n/a
break;
case 0x19: // n/a
break;
case 0x1a: // n/a
break;
case 0x1b: // n/a
break;
case 0x1c: // n/a
break;
case 0x1d: // n/a
break;
case 0x1e: // n/a
break;
case 0x1f: // n/a
break;
case 0x20: // n/a
break;
case 0x21: // n/a
break;
case 0x22: // n/a
break;
case 0x23: // n/a
break;
case 0x24: // n/a
break;
case 0x25: // n/a
break;
case 0x26: // n/a
break;
case 0x27: // n/a
break;
case 0x28: // n/a
break;
case 0x29: // n/a
break;
case 0x2a: // n/a
break;
case 0x2b: // n/a
break;
case 0x2c: // n/a
break;
case 0x2d: // n/a
break;
case 0x2e: // n/a
break;
case 0x2f: // n/a
break;
case 0x30: // n/a
break;
case 0x31: // n/a
break;
case 0x32: // n/a
break;
case 0x33: // n/a
break;
case 0x34: // n/a
break;
case 0x35: // n/a
break;
case 0x36: // n/a
break;
case 0x37: // n/a
break;
case 0x38: // n/a
break;
case 0x39: // n/a
break;
case 0x3a: // n/a
break;
case 0x3b: // n/a
break;
case 0x3c: // n/a
break;
case 0x3d: // n/a
break;
case 0x3e: // n/a
break;
case 0x3f: // n/a
break;
case 0x40: // n/a
break;
case 0x41: // n/a
break;
case 0x42: // n/a
break;
case 0x43: // n/a
break;
case 0x44: // n/a
break;
case 0x45: // n/a
break;
case 0x46: // n/a
break;
case 0x47: // n/a
break;
case 0x48: // n/a
break;
case 0x49: // n/a
break;
case 0x4a: // n/a
break;
case 0x4b: // n/a
break;
case 0x4c: // JMP - Absolute
pc = CpuUtils.address(operands[0], operands[1]);
break;
case 0x4d: // n/a
break;
case 0x4e: // n/a
break;
case 0x4f: // n/a
break;
case 0x50: // n/a
break;
case 0x51: // n/a
break;
case 0x52: // n/a
break;
case 0x53: // n/a
break;
case 0x54: // n/a
break;
case 0x55: // n/a
break;
case 0x56: // n/a
break;
case 0x57: // n/a
break;
case 0x58: // n/a
break;
case 0x59: // n/a
break;
case 0x5a: // n/a
break;
case 0x5b: // n/a
break;
case 0x5c: // n/a
break;
case 0x5d: // n/a
break;
case 0x5e: // n/a
break;
case 0x5f: // n/a
break;
case 0x60: // n/a
break;
case 0x61: // n/a
break;
case 0x62: // n/a
break;
case 0x63: // n/a
break;
case 0x64: // n/a
break;
case 0x65: // n/a
break;
case 0x66: // n/a
break;
case 0x67: // n/a
break;
case 0x68: // n/a
break;
case 0x69: // n/a
break;
case 0x6a: // n/a
break;
case 0x6b: // n/a
break;
case 0x6c: // n/a
break;
case 0x6d: // n/a
break;
case 0x6e: // n/a
break;
case 0x6f: // n/a
break;
case 0x70: // n/a
break;
case 0x71: // n/a
break;
case 0x72: // n/a
break;
case 0x73: // n/a
break;
case 0x74: // n/a
break;
case 0x75: // n/a
break;
case 0x76: // n/a
break;
case 0x77: // n/a
break;
case 0x78: // n/a
break;
case 0x79: // n/a
break;
case 0x7a: // n/a
break;
case 0x7b: // n/a
break;
case 0x7c: // n/a
break;
case 0x7d: // n/a
break;
case 0x7e: // n/a
break;
case 0x7f: // n/a
break;
case 0x80: // n/a
break;
case 0x81: // n/a
break;
case 0x82: // n/a
break;
case 0x83: // n/a
break;
case 0x84: // n/a
break;
case 0x85: // n/a
break;
case 0x86: // n/a
break;
case 0x87: // n/a
break;
case 0x88: // n/a
break;
case 0x89: // n/a
break;
case 0x8a: // n/a
break;
case 0x8b: // n/a
break;
case 0x8c: // n/a
break;
case 0x8d: // n/a
break;
case 0x8e: // n/a
break;
case 0x8f: // n/a
break;
case 0x90: // n/a
break;
case 0x91: // n/a
break;
case 0x92: // n/a
break;
case 0x93: // n/a
break;
case 0x94: // n/a
break;
case 0x95: // n/a
break;
case 0x96: // n/a
break;
case 0x97: // n/a
break;
case 0x98: // n/a
break;
case 0x99: // n/a
break;
case 0x9a: // n/a
break;
case 0x9b: // n/a
break;
case 0x9c: // n/a
break;
case 0x9d: // n/a
break;
case 0x9e: // n/a
break;
case 0x9f: // n/a
break;
case 0xa0: // LDY - Immediate
y = operands[0];
setZeroFlag(y);
setNegativeFlag(y);
break;
case 0xa1: // n/a
break;
case 0xa2: // LDX - Immediate
x = operands[0];
setZeroFlag(x);
setNegativeFlag(x);
break;
case 0xa3: // n/a
break;
case 0xa4: // n/a
break;
case 0xa5: // n/a
break;
case 0xa6: // n/a
break;
case 0xa7: // n/a
break;
case 0xa8: // n/a
break;
case 0xa9: // LDA - Immediate
a = operands[0];
setZeroFlag(a);
setNegativeFlag(a);
break;
case 0xaa: // n/a
break;
case 0xab: // n/a
break;
case 0xac: // n/a
break;
case 0xad: // n/a
break;
case 0xae: // n/a
break;
case 0xaf: // n/a
break;
case 0xb0: // n/a
break;
case 0xb1: // n/a
break;
case 0xb2: // n/a
break;
case 0xb3: // n/a
break;
case 0xb4: // n/a
break;
case 0xb5: // n/a
break;
case 0xb6: // n/a
break;
case 0xb7: // n/a
break;
case 0xb8: // n/a
break;
case 0xb9: // n/a
break;
case 0xba: // n/a
break;
case 0xbb: // n/a
break;
case 0xbc: // n/a
break;
case 0xbd: // n/a
break;
case 0xbe: // n/a
break;
case 0xbf: // n/a
break;
case 0xc0: // n/a
break;
case 0xc1: // n/a
break;
case 0xc2: // n/a
break;
case 0xc3: // n/a
break;
case 0xc4: // n/a
break;
case 0xc5: // n/a
break;
case 0xc6: // n/a
break;
case 0xc7: // n/a
break;
case 0xc8: // n/a
break;
case 0xc9: // n/a
break;
case 0xca: // n/a
break;
case 0xcb: // n/a
break;
case 0xcc: // n/a
break;
case 0xcd: // n/a
break;
case 0xce: // n/a
break;
case 0xcf: // n/a
break;
case 0xd0: // n/a
break;
case 0xd1: // n/a
break;
case 0xd2: // n/a
break;
case 0xd3: // n/a
break;
case 0xd4: // n/a
break;
case 0xd5: // n/a
break;
case 0xd6: // n/a
break;
case 0xd7: // n/a
break;
case 0xd8: // n/a
break;
case 0xd9: // n/a
break;
case 0xda: // n/a
break;
case 0xdb: // n/a
break;
case 0xdc: // n/a
break;
case 0xdd: // n/a
break;
case 0xde: // n/a
break;
case 0xdf: // n/a
break;
case 0xe0: // n/a
break;
case 0xe1: // n/a
break;
case 0xe2: // n/a
break;
case 0xe3: // n/a
break;
case 0xe4: // n/a
break;
case 0xe5: // n/a
break;
case 0xe6: // n/a
break;
case 0xe7: // n/a
break;
case 0xe8: // n/a
break;
case 0xe9: // n/a
break;
case 0xea: // NOP
break;
case 0xeb: // n/a
break;
case 0xec: // n/a
break;
case 0xed: // n/a
break;
case 0xee: // n/a
break;
case 0xef: // n/a
break;
case 0xf0: // n/a
break;
case 0xf1: // n/a
break;
case 0xf2: // n/a
break;
case 0xf3: // n/a
break;
case 0xf4: // n/a
break;
case 0xf5: // n/a
break;
case 0xf6: // n/a
break;
case 0xf7: // n/a
break;
case 0xf8: // n/a
break;
case 0xf9: // n/a
break;
case 0xfa: // n/a
break;
case 0xfb: // n/a
break;
case 0xfc: // n/a
break;
case 0xfd: // n/a
break;
case 0xfe: // n/a
break;
case 0xff: // n/a
break;
}
}
/**
* @return the negative flag
*/
public boolean getNegativeFlag() {
return negativeFlag;
}
/**
* @param register the register value to test for negativity
*/
public void setNegativeFlag(int register) {
this.negativeFlag = (((register>>>7)&0xff) == 1);
}
/**
* @param negativeFlag the negative flag to set
*/
public void setNegativeFlag(boolean negativeFlag) {
this.negativeFlag = negativeFlag;
}
/**
* @return the carry flag
*/
public boolean getCarryFlag() {
return carryFlag;
}
/**
* @param carryFlag the carry flag to set
*/
public void setCarryFlag(boolean carryFlag) {
this.carryFlag = carryFlag;
}
/**
* @return the zero flag
*/
public boolean getZeroFlag() {
return zeroFlag;
}
/**
* @param register the register to test for zero
*/
public void setZeroFlag(int register) {
this.zeroFlag = (register == 0);
}
/**
* @param zeroFlag the zero flag to set
*/
public void setZeroFlag(boolean zeroFlag) {
this.zeroFlag = zeroFlag;
}
/**
* @return the irq disable flag
*/
public boolean getIrqDisableFlag() {
return irqDisableFlag;
}
/**
* @param irqDisableFlag the irq disable flag to set
*/
public void setIrqDisableFlag(boolean irqDisableFlag) {
this.irqDisableFlag = irqDisableFlag;
}
/**
* @return the decimal mode flag
*/
public boolean getDecimalModeFlag() {
return decimalModeFlag;
}
/**
* @param decimalModeFlag the decimal mde flag to set
*/
public void setDecimalModeFlag(boolean decimalModeFlag) {
this.decimalModeFlag = decimalModeFlag;
}
/**
* @return the break flag
*/
public boolean getBreakFlag() {
return breakFlag;
}
/**
* @param breakFlag the break flag to set
*/
public void setBreakFlag(boolean breakFlag) {
this.breakFlag = breakFlag;
}
/**
* @return the overflow flag
*/
public boolean getOverflowFlag() {
return overflowFlag;
}
/**
* @param overflowFlag the overflow flag to set
*/
public void setOverflowFlag(boolean overflowFlag) {
this.overflowFlag = overflowFlag;
}
public int getAccumulator() {
return a;
}
public int getXRegister() {
return x;
}
public int getYRegister() {
return y;
}
public int getProgramCounter() {
return pc;
}
/**
* @return A string representing the current status register state.
*/
public String statusRegisterString() {
StringBuffer sb = new StringBuffer();
sb.append("(N:" + (getNegativeFlag() ? '1' : '0') + ", ");
sb.append("V:" + (getOverflowFlag() ? '1' : '0') + ", ");
sb.append("B:" + (getBreakFlag() ? '1' : '0') + ", ");
sb.append("D:" + (getDecimalModeFlag() ? '1' : '0') + ", ");
sb.append("I:" + (getIrqDisableFlag() ? '1' : '0') + ", ");
sb.append("Z:" + (getZeroFlag() ? '1' : '0') + ", ");
sb.append("C:" + (getCarryFlag() ? '1' : '0') + ")");
return sb.toString();
}
/**
* 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("%-14s", opcode));
sb.append("A=" + String.format("$%02X", a) + " ");
sb.append("X=" + String.format("$%02X", x) + " ");
sb.append("Y=" + String.format("$%02X", y) + " ");
sb.append("PC=" + String.format("$%04X", pc)+ " ");
sb.append("P=" + statusRegisterString());
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.
*/
protected void incProgramCounter() {
if (pc == 0xffff) {
pc = 0;
} else {
++pc;
}
}
}
@@ -0,0 +1,37 @@
package com.loomcom.symon;
import com.loomcom.symon.InstructionTable.Mode;
public class CpuUtils {
/**
* Given two bytes, return an address.
*/
public static int address(int lowByte, int hiByte) {
return ((hiByte<<8)|lowByte);
}
/**
* Given an opcode and its operands, return a formatted name.
*
* @param opcode
* @param operands
* @return
*/
public static String opcode(int opcode, int op1, int op2) {
String opcodeName = Cpu.opcodeNames[opcode];
if (opcodeName == null) { return "???"; }
StringBuffer sb = new StringBuffer(opcodeName);
switch (Cpu.instructionModes[opcode]) {
case ABS:
sb.append(String.format(" $%04X", address(op1, op2)));
break;
case IMM:
sb.append(String.format(" #$%02X", op1));
}
return sb.toString();
}
}
@@ -0,0 +1,249 @@
package com.loomcom.symon;
public interface InstructionTable {
/**
* Enumeration of Addressing Modes.
*/
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.
/**
* Instruction opcode names.
*/
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
};
/**
* Instruction addressing modes.
*/
public static final Mode[] instructionModes = {
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
};
/**
* Size, in bytes, required for each instruction.
*/
public static final int[] instructionSizes = {
1, 2, 0, 0, 0, 2, 2, 0, 1, 2, 1, 0, 0, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0,
3, 2, 0, 0, 2, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0,
1, 2, 0, 0, 0, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0,
1, 2, 0, 0, 0, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0,
2, 2, 0, 0, 2, 2, 2, 0, 1, 0, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 2, 2, 2, 0, 1, 3, 1, 0, 0, 3, 0, 0,
2, 2, 2, 0, 2, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
0, 2, 0, 0, 2, 2, 2, 0, 1, 3, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 2, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0,
2, 2, 0, 0, 2, 2, 2, 0, 1, 2, 1, 0, 3, 3, 3, 0,
2, 2, 0, 0, 0, 2, 2, 0, 1, 3, 0, 0, 0, 3, 3, 0
};
/**
* Number of clock cycles required for each instruction
*/
public static final int[] instructionClocks = {
7, 6, 0, 0, 0, 3, 5, 0, 3, 2, 2, 0, 0, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0,
6, 6, 0, 0, 3, 3, 5, 0, 4, 2, 2, 0, 4, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0,
6, 6, 0, 0, 0, 3, 5, 0, 3, 2, 2, 0, 3, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0,
6, 6, 0, 0, 0, 3, 5, 0, 4, 2, 2, 0, 5, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0,
2, 6, 0, 0, 3, 3, 3, 0, 2, 0, 2, 0, 4, 4, 4, 0,
2, 6, 0, 0, 4, 4, 4, 0, 2, 5, 2, 0, 0, 5, 0, 0,
2, 6, 2, 0, 3, 3, 3, 0, 2, 2, 2, 0, 4, 4, 4, 0,
0, 5, 0, 0, 4, 4, 4, 0, 2, 4, 2, 0, 4, 4, 4, 0,
2, 6, 0, 0, 3, 3, 5, 0, 2, 2, 2, 0, 4, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0,
2, 6, 0, 0, 3, 3, 5, 0, 2, 2, 2, 0, 4, 4, 6, 0,
2, 5, 0, 0, 0, 4, 6, 0, 2, 4, 0, 0, 0, 4, 7, 0
};
}
@@ -0,0 +1,87 @@
package com.loomcom.symon;
import com.loomcom.symon.exceptions.*;
/**
* MemoryRange is a simple container class representing a literal
* range of memory, with a staraddress, and an end address. It has
* guards against creating impossible memory ranges, and implements
* some useful methods for checking address inclusion and range
* overlaps.
*/
public class MemoryRange implements Comparable<MemoryRange> {
/** The starting address of the memory range. */
public int startAddress;
/** The ending address of the memory range. */
public int endAddress;
public MemoryRange(int startAddress, int endAddress)
throws MemoryRangeException {
if (startAddress < 0 || endAddress < 0) {
throw new MemoryRangeException("Addresses cannot be less than 0.");
}
if (startAddress >= endAddress) {
throw new MemoryRangeException("End address must be greater " +
"than start address.");
}
this.startAddress = startAddress;
this.endAddress = endAddress;
}
/**
* @returns the starting address.
*/
public int startAddress() {
return startAddress;
}
/**
* @returns the ending address.
*/
public int endAddress() {
return endAddress;
}
/**
* Checks for address inclusion in the range.
*
* @returns true if the address is included within this range,
* false otherwise.
*/
public boolean includes(int address) {
return (address <= endAddress &&
address >= startAddress);
}
/**
* Checks for overlapping memory ranges.
*
* @returns true if this range overlaps in any way with the other.
*/
public boolean overlaps(MemoryRange other) {
return (this.includes(other.startAddress()) ||
other.includes(this.startAddress()));
}
// Implementation of Comparable interface
public int compareTo(MemoryRange other) {
if (other == null) {
throw new NullPointerException("Cannot compare to null.");
}
if (this == other) {
return 0;
}
Integer thisStartAddr = new Integer(this.startAddress());
Integer thatStartAddr = new Integer(other.startAddress());
return thisStartAddr.compareTo(thatStartAddr);
}
public String toString() {
StringBuffer desc = new StringBuffer("@");
desc.append(String.format("0x%04x", startAddress));
desc.append("-");
desc.append(String.format("0x%04x", endAddress));
return desc.toString();
}
}
@@ -0,0 +1,160 @@
/**
* A simple profiler, for debugging the simulator.
* It is safe to ignore me.
*/
package com.loomcom.symon;
import com.loomcom.symon.devices.*;
import com.loomcom.symon.exceptions.*;
public class Profiler implements InstructionTable {
public static void main(String[] args) {
// new Profiler().profileMemoryReads();
// new Profiler().dumpOpCodes();
new Profiler().profileProgram();
}
public void dumpOpCodes() {
for (int i = 0; i < 0x100; i++) {
String name = opcodeNames[i];
Mode mode = instructionModes[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 profileProgram() {
Bus bus = new Bus(0, 65535);
Cpu cpu = new Cpu();
bus.addCpu(cpu);
try {
bus.addDevice(new Memory(0x0000, 0x10000));
} catch (MemoryRangeException ex) {
System.err.println("Memory Range Exception! " + ex.getMessage());
return;
}
// Start at 0x0300
bus.write(0xfffc, 0x00);
bus.write(0xfffd, 0x03);
// The program to run, in an infinite loop.
bus.write(0x0300, 0xa9); // LDA #$FF
bus.write(0x0301, 0xff);
bus.write(0x0302, 0xea); // NOP
bus.write(0x0303, 0xea); // NOP
bus.write(0x0304, 0xa0); // LDY #$1A
bus.write(0x0305, 0x1a);
bus.write(0x0306, 0xea); // NOP
bus.write(0x0307, 0xea); // NOP
bus.write(0x0308, 0xa2); // LDX #$03
bus.write(0x0309, 0x03);
bus.write(0x030a, 0xa9); // LDA #$00
bus.write(0x030b, 0x00);
bus.write(0x030c, 0xa2); // LDX #$00
bus.write(0x030d, 0x00);
bus.write(0x030e, 0xa0); // LDY #$00
bus.write(0x030f, 0x00);
bus.write(0x0310, 0x4c); // JMP #$0300
bus.write(0x0311, 0x00);
bus.write(0x0312, 0x03);
long sum = 0;
// The number of times to run the program
long iters = 1000;
// The number of steps to take when running the program
long steps = 100000;
for (int i = 0; i < iters; i++) {
long startTime = System.nanoTime();
// Reset the CPU (does not clear memory)
cpu.reset();
for (int j = 0; j < steps; j++) {
cpu.step();
}
long endTime = System.nanoTime();
long diff = endTime - startTime;
sum += diff;
}
long average = sum / iters;
long totalSteps = steps * iters;
long avgStep = sum / totalSteps;
System.out.println("Total instructions executed: " +
String.format("%,d", totalSteps));
System.out.println("Total time taken: " +
String.format("%,d us", sum / 1000));
System.out.println("Average time per step: " +
avgStep + " ns ");
}
public void profileMemoryReads() {
// Create a bus.
Bus b = new Bus(0, 65535);
try {
// Create eight devices, each 8KB, to fill the bus.
b.addDevice(new Memory(0x0000, 0x2000)); // 8KB @ $0000-$1fff
b.addDevice(new Memory(0x2000, 0x2000)); // 8KB @ $2000-$3fff
b.addDevice(new Memory(0x4000, 0x2000)); // 8KB @ $4000-$5fff
b.addDevice(new Memory(0x6000, 0x2000)); // 8KB @ $6000-$7fff
b.addDevice(new Memory(0x8000, 0x2000)); // 8KB @ $8000-$9fff
b.addDevice(new Memory(0xa000, 0x2000)); // 8KB @ $a000-$bfff
b.addDevice(new Memory(0xc000, 0x2000)); // 8KB @ $c000-$dfff
b.addDevice(new Memory(0xe000, 0x2000)); // 8KB @ $e000-$ffff
} catch (MemoryRangeException ex) {
System.err.println("Memory Range Exception! " + ex.getMessage());
return;
}
// Read memory
long sum = 0;
long average = 0;
long iters = 500;
for (int i = 0; i < iters; i++) {
long startTime = System.nanoTime();
// Read and assign to a buffer
int buf = 0;
for (int j = 0; j < 0xffff; j++) {
buf = b.read(j);
if (buf != 0xff) {
System.out.println("WARNING! MEMORY SHOULD HAVE " +
"BEEN $FF, WAS: " + buf);
System.exit(0);
}
}
long endTime = System.nanoTime();
long diff = endTime - startTime;
sum += diff;
average = sum / (i + 1);
}
System.out.println("Average time to read 64KB: " + average +
" ns (" + (average / 1000) + " us)");
System.out.println("Average time to read one byte: " +
sum / (64 * 1024 * iters) + " ns");
}
}
@@ -0,0 +1,109 @@
package com.loomcom.symon;
import com.loomcom.symon.devices.*;
import com.loomcom.symon.exceptions.*;
/**
* Main control class for the J6502 Simulator.
*/
public class Simulator {
/**
* Command-line parser used by this simulator.
*/
private CommandParser parser;
/**
* The CPU itself.
*/
private Cpu cpu;
/**
* The Bus responsible for routing memory read/write requests to the
* correct IO devices.
*/
private Bus bus;
public Simulator() throws MemoryRangeException {
cpu = new Cpu();
bus = new Bus(0x0000, 0xffff);
bus.addCpu(cpu);
bus.addDevice(new Memory(0x0000, 0x10000));
parser = new CommandParser(System.in, System.out, this);
}
public void run() {
parser.run();
}
public void step() {
}
public int read(int address) {
return 0;
}
public void write(int address, int value) {
}
/**
* A test method.
*/
public void runTest() {
// Start at 0x0300
bus.write(0xfffc, 0x00);
bus.write(0xfffd, 0x03);
bus.write(0x0300, 0xa9); // LDA #$FF
bus.write(0x0301, 0xff);
bus.write(0x0302, 0xea); // NOP
bus.write(0x0303, 0xea); // NOP
bus.write(0x0304, 0xea); // NOP
bus.write(0x0305, 0xa0); // LDY #$1A
bus.write(0x0306, 0x1a);
bus.write(0x0307, 0xea); // NOP
bus.write(0x0308, 0xea); // NOP
bus.write(0x0309, 0xa2); // LDX #$90
bus.write(0x030a, 0x90);
bus.write(0x030b, 0xea); // NOP
bus.write(0x030c, 0xea); // NOP
bus.write(0x030d, 0xea); // NOP
bus.write(0x030e, 0xa2); // LDX #$02
bus.write(0x030f, 0x02);
bus.write(0x0310, 0xa9); // LDA #$00
bus.write(0x0311, 0x00);
bus.write(0x0312, 0xa2); // LDX #$00
bus.write(0x0313, 0x00);
bus.write(0x0314, 0xa0); // LDY #$00
bus.write(0x0315, 0x00);
bus.write(0x0316, 0x4c); // JMP #$0300
bus.write(0x0317, 0x00);
bus.write(0x0318, 0x03);
cpu.reset();
for (int i = 0; i <= 40; i++) {
cpu.step();
System.out.println(cpu.toString());
}
}
/**
* Main simulator routine.
*/
public static void main(String[] args) {
try {
new Simulator().runTest();
} catch (MemoryRangeException ex) {
System.err.println("Error: " + ex.toString());
}
}
}
@@ -0,0 +1,73 @@
package com.loomcom.symon.devices;
import com.loomcom.symon.*;
import com.loomcom.symon.exceptions.*;
/**
* A memory-mapped IO Device.
*/
public abstract class Device implements Comparable<Device> {
/** The memory range for this device. */
private MemoryRange memoryRange;
/** The name of the device. */
private String name;
/** Reference to the bus where this Device is attached. */
private Bus bus;
public Device(int address, int size, String name)
throws MemoryRangeException {
this.memoryRange = new MemoryRange(address, address + size - 1);
this.name = name;
}
public Device(int address, int size) throws MemoryRangeException {
this(address, size, null);
}
/* Methods required to be implemented by inheriting classes. */
public abstract void write(int address, int data);
public abstract int read(int address);
public abstract String toString();
public void setBus(Bus bus) {
this.bus = bus;
}
public MemoryRange getMemoryRange() {
return memoryRange;
}
public int endAddress() {
return memoryRange.endAddress();
}
public int startAddress() {
return memoryRange.startAddress();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* Compares two devices. The sort order is defined by the sort
* order of the device's memory ranges.
*/
public int compareTo(Device other) {
if (other == null) {
throw new NullPointerException("Cannot compare to null.");
}
if (this == other) {
return 0;
}
return getMemoryRange().compareTo(other.getMemoryRange());
}
}
@@ -0,0 +1,39 @@
package com.loomcom.symon.devices;
import java.util.*;
import com.loomcom.symon.*;
import com.loomcom.symon.exceptions.*;
public class Memory extends Device {
private boolean readOnly;
private int[] mem;
public Memory(int address, int size, boolean readOnly)
throws MemoryRangeException {
super(address, size, "RW Memory");
this.readOnly = readOnly;
this.mem = new int[size];
// Initialize all locations to 0xff
Arrays.fill(this.mem, 0xff);
}
public Memory(int address, int size)
throws MemoryRangeException {
this(address, size, false);
}
public void write(int address, int data) {
this.mem[address] = data;
}
public int read(int address) {
return this.mem[address];
}
public String toString() {
return "Memory: " + getMemoryRange().toString();
}
}
@@ -0,0 +1,11 @@
package com.loomcom.symon.exceptions;
/**
* Exception that will be thrown if access to memory or IO cannot be
* accessed.
*/
public class MemoryAccessException extends Exception {
public MemoryAccessException(String msg) {
super(msg);
}
}
@@ -0,0 +1,10 @@
package com.loomcom.symon.exceptions;
/**
* Exception that will be thrown if devices conflict in the IO map.
*/
public class MemoryRangeException extends Exception {
public MemoryRangeException(String msg) {
super(msg);
}
}