reverted useless threaded buffering code
[picodrive.git] / Pico / cd / Memory.c
... / ...
CommitLineData
1// Memory I/O handlers for Sega/Mega CD.\r
2// Loosely based on Gens code.\r
3// (c) Copyright 2007, Grazvydas "notaz" Ignotas\r
4\r
5\r
6#include "../PicoInt.h"\r
7\r
8#include "../sound/ym2612.h"\r
9#include "../sound/sn76496.h"\r
10\r
11#include "gfx_cd.h"\r
12#include "pcm.h"\r
13\r
14#ifndef UTYPES_DEFINED\r
15typedef unsigned char u8;\r
16typedef unsigned short u16;\r
17typedef unsigned int u32;\r
18#define UTYPES_DEFINED\r
19#endif\r
20\r
21#ifdef _MSC_VER\r
22#define rdprintf\r
23#define wrdprintf\r
24#else\r
25//#define rdprintf dprintf\r
26#define rdprintf(...)\r
27//#define wrdprintf dprintf\r
28#define wrdprintf(...)\r
29#endif\r
30\r
31#ifdef EMU_CORE_DEBUG\r
32extern u32 lastread_a, lastread_d[16], lastwrite_cyc_d[16];\r
33extern int lrp_cyc, lwp_cyc;\r
34#undef USE_POLL_DETECT\r
35#endif\r
36\r
37// -----------------------------------------------------------------\r
38\r
39// poller detection\r
40#define POLL_LIMIT 16\r
41#define POLL_CYCLES 124\r
42// int m68k_poll_addr, m68k_poll_cnt;\r
43unsigned int s68k_poll_adclk, s68k_poll_cnt;\r
44\r
45#ifndef _ASM_CD_MEMORY_C\r
46static u32 m68k_reg_read16(u32 a)\r
47{\r
48 u32 d=0;\r
49 a &= 0x3e;\r
50 // dprintf("m68k_regs r%2i: [%02x] @%06x", realsize&~1, a+(realsize&1), SekPc);\r
51\r
52 switch (a) {\r
53 case 0:\r
54 d = ((Pico_mcd->s68k_regs[0x33]<<13)&0x8000) | Pico_mcd->m.busreq; // here IFL2 is always 0, just like in Gens\r
55 goto end;\r
56 case 2:\r
57 d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0xc7);\r
58 // the DMNA delay must only be visible on s68k side (Lunar2, Silpheed)\r
59 if (Pico_mcd->m.state_flags&2) { d &= ~1; d |= 2; }\r
60 //printf("m68k_regs r3: %02x @%06x\n", (u8)d, SekPc);\r
61 goto end;\r
62 case 4:\r
63 d = Pico_mcd->s68k_regs[4]<<8;\r
64 goto end;\r
65 case 6:\r
66 d = *(u16 *)(Pico_mcd->bios + 0x72);\r
67 goto end;\r
68 case 8:\r
69 d = Read_CDC_Host(0);\r
70 goto end;\r
71 case 0xA:\r
72 elprintf(EL_UIO, "m68k FIXME: reserved read");\r
73 goto end;\r
74 case 0xC:\r
75 d = Pico_mcd->m.timer_stopwatch >> 16;\r
76 dprintf("m68k stopwatch timer read (%04x)", d);\r
77 goto end;\r
78 }\r
79\r
80 if (a < 0x30) {\r
81 // comm flag/cmd/status (0xE-0x2F)\r
82 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];\r
83 goto end;\r
84 }\r
85\r
86 elprintf(EL_UIO, "m68k_regs FIXME invalid read @ %02x", a);\r
87\r
88end:\r
89\r
90 return d;\r
91}\r
92#endif\r
93\r
94#ifndef _ASM_CD_MEMORY_C\r
95static\r
96#endif\r
97void m68k_reg_write8(u32 a, u32 d)\r
98{\r
99 a &= 0x3f;\r
100 // dprintf("m68k_regs w%2i: [%02x] %02x @%06x", realsize, a, d, SekPc);\r
101\r
102 switch (a) {\r
103 case 0:\r
104 d &= 1;\r
105 if ((d&1) && (Pico_mcd->s68k_regs[0x33]&(1<<2))) { elprintf(EL_INTS, "m68k: s68k irq 2"); SekInterruptS68k(2); }\r
106 return;\r
107 case 1:\r
108 d &= 3;\r
109 if (!(d&1)) Pico_mcd->m.state_flags |= 1; // reset pending, needed to be sure we fetch the right vectors on reset\r
110 if ( (Pico_mcd->m.busreq&1) != (d&1)) dprintf("m68k: s68k reset %i", !(d&1));\r
111 if ( (Pico_mcd->m.busreq&2) != (d&2)) dprintf("m68k: s68k brq %i", (d&2)>>1);\r
112 if ((Pico_mcd->m.state_flags&1) && (d&3)==1) {\r
113 SekResetS68k(); // S68k comes out of RESET or BRQ state\r
114 Pico_mcd->m.state_flags&=~1;\r
115 dprintf("m68k: resetting s68k, cycles=%i", SekCyclesLeft);\r
116 }\r
117 Pico_mcd->m.busreq = d;\r
118 return;\r
119 case 2:\r
120 dprintf("m68k: prg wp=%02x", d);\r
121 Pico_mcd->s68k_regs[2] = d; // really use s68k side register\r
122 return;\r
123 case 3: {\r
124 u32 dold = Pico_mcd->s68k_regs[3]&0x1f;\r
125 //printf("m68k_regs w3: %02x @%06x\n", (u8)d, SekPc);\r
126 d &= 0xc2;\r
127 if ((dold>>6) != ((d>>6)&3))\r
128 dprintf("m68k: prg bank: %i -> %i", (Pico_mcd->s68k_regs[a]>>6), ((d>>6)&3));\r
129 //if ((Pico_mcd->s68k_regs[3]&4) != (d&4)) dprintf("m68k: ram mode %i mbit", (d&4) ? 1 : 2);\r
130 //if ((Pico_mcd->s68k_regs[3]&2) != (d&2)) dprintf("m68k: %s", (d&4) ? ((d&2) ? "word swap req" : "noop?") :\r
131 // ((d&2) ? "word ram to s68k" : "word ram to m68k"));\r
132 if (dold & 4) {\r
133 d ^= 2; // writing 0 to DMNA actually sets it, 1 does nothing\r
134 } else {\r
135 //dold &= ~2; // ??\r
136#if 1\r
137 if ((d & 2) && !(dold & 2)) {\r
138 Pico_mcd->m.state_flags |= 2; // we must delay setting DMNA bit (needed for Silpheed)\r
139 d &= ~2;\r
140 }\r
141#else\r
142 if (d & 2) dold &= ~1; // return word RAM to s68k in 2M mode\r
143#endif\r
144 }\r
145 Pico_mcd->s68k_regs[3] = d | dold; // really use s68k side register\r
146#ifdef USE_POLL_DETECT\r
147 if ((s68k_poll_adclk&0xfe) == 2 && s68k_poll_cnt > POLL_LIMIT) {\r
148 SekSetStopS68k(0); s68k_poll_adclk = 0;\r
149 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r
150 }\r
151#endif\r
152 return;\r
153 }\r
154 case 6:\r
155 Pico_mcd->bios[0x72 + 1] = d; // simple hint vector changer\r
156 return;\r
157 case 7:\r
158 Pico_mcd->bios[0x72] = d;\r
159 dprintf("hint vector set to %08x", PicoRead32(0x70));\r
160 return;\r
161 case 0xf:\r
162 d = (d << 1) | ((d >> 7) & 1); // rol8 1 (special case)\r
163 case 0xe:\r
164 //dprintf("m68k: comm flag: %02x", d);\r
165 Pico_mcd->s68k_regs[0xe] = d;\r
166#ifdef USE_POLL_DETECT\r
167 if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {\r
168 SekSetStopS68k(0); s68k_poll_adclk = 0;\r
169 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r
170 }\r
171#endif\r
172 return;\r
173 }\r
174\r
175 if ((a&0xf0) == 0x10) {\r
176 Pico_mcd->s68k_regs[a] = d;\r
177#ifdef USE_POLL_DETECT\r
178 if ((a&0xfe) == (s68k_poll_adclk&0xfe) && s68k_poll_cnt > POLL_LIMIT) {\r
179 SekSetStopS68k(0); s68k_poll_adclk = 0;\r
180 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r
181 }\r
182#endif\r
183 return;\r
184 }\r
185\r
186 elprintf(EL_UIO, "m68k FIXME: invalid write? [%02x] %02x", a, d);\r
187}\r
188\r
189#ifndef _ASM_CD_MEMORY_C\r
190static\r
191#endif\r
192u32 s68k_poll_detect(u32 a, u32 d)\r
193{\r
194#ifdef USE_POLL_DETECT\r
195 // needed mostly for Cyclone, which doesn't always check it's cycle counter\r
196 if (SekIsStoppedS68k()) return d;\r
197 // polling detection\r
198 if (a == (s68k_poll_adclk&0xff)) {\r
199 unsigned int clkdiff = SekCyclesDoneS68k() - (s68k_poll_adclk>>8);\r
200 if (clkdiff <= POLL_CYCLES) {\r
201 s68k_poll_cnt++;\r
202 //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt);\r
203 if (s68k_poll_cnt > POLL_LIMIT) {\r
204 SekSetStopS68k(1);\r
205 elprintf(EL_CDPOLL, "s68k poll detected @ %06x, a=%02x", SekPcS68k, a);\r
206 }\r
207 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;\r
208 return d;\r
209 }\r
210 }\r
211 s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a;\r
212 s68k_poll_cnt = 0;\r
213#endif\r
214 return d;\r
215}\r
216\r
217#define READ_FONT_DATA(basemask) \\r
218{ \\r
219 unsigned int fnt = *(unsigned int *)(Pico_mcd->s68k_regs + 0x4c); \\r
220 unsigned int col0 = (fnt >> 8) & 0x0f, col1 = (fnt >> 12) & 0x0f; \\r
221 if (fnt & (basemask << 0)) d = col1 ; else d = col0; \\r
222 if (fnt & (basemask << 1)) d |= col1 << 4; else d |= col0 << 4; \\r
223 if (fnt & (basemask << 2)) d |= col1 << 8; else d |= col0 << 8; \\r
224 if (fnt & (basemask << 3)) d |= col1 << 12; else d |= col0 << 12; \\r
225}\r
226\r
227\r
228#ifndef _ASM_CD_MEMORY_C\r
229static\r
230#endif\r
231u32 s68k_reg_read16(u32 a)\r
232{\r
233 u32 d=0;\r
234\r
235 // dprintf("s68k_regs r%2i: [%02x] @ %06x", realsize&~1, a+(realsize&1), SekPcS68k);\r
236\r
237 switch (a) {\r
238 case 0:\r
239 return ((Pico_mcd->s68k_regs[0]&3)<<8) | 1; // ver = 0, not in reset state\r
240 case 2:\r
241 d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f);\r
242 //printf("s68k_regs r3: %02x @%06x\n", (u8)d, SekPcS68k);\r
243 return s68k_poll_detect(a, d);\r
244 case 6:\r
245 return CDC_Read_Reg();\r
246 case 8:\r
247 return Read_CDC_Host(1); // Gens returns 0 here on byte reads\r
248 case 0xC:\r
249 d = Pico_mcd->m.timer_stopwatch >> 16;\r
250 dprintf("s68k stopwatch timer read (%04x)", d);\r
251 return d;\r
252 case 0x30:\r
253 dprintf("s68k int3 timer read (%02x)", Pico_mcd->s68k_regs[31]);\r
254 return Pico_mcd->s68k_regs[31];\r
255 case 0x34: // fader\r
256 return 0; // no busy bit\r
257 case 0x50: // font data (check: Lunar 2, Silpheed)\r
258 READ_FONT_DATA(0x00100000);\r
259 return d;\r
260 case 0x52:\r
261 READ_FONT_DATA(0x00010000);\r
262 return d;\r
263 case 0x54:\r
264 READ_FONT_DATA(0x10000000);\r
265 return d;\r
266 case 0x56:\r
267 READ_FONT_DATA(0x01000000);\r
268 return d;\r
269 }\r
270\r
271 d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1];\r
272\r
273 if (a >= 0x0e && a < 0x30)\r
274 return s68k_poll_detect(a, d);\r
275\r
276 return d;\r
277}\r
278\r
279#ifndef _ASM_CD_MEMORY_C\r
280static\r
281#endif\r
282void s68k_reg_write8(u32 a, u32 d)\r
283{\r
284 //dprintf("s68k_regs w%2i: [%02x] %02x @ %06x", realsize, a, d, SekPcS68k);\r
285\r
286 // Warning: d might have upper bits set\r
287 switch (a) {\r
288 case 2:\r
289 return; // only m68k can change WP\r
290 case 3: {\r
291 int dold = Pico_mcd->s68k_regs[3];\r
292 //printf("s68k_regs w3: %02x @%06x\n", (u8)d, SekPcS68k);\r
293 d &= 0x1d;\r
294 d |= dold&0xc2;\r
295 if (d&4) {\r
296 if ((d ^ dold) & 5) {\r
297 d &= ~2; // in case of mode or bank change we clear DMNA (m68k req) bit\r
298 PicoMemResetCD(d);\r
299 }\r
300#ifdef _ASM_CD_MEMORY_C\r
301 if ((d ^ dold) & 0x1d)\r
302 PicoMemResetCDdecode(d);\r
303#endif\r
304 if (!(dold & 4)) {\r
305 dprintf("wram mode 2M->1M");\r
306 wram_2M_to_1M(Pico_mcd->word_ram2M);\r
307 }\r
308 } else {\r
309 if (dold & 4) {\r
310 dprintf("wram mode 1M->2M");\r
311 if (!(d&1)) { // it didn't set the ret bit, which means it doesn't want to give WRAM to m68k\r
312 d &= ~3;\r
313 d |= (dold&1) ? 2 : 1; // then give it to the one which had bank0 in 1M mode\r
314 }\r
315 wram_1M_to_2M(Pico_mcd->word_ram2M);\r
316 PicoMemResetCD(d);\r
317 }\r
318 else\r
319 d |= dold&1;\r
320 if (d&1) d &= ~2; // return word RAM to m68k in 2M mode\r
321 }\r
322 break;\r
323 }\r
324 case 4:\r
325 dprintf("s68k CDC dest: %x", d&7);\r
326 Pico_mcd->s68k_regs[4] = (Pico_mcd->s68k_regs[4]&0xC0) | (d&7); // CDC mode\r
327 return;\r
328 case 5:\r
329 //dprintf("s68k CDC reg addr: %x", d&0xf);\r
330 break;\r
331 case 7:\r
332 CDC_Write_Reg(d);\r
333 return;\r
334 case 0xa:\r
335 dprintf("s68k set CDC dma addr");\r
336 break;\r
337 case 0xc:\r
338 case 0xd:\r
339 dprintf("s68k set stopwatch timer");\r
340 Pico_mcd->m.timer_stopwatch = 0;\r
341 return;\r
342 case 0xe:\r
343 Pico_mcd->s68k_regs[0xf] = (d>>1) | (d<<7); // ror8 1, Gens note: Dragons lair\r
344 return;\r
345 case 0x31:\r
346 dprintf("s68k set int3 timer: %02x", d);\r
347 Pico_mcd->m.timer_int3 = (d & 0xff) << 16;\r
348 break;\r
349 case 0x33: // IRQ mask\r
350 dprintf("s68k irq mask: %02x", d);\r
351 if ((d&(1<<4)) && (Pico_mcd->s68k_regs[0x37]&4) && !(Pico_mcd->s68k_regs[0x33]&(1<<4))) {\r
352 CDD_Export_Status();\r
353 }\r
354 break;\r
355 case 0x34: // fader\r
356 Pico_mcd->s68k_regs[a] = (u8) d & 0x7f;\r
357 return;\r
358 case 0x36:\r
359 return; // d/m bit is unsetable\r
360 case 0x37: {\r
361 u32 d_old = Pico_mcd->s68k_regs[0x37];\r
362 Pico_mcd->s68k_regs[0x37] = d&7;\r
363 if ((d&4) && !(d_old&4)) {\r
364 CDD_Export_Status();\r
365 }\r
366 return;\r
367 }\r
368 case 0x4b:\r
369 Pico_mcd->s68k_regs[a] = (u8) d;\r
370 CDD_Import_Command();\r
371 return;\r
372 }\r
373\r
374 if ((a&0x1f0) == 0x10 || (a >= 0x38 && a < 0x42))\r
375 {\r
376 elprintf(EL_UIO, "s68k FIXME: invalid write @ %02x?", a);\r
377 return;\r
378 }\r
379\r
380 Pico_mcd->s68k_regs[a] = (u8) d;\r
381}\r
382\r
383\r
384static u32 OtherRead16End(u32 a, int realsize)\r
385{\r
386 u32 d=0;\r
387\r
388#ifndef _ASM_CD_MEMORY_C\r
389 if ((a&0xffffc0)==0xa12000) {\r
390 d=m68k_reg_read16(a);\r
391 goto end;\r
392 }\r
393\r
394 if (a==0x400000) {\r
395 if (SRam.data != NULL) d=3; // 64k cart\r
396 goto end;\r
397 }\r
398\r
399 if ((a&0xfe0000)==0x600000) {\r
400 if (SRam.data != NULL) {\r
401 d=SRam.data[((a>>1)&0xffff)+0x2000];\r
402 if (realsize == 8) d|=d<<8;\r
403 }\r
404 goto end;\r
405 }\r
406\r
407 if (a==0x7ffffe) {\r
408 d=Pico_mcd->m.bcram_reg;\r
409 goto end;\r
410 }\r
411#endif\r
412\r
413 elprintf(EL_UIO, "m68k FIXME: unusual r%i: %06x @%06x", realsize&~1, (a&0xfffffe)+(realsize&1), SekPc);\r
414\r
415#ifndef _ASM_CD_MEMORY_C\r
416end:\r
417#endif\r
418 return d;\r
419}\r
420\r
421\r
422static void OtherWrite8End(u32 a, u32 d, int realsize)\r
423{\r
424#ifndef _ASM_CD_MEMORY_C\r
425 if ((a&0xffffc0)==0xa12000) { m68k_reg_write8(a, d); return; }\r
426\r
427 if ((a&0xfe0000)==0x600000) {\r
428 if (SRam.data != NULL && (Pico_mcd->m.bcram_reg&1)) {\r
429 SRam.data[((a>>1)&0xffff)+0x2000]=d;\r
430 SRam.changed = 1;\r
431 }\r
432 return;\r
433 }\r
434\r
435 if (a==0x7fffff) {\r
436 Pico_mcd->m.bcram_reg=d;\r
437 return;\r
438 }\r
439#endif\r
440\r
441 elprintf(EL_UIO, "m68k FIXME: strange w%i: [%06x], %08x @%06x", realsize, a&0xffffff, d, SekPc);\r
442}\r
443\r
444#ifndef _ASM_CD_MEMORY_C\r
445#define _CD_MEMORY_C\r
446#undef _ASM_MEMORY_C\r
447#include "../MemoryCmn.c"\r
448#include "cell_map.c"\r
449#endif\r
450\r
451\r
452// -----------------------------------------------------------------\r
453// Read Rom and read Ram\r
454\r
455#ifdef _ASM_CD_MEMORY_C\r
456u32 PicoReadM68k8(u32 a);\r
457#else\r
458u32 PicoReadM68k8(u32 a)\r
459{\r
460 u32 d=0;\r
461\r
462 a&=0xffffff;\r
463\r
464 switch (a >> 17)\r
465 {\r
466 case 0x00>>1: // BIOS: 000000 - 020000\r
467 d = *(u8 *)(Pico_mcd->bios+(a^1));\r
468 break;\r
469 case 0x02>>1: // prg RAM\r
470 if ((Pico_mcd->m.busreq&3)!=1) {\r
471 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
472 d = *(prg_bank+((a^1)&0x1ffff));\r
473 }\r
474 break;\r
475 case 0x20>>1: // word RAM: 200000 - 220000\r
476 wrdprintf("m68k_wram r8: [%06x] @%06x", a, SekPc);\r
477 a &= 0x1ffff;\r
478 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
479 int bank = Pico_mcd->s68k_regs[3]&1;\r
480 d = Pico_mcd->word_ram1M[bank][a^1];\r
481 } else {\r
482 // allow access in any mode, like Gens does\r
483 d = Pico_mcd->word_ram2M[a^1];\r
484 }\r
485 wrdprintf("ret = %02x", (u8)d);\r
486 break;\r
487 case 0x22>>1: // word RAM: 220000 - 240000\r
488 wrdprintf("m68k_wram r8: [%06x] @%06x", a, SekPc);\r
489 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
490 int bank = Pico_mcd->s68k_regs[3]&1;\r
491 a = (a&3) | (cell_map(a >> 2) << 2); // cell arranged\r
492 d = Pico_mcd->word_ram1M[bank][a^1];\r
493 } else {\r
494 // allow access in any mode, like Gens does\r
495 d = Pico_mcd->word_ram2M[(a^1)&0x3ffff];\r
496 }\r
497 wrdprintf("ret = %02x", (u8)d);\r
498 break;\r
499 case 0xc0>>1: case 0xc2>>1: case 0xc4>>1: case 0xc6>>1:\r
500 case 0xc8>>1: case 0xca>>1: case 0xcc>>1: case 0xce>>1:\r
501 case 0xd0>>1: case 0xd2>>1: case 0xd4>>1: case 0xd6>>1:\r
502 case 0xd8>>1: case 0xda>>1: case 0xdc>>1: case 0xde>>1:\r
503 // VDP\r
504 if ((a&0xe700e0)==0xc00000) {\r
505 d=PicoVideoRead(a);\r
506 if ((a&1)==0) d>>=8;\r
507 }\r
508 break;\r
509 case 0xe0>>1: case 0xe2>>1: case 0xe4>>1: case 0xe6>>1:\r
510 case 0xe8>>1: case 0xea>>1: case 0xec>>1: case 0xee>>1:\r
511 case 0xf0>>1: case 0xf2>>1: case 0xf4>>1: case 0xf6>>1:\r
512 case 0xf8>>1: case 0xfa>>1: case 0xfc>>1: case 0xfe>>1:\r
513 // RAM:\r
514 d = *(u8 *)(Pico.ram+((a^1)&0xffff));\r
515 break;\r
516 default:\r
517 if ((a&0xff4000)==0xa00000) { d=z80Read8(a); break; } // Z80 Ram\r
518 if ((a&0xffffc0)==0xa12000)\r
519 rdprintf("m68k_regs r8: [%02x] @%06x", a&0x3f, SekPc);\r
520\r
521 d=OtherRead16(a&~1, 8|(a&1)); if ((a&1)==0) d>>=8;\r
522\r
523 if ((a&0xffffc0)==0xa12000)\r
524 rdprintf("ret = %02x", (u8)d);\r
525 break;\r
526 }\r
527\r
528\r
529 elprintf(EL_IO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);\r
530#ifdef EMU_CORE_DEBUG\r
531 if (a>=Pico.romsize) {\r
532 lastread_a = a;\r
533 lastread_d[lrp_cyc++&15] = d;\r
534 }\r
535#endif\r
536 return d;\r
537}\r
538#endif\r
539\r
540\r
541#ifdef _ASM_CD_MEMORY_C\r
542u32 PicoReadM68k16(u32 a);\r
543#else\r
544static u32 PicoReadM68k16(u32 a)\r
545{\r
546 u32 d=0;\r
547\r
548 a&=0xfffffe;\r
549\r
550 switch (a >> 17)\r
551 {\r
552 case 0x00>>1: // BIOS: 000000 - 020000\r
553 d = *(u16 *)(Pico_mcd->bios+a);\r
554 break;\r
555 case 0x02>>1: // prg RAM\r
556 if ((Pico_mcd->m.busreq&3)!=1) {\r
557 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
558 wrdprintf("m68k_prgram r16: [%i,%06x] @%06x", Pico_mcd->s68k_regs[3]>>6, a, SekPc);\r
559 d = *(u16 *)(prg_bank+(a&0x1fffe));\r
560 wrdprintf("ret = %04x", d);\r
561 }\r
562 break;\r
563 case 0x20>>1: // word RAM: 200000 - 220000\r
564 wrdprintf("m68k_wram r16: [%06x] @%06x", a, SekPc);\r
565 a &= 0x1fffe;\r
566 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
567 int bank = Pico_mcd->s68k_regs[3]&1;\r
568 d = *(u16 *)(Pico_mcd->word_ram1M[bank]+a);\r
569 } else {\r
570 // allow access in any mode, like Gens does\r
571 d = *(u16 *)(Pico_mcd->word_ram2M+a);\r
572 }\r
573 wrdprintf("ret = %04x", d);\r
574 break;\r
575 case 0x22>>1: // word RAM: 220000 - 240000\r
576 wrdprintf("m68k_wram r16: [%06x] @%06x", a, SekPc);\r
577 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
578 int bank = Pico_mcd->s68k_regs[3]&1;\r
579 a = (a&2) | (cell_map(a >> 2) << 2); // cell arranged\r
580 d = *(u16 *)(Pico_mcd->word_ram1M[bank]+a);\r
581 } else {\r
582 // allow access in any mode, like Gens does\r
583 d = *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
584 }\r
585 wrdprintf("ret = %04x", d);\r
586 break;\r
587 case 0xc0>>1: case 0xc2>>1: case 0xc4>>1: case 0xc6>>1:\r
588 case 0xc8>>1: case 0xca>>1: case 0xcc>>1: case 0xce>>1:\r
589 case 0xd0>>1: case 0xd2>>1: case 0xd4>>1: case 0xd6>>1:\r
590 case 0xd8>>1: case 0xda>>1: case 0xdc>>1: case 0xde>>1:\r
591 // VDP\r
592 if ((a&0xe700e0)==0xc00000)\r
593 d=PicoVideoRead(a);\r
594 break;\r
595 case 0xe0>>1: case 0xe2>>1: case 0xe4>>1: case 0xe6>>1:\r
596 case 0xe8>>1: case 0xea>>1: case 0xec>>1: case 0xee>>1:\r
597 case 0xf0>>1: case 0xf2>>1: case 0xf4>>1: case 0xf6>>1:\r
598 case 0xf8>>1: case 0xfa>>1: case 0xfc>>1: case 0xfe>>1:\r
599 // RAM:\r
600 d=*(u16 *)(Pico.ram+(a&0xfffe));\r
601 break;\r
602 default:\r
603 if ((a&0xffffc0)==0xa12000)\r
604 rdprintf("m68k_regs r16: [%02x] @%06x", a&0x3f, SekPc);\r
605\r
606 d = OtherRead16(a, 16);\r
607\r
608 if ((a&0xffffc0)==0xa12000)\r
609 rdprintf("ret = %04x", d);\r
610 break;\r
611 }\r
612\r
613\r
614 elprintf(EL_IO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);\r
615#ifdef EMU_CORE_DEBUG\r
616 if (a>=Pico.romsize) {\r
617 lastread_a = a;\r
618 lastread_d[lrp_cyc++&15] = d;\r
619 }\r
620#endif\r
621 return d;\r
622}\r
623#endif\r
624\r
625\r
626#ifdef _ASM_CD_MEMORY_C\r
627u32 PicoReadM68k32(u32 a);\r
628#else\r
629static u32 PicoReadM68k32(u32 a)\r
630{\r
631 u32 d=0;\r
632\r
633 a&=0xfffffe;\r
634\r
635 switch (a >> 17)\r
636 {\r
637 case 0x00>>1: { // BIOS: 000000 - 020000\r
638 u16 *pm=(u16 *)(Pico_mcd->bios+a);\r
639 d = (pm[0]<<16)|pm[1];\r
640 break;\r
641 }\r
642 case 0x02>>1: // prg RAM\r
643 if ((Pico_mcd->m.busreq&3)!=1) {\r
644 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
645 u16 *pm=(u16 *)(prg_bank+(a&0x1fffe));\r
646 d = (pm[0]<<16)|pm[1];\r
647 }\r
648 break;\r
649 case 0x20>>1: // word RAM: 200000 - 220000\r
650 wrdprintf("m68k_wram r32: [%06x] @%06x", a, SekPc);\r
651 a&=0x1fffe;\r
652 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
653 int bank = Pico_mcd->s68k_regs[3]&1;\r
654 u16 *pm=(u16 *)(Pico_mcd->word_ram1M[bank]+a);\r
655 d = (pm[0]<<16)|pm[1];\r
656 } else {\r
657 // allow access in any mode, like Gens does\r
658 u16 *pm=(u16 *)(Pico_mcd->word_ram2M+a);\r
659 d = (pm[0]<<16)|pm[1];\r
660 }\r
661 wrdprintf("ret = %08x", d);\r
662 break;\r
663 case 0x22>>1: // word RAM: 220000 - 240000\r
664 wrdprintf("m68k_wram r32: [%06x] @%06x", a, SekPc);\r
665 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode, cell arranged?\r
666 u32 a1, a2;\r
667 int bank = Pico_mcd->s68k_regs[3]&1;\r
668 a1 = (a&2) | (cell_map(a >> 2) << 2);\r
669 if (a&2) a2 = cell_map((a+2) >> 2) << 2;\r
670 else a2 = a1 + 2;\r
671 d = *(u16 *)(Pico_mcd->word_ram1M[bank]+a1) << 16;\r
672 d |= *(u16 *)(Pico_mcd->word_ram1M[bank]+a2);\r
673 } else {\r
674 // allow access in any mode, like Gens does\r
675 u16 *pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
676 d = (pm[0]<<16)|pm[1];\r
677 }\r
678 wrdprintf("ret = %08x", d);\r
679 break;\r
680 case 0xc0>>1: case 0xc2>>1: case 0xc4>>1: case 0xc6>>1:\r
681 case 0xc8>>1: case 0xca>>1: case 0xcc>>1: case 0xce>>1:\r
682 case 0xd0>>1: case 0xd2>>1: case 0xd4>>1: case 0xd6>>1:\r
683 case 0xd8>>1: case 0xda>>1: case 0xdc>>1: case 0xde>>1:\r
684 // VDP\r
685 d = (PicoVideoRead(a)<<16)|PicoVideoRead(a+2);\r
686 break;\r
687 case 0xe0>>1: case 0xe2>>1: case 0xe4>>1: case 0xe6>>1:\r
688 case 0xe8>>1: case 0xea>>1: case 0xec>>1: case 0xee>>1:\r
689 case 0xf0>>1: case 0xf2>>1: case 0xf4>>1: case 0xf6>>1:\r
690 case 0xf8>>1: case 0xfa>>1: case 0xfc>>1: case 0xfe>>1: {\r
691 // RAM:\r
692 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));\r
693 d = (pm[0]<<16)|pm[1];\r
694 break;\r
695 }\r
696 default:\r
697 if ((a&0xffffc0)==0xa12000)\r
698 rdprintf("m68k_regs r32: [%02x] @%06x", a&0x3f, SekPc);\r
699\r
700 d = (OtherRead16(a, 32)<<16)|OtherRead16(a+2, 32);\r
701\r
702 if ((a&0xffffc0)==0xa12000)\r
703 rdprintf("ret = %08x", d);\r
704 break;\r
705 }\r
706\r
707\r
708 elprintf(EL_IO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);\r
709#ifdef EMU_CORE_DEBUG\r
710 if (a>=Pico.romsize) {\r
711 lastread_a = a;\r
712 lastread_d[lrp_cyc++&15] = d;\r
713 }\r
714#endif\r
715 return d;\r
716}\r
717#endif\r
718\r
719\r
720// -----------------------------------------------------------------\r
721\r
722#ifdef _ASM_CD_MEMORY_C\r
723void PicoWriteM68k8(u32 a,u8 d);\r
724#else\r
725void PicoWriteM68k8(u32 a,u8 d)\r
726{\r
727 elprintf(EL_IO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);\r
728#ifdef EMU_CORE_DEBUG\r
729 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
730#endif\r
731\r
732 if ((a&0xe00000)==0xe00000) { // Ram\r
733 *(u8 *)(Pico.ram+((a^1)&0xffff)) = d;\r
734 return;\r
735 }\r
736\r
737 // prg RAM\r
738 if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) {\r
739 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
740 *(u8 *)(prg_bank+((a^1)&0x1ffff))=d;\r
741 return;\r
742 }\r
743\r
744 a&=0xffffff;\r
745\r
746 // word RAM\r
747 if ((a&0xfc0000)==0x200000) {\r
748 wrdprintf("m68k_wram w8: [%06x] %02x @%06x", a, d, SekPc);\r
749 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
750 int bank = Pico_mcd->s68k_regs[3]&1;\r
751 if (a >= 0x220000)\r
752 a = (a&3) | (cell_map(a >> 2) << 2); // cell arranged\r
753 else a &= 0x1ffff;\r
754 *(u8 *)(Pico_mcd->word_ram1M[bank]+(a^1))=d;\r
755 } else {\r
756 // allow access in any mode, like Gens does\r
757 *(u8 *)(Pico_mcd->word_ram2M+((a^1)&0x3ffff))=d;\r
758 }\r
759 return;\r
760 }\r
761\r
762 if ((a&0xffffc0)==0xa12000) {\r
763 rdprintf("m68k_regs w8: [%02x] %02x @%06x", a&0x3f, d, SekPc);\r
764 m68k_reg_write8(a, d);\r
765 return;\r
766 }\r
767\r
768 OtherWrite8(a,d);\r
769}\r
770#endif\r
771\r
772\r
773#ifdef _ASM_CD_MEMORY_C\r
774void PicoWriteM68k16(u32 a,u16 d);\r
775#else\r
776static void PicoWriteM68k16(u32 a,u16 d)\r
777{\r
778 elprintf(EL_IO, "w16: %06x, %04x", a&0xffffff, d);\r
779#ifdef EMU_CORE_DEBUG\r
780 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
781#endif\r
782\r
783 if ((a&0xe00000)==0xe00000) { // Ram\r
784 *(u16 *)(Pico.ram+(a&0xfffe))=d;\r
785 return;\r
786 }\r
787\r
788 // prg RAM\r
789 if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) {\r
790 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
791 wrdprintf("m68k_prgram w16: [%i,%06x] %04x @%06x", Pico_mcd->s68k_regs[3]>>6, a, d, SekPc);\r
792 *(u16 *)(prg_bank+(a&0x1fffe))=d;\r
793 return;\r
794 }\r
795\r
796 a&=0xfffffe;\r
797\r
798 // word RAM\r
799 if ((a&0xfc0000)==0x200000) {\r
800 wrdprintf("m68k_wram w16: [%06x] %04x @%06x", a, d, SekPc);\r
801 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
802 int bank = Pico_mcd->s68k_regs[3]&1;\r
803 if (a >= 0x220000)\r
804 a = (a&2) | (cell_map(a >> 2) << 2); // cell arranged\r
805 else a &= 0x1fffe;\r
806 *(u16 *)(Pico_mcd->word_ram1M[bank]+a)=d;\r
807 } else {\r
808 // allow access in any mode, like Gens does\r
809 *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe))=d;\r
810 }\r
811 return;\r
812 }\r
813\r
814 // regs\r
815 if ((a&0xffffc0)==0xa12000) {\r
816 rdprintf("m68k_regs w16: [%02x] %04x @%06x", a&0x3f, d, SekPc);\r
817 if (a == 0xe) { // special case, 2 byte writes would be handled differently\r
818 Pico_mcd->s68k_regs[0xe] = d >> 8;\r
819#ifdef USE_POLL_DETECT\r
820 if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) {\r
821 SekSetStopS68k(0); s68k_poll_adclk = 0;\r
822 elprintf(EL_CDPOLL, "s68k poll release, a=%02x", a);\r
823 }\r
824#endif\r
825 return;\r
826 }\r
827 m68k_reg_write8(a, d>>8);\r
828 m68k_reg_write8(a+1,d&0xff);\r
829 return;\r
830 }\r
831\r
832 // VDP\r
833 if ((a&0xe700e0)==0xc00000) {\r
834 PicoVideoWrite(a,(u16)d);\r
835 return;\r
836 }\r
837\r
838 OtherWrite16(a,d);\r
839}\r
840#endif\r
841\r
842\r
843#ifdef _ASM_CD_MEMORY_C\r
844void PicoWriteM68k32(u32 a,u32 d);\r
845#else\r
846static void PicoWriteM68k32(u32 a,u32 d)\r
847{\r
848 elprintf(EL_IO, "w32: %06x, %08x", a&0xffffff, d);\r
849#ifdef EMU_CORE_DEBUG\r
850 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
851#endif\r
852\r
853 if ((a&0xe00000)==0xe00000)\r
854 {\r
855 // Ram:\r
856 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));\r
857 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
858 return;\r
859 }\r
860\r
861 // prg RAM\r
862 if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) {\r
863 u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6];\r
864 u16 *pm=(u16 *)(prg_bank+(a&0x1fffe));\r
865 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
866 return;\r
867 }\r
868\r
869 a&=0xfffffe;\r
870\r
871 // word RAM\r
872 if ((a&0xfc0000)==0x200000) {\r
873 if (d != 0) // don't log clears\r
874 wrdprintf("m68k_wram w32: [%06x] %08x @%06x", a, d, SekPc);\r
875 if (Pico_mcd->s68k_regs[3]&4) { // 1M mode?\r
876 int bank = Pico_mcd->s68k_regs[3]&1;\r
877 if (a >= 0x220000) { // cell arranged\r
878 u32 a1, a2;\r
879 a1 = (a&2) | (cell_map(a >> 2) << 2);\r
880 if (a&2) a2 = cell_map((a+2) >> 2) << 2;\r
881 else a2 = a1 + 2;\r
882 *(u16 *)(Pico_mcd->word_ram1M[bank]+a1) = d >> 16;\r
883 *(u16 *)(Pico_mcd->word_ram1M[bank]+a2) = d;\r
884 } else {\r
885 u16 *pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
886 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
887 }\r
888 } else {\r
889 // allow access in any mode, like Gens does\r
890 u16 *pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
891 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
892 }\r
893 return;\r
894 }\r
895\r
896 if ((a&0xffffc0)==0xa12000) {\r
897 rdprintf("m68k_regs w32: [%02x] %08x @%06x", a&0x3f, d, SekPc);\r
898 if ((a&0x3e) == 0xe) dprintf("m68k FIXME: w32 [%02x]", a&0x3f);\r
899 }\r
900\r
901 // VDP\r
902 if ((a&0xe700e0)==0xc00000)\r
903 {\r
904 PicoVideoWrite(a, (u16)(d>>16));\r
905 PicoVideoWrite(a+2,(u16)d);\r
906 return;\r
907 }\r
908\r
909 OtherWrite16(a, (u16)(d>>16));\r
910 OtherWrite16(a+2,(u16)d);\r
911}\r
912#endif\r
913\r
914\r
915// -----------------------------------------------------------------\r
916// S68k\r
917// -----------------------------------------------------------------\r
918\r
919#ifdef _ASM_CD_MEMORY_C\r
920u32 PicoReadS68k8(u32 a);\r
921#else\r
922static u32 PicoReadS68k8(u32 a)\r
923{\r
924 u32 d=0;\r
925\r
926#ifdef EMU_CORE_DEBUG\r
927 u32 ab=a&0xfffffe;\r
928#endif\r
929 a&=0xffffff;\r
930\r
931 // prg RAM\r
932 if (a < 0x80000) {\r
933 d = *(Pico_mcd->prg_ram+(a^1));\r
934 goto end;\r
935 }\r
936\r
937 // regs\r
938 if ((a&0xfffe00) == 0xff8000) {\r
939 a &= 0x1ff;\r
940 rdprintf("s68k_regs r8: [%02x] @ %06x", a, SekPcS68k);\r
941 if (a >= 0x0e && a < 0x30) {\r
942 d = Pico_mcd->s68k_regs[a];\r
943 s68k_poll_detect(a, d);\r
944 rdprintf("ret = %02x", (u8)d);\r
945 goto end;\r
946 }\r
947 else if (a >= 0x58 && a < 0x68)\r
948 d = gfx_cd_read(a&~1);\r
949 else d = s68k_reg_read16(a&~1);\r
950 if ((a&1)==0) d>>=8;\r
951 rdprintf("ret = %02x", (u8)d);\r
952 goto end;\r
953 }\r
954\r
955 // word RAM (2M area)\r
956 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
957 // test: batman returns\r
958 wrdprintf("s68k_wram2M r8: [%06x] @%06x", a, SekPcS68k);\r
959 if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode?\r
960 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
961 d = Pico_mcd->word_ram1M[bank][((a>>1)^1)&0x1ffff];\r
962 if (a&1) d &= 0x0f;\r
963 else d >>= 4;\r
964 } else {\r
965 // allow access in any mode, like Gens does\r
966 d = Pico_mcd->word_ram2M[(a^1)&0x3ffff];\r
967 }\r
968 wrdprintf("ret = %02x", (u8)d);\r
969 goto end;\r
970 }\r
971\r
972 // word RAM (1M area)\r
973 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
974 int bank;\r
975 wrdprintf("s68k_wram1M r8: [%06x] @%06x", a, SekPcS68k);\r
976// if (!(Pico_mcd->s68k_regs[3]&4))\r
977// dprintf("s68k_wram1M FIXME: wrong mode");\r
978 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
979 d = Pico_mcd->word_ram1M[bank][(a^1)&0x1ffff];\r
980 wrdprintf("ret = %02x", (u8)d);\r
981 goto end;\r
982 }\r
983\r
984 // PCM\r
985 if ((a&0xff8000)==0xff0000) {\r
986 elprintf(EL_IO, "s68k_pcm r8: [%06x] @%06x", a, SekPcS68k);\r
987 a &= 0x7fff;\r
988 if (a >= 0x2000)\r
989 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff];\r
990 else if (a >= 0x20) {\r
991 a &= 0x1e;\r
992 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;\r
993 if (a & 2) d >>= 8;\r
994 }\r
995 elprintf(EL_IO, "ret = %02x", (u8)d);\r
996 goto end;\r
997 }\r
998\r
999 // bram\r
1000 if ((a&0xff0000)==0xfe0000) {\r
1001 d = Pico_mcd->bram[(a>>1)&0x1fff];\r
1002 goto end;\r
1003 }\r
1004\r
1005 elprintf(EL_UIO, "s68k r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPcS68k);\r
1006\r
1007 end:\r
1008\r
1009 elprintf(EL_IO, "s68k r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPcS68k);\r
1010#ifdef EMU_CORE_DEBUG\r
1011 lastread_a = ab;\r
1012 lastread_d[lrp_cyc++&15] = d;\r
1013#endif\r
1014 return d;\r
1015}\r
1016#endif\r
1017\r
1018\r
1019#ifdef _ASM_CD_MEMORY_C\r
1020u32 PicoReadS68k16(u32 a);\r
1021#else\r
1022static u32 PicoReadS68k16(u32 a)\r
1023{\r
1024 u32 d=0;\r
1025\r
1026#ifdef EMU_CORE_DEBUG\r
1027 u32 ab=a&0xfffffe;\r
1028#endif\r
1029 a&=0xfffffe;\r
1030\r
1031 // prg RAM\r
1032 if (a < 0x80000) {\r
1033 wrdprintf("s68k_prgram r16: [%06x] @%06x", a, SekPcS68k);\r
1034 d = *(u16 *)(Pico_mcd->prg_ram+a);\r
1035 wrdprintf("ret = %04x", d);\r
1036 goto end;\r
1037 }\r
1038\r
1039 // regs\r
1040 if ((a&0xfffe00) == 0xff8000) {\r
1041 a &= 0x1fe;\r
1042 rdprintf("s68k_regs r16: [%02x] @ %06x", a, SekPcS68k);\r
1043 if (a >= 0x58 && a < 0x68)\r
1044 d = gfx_cd_read(a);\r
1045 else d = s68k_reg_read16(a);\r
1046 rdprintf("ret = %04x", d);\r
1047 goto end;\r
1048 }\r
1049\r
1050 // word RAM (2M area)\r
1051 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
1052 wrdprintf("s68k_wram2M r16: [%06x] @%06x", a, SekPcS68k);\r
1053 if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode?\r
1054 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1055 d = Pico_mcd->word_ram1M[bank][((a>>1)^1)&0x1ffff];\r
1056 d |= d << 4; d &= ~0xf0;\r
1057 } else {\r
1058 // allow access in any mode, like Gens does\r
1059 d = *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
1060 }\r
1061 wrdprintf("ret = %04x", d);\r
1062 goto end;\r
1063 }\r
1064\r
1065 // word RAM (1M area)\r
1066 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
1067 int bank;\r
1068 wrdprintf("s68k_wram1M r16: [%06x] @%06x", a, SekPcS68k);\r
1069// if (!(Pico_mcd->s68k_regs[3]&4))\r
1070// dprintf("s68k_wram1M FIXME: wrong mode");\r
1071 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1072 d = *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1073 wrdprintf("ret = %04x", d);\r
1074 goto end;\r
1075 }\r
1076\r
1077 // bram\r
1078 if ((a&0xff0000)==0xfe0000) {\r
1079 dprintf("FIXME: s68k_bram r16: [%06x] @%06x", a, SekPcS68k);\r
1080 a = (a>>1)&0x1fff;\r
1081 d = Pico_mcd->bram[a++]; // Gens does little endian here, and so do we..\r
1082 d|= Pico_mcd->bram[a++] << 8; // This is most likely wrong\r
1083 dprintf("ret = %04x", d);\r
1084 goto end;\r
1085 }\r
1086\r
1087 // PCM\r
1088 if ((a&0xff8000)==0xff0000) {\r
1089 dprintf("FIXME: s68k_pcm r16: [%06x] @%06x", a, SekPcS68k);\r
1090 a &= 0x7fff;\r
1091 if (a >= 0x2000)\r
1092 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff];\r
1093 else if (a >= 0x20) {\r
1094 a &= 0x1e;\r
1095 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;\r
1096 if (a & 2) d >>= 8;\r
1097 }\r
1098 dprintf("ret = %04x", d);\r
1099 goto end;\r
1100 }\r
1101\r
1102 elprintf(EL_UIO, "s68k r16: %06x, %04x @%06x", a&0xffffff, d, SekPcS68k);\r
1103\r
1104 end:\r
1105\r
1106 elprintf(EL_IO, "s68k r16: %06x, %04x @%06x", a&0xffffff, d, SekPcS68k);\r
1107#ifdef EMU_CORE_DEBUG\r
1108 lastread_a = ab;\r
1109 lastread_d[lrp_cyc++&15] = d;\r
1110#endif\r
1111 return d;\r
1112}\r
1113#endif\r
1114\r
1115\r
1116#ifdef _ASM_CD_MEMORY_C\r
1117u32 PicoReadS68k32(u32 a);\r
1118#else\r
1119static u32 PicoReadS68k32(u32 a)\r
1120{\r
1121 u32 d=0;\r
1122\r
1123#ifdef EMU_CORE_DEBUG\r
1124 u32 ab=a&0xfffffe;\r
1125#endif\r
1126 a&=0xfffffe;\r
1127\r
1128 // prg RAM\r
1129 if (a < 0x80000) {\r
1130 u16 *pm=(u16 *)(Pico_mcd->prg_ram+a);\r
1131 d = (pm[0]<<16)|pm[1];\r
1132 goto end;\r
1133 }\r
1134\r
1135 // regs\r
1136 if ((a&0xfffe00) == 0xff8000) {\r
1137 a &= 0x1fe;\r
1138 rdprintf("s68k_regs r32: [%02x] @ %06x", a, SekPcS68k);\r
1139 if (a >= 0x58 && a < 0x68)\r
1140 d = (gfx_cd_read(a)<<16)|gfx_cd_read(a+2);\r
1141 else d = (s68k_reg_read16(a)<<16)|s68k_reg_read16(a+2);\r
1142 rdprintf("ret = %08x", d);\r
1143 goto end;\r
1144 }\r
1145\r
1146 // word RAM (2M area)\r
1147 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
1148 wrdprintf("s68k_wram2M r32: [%06x] @%06x", a, SekPcS68k);\r
1149 if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode?\r
1150 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1151 a >>= 1;\r
1152 d = Pico_mcd->word_ram1M[bank][((a+0)^1)&0x1ffff] << 16;\r
1153 d |= Pico_mcd->word_ram1M[bank][((a+1)^1)&0x1ffff];\r
1154 d |= d << 4; d &= 0x0f0f0f0f;\r
1155 } else {\r
1156 // allow access in any mode, like Gens does\r
1157 u16 *pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); d = (pm[0]<<16)|pm[1];\r
1158 }\r
1159 wrdprintf("ret = %08x", d);\r
1160 goto end;\r
1161 }\r
1162\r
1163 // word RAM (1M area)\r
1164 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
1165 int bank;\r
1166 u16 *pm;\r
1167 wrdprintf("s68k_wram1M r32: [%06x] @%06x", a, SekPcS68k);\r
1168// if (!(Pico_mcd->s68k_regs[3]&4))\r
1169// dprintf("s68k_wram1M FIXME: wrong mode");\r
1170 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1171 pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); d = (pm[0]<<16)|pm[1];\r
1172 wrdprintf("ret = %08x", d);\r
1173 goto end;\r
1174 }\r
1175\r
1176 // PCM\r
1177 if ((a&0xff8000)==0xff0000) {\r
1178 dprintf("s68k_pcm r32: [%06x] @%06x", a, SekPcS68k);\r
1179 a &= 0x7fff;\r
1180 if (a >= 0x2000) {\r
1181 a >>= 1;\r
1182 d = Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][a&0xfff] << 16;\r
1183 d |= Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a+1)&0xfff];\r
1184 } else if (a >= 0x20) {\r
1185 a &= 0x1e;\r
1186 if (a & 2) {\r
1187 a >>= 2;\r
1188 d = (Pico_mcd->pcm.ch[a].addr >> (PCM_STEP_SHIFT-8)) & 0xff0000;\r
1189 d |= (Pico_mcd->pcm.ch[(a+1)&7].addr >> PCM_STEP_SHIFT) & 0xff;\r
1190 } else {\r
1191 d = Pico_mcd->pcm.ch[a>>2].addr >> PCM_STEP_SHIFT;\r
1192 d = ((d<<16)&0xff0000) | ((d>>8)&0xff); // PCM chip is LE\r
1193 }\r
1194 }\r
1195 dprintf("ret = %08x", d);\r
1196 goto end;\r
1197 }\r
1198\r
1199 // bram\r
1200 if ((a&0xff0000)==0xfe0000) {\r
1201 dprintf("FIXME: s68k_bram r32: [%06x] @%06x", a, SekPcS68k);\r
1202 a = (a>>1)&0x1fff;\r
1203 d = Pico_mcd->bram[a++] << 16; // middle endian? TODO: verify against Fusion..\r
1204 d|= Pico_mcd->bram[a++] << 24;\r
1205 d|= Pico_mcd->bram[a++];\r
1206 d|= Pico_mcd->bram[a++] << 8;\r
1207 dprintf("ret = %08x", d);\r
1208 goto end;\r
1209 }\r
1210\r
1211 elprintf(EL_UIO, "s68k r32: %06x, %08x @%06x", a&0xffffff, d, SekPcS68k);\r
1212\r
1213 end:\r
1214\r
1215 elprintf(EL_IO, "s68k r32: %06x, %08x @%06x", a&0xffffff, d, SekPcS68k);\r
1216#ifdef EMU_CORE_DEBUG\r
1217 if (ab > 0x78) { // not vectors and stuff\r
1218 lastread_a = ab;\r
1219 lastread_d[lrp_cyc++&15] = d;\r
1220 }\r
1221#endif\r
1222 return d;\r
1223}\r
1224#endif\r
1225\r
1226\r
1227#ifndef _ASM_CD_MEMORY_C\r
1228/* check: jaguar xj 220 (draws entire world using decode) */\r
1229static void decode_write8(u32 a, u8 d, int r3)\r
1230{\r
1231 u8 *pd = Pico_mcd->word_ram1M[(r3 & 1)^1] + (((a>>1)^1)&0x1ffff);\r
1232 u8 oldmask = (a&1) ? 0xf0 : 0x0f;\r
1233\r
1234 r3 &= 0x18;\r
1235 d &= 0x0f;\r
1236 if (!(a&1)) d <<= 4;\r
1237\r
1238 if (r3 == 8) {\r
1239 if ((!(*pd & (~oldmask))) && d) goto do_it;\r
1240 } else if (r3 > 8) {\r
1241 if (d) goto do_it;\r
1242 } else {\r
1243 goto do_it;\r
1244 }\r
1245\r
1246 return;\r
1247do_it:\r
1248 *pd = d | (*pd & oldmask);\r
1249}\r
1250\r
1251\r
1252static void decode_write16(u32 a, u16 d, int r3)\r
1253{\r
1254 u8 *pd = Pico_mcd->word_ram1M[(r3 & 1)^1] + (((a>>1)^1)&0x1ffff);\r
1255\r
1256 //if ((a & 0x3ffff) < 0x28000) return;\r
1257\r
1258 r3 &= 0x18;\r
1259 d &= 0x0f0f;\r
1260 d |= d >> 4;\r
1261\r
1262 if (r3 == 8) {\r
1263 u8 dold = *pd;\r
1264 if (!(dold & 0xf0)) dold |= d & 0xf0;\r
1265 if (!(dold & 0x0f)) dold |= d & 0x0f;\r
1266 *pd = dold;\r
1267 } else if (r3 > 8) {\r
1268 u8 dold = *pd;\r
1269 if (!(d & 0xf0)) d |= dold & 0xf0;\r
1270 if (!(d & 0x0f)) d |= dold & 0x0f;\r
1271 *pd = d;\r
1272 } else {\r
1273 *pd = d;\r
1274 }\r
1275}\r
1276#endif\r
1277\r
1278// -----------------------------------------------------------------\r
1279\r
1280#ifdef _ASM_CD_MEMORY_C\r
1281void PicoWriteS68k8(u32 a,u8 d);\r
1282#else\r
1283static void PicoWriteS68k8(u32 a,u8 d)\r
1284{\r
1285 elprintf(EL_IO, "s68k w8 : %06x, %02x @%06x", a&0xffffff, d, SekPcS68k);\r
1286\r
1287 a&=0xffffff;\r
1288\r
1289#ifdef EMU_CORE_DEBUG\r
1290 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
1291#endif\r
1292\r
1293 // prg RAM\r
1294 if (a < 0x80000) {\r
1295 u8 *pm=(u8 *)(Pico_mcd->prg_ram+(a^1));\r
1296 if (a >= (Pico_mcd->s68k_regs[2]<<8)) *pm=d;\r
1297 return;\r
1298 }\r
1299\r
1300 // regs\r
1301 if ((a&0xfffe00) == 0xff8000) {\r
1302 a &= 0x1ff;\r
1303 rdprintf("s68k_regs w8: [%02x] %02x @ %06x", a, d, SekPcS68k);\r
1304 if (a >= 0x58 && a < 0x68)\r
1305 gfx_cd_write16(a&~1, (d<<8)|d);\r
1306 else s68k_reg_write8(a,d);\r
1307 return;\r
1308 }\r
1309\r
1310 // word RAM (2M area)\r
1311 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
1312 int r3 = Pico_mcd->s68k_regs[3];\r
1313 wrdprintf("s68k_wram2M w8: [%06x] %02x @%06x", a, d, SekPcS68k);\r
1314 if (r3 & 4) { // 1M decode mode?\r
1315 decode_write8(a, d, r3);\r
1316 } else {\r
1317 // allow access in any mode, like Gens does\r
1318 *(u8 *)(Pico_mcd->word_ram2M+((a^1)&0x3ffff))=d;\r
1319 }\r
1320 return;\r
1321 }\r
1322\r
1323 // word RAM (1M area)\r
1324 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
1325 // Wing Commander tries to write here in wrong mode\r
1326 int bank;\r
1327 if (d)\r
1328 wrdprintf("s68k_wram1M w8: [%06x] %02x @%06x", a, d, SekPcS68k);\r
1329// if (!(Pico_mcd->s68k_regs[3]&4))\r
1330// dprintf("s68k_wram1M FIXME: wrong mode");\r
1331 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1332 *(u8 *)(Pico_mcd->word_ram1M[bank]+((a^1)&0x1ffff))=d;\r
1333 return;\r
1334 }\r
1335\r
1336 // PCM\r
1337 if ((a&0xff8000)==0xff0000) {\r
1338 a &= 0x7fff;\r
1339 if (a >= 0x2000)\r
1340 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;\r
1341 else if (a < 0x12)\r
1342 pcm_write(a>>1, d);\r
1343 return;\r
1344 }\r
1345\r
1346 // bram\r
1347 if ((a&0xff0000)==0xfe0000) {\r
1348 Pico_mcd->bram[(a>>1)&0x1fff] = d;\r
1349 SRam.changed = 1;\r
1350 return;\r
1351 }\r
1352\r
1353 elprintf(EL_UIO, "s68k w8 : %06x, %02x @%06x", a&0xffffff, d, SekPcS68k);\r
1354}\r
1355#endif\r
1356\r
1357\r
1358#ifdef _ASM_CD_MEMORY_C\r
1359void PicoWriteS68k16(u32 a,u16 d);\r
1360#else\r
1361static void PicoWriteS68k16(u32 a,u16 d)\r
1362{\r
1363 elprintf(EL_IO, "s68k w16: %06x, %04x @%06x", a&0xffffff, d, SekPcS68k);\r
1364\r
1365 a&=0xfffffe;\r
1366\r
1367#ifdef EMU_CORE_DEBUG\r
1368 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
1369#endif\r
1370\r
1371 // prg RAM\r
1372 if (a < 0x80000) {\r
1373 wrdprintf("s68k_prgram w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r
1374 if (a >= (Pico_mcd->s68k_regs[2]<<8)) // needed for Dungeon Explorer\r
1375 *(u16 *)(Pico_mcd->prg_ram+a)=d;\r
1376 return;\r
1377 }\r
1378\r
1379 // regs\r
1380 if ((a&0xfffe00) == 0xff8000) {\r
1381 a &= 0x1fe;\r
1382 rdprintf("s68k_regs w16: [%02x] %04x @ %06x", a, d, SekPcS68k);\r
1383 if (a >= 0x58 && a < 0x68)\r
1384 gfx_cd_write16(a, d);\r
1385 else {\r
1386 if (a == 0xe) { // special case, 2 byte writes would be handled differently\r
1387 Pico_mcd->s68k_regs[0xf] = d;\r
1388 return;\r
1389 }\r
1390 s68k_reg_write8(a, d>>8);\r
1391 s68k_reg_write8(a+1,d&0xff);\r
1392 }\r
1393 return;\r
1394 }\r
1395\r
1396 // word RAM (2M area)\r
1397 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
1398 int r3 = Pico_mcd->s68k_regs[3];\r
1399 wrdprintf("s68k_wram2M w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r
1400 if (r3 & 4) { // 1M decode mode?\r
1401 decode_write16(a, d, r3);\r
1402 } else {\r
1403 // allow access in any mode, like Gens does\r
1404 *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe))=d;\r
1405 }\r
1406 return;\r
1407 }\r
1408\r
1409 // word RAM (1M area)\r
1410 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
1411 int bank;\r
1412 if (d)\r
1413 wrdprintf("s68k_wram1M w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r
1414// if (!(Pico_mcd->s68k_regs[3]&4))\r
1415// dprintf("s68k_wram1M FIXME: wrong mode");\r
1416 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1417 *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe))=d;\r
1418 return;\r
1419 }\r
1420\r
1421 // PCM\r
1422 if ((a&0xff8000)==0xff0000) {\r
1423 a &= 0x7fff;\r
1424 if (a >= 0x2000)\r
1425 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a>>1)&0xfff] = d;\r
1426 else if (a < 0x12)\r
1427 pcm_write(a>>1, d & 0xff);\r
1428 return;\r
1429 }\r
1430\r
1431 // bram\r
1432 if ((a&0xff0000)==0xfe0000) {\r
1433 dprintf("s68k_bram w16: [%06x] %04x @%06x", a, d, SekPcS68k);\r
1434 a = (a>>1)&0x1fff;\r
1435 Pico_mcd->bram[a++] = d; // Gens does little endian here, an so do we..\r
1436 Pico_mcd->bram[a++] = d >> 8;\r
1437 SRam.changed = 1;\r
1438 return;\r
1439 }\r
1440\r
1441 elprintf(EL_UIO, "s68k w16: %06x, %04x @%06x", a&0xffffff, d, SekPcS68k);\r
1442}\r
1443#endif\r
1444\r
1445\r
1446#ifdef _ASM_CD_MEMORY_C\r
1447void PicoWriteS68k32(u32 a,u32 d);\r
1448#else\r
1449static void PicoWriteS68k32(u32 a,u32 d)\r
1450{\r
1451 elprintf(EL_IO, "s68k w32: %06x, %08x @%06x", a&0xffffff, d, SekPcS68k);\r
1452\r
1453 a&=0xfffffe;\r
1454\r
1455#ifdef EMU_CORE_DEBUG\r
1456 lastwrite_cyc_d[lwp_cyc++&15] = d;\r
1457#endif\r
1458\r
1459 // prg RAM\r
1460 if (a < 0x80000) {\r
1461 if (a >= (Pico_mcd->s68k_regs[2]<<8)) {\r
1462 u16 *pm=(u16 *)(Pico_mcd->prg_ram+a);\r
1463 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
1464 }\r
1465 return;\r
1466 }\r
1467\r
1468 // regs\r
1469 if ((a&0xfffe00) == 0xff8000) {\r
1470 a &= 0x1fe;\r
1471 rdprintf("s68k_regs w32: [%02x] %08x @ %06x", a, d, SekPcS68k);\r
1472 if (a >= 0x58 && a < 0x68) {\r
1473 gfx_cd_write16(a, d>>16);\r
1474 gfx_cd_write16(a+2, d&0xffff);\r
1475 } else {\r
1476 if ((a&0x1fe) == 0xe) dprintf("s68k FIXME: w32 [%02x]", a&0x3f);\r
1477 s68k_reg_write8(a, d>>24);\r
1478 s68k_reg_write8(a+1,(d>>16)&0xff);\r
1479 s68k_reg_write8(a+2,(d>>8) &0xff);\r
1480 s68k_reg_write8(a+3, d &0xff);\r
1481 }\r
1482 return;\r
1483 }\r
1484\r
1485 // word RAM (2M area)\r
1486 if ((a&0xfc0000)==0x080000) { // 080000-0bffff\r
1487 int r3 = Pico_mcd->s68k_regs[3];\r
1488 wrdprintf("s68k_wram2M w32: [%06x] %08x @%06x", a, d, SekPcS68k);\r
1489 if (r3 & 4) { // 1M decode mode?\r
1490 decode_write16(a , d >> 16, r3);\r
1491 decode_write16(a+2, d , r3);\r
1492 } else {\r
1493 // allow access in any mode, like Gens does\r
1494 u16 *pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
1495 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
1496 }\r
1497 return;\r
1498 }\r
1499\r
1500 // word RAM (1M area)\r
1501 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // 0c0000-0dffff\r
1502 int bank;\r
1503 u16 *pm;\r
1504 if (d)\r
1505 wrdprintf("s68k_wram1M w32: [%06x] %08x @%06x", a, d, SekPcS68k);\r
1506// if (!(Pico_mcd->s68k_regs[3]&4))\r
1507// dprintf("s68k_wram1M FIXME: wrong mode");\r
1508 bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1509 pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1510 pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
1511 return;\r
1512 }\r
1513\r
1514 // PCM\r
1515 if ((a&0xff8000)==0xff0000) {\r
1516 a &= 0x7fff;\r
1517 if (a >= 0x2000) {\r
1518 a >>= 1;\r
1519 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][a&0xfff] = (d >> 16);\r
1520 Pico_mcd->pcm_ram_b[Pico_mcd->pcm.bank][(a+1)&0xfff] = d;\r
1521 } else if (a < 0x12) {\r
1522 a >>= 1;\r
1523 pcm_write(a, (d>>16) & 0xff);\r
1524 pcm_write(a+1, d & 0xff);\r
1525 }\r
1526 return;\r
1527 }\r
1528\r
1529 // bram\r
1530 if ((a&0xff0000)==0xfe0000) {\r
1531 dprintf("s68k_bram w32: [%06x] %08x @%06x", a, d, SekPcS68k);\r
1532 a = (a>>1)&0x1fff;\r
1533 Pico_mcd->bram[a++] = d >> 16; // middle endian? verify?\r
1534 Pico_mcd->bram[a++] = d >> 24;\r
1535 Pico_mcd->bram[a++] = d;\r
1536 Pico_mcd->bram[a++] = d >> 8;\r
1537 SRam.changed = 1;\r
1538 return;\r
1539 }\r
1540\r
1541 elprintf(EL_UIO, "s68k w32: %06x, %08x @%06x", a&0xffffff, d, SekPcS68k);\r
1542}\r
1543#endif\r
1544\r
1545\r
1546// -----------------------------------------------------------------\r
1547\r
1548\r
1549#ifdef EMU_C68K\r
1550static __inline int PicoMemBaseM68k(u32 pc)\r
1551{\r
1552 if ((pc&0xe00000)==0xe00000)\r
1553 return (int)Pico.ram-(pc&0xff0000); // Program Counter in Ram\r
1554\r
1555 if (pc < 0x20000)\r
1556 return (int)Pico_mcd->bios; // Program Counter in BIOS\r
1557\r
1558 if ((pc&0xfc0000)==0x200000)\r
1559 {\r
1560 if (!(Pico_mcd->s68k_regs[3]&4))\r
1561 return (int)Pico_mcd->word_ram2M - 0x200000; // Program Counter in Word Ram\r
1562 if (pc < 0x220000) {\r
1563 int bank = Pico_mcd->s68k_regs[3]&1;\r
1564 return (int)Pico_mcd->word_ram1M[bank] - 0x200000;\r
1565 }\r
1566 }\r
1567\r
1568 // Error - Program Counter is invalid\r
1569 elprintf(EL_ANOMALY, "m68k FIXME: unhandled jump to %06x", pc);\r
1570\r
1571 return (int)Pico_mcd->bios;\r
1572}\r
1573\r
1574\r
1575static u32 PicoCheckPcM68k(u32 pc)\r
1576{\r
1577 pc-=PicoCpuCM68k.membase; // Get real pc\r
1578 pc&=0xfffffe;\r
1579\r
1580 PicoCpuCM68k.membase=PicoMemBaseM68k(pc);\r
1581\r
1582 return PicoCpuCM68k.membase+pc;\r
1583}\r
1584\r
1585\r
1586static __inline int PicoMemBaseS68k(u32 pc)\r
1587{\r
1588 if (pc < 0x80000) // PRG RAM\r
1589 return (int)Pico_mcd->prg_ram;\r
1590\r
1591 if ((pc&0xfc0000)==0x080000) // WORD RAM 2M area (assume we are in the right mode..)\r
1592 return (int)Pico_mcd->word_ram2M - 0x080000;\r
1593\r
1594 if ((pc&0xfe0000)==0x0c0000) { // word RAM 1M area\r
1595 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1596 return (int)Pico_mcd->word_ram1M[bank] - 0x0c0000;\r
1597 }\r
1598\r
1599 // Error - Program Counter is invalid\r
1600 elprintf(EL_ANOMALY, "s68k FIXME: unhandled jump to %06x", pc);\r
1601\r
1602 return (int)Pico_mcd->prg_ram;\r
1603}\r
1604\r
1605\r
1606static u32 PicoCheckPcS68k(u32 pc)\r
1607{\r
1608 pc-=PicoCpuCS68k.membase; // Get real pc\r
1609 pc&=0xfffffe;\r
1610\r
1611 PicoCpuCS68k.membase=PicoMemBaseS68k(pc);\r
1612\r
1613 return PicoCpuCS68k.membase+pc;\r
1614}\r
1615#endif\r
1616\r
1617#ifndef _ASM_CD_MEMORY_C\r
1618void PicoMemResetCD(int r3)\r
1619{\r
1620#ifdef EMU_F68K\r
1621 // update fetchmap..\r
1622 int i;\r
1623 if (!(r3 & 4))\r
1624 {\r
1625 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x240000; i++)\r
1626 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x200000;\r
1627 }\r
1628 else\r
1629 {\r
1630 for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x220000; i++)\r
1631 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[r3 & 1] - 0x200000;\r
1632 for (i = M68K_FETCHBANK1*0x0c/0x100; (i<<(24-FAMEC_FETCHBITS)) < 0x0e0000; i++)\r
1633 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[(r3&1)^1] - 0x0c0000;\r
1634 }\r
1635#endif\r
1636}\r
1637#endif\r
1638\r
1639PICO_INTERNAL void PicoMemSetupCD(void)\r
1640{\r
1641 // additional handlers for common code\r
1642 PicoRead16Hook = OtherRead16End;\r
1643 PicoWrite8Hook = OtherWrite8End;\r
1644\r
1645#ifdef EMU_C68K\r
1646 // Setup m68k memory callbacks:\r
1647 PicoCpuCM68k.checkpc=PicoCheckPcM68k;\r
1648 PicoCpuCM68k.fetch8 =PicoCpuCM68k.read8 =PicoReadM68k8;\r
1649 PicoCpuCM68k.fetch16=PicoCpuCM68k.read16=PicoReadM68k16;\r
1650 PicoCpuCM68k.fetch32=PicoCpuCM68k.read32=PicoReadM68k32;\r
1651 PicoCpuCM68k.write8 =PicoWriteM68k8;\r
1652 PicoCpuCM68k.write16=PicoWriteM68k16;\r
1653 PicoCpuCM68k.write32=PicoWriteM68k32;\r
1654 // s68k\r
1655 PicoCpuCS68k.checkpc=PicoCheckPcS68k;\r
1656 PicoCpuCS68k.fetch8 =PicoCpuCS68k.read8 =PicoReadS68k8;\r
1657 PicoCpuCS68k.fetch16=PicoCpuCS68k.read16=PicoReadS68k16;\r
1658 PicoCpuCS68k.fetch32=PicoCpuCS68k.read32=PicoReadS68k32;\r
1659 PicoCpuCS68k.write8 =PicoWriteS68k8;\r
1660 PicoCpuCS68k.write16=PicoWriteS68k16;\r
1661 PicoCpuCS68k.write32=PicoWriteS68k32;\r
1662#endif\r
1663#ifdef EMU_F68K\r
1664 // m68k\r
1665 PicoCpuFM68k.read_byte =PicoReadM68k8;\r
1666 PicoCpuFM68k.read_word =PicoReadM68k16;\r
1667 PicoCpuFM68k.read_long =PicoReadM68k32;\r
1668 PicoCpuFM68k.write_byte=PicoWriteM68k8;\r
1669 PicoCpuFM68k.write_word=PicoWriteM68k16;\r
1670 PicoCpuFM68k.write_long=PicoWriteM68k32;\r
1671 // s68k\r
1672 PicoCpuFS68k.read_byte =PicoReadS68k8;\r
1673 PicoCpuFS68k.read_word =PicoReadS68k16;\r
1674 PicoCpuFS68k.read_long =PicoReadS68k32;\r
1675 PicoCpuFS68k.write_byte=PicoWriteS68k8;\r
1676 PicoCpuFS68k.write_word=PicoWriteS68k16;\r
1677 PicoCpuFS68k.write_long=PicoWriteS68k32;\r
1678\r
1679 // setup FAME fetchmap\r
1680 {\r
1681 int i;\r
1682 // M68k\r
1683 // by default, point everything to fitst 64k of ROM (BIOS)\r
1684 for (i = 0; i < M68K_FETCHBANK1; i++)\r
1685 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS));\r
1686 // now real ROM (BIOS)\r
1687 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++)\r
1688 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom;\r
1689 // .. and RAM\r
1690 for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++)\r
1691 PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS));\r
1692 // S68k\r
1693 // PRG RAM is default\r
1694 for (i = 0; i < M68K_FETCHBANK1; i++)\r
1695 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram - (i<<(24-FAMEC_FETCHBITS));\r
1696 // real PRG RAM\r
1697 for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0x80000; i++)\r
1698 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram;\r
1699 // WORD RAM 2M area\r
1700 for (i = M68K_FETCHBANK1*0x08/0x100; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0xc0000; i++)\r
1701 PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x80000;\r
1702 // PicoMemResetCD() will setup word ram for both\r
1703 }\r
1704#endif\r
1705\r
1706 // m68k_poll_addr = m68k_poll_cnt = 0;\r
1707 s68k_poll_adclk = s68k_poll_cnt = 0;\r
1708}\r
1709\r
1710\r
1711#ifdef EMU_M68K\r
1712unsigned char PicoReadCD8w (unsigned int a) {\r
1713 return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k8(a) : PicoReadM68k8(a);\r
1714}\r
1715unsigned short PicoReadCD16w(unsigned int a) {\r
1716 return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k16(a) : PicoReadM68k16(a);\r
1717}\r
1718unsigned int PicoReadCD32w(unsigned int a) {\r
1719 return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k32(a) : PicoReadM68k32(a);\r
1720}\r
1721void PicoWriteCD8w (unsigned int a, unsigned char d) {\r
1722 if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k8(a, d); else PicoWriteM68k8(a, d);\r
1723}\r
1724void PicoWriteCD16w(unsigned int a, unsigned short d) {\r
1725 if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k16(a, d); else PicoWriteM68k16(a, d);\r
1726}\r
1727void PicoWriteCD32w(unsigned int a, unsigned int d) {\r
1728 if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k32(a, d); else PicoWriteM68k32(a, d);\r
1729}\r
1730\r
1731// these are allowed to access RAM\r
1732unsigned int m68k_read_pcrelative_CD8 (unsigned int a)\r
1733{\r
1734 a&=0xffffff;\r
1735 if(m68ki_cpu_p == &PicoCpuMS68k) {\r
1736 if (a < 0x80000) return *(u8 *)(Pico_mcd->prg_ram+(a^1)); // PRG Ram\r
1737 if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff)\r
1738 return *(u8 *)(Pico_mcd->word_ram2M+((a^1)&0x3ffff));\r
1739 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff)\r
1740 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1741 return *(u8 *)(Pico_mcd->word_ram1M[bank]+((a^1)&0x1ffff));\r
1742 }\r
1743 elprintf(EL_ANOMALY, "s68k_read_pcrelative_CD8 FIXME: can't handle %06x", a);\r
1744 } else {\r
1745 if((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram\r
1746 if(a<0x20000) return *(u8 *)(Pico.rom+(a^1)); // Bios\r
1747 if((a&0xfc0000)==0x200000) { // word RAM\r
1748 if(!(Pico_mcd->s68k_regs[3]&4)) // 2M?\r
1749 return *(u8 *)(Pico_mcd->word_ram2M+((a^1)&0x3ffff));\r
1750 else if (a < 0x220000) {\r
1751 int bank = Pico_mcd->s68k_regs[3]&1;\r
1752 return *(u8 *)(Pico_mcd->word_ram1M[bank]+((a^1)&0x1ffff));\r
1753 }\r
1754 }\r
1755 elprintf(EL_ANOMALY, "m68k_read_pcrelative_CD8 FIXME: can't handle %06x", a);\r
1756 }\r
1757 return 0;//(u8) lastread_d;\r
1758}\r
1759unsigned int m68k_read_pcrelative_CD16(unsigned int a)\r
1760{\r
1761 a&=0xffffff;\r
1762 if(m68ki_cpu_p == &PicoCpuMS68k) {\r
1763 if (a < 0x80000) return *(u16 *)(Pico_mcd->prg_ram+(a&~1)); // PRG Ram\r
1764 if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff)\r
1765 return *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
1766 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff)\r
1767 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1768 return *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1769 }\r
1770 elprintf(EL_ANOMALY, "s68k_read_pcrelative_CD16 FIXME: can't handle %06x", a);\r
1771 } else {\r
1772 if((a&0xe00000)==0xe00000) return *(u16 *)(Pico.ram+(a&0xfffe)); // Ram\r
1773 if(a<0x20000) return *(u16 *)(Pico.rom+(a&~1)); // Bios\r
1774 if((a&0xfc0000)==0x200000) { // word RAM\r
1775 if(!(Pico_mcd->s68k_regs[3]&4)) // 2M?\r
1776 return *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe));\r
1777 else if (a < 0x220000) {\r
1778 int bank = Pico_mcd->s68k_regs[3]&1;\r
1779 return *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1780 }\r
1781 }\r
1782 elprintf(EL_ANOMALY, "m68k_read_pcrelative_CD16 FIXME: can't handle %06x", a);\r
1783 }\r
1784 return 0;\r
1785}\r
1786unsigned int m68k_read_pcrelative_CD32(unsigned int a)\r
1787{\r
1788 u16 *pm;\r
1789 a&=0xffffff;\r
1790 if(m68ki_cpu_p == &PicoCpuMS68k) {\r
1791 if (a < 0x80000) { u16 *pm=(u16 *)(Pico_mcd->prg_ram+(a&~1)); return (pm[0]<<16)|pm[1]; } // PRG Ram\r
1792 if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff)\r
1793 { pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); return (pm[0]<<16)|pm[1]; }\r
1794 if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff)\r
1795 int bank = (Pico_mcd->s68k_regs[3]&1)^1;\r
1796 pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1797 return (pm[0]<<16)|pm[1];\r
1798 }\r
1799 elprintf(EL_ANOMALY, "s68k_read_pcrelative_CD32 FIXME: can't handle %06x", a);\r
1800 } else {\r
1801 if((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); return (pm[0]<<16)|pm[1]; } // Ram\r
1802 if(a<0x20000) { u16 *pm=(u16 *)(Pico.rom+(a&~1)); return (pm[0]<<16)|pm[1]; }\r
1803 if((a&0xfc0000)==0x200000) { // word RAM\r
1804 if(!(Pico_mcd->s68k_regs[3]&4)) // 2M?\r
1805 { pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); return (pm[0]<<16)|pm[1]; }\r
1806 else if (a < 0x220000) {\r
1807 int bank = Pico_mcd->s68k_regs[3]&1;\r
1808 pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe));\r
1809 return (pm[0]<<16)|pm[1];\r
1810 }\r
1811 }\r
1812 elprintf(EL_ANOMALY, "m68k_read_pcrelative_CD32 FIXME: can't handle %06x", a);\r
1813 }\r
1814 return 0;\r
1815}\r
1816#endif // EMU_M68K\r
1817\r