openMSX
RomAscii16_2.cc
Go to the documentation of this file.
1 // ASCII 16kB based cartridges with SRAM
2 //
3 // Examples 2kB SRAM: Daisenryaku, Harry Fox MSX Special, Hydlide 2, Jyansei
4 // Examples 8kB SRAM: A-Train
5 //
6 // this type is is almost completely a ASCII16 cartrdige
7 // However, it has 2 or 8kB of SRAM (and 128 kB ROM)
8 // Use value 0x10 to select the SRAM.
9 // SRAM in page 1 => read-only
10 // SRAM in page 2 => read-write
11 // The SRAM is mirrored in the 16 kB block
12 //
13 // The address to change banks (from ASCII16):
14 // first 16kb: 0x6000 - 0x67FF (0x6000 used)
15 // second 16kb: 0x7000 - 0x77FF (0x7000 and 0x77FF used)
16 
17 #include "RomAscii16_2.hh"
18 #include "SRAM.hh"
19 #include "serialize.hh"
20 #include <memory>
21 
22 namespace openmsx {
23 
24 RomAscii16_2::RomAscii16_2(const DeviceConfig& config, Rom&& rom_, SubType subType)
25  : RomAscii16kB(config, std::move(rom_))
26 {
27  unsigned size = (subType == ASCII16_8) ? 0x2000 // 8kB
28  : 0x0800; // 2kB
29  sram = std::make_unique<SRAM>(getName() + " SRAM", size, config);
30  reset(EmuTime::dummy());
31 }
32 
33 void RomAscii16_2::reset(EmuTime::param time)
34 {
35  sramEnabled = 0;
36  RomAscii16kB::reset(time);
37 }
38 
39 byte RomAscii16_2::readMem(word address, EmuTime::param time)
40 {
41  if ((1 << (address >> 14)) & sramEnabled) {
42  return (*sram)[address & (sram->getSize() - 1)];
43  } else {
44  return RomAscii16kB::readMem(address, time);
45  }
46 }
47 
48 const byte* RomAscii16_2::getReadCacheLine(word address) const
49 {
50  if ((1 << (address >> 14)) & sramEnabled) {
51  return &(*sram)[address & (sram->getSize() - 1)];
52  } else {
53  return RomAscii16kB::getReadCacheLine(address);
54  }
55 }
56 
57 void RomAscii16_2::writeMem(word address, byte value, EmuTime::param /*time*/)
58 {
59  if ((0x6000 <= address) && (address < 0x7800) && !(address & 0x0800)) {
60  // bank switch
61  byte region = ((address >> 12) & 1) + 1;
62  if (value == 0x10) {
63  // SRAM block
64  sramEnabled |= (1 << region);
65  } else {
66  // ROM block
67  setRom(region, value);
68  sramEnabled &= ~(1 << region);
69  }
70  invalidateDeviceRWCache(0x4000 * region, 0x4000);
71  } else {
72  // write sram
73  if ((1 << (address >> 14)) & sramEnabled & 0x04) {
74  sram->write(address & (sram->getSize() - 1), value);
75  }
76  }
77 }
78 
80 {
81  if ((1 << (address >> 14)) & sramEnabled & 0x04) {
82  // write sram
83  return nullptr;
84  } else {
85  return RomAscii16kB::getWriteCacheLine(address);
86  }
87 }
88 
89 template<typename Archive>
90 void RomAscii16_2::serialize(Archive& ar, unsigned /*version*/)
91 {
92  ar.template serializeBase<RomAscii16kB>(*this);
93  ar.serialize("sramEnabled", sramEnabled);
94 }
96 REGISTER_MSXDEVICE(RomAscii16_2, "RomAscii16_2");
97 
98 } // namespace openmsx
openmsx::RomAscii16_2::ASCII16_8
@ ASCII16_8
Definition: RomAscii16_2.hh:11
serialize.hh
openmsx::DeviceConfig
Definition: DeviceConfig.hh:19
utf8::unchecked::size
size_t size(std::string_view utf8)
Definition: utf8_unchecked.hh:227
openmsx::RomAscii16_2::readMem
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: RomAscii16_2.cc:39
openmsx::RomAscii16_2::getWriteCacheLine
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: RomAscii16_2.cc:79
openmsx::RomBlocks::sram
std::unique_ptr< SRAM > sram
Definition: RomBlocks.hh:83
openmsx::RomAscii16_2
Definition: RomAscii16_2.hh:8
openmsx::RomAscii16kB
Definition: RomAscii16kB.hh:8
openmsx::REGISTER_MSXDEVICE
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
openmsx::RomAscii16_2::SubType
SubType
Definition: RomAscii16_2.hh:11
openmsx::RomAscii16kB::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: RomAscii16kB.cc:22
openmsx::RomBlocks::getReadCacheLine
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
Definition: RomBlocks.cc:71
openmsx::RomBlocks::setRom
void setRom(byte region, unsigned block)
Selects a block of the ROM image for reading in a certain region.
Definition: RomBlocks.cc:104
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::Rom
Definition: Rom.hh:20
openmsx::RomAscii16_2::writeMem
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: RomAscii16_2.cc:57
openmsx::MSXDevice::invalidateDeviceRWCache
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:208
SRAM.hh
openmsx::RomBlocks::readMem
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: RomBlocks.cc:65
openmsx::MSXDevice::getName
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:381
openmsx::RomAscii16_2::getReadCacheLine
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading.
Definition: RomAscii16_2.cc:48
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
RomAscii16_2.hh
openmsx::RomAscii16_2::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: RomAscii16_2.cc:33
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::RomAscii16_2::RomAscii16_2
RomAscii16_2(const DeviceConfig &config, Rom &&rom, SubType subType)
Definition: RomAscii16_2.cc:24
openmsx::RomAscii16kB::getWriteCacheLine
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: RomAscii16kB.cc:38
openmsx::RomAscii16_2::serialize
void serialize(Archive &ar, unsigned version)
Definition: RomAscii16_2.cc:90