videoport.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. int line_base_cycles;
  11. extern const unsigned char hcounts_32[];
  12. extern const unsigned char hcounts_40[];
  13. #ifndef UTYPES_DEFINED
  14. typedef unsigned char u8;
  15. typedef unsigned short u16;
  16. typedef unsigned int u32;
  17. #define UTYPES_DEFINED
  18. #endif
  19. int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) = NULL;
  20. static __inline void AutoIncrement(void)
  21. {
  22. Pico.video.addr=(unsigned short)(Pico.video.addr+Pico.video.reg[0xf]);
  23. }
  24. static void VideoWrite(u16 d)
  25. {
  26. unsigned int a=Pico.video.addr;
  27. switch (Pico.video.type)
  28. {
  29. case 1: if(a&1) d=(u16)((d<<8)|(d>>8)); // If address is odd, bytes are swapped (which game needs this?)
  30. Pico.vram [(a>>1)&0x7fff]=d;
  31. if (a - ((unsigned)(Pico.video.reg[5]&0x7f) << 9) < 0x400)
  32. rendstatus |= PDRAW_DIRTY_SPRITES;
  33. break;
  34. case 3: Pico.m.dirtyPal = 1;
  35. Pico.cram [(a>>1)&0x003f]=d; break; // wraps (Desert Strike)
  36. case 5: Pico.vsram[(a>>1)&0x003f]=d; break;
  37. //default:elprintf(EL_ANOMALY, "VDP write %04x with bad type %i", d, Pico.video.type); break;
  38. }
  39. AutoIncrement();
  40. }
  41. static unsigned int VideoRead(void)
  42. {
  43. unsigned int a=0,d=0;
  44. a=Pico.video.addr; a>>=1;
  45. switch (Pico.video.type)
  46. {
  47. case 0: d=Pico.vram [a&0x7fff]; break;
  48. case 8: d=Pico.cram [a&0x003f]; break;
  49. case 4: d=Pico.vsram[a&0x003f]; break;
  50. default:elprintf(EL_ANOMALY, "VDP read with bad type %i", Pico.video.type); break;
  51. }
  52. AutoIncrement();
  53. return d;
  54. }
  55. static int GetDmaLength(void)
  56. {
  57. struct PicoVideo *pvid=&Pico.video;
  58. int len=0;
  59. // 16-bit words to transfer:
  60. len =pvid->reg[0x13];
  61. len|=pvid->reg[0x14]<<8;
  62. // Charles MacDonald:
  63. if(!len) len = 0xffff;
  64. return len;
  65. }
  66. static void DmaSlow(int len)
  67. {
  68. u16 *pd=0, *pdend, *r;
  69. unsigned int a=Pico.video.addr, a2, d;
  70. unsigned char inc=Pico.video.reg[0xf];
  71. unsigned int source;
  72. source =Pico.video.reg[0x15]<<1;
  73. source|=Pico.video.reg[0x16]<<9;
  74. source|=Pico.video.reg[0x17]<<17;
  75. elprintf(EL_VDPDMA, "DmaSlow[%i] %06x->%04x len %i inc=%i blank %i [%i] @ %06x",
  76. Pico.video.type, source, a, len, inc, (Pico.video.status&8)||!(Pico.video.reg[1]&0x40),
  77. SekCyclesDone(), SekPc);
  78. Pico.m.dma_xfers += len;
  79. SekCyclesBurnRun(CheckDMA());
  80. if ((source&0xe00000)==0xe00000) { // Ram
  81. pd=(u16 *)(Pico.ram+(source&0xfffe));
  82. pdend=(u16 *)(Pico.ram+0x10000);
  83. }
  84. else if (PicoAHW & PAHW_MCD)
  85. {
  86. elprintf(EL_VDPDMA, "DmaSlow CD, r3=%02x", Pico_mcd->s68k_regs[3]);
  87. if(source<0x20000) { // Bios area
  88. pd=(u16 *)(Pico_mcd->bios+(source&~1));
  89. pdend=(u16 *)(Pico_mcd->bios+0x20000);
  90. } else if ((source&0xfc0000)==0x200000) { // Word Ram
  91. source -= 2;
  92. if (!(Pico_mcd->s68k_regs[3]&4)) { // 2M mode
  93. pd=(u16 *)(Pico_mcd->word_ram2M+(source&0x3fffe));
  94. pdend=(u16 *)(Pico_mcd->word_ram2M+0x40000);
  95. } else {
  96. if (source < 0x220000) { // 1M mode
  97. int bank = Pico_mcd->s68k_regs[3]&1;
  98. pd=(u16 *)(Pico_mcd->word_ram1M[bank]+(source&0x1fffe));
  99. pdend=(u16 *)(Pico_mcd->word_ram1M[bank]+0x20000);
  100. } else {
  101. DmaSlowCell(source, a, len, inc);
  102. return;
  103. }
  104. }
  105. } else if ((source&0xfe0000)==0x020000) { // Prg Ram
  106. u8 *prg_ram = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];
  107. pd=(u16 *)(prg_ram+(source&0x1fffe));
  108. pdend=(u16 *)(prg_ram+0x20000);
  109. } else {
  110. elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: FIXME: unsupported src", Pico.video.type, source, a);
  111. return;
  112. }
  113. }
  114. else
  115. {
  116. // if we have DmaHook, let it handle ROM because of possible DMA delay
  117. if (PicoDmaHook && PicoDmaHook(source, len, &pd, &pdend));
  118. else if (source<Pico.romsize) { // Rom
  119. pd=(u16 *)(Pico.rom+(source&~1));
  120. pdend=(u16 *)(Pico.rom+Pico.romsize);
  121. }
  122. else {
  123. elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow[%i] %06x->%04x: invalid src", Pico.video.type, source, a);
  124. return;
  125. }
  126. }
  127. // overflow protection, might break something..
  128. if (len > pdend - pd) {
  129. len = pdend - pd;
  130. elprintf(EL_VDPDMA|EL_ANOMALY, "DmaSlow overflow");
  131. }
  132. switch (Pico.video.type)
  133. {
  134. case 1: // vram
  135. r = Pico.vram;
  136. if (inc == 2 && !(a&1) && a+len*2 < 0x10000)
  137. {
  138. // most used DMA mode
  139. memcpy16(r + (a>>1), pd, len);
  140. a += len*2;
  141. }
  142. else
  143. {
  144. for(; len; len--)
  145. {
  146. d=*pd++;
  147. if(a&1) d=(d<<8)|(d>>8);
  148. r[a>>1] = (u16)d; // will drop the upper bits
  149. // AutoIncrement
  150. a=(u16)(a+inc);
  151. // didn't src overlap?
  152. //if(pd >= pdend) pd-=0x8000; // should be good for RAM, bad for ROM
  153. }
  154. }
  155. rendstatus |= PDRAW_DIRTY_SPRITES;
  156. break;
  157. case 3: // cram
  158. Pico.m.dirtyPal = 1;
  159. r = Pico.cram;
  160. for(a2=a&0x7f; len; len--)
  161. {
  162. r[a2>>1] = (u16)*pd++; // bit 0 is ignored
  163. // AutoIncrement
  164. a2+=inc;
  165. // didn't src overlap?
  166. //if(pd >= pdend) pd-=0x8000;
  167. // good dest?
  168. if(a2 >= 0x80) break; // Todds Adventures in Slime World / Andre Agassi tennis
  169. }
  170. a=(a&0xff00)|a2;
  171. break;
  172. case 5: // vsram[a&0x003f]=d;
  173. r = Pico.vsram;
  174. for(a2=a&0x7f; len; len--)
  175. {
  176. r[a2>>1] = (u16)*pd++;
  177. // AutoIncrement
  178. a2+=inc;
  179. // didn't src overlap?
  180. //if(pd >= pdend) pd-=0x8000;
  181. // good dest?
  182. if(a2 >= 0x80) break;
  183. }
  184. a=(a&0xff00)|a2;
  185. break;
  186. default:
  187. if (Pico.video.type != 0 || (EL_LOGMASK & EL_VDPDMA))
  188. elprintf(EL_VDPDMA|EL_ANOMALY, "DMA with bad type %i", Pico.video.type);
  189. break;
  190. }
  191. // remember addr
  192. Pico.video.addr=(u16)a;
  193. }
  194. static void DmaCopy(int len)
  195. {
  196. u16 a=Pico.video.addr;
  197. unsigned char *vr = (unsigned char *) Pico.vram;
  198. unsigned char *vrs;
  199. unsigned char inc=Pico.video.reg[0xf];
  200. int source;
  201. elprintf(EL_VDPDMA, "DmaCopy len %i [%i]", len, SekCyclesDone());
  202. Pico.m.dma_xfers += len;
  203. Pico.video.status |= 2; // dma busy
  204. source =Pico.video.reg[0x15];
  205. source|=Pico.video.reg[0x16]<<8;
  206. vrs=vr+source;
  207. if (source+len > 0x10000) len=0x10000-source; // clip??
  208. for (; len; len--)
  209. {
  210. vr[a] = *vrs++;
  211. // AutoIncrement
  212. a=(u16)(a+inc);
  213. }
  214. // remember addr
  215. Pico.video.addr=a;
  216. rendstatus |= PDRAW_DIRTY_SPRITES;
  217. }
  218. // check: Contra, Megaman
  219. // note: this is still inaccurate
  220. static void DmaFill(int data)
  221. {
  222. int len;
  223. unsigned short a=Pico.video.addr;
  224. unsigned char *vr=(unsigned char *) Pico.vram;
  225. unsigned char high = (unsigned char) (data >> 8);
  226. unsigned char inc=Pico.video.reg[0xf];
  227. len=GetDmaLength();
  228. elprintf(EL_VDPDMA, "DmaFill len %i inc %i [%i]", len, inc, SekCyclesDone());
  229. Pico.m.dma_xfers += len;
  230. Pico.video.status |= 2; // dma busy
  231. // from Charles MacDonald's genvdp.txt:
  232. // Write lower byte to address specified
  233. vr[a] = (unsigned char) data;
  234. a=(u16)(a+inc);
  235. if (!inc) len=1;
  236. for (; len; len--) {
  237. // Write upper byte to adjacent address
  238. // (here we are byteswapped, so address is already 'adjacent')
  239. vr[a] = high;
  240. // Increment address register
  241. a=(u16)(a+inc);
  242. }
  243. // remember addr
  244. Pico.video.addr=a;
  245. // update length
  246. Pico.video.reg[0x13] = Pico.video.reg[0x14] = 0; // Dino Dini's Soccer (E) (by Haze)
  247. rendstatus |= PDRAW_DIRTY_SPRITES;
  248. }
  249. static void CommandDma(void)
  250. {
  251. struct PicoVideo *pvid=&Pico.video;
  252. int len=0,method=0;
  253. if ((pvid->reg[1]&0x10)==0) return; // DMA not enabled
  254. len=GetDmaLength();
  255. method=pvid->reg[0x17]>>6;
  256. if (method< 2) DmaSlow(len); // 68000 to VDP
  257. if (method==3) DmaCopy(len); // VRAM Copy
  258. }
  259. static void CommandChange(void)
  260. {
  261. struct PicoVideo *pvid=&Pico.video;
  262. unsigned int cmd=0,addr=0;
  263. cmd=pvid->command;
  264. // Get type of transfer 0xc0000030 (v/c/vsram read/write)
  265. pvid->type=(unsigned char)(((cmd>>2)&0xc)|(cmd>>30));
  266. // Get address 0x3fff0003
  267. addr =(cmd>>16)&0x3fff;
  268. addr|=(cmd<<14)&0xc000;
  269. pvid->addr=(unsigned short)addr;
  270. // Check for dma:
  271. if (cmd&0x80) CommandDma();
  272. }
  273. static void DrawSync(int blank_on)
  274. {
  275. if (Pico.m.scanline < 224 && !(PicoOpt & POPT_ALT_RENDERER) &&
  276. !PicoSkipFrame && DrawScanline <= Pico.m.scanline) {
  277. //elprintf(EL_ANOMALY, "sync");
  278. PicoDrawSync(Pico.m.scanline, blank_on);
  279. }
  280. }
  281. PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d)
  282. {
  283. struct PicoVideo *pvid=&Pico.video;
  284. //if (Pico.m.scanline < 224)
  285. // elprintf(EL_STATUS, "PicoVideoWrite [%06x] %04x", a, d);
  286. a&=0x1c;
  287. if (a==0x00) // Data port 0 or 2
  288. {
  289. // try avoiding the sync..
  290. if (Pico.m.scanline < 224 && (pvid->reg[1]&0x40) &&
  291. !(!pvid->pending &&
  292. ((pvid->command & 0xc00000f0) == 0x40000010 && Pico.vsram[pvid->addr>>1] == d))
  293. )
  294. DrawSync(0);
  295. if (pvid->pending) {
  296. CommandChange();
  297. pvid->pending=0;
  298. }
  299. // If a DMA fill has been set up, do it
  300. if ((pvid->command&0x80) && (pvid->reg[1]&0x10) && (pvid->reg[0x17]>>6)==2)
  301. {
  302. DmaFill(d);
  303. }
  304. else
  305. {
  306. // preliminary FIFO emulation for Chaos Engine, The (E)
  307. if (!(pvid->status&8) && (pvid->reg[1]&0x40) && !(PicoOpt&POPT_DIS_VDP_FIFO)) // active display?
  308. {
  309. pvid->status&=~0x200; // FIFO no longer empty
  310. pvid->lwrite_cnt++;
  311. if (pvid->lwrite_cnt >= 4) pvid->status|=0x100; // FIFO full
  312. if (pvid->lwrite_cnt > 4) {
  313. SekCyclesBurnRun(32); // penalty // 488/12-8
  314. }
  315. elprintf(EL_ASVDP, "VDP data write: %04x [%06x] {%i} #%i @ %06x", d, Pico.video.addr,
  316. Pico.video.type, pvid->lwrite_cnt, SekPc);
  317. }
  318. VideoWrite(d);
  319. }
  320. return;
  321. }
  322. if (a==0x04) // Control (command) port 4 or 6
  323. {
  324. if (pvid->pending)
  325. {
  326. if (d & 0x80) DrawSync(0); // only need sync for DMA
  327. // Low word of command:
  328. pvid->command&=0xffff0000;
  329. pvid->command|=d;
  330. pvid->pending=0;
  331. CommandChange();
  332. }
  333. else
  334. {
  335. if ((d&0xc000)==0x8000)
  336. {
  337. // Register write:
  338. int num=(d>>8)&0x1f;
  339. int dold=pvid->reg[num];
  340. int blank_on = 0;
  341. pvid->type=0; // register writes clear command (else no Sega logo in Golden Axe II)
  342. if (num > 0x0a && !(pvid->reg[1]&4)) {
  343. elprintf(EL_ANOMALY, "%02x written to reg %02x in SMS mode @ %06x", d, num, SekPc);
  344. return;
  345. }
  346. if (num == 1 && !(d&0x40) && SekCyclesDone() - line_base_cycles <= 488-390)
  347. blank_on = 1;
  348. DrawSync(blank_on);
  349. pvid->reg[num]=(unsigned char)d;
  350. switch (num)
  351. {
  352. case 0x00:
  353. elprintf(EL_INTSW, "hint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x10)>>4,
  354. (d&0x10)>>4, SekCyclesDone(), (pvid->pending_ints&0x10)>>4, SekPc);
  355. goto update_irq;
  356. case 0x01:
  357. elprintf(EL_INTSW, "vint_onoff: %i->%i [%i] pend=%i @ %06x", (dold&0x20)>>5,
  358. (d&0x20)>>5, SekCyclesDone(), (pvid->pending_ints&0x20)>>5, SekPc);
  359. goto update_irq;
  360. case 0x05:
  361. //elprintf(EL_STATUS, "spritep moved to %04x", (unsigned)(Pico.video.reg[5]&0x7f) << 9);
  362. if (d^dold) rendstatus |= PDRAW_SPRITES_MOVED;
  363. break;
  364. case 0x0c:
  365. // renderers should update their palettes if sh/hi mode is changed
  366. if ((d^dold)&8) Pico.m.dirtyPal = 2;
  367. break;
  368. }
  369. return;
  370. update_irq:
  371. #ifndef EMU_CORE_DEBUG
  372. // update IRQ level
  373. if (!SekShouldInterrupt()) // hack
  374. {
  375. int lines, pints, irq=0;
  376. lines = (pvid->reg[1] & 0x20) | (pvid->reg[0] & 0x10);
  377. pints = (pvid->pending_ints&lines);
  378. if (pints & 0x20) irq = 6;
  379. else if (pints & 0x10) irq = 4;
  380. SekInterrupt(irq); // update line
  381. if (irq) SekEndRun(24); // make it delayed
  382. }
  383. #endif
  384. }
  385. else
  386. {
  387. // High word of command:
  388. pvid->command&=0x0000ffff;
  389. pvid->command|=d<<16;
  390. pvid->pending=1;
  391. }
  392. }
  393. }
  394. }
  395. PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a)
  396. {
  397. a&=0x1c;
  398. if (a==0x04) // control port
  399. {
  400. struct PicoVideo *pv=&Pico.video;
  401. unsigned int d;
  402. d=pv->status;
  403. //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
  404. if (SekCyclesDone() - line_base_cycles >= 488-88)
  405. d|=0x0004; // H-Blank (Sonic3 vs)
  406. d |= ((pv->reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
  407. d |= (pv->pending_ints&0x20)<<2; // V-int pending?
  408. if (d&0x100) pv->status&=~0x100; // FIFO no longer full
  409. pv->pending = 0; // ctrl port reads clear write-pending flag (Charles MacDonald)
  410. elprintf(EL_SR, "SR read: %04x @ %06x", d, SekPc);
  411. return d;
  412. }
  413. // H-counter info (based on Generator):
  414. // frame:
  415. // | <- hblank? -> |
  416. // start <416> hint <36> hdisplay <38> end // CPU cycles
  417. // |---------...---------|------------|-------------|
  418. // 0 B6 E4 FF // 40 cells
  419. // 0 93 E8 FF // 32 cells
  420. // Gens (?) v-render
  421. // start <hblank=84> hint hdisplay <404> |
  422. // |---------------------|--------------------------|
  423. // E4 (hc[0x43]==0) 07 B1 // 40
  424. // E8 (hc[0x45]==0) 05 91 // 32
  425. // check: Sonic 3D Blast bonus, Cannon Fodder, Chase HQ II, 3 Ninjas kick back, Road Rash 3, Skitchin', Wheel of Fortune
  426. if ((a&0x1c)==0x08)
  427. {
  428. unsigned int d;
  429. d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME
  430. if (Pico.video.reg[12]&1)
  431. d = hcounts_40[d];
  432. else d = hcounts_32[d];
  433. elprintf(EL_HVCNT, "hv: %02x %02x (%i) @ %06x", d, Pico.video.v_counter, SekCyclesDone(), SekPc);
  434. return d | (Pico.video.v_counter << 8);
  435. }
  436. if (a==0x00) // data port
  437. {
  438. return VideoRead();
  439. }
  440. return 0;
  441. }
  442. unsigned int PicoVideoRead8(unsigned int a)
  443. {
  444. unsigned int d;
  445. a&=0x1d;
  446. switch (a)
  447. {
  448. case 0: return VideoRead() >> 8;
  449. case 1: return VideoRead() & 0xff;
  450. case 4: // control port/status reg
  451. d = Pico.video.status >> 8;
  452. if (d&1) Pico.video.status&=~0x100; // FIFO no longer full
  453. Pico.video.pending = 0;
  454. elprintf(EL_SR, "SR read (h): %02x @ %06x", d, SekPc);
  455. return d;
  456. case 5:
  457. d = Pico.video.status & 0xff;
  458. //if (PicoOpt&POPT_ALT_RENDERER) d|=0x0020; // sprite collision (Shadow of the Beast)
  459. d |= ((Pico.video.reg[1]&0x40)^0x40) >> 3; // set V-Blank if display is disabled
  460. d |= (Pico.video.pending_ints&0x20)<<2; // V-int pending?
  461. if (SekCyclesDone() - line_base_cycles >= 488-88) d |= 4; // H-Blank
  462. Pico.video.pending = 0;
  463. elprintf(EL_SR, "SR read (l): %02x @ %06x", d, SekPc);
  464. return d;
  465. case 8: // hv counter
  466. elprintf(EL_HVCNT, "vcounter: %02x (%i) @ %06x", Pico.video.v_counter, SekCyclesDone(), SekPc);
  467. return Pico.video.v_counter;
  468. case 9:
  469. d = (SekCyclesDone() - line_base_cycles) & 0x1ff; // FIXME
  470. if (Pico.video.reg[12]&1)
  471. d = hcounts_40[d];
  472. else d = hcounts_32[d];
  473. elprintf(EL_HVCNT, "hcounter: %02x (%i) @ %06x", d, SekCyclesDone(), SekPc);
  474. return d;
  475. }
  476. return 0;
  477. }
  478. // vim:shiftwidth=2:ts=2:expandtab