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