openMSX
MSXAudio.cc
Go to the documentation of this file.
1 #include "MSXAudio.hh"
2 #include "Y8950Periphery.hh"
3 #include "DACSound8U.hh"
4 #include "StringOp.hh"
5 #include "serialize.hh"
6 #include <memory>
7 
8 namespace openmsx {
9 
10 // MSXAudio
11 
13  : MSXDevice(config)
14  , y8950(getName(), config,
15  config.getChildDataAsInt("sampleram", 256) * 1024,
16  getCurrentTime(), *this)
17  , dacValue(0x80), dacEnabled(false)
18 {
19  auto type = config.getChildData("type", "philips");
21  if (cmp(type, "philips")) {
22  dac = std::make_unique<DACSound8U>(
23  getName() + " 8-bit DAC", "MSX-AUDIO 8-bit DAC",
24  config);
25  }
27 }
28 
30 {
31  // delete soon, because PanasonicAudioPeriphery still uses
32  // this object in its destructor
33  periphery.reset();
34 }
35 
36 Y8950Periphery& MSXAudio::createPeriphery(const std::string& soundDeviceName)
37 {
39  *this, getDeviceConfig2(), soundDeviceName);
40  return *periphery;
41 }
42 
43 void MSXAudio::powerUp(EmuTime::param time)
44 {
45  y8950.clearRam();
46  reset(time);
47 }
48 
49 void MSXAudio::reset(EmuTime::param time)
50 {
51  y8950.reset(time);
52  periphery->reset();
53  registerLatch = 0; // TODO check
54 }
55 
56 byte MSXAudio::readIO(word port, EmuTime::param time)
57 {
58  if ((port & 0xE8) == 0x08) {
59  // read DAC
60  return 0xFF;
61  } else {
62  return (port & 1) ? y8950.readReg(registerLatch, time)
63  : y8950.readStatus(time);
64  }
65 }
66 
67 byte MSXAudio::peekIO(word port, EmuTime::param time) const
68 {
69  if ((port & 0xE8) == 0x08) {
70  // read DAC
71  return 0xFF; // read always returns 0xFF
72  } else {
73  return (port & 1) ? y8950.peekReg(registerLatch, time)
74  : y8950.peekStatus(time);
75  }
76 }
77 
78 void MSXAudio::writeIO(word port, byte value, EmuTime::param time)
79 {
80  if ((port & 0xE8) == 0x08) {
81  dacValue = value;
82  if (dacEnabled) {
83  assert(dac);
84  dac->writeDAC(dacValue, time);
85  }
86  } else if ((port & 0x01) == 0) {
87  // 0xC0 or 0xC2
88  registerLatch = value;
89  } else {
90  // 0xC1 or 0xC3
91  y8950.writeReg(registerLatch, value, time);
92  }
93 }
94 
95 byte MSXAudio::readMem(word address, EmuTime::param time)
96 {
97  return periphery->readMem(address, time);
98 }
99 byte MSXAudio::peekMem(word address, EmuTime::param time) const
100 {
101  return periphery->peekMem(address, time);
102 }
103 void MSXAudio::writeMem(word address, byte value, EmuTime::param time)
104 {
105  periphery->writeMem(address, value, time);
106 }
107 const byte* MSXAudio::getReadCacheLine(word start) const
108 {
109  return periphery->getReadCacheLine(start);
110 }
112 {
113  return periphery->getWriteCacheLine(start);
114 }
115 
116 void MSXAudio::enableDAC(bool enable, EmuTime::param time)
117 {
118  if ((dacEnabled != enable) && dac) {
119  dacEnabled = enable;
120  byte value = dacEnabled ? dacValue : 0x80;
121  dac->writeDAC(value, time);
122  }
123 }
124 
125 template<typename Archive>
126 void MSXAudio::serialize(Archive& ar, unsigned /*version*/)
127 {
128  ar.serializePolymorphic("periphery", *periphery);
129  ar.serialize("Y8950", y8950,
130  "registerLatch", registerLatch,
131  "dacValue", dacValue,
132  "dacEnabled", dacEnabled);
133 
134  if constexpr (Archive::IS_LOADER) {
135  // restore dac status
136  if (dacEnabled) {
137  assert(dac);
138  dac->writeDAC(dacValue, getCurrentTime());
139  }
140  }
141 }
144 
145 } // namespace openmsx
std::string_view getChildData(std::string_view name) const
Definition: DeviceConfig.cc:48
void powerUp(EmuTime::param time) override
This method is called when MSX is powered up.
Definition: MSXAudio.cc:43
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXAudio.cc:99
const byte * getReadCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
Definition: MSXAudio.cc:107
void writeIO(word port, byte value, EmuTime::param time) override
Write a byte to a given IO port at a certain time to this device.
Definition: MSXAudio.cc:78
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
Definition: MSXAudio.cc:56
void writeMem(word address, byte value, EmuTime::param time) override
Write a given byte to a given location at a certain time to this device.
Definition: MSXAudio.cc:103
MSXAudio(const DeviceConfig &config)
Definition: MSXAudio.cc:12
byte * getWriteCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MSXAudio.cc:111
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MSXAudio.cc:95
void serialize(Archive &ar, unsigned version)
Definition: MSXAudio.cc:126
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXAudio.cc:49
~MSXAudio() override
Definition: MSXAudio.cc:29
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition: MSXAudio.cc:67
Y8950Periphery & createPeriphery(const std::string &soundDeviceName)
Creates a periphery object for this MSXAudio cartridge.
Definition: MSXAudio.cc:36
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:33
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:234
virtual const std::string & getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:376
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:126
static std::unique_ptr< Y8950Periphery > create(MSXAudio &audio, const DeviceConfig &config, const std::string &soundDeviceName)
Models the 4 general purpose I/O pins on the Y8950 (controlled by registers r#18 and r#19)
void reset(EmuTime::param time)
Definition: Y8950.cc:551
byte readStatus(EmuTime::param time) const
Definition: Y8950.cc:1178
byte peekReg(byte rg, EmuTime::param time) const
Definition: Y8950.cc:1155
void clearRam()
Definition: Y8950.cc:545
void writeReg(byte rg, byte data, EmuTime::param time)
Definition: Y8950.cc:896
byte readReg(byte rg, EmuTime::param time)
Definition: Y8950.cc:1140
byte peekStatus(EmuTime::param time) const
Definition: Y8950.cc:1185
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:741
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:998