sms.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. * - region support
  12. * - H counter
  13. */
  14. #include "pico_int.h"
  15. #include "memory.h"
  16. #include "sound/sn76496.h"
  17. #include "sound/emu2413/emu2413.h"
  18. extern void YM2413_regWrite(unsigned reg);
  19. extern void YM2413_dataWrite(unsigned data);
  20. static unsigned short ymflag = 0xffff;
  21. static unsigned char vdp_data_read(void)
  22. {
  23. struct PicoVideo *pv = &Pico.video;
  24. unsigned char d;
  25. d = PicoMem.vramb[MEM_LE2(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. struct PicoVideo *pv = &Pico.video;
  33. unsigned char d;
  34. z80_int_assert(0);
  35. d = pv->status | (pv->pending_ints << 7);
  36. pv->pending = pv->pending_ints = 0;
  37. pv->status = 0;
  38. elprintf(EL_SR, "VDP sr: %02x", d);
  39. return d;
  40. }
  41. static void vdp_data_write(unsigned char d)
  42. {
  43. struct PicoVideo *pv = &Pico.video;
  44. if (pv->type == 3) {
  45. if (Pico.m.hardware & 0x1) { // GG, same layout as MD
  46. unsigned a = pv->addr & 0x3f;
  47. if (a & 0x1) d &= 0x0f;
  48. if (((u8 *)PicoMem.cram)[MEM_LE2(a)] != d) Pico.m.dirtyPal = 1;
  49. ((u8 *)PicoMem.cram)[MEM_LE2(a)] = d;
  50. } else { // SMS, convert to MD layout (00BbGgRr to 0000BbBbGgGgRrRr)
  51. unsigned a = pv->addr & 0x1f;
  52. u16 c = (d&0x30)*0x40 + (d&0x0c)*0x10 + (d&0x03)*0x04;
  53. if (PicoMem.cram[a] != (c | (c>>2))) Pico.m.dirtyPal = 1;
  54. PicoMem.cram[a] = c | (c>>2);
  55. }
  56. } else {
  57. PicoMem.vramb[MEM_LE2(pv->addr)] = d;
  58. }
  59. pv->addr = (pv->addr + 1) & 0x3fff;
  60. pv->pending = 0;
  61. }
  62. static NOINLINE void vdp_reg_write(struct PicoVideo *pv, u8 a, u8 d)
  63. {
  64. int l;
  65. pv->reg[a] = d;
  66. switch (a) {
  67. case 0:
  68. l = pv->pending_ints & (d >> 3) & 2;
  69. elprintf(EL_INTS, "hint %d", l);
  70. z80_int_assert(l);
  71. break;
  72. case 1:
  73. l = pv->pending_ints & (d >> 5) & 1;
  74. elprintf(EL_INTS, "vint %d", l);
  75. z80_int_assert(l);
  76. break;
  77. }
  78. }
  79. static void vdp_ctl_write(u8 d)
  80. {
  81. struct PicoVideo *pv = &Pico.video;
  82. if (pv->pending) {
  83. if ((d >> 6) == 2) {
  84. elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
  85. if (pv->reg[d & 0x0f] != (u8)pv->addr)
  86. vdp_reg_write(pv, d & 0x0f, pv->addr);
  87. }
  88. pv->type = d >> 6;
  89. pv->addr &= 0x00ff;
  90. pv->addr |= (d & 0x3f) << 8;
  91. } else {
  92. pv->addr &= 0x3f00;
  93. pv->addr |= d;
  94. }
  95. pv->pending ^= 1;
  96. }
  97. static unsigned char z80_sms_in(unsigned short a)
  98. {
  99. unsigned char d = 0;
  100. elprintf(EL_IO, "z80 port %04x read", a);
  101. if((a&0xff)>= 0xf0){
  102. if (PicoIn.opt & POPT_EN_YM2413){
  103. switch((a&0xff))
  104. {
  105. case 0xf0:
  106. // FM reg port
  107. break;
  108. case 0xf1:
  109. // FM data port
  110. break;
  111. case 0xf2:
  112. // bit 0 = 1 active FM Pac
  113. d = ymflag;
  114. //printf("read FM Check = %02x\n", d);
  115. break;
  116. }
  117. }
  118. }
  119. else{
  120. a &= 0xc1;
  121. switch (a)
  122. {
  123. case 0x00:
  124. case 0x01:
  125. d = 0xff & ~(PicoIn.pad[0] & 0x80);
  126. break;
  127. case 0x40: /* V counter */
  128. d = Pico.video.v_counter;
  129. elprintf(EL_HVCNT, "V counter read: %02x", d);
  130. break;
  131. case 0x41: /* H counter */
  132. d = Pico.m.rotate++;
  133. elprintf(EL_HVCNT, "H counter read: %02x", d);
  134. break;
  135. case 0x80:
  136. d = vdp_data_read();
  137. break;
  138. case 0x81:
  139. d = vdp_ctl_read();
  140. break;
  141. case 0xc0: /* I/O port A and B */
  142. d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
  143. break;
  144. case 0xc1: /* I/O port B and miscellaneous */
  145. d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
  146. d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
  147. break;
  148. }
  149. }
  150. elprintf(EL_IO, "ret = %02x", d);
  151. return d;
  152. }
  153. static void z80_sms_out(unsigned short a, unsigned char d)
  154. {
  155. elprintf(EL_IO, "z80 port %04x write %02x", a, d);
  156. if((a&0xff)>= 0xf0){
  157. if (PicoIn.opt & POPT_EN_YM2413){
  158. switch((a&0xff))
  159. {
  160. case 0xf0:
  161. // FM reg port
  162. YM2413_regWrite(d);
  163. //printf("write FM register = %02x\n", d);
  164. break;
  165. case 0xf1:
  166. // FM data port
  167. YM2413_dataWrite(d);
  168. //printf("write FM data = %02x\n", d);
  169. break;
  170. case 0xf2:
  171. // bit 0 = 1 active FM Pac
  172. ymflag = d;
  173. //printf("write FM Check = %02x\n", d);
  174. break;
  175. }
  176. }
  177. }
  178. else{
  179. a &= 0xc1;
  180. switch (a)
  181. {
  182. case 0x01:
  183. Pico.ms.io_ctl = d;
  184. break;
  185. case 0x40:
  186. case 0x41:
  187. PsndDoPSG(z80_cyclesDone());
  188. SN76496Write(d);
  189. break;
  190. case 0x80:
  191. vdp_data_write(d);
  192. break;
  193. case 0x81:
  194. vdp_ctl_write(d);
  195. break;
  196. }
  197. }
  198. }
  199. static int bank_mask;
  200. static void xwrite(unsigned int a, unsigned char d);
  201. static void write_sram(unsigned short a, unsigned char d)
  202. {
  203. // SRAM is mapped in 2 16KB banks, selected by bit 2 in control reg
  204. a &= 0x3fff;
  205. a += ((Pico.ms.carthw[0x0c] & 0x04) >> 2) * 0x4000;
  206. Pico.sv.changed |= (Pico.sv.data[a] != d);
  207. Pico.sv.data[a] = d;
  208. }
  209. // 16KB bank mapping for Sega mapper
  210. static void write_bank_sega(unsigned short a, unsigned char d)
  211. {
  212. elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
  213. Pico.ms.carthw[a & 0x0f] = d;
  214. switch (a & 0x0f)
  215. {
  216. case 0x0d:
  217. d &= bank_mask;
  218. z80_map_set(z80_read_map, 0x0400, 0x3fff, Pico.rom+0x400 + (d << 14), 0);
  219. break;
  220. case 0x0e:
  221. d &= bank_mask;
  222. z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
  223. break;
  224. case 0x0c:
  225. if (d & ~0x8c)
  226. elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
  227. /*FALLTHROUGH*/
  228. case 0x0f:
  229. if (Pico.ms.carthw[0xc] & 0x08) {
  230. d = (Pico.ms.carthw[0xc] & 0x04) >> 2;
  231. z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.sv.data + d*0x4000, 0);
  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. z80_map_set(z80_write_map, 0x8000, 0xbfff, xwrite, 1);
  237. }
  238. break;
  239. }
  240. }
  241. // 8KB ROM mapping for MSX mapper
  242. static void write_bank_msx(unsigned short a, unsigned char d)
  243. {
  244. Pico.ms.mapper = 1; // TODO define (more) mapper types
  245. Pico.ms.carthw[a] = d;
  246. a = (a^2)*0x2000 + 0x4000;
  247. d &= 2*bank_mask + 1;
  248. z80_map_set(z80_read_map, a, a+0x1fff, Pico.rom + (d << 13), 0);
  249. }
  250. // TODO mapping is currently auto-selecting, but that's not very reliable.
  251. static void xwrite(unsigned int a, unsigned char d)
  252. {
  253. elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
  254. if (a >= 0xc000)
  255. PicoMem.zram[a & 0x1fff] = d;
  256. // Sega. Maps 4 bank 16KB each
  257. if (a >= 0xfff8 /*&& !Pico.ms.mapper*/)
  258. write_bank_sega(a, d);
  259. // Codemasters. Similar to Sega, but different addresses
  260. if (a == 0x0000 && !Pico.ms.mapper)
  261. write_bank_sega(0xfffd, d);
  262. if (a == 0x4000)
  263. write_bank_sega(0xfffe, d);
  264. if (a == 0x8000)
  265. write_bank_sega(0xffff, d);
  266. // Korean. 1 selectable 16KB bank at the top
  267. if (a == 0xa000)
  268. write_bank_sega(0xffff, d);
  269. // MSX. 4 selectable 8KB banks at the top
  270. if (a <= 0x0003 && (a || Pico.ms.mapper))
  271. write_bank_msx(a, d);
  272. }
  273. void PicoResetMS(void)
  274. {
  275. z80_reset();
  276. PsndReset(); // pal must be known here
  277. ymflag = 0xffff;
  278. Pico.m.dirtyPal = 1;
  279. // reset memory mapping
  280. PicoMemSetupMS();
  281. }
  282. void PicoPowerMS(void)
  283. {
  284. int s, tmp;
  285. memset(&PicoMem,0,sizeof(PicoMem));
  286. memset(&Pico.video,0,sizeof(Pico.video));
  287. memset(&Pico.m,0,sizeof(Pico.m));
  288. Pico.m.pal = 0;
  289. // calculate a mask for bank writes.
  290. // ROM loader has aligned the size for us, so this is safe.
  291. s = 0; tmp = Pico.romsize;
  292. while ((tmp >>= 1) != 0)
  293. s++;
  294. if (Pico.romsize > (1 << s))
  295. s++;
  296. tmp = 1 << s;
  297. bank_mask = (tmp - 1) >> 14;
  298. PicoReset();
  299. }
  300. void PicoMemSetupMS(void)
  301. {
  302. z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
  303. z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  304. z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
  305. z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
  306. z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
  307. z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
  308. #ifdef _USE_DRZ80
  309. drZ80.z80_in = z80_sms_in;
  310. drZ80.z80_out = z80_sms_out;
  311. #endif
  312. #ifdef _USE_CZ80
  313. Cz80_Set_INPort(&CZ80, z80_sms_in);
  314. Cz80_Set_OUTPort(&CZ80, z80_sms_out);
  315. #endif
  316. // memory mapper setup, linearly mapped, default is Sega mapper
  317. Pico.ms.carthw[0x00] = 4;
  318. Pico.ms.carthw[0x01] = 5;
  319. Pico.ms.carthw[0x02] = 2;
  320. Pico.ms.carthw[0x03] = 3;
  321. Pico.ms.carthw[0x0c] = 0;
  322. Pico.ms.carthw[0x0d] = 0;
  323. Pico.ms.carthw[0x0e] = 1;
  324. Pico.ms.carthw[0x0f] = 2;
  325. Pico.ms.mapper = 0;
  326. }
  327. void PicoStateLoadedMS(void)
  328. {
  329. if (Pico.ms.mapper) {
  330. xwrite(0x0000, Pico.ms.carthw[0]);
  331. xwrite(0x0001, Pico.ms.carthw[1]);
  332. xwrite(0x0002, Pico.ms.carthw[2]);
  333. xwrite(0x0003, Pico.ms.carthw[3]);
  334. } else {
  335. xwrite(0xfffc, Pico.ms.carthw[0x0c]);
  336. xwrite(0xfffd, Pico.ms.carthw[0x0d]);
  337. xwrite(0xfffe, Pico.ms.carthw[0x0e]);
  338. xwrite(0xffff, Pico.ms.carthw[0x0f]);
  339. }
  340. }
  341. void PicoFrameMS(void)
  342. {
  343. struct PicoVideo *pv = &Pico.video;
  344. int is_pal = Pico.m.pal;
  345. int lines = is_pal ? 313 : 262;
  346. int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
  347. int cycles_done = 0, cycles_aim = 0;
  348. int skip = PicoIn.skipFrame;
  349. int lines_vis = 192;
  350. int hint; // Hint counter
  351. int nmi;
  352. int y;
  353. PsndStartFrame();
  354. nmi = (PicoIn.pad[0] >> 7) & 1;
  355. if (!Pico.ms.nmi_state && nmi)
  356. z80_nmi();
  357. Pico.ms.nmi_state = nmi;
  358. if ((pv->reg[0] & 6) == 6 && (pv->reg[1] & 0x18))
  359. lines_vis = (pv->reg[1] & 0x08) ? 240 : 224;
  360. PicoFrameStartSMS();
  361. hint = pv->reg[0x0a];
  362. for (y = 0; y < lines; y++)
  363. {
  364. pv->v_counter = Pico.m.scanline = y;
  365. if (y > 218)
  366. pv->v_counter = y - 6;
  367. if (y < lines_vis && !skip)
  368. PicoLineSMS(y);
  369. if (y <= lines_vis)
  370. {
  371. if (--hint < 0)
  372. {
  373. hint = pv->reg[0x0a];
  374. pv->pending_ints |= 2;
  375. if (pv->reg[0] & 0x10) {
  376. elprintf(EL_INTS, "hint");
  377. z80_int_assert(1);
  378. }
  379. }
  380. }
  381. else if (y == lines_vis + 1) {
  382. pv->pending_ints |= 1;
  383. if (pv->reg[1] & 0x20) {
  384. elprintf(EL_INTS, "vint");
  385. z80_int_assert(1);
  386. }
  387. }
  388. cycles_aim += cycles_line;
  389. Pico.t.z80c_aim = cycles_aim >> 8;
  390. cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
  391. }
  392. if (PicoIn.sndOut)
  393. PsndGetSamplesMS(lines);
  394. }
  395. void PicoFrameDrawOnlyMS(void)
  396. {
  397. int lines_vis = 192;
  398. int y;
  399. PicoFrameStartSMS();
  400. for (y = 0; y < lines_vis; y++)
  401. PicoLineSMS(y);
  402. }
  403. // vim:ts=2:sw=2:expandtab