4 * (C) irixxxx, 2019-2024
6 * This work is licensed under the terms of MAME license.
7 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
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.
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
29 #define PXCONV(t) ((((t)&mr) << 11) | (((t)&mg) << 1) | (((t)&(mp|mb)) >> 10))
30 #define PXPRIO 0x0020 // prio in LS green bit
33 int (*PicoScan32xBegin)(unsigned int num);
34 int (*PicoScan32xEnd)(unsigned int num);
37 void *DrawLineDestBase32x;
38 int DrawLineDestIncrement32x;
40 static void convert_pal555(int invert_prio)
42 u32 *ps = (void *)Pico32xMem->pal;
43 u32 *pd = (void *)Pico32xMem->pal_native;
44 u32 mr = 0x001f001f; // masks for red, green, blue, prio
54 for (i = 0x100/2; i > 0; i--, ps++, pd++) {
59 Pico32x.dirty_pal = 0;
63 #define do_line_dc(pd, p32x, pmd, inv, pmd_draw_code) \
65 const u16 mr = 0x001f; \
66 const u16 mg = 0x03e0; \
67 const u16 mb = 0x7c00; \
68 const u16 mp = 0x0000; \
73 for (; i > 0 && (*pmd & 0x3f) == mdbg; pd++, pmd++, i--) { \
77 for (; i > 0 && (*pmd & 0x3f) != mdbg; pd++, pmd++, i--) { \
88 #define do_line_pp(pd, p32x, pmd, pmd_draw_code) \
93 for (; i > 0 && (*pmd & 0x3f) == mdbg; pd++, pmd++, i--) { \
94 t = pal[*(unsigned char *)(MEM_BE2((uintptr_t)(p32x++)))]; \
97 for (; i > 0 && (*pmd & 0x3f) != mdbg; pd++, pmd++, i--) { \
98 t = pal[*(unsigned char *)(MEM_BE2((uintptr_t)(p32x++)))]; \
108 #define do_line_rl(pd, p32x, pmd, pmd_draw_code) \
110 unsigned short len, t; \
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)) \
123 #define MD_LAYER_CODE_H32 \
124 *dst = dst[H32_OFFSET]
126 // this is almost never used (Wiz and menu bg gen only)
127 void FinalizeLine32xRGB555(int sh, int line, struct PicoEState *est)
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;
135 int h32 = !(Pico.video.reg[12] & 0x1);
137 FinalizeLine555(sh, line, est);
139 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 0 || // 32x blanking
140 (Pico.video.debug_p & PVD_KILL_32X))
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;
150 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
151 int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
153 do_line_dc(dst, p32x, pmd, inv_bit, MD_LAYER_CODE_H32);
155 do_line_dc(dst, p32x, pmd, inv_bit,);
159 if (Pico32x.dirty_pal)
160 convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
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)
167 do_line_pp(dst, p32xb, pmd, MD_LAYER_CODE_H32);
169 do_line_pp(dst, p32xb, pmd,);
171 else { // Run Length Mode
173 do_line_rl(dst, p32x, pmd, MD_LAYER_CODE_H32);
175 do_line_rl(dst, p32x, pmd,);
179 #define MD_LAYER_CODE \
182 #define PICOSCAN_PRE \
183 PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
184 dst = Pico.est.DrawLineDest; \
186 #define PICOSCAN_POST \
187 PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
188 Pico.est.DrawLineDest = (char *)Pico.est.DrawLineDest + DrawLineDestIncrement32x; \
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) \
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; \
202 if (lines_sft_offs & (2<<8)) pmd += H32_OFFSET; \
204 for (l = 0; l < lines; l++, pmd += 8) { \
206 p32x = dram + dram[l + (lines_sft_offs >> 24)]; \
207 do_line_dc(dst, p32x, pmd, inv_bit, md_code); \
209 dst += DrawLineDestIncrement32x/2 - 320; \
213 /* Packed Pixel Mode */ \
214 static void do_loop_pp##name(unsigned short *dst, \
215 unsigned short *dram, unsigned lines_sft_offs, int mdbg) \
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; \
224 if (lines_sft_offs & (2<<8)) pmd += H32_OFFSET; \
226 for (l = 0; l < lines; l++, pmd += 8) { \
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); \
232 dst += DrawLineDestIncrement32x/2 - 320; \
236 /* Run Length Mode */ \
237 static void do_loop_rl##name(unsigned short *dst, \
238 unsigned short *dram, unsigned lines_sft_offs, int mdbg) \
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; \
247 if (lines_sft_offs & (2<<8)) pmd += H32_OFFSET; \
249 for (l = 0; l < lines; l++, pmd += 8) { \
251 p32x = dram + dram[l + (lines_sft_offs >> 24)]; \
252 do_line_rl(dst, p32x, pmd, md_code); \
254 dst += DrawLineDestIncrement32x/2 - 320; \
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);
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)
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 };
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 };
283 void PicoDraw32xLayer(int offs, int lines, int md_bg)
285 int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
286 const do_loop_func *do_loop;
287 unsigned short *dram;
291 offs += Pico32x.sync_line;
293 Pico.est.DrawLineDest = (char *)DrawLineDestBase32x + offs * DrawLineDestIncrement32x;
294 Pico.est.DrawLineDestIncr = DrawLineDestIncrement32x;
295 dram = Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
297 if (Pico32xDrawMode == PDM32X_BOTH)
298 PicoDrawUpdateHighPal();
300 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2)
303 do_loop = do_loop_dc_f;
307 if (Pico32x.dirty_pal)
308 convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
310 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1)
313 do_loop = do_loop_pp_f;
318 do_loop = do_loop_rl_f;
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;
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;
337 do_loop[which_func](Pico.est.DrawLineDest, dram, lines_sft_offs, md_bg);
340 // mostly unused, games tend to keep 32X layer on
341 void PicoDraw32xLayerMdOnly(int offs, int lines)
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;
350 PicoDrawUpdateHighPal();
352 offs += Pico32x.sync_line;
353 dst += Pico32x.sync_line * DrawLineDestIncrement32x;
355 for (l = 0; l < lines; l++) {
357 PicoScan32xBegin(l + offs);
358 dst = (unsigned short *)Pico.est.DrawLineDest;
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++];
366 dst = Pico.est.DrawLineDest = (char *)dst + DrawLineDestIncrement32x;
369 PicoScan32xEnd(l + offs);
373 void PicoDrawSetOutFormat32x(pdso_t which, int use_32x_line_mode)
375 if (which == PDF_RGB555) {
376 // CLUT pixels needed as well, for layer priority
377 PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328);
378 PicoDrawSetOutBufMD(NULL, 0);
380 // store CLUT pixels, same layout as alt renderer
381 PicoDrawSetInternalBuf(NULL, 0);
382 PicoDrawSetOutBufMD(Pico.est.Draw2FB, 328);
385 if (use_32x_line_mode)
386 // we'll draw via FinalizeLine32xRGB555 (rare)
387 Pico32xDrawMode = PDM32X_OFF;
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;
394 void PicoDrawSetOutBuf32X(void *dest, int increment)
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);
403 // vim:shiftwidth=2:ts=2:expandtab