sms.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. * - RAM support in mapper
  12. * - region support
  13. * - H counter
  14. */
  15. #include "pico_int.h"
  16. #include "memory.h"
  17. #include "sound/sn76496.h"
  18. #include "sound/emu2413/emu2413.h"
  19. extern void YM2413_regWrite(unsigned reg);
  20. extern void YM2413_dataWrite(unsigned data);
  21. static unsigned short ymflag = 0xffff;
  22. static unsigned char vdp_data_read(void)
  23. {
  24. struct PicoVideo *pv = &Pico.video;
  25. unsigned char d;
  26. d = PicoMem.vramb[pv->addr];
  27. pv->addr = (pv->addr + 1) & 0x3fff;
  28. pv->pending = 0;
  29. return d;
  30. }
  31. static unsigned char vdp_ctl_read(void)
  32. {
  33. struct PicoVideo *pv = &Pico.video;
  34. unsigned char d;
  35. z80_int_assert(0);
  36. d = pv->status | (pv->pending_ints << 7);
  37. pv->pending = pv->pending_ints = 0;
  38. pv->status = 0;
  39. elprintf(EL_SR, "VDP sr: %02x", d);
  40. return d;
  41. }
  42. static void vdp_data_write(unsigned char d)
  43. {
  44. struct PicoVideo *pv = &Pico.video;
  45. if (pv->type == 3) {
  46. if (PicoMem.cram[pv->addr & 0x1f] != d) Pico.m.dirtyPal = 1;
  47. PicoMem.cram[pv->addr & 0x1f] = d;
  48. } else {
  49. PicoMem.vramb[pv->addr] = d;
  50. }
  51. pv->addr = (pv->addr + 1) & 0x3fff;
  52. pv->pending = 0;
  53. }
  54. static NOINLINE void vdp_reg_write(struct PicoVideo *pv, u8 a, u8 d)
  55. {
  56. int l;
  57. pv->reg[a] = d;
  58. switch (a) {
  59. case 0:
  60. l = pv->pending_ints & (d >> 3) & 2;
  61. elprintf(EL_INTS, "hint %d", l);
  62. z80_int_assert(l);
  63. break;
  64. case 1:
  65. l = pv->pending_ints & (d >> 5) & 1;
  66. elprintf(EL_INTS, "vint %d", l);
  67. z80_int_assert(l);
  68. break;
  69. }
  70. }
  71. static void vdp_ctl_write(u8 d)
  72. {
  73. struct PicoVideo *pv = &Pico.video;
  74. if (pv->pending) {
  75. if ((d >> 6) == 2) {
  76. elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
  77. if (pv->reg[d & 0x0f] != (u8)pv->addr)
  78. vdp_reg_write(pv, d & 0x0f, pv->addr);
  79. }
  80. pv->type = d >> 6;
  81. pv->addr &= 0x00ff;
  82. pv->addr |= (d & 0x3f) << 8;
  83. } else {
  84. pv->addr &= 0x3f00;
  85. pv->addr |= d;
  86. }
  87. pv->pending ^= 1;
  88. }
  89. static unsigned char z80_sms_in(unsigned short a)
  90. {
  91. unsigned char d = 0;
  92. elprintf(EL_IO, "z80 port %04x read", a);
  93. if((a&0xff)>= 0xf0){
  94. switch((a&0xff))
  95. {
  96. case 0xf0:
  97. // FM reg port
  98. break;
  99. case 0xf1:
  100. // FM data port
  101. break;
  102. case 0xf2:
  103. // bit 0 = 1 active FM Pac
  104. if (PicoIn.opt & POPT_EN_YM2413){
  105. d = ymflag;
  106. //printf("read FM Check = %02x\n", d);
  107. }
  108. break;
  109. }
  110. }
  111. else{
  112. a &= 0xc1;
  113. switch (a)
  114. {
  115. case 0x00:
  116. case 0x01:
  117. d = 0xff;
  118. break;
  119. case 0x40: /* V counter */
  120. d = Pico.video.v_counter;
  121. elprintf(EL_HVCNT, "V counter read: %02x", d);
  122. break;
  123. case 0x41: /* H counter */
  124. d = Pico.m.rotate++;
  125. elprintf(EL_HVCNT, "H counter read: %02x", d);
  126. break;
  127. case 0x80:
  128. d = vdp_data_read();
  129. break;
  130. case 0x81:
  131. d = vdp_ctl_read();
  132. break;
  133. case 0xc0: /* I/O port A and B */
  134. d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
  135. break;
  136. case 0xc1: /* I/O port B and miscellaneous */
  137. d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
  138. d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
  139. break;
  140. }
  141. }
  142. elprintf(EL_IO, "ret = %02x", d);
  143. return d;
  144. }
  145. static void z80_sms_out(unsigned short a, unsigned char d)
  146. {
  147. elprintf(EL_IO, "z80 port %04x write %02x", a, d);
  148. if((a&0xff)>= 0xf0){
  149. switch((a&0xff))
  150. {
  151. case 0xf0:
  152. // FM reg port
  153. YM2413_regWrite(d);
  154. //printf("write FM register = %02x\n", d);
  155. break;
  156. case 0xf1:
  157. // FM data port
  158. YM2413_dataWrite(d);
  159. //printf("write FM data = %02x\n", d);
  160. break;
  161. case 0xf2:
  162. // bit 0 = 1 active FM Pac
  163. if (PicoIn.opt & POPT_EN_YM2413){
  164. ymflag = d;
  165. //printf("write FM Check = %02x\n", d);
  166. }
  167. break;
  168. }
  169. }
  170. else{
  171. a &= 0xc1;
  172. switch (a)
  173. {
  174. case 0x01:
  175. Pico.ms.io_ctl = d;
  176. break;
  177. case 0x40:
  178. case 0x41:
  179. if ((d & 0x90) == 0x90)
  180. PsndDoPSG(Pico.m.scanline);
  181. SN76496Write(d);
  182. break;
  183. case 0x80:
  184. vdp_data_write(d);
  185. break;
  186. case 0x81:
  187. vdp_ctl_write(d);
  188. break;
  189. }
  190. }
  191. }
  192. static int bank_mask;
  193. static void write_bank(unsigned short a, unsigned char d)
  194. {
  195. elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
  196. switch (a & 0x0f)
  197. {
  198. case 0x0c:
  199. elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
  200. break;
  201. case 0x0d:
  202. if (d != 0)
  203. elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d);
  204. break;
  205. case 0x0e:
  206. d &= bank_mask;
  207. z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
  208. #ifdef _USE_CZ80
  209. Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
  210. #endif
  211. break;
  212. case 0x0f:
  213. d &= bank_mask;
  214. z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
  215. #ifdef _USE_CZ80
  216. Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
  217. #endif
  218. break;
  219. }
  220. Pico.ms.carthw[a & 0x0f] = d;
  221. }
  222. static void xwrite(unsigned int a, unsigned char d)
  223. {
  224. elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
  225. if (a >= 0xc000)
  226. PicoMem.zram[a & 0x1fff] = d;
  227. if (a >= 0xfff8)
  228. write_bank(a, d);
  229. }
  230. void PicoResetMS(void)
  231. {
  232. z80_reset();
  233. PsndReset(); // pal must be known here
  234. ymflag = 0xffff;
  235. }
  236. void PicoPowerMS(void)
  237. {
  238. int s, tmp;
  239. memset(&PicoMem,0,sizeof(PicoMem));
  240. memset(&Pico.video,0,sizeof(Pico.video));
  241. memset(&Pico.m,0,sizeof(Pico.m));
  242. Pico.m.pal = 0;
  243. // calculate a mask for bank writes.
  244. // ROM loader has aligned the size for us, so this is safe.
  245. s = 0; tmp = Pico.romsize;
  246. while ((tmp >>= 1) != 0)
  247. s++;
  248. if (Pico.romsize > (1 << s))
  249. s++;
  250. tmp = 1 << s;
  251. bank_mask = (tmp - 1) >> 14;
  252. Pico.ms.carthw[0x0e] = 1;
  253. Pico.ms.carthw[0x0f] = 2;
  254. PicoReset();
  255. }
  256. void PicoMemSetupMS(void)
  257. {
  258. z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
  259. z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  260. z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
  261. z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
  262. z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  263. z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
  264. #ifdef _USE_DRZ80
  265. drZ80.z80_in = z80_sms_in;
  266. drZ80.z80_out = z80_sms_out;
  267. #endif
  268. #ifdef _USE_CZ80
  269. Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
  270. Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
  271. Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
  272. Cz80_Set_INPort(&CZ80, z80_sms_in);
  273. Cz80_Set_OUTPort(&CZ80, z80_sms_out);
  274. #endif
  275. }
  276. void PicoStateLoadedMS(void)
  277. {
  278. write_bank(0xfffe, Pico.ms.carthw[0x0e]);
  279. write_bank(0xffff, Pico.ms.carthw[0x0f]);
  280. }
  281. void PicoFrameMS(void)
  282. {
  283. struct PicoVideo *pv = &Pico.video;
  284. int is_pal = Pico.m.pal;
  285. int lines = is_pal ? 313 : 262;
  286. int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
  287. int cycles_done = 0, cycles_aim = 0;
  288. int skip = PicoIn.skipFrame;
  289. int lines_vis = 192;
  290. int hint; // Hint counter
  291. int nmi;
  292. int y;
  293. PsndStartFrame();
  294. nmi = (PicoIn.pad[0] >> 7) & 1;
  295. if (!Pico.ms.nmi_state && nmi)
  296. z80_nmi();
  297. Pico.ms.nmi_state = nmi;
  298. PicoFrameStartMode4();
  299. hint = pv->reg[0x0a];
  300. for (y = 0; y < lines; y++)
  301. {
  302. pv->v_counter = Pico.m.scanline = y;
  303. if (y > 218)
  304. pv->v_counter = y - 6;
  305. if (y < lines_vis && !skip)
  306. PicoLineMode4(y);
  307. if (y <= lines_vis)
  308. {
  309. if (--hint < 0)
  310. {
  311. hint = pv->reg[0x0a];
  312. pv->pending_ints |= 2;
  313. if (pv->reg[0] & 0x10) {
  314. elprintf(EL_INTS, "hint");
  315. z80_int_assert(1);
  316. }
  317. }
  318. }
  319. else if (y == lines_vis + 1) {
  320. pv->pending_ints |= 1;
  321. if (pv->reg[1] & 0x20) {
  322. elprintf(EL_INTS, "vint");
  323. z80_int_assert(1);
  324. }
  325. }
  326. cycles_aim += cycles_line;
  327. cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
  328. }
  329. if (PicoIn.sndOut)
  330. PsndGetSamplesMS(lines);
  331. }
  332. void PicoFrameDrawOnlyMS(void)
  333. {
  334. int lines_vis = 192;
  335. int y;
  336. PicoFrameStartMode4();
  337. for (y = 0; y < lines_vis; y++)
  338. PicoLineMode4(y);
  339. }
  340. // vim:ts=2:sw=2:expandtab