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 UPDATE_CYCLES 20000
15 #define _rot_comp Pico_mcd->rot_comp
17 static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
20 static void gfx_cd_start(void)
27 if (w == 0 || h == 0) {
28 elprintf(EL_CD|EL_ANOMALY, "gfx_cd_start with %ux%u", w, h);
34 // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
35 _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18); // Jmp_Adr
36 // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
37 _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
38 _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
40 switch (_rot_comp.Reg_58 & 6) // Scr_16?
43 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
46 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
49 _rot_comp.Stamp_Map_Adr = 0x20000;
51 case 6: // .Scr_16_Dot_32
52 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
56 _rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
59 if (cycles > UPDATE_CYCLES)
60 y_step = (UPDATE_CYCLES + 5 * w - 1) / (5 * w);
64 _rot_comp.y_step = y_step;
65 pcd_event_schedule_s68k(PCD_EVENT_GFX, 5 * w * y_step);
68 void gfx_cd_update(unsigned int cycles)
70 int w = _rot_comp.Reg_62;
73 if (!(Pico_mcd->rot_comp.Reg_58 & 0x8000))
77 _rot_comp.Reg_64 -= _rot_comp.y_step;
79 if ((int)_rot_comp.Reg_64 <= 0) {
80 Pico_mcd->rot_comp.Reg_58 &= 0x7fff;
81 Pico_mcd->rot_comp.Reg_64 = 0;
82 if (Pico_mcd->s68k_regs[0x33] & PCDS_IEN1) {
83 elprintf(EL_INTS |EL_CD, "s68k: gfx_cd irq 1");
88 next = _rot_comp.Reg_64;
89 if (next > _rot_comp.y_step)
90 next = _rot_comp.y_step;
92 pcd_event_schedule(cycles, PCD_EVENT_GFX, 5 * w * next);
96 if (PicoOpt & POPT_EN_MCD_GFX)
98 unsigned int func = _rot_comp.Function;
99 unsigned short *stamp_base = (unsigned short *)
100 (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
103 gfx_do_line(func, stamp_base, w);
107 PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
112 case 0x58: d = _rot_comp.Reg_58; break;
113 case 0x5A: d = _rot_comp.Reg_5A; break;
114 case 0x5C: d = _rot_comp.Reg_5C; break;
115 case 0x5E: d = _rot_comp.Reg_5E; break;
116 case 0x60: d = _rot_comp.Reg_60; break;
117 case 0x62: d = _rot_comp.Reg_62; break;
118 case 0x64: d = _rot_comp.Reg_64; break;
120 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
123 dprintf("gfx_cd_read(%02x) = %04x", a, d);
129 static void gfx_do_line(unsigned int func, unsigned short *stamp_base,
132 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
133 unsigned int XD, Buffer_Adr;
136 XD = _rot_comp.Reg_60 & 7;
137 Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
138 ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
140 ecx = (ecx & 0xffff) << 8;
142 DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
143 _rot_comp.Vector_Adr += 8;
149 if (!(func & 1)) // NOT TILED
151 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
152 if ((ecx | edx) & mask)
154 if (func & 0x18) goto Next_Pixel;
160 if (func & 2) // mode 32x32 dot
162 if (func & 4) // 16x16 screen
164 ebx = ((ecx >> (11+5)) & 0x007f) |
165 ((edx >> (11-2)) & 0x3f80);
169 ebx = ((ecx >> (11+5)) & 0x07) |
170 ((edx >> (11+2)) & 0x38);
173 else // mode 16x16 dot
175 if (func & 4) // 16x16 screen
177 ebx = ((ecx >> (11+4)) & 0x00ff) |
178 ((edx >> (11-4)) & 0xff00);
182 ebx = ((ecx >> (11+4)) & 0x0f) |
183 ((edx >> (11+0)) & 0xf0);
187 edi = stamp_base[ebx];
188 esi = (edi & 0x7ff) << 7;
189 if (!esi) { pixel = 0; goto Pixel_Out; }
194 if (func & 2) edi |= 1; // 32 dots?
197 case 0x00: // No_Flip_0, 16x16 dots
198 ebx = (ebx >> 9) & 0x3c;
200 edi = (eax & 0x3800) ^ 0x1000; // bswap
201 eax = ((eax >> 8) & 0x40) + ebx;
203 case 0x01: // No_Flip_0, 32x32 dots
204 ebx = (ebx >> 9) & 0x7c;
206 edi = (eax & 0x3800) ^ 0x1000; // bswap
207 eax = ((eax >> 7) & 0x180) + ebx;
209 case 0x02: // No_Flip_90, 16x16 dots
210 eax = (eax >> 9) & 0x3c;
212 edi = (ebx & 0x3800) ^ 0x2800; // bswap
213 eax += ((ebx >> 8) & 0x40) ^ 0x40;
215 case 0x03: // No_Flip_90, 32x32 dots
216 eax = (eax >> 9) & 0x7c;
218 edi = (ebx & 0x3800) ^ 0x2800; // bswap
219 eax += ((ebx >> 7) & 0x180) ^ 0x180;
221 case 0x04: // No_Flip_180, 16x16 dots
222 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
224 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
225 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
227 case 0x05: // No_Flip_180, 32x32 dots
228 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
230 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
231 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
233 case 0x06: // No_Flip_270, 16x16 dots
234 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
236 edi = (ebx & 0x3800) ^ 0x1000; // bswap
237 eax += (ebx >> 8) & 0x40;
239 case 0x07: // No_Flip_270, 32x32 dots
240 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
242 edi = (ebx & 0x3800) ^ 0x1000; // bswap
243 eax += (ebx >> 7) & 0x180;
245 case 0x08: // Flip_0, 16x16 dots
246 ebx = (ebx >> 9) & 0x3c;
248 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
249 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
251 case 0x09: // Flip_0, 32x32 dots
252 ebx = (ebx >> 9) & 0x7c;
254 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
255 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
257 case 0x0a: // Flip_90, 16x16 dots
258 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
260 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
261 eax += ((ebx >> 8) & 0x40) ^ 0x40;
263 case 0x0b: // Flip_90, 32x32 dots
264 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
266 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
267 eax += ((ebx >> 7) & 0x180) ^ 0x180;
269 case 0x0c: // Flip_180, 16x16 dots
270 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
272 edi = (eax & 0x3800) ^ 0x1000; // bswap
273 eax = ((eax >> 8) & 0x40) + ebx;
275 case 0x0d: // Flip_180, 32x32 dots
276 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
278 edi = (eax & 0x3800) ^ 0x1000; // bswap
279 eax = ((eax >> 7) & 0x180) + ebx;
281 case 0x0e: // Flip_270, 16x16 dots
282 eax = (eax >> 9) & 0x3c;
284 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
285 eax += (ebx >> 8) & 0x40;
287 case 0x0f: // Flip_270, 32x32 dots
288 eax = (eax >> 9) & 0x7c;
290 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
291 eax += (ebx >> 7) & 0x180;
295 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
296 if (!(edi & 0x800)) pixel >>= 4;
300 if (!pixel && (func & 0x18)) goto Next_Pixel;
301 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
302 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
305 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
306 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
310 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
311 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
316 ecx += (DYXS << 16) >> 16; // _rot_comp.DXS;
317 edx += DYXS >> 16; // _rot_comp.DYS;
321 Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
331 // _rot_comp.V_Dot--; // will be done by caller
335 PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
337 dprintf("gfx_cd_write16(%x, %04x)", a, d);
339 if (_rot_comp.Reg_58 & 0x8000)
340 elprintf(EL_CD|EL_ANOMALY, "cd: busy gfx reg write %02x %04x", a, d);
343 case 0x58: // .Reg_Stamp_Size
344 _rot_comp.Reg_58 = d & 7;
347 case 0x5A: // .Reg_Stamp_Adr
348 _rot_comp.Reg_5A = d & 0xffe0;
351 case 0x5C: // .Reg_IM_VCell_Size
352 _rot_comp.Reg_5C = d & 0x1f;
355 case 0x5E: // .Reg_IM_Adr
356 _rot_comp.Reg_5E = d & 0xFFF8;
359 case 0x60: // .Reg_IM_Offset
360 _rot_comp.Reg_60 = d & 0x3f;
363 case 0x62: // .Reg_IM_HDot_Size
364 _rot_comp.Reg_62 = d & 0x1ff;
367 case 0x64: // .Reg_IM_VDot_Size
368 _rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
371 case 0x66: // .Reg_Vector_Adr
372 _rot_comp.Reg_66 = d & 0xfffe;
373 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
377 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
382 PICO_INTERNAL void gfx_cd_reset(void)
384 memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
388 // --------------------------------
390 #include "cell_map.c"
392 #ifndef UTYPES_DEFINED
393 typedef unsigned short u16;
396 // check: Heart of the alien, jaguar xj 220
397 PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
400 unsigned int asrc, a2;
403 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
405 switch (Pico.video.type)
411 asrc = cell_map(source >> 2) << 2;
413 // if(a&1) d=(d<<8)|(d>>8); // ??
414 r[a>>1] = *(u16 *)(base + asrc);
419 rendstatus |= PDRAW_SPRITES_MOVED;
425 for(a2=a&0x7f; len; len--)
427 asrc = cell_map(source >> 2) << 2;
429 r[a2>>1] = *(u16 *)(base + asrc);
434 if(a2 >= 0x80) break;
439 case 5: // vsram[a&0x003f]=d;
441 for(a2=a&0x7f; len; len--)
443 asrc = cell_map(source >> 2) << 2;
445 r[a2>>1] = *(u16 *)(base + asrc);
450 if(a2 >= 0x80) break;
456 Pico.video.addr=(u16)a;