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. :-)
15 //#include "m68kdasmAG.h"
18 #include "SDL_opengl.h"
31 //Do this in makefile??? Yes! Could, but it's easier to define here...
32 //#define LOG_UNMAPPED_MEMORY_ACCESSES
33 //#define ABORT_ON_UNMAPPED_MEMORY_ACCESS
34 #define ABORT_ON_ILLEGAL_INSTRUCTIONS
35 //#define ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
36 #define CPU_DEBUG_MEMORY
38 // Private function prototypes
40 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who = UNKNOWN);
41 unsigned jaguar_unknown_readword(unsigned address, uint32 who = UNKNOWN);
42 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who = UNKNOWN);
43 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who = UNKNOWN);
44 void M68K_show_context(void);
48 //extern bool hardwareTypeNTSC; // Set to false for PAL
49 #ifdef CPU_DEBUG_MEMORY
50 extern bool startMemLog; // Set by "e" key
51 extern int effect_start;
52 extern int effect_start2, effect_start3, effect_start4, effect_start5, effect_start6;
55 // Memory debugging identifiers
57 const char * whoName[9] =
58 { "Unknown", "Jaguar", "DSP", "GPU", "TOM", "JERRY", "M68K", "Blitter", "OP" };
60 uint32 jaguar_active_memory_dumps = 0;
62 uint32 jaguar_mainRom_crc32, jaguarRomSize, jaguarRunAddress;
64 uint8 * jaguar_mainRam = NULL;
65 uint8 * jaguar_mainRom = NULL;
66 uint8 * jaguar_bootRom = NULL;
67 uint8 * jaguar_CDBootROM = NULL;
68 bool BIOSLoaded = false;
69 bool CDBIOSLoaded = false;
71 #ifdef CPU_DEBUG_MEMORY
72 uint8 writeMemMax[0x400000], writeMemMin[0x400000];
73 uint8 readMem[0x400000];
74 uint32 returnAddr[4000], raPtr = 0xFFFFFFFF;
77 uint32 pcQueue[0x400];
81 // Callback function to detect illegal instructions
83 void GPUDumpDisassembly(void);
84 void GPUDumpRegisters(void);
85 static bool start = false;
86 void M68KInstructionHook(void)
88 uint32 m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
91 // Ideally, we'd save all the registers as well...
92 pcQueue[pcQPtr++] = m68kPC;
95 if (m68kPC & 0x01) // Oops! We're fetching an odd address!
97 WriteLog("M68K: Attempted to execute from an odd adress!\n\nBacktrace:\n\n");
99 static char buffer[2048];
100 for(int i=0; i<0x400; i++)
102 m68k_disassemble(buffer, pcQueue[(pcQPtr + i) & 0x3FF], M68K_CPU_TYPE_68000);
103 WriteLog("\t%08X: %s\n", pcQueue[(pcQPtr + i) & 0x3FF], buffer);
107 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
108 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
109 for(int i=0; i<10; i++)
110 WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
111 WriteLog("Jaguar: VBL interrupt is %s\n", ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64))) ? "enabled" : "disabled");
117 /* if (m68kPC >= 0x807EC4 && m68kPC <= 0x807EDB)
119 static char buffer[2048];
120 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
121 WriteLog("%08X: %s", m68kPC, buffer);
122 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
123 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
124 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
126 /* if (m68kPC == 0x8D0E48 && effect_start5)
128 WriteLog("\nM68K: At collision detection code. Exiting!\n\n");
130 GPUDumpDisassembly();
134 /* uint16 opcode = JaguarReadWord(m68kPC);
135 if (opcode == 0x4E75) // RTS
138 // WriteLog("Jaguar: Returning from subroutine to %08X\n", JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7)));
140 uint32 addr = JaguarReadLong(m68k_get_reg(NULL, M68K_REG_A7));
142 if (raPtr != 0xFFFFFFFF)
144 for(uint32 i=0; i<=raPtr; i++)
146 if (returnAddr[i] == addr)
155 returnAddr[++raPtr] = addr;
159 //Flip Out! debugging...
162 00805FDC: movea.l #$9c6f8, A0 D0=00100010, A0=00100000
163 00805FE2: move.w #$10, (A0)+ D0=00100010, A0=0009C6F8
164 00805FE6: cmpa.l #$c96f8, A0 D0=00100010, A0=0009C6FA
165 00805FEC: bne 805fe2 D0=00100010, A0=0009C6FA
167 0080603A: move.l #$11ed7c, $100.w D0=61700080, A0=000C96F8, D1=00000000, A1=000040D8
169 0012314C: move.l (A0)+, (A1)+ D0=61700080, A0=00124174, D1=00000000, A1=00F03FFC
170 0012314E: cmpa.l #$f04000, A1 D0=61700080, A0=00124178, D1=00000000, A1=00F04000
171 00123154: blt 12314c D0=61700080, A0=00124178, D1=00000000, A1=00F04000
172 00123156: move.l #$0, $f035d0.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
173 00123160: move.l #$f03000, $f02110.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
174 0012316A: move.l #$1, $f02114.l D0=61700080, A0=00124178, D1=00000000, A1=00F04000
175 00123174: rts D0=61700080, A0=00124178, D1=00000000, A1=00F04000
177 /* static char buffer[2048];
178 //if (m68kPC > 0x805F48) start = true;
179 //if (m68kPC > 0x806486) start = true;
180 //if (m68kPC == 0x805FEE) start = true;
181 //if (m68kPC == 0x80600C)// start = true;
182 if (m68kPC == 0x802058) start = true;
184 // GPUDumpRegisters();
185 // GPUDumpDisassembly();
187 // M68K_show_context();
193 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
194 WriteLog("%08X: %s \t\tD0=%08X, A0=%08X, D1=%08X, A1=%08X\n", m68kPC, buffer, m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_A1));
197 /* if (m68kPC == 0x803F16)
199 WriteLog("M68K: Registers found at $803F16:\n");
200 WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
201 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
202 WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
204 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
205 WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
207 //Looks like the DSP is supposed to return $12345678 when it finishes its validation routine...
208 // !!! Investigate !!!
209 /*extern bool doDSPDis;
210 static bool disgo = false;
211 if (m68kPC == 0x50222)
214 // WriteLog("M68K: About to stuff $12345678 into $F1B000 (=%08X)...\n", DSPReadLong(0xF1B000, M68K));
215 // DSPWriteLong(0xF1B000, 0x12345678, M68K);
218 if (m68kPC == 0x5000)
223 static char buffer[2048];
224 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
225 WriteLog("%08X: %s", m68kPC, buffer);
226 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
227 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
228 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
230 if (m68kPC == 0x82E1A)
232 static char buffer[2048];
233 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
234 WriteLog("--> [Routine start] %08X: %s", m68kPC, buffer);
235 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X(cmd), D1=%08X(# bytes), D2=%08X\n",
236 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
237 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
239 if (m68kPC == 0x82E58)
240 WriteLog("--> [Routine end]\n");
241 if (m68kPC == 0x80004)
243 WriteLog("--> [Calling BusWrite2] D2: %08X\n", m68k_get_reg(NULL, M68K_REG_D2));
244 // m68k_set_reg(M68K_REG_D2, 0x12345678);
268 if (m68kPC == 0x3000)
269 WriteLog("M68K: CD_init\n");
270 else if (m68kPC == 0x3006 + (6 * 0))
271 WriteLog("M68K: CD_mode\n");
272 else if (m68kPC == 0x3006 + (6 * 1))
273 WriteLog("M68K: CD_ack\n");
274 else if (m68kPC == 0x3006 + (6 * 2))
275 WriteLog("M68K: CD_jeri\n");
276 else if (m68kPC == 0x3006 + (6 * 3))
277 WriteLog("M68K: CD_spin\n");
278 else if (m68kPC == 0x3006 + (6 * 4))
279 WriteLog("M68K: CD_stop\n");
280 else if (m68kPC == 0x3006 + (6 * 5))
281 WriteLog("M68K: CD_mute\n");
282 else if (m68kPC == 0x3006 + (6 * 6))
283 WriteLog("M68K: CD_umute\n");
284 else if (m68kPC == 0x3006 + (6 * 7))
285 WriteLog("M68K: CD_paus\n");
286 else if (m68kPC == 0x3006 + (6 * 8))
287 WriteLog("M68K: CD_upaus\n");
288 else if (m68kPC == 0x3006 + (6 * 9))
289 WriteLog("M68K: CD_read\n");
290 else if (m68kPC == 0x3006 + (6 * 10))
291 WriteLog("M68K: CD_uread\n");
292 else if (m68kPC == 0x3006 + (6 * 11))
293 WriteLog("M68K: CD_setup\n");
294 else if (m68kPC == 0x3006 + (6 * 12))
295 WriteLog("M68K: CD_ptr\n");
296 else if (m68kPC == 0x3006 + (6 * 13))
297 WriteLog("M68K: CD_osamp\n");
298 else if (m68kPC == 0x3006 + (6 * 14))
299 WriteLog("M68K: CD_getoc\n");
300 else if (m68kPC == 0x3006 + (6 * 15))
301 WriteLog("M68K: CD_initm\n");
302 else if (m68kPC == 0x3006 + (6 * 16))
303 WriteLog("M68K: CD_initf\n");
304 else if (m68kPC == 0x3006 + (6 * 17))
305 WriteLog("M68K: CD_switch\n");
307 if (m68kPC >= 0x3000 && m68kPC <= 0x306C)
308 WriteLog("\t\tA0=%08X, A1=%08X, D0=%08X, D1=%08X, D2=%08X\n",
309 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
310 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2));
312 #ifdef ABORT_ON_ILLEGAL_INSTRUCTIONS
313 if (!m68k_is_valid_instruction(m68k_read_memory_16(m68kPC), M68K_CPU_TYPE_68000))
315 #ifndef ABORT_ON_OFFICIAL_ILLEGAL_INSTRUCTION
316 if (m68k_read_memory_16(m68kPC) == 0x4AFC)
318 // This is a kludge to let homebrew programs work properly (i.e., let the other processors
319 // keep going even when the 68K dumped back to the debugger or what have you).
321 // m68k_set_reg(M68K_REG_PC, m68kPC - 2);
322 // Try setting the vector to the illegal instruction...
323 //This doesn't work right either! Do something else! Quick!
324 // SET32(jaguar_mainRam, 0x10, m68kPC);
330 WriteLog("\nM68K encountered an illegal instruction at %08X!!!\n\nAborting!\n", m68kPC);
331 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
332 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
333 for(int i=0; i<10; i++)
334 WriteLog("%06X: %08X\n", topOfStack - (i * 4), JaguarReadLong(topOfStack - (i * 4)));
335 WriteLog("Jaguar: VBL interrupt is %s\n", ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64))) ? "enabled" : "disabled");
339 // WriteLog("\n\n68K disasm\n\n");
340 // jaguar_dasm(0x802000, 0x50C);
351 // Musashi 68000 read/write/IRQ functions
354 int irq_ack_handler(int level)
356 int vector = M68K_INT_ACK_AUTOVECTOR;
358 // The GPU/DSP/etc are probably *not* issuing an NMI, but it seems to work OK...
362 m68k_set_irq(0); // Clear the IRQ...
363 vector = 64; // Set user interrupt #0
369 unsigned int m68k_read_memory_8(unsigned int address)
371 #ifdef CPU_DEBUG_MEMORY
372 if ((address >= 0x000000) && (address <= 0x3FFFFF))
375 readMem[address] = 1;
378 //WriteLog("[RM8] Addr: %08X\n", address);
379 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
380 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
381 || address == 0x1AF05E)
382 WriteLog("[RM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, jaguar_mainRam[address]);//*/
383 unsigned int retVal = 0;
385 if ((address >= 0x000000) && (address <= 0x3FFFFF))
386 retVal = jaguar_mainRam[address];
387 // else if ((address >= 0x800000) && (address <= 0xDFFFFF))
388 else if ((address >= 0x800000) && (address <= 0xDFFEFF))
389 retVal = jaguar_mainRom[address - 0x800000];
390 else if ((address >= 0xE00000) && (address <= 0xE3FFFF))
391 retVal = jaguar_bootRom[address - 0xE00000];
392 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
393 retVal = CDROMReadByte(address);
394 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
395 retVal = TOMReadByte(address, M68K);
396 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
397 retVal = JERRYReadByte(address, M68K);
399 retVal = jaguar_unknown_readbyte(address, M68K);
401 //if (address >= 0x2800 && address <= 0x281F)
402 // WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
403 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
404 // WriteLog("M68K: Read byte $%02X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
408 void gpu_dump_disassembly(void);
409 void gpu_dump_registers(void);
411 unsigned int m68k_read_memory_16(unsigned int address)
413 #ifdef CPU_DEBUG_MEMORY
414 /* if ((address >= 0x000000) && (address <= 0x3FFFFE))
417 readMem[address] = 1, readMem[address + 1] = 1;
419 /* if (effect_start && (address >= 0x8064FC && address <= 0x806501))
421 return 0x4E71; // NOP
423 if (effect_start2 && (address >= 0x806502 && address <= 0x806507))
425 return 0x4E71; // NOP
427 if (effect_start3 && (address >= 0x806512 && address <= 0x806517))
429 return 0x4E71; // NOP
431 if (effect_start4 && (address >= 0x806524 && address <= 0x806527))
433 return 0x4E71; // NOP
435 if (effect_start5 && (address >= 0x80653E && address <= 0x806543)) //Collision detection!
437 return 0x4E71; // NOP
439 if (effect_start6 && (address >= 0x806544 && address <= 0x806547))
441 return 0x4E71; // NOP
444 //WriteLog("[RM16] Addr: %08X\n", address);
445 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005FBA)
446 // for(int i=0; i<10000; i++)
447 WriteLog("[M68K] In routine #6!\n");//*/
448 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00006696) // GPU Program #4
449 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005B3C) // GPU Program #2
450 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005BA8) // GPU Program #3
452 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
453 gpu_dump_registers();
454 gpu_dump_disassembly();
455 // for(int i=0; i<10000; i++)
456 // WriteLog("[M68K] About to run GPU!\n");
458 //WriteLog("[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
459 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x00006696 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x000066A8)
461 if (address == 0x000066A0)
463 gpu_dump_registers();
464 gpu_dump_disassembly();
466 for(int i=0; i<10000; i++)
467 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, TOMReadWord(address));
469 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
470 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
471 || address == 0x1AF05E)
472 WriteLog("[RM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, GET16(jaguar_mainRam, address));//*/
473 unsigned int retVal = 0;
475 if ((address >= 0x000000) && (address <= 0x3FFFFE))
476 // retVal = (jaguar_mainRam[address] << 8) | jaguar_mainRam[address+1];
477 retVal = GET16(jaguar_mainRam, address);
478 // else if ((address >= 0x800000) && (address <= 0xDFFFFE))
479 else if ((address >= 0x800000) && (address <= 0xDFFEFE))
480 retVal = (jaguar_mainRom[address - 0x800000] << 8) | jaguar_mainRom[address - 0x800000 + 1];
481 else if ((address >= 0xE00000) && (address <= 0xE3FFFE))
482 retVal = (jaguar_bootRom[address - 0xE00000] << 8) | jaguar_bootRom[address - 0xE00000 + 1];
483 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
484 retVal = CDROMReadWord(address, M68K);
485 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
486 retVal = TOMReadWord(address, M68K);
487 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
488 retVal = JERRYReadWord(address, M68K);
490 retVal = jaguar_unknown_readword(address, M68K);
492 //if (address >= 0xF1B000 && address <= 0xF1CFFF)
493 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
494 //if (address >= 0x2800 && address <= 0x281F)
495 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
496 //$8B3AE -> Transferred from $F1C010
497 //$8B5E4 -> Only +1 read at $808AA
498 //if (address >= 0x8B5E4 && address <= 0x8B5E4 + 16)
499 // WriteLog("M68K: Read word $%04X at $%08X [PC=%08X]\n", retVal, address, m68k_get_reg(NULL, M68K_REG_PC));
503 unsigned int m68k_read_memory_32(unsigned int address)
505 //; So, it seems that it stores the returned DWORD at $51136 and $FB074.
506 /* if (address == 0x51136 || address == 0xFB074 || address == 0x1AF05E)
507 WriteLog("[RM32 PC=%08X] Addr: %08X, val: %08X\n", m68k_get_reg(NULL, M68K_REG_PC), address, (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2));//*/
509 //WriteLog("--> [RM32]\n");
510 return (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2);
513 void m68k_write_memory_8(unsigned int address, unsigned int value)
515 #ifdef CPU_DEBUG_MEMORY
516 if ((address >= 0x000000) && (address <= 0x3FFFFF))
520 if (value > writeMemMax[address])
521 writeMemMax[address] = value;
522 if (value < writeMemMin[address])
523 writeMemMin[address] = value;
527 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
528 // WriteLog("M68K: Writing %02X at %08X\n", value, address);
529 //WriteLog("[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
531 if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
532 WriteLog("M68K: Byte %02X written at %08X by 68K\n", value, address);//*/
534 if ((address >= 0x000000) && (address <= 0x3FFFFF))
535 jaguar_mainRam[address] = value;
536 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
537 CDROMWriteByte(address, value, M68K);
538 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
539 TOMWriteByte(address, value, M68K);
540 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
541 JERRYWriteByte(address, value, M68K);
543 jaguar_unknown_writebyte(address, value, M68K);
546 void m68k_write_memory_16(unsigned int address, unsigned int value)
548 #ifdef CPU_DEBUG_MEMORY
549 if ((address >= 0x000000) && (address <= 0x3FFFFE))
553 uint8 hi = value >> 8, lo = value & 0xFF;
555 if (hi > writeMemMax[address])
556 writeMemMax[address] = hi;
557 if (hi < writeMemMin[address])
558 writeMemMin[address] = hi;
560 if (lo > writeMemMax[address+1])
561 writeMemMax[address+1] = lo;
562 if (lo < writeMemMin[address+1])
563 writeMemMin[address+1] = lo;
567 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
568 // WriteLog("M68K: Writing %04X at %08X\n", value, address);
569 //WriteLog("[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
570 //if (address >= 0xF02200 && address <= 0xF0229F)
571 // WriteLog("M68K: Writing to blitter --> %04X at %08X\n", value, address);
572 //if (address >= 0x0E75D0 && address <= 0x0E75E7)
573 // WriteLog("M68K: Writing %04X at %08X, M68K PC=%08X\n", value, address, m68k_get_reg(NULL, M68K_REG_PC));
574 /*extern uint32 totalFrames;
575 if (address == 0xF02114)
576 WriteLog("M68K: Writing to GPU_CTRL (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));
577 if (address == 0xF02110)
578 WriteLog("M68K: Writing to GPU_PC (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));//*/
579 //if (address >= 0xF03B00 && address <= 0xF03DFF)
580 // WriteLog("M68K: Writing %04X to %08X...\n", value, address);
582 /*if (address == 0x0100)//64*4)
583 WriteLog("M68K: Wrote word to VI vector value %04X...\n", value);//*/
585 if (address >= 0x18FA70 && address < (0x18FA70 + 8000))
586 WriteLog("M68K: Word %04X written at %08X by 68K\n", value, address);//*/
587 /* if (address == 0x51136 || address == 0x51138 || address == 0xFB074 || address == 0xFB076
588 || address == 0x1AF05E)
589 WriteLog("[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
591 if ((address >= 0x000000) && (address <= 0x3FFFFE))
593 /* jaguar_mainRam[address] = value >> 8;
594 jaguar_mainRam[address + 1] = value & 0xFF;*/
595 SET16(jaguar_mainRam, address, value);
597 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
598 CDROMWriteWord(address, value, M68K);
599 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
600 TOMWriteWord(address, value, M68K);
601 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
602 JERRYWriteWord(address, value, M68K);
605 jaguar_unknown_writeword(address, value, M68K);
606 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
607 WriteLog("\tA0=%08X, A1=%08X, D0=%08X, D1=%08X\n",
608 m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1),
609 m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1));
614 void m68k_write_memory_32(unsigned int address, unsigned int value)
616 //WriteLog("--> [WM32]\n");
617 /*if (address == 0x0100)//64*4)
618 WriteLog("M68K: Wrote dword to VI vector value %08X...\n", value);//*/
619 /*if (address >= 0xF03214 && address < 0xF0321F)
620 WriteLog("M68K: Writing DWORD (%08X) to GPU RAM (%08X)...\n", value, address);//*/
621 //M68K: Writing DWORD (88E30047) to GPU RAM (00F03214)...
622 /*extern bool doGPUDis;
623 if (address == 0xF03214 && value == 0x88E30047)
626 /* if (address == 0x51136 || address == 0xFB074)
627 WriteLog("[WM32 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);//*/
629 m68k_write_memory_16(address, value >> 16);
630 m68k_write_memory_16(address + 2, value & 0xFFFF);
634 uint32 jaguar_get_handler(uint32 i)
636 return JaguarReadLong(i * 4);
639 uint32 jaguar_interrupt_handler_is_valid(uint32 i) // Debug use only...
641 uint32 handler = jaguar_get_handler(i);
642 if (handler && (handler != 0xFFFFFFFF))
648 void M68K_show_context(void)
650 WriteLog("\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
651 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
652 WriteLog("\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
654 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
655 WriteLog("\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
657 WriteLog("68K disasm\n");
658 // jaguar_dasm(s68000readPC()-0x1000,0x20000);
659 jaguar_dasm(m68k_get_reg(NULL, M68K_REG_PC) - 0x80, 0x200);
660 // jaguar_dasm(0x5000, 0x14414);
662 // WriteLog("\n.......[Cart start]...........\n\n");
663 // jaguar_dasm(0x192000, 0x1000);//0x200);
665 WriteLog("..................\n");
667 if (tom_irq_enabled(IRQ_VBLANK))
669 WriteLog("vblank int: enabled\n");
670 jaguar_dasm(jaguar_get_handler(64), 0x200);
673 WriteLog("vblank int: disabled\n");
675 WriteLog("..................\n");
677 for(int i=0; i<256; i++)
678 WriteLog("handler %03i at $%08X\n", i, (unsigned int)jaguar_get_handler(i));
682 // Unknown read/write byte/word routines
685 // It's hard to believe that developers would be sloppy with their memory writes, yet in
686 // some cases the developers screwed up royal. E.g., Club Drive has the following code:
688 // 807EC4: movea.l #$f1b000, A1
689 // 807ECA: movea.l #$8129e0, A0
690 // 807ED0: move.l A0, D0
691 // 807ED2: move.l #$f1bb94, D1
692 // 807ED8: sub.l D0, D1
693 // 807EDA: lsr.l #2, D1
694 // 807EDC: move.l (A0)+, (A1)+
695 // 807EDE: dbra D1, 807edc
697 // The problem is at $807ED0--instead of putting A0 into D0, they really meant to put A1
698 // in. This mistake causes it to try and overwrite approximately $700000 worth of address
699 // space! (That is, unless the 68K causes a bus error...)
701 void jaguar_unknown_writebyte(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
703 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
704 WriteLog("Jaguar: Unknown byte %02X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
706 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
707 // extern bool finished;
709 // extern bool doDSPDis;
715 void jaguar_unknown_writeword(unsigned address, unsigned data, uint32 who/*=UNKNOWN*/)
717 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
718 WriteLog("Jaguar: Unknown word %04X written at %08X by %s (M68K PC=%06X)\n", data, address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
720 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
721 // extern bool finished;
723 // extern bool doDSPDis;
729 unsigned jaguar_unknown_readbyte(unsigned address, uint32 who/*=UNKNOWN*/)
731 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
732 WriteLog("Jaguar: Unknown byte read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
734 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
735 // extern bool finished;
737 // extern bool doDSPDis;
744 unsigned jaguar_unknown_readword(unsigned address, uint32 who/*=UNKNOWN*/)
746 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
747 WriteLog("Jaguar: Unknown word read at %08X by %s (M68K PC=%06X)\n", address, whoName[who], m68k_get_reg(NULL, M68K_REG_PC));
749 #ifdef ABORT_ON_UNMAPPED_MEMORY_ACCESS
750 // extern bool finished;
752 // extern bool doDSPDis;
760 // Disassemble M68K instructions at the given offset
763 unsigned int m68k_read_disassembler_8(unsigned int address)
765 return m68k_read_memory_8(address);
768 unsigned int m68k_read_disassembler_16(unsigned int address)
770 return m68k_read_memory_16(address);
773 unsigned int m68k_read_disassembler_32(unsigned int address)
775 return m68k_read_memory_32(address);
778 void jaguar_dasm(uint32 offset, uint32 qt)
781 static char buffer[2048];//, mem[64];
782 int pc = offset, oldpc;
784 for(uint32 i=0; i<qt; i++)
787 for(int j=0; j<64; j++)
788 mem[j^0x01] = jaguar_byte_read(pc + j);
790 pc += Dasm68000((char *)mem, buffer, 0);
791 WriteLog("%08X: %s\n", oldpc, buffer);//*/
793 pc += m68k_disassemble(buffer, pc, M68K_CPU_TYPE_68000);
794 WriteLog("%08X: %s\n", oldpc, buffer);//*/
799 uint8 JaguarReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
804 if (offset < 0x400000)
805 data = jaguar_mainRam[offset & 0x3FFFFF];
806 else if ((offset >= 0x800000) && (offset < 0xC00000))
807 data = jaguar_mainRom[offset - 0x800000];
808 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
809 data = CDROMReadByte(offset, who);
810 else if ((offset >= 0xE00000) && (offset < 0xE40000))
811 data = jaguar_bootRom[offset & 0x3FFFF];
812 else if ((offset >= 0xF00000) && (offset < 0xF10000))
813 data = TOMReadByte(offset, who);
814 else if ((offset >= 0xF10000) && (offset < 0xF20000))
815 data = JERRYReadByte(offset, who);
817 data = jaguar_unknown_readbyte(offset, who);
822 uint16 JaguarReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
825 if (offset <= 0x3FFFFE)
827 return (jaguar_mainRam[(offset+0) & 0x3FFFFF] << 8) | jaguar_mainRam[(offset+1) & 0x3FFFFF];
829 else if ((offset >= 0x800000) && (offset <= 0xBFFFFE))
832 return (jaguar_mainRom[offset+0] << 8) | jaguar_mainRom[offset+1];
834 // else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
835 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFE))
836 return CDROMReadWord(offset, who);
837 else if ((offset >= 0xE00000) && (offset <= 0xE3FFFE))
838 return (jaguar_bootRom[(offset+0) & 0x3FFFF] << 8) | jaguar_bootRom[(offset+1) & 0x3FFFF];
839 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFE))
840 return TOMReadWord(offset, who);
841 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFE))
842 return JERRYReadWord(offset, who);
844 return jaguar_unknown_readword(offset, who);
847 void JaguarWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
849 //Need to check for writes in the range of $18FA70 + 8000...
851 if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
852 WriteLog("JWB: Byte %02X written at %08X by %s\n", data, offset, whoName[who]);//*/
855 if (offset < 0x400000)
857 jaguar_mainRam[offset & 0x3FFFFF] = data;
860 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
862 CDROMWriteByte(offset, data, who);
865 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFF))
867 TOMWriteByte(offset, data, who);
870 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFF))
872 JERRYWriteByte(offset, data, who);
876 jaguar_unknown_writebyte(offset, data, who);
880 void JaguarWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
882 /*if (offset == 0x0100)//64*4)
883 WriteLog("M68K: %s wrote word to VI vector value %04X...\n", whoName[who], data);
884 if (offset == 0x0102)//64*4)
885 WriteLog("M68K: %s wrote word to VI vector+2 value %04X...\n", whoName[who], data);//*/
886 //TEMP--Mirror of F03000? Yes, but only 32-bit CPUs can do it (i.e., NOT the 68K!)
887 // PLUS, you would handle this in the GPU/DSP WriteLong code! Not here!
888 //Need to check for writes in the range of $18FA70 + 8000...
890 if (offset >= 0x18FA70 && offset < (0x18FA70 + 8000))
891 WriteLog("JWW: Word %04X written at %08X by %s\n", data, offset, whoName[who]);//*/
892 /*if (offset >= 0x2C00 && offset <= 0x2CFF)
893 WriteLog("Jaguar: Word %04X written to TOC+%02X by %s\n", data, offset-0x2C00, whoName[who]);//*/
897 if (offset <= 0x3FFFFE)
902 1A 69 F0 ($0000) -> Starfield
903 1A 73 C8 ($0001) -> Final clearing blit & bitmap blit?
906 1A 8F E8 ($0004) -> "Jaguar" small color logo?
915 //This MUST be done by the 68K!
916 /*if (offset == 0x670C)
917 WriteLog("Jaguar: %s writing to location $670C...\n", whoName[who]);*/
919 /*extern bool doGPUDis;
920 //if ((offset == 0x100000 + 75522) && who == GPU) // 76,226 -> 75522
921 if ((offset == 0x100000 + 128470) && who == GPU) // 107,167 -> 128470 (384 x 250 screen size 16BPP)
922 //if ((offset >= 0x100000 && offset <= 0x12C087) && who == GPU)
924 /*if (offset == 0x100000 + 128470) // 107,167 -> 128470 (384 x 250 screen size 16BPP)
925 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);
926 if ((data & 0xFF00) != 0x7700)
927 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
928 /*if ((offset >= 0x100000 && offset <= 0x147FFF) && who == GPU)
930 /*if ((data & 0xFF00) != 0x7700 && who == GPU)
931 WriteLog("JWW: Writing value %04X at %08X by %s...\n", data, offset, whoName[who]);//*/
932 /*if ((offset >= 0x100000 + 0x48000 && offset <= 0x12C087 + 0x48000) && who == GPU)
934 /*extern bool doGPUDis;
935 if (offset == 0x120216 && who == GPU)
937 /*extern uint32 gpu_pc;
938 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
940 uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
941 uint32 y = base / 0x300;
942 uint32 x = (base - (y * 0x300)) / 2;
943 WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
946 JWW: Writing starfield star 775E at 0011F650 (555984/1447)
948 //if (offset == (0x001E17F8 + 0x34))
949 /*if (who == GPU && offset == (0x001E17F8 + 0x34))
951 // WriteLog("JWW: Write at %08X written to by %s.\n", 0x001E17F8 + 0x34, whoName[who]);//*/
952 /*extern uint32 gpu_pc;
953 if (who == GPU && (gpu_pc == 0xF03604 || gpu_pc == 0xF03638))
955 extern int objectPtr;
956 // if (offset > 0x148000)
959 if (starCount > objectPtr)
962 // if (starCount == 1)
963 // WriteLog("--> Drawing 1st star...\n");
965 // uint32 base = offset - (offset > 0x148000 ? 0x148000 : 0x100000);
966 // uint32 y = base / 0x300;
967 // uint32 x = (base - (y * 0x300)) / 2;
968 // WriteLog("JWW: Writing starfield star %04X at %08X (%u/%u) [%s]\n", data, offset, x, y, (gpu_pc == 0xF03604 ? "s" : "L"));
970 //A star of interest...
971 //-->JWW: Writing starfield star 77C9 at 0011D31A (269/155) [s]
972 //1st trail +3(x), -1(y) -> 272, 154 -> 0011D020
973 //JWW: Blitter writing echo 77B3 at 0011D022...
975 //extern bool doGPUDis;
976 /*if (offset == 0x11D022 + 0x48000 || offset == 0x11D022)// && who == GPU)
979 WriteLog("JWW: %s writing echo %04X at %08X...\n", whoName[who], data, offset);
982 if (offset == 0x11D31A + 0x48000 || offset == 0x11D31A)
983 WriteLog("JWW: %s writing star %04X at %08X...\n", whoName[who], data, offset);//*/
985 jaguar_mainRam[(offset+0) & 0x3FFFFF] = data >> 8;
986 jaguar_mainRam[(offset+1) & 0x3FFFFF] = data & 0xFF;
989 else if (offset >= 0xDFFF00 && offset <= 0xDFFFFE)
991 CDROMWriteWord(offset, data, who);
994 else if (offset >= 0xF00000 && offset <= 0xF0FFFE)
996 TOMWriteWord(offset, data, who);
999 else if (offset >= 0xF10000 && offset <= 0xF1FFFE)
1001 JERRYWriteWord(offset, data, who);
1004 // Don't bomb on attempts to write to ROM
1005 else if (offset >= 0x800000 && offset <= 0xEFFFFF)
1008 jaguar_unknown_writeword(offset, data, who);
1011 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1012 uint32 JaguarReadLong(uint32 offset, uint32 who/*=UNKNOWN*/)
1014 return (JaguarReadWord(offset, who) << 16) | JaguarReadWord(offset+2, who);
1017 // We really should re-do this so that it does *real* 32-bit access... !!! FIX !!!
1018 void JaguarWriteLong(uint32 offset, uint32 data, uint32 who/*=UNKNOWN*/)
1020 /* extern bool doDSPDis;
1021 if (offset < 0x400 && !doDSPDis)
1023 WriteLog("JLW: Write to %08X by %s... Starting DSP log!\n\n", offset, whoName[who]);
1026 /*if (offset == 0x0100)//64*4)
1027 WriteLog("M68K: %s wrote dword to VI vector value %08X...\n", whoName[who], data);//*/
1029 JaguarWriteWord(offset, data >> 16, who);
1030 JaguarWriteWord(offset+2, data & 0xFFFF, who);
1034 // Jaguar console initialization
1036 void jaguar_init(void)
1038 #ifdef CPU_DEBUG_MEMORY
1039 memset(readMem, 0x00, 0x400000);
1040 memset(writeMemMin, 0xFF, 0x400000);
1041 memset(writeMemMax, 0x00, 0x400000);
1043 memory_malloc_secure((void **)&jaguar_mainRam, 0x400000, "Jaguar 68K CPU RAM");
1044 memory_malloc_secure((void **)&jaguar_mainRom, 0x600000, "Jaguar 68K CPU ROM");
1045 memory_malloc_secure((void **)&jaguar_bootRom, 0x040000, "Jaguar 68K CPU BIOS ROM"); // Only uses half of this!
1046 memory_malloc_secure((void **)&jaguar_CDBootROM, 0x040000, "Jaguar 68K CPU CD BIOS ROM");
1047 memset(jaguar_mainRam, 0x00, 0x400000);
1048 // memset(jaguar_mainRom, 0xFF, 0x200000); // & set it to all Fs...
1049 // memset(jaguar_mainRom, 0x00, 0x200000); // & set it to all 0s...
1050 //NOTE: This *doesn't* fix FlipOut...
1051 //Or does it? Hmm...
1052 //Seems to want $01010101... Dunno why. Investigate!
1053 memset(jaguar_mainRom, 0x01, 0x600000); // & set it to all 01s...
1054 // memset(jaguar_mainRom, 0xFF, 0x600000); // & set it to all Fs...
1056 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
1064 //New timer based code stuffola...
1065 void ScanlineCallback(void);
1066 void RenderCallback(void);
1067 //extern uint32 * backbuffer;
1068 void jaguar_reset(void)
1070 //NOTE: This causes a (virtual) crash if this is set in the config but not found... !!! FIX !!!
1071 if (vjs.useJaguarBIOS)
1072 memcpy(jaguar_mainRam, jaguar_bootRom, 8);
1074 SET32(jaguar_mainRam, 4, jaguarRunAddress);
1076 // WriteLog("jaguar_reset():\n");
1082 m68k_pulse_reset(); // Reset the 68000
1083 WriteLog("Jaguar: 68K reset. PC=%06X SP=%08X\n", m68k_get_reg(NULL, M68K_REG_PC), m68k_get_reg(NULL, M68K_REG_A7));
1085 // New timer base code stuffola...
1086 InitializeEventList();
1087 TOMResetBackbuffer(backbuffer);
1088 // SetCallbackTime(ScanlineCallback, 63.5555);
1089 SetCallbackTime(ScanlineCallback, 31.77775);
1090 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1091 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time
1094 void jaguar_done(void)
1096 #ifdef CPU_DEBUG_MEMORY
1097 /* WriteLog("\nJaguar: Memory Usage Stats (return addresses)\n\n");
1099 for(uint32 i=0; i<=raPtr; i++)
1101 WriteLog("\t%08X\n", returnAddr[i]);
1102 WriteLog("M68000 disassembly at $%08X...\n", returnAddr[i] - 16);
1103 jaguar_dasm(returnAddr[i] - 16, 16);
1108 /* int start = 0, end = 0;
1109 bool endTriggered = false, startTriggered = false;
1110 for(int i=0; i<0x400000; i++)
1112 if (readMem[i] && writeMemMin[i] != 0xFF && writeMemMax != 0x00)
1114 if (!startTriggered)
1115 startTriggered = true, endTriggered = false, start = i;
1117 WriteLog("\t\tMin/Max @ %06X: %u/%u\n", i, writeMemMin[i], writeMemMax[i]);
1123 end = i - 1, endTriggered = true, startTriggered = false;
1124 WriteLog("\tMemory range accessed: %06X - %06X\n", start, end);
1131 // for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
1132 // WriteLog("\tA%i = 0x%.8x\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
1133 int32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
1134 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", JaguarReadLong(topOfStack));
1135 for(int i=-2; i<9; i++)
1136 WriteLog("%06X: %08X\n", topOfStack + (i * 4), JaguarReadLong(topOfStack + (i * 4)));
1138 /* WriteLog("\nM68000 disassembly at $802288...\n");
1139 jaguar_dasm(0x802288, 3);
1140 WriteLog("\nM68000 disassembly at $802200...\n");
1141 jaguar_dasm(0x802200, 500);
1142 WriteLog("\nM68000 disassembly at $802518...\n");
1143 jaguar_dasm(0x802518, 100);//*/
1145 /* WriteLog("\n\nM68000 disassembly at $803F00 (look @ $803F2A)...\n");
1146 jaguar_dasm(0x803F00, 500);
1149 /* WriteLog("\n\nM68000 disassembly at $802B00 (look @ $802B5E)...\n");
1150 jaguar_dasm(0x802B00, 500);
1153 /* WriteLog("\n\nM68000 disassembly at $809900 (look @ $8099F8)...\n");
1154 jaguar_dasm(0x809900, 500);
1157 /* WriteLog("\n\nDump of $8093C8:\n\n");
1158 for(int i=0x8093C8; i<0x809900; i+=4)
1159 WriteLog("%06X: %08X\n", i, JaguarReadLong(i));//*/
1160 /* WriteLog("\n\nM68000 disassembly at $90006C...\n");
1161 jaguar_dasm(0x90006C, 500);
1163 /* WriteLog("\n\nM68000 disassembly at $1AC000...\n");
1164 jaguar_dasm(0x1AC000, 6000);
1167 // WriteLog("Jaguar: CD BIOS version %04X\n", JaguarReadWord(0x3004));
1168 WriteLog("Jaguar: Interrupt enable = %02X\n", TOMReadByte(0xF000E1) & 0x1F);
1169 WriteLog("Jaguar: VBL interrupt is %s\n", ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64))) ? "enabled" : "disabled");
1170 M68K_show_context();
1179 memory_free(jaguar_mainRom);
1180 memory_free(jaguar_mainRam);
1181 memory_free(jaguar_bootRom);
1182 memory_free(jaguar_CDBootROM);
1186 // Main Jaguar execution loop (1 frame)
1188 void JaguarExecute(uint32 * backbuffer, bool render)
1190 uint16 vp = TOMReadWord(0xF0003E) + 1;
1191 uint16 vi = TOMReadWord(0xF0004E);
1192 //Using WO registers is OK, since we're the ones controlling access--there's nothing wrong here! ;-)
1193 //Though we shouldn't be able to do it using TOMReadWord... !!! FIX !!!
1195 // uint16 vdb = TOMReadWord(0xF00046);
1196 //Note: This is the *definite* end of the display, though VDE *might* be less than this...
1197 // uint16 vbb = TOMReadWord(0xF00040);
1198 //It seems that they mean it when they say that VDE is the end of object processing.
1199 //However, we need to be able to tell the OP (or TOM) that we've reached the end of the
1200 //buffer and not to write any more pixels... !!! FIX !!!
1201 // uint16 vde = TOMReadWord(0xF00048);
1203 uint16 refreshRate = (vjs.hardwareTypeNTSC ? 60 : 50);
1204 //Not sure the above is correct, since the number of lines and timings given in the JTRM
1205 //seem to indicate the refresh rate is *half* the above...
1206 // uint16 refreshRate = (vjs.hardwareTypeNTSC ? 30 : 25);
1207 // Should these be hardwired or read from VP? Yes, from VP!
1208 uint32 M68KCyclesPerScanline
1209 = (vjs.hardwareTypeNTSC ? M68K_CLOCK_RATE_NTSC : M68K_CLOCK_RATE_PAL) / (vp * refreshRate);
1210 uint32 RISCCyclesPerScanline
1211 = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL) / (vp * refreshRate);
1213 TOMResetBackbuffer(backbuffer);
1214 /*extern int effect_start;
1216 WriteLog("JagExe: VP=%u, VI=%u, CPU CPS=%u, GPU CPS=%u\n", vp, vi, M68KCyclesPerScanline, RISCCyclesPerScanline);//*/
1218 //extern int start_logging;
1219 for(uint16 i=0; i<vp; i++)
1221 // Increment the horizontal count (why? RNG? Besides which, this is *NOT* cycle accurate!)
1222 TOMWriteWord(0xF00004, (TOMReadWord(0xF00004) + 1) & 0x7FF);
1224 TOMWriteWord(0xF00006, i); // Write the VC
1226 // if (i == vi) // Time for Vertical Interrupt?
1227 //Not sure if this is correct...
1228 //Seems to be, kinda. According to the JTRM, this should only fire on odd lines in non-interlace mode...
1229 //Which means that it normally wouldn't go when it's zero.
1230 if (i == vi && i > 0 && tom_irq_enabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1232 // We don't have to worry about autovectors & whatnot because the Jaguar
1233 // tells you through its HW registers who sent the interrupt...
1234 tom_set_pending_video_int();
1238 //if (start_logging)
1239 // WriteLog("About to execute M68K (%u)...\n", i);
1240 m68k_execute(M68KCyclesPerScanline);
1241 //if (start_logging)
1242 // WriteLog("About to execute TOM's PIT (%u)...\n", i);
1243 TOMExecPIT(RISCCyclesPerScanline);
1244 //if (start_logging)
1245 // WriteLog("About to execute JERRY's PIT (%u)...\n", i);
1246 JERRYExecPIT(RISCCyclesPerScanline);
1247 //if (start_logging)
1248 // WriteLog("About to execute JERRY's SSI (%u)...\n", i);
1249 jerry_i2s_exec(RISCCyclesPerScanline);
1250 BUTCHExec(RISCCyclesPerScanline);
1251 //if (start_logging)
1252 // WriteLog("About to execute GPU (%u)...\n", i);
1253 gpu_exec(RISCCyclesPerScanline);
1257 if (vjs.usePipelinedDSP)
1258 DSPExecP2(RISCCyclesPerScanline); // Pipelined DSP execution (3 stage)...
1260 DSPExec(RISCCyclesPerScanline); // Ordinary non-pipelined DSP
1261 // DSPExecComp(RISCCyclesPerScanline); // Comparison core
1264 //if (start_logging)
1265 // WriteLog("About to execute OP (%u)...\n", i);
1266 TOMExecScanline(i, render);
1270 // Temp debugging stuff
1272 void DumpMainMemory(void)
1274 FILE * fp = fopen("./memdump.bin", "wb");
1279 fwrite(jaguar_mainRam, 1, 0x400000, fp);
1283 uint8 * GetRamPtr(void)
1285 return jaguar_mainRam;
1289 // New Jaguar execution stack
1294 void JaguarExecuteNew(void)
1296 extern bool finished, showGUI;
1297 extern bool debounceRunKey;
1298 // Pass a message to the "joystick" code to debounce the ESC key...
1299 debounceRunKey = true;
1301 /* InitializeEventList();
1302 TOMResetBackbuffer(backbuffer);
1303 // SetCallbackTime(ScanlineCallback, 63.5555);
1304 SetCallbackTime(ScanlineCallback, 31.77775);
1305 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1306 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time//*/
1307 // uint8 * keystate = SDL_GetKeyState(NULL);
1311 double timeToNextEvent = GetTimeToNextEvent();
1312 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1314 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1315 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1319 if (vjs.usePipelinedDSP)
1320 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1322 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1327 // if (keystate[SDLK_ESCAPE])
1330 // SDL_PumpEvents(); // Needed to keep the keystate current...
1335 void ScanlineCallback(void)
1337 uint16 vc = TOMReadWord(0xF00006);
1338 uint16 vp = TOMReadWord(0xF0003E) + 1;
1339 uint16 vi = TOMReadWord(0xF0004E);
1340 // uint16 vbb = TOMReadWord(0xF00040);
1346 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1347 TOMWriteWord(0xF00006, vc);
1349 //This is a crappy kludge, but maybe it'll work for now...
1350 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1351 if (vc == vi && vc > 0 && tom_irq_enabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1353 // We don't have to worry about autovectors & whatnot because the Jaguar
1354 // tells you through its HW registers who sent the interrupt...
1355 tom_set_pending_video_int();
1359 TOMExecScanline(vc, true);
1361 //Change this to VBB???
1362 //Doesn't seem to matter (at least for Flip Out & I-War)
1369 TOMResetBackbuffer(backbuffer);
1373 // TOMResetBackbuffer(backbuffer);
1375 // SetCallbackTime(ScanlineCallback, 63.5555);
1376 SetCallbackTime(ScanlineCallback, 31.77775);
1382 void JaguarExecuteNew(void)
1384 // extern bool finished, showGUI;
1385 // extern bool debounceRunKey;
1386 // Pass a message to the "joystick" code to debounce the ESC key...
1387 // debounceRunKey = true;
1388 // finished = false;
1389 /* InitializeEventList();
1390 TOMResetBackbuffer(backbuffer);
1391 // SetCallbackTime(ScanlineCallback, 63.5555);
1392 SetCallbackTime(ScanlineCallback, 31.77775);
1393 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1394 // SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time//*/
1395 // uint8 * keystate = SDL_GetKeyState(NULL);
1400 double timeToNextEvent = GetTimeToNextEvent();
1401 //WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
1403 m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
1404 gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
1408 if (vjs.usePipelinedDSP)
1409 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Pipelined DSP execution (3 stage)...
1411 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent)); // Ordinary non-pipelined DSP
1416 // if (keystate[SDLK_ESCAPE])
1419 // SDL_PumpEvents(); // Needed to keep the keystate current...
1424 void ScanlineCallback(void)
1426 uint16 vc = TOMReadWord(0xF00006);
1427 uint16 vp = TOMReadWord(0xF0003E) + 1;
1428 uint16 vi = TOMReadWord(0xF0004E);
1429 // uint16 vbb = TOMReadWord(0xF00040);
1435 //WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
1436 TOMWriteWord(0xF00006, vc);
1438 //This is a crappy kludge, but maybe it'll work for now...
1439 //Maybe it's not so bad, since the IRQ happens on a scanline boundary...
1440 if (vc == vi && vc > 0 && tom_irq_enabled(IRQ_VBLANK)) // Time for Vertical Interrupt?
1442 // We don't have to worry about autovectors & whatnot because the Jaguar
1443 // tells you through its HW registers who sent the interrupt...
1444 tom_set_pending_video_int();
1448 TOMExecScanline(vc, true);
1450 //Change this to VBB???
1451 //Doesn't seem to matter (at least for Flip Out & I-War)
1457 TOMResetBackbuffer(backbuffer);
1462 // TOMResetBackbuffer(backbuffer);
1464 // SetCallbackTime(ScanlineCallback, 63.5555);
1465 SetCallbackTime(ScanlineCallback, 31.77775);
1470 // This isn't currently used, but maybe it should be...
1471 void RenderCallback(void)
1474 TOMResetBackbuffer(backbuffer);
1475 // SetCallbackTime(RenderCallback, 33303.082); // # Scanlines * scanline time
1476 SetCallbackTime(RenderCallback, 16651.541); // # Scanlines * scanline time