MSXMatsushita.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "MSXMatsushita.hh"
  2. #include "MSXCPU.hh"
  3. #include "SRAM.hh"
  4. #include "VDP.hh"
  5. #include "MSXCPUInterface.hh"
  6. #include "CliComm.hh"
  7. #include "MSXException.hh"
  8. #include "serialize.hh"
  9. namespace openmsx {
  10. constexpr byte ID = 0x08;
  11. MSXMatsushita::MSXMatsushita(const DeviceConfig& config)
  12. : MSXDevice(config)
  13. , MSXSwitchedDevice(getMotherBoard(), ID)
  14. , cpu(getCPU()) // used frequently, so cache it
  15. , vdp(nullptr)
  16. , lastTime(EmuTime::zero())
  17. , firmwareSwitch(config)
  18. , sram(config.findChild("sramname") ? std::make_unique<SRAM>(getName() + " SRAM", 0x800, config) : nullptr)
  19. , turboAvailable(config.getChildDataAsBool("hasturbo", false))
  20. , turboEnabled(false)
  21. {
  22. // TODO find out what ports 0x41 0x45 0x46 are used for
  23. reset(EmuTime::dummy());
  24. }
  25. void MSXMatsushita::init()
  26. {
  27. MSXDevice::init();
  28. const auto& refs = getReferences();
  29. vdp = !refs.empty() ? dynamic_cast<VDP*>(refs[0]) : nullptr;
  30. if (!vdp) {
  31. // No (valid) reference to the VDP found. We do allow this to
  32. // model MSX machines where the Matsushita device does not
  33. // influence the VDP timing. It's not know whether such
  34. // machines actually exist.
  35. return;
  36. }
  37. // Wrap the VDP ports.
  38. auto& cpuInterface = getCPUInterface();
  39. bool error = false;
  40. for (int i = 0; i < 2; ++i) {
  41. error |= !cpuInterface.replace_IO_In (0x98 + i, vdp, this);
  42. }
  43. for (int i = 0; i < 4; ++i) {
  44. error |= !cpuInterface.replace_IO_Out(0x98 + i, vdp, this);
  45. }
  46. if (error) {
  47. unwrap();
  48. throw MSXException(
  49. "Invalid Matsushita configuration: "
  50. "VDP not on IO-ports 0x98-0x9B.");
  51. }
  52. }
  53. MSXMatsushita::~MSXMatsushita()
  54. {
  55. if (!vdp) return;
  56. unwrap();
  57. }
  58. void MSXMatsushita::unwrap()
  59. {
  60. // Unwrap the VDP ports.
  61. auto& cpuInterface = getCPUInterface();
  62. for (int i = 0; i < 2; ++i) {
  63. cpuInterface.replace_IO_In (0x98 + i, this, vdp);
  64. }
  65. for (int i = 0; i < 4; ++i) {
  66. cpuInterface.replace_IO_Out(0x98 + i, this, vdp);
  67. }
  68. }
  69. void MSXMatsushita::reset(EmuTime::param /*time*/)
  70. {
  71. color1 = color2 = pattern = address = 0; // TODO check this
  72. }
  73. byte MSXMatsushita::readSwitchedIO(word port, EmuTime::param time)
  74. {
  75. // TODO: Port 7 and 8 can be read as well.
  76. byte result = peekSwitchedIO(port, time);
  77. switch (port & 0x0F) {
  78. case 3:
  79. pattern = (pattern << 2) | (pattern >> 6);
  80. break;
  81. case 9:
  82. address = (address + 1) & 0x1FFF;
  83. break;
  84. }
  85. return result;
  86. }
  87. byte MSXMatsushita::peekSwitchedIO(word port, EmuTime::param /*time*/) const
  88. {
  89. byte result;
  90. switch (port & 0x0F) {
  91. case 0:
  92. result = byte(~ID);
  93. break;
  94. case 1:
  95. result = firmwareSwitch.getStatus() ? 0x7F : 0xFF;
  96. // bit 0: turbo status, 0=on
  97. if (turboEnabled) {
  98. result &= ~0x01;
  99. }
  100. // bit 2: 0 = turbo feature available
  101. if (turboAvailable) {
  102. result &= ~0x04;
  103. }
  104. break;
  105. case 3:
  106. result = (((pattern & 0x80) ? color2 : color1) << 4)
  107. | ((pattern & 0x40) ? color2 : color1);
  108. break;
  109. case 9:
  110. if (address < 0x800 && sram) {
  111. result = (*sram)[address];
  112. } else {
  113. result = 0xFF;
  114. }
  115. break;
  116. default:
  117. result = 0xFF;
  118. }
  119. return result;
  120. }
  121. void MSXMatsushita::writeSwitchedIO(word port, byte value, EmuTime::param /*time*/)
  122. {
  123. switch (port & 0x0F) {
  124. case 1:
  125. // the turboEnabled flag works even though no turbo is available
  126. if (value & 1) {
  127. // bit0 = 1 -> 3.5MHz
  128. if (turboAvailable) {
  129. getCPU().setZ80Freq(3579545);
  130. }
  131. turboEnabled = false;
  132. } else {
  133. // bit0 = 0 -> 5.3MHz
  134. if (turboAvailable) {
  135. getCPU().setZ80Freq(5369318); // 3579545 * 3/2
  136. }
  137. turboEnabled = true;
  138. }
  139. break;
  140. case 3:
  141. color2 = (value & 0xF0) >> 4;
  142. color1 = value & 0x0F;
  143. break;
  144. case 4:
  145. pattern = value;
  146. break;
  147. case 7:
  148. // set address (low)
  149. address = (address & 0xFF00) | value;
  150. break;
  151. case 8:
  152. // set address (high)
  153. address = (address & 0x00FF) | ((value & 0x1F) << 8);
  154. break;
  155. case 9:
  156. // write sram
  157. if (address < 0x800 && sram) {
  158. sram->write(address, value);
  159. }
  160. address = (address + 1) & 0x1FFF;
  161. break;
  162. }
  163. }
  164. byte MSXMatsushita::readIO(word port, EmuTime::param time)
  165. {
  166. // TODO also delay on read?
  167. assert(vdp);
  168. return vdp->readIO(port, time);
  169. }
  170. byte MSXMatsushita::peekIO(word port, EmuTime::param time) const
  171. {
  172. assert(vdp);
  173. return vdp->peekIO(port, time);
  174. }
  175. void MSXMatsushita::writeIO(word port, byte value, EmuTime::param time)
  176. {
  177. assert(vdp);
  178. delay(time);
  179. vdp->writeIO(port, value, lastTime.getTime());
  180. }
  181. void MSXMatsushita::delay(EmuTime::param time)
  182. {
  183. if (turboAvailable && turboEnabled) {
  184. lastTime += 46; // 8us, like in S1990
  185. if (time < lastTime.getTime()) {
  186. cpu.wait(lastTime.getTime());
  187. return;
  188. }
  189. }
  190. lastTime.reset(time);
  191. }
  192. template<typename Archive>
  193. void MSXMatsushita::serialize(Archive& ar, unsigned version)
  194. {
  195. ar.template serializeBase<MSXDevice>(*this);
  196. // no need to serialize MSXSwitchedDevice base class
  197. if (sram) ar.serialize("SRAM", *sram);
  198. ar.serialize("address", address,
  199. "color1", color1,
  200. "color2", color2,
  201. "pattern", pattern);
  202. if (ar.versionAtLeast(version, 2)) {
  203. ar.serialize("lastTime", lastTime,
  204. "turboEnabled", turboEnabled);
  205. } else {
  206. // keep 'lastTime == zero'
  207. // keep 'turboEnabled == false'
  208. getCliComm().printWarning(
  209. "Loading an old savestate: the timing of the CPU-VDP "
  210. "communication emulation has changed. This may cause "
  211. "synchronization problems in replay.");
  212. }
  213. }
  214. INSTANTIATE_SERIALIZE_METHODS(MSXMatsushita);
  215. REGISTER_MSXDEVICE(MSXMatsushita, "Matsushita");
  216. } // namespace openmsx