sms.c 6.6 KB

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