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 
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  invalidateMemCache(0x4000 * region, 0x4000);
66  } else {
67  // ROM block
68  setRom(region, value);
69  sramEnabled &= ~(1 << region);
70  }
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
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
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
void reset(EmuTime::param time) override
This method is called on reset.
Definition: RomAscii16_2.cc:33
RomAscii16_2(const DeviceConfig &config, Rom &&rom, SubType subType)
Definition: RomAscii16_2.cc:24
void setRom(byte region, unsigned block)
Selects a block of the ROM image for reading in a certain region.
Definition: RomBlocks.cc:98
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
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
STL namespace.
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:374
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
size_t size(std::string_view utf8)
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
void reset(EmuTime::param time) override
This method is called on reset.
Definition: RomAscii16kB.cc:22
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: RomBlocks.cc:59
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void serialize(Archive &ar, unsigned version)
Definition: RomAscii16_2.cc:90
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:65
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
std::unique_ptr< SRAM > sram
Definition: RomBlocks.hh:82
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458
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