OXYGENHUB
◆ main openmsx / src / memory / RomDooly.cc
2.5 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
//
2
// Agigongnyong Dooly
3
//
4
5
#include "RomDooly.hh"
6
#include "CacheLine.hh"
7
#include "MSXException.hh"
8
#include "serialize.hh"
9
10
namespace openmsx {
11
12
RomDooly::RomDooly(const DeviceConfig& config, Rom&& rom_)
13
	: MSXRom(config, std::move(rom_))
14
	, romBlockDebug(*this, std::span{&conversion, 1}, 0x4000, 0x8000, 15)
15
{
16
	if (rom.size() != 0x8000) {
17
		throw MSXException("Dooly ROM-size must be exactly 32kB");
18
	}
19
	reset(EmuTime::dummy());
20
}
21
22
void RomDooly::reset(EmuTime /*time*/)
23
{
24
	conversion = 0;
25
}
26
27
byte RomDooly::peekMem(uint16_t address, EmuTime /*time*/) const
28
{
29
	if ((0x4000 <= address) && (address < 0xc000)) {
30
		byte value = rom[address - 0x4000];
31
		switch (conversion) {
32
		case 0:
33
			return value;
34
		case 1:
35
			return (value & 0xf8) | (value << 2 & 0x04) | (value >> 1 & 0x03);
36
		case 4:
37
			return (value & 0xf8) | (value >> 2 & 0x01) | (value << 1 & 0x06);
38
		case 2:
39
		case 5:
40
		case 6:
41
			switch (value & 0x07) {
42
			case 1:
43
			case 2:
44
			case 4:
45
				return value & 0xf8;
46
			case 3:
47
			case 5:
48
			case 6:
49
				if (conversion == 2) return (value & 0xf8) | (((value << 2 & 0x04) | (value >> 1 & 0x03)) ^ 0x07);
50
				if (conversion == 5) return value ^ 0x07;
51
				if (conversion == 6) return (value & 0xf8) | (((value >> 2 & 0x01) | (value << 1 & 0x06)) ^ 0x07);
52
				[[fallthrough]];
53
			default:
54
				return value;
55
			}
56
		default:
57
			return value | 0x07;
58
		}
59
	}
60
	return 0xff;
61
}
62
63
byte RomDooly::readMem(uint16_t address, EmuTime time)
64
{
65
	return peekMem(address, time);
66
}
67
68
void RomDooly::writeMem(uint16_t address, byte value, EmuTime /*time*/)
69
{
70
	// TODO: To what region does the real cartridge react?
71
	// * Using the full region [0x4000,0xc000) interferes with the
72
	//   RAM-check routine of many MSX machines (game doesn't boot)
73
	// * The game seems to write at many different addresses. The min/max
74
	//   I've seen is 0x8780/0x8f86. So my best *guess* is to extend this
75
	//   region to [0x8000,0x9000).
76
	// * This smaller region seems to make the game work (on the above
77
	//   machines that failed before).
78
	if ((0x8000 <= address) && (address < 0x9000)) {
79
		conversion = value & 0x07;
80
	}
81
}
82
83
byte* RomDooly::getWriteCacheLine(uint16_t address)
84
{
85
	if (((0x4000 & CacheLine::HIGH) <= address) && (address < (0xc000 & CacheLine::HIGH))) {
86
		return nullptr;
87
	} else {
88
		return unmappedWrite.data();
89
	}
90
}
91
92
template<typename Archive>
93
void RomDooly::serialize(Archive& ar, unsigned /*version*/)
94
{
95
	ar.template serializeBase<MSXDevice>(*this);
96
	ar.serialize("conversion", conversion);
97
}
98
INSTANTIATE_SERIALIZE_METHODS(RomDooly);
99
REGISTER_MSXDEVICE(RomDooly, "RomDooly");
100
101
} // namespace openmsx