openMSX
MSXMusic.cc
Go to the documentation of this file.
1#include "MSXMusic.hh"
2#include "CacheLine.hh"
3#include "MSXException.hh"
4#include "serialize.hh"
5#include <bit>
6
7namespace openmsx {
8
9// class MSXMusicBase
10
12 : MSXDevice(config)
13 , rom(getName() + " ROM", "rom", config)
14 , ym2413(getName(), config)
15{
16 auto sz = rom.size();
17 if (!std::has_single_bit(sz)) {
18 throw MSXException("MSX-Music ROM-size must be a non-zero power of two");
19 }
21}
22
23void MSXMusicBase::reset(EmuTime::param time)
24{
25 ym2413.reset(time);
26}
27
28void MSXMusicBase::writeIO(word port, byte value, EmuTime::param time)
29{
30 writePort(port & 1, value, time);
31}
32
33void MSXMusicBase::writePort(bool port, byte value, EmuTime::param time)
34{
35 ym2413.writePort(port, value, time);
36}
37
38byte MSXMusicBase::peekMem(word address, EmuTime::param /*time*/) const
39{
40 return *MSXMusicBase::getReadCacheLine(address);
41}
42
43byte MSXMusicBase::readMem(word address, EmuTime::param time)
44{
45 return peekMem(address, time);
46}
47
48const byte* MSXMusicBase::getReadCacheLine(word start) const
49{
50 return &rom[start & (rom.size() - 1)];
51}
52
53// version 1: initial version
54// version 2: refactored YM2413 class structure
55// version 3: removed 'registerLatch' (moved to YM2413 cores)
56template<typename Archive>
57void 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
80template<typename Archive>
81void 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
110void MSXMusicWX::reset(EmuTime::param time)
111{
113 control = 0;
114}
115
116byte 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
127byte MSXMusicWX::readMem(word address, EmuTime::param time)
128{
129 return peekMem(address, time);
130}
131
132const 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.data();
140 }
141}
142
143void 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.data();
157 }
158}
159
160template<typename Archive>
161void MSXMusicWX::serialize(Archive& ar, unsigned version)
162{
163 ar.template serializeInlinedBase<MSXMusicBase>(*this, version);
164 ar.serialize("control", control);
165}
168
169} // namespace openmsx
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:34
void invalidateDeviceRCache()
Definition: MSXDevice.hh:211
static std::array< byte, 0x10000 > unmappedRead
Definition: MSXDevice.hh:302
static std::array< byte, 0x10000 > unmappedWrite
Definition: MSXDevice.hh:303
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:125
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
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
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXMusic.cc:23
void writePort(bool port, byte value, EmuTime::param time)
Definition: MSXMusic.cc:33
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
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:57
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXMusic.cc:38
MSXMusicBase(const DeviceConfig &config)
Definition: MSXMusic.cc:11
MSXMusicWX(const DeviceConfig &config)
Definition: MSXMusic.cc:104
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
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MSXMusic.cc:116
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
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:161
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXMusic.cc:110
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
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
MSXMusic(const DeviceConfig &config)
Definition: MSXMusic.cc:75
void serialize(Archive &ar, unsigned version)
Definition: MSXMusic.cc:81
auto size() const
Definition: Rom.hh:36
void writePort(bool port, byte value, EmuTime::param time)
Definition: YM2413.cc:86
void serialize(Archive &ar, unsigned version)
Definition: YM2413.cc:124
void reset(EmuTime::param time)
Definition: YM2413.cc:80
constexpr unsigned HIGH
Definition: CacheLine.hh:10
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