From d1a05aeb2b9b0f46d2afb3dd611034a92db43c76 Mon Sep 17 00:00:00 2001 From: Max840 Date: Mon, 23 Dec 2013 23:31:51 -0500 Subject: [PATCH 1/4] Devices use start/end address --- .../java/com/loomcom/symon/Simulator.java | 8 ++++---- .../java/com/loomcom/symon/devices/Acia.java | 2 +- .../com/loomcom/symon/devices/Device.java | 8 ++++---- .../com/loomcom/symon/devices/Memory.java | 20 +++++++++---------- .../java/com/loomcom/symon/devices/Via.java | 2 +- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/loomcom/symon/Simulator.java b/src/main/java/com/loomcom/symon/Simulator.java index 3f5022d..41e09f1 100644 --- a/src/main/java/com/loomcom/symon/Simulator.java +++ b/src/main/java/com/loomcom/symon/Simulator.java @@ -152,7 +152,7 @@ public class Simulator { this.via = new Via(VIA_BASE); this.bus = new Bus(BUS_BOTTOM, BUS_TOP); this.cpu = new Cpu(); - this.ram = new Memory(MEMORY_BASE, MEMORY_SIZE, false); + this.ram = new Memory(MEMORY_BASE, MEMORY_BASE + MEMORY_SIZE - 1, false); bus.addCpu(cpu); bus.addDevice(ram); @@ -163,11 +163,11 @@ public class Simulator { File romImage = new File("rom.bin"); if (romImage.canRead()) { logger.info("Loading ROM image from file " + romImage); - this.rom = Memory.makeROM(ROM_BASE, ROM_SIZE, romImage); + this.rom = Memory.makeROM(ROM_BASE, ROM_BASE + ROM_SIZE - 1, romImage); } else { logger.info("Default ROM file " + romImage + " not found, loading empty R/W memory image."); - this.rom = Memory.makeRAM(ROM_BASE, ROM_SIZE); + this.rom = Memory.makeRAM(ROM_BASE, ROM_BASE + ROM_SIZE - 1); } bus.addDevice(rom); } @@ -568,7 +568,7 @@ public class Simulator { bus.removeDevice(rom); } // Load the new ROM image - rom = Memory.makeROM(ROM_BASE, ROM_SIZE, romFile); + rom = Memory.makeROM(ROM_BASE, ROM_BASE + ROM_SIZE - 1, romFile); bus.addDevice(rom); // Now, reset diff --git a/src/main/java/com/loomcom/symon/devices/Acia.java b/src/main/java/com/loomcom/symon/devices/Acia.java index c51f43d..95e624a 100644 --- a/src/main/java/com/loomcom/symon/devices/Acia.java +++ b/src/main/java/com/loomcom/symon/devices/Acia.java @@ -75,7 +75,7 @@ public class Acia extends Device { private boolean txEmpty = true; public Acia(int address) throws MemoryRangeException { - super(address, ACIA_SIZE, "ACIA"); + super(address, address + ACIA_SIZE - 1, "ACIA"); this.baseAddress = address; } diff --git a/src/main/java/com/loomcom/symon/devices/Device.java b/src/main/java/com/loomcom/symon/devices/Device.java index 7e995da..7ded396 100644 --- a/src/main/java/com/loomcom/symon/devices/Device.java +++ b/src/main/java/com/loomcom/symon/devices/Device.java @@ -41,14 +41,14 @@ public abstract class Device implements Comparable { /** Reference to the bus where this Device is attached. */ private Bus bus; - public Device(int address, int size, String name) + public Device(int startAddress, int endAddress, String name) throws MemoryRangeException { - this.memoryRange = new MemoryRange(address, address + size - 1); + this.memoryRange = new MemoryRange(startAddress, endAddress); this.name = name; } - public Device(int address, int size) throws MemoryRangeException { - this(address, size, null); + public Device(int startAddress, int endAddress) throws MemoryRangeException { + this(startAddress, endAddress, null); } /* Methods required to be implemented by inheriting classes. */ diff --git a/src/main/java/com/loomcom/symon/devices/Memory.java b/src/main/java/com/loomcom/symon/devices/Memory.java index 8272652..f62b558 100644 --- a/src/main/java/com/loomcom/symon/devices/Memory.java +++ b/src/main/java/com/loomcom/symon/devices/Memory.java @@ -38,26 +38,26 @@ public class Memory extends Device { /* Initialize all locations to 0x00 (BRK) */ private static final int DEFAULT_FILL = 0x00; - public Memory(int address, int size, boolean readOnly) + public Memory(int startAddress, int endAddress, boolean readOnly) throws MemoryRangeException { - super(address, size, (readOnly ? "RO Memory" : "RW Memory")); + super(startAddress, endAddress, (readOnly ? "RO Memory" : "RW Memory")); this.readOnly = readOnly; - this.mem = new int[size]; + this.mem = new int[endAddress - startAddress + 1]; this.fill(DEFAULT_FILL); } - public Memory(int address, int size) throws MemoryRangeException { - this(address, size, false); + public Memory(int startAddress, int endAddress) throws MemoryRangeException { + this(startAddress, endAddress, false); } - public static Memory makeROM(int address, int size, File f) throws MemoryRangeException, IOException { - Memory memory = new Memory(address, size, true); + public static Memory makeROM(int startAddress, int endAddress, File f) throws MemoryRangeException, IOException { + Memory memory = new Memory(startAddress, endAddress, true); memory.loadFromFile(f); return memory; } - public static Memory makeRAM(int address, int size) throws MemoryRangeException { - Memory memory = new Memory(address, size, false); + public static Memory makeRAM(int startAddress, int endAddress) throws MemoryRangeException { + Memory memory = new Memory(startAddress, endAddress, false); return memory; } @@ -109,4 +109,4 @@ public class Memory extends Device { return "Memory: " + getMemoryRange().toString(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/loomcom/symon/devices/Via.java b/src/main/java/com/loomcom/symon/devices/Via.java index b1013cb..7209535 100644 --- a/src/main/java/com/loomcom/symon/devices/Via.java +++ b/src/main/java/com/loomcom/symon/devices/Via.java @@ -47,7 +47,7 @@ public class Via extends Device { private static final int ORA_H = 15; public Via(int address) throws MemoryRangeException { - super(address, VIA_SIZE, "VIA"); + super(address, address + VIA_SIZE - 1, "VIA"); } @Override From 1c9c742477ede2240ce32cf5ab1112367b2c1f5e Mon Sep 17 00:00:00 2001 From: Max840 Date: Mon, 23 Dec 2013 23:38:38 -0500 Subject: [PATCH 2/4] Unit test updated with devices' start/end address --- src/test/java/com/loomcom/symon/BusTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/loomcom/symon/BusTest.java b/src/test/java/com/loomcom/symon/BusTest.java index b6a4e6e..f4aedd0 100644 --- a/src/test/java/com/loomcom/symon/BusTest.java +++ b/src/test/java/com/loomcom/symon/BusTest.java @@ -47,8 +47,8 @@ public class BusTest extends TestCase { } public void testAddDevice() throws MemoryRangeException { - Device memory = new Memory(0x0000, 0x0100, true); - Device rom = new Memory(0x0100, 0x0200, false); + Device memory = new Memory(0x0000, 0x00ff, true); + Device rom = new Memory(0x0100, 0x02ff, false); Bus b = new Bus(0x0000, 0xffff); @@ -85,8 +85,7 @@ public class BusTest extends TestCase { } public void testIsCompleteWithOneDevice() throws MemoryRangeException { - Device memory = new Memory(0x0000, 0x10000, true); - + Device memory = new Memory(0x0000, 0xffff, true); Bus b = new Bus(0x0000, 0xffff); assertFalse("Address space was unexpectedly complete!", b.isComplete()); b.addDevice(memory); @@ -94,8 +93,8 @@ public class BusTest extends TestCase { } public void testIsCompleteWithTwoDevices() throws MemoryRangeException { - Device memory = new Memory(0x0000, 0x8000, true); - Device rom = new Memory(0x8000, 0x8000, false); + Device memory = new Memory(0x0000, 0x7fff, true); + Device rom = new Memory(0x8000, 0xffff, false); Bus b = new Bus(0x0000, 0xffff); assertFalse("Address space was unexpectedly complete!", b.isComplete()); @@ -106,9 +105,9 @@ public class BusTest extends TestCase { } public void testIsCompleteWithThreeDevices() throws MemoryRangeException { - Device memory = new Memory(0x0000, 0x8000, true); - Device rom1 = new Memory(0x8000, 0x4000, false); - Device rom2 = new Memory(0xC000, 0x4000, false); + Device memory = new Memory(0x0000, 0x7fff, true); + Device rom1 = new Memory(0x8000, 0xBfff, false); + Device rom2 = new Memory(0xC000, 0xffff, false); Bus b = new Bus(0x0000, 0xffff); assertFalse("Address space was unexpectedly complete!", b.isComplete()); From d8deca227f03d998ae54cba6bd0fd97c5ad2f6e6 Mon Sep 17 00:00:00 2001 From: Max840 Date: Tue, 24 Dec 2013 15:35:56 -0500 Subject: [PATCH 3/4] Create Device.size() --- src/main/java/com/loomcom/symon/devices/Device.java | 8 ++++++++ src/main/java/com/loomcom/symon/devices/Memory.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/loomcom/symon/devices/Device.java b/src/main/java/com/loomcom/symon/devices/Device.java index 7ded396..86089b4 100644 --- a/src/main/java/com/loomcom/symon/devices/Device.java +++ b/src/main/java/com/loomcom/symon/devices/Device.java @@ -35,6 +35,9 @@ public abstract class Device implements Comparable { /** The memory range for this device. */ private MemoryRange memoryRange; + /** Size of the device in memory **/ + int size; + /** The name of the device. */ private String name; @@ -44,6 +47,7 @@ public abstract class Device implements Comparable { public Device(int startAddress, int endAddress, String name) throws MemoryRangeException { this.memoryRange = new MemoryRange(startAddress, endAddress); + this.size = endAddress - startAddress + 1; this.name = name; } @@ -80,6 +84,10 @@ public abstract class Device implements Comparable { return name; } + public int getSize(){ + return size; + } + public void setName(String name) { this.name = name; } diff --git a/src/main/java/com/loomcom/symon/devices/Memory.java b/src/main/java/com/loomcom/symon/devices/Memory.java index f62b558..c7c971a 100644 --- a/src/main/java/com/loomcom/symon/devices/Memory.java +++ b/src/main/java/com/loomcom/symon/devices/Memory.java @@ -42,7 +42,7 @@ public class Memory extends Device { throws MemoryRangeException { super(startAddress, endAddress, (readOnly ? "RO Memory" : "RW Memory")); this.readOnly = readOnly; - this.mem = new int[endAddress - startAddress + 1]; + this.mem = new int[this.size]; this.fill(DEFAULT_FILL); } From bc6b93e3fe1562f1cd2d420e5778c48b285d4458 Mon Sep 17 00:00:00 2001 From: Max840 Date: Thu, 26 Dec 2013 17:25:52 -0500 Subject: [PATCH 4/4] Optimization of Bus.isComplete() --- src/main/java/com/loomcom/symon/Bus.java | 36 ++++-------------------- 1 file changed, 5 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/loomcom/symon/Bus.java b/src/main/java/com/loomcom/symon/Bus.java index ec8334b..3ba26d1 100644 --- a/src/main/java/com/loomcom/symon/Bus.java +++ b/src/main/java/com/loomcom/symon/Bus.java @@ -114,40 +114,14 @@ public class Bus { return false; } - // Loop over devices and ensure they are contiguous. - MemoryRange prev = null; - int i = 0; - int length = devices.size(); + // Loop over devices and add their size + int filledMemory = 0; 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; + filledMemory += d.getSize(); } - // Must be complete. - return true; + // Returns if the total size of the devices fill the bus' memory space + return filledMemory == endAddress - startAddress + 1; } public int read(int address) throws MemoryAccessException {