5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes/rewrites by James L. Hammons
15 //#define OP_DEBUG_BMP
17 #define BLEND_Y(dst, src) op_blend_y[(((uint16)dst<<8)) | ((uint16)(src))]
18 #define BLEND_CR(dst, src) op_blend_cr[(((uint16)dst)<<8) | ((uint16)(src))]
20 #define OBJECT_TYPE_BITMAP 0 // 000
21 #define OBJECT_TYPE_SCALE 1 // 001
22 #define OBJECT_TYPE_GPU 2 // 010
23 #define OBJECT_TYPE_BRANCH 3 // 011
24 #define OBJECT_TYPE_STOP 4 // 100
26 #define CONDITION_EQUAL 0
27 #define CONDITION_LESS_THAN 1
28 #define CONDITION_GREATER_THAN 2
29 #define CONDITION_OP_FLAG_SET 3
30 #define CONDITION_SECOND_HALF_LINE 4
32 #define OPFLAG_RELEASE 8 // Bus release bit
33 #define OPFLAG_TRANS 4 // Transparency bit
34 #define OPFLAG_RMW 2 // Read-Modify-Write bit
35 #define OPFLAG_REFLECT 1 // Horizontal mirror bit
37 // Private function prototypes
39 void OPProcessFixedBitmap(int scanline, uint64 p0, uint64 p1, bool render);
40 void OPProcessScaledBitmap(int scanline, uint64 p0, uint64 p1, uint64 p2, bool render);
41 void DumpScaledObject(uint64 p0, uint64 p1, uint64 p2);
42 void DumpFixedObject(uint64 p0, uint64 p1);
43 uint64 op_load_phrase(uint32 offset);
45 // External global variables
47 extern uint32 jaguar_mainRom_crc32;
49 // Local global variables
51 static uint8 * op_blend_y;
52 static uint8 * op_blend_cr;
53 // There may be a problem with this "RAM" overlapping (and thus being independent of)
54 // some of the regular TOM RAM...
55 static uint8 objectp_ram[0x40]; // This is based at $F00000
56 uint8 objectp_running;
57 bool objectp_stop_reading_list;
59 static uint8 op_bitmap_bit_depth[8] = { 1, 2, 4, 8, 16, 24, 32, 0 };
60 //static uint32 op_bitmap_bit_size[8] =
61 // { (uint32)(0.125*65536), (uint32)(0.25*65536), (uint32)(0.5*65536), (uint32)(1*65536),
62 // (uint32)(2*65536), (uint32)(1*65536), (uint32)(1*65536), (uint32)(1*65536) };
63 static uint32 op_pointer;
65 int32 phraseWidthToPixels[8] = { 64, 32, 16, 8, 4, 2, 0, 0 };
69 // Object Processor initialization
73 // Blend tables (64K each)
74 memory_malloc_secure((void **)&op_blend_y, 0x10000, "Jaguar Object processor Y blend lookup table");
75 memory_malloc_secure((void **)&op_blend_cr, 0x10000, "Jaguar Object processor CR blend lookup table");
77 // Here we calculate the saturating blend of a signed 4-bit value and an
78 // existing Cyan/Red value as well as a signed 8-bit value and an existing intensity...
79 // Note: CRY is 4 bits Cyan, 4 bits Red, 16 bits intensitY
80 for(int i=0; i<256*256; i++)
82 int y = (i >> 8) & 0xFF;
83 int dy = (INT8)i; // Sign extend the Y index
84 int c1 = (i >> 8) & 0x0F;
85 int dc1 = (INT8)(i << 4) >> 4; // Sign extend the R index
86 int c2 = (i >> 12) & 0x0F;
87 int dc2 = (INT8)(i & 0xF0) >> 4; // Sign extend the C index
107 op_blend_cr[i] = (c2 << 4) | c1;
114 // Object Processor reset
118 memset(objectp_ram, 0x00, 0x40);
125 { "(BITMAP)", "(SCALED BITMAP)", "(GPU INT)", "(BRANCH)", "(STOP)", "???", "???", "???" };
127 { "\"==\"", "\"<\"", "\">\"", "(opflag set)", "(second half line)", "?", "?", "?" };
129 uint32 olp = op_get_list_pointer();
130 WriteLog("OP: OLP = %08X\n", olp);
131 WriteLog("OP: Phrase dump\n ----------\n");
132 for(uint32 i=0; i<0x100; i+=8)
134 uint32 hi = jaguar_long_read(olp + i), lo = jaguar_long_read(olp + i + 4);
135 WriteLog("\t%08X: %08X %08X %s", olp + i, hi, lo, opType[lo & 0x07]);
136 if ((lo & 0x07) == 3)
138 uint16 ypos = (lo >> 3) & 0x7FF;
139 uint8 cc = (lo >> 14) & 0x03;
140 uint32 link = ((hi << 11) | (lo >> 21)) & 0x3FFFF8;
141 WriteLog(" YPOS=%u, CC=%s, link=%08X", ypos, ccType[cc], link);
144 if ((lo & 0x07) == 0)
145 DumpFixedObject(op_load_phrase(olp+i), op_load_phrase(olp+i+8));
146 if ((lo & 0x07) == 1)
147 DumpScaledObject(op_load_phrase(olp+i), op_load_phrase(olp+i+8), op_load_phrase(olp+i+16));
153 // Object Processor memory access
154 // Memory range: F00010 - F00027
156 void op_byte_write(uint32 offset, uint8 data)
159 objectp_ram[offset] = data;
162 void op_word_write(uint32 offset, uint16 data)
165 // objectp_ram[offset] = (data >> 8) & 0xFF;
166 // objectp_ram[offset+1] = data & 0xFF;
167 SET16(objectp_ram, offset, data);
169 /*if (offset == 0x20)
170 WriteLog("OP: Setting lo list pointer: %04X\n", data);
172 WriteLog("OP: Setting hi list pointer: %04X\n", data);//*/
175 uint8 op_byte_read(uint32 offset)
178 return objectp_ram[offset];
181 uint16 op_word_read(uint32 offset)
183 // return (objectp_ram[offset & 0x3F] << 8) | objectp_ram[(offset+1) & 0x3F];
185 return GET16(objectp_ram, offset);
188 // F00010-F00017 R xxxxxxxx xxxxxxxx OB - current object code from the graphics processor
189 // F00020-F00023 W xxxxxxxx xxxxxxxx OLP - start of the object list
190 // F00026 W -------- -------x OBF - object processor flag
192 uint32 op_get_list_pointer(void)
194 // Note: This register is LO / HI WORD, hence the funky look of this...
195 // return (objectp_ram[0x22] << 24) | (objectp_ram[0x23] << 16) | (objectp_ram[0x20] << 8) | objectp_ram[0x21];
196 return GET16(objectp_ram, 0x20) | (GET16(objectp_ram, 0x22) << 16);
199 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
201 uint32 op_get_status_register(void)
203 // return (objectp_ram[0x26] << 24) | (objectp_ram[0x27] << 16) | (objectp_ram[0x28] << 8) | objectp_ram[0x29];
204 // return GET32(objectp_ram, 0x26);
205 return GET16(objectp_ram, 0x26);
208 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
210 void op_set_status_register(uint32 data)
212 /* objectp_ram[0x26] = (data & 0xFF000000) >> 24;
213 objectp_ram[0x27] = (data & 0x00FF0000) >> 16;
214 objectp_ram[0x28] = (data & 0x0000FF00) >> 8;
215 objectp_ram[0x29] |= (data & 0xFE);*/
216 objectp_ram[0x26] = (data & 0x0000FF00) >> 8;
217 objectp_ram[0x27] |= (data & 0xFE);
220 void op_set_current_object(uint64 object)
222 //Not sure this is right... Wouldn't it just be stored 64 bit BE?
223 // Stored as least significant 32 bits first, ms32 last in big endian
224 /* objectp_ram[0x13] = object & 0xFF; object >>= 8;
225 objectp_ram[0x12] = object & 0xFF; object >>= 8;
226 objectp_ram[0x11] = object & 0xFF; object >>= 8;
227 objectp_ram[0x10] = object & 0xFF; object >>= 8;
229 objectp_ram[0x17] = object & 0xFF; object >>= 8;
230 objectp_ram[0x16] = object & 0xFF; object >>= 8;
231 objectp_ram[0x15] = object & 0xFF; object >>= 8;
232 objectp_ram[0x14] = object & 0xFF;*/
233 // Let's try regular good old big endian...
234 objectp_ram[0x17] = object & 0xFF; object >>= 8;
235 objectp_ram[0x16] = object & 0xFF; object >>= 8;
236 objectp_ram[0x15] = object & 0xFF; object >>= 8;
237 objectp_ram[0x14] = object & 0xFF; object >>= 8;
239 objectp_ram[0x13] = object & 0xFF; object >>= 8;
240 objectp_ram[0x12] = object & 0xFF; object >>= 8;
241 objectp_ram[0x11] = object & 0xFF; object >>= 8;
242 objectp_ram[0x10] = object & 0xFF;
245 uint64 op_load_phrase(uint32 offset)
247 offset &= ~0x07; // 8 byte alignment
248 return ((uint64)jaguar_long_read(offset) << 32) | (uint64)jaguar_long_read(offset+4);
251 void OPStorePhrase(uint32 offset, uint64 p)
253 offset &= ~0x07; // 8 byte alignment
254 jaguar_long_write(offset, p >> 32);
255 jaguar_long_write(offset + 4, p & 0xFFFFFFFF);
259 // Debugging routines
261 void DumpScaledObject(uint64 p0, uint64 p1, uint64 p2)
263 WriteLog(" (SCALED BITMAP)");
264 WriteLog(" %08X --> phrase %08X %08X\n", op_pointer, (uint32)(p1>>32), (uint32)(p1&0xFFFFFFFF));
265 WriteLog(" %08X --> phrase %08X %08X ", op_pointer+8, (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
266 uint8 bitdepth = (p1 >> 12) & 0x07;
267 int16 ypos = ((p0 >> 3) & 0x3FF); // ??? What if not interlaced (/2)?
268 int32 xpos = p1 & 0xFFF;
269 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
270 uint32 iwidth = ((p1 >> 28) & 0x3FF);
271 uint32 dwidth = ((p1 >> 18) & 0x3FF); // Unsigned!
272 uint16 height = ((p0 >> 14) & 0x3FF);
273 uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
274 uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
275 uint32 firstPix = (p1 >> 49) & 0x3F;
276 uint8 flags = (p1 >> 45) & 0x0F;
277 uint8 idx = (p1 >> 38) & 0x7F;
278 uint32 pitch = (p1 >> 15) & 0x07;
279 WriteLog("\n [%u (%u) x %u @ (%i, %u) (%u bpp), l: %08X, p: %08X fp: %02X, fl:%s%s%s%s, idx:%02X, pt:%02X]\n",
280 iwidth, dwidth, height, xpos, ypos, op_bitmap_bit_depth[bitdepth], link, ptr, firstPix, (flags&OPFLAG_REFLECT ? "REFLECT " : ""), (flags&OPFLAG_RMW ? "RMW " : ""), (flags&OPFLAG_TRANS ? "TRANS " : ""), (flags&OPFLAG_RELEASE ? "RELEASE" : ""), idx, pitch);
281 uint32 hscale = p2 & 0xFF;
282 uint32 vscale = (p2 >> 8) & 0xFF;
283 uint32 remainder = (p2 >> 16) & 0xFF;
284 WriteLog(" [hsc: %02X, vsc: %02X, rem: %02X]\n", hscale, vscale, remainder);
287 void DumpFixedObject(uint64 p0, uint64 p1)
289 WriteLog(" (BITMAP)");
290 WriteLog(" %08X --> phrase %08X %08X\n", op_pointer, (uint32)(p1>>32), (uint32)(p1&0xFFFFFFFF));
291 uint8 bitdepth = (p1 >> 12) & 0x07;
292 int16 ypos = ((p0 >> 3) & 0x3FF); // ??? What if not interlaced (/2)?
293 int32 xpos = p1 & 0xFFF;
294 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
295 uint32 iwidth = ((p1 >> 28) & 0x3FF);
296 uint32 dwidth = ((p1 >> 18) & 0x3FF); // Unsigned!
297 uint16 height = ((p0 >> 14) & 0x3FF);
298 uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
299 uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
300 uint32 firstPix = (p1 >> 49) & 0x3F;
301 uint8 flags = (p1 >> 45) & 0x0F;
302 uint8 idx = (p1 >> 38) & 0x7F;
303 uint32 pitch = (p1 >> 15) & 0x07;
304 WriteLog(" [%u (%u) x %u @ (%i, %u) (%u bpp), l: %08X, p: %08X fp: %02X, fl:%s%s%s%s, idx:%02X, pt:%02X]\n",
305 iwidth, dwidth, height, xpos, ypos, op_bitmap_bit_depth[bitdepth], link, ptr, firstPix, (flags&OPFLAG_REFLECT ? "REFLECT " : ""), (flags&OPFLAG_RMW ? "RMW " : ""), (flags&OPFLAG_TRANS ? "TRANS " : ""), (flags&OPFLAG_RELEASE ? "RELEASE" : ""), idx, pitch);
309 // Object Processor main routine
311 void OPProcessList(int scanline, bool render)
313 extern int op_start_log;
314 // char * condition_to_str[8] =
315 // { "==", "<", ">", "(opflag set)", "(second half line)", "?", "?", "?" };
317 // If jaguar_exec() is working right, we should *never* have to check for this
319 /* if (scanline < tom_get_vdb())
322 if (scanline >= 525)//tom_getVideoModeHeight()+tom_get_vdb())
325 op_pointer = op_get_list_pointer();
327 objectp_stop_reading_list = false;
329 // *** BEGIN OP PROCESSOR TESTING ONLY ***
330 extern bool interactiveMode;
332 extern int objectPtr;
334 int bitmapCounter = 0;
335 // *** END OP PROCESSOR TESTING ONLY ***
337 // if (op_pointer) WriteLog(" new op list at 0x%.8x scanline %i\n",op_pointer,scanline);
340 // *** BEGIN OP PROCESSOR TESTING ONLY ***
341 if (interactiveMode && bitmapCounter == objectPtr)
345 // *** END OP PROCESSOR TESTING ONLY ***
346 if (objectp_stop_reading_list)
349 uint64 p0 = op_load_phrase(op_pointer);
351 if (scanline == tom_get_vdb() && op_start_log)
352 //if (scanline == 215 && op_start_log)
354 WriteLog("%08X --> phrase %08X %08X", op_pointer - 8, (int)(p0>>32), (int)(p0&0xFFFFFFFF));
355 if ((p0 & 0x07) == OBJECT_TYPE_BITMAP)
357 WriteLog(" (BITMAP) ");
358 uint64 p1 = op_load_phrase(op_pointer);
359 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer, (int)(p1>>32), (int)(p1&0xFFFFFFFF));
360 uint8 bitdepth = (p1 >> 12) & 0x07;
361 int16 ypos = ((p0 >> 3) & 0x3FF); // ??? What if not interlaced (/2)?
362 int32 xpos = p1 & 0xFFF;
363 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
364 uint32 iwidth = ((p1 >> 28) & 0x3FF);
365 uint32 dwidth = ((p1 >> 18) & 0x3FF); // Unsigned!
366 uint16 height = ((p0 >> 14) & 0x3FF);
367 uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
368 uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
369 uint32 firstPix = (p1 >> 49) & 0x3F;
370 uint8 flags = (p1 >> 45) & 0x0F;
371 uint8 idx = (p1 >> 38) & 0x7F;
372 uint32 pitch = (p1 >> 15) & 0x07;
373 WriteLog("\n [%u (%u) x %u @ (%i, %u) (%u bpp), l: %08X, p: %08X fp: %02X, fl:%s%s%s%s, idx:%02X, pt:%02X]\n",
374 iwidth, dwidth, height, xpos, ypos, op_bitmap_bit_depth[bitdepth], link, ptr, firstPix, (flags&OPFLAG_REFLECT ? "REFLECT " : ""), (flags&OPFLAG_RMW ? "RMW " : ""), (flags&OPFLAG_TRANS ? "TRANS " : ""), (flags&OPFLAG_RELEASE ? "RELEASE" : ""), idx, pitch);
376 if ((p0 & 0x07) == OBJECT_TYPE_SCALE)
378 WriteLog(" (SCALED BITMAP)");
379 uint64 p1 = op_load_phrase(op_pointer), p2 = op_load_phrase(op_pointer+8);
380 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer, (int)(p1>>32), (int)(p1&0xFFFFFFFF));
381 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer+8, (int)(p2>>32), (int)(p2&0xFFFFFFFF));
382 uint8 bitdepth = (p1 >> 12) & 0x07;
383 int16 ypos = ((p0 >> 3) & 0x3FF); // ??? What if not interlaced (/2)?
384 int32 xpos = p1 & 0xFFF;
385 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
386 uint32 iwidth = ((p1 >> 28) & 0x3FF);
387 uint32 dwidth = ((p1 >> 18) & 0x3FF); // Unsigned!
388 uint16 height = ((p0 >> 14) & 0x3FF);
389 uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
390 uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
391 uint32 firstPix = (p1 >> 49) & 0x3F;
392 uint8 flags = (p1 >> 45) & 0x0F;
393 uint8 idx = (p1 >> 38) & 0x7F;
394 uint32 pitch = (p1 >> 15) & 0x07;
395 WriteLog("\n [%u (%u) x %u @ (%i, %u) (%u bpp), l: %08X, p: %08X fp: %02X, fl:%s%s%s%s, idx:%02X, pt:%02X]\n",
396 iwidth, dwidth, height, xpos, ypos, op_bitmap_bit_depth[bitdepth], link, ptr, firstPix, (flags&OPFLAG_REFLECT ? "REFLECT " : ""), (flags&OPFLAG_RMW ? "RMW " : ""), (flags&OPFLAG_TRANS ? "TRANS " : ""), (flags&OPFLAG_RELEASE ? "RELEASE" : ""), idx, pitch);
397 uint32 hscale = p2 & 0xFF;
398 uint32 vscale = (p2 >> 8) & 0xFF;
399 uint32 remainder = (p2 >> 16) & 0xFF;
400 WriteLog(" [hsc: %02X, vsc: %02X, rem: %02X]\n", hscale, vscale, remainder);
402 if ((p0 & 0x07) == OBJECT_TYPE_GPU)
403 WriteLog(" (GPU)\n");
404 if ((p0 & 0x07) == OBJECT_TYPE_BRANCH)
406 WriteLog(" (BRANCH)\n");
407 uint8 * jaguar_mainRam = GetRamPtr();
408 WriteLog("[RAM] --> ");
409 for(int k=0; k<8; k++)
410 WriteLog("%02X ", jaguar_mainRam[op_pointer-8 + k]);
413 if ((p0 & 0x07) == OBJECT_TYPE_STOP)
414 WriteLog(" --> List end\n");
417 // WriteLog("%08X type %i\n", op_pointer, (uint8)p0 & 0x07);
418 switch ((uint8)p0 & 0x07)
420 case OBJECT_TYPE_BITMAP:
422 // Would *not* be /2 if interlaced...!
423 uint16 ypos = ((p0 >> 3) & 0x3FF) / 2;
424 // This is only theory implied by Rayman...!
425 // It seems that if the YPOS is zero, then bump the YPOS value so that it coincides with
426 // the VDB value. With interlacing, this would be slightly more tricky.
427 // There's probably another bit somewhere that enables this mode--but so far, doesn't seem
428 // to affect any other game in a negative way (that I've seen).
429 // Either that, or it's an undocumented bug...
431 //No, the reason this was needed is that the OP code before was wrong. Any value
432 //less than VDB will get written to the top line of the display!
434 // ypos = tom_word_read(0xF00046) / 2; // Get the VDB value
435 uint32 height = (p0 & 0xFFC000) >> 14;
436 uint32 oldOPP = op_pointer - 8;
437 // *** BEGIN OP PROCESSOR TESTING ONLY ***
438 if (inhibit && op_start_log)
439 WriteLog("!!! ^^^ This object is INHIBITED! ^^^ !!!\n");
441 if (!inhibit) // For OP testing only!
442 // *** END OP PROCESSOR TESTING ONLY ***
443 if (scanline >= ypos && height > 0)
445 uint64 p1 = op_load_phrase(op_pointer);
447 //WriteLog("OP: Writing scanline %d with ypos == %d...\n", scanline, ypos);
448 //WriteLog("--> Writing %u BPP bitmap...\n", op_bitmap_bit_depth[(p1 >> 12) & 0x07]);
449 OPProcessFixedBitmap(scanline, p0, p1, render);
453 //???Does this really happen??? Doesn't seem to work if you do this...!
454 // uint32 link = (p0 & 0x7FFFF000000) >> 21;
455 // SET16(objectp_ram, 0x20, link & 0xFFFF); // OLP
456 // SET16(objectp_ram, 0x22, link >> 16);
457 /* uint32 height = (p0 & 0xFFC000) >> 14;
460 // NOTE: Would subtract 2 if in interlaced mode...!
461 // uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
465 uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
466 uint64 dwidth = (p1 & 0xFFC0000) >> 15;
469 p0 &= ~0xFFFFF80000FFC000; // Mask out old data...
470 p0 |= (uint64)height << 14;
472 OPStorePhrase(oldOPP, p0);
474 op_pointer = (p0 & 0x000007FFFF000000) >> 21;
477 case OBJECT_TYPE_SCALE:
479 // Would *not* be /2 if interlaced...!
480 uint16 ypos = ((p0 >> 3) & 0x3FF) / 2;
481 // This is only theory implied by Rayman...!
482 // It seems that if the YPOS is zero, then bump the YPOS value so that it coincides with
483 // the VDB value. With interlacing, this would be slightly more tricky.
484 // There's probably another bit somewhere that enables this mode--but so far, doesn't seem
485 // to affect any other game in a negative way (that I've seen).
486 // Either that, or it's an undocumented bug...
488 //No, the reason this was needed is that the OP code before was wrong. Any value
489 //less than VDB will get written to the top line of the display!
491 // ypos = tom_word_read(0xF00046) / 2; // Get the VDB value
492 uint32 height = (p0 & 0xFFC000) >> 14;
493 uint32 oldOPP = op_pointer - 8;
494 // *** BEGIN OP PROCESSOR TESTING ONLY ***
495 if (inhibit && op_start_log)
497 WriteLog("!!! ^^^ This object is INHIBITED! ^^^ !!! (scanline=%u, ypos=%u, height=%u)\n", scanline, ypos, height);
498 DumpScaledObject(p0, op_load_phrase(op_pointer), op_load_phrase(op_pointer+8));
501 if (!inhibit) // For OP testing only!
502 // *** END OP PROCESSOR TESTING ONLY ***
503 if (scanline >= ypos && height > 0)
505 uint64 p1 = op_load_phrase(op_pointer);
507 uint64 p2 = op_load_phrase(op_pointer);
509 //WriteLog("OP: %08X (%d) %08X%08X %08X%08X %08X%08X\n", oldOPP, scanline, (uint32)(p0>>32), (uint32)(p0&0xFFFFFFFF), (uint32)(p1>>32), (uint32)(p1&0xFFFFFFFF), (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
510 OPProcessScaledBitmap(scanline, p0, p1, p2, render);
514 //???Does this really happen??? Doesn't seem to work if you do this...!
515 // uint32 link = (p0 & 0x7FFFF000000) >> 21;
516 // SET16(objectp_ram, 0x20, link & 0xFFFF); // OLP
517 // SET16(objectp_ram, 0x22, link >> 16);
518 /* uint32 height = (p0 & 0xFFC000) >> 14;
521 // NOTE: Would subtract 2 if in interlaced mode...!
522 // uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
524 uint8 remainder = p2 >> 16, vscale = p2 >> 8;
525 //Actually, we should skip this object if it has a vscale of zero.
526 //Or do we? Not sure... Atari Karts has a few lines that look like:
528 //000E8268 --> phrase 00010000 7000B00D
529 // [7 (0) x 1 @ (13, 0) (8 bpp), l: 000E82A0, p: 000E0FC0 fp: 00, fl:RELEASE, idx:00, pt:01]
530 // [hsc: 9A, vsc: 00, rem: 00]
531 // Could it be the vscale is overridden if the DWIDTH is zero? Hmm...
534 vscale = 0x20; // OP bug??? Nope, it isn't...! Or is it?
536 remainder -= 0x20; // 1.0f in [3.5] fixed point format
537 if (remainder & 0x80) // I.e., it's negative
539 uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
540 uint64 dwidth = (p1 & 0xFFC0000) >> 15;
542 while (remainder & 0x80)
550 p0 &= ~0xFFFFF80000FFC000; // Mask out old data...
551 p0 |= (uint64)height << 14;
553 OPStorePhrase(oldOPP, p0);
556 //WriteLog(" [%08X%08X -> ", (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
557 p2 &= ~0x0000000000FF0000;
558 p2 |= (uint64)remainder << 16;
559 //WriteLog("%08X%08X]\n", (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
560 OPStorePhrase(oldOPP+16, p2);
561 //remainder = (uint8)(p2 >> 16), vscale = (uint8)(p2 >> 8);
562 //WriteLog(" [after]: rem=%02X, vscale=%02X\n", remainder, vscale);
564 op_pointer = (p0 & 0x000007FFFF000000) >> 21;
567 case OBJECT_TYPE_GPU:
569 //WriteLog("OP: Asserting GPU IRQ #3...\n");
570 op_set_current_object(p0);
571 GPUSetIRQLine(3, ASSERT_LINE);
572 //Also, OP processing is suspended from this point until OBF (F00026) is written to...
575 //OPSuspendedByGPU = true;
576 //Dunno if the OP keeps processing from where it was interrupted, or if it just continues
577 //on the next scanline...
580 case OBJECT_TYPE_BRANCH:
582 uint16 ypos = (p0 >> 3) & 0x7FF;
583 uint8 cc = (p0 >> 14) & 0x03;
584 uint32 link = (p0 >> 21) & 0x3FFFF8;
586 // if ((ypos!=507)&&(ypos!=25))
587 // WriteLog("\t%i%s%i link=0x%.8x\n",scanline,condition_to_str[cc],ypos>>1,link);
590 case CONDITION_EQUAL:
591 //Why do this for the equal case? If they wrote an odd YPOS, then it wouldn't be detected!
592 // if (ypos != 0x7FF && (ypos & 0x01))
594 // if ((2 * tom_get_scanline()) == ypos || ypos == 0x7FF)
595 //Here we're using VC instead of the bogus tom_get_scanline() value...
596 if (tom_word_read(0xF00006) == ypos || ypos == 0x7FF)
599 case CONDITION_LESS_THAN:
600 // if ((2 * tom_get_scanline()) < ypos)
601 if (tom_word_read(0xF00006) < ypos)
604 case CONDITION_GREATER_THAN:
605 // if ((2 * tom_get_scanline()) > ypos)
606 if (tom_word_read(0xF00006) > ypos)
609 case CONDITION_OP_FLAG_SET:
610 if (op_get_status_register() & 0x01)
613 case CONDITION_SECOND_HALF_LINE:
614 // This basically means branch if bit 10 of HC is set
615 WriteLog("OP: Unexpected CONDITION_SECOND_HALF_LINE in BRANCH object\nOP: shuting down\n");
620 WriteLog("OP: Unimplemented branch condition %i\n", cc);
624 case OBJECT_TYPE_STOP:
628 //WriteLog("OP: --> STOP\n");
629 // op_set_status_register(((p0>>3) & 0xFFFFFFFF));
630 //This seems more likely...
631 op_set_current_object(p0);
635 tom_set_pending_object_int();
636 if (tom_irq_enabled(IRQ_OPFLAG) && jaguar_interrupt_handler_is_valid(64))
637 m68k_set_irq(7); // Cause an NMI to occur...
644 WriteLog("op: unknown object type %i\n", ((uint8)p0 & 0x07));
651 // Store fixed size bitmap in line buffer
654 // Interesting thing about Rayman: There seems to be a transparent bitmap (1/8/16 bpp--which?)
655 // being rendered under his feet--doesn't align when walking... Check it out!
657 void OPProcessFixedBitmap(int scanline, uint64 p0, uint64 p1, bool render)
659 // Need to make sure that when writing that it stays within the line buffer...
660 // LBUF ($F01800 - $F01D9E) 360 x 32-bit RAM
661 uint8 depth = (p1 >> 12) & 0x07; // Color depth of image
662 int32 xpos = ((int16)((p1 << 4) & 0xFFFF)) >> 4;// Image xpos in LBUF
663 uint32 iwidth = (p1 >> 28) & 0x3FF; // Image width in *phrases*
664 uint32 data = (p0 >> 40) & 0xFFFFF8; // Pixel data address
665 //#ifdef OP_DEBUG_BMP
666 // Prolly should use this... Though not sure exactly how.
667 uint32 firstPix = (p1 >> 49) & 0x3F;
668 // "The LSB is significant only for scaled objects..." -JTRM
669 // "In 1 BPP mode, all five bits are significant. In 2 BPP mode, the top four are significant..."
672 // We can ignore the RELEASE (high order) bit for now--probably forever...!
673 // uint8 flags = (p1 >> 45) & 0x0F; // REFLECT, RMW, TRANS, RELEASE
674 //Optimize: break these out to their own BOOL values
675 uint8 flags = (p1 >> 45) & 0x07; // REFLECT (0), RMW (1), TRANS (2)
676 bool flagREFLECT = (flags & OPFLAG_REFLECT ? true : false),
677 flagRMW = (flags & OPFLAG_RMW ? true : false),
678 flagTRANS = (flags & OPFLAG_TRANS ? true : false);
679 // "For images with 1 to 4 bits/pixel the top 7 to 4 bits of the index
680 // provide the most significant bits of the palette address."
681 uint8 index = (p1 >> 37) & 0xFE; // CLUT index offset (upper pix, 1-4 bpp)
682 uint32 pitch = (p1 >> 15) & 0x07; // Phrase pitch
684 // int16 scanlineWidth = tom_getVideoModeWidth();
685 uint8 * tom_ram_8 = tom_get_ram_pointer();
686 uint8 * paletteRAM = &tom_ram_8[0x400];
687 // This is OK as long as it's used correctly: For 16-bit RAM to RAM direct copies--NOT
688 // for use when using endian-corrected data (i.e., any of the *_word_read functions!)
689 uint16 * paletteRAM16 = (uint16 *)paletteRAM;
691 // WriteLog("bitmap %ix? %ibpp at %i,? firstpix=? data=0x%.8x pitch %i hflipped=%s dwidth=? (linked to ?) RMW=%s Tranparent=%s\n",
692 // iwidth, op_bitmap_bit_depth[bitdepth], xpos, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), (flags&OPFLAG_RMW ? "yes" : "no"), (flags&OPFLAG_TRANS ? "yes" : "no"));
694 // Is it OK to have a 0 for the data width??? (i.e., undocumented?)
695 // Seems to be... Seems that dwidth *can* be zero (i.e., reuse same line) as well.
696 // Pitch == 0 is OK too...
697 // if (!render || op_pointer == 0 || dwidth == 0 || ptr == 0 || pitch == 0)
698 //I'm not convinced that we need to concern ourselves with data & op_pointer here either!
699 if (!render || iwidth == 0) // || data == 0 || op_pointer == 0)
702 //#define OP_DEBUG_BMP
703 //#ifdef OP_DEBUG_BMP
704 // WriteLog("bitmap %ix%i %ibpp at %i,%i firstpix=%i data=0x%.8x pitch %i hflipped=%s dwidth=%i (linked to 0x%.8x) Transluency=%s\n",
705 // iwidth, height, op_bitmap_bit_depth[bitdepth], xpos, ypos, firstPix, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), dwidth, op_pointer, (flags&OPFLAG_RMW ? "yes" : "no"));
708 // int32 leftMargin = xpos, rightMargin = (xpos + (phraseWidthToPixels[depth] * iwidth)) - 1;
709 int32 startPos = xpos, endPos = xpos +
710 (!flagREFLECT ? (phraseWidthToPixels[depth] * iwidth) - 1
711 : -((phraseWidthToPixels[depth] * iwidth) + 1));
712 uint32 clippedWidth = 0, phraseClippedWidth = 0, dataClippedWidth = 0;//, phrasePixel = 0;
713 bool in24BPPMode = (((GET16(tom_ram_8, 0x0028) >> 1) & 0x03) == 1 ? true : false); // VMODE
714 // Not sure if this is Jaguar Two only location or what...
715 // From the docs, it is... If we want to limit here we should think of something else.
716 // int32 limit = GET16(tom_ram_8, 0x0008); // LIMIT
718 int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
720 // If the image is completely to the left or right of the line buffer, then bail.
721 //If in REFLECT mode, then these values are swapped! !!! FIX !!! [DONE]
722 //There are four possibilities:
723 // 1. image sits on left edge and no REFLECT; starts out of bounds but ends in bounds.
724 // 2. image sits on left edge and REFLECT; starts in bounds but ends out of bounds.
725 // 3. image sits on right edge and REFLECT; starts out of bounds but ends in bounds.
726 // 4. image sits on right edge and no REFLECT; starts in bounds but ends out of bounds.
727 //Numbers 2 & 4 can be caught by checking the LBUF clip while in the inner loop,
728 // numbers 1 & 3 are of concern.
729 // This *indirectly* handles only cases 2 & 4! And is WRONG is REFLECT is set...!
730 // if (rightMargin < 0 || leftMargin > lbufWidth)
732 // It might be easier to swap these (if REFLECTed) and just use XPOS down below...
733 // That way, you could simply set XPOS to leftMargin if !REFLECT and to rightMargin otherwise.
734 // Still have to be careful with the DATA and IWIDTH values though...
736 // if ((!flagREFLECT && (rightMargin < 0 || leftMargin > lbufWidth))
737 // || (flagREFLECT && (leftMargin < 0 || rightMargin > lbufWidth)))
739 if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
740 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
743 // Otherwise, find the clip limits and clip the phrase as well...
744 // NOTE: I'm fudging here by letting the actual blit overstep the bounds of the
745 // line buffer, but it shouldn't matter since there are two unused line
746 // buffers below and nothing above and I'll at most write 8 bytes outside
747 // the line buffer... I could use a fractional clip begin/end value, but
748 // this makes the blit a *lot* more hairy. I might fix this in the future
749 // if it becomes necessary. (JLH)
750 // Probably wouldn't be *that* hairy. Just use a delta that tells the inner loop
751 // which pixel in the phrase is being written, and quit when either end of phrases
752 // is reached or line buffer extents are surpassed.
754 //This stuff is probably wrong as well... !!! FIX !!!
755 //The strange thing is that it seems to work, but that's no guarantee that it's bulletproof!
756 //Yup. Seems that JagMania doesn't work correctly with this...
757 //Dunno if this is the problem, but Atari Karts is showing *some* of the road now...
762 clippedWidth = 0 - leftMargin,
763 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
764 leftMargin = 0 - (clippedWidth % phraseWidthToPixels[depth]);
767 if (rightMargin > lbufWidth)
768 clippedWidth = rightMargin - lbufWidth,
769 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];//,
770 // rightMargin = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);
771 // rightMargin = lbufWidth;
774 WriteLog("We're about to encounter a divide by zero error!\n");
775 // NOTE: We're just using endPos to figure out how much, if any, to clip by.
776 // ALSO: There may be another case where we start out of bounds and end out of bounds...!
777 if (startPos < 0) // Case #1: Begin out, end in, L to R
778 clippedWidth = 0 - startPos,
779 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
780 startPos = 0 - (clippedWidth % phraseWidthToPixels[depth]);
782 if (endPos < 0) // Case #2: Begin in, end out, R to L
783 clippedWidth = 0 - endPos,
784 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
786 if (endPos > lbufWidth) // Case #3: Begin in, end out, L to R
787 clippedWidth = endPos - lbufWidth,
788 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
790 if (startPos > lbufWidth) // Case #4: Begin out, end in, R to L
791 clippedWidth = startPos - lbufWidth,
792 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
793 startPos = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);
795 // If the image is sitting on the line buffer left or right edge, we need to compensate
796 // by decreasing the image phrase width accordingly.
797 iwidth -= phraseClippedWidth;
799 // Also, if we're clipping the phrase we need to make sure we're in the correct part of
801 // data += phraseClippedWidth * (pitch << 3);
802 data += dataClippedWidth * (pitch << 3);
804 // NOTE: When the bitmap is in REFLECT mode, the XPOS marks the *right* side of the
805 // bitmap! This makes clipping & etc. MUCH, much easier...!
806 // uint32 lbufAddress = 0x1800 + (!in24BPPMode ? leftMargin * 2 : leftMargin * 4);
807 uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
808 uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
812 // Hmm. We check above for 24 BPP mode, but don't do anything about it below...
813 // If we *were* in 24 BPP mode, how would you convert CRY to RGB24? Seems to me
814 // that if you're in CRY mode then you wouldn't be able to use 24 BPP bitmaps
817 if (depth == 0) // 1 BPP
819 // The LSB of flags is OPFLAG_REFLECT, so sign extend it and or 2 into it.
820 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
822 // Fetch 1st phrase...
823 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
824 //Note that firstPix should only be honored *if* we start with the 1st phrase of the bitmap
825 //i.e., we didn't clip on the margin...
826 pixels <<= firstPix; // Skip first N pixels (N=firstPix)...
827 int i = firstPix; // Start counter at right spot...
833 uint8 bit = pixels >> 63;
834 if (flagTRANS && bit == 0)
839 //Optimize: Set palleteRAM16 to beginning of palette RAM + index*2 and use only [bit] as index...
840 //Won't optimize RMW case though...
841 // This is the *only* correct use of endian-dependent code
842 // (i.e., mem-to-mem direct copying)!
843 *(uint16 *)currentLineBuffer = paletteRAM16[index | bit];
846 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bit) << 1]),
847 *(currentLineBuffer + 1) =
848 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bit) << 1) + 1]);
851 currentLineBuffer += lbufDelta;
855 // Fetch next phrase...
856 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
857 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
860 else if (depth == 1) // 2 BPP
863 WriteLog("OP: Fixed bitmap @ 2 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
864 index &= 0xFC; // Top six bits form CLUT index
865 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
866 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
871 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
872 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
874 for(int i=0; i<32; i++)
876 uint8 bits = pixels >> 62;
877 // Seems to me that both of these are in the same endian, so we could cast it as
878 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
879 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
880 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
881 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
882 if (flagTRANS && bits == 0)
887 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
890 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
891 *(currentLineBuffer + 1) =
892 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
895 currentLineBuffer += lbufDelta;
900 else if (depth == 2) // 4 BPP
903 WriteLog("OP: Fixed bitmap @ 4 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
904 index &= 0xF0; // Top four bits form CLUT index
905 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
906 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
911 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
912 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
914 for(int i=0; i<16; i++)
916 uint8 bits = pixels >> 60;
917 // Seems to me that both of these are in the same endian, so we could cast it as
918 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
919 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
920 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
921 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
922 if (flagTRANS && bits == 0)
927 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
930 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
931 *(currentLineBuffer + 1) =
932 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
935 currentLineBuffer += lbufDelta;
940 else if (depth == 3) // 8 BPP
943 WriteLog("OP: Fixed bitmap @ 8 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
944 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
945 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
950 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
951 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
953 for(int i=0; i<8; i++)
955 uint8 bits = pixels >> 56;
956 // Seems to me that both of these are in the same endian, so we could cast it as
957 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
958 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
959 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
960 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
961 if (flagTRANS && bits == 0)
966 *(uint16 *)currentLineBuffer = paletteRAM16[bits];
969 BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
970 *(currentLineBuffer + 1) =
971 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
974 currentLineBuffer += lbufDelta;
979 else if (depth == 4) // 16 BPP
982 WriteLog("OP: Fixed bitmap @ 16 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
983 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
984 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
989 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
990 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
992 for(int i=0; i<4; i++)
994 uint8 bitsHi = pixels >> 56, bitsLo = pixels >> 48;
995 // Seems to me that both of these are in the same endian, so we could cast it as
996 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
997 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
998 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
999 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
1000 if (flagTRANS && (bitsLo | bitsHi) == 0)
1005 *currentLineBuffer = bitsHi,
1006 *(currentLineBuffer + 1) = bitsLo;
1008 *currentLineBuffer =
1009 BLEND_CR(*currentLineBuffer, bitsHi),
1010 *(currentLineBuffer + 1) =
1011 BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1014 currentLineBuffer += lbufDelta;
1019 else if (depth == 5) // 24 BPP
1021 //Looks like Iron Soldier is the only game that uses 24BPP mode...
1022 //There *might* be others...
1023 //WriteLog("OP: Writing 24 BPP bitmap!\n");
1025 WriteLog("OP: Fixed bitmap @ 24 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
1026 // Not sure, but I think RMW only works with 16 BPP and below, and only in CRY mode...
1027 // The LSB is OPFLAG_REFLECT, so sign extend it and or 4 into it.
1028 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 4) | 0x04;
1033 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1034 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
1036 for(int i=0; i<2; i++)
1038 uint8 bits3 = pixels >> 56, bits2 = pixels >> 48,
1039 bits1 = pixels >> 40, bits0 = pixels >> 32;
1040 // Seems to me that both of these are in the same endian, so we could cast it as
1041 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
1042 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
1043 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
1044 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
1045 if (flagTRANS && (bits3 | bits2 | bits1 | bits0) == 0)
1048 *currentLineBuffer = bits3,
1049 *(currentLineBuffer + 1) = bits2,
1050 *(currentLineBuffer + 2) = bits1,
1051 *(currentLineBuffer + 3) = bits0;
1053 currentLineBuffer += lbufDelta;
1061 // Store scaled bitmap in line buffer
1063 void OPProcessScaledBitmap(int scanline, uint64 p0, uint64 p1, uint64 p2, bool render)
1065 // Need to make sure that when writing that it stays within the line buffer...
1066 // LBUF ($F01800 - $F01D9E) 360 x 32-bit RAM
1067 uint8 depth = (p1 >> 12) & 0x07; // Color depth of image
1068 int32 xpos = ((int16)((p1 << 4) & 0xFFFF)) >> 4;// Image xpos in LBUF
1069 uint32 iwidth = (p1 >> 28) & 0x3FF; // Image width in *phrases*
1070 uint32 data = (p0 >> 40) & 0xFFFFF8; // Pixel data address
1071 //#ifdef OP_DEBUG_BMP
1072 // Prolly should use this... Though not sure exactly how.
1073 //Use the upper bits as an offset into the phrase depending on the BPP. That's how!
1074 uint32 firstPix = (p1 >> 49) & 0x3F;
1075 //This is WEIRD! I'm sure I saw Atari Karts request 8 BPP FIRSTPIX! What happened???
1077 WriteLog("OP: FIRSTPIX != 0!\n");
1079 // We can ignore the RELEASE (high order) bit for now--probably forever...!
1080 // uint8 flags = (p1 >> 45) & 0x0F; // REFLECT, RMW, TRANS, RELEASE
1081 //Optimize: break these out to their own BOOL values
1082 uint8 flags = (p1 >> 45) & 0x07; // REFLECT (0), RMW (1), TRANS (2)
1083 bool flagREFLECT = (flags & OPFLAG_REFLECT ? true : false),
1084 flagRMW = (flags & OPFLAG_RMW ? true : false),
1085 flagTRANS = (flags & OPFLAG_TRANS ? true : false);
1086 uint8 index = (p1 >> 37) & 0xFE; // CLUT index offset (upper pix, 1-4 bpp)
1087 uint32 pitch = (p1 >> 15) & 0x07; // Phrase pitch
1089 // int16 scanlineWidth = tom_getVideoModeWidth();
1090 uint8 * tom_ram_8 = tom_get_ram_pointer();
1091 uint8 * paletteRAM = &tom_ram_8[0x400];
1092 // This is OK as long as it's used correctly: For 16-bit RAM to RAM direct copies--NOT
1093 // for use when using endian-corrected data (i.e., any of the *_word_read functions!)
1094 uint16 * paletteRAM16 = (uint16 *)paletteRAM;
1096 uint8 hscale = p2 & 0xFF;
1097 uint8 horizontalRemainder = hscale; // Not sure if it starts full, but seems reasonable
1098 int32 scaledWidthInPixels = (iwidth * phraseWidthToPixels[depth] * hscale) >> 5;
1099 uint32 scaledPhrasePixels = (phraseWidthToPixels[depth] * hscale) >> 5;
1101 // WriteLog("bitmap %ix? %ibpp at %i,? firstpix=? data=0x%.8x pitch %i hflipped=%s dwidth=? (linked to ?) RMW=%s Tranparent=%s\n",
1102 // iwidth, op_bitmap_bit_depth[bitdepth], xpos, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), (flags&OPFLAG_RMW ? "yes" : "no"), (flags&OPFLAG_TRANS ? "yes" : "no"));
1104 //Looks like an hscale of zero means don't draw!
1105 if (!render || iwidth == 0 || hscale == 0)
1108 //#define OP_DEBUG_BMP
1109 //#ifdef OP_DEBUG_BMP
1110 // WriteLog("bitmap %ix%i %ibpp at %i,%i firstpix=%i data=0x%.8x pitch %i hflipped=%s dwidth=%i (linked to 0x%.8x) Transluency=%s\n",
1111 // iwidth, height, op_bitmap_bit_depth[bitdepth], xpos, ypos, firstPix, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), dwidth, op_pointer, (flags&OPFLAG_RMW ? "yes" : "no"));
1114 int32 startPos = xpos, endPos = xpos +
1115 (!flagREFLECT ? scaledWidthInPixels - 1 : -(scaledWidthInPixels + 1));
1116 uint32 clippedWidth = 0, phraseClippedWidth = 0, dataClippedWidth = 0;
1117 bool in24BPPMode = (((GET16(tom_ram_8, 0x0028) >> 1) & 0x03) == 1 ? true : false); // VMODE
1118 // Not sure if this is Jaguar Two only location or what...
1119 // From the docs, it is... If we want to limit here we should think of something else.
1120 // int32 limit = GET16(tom_ram_8, 0x0008); // LIMIT
1122 int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
1124 // If the image is completely to the left or right of the line buffer, then bail.
1125 //If in REFLECT mode, then these values are swapped! !!! FIX !!! [DONE]
1126 //There are four possibilities:
1127 // 1. image sits on left edge and no REFLECT; starts out of bounds but ends in bounds.
1128 // 2. image sits on left edge and REFLECT; starts in bounds but ends out of bounds.
1129 // 3. image sits on right edge and REFLECT; starts out of bounds but ends in bounds.
1130 // 4. image sits on right edge and no REFLECT; starts in bounds but ends out of bounds.
1131 //Numbers 2 & 4 can be caught by checking the LBUF clip while in the inner loop,
1132 // numbers 1 & 3 are of concern.
1133 // This *indirectly* handles only cases 2 & 4! And is WRONG is REFLECT is set...!
1134 // if (rightMargin < 0 || leftMargin > lbufWidth)
1136 // It might be easier to swap these (if REFLECTed) and just use XPOS down below...
1137 // That way, you could simply set XPOS to leftMargin if !REFLECT and to rightMargin otherwise.
1138 // Still have to be careful with the DATA and IWIDTH values though...
1140 if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
1141 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
1144 // Otherwise, find the clip limits and clip the phrase as well...
1145 // NOTE: I'm fudging here by letting the actual blit overstep the bounds of the
1146 // line buffer, but it shouldn't matter since there are two unused line
1147 // buffers below and nothing above and I'll at most write 40 bytes outside
1148 // the line buffer... I could use a fractional clip begin/end value, but
1149 // this makes the blit a *lot* more hairy. I might fix this in the future
1150 // if it becomes necessary. (JLH)
1151 // Probably wouldn't be *that* hairy. Just use a delta that tells the inner loop
1152 // which pixel in the phrase is being written, and quit when either end of phrases
1153 // is reached or line buffer extents are surpassed.
1155 //This stuff is probably wrong as well... !!! FIX !!!
1156 //The strange thing is that it seems to work, but that's no guarantee that it's bulletproof!
1157 //Yup. Seems that JagMania doesn't work correctly with this...
1158 //Dunno if this is the problem, but Atari Karts is showing *some* of the road now...
1159 //Actually, it is! Or, it was. It doesn't seem to be clipping here, so the problem lies
1160 //elsewhere! Hmm. Putting the scaling code into the 1/2/8 BPP cases seems to draw the ground
1161 // a bit more accurately... Strange!
1162 //It's probably a case of the REFLECT flag being set and the background being written
1163 //from the right side of the screen...
1164 //But no, it isn't... At least if the diagnostics are telling the truth!
1166 // NOTE: We're just using endPos to figure out how much, if any, to clip by.
1167 // ALSO: There may be another case where we start out of bounds and end out of bounds...!
1169 //There's a problem here with scaledPhrasePixels in that it can be forced to zero when
1170 //the scaling factor is small. So fix it already! !!! FIX !!!
1171 /*if (scaledPhrasePixels == 0)
1173 WriteLog("OP: [Scaled] We're about to encounter a divide by zero error!\n");
1174 DumpScaledObject(p0, p1, p2);
1176 //NOTE: I'm almost 100% sure that this is wrong... And it is! :-p
1177 if (startPos < 0) // Case #1: Begin out, end in, L to R
1178 /* clippedWidth = 0 - startPos,
1179 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
1180 startPos = 0 - (clippedWidth % phraseWidthToPixels[depth]);*/
1181 clippedWidth = 0 - startPos,
1182 dataClippedWidth = phraseClippedWidth = clippedWidth / scaledPhrasePixels,
1183 startPos = 0 - (clippedWidth % scaledPhrasePixels);
1185 if (endPos < 0) // Case #2: Begin in, end out, R to L
1186 /* clippedWidth = 0 - endPos,
1187 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];*/
1188 clippedWidth = 0 - endPos,
1189 phraseClippedWidth = clippedWidth / scaledPhrasePixels;
1191 if (endPos > lbufWidth) // Case #3: Begin in, end out, L to R
1192 /* clippedWidth = endPos - lbufWidth,
1193 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];*/
1194 clippedWidth = endPos - lbufWidth,
1195 phraseClippedWidth = clippedWidth / scaledPhrasePixels;
1197 if (startPos > lbufWidth) // Case #4: Begin out, end in, R to L
1198 /* clippedWidth = startPos - lbufWidth,
1199 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
1200 startPos = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);*/
1201 clippedWidth = startPos - lbufWidth,
1202 dataClippedWidth = phraseClippedWidth = clippedWidth / scaledPhrasePixels,
1203 startPos = lbufWidth + (clippedWidth % scaledPhrasePixels);
1205 extern int op_start_log;
1206 if (op_start_log && clippedWidth != 0)
1207 WriteLog("OP: Clipped line. SP=%i, EP=%i, clip=%u, iwidth=%u, hscale=%02X\n", startPos, endPos, clippedWidth, iwidth, hscale);
1208 if (op_start_log && startPos == 13)
1210 WriteLog("OP: Scaled line. SP=%i, EP=%i, clip=%u, iwidth=%u, hscale=%02X, depth=%u, firstPix=%u\n", startPos, endPos, clippedWidth, iwidth, hscale, depth, firstPix);
1211 DumpScaledObject(p0, p1, p2);
1213 // If the image is sitting on the line buffer left or right edge, we need to compensate
1214 // by decreasing the image phrase width accordingly.
1215 iwidth -= phraseClippedWidth;
1217 // Also, if we're clipping the phrase we need to make sure we're in the correct part of
1219 // data += phraseClippedWidth * (pitch << 3);
1220 data += dataClippedWidth * (pitch << 3);
1222 // NOTE: When the bitmap is in REFLECT mode, the XPOS marks the *right* side of the
1223 // bitmap! This makes clipping & etc. MUCH, much easier...!
1224 // uint32 lbufAddress = 0x1800 + (!in24BPPMode ? leftMargin * 2 : leftMargin * 4);
1225 uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
1226 uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
1230 // Hmm. We check above for 24 BPP mode, but don't do anything about it below...
1231 // If we *were* in 24 BPP mode, how would you convert CRY to RGB24? Seems to me
1232 // that if you're in CRY mode then you wouldn't be able to use 24 BPP bitmaps
1235 if (depth == 0) // 1 BPP
1238 WriteLog("OP: Scaled bitmap @ 1 BPP requesting FIRSTPIX!\n");
1239 // The LSB of flags is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1240 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1243 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1245 while ((int32)iwidth > 0)
1247 uint8 bits = pixels >> 63;
1249 if (flagTRANS && bits == 0)
1254 // This is the *only* correct use of endian-dependent code
1255 // (i.e., mem-to-mem direct copying)!
1256 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1258 *currentLineBuffer =
1259 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1260 *(currentLineBuffer + 1) =
1261 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1264 currentLineBuffer += lbufDelta;
1266 horizontalRemainder -= 0x20; // Subtract 1.0f in [3.5] fixed point format
1267 while (horizontalRemainder & 0x80)
1269 horizontalRemainder += hscale;
1276 int phrasesToSkip = pixCount / 64, pixelShift = pixCount % 64;
1278 data += (pitch << 3) * phrasesToSkip;
1279 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1280 pixels <<= 1 * pixelShift;
1281 iwidth -= phrasesToSkip;
1282 pixCount = pixelShift;
1286 else if (depth == 1) // 2 BPP
1289 WriteLog("OP: Scaled bitmap @ 2 BPP requesting FIRSTPIX!\n");
1290 index &= 0xFC; // Top six bits form CLUT index
1291 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1292 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1295 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1297 while ((int32)iwidth > 0)
1299 uint8 bits = pixels >> 62;
1301 if (flagTRANS && bits == 0)
1306 // This is the *only* correct use of endian-dependent code
1307 // (i.e., mem-to-mem direct copying)!
1308 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1310 *currentLineBuffer =
1311 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1312 *(currentLineBuffer + 1) =
1313 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1316 currentLineBuffer += lbufDelta;
1318 horizontalRemainder -= 0x20; // Subtract 1.0f in [3.5] fixed point format
1319 while (horizontalRemainder & 0x80)
1321 horizontalRemainder += hscale;
1328 int phrasesToSkip = pixCount / 32, pixelShift = pixCount % 32;
1330 data += (pitch << 3) * phrasesToSkip;
1331 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1332 pixels <<= 2 * pixelShift;
1333 iwidth -= phrasesToSkip;
1334 pixCount = pixelShift;
1338 else if (depth == 2) // 4 BPP
1341 WriteLog("OP: Scaled bitmap @ 4 BPP requesting FIRSTPIX!\n");
1342 index &= 0xF0; // Top four bits form CLUT index
1343 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1344 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1347 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1349 while ((int32)iwidth > 0)
1351 uint8 bits = pixels >> 60;
1353 if (flagTRANS && bits == 0)
1358 // This is the *only* correct use of endian-dependent code
1359 // (i.e., mem-to-mem direct copying)!
1360 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1362 *currentLineBuffer =
1363 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1364 *(currentLineBuffer + 1) =
1365 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1368 currentLineBuffer += lbufDelta;
1370 horizontalRemainder -= 0x20; // Subtract 1.0f in [3.5] fixed point format
1371 while (horizontalRemainder & 0x80)
1373 horizontalRemainder += hscale;
1380 int phrasesToSkip = pixCount / 16, pixelShift = pixCount % 16;
1382 data += (pitch << 3) * phrasesToSkip;
1383 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1384 pixels <<= 4 * pixelShift;
1385 iwidth -= phrasesToSkip;
1386 pixCount = pixelShift;
1390 else if (depth == 3) // 8 BPP
1393 WriteLog("OP: Scaled bitmap @ 8 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
1394 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1395 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1398 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1400 while ((int32)iwidth > 0)
1402 uint8 bits = pixels >> 56;
1404 if (flagTRANS && bits == 0)
1409 // This is the *only* correct use of endian-dependent code
1410 // (i.e., mem-to-mem direct copying)!
1411 *(uint16 *)currentLineBuffer = paletteRAM16[bits];
1413 *currentLineBuffer =
1414 BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
1415 *(currentLineBuffer + 1) =
1416 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
1419 currentLineBuffer += lbufDelta;
1421 horizontalRemainder -= 0x20; // Subtract 1.0f in [3.5] fixed point format
1422 while (horizontalRemainder & 0x80)
1424 horizontalRemainder += hscale;
1431 int phrasesToSkip = pixCount / 8, pixelShift = pixCount % 8;
1433 data += (pitch << 3) * phrasesToSkip;
1434 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1435 pixels <<= 8 * pixelShift;
1436 iwidth -= phrasesToSkip;
1437 pixCount = pixelShift;
1441 else if (depth == 4) // 16 BPP
1444 WriteLog("OP: Scaled bitmap @ 16 BPP requesting FIRSTPIX!\n");
1445 // The LSB is OPFLAG_REFLECT, so sign extend it and OR 2 into it.
1446 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1449 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1451 while ((int32)iwidth > 0)
1453 uint8 bitsHi = pixels >> 56, bitsLo = pixels >> 48;
1455 if (flagTRANS && (bitsLo | bitsHi) == 0)
1460 *currentLineBuffer = bitsHi,
1461 *(currentLineBuffer + 1) = bitsLo;
1463 *currentLineBuffer =
1464 BLEND_CR(*currentLineBuffer, bitsHi),
1465 *(currentLineBuffer + 1) =
1466 BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1469 currentLineBuffer += lbufDelta;
1471 horizontalRemainder -= 0x20; // Subtract 1.0f in [3.5] fixed point format
1472 while (horizontalRemainder & 0x80)
1474 horizontalRemainder += hscale;
1481 int phrasesToSkip = pixCount / 4, pixelShift = pixCount % 4;
1483 data += (pitch << 3) * phrasesToSkip;
1484 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1485 pixels <<= 16 * pixelShift;
1487 iwidth -= phrasesToSkip;
1489 pixCount = pixelShift;
1493 else if (depth == 5) // 24 BPP
1495 //I'm not sure that you can scale a 24 BPP bitmap properly--the JTRM seem to indicate as much.
1496 WriteLog("OP: Writing 24 BPP scaled bitmap!\n");
1498 WriteLog("OP: Scaled bitmap @ 24 BPP requesting FIRSTPIX!\n");
1499 // Not sure, but I think RMW only works with 16 BPP and below, and only in CRY mode...
1500 // The LSB is OPFLAG_REFLECT, so sign extend it and or 4 into it.
1501 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 4) | 0x04;
1506 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1507 data += pitch << 3; // Multiply pitch * 8 (optimize: precompute this value)
1509 for(int i=0; i<2; i++)
1511 uint8 bits3 = pixels >> 56, bits2 = pixels >> 48,
1512 bits1 = pixels >> 40, bits0 = pixels >> 32;
1513 // Seems to me that both of these are in the same endian, so we could cast it as
1514 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
1515 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
1516 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
1517 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
1518 if (flagTRANS && (bits3 | bits2 | bits1 | bits0) == 0)
1521 *currentLineBuffer = bits3,
1522 *(currentLineBuffer + 1) = bits2,
1523 *(currentLineBuffer + 2) = bits1,
1524 *(currentLineBuffer + 3) = bits0;
1526 currentLineBuffer += lbufDelta;
1531 /*if (depth == 3 && startPos == 13)
1534 WriteLog("OP: Writing in the margins...\n");
1535 for(int i=0; i<100*2; i+=2)
1536 // for(int i=0; i<14*2; i+=2)
1537 tom_ram_8[0x1800 + i] = 0xFF,
1538 tom_ram_8[0x1800 + i + 1] = 0xFF;
1540 // uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
1541 // uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];