draw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2009,2010
  4. * (C) kub, 2019
  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. // NB: 32X officially doesn't support H32 mode. However, it does work since the
  11. // cartridge slot carries the EDCLK signal which is always H40 clock and is used
  12. // as video clock by the 32X. The H32 MD image is overlayed with the 320 px 32X
  13. // image which has the same on-screen width. How the /YS signal on the cartridge
  14. // slot (signalling the display of background color) is processed in this case
  15. // is however unclear and might lead to glitches due to race conditions by the
  16. // different video clocks for H32 and H40.
  17. // BGR555 to native conversion
  18. #if defined(USE_BGR555)
  19. #define PXCONV(t) ((t)&(mr|mg|mb|mp))
  20. #define PXPRIO 0x8000 // prio in MSB
  21. #elif defined(USE_BGR565)
  22. #define PXCONV(t) (((t)&mr) | (((t)&(mg|mb)) << 1) | (((t)&mp) >> 10))
  23. #define PXPRIO 0x0020 // prio in LS green bit
  24. #else // RGB565
  25. #define PXCONV(t) ((((t)&mr) << 11) | (((t)&mg) << 1) | (((t)&(mp|mb)) >> 10))
  26. #define PXPRIO 0x0020 // prio in LS green bit
  27. #endif
  28. int (*PicoScan32xBegin)(unsigned int num);
  29. int (*PicoScan32xEnd)(unsigned int num);
  30. int Pico32xDrawMode;
  31. void *DrawLineDestBase32x;
  32. int DrawLineDestIncrement32x;
  33. static void convert_pal555(int invert_prio)
  34. {
  35. u32 *ps = (void *)Pico32xMem->pal;
  36. u32 *pd = (void *)Pico32xMem->pal_native;
  37. u32 mr = 0x001f001f; // masks for red, green, blue, prio
  38. u32 mg = 0x03e003e0;
  39. u32 mb = 0x7c007c00;
  40. u32 mp = 0x80008000;
  41. u32 inv = 0;
  42. int i;
  43. if (invert_prio)
  44. inv = 0x80008000;
  45. for (i = 0x100/2; i > 0; i--, ps++, pd++) {
  46. u32 t = *ps ^ inv;
  47. *pd = PXCONV(t);
  48. }
  49. Pico32x.dirty_pal = 0;
  50. }
  51. // direct color mode
  52. #define do_line_dc(pd, p32x, pmd, inv, pmd_draw_code) \
  53. { \
  54. const u16 mr = 0x001f; \
  55. const u16 mg = 0x03e0; \
  56. const u16 mb = 0x7c00; \
  57. const u16 mp = 0x0000; \
  58. unsigned short t; \
  59. int i = 320; \
  60. \
  61. while (i > 0) { \
  62. for (; i > 0 && (*pmd & 0x3f) == mdbg; pd++, pmd++, i--) { \
  63. t = *p32x++; \
  64. *pd = PXCONV(t); \
  65. } \
  66. for (; i > 0 && (*pmd & 0x3f) != mdbg; pd++, pmd++, i--) { \
  67. t = *p32x++ ^ inv; \
  68. if (t & 0x8000) \
  69. *pd = PXCONV(t); \
  70. else \
  71. pmd_draw_code; \
  72. } \
  73. } \
  74. }
  75. // packed pixel mode
  76. #define do_line_pp(pd, p32x, pmd, pmd_draw_code) \
  77. { \
  78. unsigned short t; \
  79. int i = 320; \
  80. while (i > 0) { \
  81. for (; i > 0 && (*pmd & 0x3f) == mdbg; pd++, pmd++, i--) { \
  82. t = pal[*(unsigned char *)(MEM_BE2((uintptr_t)(p32x++)))]; \
  83. *pd = t; \
  84. } \
  85. for (; i > 0 && (*pmd & 0x3f) != mdbg; pd++, pmd++, i--) { \
  86. t = pal[*(unsigned char *)(MEM_BE2((uintptr_t)(p32x++)))]; \
  87. if (t & PXPRIO) \
  88. *pd = t; \
  89. else \
  90. pmd_draw_code; \
  91. } \
  92. } \
  93. }
  94. // run length mode
  95. #define do_line_rl(pd, p32x, pmd, pmd_draw_code) \
  96. { \
  97. unsigned short len, t; \
  98. int i; \
  99. for (i = 320; i > 0; p32x++) { \
  100. t = pal[*p32x & 0xff]; \
  101. for (len = (*p32x >> 8) + 1; len > 0 && i > 0; len--, i--, pd++, pmd++) { \
  102. if ((*pmd & 0x3f) == mdbg || (t & PXPRIO)) \
  103. *pd = t; \
  104. else \
  105. pmd_draw_code; \
  106. } \
  107. } \
  108. }
  109. // this is almost never used (Wiz and menu bg gen only)
  110. void FinalizeLine32xRGB555(int sh, int line, struct PicoEState *est)
  111. {
  112. unsigned short *pd = est->DrawLineDest;
  113. unsigned short *pal = Pico32xMem->pal_native;
  114. unsigned char *pmd = est->HighCol + 8;
  115. unsigned short *dram, *p32x;
  116. unsigned char mdbg;
  117. FinalizeLine555(sh, line, est);
  118. if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 0 || // 32x blanking
  119. (Pico.video.debug_p & PVD_KILL_32X))
  120. {
  121. return;
  122. }
  123. dram = (void *)Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
  124. p32x = dram + dram[line];
  125. mdbg = Pico.video.reg[7] & 0x3f;
  126. if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
  127. int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
  128. do_line_dc(pd, p32x, pmd, inv_bit,);
  129. return;
  130. }
  131. if (Pico32x.dirty_pal)
  132. convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
  133. if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1) { // Packed Pixel Mode
  134. unsigned char *p32xb = (void *)p32x;
  135. if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
  136. p32xb++;
  137. do_line_pp(pd, p32xb, pmd,);
  138. }
  139. else { // Run Length Mode
  140. do_line_rl(pd, p32x, pmd,);
  141. }
  142. }
  143. #define MD_LAYER_CODE \
  144. *dst = palmd[*pmd]
  145. #define PICOSCAN_PRE \
  146. PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
  147. dst = Pico.est.DrawLineDest; \
  148. #define PICOSCAN_POST \
  149. PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
  150. Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement32x; \
  151. #define make_do_loop(name, pre_code, post_code, md_code) \
  152. /* Direct Color Mode */ \
  153. static void do_loop_dc##name(unsigned short *dst, \
  154. unsigned short *dram, int lines_sft_offs, int mdbg) \
  155. { \
  156. int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0; \
  157. unsigned char *pmd = Pico.est.Draw2FB + \
  158. 328 * (lines_sft_offs & 0xff) + 8; \
  159. unsigned short *palmd = Pico.est.HighPal; \
  160. unsigned short *p32x; \
  161. int lines = lines_sft_offs >> 16; \
  162. int l; \
  163. (void)palmd; \
  164. for (l = 0; l < lines; l++, pmd += 8) { \
  165. pre_code; \
  166. p32x = dram + dram[l]; \
  167. do_line_dc(dst, p32x, pmd, inv_bit, md_code); \
  168. post_code; \
  169. dst += DrawLineDestIncrement32x/2 - 320; \
  170. } \
  171. } \
  172. \
  173. /* Packed Pixel Mode */ \
  174. static void do_loop_pp##name(unsigned short *dst, \
  175. unsigned short *dram, int lines_sft_offs, int mdbg) \
  176. { \
  177. unsigned short *pal = Pico32xMem->pal_native; \
  178. unsigned char *pmd = Pico.est.Draw2FB + \
  179. 328 * (lines_sft_offs & 0xff) + 8; \
  180. unsigned short *palmd = Pico.est.HighPal; \
  181. unsigned char *p32x; \
  182. int lines = lines_sft_offs >> 16; \
  183. int l; \
  184. (void)palmd; \
  185. for (l = 0; l < lines; l++, pmd += 8) { \
  186. pre_code; \
  187. p32x = (void *)(dram + dram[l]); \
  188. p32x += (lines_sft_offs >> 8) & 1; \
  189. do_line_pp(dst, p32x, pmd, md_code); \
  190. post_code; \
  191. dst += DrawLineDestIncrement32x/2 - 320; \
  192. } \
  193. } \
  194. \
  195. /* Run Length Mode */ \
  196. static void do_loop_rl##name(unsigned short *dst, \
  197. unsigned short *dram, int lines_sft_offs, int mdbg) \
  198. { \
  199. unsigned short *pal = Pico32xMem->pal_native; \
  200. unsigned char *pmd = Pico.est.Draw2FB + \
  201. 328 * (lines_sft_offs & 0xff) + 8; \
  202. unsigned short *palmd = Pico.est.HighPal; \
  203. unsigned short *p32x; \
  204. int lines = lines_sft_offs >> 16; \
  205. int l; \
  206. (void)palmd; \
  207. for (l = 0; l < lines; l++, pmd += 8) { \
  208. pre_code; \
  209. p32x = dram + dram[l]; \
  210. do_line_rl(dst, p32x, pmd, md_code); \
  211. post_code; \
  212. dst += DrawLineDestIncrement32x/2 - 320; \
  213. } \
  214. }
  215. #ifdef _ASM_32X_DRAW
  216. #undef make_do_loop
  217. #define make_do_loop(name, pre_code, post_code, md_code) \
  218. extern void do_loop_dc##name(unsigned short *dst, \
  219. unsigned short *dram, int lines_offs, int mdbg); \
  220. extern void do_loop_pp##name(unsigned short *dst, \
  221. unsigned short *dram, int lines_offs, int mdbg); \
  222. extern void do_loop_rl##name(unsigned short *dst, \
  223. unsigned short *dram, int lines_offs, int mdbg);
  224. #endif
  225. make_do_loop(,,,)
  226. make_do_loop(_md, , , MD_LAYER_CODE)
  227. make_do_loop(_scan, PICOSCAN_PRE, PICOSCAN_POST, )
  228. make_do_loop(_scan_md, PICOSCAN_PRE, PICOSCAN_POST, MD_LAYER_CODE)
  229. typedef void (*do_loop_func)(unsigned short *dst, unsigned short *dram, int lines, int mdbg);
  230. enum { DO_LOOP, DO_LOOP_MD, DO_LOOP_SCAN, DO_LOOP_MD_SCAN };
  231. static const do_loop_func do_loop_dc_f[] = { do_loop_dc, do_loop_dc_md, do_loop_dc_scan, do_loop_dc_scan_md };
  232. static const do_loop_func do_loop_pp_f[] = { do_loop_pp, do_loop_pp_md, do_loop_pp_scan, do_loop_pp_scan_md };
  233. static const do_loop_func do_loop_rl_f[] = { do_loop_rl, do_loop_rl_md, do_loop_rl_scan, do_loop_rl_scan_md };
  234. void PicoDraw32xLayer(int offs, int lines, int md_bg)
  235. {
  236. int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
  237. const do_loop_func *do_loop;
  238. unsigned short *dram;
  239. int lines_sft_offs;
  240. int which_func;
  241. Pico.est.DrawLineDest = (char *)DrawLineDestBase32x + offs * DrawLineDestIncrement32x;
  242. Pico.est.DrawLineDestIncr = DrawLineDestIncrement32x;
  243. dram = Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
  244. if (Pico32xDrawMode == PDM32X_BOTH)
  245. PicoDrawUpdateHighPal();
  246. if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2)
  247. {
  248. // Direct Color Mode
  249. do_loop = do_loop_dc_f;
  250. goto do_it;
  251. }
  252. if (Pico32x.dirty_pal)
  253. convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
  254. if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1)
  255. {
  256. // Packed Pixel Mode
  257. do_loop = do_loop_pp_f;
  258. }
  259. else
  260. {
  261. // Run Length Mode
  262. do_loop = do_loop_rl_f;
  263. }
  264. do_it:
  265. if (Pico32xDrawMode == PDM32X_BOTH)
  266. which_func = have_scan ? DO_LOOP_MD_SCAN : DO_LOOP_MD;
  267. else
  268. which_func = have_scan ? DO_LOOP_SCAN : DO_LOOP;
  269. lines_sft_offs = (lines << 16) | offs;
  270. if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
  271. lines_sft_offs |= 1 << 8;
  272. do_loop[which_func](Pico.est.DrawLineDest, dram, lines_sft_offs, md_bg);
  273. }
  274. // mostly unused, games tend to keep 32X layer on
  275. void PicoDraw32xLayerMdOnly(int offs, int lines)
  276. {
  277. int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
  278. unsigned short *dst = (void *)((char *)DrawLineDestBase32x + offs * DrawLineDestIncrement32x);
  279. unsigned char *pmd = Pico.est.Draw2FB + 328 * offs + 8;
  280. unsigned short *pal = Pico.est.HighPal;
  281. int poffs = 0, plen = 320;
  282. int l, p;
  283. PicoDrawUpdateHighPal();
  284. dst += poffs;
  285. for (l = 0; l < lines; l++) {
  286. if (have_scan) {
  287. PicoScan32xBegin(l + offs);
  288. dst = (unsigned short *)Pico.est.DrawLineDest + poffs;
  289. }
  290. for (p = 0; p < plen; p += 4) {
  291. dst[p + 0] = pal[*pmd++];
  292. dst[p + 1] = pal[*pmd++];
  293. dst[p + 2] = pal[*pmd++];
  294. dst[p + 3] = pal[*pmd++];
  295. }
  296. dst = Pico.est.DrawLineDest = (char *)dst + DrawLineDestIncrement32x;
  297. pmd += 328 - plen;
  298. if (have_scan)
  299. PicoScan32xEnd(l + offs);
  300. }
  301. }
  302. void PicoDrawSetOutFormat32x(pdso_t which, int use_32x_line_mode)
  303. {
  304. if (which == PDF_RGB555) {
  305. // CLUT pixels needed as well, for layer priority
  306. PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328);
  307. PicoDrawSetOutBufMD(NULL, 0);
  308. } else {
  309. // store CLUT pixels, same layout as alt renderer
  310. PicoDrawSetInternalBuf(NULL, 0);
  311. PicoDrawSetOutBufMD(Pico.est.Draw2FB, 328);
  312. }
  313. // always need upscaling for H32, before mixing in 32X layer
  314. PicoIn.opt |= POPT_EN_SOFTSCALE;
  315. if (use_32x_line_mode)
  316. // we'll draw via FinalizeLine32xRGB555 (rare)
  317. Pico32xDrawMode = PDM32X_OFF;
  318. else
  319. // in RGB555 mode the 32x layer is drawn over the MD layer, in the other
  320. // modes 32x and MD layer are merged together by the 32x renderer
  321. Pico32xDrawMode = (which == PDF_RGB555) ? PDM32X_32X_ONLY : PDM32X_BOTH;
  322. }
  323. void PicoDrawSetOutBuf32X(void *dest, int increment)
  324. {
  325. DrawLineDestBase32x = dest;
  326. DrawLineDestIncrement32x = increment;
  327. // in RGB555 mode this buffer is also used by the MD renderer
  328. if (Pico32xDrawMode != PDM32X_BOTH)
  329. PicoDrawSetOutBufMD(DrawLineDestBase32x, DrawLineDestIncrement32x);
  330. }
  331. // vim:shiftwidth=2:ts=2:expandtab