z80/ym2612 reset handling improved
[picodrive.git] / Pico / Sek.c
... / ...
CommitLineData
1// This is part of Pico Library\r
2\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
6\r
7// For commercial use, separate licencing terms must be obtained.\r
8\r
9\r
10#include "PicoInt.h"\r
11\r
12\r
13int SekCycleCnt=0; // cycles done in this frame\r
14int SekCycleAim=0; // cycle aim\r
15unsigned int SekCycleCntT=0;\r
16\r
17\r
18/* context */\r
19// Cyclone 68000\r
20#ifdef EMU_C68K\r
21struct Cyclone PicoCpuCM68k;\r
22#endif\r
23// MUSASHI 68000\r
24#ifdef EMU_M68K\r
25m68ki_cpu_core PicoCpuMM68k;\r
26#endif\r
27// FAME 68000\r
28#ifdef EMU_F68K\r
29M68K_CONTEXT PicoCpuFM68k;\r
30#endif\r
31\r
32\r
33/* callbacks */\r
34#ifdef EMU_C68K\r
35// interrupt acknowledgment\r
36static int SekIntAck(int level)\r
37{\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
43}\r
44\r
45static void SekResetAck(void)\r
46{\r
47 elprintf(EL_ANOMALY, "Reset encountered @ %06x", SekPc);\r
48}\r
49\r
50static int SekUnrecognizedOpcode()\r
51{\r
52 unsigned int pc, op;\r
53 pc = SekPc;\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
60 return 1;\r
61 }\r
62#ifdef EMU_M68K // debugging cyclone\r
63 {\r
64 extern int have_illegal;\r
65 have_illegal = 1;\r
66 }\r
67#endif\r
68 return 0;\r
69}\r
70#endif\r
71\r
72\r
73#ifdef EMU_M68K\r
74static int SekIntAckM68K(int level)\r
75{\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
78 CPU_INT_LEVEL = 0;\r
79 return M68K_INT_ACK_AUTOVECTOR;\r
80}\r
81\r
82static int SekTasCallback(void)\r
83{\r
84 return 0; // no writeback\r
85}\r
86#endif\r
87\r
88\r
89#ifdef EMU_F68K\r
90static void SekIntAckF68K(unsigned level)\r
91{\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
95}\r
96#endif\r
97\r
98\r
99PICO_INTERNAL void SekInit(void)\r
100{\r
101#ifdef EMU_C68K\r
102 CycloneInit();\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
108#endif\r
109#ifdef EMU_M68K\r
110 {\r
111 void *oldcontext = m68ki_cpu_p;\r
112 m68k_set_context(&PicoCpuMM68k);\r
113 m68k_set_cpu_type(M68K_CPU_TYPE_68000);\r
114 m68k_init();\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
119 }\r
120#endif\r
121#ifdef EMU_F68K\r
122 {\r
123 void *oldcontext = g_m68kcontext;\r
124 g_m68kcontext = &PicoCpuFM68k;\r
125 memset(&PicoCpuFM68k, 0, sizeof(PicoCpuFM68k));\r
126 fm68k_init();\r
127 PicoCpuFM68k.iack_handler = SekIntAckF68K;\r
128 PicoCpuFM68k.sr = 0x2704; // Z flag\r
129 g_m68kcontext = oldcontext;\r
130 }\r
131#endif\r
132}\r
133\r
134\r
135// Reset the 68000:\r
136PICO_INTERNAL int SekReset(void)\r
137{\r
138 if (Pico.rom==NULL) return 1;\r
139\r
140#ifdef EMU_C68K\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
148#endif\r
149#ifdef EMU_M68K\r
150 m68k_set_context(&PicoCpuMM68k); // if we ever reset m68k, we always need it's context to be set\r
151 m68ki_cpu.sp[0]=0;\r
152 m68k_set_irq(0);\r
153 m68k_pulse_reset();\r
154 REG_USP = 0; // ?\r
155#endif\r
156#ifdef EMU_F68K\r
157 {\r
158 g_m68kcontext = &PicoCpuFM68k;\r
159 fm68k_reset();\r
160 }\r
161#endif\r
162\r
163 return 0;\r
164}\r
165\r
166\r
167PICO_INTERNAL void SekSetRealTAS(int use_real)\r
168{\r
169#ifdef EMU_C68K\r
170 CycloneSetRealTAS(use_real);\r
171#endif\r
172#ifdef EMU_F68K\r
173 // TODO\r
174#endif\r
175}\r
176\r
177/* idle loop detection, not to be used in CD mode */\r
178#ifdef EMU_C68K\r
179#include "cpu/Cyclone/tools/idle.h"\r
180#endif\r
181\r
182static int *idledet_addrs = NULL;\r
183static int idledet_count = 0, idledet_bads = 0;\r
184int idledet_start_frame = 0;\r
185\r
186void SekInitIdleDet(void)\r
187{\r
188 void *tmp = realloc(idledet_addrs, 0x200*4);\r
189 if (tmp == NULL) {\r
190 free(idledet_addrs);\r
191 idledet_addrs = NULL;\r
192 }\r
193 else\r
194 idledet_addrs = tmp;\r
195 idledet_count = idledet_bads = 0;\r
196 idledet_start_frame = Pico.m.frame_count + 360;\r
197\r
198#ifdef EMU_C68K\r
199 CycloneInitIdle();\r
200#endif\r
201#ifdef EMU_F68K\r
202 fm68k_emulate(0, 0, 1);\r
203#endif\r
204}\r
205\r
206int SekIsIdleCode(unsigned short *dst, int bytes)\r
207{\r
208 // printf("SekIsIdleCode %04x %i\n", *dst, bytes);\r
209 switch (bytes)\r
210 {\r
211 case 4:\r
212 if ( (*dst & 0xfff8) == 0x4a10 || // tst.b ($aX) // there should be no need to wait\r
213 (*dst & 0xfff8) == 0x4a28 || // tst.b ($xxxx,a0) // for byte change anywhere\r
214 (*dst & 0xff3f) == 0x4a38 || // tst.x ($xxxx.w); tas ($xxxx.w)\r
215 (*dst & 0xc1ff) == 0x0038 || // move.x ($xxxx.w), dX\r
216 (*dst & 0xf13f) == 0xb038) // cmp.x ($xxxx.w), dX\r
217 return 1;\r
218 break;\r
219 case 6:\r
220 if ( ((dst[1] & 0xe0) == 0xe0 && ( // RAM and\r
221 *dst == 0x4a39 || // tst.b ($xxxxxxxx)\r
222 *dst == 0x4a79 || // tst.w ($xxxxxxxx)\r
223 *dst == 0x4ab9 || // tst.l ($xxxxxxxx)\r
224 (*dst & 0xc1ff) == 0x0039 || // move.x ($xxxxxxxx), dX\r
225 (*dst & 0xf13f) == 0xb039))||// cmp.x ($xxxxxxxx), dX\r
226 *dst == 0x0838 || // btst $X, ($xxxx.w) [6 byte op]\r
227 (*dst & 0xffbf) == 0x0c38) // cmpi.{b,w} $X, ($xxxx.w)\r
228 return 1;\r
229 break;\r
230 case 8:\r
231 if ( ((dst[2] & 0xe0) == 0xe0 && ( // RAM and\r
232 *dst == 0x0839 || // btst $X, ($xxxxxxxx.w) [8 byte op]\r
233 (*dst & 0xffbf) == 0x0c39))||// cmpi.{b,w} $X, ($xxxxxxxx)\r
234 *dst == 0x0cb8) // cmpi.l $X, ($xxxx.w)\r
235 return 1;\r
236 break;\r
237 case 12:\r
238 if ((*dst & 0xf1f8) == 0x3010 && // move.w (aX), dX\r
239 (dst[1]&0xf100) == 0x0000 && // arithmetic\r
240 (dst[3]&0xf100) == 0x0000) // arithmetic\r
241 return 1;\r
242 break;\r
243 }\r
244\r
245 return 0;\r
246}\r
247\r
248int SekRegisterIdlePatch(unsigned int pc, int oldop, int newop)\r
249{\r
250#ifdef EMU_C68K\r
251 pc -= PicoCpuCM68k.membase;\r
252#endif\r
253 pc &= ~0xff000000;\r
254 elprintf(EL_IDLE, "idle: patch %06x %04x %04x #%i", pc, oldop, newop, idledet_count);\r
255 if (pc > Pico.romsize && !(PicoAHW & PAHW_SVP)) {\r
256 if (++idledet_bads > 128) return 2; // remove detector\r
257 return 1; // don't patch\r
258 }\r
259\r
260 if (idledet_count >= 0x200 && (idledet_count & 0x1ff) == 0) {\r
261 void *tmp = realloc(idledet_addrs, (idledet_count+0x200)*4);\r
262 if (tmp == NULL) return 1;\r
263 idledet_addrs = tmp;\r
264 }\r
265\r
266 if (pc < Pico.romsize)\r
267 idledet_addrs[idledet_count++] = pc;\r
268\r
269 return 0;\r
270}\r
271\r
272void SekFinishIdleDet(void)\r
273{\r
274#ifdef EMU_C68K\r
275 CycloneFinishIdle();\r
276#endif\r
277#ifdef EMU_F68K\r
278 fm68k_emulate(0, 0, 2);\r
279#endif\r
280 while (idledet_count > 0)\r
281 {\r
282 unsigned short *op = (unsigned short *)&Pico.rom[idledet_addrs[--idledet_count]];\r
283 if ((*op & 0xfd00) == 0x7100)\r
284 *op &= 0xff, *op |= 0x6600;\r
285 else if ((*op & 0xfd00) == 0x7500)\r
286 *op &= 0xff, *op |= 0x6700;\r
287 else if ((*op & 0xfd00) == 0x7d00)\r
288 *op &= 0xff, *op |= 0x6000;\r
289 else\r
290 elprintf(EL_STATUS|EL_IDLE, "idle: don't know how to restore %04x", *op);\r
291 }\r
292}\r
293\r
294\r
295#if defined(EMU_M68K) && M68K_INSTRUCTION_HOOK == OPT_SPECIFY_HANDLER\r
296static unsigned char op_flags[0x400000/2] = { 0, };\r
297static int atexit_set = 0;\r
298\r
299static void make_idc(void)\r
300{\r
301 FILE *f = fopen("idc.idc", "w");\r
302 int i;\r
303 if (!f) return;\r
304 fprintf(f, "#include <idc.idc>\nstatic main() {\n");\r
305 for (i = 0; i < 0x400000/2; i++)\r
306 if (op_flags[i] != 0)\r
307 fprintf(f, " MakeCode(0x%06x);\n", i*2);\r
308 fprintf(f, "}\n");\r
309 fclose(f);\r
310}\r
311\r
312void instruction_hook(void)\r
313{\r
314 if (!atexit_set) {\r
315 atexit(make_idc);\r
316 atexit_set = 1;\r
317 }\r
318 if (REG_PC < 0x400000)\r
319 op_flags[REG_PC/2] = 1;\r
320}\r
321#endif\r