]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/gpu.cpp
Fixed IMASK access in DSP & GPU, more CPU browser refinements.
[virtualjaguar] / src / gpu.cpp
index f7951ccc7d932332510ca3d4c868de253669f4f5..c57b948d5088751dd169e4d20f0d5e89a7a11a3c 100644 (file)
@@ -1,9 +1,21 @@
+#if 1
+
 //
 // GPU Core
 //
-// by cal2
+// Originally by David Raingeard (Cal2)
 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
-// Cleanups, endian wrongness, and bad ASM amelioration by James L. Hammons
+// Cleanups, endian wrongness, and bad ASM amelioration by James Hammons
+// (C) 2010 Underground Software
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// Who  When        What
+// ---  ----------  -------------------------------------------------------------
+// JLH  01/16/2010  Created this log ;-)
+// JLH  11/26/2011  Added fixes for LOAD/STORE alignment issues
+
+//
 // Note: Endian wrongness probably stems from the MAME origins of this emu and
 //       the braindead way in which MAME handles memory. :-)
 //
 
 #include "gpu.h"
 
+#include <stdlib.h>
+#include <string.h>                                                            // For memset
+#include "dsp.h"
+#include "jagdasm.h"
+#include "jaguar.h"
+#include "log.h"
+#include "m68000/m68kinterface.h"
+//#include "memory.h"
+#include "tom.h"
+
+
+// Seems alignment in loads & stores was off...
+#define GPU_CORRECT_ALIGNMENT
 //#define GPU_DEBUG
 
 // For GPU dissasembly...
-/*
+
+#if 0
 #define GPU_DIS_ABS
 #define GPU_DIS_ADD
 #define GPU_DIS_ADDC
 #define GPU_DIS_SUBQT
 #define GPU_DIS_XOR
 
-bool doGPUDis = false;
-//bool doGPUDis = true;
-//*/
+//bool doGPUDis = false;
+bool doGPUDis = true;
+#endif
+
 /*
 GPU opcodes use (BIOS flying ATARI logo):
 +                    add 357416
@@ -145,19 +172,18 @@ GPU opcodes use (BIOS flying ATARI logo):
 #define REGPAGE                        0x4000
 #define DMAEN                  0x8000
 
+// External global variables
+
+extern int start_logging;
+extern int gpu_start_log;
+
 // Private function prototypes
 
 void GPUUpdateRegisterBanks(void);
-
 void GPUDumpDisassembly(void);
 void GPUDumpRegisters(void);
 void GPUDumpMemory(void);
 
-// External global variables
-
-extern int start_logging;
-extern int gpu_start_log;
-
 static void gpu_opcode_add(void);
 static void gpu_opcode_addc(void);
 static void gpu_opcode_addq(void);
@@ -223,7 +249,8 @@ static void gpu_opcode_store_r15_ri(void);
 static void gpu_opcode_sat24(void);
 static void gpu_opcode_pack(void);
 
-uint8 gpu_opcode_cycles[64] = 
+// This is wrong, since it doesn't take pipeline effects into account. !!! FIX !!!
+/*uint8 gpu_opcode_cycles[64] =
 {
        3,  3,  3,  3,  3,  3,  3,  3,
        3,  3,  3,  3,  3,  3,  3,  3,
@@ -233,10 +260,36 @@ uint8 gpu_opcode_cycles[64] =
        5,  4,  5,  6,  6,  1,  1,  1,
        1,  2,  2,  2,  1,  1,  9,  3,
        3,  1,  6,  6,  2,  2,  3,  3
-};
-
-void (*gpu_opcode[64])()= 
-{      
+};//*/
+//Here's a QnD kludge...
+//This is wrong, wrong, WRONG, but it seems to work for the time being...
+//(That is, it fixes Flip Out which relies on GPU timing rather than semaphores. Bad developers! Bad!)
+//What's needed here is a way to take pipeline effects into account (including pipeline stalls!)...
+/*uint8 gpu_opcode_cycles[64] =
+{
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  9,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  2,
+       2,  2,  2,  3,  3,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  4,  1,
+       1,  1,  3,  3,  1,  1,  1,  1
+};//*/
+uint8 gpu_opcode_cycles[64] =
+{
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1,
+       1,  1,  1,  1,  1,  1,  1,  1
+};//*/
+
+void (*gpu_opcode[64])()=
+{
        gpu_opcode_add,                                 gpu_opcode_addc,                                gpu_opcode_addq,                                gpu_opcode_addqt,
        gpu_opcode_sub,                                 gpu_opcode_subc,                                gpu_opcode_subq,                                gpu_opcode_subqt,
        gpu_opcode_neg,                                 gpu_opcode_and,                                 gpu_opcode_or,                                  gpu_opcode_xor,
@@ -255,7 +308,7 @@ void (*gpu_opcode[64])()=
        gpu_opcode_store_r14_ri,                gpu_opcode_store_r15_ri,                gpu_opcode_sat24,                               gpu_opcode_pack,
 };
 
-static uint8 * gpu_ram_8;
+static uint8 gpu_ram_8[0x1000];
 uint32 gpu_pc;
 static uint32 gpu_acc;
 static uint32 gpu_remain;
@@ -270,8 +323,8 @@ static uint32 gpu_div_control;
 // a bit before writing a result. I.e., if the result of an operation leaves a zero in
 // the carry flag, you don't have to zero gpu_flag_c before you can write that zero!
 static uint8 gpu_flag_z, gpu_flag_n, gpu_flag_c;
-static uint32 * gpu_reg_bank_0;
-static uint32 * gpu_reg_bank_1;
+static uint32 gpu_reg_bank_0[32];
+static uint32 gpu_reg_bank_1[32];
 static uint32 * gpu_reg;
 static uint32 * gpu_alternate_reg;
 
@@ -289,19 +342,19 @@ static uint32 gpu_opcode_second_parameter;
 #define IMM_2                  gpu_opcode_second_parameter
 
 #define SET_FLAG_Z(r)  (gpu_flag_z = ((r) == 0));
-#define SET_FLAG_N(r)  (gpu_flag_n = (((UINT32)(r) >> 31) & 0x01));
+#define SET_FLAG_N(r)  (gpu_flag_n = (((uint32)(r) >> 31) & 0x01));
 
 #define RESET_FLAG_Z() gpu_flag_z = 0;
 #define RESET_FLAG_N() gpu_flag_n = 0;
-#define RESET_FLAG_C() gpu_flag_c = 0;    
+#define RESET_FLAG_C() gpu_flag_c = 0;
 
 #define CLR_Z                          (gpu_flag_z = 0)
 #define CLR_ZN                         (gpu_flag_z = gpu_flag_n = 0)
 #define CLR_ZNC                                (gpu_flag_z = gpu_flag_n = gpu_flag_c = 0)
 #define SET_Z(r)                       (gpu_flag_z = ((r) == 0))
-#define SET_N(r)                       (gpu_flag_n = (((UINT32)(r) >> 31) & 0x01))
-#define SET_C_ADD(a,b)         (gpu_flag_c = ((UINT32)(b) > (UINT32)(~(a))))
-#define SET_C_SUB(a,b)         (gpu_flag_c = ((UINT32)(b) > (UINT32)(a)))
+#define SET_N(r)                       (gpu_flag_n = (((uint32)(r) >> 31) & 0x01))
+#define SET_C_ADD(a,b)         (gpu_flag_c = ((uint32)(b) > (uint32)(~(a))))
+#define SET_C_SUB(a,b)         (gpu_flag_c = ((uint32)(b) > (uint32)(a)))
 #define SET_ZN(r)                      SET_N(r); SET_Z(r)
 #define SET_ZNC_ADD(a,b,r)     SET_N(r); SET_Z(r); SET_C_ADD(a,b)
 #define SET_ZNC_SUB(a,b,r)     SET_N(r); SET_Z(r); SET_C_SUB(a,b)
@@ -314,8 +367,8 @@ uint8 * branch_condition_table = 0;
 
 uint32 gpu_opcode_use[64];
 
-char * gpu_opcode_str[64]= 
-{      
+const char * gpu_opcode_str[64]=
+{
        "add",                          "addc",                         "addq",                         "addqt",
        "sub",                          "subc",                         "subq",                         "subqt",
        "neg",                          "and",                          "or",                           "xor",
@@ -337,12 +390,12 @@ char * gpu_opcode_str[64]=
 static uint32 gpu_in_exec = 0;
 static uint32 gpu_releaseTimeSlice_flag = 0;
 
-void gpu_releaseTimeslice(void)
+void GPUReleaseTimeslice(void)
 {
        gpu_releaseTimeSlice_flag = 1;
 }
 
-uint32 gpu_get_pc(void)
+uint32 GPUGetPC(void)
 {
        return gpu_pc;
 }
@@ -384,6 +437,9 @@ void build_branch_condition_table(void)
 //
 uint8 GPUReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: ReadByte--Attempt to read from GPU register file by %s!\n", whoName[who]);
+
        if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
                return gpu_ram_8[offset & 0xFFF];
        else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
@@ -408,6 +464,9 @@ uint8 GPUReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
 //
 uint16 GPUReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: ReadWord--Attempt to read from GPU register file by %s!\n", whoName[who]);
+
        if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE+0x1000))
        {
                offset &= 0xFFF;
@@ -429,9 +488,9 @@ uint16 GPUReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
                        return data >> 16;
        }
 
-//TEMP--Mirror of F03000?
-if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
-WriteLog("[GPUR16] --> Possible GPU RAM mirror access by %s!", whoName[who]);
+//TEMP--Mirror of F03000? No. Writes only...
+//if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
+//WriteLog("[GPUR16] --> Possible GPU RAM mirror access by %s!", whoName[who]);
 
        return JaguarReadWord(offset, who);
 }
@@ -441,6 +500,9 @@ WriteLog("[GPUR16] --> Possible GPU RAM mirror access by %s!", whoName[who]);
 //
 uint32 GPUReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: ReadLong--Attempt to read from GPU register file by %s!\n", whoName[who]);
+
 //     if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE + 0x1000))
        if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFC))
        {
@@ -461,7 +523,7 @@ uint32 GPUReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
                        gpu_flag_n = (gpu_flag_n ? 1 : 0);
 
                        gpu_flags = (gpu_flags & 0xFFFFFFF8) | (gpu_flag_n << 2) | (gpu_flag_c << 1) | gpu_flag_z;
-                                       
+
                        return gpu_flags & 0xFFFFC1FF;
                case 0x04:
                        return gpu_matrix_control;
@@ -484,9 +546,9 @@ uint32 GPUReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
                        return 0;
                }
        }
-//TEMP--Mirror of F03000?
-if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
-       WriteLog("[GPUR32] --> Possible GPU RAM mirror access by %s!\n", whoName[who]);
+//TEMP--Mirror of F03000? No. Writes only...
+//if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
+//     WriteLog("[GPUR32] --> Possible GPU RAM mirror access by %s!\n", whoName[who]);
 /*if (offset >= 0xF1D000 && offset <= 0xF1DFFF)
        WriteLog("[GPUR32] --> Reading from Wavetable ROM!\n");//*/
 
@@ -498,6 +560,9 @@ if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
 //
 void GPUWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: WriteByte--Attempt to write to GPU register file by %s!\n", whoName[who]);
+
        if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFF))
        {
                gpu_ram_8[offset & 0xFFF] = data;
@@ -536,6 +601,9 @@ void GPUWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
 //
 void GPUWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: WriteWord--Attempt to write to GPU register file by %s!\n", whoName[who]);
+
        if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFE))
        {
                gpu_ram_8[offset & 0xFFF] = (data>>8) & 0xFF;
@@ -543,6 +611,10 @@ void GPUWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
 /*             offset &= 0xFFF;
                SET16(gpu_ram_8, offset, data);//*/
 
+/*if (offset >= 0xF03214 && offset < 0xF0321F)
+       WriteLog("GPU: Writing WORD (%04X) to GPU RAM (%08X)...\n", data, offset);//*/
+
+
 //This is the same stupid worthless code that was in the DSP!!! AARRRGGGGHHHHH!!!!!!
 /*             if (!gpu_in_exec)
                {
@@ -571,16 +643,19 @@ void GPUWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
                        else
                                gpu_div_control = (gpu_div_control & 0x0000FFFF) | ((data & 0xFFFF) << 16);
                }
-               else 
+               else
                {
 //WriteLog("[GPU W16:%08X,%04X]", offset, data);
                        uint32 old_data = GPUReadLong(offset & 0xFFFFFFC, who);
+
                        if (offset & 0x02)
                                old_data = (old_data & 0xFFFF0000) | (data & 0xFFFF);
                        else
                                old_data = (old_data & 0x0000FFFF) | ((data & 0xFFFF) << 16);
+
                        GPUWriteLong(offset & 0xFFFFFFC, old_data, who);
                }
+
                return;
        }
        else if ((offset == GPU_WORK_RAM_BASE + 0x0FFF) || (GPU_CONTROL_RAM_BASE + 0x1F))
@@ -601,23 +676,22 @@ void GPUWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
 //
 void GPUWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
 {
+       if (offset >= 0xF02000 && offset <= 0xF020FF)
+               WriteLog("GPU: WriteLong--Attempt to write to GPU register file by %s!\n", whoName[who]);
+
 //     if ((offset >= GPU_WORK_RAM_BASE) && (offset < GPU_WORK_RAM_BASE + 0x1000))
        if ((offset >= GPU_WORK_RAM_BASE) && (offset <= GPU_WORK_RAM_BASE + 0x0FFC))
        {
 #ifdef GPU_DEBUG
                if (offset & 0x03)
                {
-                       WriteLog("GPU: Write32--unaligned write @ %08X [%08X]\n", offset, data);
+                       WriteLog("GPU: Write32--unaligned write @ %08X [%08X] by %s\n", offset, data, whoName[who]);
                        GPUDumpRegisters();
                }
 #endif // GPU_DEBUG
 
-/*             gpu_ram_8[offset & 0xFFF] = (data >> 24) & 0xFF,
-               gpu_ram_8[(offset+1) & 0xFFF] = (data >> 16) & 0xFF,
-               gpu_ram_8[(offset+2) & 0xFFF] = (data >> 8) & 0xFF,
-               gpu_ram_8[(offset+3) & 0xFFF] = data & 0xFF;//*/
                offset &= 0xFFF;
-               SET32(gpu_ram_8, offset, data);//*/
+               SET32(gpu_ram_8, offset, data);
                return;
        }
 //     else if ((offset >= GPU_CONTROL_RAM_BASE) && (offset < GPU_CONTROL_RAM_BASE+0x20))
@@ -629,7 +703,9 @@ void GPUWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
                case 0x00:
                {
                        bool IMASKCleared = (gpu_flags & IMASK) && !(data & IMASK);
-                       gpu_flags = data;
+                       // NOTE: According to the JTRM, writing a 1 to IMASK has no effect; only the
+                       //       IRQ logic can set it. So we mask it out here to prevent problems...
+                       gpu_flags = data & (~IMASK);
                        gpu_flag_z = gpu_flags & ZERO_FLAG;
                        gpu_flag_c = (gpu_flags & CARRY_FLAG) >> 1;
                        gpu_flag_n = (gpu_flags & NEGA_FLAG) >> 2;
@@ -651,7 +727,7 @@ void GPUWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
                        gpu_matrix_control = data;
                        break;
                case 0x08:
-                       // Can only point to long aligned addresses
+                       // This can only point to long aligned addresses
                        gpu_pointer_to_matrix = data & 0xFFFFFFFC;
                        break;
                case 0x0C:
@@ -664,7 +740,7 @@ WriteLog("GPU: %s setting GPU PC to %08X %s\n", whoName[who], gpu_pc, (GPU_RUNNI
 #endif // GPU_DEBUG
                        break;
                case 0x14:
-               {       
+               {
 //                     uint32 gpu_was_running = GPU_RUNNING;
                        data &= ~0xF7C0;                // Disable writes to INT_LAT0-4 & TOM version number
 
@@ -672,13 +748,14 @@ WriteLog("GPU: %s setting GPU PC to %08X %s\n", whoName[who], gpu_pc, (GPU_RUNNI
                        if (data & 0x02)
                        {
 //WriteLog("GPU->CPU interrupt\n");
-                               if (tom_irq_enabled(IRQ_GPU))
+                               if (TOMIRQEnabled(IRQ_GPU))
                                {
-                                       if ((tom_irq_enabled(IRQ_GPU)) && (jaguar_interrupt_handler_is_valid(64)))
+//This is the programmer's responsibility, to make sure the handler is valid, not ours!
+//                                     if ((TOMIRQEnabled(IRQ_GPU))// && (JaguarInterruptHandlerIsValid(64)))
                                        {
-                                               tom_set_pending_gpu_int();
-                                               m68k_set_irq(7);                        // Set 68000 NMI
-                                               gpu_releaseTimeslice();
+                                               TOMSetPendingGPUInt();
+                                               m68k_set_irq(2);                        // Set 68000 IPL 2
+                                               GPUReleaseTimeslice();
                                        }
                                }
                                data &= ~0x02;
@@ -690,7 +767,7 @@ WriteLog("GPU: %s setting GPU PC to %08X %s\n", whoName[who], gpu_pc, (GPU_RUNNI
 //WriteLog("CPU->GPU interrupt\n");
                                GPUSetIRQLine(0, ASSERT_LINE);
                                m68k_end_timeslice();
-                               dsp_releaseTimeslice();
+                               DSPReleaseTimeslice();
                                data &= ~0x04;
                        }
 
@@ -699,7 +776,6 @@ WriteLog("GPU: %s setting GPU PC to %08X %s\n", whoName[who], gpu_pc, (GPU_RUNNI
                        {
                                //WriteLog("asked to perform a single step (single step is %senabled)\n",(data&0x8)?"":"not ");
                        }
-//                     gpu_control = (gpu_control & 0x107C0) | (data & (~0x107C0));
                        gpu_control = (gpu_control & 0xF7C0) | (data & (~0xF7C0));
 
                        // if gpu wasn't running but is now running, execute a few cycles
@@ -709,13 +785,13 @@ WriteLog("GPU: %s setting GPU PC to %08X %s\n", whoName[who], gpu_pc, (GPU_RUNNI
                        {
                                WriteLog("GPU: Write32--About to do stupid braindead GPU execution for 200 cycles.\n");
 #endif // GPU_DEBUG
-                               gpu_exec(200);
+                               GPUExec(200);
 #ifdef GPU_DEBUG
                        }
 #endif // GPU_DEBUG//*/
 #else
                        if (gpu_control & 0x18)
-                               gpu_exec(1);
+                               GPUExec(1);
 #endif // #ifndef GPU_SINGLE_STEPPING
 #ifdef GPU_DEBUG
 WriteLog("Write to GPU CTRL by %s: %08X ", whoName[who], data);
@@ -727,6 +803,93 @@ WriteLog("\n");
 #endif // GPU_DEBUG
 //if (GPU_RUNNING)
 //     GPUDumpDisassembly();
+/*if (GPU_RUNNING)
+{
+       if (gpu_pc == 0xF035D8)
+       {
+//             GPUDumpDisassembly();
+//             log_done();
+//             exit(1);
+               gpu_control &= 0xFFFFFFFE;      // Don't run it and let's see what happens!
+//Hmm. Seems to lock up when going into the demo...
+//Try to disable the collision altogether!
+       }
+}//*/
+extern int effect_start5;
+static bool finished = false;
+//if (GPU_RUNNING && effect_start5 && !finished)
+if (GPU_RUNNING && effect_start5 && gpu_pc == 0xF035D8)
+{
+       // Let's do a dump of $6528!
+/*     uint32 numItems = JaguarReadWord(0x6BD6);
+       WriteLog("\nDump of $6528: %u items.\n\n", numItems);
+       for(int i=0; i<numItems*3*4; i+=3*4)
+       {
+               WriteLog("\t%04X: %08X %08X %08X -> ", 0x6528+i, JaguarReadLong(0x6528+i),
+                       JaguarReadLong(0x6528+i+4), JaguarReadLong(0x6528+i+8));
+               uint16 link = JaguarReadWord(0x6528+i+8+2);
+               for(int j=0; j<40; j+=4)
+                       WriteLog("%08X ", JaguarReadLong(link + j));
+               WriteLog("\n");
+       }
+       WriteLog("\n");//*/
+       // Let's try a manual blit here...
+//This isn't working the way it should! !!! FIX !!!
+//Err, actually, it is.
+// NOW, it works right! Problem solved!!! It's a blitter bug!
+/*     uint32 src = 0x4D54, dst = 0xF03000, width = 10 * 4;
+       for(int y=0; y<127; y++)
+       {
+               for(int x=0; x<2; x++)
+               {
+                       JaguarWriteLong(dst, JaguarReadLong(src));
+
+                       src += 4;
+                       dst += 4;
+               }
+               src += width - (2 * 4);
+       }//*/
+/*     finished = true;
+       doGPUDis = true;
+       WriteLog("\nGPU: About to execute collision detection code.\n\n");//*/
+
+/*     WriteLog("\nGPU: About to execute collision detection code. Data @ 4D54:\n\n");
+       int count = 0;
+       for(int i=0x004D54; i<0x004D54+2048; i++)
+       {
+               WriteLog("%02X ", JaguarReadByte(i));
+               count++;
+               if (count == 32)
+               {
+                       count = 0;
+                       WriteLog("\n");
+               }
+       }
+       WriteLog("\n\nData @ F03000:\n\n");
+       count = 0;
+       for(int i=0xF03000; i<0xF03200; i++)
+       {
+               WriteLog("%02X ", JaguarReadByte(i));
+               count++;
+               if (count == 32)
+               {
+                       count = 0;
+                       WriteLog("\n");
+               }
+       }
+       WriteLog("\n\n");
+       log_done();
+       exit(0);//*/
+}
+//if (!GPU_RUNNING)
+//     doGPUDis = false;
+/*if (!GPU_RUNNING && finished)
+{
+       WriteLog("\nGPU: Finished collision detection code. Exiting!\n\n");
+       GPUDumpRegisters();
+       log_done();
+       exit(0);
+}//*/
                        // (?) If we're set running by the M68K (or DSP?) then end its timeslice to
                        // allow the GPU a chance to run...
                        // Yes! This partially fixed Trevor McFur...
@@ -749,6 +912,7 @@ WriteLog("\n");
 
 //     JaguarWriteWord(offset, (data >> 16) & 0xFFFF, who);
 //     JaguarWriteWord(offset+2, data & 0xFFFF, who);
+// We're a 32-bit processor, we can do a long write...!
        JaguarWriteLong(offset, data, who);
 }
 
@@ -776,12 +940,12 @@ void GPUHandleIRQs(void)
 
        // Get the interrupt latch & enable bits
        uint32 bits = (gpu_control >> 6) & 0x1F, mask = (gpu_flags >> 4) & 0x1F;
-       
+
        // Bail out if latched interrupts aren't enabled
        bits &= mask;
        if (!bits)
                return;
-       
+
        // Determine which interrupt to service
        uint32 which = 0; //Isn't there a #pragma to disable this warning???
        if (bits & 0x01)
@@ -798,18 +962,18 @@ void GPUHandleIRQs(void)
        if (start_logging)
                WriteLog("GPU: Generating IRQ #%i\n", which);
 
-       // set the interrupt flag 
+       // set the interrupt flag
        gpu_flags |= IMASK;
        GPUUpdateRegisterBanks();
 
-       // subqt  #4,r31                ; pre-decrement stack pointer 
-       // move  pc,r30                 ; address of interrupted code 
+       // subqt  #4,r31                ; pre-decrement stack pointer
+       // move  pc,r30                 ; address of interrupted code
        // store  r30,(r31)     ; store return address
        gpu_reg[31] -= 4;
        GPUWriteLong(gpu_reg[31], gpu_pc - 2, GPU);
-       
-       // movei  #service_address,r30  ; pointer to ISR entry 
-       // jump  (r30)                                  ; jump to ISR 
+
+       // movei  #service_address,r30  ; pointer to ISR entry
+       // jump  (r30)                                  ; jump to ISR
        // nop
        gpu_pc = gpu_reg[30] = GPU_WORK_RAM_BASE + (which * 0x10);
 }
@@ -833,24 +997,22 @@ void GPUSetIRQLine(int irqline, int state)
 //#include "gpu2.h"
 //#include "gpu3.h"
 
-void gpu_init(void)
+void GPUInit(void)
 {
-       memory_malloc_secure((void **)&gpu_ram_8, 0x1000, "GPU work RAM");
-//     memory_malloc_secure((void **)&gpu_reg, 32*sizeof(int32), "GPU bank 0 regs");
-//     memory_malloc_secure((void **)&gpu_alternate_reg, 32*sizeof(int32), "GPU bank 1 regs");
-       memory_malloc_secure((void **)&gpu_reg_bank_0, 32 * sizeof(int32), "GPU bank 0 regs");
-       memory_malloc_secure((void **)&gpu_reg_bank_1, 32 * sizeof(int32), "GPU bank 1 regs");
+//     memory_malloc_secure((void **)&gpu_ram_8, 0x1000, "GPU work RAM");
+//     memory_malloc_secure((void **)&gpu_reg_bank_0, 32 * sizeof(int32), "GPU bank 0 regs");
+//     memory_malloc_secure((void **)&gpu_reg_bank_1, 32 * sizeof(int32), "GPU bank 1 regs");
 
        build_branch_condition_table();
 
-       gpu_reset();
+       GPUReset();
 
 //TEMPORARY: Testing only!
 //     gpu2_init();
 //     gpu3_init();
 }
 
-void gpu_reset(void)
+void GPUReset(void)
 {
        // GPU registers (directly visible)
        gpu_flags                         = 0x00000000;
@@ -876,15 +1038,15 @@ void gpu_reset(void)
        memset(gpu_ram_8, 0xFF, 0x1000);
        gpu_in_exec = 0;
 //not needed   GPUInterruptPending = false;
-       gpu_reset_stats();
+       GPUResetStats();
 }
 
-uint32 gpu_read_pc(void)
+uint32 GPUReadPC(void)
 {
        return gpu_pc;
 }
 
-void gpu_reset_stats(void)
+void GPUResetStats(void)
 {
        for(uint32 i=0; i<64; i++)
                gpu_opcode_use[i] = 0;
@@ -936,26 +1098,17 @@ void GPUDumpMemory(void)
                        gpu_ram_8[i+1], gpu_ram_8[i+2], gpu_ram_8[i+3]);
 }
 
-void gpu_done(void)
-{ 
-       WriteLog("GPU: stopped at PC=%08X (GPU %s running)\n", (unsigned int)gpu_pc, GPU_RUNNING ? "was" : "wasn't");
+void GPUDone(void)
+{
+       WriteLog("GPU: Stopped at PC=%08X (GPU %s running)\n", (unsigned int)gpu_pc, GPU_RUNNING ? "was" : "wasn't");
 
-       // Get the interrupt latch & enable bits 
+       // Get the interrupt latch & enable bits
        uint8 bits = (gpu_control >> 6) & 0x1F, mask = (gpu_flags >> 4) & 0x1F;
        WriteLog("GPU: Latch bits = %02X, enable bits = %02X\n", bits, mask);
 
        GPUDumpRegisters();
        GPUDumpDisassembly();
 
-/*     WriteLog("---[GPU code at %08X]---------------------------\n", gpu_pc);
-       j = gpu_pc - 64;
-       for(int i=0; i<4096; i++)
-       {
-               uint32 oldj = j;
-               j += dasmjag(JAGUAR_GPU, buffer, j);
-               WriteLog("\t%08X: %s\n", oldj, buffer);
-       }*/
-
        WriteLog("\nGPU opcodes use:\n");
        for(int i=0; i<64; i++)
        {
@@ -964,7 +1117,9 @@ void gpu_done(void)
        }
        WriteLog("\n");
 
-       memory_free(gpu_ram_8);
+//     memory_free(gpu_ram_8);
+//     memory_free(gpu_reg_bank_0);
+//     memory_free(gpu_reg_bank_1);
 }
 
 //
@@ -973,7 +1128,7 @@ void gpu_done(void)
 static int testCount = 1;
 static int len = 0;
 static bool tripwire = false;
-void gpu_exec(int32 cycles)
+void GPUExec(int32 cycles)
 {
        if (!GPU_RUNNING)
                return;
@@ -991,6 +1146,35 @@ void gpu_exec(int32 cycles)
 
        while (cycles > 0 && GPU_RUNNING)
        {
+if (gpu_ram_8[0x054] == 0x98 && gpu_ram_8[0x055] == 0x0A && gpu_ram_8[0x056] == 0x03
+       && gpu_ram_8[0x057] == 0x00 && gpu_ram_8[0x058] == 0x00 && gpu_ram_8[0x059] == 0x00)
+{
+       if (gpu_pc == 0xF03000)
+       {
+               extern uint32 starCount;
+               starCount = 0;
+/*             WriteLog("GPU: Starting starfield generator... Dump of [R03=%08X]:\n", gpu_reg_bank_0[03]);
+               uint32 base = gpu_reg_bank_0[3];
+               for(uint32 i=0; i<0x100; i+=16)
+               {
+                       WriteLog("%02X: ", i);
+                       for(uint32 j=0; j<16; j++)
+                       {
+                               WriteLog("%02X ", JaguarReadByte(base + i + j));
+                       }
+                       WriteLog("\n");
+               }*/
+       }
+//     if (gpu_pc == 0xF03)
+       {
+       }
+}//*/
+/*if (gpu_pc == 0xF03B9E && gpu_reg_bank_0[01] == 0)
+{
+       GPUDumpRegisters();
+       WriteLog("GPU: Starting disassembly log...\n");
+       doGPUDis = true;
+}//*/
 /*if (gpu_pc == 0xF0359A)
 {
        doGPUDis = true;
@@ -999,7 +1183,7 @@ void gpu_exec(int32 cycles)
 /*             gpu_flag_c = (gpu_flag_c ? 1 : 0);
                gpu_flag_z = (gpu_flag_z ? 1 : 0);
                gpu_flag_n = (gpu_flag_n ? 1 : 0);*/
-       
+
                uint16 opcode = GPUReadWord(gpu_pc, GPU);
                uint32 index = opcode >> 10;
                gpu_instruction = opcode;                               // Added for GPU #3...
@@ -1105,7 +1289,7 @@ WriteLog("GPU: [%08X] %s (RM=%08X, RN=%08X) -> ", gpu_pc, buffer, RM, RN);
 //             gpu3_opcode[index]();
 
 // BIOS hacking
-//GPU: [00F03548] jr      nz,00F03560 (0xd561) (RM=00F03114, RN=00000004) ->     --> JR: Branch taken. 
+//GPU: [00F03548] jr      nz,00F03560 (0xd561) (RM=00F03114, RN=00000004) ->     --> JR: Branch taken.
 /*static bool firstTime = true;
 if (gpu_pc == 0xF03548 && firstTime)
 {
@@ -1132,7 +1316,7 @@ if (gpu_start_log)
        WriteLog("(RM=%08X, RN=%08X)\n", RM, RN);//*/
 if ((gpu_pc < 0xF03000 || gpu_pc > 0xF03FFF) && !tripwire)
 {
-       WriteLog("GPU: Executing outside local RAM!\n");
+       WriteLog("GPU: Executing outside local RAM! GPU_PC: %08X\n", gpu_pc);
        tripwire = true;
 }
        }
@@ -1182,7 +1366,7 @@ GPU opcodes use (offset punch--vertically below bad guy):
 static void gpu_opcode_jump(void)
 {
 #ifdef GPU_DIS_JUMP
-char * condition[32] =
+const char * condition[32] =
 {      "T", "nz", "z", "???", "nc", "nc nz", "nc z", "???", "c", "c nz",
        "c z", "???", "???", "???", "???", "???", "???", "???", "???",
        "???", "nn", "nn nz", "nn z", "???", "n", "n nz", "n z", "???",
@@ -1206,7 +1390,7 @@ char * condition[32] =
 if (gpu_start_log)
        WriteLog("    --> JUMP: Branch taken.\n");
                uint32 delayed_pc = RM;
-               gpu_exec(1);
+               GPUExec(1);
                gpu_pc = delayed_pc;
 /*             uint16 opcode = GPUReadWord(gpu_pc, GPU);
                gpu_opcode_first_parameter = (opcode >> 5) & 0x1F;
@@ -1225,7 +1409,7 @@ if (gpu_start_log)
 static void gpu_opcode_jr(void)
 {
 #ifdef GPU_DIS_JR
-char * condition[32] =
+const char * condition[32] =
 {      "T", "nz", "z", "???", "nc", "nc nz", "nc z", "???", "c", "c nz",
        "c z", "???", "???", "???", "???", "???", "???", "???", "???",
        "???", "nn", "nn nz", "nn z", "???", "n", "n nz", "n z", "???",
@@ -1235,8 +1419,8 @@ char * condition[32] =
 #endif
 /*     if (CONDITION(jaguar.op & 31))
        {
-               INT32 r1 = (INT8)((jaguar.op >> 2) & 0xF8) >> 2;
-               UINT32 newpc = jaguar.PC + r1;
+               int32 r1 = (INT8)((jaguar.op >> 2) & 0xF8) >> 2;
+               uint32 newpc = jaguar.PC + r1;
                CALL_MAME_DEBUG;
                jaguar.op = ROPCODE(jaguar.PC);
                jaguar.PC = newpc;
@@ -1261,7 +1445,7 @@ if (gpu_start_log)
        WriteLog("    --> JR: Branch taken.\n");
                int32 offset = (IMM_1 & 0x10 ? 0xFFFFFFF0 | IMM_1 : IMM_1);             // Sign extend IMM_1
                int32 delayed_pc = gpu_pc + (offset * 2);
-               gpu_exec(1);
+               GPUExec(1);
                gpu_pc = delayed_pc;
 /*             uint16 opcode = GPUReadWord(gpu_pc, GPU);
                gpu_opcode_first_parameter = (opcode >> 5) & 0x1F;
@@ -1283,7 +1467,7 @@ static void gpu_opcode_add(void)
        if (doGPUDis)
                WriteLog("%06X: ADD    R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 res = RN + RM;
+       uint32 res = RN + RM;
        CLR_ZNC; SET_ZNC_ADD(RN, RM, res);
        RN = res;
 #ifdef GPU_DIS_ADD
@@ -1299,14 +1483,14 @@ static void gpu_opcode_addc(void)
                WriteLog("%06X: ADDC   R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
 /*     int dreg = jaguar.op & 31;
-       UINT32 r1 = jaguar.r[(jaguar.op >> 5) & 31];
-       UINT32 r2 = jaguar.r[dreg];
-       UINT32 res = r2 + r1 + ((jaguar.FLAGS >> 1) & 1);
+       uint32 r1 = jaguar.r[(jaguar.op >> 5) & 31];
+       uint32 r2 = jaguar.r[dreg];
+       uint32 res = r2 + r1 + ((jaguar.FLAGS >> 1) & 1);
        jaguar.r[dreg] = res;
        CLR_ZNC; SET_ZNC_ADD(r2,r1,res);*/
 
-       UINT32 res = RN + RM + gpu_flag_c;
-       UINT32 carry = gpu_flag_c;
+       uint32 res = RN + RM + gpu_flag_c;
+       uint32 carry = gpu_flag_c;
 //     SET_ZNC_ADD(RN, RM, res); //???BUG??? Yes!
        SET_ZNC_ADD(RN + carry, RM, res);
 //     SET_ZNC_ADD(RN, RM + carry, res);
@@ -1323,8 +1507,8 @@ static void gpu_opcode_addq(void)
        if (doGPUDis)
                WriteLog("%06X: ADDQ   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 r1 = gpu_convert_zero[IMM_1];
-       UINT32 res = RN + r1;
+       uint32 r1 = gpu_convert_zero[IMM_1];
+       uint32 res = RN + r1;
        CLR_ZNC; SET_ZNC_ADD(RN, r1, res);
        RN = res;
 #ifdef GPU_DIS_ADDQ
@@ -1352,7 +1536,7 @@ static void gpu_opcode_sub(void)
        if (doGPUDis)
                WriteLog("%06X: SUB    R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 res = RN - RM;
+       uint32 res = RN - RM;
        SET_ZNC_SUB(RN, RM, res);
        RN = res;
 #ifdef GPU_DIS_SUB
@@ -1367,25 +1551,36 @@ static void gpu_opcode_subc(void)
        if (doGPUDis)
                WriteLog("%06X: SUBC   R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 res = RN - RM - gpu_flag_c;
-       UINT32 borrow = gpu_flag_c;
+       uint32 res = RN - RM - gpu_flag_c;
+       uint32 borrow = gpu_flag_c;
 //     SET_ZNC_SUB(RN, RM, res); //???BUG??? YES!!!
-       SET_ZNC_SUB(RN - borrow, RM, res);
+//No matter how you do it, there is a problem. With below, it's 0-0 with carry,
+//and the one below it it's FFFFFFFF - FFFFFFFF with carry... !!! FIX !!!
+//     SET_ZNC_SUB(RN - borrow, RM, res);
+       SET_ZNC_SUB(RN, RM + borrow, res);
        RN = res;
 #ifdef GPU_DIS_SUBC
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
 }
+/*
+N = 5, M = 3, 3 - 5 = -2, C = 1... Or, in our case:
+N = 0, M = 1, 0 - 1 = -1, C = 0!
 
+#define SET_C_SUB(a,b)         (gpu_flag_c = ((uint32)(b) > (uint32)(a)))
+#define SET_ZN(r)                      SET_N(r); SET_Z(r)
+#define SET_ZNC_ADD(a,b,r)     SET_N(r); SET_Z(r); SET_C_ADD(a,b)
+#define SET_ZNC_SUB(a,b,r)     SET_N(r); SET_Z(r); SET_C_SUB(a,b)
+*/
 static void gpu_opcode_subq(void)
 {
 #ifdef GPU_DIS_SUBQ
        if (doGPUDis)
                WriteLog("%06X: SUBQ   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 r1 = gpu_convert_zero[IMM_1];
-       UINT32 res = RN - r1;
+       uint32 r1 = gpu_convert_zero[IMM_1];
+       uint32 res = RN - r1;
        SET_ZNC_SUB(RN, r1, res);
        RN = res;
 #ifdef GPU_DIS_SUBQ
@@ -1413,7 +1608,7 @@ static void gpu_opcode_cmp(void)
        if (doGPUDis)
                WriteLog("%06X: CMP    R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 res = RN - RM;
+       uint32 res = RN - RM;
        SET_ZNC_SUB(RN, RM, res);
 #ifdef GPU_DIS_CMP
        if (doGPUDis)
@@ -1429,8 +1624,8 @@ static void gpu_opcode_cmpq(void)
        if (doGPUDis)
                WriteLog("%06X: CMPQ   #%d, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, sqtable[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 r1 = sqtable[IMM_1 & 0x1F]; // I like this better -> (INT8)(jaguar.op >> 2) >> 3;
-       UINT32 res = RN - r1;
+       uint32 r1 = sqtable[IMM_1 & 0x1F]; // I like this better -> (INT8)(jaguar.op >> 2) >> 3;
+       uint32 res = RN - r1;
        SET_ZNC_SUB(RN, r1, res);
 #ifdef GPU_DIS_CMPQ
        if (doGPUDis)
@@ -1498,7 +1693,7 @@ static void gpu_opcode_move_pc(void)
 {
 #ifdef GPU_DIS_MOVEPC
        if (doGPUDis)
-               WriteLog("%06X: MOVE   PC, R%02u [NCZ:%u%u%u, PC=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, gpu_pc-2, IMM_2, RN);
+               WriteLog("%06X: MOVE   PC, R%02u [NCZ:%u%u%u, PC=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, gpu_pc-2, IMM_2, RN);
 #endif
        // Should be previous PC--this might not always be previous instruction!
        // Then again, this will point right at the *current* instruction, i.e., MOVE PC,R!
@@ -1541,7 +1736,16 @@ static void gpu_opcode_store_r14_indexed(void)
        if (doGPUDis)
                WriteLog("%06X: STORE  R%02u, (R14+$%02X) [NCZ:%u%u%u, R%02u=%08X, R14+$%02X=%08X]\n", gpu_pc-2, IMM_2, gpu_convert_zero[IMM_1] << 2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, gpu_convert_zero[IMM_1] << 2, gpu_reg[14]+(gpu_convert_zero[IMM_1] << 2));
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[14] + (gpu_convert_zero[IMM_1] << 2);
+       
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               GPUWriteLong(address & 0xFFFFFFFC, RN, GPU);
+       else
+               GPUWriteLong(address, RN, GPU);
+#else
        GPUWriteLong(gpu_reg[14] + (gpu_convert_zero[IMM_1] << 2), RN, GPU);
+#endif
 }
 
 static void gpu_opcode_store_r15_indexed(void)
@@ -1550,7 +1754,16 @@ static void gpu_opcode_store_r15_indexed(void)
        if (doGPUDis)
                WriteLog("%06X: STORE  R%02u, (R15+$%02X) [NCZ:%u%u%u, R%02u=%08X, R15+$%02X=%08X]\n", gpu_pc-2, IMM_2, gpu_convert_zero[IMM_1] << 2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, gpu_convert_zero[IMM_1] << 2, gpu_reg[15]+(gpu_convert_zero[IMM_1] << 2));
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[15] + (gpu_convert_zero[IMM_1] << 2);
+
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               GPUWriteLong(address & 0xFFFFFFFC, RN, GPU);
+       else
+               GPUWriteLong(address, RN, GPU);
+#else
        GPUWriteLong(gpu_reg[15] + (gpu_convert_zero[IMM_1] << 2), RN, GPU);
+#endif
 }
 
 static void gpu_opcode_load_r14_ri(void)
@@ -1559,7 +1772,16 @@ static void gpu_opcode_load_r14_ri(void)
        if (doGPUDis)
                WriteLog("%06X: LOAD   (R14+R%02u), R%02u [NCZ:%u%u%u, R14+R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM+gpu_reg[14], IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[14] + RM;
+
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               RN = GPUReadLong(address & 0xFFFFFFFC, GPU);
+       else
+               RN = GPUReadLong(address, GPU);
+#else
        RN = GPUReadLong(gpu_reg[14] + RM, GPU);
+#endif
 #ifdef GPU_DIS_LOAD14R
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
@@ -1572,7 +1794,16 @@ static void gpu_opcode_load_r15_ri(void)
        if (doGPUDis)
                WriteLog("%06X: LOAD   (R15+R%02u), R%02u [NCZ:%u%u%u, R15+R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM+gpu_reg[15], IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[15] + RM;
+
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               RN = GPUReadLong(address & 0xFFFFFFFC, GPU);
+       else
+               RN = GPUReadLong(address, GPU);
+#else
        RN = GPUReadLong(gpu_reg[15] + RM, GPU);
+#endif
 #ifdef GPU_DIS_LOAD15R
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
@@ -1585,7 +1816,16 @@ static void gpu_opcode_store_r14_ri(void)
        if (doGPUDis)
                WriteLog("%06X: STORE  R%02u, (R14+R%02u) [NCZ:%u%u%u, R%02u=%08X, R14+R%02u=%08X]\n", gpu_pc-2, IMM_2, IMM_1, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, IMM_1, RM+gpu_reg[14]);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[14] + RM;
+
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               GPUWriteLong(address & 0xFFFFFFFC, RN, GPU);
+       else
+               GPUWriteLong(address, RN, GPU);
+#else
        GPUWriteLong(gpu_reg[14] + RM, RN, GPU);
+#endif
 }
 
 static void gpu_opcode_store_r15_ri(void)
@@ -1594,7 +1834,16 @@ static void gpu_opcode_store_r15_ri(void)
        if (doGPUDis)
                WriteLog("%06X: STORE  R%02u, (R15+R%02u) [NCZ:%u%u%u, R%02u=%08X, R15+R%02u=%08X]\n", gpu_pc-2, IMM_2, IMM_1, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, IMM_1, RM+gpu_reg[15]);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT_STORE
+       uint32 address = gpu_reg[15] + RM;
+
+       if (address >= 0xF03000 && address <= 0xF03FFF)
+               GPUWriteLong(address & 0xFFFFFFFC, RN, GPU);
+       else
+               GPUWriteLong(address, RN, GPU);
+#else
        GPUWriteLong(gpu_reg[15] + RM, RN, GPU);
+#endif
 }
 
 static void gpu_opcode_nop(void)
@@ -1644,10 +1893,17 @@ static void gpu_opcode_storew(void)
        if (doGPUDis)
                WriteLog("%06X: STOREW R%02u, (R%02u) [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X]\n", gpu_pc-2, IMM_2, IMM_1, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, IMM_1, RM);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               GPUWriteLong(RM & 0xFFFFFFFE, RN & 0xFFFF, GPU);
+       else
+               JaguarWriteWord(RM, RN, GPU);
+#else
        if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
                GPUWriteLong(RM, RN & 0xFFFF, GPU);
        else
                JaguarWriteWord(RM, RN, GPU);
+#endif
 }
 
 static void gpu_opcode_store(void)
@@ -1656,13 +1912,33 @@ static void gpu_opcode_store(void)
        if (doGPUDis)
                WriteLog("%06X: STORE  R%02u, (R%02u) [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X]\n", gpu_pc-2, IMM_2, IMM_1, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN, IMM_1, RM);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               GPUWriteLong(RM & 0xFFFFFFFC, RN, GPU);
+       else
+               GPUWriteLong(RM, RN, GPU);
+#else
        GPUWriteLong(RM, RN, GPU);
+#endif
 }
 
 static void gpu_opcode_storep(void)
 {
+#ifdef GPU_CORRECT_ALIGNMENT
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+       {
+               GPUWriteLong((RM & 0xFFFFFFF8) + 0, gpu_hidata, GPU);
+               GPUWriteLong((RM & 0xFFFFFFF8) + 4, RN, GPU);
+       }
+       else
+       {
+               GPUWriteLong(RM + 0, gpu_hidata, GPU);
+               GPUWriteLong(RM + 4, RN, GPU);
+       }
+#else
        GPUWriteLong(RM + 0, gpu_hidata, GPU);
        GPUWriteLong(RM + 4, RN, GPU);
+#endif
 }
 
 static void gpu_opcode_loadb(void)
@@ -1687,23 +1963,60 @@ static void gpu_opcode_loadw(void)
        if (doGPUDis)
                WriteLog("%06X: LOADW  (R%02u), R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               RN = GPUReadLong(RM & 0xFFFFFFFE, GPU) & 0xFFFF;
+       else
+               RN = JaguarReadWord(RM, GPU);
+#else
        if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
                RN = GPUReadLong(RM, GPU) & 0xFFFF;
        else
                RN = JaguarReadWord(RM, GPU);
+#endif
 #ifdef GPU_DIS_LOADW
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
 }
 
+// According to the docs, & "Do The Same", this address is long aligned...
+// So let's try it:
+// And it works!!! Need to fix all instances...
+// Also, Power Drive Rally seems to contradict the idea that only LOADs in
+// the $F03000-$F03FFF range are aligned...
+#warning "!!! Alignment issues, need to find definitive final word on this !!!"
+/*
+Preliminary testing on real hardware seems to confirm that something strange goes on
+with unaligned reads in main memory. When the address is off by 1, the result is the
+same as the long address with the top byte replaced by something. So if the read is
+from $401, and $400 has 12 34 56 78, the value read will be $nn345678, where nn is a currently unknown vlaue.
+When the address is off by 2, the result would be $nnnn5678, where nnnn is unknown.
+When the address is off by 3, the result would be $nnnnnn78, where nnnnnn is unknown.
+It may be that the "unknown" values come from the prefetch queue, but not sure how
+to test that. They seem to be stable, though, which would indicate such a mechanism.
+Sometimes, however, the off by 2 case returns $12345678!
+*/
 static void gpu_opcode_load(void)
 {
 #ifdef GPU_DIS_LOAD
        if (doGPUDis)
                WriteLog("%06X: LOAD   (R%02u), R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 mask[4] = { 0x00000000, 0xFF000000, 0xFFFF0000, 0xFFFFFF00 };
+//     if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               RN = GPUReadLong(RM & 0xFFFFFFFC, GPU);
+//             RN = GPUReadLong(RM & 0x00FFFFFC, GPU);
+//     else
+//             RN = GPUReadLong(RM, GPU);
+       // Simulate garbage in unaligned reads...
+//seems that this behavior is different in GPU mem vs. main mem...
+//     if ((RM < 0xF03000) || (RM > 0xF0BFFF))
+//             RN |= mask[RM & 0x03];
+#else
        RN = GPUReadLong(RM, GPU);
+#endif
 #ifdef GPU_DIS_LOAD
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
@@ -1712,8 +2025,21 @@ static void gpu_opcode_load(void)
 
 static void gpu_opcode_loadp(void)
 {
+#ifdef GPU_CORRECT_ALIGNMENT
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+       {
+               gpu_hidata = GPUReadLong((RM & 0xFFFFFFF8) + 0, GPU);
+               RN                 = GPUReadLong((RM & 0xFFFFFFF8) + 4, GPU);
+       }
+       else
+       {
+               gpu_hidata = GPUReadLong(RM + 0, GPU);
+               RN                 = GPUReadLong(RM + 4, GPU);
+       }
+#else
        gpu_hidata = GPUReadLong(RM + 0, GPU);
        RN                 = GPUReadLong(RM + 4, GPU);
+#endif
 }
 
 static void gpu_opcode_load_r14_indexed(void)
@@ -1722,7 +2048,16 @@ static void gpu_opcode_load_r14_indexed(void)
        if (doGPUDis)
                WriteLog("%06X: LOAD   (R14+$%02X), R%02u [NCZ:%u%u%u, R14+$%02X=%08X, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1] << 2, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, gpu_convert_zero[IMM_1] << 2, gpu_reg[14]+(gpu_convert_zero[IMM_1] << 2), IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[14] + (gpu_convert_zero[IMM_1] << 2);
+
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               RN = GPUReadLong(address & 0xFFFFFFFC, GPU);
+       else
+               RN = GPUReadLong(address, GPU);
+#else
        RN = GPUReadLong(gpu_reg[14] + (gpu_convert_zero[IMM_1] << 2), GPU);
+#endif
 #ifdef GPU_DIS_LOAD14I
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
@@ -1735,7 +2070,16 @@ static void gpu_opcode_load_r15_indexed(void)
        if (doGPUDis)
                WriteLog("%06X: LOAD   (R15+$%02X), R%02u [NCZ:%u%u%u, R15+$%02X=%08X, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1] << 2, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, gpu_convert_zero[IMM_1] << 2, gpu_reg[15]+(gpu_convert_zero[IMM_1] << 2), IMM_2, RN);
 #endif
+#ifdef GPU_CORRECT_ALIGNMENT
+       uint32 address = gpu_reg[15] + (gpu_convert_zero[IMM_1] << 2);
+
+       if ((RM >= 0xF03000) && (RM <= 0xF03FFF))
+               RN = GPUReadLong(address & 0xFFFFFFFC, GPU);
+       else
+               RN = GPUReadLong(address, GPU);
+#else
        RN = GPUReadLong(gpu_reg[15] + (gpu_convert_zero[IMM_1] << 2), GPU);
+#endif
 #ifdef GPU_DIS_LOAD15I
        if (doGPUDis)
                WriteLog("[NCZ:%u%u%u, R%02u=%08X]\n", gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
@@ -1848,7 +2192,7 @@ static void gpu_opcode_bclr(void)
        if (doGPUDis)
                WriteLog("%06X: BCLR   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 res = RN & ~(1 << IMM_1);
+       uint32 res = RN & ~(1 << IMM_1);
        RN = res;
        SET_ZN(res);
 #ifdef GPU_DIS_BCLR
@@ -1876,7 +2220,7 @@ static void gpu_opcode_bset(void)
        if (doGPUDis)
                WriteLog("%06X: BSET   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 res = RN | (1 << IMM_1);
+       uint32 res = RN | (1 << IMM_1);
        RN = res;
        SET_ZN(res);
 #ifdef GPU_DIS_BSET
@@ -1894,7 +2238,7 @@ static void gpu_opcode_imacn(void)
 static void gpu_opcode_mtoi(void)
 {
        uint32 _RM = RM;
-       uint32 res = RN = (((INT32)_RM >> 8) & 0xFF800000) | (_RM & 0x007FFFFF);
+       uint32 res = RN = (((int32)_RM >> 8) & 0xFF800000) | (_RM & 0x007FFFFF);
        SET_ZN(res);
 }
 
@@ -1930,7 +2274,7 @@ static void gpu_opcode_mmult(void)
        if (gpu_matrix_control & 0x10)                          // Column stepping
        {
                for(int i=0; i<count; i++)
-               { 
+               {
                        int16 a;
                        if (i & 0x01)
                                a = (int16)((gpu_alternate_reg[IMM_1 + (i >> 1)] >> 16) & 0xFFFF);
@@ -1999,8 +2343,8 @@ static void gpu_opcode_div(void)  // RN / RM
        {
                if (gpu_div_control & 0x01)             // 16.16 division
                {
-                       RN = ((UINT64)RN << 16) / RM;
-                       gpu_remain = ((UINT64)RN << 16) % RM;
+                       RN = ((uint64)RN << 16) / RM;
+                       gpu_remain = ((uint64)RN << 16) % RM;
                }
                else
                {
@@ -2056,7 +2400,7 @@ static void gpu_opcode_neg(void)
        if (doGPUDis)
                WriteLog("%06X: NEG    R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 res = -RN;
+       uint32 res = -RN;
        SET_ZNC_SUB(0, RN, res);
        RN = res;
 #ifdef GPU_DIS_NEG
@@ -2073,8 +2417,8 @@ static void gpu_opcode_shlq(void)
 #endif
 // Was a bug here...
 // (Look at Aaron's code: If r1 = 32, then 32 - 32 = 0 which is wrong!)
-       INT32 r1 = 32 - IMM_1;
-       UINT32 res = RN << r1;
+       int32 r1 = 32 - IMM_1;
+       uint32 res = RN << r1;
        SET_ZN(res); gpu_flag_c = (RN >> 31) & 1;
        RN = res;
 #ifdef GPU_DIS_SHLQ
@@ -2089,8 +2433,8 @@ static void gpu_opcode_shrq(void)
        if (doGPUDis)
                WriteLog("%06X: SHRQ   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       INT32 r1 = gpu_convert_zero[IMM_1];
-       UINT32 res = RN >> r1;
+       int32 r1 = gpu_convert_zero[IMM_1];
+       uint32 res = RN >> r1;
        SET_ZN(res); gpu_flag_c = RN & 1;
        RN = res;
 #ifdef GPU_DIS_SHRQ
@@ -2105,8 +2449,8 @@ static void gpu_opcode_ror(void)
        if (doGPUDis)
                WriteLog("%06X: ROR    R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 r1 = RM & 0x1F;
-       UINT32 res = (RN >> r1) | (RN << (32 - r1));
+       uint32 r1 = RM & 0x1F;
+       uint32 res = (RN >> r1) | (RN << (32 - r1));
        SET_ZN(res); gpu_flag_c = (RN >> 31) & 1;
        RN = res;
 #ifdef GPU_DIS_ROR
@@ -2121,9 +2465,9 @@ static void gpu_opcode_rorq(void)
        if (doGPUDis)
                WriteLog("%06X: RORQ   #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 r1 = gpu_convert_zero[IMM_1 & 0x1F];
-       UINT32 r2 = RN;
-       UINT32 res = (r2 >> r1) | (r2 << (32 - r1));
+       uint32 r1 = gpu_convert_zero[IMM_1 & 0x1F];
+       uint32 r2 = RN;
+       uint32 res = (r2 >> r1) | (r2 << (32 - r1));
        RN = res;
        SET_ZN(res); gpu_flag_c = (r2 >> 31) & 0x01;
 #ifdef GPU_DIS_RORQ
@@ -2135,9 +2479,9 @@ static void gpu_opcode_rorq(void)
 static void gpu_opcode_sha(void)
 {
 /*     int dreg = jaguar.op & 31;
-       INT32 r1 = (INT32)jaguar.r[(jaguar.op >> 5) & 31];
-       UINT32 r2 = jaguar.r[dreg];
-       UINT32 res;
+       int32 r1 = (int32)jaguar.r[(jaguar.op >> 5) & 31];
+       uint32 r2 = jaguar.r[dreg];
+       uint32 res;
 
        CLR_ZNC;
        if (r1 < 0)
@@ -2147,7 +2491,7 @@ static void gpu_opcode_sha(void)
        }
        else
        {
-               res = (r1 >= 32) ? ((INT32)r2 >> 31) : ((INT32)r2 >> r1);
+               res = (r1 >= 32) ? ((int32)r2 >> 31) : ((int32)r2 >> r1);
                jaguar.FLAGS |= (r2 << 1) & 2;
        }
        jaguar.r[dreg] = res;
@@ -2157,16 +2501,16 @@ static void gpu_opcode_sha(void)
        if (doGPUDis)
                WriteLog("%06X: SHA    R%02u, R%02u [NCZ:%u%u%u, R%02u=%08X, R%02u=%08X] -> ", gpu_pc-2, IMM_1, IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_1, RM, IMM_2, RN);
 #endif
-       UINT32 res;
+       uint32 res;
 
-       if ((INT32)RM < 0)
+       if ((int32)RM < 0)
        {
-               res = ((INT32)RM <= -32) ? 0 : (RN << -(INT32)RM);
+               res = ((int32)RM <= -32) ? 0 : (RN << -(int32)RM);
                gpu_flag_c = RN >> 31;
        }
        else
        {
-               res = ((INT32)RM >= 32) ? ((INT32)RN >> 31) : ((INT32)RN >> (INT32)RM);
+               res = ((int32)RM >= 32) ? ((int32)RN >> 31) : ((int32)RN >> (int32)RM);
                gpu_flag_c = RN & 0x01;
        }
        RN = res;
@@ -2212,7 +2556,7 @@ static void gpu_opcode_sharq(void)
        if (doGPUDis)
                WriteLog("%06X: SHARQ  #%u, R%02u [NCZ:%u%u%u, R%02u=%08X] -> ", gpu_pc-2, gpu_convert_zero[IMM_1], IMM_2, gpu_flag_n, gpu_flag_c, gpu_flag_z, IMM_2, RN);
 #endif
-       UINT32 res = (INT32)RN >> gpu_convert_zero[IMM_1];
+       uint32 res = (int32)RN >> gpu_convert_zero[IMM_1];
        SET_ZN(res); gpu_flag_c = RN & 0x01;
        RN = res;
 #ifdef GPU_DIS_SHARQ
@@ -2247,3 +2591,13 @@ static void gpu_opcode_sh(void)
 //Temporary: Testing only!
 //#include "gpu2.cpp"
 //#include "gpu3.cpp"
+
+#else
+
+// New thread-safe GPU core
+
+int GPUCore(void * data)
+{
+}
+
+#endif