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