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