]> Shamusworld >> Repos - virtualjaguar/blob - src/objectp.cpp
Changes for the upcoming 1.0.5 release
[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 = JaguarReadLong(olp + i, OP), lo = JaguarReadLong(olp + i + 4, OP);
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 uint8 OPReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
157 {
158         offset &= 0x3F;
159         return objectp_ram[offset];
160 }
161
162 uint16 OPReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
163 {
164         offset &= 0x3F;
165         return GET16(objectp_ram, offset);
166 }
167
168 //      F00010-F00017   R     xxxxxxxx xxxxxxxx   OB - current object code from the graphics processor
169 //      F00020-F00023     W   xxxxxxxx xxxxxxxx   OLP - start of the object list
170 //      F00026            W   -------- -------x   OBF - object processor flag
171
172 void OPWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
173 {
174         offset &= 0x3F;
175         objectp_ram[offset] = data;
176 }
177
178 void OPWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
179 {
180         offset &= 0x3F;
181         SET16(objectp_ram, offset, data);
182
183 /*if (offset == 0x20)
184 WriteLog("OP: Setting lo list pointer: %04X\n", data);
185 if (offset == 0x22)
186 WriteLog("OP: Setting hi list pointer: %04X\n", data);//*/
187 }
188
189 uint32 op_get_list_pointer(void)
190 {
191         // Note: This register is LO / HI WORD, hence the funky look of this...
192 //      return (objectp_ram[0x22] << 24) | (objectp_ram[0x23] << 16) | (objectp_ram[0x20] << 8) | objectp_ram[0x21];
193         return GET16(objectp_ram, 0x20) | (GET16(objectp_ram, 0x22) << 16);
194 }
195
196 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
197
198 uint32 op_get_status_register(void)
199 {
200 //      return (objectp_ram[0x26] << 24) | (objectp_ram[0x27] << 16) | (objectp_ram[0x28] << 8) | objectp_ram[0x29];
201 //      return GET32(objectp_ram, 0x26);
202         return GET16(objectp_ram, 0x26);
203 }
204
205 // This is WRONG, since the OBF is only 16 bits wide!!! [FIXED]
206
207 void op_set_status_register(uint32 data)
208 {
209 /*      objectp_ram[0x26] = (data & 0xFF000000) >> 24;
210         objectp_ram[0x27] = (data & 0x00FF0000) >> 16;
211         objectp_ram[0x28] = (data & 0x0000FF00) >> 8;
212         objectp_ram[0x29] |= (data & 0xFE);*/
213         objectp_ram[0x26] = (data & 0x0000FF00) >> 8;
214         objectp_ram[0x27] |= (data & 0xFE);
215 }
216
217 void op_set_current_object(uint64 object)
218 {
219 //Not sure this is right... Wouldn't it just be stored 64 bit BE?
220         // Stored as least significant 32 bits first, ms32 last in big endian
221 /*      objectp_ram[0x13] = object & 0xFF; object >>= 8;
222         objectp_ram[0x12] = object & 0xFF; object >>= 8;
223         objectp_ram[0x11] = object & 0xFF; object >>= 8;
224         objectp_ram[0x10] = object & 0xFF; object >>= 8;
225
226         objectp_ram[0x17] = object & 0xFF; object >>= 8;
227         objectp_ram[0x16] = object & 0xFF; object >>= 8;
228         objectp_ram[0x15] = object & 0xFF; object >>= 8;
229         objectp_ram[0x14] = object & 0xFF;*/
230 // Let's try regular good old big endian...
231         objectp_ram[0x17] = object & 0xFF; object >>= 8;
232         objectp_ram[0x16] = object & 0xFF; object >>= 8;
233         objectp_ram[0x15] = object & 0xFF; object >>= 8;
234         objectp_ram[0x14] = object & 0xFF; object >>= 8;
235
236         objectp_ram[0x13] = object & 0xFF; object >>= 8;
237         objectp_ram[0x12] = object & 0xFF; object >>= 8;
238         objectp_ram[0x11] = object & 0xFF; object >>= 8;
239         objectp_ram[0x10] = object & 0xFF;
240 }
241
242 uint64 op_load_phrase(uint32 offset)
243 {
244         offset &= ~0x07;                                                // 8 byte alignment
245         return ((uint64)JaguarReadLong(offset, OP) << 32) | (uint64)JaguarReadLong(offset+4, OP);
246 }
247
248 void OPStorePhrase(uint32 offset, uint64 p)
249 {
250         offset &= ~0x07;                                                // 8 byte alignment
251         JaguarWriteLong(offset, p >> 32, OP);
252         JaguarWriteLong(offset + 4, p & 0xFFFFFFFF, OP);
253 }
254
255 //
256 // Debugging routines
257 //
258 void DumpScaledObject(uint64 p0, uint64 p1, uint64 p2)
259 {
260         WriteLog(" (SCALED BITMAP)");
261         WriteLog(" %08X --> phrase %08X %08X\n", op_pointer, (uint32)(p1>>32), (uint32)(p1&0xFFFFFFFF));
262         WriteLog("                 %08X --> phrase %08X %08X ", op_pointer+8, (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
263         uint8 bitdepth = (p1 >> 12) & 0x07;
264         int16 ypos = ((p0 >> 3) & 0x3FF);                       // ??? What if not interlaced (/2)?
265         int32 xpos = p1 & 0xFFF;
266         xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
267         uint32 iwidth = ((p1 >> 28) & 0x3FF);
268         uint32 dwidth = ((p1 >> 18) & 0x3FF);           // Unsigned!
269         uint16 height = ((p0 >> 14) & 0x3FF);
270         uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
271         uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
272         uint32 firstPix = (p1 >> 49) & 0x3F;
273         uint8 flags = (p1 >> 45) & 0x0F;
274         uint8 idx = (p1 >> 38) & 0x7F;
275         uint32 pitch = (p1 >> 15) & 0x07;
276         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",
277                 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);
278         uint32 hscale = p2 & 0xFF;
279         uint32 vscale = (p2 >> 8) & 0xFF;
280         uint32 remainder = (p2 >> 16) & 0xFF;
281         WriteLog("    [hsc: %02X, vsc: %02X, rem: %02X]\n", hscale, vscale, remainder);
282 }
283
284 void DumpFixedObject(uint64 p0, uint64 p1)
285 {
286         WriteLog(" (BITMAP)");
287         WriteLog(" %08X --> phrase %08X %08X\n", op_pointer, (uint32)(p1>>32), (uint32)(p1&0xFFFFFFFF));
288         uint8 bitdepth = (p1 >> 12) & 0x07;
289         int16 ypos = ((p0 >> 3) & 0x3FF);                       // ??? What if not interlaced (/2)?
290         int32 xpos = p1 & 0xFFF;
291         xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
292         uint32 iwidth = ((p1 >> 28) & 0x3FF);
293         uint32 dwidth = ((p1 >> 18) & 0x3FF);           // Unsigned!
294         uint16 height = ((p0 >> 14) & 0x3FF);
295         uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
296         uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
297         uint32 firstPix = (p1 >> 49) & 0x3F;
298         uint8 flags = (p1 >> 45) & 0x0F;
299         uint8 idx = (p1 >> 38) & 0x7F;
300         uint32 pitch = (p1 >> 15) & 0x07;
301         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",
302                 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);
303 }
304
305 //
306 // Object Processor main routine
307 //
308 void OPProcessList(int scanline, bool render)
309 {
310 extern int op_start_log;
311 //      char * condition_to_str[8] =
312 //              { "==", "<", ">", "(opflag set)", "(second half line)", "?", "?", "?" };
313
314 // If jaguar_exec() is working right, we should *never* have to check for this
315 // condition...
316 /*      if (scanline < tom_get_vdb())
317                 return;
318
319         if (scanline >= 525)//tom_getVideoModeHeight()+tom_get_vdb())
320                 return;//*/
321
322         op_pointer = op_get_list_pointer();
323
324         objectp_stop_reading_list = false;
325
326 // *** BEGIN OP PROCESSOR TESTING ONLY ***
327 extern bool interactiveMode;
328 extern bool iToggle;
329 extern int objectPtr;
330 bool inhibit;
331 int bitmapCounter = 0;
332 // *** END OP PROCESSOR TESTING ONLY ***
333
334 //      if (op_pointer) WriteLog(" new op list at 0x%.8x scanline %i\n",op_pointer,scanline);
335         while (op_pointer)
336         {
337 // *** BEGIN OP PROCESSOR TESTING ONLY ***
338 if (interactiveMode && bitmapCounter == objectPtr)
339         inhibit = iToggle;
340 else
341         inhibit = false;
342 // *** END OP PROCESSOR TESTING ONLY ***
343                 if (objectp_stop_reading_list)
344                         return;
345                         
346                 uint64 p0 = op_load_phrase(op_pointer);
347                 op_pointer += 8;
348 if (scanline == tom_get_vdb() && op_start_log)
349 //if (scanline == 215 && op_start_log)
350 {
351 WriteLog("%08X --> phrase %08X %08X", op_pointer - 8, (int)(p0>>32), (int)(p0&0xFFFFFFFF));
352 if ((p0 & 0x07) == OBJECT_TYPE_BITMAP)
353 {
354 WriteLog(" (BITMAP) ");
355 uint64 p1 = op_load_phrase(op_pointer);
356 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer, (int)(p1>>32), (int)(p1&0xFFFFFFFF));
357         uint8 bitdepth = (p1 >> 12) & 0x07;
358         int16 ypos = ((p0 >> 3) & 0x3FF);                       // ??? What if not interlaced (/2)?
359 int32 xpos = p1 & 0xFFF;
360 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
361         uint32 iwidth = ((p1 >> 28) & 0x3FF);
362         uint32 dwidth = ((p1 >> 18) & 0x3FF);           // Unsigned!
363         uint16 height = ((p0 >> 14) & 0x3FF);
364         uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
365         uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
366         uint32 firstPix = (p1 >> 49) & 0x3F;
367         uint8 flags = (p1 >> 45) & 0x0F;
368         uint8 idx = (p1 >> 38) & 0x7F;
369         uint32 pitch = (p1 >> 15) & 0x07;
370 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",
371         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);
372 }
373 if ((p0 & 0x07) == OBJECT_TYPE_SCALE)
374 {
375 WriteLog(" (SCALED BITMAP)");
376 uint64 p1 = op_load_phrase(op_pointer), p2 = op_load_phrase(op_pointer+8);
377 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer, (int)(p1>>32), (int)(p1&0xFFFFFFFF));
378 WriteLog("\n%08X --> phrase %08X %08X ", op_pointer+8, (int)(p2>>32), (int)(p2&0xFFFFFFFF));
379         uint8 bitdepth = (p1 >> 12) & 0x07;
380         int16 ypos = ((p0 >> 3) & 0x3FF);                       // ??? What if not interlaced (/2)?
381 int32 xpos = p1 & 0xFFF;
382 xpos = (xpos & 0x800 ? xpos | 0xFFFFF000 : xpos);
383         uint32 iwidth = ((p1 >> 28) & 0x3FF);
384         uint32 dwidth = ((p1 >> 18) & 0x3FF);           // Unsigned!
385         uint16 height = ((p0 >> 14) & 0x3FF);
386         uint32 link = ((p0 >> 24) & 0x7FFFF) << 3;
387         uint32 ptr = ((p0 >> 43) & 0x1FFFFF) << 3;
388         uint32 firstPix = (p1 >> 49) & 0x3F;
389         uint8 flags = (p1 >> 45) & 0x0F;
390         uint8 idx = (p1 >> 38) & 0x7F;
391         uint32 pitch = (p1 >> 15) & 0x07;
392 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",
393         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);
394         uint32 hscale = p2 & 0xFF;
395         uint32 vscale = (p2 >> 8) & 0xFF;
396         uint32 remainder = (p2 >> 16) & 0xFF;
397 WriteLog("    [hsc: %02X, vsc: %02X, rem: %02X]\n", hscale, vscale, remainder);
398 }
399 if ((p0 & 0x07) == OBJECT_TYPE_GPU)
400 WriteLog(" (GPU)\n");
401 if ((p0 & 0x07) == OBJECT_TYPE_BRANCH)
402 {
403 WriteLog(" (BRANCH)\n");
404 uint8 * jaguar_mainRam = GetRamPtr();
405 WriteLog("[RAM] --> ");
406 for(int k=0; k<8; k++)
407         WriteLog("%02X ", jaguar_mainRam[op_pointer-8 + k]);
408 WriteLog("\n");
409 }
410 if ((p0 & 0x07) == OBJECT_TYPE_STOP)
411 WriteLog("    --> List end\n");
412 }//*/
413                 
414 //              WriteLog("%08X type %i\n", op_pointer, (uint8)p0 & 0x07);               
415                 switch ((uint8)p0 & 0x07)
416                 {
417                 case OBJECT_TYPE_BITMAP:
418                 {
419                         // Would *not* be /2 if interlaced...!
420                         uint16 ypos = ((p0 >> 3) & 0x3FF) / 2;
421 // This is only theory implied by Rayman...!
422 // It seems that if the YPOS is zero, then bump the YPOS value so that it coincides with
423 // the VDB value. With interlacing, this would be slightly more tricky.
424 // There's probably another bit somewhere that enables this mode--but so far, doesn't seem
425 // to affect any other game in a negative way (that I've seen).
426 // Either that, or it's an undocumented bug...
427
428 //No, the reason this was needed is that the OP code before was wrong. Any value
429 //less than VDB will get written to the top line of the display!
430 //                      if (ypos == 0)
431 //                              ypos = TOMReadWord(0xF00046, OP) / 2;                   // Get the VDB value
432                         uint32 height = (p0 & 0xFFC000) >> 14;
433                         uint32 oldOPP = op_pointer - 8;
434 // *** BEGIN OP PROCESSOR TESTING ONLY ***
435 if (inhibit && op_start_log)
436         WriteLog("!!! ^^^ This object is INHIBITED! ^^^ !!!\n");
437 bitmapCounter++;
438 if (!inhibit)   // For OP testing only!
439 // *** END OP PROCESSOR TESTING ONLY ***
440                         if (scanline >= ypos && height > 0)
441                         {
442                                 uint64 p1 = op_load_phrase(op_pointer);
443                                 op_pointer += 8;
444 //WriteLog("OP: Writing scanline %d with ypos == %d...\n", scanline, ypos);
445 //WriteLog("--> Writing %u BPP bitmap...\n", op_bitmap_bit_depth[(p1 >> 12) & 0x07]);
446                                 OPProcessFixedBitmap(scanline, p0, p1, render);
447
448                                 // OP write-backs
449
450 //???Does this really happen??? Doesn't seem to work if you do this...!
451 //                              uint32 link = (p0 & 0x7FFFF000000) >> 21;
452 //                              SET16(objectp_ram, 0x20, link & 0xFFFF);        // OLP
453 //                              SET16(objectp_ram, 0x22, link >> 16);
454 /*                              uint32 height = (p0 & 0xFFC000) >> 14;
455                                 if (height - 1 > 0)
456                                         height--;*/
457                                 // NOTE: Would subtract 2 if in interlaced mode...!
458 //                              uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
459 //                              if (height)
460                                         height--;
461
462                                 uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
463                                 uint64 dwidth = (p1 & 0xFFC0000) >> 15;
464                                 data += dwidth;
465
466                                 p0 &= ~0xFFFFF80000FFC000;                      // Mask out old data...
467                                 p0 |= (uint64)height << 14;
468                                 p0 |= data << 40;
469                                 OPStorePhrase(oldOPP, p0);
470                         }
471                         op_pointer = (p0 & 0x000007FFFF000000) >> 21;
472                         break;
473                 }
474                 case OBJECT_TYPE_SCALE:
475                 {
476                         // Would *not* be /2 if interlaced...!
477                         uint16 ypos = ((p0 >> 3) & 0x3FF) / 2;
478 // This is only theory implied by Rayman...!
479 // It seems that if the YPOS is zero, then bump the YPOS value so that it coincides with
480 // the VDB value. With interlacing, this would be slightly more tricky.
481 // There's probably another bit somewhere that enables this mode--but so far, doesn't seem
482 // to affect any other game in a negative way (that I've seen).
483 // Either that, or it's an undocumented bug...
484
485 //No, the reason this was needed is that the OP code before was wrong. Any value
486 //less than VDB will get written to the top line of the display!
487 //                      if (ypos == 0)
488 //                              ypos = TOMReadWord(0xF00046, OP) / 2;                   // Get the VDB value
489                         uint32 height = (p0 & 0xFFC000) >> 14;
490                         uint32 oldOPP = op_pointer - 8;
491 // *** BEGIN OP PROCESSOR TESTING ONLY ***
492 if (inhibit && op_start_log)
493 {
494         WriteLog("!!! ^^^ This object is INHIBITED! ^^^ !!! (scanline=%u, ypos=%u, height=%u)\n", scanline, ypos, height);
495         DumpScaledObject(p0, op_load_phrase(op_pointer), op_load_phrase(op_pointer+8));
496 }
497 bitmapCounter++;
498 if (!inhibit)   // For OP testing only!
499 // *** END OP PROCESSOR TESTING ONLY ***
500                         if (scanline >= ypos && height > 0)
501                         {
502                                 uint64 p1 = op_load_phrase(op_pointer);
503                                 op_pointer += 8;
504                                 uint64 p2 = op_load_phrase(op_pointer);
505                                 op_pointer += 8;
506 //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));
507                                 OPProcessScaledBitmap(scanline, p0, p1, p2, render);
508
509                                 // OP write-backs
510
511 //???Does this really happen??? Doesn't seem to work if you do this...!
512 //                              uint32 link = (p0 & 0x7FFFF000000) >> 21;
513 //                              SET16(objectp_ram, 0x20, link & 0xFFFF);        // OLP
514 //                              SET16(objectp_ram, 0x22, link >> 16);
515 /*                              uint32 height = (p0 & 0xFFC000) >> 14;
516                                 if (height - 1 > 0)
517                                         height--;*/
518                                 // NOTE: Would subtract 2 if in interlaced mode...!
519 //                              uint64 height = ((p0 & 0xFFC000) - 0x4000) & 0xFFC000;
520
521                                 uint8 remainder = p2 >> 16, vscale = p2 >> 8;
522 //Actually, we should skip this object if it has a vscale of zero.
523 //Or do we? Not sure... Atari Karts has a few lines that look like:
524 // (SCALED BITMAP)
525 //000E8268 --> phrase 00010000 7000B00D 
526 //    [7 (0) x 1 @ (13, 0) (8 bpp), l: 000E82A0, p: 000E0FC0 fp: 00, fl:RELEASE, idx:00, pt:01]
527 //    [hsc: 9A, vsc: 00, rem: 00]
528 // Could it be the vscale is overridden if the DWIDTH is zero? Hmm...
529
530                                 if (vscale == 0)
531                                         vscale = 0x20;                                  // OP bug??? Nope, it isn't...! Or is it?
532
533                                 remainder -= 0x20;                                      // 1.0f in [3.5] fixed point format
534                                 if (remainder & 0x80)                           // I.e., it's negative
535                                 {
536                                         uint64 data = (p0 & 0xFFFFF80000000000) >> 40;
537                                         uint64 dwidth = (p1 & 0xFFC0000) >> 15;
538
539                                         while (remainder & 0x80)
540                                         {
541                                                 remainder += vscale;
542                                                 if (height)
543                                                         height--;
544
545                                                 data += dwidth;
546                                         }
547                                         p0 &= ~0xFFFFF80000FFC000;              // Mask out old data...
548                                         p0 |= (uint64)height << 14;
549                                         p0 |= data << 40;
550                                         OPStorePhrase(oldOPP, p0);
551                                 }
552
553 //WriteLog(" [%08X%08X -> ", (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
554                                 p2 &= ~0x0000000000FF0000;
555                                 p2 |= (uint64)remainder << 16;
556 //WriteLog("%08X%08X]\n", (uint32)(p2>>32), (uint32)(p2&0xFFFFFFFF));
557                                 OPStorePhrase(oldOPP+16, p2);
558 //remainder = (uint8)(p2 >> 16), vscale = (uint8)(p2 >> 8);
559 //WriteLog(" [after]: rem=%02X, vscale=%02X\n", remainder, vscale);
560                         }
561                         op_pointer = (p0 & 0x000007FFFF000000) >> 21;
562                         break;
563                 }
564                 case OBJECT_TYPE_GPU:
565                 {
566 //WriteLog("OP: Asserting GPU IRQ #3...\n");
567                         op_set_current_object(p0);
568                         GPUSetIRQLine(3, ASSERT_LINE);
569 //Also, OP processing is suspended from this point until OBF (F00026) is written to...
570 // !!! FIX !!!
571 //Do something like:
572 //OPSuspendedByGPU = true;
573 //Dunno if the OP keeps processing from where it was interrupted, or if it just continues
574 //on the next scanline...
575                         break;
576                 }
577                 case OBJECT_TYPE_BRANCH:
578                 {
579                         uint16 ypos = (p0 >> 3) & 0x7FF;
580                         uint8  cc   = (p0 >> 14) & 0x03;
581                         uint32 link = (p0 >> 21) & 0x3FFFF8;
582                         
583 //                      if ((ypos!=507)&&(ypos!=25))
584 //                              WriteLog("\t%i%s%i link=0x%.8x\n",scanline,condition_to_str[cc],ypos>>1,link);
585                         switch (cc)
586                         {
587                         case CONDITION_EQUAL:
588 //Why do this for the equal case? If they wrote an odd YPOS, then it wouldn't be detected!
589 //                              if (ypos != 0x7FF && (ypos & 0x01))
590 //                                      ypos ^= 0x01;
591 //                              if ((2 * tom_get_scanline()) == ypos || ypos == 0x7FF)
592 //Here we're using VC instead of the bogus tom_get_scanline() value...
593                                 if (TOMReadWord(0xF00006, OP) == ypos || ypos == 0x7FF)
594                                         op_pointer = link;
595                                 break;
596                         case CONDITION_LESS_THAN:
597 //                              if ((2 * tom_get_scanline()) < ypos)
598                                 if (TOMReadWord(0xF00006, OP) < ypos)
599                                         op_pointer = link;
600                                 break;
601                         case CONDITION_GREATER_THAN:
602 //                              if ((2 * tom_get_scanline()) > ypos)
603                                 if (TOMReadWord(0xF00006, OP) > ypos)
604                                         op_pointer = link;
605                                 break;
606                         case CONDITION_OP_FLAG_SET:
607                                 if (op_get_status_register() & 0x01)
608                                         op_pointer = link;
609                                 break;
610                         case CONDITION_SECOND_HALF_LINE:
611                                 // This basically means branch if bit 10 of HC is set
612                                 WriteLog("OP: Unexpected CONDITION_SECOND_HALF_LINE in BRANCH object\nOP: shuting down\n");
613                                 fclose(log_get());
614                                 exit(0);
615                                 break;
616                         default:
617                                 WriteLog("OP: Unimplemented branch condition %i\n", cc);
618                         }
619                         break;
620                 }
621                 case OBJECT_TYPE_STOP:
622                 {
623 //op_start_log = 0;
624                         // unsure
625 //WriteLog("OP: --> STOP\n");
626 //                      op_set_status_register(((p0>>3) & 0xFFFFFFFF));
627 //This seems more likely...
628                         op_set_current_object(p0);
629                         
630                         if (p0 & 0x08)
631                         {
632                                 tom_set_pending_object_int();
633                                 if (tom_irq_enabled(IRQ_OPFLAG) && jaguar_interrupt_handler_is_valid(64))
634                                         m68k_set_irq(7);                                // Cause an NMI to occur...
635                         }
636
637                         return;
638 //                      break;
639                 }
640                 default:
641                         WriteLog("op: unknown object type %i\n", ((uint8)p0 & 0x07)); 
642                         return;
643                 }
644         }
645 }
646
647 //
648 // Store fixed size bitmap in line buffer
649 //
650
651 // Interesting thing about Rayman: There seems to be a transparent bitmap (1/8/16 bpp--which?)
652 // being rendered under his feet--doesn't align when walking... Check it out!
653
654 void OPProcessFixedBitmap(int scanline, uint64 p0, uint64 p1, bool render)
655 {
656 // Need to make sure that when writing that it stays within the line buffer...
657 // LBUF ($F01800 - $F01D9E) 360 x 32-bit RAM
658         uint8 depth = (p1 >> 12) & 0x07;                                // Color depth of image
659         int32 xpos = ((int16)((p1 << 4) & 0xFFFF)) >> 4;// Image xpos in LBUF
660         uint32 iwidth = (p1 >> 28) & 0x3FF;                             // Image width in *phrases*
661         uint32 data = (p0 >> 40) & 0xFFFFF8;                    // Pixel data address
662 //#ifdef OP_DEBUG_BMP
663 // Prolly should use this... Though not sure exactly how.
664         uint32  firstPix = (p1 >> 49) & 0x3F;
665         // "The LSB is significant only for scaled objects..." -JTRM
666         // "In 1 BPP mode, all five bits are significant. In 2 BPP mode, the top four are significant..."
667         firstPix &= 0x3E;
668 //#endif
669 // We can ignore the RELEASE (high order) bit for now--probably forever...!
670 //      uint8 flags = (p1 >> 45) & 0x0F;        // REFLECT, RMW, TRANS, RELEASE
671 //Optimize: break these out to their own BOOL values
672         uint8 flags = (p1 >> 45) & 0x07;                                // REFLECT (0), RMW (1), TRANS (2)
673         bool flagREFLECT = (flags & OPFLAG_REFLECT ? true : false),
674                 flagRMW = (flags & OPFLAG_RMW ? true : false),
675                 flagTRANS = (flags & OPFLAG_TRANS ? true : false);
676 // "For images with 1 to 4 bits/pixel the top 7 to 4 bits of the index
677 //  provide the most significant bits of the palette address."
678         uint8 index = (p1 >> 37) & 0xFE;                                // CLUT index offset (upper pix, 1-4 bpp)
679         uint32 pitch = (p1 >> 15) & 0x07;                               // Phrase pitch
680
681 //      int16 scanlineWidth = tom_getVideoModeWidth();
682         uint8 * tom_ram_8 = tom_get_ram_pointer();
683         uint8 * paletteRAM = &tom_ram_8[0x400];
684         // This is OK as long as it's used correctly: For 16-bit RAM to RAM direct copies--NOT
685         // for use when using endian-corrected data (i.e., any of the *_word_read functions!)
686         uint16 * paletteRAM16 = (uint16 *)paletteRAM;
687
688 //      WriteLog("bitmap %ix? %ibpp at %i,? firstpix=? data=0x%.8x pitch %i hflipped=%s dwidth=? (linked to ?) RMW=%s Tranparent=%s\n",
689 //              iwidth, op_bitmap_bit_depth[bitdepth], xpos, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), (flags&OPFLAG_RMW ? "yes" : "no"), (flags&OPFLAG_TRANS ? "yes" : "no"));
690
691 // Is it OK to have a 0 for the data width??? (i.e., undocumented?)
692 // Seems to be... Seems that dwidth *can* be zero (i.e., reuse same line) as well.
693 // Pitch == 0 is OK too...
694 //      if (!render || op_pointer == 0 || dwidth == 0 || ptr == 0 || pitch == 0)
695 //I'm not convinced that we need to concern ourselves with data & op_pointer here either!
696         if (!render || iwidth == 0) // || data == 0 || op_pointer == 0)
697                 return;
698
699 //#define OP_DEBUG_BMP
700 //#ifdef OP_DEBUG_BMP
701 //      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",
702 //              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"));
703 //#endif
704
705 //      int32 leftMargin = xpos, rightMargin = (xpos + (phraseWidthToPixels[depth] * iwidth)) - 1;
706         int32 startPos = xpos, endPos = xpos +
707                 (!flagREFLECT ? (phraseWidthToPixels[depth] * iwidth) - 1
708                 : -((phraseWidthToPixels[depth] * iwidth) + 1));
709         uint32 clippedWidth = 0, phraseClippedWidth = 0, dataClippedWidth = 0;//, phrasePixel = 0;
710         bool in24BPPMode = (((GET16(tom_ram_8, 0x0028) >> 1) & 0x03) == 1 ? true : false);      // VMODE
711         // Not sure if this is Jaguar Two only location or what...
712         // From the docs, it is... If we want to limit here we should think of something else.
713 //      int32 limit = GET16(tom_ram_8, 0x0008);                 // LIMIT
714         int32 limit = 720;
715         int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
716
717         // If the image is completely to the left or right of the line buffer, then bail.
718 //If in REFLECT mode, then these values are swapped! !!! FIX !!! [DONE]
719 //There are four possibilities:
720 //  1. image sits on left edge and no REFLECT; starts out of bounds but ends in bounds.
721 //  2. image sits on left edge and REFLECT; starts in bounds but ends out of bounds.
722 //  3. image sits on right edge and REFLECT; starts out of bounds but ends in bounds.
723 //  4. image sits on right edge and no REFLECT; starts in bounds but ends out of bounds.
724 //Numbers 2 & 4 can be caught by checking the LBUF clip while in the inner loop,
725 // numbers 1 & 3 are of concern.
726 // This *indirectly* handles only cases 2 & 4! And is WRONG is REFLECT is set...!
727 //      if (rightMargin < 0 || leftMargin > lbufWidth)
728
729 // It might be easier to swap these (if REFLECTed) and just use XPOS down below...
730 // That way, you could simply set XPOS to leftMargin if !REFLECT and to rightMargin otherwise.
731 // Still have to be careful with the DATA and IWIDTH values though...
732
733 //      if ((!flagREFLECT && (rightMargin < 0 || leftMargin > lbufWidth))
734 //              || (flagREFLECT && (leftMargin < 0 || rightMargin > lbufWidth)))
735 //              return;
736         if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
737                 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
738                 return;
739
740         // Otherwise, find the clip limits and clip the phrase as well...
741         // NOTE: I'm fudging here by letting the actual blit overstep the bounds of the
742         //       line buffer, but it shouldn't matter since there are two unused line
743         //       buffers below and nothing above and I'll at most write 8 bytes outside
744         //       the line buffer... I could use a fractional clip begin/end value, but
745         //       this makes the blit a *lot* more hairy. I might fix this in the future
746         //       if it becomes necessary. (JLH)
747         //       Probably wouldn't be *that* hairy. Just use a delta that tells the inner loop
748         //       which pixel in the phrase is being written, and quit when either end of phrases
749         //       is reached or line buffer extents are surpassed.
750
751 //This stuff is probably wrong as well... !!! FIX !!!
752 //The strange thing is that it seems to work, but that's no guarantee that it's bulletproof!
753 //Yup. Seems that JagMania doesn't work correctly with this...
754 //Dunno if this is the problem, but Atari Karts is showing *some* of the road now...
755 //      if (!flagREFLECT)
756
757 /*
758         if (leftMargin < 0)
759                 clippedWidth = 0 - leftMargin,
760                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
761                 leftMargin = 0 - (clippedWidth % phraseWidthToPixels[depth]);
762 //              leftMargin = 0;
763
764         if (rightMargin > lbufWidth)
765                 clippedWidth = rightMargin - lbufWidth,
766                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];//,
767 //              rightMargin = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);
768 //              rightMargin = lbufWidth;
769 */
770 if (depth > 5)
771         WriteLog("We're about to encounter a divide by zero error!\n");
772         // NOTE: We're just using endPos to figure out how much, if any, to clip by.
773         // ALSO: There may be another case where we start out of bounds and end out of bounds...!
774         if (startPos < 0)                       // Case #1: Begin out, end in, L to R
775                 clippedWidth = 0 - startPos,
776                 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
777                 startPos = 0 - (clippedWidth % phraseWidthToPixels[depth]);
778
779         if (endPos < 0)                         // Case #2: Begin in, end out, R to L
780                 clippedWidth = 0 - endPos,
781                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
782
783         if (endPos > lbufWidth)         // Case #3: Begin in, end out, L to R
784                 clippedWidth = endPos - lbufWidth,
785                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];
786
787         if (startPos > lbufWidth)       // Case #4: Begin out, end in, R to L
788                 clippedWidth = startPos - lbufWidth,
789                 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
790                 startPos = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);
791
792         // If the image is sitting on the line buffer left or right edge, we need to compensate
793         // by decreasing the image phrase width accordingly.
794         iwidth -= phraseClippedWidth;
795
796         // Also, if we're clipping the phrase we need to make sure we're in the correct part of
797         // the pixel data.
798 //      data += phraseClippedWidth * (pitch << 3);
799         data += dataClippedWidth * (pitch << 3);
800
801         // NOTE: When the bitmap is in REFLECT mode, the XPOS marks the *right* side of the
802         //       bitmap! This makes clipping & etc. MUCH, much easier...!
803 //      uint32 lbufAddress = 0x1800 + (!in24BPPMode ? leftMargin * 2 : leftMargin * 4);
804         uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
805         uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
806
807         // Render.
808
809 // Hmm. We check above for 24 BPP mode, but don't do anything about it below...
810 // If we *were* in 24 BPP mode, how would you convert CRY to RGB24? Seems to me
811 // that if you're in CRY mode then you wouldn't be able to use 24 BPP bitmaps
812 // anyway.
813
814         if (depth == 0)                                                                 // 1 BPP
815         {
816                 // The LSB of flags is OPFLAG_REFLECT, so sign extend it and or 2 into it.
817                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
818
819                 // Fetch 1st phrase...
820                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
821 //Note that firstPix should only be honored *if* we start with the 1st phrase of the bitmap
822 //i.e., we didn't clip on the margin...
823                 pixels <<= firstPix;                                            // Skip first N pixels (N=firstPix)...
824                 int i = firstPix;                                                       // Start counter at right spot...
825
826                 while (iwidth--)
827                 {
828                         while (i++ < 64)
829                         {
830                                 uint8 bit = pixels >> 63;
831                                 if (flagTRANS && bit == 0)
832                                         ;       // Do nothing...
833                                 else
834                                 {
835                                         if (!flagRMW)
836 //Optimize: Set palleteRAM16 to beginning of palette RAM + index*2 and use only [bit] as index...
837 //Won't optimize RMW case though...
838                                                 // This is the *only* correct use of endian-dependent code
839                                                 // (i.e., mem-to-mem direct copying)!
840                                                 *(uint16 *)currentLineBuffer = paletteRAM16[index | bit];
841                                         else
842                                                 *currentLineBuffer = 
843                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bit) << 1]),
844                                                 *(currentLineBuffer + 1) = 
845                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bit) << 1) + 1]);
846                                 }
847
848                                 currentLineBuffer += lbufDelta;
849                                 pixels <<= 1;
850                         }
851                         i = 0;
852                         // Fetch next phrase...
853                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
854                         pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
855                 }
856         }
857         else if (depth == 1)                                                    // 2 BPP
858         {
859 if (firstPix)
860         WriteLog("OP: Fixed bitmap @ 2 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
861                 index &= 0xFC;                                                          // Top six bits form CLUT index
862                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
863                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
864
865                 while (iwidth--)
866                 {
867                         // Fetch phrase...
868                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
869                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
870
871                         for(int i=0; i<32; i++)
872                         {
873                                 uint8 bits = pixels >> 62;
874 // Seems to me that both of these are in the same endian, so we could cast it as
875 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
876 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
877 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
878 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
879                                 if (flagTRANS && bits == 0)
880                                         ;       // Do nothing...
881                                 else
882                                 {
883                                         if (!flagRMW)
884                                                 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
885                                         else
886                                                 *currentLineBuffer = 
887                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
888                                                 *(currentLineBuffer + 1) = 
889                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
890                                 }
891
892                                 currentLineBuffer += lbufDelta;
893                                 pixels <<= 2;
894                         }
895                 }
896         }
897         else if (depth == 2)                                                    // 4 BPP
898         {
899 if (firstPix)
900         WriteLog("OP: Fixed bitmap @ 4 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
901                 index &= 0xF0;                                                          // Top four bits form CLUT index
902                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
903                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
904
905                 while (iwidth--)
906                 {
907                         // Fetch phrase...
908                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
909                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
910
911                         for(int i=0; i<16; i++)
912                         {
913                                 uint8 bits = pixels >> 60;
914 // Seems to me that both of these are in the same endian, so we could cast it as
915 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
916 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
917 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
918 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
919                                 if (flagTRANS && bits == 0)
920                                         ;       // Do nothing...
921                                 else
922                                 {
923                                         if (!flagRMW)
924                                                 *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
925                                         else
926                                                 *currentLineBuffer = 
927                                                         BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
928                                                 *(currentLineBuffer + 1) = 
929                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
930                                 }
931
932                                 currentLineBuffer += lbufDelta;
933                                 pixels <<= 4;
934                         }
935                 }
936         }
937         else if (depth == 3)                                                    // 8 BPP
938         {
939 if (firstPix)
940         WriteLog("OP: Fixed bitmap @ 8 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
941                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
942                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
943
944                 while (iwidth--)
945                 {
946                         // Fetch phrase...
947                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
948                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
949
950                         for(int i=0; i<8; i++)
951                         {
952                                 uint8 bits = pixels >> 56;
953 // Seems to me that both of these are in the same endian, so we could cast it as
954 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
955 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
956 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
957 // No, it isn't because we read the memory in an endian safe way--this *won't* work...
958                                 if (flagTRANS && bits == 0)
959                                         ;       // Do nothing...
960                                 else
961                                 {
962                                         if (!flagRMW)
963                                                 *(uint16 *)currentLineBuffer = paletteRAM16[bits];
964                                         else
965                                                 *currentLineBuffer = 
966                                                         BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
967                                                 *(currentLineBuffer + 1) = 
968                                                         BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
969                                 }
970
971                                 currentLineBuffer += lbufDelta;
972                                 pixels <<= 8;
973                         }
974                 }
975         }
976         else if (depth == 4)                                                    // 16 BPP
977         {
978 if (firstPix)
979         WriteLog("OP: Fixed bitmap @ 16 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
980                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
981                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
982
983                 while (iwidth--)
984                 {
985                         // Fetch phrase...
986                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
987                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
988
989                         for(int i=0; i<4; i++)
990                         {
991                                 uint8 bitsHi = pixels >> 56, bitsLo = pixels >> 48;
992 // Seems to me that both of these are in the same endian, so we could cast it as
993 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
994 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
995 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
996 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
997                                 if (flagTRANS && (bitsLo | bitsHi) == 0)
998                                         ;       // Do nothing...
999                                 else
1000                                 {
1001                                         if (!flagRMW)
1002                                                 *currentLineBuffer = bitsHi,
1003                                                 *(currentLineBuffer + 1) = bitsLo;
1004                                         else
1005                                                 *currentLineBuffer = 
1006                                                         BLEND_CR(*currentLineBuffer, bitsHi),
1007                                                 *(currentLineBuffer + 1) = 
1008                                                         BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1009                                 }
1010
1011                                 currentLineBuffer += lbufDelta;
1012                                 pixels <<= 16;
1013                         }
1014                 }
1015         }
1016         else if (depth == 5)                                                    // 24 BPP
1017         {
1018 //Looks like Iron Soldier is the only game that uses 24BPP mode...
1019 //There *might* be others...
1020 //WriteLog("OP: Writing 24 BPP bitmap!\n");
1021 if (firstPix)
1022         WriteLog("OP: Fixed bitmap @ 24 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
1023                 // Not sure, but I think RMW only works with 16 BPP and below, and only in CRY mode...
1024                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 4 into it.
1025                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 4) | 0x04;
1026
1027                 while (iwidth--)
1028                 {
1029                         // Fetch phrase...
1030                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1031                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
1032
1033                         for(int i=0; i<2; i++)
1034                         {
1035                                 uint8 bits3 = pixels >> 56, bits2 = pixels >> 48,
1036                                         bits1 = pixels >> 40, bits0 = pixels >> 32;
1037 // Seems to me that both of these are in the same endian, so we could cast it as
1038 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
1039 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
1040 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
1041 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
1042                                 if (flagTRANS && (bits3 | bits2 | bits1 | bits0) == 0)
1043                                         ;       // Do nothing...
1044                                 else
1045                                         *currentLineBuffer = bits3,
1046                                         *(currentLineBuffer + 1) = bits2,
1047                                         *(currentLineBuffer + 2) = bits1,
1048                                         *(currentLineBuffer + 3) = bits0;
1049
1050                                 currentLineBuffer += lbufDelta;
1051                                 pixels <<= 32;
1052                         }
1053                 }
1054         }
1055 }
1056
1057 //
1058 // Store scaled bitmap in line buffer
1059 //
1060 void OPProcessScaledBitmap(int scanline, uint64 p0, uint64 p1, uint64 p2, bool render)
1061 {
1062 // Need to make sure that when writing that it stays within the line buffer...
1063 // LBUF ($F01800 - $F01D9E) 360 x 32-bit RAM
1064         uint8 depth = (p1 >> 12) & 0x07;                                // Color depth of image
1065         int32 xpos = ((int16)((p1 << 4) & 0xFFFF)) >> 4;// Image xpos in LBUF
1066         uint32 iwidth = (p1 >> 28) & 0x3FF;                             // Image width in *phrases*
1067         uint32 data = (p0 >> 40) & 0xFFFFF8;                    // Pixel data address
1068 //#ifdef OP_DEBUG_BMP
1069 // Prolly should use this... Though not sure exactly how.
1070 //Use the upper bits as an offset into the phrase depending on the BPP. That's how!
1071         uint32 firstPix = (p1 >> 49) & 0x3F;
1072 //This is WEIRD! I'm sure I saw Atari Karts request 8 BPP FIRSTPIX! What happened???
1073 if (firstPix)
1074         WriteLog("OP: FIRSTPIX != 0!\n");
1075 //#endif
1076 // We can ignore the RELEASE (high order) bit for now--probably forever...!
1077 //      uint8 flags = (p1 >> 45) & 0x0F;        // REFLECT, RMW, TRANS, RELEASE
1078 //Optimize: break these out to their own BOOL values
1079         uint8 flags = (p1 >> 45) & 0x07;                                // REFLECT (0), RMW (1), TRANS (2)
1080         bool flagREFLECT = (flags & OPFLAG_REFLECT ? true : false),
1081                 flagRMW = (flags & OPFLAG_RMW ? true : false),
1082                 flagTRANS = (flags & OPFLAG_TRANS ? true : false);
1083         uint8 index = (p1 >> 37) & 0xFE;                                // CLUT index offset (upper pix, 1-4 bpp)
1084         uint32 pitch = (p1 >> 15) & 0x07;                               // Phrase pitch
1085
1086 //      int16 scanlineWidth = tom_getVideoModeWidth();
1087         uint8 * tom_ram_8 = tom_get_ram_pointer();
1088         uint8 * paletteRAM = &tom_ram_8[0x400];
1089         // This is OK as long as it's used correctly: For 16-bit RAM to RAM direct copies--NOT
1090         // for use when using endian-corrected data (i.e., any of the *_word_read functions!)
1091         uint16 * paletteRAM16 = (uint16 *)paletteRAM;
1092
1093         uint8 hscale = p2 & 0xFF;
1094         uint8 horizontalRemainder = hscale;                             // Not sure if it starts full, but seems reasonable
1095         int32 scaledWidthInPixels = (iwidth * phraseWidthToPixels[depth] * hscale) >> 5;
1096         uint32 scaledPhrasePixels = (phraseWidthToPixels[depth] * hscale) >> 5;
1097
1098 //      WriteLog("bitmap %ix? %ibpp at %i,? firstpix=? data=0x%.8x pitch %i hflipped=%s dwidth=? (linked to ?) RMW=%s Tranparent=%s\n",
1099 //              iwidth, op_bitmap_bit_depth[bitdepth], xpos, ptr, pitch, (flags&OPFLAG_REFLECT ? "yes" : "no"), (flags&OPFLAG_RMW ? "yes" : "no"), (flags&OPFLAG_TRANS ? "yes" : "no"));
1100
1101 //Looks like an hscale of zero means don't draw!
1102         if (!render || iwidth == 0 || hscale == 0)
1103                 return;
1104
1105 //#define OP_DEBUG_BMP
1106 //#ifdef OP_DEBUG_BMP
1107 //      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",
1108 //              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"));
1109 //#endif
1110
1111         int32 startPos = xpos, endPos = xpos +
1112                 (!flagREFLECT ? scaledWidthInPixels - 1 : -(scaledWidthInPixels + 1));
1113         uint32 clippedWidth = 0, phraseClippedWidth = 0, dataClippedWidth = 0;
1114         bool in24BPPMode = (((GET16(tom_ram_8, 0x0028) >> 1) & 0x03) == 1 ? true : false);      // VMODE
1115         // Not sure if this is Jaguar Two only location or what...
1116         // From the docs, it is... If we want to limit here we should think of something else.
1117 //      int32 limit = GET16(tom_ram_8, 0x0008);                 // LIMIT
1118         int32 limit = 720;
1119         int32 lbufWidth = (!in24BPPMode ? limit - 1 : (limit / 2) - 1); // Zero based limit...
1120
1121         // If the image is completely to the left or right of the line buffer, then bail.
1122 //If in REFLECT mode, then these values are swapped! !!! FIX !!! [DONE]
1123 //There are four possibilities:
1124 //  1. image sits on left edge and no REFLECT; starts out of bounds but ends in bounds.
1125 //  2. image sits on left edge and REFLECT; starts in bounds but ends out of bounds.
1126 //  3. image sits on right edge and REFLECT; starts out of bounds but ends in bounds.
1127 //  4. image sits on right edge and no REFLECT; starts in bounds but ends out of bounds.
1128 //Numbers 2 & 4 can be caught by checking the LBUF clip while in the inner loop,
1129 // numbers 1 & 3 are of concern.
1130 // This *indirectly* handles only cases 2 & 4! And is WRONG is REFLECT is set...!
1131 //      if (rightMargin < 0 || leftMargin > lbufWidth)
1132
1133 // It might be easier to swap these (if REFLECTed) and just use XPOS down below...
1134 // That way, you could simply set XPOS to leftMargin if !REFLECT and to rightMargin otherwise.
1135 // Still have to be careful with the DATA and IWIDTH values though...
1136
1137         if ((!flagREFLECT && (endPos < 0 || startPos > lbufWidth))
1138                 || (flagREFLECT && (startPos < 0 || endPos > lbufWidth)))
1139                 return;
1140
1141         // Otherwise, find the clip limits and clip the phrase as well...
1142         // NOTE: I'm fudging here by letting the actual blit overstep the bounds of the
1143         //       line buffer, but it shouldn't matter since there are two unused line
1144         //       buffers below and nothing above and I'll at most write 40 bytes outside
1145         //       the line buffer... I could use a fractional clip begin/end value, but
1146         //       this makes the blit a *lot* more hairy. I might fix this in the future
1147         //       if it becomes necessary. (JLH)
1148         //       Probably wouldn't be *that* hairy. Just use a delta that tells the inner loop
1149         //       which pixel in the phrase is being written, and quit when either end of phrases
1150         //       is reached or line buffer extents are surpassed.
1151
1152 //This stuff is probably wrong as well... !!! FIX !!!
1153 //The strange thing is that it seems to work, but that's no guarantee that it's bulletproof!
1154 //Yup. Seems that JagMania doesn't work correctly with this...
1155 //Dunno if this is the problem, but Atari Karts is showing *some* of the road now...
1156 //Actually, it is! Or, it was. It doesn't seem to be clipping here, so the problem lies
1157 //elsewhere! Hmm. Putting the scaling code into the 1/2/8 BPP cases seems to draw the ground
1158 // a bit more accurately... Strange!
1159 //It's probably a case of the REFLECT flag being set and the background being written
1160 //from the right side of the screen...
1161 //But no, it isn't... At least if the diagnostics are telling the truth!
1162
1163         // NOTE: We're just using endPos to figure out how much, if any, to clip by.
1164         // ALSO: There may be another case where we start out of bounds and end out of bounds...!
1165
1166 //There's a problem here with scaledPhrasePixels in that it can be forced to zero when
1167 //the scaling factor is small. So fix it already! !!! FIX !!!
1168 /*if (scaledPhrasePixels == 0)
1169 {
1170         WriteLog("OP: [Scaled] We're about to encounter a divide by zero error!\n");
1171         DumpScaledObject(p0, p1, p2);
1172 }//*/
1173 //NOTE: I'm almost 100% sure that this is wrong... And it is! :-p
1174         if (startPos < 0)                       // Case #1: Begin out, end in, L to R
1175 /*              clippedWidth = 0 - startPos,
1176                 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
1177                 startPos = 0 - (clippedWidth % phraseWidthToPixels[depth]);*/
1178                 clippedWidth = 0 - startPos,
1179                 dataClippedWidth = phraseClippedWidth = clippedWidth / scaledPhrasePixels,
1180                 startPos = 0 - (clippedWidth % scaledPhrasePixels);
1181
1182         if (endPos < 0)                         // Case #2: Begin in, end out, R to L
1183 /*              clippedWidth = 0 - endPos,
1184                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];*/
1185                 clippedWidth = 0 - endPos,
1186                 phraseClippedWidth = clippedWidth / scaledPhrasePixels;
1187
1188         if (endPos > lbufWidth)         // Case #3: Begin in, end out, L to R
1189 /*              clippedWidth = endPos - lbufWidth,
1190                 phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth];*/
1191                 clippedWidth = endPos - lbufWidth,
1192                 phraseClippedWidth = clippedWidth / scaledPhrasePixels;
1193
1194         if (startPos > lbufWidth)       // Case #4: Begin out, end in, R to L
1195 /*              clippedWidth = startPos - lbufWidth,
1196                 dataClippedWidth = phraseClippedWidth = clippedWidth / phraseWidthToPixels[depth],
1197                 startPos = lbufWidth + (clippedWidth % phraseWidthToPixels[depth]);*/
1198                 clippedWidth = startPos - lbufWidth,
1199                 dataClippedWidth = phraseClippedWidth = clippedWidth / scaledPhrasePixels,
1200                 startPos = lbufWidth + (clippedWidth % scaledPhrasePixels);
1201
1202 extern int op_start_log;
1203 if (op_start_log && clippedWidth != 0)
1204         WriteLog("OP: Clipped line. SP=%i, EP=%i, clip=%u, iwidth=%u, hscale=%02X\n", startPos, endPos, clippedWidth, iwidth, hscale);
1205 if (op_start_log && startPos == 13)
1206 {
1207         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);
1208         DumpScaledObject(p0, p1, p2);
1209 }
1210         // If the image is sitting on the line buffer left or right edge, we need to compensate
1211         // by decreasing the image phrase width accordingly.
1212         iwidth -= phraseClippedWidth;
1213
1214         // Also, if we're clipping the phrase we need to make sure we're in the correct part of
1215         // the pixel data.
1216 //      data += phraseClippedWidth * (pitch << 3);
1217         data += dataClippedWidth * (pitch << 3);
1218
1219         // NOTE: When the bitmap is in REFLECT mode, the XPOS marks the *right* side of the
1220         //       bitmap! This makes clipping & etc. MUCH, much easier...!
1221 //      uint32 lbufAddress = 0x1800 + (!in24BPPMode ? leftMargin * 2 : leftMargin * 4);
1222         uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
1223         uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
1224
1225         // Render.
1226
1227 // Hmm. We check above for 24 BPP mode, but don't do anything about it below...
1228 // If we *were* in 24 BPP mode, how would you convert CRY to RGB24? Seems to me
1229 // that if you're in CRY mode then you wouldn't be able to use 24 BPP bitmaps
1230 // anyway.
1231
1232         if (depth == 0)                                                                 // 1 BPP
1233         {
1234 if (firstPix != 0)
1235         WriteLog("OP: Scaled bitmap @ 1 BPP requesting FIRSTPIX!\n");
1236                 // The LSB of flags is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1237                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1238
1239                 int pixCount = 0;
1240                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1241
1242                 while ((int32)iwidth > 0)
1243                 {
1244                         uint8 bits = pixels >> 63;
1245
1246                         if (flagTRANS && bits == 0)
1247                                 ;       // Do nothing...
1248                         else
1249                         {
1250                                 if (!flagRMW)
1251                                         // This is the *only* correct use of endian-dependent code
1252                                         // (i.e., mem-to-mem direct copying)!
1253                                         *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1254                                 else
1255                                         *currentLineBuffer = 
1256                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1257                                         *(currentLineBuffer + 1) = 
1258                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1259                         }
1260
1261                         currentLineBuffer += lbufDelta;
1262
1263                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1264                         while (horizontalRemainder & 0x80)
1265                         {
1266                                 horizontalRemainder += hscale;
1267                                 pixCount++;
1268                                 pixels <<= 1;
1269                         }
1270
1271                         if (pixCount > 63)
1272                         {
1273                                 int phrasesToSkip = pixCount / 64, pixelShift = pixCount % 64;
1274
1275                                 data += (pitch << 3) * phrasesToSkip;
1276                                 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1277                                 pixels <<= 1 * pixelShift;
1278                                 iwidth -= phrasesToSkip;
1279                                 pixCount = pixelShift;
1280                         }
1281                 }
1282         }
1283         else if (depth == 1)                                                    // 2 BPP
1284         {
1285 if (firstPix != 0)
1286         WriteLog("OP: Scaled bitmap @ 2 BPP requesting FIRSTPIX!\n");
1287                 index &= 0xFC;                                                          // Top six bits form CLUT index
1288                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1289                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1290
1291                 int pixCount = 0;
1292                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1293
1294                 while ((int32)iwidth > 0)
1295                 {
1296                         uint8 bits = pixels >> 62;
1297
1298                         if (flagTRANS && bits == 0)
1299                                 ;       // Do nothing...
1300                         else
1301                         {
1302                                 if (!flagRMW)
1303                                         // This is the *only* correct use of endian-dependent code
1304                                         // (i.e., mem-to-mem direct copying)!
1305                                         *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1306                                 else
1307                                         *currentLineBuffer = 
1308                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1309                                         *(currentLineBuffer + 1) = 
1310                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1311                         }
1312
1313                         currentLineBuffer += lbufDelta;
1314
1315                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1316                         while (horizontalRemainder & 0x80)
1317                         {
1318                                 horizontalRemainder += hscale;
1319                                 pixCount++;
1320                                 pixels <<= 2;
1321                         }
1322
1323                         if (pixCount > 31)
1324                         {
1325                                 int phrasesToSkip = pixCount / 32, pixelShift = pixCount % 32;
1326
1327                                 data += (pitch << 3) * phrasesToSkip;
1328                                 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1329                                 pixels <<= 2 * pixelShift;
1330                                 iwidth -= phrasesToSkip;
1331                                 pixCount = pixelShift;
1332                         }
1333                 }
1334         }
1335         else if (depth == 2)                                                    // 4 BPP
1336         {
1337 if (firstPix != 0)
1338         WriteLog("OP: Scaled bitmap @ 4 BPP requesting FIRSTPIX!\n");
1339                 index &= 0xF0;                                                          // Top four bits form CLUT index
1340                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1341                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1342
1343                 int pixCount = 0;
1344                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1345
1346                 while ((int32)iwidth > 0)
1347                 {
1348                         uint8 bits = pixels >> 60;
1349
1350                         if (flagTRANS && bits == 0)
1351                                 ;       // Do nothing...
1352                         else
1353                         {
1354                                 if (!flagRMW)
1355                                         // This is the *only* correct use of endian-dependent code
1356                                         // (i.e., mem-to-mem direct copying)!
1357                                         *(uint16 *)currentLineBuffer = paletteRAM16[index | bits];
1358                                 else
1359                                         *currentLineBuffer = 
1360                                                 BLEND_CR(*currentLineBuffer, paletteRAM[(index | bits) << 1]),
1361                                         *(currentLineBuffer + 1) = 
1362                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[((index | bits) << 1) + 1]);
1363                         }
1364
1365                         currentLineBuffer += lbufDelta;
1366
1367                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1368                         while (horizontalRemainder & 0x80)
1369                         {
1370                                 horizontalRemainder += hscale;
1371                                 pixCount++;
1372                                 pixels <<= 4;
1373                         }
1374
1375                         if (pixCount > 15)
1376                         {
1377                                 int phrasesToSkip = pixCount / 16, pixelShift = pixCount % 16;
1378
1379                                 data += (pitch << 3) * phrasesToSkip;
1380                                 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1381                                 pixels <<= 4 * pixelShift;
1382                                 iwidth -= phrasesToSkip;
1383                                 pixCount = pixelShift;
1384                         }
1385                 }
1386         }
1387         else if (depth == 3)                                                    // 8 BPP
1388         {
1389 if (firstPix)
1390         WriteLog("OP: Scaled bitmap @ 8 BPP requesting FIRSTPIX! (fp=%u)\n", firstPix);
1391                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 2 into it.
1392                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1393
1394                 int pixCount = 0;
1395                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1396
1397                 while ((int32)iwidth > 0)
1398                 {
1399                         uint8 bits = pixels >> 56;
1400
1401                         if (flagTRANS && bits == 0)
1402                                 ;       // Do nothing...
1403                         else
1404                         {
1405                                 if (!flagRMW)
1406                                         // This is the *only* correct use of endian-dependent code
1407                                         // (i.e., mem-to-mem direct copying)!
1408                                         *(uint16 *)currentLineBuffer = paletteRAM16[bits];
1409                                 else
1410                                         *currentLineBuffer = 
1411                                                 BLEND_CR(*currentLineBuffer, paletteRAM[bits << 1]),
1412                                         *(currentLineBuffer + 1) = 
1413                                                 BLEND_Y(*(currentLineBuffer + 1), paletteRAM[(bits << 1) + 1]);
1414                         }
1415
1416                         currentLineBuffer += lbufDelta;
1417
1418                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1419                         while (horizontalRemainder & 0x80)
1420                         {
1421                                 horizontalRemainder += hscale;
1422                                 pixCount++;
1423                                 pixels <<= 8;
1424                         }
1425
1426                         if (pixCount > 7)
1427                         {
1428                                 int phrasesToSkip = pixCount / 8, pixelShift = pixCount % 8;
1429
1430                                 data += (pitch << 3) * phrasesToSkip;
1431                                 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1432                                 pixels <<= 8 * pixelShift;
1433                                 iwidth -= phrasesToSkip;
1434                                 pixCount = pixelShift;
1435                         }
1436                 }
1437         }
1438         else if (depth == 4)                                                    // 16 BPP
1439         {
1440 if (firstPix != 0)
1441         WriteLog("OP: Scaled bitmap @ 16 BPP requesting FIRSTPIX!\n");
1442                 // The LSB is OPFLAG_REFLECT, so sign extend it and OR 2 into it.
1443                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 5) | 0x02;
1444
1445                 int pixCount = 0;
1446                 uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1447
1448                 while ((int32)iwidth > 0)
1449                 {
1450                         uint8 bitsHi = pixels >> 56, bitsLo = pixels >> 48;
1451
1452                         if (flagTRANS && (bitsLo | bitsHi) == 0)
1453                                 ;       // Do nothing...
1454                         else
1455                         {
1456                                 if (!flagRMW)
1457                                         *currentLineBuffer = bitsHi,
1458                                         *(currentLineBuffer + 1) = bitsLo;
1459                                 else
1460                                         *currentLineBuffer = 
1461                                                 BLEND_CR(*currentLineBuffer, bitsHi),
1462                                         *(currentLineBuffer + 1) = 
1463                                                 BLEND_Y(*(currentLineBuffer + 1), bitsLo);
1464                         }
1465
1466                         currentLineBuffer += lbufDelta;
1467
1468                         horizontalRemainder -= 0x20;            // Subtract 1.0f in [3.5] fixed point format
1469                         while (horizontalRemainder & 0x80)
1470                         {
1471                                 horizontalRemainder += hscale;
1472                                 pixCount++;
1473                                 pixels <<= 16;
1474                         }
1475
1476                         if (pixCount > 3)
1477                         {
1478                                 int phrasesToSkip = pixCount / 4, pixelShift = pixCount % 4;
1479
1480                                 data += (pitch << 3) * phrasesToSkip;
1481                                 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1482                                 pixels <<= 16 * pixelShift;
1483
1484                                 iwidth -= phrasesToSkip;
1485
1486                                 pixCount = pixelShift;
1487                         }
1488                 }
1489         }
1490         else if (depth == 5)                                                    // 24 BPP
1491         {
1492 //I'm not sure that you can scale a 24 BPP bitmap properly--the JTRM seem to indicate as much.
1493 WriteLog("OP: Writing 24 BPP scaled bitmap!\n");
1494 if (firstPix != 0)
1495         WriteLog("OP: Scaled bitmap @ 24 BPP requesting FIRSTPIX!\n");
1496                 // Not sure, but I think RMW only works with 16 BPP and below, and only in CRY mode...
1497                 // The LSB is OPFLAG_REFLECT, so sign extend it and or 4 into it.
1498                 int32 lbufDelta = ((int8)((flags << 7) & 0xFF) >> 4) | 0x04;
1499
1500                 while (iwidth--)
1501                 {
1502                         // Fetch phrase...
1503                         uint64 pixels = ((uint64)JaguarReadLong(data, OP) << 32) | JaguarReadLong(data + 4, OP);
1504                         data += pitch << 3;                                             // Multiply pitch * 8 (optimize: precompute this value)
1505
1506                         for(int i=0; i<2; i++)
1507                         {
1508                                 uint8 bits3 = pixels >> 56, bits2 = pixels >> 48,
1509                                         bits1 = pixels >> 40, bits0 = pixels >> 32;
1510 // Seems to me that both of these are in the same endian, so we could cast it as
1511 // uint16 * and do straight across copies (what about 24 bpp? Treat it differently...)
1512 // This only works for the palettized modes (1 - 8 BPP), since we actually have to
1513 // copy data from memory in 16 BPP mode (or does it? Isn't this the same as the CLUT case?)
1514 // No, it isn't because we read the memory in an endian safe way--it *won't* work...
1515                                 if (flagTRANS && (bits3 | bits2 | bits1 | bits0) == 0)
1516                                         ;       // Do nothing...
1517                                 else
1518                                         *currentLineBuffer = bits3,
1519                                         *(currentLineBuffer + 1) = bits2,
1520                                         *(currentLineBuffer + 2) = bits1,
1521                                         *(currentLineBuffer + 3) = bits0;
1522
1523                                 currentLineBuffer += lbufDelta;
1524                                 pixels <<= 32;
1525                         }
1526                 }
1527         }
1528 /*if (depth == 3 && startPos == 13)
1529 {
1530 if (op_start_log)
1531 WriteLog("OP: Writing in the margins...\n");
1532         for(int i=0; i<100*2; i+=2)
1533 //      for(int i=0; i<14*2; i+=2)
1534                 tom_ram_8[0x1800 + i] = 0xFF,
1535                 tom_ram_8[0x1800 + i + 1] = 0xFF;
1536 }*/
1537 //      uint32 lbufAddress = 0x1800 + (!in24BPPMode ? startPos * 2 : startPos * 4);
1538 //      uint8 * currentLineBuffer = &tom_ram_8[lbufAddress];
1539 }