5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (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. :-)
12 //#include "m68kdasmAG.h"
14 //#define LOG_UNMAPPED_MEMORY_ACCESSES
15 //#define SOUND_OUTPUT
17 #define JAGUAR_WIP_RELEASE
18 //Do this in makefile??? Yes! Could, but it's easier to define here...
19 //#define LOG_UNMAPPED_MEMORY_ACCESSES
21 // Private function prototypes
23 unsigned jaguar_unknown_readbyte(unsigned address);
24 unsigned jaguar_unknown_readword(unsigned address);
25 void jaguar_unknown_writebyte(unsigned address, unsigned data);
26 void jaguar_unknown_writeword(unsigned address, unsigned data);
27 void M68K_show_context(void);
31 extern bool hardwareTypeNTSC; // Set to false for PAL
33 // These values are overridden by command line switches...
35 bool dsp_enabled = false;
36 bool jaguar_use_bios = true; // Default is now to USE the BIOS
37 uint32 jaguar_active_memory_dumps = 0;
39 uint32 jaguar_mainRom_crc32;
41 static uint32 m68k_cycles_per_scanline;
42 static uint32 gpu_cycles_per_scanline;
43 static uint32 dsp_cycles_per_scanline;
44 static uint32 jaguar_screen_scanlines;
46 /*static*/ uint8 * jaguar_mainRam = NULL;
47 /*static*/ uint8 * jaguar_bootRom = NULL;
48 /*static*/ uint8 * jaguar_mainRom = NULL;
52 // Callback function to detect illegal instructions
54 void M68KInstructionHook(void)
56 uint32 m68kPC = m68k_get_reg(NULL, M68K_REG_PC);
58 /* static char buffer[2048];
59 m68k_disassemble(buffer, m68kPC, M68K_CPU_TYPE_68000);
60 WriteLog("%08X: %s \t\tD0=%08X, A0=%08X\n", m68kPC, buffer, m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_A0));//*/
62 /* if (m68kPC == 0x803F16)
64 WriteLog("M68K: Registers found at $803F16:\n");
65 WriteLog( "\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
66 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
67 WriteLog( "\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
69 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
70 WriteLog( "\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
73 if (!m68k_is_valid_instruction(jaguar_word_read(m68kPC), M68K_CPU_TYPE_68000))
75 WriteLog("\nEncountered illegal instruction at %08X!!!\n\nAborting!\n", m68kPC);
76 uint32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
77 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", jaguar_long_read(topOfStack));
78 for(int i=0; i<10; i++)
79 WriteLog("%06X: %08X\n", topOfStack - (i * 4), jaguar_long_read(topOfStack - (i * 4)));
80 WriteLog("Jaguar: VBL interrupt is %s\n", ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64))) ? "enabled" : "disabled");
88 // Musashi 68000 read/write/IRQ functions
91 int irq_ack_handler(int level)
93 int vector = M68K_INT_ACK_AUTOVECTOR;
95 // The GPU/DSP/etc are probably *not* issuing an NMI, but it seems to work OK...
99 m68k_set_irq(0); // Clear the IRQ...
100 vector = 64; // Set user interrupt #0
106 unsigned int m68k_read_memory_8(unsigned int address)
108 //WriteLog( "[RM8] Addr: %08X\n", address);
109 unsigned int retVal = 0;
111 if ((address >= 0x000000) && (address <= 0x3FFFFF))
112 retVal = jaguar_mainRam[address];
113 else if ((address >= 0x800000) && (address <= 0xDFFFFF))
114 retVal = jaguar_mainRom[address - 0x800000];
115 else if ((address >= 0xE00000) && (address <= 0xE3FFFF))
116 retVal = jaguar_bootRom[address - 0xE00000];
117 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
118 retVal = cdrom_byte_read(address);
119 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
120 retVal = tom_byte_read(address);
121 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
122 retVal = jerry_byte_read(address);
124 retVal = jaguar_unknown_readbyte(address);
129 void gpu_dump_disassembly(void);
130 void gpu_dump_registers(void);
132 unsigned int m68k_read_memory_16(unsigned int address)
134 //WriteLog( "[RM16] Addr: %08X\n", address);
135 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005FBA)
136 // for(int i=0; i<10000; i++)
137 WriteLog("[M68K] In routine #6!\n");//*/
138 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00006696) // GPU Program #4
139 //if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005B3C) // GPU Program #2
140 /*if (m68k_get_reg(NULL, M68K_REG_PC) == 0x00005BA8) // GPU Program #3
142 WriteLog("[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, tom_word_read(address));
143 gpu_dump_registers();
144 gpu_dump_disassembly();
145 // for(int i=0; i<10000; i++)
146 // WriteLog( "[M68K] About to run GPU!\n");
148 //WriteLog( "[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
149 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x00006696 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x000066A8)
151 if (address == 0x000066A0)
153 gpu_dump_registers();
154 gpu_dump_disassembly();
156 for(int i=0; i<10000; i++)
157 WriteLog( "[M68K] About to run GPU! (Addr:%08X, data:%04X)\n", address, tom_word_read(address));
159 unsigned int retVal = 0;
161 if ((address >= 0x000000) && (address <= 0x3FFFFE))
162 retVal = (jaguar_mainRam[address] << 8) | jaguar_mainRam[address+1];
163 else if ((address >= 0x800000) && (address <= 0xDFFFFE))
164 retVal = (jaguar_mainRom[address - 0x800000] << 8) | jaguar_mainRom[address - 0x800000 + 1];
165 else if ((address >= 0xE00000) && (address <= 0xE3FFFE))
166 retVal = (jaguar_bootRom[address - 0xE00000] << 8) | jaguar_bootRom[address - 0xE00000 + 1];
167 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
168 retVal = cdrom_word_read(address);
169 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
170 retVal = tom_word_read(address);
171 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
172 retVal = jerry_word_read(address);
175 //WriteLog( "[RM16] Unknown address: %08X\n", address);
176 retVal = jaguar_unknown_readword(address);
182 unsigned int m68k_read_memory_32(unsigned int address)
184 //WriteLog( "--> [RM32]\n");
185 return (m68k_read_memory_16(address) << 16) | m68k_read_memory_16(address + 2);
188 void m68k_write_memory_8(unsigned int address, unsigned int value)
190 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
191 // WriteLog("M68K: Writing %02X at %08X\n", value, address);
192 //WriteLog( "[WM8 PC=%08X] Addr: %08X, val: %02X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
193 if ((address >= 0x000000) && (address <= 0x3FFFFF))
194 jaguar_mainRam[address] = value;
195 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFF))
196 cdrom_byte_write(address, value);
197 else if ((address >= 0xF00000) && (address <= 0xF0FFFF))
198 tom_byte_write(address, value);
199 else if ((address >= 0xF10000) && (address <= 0xF1FFFF))
200 jerry_byte_write(address, value);
202 jaguar_unknown_writebyte(address, value);
205 void m68k_write_memory_16(unsigned int address, unsigned int value)
207 extern int dsp_pc;//, dsp_control;
208 if (address == 0xF1A116 && (value & 0x01))
210 WriteLog(" M68K(16): DSP is GO! (DSP_PC: %08X)\n\n", dsp_pc);
212 /* static char buffer[512];
214 while (j <= 0xF1BFFF)
217 j += dasmjag(JAGUAR_DSP, buffer, j);
218 WriteLog( "\t%08X: %s\n", oldj, buffer);
223 // WriteLog("M68K(16): DSP halted... (Old value: %08X)\n", dsp_control);
225 //if ((address >= 0x1FF020 && address <= 0x1FF03F) || (address >= 0x1FF820 && address <= 0x1FF83F))
226 // WriteLog("M68K: Writing %04X at %08X\n", value, address);
227 //WriteLog( "[WM16 PC=%08X] Addr: %08X, val: %04X\n", m68k_get_reg(NULL, M68K_REG_PC), address, value);
228 //if (address >= 0xF02200 && address <= 0xF0229F)
229 // WriteLog("M68K: Writing to blitter --> %04X at %08X\n", value, address);
230 //if (address >= 0x0E75D0 && address <= 0x0E75E7)
231 // WriteLog("M68K: Writing %04X at %08X, M68K PC=%08X\n", value, address, m68k_get_reg(NULL, M68K_REG_PC));
232 /*extern uint32 totalFrames;
233 extern bool suppressOutput;
234 if (totalFrames >= 59)
235 suppressOutput = false;//*/
236 /*if (address == 0xF02114)
237 WriteLog("M68K: Writing to GPU_CTRL (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));
238 if (address == 0xF02110)
239 WriteLog("M68K: Writing to GPU_PC (frame:%u)... [M68K PC:%08X]\n", totalFrames, m68k_get_reg(NULL, M68K_REG_PC));//*/
240 //if (address >= 0xF03B00 && address <= 0xF03DFF)
241 // WriteLog("M68K: Writing %04X to %08X...\n", value, address);
243 if ((address >= 0x000000) && (address <= 0x3FFFFE))
245 jaguar_mainRam[address] = value >> 8;
246 jaguar_mainRam[address + 1] = value & 0xFF;
248 else if ((address >= 0xDFFF00) && (address <= 0xDFFFFE))
249 cdrom_word_write(address, value);
250 else if ((address >= 0xF00000) && (address <= 0xF0FFFE))
251 tom_word_write(address, value);
252 else if ((address >= 0xF10000) && (address <= 0xF1FFFE))
253 jerry_word_write(address, value);
255 jaguar_unknown_writeword(address, value);
258 void m68k_write_memory_32(unsigned int address, unsigned int value)
260 extern int dsp_pc;//, dsp_control;
261 if (address == 0xF1A114 && (value & 0x01))
262 WriteLog("M68K(32): DSP is GO! (DSP_PC: %08X)\n", dsp_pc);
264 // WriteLog("M68K(32): DSP halted... (Old value: %08X)\n", dsp_control);
266 //WriteLog( "--> [WM32]\n");
267 m68k_write_memory_16(address, value >> 16);
268 m68k_write_memory_16(address + 2, value & 0xFFFF);
273 uint32 jaguar_get_handler(uint32 i)
275 // return (jaguar_word_read(i<<2) << 16) | jaguar_word_read((i<<2) + 2);
276 // return (jaguar_word_read(i*4) << 16) | jaguar_word_read((i*4) + 2);
277 return jaguar_long_read(i * 4);
280 uint32 jaguar_interrupt_handler_is_valid(uint32 i)
282 uint32 handler = jaguar_get_handler(i);
283 if (handler && (handler != 0xFFFFFFFF))
289 void M68K_show_context(void)
291 WriteLog( "\t68K PC=%06X\n", m68k_get_reg(NULL, M68K_REG_PC));
292 for(int i=M68K_REG_D0; i<=M68K_REG_D7; i++)
293 WriteLog( "\tD%i = %08X\n", i-M68K_REG_D0, m68k_get_reg(NULL, (m68k_register_t)i));
295 for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
296 WriteLog( "\tA%i = %08X\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
298 WriteLog( "68K disasm\n");
299 // jaguar_dasm(s68000readPC()-0x1000,0x20000);
300 jaguar_dasm(m68k_get_reg(NULL, M68K_REG_PC) - 0x80, 0x200);
301 // jaguar_dasm(0x5000, 0x14414);
303 WriteLog( "..................\n");
305 if (tom_irq_enabled(IRQ_VBLANK))
307 WriteLog( "vblank int: enabled\n");
308 jaguar_dasm(jaguar_get_handler(64), 0x200);
311 WriteLog( "vblank int: disabled\n");
313 WriteLog( "..................\n");
315 for(int i=0; i<256; i++)
316 WriteLog( "handler %03i at $%08X\n", i, (unsigned int)jaguar_get_handler(i));
320 // Unknown read/write byte/word routines
323 void jaguar_unknown_writebyte(unsigned address, unsigned data)
325 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
326 WriteLog( "jaguar: unknown byte %02X write at %08X (PC=%06X)\n", data, address, m68k_get_reg(NULL, M68K_REG_PC));
330 void jaguar_unknown_writeword(unsigned address, unsigned data)
332 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
333 WriteLog( "jaguar: unknown word %04X write at %08X (PC=%06X)\n", data, address, m68k_get_reg(NULL, M68K_REG_PC));
337 unsigned jaguar_unknown_readbyte(unsigned address)
339 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
340 WriteLog( "jaguar: unknown byte read at %08X (PC=%06X)\n", address, m68k_get_reg(NULL, M68K_REG_PC));
345 unsigned jaguar_unknown_readword(unsigned address)
347 #ifdef LOG_UNMAPPED_MEMORY_ACCESSES
348 WriteLog( "Jaguar: Unknown word read at %08X (PC=%06X)\n", address, m68k_get_reg(NULL, M68K_REG_PC));
354 // Disassemble M68K instructions at the given offset
357 unsigned int m68k_read_disassembler_8(unsigned int address)
359 return m68k_read_memory_8(address);
362 unsigned int m68k_read_disassembler_16(unsigned int address)
364 return m68k_read_memory_16(address);
367 unsigned int m68k_read_disassembler_32(unsigned int address)
369 return m68k_read_memory_32(address);
372 void jaguar_dasm(uint32 offset, uint32 qt)
375 static char buffer[2048];//, mem[64];
376 int pc = offset, oldpc;
378 for(uint32 i=0; i<qt; i++)
381 for(int j=0; j<64; j++)
382 mem[j^0x01] = jaguar_byte_read(pc + j);
384 pc += Dasm68000((char *)mem, buffer, 0);
385 WriteLog("%08X: %s\n", oldpc, buffer);//*/
387 pc += m68k_disassemble(buffer, pc, M68K_CPU_TYPE_68000);
388 WriteLog("%08X: %s\n", oldpc, buffer);//*/
393 unsigned jaguar_byte_read(unsigned int offset)
398 if (offset < 0x400000)
399 // data = (jaguar_mainRam[(offset^0x01) & 0x3FFFFF]);
400 data = jaguar_mainRam[offset & 0x3FFFFF];
401 else if ((offset >= 0x800000) && (offset < 0xC00000))
402 // data = (jaguar_mainRom[(offset^0x01)-0x800000]);
403 data = jaguar_mainRom[offset - 0x800000];
404 // else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
405 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
406 data = cdrom_byte_read(offset);
407 else if ((offset >= 0xE00000) && (offset < 0xE40000))
408 // data = (jaguar_bootRom[(offset^0x01) & 0x3FFFF]);
409 data = jaguar_bootRom[offset & 0x3FFFF];
410 else if ((offset >= 0xF00000) && (offset < 0xF10000))
411 data = tom_byte_read(offset);
412 else if ((offset >= 0xF10000) && (offset < 0xF20000))
413 data = jerry_byte_read(offset);
415 data = jaguar_unknown_readbyte(offset);
420 unsigned jaguar_word_read(unsigned int offset)
422 //TEMP--Mirror of F03000?
423 /*if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
424 WriteLog( "[JWR16] --> Possible GPU RAM mirror access! [%08X]\n", offset);//*/
427 if (offset <= 0x3FFFFE)
429 // return (jaguar_mainRam[(offset+1) & 0x3FFFFF] << 8) | jaguar_mainRam[(offset+0) & 0x3FFFFF];
430 return (jaguar_mainRam[(offset+0) & 0x3FFFFF] << 8) | jaguar_mainRam[(offset+1) & 0x3FFFFF];
432 else if ((offset >= 0x800000) && (offset <= 0xBFFFFE))
435 // return (jaguar_mainRom[offset+1] << 8) | jaguar_mainRom[offset+0];
436 return (jaguar_mainRom[offset+0] << 8) | jaguar_mainRom[offset+1];
438 // else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
439 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFE))
440 return cdrom_word_read(offset);
441 else if ((offset >= 0xE00000) && (offset <= 0xE3FFFE))
442 // return *((uint16 *)&jaguar_bootRom[offset & 0x3FFFF]);
443 return (jaguar_bootRom[(offset+0) & 0x3FFFF] << 8) | jaguar_bootRom[(offset+1) & 0x3FFFF];
444 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFE))
445 return tom_word_read(offset);
446 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFE))
448 //WriteLog("Reading from JERRY offset %08X...\n", offset);
449 return jerry_word_read(offset);
452 return jaguar_unknown_readword(offset);
455 void jaguar_byte_write(unsigned offset, unsigned data)
458 if (offset < 0x400000)
460 // jaguar_mainRam[(offset^0x01) & 0x3FFFFF] = data;
461 jaguar_mainRam[offset & 0x3FFFFF] = data;
464 // else if ((offset >= 0xDFFF00) && (offset < 0xDFFF00))
465 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFF))
467 cdrom_byte_write(offset, data);
470 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFF))
472 tom_byte_write(offset, data);
475 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFF))
477 jerry_byte_write(offset, data);
481 jaguar_unknown_writebyte(offset, data);
484 void jaguar_word_write(unsigned offset, unsigned data)
486 extern int dsp_pc;//, dsp_control;
487 if (offset == 0xF1A116 && (data & 0x01))
488 WriteLog(" JagWW: DSP is GO! (DSP_PC: %08X)\n", dsp_pc);
490 // WriteLog("JagWW: DSP halted... (Old value: %08X)\n", dsp_control);
492 //extern int blit_start_log;
493 //if (blit_start_log)
495 if (offset == 0x0674DE)
496 WriteLog( "[JWW16] Bad write starting @ 0674DE! [%04X]\n", data);
498 //TEMP--Mirror of F03000?
499 //if (offset >= 0xF0B000 && offset <= 0xF0BFFF)
500 //WriteLog( "[JWW16] --> Possible GPU RAM mirror access! [%08X]", offset);
501 //if ((offset >= 0x1FF020 && offset <= 0x1FF03F) || (offset >= 0x1FF820 && offset <= 0x1FF83F))
502 // WriteLog("JagWW: Writing %04X at %08X\n", data, offset);
505 if (offset <= 0x3FFFFE)
507 // jaguar_mainRam[(offset+0) & 0x3FFFFF] = data & 0xFF;
508 // jaguar_mainRam[(offset+1) & 0x3FFFFF] = (data>>8) & 0xFF;
509 jaguar_mainRam[(offset+0) & 0x3FFFFF] = (data>>8) & 0xFF;
510 jaguar_mainRam[(offset+1) & 0x3FFFFF] = data & 0xFF;
513 else if ((offset >= 0xDFFF00) && (offset <= 0xDFFFFE))
515 cdrom_word_write(offset, data);
518 else if ((offset >= 0xF00000) && (offset <= 0xF0FFFE))
520 tom_word_write(offset, data);
523 else if ((offset >= 0xF10000) && (offset <= 0xF1FFFE))
525 jerry_word_write(offset, data);
529 jaguar_unknown_writeword(offset, data);
532 unsigned jaguar_long_read(unsigned int offset)
534 return (jaguar_word_read(offset) << 16) | jaguar_word_read(offset+2);
537 void jaguar_long_write(unsigned offset, unsigned data)
539 extern int dsp_pc;//, dsp_control;
540 if (offset == 0xF1A114 && (data & 0x01))
541 WriteLog("JagLW: DSP is GO! (DSP_PC: %08X)\n", dsp_pc);
543 // WriteLog("JagLW: DSP halted... (Old value: %08X)\n", dsp_control);
545 jaguar_word_write(offset, data >> 16);
546 jaguar_word_write(offset+2, data & 0xFFFF);
550 // Jaguar console initialization
552 void jaguar_init(void)
554 memory_malloc_secure((void **)&jaguar_mainRam, 0x400000, "Jaguar 68K CPU RAM");
555 memory_malloc_secure((void **)&jaguar_bootRom, 0x040000, "Jaguar 68K CPU BIOS ROM");
556 memory_malloc_secure((void **)&jaguar_mainRom, 0x600000, "Jaguar 68K CPU ROM");
557 memset(jaguar_mainRam, 0x00, 0x400000);
558 // memset(jaguar_mainRom, 0xFF, 0x200000); // & set it to all Fs...
559 memset(jaguar_mainRom, 0x00, 0x200000); // & set it to all 0s...
561 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\Brain Dead 13.cdi");
562 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\baldies.cdi");
563 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\mystdemo.cdi");
564 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\battlemorph.cdi");
565 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\primalrage.cdi");
566 // cd_bios_boot("C:\\ftp\\jaguar\\cd\\Dragons Lair.cdi");
569 // GPU/DSP/video clock rate 26.590906 26.593900
570 // 68000 clock rate 13.295453 13.296950
571 // (clock rates in MHz)
573 // Should these be hardwired or read from VP?
574 jaguar_screen_scanlines = (hardwareTypeNTSC ? 524 : 624);
575 //Should the divisor be 50 for PAL??? Let's try it!
576 m68k_cycles_per_scanline = (hardwareTypeNTSC ? 13295453 : 13296950) / (jaguar_screen_scanlines * (hardwareTypeNTSC ? 60 : 50));
577 gpu_cycles_per_scanline = dsp_cycles_per_scanline
578 = (hardwareTypeNTSC ? 26590906 : 26593900) / (jaguar_screen_scanlines * (hardwareTypeNTSC ? 60 : 50));
584 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
592 void jaguar_done(void)
595 // for(int i=M68K_REG_A0; i<=M68K_REG_A7; i++)
596 // WriteLog("\tA%i = 0x%.8x\n", i-M68K_REG_A0, m68k_get_reg(NULL, (m68k_register_t)i));
597 int32 topOfStack = m68k_get_reg(NULL, M68K_REG_A7);
598 WriteLog("M68K: Top of stack: %08X. Stack trace:\n", jaguar_long_read(topOfStack));
599 for(int i=-2; i<9; i++)
600 WriteLog("%06X: %08X\n", topOfStack + (i * 4), jaguar_long_read(topOfStack + (i * 4)));
602 /* WriteLog("\nM68000 disassembly at $802288...\n");
603 jaguar_dasm(0x802288, 3);
604 WriteLog("\nM68000 disassembly at $802200...\n");
605 jaguar_dasm(0x802200, 500);
606 WriteLog("\nM68000 disassembly at $802518...\n");
607 jaguar_dasm(0x802518, 100);//*/
609 /* WriteLog("\n\nM68000 disassembly at $803F00 (look @ $803F2A)...\n");
610 jaguar_dasm(0x803F00, 500);
613 /* WriteLog("\n\nM68000 disassembly at $802B00 (look @ $802B5E)...\n");
614 jaguar_dasm(0x802B00, 500);
617 // WriteLog("Jaguar: CD BIOS version %04X\n", jaguar_word_read(0x3004));
618 WriteLog("Jaguar: Interrupt enable = %02X\n", tom_byte_read(0xF000E1) & 0x1F);
619 WriteLog("Jaguar: VBL interrupt is %s\n", ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64))) ? "enabled" : "disabled");
629 memory_free(jaguar_mainRom);
630 memory_free(jaguar_bootRom);
631 memory_free(jaguar_mainRam);
634 void jaguar_reset(void)
637 memcpy(jaguar_mainRam, jaguar_bootRom, 8);
640 SET32(jaguar_mainRam, 4, 0x00802000);
641 // Handle PD stuff...
642 // This should definitely go elsewhere (like in the cart load section)!
643 if (jaguar_mainRom[0] == 0x60 && jaguar_mainRom[1] == 0x1A)
645 uint32 runAddress = GET32(jaguar_mainRom, 0x2A);
646 uint32 progLength = GET32(jaguar_mainRom, 0x02);
647 WriteLog("Setting up PD ROM... Run address: %08X, length: %08X\n", runAddress, progLength);
648 memcpy(jaguar_mainRam + runAddress, jaguar_mainRom + 0x2E, progLength);
649 SET32(jaguar_mainRam, 4, runAddress);
653 // WriteLog("jaguar_reset():\n");
663 m68k_pulse_reset(); // Reset the 68000
664 WriteLog( "\t68K PC=%06X SP=%08X\n", m68k_get_reg(NULL, M68K_REG_PC), m68k_get_reg(NULL, M68K_REG_A7));
667 void jaguar_reset_handler(void)
671 void jaguar_exec(int16 * backbuffer, bool render)
673 uint32 i, vblank_duration = tom_get_vdb();
676 if ((tom_irq_enabled(IRQ_VBLANK)) && (jaguar_interrupt_handler_is_valid(64)))
678 if (jaguar_word_read(0xF0004E) != 0x07FF) // VI (11 bits wide!)
680 tom_set_pending_video_int();
681 // s68000interrupt(7, IRQ_VBLANK+64);
682 // s68000flushInterrupts();
683 m68k_set_irq(7); // IRQ_VBLANK+64??? Not autovectored??? No.
684 // Could set a global variable here, to signal that this is a VBLANK interrupt...
685 // Then again, since IRQ_VBLANK is set to zero, this would not be necessary in this case.
689 for(i=0; i<vblank_duration; i++)
691 /* uint32 invalid_instruction_address = s68000exec(m68k_cycles_per_scanline);
692 if (invalid_instruction_address != 0x80000000)
693 cd_bios_process(invalid_instruction_address);*/
694 m68k_execute(m68k_cycles_per_scanline);
695 // No CD handling... !!! FIX !!!
698 tom_pit_exec(m68k_cycles_per_scanline);
699 tom_exec_scanline(backbuffer, i, false);
700 jerry_pit_exec(m68k_cycles_per_scanline);
701 jerry_i2s_exec(m68k_cycles_per_scanline);
702 gpu_exec(gpu_cycles_per_scanline);
704 dsp_exec(dsp_cycles_per_scanline);
707 for (; i<jaguar_screen_scanlines; i++)
709 /* uint32 invalid_instruction_address = s68000exec(m68k_cycles_per_scanline);
710 if (invalid_instruction_address != 0x80000000)
711 cd_bios_process(invalid_instruction_address);*/
712 m68k_execute(m68k_cycles_per_scanline);
713 // No CD handling... !!! FIX !!!
715 tom_pit_exec(m68k_cycles_per_scanline);
716 jerry_pit_exec(m68k_cycles_per_scanline);
717 jerry_i2s_exec(m68k_cycles_per_scanline);
718 tom_exec_scanline(backbuffer, i, render);
719 gpu_exec(gpu_cycles_per_scanline);
721 dsp_exec(dsp_cycles_per_scanline);
722 backbuffer += tom_width;
725 system_sound_update();
730 // Main Jaguar execution loop (1 frame)
732 void JaguarExecute(int16 * backbuffer, bool render)
734 uint16 vp = tom_word_read(0xF0003E);//Hmm. This is a WO register. Will work? Looks like. But wrong behavior!
735 uint16 vi = tom_word_read(0xF0004E);//Another WO register...
736 uint16 vdb = tom_word_read(0xF00046);
737 // uint16 endingLine =
738 //Note: This is the *definite* end of the display, though VDE *might* be less than this...
739 // uint16 vbb = tom_word_read(0xF00040);
740 //It seems that they mean it when they say that VDE is the end of object processing.
741 //However, we need to be able to tell the OP (or TOM) that we've reached the end of the
742 //buffer and not to write any more pixels...
743 uint16 vde = tom_word_read(0xF00048);
745 /*extern int effect_start;
748 WriteLog("JagExe: VP=%u, VI=%u, VDB=%u, VBB=%u CPU CPS=%u, GPU CPS=%u\n", vp, vi, vdb, vbb, m68k_cycles_per_scanline, gpu_cycles_per_scanline);
751 for(uint16 i=0; i<vp; i++)
753 // Increment the horizontal count (why? RNG?)
754 tom_word_write(0xF00004, tom_word_read(0xF00004) + 1);
756 tom_word_write(0xF00006, i); // Write the VC
758 if (i == vi) // Time for Vertical Interrupt?
760 if (tom_irq_enabled(IRQ_VBLANK) && jaguar_interrupt_handler_is_valid(64))
762 // We don't have to worry about autovectors & whatnot because the Jaguar
763 // tells you through registers who sent the interrupt...
764 tom_set_pending_video_int();
769 // uint32 invalid_instruction_address = s68000exec(m68k_cycles_per_scanline);
770 // if (invalid_instruction_address != 0x80000000)
771 // cd_bios_process(invalid_instruction_address);
772 // These are divided by 2 because we're executing *half* lines...!
773 m68k_execute(m68k_cycles_per_scanline / 2);
774 // No CD handling... !!! FIX !!!
775 cd_bios_exec(i); // NOTE: Ignores parameter...
776 tom_pit_exec(m68k_cycles_per_scanline / 2);
777 jerry_pit_exec(m68k_cycles_per_scanline / 2);
778 jerry_i2s_exec(m68k_cycles_per_scanline / 2);
779 gpu_exec(gpu_cycles_per_scanline / 2);
781 dsp_exec(dsp_cycles_per_scanline / 2);
783 //Interlacing is still not handled correctly here...
784 if (i >= vdb && i < vde)//vbb)
786 if (!(i & 0x01)) // Execute OP only on even lines (non-interlaced only!)
788 tom_exec_scanline(backbuffer, i/2, render); // i/2 is a kludge...
789 backbuffer += TOMGetSDLScreenPitch() / 2; // Convert bytes to words...
795 system_sound_update();
799 // Temp debugging stuff
801 void DumpMainMemory(void)
803 FILE * fp = fopen("./memdump.bin", "wb");
808 fwrite(jaguar_mainRam, 1, 0x400000, fp);
812 uint8 * GetRamPtr(void)
814 return jaguar_mainRam;