32x: built-in BIOS; reset handling; 68k memhandler split
[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
eff55556 10#define _rot_comp Pico_mcd->rot_comp
d1df8786 11
75736070 12static const int Table_Rot_Time[] =
d1df8786 13{
14 0x00054000, 0x00048000, 0x00040000, 0x00036000, //; 008-032 ; briefing - sprite
15 0x0002E000, 0x00028000, 0x00024000, 0x00022000, //; 036-064 ; arbre souvent
16 0x00021000, 0x00020000, 0x0001E000, 0x0001B800, //; 068-096 ; map thunderstrike
17 0x00019800, 0x00017A00, 0x00015C00, 0x00013E00, //; 100-128 ; logo défoncé
18
19 0x00012000, 0x00011800, 0x00011000, 0x00010800, //; 132-160 ; briefing - map
20 0x00010000, 0x0000F800, 0x0000F000, 0x0000E800, //; 164-192
21 0x0000E000, 0x0000D800, 0x0000D000, 0x0000C800, //; 196-224
22 0x0000C000, 0x0000B800, 0x0000B000, 0x0000A800, //; 228-256 ; batman visage
23
24 0x0000A000, 0x00009F00, 0x00009E00, 0x00009D00, //; 260-288
25 0x00009C00, 0x00009B00, 0x00009A00, 0x00009900, //; 292-320
26 0x00009800, 0x00009700, 0x00009600, 0x00009500, //; 324-352
27 0x00009400, 0x00009300, 0x00009200, 0x00009100, //; 356-384
28
29 0x00009000, 0x00008F00, 0x00008E00, 0x00008D00, //; 388-416
30 0x00008C00, 0x00008B00, 0x00008A00, 0x00008900, //; 420-448
31 0x00008800, 0x00008700, 0x00008600, 0x00008500, //; 452-476
32 0x00008400, 0x00008300, 0x00008200, 0x00008100, //; 480-512
33};
34
35
cb4a513a 36static void gfx_cd_start(void)
37{
d1df8786 38 int upd_len;
39
eff55556 40 // _rot_comp.XD_Mul = ((_rot_comp.Reg_5C & 0x1f) + 1) * 4; // unused
41 _rot_comp.Function = (_rot_comp.Reg_58 & 7) | (Pico_mcd->s68k_regs[3] & 0x18); // Jmp_Adr
42 // _rot_comp.Buffer_Adr = (_rot_comp.Reg_5E & 0xfff8) << 2; // unused?
43 _rot_comp.YD = (_rot_comp.Reg_60 >> 3) & 7;
44 _rot_comp.Vector_Adr = (_rot_comp.Reg_66 & 0xfffe) << 2;
a4030801 45
eff55556 46 upd_len = (_rot_comp.Reg_62 >> 3) & 0x3f;
d1df8786 47 upd_len = Table_Rot_Time[upd_len];
eff55556 48 _rot_comp.Draw_Speed = _rot_comp.Float_Part = upd_len;
d1df8786 49
eff55556 50 _rot_comp.Reg_58 |= 0x8000; // Stamp_Size, we start a new GFX operation
d1df8786 51
eff55556 52 switch (_rot_comp.Reg_58 & 6) // Scr_16?
a4030801 53 {
54 case 0: // ?
eff55556 55 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xff80) << 2;
a4030801 56 break;
57 case 2: // .Dot_32
eff55556 58 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xffe0) << 2;
a4030801 59 break;
60 case 4: // .Scr_16
eff55556 61 _rot_comp.Stamp_Map_Adr = 0x20000;
a4030801 62 break;
63 case 6: // .Scr_16_Dot_32
eff55556 64 _rot_comp.Stamp_Map_Adr = (_rot_comp.Reg_5A & 0xe000) << 2;
a4030801 65 break;
66 }
67
eff55556 68 dprintf("gfx_cd_start, stamp_map_addr=%06x", _rot_comp.Stamp_Map_Adr);
c008977e 69
d1df8786 70 gfx_cd_update();
71}
72
73
74static void gfx_completed(void)
75{
eff55556 76 _rot_comp.Reg_58 &= 0x7fff; // Stamp_Size
77 _rot_comp.Reg_64 = 0;
cb4a513a 78 if (Pico_mcd->s68k_regs[0x33] & (1<<1))
79 {
ca61ee42 80 elprintf(EL_INTS, "gfx_cd irq 1");
cb4a513a 81 SekInterruptS68k(1);
82 }
83}
84
d1df8786 85
00bd648e 86static void gfx_do(unsigned int func, unsigned short *stamp_base, unsigned int H_Dot)
a4030801 87{
528ec956 88 unsigned int eax, ebx, ecx, edx, esi, edi, pixel;
00bd648e 89 unsigned int XD, Buffer_Adr;
528ec956 90 int DYXS;
a4030801 91
eff55556 92 XD = _rot_comp.Reg_60 & 7;
93 Buffer_Adr = ((_rot_comp.Reg_5E & 0xfff8) + _rot_comp.YD) << 2;
94 ecx = *(unsigned int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr);
a4030801 95 edx = ecx >> 16;
96 ecx = (ecx & 0xffff) << 8;
97 edx <<= 8;
eff55556 98 DYXS = *(int *)(Pico_mcd->word_ram2M + _rot_comp.Vector_Adr + 4);
99 _rot_comp.Vector_Adr += 8;
a4030801 100
101 // MAKE_IMAGE_LINE
528ec956 102 while (H_Dot)
a4030801 103 {
c008977e 104 // MAKE_IMAGE_PIXEL
105 if (!(func & 1)) // NOT TILED
106 {
107 int mask = (func & 4) ? 0x00800000 : 0x00f80000;
108 if ((ecx | edx) & mask)
109 {
110 if (func & 0x18) goto Next_Pixel;
111 pixel = 0;
112 goto Pixel_Out;
113 }
114 }
115
a4030801 116 if (func & 2) // mode 32x32 dot
117 {
118 if (func & 4) // 16x16 screen
119 {
00bd648e 120 ebx = ((ecx >> (11+5)) & 0x007f) |
121 ((edx >> (11-2)) & 0x3f80);
a4030801 122 }
123 else // 1x1 screen
124 {
00bd648e 125 ebx = ((ecx >> (11+5)) & 0x07) |
126 ((edx >> (11+2)) & 0x38);
a4030801 127 }
128 }
129 else // mode 16x16 dot
130 {
131 if (func & 4) // 16x16 screen
132 {
00bd648e 133 ebx = ((ecx >> (11+4)) & 0x00ff) |
134 ((edx >> (11-4)) & 0xff00);
a4030801 135 }
136 else // 1x1 screen
137 {
00bd648e 138 ebx = ((ecx >> (11+4)) & 0x0f) |
139 ((edx >> (11+0)) & 0xf0);
a4030801 140 }
141 }
a4030801 142
c008977e 143 edi = stamp_base[ebx];
00bd648e 144 esi = (edi & 0x7ff) << 7;
528ec956 145 if (!esi) { pixel = 0; goto Pixel_Out; }
00bd648e 146 edi >>= (11+1);
a4030801 147 edi &= (0x1c>>1);
148 eax = ecx;
149 ebx = edx;
150 if (func & 2) edi |= 1; // 32 dots?
151 switch (edi)
152 {
153 case 0x00: // No_Flip_0, 16x16 dots
154 ebx = (ebx >> 9) & 0x3c;
155 ebx += esi;
156 edi = (eax & 0x3800) ^ 0x1000; // bswap
157 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 158 break;
159 case 0x01: // No_Flip_0, 32x32 dots
160 ebx = (ebx >> 9) & 0x7c;
161 ebx += esi;
162 edi = (eax & 0x3800) ^ 0x1000; // bswap
163 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 164 break;
165 case 0x02: // No_Flip_90, 16x16 dots
166 eax = (eax >> 9) & 0x3c;
167 eax += esi;
168 edi = (ebx & 0x3800) ^ 0x2800; // bswap
169 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 170 break;
171 case 0x03: // No_Flip_90, 32x32 dots
172 eax = (eax >> 9) & 0x7c;
173 eax += esi;
174 edi = (ebx & 0x3800) ^ 0x2800; // bswap
01bc6b19 175 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 176 break;
177 case 0x04: // No_Flip_180, 16x16 dots
178 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
179 ebx += esi;
180 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
181 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 182 break;
183 case 0x05: // No_Flip_180, 32x32 dots
184 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
185 ebx += esi;
186 edi = (eax & 0x3800) ^ 0x2800; // bswap and flip
187 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 188 break;
189 case 0x06: // No_Flip_270, 16x16 dots
190 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
191 eax += esi;
192 edi = (ebx & 0x3800) ^ 0x1000; // bswap
193 eax += (ebx >> 8) & 0x40;
a4030801 194 break;
195 case 0x07: // No_Flip_270, 32x32 dots
196 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
197 eax += esi;
198 edi = (ebx & 0x3800) ^ 0x1000; // bswap
199 eax += (ebx >> 7) & 0x180;
a4030801 200 break;
201 case 0x08: // Flip_0, 16x16 dots
01bc6b19 202 ebx = (ebx >> 9) & 0x3c;
a4030801 203 ebx += esi;
204 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
205 eax = (((eax >> 8) & 0x40) ^ 0x40) + ebx;
a4030801 206 break;
207 case 0x09: // Flip_0, 32x32 dots
01bc6b19 208 ebx = (ebx >> 9) & 0x7c;
a4030801 209 ebx += esi;
210 edi = (eax & 0x3800) ^ 0x2800; // bswap, flip
211 eax = (((eax >> 7) & 0x180) ^ 0x180) + ebx;
a4030801 212 break;
213 case 0x0a: // Flip_90, 16x16 dots
214 eax = ((eax >> 9) & 0x3c) ^ 0x3c;
215 eax += esi;
216 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
217 eax += ((ebx >> 8) & 0x40) ^ 0x40;
a4030801 218 break;
219 case 0x0b: // Flip_90, 32x32 dots
220 eax = ((eax >> 9) & 0x7c) ^ 0x7c;
221 eax += esi;
222 edi = (ebx & 0x3800) ^ 0x2800; // bswap, flip
223 eax += ((ebx >> 7) & 0x180) ^ 0x180;
a4030801 224 break;
225 case 0x0c: // Flip_180, 16x16 dots
226 ebx = ((ebx >> 9) & 0x3c) ^ 0x3c;
227 ebx += esi;
228 edi = (eax & 0x3800) ^ 0x1000; // bswap
229 eax = ((eax >> 8) & 0x40) + ebx;
a4030801 230 break;
231 case 0x0d: // Flip_180, 32x32 dots
232 ebx = ((ebx >> 9) & 0x7c) ^ 0x7c;
233 ebx += esi;
234 edi = (eax & 0x3800) ^ 0x1000; // bswap
235 eax = ((eax >> 7) & 0x180) + ebx;
a4030801 236 break;
237 case 0x0e: // Flip_270, 16x16 dots
238 eax = (eax >> 9) & 0x3c;
239 eax += esi;
240 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
241 eax += (ebx >> 8) & 0x40;
a4030801 242 break;
243 case 0x0f: // Flip_270, 32x32 dots
244 eax = (eax >> 9) & 0x7c;
245 eax += esi;
246 edi = (ebx & 0x3800) ^ 0x1000; // bswap, flip
247 eax += (ebx >> 7) & 0x180;
a4030801 248 break;
249 }
250
01bc6b19 251 pixel = *(Pico_mcd->word_ram2M + (edi >> 12) + eax);
252 if (!(edi & 0x800)) pixel >>= 4;
528ec956 253 else pixel &= 0x0f;
01bc6b19 254
a4030801 255Pixel_Out:
a4030801 256 if (!pixel && (func & 0x18)) goto Next_Pixel;
528ec956 257 esi = Buffer_Adr + ((XD>>1)^1); // pixel addr
a4030801 258 eax = *(Pico_mcd->word_ram2M + esi); // old pixel
528ec956 259 if (XD & 1)
a4030801 260 {
261 if ((eax & 0x0f) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
262 *(Pico_mcd->word_ram2M + esi) = pixel | (eax & 0xf0);
263 }
264 else
265 {
266 if ((eax & 0xf0) && (func & 0x18) == 0x08) goto Next_Pixel; // underwrite
267 *(Pico_mcd->word_ram2M + esi) = (pixel << 4) | (eax & 0xf);
268 }
269
270
271Next_Pixel:
eff55556 272 ecx += (DYXS << 16) >> 16; // _rot_comp.DXS;
273 edx += DYXS >> 16; // _rot_comp.DYS;
528ec956 274 XD++;
275 if (XD >= 8)
a4030801 276 {
eff55556 277 Buffer_Adr += ((_rot_comp.Reg_5C & 0x1f) + 1) << 5;
528ec956 278 XD = 0;
a4030801 279 }
528ec956 280 H_Dot--;
a4030801 281 }
528ec956 282 // end while
a4030801 283
284
00bd648e 285// nothing_to_draw:
eff55556 286 _rot_comp.YD++;
287 // _rot_comp.V_Dot--; // will be done by caller
a4030801 288}
d1df8786 289
290
eff55556 291PICO_INTERNAL void gfx_cd_update(void)
cb4a513a 292{
eff55556 293 int V_Dot = _rot_comp.Reg_64 & 0xff;
d1df8786 294 int jobs;
295
eff55556 296 dprintf("gfx_cd_update, Reg_64 = %04x", _rot_comp.Reg_64);
d1df8786 297
00bd648e 298 if (!V_Dot)
d1df8786 299 {
d1df8786 300 gfx_completed();
301 return;
302 }
303
eff55556 304 jobs = _rot_comp.Float_Part >> 16;
d1df8786 305
306 if (!jobs)
307 {
eff55556 308 _rot_comp.Float_Part += _rot_comp.Draw_Speed;
d1df8786 309 return;
310 }
311
eff55556 312 _rot_comp.Float_Part &= 0xffff;
313 _rot_comp.Float_Part += _rot_comp.Draw_Speed;
d1df8786 314
602133e1 315 if (PicoOpt & POPT_EN_MCD_GFX)
d1df8786 316 {
eff55556 317 unsigned int func = _rot_comp.Function;
318 unsigned int H_Dot = _rot_comp.Reg_62 & 0x1ff;
319 unsigned short *stamp_base = (unsigned short *) (Pico_mcd->word_ram2M + _rot_comp.Stamp_Map_Adr);
a4030801 320
00bd648e 321 while (jobs--)
322 {
323 gfx_do(func, stamp_base, H_Dot); // jmp [Jmp_Adr]:
d1df8786 324
00bd648e 325 V_Dot--; // dec byte [V_Dot]
326 if (V_Dot == 0)
327 {
328 // GFX_Completed:
329 gfx_completed();
330 return;
331 }
332 }
333 }
334 else
335 {
336 if (jobs >= V_Dot)
d1df8786 337 {
d1df8786 338 gfx_completed();
339 return;
340 }
00bd648e 341 V_Dot -= jobs;
d1df8786 342 }
00bd648e 343
eff55556 344 _rot_comp.Reg_64 = V_Dot;
cb4a513a 345}
346
347
eff55556 348PICO_INTERNAL_ASM unsigned int gfx_cd_read(unsigned int a)
cb4a513a 349{
d1df8786 350 unsigned int d = 0;
351
352 switch (a) {
eff55556 353 case 0x58: d = _rot_comp.Reg_58; break;
354 case 0x5A: d = _rot_comp.Reg_5A; break;
355 case 0x5C: d = _rot_comp.Reg_5C; break;
356 case 0x5E: d = _rot_comp.Reg_5E; break;
357 case 0x60: d = _rot_comp.Reg_60; break;
358 case 0x62: d = _rot_comp.Reg_62; break;
359 case 0x64: d = _rot_comp.Reg_64; break;
d1df8786 360 case 0x66: break;
fa1e5e29 361 default: dprintf("gfx_cd_read FIXME: unexpected address: %02x", a); break;
d1df8786 362 }
363
364 dprintf("gfx_cd_read(%02x) = %04x", a, d);
cb4a513a 365
89fa852d 366 return d;
cb4a513a 367}
368
eff55556 369PICO_INTERNAL_ASM void gfx_cd_write16(unsigned int a, unsigned int d)
cb4a513a 370{
5c69a605 371 dprintf("gfx_cd_write16(%x, %04x)", a, d);
cb4a513a 372
373 switch (a) {
d1df8786 374 case 0x58: // .Reg_Stamp_Size
eff55556 375 _rot_comp.Reg_58 = d & 7;
d1df8786 376 return;
377
378 case 0x5A: // .Reg_Stamp_Adr
eff55556 379 _rot_comp.Reg_5A = d & 0xffe0;
d1df8786 380 return;
381
382 case 0x5C: // .Reg_IM_VCell_Size
eff55556 383 _rot_comp.Reg_5C = d & 0x1f;
d1df8786 384 return;
385
386 case 0x5E: // .Reg_IM_Adr
eff55556 387 _rot_comp.Reg_5E = d & 0xFFF8;
d1df8786 388 return;
389
390 case 0x60: // .Reg_IM_Offset
eff55556 391 _rot_comp.Reg_60 = d & 0x3f;
d1df8786 392 return;
393
394 case 0x62: // .Reg_IM_HDot_Size
eff55556 395 _rot_comp.Reg_62 = d & 0x1ff;
d1df8786 396 return;
397
398 case 0x64: // .Reg_IM_VDot_Size
eff55556 399 _rot_comp.Reg_64 = d & 0xff; // V_Dot, must be 32bit?
d1df8786 400 return;
401
402 case 0x66: // .Reg_Vector_Adr
eff55556 403 _rot_comp.Reg_66 = d & 0xfffe;
cb4a513a 404 if (Pico_mcd->s68k_regs[3]&4) return; // can't do tanformations in 1M mode
405 gfx_cd_start();
406 return;
d1df8786 407
5c69a605 408 default: dprintf("gfx_cd_write16 FIXME: unexpected address: %02x", a); return;
cb4a513a 409 }
410}
411
d1df8786 412
eff55556 413PICO_INTERNAL void gfx_cd_reset(void)
51a902ae 414{
eff55556 415 memset(&_rot_comp.Reg_58, 0, sizeof(_rot_comp));
fa1e5e29 416}
417
418
419// --------------------------------
420
421#include "cell_map.c"
422
eff55556 423#ifndef UTYPES_DEFINED
fa1e5e29 424typedef unsigned short u16;
eff55556 425#endif
fa1e5e29 426
0a051f55 427// check: Heart of the alien, jaguar xj 220
eff55556 428PICO_INTERNAL void DmaSlowCell(unsigned int source, unsigned int a, int len, unsigned char inc)
fa1e5e29 429{
430 unsigned char *base;
431 unsigned int asrc, a2;
432 u16 *r;
433
434 base = Pico_mcd->word_ram1M[Pico_mcd->s68k_regs[3]&1];
435
436 switch (Pico.video.type)
437 {
438 case 1: // vram
439 r = Pico.vram;
440 for(; len; len--)
441 {
442 asrc = cell_map(source >> 2) << 2;
443 asrc |= source & 2;
444 // if(a&1) d=(d<<8)|(d>>8); // ??
445 r[a>>1] = *(u16 *)(base + asrc);
446 source += 2;
447 // AutoIncrement
448 a=(u16)(a+inc);
449 }
602133e1 450 rendstatus |= PDRAW_SPRITES_MOVED;
fa1e5e29 451 break;
452
453 case 3: // cram
454 Pico.m.dirtyPal = 1;
455 r = Pico.cram;
456 for(a2=a&0x7f; len; len--)
457 {
458 asrc = cell_map(source >> 2) << 2;
459 asrc |= source & 2;
460 r[a2>>1] = *(u16 *)(base + asrc);
461 source += 2;
462 // AutoIncrement
463 a2+=inc;
464 // good dest?
465 if(a2 >= 0x80) break;
466 }
467 a=(a&0xff00)|a2;
468 break;
469
470 case 5: // vsram[a&0x003f]=d;
471 r = Pico.vsram;
472 for(a2=a&0x7f; len; len--)
473 {
474 asrc = cell_map(source >> 2) << 2;
475 asrc |= source & 2;
476 r[a2>>1] = *(u16 *)(base + asrc);
477 source += 2;
478 // AutoIncrement
479 a2+=inc;
480 // good dest?
481 if(a2 >= 0x80) break;
482 }
483 a=(a&0xff00)|a2;
484 break;
485 }
486 // remember addr
487 Pico.video.addr=(u16)a;
51a902ae 488}
489