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