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. :-)
14 //#include "m68kdasmAG.h"
17 #include "SDL_opengl.h"
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
27 // Private function prototypes
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);
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;
44 // Memory debugging identifiers
47 { "Unknown", "Jaguar", "DSP", "GPU", "TOM", "JERRY", "M68K", "Blitter", "OP" };
49 uint32 jaguar_active_memory_dumps = 0;
51 uint32 jaguar_mainRom_crc32, jaguarRomSize, jaguarRunAddress;
53 /*static*/ uint8 * jaguar_mainRam = NULL;
54 /*static*/ uint8 * jaguar_mainRom = NULL;
55 /*static*/ uint8 * jaguar_bootRom = NULL;
56 /*static*/ uint8 * jaguar_CDBootROM = NULL;
58 #ifdef CPU_DEBUG_MEMORY
59 uint8 writeMemMax[0x400000], writeMemMin[0x400000];
60 uint8 readMem[0x400000];
61 uint32 returnAddr[4000], raPtr = 0xFFFFFFFF;
64 uint32 pcQueue[0x400];
68 // Callback function to detect illegal instructions
70 void GPUDumpDisassembly(void);
71 void GPUDumpRegisters(void);
72 static bool start = false;
73 void M68KInstructionHook(void)
75 uint32 m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
78 // Ideally, we'd save all the registers as well...
79 pcQueue[pcQPtr++] = m68kPC;
82 if (m68kPC & 0x01) // Oops! We're fetching an odd address!
84 WriteLog("M68K: Attempted to execute from an odd adress!\n\nBacktrace:\n\n");
86 static char buffer[2048];
87 for(int i=0; i<0x400; i++)
89 m68k_disassemble(buffer, pcQueue[(pcQPtr + i) & 0x3FF], M68K_CPU_TYPE_68000);
90 WriteLog("\t%08X: %s\n", pcQueue[(pcQPtr + i) & 0x3FF], buffer);
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");
104 /* if (m68kPC >= 0x807EC4 && m68kPC <= 0x807EDB)
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));
113 /* if (m68kPC == 0x8D0E48 && effect_start5)
115 WriteLog("\nM68K: At collision detection code. Exiting!\n\n");
117 GPUDumpDisassembly();
121 /* uint16 opcode = JaguarReadWord(m68kPC);
122 if (opcode == 0x4E75) // RTS
125 // WriteLog("Jaguar: Returning from subroutine to %08X\n", JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7)));
127 uint32 addr = JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7));
129 if (raPtr != 0xFFFFFFFF)
131 for(uint32 i=0; i<=raPtr; i++)
133 if (returnAddr[i] == addr)
142 returnAddr[++raPtr] = addr;
146 //Flip Out! debugging...
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
154 0080603A: move.l #$11ed7c, $100.w D0=61700080, A0=000C96F8, D1=00000000, A1=000040D8
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
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;
171 // GPUDumpRegisters();
172 // GPUDumpDisassembly();
174 // M68K_show_context();
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));
184 /* if (m68kPC == 0x803F16)
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));
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));
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)
201 // WriteLog("M68K: About to stuff $12345678 into $F1B000 (=%08X)...\n", DSPReadLong(0xF1B000, M68K));
202 // DSPWriteLong(0xF1B000, 0x12345678, M68K);
205 if (m68kPC == 0x5000)
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));
217 if (m68kPC == 0x82E1A)
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));
226 if (m68kPC == 0x82E58)
227 WriteLog("--> [Routine end]\n");
228 if (m68kPC == 0x80004)
230 WriteLog("--> [Calling BusWrite2] D2: %08X\n", m68k_get_reg(NULL, M68K_REG_D2));
231 // m68k_set_reg(M68K_REG_D2, 0x12345678);
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");
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));
299 #ifdef ABORT_ON_ILLEGAL_INSTRUCTIONS
300 if (!m68k_is_valid_instruction(m68k_read_memory_16(m68kPC), M68K_CPU_TYPE_68000))
302 #ifndef ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
303 if (m68k_read_memory_16(m68kPC) == 0x4AFC)
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).
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);
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");
326 // WriteLog("\n\n68K disasm\n\n");
327 // jaguar_dasm(0x802000, 0x50C);
338 // Musashi 68000 read/write/IRQ functions
341 int irq_ack_handler(int level)
343 int vector = M68K_INT_ACK_AUTOVECTOR;
345 // The GPU/DSP/etc are probably *not* issuing an NMI, but it seems to work OK...
349 m68k_set_irq(0); // Clear the IRQ...
350 vector = 64; // Set user interrupt #0
356 unsigned int m68k_read_memory_8(unsigned int address)
358 #ifdef CPU_DEBUG_MEMORY
359 if ((address >= 0x000000) && (address <= 0x3FFFFF))
362 readMem[address] = 1;
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;
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);
386 retVal = jaguar_unknown_readbyte(address, M68K);
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));
395 void gpu_dump_disassembly(void);
396 void gpu_dump_registers(void);
398 unsigned int m68k_read_memory_16(unsigned int address)
400 #ifdef CPU_DEBUG_MEMORY
401 /* if ((address >= 0x000000) && (address <= 0x3FFFFE))
404 readMem[address] = 1, readMem[address + 1] = 1;
406 /* if (effect_start && (address >= 0x8064FC && address <= 0x806501))
408 return 0x4E71; // NOP
410 if (effect_start2 && (address >= 0x806502 && address <= 0x806507))
412 return 0x4E71; // NOP
414 if (effect_start3 && (address >= 0x806512 && address <= 0x806517))
416 return 0x4E71; // NOP
418 if (effect_start4 && (address >= 0x806524 && address <= 0x806527))
420 return 0x4E71; // NOP
422 if (effect_start5 && (address >= 0x80653E && address <= 0x806543)) //Collision detection!
424 return 0x4E71; // NOP
426 if (effect_start6 && (address >= 0x806544 && address <= 0x806547))
428 return 0x4E71; // NOP
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
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");
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)
448 if (address == 0x000066A0)
450 gpu_dump_registers();
451 gpu_dump_disassembly();
453 for(int i=0; i<10000; i++)
454 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
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;
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);
477 retVal = jaguar_unknown_readword(address, M68K);
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));
490 unsigned int m68k_read_memory_32(unsigned int address)
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));//*/
496 //WriteLog("--> [RM32]\n");
497 return (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2);
500 void m68k_write_memory_8(unsigned int address, unsigned int value)
502 #ifdef CPU_DEBUG_MEMORY
503 if ((address >= 0x000000) && (address <= 0x3FFFFF))
507 if (value > writeMemMax[address])
508 writeMemMax[address] = value;
509 if (value < writeMemMin[address])
510 writeMemMin[address] = value;
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);
518 if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
519 WriteLog("M68K: Byte %02X written at %08X by 68K\n", value, address);//*/
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);
530 jaguar_unknown_writebyte(address, value, M68K);
533 void m68k_write_memory_16(unsigned int address, unsigned int value)
535 #ifdef CPU_DEBUG_MEMORY
536 if ((address >= 0x000000) && (address <= 0x3FFFFE))
540 uint8 hi = value >> 8, lo = value & 0xFF;
542 if (hi > writeMemMax[address])
543 writeMemMax[address] = hi;
544 if (hi < writeMemMin[address])
545 writeMemMin[address] = hi;
547 if (lo > writeMemMax[address+1])
548 writeMemMax[address+1] = lo;
549 if (lo < writeMemMin[address+1])
550 writeMemMin[address+1] = lo;
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);
569 /*if (address == 0x0100)//64*4)
570 WriteLog("M68K: Wrote word to VI vector value %04X...\n", value);//*/
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);//*/
578 if ((address >= 0x000000) && (address <= 0x3FFFFE))
580 /* jaguar_mainRam[address] = value >> 8;
581 jaguar_mainRam[address + 1] = value & 0xFF;*/
582 SET16(jaguar_mainRam, address, value);
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);
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));
601 void m68k_write_memory_32(unsigned int address, unsigned int value)
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)
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);//*/
616 m68k_write_memory_16(address, value >> 16);
617 m68k_write_memory_16(address + 2, value & 0xFFFF);
621 uint32 jaguar_get_handler(uint32 i)
623 return JaguarReadLong(i * 4);
626 uint32 jaguar_interrupt_handler_is_valid(uint32 i) // Debug use only...
628 uint32 handler = jaguar_get_handler(i);
629 if (handler && (handler != 0xFFFFFFFF))
635 void M68K_show_context(void)
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));
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));
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);
649 // WriteLog("\n.......[Cart start]...........\n\n");
650 // jaguar_dasm(0x192000, 0x1000);//0x200);
652 WriteLog("..................\n");
654 if (tom_irq_enabled(IRQ_VBLANK))
656 WriteLog("vblank int: enabled\n");
657 jaguar_dasm(jaguar_get_handler(64), 0x200);
660 WriteLog("vblank int: disabled\n");
662 WriteLog("..................\n");
664 for(int i=0; i<256; i++)
665 WriteLog("handler %03i at $%08X\n", i, (unsigned int)jaguar_get_handler(i));
669 // Unknown read/write byte/word routines
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:
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
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...)
688 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
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));
693 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
694 extern bool finished;
696 extern bool doDSPDis;
702 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
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));
707 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
708 extern bool finished;
710 extern bool doDSPDis;
716 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who/*=UNKNOWN*/)
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));
721 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
722 extern bool finished;
724 extern bool doDSPDis;
731 unsigned jaguar_unknown_readword(unsigned address, uint32 who/*=UNKNOWN*/)
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));
736 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
737 extern bool finished;
739 extern bool doDSPDis;
747 // Disassemble M68K instructions at the given offset
750 unsigned int m68k_read_disassembler_8(unsigned int address)
752 return m68k_read_memory_8(address);
755 unsigned int m68k_read_disassembler_16(unsigned int address)
757 return m68k_read_memory_16(address);
760 unsigned int m68k_read_disassembler_32(unsigned int address)
762 return m68k_read_memory_32(address);
765 void jaguar_dasm(uint32 offset, uint32 qt)
768 static char buffer[2048];//, mem[64];
769 int pc = offset, oldpc;
771 for(uint32 i=0; i<qt; i++)
774 for(int j=0; j<64; j++)
775 mem[j^0x01] = jaguar_byte_read(pc + j);
777 pc += Dasm68000((char *)mem, buffer, 0);
778 WriteLog("%08X: %s\n", oldpc, buffer);//*/
780 pc += m68k_disassemble(buffer, pc, M68K_CPU_TYPE_68000);
781 WriteLog("%08X: %s\n", oldpc, buffer);//*/
786 uint8 JaguarReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
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);
804 data = jaguar_unknown_readbyte(offset, who);
809 uint16 JaguarReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
812 if (offset <= 0x3FFFFE)
814 return (jaguar_mainRam[(offset+0) & 0x3FFFFF] << 8) | jaguar_mainRam[(offset+1) & 0x3FFFFF];
816 else if ((offset >= 0x800000) && (offset <= 0xBFFFFE))
819 return (jaguar_mainRom[offset+0] << 8) | jaguar_mainRom[offset+1];
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);
831 return jaguar_unknown_readword(offset, who);
834 void JaguarWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
836 //Need to check for writes in the range of $18FA70 + 8000...
838 if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
839 WriteLog("JWB: Byte %02X written at %08X by %s\n", data, offset, whoName[who]);//*/
842 if (offset < 0x400000)
844 jaguar_mainRam[offset & 0x3FFFFF] = data;
847 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
849 CDROMWriteByte(offset, data, who);
852 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFF))
854 TOMWriteByte(offset, data, who);
857 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFF))
859 JERRYWriteByte(offset, data, who);
863 jaguar_unknown_writebyte(offset, data, who);
867 void JaguarWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
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...
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]);//*/
884 if (offset <= 0x3FFFFE)
889 1A 69 F0 ($0000) -> Starfield
890 1A 73 C8 ($0001) -> Final clearing blit & bitmap blit?
893 1A 8F E8 ($0004) -> "Jaguar" small color logo?
902 //This MUST be done by the 68K!
903 /*if (offset == 0x670C)
904 WriteLog("Jaguar: %s writing to location $670C...\n", whoName[who]);*/
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)
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)
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)
921 /*extern bool doGPUDis;
922 if (offset == 0x120216 && who == GPU)
924 /*extern uint32 gpu_pc;
925 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
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"));
933 JWW: Writing starfield star 775E at 0011F650 (555984/1447)
935 //if (offset == (0x001E17F8 + 0x34))
936 /*if (who == GPU && offset == (0x001E17F8 + 0x34))
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))
942 extern int objectPtr;
943 // if (offset > 0x148000)
946 if (starCount > objectPtr)
949 // if (starCount == 1)
950 // WriteLog("--> Drawing 1st star...\n");
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"));
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...
962 //extern bool doGPUDis;
963 /*if (offset == 0x11D022 + 0x48000 || offset == 0x11D022)// && who == GPU)
966 WriteLog("JWW: %s writing echo %04X at %08X...\n", whoName[who], data, offset);
969 if (offset == 0x11D31A + 0x48000 || offset == 0x11D31A)
970 WriteLog("JWW: %s writing star %04X at %08X...\n", whoName[who], data, offset);//*/
972 jaguar_mainRam[(offset+0) & 0x3FFFFF] = data >> 8;
973 jaguar_mainRam[(offset+1) & 0x3FFFFF] = data & 0xFF;
976 else if (offset >= 0xDFFF00 && offset <= 0xDFFFFE)
978 CDROMWriteWord(offset, data, who);
981 else if (offset >= 0xF00000 && offset <= 0xF0FFFE)
983 TOMWriteWord(offset, data, who);
986 else if (offset >= 0xF10000 && offset <= 0xF1FFFE)
988 JERRYWriteWord(offset, data, who);
991 // Don't bomb on attempts to write to ROM
992 else if (offset >= 0x800000 && offset <= 0xEFFFFF)
995 jaguar_unknown_writeword(offset, data, who);
998 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
999 uint32 JaguarReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
1001 return (JaguarReadWord(offset, who) << 16) | JaguarReadWord(offset+2, who);
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*/)
1007 /* extern bool doDSPDis;
1008 if (offset < 0x400 && !doDSPDis)
1010 WriteLog("JLW: Write to %08X by %s... Starting DSP log!\n\n", offset, whoName[who]);
1013 /*if (offset == 0x0100)//64*4)
1014 WriteLog("M68K: %s wrote dword to VI vector value %08X...\n", whoName[who], data);//*/
1016 JaguarWriteWord(offset, data >> 16, who);
1017 JaguarWriteWord(offset+2, data & 0xFFFF, who);
1021 // Jaguar console initialization
1023 void jaguar_init(void)
1025 #ifdef CPU_DEBUG_MEMORY
1026 memset(readMem, 0x00, 0x400000);
1027 memset(writeMemMin, 0xFF, 0x400000);
1028 memset(writeMemMax, 0x00, 0x400000);
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...
1043 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
1051 //New timer based code stuffola...
1052 void ScanlineCallback(void);
1053 void RenderCallback(void);
1054 extern uint32 * backbuffer;
1055 void jaguar_reset(void)
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);
1061 SET32(jaguar_mainRam, 4, jaguarRunAddress);
1063 // WriteLog("jaguar_reset():\n");
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));
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
1081 void jaguar_done(void)
1083 #ifdef CPU_DEBUG_MEMORY
1084 /* WriteLog("\nJaguar: Memory Usage Stats (return addresses)\n\n");
1086 for(uint32 i=0; i<=raPtr; i++)
1088 WriteLog("\t%08X\n", returnAddr[i]);
1089 WriteLog("M68000 disassembly at $%08X...\n", returnAddr[i] - 16);
1090 jaguar_dasm(returnAddr[i] - 16, 16);
1095 /* int start = 0, end = 0;
1096 bool endTriggered = false, startTriggered = false;
1097 for(int i=0; i<0x400000; i++)
1099 if (readMem[i] && writeMemMin[i] != 0xFF && writeMemMax != 0x00)
1101 if (!startTriggered)
1102 startTriggered = true, endTriggered = false, start = i;
1104 WriteLog("\t\tMin/Max @ %06X: %u/%u\n", i, writeMemMin[i], writeMemMax[i]);
1110 end = i - 1, endTriggered = true, startTriggered = false;
1111 WriteLog("\tMemory range accessed: %06X - %06X\n", start, end);
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)));
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);//*/
1132 /* WriteLog("\n\nM68000 disassembly at $803F00 (look @ $803F2A)...\n");
1133 jaguar_dasm(0x803F00, 500);
1136 /* WriteLog("\n\nM68000 disassembly at $802B00 (look @ $802B5E)...\n");
1137 jaguar_dasm(0x802B00, 500);
1140 /* WriteLog("\n\nM68000 disassembly at $809900 (look @ $8099F8)...\n");
1141 jaguar_dasm(0x809900, 500);
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);
1150 /* WriteLog("\n\nM68000 disassembly at $1AC000...\n");
1151 jaguar_dasm(0x1AC000, 6000);
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();
1166 memory_free(jaguar_mainRom);
1167 memory_free(jaguar_mainRam);
1168 memory_free(jaguar_bootRom);
1169 memory_free(jaguar_CDBootROM);
1173 // Main Jaguar execution loop (1 frame)
1175 void JaguarExecute(uint32 * backbuffer, bool render)
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 !!!
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);
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);
1200 TOMResetBackbuffer(backbuffer);
1201 /*extern int effect_start;
1203 WriteLog("JagExe: VP=%u, VI=%u, CPU CPS=%u, GPU CPS=%u\n", vp, vi, M68KCyclesPerScanline, RISCCyclesPerScanline);//*/
1205 //extern int start_logging;
1206 for(uint16 i=0; i<vp; i++)
1208 // Increment the horizontal count (why? RNG? Besides which, this is *NOT* cycle accurate!)
1209 TOMWriteWord(0xF00004, (TOMReadWord(0xF00004) + 1) & 0x7FF);
1211 TOMWriteWord(0xF00006, i); // Write the VC
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?
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();
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);
1244 if (vjs.usePipelinedDSP)
1245 DSPExecP2(RISCCyclesPerScanline); // Pipelined DSP execution (3 stage)...
1247 DSPExec(RISCCyclesPerScanline); // Ordinary non-pipelined DSP
1248 // DSPExecComp(RISCCyclesPerScanline); // Comparison core
1251 //if (start_logging)
1252 // WriteLog("About to execute OP (%u)...\n", i);
1253 TOMExecScanline(i, render);
1257 // Temp debugging stuff
1259 void DumpMainMemory(void)
1261 FILE * fp = fopen("./memdump.bin", "wb");
1266 fwrite(jaguar_mainRam, 1, 0x400000, fp);
1270 uint8 * GetRamPtr(void)
1272 return jaguar_mainRam;
1276 // New Jaguar execution stack
1281 void JaguarExecuteNew(void)
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;
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);
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));
1301 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1302 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1306 if (vjs.usePipelinedDSP)
1307 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1309 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1314 // if (keystate[SDLK_ESCAPE])
1317 // SDL_PumpEvents(); // Needed to keep the keystate current...
1322 void ScanlineCallback(void)
1324 uint16 vc = TOMReadWord(0xF00006);
1325 uint16 vp = TOMReadWord(0xF0003E) + 1;
1326 uint16 vi = TOMReadWord(0xF0004E);
1327 // uint16 vbb = TOMReadWord(0xF00040);
1333 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1334 TOMWriteWord(0xF00006, vc);
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?
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();
1346 TOMExecScanline(vc, true);
1348 //Change this to VBB???
1349 //Doesn't seem to matter (at least for Flip Out & I-War)
1356 TOMResetBackbuffer(backbuffer);
1360 // TOMResetBackbuffer(backbuffer);
1362 // SetCallbackTime(ScanlineCallback, 63.5555);
1363 SetCallbackTime(ScanlineCallback, 31.77775);
1369 void JaguarExecuteNew(void)
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);
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));
1390 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1391 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1395 if (vjs.usePipelinedDSP)
1396 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1398 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1403 // if (keystate[SDLK_ESCAPE])
1406 // SDL_PumpEvents(); // Needed to keep the keystate current...
1411 void ScanlineCallback(void)
1413 uint16 vc = TOMReadWord(0xF00006);
1414 uint16 vp = TOMReadWord(0xF0003E) + 1;
1415 uint16 vi = TOMReadWord(0xF0004E);
1416 // uint16 vbb = TOMReadWord(0xF00040);
1422 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1423 TOMWriteWord(0xF00006, vc);
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?
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();
1435 TOMExecScanline(vc, true);
1437 //Change this to VBB???
1438 //Doesn't seem to matter (at least for Flip Out & I-War)
1444 TOMResetBackbuffer(backbuffer);
1449 // TOMResetBackbuffer(backbuffer);
1451 // SetCallbackTime(ScanlineCallback, 63.5555);
1452 SetCallbackTime(ScanlineCallback, 31.77775);
1457 // This isn't currently used, but maybe it should be...
1458 void RenderCallback(void)
1461 TOMResetBackbuffer(backbuffer);
1462 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1463 SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time