fix yet another portability issue
[picodrive.git] / pico / cd / gfx_cd.c
CommitLineData
6cadc2da 1// This is a direct rewrite of gfx_cd.asm (x86 asm to C).
2// You can even find some x86 register names :)
3// Original code (c) 2002 by Stéphane Dallongeville
4
5// (c) Copyright 2007, Grazvydas "notaz" Ignotas
6
cb4a513a 7
efcba75f 8#include "../pico_int.h"
cb4a513a 9
9c9cda8c 10#undef dprintf
11#define dprintf(...)
12
eff55556 13#define _rot_comp Pico_mcd->rot_comp
d1df8786 14
ae214f1c 15static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
16 unsigned int H_Dot);
d1df8786 17
cb4a513a 18static void gfx_cd_start(void)
19{
ae214f1c 20 int w, h;
21
22 w = _rot_comp.Reg_62;
23 h = _rot_comp.Reg_64;
24 if (w == 0 || h == 0) {
25 elprintf(EL_CD|EL_ANOMALY, "gfx_cd_start with %ux%u", w, h);
26 _rot_comp.Reg_64 = 0;
27 // irq?
28 return;
29 }
d1df8786 30
eff55556 31 // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
32 _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18); // Jmp_Adr
33 // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
34 _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
35 _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
a4030801 36
eff55556 37 _rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
d1df8786 38
ae214f1c 39 pcd_event_schedule_s68k(PCD_EVENT_GFX, 5 * w * h);
40
eff55556 41 switch (_rot_comp.Reg_58 & 6) // Scr_16?
a4030801 42 {
43 case 0: // ?
eff55556 44 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
a4030801 45 break;
46 case 2: // .Dot_32
eff55556 47 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
a4030801 48 break;
49 case 4: // .Scr_16
eff55556 50 _rot_comp.Stamp_Map_Adr = 0x20000;
a4030801 51 break;
52 case 6: // .Scr_16_Dot_32
eff55556 53 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
a4030801 54 break;
55 }
56
ae214f1c 57 if (PicoOpt & POPT_EN_MCD_GFX)
58 {
59 unsigned int func = _rot_comp.Function;
60 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
c008977e 61
ae214f1c 62 while (h--)
63 gfx_do_line(func, stamp_base, w);
64 }
d1df8786 65}
66
67
ae214f1c 68PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
d1df8786 69{
ae214f1c 70 unsigned int d = 0;
71
72 switch (a) {
73 case 0x58: d = _rot_comp.Reg_58; break;
74 case 0x5A: d = _rot_comp.Reg_5A; break;
75 case 0x5C: d = _rot_comp.Reg_5C; break;
76 case 0x5E: d = _rot_comp.Reg_5E; break;
77 case 0x60: d = _rot_comp.Reg_60; break;
78 case 0x62: d = _rot_comp.Reg_62; break;
79 case 0x64:
80 d = _rot_comp.Reg_64;
81 if (_rot_comp.Reg_64 > 1)
82 // fudge..
83 _rot_comp.Reg_64--;
84 break;
85 case 0x66: break;
86 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
cb4a513a 87 }
cb4a513a 88
ae214f1c 89 dprintf("gfx_cd_read(%02x) = %04x", a, d);
90
91 return d;
92
93}
d1df8786 94
ae214f1c 95static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
96 unsigned int H_Dot)
a4030801 97{
528ec956 98 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
00bd648e 99 unsigned int XD, Buffer_Adr;
528ec956 100 int DYXS;
a4030801 101
eff55556 102 XD = _rot_comp.Reg_60 & 7;
103 Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
104 ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
a4030801 105 edx = ecx >> 16;
106 ecx = (ecx & 0xffff) << 8;
107 edx <<= 8;
eff55556 108 DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
109 _rot_comp.Vector_Adr += 8;
a4030801 110
111 // MAKE_IMAGE_LINE
528ec956 112 while (H_Dot)
a4030801 113 {
c008977e 114 // MAKE_IMAGE_PIXEL
115 if (!(func & 1)) // NOT TILED
116 {
117 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
118 if ((ecx | edx) & mask)
119 {
120 if (func & 0x18) goto Next_Pixel;
121 pixel = 0;
122 goto Pixel_Out;
123 }
124 }
125
a4030801 126 if (func & 2) // mode 32x32 dot
127 {
128 if (func & 4) // 16x16 screen
129 {
00bd648e 130 ebx = ((ecx >> (11+5)) & 0x007f) |
131 ((edx >> (11-2)) & 0x3f80);
a4030801 132 }
133 else // 1x1 screen
134 {
00bd648e 135 ebx = ((ecx >> (11+5)) & 0x07) |
136 ((edx >> (11+2)) & 0x38);
a4030801 137 }
138 }
139 else // mode 16x16 dot
140 {
141 if (func & 4) // 16x16 screen
142 {
00bd648e 143 ebx = ((ecx >> (11+4)) & 0x00ff) |
144 ((edx >> (11-4)) & 0xff00);
a4030801 145 }
146 else // 1x1 screen
147 {
00bd648e 148 ebx = ((ecx >> (11+4)) & 0x0f) |
149 ((edx >> (11+0)) & 0xf0);
a4030801 150 }
151 }
a4030801 152
c008977e 153 edi = stamp_base[ebx];
00bd648e 154 esi = (edi & 0x7ff) << 7;
528ec956 155 if (!esi) { pixel = 0; goto Pixel_Out; }
00bd648e 156 edi >>= (11+1);
a4030801 157 edi &= (0x1c>>1);
158 eax = ecx;
159 ebx = edx;
160 if (func & 2) edi |= 1; // 32 dots?
161 switch (edi)
162 {
163 case 0x00: // No_Flip_0, 16x16 dots
164 ebx = (ebx >> 9) & 0x3c;
165 ebx += esi;
166 edi = (eax & 0x3800) ^ 0x1000; // bswap
167 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 168 break;
169 case 0x01: // No_Flip_0, 32x32 dots
170 ebx = (ebx >> 9) & 0x7c;
171 ebx += esi;
172 edi = (eax & 0x3800) ^ 0x1000; // bswap
173 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 174 break;
175 case 0x02: // No_Flip_90, 16x16 dots
176 eax = (eax >> 9) & 0x3c;
177 eax += esi;
178 edi = (ebx & 0x3800) ^ 0x2800; // bswap
179 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 180 break;
181 case 0x03: // No_Flip_90, 32x32 dots
182 eax = (eax >> 9) & 0x7c;
183 eax += esi;
184 edi = (ebx & 0x3800) ^ 0x2800; // bswap
01bc6b19 185 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 186 break;
187 case 0x04: // No_Flip_180, 16x16 dots
188 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
189 ebx += esi;
190 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
191 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 192 break;
193 case 0x05: // No_Flip_180, 32x32 dots
194 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
195 ebx += esi;
196 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
197 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 198 break;
199 case 0x06: // No_Flip_270, 16x16 dots
200 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
201 eax += esi;
202 edi = (ebx & 0x3800) ^ 0x1000; // bswap
203 eax += (ebx >> 8) & 0x40;
a4030801 204 break;
205 case 0x07: // No_Flip_270, 32x32 dots
206 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
207 eax += esi;
208 edi = (ebx & 0x3800) ^ 0x1000; // bswap
209 eax += (ebx >> 7) & 0x180;
a4030801 210 break;
211 case 0x08: // Flip_0, 16x16 dots
01bc6b19 212 ebx = (ebx >> 9) & 0x3c;
a4030801 213 ebx += esi;
214 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
215 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 216 break;
217 case 0x09: // Flip_0, 32x32 dots
01bc6b19 218 ebx = (ebx >> 9) & 0x7c;
a4030801 219 ebx += esi;
220 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
221 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 222 break;
223 case 0x0a: // Flip_90, 16x16 dots
224 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
225 eax += esi;
226 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
227 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 228 break;
229 case 0x0b: // Flip_90, 32x32 dots
230 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
231 eax += esi;
232 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
233 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 234 break;
235 case 0x0c: // Flip_180, 16x16 dots
236 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
237 ebx += esi;
238 edi = (eax & 0x3800) ^ 0x1000; // bswap
239 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 240 break;
241 case 0x0d: // Flip_180, 32x32 dots
242 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
243 ebx += esi;
244 edi = (eax & 0x3800) ^ 0x1000; // bswap
245 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 246 break;
247 case 0x0e: // Flip_270, 16x16 dots
248 eax = (eax >> 9) & 0x3c;
249 eax += esi;
250 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
251 eax += (ebx >> 8) & 0x40;
a4030801 252 break;
253 case 0x0f: // Flip_270, 32x32 dots
254 eax = (eax >> 9) & 0x7c;
255 eax += esi;
256 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
257 eax += (ebx >> 7) & 0x180;
a4030801 258 break;
259 }
260
01bc6b19 261 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
262 if (!(edi & 0x800)) pixel >>= 4;
528ec956 263 else pixel &= 0x0f;
01bc6b19 264
a4030801 265Pixel_Out:
a4030801 266 if (!pixel && (func & 0x18)) goto Next_Pixel;
528ec956 267 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
a4030801 268 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
528ec956 269 if (XD & 1)
a4030801 270 {
271 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
272 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
273 }
274 else
275 {
276 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
277 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
278 }
279
280
281Next_Pixel:
eff55556 282 ecx += (DYXS << 16) >> 16; // _rot_comp.DXS;
283 edx += DYXS >> 16; // _rot_comp.DYS;
528ec956 284 XD++;
285 if (XD >= 8)
a4030801 286 {
eff55556 287 Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
528ec956 288 XD = 0;
a4030801 289 }
528ec956 290 H_Dot--;
a4030801 291 }
528ec956 292 // end while
a4030801 293
294
00bd648e 295// nothing_to_draw:
eff55556 296 _rot_comp.YD++;
297 // _rot_comp.V_Dot--; // will be done by caller
a4030801 298}
d1df8786 299
300
eff55556 301PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
cb4a513a 302{
5c69a605 303 dprintf("gfx_cd_write16(%x, %04x)", a, d);
cb4a513a 304
ae214f1c 305 if (_rot_comp.Reg_58 & 0x8000)
306 elprintf(EL_CD|EL_ANOMALY, "cd: busy gfx reg write %02x %04x", a, d);
307
cb4a513a 308 switch (a) {
d1df8786 309 case 0x58: // .Reg_Stamp_Size
eff55556 310 _rot_comp.Reg_58 = d & 7;
d1df8786 311 return;
312
313 case 0x5A: // .Reg_Stamp_Adr
eff55556 314 _rot_comp.Reg_5A = d & 0xffe0;
d1df8786 315 return;
316
317 case 0x5C: // .Reg_IM_VCell_Size
eff55556 318 _rot_comp.Reg_5C = d & 0x1f;
d1df8786 319 return;
320
321 case 0x5E: // .Reg_IM_Adr
eff55556 322 _rot_comp.Reg_5E = d & 0xFFF8;
d1df8786 323 return;
324
325 case 0x60: // .Reg_IM_Offset
eff55556 326 _rot_comp.Reg_60 = d & 0x3f;
d1df8786 327 return;
328
329 case 0x62: // .Reg_IM_HDot_Size
eff55556 330 _rot_comp.Reg_62 = d & 0x1ff;
d1df8786 331 return;
332
333 case 0x64: // .Reg_IM_VDot_Size
eff55556 334 _rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
d1df8786 335 return;
336
337 case 0x66: // .Reg_Vector_Adr
eff55556 338 _rot_comp.Reg_66 = d & 0xfffe;
cb4a513a 339 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
340 gfx_cd_start();
341 return;
d1df8786 342
5c69a605 343 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
cb4a513a 344 }
345}
346
d1df8786 347
eff55556 348PICO_INTERNAL void gfx_cd_reset(void)
51a902ae 349{
eff55556 350 memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
fa1e5e29 351}
352
353
354// --------------------------------
355
356#include "cell_map.c"
357
eff55556 358#ifndef UTYPES_DEFINED
fa1e5e29 359typedef unsigned short u16;
eff55556 360#endif
fa1e5e29 361
0a051f55 362// check: Heart of the alien, jaguar xj 220
eff55556 363PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
fa1e5e29 364{
365 unsigned char *base;
366 unsigned int asrc, a2;
367 u16 *r;
368
369 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
370
371 switch (Pico.video.type)
372 {
373 case 1: // vram
374 r = Pico.vram;
375 for(; len; len--)
376 {
377 asrc = cell_map(source >> 2) << 2;
378 asrc |= source & 2;
379 // if(a&1) d=(d<<8)|(d>>8); // ??
380 r[a>>1] = *(u16 *)(base + asrc);
381 source += 2;
382 // AutoIncrement
383 a=(u16)(a+inc);
384 }
602133e1 385 rendstatus |= PDRAW_SPRITES_MOVED;
fa1e5e29 386 break;
387
388 case 3: // cram
389 Pico.m.dirtyPal = 1;
390 r = Pico.cram;
391 for(a2=a&0x7f; len; len--)
392 {
393 asrc = cell_map(source >> 2) << 2;
394 asrc |= source & 2;
395 r[a2>>1] = *(u16 *)(base + asrc);
396 source += 2;
397 // AutoIncrement
398 a2+=inc;
399 // good dest?
400 if(a2 >= 0x80) break;
401 }
402 a=(a&0xff00)|a2;
403 break;
404
405 case 5: // vsram[a&0x003f]=d;
406 r = Pico.vsram;
407 for(a2=a&0x7f; len; len--)
408 {
409 asrc = cell_map(source >> 2) << 2;
410 asrc |= source & 2;
411 r[a2>>1] = *(u16 *)(base + asrc);
412 source += 2;
413 // AutoIncrement
414 a2+=inc;
415 // good dest?
416 if(a2 >= 0x80) break;
417 }
418 a=(a&0xff00)|a2;
419 break;
420 }
421 // remember addr
422 Pico.video.addr=(u16)a;
51a902ae 423}
424