| 1 |
#include "SensorKid.hh" |
| 2 |
|
| 3 |
#include "CommandController.hh" |
| 4 |
#include "MSXCliComm.hh" |
| 5 |
#include "MSXException.hh" |
| 6 |
#include "StringSetting.hh" |
| 7 |
#include "serialize.hh" |
| 8 |
|
| 9 |
#include "narrow.hh" |
| 10 |
|
| 11 |
namespace openmsx { |
| 12 |
|
| 13 |
SensorKid::SensorKid(const DeviceConfig& config) |
| 14 |
: MSXDevice(config) |
| 15 |
, portStatusCallback(getCommandController(), |
| 16 |
getName() + "_port_status_callback", |
| 17 |
"Tcl proc to call when an Sensor Kid port status is changed", |
| 18 |
"", Setting::Save::NO) |
| 19 |
, acquireCallback(getCommandController(), |
| 20 |
getName() + "_acquire_callback", |
| 21 |
"Tcl proc called to acquire analog data. " |
| 22 |
"Input: port number (0-3). " |
| 23 |
"Output: the value for that port (0-255).", |
| 24 |
"", Setting::Save::NO) |
| 25 |
{ |
| 26 |
reset(getCurrentTime()); |
| 27 |
} |
| 28 |
|
| 29 |
void SensorKid::reset(EmuTime ) |
| 30 |
{ |
| 31 |
prev = 255; |
| 32 |
mb4052_ana = 0; |
| 33 |
mb4052_count = 0; |
| 34 |
} |
| 35 |
|
| 36 |
void SensorKid::writeIO(uint16_t port, byte value, EmuTime ) |
| 37 |
{ |
| 38 |
if ((port & 1) != 0) return; |
| 39 |
|
| 40 |
byte diff = prev ^ value; |
| 41 |
prev = value; |
| 42 |
|
| 43 |
if (diff & 0xC0) { |
| 44 |
|
| 45 |
putPort(value, diff); |
| 46 |
} |
| 47 |
|
| 48 |
if (diff & 0x10) { |
| 49 |
|
| 50 |
if (value & 0x10) { |
| 51 |
|
| 52 |
mb4052_ana = getAnalog(value & 0x0C); |
| 53 |
mb4052_count = 12; |
| 54 |
} else { |
| 55 |
|
| 56 |
mb4052_count = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
if ((diff & 0x01) && (value & 0x01)) { |
| 60 |
|
| 61 |
if (mb4052_count > 0) --mb4052_count; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
byte SensorKid::readIO(uint16_t port, EmuTime ) |
| 66 |
{ |
| 67 |
if ((port & 1) == 0) { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
byte result = [&] { |
| 74 |
if (mb4052_count == 10) { |
| 75 |
return byte(0); |
| 76 |
} else if ((mb4052_count < 10) && (mb4052_count > 1)) { |
| 77 |
return byte((mb4052_ana >> (mb4052_count - 2)) & 1); |
| 78 |
} else { |
| 79 |
return byte(1); |
| 80 |
} |
| 81 |
}(); |
| 82 |
return 0xFE | result; |
| 83 |
} else { |
| 84 |
|
| 85 |
|
| 86 |
return 0xFC | ((prev >> 5) & 0x02) | ((prev >> 7) & 0x01); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
byte SensorKid::getAnalog(byte chi) const |
| 91 |
{ |
| 92 |
|
| 93 |
|
| 94 |
byte port = ((chi >> 1) & 2) | ((chi >> 3) & 1); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
byte result = 255; |
| 105 |
try { |
| 106 |
auto obj = acquireCallback.execute(port); |
| 107 |
if (obj != TclObject()) { |
| 108 |
auto tmp = obj.getInt(getCommandController().getInterpreter()); |
| 109 |
if ((tmp < 0) || (tmp > 255)) { |
| 110 |
throw MSXException("outside range 0..255"); |
| 111 |
} |
| 112 |
result = narrow_cast<byte>(tmp); |
| 113 |
} |
| 114 |
} catch (MSXException& e) { |
| 115 |
getCliComm().printWarning( |
| 116 |
"Wrong result for callback function \"", |
| 117 |
acquireCallback.getSetting().getFullName(), |
| 118 |
"\": ", e.getMessage()); |
| 119 |
} |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
void SensorKid::putPort(byte data, byte diff) const |
| 124 |
{ |
| 125 |
|
| 126 |
|
| 127 |
if (diff & 0x80) { |
| 128 |
|
| 129 |
portStatusCallback.execute(0, (data & 0x80) == 0); |
| 130 |
} |
| 131 |
if (diff & 0x40) { |
| 132 |
|
| 133 |
portStatusCallback.execute(1, (data & 0x40) == 0); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
template<typename Archive> |
| 138 |
void SensorKid::serialize(Archive& ar, unsigned ) |
| 139 |
{ |
| 140 |
ar.template serializeBase<MSXDevice>(*this); |
| 141 |
ar.serialize("prev", prev, |
| 142 |
"mb4052_ana", mb4052_ana, |
| 143 |
"mb4052_count", mb4052_count); |
| 144 |
} |
| 145 |
INSTANTIATE_SERIALIZE_METHODS(SensorKid); |
| 146 |
REGISTER_MSXDEVICE(SensorKid, "SensorKid"); |
| 147 |
|
| 148 |
} |