initial import
[picodrive.git] / Pico / Sek.c
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
13 int SekCycleCnt=0; // cycles done in this frame\r
14 int SekCycleAim=0; // cycle aim\r
15 unsigned int SekCycleCntT=0;\r
16 \r
17 #ifdef EMU_C68K\r
18 // ---------------------- Cyclone 68000 ----------------------\r
19 struct Cyclone PicoCpu;\r
20 #endif\r
21 \r
22 #ifdef EMU_M68K\r
23 // ---------------------- MUSASHI 68000 ----------------------\r
24 m68ki_cpu_core PicoM68kCPU; // MD's CPU\r
25 #endif\r
26 \r
27 #ifdef EMU_A68K\r
28 // ---------------------- A68K ----------------------\r
29 \r
30 void __cdecl M68000_RESET();\r
31 int m68k_ICount=0;\r
32 unsigned int mem_amask=0xffffff; // 24-bit bus\r
33 unsigned int mame_debug=0,cur_mrhard=0,m68k_illegal_opcode=0,illegal_op=0,illegal_pc=0,opcode_entry=0; // filler\r
34 \r
35 static int IrqCallback(int i) { i; return -1; }\r
36 static int DoReset() { return 0; }\r
37 static int (*ResetCallback)()=DoReset;\r
38 \r
39 #pragma warning (disable:4152)\r
40 #endif\r
41 \r
42 \r
43 \r
44 // interrupt acknowledgment\r
45 #ifdef EMU_C68K\r
46 static void SekIntAck(int level)\r
47 {\r
48   // try to emulate VDP's reaction to 68000 int ack\r
49   if     (level == 4) Pico.video.pending_ints  =  0;\r
50   else if(level == 6) Pico.video.pending_ints &= ~0x20;\r
51   PicoCpu.irq = 0;\r
52 }\r
53 \r
54 static void SekResetAck()\r
55 {\r
56 #if defined(__DEBUG_PRINT) || defined(WIN32)\r
57   dprintf("Reset encountered @ %06x", SekPc);\r
58 #endif\r
59 }\r
60 \r
61 static int SekUnrecognizedOpcode()\r
62 {\r
63   unsigned int pc, op;\r
64   pc = SekPc;\r
65   op = PicoCpu.read16(pc);\r
66 #if defined(__DEBUG_PRINT) || defined(WIN32)\r
67   dprintf("Unrecognized Opcode %04x @ %06x", op, pc);\r
68 #endif\r
69   // see if we are not executing trash\r
70   if (pc < 0x200 || (pc > Pico.romsize+4 && (pc&0xe00000)!=0xe00000)) {\r
71     PicoCpu.cycles = 0;\r
72     PicoCpu.stopped = 1;\r
73     return 1;\r
74   }\r
75   //exit(1);\r
76   return 0;\r
77 }\r
78 #endif\r
79 \r
80 \r
81 #ifdef EMU_M68K\r
82 static int SekIntAckM68K(int level)\r
83 {\r
84   if     (level == 4) { Pico.video.pending_ints  =  0;    } // dprintf("hack: [%i|%i]", Pico.m.scanline, SekCyclesDone()); }\r
85   else if(level == 6) { Pico.video.pending_ints &= ~0x20; } // dprintf("vack: [%i|%i]", Pico.m.scanline, SekCyclesDone()); }\r
86   CPU_INT_LEVEL = 0;\r
87   return M68K_INT_ACK_AUTOVECTOR;\r
88 }\r
89 #endif\r
90 \r
91 \r
92 \r
93 int SekInit()\r
94 {\r
95 #ifdef EMU_C68K\r
96   CycloneInit();\r
97   memset(&PicoCpu,0,sizeof(PicoCpu));\r
98   PicoCpu.IrqCallback=SekIntAck;\r
99   PicoCpu.ResetCallback=SekResetAck;\r
100   PicoCpu.UnrecognizedCallback=SekUnrecognizedOpcode;\r
101 #endif\r
102 #ifdef EMU_A68K\r
103   memset(&M68000_regs,0,sizeof(M68000_regs));\r
104   M68000_regs.IrqCallback=IrqCallback;\r
105   M68000_regs.pResetCallback=ResetCallback;\r
106   M68000_RESET(); // Init cpu emulator\r
107 #endif\r
108 #ifdef EMU_M68K\r
109   {\r
110     void *oldcontext = m68ki_cpu_p;\r
111     m68k_set_context(&PicoM68kCPU);\r
112     m68k_set_cpu_type(M68K_CPU_TYPE_68000);\r
113     m68k_init();\r
114     m68k_set_int_ack_callback(SekIntAckM68K);\r
115     m68k_pulse_reset(); // Init cpu emulator\r
116     m68k_set_context(oldcontext);\r
117   }\r
118 #endif\r
119 \r
120   return 0;\r
121 }\r
122 \r
123 // Reset the 68000:\r
124 int SekReset()\r
125 {\r
126   if (Pico.rom==NULL) return 1;\r
127 \r
128 #ifdef EMU_C68K\r
129   PicoCpu.stopped=0;\r
130   PicoCpu.osp=0;\r
131   PicoCpu.srh =0x27; // Supervisor mode\r
132   PicoCpu.flags=4;   // Z set\r
133   PicoCpu.irq=0;\r
134   PicoCpu.a[7]=PicoCpu.read32(0); // Stack Pointer\r
135   PicoCpu.membase=0;\r
136   PicoCpu.pc=PicoCpu.checkpc(PicoCpu.read32(4)); // Program Counter\r
137 #endif\r
138 #ifdef EMU_A68K\r
139   // Reset CPU: fetch SP and PC\r
140   M68000_regs.srh=0x27; // Supervisor mode\r
141   M68000_regs.a[7]=PicoRead32(0);\r
142   M68000_regs.pc  =PicoRead32(4);\r
143   PicoInitPc(M68000_regs.pc);\r
144 #endif\r
145 #ifdef EMU_M68K\r
146   {\r
147     void *oldcontext = m68ki_cpu_p;\r
148     m68k_set_context(&PicoM68kCPU);\r
149     m68k_pulse_reset();\r
150     m68k_set_context(oldcontext);\r
151   }\r
152 #endif\r
153 \r
154   return 0;\r
155 }\r
156 \r
157 \r
158 int SekInterrupt(int irq)\r
159 {\r
160 #ifdef EMU_C68K\r
161   PicoCpu.irq=irq;\r
162 #endif\r
163 #ifdef EMU_A68K\r
164   M68000_regs.irq=irq; // raise irq (gets lowered after taken)\r
165 #endif\r
166 #ifdef EMU_M68K\r
167   {\r
168     void *oldcontext = m68ki_cpu_p;\r
169     m68k_set_context(&PicoM68kCPU);\r
170     m68k_set_irq(irq); // raise irq (gets lowered after taken or must be done in ack)\r
171     m68k_set_context(oldcontext);\r
172   }\r
173 #endif\r
174   return 0;\r
175 }\r
176 \r
177 //int SekPc() { return PicoCpu.pc-PicoCpu.membase; }\r
178 //int SekPc() { return M68000_regs.pc; }\r
179 //int SekPc() { return m68k_get_reg(NULL, M68K_REG_PC); }\r
180 \r
181 void SekState(unsigned char *data)\r
182 {\r
183 #ifdef EMU_C68K\r
184   memcpy(data,PicoCpu.d,0x44);\r
185 #elif defined(EMU_A68K)\r
186   memcpy(data,      M68000_regs.d, 0x40);\r
187   memcpy(data+0x40,&M68000_regs.pc,0x04);\r
188 #elif defined(EMU_M68K)\r
189   memcpy(data,      PicoM68kCPU.dar,0x40);\r
190   memcpy(data+0x40,&PicoM68kCPU.pc, 0x04);\r
191 #endif\r
192 }\r