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
8namespace openmsx {
9
10// MSXAudio
11
13 : MSXDevice(config)
14 , y8950(getName(), config,
15 config.getChildDataAsInt("sampleram", 256) * 1024,
16 getCurrentTime(), *this)
17{
18 auto type = config.getChildData("type", "philips");
20 if (cmp(type, "philips")) {
21 dac = std::make_unique<DACSound8U>(
22 getName() + " 8-bit DAC", "MSX-AUDIO 8-bit DAC",
23 config);
24 }
26}
27
29{
30 // delete soon, because PanasonicAudioPeriphery still uses
31 // this object in its destructor
32 periphery.reset();
33}
34
35Y8950Periphery& MSXAudio::createPeriphery(const std::string& soundDeviceName)
36{
38 *this, getDeviceConfig2(), soundDeviceName);
39 return *periphery;
40}
41
42void MSXAudio::powerUp(EmuTime::param time)
43{
44 y8950.clearRam();
45 reset(time);
46}
47
48void MSXAudio::reset(EmuTime::param time)
49{
50 y8950.reset(time);
51 periphery->reset();
52 registerLatch = 0; // TODO check
53}
54
55byte MSXAudio::readIO(word port, EmuTime::param time)
56{
57 if ((port & 0xE8) == 0x08) {
58 // read DAC
59 return 0xFF;
60 } else {
61 return (port & 1) ? y8950.readReg(registerLatch, time)
62 : y8950.readStatus(time);
63 }
64}
65
66byte MSXAudio::peekIO(word port, EmuTime::param time) const
67{
68 if ((port & 0xE8) == 0x08) {
69 // read DAC
70 return 0xFF; // read always returns 0xFF
71 } else {
72 return (port & 1) ? y8950.peekReg(registerLatch, time)
73 : y8950.peekStatus(time);
74 }
75}
76
77void MSXAudio::writeIO(word port, byte value, EmuTime::param time)
78{
79 if ((port & 0xE8) == 0x08) {
80 dacValue = value;
81 if (dacEnabled) {
82 assert(dac);
83 dac->writeDAC(dacValue, time);
84 }
85 } else if ((port & 0x01) == 0) {
86 // 0xC0 or 0xC2
87 registerLatch = value;
88 } else {
89 // 0xC1 or 0xC3
90 y8950.writeReg(registerLatch, value, time);
91 }
92}
93
94byte MSXAudio::readMem(word address, EmuTime::param time)
95{
96 return periphery->readMem(address, time);
97}
98byte MSXAudio::peekMem(word address, EmuTime::param time) const
99{
100 return periphery->peekMem(address, time);
101}
102void MSXAudio::writeMem(word address, byte value, EmuTime::param time)
103{
104 periphery->writeMem(address, value, time);
105}
106const byte* MSXAudio::getReadCacheLine(word start) const
107{
108 return periphery->getReadCacheLine(start);
109}
111{
112 return periphery->getWriteCacheLine(start);
113}
114
115void MSXAudio::enableDAC(bool enable, EmuTime::param time)
116{
117 if ((dacEnabled != enable) && dac) {
118 dacEnabled = enable;
119 byte value = dacEnabled ? dacValue : 0x80;
120 dac->writeDAC(value, time);
121 }
122}
123
124template<typename Archive>
125void MSXAudio::serialize(Archive& ar, unsigned /*version*/)
126{
127 ar.serializePolymorphic("periphery", *periphery);
128 ar.serialize("Y8950", y8950,
129 "registerLatch", registerLatch,
130 "dacValue", dacValue,
131 "dacEnabled", dacEnabled);
132
133 if constexpr (Archive::IS_LOADER) {
134 // restore dac status
135 if (dacEnabled) {
136 assert(dac);
137 dac->writeDAC(dacValue, getCurrentTime());
138 }
139 }
140}
143
144} // 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:42
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXAudio.cc:98
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:106
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:77
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:55
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:102
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:110
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MSXAudio.cc:94
void serialize(Archive &ar, unsigned version)
Definition: MSXAudio.cc:125
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXAudio.cc:48
~MSXAudio() override
Definition: MSXAudio.cc:28
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition: MSXAudio.cc:66
Y8950Periphery & createPeriphery(const std::string &soundDeviceName)
Creates a periphery object for this MSXAudio cartridge.
Definition: MSXAudio.cc:35
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:34
const DeviceConfig & getDeviceConfig2() const
Definition: MSXDevice.hh:235
virtual const std::string & getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:375
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:125
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:557
uint8_t peekReg(uint8_t rg, EmuTime::param time) const
Definition: Y8950.cc:1161
uint8_t peekStatus(EmuTime::param time) const
Definition: Y8950.cc:1191
uint8_t readStatus(EmuTime::param time) const
Definition: Y8950.cc:1184
void clearRam()
Definition: Y8950.cc:551
void writeReg(uint8_t rg, uint8_t data, EmuTime::param time)
Definition: Y8950.cc:903
uint8_t readReg(uint8_t rg, EmuTime::param time)
Definition: Y8950.cc:1146
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:730
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:1021