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 "SDL_opengl.h"
30 //Do this in makefile??? Yes! Could, but it's easier to define here...
31 //#define LOG_UNMAPPED_MEMORY_ACCESSES
32 //#define ABORT_ON_UNMAPPED_MEMORY_ACCESS
33 #define ABORT_ON_ILLEGAL_INSTRUCTIONS
34 //#define ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
35 #define CPU_DEBUG_MEMORY
37 // Private function prototypes
39 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who = UNKNOWN);
40 unsigned jaguar_unknown_readword(unsigned address, uint32 who = UNKNOWN);
41 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who = UNKNOWN);
42 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who = UNKNOWN);
43 void M68K_show_context(void);
47 //extern bool hardwareTypeNTSC; // Set to false for PAL
48 #ifdef CPU_DEBUG_MEMORY
49 extern bool startMemLog; // Set by "e" key
50 extern int effect_start;
51 extern int effect_start2, effect_start3, effect_start4, effect_start5, effect_start6;
54 // Memory debugging identifiers
56 const char * whoName[9] =
57 { "Unknown", "Jaguar", "DSP", "GPU", "TOM", "JERRY", "M68K", "Blitter", "OP" };
59 uint32 jaguar_active_memory_dumps = 0;
61 uint32 jaguarMainRomCRC32, jaguarRomSize, jaguarRunAddress;
63 uint8 * jaguarMainRam = NULL;
64 uint8 * jaguarMainRom = NULL;
65 uint8 * jaguarBootRom = NULL;
66 uint8 * jaguarCDBootROM = NULL;
67 bool BIOSLoaded = false;
68 bool CDBIOSLoaded = false;
70 #ifdef CPU_DEBUG_MEMORY
71 uint8 writeMemMax[0x400000], writeMemMin[0x400000];
72 uint8 readMem[0x400000];
73 uint32 returnAddr[4000], raPtr = 0xFFFFFFFF;
76 uint32 pcQueue[0x400];
80 // Callback function to detect illegal instructions
82 void GPUDumpDisassembly(void);
83 void GPUDumpRegisters(void);
84 static bool start = false;
85 void M68KInstructionHook(void)
87 uint32 m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
90 // Ideally, we'd save all the registers as well...
91 pcQueue[pcQPtr++] = m68kPC;
94 if (m68kPC & 0x01) // Oops! We're fetching an odd address!
96 WriteLog("M68K: Attempted to execute from an odd adress!\n\nBacktrace:\n\n");
98 static char buffer[2048];
99 for(int i=0; i<0x400; i++)
101 m68k_disassemble(buffer, pcQueue[(pcQPtr + i) & 0x3FF], M68K_CPU_TYPE_68000);
102 WriteLog("\t%08X: %s\n", pcQueue[(pcQPtr + i) & 0x3FF], buffer);
106 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
107 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
108 for(int i=0; i<10; i++)
109 WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
110 WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
116 /* if (m68kPC >= 0x807EC4 && m68kPC <= 0x807EDB)
118 static char buffer[2048];
119 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
120 WriteLog("%08X: %s", m68kPC, buffer);
121 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
122 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
123 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
125 /* if (m68kPC == 0x8D0E48 && effect_start5)
127 WriteLog("\nM68K: At collision detection code. Exiting!\n\n");
129 GPUDumpDisassembly();
133 /* uint16 opcode = JaguarReadWord(m68kPC);
134 if (opcode == 0x4E75) // RTS
137 // WriteLog("Jaguar: Returning from subroutine to %08X\n", JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7)));
139 uint32 addr = JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7));
141 if (raPtr != 0xFFFFFFFF)
143 for(uint32 i=0; i<=raPtr; i++)
145 if (returnAddr[i] == addr)
154 returnAddr[++raPtr] = addr;
158 //Flip Out! debugging...
161 00805FDC: movea.l #$9c6f8, A0 D0=00100010, A0=00100000
162 00805FE2: move.w #$10, (A0)+ D0=00100010, A0=0009C6F8
163 00805FE6: cmpa.l #$c96f8, A0 D0=00100010, A0=0009C6FA
164 00805FEC: bne 805fe2 D0=00100010, A0=0009C6FA
166 0080603A: move.l #$11ed7c, $100.w D0=61700080, A0=000C96F8, D1=00000000, A1=000040D8
168 0012314C: move.l (A0)+, (A1)+ D0=61700080, A0=00124174, D1=00000000, A1=00F03FFC
169 0012314E: cmpa.l #$f04000, A1 D0=61700080, A0=00124178, D1=00000000, A1=00F04000
170 00123154: blt 12314c D0=61700080, A0=00124178, D1=00000000, A1=00F04000
171 00123156: move.l #$0, $f035d0.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
172 00123160: move.l #$f03000, $f02110.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
173 0012316A: move.l #$1, $f02114.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
174 00123174: rts D0=61700080, A0=00124178, D1=00000000, A1=00F04000
176 /* static char buffer[2048];
177 //if (m68kPC > 0x805F48) start = true;
178 //if (m68kPC > 0x806486) start = true;
179 //if (m68kPC == 0x805FEE) start = true;
180 //if (m68kPC == 0x80600C)// start = true;
181 if (m68kPC == 0x802058) start = true;
183 // GPUDumpRegisters();
184 // GPUDumpDisassembly();
186 // M68K_show_context();
192 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
193 WriteLog("%08X: %s \t\tD0=%08X, A0=%08X, D1=%08X, A1=%08X\n", m68kPC, buffer, m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_A1));
196 /* if (m68kPC == 0x803F16)
198 WriteLog("M68K: Registers found at $803F16:\n");
199 WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
200 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
201 WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
203 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
204 WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
206 //Looks like the DSP is supposed to return $12345678 when it finishes its validation routine...
207 // !!! Investigate !!!
208 /*extern bool doDSPDis;
209 static bool disgo = false;
210 if (m68kPC == 0x50222)
213 // WriteLog("M68K: About to stuff $12345678 into $F1B000 (=%08X)...\n", DSPReadLong(0xF1B000, M68K));
214 // DSPWriteLong(0xF1B000, 0x12345678, M68K);
217 if (m68kPC == 0x5000)
222 static char buffer[2048];
223 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
224 WriteLog("%08X: %s", m68kPC, buffer);
225 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
226 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
227 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
229 if (m68kPC == 0x82E1A)
231 static char buffer[2048];
232 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
233 WriteLog("--> [Routine start] %08X: %s", m68kPC, buffer);
234 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X(cmd), D1=%08X(# bytes), D2=%08X\n",
235 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
236 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
238 if (m68kPC == 0x82E58)
239 WriteLog("--> [Routine end]\n");
240 if (m68kPC == 0x80004)
242 WriteLog("--> [Calling BusWrite2] D2: %08X\n", m68k_get_reg(NULL, M68K_REG_D2));
243 // m68k_set_reg(M68K_REG_D2, 0x12345678);
267 if (m68kPC == 0x3000)
268 WriteLog("M68K: CD_init\n");
269 else if (m68kPC == 0x3006 + (6 * 0))
270 WriteLog("M68K: CD_mode\n");
271 else if (m68kPC == 0x3006 + (6 * 1))
272 WriteLog("M68K: CD_ack\n");
273 else if (m68kPC == 0x3006 + (6 * 2))
274 WriteLog("M68K: CD_jeri\n");
275 else if (m68kPC == 0x3006 + (6 * 3))
276 WriteLog("M68K: CD_spin\n");
277 else if (m68kPC == 0x3006 + (6 * 4))
278 WriteLog("M68K: CD_stop\n");
279 else if (m68kPC == 0x3006 + (6 * 5))
280 WriteLog("M68K: CD_mute\n");
281 else if (m68kPC == 0x3006 + (6 * 6))
282 WriteLog("M68K: CD_umute\n");
283 else if (m68kPC == 0x3006 + (6 * 7))
284 WriteLog("M68K: CD_paus\n");
285 else if (m68kPC == 0x3006 + (6 * 8))
286 WriteLog("M68K: CD_upaus\n");
287 else if (m68kPC == 0x3006 + (6 * 9))
288 WriteLog("M68K: CD_read\n");
289 else if (m68kPC == 0x3006 + (6 * 10))
290 WriteLog("M68K: CD_uread\n");
291 else if (m68kPC == 0x3006 + (6 * 11))
292 WriteLog("M68K: CD_setup\n");
293 else if (m68kPC == 0x3006 + (6 * 12))
294 WriteLog("M68K: CD_ptr\n");
295 else if (m68kPC == 0x3006 + (6 * 13))
296 WriteLog("M68K: CD_osamp\n");
297 else if (m68kPC == 0x3006 + (6 * 14))
298 WriteLog("M68K: CD_getoc\n");
299 else if (m68kPC == 0x3006 + (6 * 15))
300 WriteLog("M68K: CD_initm\n");
301 else if (m68kPC == 0x3006 + (6 * 16))
302 WriteLog("M68K: CD_initf\n");
303 else if (m68kPC == 0x3006 + (6 * 17))
304 WriteLog("M68K: CD_switch\n");
306 if (m68kPC >= 0x3000 && m68kPC <= 0x306C)
307 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
308 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
309 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
311 #ifdef ABORT_ON_ILLEGAL_INSTRUCTIONS
312 if (!m68k_is_valid_instruction(m68k_read_memory_16(m68kPC), M68K_CPU_TYPE_68000))
314 #ifndef ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
315 if (m68k_read_memory_16(m68kPC) == 0x4AFC)
317 // This is a kludge to let homebrew programs work properly (i.e., let the other processors
318 // keep going even when the 68K dumped back to the debugger or what have you).
320 // m68k_set_reg(M68K_REG_PC, m68kPC - 2);
321 // Try setting the vector to the illegal instruction...
322 //This doesn't work right either! Do something else! Quick!
323 // SET32(jaguar_mainRam, 0x10, m68kPC);
329 WriteLog("\nM68K encountered an illegal instruction at %08X!!!\n\nAborting!\n", m68kPC);
330 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
331 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
332 for(int i=0; i<10; i++)
333 WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
334 WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
338 // WriteLog("\n\n68K disasm\n\n");
339 // jaguar_dasm(0x802000, 0x50C);
350 // Musashi 68000 read/write/IRQ functions
353 int irq_ack_handler(int level)
355 int vector = M68K_INT_ACK_AUTOVECTOR;
357 // The GPU/DSP/etc are probably *not* issuing an NMI, but it seems to work OK...
361 m68k_set_irq(0); // Clear the IRQ...
362 vector = 64; // Set user interrupt #0
368 unsigned int m68k_read_memory_8(unsigned int address)
370 #ifdef CPU_DEBUG_MEMORY
371 if ((address >= 0x000000) && (address <= 0x3FFFFF))
374 readMem[address] = 1;
377 //WriteLog("[RM8] Addr: %08X\n", address);
378 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
379 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
380 || address == 0x1AF05E)
381 WriteLog("[RM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, jaguar_mainRam[address]);//*/
382 unsigned int retVal = 0;
384 if ((address >= 0x000000) && (address <= 0x3FFFFF))
385 retVal = jaguarMainRam[address];
386 // else if ((address >= 0x800000) && (address <= 0xDFFFFF))
387 else if ((address >= 0x800000) && (address <= 0xDFFEFF))
388 retVal = jaguarMainRom[address - 0x800000];
389 else if ((address >= 0xE00000) && (address <= 0xE3FFFF))
390 retVal = jaguarBootRom[address - 0xE00000];
391 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
392 retVal = CDROMReadByte(address);
393 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
394 retVal = TOMReadByte(address, M68K);
395 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
396 retVal = JERRYReadByte(address, M68K);
398 retVal = jaguar_unknown_readbyte(address, M68K);
400 //if (address >= 0x2800 && address <= 0x281F)
401 // WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
402 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
403 // WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
407 void gpu_dump_disassembly(void);
408 void gpu_dump_registers(void);
410 unsigned int m68k_read_memory_16(unsigned int address)
412 #ifdef CPU_DEBUG_MEMORY
413 /* if ((address >= 0x000000) && (address <= 0x3FFFFE))
416 readMem[address] = 1, readMem[address + 1] = 1;
418 /* if (effect_start && (address >= 0x8064FC && address <= 0x806501))
420 return 0x4E71; // NOP
422 if (effect_start2 && (address >= 0x806502 && address <= 0x806507))
424 return 0x4E71; // NOP
426 if (effect_start3 && (address >= 0x806512 && address <= 0x806517))
428 return 0x4E71; // NOP
430 if (effect_start4 && (address >= 0x806524 && address <= 0x806527))
432 return 0x4E71; // NOP
434 if (effect_start5 && (address >= 0x80653E && address <= 0x806543)) //Collision detection!
436 return 0x4E71; // NOP
438 if (effect_start6 && (address >= 0x806544 && address <= 0x806547))
440 return 0x4E71; // NOP
443 //WriteLog("[RM16] Addr: %08X\n", address);
444 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005FBA)
445 // for(int i=0; i<10000; i++)
446 WriteLog("[M68K] In routine #6!\n");//*/
447 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00006696) // GPU Program #4
448 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005B3C) // GPU Program #2
449 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005BA8) // GPU Program #3
451 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
452 gpu_dump_registers();
453 gpu_dump_disassembly();
454 // for(int i=0; i<10000; i++)
455 // WriteLog("[M68K] About to run GPU!\n");
457 //WriteLog("[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
458 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x00006696 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x000066A8)
460 if (address == 0x000066A0)
462 gpu_dump_registers();
463 gpu_dump_disassembly();
465 for(int i=0; i<10000; i++)
466 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
468 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
469 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
470 || address == 0x1AF05E)
471 WriteLog("[RM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, GET16(jaguar_mainRam, address));//*/
472 unsigned int retVal = 0;
474 if ((address >= 0x000000) && (address <= 0x3FFFFE))
475 // retVal = (jaguar_mainRam[address] << 8) | jaguar_mainRam[address+1];
476 retVal = GET16(jaguarMainRam, address);
477 // else if ((address >= 0x800000) && (address <= 0xDFFFFE))
478 else if ((address >= 0x800000) && (address <= 0xDFFEFE))
479 retVal = (jaguarMainRom[address - 0x800000] << 8) | jaguarMainRom[address - 0x800000 + 1];
480 else if ((address >= 0xE00000) && (address <= 0xE3FFFE))
481 retVal = (jaguarBootRom[address - 0xE00000] << 8) | jaguarBootRom[address - 0xE00000 + 1];
482 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
483 retVal = CDROMReadWord(address, M68K);
484 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
485 retVal = TOMReadWord(address, M68K);
486 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
487 retVal = JERRYReadWord(address, M68K);
489 retVal = jaguar_unknown_readword(address, M68K);
491 //if (address >= 0xF1B000 && address <= 0xF1CFFF)
492 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
493 //if (address >= 0x2800 && address <= 0x281F)
494 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
495 //$8B3AE -> Transferred from $F1C010
496 //$8B5E4 -> Only +1 read at $808AA
497 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
498 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
502 unsigned int m68k_read_memory_32(unsigned int address)
504 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
505 /* if (address == 0x51136 || address == 0xFB074 || address == 0x1AF05E)
506 WriteLog("[RM32 PC=%08X] Addr: %08X, val: %08X\n", m68k_get_reg(NULL, M68K_REG_PC), address, (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2));//*/
508 //WriteLog("--> [RM32]\n");
509 return (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2);
512 void m68k_write_memory_8(unsigned int address, unsigned int value)
514 #ifdef CPU_DEBUG_MEMORY
515 if ((address >= 0x000000) && (address <= 0x3FFFFF))
519 if (value > writeMemMax[address])
520 writeMemMax[address] = value;
521 if (value < writeMemMin[address])
522 writeMemMin[address] = value;
526 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
527 // WriteLog("M68K: Writing %02X at %08X\n", value, address);
528 //WriteLog("[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
530 if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
531 WriteLog("M68K: Byte %02X written at %08X by 68K\n", value, address);//*/
533 if ((address >= 0x000000) && (address <= 0x3FFFFF))
534 jaguarMainRam[address] = value;
535 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
536 CDROMWriteByte(address, value, M68K);
537 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
538 TOMWriteByte(address, value, M68K);
539 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
540 JERRYWriteByte(address, value, M68K);
542 jaguar_unknown_writebyte(address, value, M68K);
545 void m68k_write_memory_16(unsigned int address, unsigned int value)
547 #ifdef CPU_DEBUG_MEMORY
548 if ((address >= 0x000000) && (address <= 0x3FFFFE))
552 uint8 hi = value >> 8, lo = value & 0xFF;
554 if (hi > writeMemMax[address])
555 writeMemMax[address] = hi;
556 if (hi < writeMemMin[address])
557 writeMemMin[address] = hi;
559 if (lo > writeMemMax[address+1])
560 writeMemMax[address+1] = lo;
561 if (lo < writeMemMin[address+1])
562 writeMemMin[address+1] = lo;
566 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
567 // WriteLog("M68K: Writing %04X at %08X\n", value, address);
568 //WriteLog("[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
569 //if (address >= 0xF02200 && address <= 0xF0229F)
570 // WriteLog("M68K: Writing to blitter --> %04X at %08X\n", value, address);
571 //if (address >= 0x0E75D0 && address <= 0x0E75E7)
572 // WriteLog("M68K: Writing %04X at %08X, M68K PC=%08X\n", value, address, m68k_get_reg(NULL, M68K_REG_PC));
573 /*extern uint32 totalFrames;
574 if (address == 0xF02114)
575 WriteLog("M68K: Writing to GPU_CTRL (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));
576 if (address == 0xF02110)
577 WriteLog("M68K: Writing to GPU_PC (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));//*/
578 //if (address >= 0xF03B00 && address <= 0xF03DFF)
579 // WriteLog("M68K: Writing %04X to %08X...\n", value, address);
581 /*if (address == 0x0100)//64*4)
582 WriteLog("M68K: Wrote word to VI vector value %04X...\n", value);//*/
584 if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
585 WriteLog("M68K: Word %04X written at %08X by 68K\n", value, address);//*/
586 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
587 || address == 0x1AF05E)
588 WriteLog("[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
590 if ((address >= 0x000000) && (address <= 0x3FFFFE))
592 /* jaguar_mainRam[address] = value >> 8;
593 jaguar_mainRam[address + 1] = value & 0xFF;*/
594 SET16(jaguarMainRam, address, value);
596 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
597 CDROMWriteWord(address, value, M68K);
598 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
599 TOMWriteWord(address, value, M68K);
600 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
601 JERRYWriteWord(address, value, M68K);
604 jaguar_unknown_writeword(address, value, M68K);
605 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
606 WriteLog("\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
607 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
608 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
613 void m68k_write_memory_32(unsigned int address, unsigned int value)
615 //WriteLog("--> [WM32]\n");
616 /*if (address == 0x0100)//64*4)
617 WriteLog("M68K: Wrote dword to VI vector value %08X...\n", value);//*/
618 /*if (address >= 0xF03214 && address < 0xF0321F)
619 WriteLog("M68K: Writing DWORD (%08X) to GPU RAM (%08X)...\n", value, address);//*/
620 //M68K: Writing DWORD (88E30047) to GPU RAM (00F03214)...
621 /*extern bool doGPUDis;
622 if (address == 0xF03214 && value == 0x88E30047)
625 /* if (address == 0x51136 || address == 0xFB074)
626 WriteLog("[WM32 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
628 m68k_write_memory_16(address, value >> 16);
629 m68k_write_memory_16(address + 2, value & 0xFFFF);
633 uint32 JaguarGetHandler(uint32 i)
635 return JaguarReadLong(i * 4);
638 bool JaguarInterruptHandlerIsValid(uint32 i) // Debug use only...
640 uint32 handler = JaguarGetHandler(i);
641 return (handler && (handler != 0xFFFFFFFF) ? 1 : 0);
644 void M68K_show_context(void)
646 WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
647 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
648 WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
650 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
651 WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
653 WriteLog("68K disasm\n");
654 // jaguar_dasm(s68000readPC()-0x1000,0x20000);
655 JaguarDasm(m68k_get_reg(NULL, M68K_REG_PC) - 0x80, 0x200);
656 // jaguar_dasm(0x5000, 0x14414);
658 // WriteLog("\n.......[Cart start]...........\n\n");
659 // jaguar_dasm(0x192000, 0x1000);//0x200);
661 WriteLog("..................\n");
663 if (TOMIRQEnabled(IRQ_VBLANK))
665 WriteLog("vblank int: enabled\n");
666 JaguarDasm(JaguarGetHandler(64), 0x200);
669 WriteLog("vblank int: disabled\n");
671 WriteLog("..................\n");
673 for(int i=0; i<256; i++)
674 WriteLog("handler %03i at $%08X\n", i, (unsigned int)JaguarGetHandler(i));
678 // Unknown read/write byte/word routines
681 // It's hard to believe that developers would be sloppy with their memory writes, yet in
682 // some cases the developers screwed up royal. E.g., Club Drive has the following code:
684 // 807EC4: movea.l #$f1b000, A1
685 // 807ECA: movea.l #$8129e0, A0
686 // 807ED0: move.l A0, D0
687 // 807ED2: move.l #$f1bb94, D1
688 // 807ED8: sub.l D0, D1
689 // 807EDA: lsr.l #2, D1
690 // 807EDC: move.l (A0)+, (A1)+
691 // 807EDE: dbra D1, 807edc
693 // The problem is at $807ED0--instead of putting A0 into D0, they really meant to put A1
694 // in. This mistake causes it to try and overwrite approximately $700000 worth of address
695 // space! (That is, unless the 68K causes a bus error...)
697 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
699 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
700 WriteLog("Jaguar: Unknown byte %02X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
702 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
703 // extern bool finished;
705 // extern bool doDSPDis;
711 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
713 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
714 WriteLog("Jaguar: Unknown word %04X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
716 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
717 // extern bool finished;
719 // extern bool doDSPDis;
725 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who/*=UNKNOWN*/)
727 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
728 WriteLog("Jaguar: Unknown byte read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
730 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
731 // extern bool finished;
733 // extern bool doDSPDis;
740 unsigned jaguar_unknown_readword(unsigned address, uint32 who/*=UNKNOWN*/)
742 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
743 WriteLog("Jaguar: Unknown word read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
745 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
746 // extern bool finished;
748 // extern bool doDSPDis;
756 // Disassemble M68K instructions at the given offset
759 unsigned int m68k_read_disassembler_8(unsigned int address)
761 return m68k_read_memory_8(address);
764 unsigned int m68k_read_disassembler_16(unsigned int address)
766 return m68k_read_memory_16(address);
769 unsigned int m68k_read_disassembler_32(unsigned int address)
771 return m68k_read_memory_32(address);
774 void JaguarDasm(uint32 offset, uint32 qt)
777 static char buffer[2048];//, mem[64];
778 int pc = offset, oldpc;
780 for(uint32 i=0; i<qt; i++)
783 for(int j=0; j<64; j++)
784 mem[j^0x01] = jaguar_byte_read(pc + j);
786 pc += Dasm68000((char *)mem, buffer, 0);
787 WriteLog("%08X: %s\n", oldpc, buffer);//*/
789 pc += m68k_disassemble(buffer, pc, M68K_CPU_TYPE_68000);
790 WriteLog("%08X: %s\n", oldpc, buffer);//*/
795 uint8 JaguarReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
800 if (offset < 0x400000)
801 data = jaguarMainRam[offset & 0x3FFFFF];
802 else if ((offset >= 0x800000) && (offset < 0xC00000))
803 data = jaguarMainRom[offset - 0x800000];
804 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
805 data = CDROMReadByte(offset, who);
806 else if ((offset >= 0xE00000) && (offset < 0xE40000))
807 data = jaguarBootRom[offset & 0x3FFFF];
808 else if ((offset >= 0xF00000) && (offset < 0xF10000))
809 data = TOMReadByte(offset, who);
810 else if ((offset >= 0xF10000) && (offset < 0xF20000))
811 data = JERRYReadByte(offset, who);
813 data = jaguar_unknown_readbyte(offset, who);
818 uint16 JaguarReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
821 if (offset <= 0x3FFFFE)
823 return (jaguarMainRam[(offset+0) & 0x3FFFFF] << 8) | jaguarMainRam[(offset+1) & 0x3FFFFF];
825 else if ((offset >= 0x800000) && (offset <= 0xBFFFFE))
828 return (jaguarMainRom[offset+0] << 8) | jaguarMainRom[offset+1];
830 // else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
831 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFE))
832 return CDROMReadWord(offset, who);
833 else if ((offset >= 0xE00000) && (offset <= 0xE3FFFE))
834 return (jaguarBootRom[(offset+0) & 0x3FFFF] << 8) | jaguarBootRom[(offset+1) & 0x3FFFF];
835 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFE))
836 return TOMReadWord(offset, who);
837 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFE))
838 return JERRYReadWord(offset, who);
840 return jaguar_unknown_readword(offset, who);
843 void JaguarWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
845 //Need to check for writes in the range of $18FA70 + 8000...
847 if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
848 WriteLog("JWB: Byte %02X written at %08X by %s\n", data, offset, whoName[who]);//*/
851 if (offset < 0x400000)
853 jaguarMainRam[offset & 0x3FFFFF] = data;
856 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
858 CDROMWriteByte(offset, data, who);
861 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFF))
863 TOMWriteByte(offset, data, who);
866 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFF))
868 JERRYWriteByte(offset, data, who);
872 jaguar_unknown_writebyte(offset, data, who);
876 void JaguarWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
878 /*if (offset == 0x0100)//64*4)
879 WriteLog("M68K: %s wrote word to VI vector value %04X...\n", whoName[who], data);
880 if (offset == 0x0102)//64*4)
881 WriteLog("M68K: %s wrote word to VI vector+2 value %04X...\n", whoName[who], data);//*/
882 //TEMP--Mirror of F03000? Yes, but only 32-bit CPUs can do it (i.e., NOT the 68K!)
883 // PLUS, you would handle this in the GPU/DSP WriteLong code! Not here!
884 //Need to check for writes in the range of $18FA70 + 8000...
886 if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
887 WriteLog("JWW: Word %04X written at %08X by %s\n", data, offset, whoName[who]);//*/
888 /*if (offset >= 0x2C00 && offset <= 0x2CFF)
889 WriteLog("Jaguar: Word %04X written to TOC+%02X by %s\n", data, offset-0x2C00, whoName[who]);//*/
893 if (offset <= 0x3FFFFE)
898 1A 69 F0 ($0000) -> Starfield
899 1A 73 C8 ($0001) -> Final clearing blit & bitmap blit?
902 1A 8F E8 ($0004) -> "Jaguar" small color logo?
911 //This MUST be done by the 68K!
912 /*if (offset == 0x670C)
913 WriteLog("Jaguar: %s writing to location $670C...\n", whoName[who]);*/
915 /*extern bool doGPUDis;
916 //if ((offset == 0x100000 + 75522) && who == GPU) // 76,226 -> 75522
917 if ((offset == 0x100000 + 128470) && who == GPU) // 107,167 -> 128470 (384 x 250 screen size 16BPP)
918 //if ((offset >= 0x100000 && offset <= 0x12C087) && who == GPU)
920 /*if (offset == 0x100000 + 128470) // 107,167 -> 128470 (384 x 250 screen size 16BPP)
921 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);
922 if ((data & 0xFF00) != 0x7700)
923 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
924 /*if ((offset >= 0x100000 && offset <= 0x147FFF) && who == GPU)
926 /*if ((data & 0xFF00) != 0x7700 && who == GPU)
927 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
928 /*if ((offset >= 0x100000 + 0x48000 && offset <= 0x12C087 + 0x48000) && who == GPU)
930 /*extern bool doGPUDis;
931 if (offset == 0x120216 && who == GPU)
933 /*extern uint32 gpu_pc;
934 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
936 uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
937 uint32 y = base / 0x300;
938 uint32 x = (base - (y * 0x300)) / 2;
939 WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
942 JWW: Writing starfield star 775E at 0011F650 (555984/1447)
944 //if (offset == (0x001E17F8 + 0x34))
945 /*if (who == GPU && offset == (0x001E17F8 + 0x34))
947 // WriteLog("JWW: Write at %08X written to by %s.\n", 0x001E17F8 + 0x34, whoName[who]);//*/
948 /*extern uint32 gpu_pc;
949 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
951 extern int objectPtr;
952 // if (offset > 0x148000)
955 if (starCount > objectPtr)
958 // if (starCount == 1)
959 // WriteLog("--> Drawing 1st star...\n");
961 // uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
962 // uint32 y = base / 0x300;
963 // uint32 x = (base - (y * 0x300)) / 2;
964 // WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
966 //A star of interest...
967 //-->JWW: Writing starfield star 77C9 at 0011D31A (269/155) [s]
968 //1st trail +3(x), -1(y) -> 272, 154 -> 0011D020
969 //JWW: Blitter writing echo 77B3 at 0011D022...
971 //extern bool doGPUDis;
972 /*if (offset == 0x11D022 + 0x48000 || offset == 0x11D022)// && who == GPU)
975 WriteLog("JWW: %s writing echo %04X at %08X...\n", whoName[who], data, offset);
978 if (offset == 0x11D31A + 0x48000 || offset == 0x11D31A)
979 WriteLog("JWW: %s writing star %04X at %08X...\n", whoName[who], data, offset);//*/
981 jaguarMainRam[(offset+0) & 0x3FFFFF] = data >> 8;
982 jaguarMainRam[(offset+1) & 0x3FFFFF] = data & 0xFF;
985 else if (offset >= 0xDFFF00 && offset <= 0xDFFFFE)
987 CDROMWriteWord(offset, data, who);
990 else if (offset >= 0xF00000 && offset <= 0xF0FFFE)
992 TOMWriteWord(offset, data, who);
995 else if (offset >= 0xF10000 && offset <= 0xF1FFFE)
997 JERRYWriteWord(offset, data, who);
1000 // Don't bomb on attempts to write to ROM
1001 else if (offset >= 0x800000 && offset <= 0xEFFFFF)
1004 jaguar_unknown_writeword(offset, data, who);
1007 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1008 uint32 JaguarReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
1010 return (JaguarReadWord(offset, who) << 16) | JaguarReadWord(offset+2, who);
1013 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1014 void JaguarWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
1016 /* extern bool doDSPDis;
1017 if (offset < 0x400 && !doDSPDis)
1019 WriteLog("JLW: Write to %08X by %s... Starting DSP log!\n\n", offset, whoName[who]);
1022 /*if (offset == 0x0100)//64*4)
1023 WriteLog("M68K: %s wrote dword to VI vector value %08X...\n", whoName[who], data);//*/
1025 JaguarWriteWord(offset, data >> 16, who);
1026 JaguarWriteWord(offset+2, data & 0xFFFF, who);
1030 // Jaguar console initialization
1032 void JaguarInit(void)
1034 #ifdef CPU_DEBUG_MEMORY
1035 memset(readMem, 0x00, 0x400000);
1036 memset(writeMemMin, 0xFF, 0x400000);
1037 memset(writeMemMax, 0x00, 0x400000);
1039 memory_malloc_secure((void **)&jaguarMainRam, 0x400000, "Jaguar 68K CPU RAM");
1040 memory_malloc_secure((void **)&jaguarMainRom, 0x600000, "Jaguar 68K CPU ROM");
1041 memory_malloc_secure((void **)&jaguarBootRom, 0x040000, "Jaguar 68K CPU BIOS ROM"); // Only uses half of this!
1042 memory_malloc_secure((void **)&jaguarCDBootROM, 0x040000, "Jaguar 68K CPU CD BIOS ROM");
1043 memset(jaguarMainRam, 0x00, 0x400000);
1044 // memset(jaguar_mainRom, 0xFF, 0x200000); // & set it to all Fs...
1045 // memset(jaguar_mainRom, 0x00, 0x200000); // & set it to all 0s...
1046 //NOTE: This *doesn't* fix FlipOut...
1047 //Or does it? Hmm...
1048 //Seems to want $01010101... Dunno why. Investigate!
1049 memset(jaguarMainRom, 0x01, 0x600000); // & set it to all 01s...
1050 // memset(jaguar_mainRom, 0xFF, 0x600000); // & set it to all Fs...
1052 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
1060 //New timer based code stuffola...
1061 void ScanlineCallback(void);
1062 void RenderCallback(void);
1063 //extern uint32 * backbuffer;
1064 void JaguarReset(void)
1066 //NOTE: This causes a (virtual) crash if this is set in the config but not found... !!! FIX !!!
1067 if (vjs.useJaguarBIOS)
1068 memcpy(jaguarMainRam, jaguarBootRom, 8);
1070 SET32(jaguarMainRam, 4, jaguarRunAddress);
1072 // WriteLog("jaguar_reset():\n");
1078 m68k_pulse_reset(); // Reset the 68000
1079 WriteLog("Jaguar: 68K reset. PC=%06X SP=%08X\n", m68k_get_reg(NULL, M68K_REG_PC), m68k_get_reg(NULL, M68K_REG_A7));
1081 // New timer base code stuffola...
1082 InitializeEventList();
1083 TOMResetBackbuffer(backbuffer);
1084 // SetCallbackTime(ScanlineCallback, 63.5555);
1085 SetCallbackTime(ScanlineCallback, 31.77775);
1086 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1087 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time
1090 void JaguarDone(void)
1092 #ifdef CPU_DEBUG_MEMORY
1093 /* WriteLog("\nJaguar: Memory Usage Stats (return addresses)\n\n");
1095 for(uint32 i=0; i<=raPtr; i++)
1097 WriteLog("\t%08X\n", returnAddr[i]);
1098 WriteLog("M68000 disassembly at $%08X...\n", returnAddr[i] - 16);
1099 jaguar_dasm(returnAddr[i] - 16, 16);
1104 /* int start = 0, end = 0;
1105 bool endTriggered = false, startTriggered = false;
1106 for(int i=0; i<0x400000; i++)
1108 if (readMem[i] && writeMemMin[i] != 0xFF && writeMemMax != 0x00)
1110 if (!startTriggered)
1111 startTriggered = true, endTriggered = false, start = i;
1113 WriteLog("\t\tMin/Max @ %06X: %u/%u\n", i, writeMemMin[i], writeMemMax[i]);
1119 end = i - 1, endTriggered = true, startTriggered = false;
1120 WriteLog("\tMemory range accessed: %06X - %06X\n", start, end);
1127 // for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
1128 // WriteLog("\tA%i = 0x%.8x\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
1129 int32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
1130 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
1131 for(int i=-2; i<9; i++)
1132 WriteLog("%06X: %08X\n", topOfStack + (i * 4), JaguarReadLong(topOfStack + (i * 4)));
1134 /* WriteLog("\nM68000 disassembly at $802288...\n");
1135 jaguar_dasm(0x802288, 3);
1136 WriteLog("\nM68000 disassembly at $802200...\n");
1137 jaguar_dasm(0x802200, 500);
1138 WriteLog("\nM68000 disassembly at $802518...\n");
1139 jaguar_dasm(0x802518, 100);//*/
1141 /* WriteLog("\n\nM68000 disassembly at $803F00 (look @ $803F2A)...\n");
1142 jaguar_dasm(0x803F00, 500);
1145 /* WriteLog("\n\nM68000 disassembly at $802B00 (look @ $802B5E)...\n");
1146 jaguar_dasm(0x802B00, 500);
1149 /* WriteLog("\n\nM68000 disassembly at $809900 (look @ $8099F8)...\n");
1150 jaguar_dasm(0x809900, 500);
1153 /* WriteLog("\n\nDump of $8093C8:\n\n");
1154 for(int i=0x8093C8; i<0x809900; i+=4)
1155 WriteLog("%06X: %08X\n", i, JaguarReadLong(i));//*/
1156 /* WriteLog("\n\nM68000 disassembly at $90006C...\n");
1157 jaguar_dasm(0x90006C, 500);
1159 /* WriteLog("\n\nM68000 disassembly at $1AC000...\n");
1160 jaguar_dasm(0x1AC000, 6000);
1163 // WriteLog("Jaguar: CD BIOS version %04X\n", JaguarReadWord(0x3004));
1164 WriteLog("Jaguar: Interrupt enable = %02X\n", TOMReadByte(0xF000E1) & 0x1F);
1165 WriteLog("Jaguar: VBL interrupt is %s\n", ((TOMIRQEnabled(IRQ_VBLANK)) && (JaguarInterruptHandlerIsValid(64))) ? "enabled" : "disabled");
1166 M68K_show_context();
1175 memory_free(jaguarMainRom);
1176 memory_free(jaguarMainRam);
1177 memory_free(jaguarBootRom);
1178 memory_free(jaguarCDBootROM);
1182 // Main Jaguar execution loop (1 frame)
1184 void JaguarExecute(uint32 * backbuffer, bool render)
1186 uint16 vp = TOMReadWord(0xF0003E) + 1;
1187 uint16 vi = TOMReadWord(0xF0004E);
1188 //Using WO registers is OK, since we're the ones controlling access--there's nothing wrong here! ;-)
1189 //Though we shouldn't be able to do it using TOMReadWord... !!! FIX !!!
1191 // uint16 vdb = TOMReadWord(0xF00046);
1192 //Note: This is the *definite* end of the display, though VDE *might* be less than this...
1193 // uint16 vbb = TOMReadWord(0xF00040);
1194 //It seems that they mean it when they say that VDE is the end of object processing.
1195 //However, we need to be able to tell the OP (or TOM) that we've reached the end of the
1196 //buffer and not to write any more pixels... !!! FIX !!!
1197 // uint16 vde = TOMReadWord(0xF00048);
1199 uint16 refreshRate = (vjs.hardwareTypeNTSC ? 60 : 50);
1200 //Not sure the above is correct, since the number of lines and timings given in the JTRM
1201 //seem to indicate the refresh rate is *half* the above...
1202 // uint16 refreshRate = (vjs.hardwareTypeNTSC ? 30 : 25);
1203 // Should these be hardwired or read from VP? Yes, from VP!
1204 uint32 M68KCyclesPerScanline
1205 = (vjs.hardwareTypeNTSC ? M68K_CLOCK_RATE_NTSC : M68K_CLOCK_RATE_PAL) / (vp * refreshRate);
1206 uint32 RISCCyclesPerScanline
1207 = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL) / (vp * refreshRate);
1209 TOMResetBackbuffer(backbuffer);
1210 /*extern int effect_start;
1212 WriteLog("JagExe: VP=%u, VI=%u, CPU CPS=%u, GPU CPS=%u\n", vp, vi, M68KCyclesPerScanline, RISCCyclesPerScanline);//*/
1214 //extern int start_logging;
1215 for(uint16 i=0; i<vp; i++)
1217 // Increment the horizontal count (why? RNG? Besides which, this is *NOT* cycle accurate!)
1218 TOMWriteWord(0xF00004, (TOMReadWord(0xF00004) + 1) & 0x7FF);
1220 TOMWriteWord(0xF00006, i); // Write the VC
1222 // if (i == vi) // Time for Vertical Interrupt?
1223 //Not sure if this is correct...
1224 //Seems to be, kinda. According to the JTRM, this should only fire on odd lines in non-interlace mode...
1225 //Which means that it normally wouldn't go when it's zero.
1226 if (i == vi && i > 0 && TOMIRQEnabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1228 // We don't have to worry about autovectors & whatnot because the Jaguar
1229 // tells you through its HW registers who sent the interrupt...
1230 TOMSetPendingVideoInt();
1234 //if (start_logging)
1235 // WriteLog("About to execute M68K (%u)...\n", i);
1236 m68k_execute(M68KCyclesPerScanline);
1237 //if (start_logging)
1238 // WriteLog("About to execute TOM's PIT (%u)...\n", i);
1239 TOMExecPIT(RISCCyclesPerScanline);
1240 //if (start_logging)
1241 // WriteLog("About to execute JERRY's PIT (%u)...\n", i);
1242 JERRYExecPIT(RISCCyclesPerScanline);
1243 //if (start_logging)
1244 // WriteLog("About to execute JERRY's SSI (%u)...\n", i);
1245 JERRYI2SExec(RISCCyclesPerScanline);
1246 BUTCHExec(RISCCyclesPerScanline);
1247 //if (start_logging)
1248 // WriteLog("About to execute GPU (%u)...\n", i);
1249 GPUExec(RISCCyclesPerScanline);
1253 if (vjs.usePipelinedDSP)
1254 DSPExecP2(RISCCyclesPerScanline); // Pipelined DSP execution (3 stage)...
1256 DSPExec(RISCCyclesPerScanline); // Ordinary non-pipelined DSP
1257 // DSPExecComp(RISCCyclesPerScanline); // Comparison core
1260 //if (start_logging)
1261 // WriteLog("About to execute OP (%u)...\n", i);
1262 TOMExecScanline(i, render);
1266 // Temp debugging stuff
1268 void DumpMainMemory(void)
1270 FILE * fp = fopen("./memdump.bin", "wb");
1275 fwrite(jaguarMainRam, 1, 0x400000, fp);
1279 uint8 * GetRamPtr(void)
1281 return jaguarMainRam;
1285 // New Jaguar execution stack
1290 void JaguarExecuteNew(void)
1292 extern bool finished, showGUI;
1293 extern bool debounceRunKey;
1294 // Pass a message to the "joystick" code to debounce the ESC key...
1295 debounceRunKey = true;
1297 /* InitializeEventList();
1298 TOMResetBackbuffer(backbuffer);
1299 // SetCallbackTime(ScanlineCallback, 63.5555);
1300 SetCallbackTime(ScanlineCallback, 31.77775);
1301 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1302 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time//*/
1303 // uint8 * keystate = SDL_GetKeyState(NULL);
1307 double timeToNextEvent = GetTimeToNextEvent();
1308 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1310 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1311 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1315 if (vjs.usePipelinedDSP)
1316 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1318 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1323 // if (keystate[SDLK_ESCAPE])
1326 // SDL_PumpEvents(); // Needed to keep the keystate current...
1331 void ScanlineCallback(void)
1333 uint16 vc = TOMReadWord(0xF00006);
1334 uint16 vp = TOMReadWord(0xF0003E) + 1;
1335 uint16 vi = TOMReadWord(0xF0004E);
1336 // uint16 vbb = TOMReadWord(0xF00040);
1342 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1343 TOMWriteWord(0xF00006, vc);
1345 //This is a crappy kludge, but maybe it'll work for now...
1346 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1347 if (vc == vi && vc > 0 && tom_irq_enabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1349 // We don't have to worry about autovectors & whatnot because the Jaguar
1350 // tells you through its HW registers who sent the interrupt...
1351 tom_set_pending_video_int();
1355 TOMExecScanline(vc, true);
1357 //Change this to VBB???
1358 //Doesn't seem to matter (at least for Flip Out & I-War)
1365 TOMResetBackbuffer(backbuffer);
1369 // TOMResetBackbuffer(backbuffer);
1371 // SetCallbackTime(ScanlineCallback, 63.5555);
1372 SetCallbackTime(ScanlineCallback, 31.77775);
1378 void JaguarExecuteNew(void)
1380 // extern bool finished, showGUI;
1381 // extern bool debounceRunKey;
1382 // Pass a message to the "joystick" code to debounce the ESC key...
1383 // debounceRunKey = true;
1384 // finished = false;
1385 /* InitializeEventList();
1386 TOMResetBackbuffer(backbuffer);
1387 // SetCallbackTime(ScanlineCallback, 63.5555);
1388 SetCallbackTime(ScanlineCallback, 31.77775);
1389 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1390 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time//*/
1391 // uint8 * keystate = SDL_GetKeyState(NULL);
1396 double timeToNextEvent = GetTimeToNextEvent();
1397 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1399 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1400 GPUExec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1404 if (vjs.usePipelinedDSP)
1405 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1407 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1412 // if (keystate[SDLK_ESCAPE])
1415 // SDL_PumpEvents(); // Needed to keep the keystate current...
1420 void ScanlineCallback(void)
1422 uint16 vc = TOMReadWord(0xF00006);
1423 uint16 vp = TOMReadWord(0xF0003E) + 1;
1424 uint16 vi = TOMReadWord(0xF0004E);
1425 // uint16 vbb = TOMReadWord(0xF00040);
1431 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1432 TOMWriteWord(0xF00006, vc);
1434 //This is a crappy kludge, but maybe it'll work for now...
1435 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1436 if (vc == vi && vc > 0 && TOMIRQEnabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1438 // We don't have to worry about autovectors & whatnot because the Jaguar
1439 // tells you through its HW registers who sent the interrupt...
1440 TOMSetPendingVideoInt();
1444 TOMExecScanline(vc, true);
1446 //Change this to VBB???
1447 //Doesn't seem to matter (at least for Flip Out & I-War)
1453 TOMResetBackbuffer(backbuffer);
1458 // TOMResetBackbuffer(backbuffer);
1460 // SetCallbackTime(ScanlineCallback, 63.5555);
1461 SetCallbackTime(ScanlineCallback, 31.77775);
1466 // This isn't currently used, but maybe it should be...
1467 void RenderCallback(void)
1470 TOMResetBackbuffer(backbuffer);
1471 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1472 SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time