]> Shamusworld >> Repos - virtualjaguar/blob - src/objectp.cpp
Virtual Jaguar 1.0.5 update (Shamus)
[virtualjaguar] / src / objectp.cpp
1 //
2 // Object Processor
3 //
4 // by cal2
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/fixes/rewrites by James L. Hammons
7 //
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "jaguar.h"
13
14 //#define OP_DEBUG
15 //#define OP_DEBUG_BMP
16
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))]
19
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
25
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
31
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
36
37 // Private function prototypes
38
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);
44
45 // External global variables
46
47 extern uint32 jaguar_mainRom_crc32;
48
49 // Local global variables
50
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;
58
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;
64
65 int32 phraseWidthToPixels[8] = { 64, 32, 16, 8, 4, 2, 0, 0 };
66
67
68 //
69 // Object Processor initialization
70 //
71 void op_init(void)
72 {
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");
76
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++)
81         {
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
88
89                 y += dy;
90                 if (y < 0)
91                         y = 0;
92                 else if (y > 0xFF)
93                         y = 0xFF;
94                 op_blend_y[i] = y;
95
96                 c1 += dc1;
97                 if (c1 < 0)
98                         c1 = 0;
99                 else if (c1 > 0x0F)
100                         c1 = 0x0F;
101                 c2 += dc2;
102
103                 if (c2 < 0)
104                         c2 = 0;
105                 else if (c2 > 0x0F)
106                         c2 = 0x0F;
107                 op_blend_cr[i] = (c2 << 4) | c1;
108         }
109
110         op_reset();
111 }
112
113 //
114 // Object Processor reset
115 //
116 void op_reset(void)
117 {
118         memset(objectp_ram, 0x00, 0x40);
119         objectp_running = 0;
120 }
121
122 void op_done(void)
123 {
124         char * opType[8] =
125         { "(BITMAP)", "(SCALED BITMAP)", "(GPU INT)", "(BRANCH)", "(STOP)", "???", "???", "???" };
126         char * ccType[8] =
127                 { "\"==\"", "\"<\"", "\">\"", "(opflag set)", "(second half line)", "?", "?", "?" };
128
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)
133         {
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)
137                 {
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);
142                 }
143                 WriteLog("\n");
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));
148         }
149         WriteLog("\n");
150 }
151
152 //
153 // Object Processor memory access
154 // Memory range: F00010 - F00027
155 //
156 void op_byte_write(uint32 offset, uint8 data)
157 {
158         offset &= 0x3F;
159         objectp_ram[offset] = data;
160 }
161
162 void op_word_write(uint32 offset, uint16 data)
163 {
164         offset &= 0x3F;
165 //      objectp_ram[offset] = (data >> 8) & 0xFF;
166 //      objectp_ram[offset+1] = data & 0xFF;
167         SET16(objectp_ram, offset, data);
168
169 /*if (offset == 0x20)
170 WriteLog("OP: Setting lo list pointer: %04X\n", data);
171 if (offset == 0x22)
172 WriteLog("OP: Setting hi list pointer: %04X\n", data);//*/
173 }
174
175 uint8 op_byte_read(uint32 offset)
176 {
177         offset &= 0x3F;
178         return objectp_ram[offset];
179 }
180
181 uint16 op_word_read(uint32 offset)
182 {
183 //      return (objectp_ram[offset & 0x3F] << 8) | objectp_ram[(offset+1) & 0x3F];
184         offset &= 0x3F;
185         return GET16(objectp_ram, offset);
186 }
187
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
191
192 uint32 op_get_list_pointer(void)
193 {
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);
197 }
198
199 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
200
201 uint32 op_get_status_register(void)
202 {
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);
206 }
207
208 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
209
210 void op_set_status_register(uint32 data)
211 {
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);
218 }
219
220 void op_set_current_object(uint64 object)
221 {
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;
228
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;
238
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;
243 }
244
245 uint64 op_load_phrase(uint32 offset)
246 {
247         offset &= ~0x07;                                                // 8 byte alignment
248         return ((uint64)jaguar_long_read(offset) << 32) | (uint64)jaguar_long_read(offset+4);
249 }
250
251 void OPStorePhrase(uint32 offset, uint64 p)
252 {
253         offset &= ~0x07;                                                // 8 byte alignment
254         jaguar_long_write(offset, p >> 32);
255         jaguar_long_write(offset + 4, p & 0xFFFFFFFF);
256 }
257
258 //
259 // Debugging routines
260 //
261 void DumpScaledObject(uint64 p0, uint64 p1, uint64 p2)
262 {
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);
285 }
286
287 void DumpFixedObject(uint64 p0, uint64 p1)
288 {
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);
306 }
307
308 //
309 // Object Processor main routine
310 //
311 void OPProcessList(int scanline, bool render)
312 {
313 extern int op_start_log;
314 //      char * condition_to_str[8] =
315 //              { "==", "<", ">", "(opflag set)", "(second half line)", "?", "?", "?" };
316
317 // If jaguar_exec() is working right, we should *never* have to check for this
318 // condition...
319 /*      if (scanline < tom_get_vdb())
320                 return;
321
322         if (scanline >= 525)//tom_getVideoModeHeight()+tom_get_vdb())
323                 return;//*/
324
325         op_pointer = op_get_list_pointer();
326
327         objectp_stop_reading_list = false;
328
329 // *** BEGIN OP PROCESSOR TESTING ONLY ***
330 extern bool interactiveMode;
331 extern bool iToggle;
332 extern int objectPtr;
333 bool inhibit;
334 int bitmapCounter = 0;
335 // *** END OP PROCESSOR TESTING ONLY ***
336
337 //      if (op_pointer) WriteLog(" new op list at 0x%.8x scanline %i\n",op_pointer,scanline);
338         while (op_pointer)
339         {
340 // *** BEGIN OP PROCESSOR TESTING ONLY ***
341 if (interactiveMode && bitmapCounter == objectPtr)
342         inhibit = iToggle;
343 else
344         inhibit = false;
345 // *** END OP PROCESSOR TESTING ONLY ***
346                 if (objectp_stop_reading_list)
347                         return;
348                         
349                 uint64 p0 = op_load_phrase(op_pointer);
350                 op_pointer += 8;
351 if (scanline == tom_get_vdb() && op_start_log)
352 //if (scanline == 215 && op_start_log)
353 {
354 WriteLog("%08X --> phrase %08X %08X", op_pointer - 8, (int)(p0>>32), (int)(p0&0xFFFFFFFF));
355 if ((p0 & 0x07) == OBJECT_TYPE_BITMAP)
356 {
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);
375 }
376 if ((p0 & 0x07) == OBJECT_TYPE_SCALE)
377 {
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);
401 }
402 if ((p0 & 0x07) == OBJECT_TYPE_GPU)
403 WriteLog(" (GPU)\n");
404 if ((p0 & 0x07) == OBJECT_TYPE_BRANCH)
405 {
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]);
411 WriteLog("\n");
412 }
413 if ((p0 & 0x07) == OBJECT_TYPE_STOP)
414 WriteLog("    --> List end\n");
415 }//*/
416                 
417 //              WriteLog("%08X type %i\n", op_pointer, (uint8)p0 & 0x07);               
418                 switch ((uint8)p0 & 0x07)
419                 {
420                 case OBJECT_TYPE_BITMAP:
421                 {
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...
430
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!
433 //                      if (ypos == 0)
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");
440 bitmapCounter++;
441 if (!inhibit)   // For OP testing only!
442 // *** END OP PROCESSOR TESTING ONLY ***
443                         if (scanline >= ypos && height > 0)
444                         {
445                                 uint64 p1 = op_load_phrase(op_pointer);
446                                 op_pointer += 8;
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);
450
451                                 // OP write-backs
452
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;
458                                 if (height - 1 > 0)
459                                         height--;*/
460                                 // NOTE: Would subtract 2 if in interlaced mode...!
461 //                              uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
462 //                              if (height)
463                                         height--;
464
465                                 uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
466                                 uint64 dwidth = (p1 & 0xFFC0000) >> 15;
467                                 data += dwidth;
468
469                                 p0 &= ~0xFFFFF80000FFC000;                      // Mask out old data...
470                                 p0 |= (uint64)height << 14;
471                                 p0 |= data << 40;
472                                 OPStorePhrase(oldOPP, p0);
473                         }
474                         op_pointer = (p0 & 0x000007FFFF000000) >> 21;
475                         break;
476                 }
477                 case OBJECT_TYPE_SCALE:
478                 {
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...
487
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!
490 //                      if (ypos == 0)
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)
496 {
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));
499 }
500 bitmapCounter++;
501 if (!inhibit)   // For OP testing only!
502 // *** END OP PROCESSOR TESTING ONLY ***
503                         if (scanline >= ypos && height > 0)
504                         {
505                                 uint64 p1 = op_load_phrase(op_pointer);
506                                 op_pointer += 8;
507                                 uint64 p2 = op_load_phrase(op_pointer);
508                                 op_pointer += 8;
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);
511
512                                 // OP write-backs
513
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;
519                                 if (height - 1 > 0)
520                                         height--;*/
521                                 // NOTE: Would subtract 2 if in interlaced mode...!
522 //                              uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
523
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:
527 // (SCALED BITMAP)
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...
532
533                                 if (vscale == 0)
534                                         vscale = 0x20;                                  // OP bug??? Nope, it isn't...! Or is it?
535
536                                 remainder -= 0x20;                                      // 1.0f in [3.5] fixed point format
537                                 if (remainder & 0x80)                           // I.e., it's negative
538                                 {
539                                         uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
540                                         uint64 dwidth = (p1 & 0xFFC0000) >> 15;
541
542                                         while (remainder & 0x80)
543                                         {
544                                                 remainder += vscale;
545                                                 if (height)
546                                                         height--;
547
548                                                 data += dwidth;
549                                         }
550                                         p0 &= ~0xFFFFF80000FFC000;              // Mask out old data...
551                                         p0 |= (uint64)height << 14;
552                                         p0 |= data << 40;
553                                         OPStorePhrase(oldOPP, p0);
554                                 }
555
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);
563                         }
564                         op_pointer = (p0 & 0x000007FFFF000000) >> 21;
565                         break;
566                 }
567                 case OBJECT_TYPE_GPU:
568                 {
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...
573 // !!! FIX !!!
574 //Do something like:
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...
578                         break;
579                 }
580                 case OBJECT_TYPE_BRANCH:
581                 {
582                         uint16 ypos = (p0 >> 3) & 0x7FF;
583                         uint8  cc   = (p0 >> 14) & 0x03;
584                         uint32 link = (p0 >> 21) & 0x3FFFF8;
585                         
586 //                      if ((ypos!=507)&&(ypos!=25))
587 //                              WriteLog("\t%i%s%i link=0x%.8x\n",scanline,condition_to_str[cc],ypos>>1,link);
588                         switch (cc)
589                         {
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))
593 //                                      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)
597                                         op_pointer = link;
598                                 break;
599                         case CONDITION_LESS_THAN:
600 //                              if ((2 * tom_get_scanline()) < ypos)
601                                 if (tom_word_read(0xF00006) < ypos)
602                                         op_pointer = link;
603                                 break;
604                         case CONDITION_GREATER_THAN:
605 //                              if ((2 * tom_get_scanline()) > ypos)
606                                 if (tom_word_read(0xF00006) > ypos)
607                                         op_pointer = link;
608                                 break;
609                         case CONDITION_OP_FLAG_SET:
610                                 if (op_get_status_register() & 0x01)
611                                         op_pointer = link;
612                                 break;
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");
616                                 fclose(log_get());
617                                 exit(0);
618                                 break;
619                         default:
620                                 WriteLog("OP: Unimplemented branch condition %i\n", cc);
621                         }
622                         break;
623                 }
624                 case OBJECT_TYPE_STOP:
625                 {
626 //op_start_log = 0;
627                         // unsure
628 //WriteLog("OP: --> STOP\n");
629 //                      op_set_status_register(((p0>>3) & 0xFFFFFFFF));
630 //This seems more likely...
631                         op_set_current_object(p0);
632                         
633                         if (p0 & 0x08)
634                         {
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...
638                         }
639
640                         return;
641 //                      break;
642                 }
643                 default:
644                         WriteLog("op: unknown object type %i\n", ((uint8)p0 & 0x07)); 
645                         return;
646                 }
647         }
648 }
649
650 //
651 // Store fixed size bitmap in line buffer
652 //
653
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!
656
657 void OPProcessFixedBitmap(int scanline, uint64 p0, uint64 p1, bool render)
658 {
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..."
670         firstPix &= 0x3E;
671 //#endif
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
683
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;
690
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"));
693
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)
700                 return;
701
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"));
706 //#endif
707
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
717         int32 limit = 720;
718         int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
719
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)
731
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...
735
736 //      if ((!flagREFLECT && (rightMargin < 0 || leftMargin > lbufWidth))
737 //              || (flagREFLECT && (leftMargin < 0 || rightMargin > lbufWidth)))
738 //              return;
739         if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
740                 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
741                 return;
742
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.
753
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...
758 //      if (!flagREFLECT)
759
760 /*
761         if (leftMargin < 0)
762                 clippedWidth = 0 - leftMargin,
763                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
764                 leftMargin = 0 - (clippedWidth % phraseWidthToPixels[depth]);
765 //              leftMargin = 0;
766
767         if (rightMargin > lbufWidth)
768                 clippedWidth = rightMargin - lbufWidth,
769                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];//,
770 //              rightMargin = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);
771 //              rightMargin = lbufWidth;
772 */
773 if (depth > 5)
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]);
781
782         if (endPos < 0)                         // Case #2: Begin in, end out, R to L
783                 clippedWidth = 0 - endPos,
784                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
785
786         if (endPos > lbufWidth)         // Case #3: Begin in, end out, L to R
787                 clippedWidth = endPos - lbufWidth,
788                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
789
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]);
794
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;
798
799         // Also, if we're clipping the phrase we need to make sure we're in the correct part of
800         // the pixel data.
801 //      data += phraseClippedWidth * (pitch << 3);
802         data += dataClippedWidth * (pitch << 3);
803
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];
809
810         // Render.
811
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
815 // anyway.
816
817         if (depth == 0)                                                                 // 1 BPP
818         {
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;
821
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...
828
829                 while (iwidth--)
830                 {
831                         while (i++ < 64)
832                         {
833                                 uint8 bit = pixels >> 63;
834                                 if (flagTRANS && bit == 0)
835                                         ;       // Do nothing...
836                                 else
837                                 {
838                                         if (!flagRMW)
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];
844                                         else
845                                                 *currentLineBuffer = 
846                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bit) << 1]),
847                                                 *(currentLineBuffer + 1) = 
848                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bit) << 1) + 1]);
849                                 }
850
851                                 currentLineBuffer += lbufDelta;
852                                 pixels <<= 1;
853                         }
854                         i = 0;
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);
858                 }
859         }
860         else if (depth == 1)                                                    // 2 BPP
861         {
862 if (firstPix)
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;
867
868                 while (iwidth--)
869                 {
870                         // Fetch phrase...
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)
873
874                         for(int i=0; i<32; i++)
875                         {
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)
883                                         ;       // Do nothing...
884                                 else
885                                 {
886                                         if (!flagRMW)
887                                                 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
888                                         else
889                                                 *currentLineBuffer = 
890                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
891                                                 *(currentLineBuffer + 1) = 
892                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
893                                 }
894
895                                 currentLineBuffer += lbufDelta;
896                                 pixels <<= 2;
897                         }
898                 }
899         }
900         else if (depth == 2)                                                    // 4 BPP
901         {
902 if (firstPix)
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;
907
908                 while (iwidth--)
909                 {
910                         // Fetch phrase...
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)
913
914                         for(int i=0; i<16; i++)
915                         {
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)
923                                         ;       // Do nothing...
924                                 else
925                                 {
926                                         if (!flagRMW)
927                                                 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
928                                         else
929                                                 *currentLineBuffer = 
930                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
931                                                 *(currentLineBuffer + 1) = 
932                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
933                                 }
934
935                                 currentLineBuffer += lbufDelta;
936                                 pixels <<= 4;
937                         }
938                 }
939         }
940         else if (depth == 3)                                                    // 8 BPP
941         {
942 if (firstPix)
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;
946
947                 while (iwidth--)
948                 {
949                         // Fetch phrase...
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)
952
953                         for(int i=0; i<8; i++)
954                         {
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)
962                                         ;       // Do nothing...
963                                 else
964                                 {
965                                         if (!flagRMW)
966                                                 *(uint16 *)currentLineBuffer = paletteRAM16[bits];
967                                         else
968                                                 *currentLineBuffer = 
969                                                         BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
970                                                 *(currentLineBuffer + 1) = 
971                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
972                                 }
973
974                                 currentLineBuffer += lbufDelta;
975                                 pixels <<= 8;
976                         }
977                 }
978         }
979         else if (depth == 4)                                                    // 16 BPP
980         {
981 if (firstPix)
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;
985
986                 while (iwidth--)
987                 {
988                         // Fetch phrase...
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)
991
992                         for(int i=0; i<4; i++)
993                         {
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)
1001                                         ;       // Do nothing...
1002                                 else
1003                                 {
1004                                         if (!flagRMW)
1005                                                 *currentLineBuffer = bitsHi,
1006                                                 *(currentLineBuffer + 1) = bitsLo;
1007                                         else
1008                                                 *currentLineBuffer = 
1009                                                         BLEND_CR(*currentLineBuffer, bitsHi),
1010                                                 *(currentLineBuffer + 1) = 
1011                                                         BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1012                                 }
1013
1014                                 currentLineBuffer += lbufDelta;
1015                                 pixels <<= 16;
1016                         }
1017                 }
1018         }
1019         else if (depth == 5)                                                    // 24 BPP
1020         {
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");
1024 if (firstPix)
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;
1029
1030                 while (iwidth--)
1031                 {
1032                         // Fetch phrase...
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)
1035
1036                         for(int i=0; i<2; i++)
1037                         {
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)
1046                                         ;       // Do nothing...
1047                                 else
1048                                         *currentLineBuffer = bits3,
1049                                         *(currentLineBuffer + 1) = bits2,
1050                                         *(currentLineBuffer + 2) = bits1,
1051                                         *(currentLineBuffer + 3) = bits0;
1052
1053                                 currentLineBuffer += lbufDelta;
1054                                 pixels <<= 32;
1055                         }
1056                 }
1057         }
1058 }
1059
1060 //
1061 // Store scaled bitmap in line buffer
1062 //
1063 void OPProcessScaledBitmap(int scanline, uint64 p0, uint64 p1, uint64 p2, bool render)
1064 {
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???
1076 if (firstPix)
1077         WriteLog("OP: FIRSTPIX != 0!\n");
1078 //#endif
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
1088
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;
1095
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;
1100
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"));
1103
1104 //Looks like an hscale of zero means don't draw!
1105         if (!render || iwidth == 0 || hscale == 0)
1106                 return;
1107
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"));
1112 //#endif
1113
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
1121         int32 limit = 720;
1122         int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
1123
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)
1135
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...
1139
1140         if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
1141                 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
1142                 return;
1143
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.
1154
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!
1165
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...!
1168
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)
1172 {
1173         WriteLog("OP: [Scaled] We're about to encounter a divide by zero error!\n");
1174         DumpScaledObject(p0, p1, p2);
1175 }//*/
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);
1184
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;
1190
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;
1196
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);
1204
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)
1209 {
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);
1212 }
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;
1216
1217         // Also, if we're clipping the phrase we need to make sure we're in the correct part of
1218         // the pixel data.
1219 //      data += phraseClippedWidth * (pitch << 3);
1220         data += dataClippedWidth * (pitch << 3);
1221
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];
1227
1228         // Render.
1229
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
1233 // anyway.
1234
1235         if (depth == 0)                                                                 // 1 BPP
1236         {
1237 if (firstPix != 0)
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;
1241
1242                 int pixCount = 0;
1243                 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1244
1245                 while ((int32)iwidth > 0)
1246                 {
1247                         uint8 bits = pixels >> 63;
1248
1249                         if (flagTRANS && bits == 0)
1250                                 ;       // Do nothing...
1251                         else
1252                         {
1253                                 if (!flagRMW)
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];
1257                                 else
1258                                         *currentLineBuffer = 
1259                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1260                                         *(currentLineBuffer + 1) = 
1261                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1262                         }
1263
1264                         currentLineBuffer += lbufDelta;
1265
1266                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1267                         while (horizontalRemainder & 0x80)
1268                         {
1269                                 horizontalRemainder += hscale;
1270                                 pixCount++;
1271                                 pixels <<= 1;
1272                         }
1273
1274                         if (pixCount > 63)
1275                         {
1276                                 int phrasesToSkip = pixCount / 64, pixelShift = pixCount % 64;
1277
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;
1283                         }
1284                 }
1285         }
1286         else if (depth == 1)                                                    // 2 BPP
1287         {
1288 if (firstPix != 0)
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;
1293
1294                 int pixCount = 0;
1295                 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1296
1297                 while ((int32)iwidth > 0)
1298                 {
1299                         uint8 bits = pixels >> 62;
1300
1301                         if (flagTRANS && bits == 0)
1302                                 ;       // Do nothing...
1303                         else
1304                         {
1305                                 if (!flagRMW)
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];
1309                                 else
1310                                         *currentLineBuffer = 
1311                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1312                                         *(currentLineBuffer + 1) = 
1313                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1314                         }
1315
1316                         currentLineBuffer += lbufDelta;
1317
1318                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1319                         while (horizontalRemainder & 0x80)
1320                         {
1321                                 horizontalRemainder += hscale;
1322                                 pixCount++;
1323                                 pixels <<= 2;
1324                         }
1325
1326                         if (pixCount > 31)
1327                         {
1328                                 int phrasesToSkip = pixCount / 32, pixelShift = pixCount % 32;
1329
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;
1335                         }
1336                 }
1337         }
1338         else if (depth == 2)                                                    // 4 BPP
1339         {
1340 if (firstPix != 0)
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;
1345
1346                 int pixCount = 0;
1347                 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1348
1349                 while ((int32)iwidth > 0)
1350                 {
1351                         uint8 bits = pixels >> 60;
1352
1353                         if (flagTRANS && bits == 0)
1354                                 ;       // Do nothing...
1355                         else
1356                         {
1357                                 if (!flagRMW)
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];
1361                                 else
1362                                         *currentLineBuffer = 
1363                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1364                                         *(currentLineBuffer + 1) = 
1365                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1366                         }
1367
1368                         currentLineBuffer += lbufDelta;
1369
1370                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1371                         while (horizontalRemainder & 0x80)
1372                         {
1373                                 horizontalRemainder += hscale;
1374                                 pixCount++;
1375                                 pixels <<= 4;
1376                         }
1377
1378                         if (pixCount > 15)
1379                         {
1380                                 int phrasesToSkip = pixCount / 16, pixelShift = pixCount % 16;
1381
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;
1387                         }
1388                 }
1389         }
1390         else if (depth == 3)                                                    // 8 BPP
1391         {
1392 if (firstPix)
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;
1396
1397                 int pixCount = 0;
1398                 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1399
1400                 while ((int32)iwidth > 0)
1401                 {
1402                         uint8 bits = pixels >> 56;
1403
1404                         if (flagTRANS && bits == 0)
1405                                 ;       // Do nothing...
1406                         else
1407                         {
1408                                 if (!flagRMW)
1409                                         // This is the *only* correct use of endian-dependent code
1410                                         // (i.e., mem-to-mem direct copying)!
1411                                         *(uint16 *)currentLineBuffer = paletteRAM16[bits];
1412                                 else
1413                                         *currentLineBuffer = 
1414                                                 BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
1415                                         *(currentLineBuffer + 1) = 
1416                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
1417                         }
1418
1419                         currentLineBuffer += lbufDelta;
1420
1421                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1422                         while (horizontalRemainder & 0x80)
1423                         {
1424                                 horizontalRemainder += hscale;
1425                                 pixCount++;
1426                                 pixels <<= 8;
1427                         }
1428
1429                         if (pixCount > 7)
1430                         {
1431                                 int phrasesToSkip = pixCount / 8, pixelShift = pixCount % 8;
1432
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;
1438                         }
1439                 }
1440         }
1441         else if (depth == 4)                                                    // 16 BPP
1442         {
1443 if (firstPix != 0)
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;
1447
1448                 int pixCount = 0;
1449                 uint64 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1450
1451                 while ((int32)iwidth > 0)
1452                 {
1453                         uint8 bitsHi = pixels >> 56, bitsLo = pixels >> 48;
1454
1455                         if (flagTRANS && (bitsLo | bitsHi) == 0)
1456                                 ;       // Do nothing...
1457                         else
1458                         {
1459                                 if (!flagRMW)
1460                                         *currentLineBuffer = bitsHi,
1461                                         *(currentLineBuffer + 1) = bitsLo;
1462                                 else
1463                                         *currentLineBuffer = 
1464                                                 BLEND_CR(*currentLineBuffer, bitsHi),
1465                                         *(currentLineBuffer + 1) = 
1466                                                 BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1467                         }
1468
1469                         currentLineBuffer += lbufDelta;
1470
1471                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1472                         while (horizontalRemainder & 0x80)
1473                         {
1474                                 horizontalRemainder += hscale;
1475                                 pixCount++;
1476                                 pixels <<= 16;
1477                         }
1478
1479                         if (pixCount > 3)
1480                         {
1481                                 int phrasesToSkip = pixCount / 4, pixelShift = pixCount % 4;
1482
1483                                 data += (pitch << 3) * phrasesToSkip;
1484                                 pixels = ((uint64)jaguar_long_read(data) << 32) | jaguar_long_read(data + 4);
1485                                 pixels <<= 16 * pixelShift;
1486
1487                                 iwidth -= phrasesToSkip;
1488
1489                                 pixCount = pixelShift;
1490                         }
1491                 }
1492         }
1493         else if (depth == 5)                                                    // 24 BPP
1494         {
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");
1497 if (firstPix != 0)
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;
1502
1503                 while (iwidth--)
1504                 {
1505                         // Fetch phrase...
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)
1508
1509                         for(int i=0; i<2; i++)
1510                         {
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)
1519                                         ;       // Do nothing...
1520                                 else
1521                                         *currentLineBuffer = bits3,
1522                                         *(currentLineBuffer + 1) = bits2,
1523                                         *(currentLineBuffer + 2) = bits1,
1524                                         *(currentLineBuffer + 3) = bits0;
1525
1526                                 currentLineBuffer += lbufDelta;
1527                                 pixels <<= 32;
1528                         }
1529                 }
1530         }
1531 /*if (depth == 3 && startPos == 13)
1532 {
1533 if (op_start_log)
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;
1539 }*/
1540 //      uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
1541 //      uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
1542 }