32x: final renderer tweaks; PWM disable kills PWM irqs
[picodrive.git] / pico / 32x / draw.c
CommitLineData
974fdb5b 1#include "../pico_int.h"
2
5a681086 3int (*PicoScan32xBegin)(unsigned int num);
4int (*PicoScan32xEnd)(unsigned int num);
5int Pico32xDrawMode;
6
5e49c3a8 7static void convert_pal555(int invert_prio)
974fdb5b 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;
5e49c3a8 14 unsigned int inv = 0;
974fdb5b 15 int i;
16
5e49c3a8 17 if (invert_prio)
18 inv = 0x00200020;
19
974fdb5b 20 // place prio to LS green bit
21 for (i = 0x100/2; i > 0; i--, ps++, pd++) {
22 unsigned int t = *ps;
5e49c3a8 23 *pd = (((t & m1) << 11) | ((t & m2) << 1) | ((t & m3) >> 10)) ^ inv;
974fdb5b 24 }
25
26 Pico32x.dirty_pal = 0;
27}
28
5a681086 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; \
e51e5983 53 for (i = 320; i > 0; i--, pd++, p32x++, pmd++) { \
54 t = pal[*(unsigned char *)((long)p32x ^ 1)]; \
07e5dbab 55 if ((t & 0x20) || *pmd == mdbg) \
5a681086 56 *pd = t; \
57 else \
58 pmd_draw_code; \
5a681086 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
974fdb5b 78void FinalizeLine32xRGB555(int sh, int line)
79{
80 unsigned short *pd = DrawLineDest;
81 unsigned short *pal = Pico32xMem->pal_native;
5a681086 82 unsigned char *pmd = HighCol + 8;
83 unsigned short *dram, *p32x;
84 unsigned char mdbg;
974fdb5b 85
5a681086 86 FinalizeLine555(sh, line);
c987bb5c 87
5a681086 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 {
5e49c3a8 93 return;
5a681086 94 }
5e49c3a8 95
266c6afa 96 dram = (void *)Pico32xMem->dram[Pico32x.vdp_regs[0x0a/2] & P32XV_FS];
5a681086 97 p32x = dram + dram[line];
98 mdbg = Pico.video.reg[7] & 0x3f;
266c6afa 99
100 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 2) { // Direct Color Mode
5a681086 101 int inv_bit = (Pico32x.vdp_regs[0] & P32XV_PRI) ? 0x8000 : 0;
102 do_line_dc(pd, p32x, pmd, inv_bit,);
266c6afa 103 return;
104 }
105
974fdb5b 106 if (Pico32x.dirty_pal)
5e49c3a8 107 convert_pal555(Pico32x.vdp_regs[0] & P32XV_PRI);
974fdb5b 108
266c6afa 109 if ((Pico32x.vdp_regs[0] & P32XV_Mx) == 1) { // Packed Pixel Mode
e51e5983 110 unsigned char *p32xb = (void *)p32x;
111 if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
112 p32xb++;
113 do_line_pp(pd, p32xb, pmd,);
974fdb5b 114 }
266c6afa 115 else { // Run Length Mode
5a681086 116 do_line_rl(pd, p32x, pmd,);
117 }
118}
119
120#define MD_LAYER_CODE \
121 *dst = palmd[*pmd]
122
123#define PICOSCAN_PRE \
e51e5983 124 PicoScan32xBegin(l + (lines_sft_offs & 0xff)); \
5a681086 125 dst = DrawLineDest; \
126
127#define PICOSCAN_POST \
e51e5983 128 PicoScan32xEnd(l + (lines_sft_offs & 0xff)); \
5a681086 129
130#define make_do_loop(name, pre_code, post_code, md_code) \
131/* Direct Color Mode */ \
132static void do_loop_dc##name(unsigned short *dst, \
e51e5983 133 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 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; \
e51e5983 139 int lines = lines_sft_offs >> 16; \
5a681086 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 */ \
151static void do_loop_pp##name(unsigned short *dst, \
e51e5983 152 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 153{ \
154 unsigned short *pal = Pico32xMem->pal_native; \
155 unsigned char *pmd = PicoDraw2FB + 328 * 8 + 8; \
156 unsigned short *palmd = HighPal; \
e51e5983 157 unsigned char *p32x; \
158 int lines = lines_sft_offs >> 16; \
5a681086 159 int l; \
160 (void)palmd; \
161 for (l = 0; l < lines; l++, pmd += 8) { \
162 pre_code; \
e51e5983 163 p32x = (void *)(dram + dram[l]); \
164 p32x += (lines_sft_offs >> 8) & 1; \
5a681086 165 do_line_pp(dst, p32x, pmd, md_code); \
166 post_code; \
167 } \
168} \
169 \
170/* Run Length Mode */ \
171static void do_loop_rl##name(unsigned short *dst, \
e51e5983 172 unsigned short *dram, int lines_sft_offs, int mdbg) \
5a681086 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; \
e51e5983 178 int lines = lines_sft_offs >> 16; \
5a681086 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) \
192extern void do_loop_dc##name(unsigned short *dst, \
193 unsigned short *dram, int lines_offs, int mdbg); \
194extern void do_loop_pp##name(unsigned short *dst, \
195 unsigned short *dram, int lines_offs, int mdbg); \
196extern void do_loop_rl##name(unsigned short *dst, \
197 unsigned short *dram, int lines_offs, int mdbg);
198#endif
199
200make_do_loop(,,,)
201make_do_loop(_md, , , MD_LAYER_CODE)
202make_do_loop(_scan, PICOSCAN_PRE, PICOSCAN_POST, )
203make_do_loop(_scan_md, PICOSCAN_PRE, PICOSCAN_POST, MD_LAYER_CODE)
204
205typedef void (*do_loop_func)(unsigned short *dst, unsigned short *dram, int lines, int mdbg);
206enum { DO_LOOP, DO_LOOP_MD, DO_LOOP_SCAN, DO_LOOP_MD_SCAN };
207
208static const do_loop_func do_loop_dc_f[] = { do_loop_dc, do_loop_dc_md, do_loop_dc_scan, do_loop_dc_scan_md };
209static const do_loop_func do_loop_pp_f[] = { do_loop_pp, do_loop_pp_md, do_loop_pp_scan, do_loop_pp_scan_md };
210static 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
212void 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;
e51e5983 217 int lines_sft_offs;
5a681086 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
249do_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;
e51e5983 254 lines_sft_offs = (lines << 16) | offs;
255 if (Pico32x.vdp_regs[2 / 2] & P32XV_SFT)
256 lines_sft_offs |= 1 << 8;
5a681086 257
e51e5983 258 do_loop[which_func](DrawLineDest, dram, lines_sft_offs, md_bg);
5a681086 259}
260
261void 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;
266c6afa 275 }
974fdb5b 276}
277