446b782fcf1eab41a3fe140e219f90eb27cbaac6
[picodrive.git] / draw.c
1 #include "../pico_int.h"
2
3 int (*PicoScan32xBegin)(unsigned int num);
4 int (*PicoScan32xEnd)(unsigned int num);
5 int Pico32xDrawMode;
6
7 static void convert_pal555(int invert_prio)
8 {
9   unsigned int *ps = (void *)Pico32xMem->pal;
10   unsigned int *pd = (void *)Pico32xMem->pal_native;
11   unsigned int m1 = 0x001f001f;
12   unsigned int m2 = 0x03e003e0;
13   unsigned int m3 = 0xfc00fc00;
14   unsigned int inv = 0;
15   int i;
16
17   if (invert_prio)
18     inv = 0x00200020;
19
20   // place prio to LS green bit
21   for (i = 0x100/2; i > 0; i--, ps++, pd++) {
22     unsigned int t = *ps;
23     *pd = (((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10)) ^ inv;
24   }
25
26   Pico32x.dirty_pal = 0;
27 }
28
29 // direct color mode
30 #define do_line_dc(pd, p32x, pmd, inv, pmd_draw_code)             \
31 {                                                                 \
32   const unsigned int m1 = 0x001f;                                 \
33   const unsigned int m2 = 0x03e0;                                 \
34   const unsigned int m3 = 0x7c00;                                 \
35   int i;                                                          \
36                                                                   \
37   for (i = 320; i > 0; i--, pd++, p32x++, pmd++) {                \
38     unsigned short t = *p32x;                                     \
39     if (*pmd != mdbg && !((t ^ inv) & 0x8000)) {                  \
40       pmd_draw_code;                                              \
41       continue;                                                   \
42     }                                                             \
43                                                                   \
44     *pd = ((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10);  \
45   }                                                               \
46 }
47
48 // packed pixel mode
49 #define do_line_pp(pd, p32x, pmd, pmd_draw_code)                  \
50 {                                                                 \
51   unsigned short t;                                               \
52   int i;                                                          \
53   for (i = 320; i > 0; i--, pd++, p32x++, pmd++) {                \
54     t = pal[*(unsigned char *)((long)p32x ^ 1)];                  \
55     if ((t & 0x20) || *pmd == mdbg)                               \
56       *pd = t;                                                    \
57     else                                                          \
58       pmd_draw_code;                                              \
59   }                                                               \
60
61
62 // run length mode
63 #define do_line_rl(pd, p32x, pmd, pmd_draw_code)                  \
64 {                                                                 \
65   unsigned short len, t;                                          \
66   int i;                                                          \
67   for (i = 320; i > 0; p32x++) {                                  \
68     t = pal[*p32x & 0xff];                                        \
69     for (len = (*p32x >> 8) + 1; len > 0 && i > 0; len--, i--, pd++, pmd++) { \
70       if (*pmd == mdbg || (t & 0x20))                             \
71         *pd = t;                                                  \
72       else                                                        \
73         pmd_draw_code;                                            \
74     }                                                             \
75   }                                                               \
76 }
77
78 void FinalizeLine32xRGB555(int sh, int line)
79 {
80   unsigned short *pd = DrawLineDest;
81   unsigned short *pal = Pico32xMem->pal_native;
82   unsigned char  *pmd = HighCol + 8;
83   unsigned short *dram, *p32x;
84   unsigned char   mdbg;
85
86   FinalizeLine555(sh, line);
87
88   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 0 || // 32x blanking
89       // XXX: how is 32col mode hadled by real hardware?
90       !(Pico.video.reg[12] & 1) || // 32col mode
91       !(PicoDrawMask & PDRAW_32X_ON))
92   {
93     return;
94   }
95
96   dram = (void *)Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
97   p32x = dram + dram[line];
98   mdbg = Pico.video.reg[7] & 0x3f;
99
100   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
101     int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
102     do_line_dc(pd, p32x, pmd, inv_bit,);
103     return;
104   }
105
106   if (Pico32x.dirty_pal)
107     convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
108
109   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1) { // Packed Pixel Mode
110     unsigned char *p32xb = (void *)p32x;
111     if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
112       p32xb++;
113     do_line_pp(pd, p32xb, pmd,);
114   }
115   else { // Run Length Mode
116     do_line_rl(pd, p32x, pmd,);
117   }
118 }
119
120 #define MD_LAYER_CODE \
121   *dst = palmd[*pmd]
122
123 #define PICOSCAN_PRE \
124   PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
125   dst = DrawLineDest; \
126
127 #define PICOSCAN_POST \
128   PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
129
130 #define make_do_loop(name, pre_code, post_code, md_code)        \
131 /* Direct Color Mode */                                         \
132 static void do_loop_dc##name(unsigned short *dst,               \
133     unsigned short *dram, int lines_sft_offs, int mdbg)         \
134 {                                                               \
135   int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0; \
136   unsigned char  *pmd = PicoDraw2FB + 328 * 8 + 8;              \
137   unsigned short *palmd = HighPal;                              \
138   unsigned short *p32x;                                         \
139   int lines = lines_sft_offs >> 16;                             \
140   int l;                                                        \
141   (void)palmd;                                                  \
142   for (l = 0; l < lines; l++, pmd += 8) {                       \
143     pre_code;                                                   \
144     p32x = dram + dram[l];                                      \
145     do_line_dc(dst, p32x, pmd, inv_bit, md_code);               \
146     post_code;                                                  \
147   }                                                             \
148 }                                                               \
149                                                                 \
150 /* Packed Pixel Mode */                                         \
151 static void do_loop_pp##name(unsigned short *dst,               \
152     unsigned short *dram, int lines_sft_offs, int mdbg)         \
153 {                                                               \
154   unsigned short *pal = Pico32xMem->pal_native;                 \
155   unsigned char  *pmd = PicoDraw2FB + 328 * 8 + 8;              \
156   unsigned short *palmd = HighPal;                              \
157   unsigned char  *p32x;                                         \
158   int lines = lines_sft_offs >> 16;                             \
159   int l;                                                        \
160   (void)palmd;                                                  \
161   for (l = 0; l < lines; l++, pmd += 8) {                       \
162     pre_code;                                                   \
163     p32x = (void *)(dram + dram[l]);                            \
164     p32x += (lines_sft_offs >> 8) & 1;                          \
165     do_line_pp(dst, p32x, pmd, md_code);                        \
166     post_code;                                                  \
167   }                                                             \
168 }                                                               \
169                                                                 \
170 /* Run Length Mode */                                           \
171 static void do_loop_rl##name(unsigned short *dst,               \
172     unsigned short *dram, int lines_sft_offs, int mdbg)         \
173 {                                                               \
174   unsigned short *pal = Pico32xMem->pal_native;                 \
175   unsigned char  *pmd = PicoDraw2FB + 328 * 8 + 8;              \
176   unsigned short *palmd = HighPal;                              \
177   unsigned short *p32x;                                         \
178   int lines = lines_sft_offs >> 16;                             \
179   int l;                                                        \
180   (void)palmd;                                                  \
181   for (l = 0; l < lines; l++, pmd += 8) {                       \
182     pre_code;                                                   \
183     p32x = dram + dram[l];                                      \
184     do_line_rl(dst, p32x, pmd, md_code);                        \
185     post_code;                                                  \
186   }                                                             \
187 }
188
189 #ifdef _ASM_32X_DRAW
190 #undef make_do_loop
191 #define make_do_loop(name, pre_code, post_code, md_code) \
192 extern void do_loop_dc##name(unsigned short *dst,        \
193     unsigned short *dram, int lines_offs, int mdbg);     \
194 extern void do_loop_pp##name(unsigned short *dst,        \
195     unsigned short *dram, int lines_offs, int mdbg);     \
196 extern void do_loop_rl##name(unsigned short *dst,        \
197     unsigned short *dram, int lines_offs, int mdbg);
198 #endif
199
200 make_do_loop(,,,)
201 make_do_loop(_md, , , MD_LAYER_CODE)
202 make_do_loop(_scan, PICOSCAN_PRE, PICOSCAN_POST, )
203 make_do_loop(_scan_md, PICOSCAN_PRE, PICOSCAN_POST, MD_LAYER_CODE)
204
205 typedef void (*do_loop_func)(unsigned short *dst, unsigned short *dram, int lines, int mdbg);
206 enum { DO_LOOP, DO_LOOP_MD, DO_LOOP_SCAN, DO_LOOP_MD_SCAN };
207
208 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 };
209 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 };
210 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 };
211
212 void PicoDraw32xLayer(int offs, int lines, int md_bg)
213 {
214   int have_scan = PicoScan32xBegin != NULL && PicoScan32xEnd != NULL;
215   const do_loop_func *do_loop;
216   unsigned short *dram;
217   int lines_sft_offs;
218   int which_func;
219
220   DrawLineDest = DrawLineDestBase + offs * DrawLineDestIncrement;
221   dram = Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
222
223   if (Pico32xDrawMode == 2) {
224     if (Pico.m.dirtyPal)
225       PicoDrawUpdateHighPal();
226   }
227
228   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2)
229   {
230     // Direct Color Mode
231     do_loop = do_loop_dc_f;
232     goto do_it;
233   }
234
235   if (Pico32x.dirty_pal)
236     convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
237
238   if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1)
239   {
240     // Packed Pixel Mode
241     do_loop = do_loop_pp_f;
242   }
243   else
244   {
245     // Run Length Mode
246     do_loop = do_loop_rl_f;
247   }
248
249 do_it:
250   if (Pico32xDrawMode == 2)
251     which_func = have_scan ? DO_LOOP_MD_SCAN : DO_LOOP_MD;
252   else
253     which_func = have_scan ? DO_LOOP_SCAN : DO_LOOP;
254   lines_sft_offs = (lines << 16) | offs;
255   if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
256     lines_sft_offs |= 1 << 8;
257
258   do_loop[which_func](DrawLineDest, dram, lines_sft_offs, md_bg);
259 }
260
261 void PicoDraw32xSetFrameMode(int is_on, int only_32x)
262 {
263 #ifdef _ASM_32X_DRAW
264   extern void *Pico32xNativePal;
265   Pico32xNativePal = Pico32xMem->pal_native;
266 #endif
267
268   if (is_on) {
269     // use the same layout as alt renderer
270     PicoDrawSetInternalBuf(PicoDraw2FB + 328*8, 328);
271     Pico32xDrawMode = only_32x ? 1 : 2;
272   } else {
273     PicoDrawSetInternalBuf(NULL, 0);
274     Pico32xDrawMode = 0;
275   }
276 }
277