Unit testing. MemoryRange work.
This commit is contained in:
1 parent
541e7c609c
commit
5bbd72d44e
7 files changed
+289
-88
No files matched your search
@@ -1,20 +1,32 @@
|
||||
package com.loomcom.lm6502;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
public class AddressDecoder {
|
||||
/**
|
||||
* Map of memory ranges to IO devices.
|
||||
*/
|
||||
private Map<MemoryRange, Device> m_ioMap;
|
||||
|
||||
public AddressDecoder() {}
|
||||
public static final int MEMORY_BOTTOM = 0x0000;
|
||||
public static final int MEMORY_TOP = 0xFFFF;
|
||||
|
||||
/**
|
||||
* Ordered map of memory ranges to IO devices.
|
||||
*/
|
||||
private SortedMap<MemoryRange, Device> ioMap;
|
||||
|
||||
public AddressDecoder() {
|
||||
ioMap = new TreeMap();
|
||||
}
|
||||
|
||||
public void addDevice(Device d)
|
||||
throws MemoryConflictException {
|
||||
throws MemoryRangeException {
|
||||
// Make sure there's no memory overlap.
|
||||
for (MemoryRange memRange : ioMap.keySet()) {
|
||||
if (d.getMemoryRange().overlapsWith(memRange)) {
|
||||
throw new MemoryRangeException("The device being added overlaps with an existing device.");
|
||||
}
|
||||
}
|
||||
|
||||
// Add the device to the map.
|
||||
m_ioMap.put(d.getMemoryRange(), d);
|
||||
ioMap.put(d.getMemoryRange(), d);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -23,49 +35,40 @@ public class AddressDecoder {
|
||||
* device.
|
||||
*/
|
||||
public boolean isComplete() {
|
||||
// Emtpy maps cannot be complete.
|
||||
if (ioMap.isEmpty()) { return false; }
|
||||
|
||||
// Loop over devices and ensure they are contiguous.
|
||||
MemoryRange prev = null;
|
||||
int i = 0;
|
||||
int size = ioMap.size();
|
||||
for (Map.Entry e : ioMap.entrySet()) {
|
||||
MemoryRange cur = (MemoryRange)e.getKey();
|
||||
if (i == 0) {
|
||||
// If the first entry doesn't start at MEMORY_BOTTOM, return false.
|
||||
if (cur.getStartAddress() != MEMORY_BOTTOM) { return false; }
|
||||
} else if (i < size - 1) {
|
||||
// Otherwise, compare previous map's end against this map's
|
||||
// top. They must be adjacent!
|
||||
if (cur.getStartAddress() - 1 != prev.getEndAddress()) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// If the last entry doesn't end at MEMORY_TOP, return false;
|
||||
if (cur.getEndAddress() != MEMORY_TOP) { return false; }
|
||||
}
|
||||
i++;
|
||||
prev = cur;
|
||||
}
|
||||
|
||||
// Must be complete.
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the memory map is 'sparse', i.e., there
|
||||
* are gaps between IO devices.
|
||||
* Returns true if there are any gap in the memory map.
|
||||
*/
|
||||
public boolean isSparse() {
|
||||
public boolean hasGaps() {
|
||||
return !isComplete();
|
||||
}
|
||||
}
|
||||
|
||||
class MemoryRange {
|
||||
public int m_startAddress;
|
||||
public int m_endAddress;
|
||||
|
||||
/**
|
||||
* @returns true if the address is included within this range,
|
||||
* false otherwise.
|
||||
*/
|
||||
public boolean includes(int address) {
|
||||
return (address <= m_endAddress &&
|
||||
address >= m_startAddress);
|
||||
}
|
||||
|
||||
public void setStartAddress(int startAddress) {
|
||||
m_startAddress = startAddress;
|
||||
}
|
||||
|
||||
public void setEndAddress(int endAddress) {
|
||||
m_endAddress = endAddress;
|
||||
}
|
||||
|
||||
public int getStartAddress() {
|
||||
return m_startAddress;
|
||||
}
|
||||
|
||||
public int getEndAddress() {
|
||||
return m_endAddress;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Excption that will be thrown if devices conflict in the IO map.
|
||||
*/
|
||||
class MemoryConflictException extends Exception {}
|
||||
}
|
||||
@@ -4,13 +4,14 @@ import java.io.*;
|
||||
|
||||
public class CommandParser {
|
||||
|
||||
private BufferedReader m_in;
|
||||
private BufferedWriter m_out;
|
||||
private Simulator m_simulator;
|
||||
private BufferedReader in;
|
||||
private BufferedWriter out;
|
||||
private Simulator simulator;
|
||||
|
||||
public CommandParser(InputStream in, OutputStream out, Simulator s) {
|
||||
m_in = new BufferedReader(new InputStreamReader(in));
|
||||
m_out = new BufferedWriter(new OutputStreamWriter(out));
|
||||
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() {
|
||||
@@ -45,20 +46,20 @@ public class CommandParser {
|
||||
}
|
||||
|
||||
private void prompt() throws IOException {
|
||||
m_out.write("j6502> ");
|
||||
m_out.flush();
|
||||
out.write("j6502> ");
|
||||
out.flush();
|
||||
}
|
||||
|
||||
private String readLine() throws IOException {
|
||||
String line = m_in.readLine();
|
||||
String line = in.readLine();
|
||||
if (line == null) { return null; }
|
||||
return line.trim();
|
||||
}
|
||||
|
||||
private void writeLine(String line) throws IOException {
|
||||
m_out.write(line);
|
||||
m_out.newLine();
|
||||
m_out.flush();
|
||||
out.write(line);
|
||||
out.newLine();
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,33 +5,22 @@ package com.loomcom.lm6502;
|
||||
*/
|
||||
public class Cpu {
|
||||
|
||||
/**
|
||||
* The Program Counter.
|
||||
*/
|
||||
private int m_pc;
|
||||
|
||||
/**
|
||||
* The system stack pointer.
|
||||
*/
|
||||
private int m_sp;
|
||||
|
||||
/**
|
||||
* Reference to the simulator
|
||||
*/
|
||||
private Simulator m_sim;
|
||||
private int pc;
|
||||
private int sp;
|
||||
private Simulator sim;
|
||||
|
||||
public Cpu(Simulator sim) {
|
||||
reset();
|
||||
this.m_sim = sim;
|
||||
this.sim = sim;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the CPU to known initial values.
|
||||
*/
|
||||
public void reset() {
|
||||
m_sp = 0x01ff;
|
||||
sp = 0x01ff;
|
||||
/* locations fffc and fffd hold the reset vector address */
|
||||
m_pc = 0xfffc;
|
||||
pc = 0xfffc;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +40,7 @@ public class Cpu {
|
||||
* Program Counter.
|
||||
*/
|
||||
private int readAddress() {
|
||||
return readAddress(m_pc);
|
||||
return readAddress(pc);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,11 +51,11 @@ public class Cpu {
|
||||
* @return The address specified in the two bytes at location <tt>addr</tt>
|
||||
*/
|
||||
private int readAddress(int address) {
|
||||
return (m_sim.read(address)<<8 & m_sim.read(address+1));
|
||||
return (sim.read(address)<<8 & sim.read(address+1));
|
||||
}
|
||||
|
||||
public Simulator getSimulator() {
|
||||
return m_sim;
|
||||
return sim;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,34 +7,34 @@
|
||||
public abstract class Device {
|
||||
|
||||
/** The memory range for this device. */
|
||||
private MemoryRange m_memoryRange;
|
||||
private MemoryRange memoryRange;
|
||||
|
||||
/** Reference to the CPU, for interrupts. */
|
||||
private Cpu m_cpu;
|
||||
private Cpu cpu;
|
||||
|
||||
public Device(MemoryRange range, Cpu cpu) {
|
||||
m_memoryRange = range;
|
||||
m_cpu = cpu;
|
||||
this.memoryRange = range;
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
public MemoryRange getMemoryRange() {
|
||||
return m_memoryRange;
|
||||
return memoryRange;
|
||||
}
|
||||
|
||||
public int getEndAddress() {
|
||||
return m_memoryRange.getEndAddress();
|
||||
return memoryRange.getEndAddress();
|
||||
}
|
||||
|
||||
public int getStartAddress() {
|
||||
return m_memoryRange.getStartAddress();
|
||||
return memoryRange.getStartAddress();
|
||||
}
|
||||
|
||||
public void generateInterrupt() {
|
||||
m_cpu.interrupt();
|
||||
cpu.interrupt();
|
||||
}
|
||||
|
||||
public void generateNonMaskableInterrupt() {
|
||||
m_cpu.nmiInterrupt();
|
||||
cpu.nmiInterrupt();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.loomcom.lm6502;
|
||||
|
||||
public class MemoryRange implements Comparable<MemoryRange> {
|
||||
public int startAddress;
|
||||
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;
|
||||
}
|
||||
|
||||
public int getStartAddress() {
|
||||
return startAddress;
|
||||
}
|
||||
|
||||
public int getEndAddress() {
|
||||
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 overlapsWith(MemoryRange other) {
|
||||
return ((this.getEndAddress() >= other.getStartAddress() &&
|
||||
this.getEndAddress() <= other.getEndAddress()) ||
|
||||
(other.getEndAddress() >= this.getStartAddress() &&
|
||||
other.getEndAddress() <= this.getEndAddress()) ||
|
||||
(this.getStartAddress() <= other.getStartAddress() &&
|
||||
this.getEndAddress() >= other.getEndAddress()) ||
|
||||
(other.getStartAddress() <= this.getStartAddress() &&
|
||||
other.getEndAddress() >= this.getEndAddress()));
|
||||
}
|
||||
|
||||
// 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.getStartAddress());
|
||||
Integer thatStartAddr = new Integer(other.getStartAddress());
|
||||
return thisStartAddr.compareTo(thatStartAddr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.loomcom.lm6502;
|
||||
|
||||
/**
|
||||
* Exception that will be thrown if devices conflict in the IO map.
|
||||
*/
|
||||
class MemoryRangeException extends Exception {
|
||||
public MemoryRangeException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user