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.devices;
|
2008-12-11 19:41:05 +00:00
|
|
|
|
2013-12-29 03:55:26 +00:00
|
|
|
import com.loomcom.symon.Bus;
|
|
|
|
import com.loomcom.symon.MemoryRange;
|
|
|
|
import com.loomcom.symon.exceptions.MemoryAccessException;
|
|
|
|
import com.loomcom.symon.exceptions.MemoryRangeException;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2008-12-06 04:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A memory-mapped IO Device.
|
|
|
|
*/
|
|
|
|
|
2008-12-12 00:38:39 +00:00
|
|
|
public abstract class Device implements Comparable<Device> {
|
2008-12-06 04:00:48 +00:00
|
|
|
|
2013-12-29 07:35:22 +00:00
|
|
|
/**
|
|
|
|
* Size of the device in memory
|
|
|
|
*/
|
|
|
|
int size;
|
|
|
|
|
2013-12-29 03:55:26 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listeners to notify on update.
|
|
|
|
*/
|
|
|
|
private Set<DeviceChangeListener> deviceChangeListeners;
|
|
|
|
|
2013-12-29 07:35:22 +00:00
|
|
|
public Device(int startAddress, int endAddress, String name)
|
2013-12-29 03:55:26 +00:00
|
|
|
throws MemoryRangeException {
|
2013-12-29 07:35:22 +00:00
|
|
|
this.memoryRange = new MemoryRange(startAddress, endAddress);
|
|
|
|
this.size = endAddress - startAddress + 1;
|
2013-12-29 03:55:26 +00:00
|
|
|
this.name = name;
|
|
|
|
this.deviceChangeListeners = new HashSet<DeviceChangeListener>();
|
|
|
|
}
|
|
|
|
|
2013-12-29 07:35:22 +00:00
|
|
|
public Device(int startAddress, int endAddress) throws MemoryRangeException {
|
|
|
|
this(startAddress, endAddress, null);
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2013-12-29 03:55:26 +00:00
|
|
|
|
|
|
|
/* Methods required to be implemented by inheriting classes. */
|
|
|
|
public abstract void write(int address, int data) throws MemoryAccessException;
|
|
|
|
|
|
|
|
public abstract int read(int address) throws MemoryAccessException;
|
|
|
|
|
|
|
|
public abstract String toString();
|
|
|
|
|
|
|
|
public Bus getBus() {
|
|
|
|
return this.bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-29 07:35:22 +00:00
|
|
|
public int getSize() {
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-12-29 03:55:26 +00:00
|
|
|
public void registerListener(DeviceChangeListener listener) {
|
|
|
|
deviceChangeListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyListeners() {
|
|
|
|
for (DeviceChangeListener l : deviceChangeListeners) {
|
|
|
|
l.deviceStateChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
2008-12-28 05:09:47 +00:00
|
|
|
}
|
2008-12-11 19:41:05 +00:00
|
|
|
}
|