diff --git a/.gitignore b/.gitignore index 6c3b3ba..5d06257 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *~ *# target +.classpath +.project +.settings diff --git a/src/main/java/com/loomcom/j6502/AddressDecoder.java b/src/main/java/com/loomcom/j6502/AddressDecoder.java index 40a9d78..dd959ca 100644 --- a/src/main/java/com/loomcom/j6502/AddressDecoder.java +++ b/src/main/java/com/loomcom/j6502/AddressDecoder.java @@ -3,7 +3,6 @@ package com.loomcom.j6502; import java.util.Map; public class AddressDecoder { - /** * Map of memory ranges to IO devices. */ @@ -11,10 +10,11 @@ public class AddressDecoder { public AddressDecoder() {} - public void addDevice(Device d) - throws MemoryConflictException { - // Make sure there's no memory overlap. - // Add the device to the map. + public void addDevice(Device d) + throws MemoryConflictException { + // Make sure there's no memory overlap. + // Add the device to the map. + m_ioMap.put(d.getMemoryRange(), d); } /** @@ -23,7 +23,7 @@ public class AddressDecoder { * device. */ public boolean isComplete() { - return true; + return true; } /** @@ -31,9 +31,8 @@ public class AddressDecoder { * are gaps between IO devices. */ public boolean isSparse() { - return !isComplete(); + return !isComplete(); } - } class MemoryRange { diff --git a/src/main/java/com/loomcom/j6502/Cpu.java b/src/main/java/com/loomcom/j6502/Cpu.java index 42d623f..fe47243 100644 --- a/src/main/java/com/loomcom/j6502/Cpu.java +++ b/src/main/java/com/loomcom/j6502/Cpu.java @@ -5,35 +5,33 @@ package com.loomcom.j6502; */ public class Cpu { - /** - * The Address Decoder responsible for routing memory - * read/write requests to the correct IO devices. Any - * simulated device can be - */ - AddressDecoder m_adc; - /** * The Program Counter. */ - int m_pc; + private int m_pc; /** * The system stack pointer. */ - int m_sp; - + private int m_sp; + /** - * The internal + * Reference to the simulator */ + private Simulator m_sim; - public Cpu() { - reset(); + public Cpu(Simulator sim) { + reset(); + this.m_sim = sim; } /** * Reset the CPU to known initial values. */ public void reset() { + m_sp = 0x01ff; + /* locations fffc and fffd hold the reset vector address */ + m_pc = 0xfffc; } /** @@ -47,6 +45,29 @@ public class Cpu { */ public void nmiInterrupt() { } + + /** + * @return An address specified by the two bytes immediately following the + * Program Counter. + */ + private int readAddress() { + return readAddress(m_pc); + } + + /** + * Read the two bytes located at addr and addr + 1, + * and return the address held there. + * + * @param address + * @return The address specified in the two bytes at location addr + */ + private int readAddress(int address) { + return (m_sim.read(address)<<8 & m_sim.read(address+1)); + } + + public Simulator getSimulator() { + return m_sim; + } } diff --git a/src/main/java/com/loomcom/j6502/Device.java b/src/main/java/com/loomcom/j6502/Device.java index 8ba862f..35c3722 100644 --- a/src/main/java/com/loomcom/j6502/Device.java +++ b/src/main/java/com/loomcom/j6502/Device.java @@ -9,20 +9,32 @@ public abstract class Device { /** The memory range for this device. */ private MemoryRange m_memoryRange; - /** Referece to the CPU, for interrupts. */ + /** Reference to the CPU, for interrupts. */ private Cpu m_cpu; public Device(MemoryRange range, Cpu cpu) { - m_memoryRange = range; - m_cpu = cpu; + m_memoryRange = range; + m_cpu = cpu; + } + + public MemoryRange getMemoryRange() { + return m_memoryRange; } - public void generateInterrupt() { - m_cpu.interrupt(); + public int getEndAddress() { + return m_memoryRange.getEndAddress(); + } + + public int getStartAddress() { + return m_memoryRange.getStartAddress(); + } + + public void generateInterrupt() { + m_cpu.interrupt(); } public void generateNonMaskableInterrupt() { - m_cpu.nmiInterrupt(); + m_cpu.nmiInterrupt(); } } \ No newline at end of file diff --git a/src/main/java/com/loomcom/j6502/Simulator.java b/src/main/java/com/loomcom/j6502/Simulator.java index 52fde5a..c0732c8 100644 --- a/src/main/java/com/loomcom/j6502/Simulator.java +++ b/src/main/java/com/loomcom/j6502/Simulator.java @@ -7,24 +7,42 @@ import java.io.IOException; */ public class Simulator { - CommandParser m_parser; + /** + * Command-line parser used by this simulator. + */ + CommandParser m_parser; + + /** + * The CPU itself. + */ Cpu m_cpu; + + /** + * The Address Decoder responsible for routing memory + * read/write requests to the correct IO devices. + */ + AddressDecoder m_adc; public Simulator() { - m_cpu = new Cpu(); - m_parser = new CommandParser(System.in, System.out, this); + m_cpu = new Cpu(this); + m_parser = new CommandParser(System.in, System.out, this); } public void run() { - m_parser.run(); + m_parser.run(); } public static void main(String[] args) { - new Simulator().run(); + new Simulator().run(); } public void step() { - } + public int read(int address) { + return 0; + } + + public void write(int address, int value) { + } } \ No newline at end of file diff --git a/src/test/java/com/loomcom/j6502/CpuTest.java b/src/test/java/com/loomcom/j6502/CpuTest.java new file mode 100644 index 0000000..811f606 --- /dev/null +++ b/src/test/java/com/loomcom/j6502/CpuTest.java @@ -0,0 +1,78 @@ +/** + * + */ +package com.loomcom.j6502; + +import static org.junit.Assert.*; + +import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + +/** + * @author sethm + * + */ +public class CpuTest { + + @Test + public void testCpu() { + Cpu cpu = new Cpu(new Simulator()); + assertNotNull(cpu); + } + + @Test + public void testReset() { + fail("Not yet implemented"); + } + + @Test + public void testInterrupt() { + fail("Not yet implemented"); + } + + @Test + public void testNmiInterrupt() { + fail("Not yet implemented"); + } + + @Test + public void testGetSimulator() { + Simulator sim = new Simulator(); + Cpu cpu = new Cpu(sim); + assertEquals(sim, cpu.getSimulator()); + } + +} + +class MockSimulator extends Simulator { + public int step; + public boolean hasRun = false; + public Map memory; + + @Override + public int read(int address) { + Integer val = memory.get(new Integer(address)); + if (val == null) { + throw new NullPointerException("Read from a non-existent memory location"); + } else { + return val.intValue(); + } + } + + @Override + public void run() { + hasRun = true; + } + + @Override + public void step() { + step++; + } + + @Override + public void write(int address, int value) { + // TODO Auto-generated method stub + super.write(address, value); + } +}