amalgamation
[picodrive.git] / Pico / cd / Sek.c
1 // (c) Copyright 2006 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 #ifdef EMU_C68K
11 // ---------------------- Cyclone 68000 ----------------------
12 struct Cyclone PicoCpuS68k;
13 #endif
14
15 #ifdef EMU_M68K
16 // ---------------------- MUSASHI 68000 ----------------------
17 m68ki_cpu_core PicoS68kCPU; // Mega CD's CPU
18 #endif
19
20 static int new_irq_level(int level)
21 {
22   int level_new = 0, irqs;
23   Pico_mcd->m.s68k_pend_ints &= ~(1 << level);
24   irqs = Pico_mcd->m.s68k_pend_ints;
25   irqs &= Pico_mcd->s68k_regs[0x33];
26   while ((irqs >>= 1)) level_new++;
27
28   return level_new;
29 }
30
31 #ifdef EMU_M68K
32 static int SekIntAckS68kM(int level)
33 {
34   int level_new = new_irq_level(level);
35   dprintf("s68kACK %i -> %i", level, level_new);
36   CPU_INT_LEVEL = level_new << 8;
37   return M68K_INT_ACK_AUTOVECTOR;
38 }
39 #endif
40
41 #ifdef EMU_C68K
42 // interrupt acknowledgement
43 static int SekIntAckS68k(int level)
44 {
45   int level_new = new_irq_level(level);
46
47   dprintf("s68kACK %i -> %i", level, level_new);
48   PicoCpuS68k.irq = level_new;
49   return CYCLONE_INT_ACK_AUTOVECTOR;
50 }
51
52 static void SekResetAckS68k(void)
53 {
54   dprintf("s68k: Reset encountered @ %06x", SekPcS68k);
55 }
56
57 static int SekUnrecognizedOpcodeS68k(void)
58 {
59   unsigned int pc, op;
60   pc = SekPcS68k;
61   op = PicoCpuS68k.read16(pc);
62   dprintf("Unrecognized Opcode %04x @ %06x", op, pc);
63   //exit(1);
64   return 0;
65 }
66 #endif
67
68
69
70 PICO_INTERNAL int SekInitS68k()
71 {
72 #ifdef EMU_C68K
73 //  CycloneInit();
74   memset(&PicoCpuS68k,0,sizeof(PicoCpuS68k));
75   PicoCpuS68k.IrqCallback=SekIntAckS68k;
76   PicoCpuS68k.ResetCallback=SekResetAckS68k;
77   PicoCpuS68k.UnrecognizedCallback=SekUnrecognizedOpcodeS68k;
78 #endif
79 #ifdef EMU_M68K
80   {
81     // Musashi is not very context friendly..
82     void *oldcontext = m68ki_cpu_p;
83     m68k_set_context(&PicoS68kCPU);
84     m68k_set_cpu_type(M68K_CPU_TYPE_68000);
85     m68k_init();
86     m68k_set_int_ack_callback(SekIntAckS68kM);
87 //  m68k_pulse_reset(); // not yet, memmap is not set up
88     m68k_set_context(oldcontext);
89   }
90 #endif
91
92   return 0;
93 }
94
95 // Reset the 68000:
96 PICO_INTERNAL int SekResetS68k()
97 {
98   if (Pico.rom==NULL) return 1;
99
100 #ifdef EMU_C68K
101   PicoCpuS68k.state_flags=0;
102   PicoCpuS68k.osp=0;
103   PicoCpuS68k.srh =0x27; // Supervisor mode
104   PicoCpuS68k.flags=4;   // Z set
105   PicoCpuS68k.irq=0;
106   PicoCpuS68k.a[7]=PicoCpuS68k.read32(0); // Stack Pointer
107   PicoCpuS68k.membase=0;
108   PicoCpuS68k.pc=PicoCpuS68k.checkpc(PicoCpuS68k.read32(4)); // Program Counter
109 #endif
110 #ifdef EMU_M68K
111   {
112     void *oldcontext = m68ki_cpu_p;
113
114     m68k_set_context(&PicoS68kCPU);
115     m68ki_cpu.sp[0]=0;
116     m68k_set_irq(0);
117     m68k_pulse_reset();
118     m68k_set_context(oldcontext);
119   }
120 #endif
121
122   return 0;
123 }
124
125 PICO_INTERNAL int SekInterruptS68k(int irq)
126 {
127   int irqs, real_irq = 1;
128   Pico_mcd->m.s68k_pend_ints |= 1 << irq;
129   irqs = Pico_mcd->m.s68k_pend_ints >> 1;
130   while ((irqs >>= 1)) real_irq++; // this is probably only needed for Cyclone
131
132 #ifdef EMU_C68K
133   PicoCpuS68k.irq=real_irq;
134 #endif
135 #ifdef EMU_M68K
136   void *oldcontext = m68ki_cpu_p;
137   m68k_set_context(&PicoS68kCPU);
138   m68k_set_irq(real_irq); // raise irq (gets lowered after taken or must be done in ack)
139   m68k_set_context(oldcontext);
140 #endif
141   return 0;
142 }
143