platform ps2, handle audio similar to psp
[picodrive.git] / pico / memory.c
CommitLineData
cff531af 1/*\r
2 * memory handling\r
3 * (c) Copyright Dave, 2004\r
4 * (C) notaz, 2006-2010\r
7bf552b5 5 * (C) irixxxx, 2019-2024\r
cff531af 6 *\r
7 * This work is licensed under the terms of MAME license.\r
8 * See COPYING file in the top-level directory.\r
9 */\r
cc68a136 10\r
efcba75f 11#include "pico_int.h"\r
af37bca8 12#include "memory.h"\r
cc68a136 13\r
cc68a136 14#include "sound/ym2612.h"\r
15#include "sound/sn76496.h"\r
16\r
c8d1e9b6 17extern unsigned int lastSSRamWrite; // used by serial eeprom code\r
cc68a136 18\r
bcf65fd6 19uptr m68k_read8_map [0x1000000 >> M68K_MEM_SHIFT];\r
20uptr m68k_read16_map [0x1000000 >> M68K_MEM_SHIFT];\r
21uptr m68k_write8_map [0x1000000 >> M68K_MEM_SHIFT];\r
22uptr m68k_write16_map[0x1000000 >> M68K_MEM_SHIFT];\r
af37bca8 23\r
7ed05f84 24static void xmap_set(uptr *map, int shift, u32 start_addr, u32 end_addr,\r
0ace9b9a 25 const void *func_or_mh, int is_func)\r
af37bca8 26{\r
faf543ce 27#ifdef __clang__\r
28 // workaround bug (segfault) in \r
29 // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)\r
30 volatile \r
31#endif\r
bcf65fd6 32 uptr addr = (uptr)func_or_mh;\r
af37bca8 33 int mask = (1 << shift) - 1;\r
34 int i;\r
35\r
36 if ((start_addr & mask) != 0 || (end_addr & mask) != mask) {\r
37 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: tried to map bad range: %06x-%06x",\r
38 start_addr, end_addr);\r
39 return;\r
40 }\r
41\r
42 if (addr & 1) {\r
43 elprintf(EL_STATUS|EL_ANOMALY, "xmap_set: ptr is not aligned: %08lx", addr);\r
44 return;\r
45 }\r
46\r
47 if (!is_func)\r
48 addr -= start_addr;\r
49\r
50 for (i = start_addr >> shift; i <= end_addr >> shift; i++) {\r
51 map[i] = addr >> 1;\r
52 if (is_func)\r
add51c49 53 map[i] |= MAP_FLAG;\r
af37bca8 54 }\r
55}\r
56\r
7ed05f84 57void z80_map_set(uptr *map, u16 start_addr, u16 end_addr,\r
0ace9b9a 58 const void *func_or_mh, int is_func)\r
af37bca8 59{\r
60 xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);\r
032c76a3 61#ifdef _USE_CZ80\r
62 if (!is_func)\r
63 Cz80_Set_Fetch(&CZ80, start_addr, end_addr, (FPTR)func_or_mh);\r
64#endif\r
af37bca8 65}\r
66\r
7ed05f84 67void cpu68k_map_set(uptr *map, u32 start_addr, u32 end_addr,\r
0ace9b9a 68 const void *func_or_mh, int is_func)\r
af37bca8 69{\r
02ff0254 70 xmap_set(map, M68K_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func & 1);\r
c6b118c0 71#ifdef EMU_F68K\r
72 // setup FAME fetchmap\r
02ff0254 73 if (!(is_func & 1))\r
c6b118c0 74 {\r
02ff0254 75 M68K_CONTEXT *ctx = is_func & 2 ? &PicoCpuFS68k : &PicoCpuFM68k;\r
c6b118c0 76 int shiftout = 24 - FAMEC_FETCHBITS;\r
77 int i = start_addr >> shiftout;\r
78 uptr base = (uptr)func_or_mh - (i << shiftout);\r
79 for (; i <= (end_addr >> shiftout); i++)\r
02ff0254 80 ctx->Fetch[i] = base;\r
c6b118c0 81 }\r
82#endif\r
af37bca8 83}\r
84\r
85// more specialized/optimized function (does same as above)\r
8eeb3426 86void cpu68k_map_read_mem(u32 start_addr, u32 end_addr, void *ptr, int is_sub)\r
87{\r
88 uptr *r8map, *r16map;\r
89 uptr addr = (uptr)ptr;\r
90 int shift = M68K_MEM_SHIFT;\r
91 int i;\r
92\r
93 if (!is_sub) {\r
94 r8map = m68k_read8_map;\r
95 r16map = m68k_read16_map;\r
96 } else {\r
97 r8map = s68k_read8_map;\r
98 r16map = s68k_read16_map;\r
99 }\r
100\r
101 addr -= start_addr;\r
102 addr >>= 1;\r
103 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
104 r8map[i] = r16map[i] = addr;\r
105#ifdef EMU_F68K\r
106 // setup FAME fetchmap\r
107 {\r
108 M68K_CONTEXT *ctx = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
109 int shiftout = 24 - FAMEC_FETCHBITS;\r
110 i = start_addr >> shiftout;\r
111 addr = (uptr)ptr - (i << shiftout);\r
112 for (; i <= (end_addr >> shiftout); i++)\r
113 ctx->Fetch[i] = addr;\r
114 }\r
115#endif\r
116}\r
117\r
7ed05f84 118void cpu68k_map_all_ram(u32 start_addr, u32 end_addr, void *ptr, int is_sub)\r
af37bca8 119{\r
bcf65fd6 120 uptr *r8map, *r16map, *w8map, *w16map;\r
121 uptr addr = (uptr)ptr;\r
af37bca8 122 int shift = M68K_MEM_SHIFT;\r
123 int i;\r
124\r
125 if (!is_sub) {\r
126 r8map = m68k_read8_map;\r
127 r16map = m68k_read16_map;\r
128 w8map = m68k_write8_map;\r
129 w16map = m68k_write16_map;\r
130 } else {\r
131 r8map = s68k_read8_map;\r
132 r16map = s68k_read16_map;\r
133 w8map = s68k_write8_map;\r
134 w16map = s68k_write16_map;\r
135 }\r
136\r
137 addr -= start_addr;\r
138 addr >>= 1;\r
139 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
140 r8map[i] = r16map[i] = w8map[i] = w16map[i] = addr;\r
c6b118c0 141#ifdef EMU_F68K\r
142 // setup FAME fetchmap\r
143 {\r
144 M68K_CONTEXT *ctx = is_sub ? &PicoCpuFS68k : &PicoCpuFM68k;\r
145 int shiftout = 24 - FAMEC_FETCHBITS;\r
146 i = start_addr >> shiftout;\r
147 addr = (uptr)ptr - (i << shiftout);\r
148 for (; i <= (end_addr >> shiftout); i++)\r
149 ctx->Fetch[i] = addr;\r
150 }\r
151#endif\r
af37bca8 152}\r
153\r
8eeb3426 154void cpu68k_map_read_funcs(u32 start_addr, u32 end_addr, u32 (*r8)(u32), u32 (*r16)(u32), int is_sub)\r
155{\r
156 uptr *r8map, *r16map;\r
157 uptr ar8 = (uptr)r8, ar16 = (uptr)r16;\r
158 int shift = M68K_MEM_SHIFT;\r
159 int i;\r
160\r
161 if (!is_sub) {\r
162 r8map = m68k_read8_map;\r
163 r16map = m68k_read16_map;\r
164 } else {\r
165 r8map = s68k_read8_map;\r
166 r16map = s68k_read16_map;\r
167 }\r
168\r
169 ar8 = (ar8 >> 1 ) | MAP_FLAG;\r
170 ar16 = (ar16 >> 1 ) | MAP_FLAG;\r
171 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
172 r8map[i] = ar8, r16map[i] = ar16;\r
173}\r
174\r
175void cpu68k_map_all_funcs(u32 start_addr, u32 end_addr, u32 (*r8)(u32), u32 (*r16)(u32), void (*w8)(u32, u32), void (*w16)(u32, u32), int is_sub)\r
176{\r
177 uptr *r8map, *r16map, *w8map, *w16map;\r
178 uptr ar8 = (uptr)r8, ar16 = (uptr)r16;\r
179 uptr aw8 = (uptr)w8, aw16 = (uptr)w16;\r
180 int shift = M68K_MEM_SHIFT;\r
181 int i;\r
182\r
183 if (!is_sub) {\r
184 r8map = m68k_read8_map;\r
185 r16map = m68k_read16_map;\r
186 w8map = m68k_write8_map;\r
187 w16map = m68k_write16_map;\r
188 } else {\r
189 r8map = s68k_read8_map;\r
190 r16map = s68k_read16_map;\r
191 w8map = s68k_write8_map;\r
192 w16map = s68k_write16_map;\r
193 }\r
194\r
195 ar8 = (ar8 >> 1 ) | MAP_FLAG;\r
196 ar16 = (ar16 >> 1 ) | MAP_FLAG;\r
197 aw8 = (aw8 >> 1 ) | MAP_FLAG;\r
198 aw16 = (aw16 >> 1 ) | MAP_FLAG;\r
199 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
200 r8map[i] = ar8, r16map[i] = ar16, w8map[i] = aw8, w16map[i] = aw16;\r
201}\r
202\r
9f29605f 203u32 PicoRead16_floating(u32 a)\r
204{\r
205 // faking open bus\r
206 u32 d = (Pico.m.rotate += 0x41);\r
207 d ^= (d << 5) ^ (d << 8);\r
41670496 208 if ((a & 0xff0000) == 0xa10000) return d; // MegaCD pulldowns don't work here curiously\r
9f29605f 209 return (PicoIn.AHW & PAHW_MCD) ? 0x00 : d; // pulldown if MegaCD2 attached\r
210}\r
211\r
af37bca8 212static u32 m68k_unmapped_read8(u32 a)\r
213{\r
214 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);\r
1eb630c2 215 return a < 0x400000 ? 0 : (u8)PicoRead16_floating(a);\r
af37bca8 216}\r
217\r
218static u32 m68k_unmapped_read16(u32 a)\r
219{\r
220 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);\r
1eb630c2 221 return a < 0x400000 ? 0 : PicoRead16_floating(a);\r
af37bca8 222}\r
223\r
224static void m68k_unmapped_write8(u32 a, u32 d)\r
225{\r
226 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);\r
227}\r
228\r
229static void m68k_unmapped_write16(u32 a, u32 d)\r
230{\r
231 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);\r
232}\r
233\r
7ed05f84 234void m68k_map_unmap(u32 start_addr, u32 end_addr)\r
af37bca8 235{\r
faf543ce 236#ifdef __clang__\r
237 // workaround bug (segfault) in \r
238 // Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn)\r
239 volatile \r
240#endif\r
bcf65fd6 241 uptr addr;\r
af37bca8 242 int shift = M68K_MEM_SHIFT;\r
243 int i;\r
244\r
bcf65fd6 245 addr = (uptr)m68k_unmapped_read8;\r
af37bca8 246 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
add51c49 247 m68k_read8_map[i] = (addr >> 1) | MAP_FLAG;\r
af37bca8 248\r
bcf65fd6 249 addr = (uptr)m68k_unmapped_read16;\r
af37bca8 250 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
add51c49 251 m68k_read16_map[i] = (addr >> 1) | MAP_FLAG;\r
af37bca8 252\r
bcf65fd6 253 addr = (uptr)m68k_unmapped_write8;\r
af37bca8 254 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
add51c49 255 m68k_write8_map[i] = (addr >> 1) | MAP_FLAG;\r
af37bca8 256\r
bcf65fd6 257 addr = (uptr)m68k_unmapped_write16;\r
af37bca8 258 for (i = start_addr >> shift; i <= end_addr >> shift; i++)\r
add51c49 259 m68k_write16_map[i] = (addr >> 1) | MAP_FLAG;\r
af37bca8 260}\r
261\r
78d817c3 262#ifndef _ASM_MEMORY_C\r
af37bca8 263MAKE_68K_READ8(m68k_read8, m68k_read8_map)\r
264MAKE_68K_READ16(m68k_read16, m68k_read16_map)\r
265MAKE_68K_READ32(m68k_read32, m68k_read16_map)\r
266MAKE_68K_WRITE8(m68k_write8, m68k_write8_map)\r
267MAKE_68K_WRITE16(m68k_write16, m68k_write16_map)\r
268MAKE_68K_WRITE32(m68k_write32, m68k_write16_map)\r
78d817c3 269#endif\r
af37bca8 270\r
271// -----------------------------------------------------------------\r
272\r
273static u32 ym2612_read_local_68k(void);\r
274static int ym2612_write_local(u32 a, u32 d, int is_from_z80);\r
275static void z80_mem_setup(void);\r
cc68a136 276\r
0ace9b9a 277#ifdef _ASM_MEMORY_C\r
278u32 PicoRead8_sram(u32 a);\r
279u32 PicoRead16_sram(u32 a);\r
280#endif\r
cc68a136 281\r
03e4f2a3 282#ifdef EMU_CORE_DEBUG\r
cc68a136 283u32 lastread_a, lastread_d[16]={0,}, lastwrite_cyc_d[16]={0,}, lastwrite_mus_d[16]={0,};\r
284int lrp_cyc=0, lrp_mus=0, lwp_cyc=0, lwp_mus=0;\r
285extern unsigned int ppop;\r
286#endif\r
287\r
4f65685b 288#ifdef IO_STATS\r
289void log_io(unsigned int addr, int bits, int rw);\r
dca310c4 290#elif defined(_MSC_VER)\r
291#define log_io\r
4f65685b 292#else\r
293#define log_io(...)\r
294#endif\r
295\r
70357ce5 296#if defined(EMU_C68K)\r
5e89f0f5 297void cyclone_crashed(u32 pc, struct Cyclone *context)\r
cc68a136 298{\r
bf61bea0 299 elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x",\r
5e89f0f5 300 context == &PicoCpuCM68k ? 'm' : 's', pc);\r
301 context->membase = (u32)Pico.rom;\r
302 context->pc = (u32)Pico.rom + Pico.romsize;\r
cc68a136 303}\r
304#endif\r
305\r
cc68a136 306// -----------------------------------------------------------------\r
af37bca8 307// memmap helpers\r
cc68a136 308\r
531a8f38 309static u32 read_pad_3btn(int i, u32 out_bits)\r
e5503e2f 310{\r
93f9619e 311 u32 pad = ~PicoIn.padInt[i]; // Get inverse of pad MXYZ SACB RLDU\r
531a8f38 312 u32 value;\r
e5503e2f 313\r
531a8f38 314 if (out_bits & 0x40) // TH\r
315 value = pad & 0x3f; // ?1CB RLDU\r
316 else\r
317 value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU\r
e5503e2f 318\r
531a8f38 319 value |= out_bits & 0x40;\r
320 return value;\r
321}\r
322\r
323static u32 read_pad_6btn(int i, u32 out_bits)\r
324{\r
93f9619e 325 u32 pad = ~PicoIn.padInt[i]; // Get inverse of pad MXYZ SACB RLDU\r
531a8f38 326 int phase = Pico.m.padTHPhase[i];\r
327 u32 value;\r
328\r
329 if (phase == 2 && !(out_bits & 0x40)) {\r
330 value = (pad & 0xc0) >> 2; // ?0SA 0000\r
331 goto out;\r
332 }\r
333 else if(phase == 3) {\r
334 if (out_bits & 0x40)\r
6890dcfa 335 value = (pad & 0x30) | ((pad >> 8) & 0xf); // ?1CB MXYZ\r
531a8f38 336 else\r
6890dcfa 337 value = ((pad & 0xc0) >> 2) | 0x0f; // ?0SA 1111\r
531a8f38 338 goto out;\r
e5503e2f 339 }\r
340\r
531a8f38 341 if (out_bits & 0x40) // TH\r
342 value = pad & 0x3f; // ?1CB RLDU\r
343 else\r
344 value = ((pad & 0xc0) >> 2) | (pad & 3); // ?0SA 00DU\r
e5503e2f 345\r
531a8f38 346out:\r
347 value |= out_bits & 0x40;\r
348 return value;\r
e5503e2f 349}\r
350\r
d650a7a2 351static u32 read_pad_team(int i, u32 out_bits)\r
352{\r
353 u32 pad;\r
354 int phase = Pico.m.padTHPhase[i];\r
355 u32 value;\r
356\r
1d5885dd 357 switch (phase) {\r
358 case 0:\r
d650a7a2 359 value = 0x03;\r
1d5885dd 360 break;\r
361 case 1:\r
d650a7a2 362 value = 0x0f;\r
1d5885dd 363 break;\r
364 case 4: case 5: case 6: case 7: // controller IDs, all 3 btn for now\r
365 value = 0x00;\r
366 break;\r
367 case 8: case 10: case 12: case 14:\r
368 pad = ~PicoIn.padInt[(phase-8) >> 1];\r
d650a7a2 369 value = pad & 0x0f; // ?x?x RLDU\r
1d5885dd 370 break;\r
371 case 9: case 11: case 13: case 15:\r
372 pad = ~PicoIn.padInt[(phase-8) >> 1];\r
d650a7a2 373 value = (pad & 0xf0) >> 4; // ?x?x SACB\r
1d5885dd 374 break;\r
375 default:\r
376 value = 0;\r
377 break;\r
d650a7a2 378 }\r
379\r
d650a7a2 380 value |= (out_bits & 0x40) | ((out_bits & 0x20)>>1);\r
381 return value;\r
382}\r
383\r
384static u32 read_pad_4way(int i, u32 out_bits)\r
385{\r
386 u32 pad = (PicoMem.ioports[2] & 0x70) >> 4;\r
387 u32 value = 0;\r
388\r
1d5885dd 389 if (i == 0 && pad <= 3)\r
390 value = read_pad_3btn(pad, out_bits);\r
d650a7a2 391\r
392 value |= (out_bits & 0x40);\r
393 return value;\r
394}\r
395\r
531a8f38 396static u32 read_nothing(int i, u32 out_bits)\r
397{\r
398 return 0xff;\r
399}\r
400\r
401typedef u32 (port_read_func)(int index, u32 out_bits);\r
402\r
403static port_read_func *port_readers[3] = {\r
404 read_pad_3btn,\r
405 read_pad_3btn,\r
406 read_nothing\r
407};\r
0ace9b9a 408\r
6c6d449e 409static int padTHLatency[3]; // TODO this should be in the save file structures\r
66e9abff 410\r
531a8f38 411static NOINLINE u32 port_read(int i)\r
412{\r
88fd63ad 413 u32 data_reg = PicoMem.ioports[i + 1];\r
414 u32 ctrl_reg = PicoMem.ioports[i + 4] | 0x80;\r
531a8f38 415 u32 in, out;\r
416\r
417 out = data_reg & ctrl_reg;\r
1d366b1a 418\r
419 // pull-ups: should be 0x7f, but Decap Attack has a bug where it temp.\r
133006a9 420 // disables output before doing TH-low read, so emulate RC filter for TH.\r
1d366b1a 421 // Decap Attack reportedly doesn't work on Nomad but works on must\r
422 // other MD revisions (different pull-up strength?).\r
e05680a2 423 u32 mask = 0x3f;\r
6c6d449e 424 if (CYCLES_GE(padTHLatency[i], SekCyclesDone()+100))\r
425 padTHLatency[i] = SekCyclesDone(); // kludge to cope with cycle wrap\r
e05680a2 426 if (CYCLES_GE(SekCyclesDone(), padTHLatency[i])) {\r
427 mask |= 0x40;\r
428 padTHLatency[i] = SekCyclesDone();\r
429 }\r
66e9abff 430 out |= mask & ~ctrl_reg;\r
531a8f38 431\r
432 in = port_readers[i](i, out);\r
433\r
434 return (in & ~ctrl_reg) | (data_reg & ctrl_reg);\r
435}\r
436\r
437void PicoSetInputDevice(int port, enum input_device device)\r
438{\r
439 port_read_func *func;\r
440\r
441 if (port < 0 || port > 2)\r
442 return;\r
443\r
1d5885dd 444 if (port == 1 && port_readers[0] == read_pad_team)\r
445 func = read_nothing;\r
446\r
447 else switch (device) {\r
531a8f38 448 case PICO_INPUT_PAD_3BTN:\r
449 func = read_pad_3btn;\r
450 break;\r
451\r
452 case PICO_INPUT_PAD_6BTN:\r
453 func = read_pad_6btn;\r
454 break;\r
455\r
d650a7a2 456 case PICO_INPUT_PAD_TEAM:\r
457 func = read_pad_team;\r
458 break;\r
459\r
460 case PICO_INPUT_PAD_4WAY:\r
461 func = read_pad_4way;\r
462 break;\r
463\r
531a8f38 464 default:\r
465 func = read_nothing;\r
466 break;\r
467 }\r
468\r
469 port_readers[port] = func;\r
470}\r
471\r
472NOINLINE u32 io_ports_read(u32 a)\r
cc68a136 473{\r
af37bca8 474 u32 d;\r
475 a = (a>>1) & 0xf;\r
476 switch (a) {\r
477 case 0: d = Pico.m.hardware; break; // Hardware value (Version register)\r
531a8f38 478 case 1: d = port_read(0); break;\r
479 case 2: d = port_read(1); break;\r
480 case 3: d = port_read(2); break;\r
88fd63ad 481 default: d = PicoMem.ioports[a]; break; // IO ports can be used as RAM\r
7969166e 482 }\r
af37bca8 483 return d;\r
cc68a136 484}\r
cc68a136 485\r
531a8f38 486NOINLINE void io_ports_write(u32 a, u32 d)\r
9dc09829 487{\r
af37bca8 488 a = (a>>1) & 0xf;\r
489\r
490 // 6 button gamepad: if TH went from 0 to 1, gamepad changes state\r
531a8f38 491 if (1 <= a && a <= 2)\r
af37bca8 492 {\r
493 Pico.m.padDelay[a - 1] = 0;\r
d650a7a2 494 if (port_readers[a - 1] == read_pad_team) {\r
495 if (d & 0x40)\r
496 Pico.m.padTHPhase[a - 1] = 0;\r
497 else if ((d^PicoMem.ioports[a]) & 0x60)\r
498 Pico.m.padTHPhase[a - 1]++;\r
1d5885dd 499 } else if (port_readers[0] == read_pad_4way) {\r
d650a7a2 500 if (a == 2 && ((PicoMem.ioports[a] ^ d) & 0x70))\r
501 Pico.m.padTHPhase[0] = 0;\r
502 if (a == 1 && !(PicoMem.ioports[a] & 0x40) && (d & 0x40))\r
503 Pico.m.padTHPhase[0]++;\r
504 } else if (!(PicoMem.ioports[a] & 0x40) && (d & 0x40))\r
af37bca8 505 Pico.m.padTHPhase[a - 1]++;\r
9dc09829 506 }\r
af37bca8 507\r
e05680a2 508 // after switching TH to input there's a latency before the pullup value is \r
509 // read back as input (see Decap Attack, not in Samurai Showdown, 32x WWF Raw)\r
6c6d449e 510 if (4 <= a && a <= 5) {\r
e2a5b098 511 if ((PicoMem.ioports[a] & 0x40) && !(d & 0x40) && !(PicoMem.ioports[a - 3] & 0x40))\r
512 // latency after switching to input and output was low\r
6c6d449e 513 padTHLatency[a - 4] = SekCyclesDone() + 25;\r
e05680a2 514 }\r
66e9abff 515\r
5e89f0f5 516 // certain IO ports can be used as RAM\r
88fd63ad 517 PicoMem.ioports[a] = d;\r
9dc09829 518}\r
519\r
ae214f1c 520static int z80_cycles_from_68k(void)\r
521{\r
88fd63ad 522 int m68k_cnt = SekCyclesDone() - Pico.t.m68c_frame_start;\r
3162a710 523 return cycles_68k_to_z80(m68k_cnt);\r
ae214f1c 524}\r
525\r
0ace9b9a 526void NOINLINE ctl_write_z80busreq(u32 d)\r
7969166e 527{\r
af37bca8 528 d&=1; d^=1;\r
ebd70cb5 529 elprintf(EL_BUSREQ, "set_zrun: %i->%i [%u] @%06x", Pico.m.z80Run, d, SekCyclesDone(), SekPc);\r
af37bca8 530 if (d ^ Pico.m.z80Run)\r
531 {\r
532 if (d)\r
533 {\r
2f0c5639 534 Pico.t.z80c_aim = Pico.t.z80c_cnt = z80_cycles_from_68k() + 2;\r
535 Pico.t.z80c_cnt += Pico.t.z80_busdelay >> 8;\r
71f9c68f 536 Pico.t.z80_busdelay &= 0xff;\r
af37bca8 537 }\r
538 else\r
539 {\r
93f9619e 540 if ((PicoIn.opt & POPT_EN_Z80) && !Pico.m.z80_reset) {\r
274dd51a 541 // Z80 grants bus after the current M cycle, even within an insn\r
71f9c68f 542 // simulate this by accumulating the last insn overhang in busdelay\r
274dd51a 543 unsigned granted;\r
f6c49d38 544 pprof_start(m68k);\r
ae214f1c 545 PicoSyncZ80(SekCyclesDone());\r
274dd51a 546 pprof_end_sub(m68k);\r
547 granted = Pico.t.z80c_aim + 6; // M cycle is 3-6 cycles \r
71f9c68f 548 Pico.t.z80_busdelay += (Pico.t.z80c_cnt - granted) << 8;\r
549 Pico.t.z80c_cnt = granted;\r
f6c49d38 550 }\r
af37bca8 551 }\r
552 Pico.m.z80Run = d;\r
7969166e 553 }\r
af37bca8 554}\r
555\r
0ace9b9a 556void NOINLINE ctl_write_z80reset(u32 d)\r
af37bca8 557{\r
558 d&=1; d^=1;\r
ebd70cb5 559 elprintf(EL_BUSREQ, "set_zreset: %i->%i [%u] @%06x", Pico.m.z80_reset, d, SekCyclesDone(), SekPc);\r
af37bca8 560 if (d ^ Pico.m.z80_reset)\r
561 {\r
562 if (d)\r
563 {\r
93f9619e 564 if ((PicoIn.opt & POPT_EN_Z80) && Pico.m.z80Run) {\r
f6c49d38 565 pprof_start(m68k);\r
af37bca8 566 PicoSyncZ80(SekCyclesDone());\r
f6c49d38 567 pprof_end_sub(m68k);\r
568 }\r
274dd51a 569 Pico.t.z80_busdelay &= 0xff; // also resets bus request\r
af37bca8 570 YM2612ResetChip();\r
571 timers_reset();\r
7969166e 572 }\r
af37bca8 573 else\r
574 {\r
274dd51a 575 Pico.t.z80c_aim = Pico.t.z80c_cnt = z80_cycles_from_68k() + 2;\r
af37bca8 576 z80_reset();\r
7969166e 577 }\r
af37bca8 578 Pico.m.z80_reset = d;\r
7969166e 579 }\r
580}\r
cc68a136 581\r
5d638db0 582static void psg_write_68k(u32 d)\r
583{\r
15caa286 584 PsndDoPSG(z80_cycles_from_68k());\r
5d638db0 585 SN76496Write(d);\r
586}\r
587\r
588static void psg_write_z80(u32 d)\r
589{\r
15caa286 590 PsndDoPSG(z80_cyclesDone());\r
5d638db0 591 SN76496Write(d);\r
592}\r
593\r
af37bca8 594// -----------------------------------------------------------------\r
fa1e5e29 595\r
0ace9b9a 596#ifndef _ASM_MEMORY_C\r
597\r
af37bca8 598// cart (save) RAM area (usually 0x200000 - ...)\r
599static u32 PicoRead8_sram(u32 a)\r
600{\r
af37bca8 601 u32 d;\r
88fd63ad 602 if (Pico.sv.start <= a && a <= Pico.sv.end && (Pico.m.sram_reg & SRR_MAPPED))\r
af37bca8 603 {\r
88fd63ad 604 if (Pico.sv.flags & SRF_EEPROM) {\r
af37bca8 605 d = EEPROM_read();\r
45f2f245 606 if (!(a & 1))\r
607 d >>= 8;\r
78d817c3 608 d &= 0xff;\r
45f2f245 609 } else\r
88fd63ad 610 d = *(u8 *)(Pico.sv.data - Pico.sv.start + a);\r
45f2f245 611 elprintf(EL_SRAMIO, "sram r8 [%06x] %02x @ %06x", a, d, SekPc);\r
af37bca8 612 return d;\r
613 }\r
cc68a136 614\r
45f2f245 615 // XXX: this is banking unfriendly\r
af37bca8 616 if (a < Pico.romsize)\r
57c5a5e5 617 return Pico.rom[MEM_BE2(a)];\r
af37bca8 618 \r
619 return m68k_unmapped_read8(a);\r
620}\r
cc68a136 621\r
af37bca8 622static u32 PicoRead16_sram(u32 a)\r
cc68a136 623{\r
af37bca8 624 u32 d;\r
88fd63ad 625 if (Pico.sv.start <= a && a <= Pico.sv.end && (Pico.m.sram_reg & SRR_MAPPED))\r
af37bca8 626 {\r
88fd63ad 627 if (Pico.sv.flags & SRF_EEPROM)\r
af37bca8 628 d = EEPROM_read();\r
45f2f245 629 else {\r
88fd63ad 630 u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a);\r
af37bca8 631 d = pm[0] << 8;\r
632 d |= pm[1];\r
633 }\r
634 elprintf(EL_SRAMIO, "sram r16 [%06x] %04x @ %06x", a, d, SekPc);\r
635 return d;\r
636 }\r
cc68a136 637\r
af37bca8 638 if (a < Pico.romsize)\r
639 return *(u16 *)(Pico.rom + a);\r
cc68a136 640\r
af37bca8 641 return m68k_unmapped_read16(a);\r
642}\r
cc68a136 643\r
0ace9b9a 644#endif // _ASM_MEMORY_C\r
645\r
af37bca8 646static void PicoWrite8_sram(u32 a, u32 d)\r
647{\r
88fd63ad 648 if (a > Pico.sv.end || a < Pico.sv.start || !(Pico.m.sram_reg & SRR_MAPPED)) {\r
45f2f245 649 m68k_unmapped_write8(a, d);\r
650 return;\r
651 }\r
652\r
653 elprintf(EL_SRAMIO, "sram w8 [%06x] %02x @ %06x", a, d & 0xff, SekPc);\r
88fd63ad 654 if (Pico.sv.flags & SRF_EEPROM)\r
af37bca8 655 {\r
45f2f245 656 EEPROM_write8(a, d);\r
cc68a136 657 }\r
45f2f245 658 else {\r
88fd63ad 659 u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a);\r
af37bca8 660 if (*pm != (u8)d) {\r
88fd63ad 661 Pico.sv.changed = 1;\r
af37bca8 662 *pm = (u8)d;\r
663 }\r
664 }\r
665}\r
cc68a136 666\r
af37bca8 667static void PicoWrite16_sram(u32 a, u32 d)\r
668{\r
88fd63ad 669 if (a > Pico.sv.end || a < Pico.sv.start || !(Pico.m.sram_reg & SRR_MAPPED)) {\r
45f2f245 670 m68k_unmapped_write16(a, d);\r
671 return;\r
672 }\r
673\r
674 elprintf(EL_SRAMIO, "sram w16 [%06x] %04x @ %06x", a, d & 0xffff, SekPc);\r
88fd63ad 675 if (Pico.sv.flags & SRF_EEPROM)\r
45f2f245 676 {\r
677 EEPROM_write16(d);\r
678 }\r
679 else {\r
88fd63ad 680 u8 *pm = (u8 *)(Pico.sv.data - Pico.sv.start + a);\r
1dd0871f 681 if (pm[0] != (u8)(d >> 8)) {\r
88fd63ad 682 Pico.sv.changed = 1;\r
1dd0871f 683 pm[0] = (u8)(d >> 8);\r
684 }\r
685 if (pm[1] != (u8)d) {\r
88fd63ad 686 Pico.sv.changed = 1;\r
1dd0871f 687 pm[1] = (u8)d;\r
45f2f245 688 }\r
689 }\r
af37bca8 690}\r
cc68a136 691\r
af37bca8 692// z80 area (0xa00000 - 0xa0ffff)\r
693// TODO: verify mirrors VDP and bank reg (bank area mirroring verified)\r
694static u32 PicoRead8_z80(u32 a)\r
695{\r
9f29605f 696 u32 d;\r
71f9c68f 697 if ((Pico.m.z80Run | Pico.m.z80_reset | (z80_cycles_from_68k() < Pico.t.z80c_cnt)) &&\r
698 !(PicoIn.quirks & PQUIRK_NO_Z80_BUS_LOCK)) {\r
af37bca8 699 elprintf(EL_ANOMALY, "68k z80 read with no bus! [%06x] @ %06x", a, SekPc);\r
9f29605f 700 return (u8)PicoRead16_floating(a);\r
af37bca8 701 }\r
a17fb021 702 SekCyclesBurnRun(1);\r
c060a9ab 703\r
133006a9 704 if ((a & 0x4000) == 0x0000) {\r
88fd63ad 705 d = PicoMem.zram[a & 0x1fff];\r
133006a9 706 } else if ((a & 0x6000) == 0x4000) // 0x4000-0x5fff\r
af37bca8 707 d = ym2612_read_local_68k(); \r
9f29605f 708 else {\r
af37bca8 709 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);\r
9f29605f 710 d = (u8)PicoRead16_floating(a);\r
711 }\r
af37bca8 712 return d;\r
713}\r
b542be46 714\r
af37bca8 715static u32 PicoRead16_z80(u32 a)\r
716{\r
717 u32 d = PicoRead8_z80(a);\r
718 return d | (d << 8);\r
719}\r
720\r
721static void PicoWrite8_z80(u32 a, u32 d)\r
722{\r
a17fb021 723 if ((Pico.m.z80Run | Pico.m.z80_reset) && !(PicoIn.quirks & PQUIRK_NO_Z80_BUS_LOCK)) {\r
af37bca8 724 // verified on real hw\r
725 elprintf(EL_ANOMALY, "68k z80 write with no bus or reset! [%06x] %02x @ %06x", a, d&0xff, SekPc);\r
726 return;\r
727 }\r
a17fb021 728 SekCyclesBurnRun(1);\r
af37bca8 729\r
730 if ((a & 0x4000) == 0x0000) { // z80 RAM\r
88fd63ad 731 PicoMem.zram[a & 0x1fff] = (u8)d;\r
af37bca8 732 return;\r
733 }\r
734 if ((a & 0x6000) == 0x4000) { // FM Sound\r
93f9619e 735 if (PicoIn.opt & POPT_EN_FM)\r
8ac9ab7f 736 ym2612_write_local(a & 3, d & 0xff, 0);\r
af37bca8 737 return;\r
738 }\r
739 // TODO: probably other VDP access too? Maybe more mirrors?\r
740 if ((a & 0x7ff9) == 0x7f11) { // PSG Sound\r
5d638db0 741 psg_write_68k(d);\r
af37bca8 742 return;\r
743 }\r
af37bca8 744 if ((a & 0x7f00) == 0x6000) // Z80 BANK register\r
745 {\r
746 Pico.m.z80_bank68k >>= 1;\r
747 Pico.m.z80_bank68k |= d << 8;\r
748 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one\r
749 elprintf(EL_Z80BNK, "z80 bank=%06x", Pico.m.z80_bank68k << 15);\r
750 return;\r
cc68a136 751 }\r
af37bca8 752 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @ %06x", a, d&0xff, SekPc);\r
cc68a136 753}\r
754\r
af37bca8 755static void PicoWrite16_z80(u32 a, u32 d)\r
cc68a136 756{\r
af37bca8 757 // for RAM, only most significant byte is sent\r
758 // TODO: verify remaining accesses\r
759 PicoWrite8_z80(a, d >> 8);\r
760}\r
cc68a136 761\r
0ace9b9a 762#ifndef _ASM_MEMORY_C\r
763\r
af37bca8 764// IO/control area (0xa10000 - 0xa1ffff)\r
765u32 PicoRead8_io(u32 a)\r
766{\r
767 u32 d;\r
cc68a136 768\r
af37bca8 769 if ((a & 0xffe0) == 0x0000) { // I/O ports\r
770 d = io_ports_read(a);\r
cc68a136 771 goto end;\r
772 }\r
cc68a136 773\r
9f29605f 774 d = PicoRead16_floating(a);\r
cc68a136 775\r
5e89f0f5 776 if ((a & 0xfc00) == 0x1000) {\r
5e89f0f5 777 if ((a & 0xff01) == 0x1100) { // z80 busreq (verified)\r
d97d056c 778 // bit8 seems to be readable in this range\r
779 if (!(a & 1)) {\r
780 d &= ~0x01;\r
71f9c68f 781 // Z80 ahead of 68K only if in BUSREQ, BUSACK only after 68K reached Z80\r
a17fb021 782 d |= (z80_cycles_from_68k() < Pico.t.z80c_cnt);\r
d97d056c 783 d |= (Pico.m.z80Run | Pico.m.z80_reset) & 1;\r
784 elprintf(EL_BUSREQ, "get_zrun: %02x [%u] @%06x", d, SekCyclesDone(), SekPc);\r
785 }\r
5e89f0f5 786 }\r
af37bca8 787 goto end;\r
cc68a136 788 }\r
af37bca8 789\r
93f9619e 790 d = PicoRead8_32x(a);\r
be2c4208 791\r
af37bca8 792end:\r
cc68a136 793 return d;\r
794}\r
795\r
af37bca8 796u32 PicoRead16_io(u32 a)\r
cc68a136 797{\r
af37bca8 798 u32 d;\r
cc68a136 799\r
af37bca8 800 if ((a & 0xffe0) == 0x0000) { // I/O ports\r
801 d = io_ports_read(a);\r
bdd6a009 802 d |= d << 8;\r
cc68a136 803 goto end;\r
804 }\r
805\r
9f29605f 806 d = PicoRead16_floating(a);\r
cc68a136 807\r
af37bca8 808 // bit8 seems to be readable in this range\r
5e89f0f5 809 if ((a & 0xfc00) == 0x1000) {\r
5e89f0f5 810 if ((a & 0xff00) == 0x1100) { // z80 busreq\r
d97d056c 811 d &= ~0x0100;\r
a17fb021 812 d |= (z80_cycles_from_68k() < Pico.t.z80c_cnt) << 8;\r
5e89f0f5 813 d |= ((Pico.m.z80Run | Pico.m.z80_reset) & 1) << 8;\r
ebd70cb5 814 elprintf(EL_BUSREQ, "get_zrun: %04x [%u] @%06x", d, SekCyclesDone(), SekPc);\r
5e89f0f5 815 }\r
af37bca8 816 goto end;\r
cc68a136 817 }\r
af37bca8 818\r
93f9619e 819 d = PicoRead16_32x(a);\r
be2c4208 820\r
af37bca8 821end:\r
cc68a136 822 return d;\r
823}\r
cc68a136 824\r
af37bca8 825void PicoWrite8_io(u32 a, u32 d)\r
826{\r
827 if ((a & 0xffe1) == 0x0001) { // I/O ports (verified: only LSB!)\r
828 io_ports_write(a, d);\r
829 return;\r
830 }\r
831 if ((a & 0xff01) == 0x1100) { // z80 busreq\r
832 ctl_write_z80busreq(d);\r
833 return;\r
834 }\r
835 if ((a & 0xff01) == 0x1200) { // z80 reset\r
836 ctl_write_z80reset(d);\r
837 return;\r
838 }\r
839 if (a == 0xa130f1) { // sram access register\r
840 elprintf(EL_SRAMIO, "sram reg=%02x", d);\r
45f2f245 841 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);\r
842 Pico.m.sram_reg |= (u8)(d & 3);\r
af37bca8 843 return;\r
844 }\r
93f9619e 845 PicoWrite8_32x(a, d);\r
af37bca8 846}\r
cc68a136 847\r
af37bca8 848void PicoWrite16_io(u32 a, u32 d)\r
cc68a136 849{\r
af37bca8 850 if ((a & 0xffe0) == 0x0000) { // I/O ports (verified: only LSB!)\r
851 io_ports_write(a, d);\r
852 return;\r
853 }\r
854 if ((a & 0xff00) == 0x1100) { // z80 busreq\r
855 ctl_write_z80busreq(d >> 8);\r
856 return;\r
857 }\r
858 if ((a & 0xff00) == 0x1200) { // z80 reset\r
859 ctl_write_z80reset(d >> 8);\r
860 return;\r
861 }\r
862 if (a == 0xa130f0) { // sram access register\r
863 elprintf(EL_SRAMIO, "sram reg=%02x", d);\r
45f2f245 864 Pico.m.sram_reg &= ~(SRR_MAPPED|SRR_READONLY);\r
865 Pico.m.sram_reg |= (u8)(d & 3);\r
af37bca8 866 return;\r
867 }\r
93f9619e 868 PicoWrite16_32x(a, d);\r
af37bca8 869}\r
cc68a136 870\r
0ace9b9a 871#endif // _ASM_MEMORY_C\r
872\r
af37bca8 873// VDP area (0xc00000 - 0xdfffff)\r
874// TODO: verify if lower byte goes to PSG on word writes\r
75b84e4b 875u32 PicoRead8_vdp(u32 a)\r
af37bca8 876{\r
9f29605f 877 u32 d;\r
75b84e4b 878 if ((a & 0x00f0) == 0x0000) {\r
879 switch (a & 0x0d)\r
880 {\r
d97d056c 881 case 0x00: d = PicoVideoRead8DataH(0); break;\r
882 case 0x01: d = PicoVideoRead8DataL(0); break;\r
883 case 0x04: d = PicoVideoRead8CtlH(0); break;\r
884 case 0x05: d = PicoVideoRead8CtlL(0); break;\r
75b84e4b 885 case 0x08:\r
d97d056c 886 case 0x0c: d = PicoVideoRead8HV_H(0); break;\r
75b84e4b 887 case 0x09:\r
d97d056c 888 case 0x0d: d = PicoVideoRead8HV_L(0); break;\r
9f29605f 889 default: d = (u8)PicoRead16_floating(a); break;\r
75b84e4b 890 }\r
9f29605f 891 } else {\r
d97d056c 892 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);\r
9f29605f 893 d = (u8)PicoRead16_floating(a);\r
894 }\r
d97d056c 895 return d;\r
cc68a136 896}\r
897\r
af37bca8 898static u32 PicoRead16_vdp(u32 a)\r
cc68a136 899{\r
af37bca8 900 if ((a & 0x00e0) == 0x0000)\r
901 return PicoVideoRead(a);\r
cc68a136 902\r
af37bca8 903 elprintf(EL_UIO|EL_ANOMALY, "68k bad read [%06x] @%06x", a, SekPc);\r
904 return 0;\r
cc68a136 905}\r
906\r
af37bca8 907static void PicoWrite8_vdp(u32 a, u32 d)\r
cc68a136 908{\r
af37bca8 909 if ((a & 0x00f9) == 0x0011) { // PSG Sound\r
5d638db0 910 psg_write_68k(d);\r
cc68a136 911 return;\r
912 }\r
af37bca8 913 if ((a & 0x00e0) == 0x0000) {\r
914 d &= 0xff;\r
915 PicoVideoWrite(a, d | (d << 8));\r
b542be46 916 return;\r
917 }\r
918\r
af37bca8 919 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %02x @%06x", a, d & 0xff, SekPc);\r
cc68a136 920}\r
921\r
af37bca8 922static void PicoWrite16_vdp(u32 a, u32 d)\r
923{\r
7aab4768 924 if ((a & 0x00f9) == 0x0010) { // PSG Sound\r
5d638db0 925 psg_write_68k(d);\r
7aab4768 926 return;\r
927 }\r
af37bca8 928 if ((a & 0x00e0) == 0x0000) {\r
929 PicoVideoWrite(a, d);\r
930 return;\r
931 }\r
932\r
933 elprintf(EL_UIO|EL_ANOMALY, "68k bad write [%06x] %04x @%06x", a, d & 0xffff, SekPc);\r
934}\r
cc68a136 935\r
936// -----------------------------------------------------------------\r
f53f286a 937\r
9037e45d 938#ifdef EMU_M68K\r
939static void m68k_mem_setup(void);\r
940#endif\r
941\r
f8ef8ff7 942PICO_INTERNAL void PicoMemSetup(void)\r
943{\r
88fd63ad 944 int mask, rs, sstart, a;\r
af37bca8 945\r
946 // setup the memory map\r
947 cpu68k_map_set(m68k_read8_map, 0x000000, 0xffffff, m68k_unmapped_read8, 1);\r
948 cpu68k_map_set(m68k_read16_map, 0x000000, 0xffffff, m68k_unmapped_read16, 1);\r
949 cpu68k_map_set(m68k_write8_map, 0x000000, 0xffffff, m68k_unmapped_write8, 1);\r
950 cpu68k_map_set(m68k_write16_map, 0x000000, 0xffffff, m68k_unmapped_write16, 1);\r
951\r
952 // ROM\r
953 // align to bank size. We know ROM loader allocated enough for this\r
954 mask = (1 << M68K_MEM_SHIFT) - 1;\r
955 rs = (Pico.romsize + mask) & ~mask;\r
e1e8ca17 956 if (rs > 0xa00000) rs = 0xa00000; // max cartridge area\r
af37bca8 957 cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico.rom, 0);\r
958 cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico.rom, 0);\r
959\r
960 // Common case of on-cart (save) RAM, usually at 0x200000-...\r
88fd63ad 961 if ((Pico.sv.flags & SRF_ENABLED) && Pico.sv.data != NULL) {\r
60392bf4 962 sstart = Pico.sv.start & ~mask;\r
88fd63ad 963 rs = Pico.sv.end - sstart;\r
af37bca8 964 rs = (rs + mask) & ~mask;\r
88fd63ad 965 if (sstart + rs >= 0x1000000)\r
966 rs = 0x1000000 - sstart;\r
967 cpu68k_map_set(m68k_read8_map, sstart, sstart + rs - 1, PicoRead8_sram, 1);\r
968 cpu68k_map_set(m68k_read16_map, sstart, sstart + rs - 1, PicoRead16_sram, 1);\r
969 cpu68k_map_set(m68k_write8_map, sstart, sstart + rs - 1, PicoWrite8_sram, 1);\r
970 cpu68k_map_set(m68k_write16_map, sstart, sstart + rs - 1, PicoWrite16_sram, 1);\r
af37bca8 971 }\r
972\r
973 // Z80 region\r
974 cpu68k_map_set(m68k_read8_map, 0xa00000, 0xa0ffff, PicoRead8_z80, 1);\r
975 cpu68k_map_set(m68k_read16_map, 0xa00000, 0xa0ffff, PicoRead16_z80, 1);\r
976 cpu68k_map_set(m68k_write8_map, 0xa00000, 0xa0ffff, PicoWrite8_z80, 1);\r
977 cpu68k_map_set(m68k_write16_map, 0xa00000, 0xa0ffff, PicoWrite16_z80, 1);\r
978\r
979 // IO/control region\r
980 cpu68k_map_set(m68k_read8_map, 0xa10000, 0xa1ffff, PicoRead8_io, 1);\r
981 cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, PicoRead16_io, 1);\r
982 cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, PicoWrite8_io, 1);\r
983 cpu68k_map_set(m68k_write16_map, 0xa10000, 0xa1ffff, PicoWrite16_io, 1);\r
984\r
985 // VDP region\r
986 for (a = 0xc00000; a < 0xe00000; a += 0x010000) {\r
987 if ((a & 0xe700e0) != 0xc00000)\r
988 continue;\r
989 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, PicoRead8_vdp, 1);\r
990 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, PicoRead16_vdp, 1);\r
991 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, PicoWrite8_vdp, 1);\r
992 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, PicoWrite16_vdp, 1);\r
993 }\r
994\r
995 // RAM and it's mirrors\r
996 for (a = 0xe00000; a < 0x1000000; a += 0x010000) {\r
88fd63ad 997 cpu68k_map_set(m68k_read8_map, a, a + 0xffff, PicoMem.ram, 0);\r
998 cpu68k_map_set(m68k_read16_map, a, a + 0xffff, PicoMem.ram, 0);\r
999 cpu68k_map_set(m68k_write8_map, a, a + 0xffff, PicoMem.ram, 0);\r
1000 cpu68k_map_set(m68k_write16_map, a, a + 0xffff, PicoMem.ram, 0);\r
af37bca8 1001 }\r
1002\r
cc68a136 1003 // Setup memory callbacks:\r
70357ce5 1004#ifdef EMU_C68K\r
5e89f0f5 1005 PicoCpuCM68k.read8 = (void *)m68k_read8_map;\r
1006 PicoCpuCM68k.read16 = (void *)m68k_read16_map;\r
1007 PicoCpuCM68k.read32 = (void *)m68k_read16_map;\r
1008 PicoCpuCM68k.write8 = (void *)m68k_write8_map;\r
1009 PicoCpuCM68k.write16 = (void *)m68k_write16_map;\r
1010 PicoCpuCM68k.write32 = (void *)m68k_write16_map;\r
1011 PicoCpuCM68k.checkpc = NULL; /* unused */\r
1012 PicoCpuCM68k.fetch8 = NULL;\r
1013 PicoCpuCM68k.fetch16 = NULL;\r
1014 PicoCpuCM68k.fetch32 = NULL;\r
cc68a136 1015#endif\r
70357ce5 1016#ifdef EMU_F68K\r
4cc0fcaf 1017 PicoCpuFM68k.read_byte = (void *)m68k_read8;\r
1018 PicoCpuFM68k.read_word = (void *)m68k_read16;\r
1019 PicoCpuFM68k.read_long = (void *)m68k_read32;\r
1020 PicoCpuFM68k.write_byte = (void *)m68k_write8;\r
1021 PicoCpuFM68k.write_word = (void *)m68k_write16;\r
1022 PicoCpuFM68k.write_long = (void *)m68k_write32;\r
70357ce5 1023#endif\r
9037e45d 1024#ifdef EMU_M68K\r
1025 m68k_mem_setup();\r
1026#endif\r
c8d1e9b6 1027\r
1028 z80_mem_setup();\r
cc68a136 1029}\r
1030\r
cc68a136 1031#ifdef EMU_M68K\r
9037e45d 1032unsigned int (*pm68k_read_memory_8) (unsigned int address) = NULL;\r
1033unsigned int (*pm68k_read_memory_16)(unsigned int address) = NULL;\r
1034unsigned int (*pm68k_read_memory_32)(unsigned int address) = NULL;\r
1035void (*pm68k_write_memory_8) (unsigned int address, unsigned char value) = NULL;\r
1036void (*pm68k_write_memory_16)(unsigned int address, unsigned short value) = NULL;\r
1037void (*pm68k_write_memory_32)(unsigned int address, unsigned int value) = NULL;\r
cc68a136 1038\r
9037e45d 1039/* it appears that Musashi doesn't always mask the unused bits */\r
1040unsigned int m68k_read_memory_8 (unsigned int address) { return pm68k_read_memory_8 (address) & 0xff; }\r
1041unsigned int m68k_read_memory_16(unsigned int address) { return pm68k_read_memory_16(address) & 0xffff; }\r
1042unsigned int m68k_read_memory_32(unsigned int address) { return pm68k_read_memory_32(address); }\r
1043void m68k_write_memory_8 (unsigned int address, unsigned int value) { pm68k_write_memory_8 (address, (u8)value); }\r
1044void m68k_write_memory_16(unsigned int address, unsigned int value) { pm68k_write_memory_16(address,(u16)value); }\r
1045void m68k_write_memory_32(unsigned int address, unsigned int value) { pm68k_write_memory_32(address, value); }\r
9037e45d 1046\r
1047static void m68k_mem_setup(void)\r
1048{\r
af37bca8 1049 pm68k_read_memory_8 = m68k_read8;\r
1050 pm68k_read_memory_16 = m68k_read16;\r
1051 pm68k_read_memory_32 = m68k_read32;\r
1052 pm68k_write_memory_8 = m68k_write8;\r
1053 pm68k_write_memory_16 = m68k_write16;\r
1054 pm68k_write_memory_32 = m68k_write32;\r
cc68a136 1055}\r
cc68a136 1056#endif // EMU_M68K\r
1057\r
1058\r
4b9c5888 1059// -----------------------------------------------------------------\r
1060\r
4b9c5888 1061static int get_scanline(int is_from_z80)\r
1062{\r
1063 if (is_from_z80) {\r
f61d0a45 1064 // ugh... compute by dividing cycles since frame start by cycles per line\r
1065 // need some fractional resolution here, else there may be an extra line\r
7263343d 1066 int cycles_line = cycles_68k_to_z80((unsigned)(488.5*256))+1; // cycles per line, Q8\r
f61d0a45 1067 int cycles_z80 = (z80_cyclesLeft<0 ? Pico.t.z80c_aim:z80_cyclesDone())<<8;\r
1068 int cycles = cycles_line * Pico.t.z80_scanline;\r
1069 // approximation by multiplying with inverse\r
7263343d 1070 if (cycles_z80 - cycles >= 4*cycles_line) {\r
f61d0a45 1071 // compute 1/cycles_line, storing the result to avoid future dividing\r
1072 static int cycles_line_o, cycles_line_i;\r
1073 if (cycles_line_o != cycles_line)\r
1074 { cycles_line_o = cycles_line, cycles_line_i = (1<<22) / cycles_line; }\r
1075 // compute lines = diff/cycles_line = diff*(1/cycles_line)\r
1076 int lines = ((cycles_z80 - cycles) * cycles_line_i) >> 22;\r
1077 Pico.t.z80_scanline += lines, cycles += cycles_line * lines;\r
1078 }\r
1079 // handle any rounding leftover\r
1080 while (cycles_z80 - cycles >= cycles_line)\r
1081 Pico.t.z80_scanline ++, cycles += cycles_line;\r
88fd63ad 1082 return Pico.t.z80_scanline;\r
4b9c5888 1083 }\r
1084\r
2aa27095 1085 return Pico.m.scanline;\r
4b9c5888 1086}\r
1087\r
0e2e188e 1088#define ym2612_update_status(xcycles) \\r
83025d7a 1089 ym2612.OPN.ST.status &= ~0x80; \\r
1090 ym2612.OPN.ST.status |= (xcycles < Pico.t.ym2612_busy) * 0x80; \\r
0e2e188e 1091 if (xcycles >= Pico.t.timer_a_next_oflow) \\r
1092 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 1; \\r
1093 if (xcycles >= Pico.t.timer_b_next_oflow) \\r
1094 ym2612.OPN.ST.status |= (ym2612.OPN.ST.mode >> 2) & 2\r
1095\r
48dc74f2 1096/* probably should not be in this file, but it's near related code here */\r
43e6eaad 1097void ym2612_sync_timers(int z80_cycles, int mode_old, int mode_new)\r
1098{\r
1099 int xcycles = z80_cycles << 8;\r
1100\r
0e2e188e 1101 // update timer status\r
1102 ym2612_update_status(xcycles);\r
43e6eaad 1103\r
0e2e188e 1104 // update timer a\r
43e6eaad 1105 if (mode_old & 1)\r
0e2e188e 1106 while (xcycles >= Pico.t.timer_a_next_oflow)\r
6311a3ba 1107 Pico.t.timer_a_next_oflow += Pico.t.timer_a_step;\r
43e6eaad 1108\r
0e2e188e 1109 // turning on/off\r
1110 if ((mode_old ^ mode_new) & 1)\r
43e6eaad 1111 {\r
48dc74f2 1112 if (mode_old & 1)\r
6311a3ba 1113 Pico.t.timer_a_next_oflow = TIMER_NO_OFLOW;\r
0e2e188e 1114 else {\r
1115 /* The internal tick of the YM2612 takes 144 clock cycles (with clock\r
1116 * being OSC/7), or 67.2 z80 cycles. Timers are run once each tick.\r
1117 * Starting a timer takes place at the next tick, so xcycles needs to be\r
1118 * rounded up to that: t = next tick# = (xcycles / TICK_ZCYCLES) + 1\r
1119 */\r
80f51a1d 1120 unsigned t = ((xcycles * (((1LL<<32)/TIMER_A_TICK_ZCYCLES)+1))>>32) + 1;\r
0e2e188e 1121 Pico.t.timer_a_next_oflow = t*TIMER_A_TICK_ZCYCLES + Pico.t.timer_a_step;\r
1122 }\r
43e6eaad 1123 }\r
0e2e188e 1124\r
43e6eaad 1125 if (mode_new & 1)\r
6311a3ba 1126 elprintf(EL_YMTIMER, "timer a upd to %i @ %i", Pico.t.timer_a_next_oflow>>8, z80_cycles);\r
43e6eaad 1127\r
0e2e188e 1128 // update timer b\r
43e6eaad 1129 if (mode_old & 2)\r
0e2e188e 1130 while (xcycles >= Pico.t.timer_b_next_oflow)\r
6311a3ba 1131 Pico.t.timer_b_next_oflow += Pico.t.timer_b_step;\r
43e6eaad 1132\r
0e2e188e 1133 // turning on/off\r
43e6eaad 1134 if ((mode_old ^ mode_new) & 2)\r
1135 {\r
48dc74f2 1136 if (mode_old & 2)\r
6311a3ba 1137 Pico.t.timer_b_next_oflow = TIMER_NO_OFLOW;\r
0e2e188e 1138 else {\r
1139 /* timer b has a divider of 16 which runs in its own counter. It is not\r
1140 * reset by loading timer b. The first run of timer b after loading is\r
1141 * therefore shorter by up to 15 ticks.\r
1142 */\r
80f51a1d 1143 unsigned t = ((xcycles * (((1LL<<32)/TIMER_A_TICK_ZCYCLES)+1))>>32) + 1;\r
0e2e188e 1144 int step = Pico.t.timer_b_step - TIMER_A_TICK_ZCYCLES*(t&15);\r
1145 Pico.t.timer_b_next_oflow = t*TIMER_A_TICK_ZCYCLES + step;\r
1146 }\r
43e6eaad 1147 }\r
0e2e188e 1148\r
43e6eaad 1149 if (mode_new & 2)\r
6311a3ba 1150 elprintf(EL_YMTIMER, "timer b upd to %i @ %i", Pico.t.timer_b_next_oflow>>8, z80_cycles);\r
43e6eaad 1151}\r
1152\r
4b9c5888 1153// ym2612 DAC and timer I/O handlers for z80\r
af37bca8 1154static int ym2612_write_local(u32 a, u32 d, int is_from_z80)\r
4b9c5888 1155{\r
83025d7a 1156 int cycles = is_from_z80 ? z80_cyclesDone() : z80_cycles_from_68k();\r
4b9c5888 1157 int addr;\r
1158\r
1159 a &= 3;\r
4b9c5888 1160 switch (a)\r
1161 {\r
1162 case 0: /* address port 0 */\r
225260ba 1163 case 2: /* address port 1 */\r
4b9c5888 1164 ym2612.OPN.ST.address = d;\r
225260ba 1165 ym2612.addr_A1 = (a & 2) >> 1;\r
4b9c5888 1166#ifdef __GP2X__\r
93f9619e 1167 if (PicoIn.opt & POPT_EXT_FM) YM2612Write_940(a, d, -1);\r
4b9c5888 1168#endif\r
1169 return 0;\r
1170\r
1171 case 1: /* data port 0 */\r
225260ba 1172 case 3: /* data port 1 */\r
1173 addr = ym2612.OPN.ST.address | ((int)ym2612.addr_A1 << 8);\r
4b9c5888 1174 ym2612.REGS[addr] = d;\r
1175\r
83025d7a 1176 // the busy flag in the YM2612 status is actually a 32 cycle timer\r
1177 // (89.6 Z80 cycles), triggered by any write to the data port.\r
f5c022a8 1178 Pico.t.ym2612_busy = (cycles << 8) + YMBUSY_ZCYCLES; // Q8 for convenience\r
83025d7a 1179\r
4b9c5888 1180 switch (addr)\r
1181 {\r
1182 case 0x24: // timer A High 8\r
1183 case 0x25: { // timer A Low 2\r
1184 int TAnew = (addr == 0x24) ? ((ym2612.OPN.ST.TA & 0x03)|(((int)d)<<2))\r
1185 : ((ym2612.OPN.ST.TA & 0x3fc)|(d&3));\r
1186 if (ym2612.OPN.ST.TA != TAnew)\r
1187 {\r
c3d70d13 1188 ym2612_sync_timers(cycles, ym2612.OPN.ST.mode, ym2612.OPN.ST.mode);\r
4b9c5888 1189 //elprintf(EL_STATUS, "timer a set %i", TAnew);\r
1190 ym2612.OPN.ST.TA = TAnew;\r
e53704e6 1191 //ym2612.OPN.ST.TAC = (1024-TAnew)*18;\r
4b9c5888 1192 //ym2612.OPN.ST.TAT = 0;\r
7263343d 1193 Pico.t.timer_a_step = TIMER_A_TICK_ZCYCLES * (1024 - TAnew);\r
6311a3ba 1194 elprintf(EL_YMTIMER, "timer a set to %i, %i", 1024 - TAnew, Pico.t.timer_a_next_oflow>>8);\r
4b9c5888 1195 }\r
1196 return 0;\r
1197 }\r
1198 case 0x26: // timer B\r
1199 if (ym2612.OPN.ST.TB != d) {\r
c3d70d13 1200 ym2612_sync_timers(cycles, ym2612.OPN.ST.mode, ym2612.OPN.ST.mode);\r
4b9c5888 1201 //elprintf(EL_STATUS, "timer b set %i", d);\r
1202 ym2612.OPN.ST.TB = d;\r
e53704e6 1203 //ym2612.OPN.ST.TBC = (256-d) * 288;\r
4b9c5888 1204 //ym2612.OPN.ST.TBT = 0;\r
7263343d 1205 Pico.t.timer_b_step = TIMER_B_TICK_ZCYCLES * (256 - d);\r
6311a3ba 1206 elprintf(EL_YMTIMER, "timer b set to %i, %i", 256 - d, Pico.t.timer_b_next_oflow>>8);\r
4b9c5888 1207 }\r
1208 return 0;\r
1209 case 0x27: { /* mode, timer control */\r
1210 int old_mode = ym2612.OPN.ST.mode;\r
6f7beab4 1211\r
43e6eaad 1212 elprintf(EL_YMTIMER, "st mode %02x", d);\r
1213 ym2612_sync_timers(cycles, old_mode, d);\r
4b9c5888 1214\r
0e2e188e 1215 ym2612.OPN.ST.mode = d;\r
1216\r
43e6eaad 1217 /* reset Timer a flag */\r
1218 if (d & 0x10)\r
1219 ym2612.OPN.ST.status &= ~1;\r
4b9c5888 1220\r
1221 /* reset Timer b flag */\r
1222 if (d & 0x20)\r
1223 ym2612.OPN.ST.status &= ~2;\r
1224\r
43e6eaad 1225 if ((d ^ old_mode) & 0xc0) {\r
4b9c5888 1226#ifdef __GP2X__\r
93f9619e 1227 if (PicoIn.opt & POPT_EXT_FM) return YM2612Write_940(a, d, get_scanline(is_from_z80));\r
4b9c5888 1228#endif\r
2e5cbf5b 1229 PsndDoFM(cycles);\r
43e6eaad 1230 return 1;\r
1231 }\r
4b9c5888 1232 return 0;\r
1233 }\r
225260ba 1234 case 0x2a: { /* DAC data */\r
225260ba 1235 //elprintf(EL_STATUS, "%03i dac w %08x z80 %i", cycles, d, is_from_z80);\r
1236 if (ym2612.dacen)\r
1237 PsndDoDAC(cycles);\r
1238 ym2612.dacout = ((int)d - 0x80) << 6;\r
1239 return 0;\r
1240 }\r
4b9c5888 1241 case 0x2b: { /* DAC Sel (YM2612) */\r
43e14010 1242 ym2612.dacen = d & 0x80;\r
4b9c5888 1243#ifdef __GP2X__\r
43e14010 1244 if (PicoIn.opt & POPT_EXT_FM) YM2612Write_940(a, d, get_scanline(is_from_z80));\r
4b9c5888 1245#endif\r
1246 return 0;\r
1247 }\r
1248 }\r
1249 break;\r
4b9c5888 1250 }\r
1251\r
1252#ifdef __GP2X__\r
93f9619e 1253 if (PicoIn.opt & POPT_EXT_FM)\r
4b9c5888 1254 return YM2612Write_940(a, d, get_scanline(is_from_z80));\r
1255#endif\r
83025d7a 1256 PsndDoFM(cycles);\r
4b9c5888 1257 return YM2612Write_(a, d);\r
1258}\r
1259\r
453d2a6e 1260\r
553c3eaa 1261static u32 ym2612_read_local_z80(void)\r
4b9c5888 1262{\r
1263 int xcycles = z80_cyclesDone() << 8;\r
4b9c5888 1264\r
0e2e188e 1265 ym2612_update_status(xcycles);\r
43e6eaad 1266\r
6311a3ba 1267 elprintf(EL_YMTIMER, "timer z80 read %i, sched %i, %i @ %i|%i",\r
1268 ym2612.OPN.ST.status, Pico.t.timer_a_next_oflow >> 8,\r
1269 Pico.t.timer_b_next_oflow >> 8, xcycles >> 8, (xcycles >> 8) / 228);\r
43e6eaad 1270 return ym2612.OPN.ST.status;\r
1271}\r
1272\r
af37bca8 1273static u32 ym2612_read_local_68k(void)\r
43e6eaad 1274{\r
ae214f1c 1275 int xcycles = z80_cycles_from_68k() << 8;\r
43e6eaad 1276\r
0e2e188e 1277 ym2612_update_status(xcycles);\r
43e6eaad 1278\r
6311a3ba 1279 elprintf(EL_YMTIMER, "timer 68k read %i, sched %i, %i @ %i|%i",\r
1280 ym2612.OPN.ST.status, Pico.t.timer_a_next_oflow >> 8,\r
1281 Pico.t.timer_b_next_oflow >> 8, xcycles >> 8, (xcycles >> 8) / 228);\r
4b9c5888 1282 return ym2612.OPN.ST.status;\r
1283}\r
1284\r
d2721b08 1285void ym2612_pack_state(void)\r
1286{\r
e53704e6 1287 // timers are saved as tick counts, in 16.16 int format\r
f5c022a8 1288 int tac, tat = 0, tbc, tbt = 0, busy = 0;\r
e53704e6 1289 tac = 1024 - ym2612.OPN.ST.TA;\r
1290 tbc = 256 - ym2612.OPN.ST.TB;\r
f5c022a8 1291 if (Pico.t.ym2612_busy > 0)\r
1292 busy = cycles_z80_to_68k(Pico.t.ym2612_busy);\r
6311a3ba 1293 if (Pico.t.timer_a_next_oflow != TIMER_NO_OFLOW)\r
1294 tat = (int)((double)(Pico.t.timer_a_step - Pico.t.timer_a_next_oflow)\r
1295 / (double)Pico.t.timer_a_step * tac * 65536);\r
1296 if (Pico.t.timer_b_next_oflow != TIMER_NO_OFLOW)\r
1297 tbt = (int)((double)(Pico.t.timer_b_step - Pico.t.timer_b_next_oflow)\r
1298 / (double)Pico.t.timer_b_step * tbc * 65536);\r
e53704e6 1299 elprintf(EL_YMTIMER, "save: timer a %i/%i", tat >> 16, tac);\r
1300 elprintf(EL_YMTIMER, "save: timer b %i/%i", tbt >> 16, tbc);\r
1301\r
d2721b08 1302#ifdef __GP2X__\r
93f9619e 1303 if (PicoIn.opt & POPT_EXT_FM)\r
e4fb433c 1304 YM2612PicoStateSave2_940(tat, tbt);\r
d2721b08 1305 else\r
1306#endif\r
f5c022a8 1307 YM2612PicoStateSave2(tat, tbt, busy);\r
d2721b08 1308}\r
1309\r
453d2a6e 1310void ym2612_unpack_state(void)\r
1311{\r
f5c022a8 1312 int i, ret, tac, tat, tbc, tbt, busy = 0;\r
453d2a6e 1313 YM2612PicoStateLoad();\r
1314\r
1315 // feed all the registers and update internal state\r
db49317b 1316 for (i = 0x20; i < 0xA0; i++) {\r
453d2a6e 1317 ym2612_write_local(0, i, 0);\r
1318 ym2612_write_local(1, ym2612.REGS[i], 0);\r
1319 }\r
db49317b 1320 for (i = 0x30; i < 0xA0; i++) {\r
1321 ym2612_write_local(2, i, 0);\r
1322 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
1323 }\r
1324 for (i = 0xAF; i >= 0xA0; i--) { // must apply backwards\r
1325 ym2612_write_local(2, i, 0);\r
1326 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
1327 ym2612_write_local(0, i, 0);\r
1328 ym2612_write_local(1, ym2612.REGS[i], 0);\r
1329 }\r
1330 for (i = 0xB0; i < 0xB8; i++) {\r
1331 ym2612_write_local(0, i, 0);\r
1332 ym2612_write_local(1, ym2612.REGS[i], 0);\r
453d2a6e 1333 ym2612_write_local(2, i, 0);\r
1334 ym2612_write_local(3, ym2612.REGS[i|0x100], 0);\r
1335 }\r
d2721b08 1336\r
1337#ifdef __GP2X__\r
93f9619e 1338 if (PicoIn.opt & POPT_EXT_FM)\r
e4fb433c 1339 ret = YM2612PicoStateLoad2_940(&tat, &tbt);\r
d2721b08 1340 else\r
1341#endif\r
f5c022a8 1342 ret = YM2612PicoStateLoad2(&tat, &tbt, &busy);\r
db49317b 1343 if (ret != 0) {\r
1344 elprintf(EL_STATUS, "old ym2612 state");\r
1345 return; // no saved timers\r
1346 }\r
e53704e6 1347\r
f5c022a8 1348 Pico.t.ym2612_busy = cycles_68k_to_z80(busy);\r
e53704e6 1349 tac = (1024 - ym2612.OPN.ST.TA) << 16;\r
1350 tbc = (256 - ym2612.OPN.ST.TB) << 16;\r
1351 if (ym2612.OPN.ST.mode & 1)\r
6311a3ba 1352 Pico.t.timer_a_next_oflow = (int)((double)(tac - tat) / (double)tac * Pico.t.timer_a_step);\r
e53704e6 1353 else\r
6311a3ba 1354 Pico.t.timer_a_next_oflow = TIMER_NO_OFLOW;\r
e53704e6 1355 if (ym2612.OPN.ST.mode & 2)\r
6311a3ba 1356 Pico.t.timer_b_next_oflow = (int)((double)(tbc - tbt) / (double)tbc * Pico.t.timer_b_step);\r
e53704e6 1357 else\r
6311a3ba 1358 Pico.t.timer_b_next_oflow = TIMER_NO_OFLOW;\r
1359 elprintf(EL_YMTIMER, "load: %i/%i, timer_a_next_oflow %i", tat>>16, tac>>16, Pico.t.timer_a_next_oflow >> 8);\r
1360 elprintf(EL_YMTIMER, "load: %i/%i, timer_b_next_oflow %i", tbt>>16, tbc>>16, Pico.t.timer_b_next_oflow >> 8);\r
453d2a6e 1361}\r
1362\r
f3a57b2d 1363#if defined(NO_32X) && defined(_ASM_MEMORY_C)\r
1364// referenced by asm code\r
1365u32 PicoRead8_32x(u32 a) { return 0; }\r
1366u32 PicoRead16_32x(u32 a) { return 0; }\r
1367void PicoWrite8_32x(u32 a, u32 d) {}\r
1368void PicoWrite16_32x(u32 a, u32 d) {}\r
1369#endif\r
1370\r
cc68a136 1371// -----------------------------------------------------------------\r
1372// z80 memhandlers\r
1373\r
e0c4dac1 1374static void access_68k_bus(int delay) // bus delay as Q8\r
1375{\r
a17fb021 1376 // TODO: if the 68K is in DMA wait, Z80 has to wait until DMA ends\r
a17fb021 1377\r
e0c4dac1 1378 // 68k bus access delay for z80. The fractional part needs to be accumulated\r
1379 // until an additional cycle is full. That is then added to the integer part.\r
71f9c68f 1380 Pico.t.z80_busdelay += (delay&0xff); // accumulate\r
e0c4dac1 1381 z80_subCLeft((delay>>8) + (Pico.t.z80_busdelay>>8));\r
71f9c68f 1382 Pico.t.z80_busdelay &= 0xff; // leftover cycle fraction\r
7263343d 1383 // don't use SekCyclesBurn() here since the Z80 doesn't run in cycle lock to\r
e0c4dac1 1384 // the 68K. Count the stolen cycles to be accounted later in the 68k CPU runs\r
c5ecd7a0 1385 Pico.t.z80_buscycles += 8; // TODO <=8.4 for Rick 2, but >=8.9 for misc_test\r
e0c4dac1 1386}\r
1387\r
553c3eaa 1388static unsigned char z80_md_vdp_read(unsigned short a)\r
cc68a136 1389{\r
133006a9 1390 if ((a & 0xff00) == 0x7f00) {\r
7263343d 1391 // 68k bus access delay=3.3 per kabuto, for notaz picotest 2.42<delay<2.57?\r
1392 access_68k_bus(0x280); // Q8, picotest: 0x26d(>2.42) - 0x292(<2.57)\r
d1b8bcc6 1393\r
75b84e4b 1394 switch (a & 0x0d)\r
1395 {\r
1613ec6c 1396 case 0x00: return PicoVideoRead8DataH(1);\r
1397 case 0x01: return PicoVideoRead8DataL(1);\r
1398 case 0x04: return PicoVideoRead8CtlH(1);\r
1399 case 0x05: return PicoVideoRead8CtlL(1);\r
75b84e4b 1400 case 0x08:\r
46b4c1d3 1401 case 0x0c: return PicoVideoGetV(get_scanline(1), 1);\r
75b84e4b 1402 case 0x09:\r
1403 case 0x0d: return Pico.m.rotate++;\r
1404 }\r
1405 }\r
b0e08dff 1406\r
c8d1e9b6 1407 elprintf(EL_ANOMALY, "z80 invalid r8 [%06x] %02x", a, 0xff);\r
1408 return 0xff;\r
1409}\r
cc68a136 1410\r
553c3eaa 1411static unsigned char z80_md_bank_read(unsigned short a)\r
c8d1e9b6 1412{\r
c8d1e9b6 1413 unsigned int addr68k;\r
a17fb021 1414 unsigned char ret = 0xff;\r
cc68a136 1415\r
7263343d 1416 // 68k bus access delay=3.3 per kabuto, but for notaz picotest 3.02<delay<3.32\r
1417 access_68k_bus(0x340); // Q8, picotest: 0x306(>3.02)-0x351(<3.32)\r
d1b8bcc6 1418\r
1419 addr68k = Pico.m.z80_bank68k << 15;\r
1420 addr68k |= a & 0x7fff;\r
c8d1e9b6 1421\r
a17fb021 1422 if (addr68k < 0xe00000) // can't read from 68K RAM\r
1423 ret = m68k_read8(addr68k);\r
cc68a136 1424\r
c8d1e9b6 1425 elprintf(EL_Z80BNK, "z80->68k r8 [%06x] %02x", addr68k, ret);\r
cc68a136 1426 return ret;\r
1427}\r
1428\r
553c3eaa 1429static void z80_md_ym2612_write(unsigned int a, unsigned char data)\r
cc68a136 1430{\r
93f9619e 1431 if (PicoIn.opt & POPT_EN_FM)\r
8ac9ab7f 1432 ym2612_write_local(a, data, 1);\r
c8d1e9b6 1433}\r
cc68a136 1434\r
553c3eaa 1435static void z80_md_vdp_br_write(unsigned int a, unsigned char data)\r
c8d1e9b6 1436{\r
c8d1e9b6 1437 if ((a&0xfff9) == 0x7f11) // 7f11 7f13 7f15 7f17\r
cc68a136 1438 {\r
5d638db0 1439 psg_write_z80(data);\r
cc68a136 1440 return;\r
1441 }\r
b0e08dff 1442 // at least VDP data writes hang my machine\r
cc68a136 1443\r
c8d1e9b6 1444 if ((a>>8) == 0x60)\r
cc68a136 1445 {\r
c8d1e9b6 1446 Pico.m.z80_bank68k >>= 1;\r
1447 Pico.m.z80_bank68k |= data << 8;\r
1448 Pico.m.z80_bank68k &= 0x1ff; // 9 bits and filled in the new top one\r
cc68a136 1449 return;\r
1450 }\r
1451\r
c8d1e9b6 1452 elprintf(EL_ANOMALY, "z80 invalid w8 [%06x] %02x", a, data);\r
1453}\r
cc68a136 1454\r
553c3eaa 1455static void z80_md_bank_write(unsigned int a, unsigned char data)\r
c8d1e9b6 1456{\r
c8d1e9b6 1457 unsigned int addr68k;\r
69996cb7 1458\r
7263343d 1459 // 68k bus access delay=3.3 per kabuto, but for notaz picotest 3.02<delay<3.32\r
1460 access_68k_bus(0x340); // Q8, picotest: 0x306(>3.02)-0x351(<3.32)\r
134092fe 1461\r
c8d1e9b6 1462 addr68k = Pico.m.z80_bank68k << 15;\r
1463 addr68k += a & 0x7fff;\r
1464\r
1465 elprintf(EL_Z80BNK, "z80->68k w8 [%06x] %02x", addr68k, data);\r
af37bca8 1466 m68k_write8(addr68k, data);\r
cc68a136 1467}\r
1468\r
c8d1e9b6 1469// -----------------------------------------------------------------\r
1470\r
1471static unsigned char z80_md_in(unsigned short p)\r
a4221917 1472{\r
c8d1e9b6 1473 elprintf(EL_ANOMALY, "Z80 port %04x read", p);\r
1474 return 0xff;\r
a4221917 1475}\r
1476\r
c8d1e9b6 1477static void z80_md_out(unsigned short p, unsigned char d)\r
cc68a136 1478{\r
c8d1e9b6 1479 elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d);\r
cc68a136 1480}\r
c8d1e9b6 1481\r
af37bca8 1482static void z80_mem_setup(void)\r
c8d1e9b6 1483{\r
88fd63ad 1484 z80_map_set(z80_read_map, 0x0000, 0x1fff, PicoMem.zram, 0);\r
1485 z80_map_set(z80_read_map, 0x2000, 0x3fff, PicoMem.zram, 0);\r
c8d1e9b6 1486 z80_map_set(z80_read_map, 0x4000, 0x5fff, ym2612_read_local_z80, 1);\r
1487 z80_map_set(z80_read_map, 0x6000, 0x7fff, z80_md_vdp_read, 1);\r
1488 z80_map_set(z80_read_map, 0x8000, 0xffff, z80_md_bank_read, 1);\r
1489\r
88fd63ad 1490 z80_map_set(z80_write_map, 0x0000, 0x1fff, PicoMem.zram, 0);\r
1491 z80_map_set(z80_write_map, 0x2000, 0x3fff, PicoMem.zram, 0);\r
c8d1e9b6 1492 z80_map_set(z80_write_map, 0x4000, 0x5fff, z80_md_ym2612_write, 1);\r
1493 z80_map_set(z80_write_map, 0x6000, 0x7fff, z80_md_vdp_br_write, 1);\r
1494 z80_map_set(z80_write_map, 0x8000, 0xffff, z80_md_bank_write, 1);\r
1495\r
1496#ifdef _USE_DRZ80\r
1497 drZ80.z80_in = z80_md_in;\r
1498 drZ80.z80_out = z80_md_out;\r
1499#endif\r
1500#ifdef _USE_CZ80\r
c8d1e9b6 1501 Cz80_Set_INPort(&CZ80, z80_md_in);\r
1502 Cz80_Set_OUTPort(&CZ80, z80_md_out);\r
a4221917 1503#endif\r
c8d1e9b6 1504}\r
cc68a136 1505\r
531a8f38 1506// vim:shiftwidth=2:ts=2:expandtab\r