openMSX
MSXMusic.cc
Go to the documentation of this file.
1 #include "MSXMusic.hh"
2 #include "CacheLine.hh"
3 #include "Math.hh"
4 #include "MSXException.hh"
5 #include "serialize.hh"
6 
7 namespace openmsx {
8 
9 // class MSXMusicBase
10 
12  : MSXDevice(config)
13  , rom(getName() + " ROM", "rom", config)
14  , ym2413(getName(), config)
15 {
16  auto sz = rom.getSize();
17  if ((sz == 0) || !Math::ispow2(sz)) {
18  throw MSXException("MSX-Music ROM-size must be a non-zero power of two");
19  }
21 }
22 
23 void MSXMusicBase::reset(EmuTime::param time)
24 {
25  ym2413.reset(time);
26 }
27 
28 void MSXMusicBase::writeIO(word port, byte value, EmuTime::param time)
29 {
30  writePort(port & 1, value, time);
31 }
32 
33 void MSXMusicBase::writePort(bool port, byte value, EmuTime::param time)
34 {
35  ym2413.writePort(port, value, time);
36 }
37 
38 byte MSXMusicBase::peekMem(word address, EmuTime::param /*time*/) const
39 {
40  return *MSXMusicBase::getReadCacheLine(address);
41 }
42 
43 byte MSXMusicBase::readMem(word address, EmuTime::param time)
44 {
45  return peekMem(address, time);
46 }
47 
48 const byte* MSXMusicBase::getReadCacheLine(word start) const
49 {
50  return &rom[start & (rom.getSize() - 1)];
51 }
52 
53 // version 1: initial version
54 // version 2: refactored YM2413 class structure
55 // version 3: removed 'registerLatch' (moved to YM2413 cores)
56 template<typename Archive>
57 void MSXMusicBase::serialize(Archive& ar, unsigned version)
58 {
59  ar.template serializeBase<MSXDevice>(*this);
60 
61  if (ar.versionAtLeast(version, 2)) {
62  ar.serialize("ym2413", ym2413);
63  } else {
64  // In older versions, the 'ym2413' level was missing, delegate
65  // directly to YM2413 without emitting the 'ym2413' tag.
66  ym2413.serialize(ar, version);
67  }
68 }
70 
71 
72 
73 // class MSXMusic
74 
76  : MSXMusicBase(config)
77 {
78 }
79 
80 template<typename Archive>
81 void MSXMusic::serialize(Archive& ar, unsigned version)
82 {
83  ar.template serializeInlinedBase<MSXMusicBase>(*this, version);
84 }
87 
88 
89 
90 // class MSXMusicWX
91 
92 // Thanks to NYYRIKKI for figuring this out:
93 // - writes to 0x7ff0-0x7fff set a control register (mirrored 16x)
94 // - writes to any other memory region have no effect
95 // - bit 0 of this control register can be used to disable reading the ROM
96 // (0=enable, 1=disabled), other bits seem to have no effect
97 // - reading from 0x7ff0-0x7fff return the last written value OR 0xfc, IOW the
98 // lower two bits are the last written value, higher bits always read 1
99 // - reading from 0x4000-0x7fef returns the content of the ROM if the ROM is
100 // enabled (bit 0 of the control register = 0), when the ROM is disabled
101 // reads return 0xff
102 // - reading any other memory location returns 0xff
103 
105  : MSXMusicBase(config)
106 {
108 }
109 
110 void MSXMusicWX::reset(EmuTime::param time)
111 {
112  MSXMusicBase::reset(time);
113  control = 0;
114 }
115 
116 byte MSXMusicWX::peekMem(word address, EmuTime::param time) const
117 {
118  if ((0x7FF0 <= address) && (address < 0x8000)) {
119  return control | 0xFC;
120  } else if ((control & 1) == 0) {
121  return MSXMusicBase::peekMem(address, time);
122  } else {
123  return 0xFF;
124  }
125 }
126 
127 byte MSXMusicWX::readMem(word address, EmuTime::param time)
128 {
129  return peekMem(address, time);
130 }
131 
132 const byte* MSXMusicWX::getReadCacheLine(word start) const
133 {
134  if ((0x7FF0 & CacheLine::HIGH) == start) {
135  return nullptr;
136  } else if ((control & 1) == 0) {
137  return MSXMusicBase::getReadCacheLine(start);
138  } else {
139  return unmappedRead;
140  }
141 }
142 
143 void MSXMusicWX::writeMem(word address, byte value, EmuTime::param /*time*/)
144 {
145  if ((0x7FF0 <= address) && (address < 0x8000)) {
146  control = value & 3;
148  }
149 }
150 
152 {
153  if ((0x7FF0 & CacheLine::HIGH) == start) {
154  return nullptr;
155  } else {
156  return unmappedWrite;
157  }
158 }
159 
160 template<typename Archive>
161 void MSXMusicWX::serialize(Archive& ar, unsigned version)
162 {
163  ar.template serializeInlinedBase<MSXMusicBase>(*this, version);
164  ar.serialize("control", control);
165 }
168 
169 } // namespace openmsx
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:32
openmsx::MSXMusicBase
Definition: MSXMusic.hh:12
openmsx::MSXMusicWX::readMem
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MSXMusic.cc:127
openmsx::MSXMusicWX::peekMem
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXMusic.cc:116
serialize.hh
openmsx::YM2413::reset
void reset(EmuTime::param time)
Definition: YM2413.cc:79
openmsx::MSXMusicWX
Definition: MSXMusic.hh:48
openmsx::DeviceConfig
Definition: DeviceConfig.hh:20
openmsx::MSXDevice::unmappedWrite
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:301
MSXException.hh
openmsx::MSXException
Definition: MSXException.hh:10
Math::ispow2
constexpr bool ispow2(T x) noexcept
Is the given number an integral power of two? That is, does it have exactly one 1-bit in binary repre...
Definition: Math.hh:57
openmsx::MSXMusicBase::MSXMusicBase
MSXMusicBase(const DeviceConfig &config)
Definition: MSXMusic.cc:11
openmsx::CacheLine::HIGH
constexpr unsigned HIGH
Definition: CacheLine.hh:10
openmsx::REGISTER_MSXDEVICE
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
openmsx::MSXMusicBase::getReadCacheLine
const byte * getReadCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
Definition: MSXMusic.cc:48
openmsx::MSXMusicBase::writePort
void writePort(bool port, byte value, EmuTime::param time)
Definition: MSXMusic.cc:33
openmsx::MSXMusicBase::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:57
openmsx::Keys::getName
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:740
openmsx::YM2413::writePort
void writePort(bool port, byte value, EmuTime::param time)
Definition: YM2413.cc:85
openmsx::MSXDevice::getCurrentTime
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:131
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
CacheLine.hh
openmsx::MSXMusicBase::writeIO
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: MSXMusic.cc:28
openmsx::MSXDevice::invalidateDeviceRCache
void invalidateDeviceRCache()
Definition: MSXDevice.hh:209
openmsx::MSXMusicBase::readMem
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MSXMusic.cc:43
openmsx::MSXDevice::unmappedRead
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:300
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXMusicWX::MSXMusicWX
MSXMusicWX(const DeviceConfig &config)
Definition: MSXMusic.cc:104
openmsx::MSXMusicWX::getReadCacheLine
const byte * getReadCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
Definition: MSXMusic.cc:132
openmsx::MSXMusicBase::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXMusic.cc:23
openmsx::MSXMusicWX::writeMem
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: MSXMusic.cc:143
openmsx::MSXMusic
Definition: MSXMusic.hh:36
MSXMusic.hh
openmsx::MSXMusicWX::getWriteCacheLine
byte * getWriteCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MSXMusic.cc:151
openmsx::MSXMusicWX::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXMusic.cc:110
openmsx::MSXMusic::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:81
openmsx::MSXMusicBase::peekMem
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXMusic.cc:38
Math.hh
openmsx::MSXMusic::MSXMusic
MSXMusic(const DeviceConfig &config)
Definition: MSXMusic.cc:75
openmsx::Rom::getSize
unsigned getSize() const
Definition: Rom.hh:32
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::MSXMusicWX::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:161
openmsx::YM2413::serialize
void serialize(Archive &ar, unsigned version)
Definition: YM2413.cc:115
openmsx::MSXMusicBase::rom
Rom rom
Definition: MSXMusic.hh:29
openmsx::MSXMusicBase::ym2413
YM2413 ym2413
Definition: MSXMusic.hh:30