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
5 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
8 #include "../pico_int.h"
13 #define _rot_comp Pico_mcd->rot_comp
15 static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
18 static void gfx_cd_start(void)
24 if (w == 0 || h == 0) {
25 elprintf(EL_CD|EL_ANOMALY, "gfx_cd_start with %ux%u", w, h);
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;
37 _rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
39 pcd_event_schedule_s68k(PCD_EVENT_GFX, 5 * w * h);
41 switch (_rot_comp.Reg_58 & 6) // Scr_16?
44 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
47 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
50 _rot_comp.Stamp_Map_Adr = 0x20000;
52 case 6: // .Scr_16_Dot_32
53 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
57 if (PicoOpt & POPT_EN_MCD_GFX)
59 unsigned int func = _rot_comp.Function;
60 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
63 gfx_do_line(func, stamp_base, w);
68 PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int 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;
81 if (_rot_comp.Reg_64 > 1)
86 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
89 dprintf("gfx_cd_read(%02x) = %04x", a, d);
95 static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
98 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
99 unsigned int XD, Buffer_Adr;
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);
106 ecx = (ecx & 0xffff) << 8;
108 DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
109 _rot_comp.Vector_Adr += 8;
115 if (!(func & 1)) // NOT TILED
117 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
118 if ((ecx | edx) & mask)
120 if (func & 0x18) goto Next_Pixel;
126 if (func & 2) // mode 32x32 dot
128 if (func & 4) // 16x16 screen
130 ebx = ((ecx >> (11+5)) & 0x007f) |
131 ((edx >> (11-2)) & 0x3f80);
135 ebx = ((ecx >> (11+5)) & 0x07) |
136 ((edx >> (11+2)) & 0x38);
139 else // mode 16x16 dot
141 if (func & 4) // 16x16 screen
143 ebx = ((ecx >> (11+4)) & 0x00ff) |
144 ((edx >> (11-4)) & 0xff00);
148 ebx = ((ecx >> (11+4)) & 0x0f) |
149 ((edx >> (11+0)) & 0xf0);
153 edi = stamp_base[ebx];
154 esi = (edi & 0x7ff) << 7;
155 if (!esi) { pixel = 0; goto Pixel_Out; }
160 if (func & 2) edi |= 1; // 32 dots?
163 case 0x00: // No_Flip_0, 16x16 dots
164 ebx = (ebx >> 9) & 0x3c;
166 edi = (eax & 0x3800) ^ 0x1000; // bswap
167 eax = ((eax >> 8) & 0x40) + ebx;
169 case 0x01: // No_Flip_0, 32x32 dots
170 ebx = (ebx >> 9) & 0x7c;
172 edi = (eax & 0x3800) ^ 0x1000; // bswap
173 eax = ((eax >> 7) & 0x180) + ebx;
175 case 0x02: // No_Flip_90, 16x16 dots
176 eax = (eax >> 9) & 0x3c;
178 edi = (ebx & 0x3800) ^ 0x2800; // bswap
179 eax += ((ebx >> 8) & 0x40) ^ 0x40;
181 case 0x03: // No_Flip_90, 32x32 dots
182 eax = (eax >> 9) & 0x7c;
184 edi = (ebx & 0x3800) ^ 0x2800; // bswap
185 eax += ((ebx >> 7) & 0x180) ^ 0x180;
187 case 0x04: // No_Flip_180, 16x16 dots
188 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
190 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
191 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
193 case 0x05: // No_Flip_180, 32x32 dots
194 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
196 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
197 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
199 case 0x06: // No_Flip_270, 16x16 dots
200 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
202 edi = (ebx & 0x3800) ^ 0x1000; // bswap
203 eax += (ebx >> 8) & 0x40;
205 case 0x07: // No_Flip_270, 32x32 dots
206 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
208 edi = (ebx & 0x3800) ^ 0x1000; // bswap
209 eax += (ebx >> 7) & 0x180;
211 case 0x08: // Flip_0, 16x16 dots
212 ebx = (ebx >> 9) & 0x3c;
214 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
215 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
217 case 0x09: // Flip_0, 32x32 dots
218 ebx = (ebx >> 9) & 0x7c;
220 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
221 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
223 case 0x0a: // Flip_90, 16x16 dots
224 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
226 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
227 eax += ((ebx >> 8) & 0x40) ^ 0x40;
229 case 0x0b: // Flip_90, 32x32 dots
230 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
232 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
233 eax += ((ebx >> 7) & 0x180) ^ 0x180;
235 case 0x0c: // Flip_180, 16x16 dots
236 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
238 edi = (eax & 0x3800) ^ 0x1000; // bswap
239 eax = ((eax >> 8) & 0x40) + ebx;
241 case 0x0d: // Flip_180, 32x32 dots
242 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
244 edi = (eax & 0x3800) ^ 0x1000; // bswap
245 eax = ((eax >> 7) & 0x180) + ebx;
247 case 0x0e: // Flip_270, 16x16 dots
248 eax = (eax >> 9) & 0x3c;
250 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
251 eax += (ebx >> 8) & 0x40;
253 case 0x0f: // Flip_270, 32x32 dots
254 eax = (eax >> 9) & 0x7c;
256 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
257 eax += (ebx >> 7) & 0x180;
261 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
262 if (!(edi & 0x800)) pixel >>= 4;
266 if (!pixel && (func & 0x18)) goto Next_Pixel;
267 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
268 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
271 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
272 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
276 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
277 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
282 ecx += (DYXS << 16) >> 16; // _rot_comp.DXS;
283 edx += DYXS >> 16; // _rot_comp.DYS;
287 Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
297 // _rot_comp.V_Dot--; // will be done by caller
301 PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
303 dprintf("gfx_cd_write16(%x, %04x)", a, d);
305 if (_rot_comp.Reg_58 & 0x8000)
306 elprintf(EL_CD|EL_ANOMALY, "cd: busy gfx reg write %02x %04x", a, d);
309 case 0x58: // .Reg_Stamp_Size
310 _rot_comp.Reg_58 = d & 7;
313 case 0x5A: // .Reg_Stamp_Adr
314 _rot_comp.Reg_5A = d & 0xffe0;
317 case 0x5C: // .Reg_IM_VCell_Size
318 _rot_comp.Reg_5C = d & 0x1f;
321 case 0x5E: // .Reg_IM_Adr
322 _rot_comp.Reg_5E = d & 0xFFF8;
325 case 0x60: // .Reg_IM_Offset
326 _rot_comp.Reg_60 = d & 0x3f;
329 case 0x62: // .Reg_IM_HDot_Size
330 _rot_comp.Reg_62 = d & 0x1ff;
333 case 0x64: // .Reg_IM_VDot_Size
334 _rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
337 case 0x66: // .Reg_Vector_Adr
338 _rot_comp.Reg_66 = d & 0xfffe;
339 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
343 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
348 PICO_INTERNAL void gfx_cd_reset(void)
350 memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
354 // --------------------------------
356 #include "cell_map.c"
358 #ifndef UTYPES_DEFINED
359 typedef unsigned short u16;
362 // check: Heart of the alien, jaguar xj 220
363 PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
366 unsigned int asrc, a2;
369 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
371 switch (Pico.video.type)
377 asrc = cell_map(source >> 2) << 2;
379 // if(a&1) d=(d<<8)|(d>>8); // ??
380 r[a>>1] = *(u16 *)(base + asrc);
385 rendstatus |= PDRAW_SPRITES_MOVED;
391 for(a2=a&0x7f; len; len--)
393 asrc = cell_map(source >> 2) << 2;
395 r[a2>>1] = *(u16 *)(base + asrc);
400 if(a2 >= 0x80) break;
405 case 5: // vsram[a&0x003f]=d;
407 for(a2=a&0x7f; len; len--)
409 asrc = cell_map(source >> 2) << 2;
411 r[a2>>1] = *(u16 *)(base + asrc);
416 if(a2 >= 0x80) break;
422 Pico.video.addr=(u16)a;