eeprom crash fix, PacMan2 hack
[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 int SekInit()\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(); // Init cpu emulator\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 return 0;\r
134}\r
135\r
136\r
137// Reset the 68000:\r
138PICO_INTERNAL int SekReset()\r
139{\r
140 if (Pico.rom==NULL) return 1;\r
141\r
142#ifdef EMU_C68K\r
143 PicoCpuCM68k.state_flags=0;\r
144 PicoCpuCM68k.osp=0;\r
145 PicoCpuCM68k.srh =0x27; // Supervisor mode\r
146 PicoCpuCM68k.irq=0;\r
147 PicoCpuCM68k.a[7]=PicoCpuCM68k.read32(0); // Stack Pointer\r
148 PicoCpuCM68k.membase=0;\r
149 PicoCpuCM68k.pc=PicoCpuCM68k.checkpc(PicoCpuCM68k.read32(4)); // Program Counter\r
150#endif\r
151#ifdef EMU_M68K\r
152 m68k_set_context(&PicoCpuMM68k); // if we ever reset m68k, we always need it's context to be set\r
153 m68ki_cpu.sp[0]=0;\r
154 m68k_set_irq(0);\r
155 m68k_pulse_reset();\r
156 REG_USP = 0; // ?\r
157#endif\r
158#ifdef EMU_F68K\r
159 {\r
160 g_m68kcontext = &PicoCpuFM68k;\r
161 fm68k_reset();\r
162 }\r
163#endif\r
164\r
165 return 0;\r
166}\r
167\r
168\r
169// data must be word aligned\r
170PICO_INTERNAL void SekState(int *data)\r
171{\r
172#ifdef EMU_C68K\r
173 memcpy32(data,(int *)PicoCpuCM68k.d,0x44/4);\r
174 data[0x11] = PicoCpuCM68k.flags;\r
175#elif defined(EMU_M68K)\r
176 memcpy32(data, (int *)PicoCpuMM68k.dar, 0x40/4);\r
177 data[0x10] = PicoCpuMM68k.pc;\r
178 data[0x11] = m68k_get_reg(&PicoCpuMM68k, M68K_REG_SR);\r
179#elif defined(EMU_F68K)\r
180 memcpy32(data, (int *)PicoCpuFM68k.dreg, 0x40/4);\r
181 data[0x10] = PicoCpuFM68k.pc;\r
182 data[0x11] = PicoCpuFM68k.sr;\r
183#endif\r
184}\r
185\r
186PICO_INTERNAL void SekSetRealTAS(int use_real)\r
187{\r
188#ifdef EMU_C68K\r
189 CycloneSetRealTAS(use_real);\r
190#endif\r
191#ifdef EMU_F68K\r
192 // TODO\r
193#endif\r
194}\r
195\r