update libpicofe
[picodrive.git] / pico / 32x / draw.c
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  */
8 #include "../pico_int.h"
9
10 int (*PicoScan32xBegin)(unsigned int num);
11 int (*PicoScan32xEnd)(unsigned int num);
12 int Pico32xDrawMode;
13
14 static void convert_pal555(int invert_prio)
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;
21   unsigned int inv = 0;
22   int i;
23
24   if (invert_prio)
25     inv = 0x00200020;
26
27   // place prio to LS green bit
28   for (i = 0x100/2; i > 0; i--, ps++, pd++) {
29     unsigned int t = *ps;
30     *pd = (((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10)) ^ inv;
31   }
32
33   Pico32x.dirty_pal = 0;
34 }
35
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;                                     \
46     if ((*pmd & 0x3f) != mdbg && !((t ^ inv) & 0x8000)) {         \
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;                                                          \
60   for (i = 320; i > 0; i--, pd++, p32x++, pmd++) {                \
61     t = pal[*(unsigned char *)((long)p32x ^ 1)];                  \
62     if ((t & 0x20) || (*pmd & 0x3f) == mdbg)                      \
63       *pd = t;                                                    \
64     else                                                          \
65       pmd_draw_code;                                              \
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++) { \
77       if ((*pmd & 0x3f) == mdbg || (t & 0x20))                    \
78         *pd = t;                                                  \
79       else                                                        \
80         pmd_draw_code;                                            \
81     }                                                             \
82   }                                                               \
83 }
84
85 // this is almost never used (Wiz and menu bg gen only)
86 void FinalizeLine32xRGB555(int sh, int line, struct PicoEState *est)
87 {
88   unsigned short *pd = est->DrawLineDest;
89   unsigned short *pal = Pico32xMem->pal_native;
90   unsigned char  *pmd = est->HighCol + 8;
91   unsigned short *dram, *p32x;
92   unsigned char   mdbg;
93
94   FinalizeLine555(sh, line, est);
95
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
99       !(PicoDrawMask & PDRAW_32X_ON))
100   {
101     return;
102   }
103
104   dram = (void *)Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
105   p32x = dram + dram[line];
106   mdbg = Pico.video.reg[7] & 0x3f;
107
108   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
109     int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
110     do_line_dc(pd, p32x, pmd, inv_bit,);
111     return;
112   }
113
114   if (Pico32x.dirty_pal)
115     convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
116
117   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1) { // Packed Pixel Mode
118     unsigned char *p32xb = (void *)p32x;
119     if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
120       p32xb++;
121     do_line_pp(pd, p32xb, pmd,);
122   }
123   else { // Run Length Mode
124     do_line_rl(pd, p32x, pmd,);
125   }
126 }
127
128 #define MD_LAYER_CODE \
129   *dst = palmd[*pmd]
130
131 #define PICOSCAN_PRE \
132   PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
133   dst = Pico.est.DrawLineDest; \
134
135 #define PICOSCAN_POST \
136   PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
137
138 #define make_do_loop(name, pre_code, post_code, md_code)        \
139 /* Direct Color Mode */                                         \
140 static void do_loop_dc##name(unsigned short *dst,               \
141     unsigned short *dram, int lines_sft_offs, int mdbg)         \
142 {                                                               \
143   int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0; \
144   unsigned char  *pmd = Pico.est.Draw2FB +                      \
145                           328 * (lines_sft_offs & 0xff) + 8;    \
146   unsigned short *palmd = Pico.est.HighPal;                     \
147   unsigned short *p32x;                                         \
148   int lines = lines_sft_offs >> 16;                             \
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 */                                         \
160 static void do_loop_pp##name(unsigned short *dst,               \
161     unsigned short *dram, int lines_sft_offs, int mdbg)         \
162 {                                                               \
163   unsigned short *pal = Pico32xMem->pal_native;                 \
164   unsigned char  *pmd = Pico.est.Draw2FB +                      \
165                           328 * (lines_sft_offs & 0xff) + 8;    \
166   unsigned short *palmd = Pico.est.HighPal;                     \
167   unsigned char  *p32x;                                         \
168   int lines = lines_sft_offs >> 16;                             \
169   int l;                                                        \
170   (void)palmd;                                                  \
171   for (l = 0; l < lines; l++, pmd += 8) {                       \
172     pre_code;                                                   \
173     p32x = (void *)(dram + dram[l]);                            \
174     p32x += (lines_sft_offs >> 8) & 1;                          \
175     do_line_pp(dst, p32x, pmd, md_code);                        \
176     post_code;                                                  \
177   }                                                             \
178 }                                                               \
179                                                                 \
180 /* Run Length Mode */                                           \
181 static void do_loop_rl##name(unsigned short *dst,               \
182     unsigned short *dram, int lines_sft_offs, int mdbg)         \
183 {                                                               \
184   unsigned short *pal = Pico32xMem->pal_native;                 \
185   unsigned char  *pmd = Pico.est.Draw2FB +                      \
186                           328 * (lines_sft_offs & 0xff) + 8;    \
187   unsigned short *palmd = Pico.est.HighPal;                     \
188   unsigned short *p32x;                                         \
189   int lines = lines_sft_offs >> 16;                             \
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) \
203 extern void do_loop_dc##name(unsigned short *dst,        \
204     unsigned short *dram, int lines_offs, int mdbg);     \
205 extern void do_loop_pp##name(unsigned short *dst,        \
206     unsigned short *dram, int lines_offs, int mdbg);     \
207 extern void do_loop_rl##name(unsigned short *dst,        \
208     unsigned short *dram, int lines_offs, int mdbg);
209 #endif
210
211 make_do_loop(,,,)
212 make_do_loop(_md, , , MD_LAYER_CODE)
213 make_do_loop(_scan, PICOSCAN_PRE, PICOSCAN_POST, )
214 make_do_loop(_scan_md, PICOSCAN_PRE, PICOSCAN_POST, MD_LAYER_CODE)
215
216 typedef void (*do_loop_func)(unsigned short *dst, unsigned short *dram, int lines, int mdbg);
217 enum { DO_LOOP, DO_LOOP_MD, DO_LOOP_SCAN, DO_LOOP_MD_SCAN };
218
219 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 };
220 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 };
221 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 };
222
223 void 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;
228   int lines_sft_offs;
229   int which_func;
230
231   Pico.est.DrawLineDest = (char *)DrawLineDestBase + offs * DrawLineDestIncrement;
232   dram = Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
233
234   if (Pico32xDrawMode == PDM32X_BOTH) {
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
260 do_it:
261   if (Pico32xDrawMode == PDM32X_BOTH)
262     which_func = have_scan ? DO_LOOP_MD_SCAN : DO_LOOP_MD;
263   else
264     which_func = have_scan ? DO_LOOP_SCAN : DO_LOOP;
265   lines_sft_offs = (lines << 16) | offs;
266   if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
267     lines_sft_offs |= 1 << 8;
268
269   do_loop[which_func](Pico.est.DrawLineDest, dram, lines_sft_offs, md_bg);
270 }
271
272 // mostly unused, games tend to keep 32X layer on
273 void PicoDraw32xLayerMdOnly(int offs, int lines)
274 {
275   int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
276   unsigned short *dst = (void *)((char *)DrawLineDestBase + offs * DrawLineDestIncrement);
277   unsigned char  *pmd = Pico.est.Draw2FB + 328 * offs + 8;
278   unsigned short *pal = Pico.est.HighPal;
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);
295       dst = Pico.est.DrawLineDest + poffs;
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
310 void PicoDrawSetOutFormat32x(pdso_t which, int use_32x_line_mode)
311 {
312 #ifdef _ASM_32X_DRAW
313   extern void *Pico32xNativePal;
314   Pico32xNativePal = Pico32xMem->pal_native;
315 #endif
316
317   if (which == PDF_RGB555 && use_32x_line_mode) {
318     // we'll draw via FinalizeLine32xRGB555 (rare)
319     PicoDrawSetInternalBuf(NULL, 0);
320     Pico32xDrawMode = PDM32X_OFF;
321     return;
322   }
323
324   // use the same layout as alt renderer
325   PicoDrawSetInternalBuf(Pico.est.Draw2FB, 328);
326   Pico32xDrawMode = (which == PDF_RGB555) ? PDM32X_32X_ONLY : PDM32X_BOTH;
327 }
328
329 // vim:shiftwidth=2:ts=2:expandtab