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) // where 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
\r
222 *dst == 0x4a39 || // tst.b ($xxxxxxxx)
\r
223 *dst == 0x4a79 || // tst.w ($xxxxxxxx)
\r
224 *dst == 0x4ab9)) || // tst.l ($xxxxxxxx)
\r
225 *dst == 0x0838) // btst $X, ($xxxx.w) [6 byte op]
\r
229 if ( (dst[2] & 0xe0) == 0xe0 && ( // RAM
\r
230 *dst == 0x0839 || // btst $X, ($xxxxxxxx.w) [8 byte op]
\r
231 (*dst & 0xffbf) == 0x0c39)) // cmpi.{b,w} $X, ($xxxxxxxx)
\r
235 if ((*dst & 0xf1f8) == 0x3010 && // move.w (aX), dX
\r
236 (dst[1]&0xf100) == 0x0000 && // arithmetic
\r
237 (dst[3]&0xf100) == 0x0000) // arithmetic
\r
245 int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop)
\r
248 pc -= PicoCpuCM68k.membase;
\r
251 elprintf(EL_IDLE, "idle: patch %06x %04x %04x #%i", pc, oldop, newop, idledet_count);
\r
252 if (pc > Pico.romsize) {
\r
253 if (++idledet_bads > 128) return 2; // remove detector
\r
254 return 1; // don't patch
\r
257 if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) {
\r
258 void *tmp = realloc(idledet_addrs, (idledet_count+0x200)*4);
\r
259 if (tmp == NULL) return 1;
\r
260 idledet_addrs = tmp;
\r
263 idledet_addrs[idledet_count++] = pc;
\r
267 void SekFinishIdleDet(void)
\r
269 int done_something = idledet_count > 0;
\r
271 CycloneFinishIdle();
\r
273 while (idledet_count > 0)
\r
275 unsigned short *op = (unsigned short *)&Pico.rom[idledet_addrs[--idledet_count]];
\r
276 if ((*op & 0xfd00) == 0x7100)
\r
277 *op &= 0xff, *op |= 0x6600;
\r
278 else if ((*op & 0xfd00) == 0x7500)
\r
279 *op &= 0xff, *op |= 0x6700;
\r
280 else if ((*op & 0xfd00) == 0x7d00)
\r
281 *op &= 0xff, *op |= 0x6000;
\r
283 elprintf(EL_STATUS|EL_IDLE, "idle: don't know how to restore %04x", *op);
\r
286 if (done_something)
\r
289 for (i = 0; i < Pico.romsize; i++)
\r
290 if (rom_verify[i] != Pico.rom[i])
\r
291 printf("ROM corruption @ %06x!\n", i), exit(1);
\r
296 #if defined(EMU_M68K) && M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER
\r
297 static unsigned char op_flags[0x400000/2] = { 0, };
\r
298 static int atexit_set = 0;
\r
300 static void make_idc(void)
\r
302 FILE *f = fopen("idc.idc", "w");
\r
305 fprintf(f, "#include <idc.idc>\nstatic main() {\n");
\r
306 for (i = 0; i < 0x400000/2; i++)
\r
307 if (op_flags[i] != 0)
\r
308 fprintf(f, " MakeCode(0x%06x);\n", i*2);
\r
313 void instruction_hook(void)
\r
319 if (REG_PC < 0x400000)
\r
320 op_flags[REG_PC/2] = 1;
\r