2012-12-30 19:27:10 +00:00
|
|
|
/*
|
2014-08-11 21:16:41 +00:00
|
|
|
* Copyright (c) 2008-2014 Seth J. Morabito <web@loomcom.com>
|
2012-12-30 19:27:10 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2008-12-13 22:59:22 +00:00
|
|
|
package com.loomcom.symon;
|
2008-12-06 04:00:48 +00:00
|
|
|
|
2013-12-30 02:18:25 +00:00
|
|
|
import com.loomcom.symon.devices.Device;
|
|
|
|
import com.loomcom.symon.exceptions.MemoryAccessException;
|
|
|
|
import com.loomcom.symon.exceptions.MemoryRangeException;
|
2014-07-25 17:32:00 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2012-04-23 03:49:18 +00:00
|
|
|
|
2013-12-30 02:18:25 +00:00
|
|
|
import java.util.SortedSet;
|
|
|
|
import java.util.TreeSet;
|
2008-12-06 04:00:48 +00:00
|
|
|
|
2008-12-12 01:22:39 +00:00
|
|
|
/**
|
|
|
|
* The Bus ties the whole thing together, man.
|
|
|
|
*/
|
2008-12-11 19:41:05 +00:00
|
|
|
public class Bus {
|
2008-12-09 04:40:27 +00:00
|
|
|
|
2012-11-26 06:49:21 +00:00
|
|
|
// The default address at which to load programs
|
|
|
|
public static int DEFAULT_LOAD_ADDRESS = 0x0200;
|
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
// By default, our bus starts at 0, and goes up to 64K
|
|
|
|
private int startAddress = 0x0000;
|
|
|
|
private int endAddress = 0xffff;
|
2012-11-26 06:49:21 +00:00
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
// The CPU
|
|
|
|
private Cpu cpu;
|
2012-11-26 06:49:21 +00:00
|
|
|
|
2014-07-25 17:32:00 +00:00
|
|
|
// Ordered sets of IO devices, associated with their priority
|
|
|
|
private Map<Integer, SortedSet<Device>> deviceMap;
|
2014-07-17 19:43:46 +00:00
|
|
|
|
|
|
|
// an array for quick lookup of adresses, brute-force style
|
|
|
|
private Device[] deviceAddressArray;
|
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
|
|
|
|
public Bus(int size) {
|
|
|
|
this(0, size - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bus(int startAddress, int endAddress) {
|
2014-07-25 17:32:00 +00:00
|
|
|
this.deviceMap = new HashMap<Integer, SortedSet<Device>>();
|
2012-04-23 03:49:18 +00:00
|
|
|
this.startAddress = startAddress;
|
|
|
|
this.endAddress = endAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int startAddress() {
|
|
|
|
return startAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int endAddress() {
|
|
|
|
return endAddress;
|
|
|
|
}
|
2014-07-17 19:43:46 +00:00
|
|
|
|
|
|
|
private void buildDeviceAddressArray() {
|
2014-07-25 19:24:16 +00:00
|
|
|
int size = (this.endAddress - this.startAddress) + 1;
|
2014-07-17 19:43:46 +00:00
|
|
|
deviceAddressArray = new Device[size];
|
2014-07-25 19:44:43 +00:00
|
|
|
|
|
|
|
// getDevices() provides an OrderedSet with devices ordered by priorities
|
|
|
|
for(Device device : getDevices()) {
|
|
|
|
MemoryRange range = device.getMemoryRange();
|
|
|
|
for(int address = range.startAddress; address <= range.endAddress; ++address) {
|
|
|
|
deviceAddressArray[address - this.startAddress] = device;
|
2014-07-17 19:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-25 17:32:00 +00:00
|
|
|
|
2014-07-17 19:43:46 +00:00
|
|
|
}
|
2012-04-23 03:49:18 +00:00
|
|
|
|
2012-11-26 06:49:21 +00:00
|
|
|
/**
|
2014-07-25 17:32:00 +00:00
|
|
|
* Add a device to the bus.
|
2012-11-26 06:49:21 +00:00
|
|
|
*
|
|
|
|
* @param device
|
2014-07-25 17:32:00 +00:00
|
|
|
* @param priority
|
2014-07-25 19:27:40 +00:00
|
|
|
* @throws MemoryRangeException
|
2012-11-26 06:49:21 +00:00
|
|
|
*/
|
2014-07-25 19:24:16 +00:00
|
|
|
public void addDevice(Device device, int priority) throws MemoryRangeException {
|
|
|
|
|
|
|
|
MemoryRange range = device.getMemoryRange();
|
|
|
|
if(range.startAddress() < this.startAddress || range.startAddress() > this.endAddress) {
|
|
|
|
throw new MemoryRangeException("start address of device " + device.getName() + " does not fall within the address range of the bus");
|
|
|
|
}
|
|
|
|
if(range.endAddress() < this.startAddress || range.endAddress() > this.endAddress) {
|
|
|
|
throw new MemoryRangeException("end address of device " + device.getName() + " does not fall within the address range of the bus");
|
|
|
|
}
|
|
|
|
|
2014-07-25 17:32:00 +00:00
|
|
|
|
|
|
|
SortedSet<Device> deviceSet = deviceMap.get(priority);
|
|
|
|
if(deviceSet == null) {
|
|
|
|
deviceSet = new TreeSet<Device>();
|
|
|
|
deviceMap.put(priority, deviceSet);
|
2014-07-17 19:43:46 +00:00
|
|
|
}
|
2014-07-25 17:32:00 +00:00
|
|
|
|
2014-07-28 16:19:52 +00:00
|
|
|
device.setBus(this);
|
2014-07-25 17:32:00 +00:00
|
|
|
deviceSet.add(device);
|
2014-07-17 19:43:46 +00:00
|
|
|
buildDeviceAddressArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a device to the bus. Throws a MemoryRangeException if the device overlaps with any others.
|
|
|
|
*
|
|
|
|
* @param device
|
|
|
|
* @throws MemoryRangeException
|
|
|
|
*/
|
|
|
|
public void addDevice(Device device) throws MemoryRangeException {
|
2014-07-25 17:32:00 +00:00
|
|
|
addDevice(device, 0);
|
2008-12-15 07:43:04 +00:00
|
|
|
}
|
2014-07-17 19:43:46 +00:00
|
|
|
|
2008-12-28 05:09:47 +00:00
|
|
|
|
2012-11-26 06:49:21 +00:00
|
|
|
/**
|
|
|
|
* Remove a device from the bus.
|
|
|
|
*
|
|
|
|
* @param device
|
|
|
|
*/
|
|
|
|
public void removeDevice(Device device) {
|
2014-07-25 17:32:00 +00:00
|
|
|
for(SortedSet<Device> deviceSet : deviceMap.values()) {
|
|
|
|
deviceSet.remove(device);
|
2012-11-26 06:49:21 +00:00
|
|
|
}
|
2014-07-25 17:32:00 +00:00
|
|
|
buildDeviceAddressArray();
|
2012-11-26 06:49:21 +00:00
|
|
|
}
|
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
public void addCpu(Cpu cpu) {
|
|
|
|
this.cpu = cpu;
|
2012-11-26 06:49:21 +00:00
|
|
|
cpu.setBus(this);
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2014-07-25 17:32:00 +00:00
|
|
|
if(deviceAddressArray == null) {
|
|
|
|
buildDeviceAddressArray();
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
2014-07-25 17:32:00 +00:00
|
|
|
|
|
|
|
for(int address = startAddress; address <= endAddress; ++address) {
|
2014-07-25 19:24:16 +00:00
|
|
|
if(deviceAddressArray[address - startAddress] == null) {
|
2014-07-25 17:32:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2014-07-25 17:32:00 +00:00
|
|
|
|
|
|
|
return true;
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int read(int address) throws MemoryAccessException {
|
2014-07-25 19:24:16 +00:00
|
|
|
Device d = deviceAddressArray[address - this.startAddress];
|
2014-07-17 19:43:46 +00:00
|
|
|
if(d != null) {
|
2012-04-23 03:49:18 +00:00
|
|
|
MemoryRange range = d.getMemoryRange();
|
2014-07-17 19:43:46 +00:00
|
|
|
int devAddr = address - range.startAddress();
|
|
|
|
return d.read(devAddr);
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
2014-07-17 19:43:46 +00:00
|
|
|
|
CPU bug fixes and Simulator enhancements.
Bug Fixes:
- Fixed several bugs in the CPU that caused processor status flags to
be set incorrectly. Instructions affected were: STA, STX, STY, CMP,
CPX, CPY, BIT.
- Made some internal-use-only methods on the CPU class private.
- Fixed incorrect disassembly of (Indirect,X) and (Indirect),Y
instructions. Although this didn't affect behavior, it certainly
caused me some confusion in debugging.
- Added missing "BCS" instruction to instruction table.
Enhancements:
- Now includes a full version of Lee Davison's Enhanced 6502 BASIC
bundled as source code and a ROM image. Get that REAL COMPUTER
EXPERIENCE!(tm)
- If a file named "rom.bin" exists in the same directory where the
simulator is executed, it will be loaded at addresses $d000-$ffff.
- Gave the CPU an idle loop to make simulated timing a little more
realistic (but this is still an area needing major improvement)
- Changed the CPU's toString() method to give better debugging output.
- Added a small typeahead buffer to the Console.
- Better exception messaging.
Misc:
- Bumped version to 0.5, updated README.
2012-10-22 03:05:05 +00:00
|
|
|
throw new MemoryAccessException("Bus read failed. No device at address " + String.format("$%04X", address));
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
2008-12-28 05:09:47 +00:00
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
public void write(int address, int value) throws MemoryAccessException {
|
2014-07-25 19:24:16 +00:00
|
|
|
Device d = deviceAddressArray[address - this.startAddress];
|
2014-07-17 19:43:46 +00:00
|
|
|
if(d != null) {
|
2012-04-23 03:49:18 +00:00
|
|
|
MemoryRange range = d.getMemoryRange();
|
2014-07-17 19:43:46 +00:00
|
|
|
int devAddr = address - range.startAddress();
|
|
|
|
d.write(devAddr, value);
|
|
|
|
return;
|
2012-04-23 03:49:18 +00:00
|
|
|
}
|
2014-07-17 19:43:46 +00:00
|
|
|
|
CPU bug fixes and Simulator enhancements.
Bug Fixes:
- Fixed several bugs in the CPU that caused processor status flags to
be set incorrectly. Instructions affected were: STA, STX, STY, CMP,
CPX, CPY, BIT.
- Made some internal-use-only methods on the CPU class private.
- Fixed incorrect disassembly of (Indirect,X) and (Indirect),Y
instructions. Although this didn't affect behavior, it certainly
caused me some confusion in debugging.
- Added missing "BCS" instruction to instruction table.
Enhancements:
- Now includes a full version of Lee Davison's Enhanced 6502 BASIC
bundled as source code and a ROM image. Get that REAL COMPUTER
EXPERIENCE!(tm)
- If a file named "rom.bin" exists in the same directory where the
simulator is executed, it will be loaded at addresses $d000-$ffff.
- Gave the CPU an idle loop to make simulated timing a little more
realistic (but this is still an area needing major improvement)
- Changed the CPU's toString() method to give better debugging output.
- Added a small typeahead buffer to the Console.
- Better exception messaging.
Misc:
- Bumped version to 0.5, updated README.
2012-10-22 03:05:05 +00:00
|
|
|
throw new MemoryAccessException("Bus write failed. No device at address " + String.format("$%04X", address));
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
|
|
|
|
2014-01-26 04:02:17 +00:00
|
|
|
public void assertIrq() {
|
2014-01-26 03:53:53 +00:00
|
|
|
if (cpu != null) {
|
2014-01-26 04:02:17 +00:00
|
|
|
cpu.assertIrq();
|
2014-01-26 03:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 04:02:17 +00:00
|
|
|
public void clearIrq() {
|
2014-01-26 03:53:53 +00:00
|
|
|
if (cpu != null) {
|
2014-01-26 04:02:17 +00:00
|
|
|
cpu.clearIrq();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void assertNmi() {
|
|
|
|
if (cpu != null) {
|
|
|
|
cpu.assertNmi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearNmi() {
|
|
|
|
if (cpu != null) {
|
|
|
|
cpu.clearNmi();
|
2014-01-26 03:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-23 03:49:18 +00:00
|
|
|
public SortedSet<Device> getDevices() {
|
2014-07-25 19:44:43 +00:00
|
|
|
// create an ordered set of devices, ordered by device priorities
|
2014-07-25 17:32:00 +00:00
|
|
|
SortedSet<Device> devices = new TreeSet<Device>();
|
|
|
|
|
|
|
|
List<Integer> priorities = new ArrayList<Integer>(deviceMap.keySet());
|
|
|
|
Collections.sort(priorities);
|
|
|
|
|
|
|
|
for(int priority : priorities) {
|
|
|
|
SortedSet<Device> deviceSet = deviceMap.get(priority);
|
|
|
|
for(Device device : deviceSet) {
|
|
|
|
devices.add(device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices;
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2012-04-23 03:49:18 +00:00
|
|
|
|
|
|
|
public Cpu getCpu() {
|
|
|
|
return cpu;
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2012-04-23 03:49:18 +00:00
|
|
|
|
|
|
|
public void loadProgram(int... program) throws MemoryAccessException {
|
|
|
|
int address = getCpu().getProgramCounter();
|
|
|
|
int i = 0;
|
|
|
|
for (int d : program) {
|
|
|
|
write(address + i++, d);
|
|
|
|
}
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2008-12-11 19:41:05 +00:00
|
|
|
}
|