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