openMSX
MSXPac.cc
Go to the documentation of this file.
1 #include "MSXPac.hh"
2 #include "CacheLine.hh"
3 #include "serialize.hh"
4 
5 namespace openmsx {
6 
7 static const char* const PAC_Header = "PAC2 BACKUP DATA";
8 
10  : MSXDevice(config)
11  , sram(getName() + " SRAM", 0x1FFE, config, PAC_Header)
12 {
13  reset(EmuTime::dummy());
14 }
15 
16 void MSXPac::reset(EmuTime::param /*time*/)
17 {
18  sramEnabled = false;
19  r1ffe = r1fff = 0xFF; // TODO check
20 }
21 
22 byte MSXPac::readMem(word address, EmuTime::param /*time*/)
23 {
24  byte result;
25  address &= 0x3FFF;
26  if (sramEnabled) {
27  if (address < 0x1FFE) {
28  result = sram[address];
29  } else if (address == 0x1FFE) {
30  result = r1ffe;
31  } else if (address == 0x1FFF) {
32  result = r1fff;
33  } else {
34  result = 0xFF;
35  }
36  } else {
37  result = 0xFF;
38  }
39  return result;
40 }
41 
42 const byte* MSXPac::getReadCacheLine(word address) const
43 {
44  address &= 0x3FFF;
45  if (sramEnabled) {
46  if (address < (0x1FFE & CacheLine::HIGH)) {
47  return &sram[address];
48  } else if (address == (0x1FFE & CacheLine::HIGH)) {
49  return nullptr;
50  } else {
51  return unmappedRead;
52  }
53  } else {
54  return unmappedRead;
55  }
56 }
57 
58 void MSXPac::writeMem(word address, byte value, EmuTime::param /*time*/)
59 {
60  address &= 0x3FFF;
61  switch (address) {
62  case 0x1FFE:
63  r1ffe = value;
64  checkSramEnable();
65  break;
66  case 0x1FFF:
67  r1fff = value;
68  checkSramEnable();
69  break;
70  default:
71  if (sramEnabled && (address < 0x1FFE)) {
72  sram.write(address, value);
73  }
74  }
75 }
76 
78 {
79  address &= 0x3FFF;
80  if (address == (0x1FFE & CacheLine::HIGH)) {
81  return nullptr;
82  }
83  if (sramEnabled && (address < 0x1FFE)) {
84  return nullptr;
85  } else {
86  return unmappedWrite;
87  }
88 }
89 
90 void MSXPac::checkSramEnable()
91 {
92  bool newEnabled = (r1ffe == 0x4D) && (r1fff == 0x69);
93  if (sramEnabled != newEnabled) {
94  sramEnabled = newEnabled;
95  invalidateMemCache(0x0000, 0x10000);
96  }
97 }
98 
99 template<typename Archive>
100 void MSXPac::serialize(Archive& ar, unsigned /*version*/)
101 {
102  ar.template serializeBase<MSXDevice>(*this);
103  ar.serialize("SRAM", sram,
104  "r1ffe", r1ffe,
105  "r1fff", r1fff);
106  if (ar.isLoader()) {
107  checkSramEnable();
108  }
109 }
111 REGISTER_MSXDEVICE(MSXPac, "PAC");
112 
113 } // namespace openmsx
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: MSXPac.cc:58
MSXPac(const DeviceConfig &config)
Definition: MSXPac.cc:9
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void write(unsigned addr, byte value)
Definition: SRAM.cc:67
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXPac.cc:16
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing...
Definition: MSXPac.cc:77
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading...
Definition: MSXPac.cc:42
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MSXPac.cc:22
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:31
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:274
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1006
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:275
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
void serialize(Archive &ar, unsigned version)
Definition: MSXPac.cc:100
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458