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