fame: fix roxr
[picodrive.git] / pico / 32x / draw.c
CommitLineData
cff531af 1/*
2 * PicoDrive
3 * (C) notaz, 2009,2010
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
974fdb5b 8#include "../pico_int.h"
9
5a681086 10int (*PicoScan32xBegin)(unsigned int num);
11int (*PicoScan32xEnd)(unsigned int num);
12int Pico32xDrawMode;
13
5e49c3a8 14static void convert_pal555(int invert_prio)
974fdb5b 15{
16 unsigned int *ps = (void *)Pico32xMem->pal;
17 unsigned int *pd = (void *)Pico32xMem->pal_native;
18 unsigned int m1 = 0x001f001f;
19 unsigned int m2 = 0x03e003e0;
20 unsigned int m3 = 0xfc00fc00;
5e49c3a8 21 unsigned int inv = 0;
974fdb5b 22 int i;
23
5e49c3a8 24 if (invert_prio)
25 inv = 0x00200020;
26
974fdb5b 27 // place prio to LS green bit
28 for (i = 0x100/2; i > 0; i--, ps++, pd++) {
29 unsigned int t = *ps;
5e49c3a8 30 *pd = (((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10)) ^ inv;
974fdb5b 31 }
32
33 Pico32x.dirty_pal = 0;
34}
35
5a681086 36// direct color mode
37#define do_line_dc(pd, p32x, pmd, inv, pmd_draw_code) \
38{ \
39 const unsigned int m1 = 0x001f; \
40 const unsigned int m2 = 0x03e0; \
41 const unsigned int m3 = 0x7c00; \
42 int i; \
43 \
44 for (i = 320; i > 0; i--, pd++, p32x++, pmd++) { \
45 unsigned short t = *p32x; \
6b5feeba 46 if ((*pmd & 0x3f) != mdbg && !((t ^ inv) & 0x8000)) { \
5a681086 47 pmd_draw_code; \
48 continue; \
49 } \
50 \
51 *pd = ((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10); \
52 } \
53}
54
55// packed pixel mode
56#define do_line_pp(pd, p32x, pmd, pmd_draw_code) \
57{ \
58 unsigned short t; \
59 int i; \
e51e5983 60 for (i = 320; i > 0; i--, pd++, p32x++, pmd++) { \
61 t = pal[*(unsigned char *)((long)p32x ^ 1)]; \
6b5feeba 62 if ((t & 0x20) || (*pmd & 0x3f) == mdbg) \
5a681086 63 *pd = t; \
64 else \
65 pmd_draw_code; \
5a681086 66 } \
67}
68
69// run length mode
70#define do_line_rl(pd, p32x, pmd, pmd_draw_code) \
71{ \
72 unsigned short len, t; \
73 int i; \
74 for (i = 320; i > 0; p32x++) { \
75 t = pal[*p32x & 0xff]; \
76 for (len = (*p32x >> 8) + 1; len > 0 && i > 0; len--, i--, pd++, pmd++) { \
6b5feeba 77 if ((*pmd & 0x3f) == mdbg || (t & 0x20)) \
5a681086 78 *pd = t; \
79 else \
80 pmd_draw_code; \
81 } \
82 } \
83}
84
41946d70 85// this is almost never used (Wiz and menu bg gen only)
ea38612f 86void FinalizeLine32xRGB555(int sh, int line, struct PicoEState *est)
974fdb5b 87{
99bdfd31 88 unsigned short *pd = est->DrawLineDest;
974fdb5b 89 unsigned short *pal = Pico32xMem->pal_native;
99bdfd31 90 unsigned char *pmd = est->HighCol + 8;
5a681086 91 unsigned short *dram, *p32x;
92 unsigned char mdbg;
974fdb5b 93
ea38612f 94 FinalizeLine555(sh, line, est);
c987bb5c 95
5a681086 96 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 0 || // 32x blanking
97 // XXX: how is 32col mode hadled by real hardware?
98 !(Pico.video.reg[12] & 1) || // 32col mode
e0bcb7a9 99 (Pico.video.debug_p & PVD_KILL_32X))
5a681086 100 {
5e49c3a8 101 return;
5a681086 102 }
5e49c3a8 103
266c6afa 104 dram = (void *)Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
5a681086 105 p32x = dram + dram[line];
106 mdbg = Pico.video.reg[7] & 0x3f;
266c6afa 107
108 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
5a681086 109 int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
110 do_line_dc(pd, p32x, pmd, inv_bit,);
266c6afa 111 return;
112 }
113
974fdb5b 114 if (Pico32x.dirty_pal)
5e49c3a8 115 convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
974fdb5b 116
266c6afa 117 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1) { // Packed Pixel Mode
e51e5983 118 unsigned char *p32xb = (void *)p32x;
119 if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
120 p32xb++;
121 do_line_pp(pd, p32xb, pmd,);
974fdb5b 122 }
266c6afa 123 else { // Run Length Mode
5a681086 124 do_line_rl(pd, p32x, pmd,);
125 }
126}
127
128#define MD_LAYER_CODE \
129 *dst = palmd[*pmd]
130
131#define PICOSCAN_PRE \
e51e5983 132 PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
99bdfd31 133 dst = Pico.est.DrawLineDest; \
5a681086 134
135#define PICOSCAN_POST \
e51e5983 136 PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
5a681086 137
138#define make_do_loop(name, pre_code, post_code, md_code) \
139/* Direct Color Mode */ \
140static void do_loop_dc##name(unsigned short *dst, \
e51e5983 141 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 142{ \
143 int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0; \
98a27142 144 unsigned char *pmd = Pico.est.Draw2FB + \
7a961c19 145 328 * (lines_sft_offs & 0xff) + 8; \
98a27142 146 unsigned short *palmd = Pico.est.HighPal; \
5a681086 147 unsigned short *p32x; \
e51e5983 148 int lines = lines_sft_offs >> 16; \
5a681086 149 int l; \
150 (void)palmd; \
151 for (l = 0; l < lines; l++, pmd += 8) { \
152 pre_code; \
153 p32x = dram + dram[l]; \
154 do_line_dc(dst, p32x, pmd, inv_bit, md_code); \
155 post_code; \
156 } \
157} \
158 \
159/* Packed Pixel Mode */ \
160static void do_loop_pp##name(unsigned short *dst, \
e51e5983 161 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 162{ \
163 unsigned short *pal = Pico32xMem->pal_native; \
98a27142 164 unsigned char *pmd = Pico.est.Draw2FB + \
7a961c19 165 328 * (lines_sft_offs & 0xff) + 8; \
98a27142 166 unsigned short *palmd = Pico.est.HighPal; \
e51e5983 167 unsigned char *p32x; \
168 int lines = lines_sft_offs >> 16; \
5a681086 169 int l; \
170 (void)palmd; \
171 for (l = 0; l < lines; l++, pmd += 8) { \
172 pre_code; \
e51e5983 173 p32x = (void *)(dram + dram[l]); \
174 p32x += (lines_sft_offs >> 8) & 1; \
5a681086 175 do_line_pp(dst, p32x, pmd, md_code); \
176 post_code; \
177 } \
178} \
179 \
180/* Run Length Mode */ \
181static void do_loop_rl##name(unsigned short *dst, \
e51e5983 182 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 183{ \
184 unsigned short *pal = Pico32xMem->pal_native; \
98a27142 185 unsigned char *pmd = Pico.est.Draw2FB + \
7a961c19 186 328 * (lines_sft_offs & 0xff) + 8; \
98a27142 187 unsigned short *palmd = Pico.est.HighPal; \
5a681086 188 unsigned short *p32x; \
e51e5983 189 int lines = lines_sft_offs >> 16; \
5a681086 190 int l; \
191 (void)palmd; \
192 for (l = 0; l < lines; l++, pmd += 8) { \
193 pre_code; \
194 p32x = dram + dram[l]; \
195 do_line_rl(dst, p32x, pmd, md_code); \
196 post_code; \
197 } \
198}
199
200#ifdef _ASM_32X_DRAW
201#undef make_do_loop
202#define make_do_loop(name, pre_code, post_code, md_code) \
203extern void do_loop_dc##name(unsigned short *dst, \
204 unsigned short *dram, int lines_offs, int mdbg); \
205extern void do_loop_pp##name(unsigned short *dst, \
206 unsigned short *dram, int lines_offs, int mdbg); \
207extern void do_loop_rl##name(unsigned short *dst, \
208 unsigned short *dram, int lines_offs, int mdbg);
209#endif
210
211make_do_loop(,,,)
212make_do_loop(_md, , , MD_LAYER_CODE)
213make_do_loop(_scan, PICOSCAN_PRE, PICOSCAN_POST, )
214make_do_loop(_scan_md, PICOSCAN_PRE, PICOSCAN_POST, MD_LAYER_CODE)
215
216typedef void (*do_loop_func)(unsigned short *dst, unsigned short *dram, int lines, int mdbg);
217enum { DO_LOOP, DO_LOOP_MD, DO_LOOP_SCAN, DO_LOOP_MD_SCAN };
218
219static const do_loop_func do_loop_dc_f[] = { do_loop_dc, do_loop_dc_md, do_loop_dc_scan, do_loop_dc_scan_md };
220static const do_loop_func do_loop_pp_f[] = { do_loop_pp, do_loop_pp_md, do_loop_pp_scan, do_loop_pp_scan_md };
221static const do_loop_func do_loop_rl_f[] = { do_loop_rl, do_loop_rl_md, do_loop_rl_scan, do_loop_rl_scan_md };
222
223void PicoDraw32xLayer(int offs, int lines, int md_bg)
224{
225 int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
226 const do_loop_func *do_loop;
227 unsigned short *dram;
e51e5983 228 int lines_sft_offs;
5a681086 229 int which_func;
230
99bdfd31 231 Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
5a681086 232 dram = Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
233
7a961c19 234 if (Pico32xDrawMode == PDM32X_BOTH) {
5a681086 235 if (Pico.m.dirtyPal)
236 PicoDrawUpdateHighPal();
237 }
238
239 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2)
240 {
241 // Direct Color Mode
242 do_loop = do_loop_dc_f;
243 goto do_it;
244 }
245
246 if (Pico32x.dirty_pal)
247 convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
248
249 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1)
250 {
251 // Packed Pixel Mode
252 do_loop = do_loop_pp_f;
253 }
254 else
255 {
256 // Run Length Mode
257 do_loop = do_loop_rl_f;
258 }
259
260do_it:
7a961c19 261 if (Pico32xDrawMode == PDM32X_BOTH)
5a681086 262 which_func = have_scan ? DO_LOOP_MD_SCAN : DO_LOOP_MD;
263 else
264 which_func = have_scan ? DO_LOOP_SCAN : DO_LOOP;
e51e5983 265 lines_sft_offs = (lines << 16) | offs;
266 if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
267 lines_sft_offs |= 1 << 8;
5a681086 268
99bdfd31 269 do_loop[which_func](Pico.est.DrawLineDest, dram, lines_sft_offs, md_bg);
5a681086 270}
271
7a961c19 272// mostly unused, games tend to keep 32X layer on
273void PicoDraw32xLayerMdOnly(int offs, int lines)
274{
275 int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
276 unsigned short *dst = (void *)((char *)DrawLineDestBase + offs * DrawLineDestIncrement);
98a27142 277 unsigned char *pmd = Pico.est.Draw2FB + 328 * offs + 8;
278 unsigned short *pal = Pico.est.HighPal;
7a961c19 279 int poffs = 0, plen = 320;
280 int l, p;
281
282 if (!(Pico.video.reg[12] & 1)) {
283 // 32col mode
284 poffs = 32;
285 plen = 256;
286 }
287
288 if (Pico.m.dirtyPal)
289 PicoDrawUpdateHighPal();
290
291 dst += poffs;
292 for (l = 0; l < lines; l++) {
293 if (have_scan) {
294 PicoScan32xBegin(l + offs);
99bdfd31 295 dst = Pico.est.DrawLineDest + poffs;
7a961c19 296 }
297 for (p = 0; p < plen; p += 4) {
298 dst[p + 0] = pal[*pmd++];
299 dst[p + 1] = pal[*pmd++];
300 dst[p + 2] = pal[*pmd++];
301 dst[p + 3] = pal[*pmd++];
302 }
303 dst = (void *)((char *)dst + DrawLineDestIncrement);
304 pmd += 328 - plen;
305 if (have_scan)
306 PicoScan32xEnd(l + offs);
307 }
308}
309
41946d70 310void PicoDrawSetOutFormat32x(pdso_t which, int use_32x_line_mode)
5a681086 311{
312#ifdef _ASM_32X_DRAW
313 extern void *Pico32xNativePal;
314 Pico32xNativePal = Pico32xMem->pal_native;
315#endif
316
41946d70 317 if (which == PDF_RGB555 && use_32x_line_mode) {
318 // we'll draw via FinalizeLine32xRGB555 (rare)
5a681086 319 PicoDrawSetInternalBuf(NULL, 0);
7a961c19 320 Pico32xDrawMode = PDM32X_OFF;
41946d70 321 return;
266c6afa 322 }
41946d70 323
324 // use the same layout as alt renderer
98a27142 325 PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328);
41946d70 326 Pico32xDrawMode = (which == PDF_RGB555) ? PDM32X_32X_ONLY : PDM32X_BOTH;
974fdb5b 327}
328
41946d70 329// vim:shiftwidth=2:ts=2:expandtab