| 1 |
#include "MSXTurboRPCM.hh" |
| 2 |
|
| 3 |
#include "MSXMixer.hh" |
| 4 |
#include "MSXMotherBoard.hh" |
| 5 |
#include "serialize.hh" |
| 6 |
|
| 7 |
#include "narrow.hh" |
| 8 |
#include "unreachable.hh" |
| 9 |
|
| 10 |
namespace openmsx { |
| 11 |
|
| 12 |
MSXTurboRPCM::MSXTurboRPCM(const DeviceConfig& config) |
| 13 |
: MSXDevice(config) |
| 14 |
, mixer(getMotherBoard().getMSXMixer()) |
| 15 |
, connector(getPluggingController(), "pcminput") |
| 16 |
, dac("PCM", "Turbo-R PCM", config) |
| 17 |
, reference(getCurrentTime()) |
| 18 |
{ |
| 19 |
reset(getCurrentTime()); |
| 20 |
} |
| 21 |
|
| 22 |
MSXTurboRPCM::~MSXTurboRPCM() |
| 23 |
{ |
| 24 |
hardwareMute(false); |
| 25 |
} |
| 26 |
|
| 27 |
void MSXTurboRPCM::reset(EmuTime time) |
| 28 |
{ |
| 29 |
reference.reset(time); |
| 30 |
status = 0; |
| 31 |
DValue = 0x80; |
| 32 |
hold = 0x80; |
| 33 |
dac.reset(time); |
| 34 |
hardwareMute(false); |
| 35 |
} |
| 36 |
|
| 37 |
byte MSXTurboRPCM::readIO(uint16_t port, EmuTime time) |
| 38 |
{ |
| 39 |
return peekIO(port, time); |
| 40 |
} |
| 41 |
|
| 42 |
byte MSXTurboRPCM::peekIO(uint16_t port, EmuTime time) const |
| 43 |
{ |
| 44 |
switch (port & 0x01) { |
| 45 |
case 0: |
| 46 |
|
| 47 |
|
| 48 |
return reference.getTicksTill(time) & 0x03; |
| 49 |
case 1: |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
return (getComp(time) ? 0x80 : 0x00) | (status & 0x1F); |
| 63 |
default: |
| 64 |
UNREACHABLE; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void MSXTurboRPCM::writeIO(uint16_t port, byte value, EmuTime time) |
| 69 |
{ |
| 70 |
switch (port & 0x01) { |
| 71 |
case 0: |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
reference.advance(time); |
| 76 |
DValue = value; |
| 77 |
if (status & 0x02) { |
| 78 |
assert(reference.getTime() <= time); |
| 79 |
auto time2 = (status & 0x01) |
| 80 |
? reference.getFastAdd(1) |
| 81 |
: time; |
| 82 |
assert(time <= time2); |
| 83 |
dac.writeDAC(DValue, time2); |
| 84 |
} |
| 85 |
break; |
| 86 |
|
| 87 |
case 1: |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
byte change = status ^ value; |
| 95 |
status = value; |
| 96 |
|
| 97 |
if ((change & 0x01) && ((status & 0x01) == 0)) { |
| 98 |
dac.writeDAC(DValue, time); |
| 99 |
} |
| 100 |
|
| 101 |
if ((change & 0x10) && (status & 0x10)) { |
| 102 |
hold = getSample(time); |
| 103 |
} |
| 104 |
hardwareMute(!(status & 0x02)); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
byte MSXTurboRPCM::getSample(EmuTime time) const |
| 110 |
{ |
| 111 |
return (status & 0x04) |
| 112 |
? narrow<byte>((connector.readSample(time) / 256) + 0x80) |
| 113 |
: 0x80; |
| 114 |
} |
| 115 |
|
| 116 |
bool MSXTurboRPCM::getComp(EmuTime time) const |
| 117 |
{ |
| 118 |
|
| 119 |
byte sample = (status & 0x10) ? hold : getSample(time); |
| 120 |
return sample >= DValue; |
| 121 |
} |
| 122 |
|
| 123 |
void MSXTurboRPCM::hardwareMute(bool mute) |
| 124 |
{ |
| 125 |
if (mute == hwMute) return; |
| 126 |
|
| 127 |
hwMute = mute; |
| 128 |
if (hwMute) { |
| 129 |
mixer.mute(); |
| 130 |
} else { |
| 131 |
mixer.unmute(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
template<typename Archive> |
| 137 |
void MSXTurboRPCM::serialize(Archive& ar, unsigned ) |
| 138 |
{ |
| 139 |
ar.template serializeBase<MSXDevice>(*this); |
| 140 |
ar.serialize("audioConnector", connector, |
| 141 |
"reference", reference, |
| 142 |
"status", status, |
| 143 |
"DValue", DValue, |
| 144 |
"hold", hold, |
| 145 |
"DAC", dac); |
| 146 |
|
| 147 |
hardwareMute(!(status & 0x02)); |
| 148 |
} |
| 149 |
INSTANTIATE_SERIALIZE_METHODS(MSXTurboRPCM); |
| 150 |
REGISTER_MSXDEVICE(MSXTurboRPCM, "TurboR-PCM"); |
| 151 |
|
| 152 |
} |