rearrange globals
[picodrive.git] / pico / pico / memory.c
CommitLineData
cff531af 1/*
2 * PicoDrive
3 * (C) notaz, 2008
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
efcba75f 8#include "../pico_int.h"
45f2f245 9#include "../memory.h"
d49b10c2 10#include "../sound/sn76496.h"
9037e45d 11
45f2f245 12/*
13void dump(u16 w)
9037e45d 14{
45f2f245 15 static FILE *f[0x10] = { NULL, };
16 char fname[32];
17 int num = PicoPicohw.r12 & 0xf;
9037e45d 18
45f2f245 19 w = (w << 8) | (w >> 8);
20 sprintf(fname, "ldump%i.bin", num);
21 if (f[num] == NULL)
22 f[num] = fopen(fname, "wb");
23 fwrite(&w, 1, 2, f[num]);
24 //fclose(f);
25}
26*/
9037e45d 27
45f2f245 28static u32 PicoRead8_pico(u32 a)
29{
30 u32 d = 0;
9037e45d 31
45f2f245 32 if ((a & 0xffffe0) == 0x800000) // Pico I/O
406c96c5 33 {
34 switch (a & 0x1f)
35 {
1e6b5e39 36 case 0x01: d = PicoPicohw.r1; break;
406c96c5 37 case 0x03:
93f9619e 38 d = PicoIn.pad[0]&0x1f; // d-pad
39 d |= (PicoIn.pad[0]&0x20) << 2; // pen push -> C
406c96c5 40 d = ~d;
41 break;
42
213c16ad 43 case 0x05: d = (PicoPicohw.pen_pos[0] >> 8); break; // what is MS bit for? Games read it..
44 case 0x07: d = PicoPicohw.pen_pos[0] & 0xff; break;
45 case 0x09: d = (PicoPicohw.pen_pos[1] >> 8); break;
46 case 0x0b: d = PicoPicohw.pen_pos[1] & 0xff; break;
d49b10c2 47 case 0x0d: d = (1 << (PicoPicohw.page & 7)) - 1; break;
213c16ad 48 case 0x12: d = PicoPicohw.fifo_bytes == 0 ? 0x80 : 0; break; // guess
406c96c5 49 default:
45f2f245 50 goto unhandled;
406c96c5 51 }
45f2f245 52 return d;
406c96c5 53 }
54
45f2f245 55unhandled:
56 elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc);
9037e45d 57 return d;
58}
59
45f2f245 60static u32 PicoRead16_pico(u32 a)
9037e45d 61{
45f2f245 62 u32 d = 0;
9037e45d 63
213c16ad 64 if (a == 0x800010)
d49b10c2 65 d = (PicoPicohw.fifo_bytes > 0x3f) ? 0 : (0x3f - PicoPicohw.fifo_bytes);
213c16ad 66 else if (a == 0x800012)
67 d = PicoPicohw.fifo_bytes == 0 ? 0x8000 : 0; // guess
fa22af4c 68 else
45f2f245 69 elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc);
9037e45d 70
9037e45d 71 return d;
72}
73
45f2f245 74static void PicoWrite8_pico(u32 a, u32 d)
9037e45d 75{
45f2f245 76 switch (a & ~0x800000) {
fa22af4c 77 case 0x19: case 0x1b: case 0x1d: case 0x1f: break; // 'S' 'E' 'G' 'A'
78 default:
45f2f245 79 elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc);
fa22af4c 80 break;
81 }
9037e45d 82}
83
45f2f245 84static void PicoWrite16_pico(u32 a, u32 d)
9037e45d 85{
582890c0 86 //if (a == 0x800010) dump(d);
ef4eb506 87 if (a == 0x800010)
88 {
89 PicoPicohw.fifo_bytes += 2;
90
91 if (PicoPicohw.xpcm_ptr < PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
92 *PicoPicohw.xpcm_ptr++ = d >> 8;
93 *PicoPicohw.xpcm_ptr++ = d;
94 }
95 else if (PicoPicohw.xpcm_ptr == PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
fa22af4c 96 elprintf(EL_ANOMALY|EL_PICOHW, "xpcm_buffer overflow!");
ef4eb506 97 PicoPicohw.xpcm_ptr++;
98 }
99 }
213c16ad 100 else if (a == 0x800012) {
101 int r12_old = PicoPicohw.r12;
102 PicoPicohw.r12 = d;
103 if (r12_old != d)
104 PicoReratePico();
105 }
fa22af4c 106 else
45f2f245 107 elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc);
9037e45d 108}
109
9037e45d 110
111PICO_INTERNAL void PicoMemSetupPico(void)
112{
67c81ee2 113 PicoMemSetup();
45f2f245 114
115 // no MD IO or Z80 on Pico
116 m68k_map_unmap(0x400000, 0xbfffff);
117
118 // map Pico I/O
119 cpu68k_map_set(m68k_read8_map, 0x800000, 0x80ffff, PicoRead8_pico, 1);
120 cpu68k_map_set(m68k_read16_map, 0x800000, 0x80ffff, PicoRead16_pico, 1);
121 cpu68k_map_set(m68k_write8_map, 0x800000, 0x80ffff, PicoWrite8_pico, 1);
122 cpu68k_map_set(m68k_write16_map, 0x800000, 0x80ffff, PicoWrite16_pico, 1);
9037e45d 123}
124