RP5C01.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "RP5C01.hh"
  2. #include "SRAM.hh"
  3. #include "serialize.hh"
  4. #include <cassert>
  5. #include <ctime>
  6. namespace openmsx {
  7. // TODO ALARM is not implemented (not connected on MSX)
  8. // TODO 1Hz 16Hz output not implemented (not connected on MSX)
  9. static const nibble MODE_REG = 13;
  10. static const nibble TEST_REG = 14;
  11. static const nibble RESET_REG = 15;
  12. static const nibble TIME_BLOCK = 0;
  13. static const nibble ALARM_BLOCK = 1;
  14. static const nibble MODE_BLOKSELECT = 0x3;
  15. static const nibble MODE_ALARMENABLE = 0x4;
  16. static const nibble MODE_TIMERENABLE = 0x8;
  17. static const nibble TEST_SECONDS = 0x1;
  18. static const nibble TEST_MINUTES = 0x2;
  19. static const nibble TEST_DAYS = 0x4;
  20. static const nibble TEST_YEARS = 0x8;
  21. static const nibble RESET_ALARM = 0x1;
  22. static const nibble RESET_FRACTION = 0x2;
  23. // 0-bits are ignored on writing and return 0 on reading
  24. static const nibble mask[4][13] = {
  25. { 0xf, 0x7, 0xf, 0x7, 0xf, 0x3, 0x7, 0xf, 0x3, 0xf, 0x1, 0xf, 0xf},
  26. { 0x0, 0x0, 0xf, 0x7, 0xf, 0x3, 0x7, 0xf, 0x3, 0x0, 0x1, 0x3, 0x0},
  27. { 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf},
  28. { 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf}
  29. };
  30. RP5C01::RP5C01(CommandController& commandController, SRAM& regs_,
  31. EmuTime::param time, const std::string& name)
  32. : regs(regs_)
  33. , modeSetting(
  34. commandController,
  35. ((name == "Real time clock") ? "rtcmode" // bw-compat
  36. : (name + " mode")),
  37. "Real Time Clock mode", RP5C01::EMUTIME,
  38. EnumSetting<RP5C01::RTCMode>::Map{
  39. {"EmuTime", RP5C01::EMUTIME},
  40. {"RealTime", RP5C01::REALTIME}})
  41. , reference(time)
  42. {
  43. initializeTime();
  44. reset(time);
  45. }
  46. void RP5C01::reset(EmuTime::param time)
  47. {
  48. modeReg = MODE_TIMERENABLE;
  49. testReg = 0;
  50. resetReg = 0;
  51. updateTimeRegs(time);
  52. }
  53. nibble RP5C01::readPort(nibble port, EmuTime::param time)
  54. {
  55. switch (port) {
  56. case MODE_REG:
  57. case TEST_REG:
  58. case RESET_REG:
  59. // nothing
  60. break;
  61. default:
  62. unsigned block = modeReg & MODE_BLOKSELECT;
  63. if (block == TIME_BLOCK) {
  64. updateTimeRegs(time);
  65. }
  66. }
  67. return peekPort(port);
  68. }
  69. nibble RP5C01::peekPort(nibble port) const
  70. {
  71. assert(port <= 0x0f);
  72. switch (port) {
  73. case MODE_REG:
  74. return modeReg;
  75. case TEST_REG:
  76. case RESET_REG:
  77. // write only
  78. return 0x0f; // TODO check this
  79. default:
  80. unsigned block = modeReg & MODE_BLOKSELECT;
  81. nibble tmp = regs[block * 13 + port];
  82. return tmp & mask[block][port];
  83. }
  84. }
  85. void RP5C01::writePort(nibble port, nibble value, EmuTime::param time)
  86. {
  87. assert (port<=0x0f);
  88. switch (port) {
  89. case MODE_REG:
  90. updateTimeRegs(time);
  91. modeReg = value;
  92. break;
  93. case TEST_REG:
  94. updateTimeRegs(time);
  95. testReg = value;
  96. break;
  97. case RESET_REG:
  98. resetReg = value;
  99. if (value & RESET_ALARM) {
  100. resetAlarm();
  101. }
  102. if (value & RESET_FRACTION) {
  103. fraction = 0;
  104. }
  105. break;
  106. default:
  107. unsigned block = modeReg & MODE_BLOKSELECT;
  108. if (block == TIME_BLOCK) {
  109. updateTimeRegs(time);
  110. }
  111. regs.write(block * 13 + port, value & mask[block][port]);
  112. if (block == TIME_BLOCK) {
  113. regs2Time();
  114. }
  115. }
  116. }
  117. void RP5C01::initializeTime()
  118. {
  119. time_t t = time(nullptr);
  120. struct tm *tm = localtime(&t);
  121. fraction = 0; // fractions of a second
  122. seconds = tm->tm_sec; // 0-59
  123. minutes = tm->tm_min; // 0-59
  124. hours = tm->tm_hour; // 0-23
  125. dayWeek = tm->tm_wday; // 0-6 0=sunday
  126. days = tm->tm_mday-1; // 0-30
  127. months = tm->tm_mon; // 0-11
  128. years = tm->tm_year - 80; // 0-99 0=1980
  129. leapYear = tm->tm_year % 4; // 0-3 0=leap year
  130. time2Regs();
  131. }
  132. void RP5C01::regs2Time()
  133. {
  134. seconds = regs[TIME_BLOCK * 13 + 0] + 10 * regs[TIME_BLOCK * 13 + 1];
  135. minutes = regs[TIME_BLOCK * 13 + 2] + 10 * regs[TIME_BLOCK * 13 + 3];
  136. hours = regs[TIME_BLOCK * 13 + 4] + 10 * regs[TIME_BLOCK * 13 + 5];
  137. dayWeek = regs[TIME_BLOCK * 13 + 6];
  138. days = regs[TIME_BLOCK * 13 + 7] + 10 * regs[TIME_BLOCK * 13 + 8] - 1;
  139. months = regs[TIME_BLOCK * 13 + 9] + 10 * regs[TIME_BLOCK * 13 +10] - 1;
  140. years = regs[TIME_BLOCK * 13 +11] + 10 * regs[TIME_BLOCK * 13 +12];
  141. leapYear = regs[ALARM_BLOCK * 13 +11];
  142. if (!regs[ALARM_BLOCK * 13 + 10]) {
  143. // 12 hours mode
  144. if (hours >= 20) hours = (hours - 20) + 12;
  145. }
  146. }
  147. void RP5C01::time2Regs()
  148. {
  149. unsigned hours_ = hours;
  150. if (!regs[ALARM_BLOCK * 13 + 10]) {
  151. // 12 hours mode
  152. if (hours >= 12) hours_ = (hours - 12) + 20;
  153. }
  154. regs.write(TIME_BLOCK * 13 + 0, seconds % 10);
  155. regs.write(TIME_BLOCK * 13 + 1, seconds / 10);
  156. regs.write(TIME_BLOCK * 13 + 2, minutes % 10);
  157. regs.write(TIME_BLOCK * 13 + 3, minutes / 10);
  158. regs.write(TIME_BLOCK * 13 + 4, hours_ % 10);
  159. regs.write(TIME_BLOCK * 13 + 5, hours_ / 10);
  160. regs.write(TIME_BLOCK * 13 + 6, dayWeek);
  161. regs.write(TIME_BLOCK * 13 + 7, (days+1) % 10); // 0-30 -> 1-31
  162. regs.write(TIME_BLOCK * 13 + 8, (days+1) / 10); // 0-11 -> 1-12
  163. regs.write(TIME_BLOCK * 13 + 9, (months+1) % 10);
  164. regs.write(TIME_BLOCK * 13 + 10, (months+1) / 10);
  165. regs.write(TIME_BLOCK * 13 + 11, years % 10);
  166. regs.write(TIME_BLOCK * 13 + 12, years / 10);
  167. regs.write(ALARM_BLOCK * 13 + 11, leapYear);
  168. }
  169. static int daysInMonth(int month, unsigned leapYear)
  170. {
  171. const unsigned daysInMonths[12] = {
  172. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  173. };
  174. month %= 12;
  175. return ((month == 1) && (leapYear == 0)) ? 29 : daysInMonths[month];
  176. }
  177. void RP5C01::updateTimeRegs(EmuTime::param time)
  178. {
  179. if (modeSetting.getEnum() == EMUTIME) {
  180. // sync with EmuTime, perfect emulation
  181. auto elapsed = unsigned(reference.getTicksTill(time));
  182. reference.advance(time);
  183. // in test mode increase sec/min/.. at a rate of 16384Hz
  184. fraction += (modeReg & MODE_TIMERENABLE) ? elapsed : 0;
  185. unsigned carrySeconds = (testReg & TEST_SECONDS)
  186. ? elapsed : fraction / FREQ;
  187. seconds += carrySeconds;
  188. unsigned carryMinutes = (testReg & TEST_MINUTES)
  189. ? elapsed : seconds / 60;
  190. minutes += carryMinutes;
  191. hours += minutes / 60;
  192. unsigned carryDays = (testReg & TEST_DAYS)
  193. ? elapsed : hours / 24;
  194. days += carryDays;
  195. dayWeek += carryDays;
  196. while (days >= daysInMonth(months, leapYear)) {
  197. // TODO not correct because leapYear is not updated
  198. // is only triggered when we update several months
  199. // at a time (but might happen in TEST_DAY mode)
  200. days -= daysInMonth(months, leapYear);
  201. months++;
  202. }
  203. unsigned carryYears = (testReg & TEST_YEARS)
  204. ? elapsed : unsigned(months / 12);
  205. years += carryYears;
  206. leapYear += carryYears;
  207. fraction %= FREQ;
  208. seconds %= 60;
  209. minutes %= 60;
  210. hours %= 24;
  211. dayWeek %= 7;
  212. months %= 12;
  213. years %= 100;
  214. leapYear %= 4;
  215. time2Regs();
  216. } else {
  217. // sync with host clock
  218. // writes to time, test and reset registers have no effect
  219. initializeTime();
  220. }
  221. }
  222. void RP5C01::resetAlarm()
  223. {
  224. for (unsigned i = 2; i <= 8; ++i) {
  225. regs.write(ALARM_BLOCK * 13 + i, 0);
  226. }
  227. }
  228. template<typename Archive>
  229. void RP5C01::serialize(Archive& ar, unsigned /*version*/)
  230. {
  231. ar.serialize("reference", reference);
  232. ar.serialize("fraction", fraction);
  233. ar.serialize("seconds", seconds);
  234. ar.serialize("minutes", minutes);
  235. ar.serialize("hours", hours);
  236. ar.serialize("dayWeek", dayWeek);
  237. ar.serialize("years", years);
  238. ar.serialize("leapYear", leapYear);
  239. ar.serialize("days", days);
  240. ar.serialize("months", months);
  241. ar.serialize("modeReg", modeReg);
  242. ar.serialize("testReg", testReg);
  243. ar.serialize("resetReg", resetReg);
  244. }
  245. INSTANTIATE_SERIALIZE_METHODS(RP5C01);
  246. } // namespace openmsx