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