From: Wouter V. <m97...@us...> - 2006-01-28 16:36:32
|
Update of /cvsroot/openmsx/openMSX/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3886/src Modified Files: MSXDeviceSwitch.cc MSXDeviceSwitch.hh Log Message: made MSXSwitchedDevice more robust when inserted/removed dynamically Index: MSXDeviceSwitch.cc =================================================================== RCS file: /cvsroot/openmsx/openMSX/src/MSXDeviceSwitch.cc,v retrieving revision 1.27 retrieving revision 1.28 diff -u -d -r1.27 -r1.28 --- MSXDeviceSwitch.cc 3 Dec 2005 18:58:10 -0000 1.27 +++ MSXDeviceSwitch.cc 28 Jan 2006 16:36:24 -0000 1.28 @@ -3,6 +3,8 @@ #include "MSXDeviceSwitch.hh" #include "MSXCPUInterface.hh" #include "MSXMotherBoard.hh" +#include "MSXException.hh" +#include "StringOp.hh" #include <cassert> namespace openmsx { @@ -34,39 +36,49 @@ for (int i = 0; i < 256; ++i) { devices[i] = NULL; } + count = 0; selected = 0; - - // TODO register/unregister dynamically - MSXCPUInterface& interface = getMotherBoard().getCPUInterface(); - for (byte port = 0x40; port < 0x50; ++port) { - interface.register_IO_In (port, this); - interface.register_IO_Out(port, this); - } } MSXDeviceSwitch::~MSXDeviceSwitch() { - MSXCPUInterface& interface = getMotherBoard().getCPUInterface(); - for (byte port = 0x40; port < 0x50; ++port) { - interface.unregister_IO_Out(port, this); - interface.unregister_IO_In (port, this); - } for (int i = 0; i < 256; i++) { // all devices must be unregistered assert(devices[i] == NULL); } + assert(count == 0); } void MSXDeviceSwitch::registerDevice(byte id, MSXSwitchedDevice* device) { //PRT_DEBUG("Switch: register device with id " << (int)id); - assert(devices[id] == NULL); + if (devices[id]) { + // TODO implement multiplexing + throw MSXException("Already have a switched device with id " + + StringOp::toString((int)id)); + } devices[id] = device; + if (count == 0) { + MSXCPUInterface& interface = getMotherBoard().getCPUInterface(); + for (byte port = 0x40; port < 0x50; ++port) { + interface.register_IO_In (port, this); + interface.register_IO_Out(port, this); + } + } + ++count; } void MSXDeviceSwitch::unregisterDevice(byte id) { + --count; + if (count == 0) { + MSXCPUInterface& interface = getMotherBoard().getCPUInterface(); + for (byte port = 0x40; port < 0x50; ++port) { + interface.unregister_IO_Out(port, this); + interface.unregister_IO_In (port, this); + } + } assert(devices[id] != NULL); devices[id] = NULL; } Index: MSXDeviceSwitch.hh =================================================================== RCS file: /cvsroot/openmsx/openMSX/src/MSXDeviceSwitch.hh,v retrieving revision 1.18 retrieving revision 1.19 diff -u -d -r1.18 -r1.19 --- MSXDeviceSwitch.hh 3 Dec 2005 18:58:10 -0000 1.18 +++ MSXDeviceSwitch.hh 28 Jan 2006 16:36:24 -0000 1.19 @@ -87,8 +87,9 @@ virtual void writeIO(word port, byte value, const EmuTime& time); private: - byte selected; MSXSwitchedDevice* devices[256]; + unsigned count; + byte selected; }; } // namespace openmsx |