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