openMSX
RomHolyQuran2.cc
Go to the documentation of this file.
1// Holy Qu'ran cartridge
2// It is like an ASCII 8KB, but using the 5000h, 5400h, 5800h and 5C00h
3// addresses.
4//
5// This is very similar to RomHolyQuran, but this mapper type works with the
6// encrypted ROM content. Thanks to n_n for implementing it in meisei and
7// sharing his implementation with us (and pointing us to it).
8
9#include "RomHolyQuran2.hh"
10#include "MSXCPU.hh"
11#include "MSXException.hh"
12#include "enumerate.hh"
13#include "outer.hh"
14#include "ranges.hh"
15#include "serialize.hh"
16#include <array>
17
18namespace openmsx {
19
20// protection uses a simple rotation on databus, some lines inverted:
21// out0 = ~in3 out1 = in7 out2 = ~in5 out3 = ~in1
22// out4 = in0 out5 = in4 out6 = ~in2 out7 = in6
23static constexpr auto decryptLUT = [] {
24 std::array<byte, 256> result = {};
25 //for (auto [i, r] : enumerate(result)) { msvc bug
26 for (int i = 0; i < 256; ++i) {
27 result[i] = (((i << 4) & 0x50) |
28 ((i >> 3) & 0x05) |
29 ((i << 1) & 0xa0) |
30 ((i << 2) & 0x08) |
31 ((i >> 6) & 0x02)) ^ 0x4d;
32 }
33 return result;
34}();
35
37 : MSXRom(config, std::move(rom_))
38 , romBlocks(*this)
39{
40 if (rom.getSize() != 0x100000) { // 1MB
41 throw MSXException("Holy Quaran ROM should be exactly 1MB in size");
42 }
43 reset(EmuTime::dummy());
44}
45
46void RomHolyQuran2::reset(EmuTime::param /*time*/)
47{
48 ranges::fill(bank, &rom[0]);
49 decrypt = false;
50}
51
52byte RomHolyQuran2::readMem(word address, EmuTime::param time)
53{
54 byte result = RomHolyQuran2::peekMem(address, time);
55 if (!decrypt) [[unlikely]] {
56 if (getCPU().isM1Cycle(address)) {
57 // start decryption when we start executing the rom
58 decrypt = true;
59 }
60 }
61 return result;
62}
63
64byte RomHolyQuran2::peekMem(word address, EmuTime::param /*time*/) const
65{
66 if ((0x4000 <= address) && (address < 0xc000)) {
67 unsigned b = (address - 0x4000) >> 13;
68 byte raw = bank[b][address & 0x1fff];
69 return decrypt ? decryptLUT[raw] : raw;
70 } else {
71 return 0xff;
72 }
73}
74
75void RomHolyQuran2::writeMem(word address, byte value, EmuTime::param /*time*/)
76{
77 // TODO are switch addresses mirrored?
78 if ((0x5000 <= address) && (address < 0x6000)) {
79 byte region = (address >> 10) & 3;
80 bank[region] = &rom[(value & 127) * 0x2000];
81 }
82}
83
84const byte* RomHolyQuran2::getReadCacheLine(word address) const
85{
86 if ((0x4000 <= address) && (address < 0xc000)) {
87 return nullptr;
88 } else {
89 return unmappedRead;
90 }
91}
92
94{
95 if ((0x5000 <= address) && (address < 0x6000)) {
96 return nullptr;
97 } else {
98 return unmappedWrite;
99 }
100}
101
102template<typename Archive>
103void RomHolyQuran2::serialize(Archive& ar, unsigned /*version*/)
104{
105 // skip MSXRom base class
106 ar.template serializeBase<MSXDevice>(*this);
107
108 unsigned bb[4];
109 if constexpr (Archive::IS_LOADER) {
110 ar.serialize("banks", bb);
111 for (auto [i, b] : enumerate(bb)) {
112 bank[i] = &rom[(b & 127) * 0x2000];
113 }
114 } else {
115 for (auto [i, b] : enumerate(bb)) {
116 b = (bank[i] - &rom[0]) / 0x2000;
117 }
118 ar.serialize("banks", bb);
119 }
120
121 ar.serialize("decrypt", decrypt);
122}
125
126
127RomHolyQuran2::Blocks::Blocks(RomHolyQuran2& device_)
128 : RomBlockDebuggableBase(device_)
129{
130}
131
132byte RomHolyQuran2::Blocks::read(unsigned address)
133{
134 if ((address < 0x4000) || (address >= 0xc000)) return 255;
135 unsigned page = (address - 0x4000) / 0x2000;
136 auto& device = OUTER(RomHolyQuran2, romBlocks);
137 return (device.bank[page] - &device.rom[0]) / 0x2000;
138}
139
140} // namespace openmsx
MSXCPU & getCPU() const
Definition: MSXDevice.cc:130
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:301
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:302
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
RomHolyQuran2(const DeviceConfig &config, Rom &&rom)
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
void serialize(Archive &ar, unsigned version)
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.
void reset(EmuTime::param time) override
This method is called on reset.
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
unsigned getSize() const
Definition: Rom.hh:34
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
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
constexpr void fill(ForwardRange &&range, const T &value)
Definition: ranges.hh:256
STL namespace.
#define OUTER(type, member)
Definition: outer.hh:41
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1009