allow to add symbols for host_dasm
[picodrive.git] / pico / 32x / 32x.c
CommitLineData
be2c4208 1#include "../pico_int.h"
974fdb5b 2#include "../sound/ym2612.h"
be2c4208 3
4struct Pico32x Pico32x;
83ff19ec 5SH2 sh2s[2];
be2c4208 6
b78efee2 7static void sh2_irq_cb(int id, int level)
4ea707e1 8{
9 // diagnostic for now
b78efee2 10 elprintf(EL_32X, "%csh2 ack %d @ %08x", id ? 's' : 'm', level, sh2_pc(id));
4ea707e1 11}
12
13void p32x_update_irls(void)
14{
15 int irqs, mlvl = 0, slvl = 0;
16
17 // msh2
18 irqs = (Pico32x.sh2irqs | Pico32x.sh2irqi[0]) & ((Pico32x.sh2irq_mask[0] << 3) | P32XI_VRES);
19 while ((irqs >>= 1))
20 mlvl++;
21 mlvl *= 2;
22
23 // ssh2
24 irqs = (Pico32x.sh2irqs | Pico32x.sh2irqi[1]) & ((Pico32x.sh2irq_mask[1] << 3) | P32XI_VRES);
25 while ((irqs >>= 1))
26 slvl++;
27 slvl *= 2;
28
29 elprintf(EL_32X, "update_irls: m %d, s %d", mlvl, slvl);
30 sh2_irl_irq(&msh2, mlvl);
4ea707e1 31 sh2_irl_irq(&ssh2, slvl);
87accdf7 32 mlvl = mlvl ? 1 : 0;
33 slvl = slvl ? 1 : 0;
34 p32x_poll_event(mlvl | (slvl << 1), 0);
4ea707e1 35}
36
be2c4208 37void Pico32xStartup(void)
38{
39 elprintf(EL_STATUS|EL_32X, "32X startup");
40
679af8a3 41 // TODO: OOM handling
be2c4208 42 PicoAHW |= PAHW_32X;
b78efee2 43 sh2_init(&msh2, 0);
4ea707e1 44 msh2.irq_callback = sh2_irq_cb;
b78efee2 45 sh2_init(&ssh2, 1);
4ea707e1 46 ssh2.irq_callback = sh2_irq_cb;
83ff19ec 47
48 PicoMemSetup32x();
acd35d4c 49
be2c4208 50 if (!Pico.m.pal)
974fdb5b 51 Pico32x.vdp_regs[0] |= P32XV_nPAL;
be2c4208 52
1d7a28a7 53 PREG8(Pico32xMem->sh2_peri_regs[0], 4) =
54 PREG8(Pico32xMem->sh2_peri_regs[1], 4) = 0x84; // SCI SSR
55
974fdb5b 56 emu_32x_startup();
be2c4208 57}
58
83ff19ec 59#define HWSWAP(x) (((x) << 16) | ((x) >> 16))
60void p32x_reset_sh2s(void)
61{
62 elprintf(EL_32X, "sh2 reset");
63
64 sh2_reset(&msh2);
65 sh2_reset(&ssh2);
66
67 // if we don't have BIOS set, perform it's work here.
68 // MSH2
69 if (p32x_bios_m == NULL) {
70 unsigned int idl_src, idl_dst, idl_size; // initial data load
71 unsigned int vbr;
72
73 // initial data
74 idl_src = HWSWAP(*(unsigned int *)(Pico.rom + 0x3d4)) & ~0xf0000000;
75 idl_dst = HWSWAP(*(unsigned int *)(Pico.rom + 0x3d8)) & ~0xf0000000;
76 idl_size= HWSWAP(*(unsigned int *)(Pico.rom + 0x3dc));
77 if (idl_size > Pico.romsize || idl_src + idl_size > Pico.romsize ||
78 idl_size > 0x40000 || idl_dst + idl_size > 0x40000 || (idl_src & 3) || (idl_dst & 3)) {
79 elprintf(EL_STATUS|EL_ANOMALY, "32x: invalid initial data ptrs: %06x -> %06x, %06x",
80 idl_src, idl_dst, idl_size);
81 }
82 else
83 memcpy(Pico32xMem->sdram + idl_dst, Pico.rom + idl_src, idl_size);
84
85 // GBR/VBR
86 vbr = HWSWAP(*(unsigned int *)(Pico.rom + 0x3e8));
87 sh2_set_gbr(0, 0x20004000);
88 sh2_set_vbr(0, vbr);
89
90 // checksum and M_OK
91 Pico32x.regs[0x28 / 2] = *(unsigned short *)(Pico.rom + 0x18e);
92 // program will set M_OK
93 }
94
95 // SSH2
96 if (p32x_bios_s == NULL) {
97 unsigned int vbr;
98
99 // GBR/VBR
100 vbr = HWSWAP(*(unsigned int *)(Pico.rom + 0x3ec));
101 sh2_set_gbr(1, 0x20004000);
102 sh2_set_vbr(1, vbr);
103 // program will set S_OK
104 }
105}
106
be2c4208 107void Pico32xInit(void)
108{
974fdb5b 109}
110
111void PicoPower32x(void)
112{
113 memset(&Pico32x, 0, sizeof(Pico32x));
5e49c3a8 114
83ff19ec 115 Pico32x.regs[0] = P32XS_REN|P32XS_nRES; // verified
974fdb5b 116 Pico32x.vdp_regs[0x0a/2] = P32XV_VBLK|P32XV_HBLK|P32XV_PEN;
87accdf7 117 Pico32x.sh2_regs[0] = P32XS2_ADEN;
be2c4208 118}
119
5e49c3a8 120void PicoUnload32x(void)
121{
122 if (Pico32xMem != NULL)
123 free(Pico32xMem);
124 Pico32xMem = NULL;
e898de13 125 sh2_finish(&msh2);
126 sh2_finish(&ssh2);
5e49c3a8 127
128 PicoAHW &= ~PAHW_32X;
129}
130
be2c4208 131void PicoReset32x(void)
132{
83ff19ec 133 if (PicoAHW & PAHW_32X) {
134 Pico32x.sh2irqs |= P32XI_VRES;
135 p32x_update_irls();
136 p32x_poll_event(3, 0);
137 }
be2c4208 138}
139
974fdb5b 140static void p32x_start_blank(void)
141{
142 // enter vblank
143 Pico32x.vdp_regs[0x0a/2] |= P32XV_VBLK|P32XV_PEN;
144
4ea707e1 145 // FB swap waits until vblank
974fdb5b 146 if ((Pico32x.vdp_regs[0x0a/2] ^ Pico32x.pending_fb) & P32XV_FS) {
147 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_FS;
148 Pico32x.vdp_regs[0x0a/2] |= Pico32x.pending_fb;
149 Pico32xSwapDRAM(Pico32x.pending_fb ^ 1);
150 }
4ea707e1 151
97d3f47f 152 Pico32x.sh2irqs |= P32XI_VINT;
153 p32x_update_irls();
87accdf7 154 p32x_poll_event(3, 1);
974fdb5b 155}
156
236990cf 157static __inline void run_m68k(int cyc)
974fdb5b 158{
236990cf 159#if defined(EMU_C68K)
160 PicoCpuCM68k.cycles = cyc;
974fdb5b 161 CycloneRun(&PicoCpuCM68k);
236990cf 162 SekCycleCnt += cyc - PicoCpuCM68k.cycles;
974fdb5b 163#elif defined(EMU_M68K)
236990cf 164 SekCycleCnt += m68k_execute(cyc);
974fdb5b 165#elif defined(EMU_F68K)
236990cf 166 SekCycleCnt += fm68k_emulate(cyc+1, 0, 0);
974fdb5b 167#endif
168}
169
266c6afa 170// ~1463.8, but due to cache misses and slow mem
171// it's much lower than that
be20816c 172//#define SH2_LINE_CYCLES 735
c987bb5c 173#define CYCLES_M68K2SH2(x) ((x) * 6 / 4)
266c6afa 174
974fdb5b 175#define PICO_32X
c987bb5c 176#define CPUS_RUN_SIMPLE(m68k_cycles,s68k_cycles) \
236990cf 177{ \
178 int slice; \
179 SekCycleAim += m68k_cycles; \
180 while (SekCycleCnt < SekCycleAim) { \
181 slice = SekCycleCnt; \
182 run_m68k(SekCycleAim - SekCycleCnt); \
83ff19ec 183 if (!(Pico32x.regs[0] & P32XS_nRES)) \
184 continue; /* SH2s reseting */ \
236990cf 185 slice = SekCycleCnt - slice; /* real count from 68k */ \
186 if (SekCycleCnt < SekCycleAim) \
187 elprintf(EL_32X, "slice %d", slice); \
188 if (!(Pico32x.emu_flags & (P32XF_SSH2POLL|P32XF_SSH2VPOLL))) \
189 sh2_execute(&ssh2, CYCLES_M68K2SH2(slice)); \
190 if (!(Pico32x.emu_flags & (P32XF_MSH2POLL|P32XF_MSH2VPOLL))) \
191 sh2_execute(&msh2, CYCLES_M68K2SH2(slice)); \
192 } \
193}
acd35d4c 194
c987bb5c 195#define STEP_68K 24
196#define CPUS_RUN_LOCKSTEP(m68k_cycles,s68k_cycles) \
87accdf7 197{ \
198 int i; \
c987bb5c 199 for (i = 0; i <= (m68k_cycles) - STEP_68K; i += STEP_68K) { \
236990cf 200 run_m68k(STEP_68K); \
c987bb5c 201 if (!(Pico32x.emu_flags & (P32XF_MSH2POLL|P32XF_MSH2VPOLL))) \
202 sh2_execute(&msh2, CYCLES_M68K2SH2(STEP_68K)); \
203 if (!(Pico32x.emu_flags & (P32XF_SSH2POLL|P32XF_SSH2VPOLL))) \
204 sh2_execute(&ssh2, CYCLES_M68K2SH2(STEP_68K)); \
87accdf7 205 } \
c987bb5c 206 /* last step */ \
207 i = (m68k_cycles) - i; \
236990cf 208 run_m68k(i); \
c987bb5c 209 if (!(Pico32x.emu_flags & (P32XF_MSH2POLL|P32XF_MSH2VPOLL))) \
210 sh2_execute(&msh2, CYCLES_M68K2SH2(i)); \
211 if (!(Pico32x.emu_flags & (P32XF_SSH2POLL|P32XF_SSH2VPOLL))) \
212 sh2_execute(&ssh2, CYCLES_M68K2SH2(i)); \
87accdf7 213}
214
236990cf 215#define CPUS_RUN CPUS_RUN_SIMPLE
216//#define CPUS_RUN CPUS_RUN_LOCKSTEP
87accdf7 217
974fdb5b 218#include "../pico_cmn.c"
219
220void PicoFrame32x(void)
221{
db1d3564 222 pwm_frame_smp_cnt = 0;
223
4ea707e1 224 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_VBLK; // get out of vblank
db1d3564 225 if ((Pico32x.vdp_regs[0] & P32XV_Mx) != 0) // no forced blanking
226 Pico32x.vdp_regs[0x0a/2] &= ~P32XV_PEN; // no palette access
4ea707e1 227
87accdf7 228 p32x_poll_event(3, 1);
974fdb5b 229
230 PicoFrameStart();
231 PicoFrameHints();
be20816c 232 elprintf(EL_32X, "poll: %02x", Pico32x.emu_flags);
974fdb5b 233}
db1d3564 234