sms.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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[MEM_LE2(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[MEM_LE2(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. if (PicoIn.opt & POPT_EN_YM2413){
  95. switch((a&0xff))
  96. {
  97. case 0xf0:
  98. // FM reg port
  99. break;
  100. case 0xf1:
  101. // FM data port
  102. break;
  103. case 0xf2:
  104. // bit 0 = 1 active FM Pac
  105. d = ymflag;
  106. //printf("read FM Check = %02x\n", d);
  107. break;
  108. }
  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. if (PicoIn.opt & POPT_EN_YM2413){
  150. switch((a&0xff))
  151. {
  152. case 0xf0:
  153. // FM reg port
  154. YM2413_regWrite(d);
  155. //printf("write FM register = %02x\n", d);
  156. break;
  157. case 0xf1:
  158. // FM data port
  159. YM2413_dataWrite(d);
  160. //printf("write FM data = %02x\n", d);
  161. break;
  162. case 0xf2:
  163. // bit 0 = 1 active FM Pac
  164. ymflag = d;
  165. //printf("write FM Check = %02x\n", d);
  166. break;
  167. }
  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. PsndDoPSG(z80_cyclesDone());
  180. SN76496Write(d);
  181. break;
  182. case 0x80:
  183. vdp_data_write(d);
  184. break;
  185. case 0x81:
  186. vdp_ctl_write(d);
  187. break;
  188. }
  189. }
  190. }
  191. static int bank_mask;
  192. static void xwrite(unsigned int a, unsigned char d);
  193. static void write_sram(unsigned short a, unsigned char d)
  194. {
  195. // SRAM is mapped in 2 16KB banks, selected by bit 2 in control reg
  196. a &= 0x3fff;
  197. a += ((Pico.ms.carthw[0x0c] & 0x04) >> 2) * 0x4000;
  198. Pico.sv.changed |= (Pico.sv.data[a] != d);
  199. Pico.sv.data[a] = d;
  200. }
  201. static void write_bank(unsigned short a, unsigned char d)
  202. {
  203. elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
  204. Pico.ms.carthw[a & 0x0f] = d;
  205. switch (a & 0x0f)
  206. {
  207. case 0x0d:
  208. d &= bank_mask;
  209. z80_map_set(z80_read_map, 0x0400, 0x3fff, Pico.rom+0x400 + (d << 14), 0);
  210. #ifdef _USE_CZ80
  211. Cz80_Set_Fetch(&CZ80, 0x0400, 0x3fff, (FPTR)Pico.rom+0x400 + (d << 14));
  212. #endif
  213. break;
  214. case 0x0e:
  215. d &= bank_mask;
  216. z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
  217. #ifdef _USE_CZ80
  218. Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
  219. #endif
  220. break;
  221. case 0x0c:
  222. if (d & ~0x8c)
  223. elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
  224. /*FALLTHROUGH*/
  225. case 0x0f:
  226. if (Pico.ms.carthw[0xc] & 0x08) {
  227. d = (Pico.ms.carthw[0xc] & 0x04) >> 2;
  228. z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.sv.data + d*0x4000, 0);
  229. #ifdef _USE_CZ80
  230. Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.sv.data + d*0x4000);
  231. #endif
  232. z80_map_set(z80_write_map, 0x8000, 0xbfff, write_sram, 1);
  233. } else {
  234. d = Pico.ms.carthw[0xf] & bank_mask;
  235. z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
  236. #ifdef _USE_CZ80
  237. Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
  238. #endif
  239. z80_map_set(z80_write_map, 0x8000, 0xbfff, xwrite, 1);
  240. }
  241. break;
  242. }
  243. }
  244. static void xwrite(unsigned int a, unsigned char d)
  245. {
  246. elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
  247. if (a >= 0xc000)
  248. PicoMem.zram[a & 0x1fff] = d;
  249. if (a >= 0xfff8)
  250. write_bank(a, d);
  251. // codemasters
  252. if (a == 0x0000)
  253. write_bank(0xfffd, d);
  254. if (a == 0x4000)
  255. write_bank(0xfffe, d);
  256. if (a == 0x8000)
  257. write_bank(0xffff, d);
  258. // korean
  259. if (a == 0xa000)
  260. write_bank(0xffff, d);
  261. }
  262. void PicoResetMS(void)
  263. {
  264. z80_reset();
  265. PsndReset(); // pal must be known here
  266. ymflag = 0xffff;
  267. }
  268. void PicoPowerMS(void)
  269. {
  270. int s, tmp;
  271. memset(&PicoMem,0,sizeof(PicoMem));
  272. memset(&Pico.video,0,sizeof(Pico.video));
  273. memset(&Pico.m,0,sizeof(Pico.m));
  274. Pico.m.pal = 0;
  275. // calculate a mask for bank writes.
  276. // ROM loader has aligned the size for us, so this is safe.
  277. s = 0; tmp = Pico.romsize;
  278. while ((tmp >>= 1) != 0)
  279. s++;
  280. if (Pico.romsize > (1 << s))
  281. s++;
  282. tmp = 1 << s;
  283. bank_mask = (tmp - 1) >> 14;
  284. Pico.ms.carthw[0x0c] = 0;
  285. Pico.ms.carthw[0x0d] = 0;
  286. Pico.ms.carthw[0x0e] = 1;
  287. Pico.ms.carthw[0x0f] = 2;
  288. PicoReset();
  289. }
  290. void PicoMemSetupMS(void)
  291. {
  292. z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
  293. z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  294. z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
  295. z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
  296. z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  297. z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
  298. #ifdef _USE_DRZ80
  299. drZ80.z80_in = z80_sms_in;
  300. drZ80.z80_out = z80_sms_out;
  301. #endif
  302. #ifdef _USE_CZ80
  303. Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
  304. Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
  305. Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
  306. Cz80_Set_INPort(&CZ80, z80_sms_in);
  307. Cz80_Set_OUTPort(&CZ80, z80_sms_out);
  308. #endif
  309. }
  310. void PicoStateLoadedMS(void)
  311. {
  312. write_bank(0xfffc, Pico.ms.carthw[0x0c]);
  313. write_bank(0xfffd, Pico.ms.carthw[0x0d]);
  314. write_bank(0xfffe, Pico.ms.carthw[0x0e]);
  315. write_bank(0xffff, Pico.ms.carthw[0x0f]);
  316. }
  317. void PicoFrameMS(void)
  318. {
  319. struct PicoVideo *pv = &Pico.video;
  320. int is_pal = Pico.m.pal;
  321. int lines = is_pal ? 313 : 262;
  322. int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
  323. int cycles_done = 0, cycles_aim = 0;
  324. int skip = PicoIn.skipFrame;
  325. int lines_vis = 192;
  326. int hint; // Hint counter
  327. int nmi;
  328. int y;
  329. PsndStartFrame();
  330. nmi = (PicoIn.pad[0] >> 7) & 1;
  331. if (!Pico.ms.nmi_state && nmi)
  332. z80_nmi();
  333. Pico.ms.nmi_state = nmi;
  334. if ((Pico.video.reg[0] & 6) == 6 && (Pico.video.reg[1] & 0x18))
  335. lines_vis = (Pico.video.reg[1] & 0x08) ? 240 : 224;
  336. PicoFrameStartMode4();
  337. hint = pv->reg[0x0a];
  338. for (y = 0; y < lines; y++)
  339. {
  340. pv->v_counter = Pico.m.scanline = y;
  341. if (y > 218)
  342. pv->v_counter = y - 6;
  343. if (y < lines_vis && !skip)
  344. PicoLineMode4(y);
  345. if (y <= lines_vis)
  346. {
  347. if (--hint < 0)
  348. {
  349. hint = pv->reg[0x0a];
  350. pv->pending_ints |= 2;
  351. if (pv->reg[0] & 0x10) {
  352. elprintf(EL_INTS, "hint");
  353. z80_int_assert(1);
  354. }
  355. }
  356. }
  357. else if (y == lines_vis + 1) {
  358. pv->pending_ints |= 1;
  359. if (pv->reg[1] & 0x20) {
  360. elprintf(EL_INTS, "vint");
  361. z80_int_assert(1);
  362. }
  363. }
  364. cycles_aim += cycles_line;
  365. Pico.t.z80c_aim = cycles_aim >> 8;
  366. cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
  367. }
  368. if (PicoIn.sndOut)
  369. PsndGetSamplesMS(lines);
  370. }
  371. void PicoFrameDrawOnlyMS(void)
  372. {
  373. int lines_vis = 192;
  374. int y;
  375. PicoFrameStartMode4();
  376. for (y = 0; y < lines_vis; y++)
  377. PicoLineMode4(y);
  378. }
  379. // vim:ts=2:sw=2:expandtab