videoport.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * PicoDrive
  3. * (c) Copyright Dave, 2004
  4. * (C) notaz, 2006-2009
  5. *
  6. * This work is licensed under the terms of MAME license.
  7. * See COPYING file in the top-level directory.
  8. */
  9. #include "pico_int.h"
  10. #define NEED_DMA_SOURCE
  11. #include "memory.h"
  12. extern const unsigned char hcounts_32[];
  13. extern const unsigned char hcounts_40[];
  14. #ifndef UTYPES_DEFINED
  15. typedef unsigned char u8;
  16. typedef unsigned short u16;
  17. typedef unsigned int u32;
  18. #define UTYPES_DEFINED
  19. #endif
  20. int (*PicoDmaHook)(unsigned int source, int len, unsigned short **base, unsigned int *mask) = NULL;
  21. static __inline void AutoIncrement(void)
  22. {
  23. Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
  24. }
  25. static NOINLINE void VideoWrite128(u32 a, u16 d)
  26. {
  27. // nasty
  28. a = ((a & 2) >> 1) | ((a & 0x400) >> 9) | (a & 0x3FC) | ((a & 0x1F800) >> 1);
  29. ((u8 *)PicoMem.vram)[a] = d;
  30. }
  31. static void VideoWrite(u16 d)
  32. {
  33. unsigned int a = Pico.video.addr;
  34. switch (Pico.video.type)
  35. {
  36. case 1: if (a & 1)
  37. d = (u16)((d << 8) | (d >> 8));
  38. PicoMem.vram [(a >> 1) & 0x7fff] = d;
  39. if (a - ((unsigned)(Pico.video.reg[5]&0x7f) << 9) < 0x400)
  40. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  41. break;
  42. case 3: Pico.m.dirtyPal = 1;
  43. PicoMem.cram [(a >> 1) & 0x3f] = d; break;
  44. case 5: PicoMem.vsram[(a >> 1) & 0x3f] = d; break;
  45. case 0x81:
  46. a |= Pico.video.addr_u << 16;
  47. VideoWrite128(a, d);
  48. break;
  49. //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;
  50. }
  51. AutoIncrement();
  52. }
  53. static unsigned int VideoRead(void)
  54. {
  55. unsigned int a=0,d=0;
  56. a=Pico.video.addr; a>>=1;
  57. switch (Pico.video.type)
  58. {
  59. case 0: d=PicoMem.vram [a & 0x7fff]; break;
  60. case 8: d=PicoMem.cram [a & 0x003f]; break;
  61. case 4: d=PicoMem.vsram[a & 0x003f]; break;
  62. default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;
  63. }
  64. AutoIncrement();
  65. return d;
  66. }
  67. static int GetDmaLength(void)
  68. {
  69. struct PicoVideo *pvid=&Pico.video;
  70. int len=0;
  71. // 16-bit words to transfer:
  72. len =pvid->reg[0x13];
  73. len|=pvid->reg[0x14]<<8;
  74. len = ((len - 1) & 0xffff) + 1;
  75. return len;
  76. }
  77. static void DmaSlow(int len, unsigned int source)
  78. {
  79. u32 inc = Pico.video.reg[0xf];
  80. u32 a = Pico.video.addr;
  81. u16 *r, *base = NULL;
  82. u32 mask = 0x1ffff;
  83. elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%u] @ %06x",
  84. Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
  85. SekCyclesDone(), SekPc);
  86. Pico.m.dma_xfers += len;
  87. if (Pico.m.dma_xfers < len) // lame 16bit var
  88. Pico.m.dma_xfers = ~0;
  89. SekCyclesBurnRun(CheckDMA());
  90. if ((source & 0xe00000) == 0xe00000) { // Ram
  91. base = (u16 *)PicoMem.ram;
  92. mask = 0xffff;
  93. }
  94. else if (PicoIn.AHW & PAHW_MCD)
  95. {
  96. u8 r3 = Pico_mcd->s68k_regs[3];
  97. elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", r3);
  98. if (source < 0x20000) { // Bios area
  99. base = (u16 *)Pico_mcd->bios;
  100. } else if ((source & 0xfc0000) == 0x200000) { // Word Ram
  101. if (!(r3 & 4)) { // 2M mode
  102. base = (u16 *)(Pico_mcd->word_ram2M + (source & 0x20000));
  103. } else {
  104. if (source < 0x220000) { // 1M mode
  105. int bank = r3 & 1;
  106. base = (u16 *)(Pico_mcd->word_ram1M[bank]);
  107. } else {
  108. DmaSlowCell(source - 2, a, len, inc);
  109. return;
  110. }
  111. }
  112. source -= 2;
  113. } else if ((source & 0xfe0000) == 0x020000) { // Prg Ram
  114. base = (u16 *)Pico_mcd->prg_ram_b[r3 >> 6];
  115. source -= 2; // XXX: test
  116. }
  117. }
  118. else
  119. {
  120. // if we have DmaHook, let it handle ROM because of possible DMA delay
  121. u32 source2;
  122. if (PicoDmaHook && (source2 = PicoDmaHook(source, len, &base, &mask)))
  123. source = source2;
  124. else // Rom
  125. base = m68k_dma_source(source);
  126. }
  127. if (!base) {
  128. elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
  129. return;
  130. }
  131. // operate in words
  132. source >>= 1;
  133. mask >>= 1;
  134. switch (Pico.video.type)
  135. {
  136. case 1: // vram
  137. r = PicoMem.vram;
  138. if (inc == 2 && !(a & 1) && a + len * 2 < 0x10000
  139. && !(((source + len - 1) ^ source) & ~mask))
  140. {
  141. // most used DMA mode
  142. memcpy((char *)r + a, base + (source & mask), len * 2);
  143. a += len * 2;
  144. }
  145. else
  146. {
  147. for(; len; len--)
  148. {
  149. u16 d = base[source++ & mask];
  150. if(a & 1) d=(d<<8)|(d>>8);
  151. r[a >> 1] = d;
  152. // AutoIncrement
  153. a = (u16)(a + inc);
  154. }
  155. }
  156. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  157. break;
  158. case 3: // cram
  159. Pico.m.dirtyPal = 1;
  160. r = PicoMem.cram;
  161. for (; len; len--)
  162. {
  163. r[(a / 2) & 0x3f] = base[source++ & mask];
  164. // AutoIncrement
  165. a += inc;
  166. }
  167. break;
  168. case 5: // vsram
  169. r = PicoMem.vsram;
  170. for (; len; len--)
  171. {
  172. r[(a / 2) & 0x3f] = base[source++ & mask];
  173. // AutoIncrement
  174. a += inc;
  175. }
  176. break;
  177. case 0x81: // vram 128k
  178. a |= Pico.video.addr_u << 16;
  179. for(; len; len--)
  180. {
  181. VideoWrite128(a, base[source++ & mask]);
  182. // AutoIncrement
  183. a = (a + inc) & 0x1ffff;
  184. }
  185. Pico.video.addr_u = a >> 16;
  186. break;
  187. default:
  188. if (Pico.video.type != 0 || (EL_LOGMASK & EL_VDPDMA))
  189. elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
  190. break;
  191. }
  192. // remember addr
  193. Pico.video.addr=(u16)a;
  194. }
  195. static void DmaCopy(int len)
  196. {
  197. u16 a = Pico.video.addr;
  198. u8 *vr = (u8 *)PicoMem.vram;
  199. u8 inc = Pico.video.reg[0xf];
  200. int source;
  201. elprintf(EL_VDPDMA, "DmaCopy len %i [%u]", len, SekCyclesDone());
  202. Pico.m.dma_xfers += len;
  203. if (Pico.m.dma_xfers < len)
  204. Pico.m.dma_xfers = ~0;
  205. Pico.video.status |= 2; // dma busy
  206. source =Pico.video.reg[0x15];
  207. source|=Pico.video.reg[0x16]<<8;
  208. for (; len; len--)
  209. {
  210. vr[a] = vr[source++ & 0xffff];
  211. // AutoIncrement
  212. a=(u16)(a+inc);
  213. }
  214. // remember addr
  215. Pico.video.addr=a;
  216. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  217. }
  218. static NOINLINE void DmaFill(int data)
  219. {
  220. u16 a = Pico.video.addr;
  221. u8 *vr = (u8 *)PicoMem.vram;
  222. u8 high = (u8)(data >> 8);
  223. u8 inc = Pico.video.reg[0xf];
  224. int source;
  225. int len, l;
  226. len = GetDmaLength();
  227. elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%u]", len, inc, SekCyclesDone());
  228. Pico.m.dma_xfers += len;
  229. if (Pico.m.dma_xfers < len) // lame 16bit var
  230. Pico.m.dma_xfers = ~0;
  231. Pico.video.status |= 2; // dma busy
  232. switch (Pico.video.type)
  233. {
  234. case 1: // vram
  235. for (l = len; l; l--) {
  236. // Write upper byte to adjacent address
  237. // (here we are byteswapped, so address is already 'adjacent')
  238. vr[a] = high;
  239. // Increment address register
  240. a = (u16)(a + inc);
  241. }
  242. break;
  243. case 3: // cram
  244. case 5: { // vsram
  245. // TODO: needs fifo; anyone using these?
  246. static int once;
  247. if (!once++)
  248. elprintf(EL_STATUS|EL_ANOMALY|EL_VDPDMA, "TODO: cram/vsram fill");
  249. }
  250. default:
  251. a += len * inc;
  252. break;
  253. }
  254. // remember addr
  255. Pico.video.addr = a;
  256. // register update
  257. Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0;
  258. source = Pico.video.reg[0x15];
  259. source |= Pico.video.reg[0x16] << 8;
  260. source += len;
  261. Pico.video.reg[0x15] = source;
  262. Pico.video.reg[0x16] = source >> 8;
  263. Pico.est.rendstatus |= PDRAW_DIRTY_SPRITES;
  264. }
  265. static NOINLINE void CommandDma(void)
  266. {
  267. struct PicoVideo *pvid=&Pico.video;
  268. u32 len, method;
  269. u32 source;
  270. if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
  271. len = GetDmaLength();
  272. source =Pico.video.reg[0x15];
  273. source|=Pico.video.reg[0x16] << 8;
  274. source|=Pico.video.reg[0x17] << 16;
  275. method=pvid->reg[0x17]>>6;
  276. if (method < 2)
  277. DmaSlow(len, source << 1); // 68000 to VDP
  278. else if (method == 3)
  279. DmaCopy(len); // VRAM Copy
  280. else
  281. return;
  282. source += len;
  283. Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0;
  284. Pico.video.reg[0x15] = source;
  285. Pico.video.reg[0x16] = source >> 8;
  286. }
  287. static NOINLINE void CommandChange(void)
  288. {
  289. struct PicoVideo *pvid = &Pico.video;
  290. unsigned int cmd, addr;
  291. cmd = pvid->command;
  292. // Get type of transfer 0xc0000030 (v/c/vsram read/write)
  293. pvid->type = (u8)(((cmd >> 2) & 0xc) | (cmd >> 30));
  294. if (pvid->type == 1) // vram
  295. pvid->type |= pvid->reg[1] & 0x80; // 128k
  296. // Get address 0x3fff0003
  297. addr = (cmd >> 16) & 0x3fff;
  298. addr |= (cmd << 14) & 0xc000;
  299. pvid->addr = (u16)addr;
  300. pvid->addr_u = (u8)((cmd >> 2) & 1);
  301. }
  302. static void DrawSync(int blank_on)
  303. {
  304. if (Pico.m.scanline < 224 && !(PicoIn.opt & POPT_ALT_RENDERER) &&
  305. !PicoIn.skipFrame && Pico.est.DrawScanline <= Pico.m.scanline) {
  306. //elprintf(EL_ANOMALY, "sync");
  307. PicoDrawSync(Pico.m.scanline, blank_on);
  308. }
  309. }
  310. PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
  311. {
  312. struct PicoVideo *pvid=&Pico.video;
  313. //elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x [%u] @ %06x",
  314. // a, d, SekCyclesDone(), SekPc);
  315. a &= 0x1c;
  316. switch (a)
  317. {
  318. case 0x00: // Data port 0 or 2
  319. // try avoiding the sync..
  320. if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&
  321. !(!pvid->pending &&
  322. ((pvid->command & 0xc00000f0) == 0x40000010 && PicoMem.vsram[pvid->addr>>1] == d))
  323. )
  324. DrawSync(0);
  325. if (pvid->pending) {
  326. CommandChange();
  327. pvid->pending=0;
  328. }
  329. if (!(pvid->status & SR_VB) && !(PicoIn.opt&POPT_DIS_VDP_FIFO))
  330. {
  331. int use = pvid->type == 1 ? 2 : 1;
  332. pvid->lwrite_cnt -= use;
  333. if (pvid->lwrite_cnt < 0)
  334. SekCyclesLeft = 0;
  335. elprintf(EL_ASVDP, "VDP data write: [%04x] %04x [%u] {%i} #%i @ %06x",
  336. Pico.video.addr, d, SekCyclesDone(), Pico.video.type, pvid->lwrite_cnt, SekPc);
  337. }
  338. VideoWrite(d);
  339. if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
  340. DmaFill(d);
  341. break;
  342. case 0x04: // Control (command) port 4 or 6
  343. if (pvid->pending)
  344. {
  345. // Low word of command:
  346. pvid->command &= 0xffff0000;
  347. pvid->command |= d;
  348. pvid->pending = 0;
  349. CommandChange();
  350. // Check for dma:
  351. if (d & 0x80) {
  352. DrawSync(0);
  353. CommandDma();
  354. }
  355. }
  356. else
  357. {
  358. if ((d&0xc000)==0x8000)
  359. {
  360. // Register write:
  361. int num=(d>>8)&0x1f;
  362. int dold=pvid->reg[num];
  363. int blank_on = 0;
  364. pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
  365. if (num > 0x0a && !(pvid->reg[1]&4)) {
  366. elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);
  367. return;
  368. }
  369. if (num == 1 && !(d&0x40) && SekCyclesDone() - Pico.t.m68c_line_start <= 488-390)
  370. blank_on = 1;
  371. DrawSync(blank_on);
  372. pvid->reg[num]=(unsigned char)d;
  373. switch (num)
  374. {
  375. case 0x00:
  376. elprintf(EL_INTSW, "hint_onoff: %i->%i [%u] pend=%i @ %06x", (dold&0x10)>>4,
  377. (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);
  378. goto update_irq;
  379. case 0x01:
  380. elprintf(EL_INTSW, "vint_onoff: %i->%i [%u] pend=%i @ %06x", (dold&0x20)>>5,
  381. (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);
  382. if (!(pvid->status & PVS_VB2))
  383. pvid->status &= ~SR_VB;
  384. pvid->status |= ((d >> 3) ^ SR_VB) & SR_VB; // forced blanking
  385. goto update_irq;
  386. case 0x05:
  387. //elprintf(EL_STATUS, "spritep moved to %04x", (unsigned)(Pico.video.reg[5]&0x7f) << 9);
  388. if (d^dold) Pico.est.rendstatus |= PDRAW_SPRITES_MOVED;
  389. break;
  390. case 0x0c:
  391. // renderers should update their palettes if sh/hi mode is changed
  392. if ((d^dold)&8) Pico.m.dirtyPal = 2;
  393. break;
  394. }
  395. return;
  396. update_irq:
  397. #ifndef EMU_CORE_DEBUG
  398. // update IRQ level
  399. if (!SekShouldInterrupt()) // hack
  400. {
  401. int lines, pints, irq = 0;
  402. lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
  403. pints = pvid->pending_ints & lines;
  404. if (pints & 0x20) irq = 6;
  405. else if (pints & 0x10) irq = 4;
  406. SekInterrupt(irq); // update line
  407. // this is broken because cost of current insn isn't known here
  408. if (irq) SekEndRun(21); // make it delayed
  409. }
  410. #endif
  411. }
  412. else
  413. {
  414. // High word of command:
  415. pvid->command&=0x0000ffff;
  416. pvid->command|=d<<16;
  417. pvid->pending=1;
  418. }
  419. }
  420. break;
  421. // case 0x08: // 08 0a - HV counter - lock up
  422. // case 0x0c: // 0c 0e - HV counter - lock up
  423. // case 0x10: // 10 12 - PSG - handled by caller
  424. // case 0x14: // 14 16 - PSG - handled by caller
  425. // case 0x18: // 18 1a - no effect?
  426. case 0x1c: // 1c 1e - debug
  427. pvid->debug = d;
  428. pvid->debug_p = 0;
  429. if (d & (1 << 6)) {
  430. pvid->debug_p |= PVD_KILL_A | PVD_KILL_B;
  431. pvid->debug_p |= PVD_KILL_S_LO | PVD_KILL_S_HI;
  432. }
  433. switch ((d >> 7) & 3) {
  434. case 1:
  435. pvid->debug_p &= ~(PVD_KILL_S_LO | PVD_KILL_S_HI);
  436. pvid->debug_p |= PVD_FORCE_S;
  437. break;
  438. case 2:
  439. pvid->debug_p &= ~PVD_KILL_A;
  440. pvid->debug_p |= PVD_FORCE_A;
  441. break;
  442. case 3:
  443. pvid->debug_p &= ~PVD_KILL_B;
  444. pvid->debug_p |= PVD_FORCE_B;
  445. break;
  446. }
  447. break;
  448. }
  449. }
  450. static u32 SrLow(const struct PicoVideo *pv)
  451. {
  452. unsigned int c, d = pv->status;
  453. c = SekCyclesDone() - Pico.t.m68c_line_start - 39;
  454. if (c < 92)
  455. d |= SR_HB;
  456. return d;
  457. }
  458. PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
  459. {
  460. a &= 0x1c;
  461. if (a == 0x04) // control port
  462. {
  463. struct PicoVideo *pv = &Pico.video;
  464. unsigned int d = SrLow(pv);
  465. pv->pending = 0;
  466. elprintf(EL_SR, "SR read: %04x [%u] @ %06x", d, SekCyclesDone(), SekPc);
  467. return d;
  468. }
  469. // H-counter info (based on Generator):
  470. // frame:
  471. // | <- hblank? -> |
  472. // start <416> hint <36> hdisplay <38> end // CPU cycles
  473. // |---------...---------|------------|-------------|
  474. // 0 B6 E4 FF // 40 cells
  475. // 0 93 E8 FF // 32 cells
  476. // Gens (?) v-render
  477. // start <hblank=84> hint hdisplay <404> |
  478. // |---------------------|--------------------------|
  479. // E4 (hc[0x43]==0) 07 B1 // 40
  480. // E8 (hc[0x45]==0) 05 91 // 32
  481. // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
  482. if ((a&0x1c)==0x08)
  483. {
  484. unsigned int d;
  485. d = (SekCyclesDone() - Pico.t.m68c_line_start) & 0x1ff; // FIXME
  486. if (Pico.video.reg[12]&1)
  487. d = hcounts_40[d];
  488. else d = hcounts_32[d];
  489. elprintf(EL_HVCNT, "hv: %02x %02x [%u] @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);
  490. return d | (Pico.video.v_counter << 8);
  491. }
  492. if (a==0x00) // data port
  493. {
  494. return VideoRead();
  495. }
  496. return 0;
  497. }
  498. unsigned char PicoVideoRead8DataH(void)
  499. {
  500. return VideoRead() >> 8;
  501. }
  502. unsigned char PicoVideoRead8DataL(void)
  503. {
  504. return VideoRead();
  505. }
  506. unsigned char PicoVideoRead8CtlH(void)
  507. {
  508. u8 d = (u8)(Pico.video.status >> 8);
  509. Pico.video.pending = 0;
  510. elprintf(EL_SR, "SR read (h): %02x @ %06x", d, SekPc);
  511. return d;
  512. }
  513. unsigned char PicoVideoRead8CtlL(void)
  514. {
  515. u8 d = SrLow(&Pico.video);
  516. Pico.video.pending = 0;
  517. elprintf(EL_SR, "SR read (l): %02x @ %06x", d, SekPc);
  518. return d;
  519. }
  520. unsigned char PicoVideoRead8HV_H(void)
  521. {
  522. elprintf(EL_HVCNT, "vcounter: %02x [%u] @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);
  523. return Pico.video.v_counter;
  524. }
  525. // FIXME: broken
  526. unsigned char PicoVideoRead8HV_L(void)
  527. {
  528. u32 d = (SekCyclesDone() - Pico.t.m68c_line_start) & 0x1ff; // FIXME
  529. if (Pico.video.reg[12]&1)
  530. d = hcounts_40[d];
  531. else d = hcounts_32[d];
  532. elprintf(EL_HVCNT, "hcounter: %02x [%u] @ %06x", d, SekCyclesDone(), SekPc);
  533. return d;
  534. }
  535. // vim:shiftwidth=2:ts=2:expandtab