gfx_cd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // This is a direct rewrite of gfx_cd.asm (x86 asm to C).
  2. // You can even find some x86 register names :)
  3. // Original code (c) 2002 by Stéphane Dallongeville
  4. // (c) Copyright 2007, Grazvydas "notaz" Ignotas
  5. #include "../PicoInt.h"
  6. #define _rot_comp Pico_mcd->rot_comp
  7. static const int Table_Rot_Time[] =
  8. {
  9. 0x00054000, 0x00048000, 0x00040000, 0x00036000, //; 008-032 ; briefing - sprite
  10. 0x0002E000, 0x00028000, 0x00024000, 0x00022000, //; 036-064 ; arbre souvent
  11. 0x00021000, 0x00020000, 0x0001E000, 0x0001B800, //; 068-096 ; map thunderstrike
  12. 0x00019800, 0x00017A00, 0x00015C00, 0x00013E00, //; 100-128 ; logo défoncé
  13. 0x00012000, 0x00011800, 0x00011000, 0x00010800, //; 132-160 ; briefing - map
  14. 0x00010000, 0x0000F800, 0x0000F000, 0x0000E800, //; 164-192
  15. 0x0000E000, 0x0000D800, 0x0000D000, 0x0000C800, //; 196-224
  16. 0x0000C000, 0x0000B800, 0x0000B000, 0x0000A800, //; 228-256 ; batman visage
  17. 0x0000A000, 0x00009F00, 0x00009E00, 0x00009D00, //; 260-288
  18. 0x00009C00, 0x00009B00, 0x00009A00, 0x00009900, //; 292-320
  19. 0x00009800, 0x00009700, 0x00009600, 0x00009500, //; 324-352
  20. 0x00009400, 0x00009300, 0x00009200, 0x00009100, //; 356-384
  21. 0x00009000, 0x00008F00, 0x00008E00, 0x00008D00, //; 388-416
  22. 0x00008C00, 0x00008B00, 0x00008A00, 0x00008900, //; 420-448
  23. 0x00008800, 0x00008700, 0x00008600, 0x00008500, //; 452-476
  24. 0x00008400, 0x00008300, 0x00008200, 0x00008100, //; 480-512
  25. };
  26. static void gfx_cd_start(void)
  27. {
  28. int upd_len;
  29. // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
  30. _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18); // Jmp_Adr
  31. // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
  32. _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
  33. _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
  34. upd_len = (_rot_comp.Reg_62 >> 3) & 0x3f;
  35. upd_len = Table_Rot_Time[upd_len];
  36. _rot_comp.Draw_Speed = _rot_comp.Float_Part = upd_len;
  37. _rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
  38. switch (_rot_comp.Reg_58 & 6) // Scr_16?
  39. {
  40. case 0: // ?
  41. _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
  42. break;
  43. case 2: // .Dot_32
  44. _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
  45. break;
  46. case 4: // .Scr_16
  47. _rot_comp.Stamp_Map_Adr = 0x20000;
  48. break;
  49. case 6: // .Scr_16_Dot_32
  50. _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
  51. break;
  52. }
  53. dprintf("gfx_cd_start, stamp_map_addr=%06x", _rot_comp.Stamp_Map_Adr);
  54. gfx_cd_update();
  55. }
  56. static void gfx_completed(void)
  57. {
  58. _rot_comp.Reg_58 &= 0x7fff; // Stamp_Size
  59. _rot_comp.Reg_64 = 0;
  60. if (Pico_mcd->s68k_regs[0x33] & (1<<1))
  61. {
  62. dprintf("gfx_cd irq 1");
  63. SekInterruptS68k(1);
  64. }
  65. }
  66. static void gfx_do(unsigned int func, unsigned short *stamp_base, unsigned int H_Dot)
  67. {
  68. unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
  69. unsigned int XD, Buffer_Adr;
  70. int DYXS;
  71. XD = _rot_comp.Reg_60 & 7;
  72. Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
  73. ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
  74. edx = ecx >> 16;
  75. ecx = (ecx & 0xffff) << 8;
  76. edx <<= 8;
  77. DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
  78. _rot_comp.Vector_Adr += 8;
  79. // MAKE_IMAGE_LINE
  80. while (H_Dot)
  81. {
  82. // MAKE_IMAGE_PIXEL
  83. if (!(func & 1)) // NOT TILED
  84. {
  85. int mask = (func & 4) ? 0x00800000 : 0x00f80000;
  86. if ((ecx | edx) & mask)
  87. {
  88. if (func & 0x18) goto Next_Pixel;
  89. pixel = 0;
  90. goto Pixel_Out;
  91. }
  92. }
  93. if (func & 2) // mode 32x32 dot
  94. {
  95. if (func & 4) // 16x16 screen
  96. {
  97. ebx = ((ecx >> (11+5)) & 0x007f) |
  98. ((edx >> (11-2)) & 0x3f80);
  99. }
  100. else // 1x1 screen
  101. {
  102. ebx = ((ecx >> (11+5)) & 0x07) |
  103. ((edx >> (11+2)) & 0x38);
  104. }
  105. }
  106. else // mode 16x16 dot
  107. {
  108. if (func & 4) // 16x16 screen
  109. {
  110. ebx = ((ecx >> (11+4)) & 0x00ff) |
  111. ((edx >> (11-4)) & 0xff00);
  112. }
  113. else // 1x1 screen
  114. {
  115. ebx = ((ecx >> (11+4)) & 0x0f) |
  116. ((edx >> (11+0)) & 0xf0);
  117. }
  118. }
  119. edi = stamp_base[ebx];
  120. esi = (edi & 0x7ff) << 7;
  121. if (!esi) { pixel = 0; goto Pixel_Out; }
  122. edi >>= (11+1);
  123. edi &= (0x1c>>1);
  124. eax = ecx;
  125. ebx = edx;
  126. if (func & 2) edi |= 1; // 32 dots?
  127. switch (edi)
  128. {
  129. case 0x00: // No_Flip_0, 16x16 dots
  130. ebx = (ebx >> 9) & 0x3c;
  131. ebx += esi;
  132. edi = (eax & 0x3800) ^ 0x1000; // bswap
  133. eax = ((eax >> 8) & 0x40) + ebx;
  134. break;
  135. case 0x01: // No_Flip_0, 32x32 dots
  136. ebx = (ebx >> 9) & 0x7c;
  137. ebx += esi;
  138. edi = (eax & 0x3800) ^ 0x1000; // bswap
  139. eax = ((eax >> 7) & 0x180) + ebx;
  140. break;
  141. case 0x02: // No_Flip_90, 16x16 dots
  142. eax = (eax >> 9) & 0x3c;
  143. eax += esi;
  144. edi = (ebx & 0x3800) ^ 0x2800; // bswap
  145. eax += ((ebx >> 8) & 0x40) ^ 0x40;
  146. break;
  147. case 0x03: // No_Flip_90, 32x32 dots
  148. eax = (eax >> 9) & 0x7c;
  149. eax += esi;
  150. edi = (ebx & 0x3800) ^ 0x2800; // bswap
  151. eax += ((ebx >> 7) & 0x180) ^ 0x180;
  152. break;
  153. case 0x04: // No_Flip_180, 16x16 dots
  154. ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
  155. ebx += esi;
  156. edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
  157. eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
  158. break;
  159. case 0x05: // No_Flip_180, 32x32 dots
  160. ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
  161. ebx += esi;
  162. edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
  163. eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
  164. break;
  165. case 0x06: // No_Flip_270, 16x16 dots
  166. eax = ((eax >> 9) & 0x3c) ^ 0x3c;
  167. eax += esi;
  168. edi = (ebx & 0x3800) ^ 0x1000; // bswap
  169. eax += (ebx >> 8) & 0x40;
  170. break;
  171. case 0x07: // No_Flip_270, 32x32 dots
  172. eax = ((eax >> 9) & 0x7c) ^ 0x7c;
  173. eax += esi;
  174. edi = (ebx & 0x3800) ^ 0x1000; // bswap
  175. eax += (ebx >> 7) & 0x180;
  176. break;
  177. case 0x08: // Flip_0, 16x16 dots
  178. ebx = (ebx >> 9) & 0x3c;
  179. ebx += esi;
  180. edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
  181. eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
  182. break;
  183. case 0x09: // Flip_0, 32x32 dots
  184. ebx = (ebx >> 9) & 0x7c;
  185. ebx += esi;
  186. edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
  187. eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
  188. break;
  189. case 0x0a: // Flip_90, 16x16 dots
  190. eax = ((eax >> 9) & 0x3c) ^ 0x3c;
  191. eax += esi;
  192. edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
  193. eax += ((ebx >> 8) & 0x40) ^ 0x40;
  194. break;
  195. case 0x0b: // Flip_90, 32x32 dots
  196. eax = ((eax >> 9) & 0x7c) ^ 0x7c;
  197. eax += esi;
  198. edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
  199. eax += ((ebx >> 7) & 0x180) ^ 0x180;
  200. break;
  201. case 0x0c: // Flip_180, 16x16 dots
  202. ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
  203. ebx += esi;
  204. edi = (eax & 0x3800) ^ 0x1000; // bswap
  205. eax = ((eax >> 8) & 0x40) + ebx;
  206. break;
  207. case 0x0d: // Flip_180, 32x32 dots
  208. ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
  209. ebx += esi;
  210. edi = (eax & 0x3800) ^ 0x1000; // bswap
  211. eax = ((eax >> 7) & 0x180) + ebx;
  212. break;
  213. case 0x0e: // Flip_270, 16x16 dots
  214. eax = (eax >> 9) & 0x3c;
  215. eax += esi;
  216. edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
  217. eax += (ebx >> 8) & 0x40;
  218. break;
  219. case 0x0f: // Flip_270, 32x32 dots
  220. eax = (eax >> 9) & 0x7c;
  221. eax += esi;
  222. edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
  223. eax += (ebx >> 7) & 0x180;
  224. break;
  225. }
  226. pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
  227. if (!(edi & 0x800)) pixel >>= 4;
  228. else pixel &= 0x0f;
  229. Pixel_Out:
  230. if (!pixel && (func & 0x18)) goto Next_Pixel;
  231. esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
  232. eax = *(Pico_mcd->word_ram2M + esi); // old pixel
  233. if (XD & 1)
  234. {
  235. if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
  236. *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
  237. }
  238. else
  239. {
  240. if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
  241. *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
  242. }
  243. Next_Pixel:
  244. ecx += (DYXS << 16) >> 16; // _rot_comp.DXS;
  245. edx += DYXS >> 16; // _rot_comp.DYS;
  246. XD++;
  247. if (XD >= 8)
  248. {
  249. Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
  250. XD = 0;
  251. }
  252. H_Dot--;
  253. }
  254. // end while
  255. // nothing_to_draw:
  256. _rot_comp.YD++;
  257. // _rot_comp.V_Dot--; // will be done by caller
  258. }
  259. PICO_INTERNAL void gfx_cd_update(void)
  260. {
  261. int V_Dot = _rot_comp.Reg_64 & 0xff;
  262. int jobs;
  263. dprintf("gfx_cd_update, Reg_64 = %04x", _rot_comp.Reg_64);
  264. if (!V_Dot)
  265. {
  266. gfx_completed();
  267. return;
  268. }
  269. jobs = _rot_comp.Float_Part >> 16;
  270. if (!jobs)
  271. {
  272. _rot_comp.Float_Part += _rot_comp.Draw_Speed;
  273. return;
  274. }
  275. _rot_comp.Float_Part &= 0xffff;
  276. _rot_comp.Float_Part += _rot_comp.Draw_Speed;
  277. if (PicoOpt & 0x1000) // scale/rot enabled
  278. {
  279. unsigned int func = _rot_comp.Function;
  280. unsigned int H_Dot = _rot_comp.Reg_62 & 0x1ff;
  281. unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
  282. while (jobs--)
  283. {
  284. gfx_do(func, stamp_base, H_Dot); // jmp [Jmp_Adr]:
  285. V_Dot--; // dec byte [V_Dot]
  286. if (V_Dot == 0)
  287. {
  288. // GFX_Completed:
  289. gfx_completed();
  290. return;
  291. }
  292. }
  293. }
  294. else
  295. {
  296. if (jobs >= V_Dot)
  297. {
  298. gfx_completed();
  299. return;
  300. }
  301. V_Dot -= jobs;
  302. }
  303. _rot_comp.Reg_64 = V_Dot;
  304. }
  305. PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
  306. {
  307. unsigned int d = 0;
  308. switch (a) {
  309. case 0x58: d = _rot_comp.Reg_58; break;
  310. case 0x5A: d = _rot_comp.Reg_5A; break;
  311. case 0x5C: d = _rot_comp.Reg_5C; break;
  312. case 0x5E: d = _rot_comp.Reg_5E; break;
  313. case 0x60: d = _rot_comp.Reg_60; break;
  314. case 0x62: d = _rot_comp.Reg_62; break;
  315. case 0x64: d = _rot_comp.Reg_64; break;
  316. case 0x66: break;
  317. default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
  318. }
  319. dprintf("gfx_cd_read(%02x) = %04x", a, d);
  320. return d;
  321. }
  322. PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
  323. {
  324. dprintf("gfx_cd_write16(%x, %04x)", a, d);
  325. switch (a) {
  326. case 0x58: // .Reg_Stamp_Size
  327. _rot_comp.Reg_58 = d & 7;
  328. return;
  329. case 0x5A: // .Reg_Stamp_Adr
  330. _rot_comp.Reg_5A = d & 0xffe0;
  331. return;
  332. case 0x5C: // .Reg_IM_VCell_Size
  333. _rot_comp.Reg_5C = d & 0x1f;
  334. return;
  335. case 0x5E: // .Reg_IM_Adr
  336. _rot_comp.Reg_5E = d & 0xFFF8;
  337. return;
  338. case 0x60: // .Reg_IM_Offset
  339. _rot_comp.Reg_60 = d & 0x3f;
  340. return;
  341. case 0x62: // .Reg_IM_HDot_Size
  342. _rot_comp.Reg_62 = d & 0x1ff;
  343. return;
  344. case 0x64: // .Reg_IM_VDot_Size
  345. _rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
  346. return;
  347. case 0x66: // .Reg_Vector_Adr
  348. _rot_comp.Reg_66 = d & 0xfffe;
  349. if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
  350. gfx_cd_start();
  351. return;
  352. default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
  353. }
  354. }
  355. PICO_INTERNAL void gfx_cd_reset(void)
  356. {
  357. memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
  358. }
  359. // --------------------------------
  360. #include "cell_map.c"
  361. #ifndef UTYPES_DEFINED
  362. typedef unsigned short u16;
  363. #endif
  364. // check: Heart of the alien, jaguar xj 220
  365. PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
  366. {
  367. unsigned char *base;
  368. unsigned int asrc, a2;
  369. u16 *r;
  370. base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
  371. switch (Pico.video.type)
  372. {
  373. case 1: // vram
  374. r = Pico.vram;
  375. for(; len; len--)
  376. {
  377. asrc = cell_map(source >> 2) << 2;
  378. asrc |= source & 2;
  379. // if(a&1) d=(d<<8)|(d>>8); // ??
  380. r[a>>1] = *(u16 *)(base + asrc);
  381. source += 2;
  382. // AutoIncrement
  383. a=(u16)(a+inc);
  384. }
  385. rendstatus|=0x10;
  386. break;
  387. case 3: // cram
  388. Pico.m.dirtyPal = 1;
  389. r = Pico.cram;
  390. for(a2=a&0x7f; len; len--)
  391. {
  392. asrc = cell_map(source >> 2) << 2;
  393. asrc |= source & 2;
  394. r[a2>>1] = *(u16 *)(base + asrc);
  395. source += 2;
  396. // AutoIncrement
  397. a2+=inc;
  398. // good dest?
  399. if(a2 >= 0x80) break;
  400. }
  401. a=(a&0xff00)|a2;
  402. break;
  403. case 5: // vsram[a&0x003f]=d;
  404. r = Pico.vsram;
  405. for(a2=a&0x7f; len; len--)
  406. {
  407. asrc = cell_map(source >> 2) << 2;
  408. asrc |= source & 2;
  409. r[a2>>1] = *(u16 *)(base + asrc);
  410. source += 2;
  411. // AutoIncrement
  412. a2+=inc;
  413. // good dest?
  414. if(a2 >= 0x80) break;
  415. }
  416. a=(a&0xff00)|a2;
  417. break;
  418. }
  419. // remember addr
  420. Pico.video.addr=(u16)a;
  421. }