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