VideoPort.c 16 KB

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