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
166 void SekStepM68k(void)
\r
168 SekCycleAim=SekCycleCnt+1;
\r
169 #if defined(EMU_CORE_DEBUG)
\r
170 SekCycleCnt+=CM_compareRun(1, 0);
\r
171 #elif defined(EMU_C68K)
\r
172 PicoCpuCM68k.cycles=1;
\r
173 CycloneRun(&PicoCpuCM68k);
\r
174 SekCycleCnt+=1-PicoCpuCM68k.cycles;
\r
175 #elif defined(EMU_M68K)
\r
176 SekCycleCnt+=m68k_execute(1);
\r
177 #elif defined(EMU_F68K)
\r
178 SekCycleCnt+=fm68k_emulate(1, 0, 0);
\r
182 PICO_INTERNAL void SekSetRealTAS(int use_real)
\r
185 CycloneSetRealTAS(use_real);
\r
193 /* idle loop detection, not to be used in CD mode */
\r
195 #include "cpu/Cyclone/tools/idle.h"
\r
198 static int *idledet_addrs = NULL;
\r
199 static int idledet_count = 0, idledet_bads = 0;
\r
200 int idledet_start_frame = 0;
\r
202 void SekInitIdleDet(void)
\r
204 void *tmp = realloc(idledet_addrs, 0x200*4);
\r
206 free(idledet_addrs);
\r
207 idledet_addrs = NULL;
\r
210 idledet_addrs = tmp;
\r
211 idledet_count = idledet_bads = 0;
\r
212 idledet_start_frame = Pico.m.frame_count + 360;
\r
218 fm68k_emulate(0, 0, 1);
\r
222 int SekIsIdleCode(unsigned short *dst, int bytes)
\r
224 // printf("SekIsIdleCode %04x %i\n", *dst, bytes);
\r
228 if ( (*dst & 0xfff8) == 0x4a10 || // tst.b ($aX) // there should be no need to wait
\r
229 (*dst & 0xfff8) == 0x4a28 || // tst.b ($xxxx,a0) // for byte change anywhere
\r
230 (*dst & 0xff3f) == 0x4a38 || // tst.x ($xxxx.w); tas ($xxxx.w)
\r
231 (*dst & 0xc1ff) == 0x0038 || // move.x ($xxxx.w), dX
\r
232 (*dst & 0xf13f) == 0xb038) // cmp.x ($xxxx.w), dX
\r
236 if ( ((dst[1] & 0xe0) == 0xe0 && ( // RAM and
\r
237 *dst == 0x4a39 || // tst.b ($xxxxxxxx)
\r
238 *dst == 0x4a79 || // tst.w ($xxxxxxxx)
\r
239 *dst == 0x4ab9 || // tst.l ($xxxxxxxx)
\r
240 (*dst & 0xc1ff) == 0x0039 || // move.x ($xxxxxxxx), dX
\r
241 (*dst & 0xf13f) == 0xb039))||// cmp.x ($xxxxxxxx), dX
\r
242 *dst == 0x0838 || // btst $X, ($xxxx.w) [6 byte op]
\r
243 (*dst & 0xffbf) == 0x0c38) // cmpi.{b,w} $X, ($xxxx.w)
\r
247 if ( ((dst[2] & 0xe0) == 0xe0 && ( // RAM and
\r
248 *dst == 0x0839 || // btst $X, ($xxxxxxxx.w) [8 byte op]
\r
249 (*dst & 0xffbf) == 0x0c39))||// cmpi.{b,w} $X, ($xxxxxxxx)
\r
250 *dst == 0x0cb8) // cmpi.l $X, ($xxxx.w)
\r
254 if ((*dst & 0xf1f8) == 0x3010 && // move.w (aX), dX
\r
255 (dst[1]&0xf100) == 0x0000 && // arithmetic
\r
256 (dst[3]&0xf100) == 0x0000) // arithmetic
\r
264 int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop)
\r
267 pc -= PicoCpuCM68k.membase;
\r
270 elprintf(EL_IDLE, "idle: patch %06x %04x %04x #%i", pc, oldop, newop, idledet_count);
\r
271 if (pc > Pico.romsize && !(PicoAHW & PAHW_SVP)) {
\r
272 if (++idledet_bads > 128) return 2; // remove detector
\r
273 return 1; // don't patch
\r
276 if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) {
\r
277 void *tmp = realloc(idledet_addrs, (idledet_count+0x200)*4);
\r
278 if (tmp == NULL) return 1;
\r
279 idledet_addrs = tmp;
\r
282 if (pc < Pico.romsize)
\r
283 idledet_addrs[idledet_count++] = pc;
\r
288 void SekFinishIdleDet(void)
\r
291 CycloneFinishIdle();
\r
294 fm68k_emulate(0, 0, 2);
\r
296 while (idledet_count > 0)
\r
298 unsigned short *op = (unsigned short *)&Pico.rom[idledet_addrs[--idledet_count]];
\r
299 if ((*op & 0xfd00) == 0x7100)
\r
300 *op &= 0xff, *op |= 0x6600;
\r
301 else if ((*op & 0xfd00) == 0x7500)
\r
302 *op &= 0xff, *op |= 0x6700;
\r
303 else if ((*op & 0xfd00) == 0x7d00)
\r
304 *op &= 0xff, *op |= 0x6000;
\r
306 elprintf(EL_STATUS|EL_IDLE, "idle: don't know how to restore %04x", *op);
\r
311 #if defined(EMU_M68K) && M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
\r
312 static unsigned char op_flags[0x400000/2] = { 0, };
\r
313 static int atexit_set = 0;
\r
315 static void make_idc(void)
\r
317 FILE *f = fopen("idc.idc", "w");
\r
320 fprintf(f, "#include <idc.idc>\nstatic main() {\n");
\r
321 for (i = 0; i < 0x400000/2; i++)
\r
322 if (op_flags[i] != 0)
\r
323 fprintf(f, " MakeCode(0x%06x);\n", i*2);
\r
328 void instruction_hook(void)
\r
334 if (REG_PC < 0x400000)
\r
335 op_flags[REG_PC/2] = 1;
\r