sms.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * SMS emulation
  3. * (C) notaz, 2009-2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. /*
  9. * TODO:
  10. * - start in a state as if BIOS ran
  11. * - remaining status flags (OVR/COL)
  12. * - RAM support in mapper
  13. * - region support
  14. * - Pause button (NMI)
  15. * - SN76496 DAC-like usage
  16. * - H counter
  17. */
  18. #include "pico_int.h"
  19. #include "memory.h"
  20. #include "sound/sn76496.h"
  21. static unsigned char vdp_data_read(void)
  22. {
  23. struct PicoVideo *pv = &Pico.video;
  24. unsigned char d;
  25. d = Pico.vramb[pv->addr];
  26. pv->addr = (pv->addr + 1) & 0x3fff;
  27. pv->pending = 0;
  28. return d;
  29. }
  30. static unsigned char vdp_ctl_read(void)
  31. {
  32. unsigned char d = Pico.video.pending_ints << 7;
  33. Pico.video.pending = 0;
  34. Pico.video.pending_ints = 0;
  35. elprintf(EL_SR, "VDP sr: %02x", d);
  36. return d;
  37. }
  38. static void vdp_data_write(unsigned char d)
  39. {
  40. struct PicoVideo *pv = &Pico.video;
  41. if (pv->type == 3) {
  42. Pico.cram[pv->addr & 0x1f] = d;
  43. Pico.m.dirtyPal = 1;
  44. } else {
  45. Pico.vramb[pv->addr] = d;
  46. }
  47. pv->addr = (pv->addr + 1) & 0x3fff;
  48. pv->pending = 0;
  49. }
  50. static void vdp_ctl_write(unsigned char d)
  51. {
  52. struct PicoVideo *pv = &Pico.video;
  53. if (pv->pending) {
  54. if ((d >> 6) == 2) {
  55. pv->reg[d & 0x0f] = pv->addr;
  56. elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
  57. }
  58. pv->type = d >> 6;
  59. pv->addr &= 0x00ff;
  60. pv->addr |= (d & 0x3f) << 8;
  61. } else {
  62. pv->addr &= 0x3f00;
  63. pv->addr |= d;
  64. }
  65. pv->pending ^= 1;
  66. }
  67. static unsigned char z80_sms_in(unsigned short a)
  68. {
  69. unsigned char d = 0;
  70. elprintf(EL_IO, "z80 port %04x read", a);
  71. a &= 0xc1;
  72. switch (a)
  73. {
  74. case 0x00:
  75. case 0x01:
  76. d = 0xff;
  77. break;
  78. case 0x40: /* V counter */
  79. d = Pico.video.v_counter;
  80. elprintf(EL_HVCNT, "V counter read: %02x", d);
  81. break;
  82. case 0x41: /* H counter */
  83. d = Pico.m.rotate++;
  84. elprintf(EL_HVCNT, "H counter read: %02x", d);
  85. break;
  86. case 0x80:
  87. d = vdp_data_read();
  88. break;
  89. case 0x81:
  90. d = vdp_ctl_read();
  91. break;
  92. case 0xc0: /* I/O port A and B */
  93. d = ~((PicoPad[0] & 0x3f) | (PicoPad[1] << 6));
  94. break;
  95. case 0xc1: /* I/O port B and miscellaneous */
  96. d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
  97. d |= ~(PicoPad[1] >> 2) & 0x0f;
  98. break;
  99. }
  100. elprintf(EL_IO, "ret = %02x", d);
  101. return d;
  102. }
  103. static void z80_sms_out(unsigned short a, unsigned char d)
  104. {
  105. elprintf(EL_IO, "z80 port %04x write %02x", a, d);
  106. a &= 0xc1;
  107. switch (a)
  108. {
  109. case 0x01:
  110. Pico.ms.io_ctl = d;
  111. break;
  112. case 0x40:
  113. case 0x41:
  114. if (PicoOpt & POPT_EN_PSG)
  115. SN76496Write(d);
  116. break;
  117. case 0x80:
  118. vdp_data_write(d);
  119. break;
  120. case 0x81:
  121. vdp_ctl_write(d);
  122. break;
  123. }
  124. }
  125. static int bank_mask;
  126. static void write_bank(unsigned short a, unsigned char d)
  127. {
  128. elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
  129. switch (a & 0x0f)
  130. {
  131. case 0x0c:
  132. elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
  133. break;
  134. case 0x0d:
  135. if (d != 0)
  136. elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d);
  137. break;
  138. case 0x0e:
  139. d &= bank_mask;
  140. z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
  141. #ifdef _USE_CZ80
  142. Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
  143. #endif
  144. break;
  145. case 0x0f:
  146. d &= bank_mask;
  147. z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
  148. #ifdef _USE_CZ80
  149. Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
  150. #endif
  151. break;
  152. }
  153. Pico.ms.carthw[a & 0x0f] = d;
  154. }
  155. static void xwrite(unsigned int a, unsigned char d)
  156. {
  157. elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
  158. if (a >= 0xc000)
  159. Pico.zram[a & 0x1fff] = d;
  160. if (a >= 0xfff8)
  161. write_bank(a, d);
  162. }
  163. void PicoResetMS(void)
  164. {
  165. z80_reset();
  166. PsndReset(); // pal must be known here
  167. }
  168. void PicoPowerMS(void)
  169. {
  170. int s, tmp;
  171. memset(&Pico.ram,0,(unsigned char *)&Pico.rom - Pico.ram);
  172. memset(&Pico.video,0,sizeof(Pico.video));
  173. memset(&Pico.m,0,sizeof(Pico.m));
  174. Pico.m.pal = 0;
  175. // calculate a mask for bank writes.
  176. // ROM loader has aligned the size for us, so this is safe.
  177. s = 0; tmp = Pico.romsize;
  178. while ((tmp >>= 1) != 0)
  179. s++;
  180. if (Pico.romsize > (1 << s))
  181. s++;
  182. tmp = 1 << s;
  183. bank_mask = (tmp - 1) >> 14;
  184. Pico.ms.carthw[0x0e] = 1;
  185. Pico.ms.carthw[0x0f] = 2;
  186. PicoReset();
  187. }
  188. void PicoMemSetupMS(void)
  189. {
  190. z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
  191. z80_map_set(z80_read_map, 0xc000, 0xdfff, Pico.zram, 0);
  192. z80_map_set(z80_read_map, 0xe000, 0xffff, Pico.zram, 0);
  193. z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
  194. z80_map_set(z80_write_map, 0xc000, 0xdfff, Pico.zram, 0);
  195. z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
  196. #ifdef _USE_DRZ80
  197. drZ80.z80_in = z80_sms_in;
  198. drZ80.z80_out = z80_sms_out;
  199. #endif
  200. #ifdef _USE_CZ80
  201. Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
  202. Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)Pico.zram);
  203. Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)Pico.zram);
  204. Cz80_Set_INPort(&CZ80, z80_sms_in);
  205. Cz80_Set_OUTPort(&CZ80, z80_sms_out);
  206. #endif
  207. }
  208. void PicoStateLoadedMS(void)
  209. {
  210. write_bank(0xfffe, Pico.ms.carthw[0x0e]);
  211. write_bank(0xffff, Pico.ms.carthw[0x0f]);
  212. }
  213. void PicoFrameMS(void)
  214. {
  215. struct PicoVideo *pv = &Pico.video;
  216. int is_pal = Pico.m.pal;
  217. int lines = is_pal ? 313 : 262;
  218. int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
  219. int cycles_done = 0, cycles_aim = 0;
  220. int skip = PicoSkipFrame;
  221. int lines_vis = 192;
  222. int hint; // Hint counter
  223. int y;
  224. PicoFrameStartMode4();
  225. hint = pv->reg[0x0a];
  226. for (y = 0; y < lines; y++)
  227. {
  228. pv->v_counter = Pico.m.scanline = y;
  229. if (y > 218)
  230. pv->v_counter = y - 6;
  231. if (y < lines_vis && !skip)
  232. PicoLineMode4(y);
  233. if (y <= lines_vis)
  234. {
  235. if (--hint < 0)
  236. {
  237. hint = pv->reg[0x0a];
  238. pv->pending_ints |= 2;
  239. if (pv->reg[0] & 0x10) {
  240. elprintf(EL_INTS, "hint");
  241. z80_int();
  242. }
  243. }
  244. }
  245. else if (y == lines_vis + 1) {
  246. pv->pending_ints |= 1;
  247. if (pv->reg[1] & 0x20) {
  248. elprintf(EL_INTS, "vint");
  249. z80_int();
  250. }
  251. }
  252. cycles_aim += cycles_line;
  253. cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
  254. }
  255. if (PsndOut)
  256. PsndGetSamplesMS();
  257. }
  258. void PicoFrameDrawOnlyMS(void)
  259. {
  260. int lines_vis = 192;
  261. int y;
  262. PicoFrameStartMode4();
  263. for (y = 0; y < lines_vis; y++)
  264. PicoLineMode4(y);
  265. }