openMSX
RomAscii8_8.cc
Go to the documentation of this file.
1 // ASCII 8kB based cartridges with SRAM
2 // - ASCII8-8 / KOEI-8 / KOEI-32 / WIZARDRY
3 //
4 // The address to change banks:
5 // bank 1: 0x6000 - 0x67ff (0x6000 used)
6 // bank 2: 0x6800 - 0x6fff (0x6800 used)
7 // bank 3: 0x7000 - 0x77ff (0x7000 used)
8 // bank 4: 0x7800 - 0x7fff (0x7800 used)
9 //
10 // To select SRAM set bit 7 (for WIZARDRY) or the bit just above the
11 // rom selection bits (bit 5/6/7 depending on ROM size). For KOEI-32
12 // the lowest bits indicate which SRAM page is selected. SRAM is
13 // readable at 0x8000-0xBFFF. For the KOEI-x types SRAM is also
14 // readable at 0x4000-0x5FFF
15 
16 #include "RomAscii8_8.hh"
17 #include "Rom.hh"
18 #include "SRAM.hh"
19 #include "serialize.hh"
20 #include "memory.hh"
21 
22 namespace openmsx {
23 
25  std::unique_ptr<Rom> rom_, SubType subType)
26  : Rom8kBBlocks(config, std::move(rom_))
27  , sramEnableBit((subType == WIZARDRY) ? 0x80
28  : rom->getSize() / 0x2000)
29  , sramPages(((subType == KOEI_8) || (subType == KOEI_32))
30  ? 0x34 : 0x30)
31 {
32  sram = make_unique<SRAM>(
33  getName() + " SRAM",
34  (subType == KOEI_32) ? 0x8000 : 0x2000, config);
36 }
37 
39 {
40 }
41 
43 {
44  setUnmapped(0);
45  setUnmapped(1);
46  for (int i = 2; i < 6; i++) {
47  setRom(i, 0);
48  }
49  setUnmapped(6);
50  setUnmapped(7);
51 
52  sramEnabled = 0;
53 }
54 
55 void RomAscii8_8::writeMem(word address, byte value, EmuTime::param /*time*/)
56 {
57  if ((0x6000 <= address) && (address < 0x8000)) {
58  // bank switching
59  byte region = ((address >> 11) & 3) + 2;
60  if (value & sramEnableBit) {
61  sramEnabled |= (1 << region) & sramPages;
62  sramBlock[region] = value & ((sram->getSize() / 0x2000) - 1);
63  setBank(region, &(*sram)[sramBlock[region] * 0x2000], value);
64  } else {
65  sramEnabled &= ~(1 << region);
66  setRom(region, value);
67  }
68  } else {
69  byte bank = address >> 13;
70  if ((1 << bank) & sramEnabled) {
71  // write to SRAM
72  word addr = (sramBlock[bank] * 0x2000) + (address & 0x1FFF);
73  sram->write(addr, value);
74  }
75  }
76 }
77 
79 {
80  if ((0x6000 <= address) && (address < 0x8000)) {
81  // bank switching
82  return nullptr;
83  } else if ((1 << (address >> 13)) & sramEnabled) {
84  // write to SRAM
85  return nullptr;
86  } else {
87  return unmappedWrite;
88  }
89 }
90 
91 template<typename Archive>
92 void RomAscii8_8::serialize(Archive& ar, unsigned /*version*/)
93 {
94  ar.template serializeBase<Rom8kBBlocks>(*this);
95  ar.serialize("sramEnabled", sramEnabled);
96  ar.serialize("sramBlock", sramBlock);
97 }
99 REGISTER_MSXDEVICE(RomAscii8_8, "RomAscii8_8");
100 
101 } // namespace openmsx