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