]> Shamusworld >> Repos - virtualjaguar/blob - src/jaguar.cpp
0a54ea4ba1579d5d47f78b44d2e57f4bf128c030
[virtualjaguar] / src / jaguar.cpp
1 //
2 // JAGUAR.CPP
3 //
4 // Originally by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Carwin Jones (BeOS)
6 // Cleanups and endian wrongness amelioration by James L. Hammons
7 // Note: Endian wrongness probably stems from the MAME origins of this emu and
8 //       the braindead way in which MAME handles memory. :-)
9 //
10
11 #include "jaguar.h"
12
13 #include <SDL.h>
14 #include "SDL_opengl.h"
15 #include "cdrom.h"
16 #include "dsp.h"
17 #include "event.h"
18 #include "gpu.h"
19 #include "gui.h"
20 #include "jerry.h"
21 #include "joystick.h"
22 #include "log.h"
23 #include "m68k.h"
24 #include "memory.h"
25 #include "settings.h"
26 #include "tom.h"
27 #include "video.h"
28
29 #define CPU_DEBUG
30 //Do this in makefile??? Yes! Could, but it's easier to define here...
31 //#define LOG_UNMAPPED_MEMORY_ACCESSES
32 //#define ABORT_ON_UNMAPPED_MEMORY_ACCESS
33 #define ABORT_ON_ILLEGAL_INSTRUCTIONS
34 //#define ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
35 #define CPU_DEBUG_MEMORY
36
37 // Private function prototypes
38
39 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who = UNKNOWN);
40 unsigned jaguar_unknown_readword(unsigned address, uint32 who = UNKNOWN);
41 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who = UNKNOWN);
42 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who = UNKNOWN);
43 void M68K_show_context(void);
44
45 // External variables
46
47 //extern bool hardwareTypeNTSC;                                         // Set to false for PAL
48 #ifdef CPU_DEBUG_MEMORY
49 extern bool startMemLog;                                                        // Set by "e" key
50 extern int effect_start;
51 extern int effect_start2, effect_start3, effect_start4, effect_start5, effect_start6;
52 #endif
53
54 // Memory debugging identifiers
55
56 const char * whoName[9] =
57         { "Unknown", "Jaguar", "DSP", "GPU", "TOM", "JERRY", "M68K", "Blitter", "OP" };
58
59 uint32 jaguar_active_memory_dumps = 0;
60
61 uint32 jaguarMainRomCRC32, jaguarRomSize, jaguarRunAddress;
62
63 uint8 * jaguarMainRam = NULL;
64 uint8 * jaguarMainRom = NULL;
65 uint8 * jaguarBootRom = NULL;
66 uint8 * jaguarCDBootROM = NULL;
67 bool BIOSLoaded = false;
68 bool CDBIOSLoaded = false;
69
70 #ifdef CPU_DEBUG_MEMORY
71 uint8 writeMemMax[0x400000], writeMemMin[0x400000];
72 uint8 readMem[0x400000];
73 uint32 returnAddr[4000], raPtr = 0xFFFFFFFF;
74 #endif
75
76 uint32 pcQueue[0x400];
77 uint32 pcQPtr = 0;
78
79 //
80 // Callback function to detect illegal instructions
81 //
82 void GPUDumpDisassembly(void);
83 void GPUDumpRegisters(void);
84         static bool start = false;
85 void M68KInstructionHook(void)
86 {
87         uint32 m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
88
89 // For tracebacks...
90 // Ideally, we'd save all the registers as well...
91         pcQueue[pcQPtr++] = m68kPC;
92         pcQPtr &= 0x3FF;
93
94         if (m68kPC & 0x01)              // Oops! We're fetching an odd address!
95         {
96                 WriteLog("M68K: Attempted to execute from an odd adress!\n\nBacktrace:\n\n");
97
98                 static char buffer[2048];
99                 for(int i=0; i<0x400; i++)
100                 {
101                         m68k_disassemble(buffer, pcQueue[(pcQPtr + i) & 0x3FF], M68K_CPU_TYPE_68000);
102                         WriteLog("\t%08X: %s\n", pcQueue[(pcQPtr + i) & 0x3FF], buffer);
103                 }
104                 WriteLog("\n");
105
106                 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
107                 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
108                 for(int i=0; i<10; i++)
109                         WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
110                 WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
111                 M68K_show_context();
112                 LogDone();
113                 exit(0);
114         }
115
116 /*      if (m68kPC >= 0x807EC4 && m68kPC <= 0x807EDB)
117         {
118                 static char buffer[2048];
119                 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
120                 WriteLog("%08X: %s", m68kPC, buffer);
121                 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
122                         m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
123                         m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
124         }//*/
125 /*      if (m68kPC == 0x8D0E48 && effect_start5)
126         {
127                 WriteLog("\nM68K: At collision detection code. Exiting!\n\n");
128                 GPUDumpRegisters();
129                 GPUDumpDisassembly();
130                 log_done();
131                 exit(0);
132         }//*/
133 /*      uint16 opcode = JaguarReadWord(m68kPC);
134         if (opcode == 0x4E75)   // RTS
135         {
136                 if (startMemLog)
137 //                      WriteLog("Jaguar: Returning from subroutine to %08X\n", JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7)));
138                 {
139                         uint32 addr = JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7));
140                         bool found = false;
141                         if (raPtr != 0xFFFFFFFF)
142                         {
143                                 for(uint32 i=0; i<=raPtr; i++)
144                                 {
145                                         if (returnAddr[i] == addr)
146                                         {
147                                                 found = true;
148                                                 break;
149                                         }
150                                 }
151                         }
152
153                         if (!found)
154                                 returnAddr[++raPtr] = addr;
155                 }
156         }//*/
157
158 //Flip Out! debugging...
159 //805F46, 806486
160 /*
161 00805FDC: movea.l #$9c6f8, A0           D0=00100010, A0=00100000
162 00805FE2: move.w  #$10, (A0)+           D0=00100010, A0=0009C6F8
163 00805FE6: cmpa.l  #$c96f8, A0           D0=00100010, A0=0009C6FA
164 00805FEC: bne     805fe2                D0=00100010, A0=0009C6FA
165
166 0080603A: move.l  #$11ed7c, $100.w              D0=61700080, A0=000C96F8, D1=00000000, A1=000040D8
167
168 0012314C: move.l  (A0)+, (A1)+          D0=61700080, A0=00124174, D1=00000000, A1=00F03FFC
169 0012314E: cmpa.l  #$f04000, A1          D0=61700080, A0=00124178, D1=00000000, A1=00F04000
170 00123154: blt     12314c                D0=61700080, A0=00124178, D1=00000000, A1=00F04000
171 00123156: move.l  #$0, $f035d0.l                D0=61700080, A0=00124178, D1=00000000, A1=00F04000
172 00123160: move.l  #$f03000, $f02110.l           D0=61700080, A0=00124178, D1=00000000, A1=00F04000
173 0012316A: move.l  #$1, $f02114.l                D0=61700080, A0=00124178, D1=00000000, A1=00F04000
174 00123174: rts           D0=61700080, A0=00124178, D1=00000000, A1=00F04000
175 */
176 /*      static char buffer[2048];
177 //if (m68kPC > 0x805F48) start = true;
178 //if (m68kPC > 0x806486) start = true;
179 //if (m68kPC == 0x805FEE) start = true;
180 //if (m68kPC == 0x80600C)// start = true;
181 if (m68kPC == 0x802058) start = true;
182 //{
183 //      GPUDumpRegisters();
184 //      GPUDumpDisassembly();
185 //
186 //      M68K_show_context();
187 //      log_done();
188 //      exit(0);
189 //}
190         if (start)
191         {
192         m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
193         WriteLog("%08X: %s \t\tD0=%08X, A0=%08X, D1=%08X, A1=%08X\n", m68kPC, buffer, m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_A1));
194         }//*/
195
196 /*      if (m68kPC == 0x803F16)
197         {
198                 WriteLog("M68K: Registers found at $803F16:\n");
199                 WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
200                 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
201                         WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
202                 WriteLog("\n");
203                 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
204                         WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
205         }*/
206 //Looks like the DSP is supposed to return $12345678 when it finishes its validation routine...
207 // !!! Investigate !!!
208 /*extern bool doDSPDis;
209         static bool disgo = false;
210         if (m68kPC == 0x50222)
211         {
212                 // CD BIOS hacking
213 //              WriteLog("M68K: About to stuff $12345678 into $F1B000 (=%08X)...\n", DSPReadLong(0xF1B000, M68K));
214 //              DSPWriteLong(0xF1B000, 0x12345678, M68K);
215 //              disgo = true;
216         }
217         if (m68kPC == 0x5000)
218 //              doDSPDis = true;
219                 disgo = true;
220         if (disgo)
221         {
222                 static char buffer[2048];
223                 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
224                 WriteLog("%08X: %s", m68kPC, buffer);
225                 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
226                         m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
227                         m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
228         }//*/
229         if (m68kPC == 0x82E1A)
230         {
231                 static char buffer[2048];
232                 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
233                 WriteLog("--> [Routine start] %08X: %s", m68kPC, buffer);
234                 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X(cmd), D1=%08X(# bytes), D2=%08X\n",
235                         m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
236                         m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
237         }//*/
238         if (m68kPC == 0x82E58)
239                 WriteLog("--> [Routine end]\n");
240         if (m68kPC == 0x80004)
241         {
242                 WriteLog("--> [Calling BusWrite2] D2: %08X\n", m68k_get_reg(NULL, M68K_REG_D2));
243 //              m68k_set_reg(M68K_REG_D2, 0x12345678);
244         }//*/
245 /*
246 CD_init::       -> $3000
247 BIOS_VER::      -> $3004
248 CD_mode::       -> $3006
249 CD_ack::        -> $300C
250 CD_jeri::       -> $3012
251 CD_spin::       -> $3018
252 CD_stop::       -> $301E
253 CD_mute::       -> $3024
254 CD_umute::      -> $302A
255 CD_paus::       -> $3030
256 CD_upaus::      -> $3036
257 CD_read::       -> $303C
258 CD_uread::      -> $3042
259 CD_setup::      -> $3048
260 CD_ptr::        -> $304E
261 CD_osamp::      -> $3054
262 CD_getoc::      -> $305A
263 CD_initm::      -> $3060
264 CD_initf::      -> $3066
265 CD_switch::     -> $306C
266 */
267         if (m68kPC == 0x3000)
268                 WriteLog("M68K: CD_init\n");
269         else if (m68kPC == 0x3006 + (6 * 0))
270                 WriteLog("M68K: CD_mode\n");
271         else if (m68kPC == 0x3006 + (6 * 1))
272                 WriteLog("M68K: CD_ack\n");
273         else if (m68kPC == 0x3006 + (6 * 2))
274                 WriteLog("M68K: CD_jeri\n");
275         else if (m68kPC == 0x3006 + (6 * 3))
276                 WriteLog("M68K: CD_spin\n");
277         else if (m68kPC == 0x3006 + (6 * 4))
278                 WriteLog("M68K: CD_stop\n");
279         else if (m68kPC == 0x3006 + (6 * 5))
280                 WriteLog("M68K: CD_mute\n");
281         else if (m68kPC == 0x3006 + (6 * 6))
282                 WriteLog("M68K: CD_umute\n");
283         else if (m68kPC == 0x3006 + (6 * 7))
284                 WriteLog("M68K: CD_paus\n");
285         else if (m68kPC == 0x3006 + (6 * 8))
286                 WriteLog("M68K: CD_upaus\n");
287         else if (m68kPC == 0x3006 + (6 * 9))
288                 WriteLog("M68K: CD_read\n");
289         else if (m68kPC == 0x3006 + (6 * 10))
290                 WriteLog("M68K: CD_uread\n");
291         else if (m68kPC == 0x3006 + (6 * 11))
292                 WriteLog("M68K: CD_setup\n");
293         else if (m68kPC == 0x3006 + (6 * 12))
294                 WriteLog("M68K: CD_ptr\n");
295         else if (m68kPC == 0x3006 + (6 * 13))
296                 WriteLog("M68K: CD_osamp\n");
297         else if (m68kPC == 0x3006 + (6 * 14))
298                 WriteLog("M68K: CD_getoc\n");
299         else if (m68kPC == 0x3006 + (6 * 15))
300                 WriteLog("M68K: CD_initm\n");
301         else if (m68kPC == 0x3006 + (6 * 16))
302                 WriteLog("M68K: CD_initf\n");
303         else if (m68kPC == 0x3006 + (6 * 17))
304                 WriteLog("M68K: CD_switch\n");
305
306         if (m68kPC >= 0x3000 && m68kPC <= 0x306C)
307                 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
308                         m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
309                         m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
310 //*/
311 #ifdef ABORT_ON_ILLEGAL_INSTRUCTIONS
312         if (!m68k_is_valid_instruction(m68k_read_memory_16(m68kPC), M68K_CPU_TYPE_68000))
313         {
314 #ifndef ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
315                 if (m68k_read_memory_16(m68kPC) == 0x4AFC)
316                 {
317                         // This is a kludge to let homebrew programs work properly (i.e., let the other processors
318                         // keep going even when the 68K dumped back to the debugger or what have you).
319 //dis no wok right!
320 //                      m68k_set_reg(M68K_REG_PC, m68kPC - 2);
321 // Try setting the vector to the illegal instruction...
322 //This doesn't work right either! Do something else! Quick!
323 //                      SET32(jaguar_mainRam, 0x10, m68kPC);
324
325                         return;
326                 }
327 #endif
328
329                 WriteLog("\nM68K encountered an illegal instruction at %08X!!!\n\nAborting!\n", m68kPC);
330                 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
331                 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
332                 for(int i=0; i<10; i++)
333                         WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
334                 WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
335                 M68K_show_context();
336
337 //temp
338 //      WriteLog("\n\n68K disasm\n\n");
339 //      jaguar_dasm(0x802000, 0x50C);
340 //      WriteLog("\n\n");
341 //endoftemp
342
343                 LogDone();
344                 exit(0);
345         }//*/
346 #endif
347 }
348
349 //
350 // Musashi 68000 read/write/IRQ functions
351 //
352
353 int irq_ack_handler(int level)
354 {
355         int vector = M68K_INT_ACK_AUTOVECTOR;
356
357         // The GPU/DSP/etc are probably *not* issuing an NMI, but it seems to work OK...
358
359         if (level == 7)
360         {
361                 m68k_set_irq(0);                                                // Clear the IRQ...
362                 vector = 64;                                                    // Set user interrupt #0
363         }
364
365         return vector;
366 }
367
368 unsigned int m68k_read_memory_8(unsigned int address)
369 {
370 #ifdef CPU_DEBUG_MEMORY
371         if ((address >= 0x000000) && (address <= 0x3FFFFF))
372         {
373                 if (startMemLog)
374                         readMem[address] = 1;
375         }
376 #endif
377 //WriteLog("[RM8] Addr: %08X\n", address);
378 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
379 /*      if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
380                 || address == 0x1AF05E)
381                 WriteLog("[RM8  PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, jaguar_mainRam[address]);//*/
382         unsigned int retVal = 0;
383
384         if ((address >= 0x000000) && (address <= 0x3FFFFF))
385                 retVal = jaguarMainRam[address];
386 //      else if ((address >= 0x800000) && (address <= 0xDFFFFF))
387         else if ((address >= 0x800000) && (address <= 0xDFFEFF))
388                 retVal = jaguarMainRom[address - 0x800000];
389         else if ((address >= 0xE00000) && (address <= 0xE3FFFF))
390                 retVal = jaguarBootRom[address - 0xE00000];
391         else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
392                 retVal = CDROMReadByte(address);
393         else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
394                 retVal = TOMReadByte(address, M68K);
395         else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
396                 retVal = JERRYReadByte(address, M68K);
397         else
398                 retVal = jaguar_unknown_readbyte(address, M68K);
399
400 //if (address >= 0x2800 && address <= 0x281F)
401 //      WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
402 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
403 //      WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
404     return retVal;
405 }
406
407 void gpu_dump_disassembly(void);
408 void gpu_dump_registers(void);
409
410 unsigned int m68k_read_memory_16(unsigned int address)
411 {
412 #ifdef CPU_DEBUG_MEMORY
413 /*      if ((address >= 0x000000) && (address <= 0x3FFFFE))
414         {
415                 if (startMemLog)
416                         readMem[address] = 1, readMem[address + 1] = 1;
417         }//*/
418 /*      if (effect_start && (address >= 0x8064FC && address <= 0x806501))
419         {
420                 return 0x4E71;  // NOP
421         }
422         if (effect_start2 && (address >= 0x806502 && address <= 0x806507))
423         {
424                 return 0x4E71;  // NOP
425         }
426         if (effect_start3 && (address >= 0x806512 && address <= 0x806517))
427         {
428                 return 0x4E71;  // NOP
429         }
430         if (effect_start4 && (address >= 0x806524 && address <= 0x806527))
431         {
432                 return 0x4E71;  // NOP
433         }
434         if (effect_start5 && (address >= 0x80653E && address <= 0x806543)) //Collision detection!
435         {
436                 return 0x4E71;  // NOP
437         }
438         if (effect_start6 && (address >= 0x806544 && address <= 0x806547))
439         {
440                 return 0x4E71;  // NOP
441         }//*/
442 #endif
443 //WriteLog("[RM16] Addr: %08X\n", address);
444 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005FBA)
445 //      for(int i=0; i<10000; i++)
446         WriteLog("[M68K] In routine #6!\n");//*/
447 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00006696) // GPU Program #4
448 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005B3C)    // GPU Program #2
449 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005BA8)    // GPU Program #3
450 {
451         WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
452         gpu_dump_registers();
453         gpu_dump_disassembly();
454 //      for(int i=0; i<10000; i++)
455 //              WriteLog("[M68K] About to run GPU!\n");
456 }//*/
457 //WriteLog("[WM8  PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
458 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x00006696 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x000066A8)
459 {
460         if (address == 0x000066A0)
461         {
462                 gpu_dump_registers();
463                 gpu_dump_disassembly();
464         }
465         for(int i=0; i<10000; i++)
466                 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
467 }//*/
468 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
469 /*      if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
470                 || address == 0x1AF05E)
471                 WriteLog("[RM16  PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, GET16(jaguar_mainRam, address));//*/
472     unsigned int retVal = 0;
473
474         if ((address >= 0x000000) && (address <= 0x3FFFFE))
475 //              retVal = (jaguar_mainRam[address] << 8) | jaguar_mainRam[address+1];
476                 retVal = GET16(jaguarMainRam, address);
477 //      else if ((address >= 0x800000) && (address <= 0xDFFFFE))
478         else if ((address >= 0x800000) && (address <= 0xDFFEFE))
479                 retVal = (jaguarMainRom[address - 0x800000] << 8) | jaguarMainRom[address - 0x800000 + 1];
480         else if ((address >= 0xE00000) && (address <= 0xE3FFFE))
481                 retVal = (jaguarBootRom[address - 0xE00000] << 8) | jaguarBootRom[address - 0xE00000 + 1];
482         else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
483                 retVal = CDROMReadWord(address, M68K);
484         else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
485                 retVal = TOMReadWord(address, M68K);
486         else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
487                 retVal = JERRYReadWord(address, M68K);
488         else
489                 retVal = jaguar_unknown_readword(address, M68K);
490
491 //if (address >= 0xF1B000 && address <= 0xF1CFFF)
492 //      WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
493 //if (address >= 0x2800 && address <= 0x281F)
494 //      WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
495 //$8B3AE -> Transferred from $F1C010
496 //$8B5E4 -> Only +1 read at $808AA
497 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
498 //      WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
499     return retVal;
500 }
501
502 unsigned int m68k_read_memory_32(unsigned int address)
503 {
504 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
505 /*      if (address == 0x51136 || address == 0xFB074 || address == 0x1AF05E)
506                 WriteLog("[RM32  PC=%08X] Addr: %08X, val: %08X\n", m68k_get_reg(NULL, M68K_REG_PC), address, (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2));//*/
507
508 //WriteLog("--> [RM32]\n");
509     return (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2);
510 }
511
512 void m68k_write_memory_8(unsigned int address, unsigned int value)
513 {
514 #ifdef CPU_DEBUG_MEMORY
515         if ((address >= 0x000000) && (address <= 0x3FFFFF))
516         {
517                 if (startMemLog)
518                 {
519                         if (value > writeMemMax[address])
520                                 writeMemMax[address] = value;
521                         if (value < writeMemMin[address])
522                                 writeMemMin[address] = value;
523                 }
524         }
525 #endif
526 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
527 //      WriteLog("M68K: Writing %02X at %08X\n", value, address);
528 //WriteLog("[WM8  PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
529 /*if (effect_start)
530         if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
531                 WriteLog("M68K: Byte %02X written at %08X by 68K\n", value, address);//*/
532
533         if ((address >= 0x000000) && (address <= 0x3FFFFF))
534                 jaguarMainRam[address] = value;
535         else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
536                 CDROMWriteByte(address, value, M68K);
537         else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
538                 TOMWriteByte(address, value, M68K);
539         else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
540                 JERRYWriteByte(address, value, M68K);
541         else
542                 jaguar_unknown_writebyte(address, value, M68K);
543 }
544
545 void m68k_write_memory_16(unsigned int address, unsigned int value)
546 {
547 #ifdef CPU_DEBUG_MEMORY
548         if ((address >= 0x000000) && (address <= 0x3FFFFE))
549         {
550                 if (startMemLog)
551                 {
552                         uint8 hi = value >> 8, lo = value & 0xFF;
553
554                         if (hi > writeMemMax[address])
555                                 writeMemMax[address] = hi;
556                         if (hi < writeMemMin[address])
557                                 writeMemMin[address] = hi;
558
559                         if (lo > writeMemMax[address+1])
560                                 writeMemMax[address+1] = lo;
561                         if (lo < writeMemMin[address+1])
562                                 writeMemMin[address+1] = lo;
563                 }
564         }
565 #endif
566 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
567 //      WriteLog("M68K: Writing %04X at %08X\n", value, address);
568 //WriteLog("[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
569 //if (address >= 0xF02200 && address <= 0xF0229F)
570 //      WriteLog("M68K: Writing to blitter --> %04X at %08X\n", value, address);
571 //if (address >= 0x0E75D0 && address <= 0x0E75E7)
572 //      WriteLog("M68K: Writing %04X at %08X, M68K PC=%08X\n", value, address, m68k_get_reg(NULL, M68K_REG_PC));
573 /*extern uint32 totalFrames;
574 if (address == 0xF02114)
575         WriteLog("M68K: Writing to GPU_CTRL (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));
576 if (address == 0xF02110)
577         WriteLog("M68K: Writing to GPU_PC (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));//*/
578 //if (address >= 0xF03B00 && address <= 0xF03DFF)
579 //      WriteLog("M68K: Writing %04X to %08X...\n", value, address);
580
581 /*if (address == 0x0100)//64*4)
582         WriteLog("M68K: Wrote word to VI vector value %04X...\n", value);//*/
583 /*if (effect_start)
584         if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
585                 WriteLog("M68K: Word %04X written at %08X by 68K\n", value, address);//*/
586 /*      if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
587                 || address == 0x1AF05E)
588                 WriteLog("[WM16  PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
589
590         if ((address >= 0x000000) && (address <= 0x3FFFFE))
591         {
592 /*              jaguar_mainRam[address] = value >> 8;
593                 jaguar_mainRam[address + 1] = value & 0xFF;*/
594                 SET16(jaguarMainRam, address, value);
595         }
596         else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
597                 CDROMWriteWord(address, value, M68K);
598         else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
599                 TOMWriteWord(address, value, M68K);
600         else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
601                 JERRYWriteWord(address, value, M68K);
602         else
603         {
604                 jaguar_unknown_writeword(address, value, M68K);
605 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
606                 WriteLog("\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
607                         m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
608                         m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
609 #endif
610         }
611 }
612
613 void m68k_write_memory_32(unsigned int address, unsigned int value)
614 {
615 //WriteLog("--> [WM32]\n");
616 /*if (address == 0x0100)//64*4)
617         WriteLog("M68K: Wrote dword to VI vector value %08X...\n", value);//*/
618 /*if (address >= 0xF03214 && address < 0xF0321F)
619         WriteLog("M68K: Writing DWORD (%08X) to GPU RAM (%08X)...\n", value, address);//*/
620 //M68K: Writing DWORD (88E30047) to GPU RAM (00F03214)...
621 /*extern bool doGPUDis;
622 if (address == 0xF03214 && value == 0x88E30047)
623 //      start = true;
624         doGPUDis = true;//*/
625 /*      if (address == 0x51136 || address == 0xFB074)
626                 WriteLog("[WM32  PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
627
628         m68k_write_memory_16(address, value >> 16);
629         m68k_write_memory_16(address + 2, value & 0xFFFF);
630 }
631
632
633 uint32 JaguarGetHandler(uint32 i)
634 {
635         return JaguarReadLong(i * 4);
636 }
637
638 bool JaguarInterruptHandlerIsValid(uint32 i) // Debug use only...
639 {
640         uint32 handler = JaguarGetHandler(i);
641         return (handler && (handler != 0xFFFFFFFF) ? 1 : 0);
642 }
643
644 void M68K_show_context(void)
645 {
646         WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
647         for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
648                 WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
649         WriteLog("\n");
650         for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
651                 WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
652
653         WriteLog("68K disasm\n");
654 //      jaguar_dasm(s68000readPC()-0x1000,0x20000);
655         JaguarDasm(m68k_get_reg(NULL, M68K_REG_PC) - 0x80, 0x200);
656 //      jaguar_dasm(0x5000, 0x14414);
657
658 //      WriteLog("\n.......[Cart start]...........\n\n");
659 //      jaguar_dasm(0x192000, 0x1000);//0x200);
660
661         WriteLog("..................\n");
662
663         if (TOMIRQEnabled(IRQ_VBLANK))
664         {
665                 WriteLog("vblank int: enabled\n");
666                 JaguarDasm(JaguarGetHandler(64), 0x200);
667         }
668         else
669                 WriteLog("vblank int: disabled\n");
670
671         WriteLog("..................\n");
672
673         for(int i=0; i<256; i++)
674                 WriteLog("handler %03i at $%08X\n", i, (unsigned int)JaguarGetHandler(i));
675 }
676
677 //
678 // Unknown read/write byte/word routines
679 //
680
681 // It's hard to believe that developers would be sloppy with their memory writes, yet in
682 // some cases the developers screwed up royal. E.g., Club Drive has the following code:
683 //
684 // 807EC4: movea.l #$f1b000, A1
685 // 807ECA: movea.l #$8129e0, A0
686 // 807ED0: move.l  A0, D0
687 // 807ED2: move.l  #$f1bb94, D1
688 // 807ED8: sub.l   D0, D1
689 // 807EDA: lsr.l   #2, D1
690 // 807EDC: move.l  (A0)+, (A1)+
691 // 807EDE: dbra    D1, 807edc
692 //
693 // The problem is at $807ED0--instead of putting A0 into D0, they really meant to put A1
694 // in. This mistake causes it to try and overwrite approximately $700000 worth of address
695 // space! (That is, unless the 68K causes a bus error...)
696
697 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
698 {
699 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
700         WriteLog("Jaguar: Unknown byte %02X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
701 #endif
702 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
703 //      extern bool finished;
704         finished = true;
705 //      extern bool doDSPDis;
706         if (who == DSP)
707                 doDSPDis = true;
708 #endif
709 }
710
711 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
712 {
713 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
714         WriteLog("Jaguar: Unknown word %04X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
715 #endif
716 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
717 //      extern bool finished;
718         finished = true;
719 //      extern bool doDSPDis;
720         if (who == DSP)
721                 doDSPDis = true;
722 #endif
723 }
724
725 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who/*=UNKNOWN*/)
726 {
727 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
728         WriteLog("Jaguar: Unknown byte read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
729 #endif
730 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
731 //      extern bool finished;
732         finished = true;
733 //      extern bool doDSPDis;
734         if (who == DSP)
735                 doDSPDis = true;
736 #endif
737     return 0xFF;
738 }
739
740 unsigned jaguar_unknown_readword(unsigned address, uint32 who/*=UNKNOWN*/)
741 {
742 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
743         WriteLog("Jaguar: Unknown word read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
744 #endif
745 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
746 //      extern bool finished;
747         finished = true;
748 //      extern bool doDSPDis;
749         if (who == DSP)
750                 doDSPDis = true;
751 #endif
752     return 0xFFFF;
753 }
754
755 //
756 // Disassemble M68K instructions at the given offset
757 //
758
759 unsigned int m68k_read_disassembler_8(unsigned int address)
760 {
761         return m68k_read_memory_8(address);
762 }
763
764 unsigned int m68k_read_disassembler_16(unsigned int address)
765 {
766         return m68k_read_memory_16(address);
767 }
768
769 unsigned int m68k_read_disassembler_32(unsigned int address)
770 {
771         return m68k_read_memory_32(address);
772 }
773
774 void JaguarDasm(uint32 offset, uint32 qt)
775 {
776 #ifdef CPU_DEBUG
777         static char buffer[2048];//, mem[64];
778         int pc = offset, oldpc;
779
780         for(uint32 i=0; i<qt; i++)
781         {
782 /*              oldpc = pc;
783                 for(int j=0; j<64; j++)
784                         mem[j^0x01] = jaguar_byte_read(pc + j);
785
786                 pc += Dasm68000((char *)mem, buffer, 0);
787                 WriteLog("%08X: %s\n", oldpc, buffer);//*/
788                 oldpc = pc;
789                 pc += m68k_disassemble(buffer, pc, M68K_CPU_TYPE_68000);
790                 WriteLog("%08X: %s\n", oldpc, buffer);//*/
791         }
792 #endif
793 }
794
795 uint8 JaguarReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
796 {
797         uint8 data = 0x00;
798
799         offset &= 0xFFFFFF;
800         if (offset < 0x400000)
801                 data = jaguarMainRam[offset & 0x3FFFFF];
802         else if ((offset >= 0x800000) && (offset < 0xC00000))
803                 data = jaguarMainRom[offset - 0x800000];
804         else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
805                 data = CDROMReadByte(offset, who);
806         else if ((offset >= 0xE00000) && (offset < 0xE40000))
807                 data = jaguarBootRom[offset & 0x3FFFF];
808         else if ((offset >= 0xF00000) && (offset < 0xF10000))
809                 data = TOMReadByte(offset, who);
810         else if ((offset >= 0xF10000) && (offset < 0xF20000))
811                 data = JERRYReadByte(offset, who);
812         else
813                 data = jaguar_unknown_readbyte(offset, who);
814
815         return data;
816 }
817
818 uint16 JaguarReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
819 {
820         offset &= 0xFFFFFF;
821         if (offset <= 0x3FFFFE)
822         {
823                 return (jaguarMainRam[(offset+0) & 0x3FFFFF] << 8) | jaguarMainRam[(offset+1) & 0x3FFFFF];
824         }
825         else if ((offset >= 0x800000) && (offset <= 0xBFFFFE))
826         {
827                 offset -= 0x800000;
828                 return (jaguarMainRom[offset+0] << 8) | jaguarMainRom[offset+1];
829         }
830 //      else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
831         else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFE))
832                 return CDROMReadWord(offset, who);
833         else if ((offset >= 0xE00000) && (offset <= 0xE3FFFE))
834                 return (jaguarBootRom[(offset+0) & 0x3FFFF] << 8) | jaguarBootRom[(offset+1) & 0x3FFFF];
835         else if ((offset >= 0xF00000) && (offset <= 0xF0FFFE))
836                 return TOMReadWord(offset, who);
837         else if ((offset >= 0xF10000) && (offset <= 0xF1FFFE))
838                 return JERRYReadWord(offset, who);
839
840         return jaguar_unknown_readword(offset, who);
841 }
842
843 void JaguarWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
844 {
845 //Need to check for writes in the range of $18FA70 + 8000...
846 /*if (effect_start)
847         if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
848                 WriteLog("JWB: Byte %02X written at %08X by %s\n", data, offset, whoName[who]);//*/
849
850         offset &= 0xFFFFFF;
851         if (offset < 0x400000)
852         {
853                 jaguarMainRam[offset & 0x3FFFFF] = data;
854                 return;
855         }
856         else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
857         {
858                 CDROMWriteByte(offset, data, who);
859                 return;
860         }
861         else if ((offset >= 0xF00000) && (offset <= 0xF0FFFF))
862         {
863                 TOMWriteByte(offset, data, who);
864                 return;
865         }
866         else if ((offset >= 0xF10000) && (offset <= 0xF1FFFF))
867         {
868                 JERRYWriteByte(offset, data, who);
869                 return;
870         }
871
872         jaguar_unknown_writebyte(offset, data, who);
873 }
874
875 uint32 starCount;
876 void JaguarWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
877 {
878 /*if (offset == 0x0100)//64*4)
879         WriteLog("M68K: %s wrote word to VI vector value %04X...\n", whoName[who], data);
880 if (offset == 0x0102)//64*4)
881         WriteLog("M68K: %s wrote word to VI vector+2 value %04X...\n", whoName[who], data);//*/
882 //TEMP--Mirror of F03000? Yes, but only 32-bit CPUs can do it (i.e., NOT the 68K!)
883 // PLUS, you would handle this in the GPU/DSP WriteLong code! Not here!
884 //Need to check for writes in the range of $18FA70 + 8000...
885 /*if (effect_start)
886         if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
887                 WriteLog("JWW: Word %04X written at %08X by %s\n", data, offset, whoName[who]);//*/
888 /*if (offset >= 0x2C00 && offset <= 0x2CFF)
889         WriteLog("Jaguar: Word %04X written to TOC+%02X by %s\n", data, offset-0x2C00, whoName[who]);//*/
890
891         offset &= 0xFFFFFF;
892
893         if (offset <= 0x3FFFFE)
894         {
895 /*
896 GPU Table (CD BIOS)
897
898 1A 69 F0 ($0000) -> Starfield
899 1A 73 C8 ($0001) -> Final clearing blit & bitmap blit?
900 1A 79 F0 ($0002)
901 1A 88 C0 ($0003)
902 1A 8F E8 ($0004) -> "Jaguar" small color logo?
903 1A 95 20 ($0005)
904 1A 9F 08 ($0006)
905 1A A1 38 ($0007)
906 1A AB 38 ($0008)
907 1A B3 C8 ($0009)
908 1A B9 C0 ($000A)
909 */
910
911 //This MUST be done by the 68K!
912 /*if (offset == 0x670C)
913         WriteLog("Jaguar: %s writing to location $670C...\n", whoName[who]);*/
914
915 /*extern bool doGPUDis;
916 //if ((offset == 0x100000 + 75522) && who == GPU)       // 76,226 -> 75522
917 if ((offset == 0x100000 + 128470) && who == GPU)        // 107,167 -> 128470 (384 x 250 screen size 16BPP)
918 //if ((offset >= 0x100000 && offset <= 0x12C087) && who == GPU)
919         doGPUDis = true;//*/
920 /*if (offset == 0x100000 + 128470) // 107,167 -> 128470 (384 x 250 screen size 16BPP)
921         WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);
922 if ((data & 0xFF00) != 0x7700)
923         WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
924 /*if ((offset >= 0x100000 && offset <= 0x147FFF) && who == GPU)
925         return;//*/
926 /*if ((data & 0xFF00) != 0x7700 && who == GPU)
927         WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
928 /*if ((offset >= 0x100000 + 0x48000 && offset <= 0x12C087 + 0x48000) && who == GPU)
929         return;//*/
930 /*extern bool doGPUDis;
931 if (offset == 0x120216 && who == GPU)
932         doGPUDis = true;//*/
933 /*extern uint32 gpu_pc;
934 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
935 {
936         uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
937         uint32 y = base / 0x300;
938         uint32 x = (base - (y * 0x300)) / 2;
939         WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
940 }//*/
941 /*
942 JWW: Writing starfield star 775E at 0011F650 (555984/1447)
943 */
944 //if (offset == (0x001E17F8 + 0x34))
945 /*if (who == GPU && offset == (0x001E17F8 + 0x34))
946         data = 0xFE3C;//*/
947 //      WriteLog("JWW: Write at %08X written to by %s.\n", 0x001E17F8 + 0x34, whoName[who]);//*/
948 /*extern uint32 gpu_pc;
949 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
950 {
951         extern int objectPtr;
952 //      if (offset > 0x148000)
953 //              return;
954         starCount++;
955         if (starCount > objectPtr)
956                 return;
957
958 //      if (starCount == 1)
959 //              WriteLog("--> Drawing 1st star...\n");
960 //
961 //      uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
962 //      uint32 y = base / 0x300;
963 //      uint32 x = (base - (y * 0x300)) / 2;
964 //      WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
965
966 //A star of interest...
967 //-->JWW: Writing starfield star 77C9 at 0011D31A (269/155) [s]
968 //1st trail +3(x), -1(y) -> 272, 154 -> 0011D020
969 //JWW: Blitter writing echo 77B3 at 0011D022...
970 }//*/
971 //extern bool doGPUDis;
972 /*if (offset == 0x11D022 + 0x48000 || offset == 0x11D022)// && who == GPU)
973 {
974 //      doGPUDis = true;
975         WriteLog("JWW: %s writing echo %04X at %08X...\n", whoName[who], data, offset);
976 //      LogBlit();
977 }
978 if (offset == 0x11D31A + 0x48000 || offset == 0x11D31A)
979         WriteLog("JWW: %s writing star %04X at %08X...\n", whoName[who], data, offset);//*/
980
981                 jaguarMainRam[(offset+0) & 0x3FFFFF] = data >> 8;
982                 jaguarMainRam[(offset+1) & 0x3FFFFF] = data & 0xFF;
983                 return;
984         }
985         else if (offset >= 0xDFFF00 && offset <= 0xDFFFFE)
986         {
987                 CDROMWriteWord(offset, data, who);
988                 return;
989         }
990         else if (offset >= 0xF00000 && offset <= 0xF0FFFE)
991         {
992                 TOMWriteWord(offset, data, who);
993                 return;
994         }
995         else if (offset >= 0xF10000 && offset <= 0xF1FFFE)
996         {
997                 JERRYWriteWord(offset, data, who);
998                 return;
999         }
1000         // Don't bomb on attempts to write to ROM
1001         else if (offset >= 0x800000 && offset <= 0xEFFFFF)
1002                 return;
1003
1004         jaguar_unknown_writeword(offset, data, who);
1005 }
1006
1007 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1008 uint32 JaguarReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
1009 {
1010         return (JaguarReadWord(offset, who) << 16) | JaguarReadWord(offset+2, who);
1011 }
1012
1013 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1014 void JaguarWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
1015 {
1016 /*      extern bool doDSPDis;
1017         if (offset < 0x400 && !doDSPDis)
1018         {
1019                 WriteLog("JLW: Write to %08X by %s... Starting DSP log!\n\n", offset, whoName[who]);
1020                 doDSPDis = true;
1021         }//*/
1022 /*if (offset == 0x0100)//64*4)
1023         WriteLog("M68K: %s wrote dword to VI vector value %08X...\n", whoName[who], data);//*/
1024
1025         JaguarWriteWord(offset, data >> 16, who);
1026         JaguarWriteWord(offset+2, data & 0xFFFF, who);
1027 }
1028
1029 //
1030 // Jaguar console initialization
1031 //
1032 void JaguarInit(void)
1033 {
1034 #ifdef CPU_DEBUG_MEMORY
1035         memset(readMem, 0x00, 0x400000);
1036         memset(writeMemMin, 0xFF, 0x400000);
1037         memset(writeMemMax, 0x00, 0x400000);
1038 #endif
1039         memory_malloc_secure((void **)&jaguarMainRam, 0x400000, "Jaguar 68K CPU RAM");
1040         memory_malloc_secure((void **)&jaguarMainRom, 0x600000, "Jaguar 68K CPU ROM");
1041         memory_malloc_secure((void **)&jaguarBootRom, 0x040000, "Jaguar 68K CPU BIOS ROM"); // Only uses half of this!
1042         memory_malloc_secure((void **)&jaguarCDBootROM, 0x040000, "Jaguar 68K CPU CD BIOS ROM");
1043         memset(jaguarMainRam, 0x00, 0x400000);
1044 //      memset(jaguar_mainRom, 0xFF, 0x200000); // & set it to all Fs...
1045 //      memset(jaguar_mainRom, 0x00, 0x200000); // & set it to all 0s...
1046 //NOTE: This *doesn't* fix FlipOut...
1047 //Or does it? Hmm...
1048 //Seems to want $01010101... Dunno why. Investigate!
1049         memset(jaguarMainRom, 0x01, 0x600000);  // & set it to all 01s...
1050 //      memset(jaguar_mainRom, 0xFF, 0x600000); // & set it to all Fs...
1051
1052         m68k_set_cpu_type(M68K_CPU_TYPE_68000);
1053         GPUInit();
1054         DSPInit();
1055         TOMInit();
1056         JERRYInit();
1057         CDROMInit();
1058 }
1059
1060 //New timer based code stuffola...
1061 void ScanlineCallback(void);
1062 void RenderCallback(void);
1063 //extern uint32 * backbuffer;
1064 void JaguarReset(void)
1065 {
1066 //NOTE: This causes a (virtual) crash if this is set in the config but not found... !!! FIX !!!
1067         if (vjs.useJaguarBIOS)
1068                 memcpy(jaguarMainRam, jaguarBootRom, 8);
1069         else
1070                 SET32(jaguarMainRam, 4, jaguarRunAddress);
1071
1072 //      WriteLog("jaguar_reset():\n");
1073         TOMReset();
1074         JERRYReset();
1075         GPUReset();
1076         DSPReset();
1077         CDROMReset();
1078     m68k_pulse_reset();                                                         // Reset the 68000
1079         WriteLog("Jaguar: 68K reset. PC=%06X SP=%08X\n", m68k_get_reg(NULL, M68K_REG_PC), m68k_get_reg(NULL, M68K_REG_A7));
1080
1081         // New timer base code stuffola...
1082         InitializeEventList();
1083         TOMResetBackbuffer(backbuffer);
1084 //      SetCallbackTime(ScanlineCallback, 63.5555);
1085         SetCallbackTime(ScanlineCallback, 31.77775);
1086 //      SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
1087 //      SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time
1088 }
1089
1090 void JaguarDone(void)
1091 {
1092 #ifdef CPU_DEBUG_MEMORY
1093 /*      WriteLog("\nJaguar: Memory Usage Stats (return addresses)\n\n");
1094
1095         for(uint32 i=0; i<=raPtr; i++)
1096         {
1097                 WriteLog("\t%08X\n", returnAddr[i]);
1098                 WriteLog("M68000 disassembly at $%08X...\n", returnAddr[i] - 16);
1099                 jaguar_dasm(returnAddr[i] - 16, 16);
1100                 WriteLog("\n");
1101         }
1102         WriteLog("\n");//*/
1103
1104 /*      int start = 0, end = 0;
1105         bool endTriggered = false, startTriggered = false;
1106         for(int i=0; i<0x400000; i++)
1107         {
1108                 if (readMem[i] && writeMemMin[i] != 0xFF && writeMemMax != 0x00)
1109                 {
1110                         if (!startTriggered)
1111                                 startTriggered = true, endTriggered = false, start = i;
1112
1113                         WriteLog("\t\tMin/Max @ %06X: %u/%u\n", i, writeMemMin[i], writeMemMax[i]);
1114                 }
1115                 else
1116                 {
1117                         if (!endTriggered)
1118                         {
1119                                 end = i - 1, endTriggered = true, startTriggered = false;
1120                                 WriteLog("\tMemory range accessed: %06X - %06X\n", start, end);
1121                         }
1122                 }
1123         }
1124         WriteLog("\n");//*/
1125 #endif
1126 //#ifdef CPU_DEBUG
1127 //      for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
1128 //              WriteLog("\tA%i = 0x%.8x\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
1129         int32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
1130         WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
1131         for(int i=-2; i<9; i++)
1132                 WriteLog("%06X: %08X\n", topOfStack + (i * 4), JaguarReadLong(topOfStack + (i * 4)));
1133
1134 /*      WriteLog("\nM68000 disassembly at $802288...\n");
1135         jaguar_dasm(0x802288, 3);
1136         WriteLog("\nM68000 disassembly at $802200...\n");
1137         jaguar_dasm(0x802200, 500);
1138         WriteLog("\nM68000 disassembly at $802518...\n");
1139         jaguar_dasm(0x802518, 100);//*/
1140
1141 /*      WriteLog("\n\nM68000 disassembly at $803F00 (look @ $803F2A)...\n");
1142         jaguar_dasm(0x803F00, 500);
1143         WriteLog("\n");//*/
1144
1145 /*      WriteLog("\n\nM68000 disassembly at $802B00 (look @ $802B5E)...\n");
1146         jaguar_dasm(0x802B00, 500);
1147         WriteLog("\n");//*/
1148
1149 /*      WriteLog("\n\nM68000 disassembly at $809900 (look @ $8099F8)...\n");
1150         jaguar_dasm(0x809900, 500);
1151         WriteLog("\n");//*/
1152 //8099F8
1153 /*      WriteLog("\n\nDump of $8093C8:\n\n");
1154         for(int i=0x8093C8; i<0x809900; i+=4)
1155                 WriteLog("%06X: %08X\n", i, JaguarReadLong(i));//*/
1156 /*      WriteLog("\n\nM68000 disassembly at $90006C...\n");
1157         jaguar_dasm(0x90006C, 500);
1158         WriteLog("\n");//*/
1159 /*      WriteLog("\n\nM68000 disassembly at $1AC000...\n");
1160         jaguar_dasm(0x1AC000, 6000);
1161         WriteLog("\n");//*/
1162
1163 //      WriteLog("Jaguar: CD BIOS version %04X\n", JaguarReadWord(0x3004));
1164         WriteLog("Jaguar: Interrupt enable = %02X\n", TOMReadByte(0xF000E1) & 0x1F);
1165         WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
1166         M68K_show_context();
1167 //#endif
1168
1169         CDROMDone();
1170         GPUDone();
1171         DSPDone();
1172         TOMDone();
1173         JERRYDone();
1174
1175         memory_free(jaguarMainRom);
1176         memory_free(jaguarMainRam);
1177         memory_free(jaguarBootRom);
1178         memory_free(jaguarCDBootROM);
1179 }
1180
1181 //
1182 // Main Jaguar execution loop (1 frame)
1183 //
1184 void JaguarExecute(uint32 * backbuffer, bool render)
1185 {
1186         uint16 vp = TOMReadWord(0xF0003E) + 1;
1187         uint16 vi = TOMReadWord(0xF0004E);
1188 //Using WO registers is OK, since we're the ones controlling access--there's nothing wrong here! ;-)
1189 //Though we shouldn't be able to do it using TOMReadWord... !!! FIX !!!
1190
1191 //      uint16 vdb = TOMReadWord(0xF00046);
1192 //Note: This is the *definite* end of the display, though VDE *might* be less than this...
1193 //      uint16 vbb = TOMReadWord(0xF00040);
1194 //It seems that they mean it when they say that VDE is the end of object processing.
1195 //However, we need to be able to tell the OP (or TOM) that we've reached the end of the
1196 //buffer and not to write any more pixels... !!! FIX !!!
1197 //      uint16 vde = TOMReadWord(0xF00048);
1198
1199         uint16 refreshRate = (vjs.hardwareTypeNTSC ? 60 : 50);
1200 //Not sure the above is correct, since the number of lines and timings given in the JTRM
1201 //seem to indicate the refresh rate is *half* the above...
1202 //      uint16 refreshRate = (vjs.hardwareTypeNTSC ? 30 : 25);
1203         // Should these be hardwired or read from VP? Yes, from VP!
1204         uint32 M68KCyclesPerScanline
1205                 = (vjs.hardwareTypeNTSC ? M68K_CLOCK_RATE_NTSC : M68K_CLOCK_RATE_PAL) / (vp * refreshRate);
1206         uint32 RISCCyclesPerScanline
1207                 = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL) / (vp * refreshRate);
1208
1209         TOMResetBackbuffer(backbuffer);
1210 /*extern int effect_start;
1211 if (effect_start)
1212         WriteLog("JagExe: VP=%u, VI=%u, CPU CPS=%u, GPU CPS=%u\n", vp, vi, M68KCyclesPerScanline, RISCCyclesPerScanline);//*/
1213
1214 //extern int start_logging;
1215         for(uint16 i=0; i<vp; i++)
1216         {
1217                 // Increment the horizontal count (why? RNG? Besides which, this is *NOT* cycle accurate!)
1218                 TOMWriteWord(0xF00004, (TOMReadWord(0xF00004) + 1) & 0x7FF);
1219
1220                 TOMWriteWord(0xF00006, i);                                      // Write the VC
1221
1222 //              if (i == vi)                                                            // Time for Vertical Interrupt?
1223 //Not sure if this is correct...
1224 //Seems to be, kinda. According to the JTRM, this should only fire on odd lines in non-interlace mode...
1225 //Which means that it normally wouldn't go when it's zero.
1226                 if (i == vi && i > 0 && TOMIRQEnabled(IRQ_VBLANK))      // Time for Vertical Interrupt?
1227                 {
1228                         // We don't have to worry about autovectors & whatnot because the Jaguar
1229                         // tells you through its HW registers who sent the interrupt...
1230                         TOMSetPendingVideoInt();
1231                         m68k_set_irq(7);
1232                 }
1233
1234 //if (start_logging)
1235 //      WriteLog("About to execute M68K (%u)...\n", i);
1236                 m68k_execute(M68KCyclesPerScanline);
1237 //if (start_logging)
1238 //      WriteLog("About to execute TOM's PIT (%u)...\n", i);
1239                 TOMExecPIT(RISCCyclesPerScanline);
1240 //if (start_logging)
1241 //      WriteLog("About to execute JERRY's PIT (%u)...\n", i);
1242                 JERRYExecPIT(RISCCyclesPerScanline);
1243 //if (start_logging)
1244 //      WriteLog("About to execute JERRY's SSI (%u)...\n", i);
1245                 JERRYI2SExec(RISCCyclesPerScanline);
1246                 BUTCHExec(RISCCyclesPerScanline);
1247 //if (start_logging)
1248 //      WriteLog("About to execute GPU (%u)...\n", i);
1249                 GPUExec(RISCCyclesPerScanline);
1250
1251                 if (vjs.DSPEnabled)
1252                 {
1253                         if (vjs.usePipelinedDSP)
1254                                 DSPExecP2(RISCCyclesPerScanline);       // Pipelined DSP execution (3 stage)...
1255                         else
1256                                 DSPExec(RISCCyclesPerScanline);         // Ordinary non-pipelined DSP
1257 //                      DSPExecComp(RISCCyclesPerScanline);             // Comparison core
1258                 }
1259
1260 //if (start_logging)
1261 //      WriteLog("About to execute OP (%u)...\n", i);
1262                 TOMExecScanline(i, render);
1263         }
1264 }
1265
1266 // Temp debugging stuff
1267
1268 void DumpMainMemory(void)
1269 {
1270         FILE * fp = fopen("./memdump.bin", "wb");
1271
1272         if (fp == NULL)
1273                 return;
1274
1275         fwrite(jaguarMainRam, 1, 0x400000, fp);
1276         fclose(fp);
1277 }
1278
1279 uint8 * GetRamPtr(void)
1280 {
1281         return jaguarMainRam;
1282 }
1283
1284 //
1285 // New Jaguar execution stack
1286 //
1287
1288 #if 0
1289
1290 void JaguarExecuteNew(void)
1291 {
1292         extern bool finished, showGUI;
1293         extern bool debounceRunKey;
1294         // Pass a message to the "joystick" code to debounce the ESC key...
1295         debounceRunKey = true;
1296         finished = false;
1297 /*      InitializeEventList();
1298         TOMResetBackbuffer(backbuffer);
1299 //      SetCallbackTime(ScanlineCallback, 63.5555);
1300         SetCallbackTime(ScanlineCallback, 31.77775);
1301 //      SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
1302 //      SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time//*/
1303 //      uint8 * keystate = SDL_GetKeyState(NULL);
1304
1305         do
1306         {
1307                 double timeToNextEvent = GetTimeToNextEvent();
1308 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1309
1310                 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1311                 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1312
1313                 if (vjs.DSPEnabled)
1314                 {
1315                         if (vjs.usePipelinedDSP)
1316                                 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent));        // Pipelined DSP execution (3 stage)...
1317                         else
1318                                 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent));          // Ordinary non-pipelined DSP
1319                 }
1320
1321                 HandleNextEvent();
1322
1323 //              if (keystate[SDLK_ESCAPE])
1324 //                      break;
1325
1326 //          SDL_PumpEvents();   // Needed to keep the keystate current...
1327         }
1328         while (!finished);
1329 }
1330
1331 void ScanlineCallback(void)
1332 {
1333         uint16 vc = TOMReadWord(0xF00006);
1334         uint16 vp = TOMReadWord(0xF0003E) + 1;
1335         uint16 vi = TOMReadWord(0xF0004E);
1336 //      uint16 vbb = TOMReadWord(0xF00040);
1337         vc++;
1338
1339         if (vc >= vp)
1340                 vc = 0;
1341
1342 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1343         TOMWriteWord(0xF00006, vc);
1344
1345 //This is a crappy kludge, but maybe it'll work for now...
1346 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1347         if (vc == vi && vc > 0 && tom_irq_enabled(IRQ_VBLANK))  // Time for Vertical Interrupt?
1348         {
1349                 // We don't have to worry about autovectors & whatnot because the Jaguar
1350                 // tells you through its HW registers who sent the interrupt...
1351                 tom_set_pending_video_int();
1352                 m68k_set_irq(7);
1353         }
1354
1355         TOMExecScanline(vc, true);
1356
1357 //Change this to VBB???
1358 //Doesn't seem to matter (at least for Flip Out & I-War)
1359         if (vc == 0)
1360 //      if (vc == vbb)
1361         {
1362 joystick_exec();
1363
1364                 RenderBackbuffer();
1365                 TOMResetBackbuffer(backbuffer);
1366         }//*/
1367
1368 //      if (vc == 0)
1369 //              TOMResetBackbuffer(backbuffer);
1370
1371 //      SetCallbackTime(ScanlineCallback, 63.5555);
1372         SetCallbackTime(ScanlineCallback, 31.77775);
1373 }
1374
1375 #else
1376
1377 bool frameDone;
1378 void JaguarExecuteNew(void)
1379 {
1380 //      extern bool finished, showGUI;
1381 //      extern bool debounceRunKey;
1382         // Pass a message to the "joystick" code to debounce the ESC key...
1383 //      debounceRunKey = true;
1384 //      finished = false;
1385 /*      InitializeEventList();
1386         TOMResetBackbuffer(backbuffer);
1387 //      SetCallbackTime(ScanlineCallback, 63.5555);
1388         SetCallbackTime(ScanlineCallback, 31.77775);
1389 //      SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
1390 //      SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time//*/
1391 //      uint8 * keystate = SDL_GetKeyState(NULL);
1392         frameDone = false;
1393
1394         do
1395         {
1396                 double timeToNextEvent = GetTimeToNextEvent();
1397 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1398
1399                 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1400                 GPUExec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1401
1402                 if (vjs.DSPEnabled)
1403                 {
1404                         if (vjs.usePipelinedDSP)
1405                                 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent));        // Pipelined DSP execution (3 stage)...
1406                         else
1407                                 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent));          // Ordinary non-pipelined DSP
1408                 }
1409
1410                 HandleNextEvent();
1411
1412 //              if (keystate[SDLK_ESCAPE])
1413 //                      break;
1414
1415 //          SDL_PumpEvents();   // Needed to keep the keystate current...
1416         }
1417         while (!frameDone);
1418 }
1419
1420 void ScanlineCallback(void)
1421 {
1422         uint16 vc = TOMReadWord(0xF00006);
1423         uint16 vp = TOMReadWord(0xF0003E) + 1;
1424         uint16 vi = TOMReadWord(0xF0004E);
1425 //      uint16 vbb = TOMReadWord(0xF00040);
1426         vc++;
1427
1428         if (vc >= vp)
1429                 vc = 0;
1430
1431 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1432         TOMWriteWord(0xF00006, vc);
1433
1434 //This is a crappy kludge, but maybe it'll work for now...
1435 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1436         if (vc == vi && vc > 0 && TOMIRQEnabled(IRQ_VBLANK))    // Time for Vertical Interrupt?
1437         {
1438                 // We don't have to worry about autovectors & whatnot because the Jaguar
1439                 // tells you through its HW registers who sent the interrupt...
1440                 TOMSetPendingVideoInt();
1441                 m68k_set_irq(7);
1442         }
1443
1444         TOMExecScanline(vc, true);
1445
1446 //Change this to VBB???
1447 //Doesn't seem to matter (at least for Flip Out & I-War)
1448         if (vc == 0)
1449 //      if (vc == vbb)
1450         {
1451                 JoystickExec();
1452                 RenderBackbuffer();
1453                 TOMResetBackbuffer(backbuffer);
1454                 frameDone = true;
1455         }//*/
1456
1457 //      if (vc == 0)
1458 //              TOMResetBackbuffer(backbuffer);
1459
1460 //      SetCallbackTime(ScanlineCallback, 63.5555);
1461         SetCallbackTime(ScanlineCallback, 31.77775);
1462 }
1463
1464 #endif
1465
1466 // This isn't currently used, but maybe it should be...
1467 void RenderCallback(void)
1468 {
1469         RenderBackbuffer();
1470         TOMResetBackbuffer(backbuffer);
1471 //      SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
1472         SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time
1473 }