1 // This is part of Pico Library
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006 notaz, All rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
10 #include "PicoInt.h"
\r
13 int SekCycleCnt=0; // cycles done in this frame
\r
14 int SekCycleAim=0; // cycle aim
\r
15 unsigned int SekCycleCntT=0;
\r
21 struct Cyclone PicoCpuCM68k;
\r
25 m68ki_cpu_core PicoCpuMM68k;
\r
29 M68K_CONTEXT PicoCpuFM68k;
\r
35 // interrupt acknowledgment
\r
36 static int SekIntAck(int level)
\r
38 // try to emulate VDP's reaction to 68000 int ack
\r
39 if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
40 else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
41 PicoCpuCM68k.irq = 0;
\r
42 return CYCLONE_INT_ACK_AUTOVECTOR;
\r
45 static void SekResetAck(void)
\r
47 elprintf(EL_ANOMALY, "Reset encountered @ %06x", SekPc);
\r
50 static int SekUnrecognizedOpcode()
\r
52 unsigned int pc, op;
\r
54 op = PicoCpuCM68k.read16(pc);
\r
55 elprintf(EL_ANOMALY, "Unrecognized Opcode %04x @ %06x", op, pc);
\r
56 // see if we are not executing trash
\r
57 if (pc < 0x200 || (pc > Pico.romsize+4 && (pc&0xe00000)!=0xe00000)) {
\r
58 PicoCpuCM68k.cycles = 0;
\r
59 PicoCpuCM68k.state_flags |= 1;
\r
62 #ifdef EMU_M68K // debugging cyclone
\r
64 extern int have_illegal;
\r
74 static int SekIntAckM68K(int level)
\r
76 if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
77 else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
79 return M68K_INT_ACK_AUTOVECTOR;
\r
82 static int SekTasCallback(void)
\r
84 return 0; // no writeback
\r
90 static void SekIntAckF68K(unsigned level)
\r
92 if (level == 4) { Pico.video.pending_ints = 0; elprintf(EL_INTS, "hack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
93 else if(level == 6) { Pico.video.pending_ints &= ~0x20; elprintf(EL_INTS, "vack: @ %06x [%i]", SekPc, SekCycleCnt); }
\r
94 PicoCpuFM68k.interrupts[0] = 0;
\r
99 PICO_INTERNAL void SekInit(void)
\r
103 memset(&PicoCpuCM68k,0,sizeof(PicoCpuCM68k));
\r
104 PicoCpuCM68k.IrqCallback=SekIntAck;
\r
105 PicoCpuCM68k.ResetCallback=SekResetAck;
\r
106 PicoCpuCM68k.UnrecognizedCallback=SekUnrecognizedOpcode;
\r
107 PicoCpuCM68k.flags=4; // Z set
\r
111 void *oldcontext = m68ki_cpu_p;
\r
112 m68k_set_context(&PicoCpuMM68k);
\r
113 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
\r
115 m68k_set_int_ack_callback(SekIntAckM68K);
\r
116 m68k_set_tas_instr_callback(SekTasCallback);
\r
117 //m68k_pulse_reset();
\r
118 m68k_set_context(oldcontext);
\r
123 void *oldcontext = g_m68kcontext;
\r
124 g_m68kcontext = &PicoCpuFM68k;
\r
125 memset(&PicoCpuFM68k, 0, sizeof(PicoCpuFM68k));
\r
127 PicoCpuFM68k.iack_handler = SekIntAckF68K;
\r
128 PicoCpuFM68k.sr = 0x2704; // Z flag
\r
129 g_m68kcontext = oldcontext;
\r
135 // Reset the 68000:
\r
136 PICO_INTERNAL int SekReset(void)
\r
138 if (Pico.rom==NULL) return 1;
\r
141 PicoCpuCM68k.state_flags=0;
\r
142 PicoCpuCM68k.osp=0;
\r
143 PicoCpuCM68k.srh =0x27; // Supervisor mode
\r
144 PicoCpuCM68k.irq=0;
\r
145 PicoCpuCM68k.a[7]=PicoCpuCM68k.read32(0); // Stack Pointer
\r
146 PicoCpuCM68k.membase=0;
\r
147 PicoCpuCM68k.pc=PicoCpuCM68k.checkpc(PicoCpuCM68k.read32(4)); // Program Counter
\r
150 m68k_set_context(&PicoCpuMM68k); // if we ever reset m68k, we always need it's context to be set
\r
153 m68k_pulse_reset();
\r
158 g_m68kcontext = &PicoCpuFM68k;
\r
167 PICO_INTERNAL void SekSetRealTAS(int use_real)
\r
170 CycloneSetRealTAS(use_real);
\r
177 /* idle loop detection, not to be used in CD mode */
\r
179 #include "cpu/Cyclone/tools/idle.h"
\r
182 static int *idledet_addrs = NULL;
\r
183 static int idledet_count = 0, idledet_bads = 0;
\r
184 int idledet_start_frame = 0;
\r
186 static unsigned char *rom_verify = NULL;
\r
188 void SekInitIdleDet(void)
\r
190 void *tmp = realloc(idledet_addrs, 0x200*4);
\r
192 free(idledet_addrs);
\r
193 idledet_addrs = NULL;
\r
196 idledet_addrs = tmp;
\r
197 idledet_count = idledet_bads = 0;
\r
198 idledet_start_frame = Pico.m.frame_count + 360;
\r
200 rom_verify = realloc(rom_verify, Pico.romsize);
\r
201 memcpy(rom_verify, Pico.rom, Pico.romsize);
\r
207 int SekIsIdleCode(unsigned short *dst, int bytes)
\r
209 printf("SekIsIdleCode %04x %i\n", *dst, bytes);
\r
213 if ( (*dst & 0xfff8) == 0x4a10 || // tst.b ($aX) // there should be no need to wait
\r
214 (*dst & 0xfff8) == 0x4a28 || // tst.b ($xxxx,a0) // for byte change anywhere
\r
215 (*dst & 0xff3f) == 0x4a38 || // tst.x ($xxxx.w); tas ($xxxx.w)
\r
216 (*dst & 0xc1ff) == 0x0038 || // move.x ($xxxx.w), dX
\r
217 (*dst & 0xf13f) == 0xb038) // cmp.x ($xxxx.w), dX
\r
221 if ( ((dst[1] & 0xe0) == 0xe0 && ( // RAM and
\r
222 *dst == 0x4a39 || // tst.b ($xxxxxxxx)
\r
223 *dst == 0x4a79 || // tst.w ($xxxxxxxx)
\r
224 *dst == 0x4ab9 || // tst.l ($xxxxxxxx)
\r
225 (*dst & 0xc1ff) == 0x0039 || // move.x ($xxxxxxxx), dX
\r
226 (*dst & 0xf13f) == 0xb039))||// cmp.x ($xxxxxxxx), dX
\r
227 *dst == 0x0838 || // btst $X, ($xxxx.w) [6 byte op]
\r
228 (*dst & 0xffbf) == 0x0c38) // cmpi.{b,w} $X, ($xxxx.w)
\r
232 if ( ((dst[2] & 0xe0) == 0xe0 && ( // RAM and
\r
233 *dst == 0x0839 || // btst $X, ($xxxxxxxx.w) [8 byte op]
\r
234 (*dst & 0xffbf) == 0x0c39))||// cmpi.{b,w} $X, ($xxxxxxxx)
\r
235 *dst == 0x0cb8) // cmpi.l $X, ($xxxx.w)
\r
239 if ((*dst & 0xf1f8) == 0x3010 && // move.w (aX), dX
\r
240 (dst[1]&0xf100) == 0x0000 && // arithmetic
\r
241 (dst[3]&0xf100) == 0x0000) // arithmetic
\r
249 int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop)
\r
252 pc -= PicoCpuCM68k.membase;
\r
255 elprintf(EL_IDLE, "idle: patch %06x %04x %04x #%i", pc, oldop, newop, idledet_count);
\r
256 if (pc > Pico.romsize && !(PicoAHW & PAHW_SVP)) {
\r
257 if (++idledet_bads > 128) return 2; // remove detector
\r
258 return 1; // don't patch
\r
261 if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) {
\r
262 void *tmp = realloc(idledet_addrs, (idledet_count+0x200)*4);
\r
263 if (tmp == NULL) return 1;
\r
264 idledet_addrs = tmp;
\r
267 if (pc < Pico.romsize)
\r
268 idledet_addrs[idledet_count++] = pc;
\r
273 void SekFinishIdleDet(void)
\r
275 int done_something = idledet_count > 0;
\r
277 CycloneFinishIdle();
\r
279 while (idledet_count > 0)
\r
281 unsigned short *op = (unsigned short *)&Pico.rom[idledet_addrs[--idledet_count]];
\r
282 if ((*op & 0xfd00) == 0x7100)
\r
283 *op &= 0xff, *op |= 0x6600;
\r
284 else if ((*op & 0xfd00) == 0x7500)
\r
285 *op &= 0xff, *op |= 0x6700;
\r
286 else if ((*op & 0xfd00) == 0x7d00)
\r
287 *op &= 0xff, *op |= 0x6000;
\r
289 elprintf(EL_STATUS|EL_IDLE, "idle: don't know how to restore %04x", *op);
\r
292 if (done_something)
\r
295 for (i = 0; i < Pico.romsize; i++)
\r
296 if (rom_verify[i] != Pico.rom[i])
\r
297 printf("ROM corruption @ %06x!\n", i), exit(1);
\r
302 #if defined(EMU_M68K) && M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
\r
303 static unsigned char op_flags[0x400000/2] = { 0, };
\r
304 static int atexit_set = 0;
\r
306 static void make_idc(void)
\r
308 FILE *f = fopen("idc.idc", "w");
\r
311 fprintf(f, "#include <idc.idc>\nstatic main() {\n");
\r
312 for (i = 0; i < 0x400000/2; i++)
\r
313 if (op_flags[i] != 0)
\r
314 fprintf(f, " MakeCode(0x%06x);\n", i*2);
\r
319 void instruction_hook(void)
\r
325 if (REG_PC < 0x400000)
\r
326 op_flags[REG_PC/2] = 1;
\r