cso support
[picodrive.git] / Pico / cd / Sek.c
1 // (c) Copyright 2007 notaz, All rights reserved.
2
3
4 #include "../PicoInt.h"
5
6
7 int SekCycleCntS68k=0; // cycles done in this frame
8 int SekCycleAimS68k=0; // cycle aim
9
10
11 /* context */
12 // Cyclone 68000
13 #ifdef EMU_C68K
14 struct Cyclone PicoCpuCS68k;
15 #endif
16 // MUSASHI 68000
17 #ifdef EMU_M68K
18 m68ki_cpu_core PicoCpuMS68k;
19 #endif
20 // FAME 68000
21 #ifdef EMU_F68K
22 M68K_CONTEXT PicoCpuFS68k;
23 #endif
24
25
26 static int new_irq_level(int level)
27 {
28   int level_new = 0, irqs;
29   Pico_mcd->m.s68k_pend_ints &= ~(1 << level);
30   irqs = Pico_mcd->m.s68k_pend_ints;
31   irqs &= Pico_mcd->s68k_regs[0x33];
32   while ((irqs >>= 1)) level_new++;
33
34   return level_new;
35 }
36
37 #ifdef EMU_C68K
38 // interrupt acknowledgement
39 static int SekIntAckS68k(int level)
40 {
41   int level_new = new_irq_level(level);
42
43   dprintf("s68kACK %i -> %i", level, level_new);
44   PicoCpuCS68k.irq = level_new;
45   return CYCLONE_INT_ACK_AUTOVECTOR;
46 }
47
48 static void SekResetAckS68k(void)
49 {
50   dprintf("s68k: Reset encountered @ %06x", SekPcS68k);
51 }
52
53 static int SekUnrecognizedOpcodeS68k(void)
54 {
55   unsigned int pc, op;
56   pc = SekPcS68k;
57   op = PicoCpuCS68k.read16(pc);
58   dprintf("Unrecognized Opcode %04x @ %06x", op, pc);
59   //exit(1);
60   return 0;
61 }
62 #endif
63
64 #ifdef EMU_M68K
65 static int SekIntAckMS68k(int level)
66 {
67 #ifndef EMU_CORE_DEBUG
68   int level_new = new_irq_level(level);
69   dprintf("s68kACK %i -> %i", level, level_new);
70   CPU_INT_LEVEL = level_new << 8;
71 #else
72   CPU_INT_LEVEL = 0;
73 #endif
74   return M68K_INT_ACK_AUTOVECTOR;
75 }
76 #endif
77
78 #ifdef EMU_F68K
79 static void SekIntAckFS68k(unsigned level)
80 {
81   int level_new = new_irq_level(level);
82   dprintf("s68kACK %i -> %i", level, level_new);
83 #ifndef EMU_CORE_DEBUG
84   PicoCpuFS68k.interrupts[0] = level_new;
85 #else
86   {
87     extern int dbg_irq_level_sub;
88     dbg_irq_level_sub = level_new;
89     PicoCpuFS68k.interrupts[0] = 0;
90   }
91 #endif
92 }
93 #endif
94
95
96 PICO_INTERNAL int SekInitS68k()
97 {
98 #ifdef EMU_C68K
99 //  CycloneInit();
100   memset(&PicoCpuCS68k,0,sizeof(PicoCpuCS68k));
101   PicoCpuCS68k.IrqCallback=SekIntAckS68k;
102   PicoCpuCS68k.ResetCallback=SekResetAckS68k;
103   PicoCpuCS68k.UnrecognizedCallback=SekUnrecognizedOpcodeS68k;
104 #endif
105 #ifdef EMU_M68K
106   {
107     // Musashi is not very context friendly..
108     void *oldcontext = m68ki_cpu_p;
109     m68k_set_context(&PicoCpuMS68k);
110     m68k_set_cpu_type(M68K_CPU_TYPE_68000);
111     m68k_init();
112     m68k_set_int_ack_callback(SekIntAckMS68k);
113 //  m68k_pulse_reset(); // not yet, memmap is not set up
114     m68k_set_context(oldcontext);
115   }
116 #endif
117 #ifdef EMU_F68K
118   {
119     void *oldcontext = g_m68kcontext;
120     g_m68kcontext = &PicoCpuFS68k;
121     memset(&PicoCpuFS68k, 0, sizeof(PicoCpuFS68k));
122     fm68k_init();
123     PicoCpuFS68k.iack_handler = SekIntAckFS68k;
124     PicoCpuFS68k.sr = 0x2704; // Z flag
125     g_m68kcontext = oldcontext;
126   }
127 #endif
128
129   return 0;
130 }
131
132 // Reset the 68000:
133 PICO_INTERNAL int SekResetS68k()
134 {
135   if (Pico.rom==NULL) return 1;
136
137 #ifdef EMU_C68K
138   PicoCpuCS68k.state_flags=0;
139   PicoCpuCS68k.osp=0;
140   PicoCpuCS68k.srh =0x27; // Supervisor mode
141   PicoCpuCS68k.flags=4;   // Z set
142   PicoCpuCS68k.irq=0;
143   PicoCpuCS68k.a[7]=PicoCpuCS68k.read32(0); // Stack Pointer
144   PicoCpuCS68k.membase=0;
145   PicoCpuCS68k.pc=PicoCpuCS68k.checkpc(PicoCpuCS68k.read32(4)); // Program Counter
146 #endif
147 #ifdef EMU_M68K
148   {
149     void *oldcontext = m68ki_cpu_p;
150
151     m68k_set_context(&PicoCpuMS68k);
152     m68ki_cpu.sp[0]=0;
153     m68k_set_irq(0);
154     m68k_pulse_reset();
155     m68k_set_context(oldcontext);
156   }
157 #endif
158 #ifdef EMU_F68K
159   {
160     void *oldcontext = g_m68kcontext;
161     g_m68kcontext = &PicoCpuFS68k;
162     fm68k_reset();
163     g_m68kcontext = oldcontext;
164   }
165 #endif
166
167   return 0;
168 }
169
170 PICO_INTERNAL int SekInterruptS68k(int irq)
171 {
172   int irqs, real_irq = 1;
173   Pico_mcd->m.s68k_pend_ints |= 1 << irq;
174   irqs = Pico_mcd->m.s68k_pend_ints >> 1;
175   while ((irqs >>= 1)) real_irq++;
176
177 #ifdef EMU_CORE_DEBUG
178   {
179     extern int dbg_irq_level_sub;
180     dbg_irq_level_sub=real_irq;
181     return 0;
182   }
183 #endif
184 #ifdef EMU_C68K
185   PicoCpuCS68k.irq=real_irq;
186 #endif
187 #ifdef EMU_M68K
188   void *oldcontext = m68ki_cpu_p;
189   m68k_set_context(&PicoCpuMS68k);
190   m68k_set_irq(real_irq);
191   m68k_set_context(oldcontext);
192 #endif
193 #ifdef EMU_F68K
194   PicoCpuFS68k.interrupts[0]=real_irq;
195 #endif
196   return 0;
197 }
198