| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "RomHalnote.hh" |
| 27 |
|
| 28 |
#include "SRAM.hh" |
| 29 |
|
| 30 |
#include "CacheLine.hh" |
| 31 |
#include "MSXException.hh" |
| 32 |
#include "serialize.hh" |
| 33 |
|
| 34 |
#include "one_of.hh" |
| 35 |
#include "xrange.hh" |
| 36 |
|
| 37 |
#include <memory> |
| 38 |
|
| 39 |
namespace openmsx { |
| 40 |
|
| 41 |
RomHalnote::RomHalnote(const DeviceConfig& config, Rom&& rom_) |
| 42 |
: Rom8kBBlocks(config, std::move(rom_)) |
| 43 |
{ |
| 44 |
if (rom.size() != 0x100000) { |
| 45 |
throw MSXException( |
| 46 |
"Rom for HALNOTE mapper must be exactly 1MB in size."); |
| 47 |
} |
| 48 |
sram = std::make_unique<SRAM>(getName() + " SRAM", 0x4000, config); |
| 49 |
reset(EmuTime::dummy()); |
| 50 |
} |
| 51 |
|
| 52 |
void RomHalnote::reset(EmuTime ) |
| 53 |
{ |
| 54 |
subBanks[0] = subBanks[1] = 0; |
| 55 |
sramEnabled = false; |
| 56 |
subMapperEnabled = false; |
| 57 |
|
| 58 |
setUnmapped(0); |
| 59 |
setUnmapped(1); |
| 60 |
for (auto i : xrange(2, 6)) { |
| 61 |
setRom(i, 0); |
| 62 |
} |
| 63 |
setUnmapped(6); |
| 64 |
setUnmapped(7); |
| 65 |
} |
| 66 |
|
| 67 |
const byte* RomHalnote::getReadCacheLine(uint16_t address) const |
| 68 |
{ |
| 69 |
if (subMapperEnabled && (0x7000 <= address) && (address < 0x8000)) { |
| 70 |
|
| 71 |
int subBank = address < 0x7800 ? 0 : 1; |
| 72 |
return &rom[0x80000 + subBanks[subBank] * 0x800 + (address & 0x7FF)]; |
| 73 |
} else { |
| 74 |
|
| 75 |
return Rom8kBBlocks::getReadCacheLine(address); |
| 76 |
} |
| 77 |
} |
| 78 |
byte RomHalnote::readMem(uint16_t address, EmuTime ) |
| 79 |
{ |
| 80 |
|
| 81 |
return *RomHalnote::getReadCacheLine(address); |
| 82 |
} |
| 83 |
|
| 84 |
void RomHalnote::writeMem(uint16_t address, byte value, EmuTime ) |
| 85 |
{ |
| 86 |
if (address < 0x4000) { |
| 87 |
|
| 88 |
if (sramEnabled) { |
| 89 |
sram->write(address, value); |
| 90 |
} |
| 91 |
} else if (address < 0xC000) { |
| 92 |
if (address == one_of(0x77FF, 0x7FFF)) { |
| 93 |
|
| 94 |
int subBank = address < 0x7800 ? 0 : 1; |
| 95 |
if (subBanks[subBank] != value) { |
| 96 |
subBanks[subBank] = value; |
| 97 |
if (subMapperEnabled) { |
| 98 |
invalidateDeviceRCache( |
| 99 |
0x7000 + subBank * 0x800, 0x800); |
| 100 |
} |
| 101 |
} |
| 102 |
} else if ((address & 0x1FFF) == 0x0FFF) { |
| 103 |
|
| 104 |
auto bank = address >> 13; |
| 105 |
setRom(bank, value); |
| 106 |
if (bank == 2) { |
| 107 |
|
| 108 |
bool newSramEnabled = (value & 0x80) != 0; |
| 109 |
if (newSramEnabled != sramEnabled) { |
| 110 |
sramEnabled = newSramEnabled; |
| 111 |
if (sramEnabled) { |
| 112 |
setBank(0, &(*sram)[0x0000], value); |
| 113 |
setBank(1, &(*sram)[0x2000], value); |
| 114 |
} else { |
| 115 |
setUnmapped(0); |
| 116 |
setUnmapped(1); |
| 117 |
} |
| 118 |
|
| 119 |
invalidateDeviceWCache(0x0000, 0x4000); |
| 120 |
} |
| 121 |
} else if (bank == 3) { |
| 122 |
|
| 123 |
subMapperEnabled = (value & 0x80) != 0; |
| 124 |
if (subMapperEnabled) { |
| 125 |
invalidateDeviceRCache(0x7000, 0x1000); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
byte* RomHalnote::getWriteCacheLine(uint16_t address) |
| 133 |
{ |
| 134 |
if (address < 0x4000) { |
| 135 |
|
| 136 |
if (sramEnabled) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
} else if (address < 0xC000) { |
| 140 |
if ((address & CacheLine::HIGH) == one_of(0x77FF & CacheLine::HIGH, |
| 141 |
0x7FFF & CacheLine::HIGH)) { |
| 142 |
|
| 143 |
return nullptr; |
| 144 |
} else if ((address & 0x1FFF & CacheLine::HIGH) == |
| 145 |
(0x0FFF & CacheLine::HIGH)) { |
| 146 |
|
| 147 |
return nullptr; |
| 148 |
} |
| 149 |
} |
| 150 |
return unmappedWrite.data(); |
| 151 |
} |
| 152 |
|
| 153 |
template<typename Archive> |
| 154 |
void RomHalnote::serialize(Archive& ar, unsigned ) |
| 155 |
{ |
| 156 |
ar.template serializeBase<Rom8kBBlocks>(*this); |
| 157 |
ar.serialize("subBanks", subBanks, |
| 158 |
"sramEnabled", sramEnabled, |
| 159 |
"subMapperEnabled", subMapperEnabled); |
| 160 |
|
| 161 |
} |
| 162 |
INSTANTIATE_SERIALIZE_METHODS(RomHalnote); |
| 163 |
REGISTER_MSXDEVICE(RomHalnote, "RomHalnote"); |
| 164 |
|
| 165 |
} |