| e8555c89 Andy McCall |
1 |
#include "MC6850.hh" |
| 2 |
|
| 3 |
#include "MidiInDevice.hh" |
| 4 |
|
| 5 |
#include "EmuTime.hh" |
| 6 |
#include "MSXMotherBoard.hh" |
| 7 |
#include "serialize.hh" |
| 8 |
|
| 9 |
#include <array> |
| 10 |
|
| 11 |
namespace openmsx { |
| 12 |
|
| 13 |
|
| 14 |
static constexpr unsigned CR_CDS1 = 0x01; |
| 15 |
static constexpr unsigned CR_CDS2 = 0x02; |
| 16 |
static constexpr unsigned CR_CDS = CR_CDS1 | CR_CDS2; |
| 17 |
static constexpr unsigned CR_MR = CR_CDS1 | CR_CDS2; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
static constexpr unsigned CR_WS1 = 0x04; |
| 25 |
static constexpr unsigned CR_WS2 = 0x08; |
| 26 |
static constexpr unsigned CR_WS3 = 0x10; |
| 27 |
static constexpr unsigned CR_WS = CR_WS1 | CR_WS2 | CR_WS3; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static constexpr unsigned CR_TC1 = 0x20; |
| 39 |
static constexpr unsigned CR_TC2 = 0x40; |
| 40 |
static constexpr unsigned CR_TC = CR_TC1 | CR_TC2; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static constexpr unsigned CR_RIE = 0x80; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static constexpr unsigned STAT_RDRF = 0x01; |
| 54 |
static constexpr unsigned STAT_TDRE = 0x02; |
| 55 |
static constexpr unsigned STAT_DCD = 0x04; |
| 56 |
static constexpr unsigned STAT_CTS = 0x08; |
| 57 |
static constexpr unsigned STAT_FE = 0x10; |
| 58 |
static constexpr unsigned STAT_OVRN = 0x20; |
| 59 |
static constexpr unsigned STAT_PE = 0x40; |
| 60 |
static constexpr unsigned STAT_IRQ = 0x80; |
| 61 |
|
| 62 |
MC6850::MC6850(const std::string& name_, MSXMotherBoard& motherBoard, unsigned clockFreq_) |
| 63 |
: MidiInConnector(motherBoard.getPluggingController(), name_ + "-in") |
| 64 |
, syncRecv (motherBoard.getScheduler()) |
| 65 |
, syncTrans(motherBoard.getScheduler()) |
| 66 |
, clockFreq(clockFreq_) |
| 67 |
, rxIRQ(motherBoard, name_ + "-rx-IRQ") |
| 68 |
, txIRQ(motherBoard, name_ + "-tx-IRQ") |
| 69 |
, outConnector(motherBoard.getPluggingController(), name_ + "-out") |
| 70 |
{ |
| 71 |
reset(EmuTime::zero()); |
| 72 |
setDataFormat(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
void MC6850::reset(EmuTime time) |
| 77 |
{ |
| 78 |
syncRecv .removeSyncPoint(); |
| 79 |
syncTrans.removeSyncPoint(); |
| 80 |
txClock.reset(time); |
| 81 |
txClock.setFreq(clockFreq); |
| 82 |
rxIRQ.reset(); |
| 83 |
txIRQ.reset(); |
| 84 |
rxReady = false; |
| 85 |
txShiftRegValid = false; |
| 86 |
pendingOVRN = false; |
| 87 |
controlReg = CR_MR; |
| 88 |
statusReg = 0; |
| 89 |
rxDataReg = 0; |
| 90 |
setDataFormat(); |
| 91 |
} |
| 92 |
|
| 93 |
uint8_t MC6850::readStatusReg() const |
| 94 |
{ |
| 95 |
return peekStatusReg(); |
| 96 |
} |
| 97 |
|
| 98 |
uint8_t MC6850::peekStatusReg() const |
| 99 |
{ |
| 100 |
uint8_t result = statusReg; |
| 101 |
if (rxIRQ.getState() || txIRQ.getState()) result |= STAT_IRQ; |
| 102 |
return result; |
| 103 |
} |
| 104 |
|
| 105 |
uint8_t MC6850::readDataReg() |
| 106 |
{ |
| 107 |
uint8_t result = peekDataReg(); |
| 108 |
statusReg &= uint8_t(~(STAT_RDRF | STAT_OVRN)); |
| 109 |
if (pendingOVRN) { |
| 110 |
pendingOVRN = false; |
| 111 |
statusReg |= STAT_OVRN; |
| 112 |
} |
| 113 |
rxIRQ.reset(); |
| 114 |
return result; |
| 115 |
} |
| 116 |
|
| 117 |
uint8_t MC6850::peekDataReg() const |
| 118 |
{ |
| 119 |
return rxDataReg; |
| 120 |
} |
| 121 |
|
| 122 |
void MC6850::writeControlReg(uint8_t value, EmuTime time) |
| 123 |
{ |
| 124 |
uint8_t diff = value ^ controlReg; |
| 125 |
if (diff & CR_CDS) { |
| 126 |
if ((value & CR_CDS) == CR_MR) { |
| 127 |
reset(time); |
| 128 |
} else { |
| 129 |
|
| 130 |
rxReady = true; |
| 131 |
statusReg |= STAT_TDRE; |
| 132 |
|
| 133 |
txClock.reset(time); |
| 134 |
switch (value & CR_CDS) { |
| 135 |
case 0: txClock.setFreq(clockFreq, 1); break; |
| 136 |
case 1: txClock.setFreq(clockFreq, 16); break; |
| 137 |
case 2: txClock.setFreq(clockFreq, 64); break; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
controlReg = value; |
| 143 |
if (diff & CR_WS) setDataFormat(); |
| 144 |
|
| 145 |
|
| 146 |
rxIRQ.set(( value & CR_RIE) && (statusReg & STAT_RDRF)); |
| 147 |
txIRQ.set(((value & CR_TC) == 0x20) && (statusReg & STAT_TDRE)); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void MC6850::setDataFormat() |
| 152 |
{ |
| 153 |
outConnector.setDataBits(controlReg & CR_WS3 ? DataBits::D8 : DataBits::D7); |
| 154 |
|
| 155 |
static constexpr std::array<StopBits, 8> stopBits = { |
| 156 |
StopBits::S2, StopBits::S2, StopBits::S1, StopBits::S1, |
| 157 |
StopBits::S2, StopBits::S1, StopBits::S1, StopBits::S1, |
| 158 |
}; |
| 159 |
outConnector.setStopBits(stopBits[(controlReg & CR_WS) >> 2]); |
| 160 |
|
| 161 |
outConnector.setParityBit( |
| 162 |
(controlReg & (CR_WS3 | CR_WS2)) != 0x10, |
| 163 |
(controlReg & CR_WS1) ? Parity::ODD : Parity::EVEN); |
| 164 |
|
| 165 |
|
| 166 |
static constexpr std::array<uint8_t, 8> len = { |
| 167 |
1 + 7 + 1 + 2, |
| 168 |
1 + 7 + 1 + 2, |
| 169 |
1 + 7 + 1 + 1, |
| 170 |
1 + 7 + 1 + 1, |
| 171 |
1 + 8 + 0 + 2, |
| 172 |
1 + 8 + 0 + 1, |
| 173 |
1 + 8 + 1 + 1, |
| 174 |
1 + 8 + 1 + 1, |
| 175 |
}; |
| 176 |
charLen = len[(controlReg & CR_WS) >> 2]; |
| 177 |
} |
| 178 |
|
| 179 |
void MC6850::writeDataReg(uint8_t value, EmuTime time) |
| 180 |
{ |
| 181 |
if ((controlReg & CR_CDS) == CR_MR) return; |
| 182 |
|
| 183 |
txDataReg = value; |
| 184 |
statusReg &= uint8_t(~STAT_TDRE); |
| 185 |
txIRQ.reset(); |
| 186 |
|
| 187 |
if (syncTrans.isPending()) { |
| 188 |
|
| 189 |
|
| 190 |
} else { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
txClock.advance(time); |
| 195 |
txClock += 1; |
| 196 |
syncTrans.setSyncPoint(txClock.getTime()); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
void MC6850::execTrans(EmuTime time) |
| 203 |
{ |
| 204 |
assert(txClock.getTime() == time); |
| 205 |
assert((controlReg & CR_CDS) != CR_MR); |
| 206 |
|
| 207 |
if (txShiftRegValid) { |
| 208 |
txShiftRegValid = false; |
| 209 |
outConnector.recvByte(txShiftReg, time); |
| 210 |
} |
| 211 |
|
| 212 |
if (statusReg & STAT_TDRE) { |
| 213 |
|
| 214 |
} else { |
| 215 |
|
| 216 |
|
| 217 |
statusReg |= STAT_TDRE; |
| 218 |
if ((controlReg & CR_TC) == 0x20) txIRQ.set(); |
| 219 |
|
| 220 |
txShiftReg = txDataReg; |
| 221 |
txShiftRegValid = true; |
| 222 |
|
| 223 |
txClock += charLen; |
| 224 |
syncTrans.setSyncPoint(txClock.getTime()); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
void MC6850::recvByte(uint8_t value, EmuTime time) |
| 230 |
{ |
| 231 |
assert(acceptsData() && ready()); |
| 232 |
|
| 233 |
if (statusReg & STAT_RDRF) { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
pendingOVRN = true; |
| 240 |
} else { |
| 241 |
rxDataReg = value; |
| 242 |
statusReg |= STAT_RDRF; |
| 243 |
} |
| 244 |
|
| 245 |
if (controlReg & CR_RIE) rxIRQ.set(); |
| 246 |
|
| 247 |
|
| 248 |
rxReady = false; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
syncRecv.setSyncPoint(time + txClock.getPeriod() * charLen); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
void MC6850::execRecv(EmuTime time) |
| 259 |
{ |
| 260 |
assert(acceptsData()); |
| 261 |
assert(!rxReady); |
| 262 |
rxReady = true; |
| 263 |
getPluggedMidiInDev().signal(time); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
bool MC6850::ready() |
| 268 |
{ |
| 269 |
return rxReady; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
bool MC6850::acceptsData() |
| 274 |
{ |
| 275 |
return (controlReg & CR_CDS) != CR_MR; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
void MC6850::setDataBits(DataBits ) |
| 281 |
{ |
| 282 |
|
| 283 |
} |
| 284 |
void MC6850::setStopBits(StopBits ) |
| 285 |
{ |
| 286 |
|
| 287 |
} |
| 288 |
void MC6850::setParityBit(bool , Parity ) |
| 289 |
{ |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
template<typename Archive> |
| 297 |
void MC6850::serialize(Archive& ar, unsigned version) |
| 298 |
{ |
| 299 |
if (ar.versionAtLeast(version, 3)) { |
| 300 |
ar.template serializeBase<MidiInConnector>(*this); |
| 301 |
ar.serialize("outConnector", outConnector, |
| 302 |
|
| 303 |
"syncRecv", syncRecv, |
| 304 |
"syncTrans", syncTrans, |
| 305 |
|
| 306 |
"txClock", txClock, |
| 307 |
"rxIRQ", rxIRQ, |
| 308 |
"txIRQ", txIRQ, |
| 309 |
|
| 310 |
"rxReady", rxReady, |
| 311 |
"txShiftRegValid", txShiftRegValid, |
| 312 |
"pendingOVRN", pendingOVRN, |
| 313 |
|
| 314 |
"rxDataReg", rxDataReg, |
| 315 |
"txDataReg", txDataReg, |
| 316 |
"txShiftReg", txShiftReg, |
| 317 |
"controlReg", controlReg, |
| 318 |
"statusReg", statusReg); |
| 319 |
} else if (ar.versionAtLeast(version, 2)) { |
| 320 |
ar.serialize("control", controlReg); |
| 321 |
} else { |
| 322 |
controlReg = 3; |
| 323 |
} |
| 324 |
|
| 325 |
if constexpr (Archive::IS_LOADER) { |
| 326 |
setDataFormat(); |
| 327 |
} |
| 328 |
} |
| 329 |
INSTANTIATE_SERIALIZE_METHODS(MC6850); |
| 330 |
|
| 331 |
} |