Pico PCM, only one hardcoded mode for now
[picodrive.git] / Pico / Pico / Memory.c
CommitLineData
9037e45d 1#include "../PicoInt.h"
d49b10c2 2#include "../sound/sn76496.h"
9037e45d 3
4#ifndef UTYPES_DEFINED
5typedef unsigned char u8;
6typedef unsigned short u16;
7typedef unsigned int u32;
8#define UTYPES_DEFINED
9#endif
10
d49b10c2 11
9037e45d 12// -----------------------------------------------------------------
13// Read Rom and read Ram
14
15static u32 PicoReadPico8(u32 a)
16{
17 u32 d=0;
18
19 if ((a&0xe00000)==0xe00000) { d = *(u8 *)(Pico.ram+((a^1)&0xffff)); goto end; } // Ram
20 if (a<Pico.romsize) { d = *(u8 *)(Pico.rom+(a^1)); goto end; } // Rom
21
22 a&=0xffffff;
23
d49b10c2 24 if ((a&0xfffff0)==0xc00000) { // VDP
9037e45d 25 d=PicoVideoRead(a);
26 if ((a&1)==0) d>>=8;
27 goto end;
28 }
29
406c96c5 30 if ((a&0xffffe0)==0x800000) // Pico I/O
31 {
32 switch (a & 0x1f)
33 {
1e6b5e39 34 case 0x01: d = PicoPicohw.r1; break;
406c96c5 35 case 0x03:
d49b10c2 36 d = PicoPad[0]&0x1f; // d-pad
37 d |= (PicoPad[0]&0x20) << 2; // red button -> C
406c96c5 38 d = ~d;
39 break;
40
d49b10c2 41 case 0x05: d = (PicoPicohw.pen_pos[0] >> 8) & 3; break; // what is MS bit for? Games read it..
42 case 0x07: d = PicoPicohw.pen_pos[0] & 0xff; break;
43 case 0x09: d = (PicoPicohw.pen_pos[1] >> 8) & 3; break;
44 case 0x0b: d = PicoPicohw.pen_pos[1] & 0xff; break;
45 case 0x0d: d = (1 << (PicoPicohw.page & 7)) - 1; break;
406c96c5 46 case 0x12: d = 0x80; break;
47 default:
48 elprintf(EL_UIO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
49 break;
50 }
51 }
52
53// elprintf(EL_UIO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
9037e45d 54
55end:
56 elprintf(EL_IO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
57 return d;
58}
59
60static u32 PicoReadPico16(u32 a)
61{
62 u32 d=0;
63
64 if ((a&0xe00000)==0xe00000) { d=*(u16 *)(Pico.ram+(a&0xfffe)); goto end; } // Ram
65
66 a&=0xfffffe;
67
68 if (a<Pico.romsize) { d = *(u16 *)(Pico.rom+a); goto end; } // Rom
69
d49b10c2 70 if ((a&0xfffff0)==0xc00000) {
9037e45d 71 d = PicoVideoRead(a);
72 goto end;
73 }
74
d49b10c2 75 if (a == 0x800010)
76 d = (PicoPicohw.fifo_bytes > 0x3f) ? 0 : (0x3f - PicoPicohw.fifo_bytes);
406c96c5 77
78 elprintf(EL_UIO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 79
80end:
406c96c5 81 elprintf(EL_IO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 82 return d;
83}
84
85static u32 PicoReadPico32(u32 a)
86{
87 u32 d=0;
88
89 if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); d = (pm[0]<<16)|pm[1]; goto end; } // Ram
90
91 a&=0xfffffe;
92
93 if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); d = (pm[0]<<16)|pm[1]; goto end; } // Rom
94
d49b10c2 95 if ((a&0xfffff0)==0xc00000) {
9037e45d 96 d = (PicoVideoRead(a)<<16)|PicoVideoRead(a+2);
97 goto end;
98 }
99
100 elprintf(EL_UIO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
101
102end:
103 elprintf(EL_IO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
104 return d;
105}
106
107// -----------------------------------------------------------------
108// Write Ram
109
406c96c5 110void dump(u16 w)
111{
112 FILE *f = fopen("dump.bin", "a");
113 fwrite(&w, 1, 2, f);
114 fclose(f);
115}
116
117
9037e45d 118static void PicoWritePico8(u32 a,u8 d)
119{
120 elprintf(EL_IO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
121
122 if ((a&0xe00000)==0xe00000) { *(u8 *)(Pico.ram+((a^1)&0xffff))=d; return; } // Ram
123
124 a&=0xffffff;
d49b10c2 125 if ((a&0xfffff9)==0xc00011) { if (PicoOpt&2) SN76496Write(d); return; } // PSG Sound
126
127 if ((a&0xfffff0)==0xc00000) { // VDP
9037e45d 128 d&=0xff;
129 PicoVideoWrite(a,(u16)(d|(d<<8))); // Byte access gets mirrored
130 return;
131 }
132
133 elprintf(EL_UIO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
134}
135
136static void PicoWritePico16(u32 a,u16 d)
137{
138 elprintf(EL_IO, "w16: %06x, %04x", a&0xffffff, d);
139
140 if ((a&0xe00000)==0xe00000) { *(u16 *)(Pico.ram+(a&0xfffe))=d; return; } // Ram
141
142 a&=0xfffffe;
d49b10c2 143 if ((a&0xfffff0)==0xc00000) { PicoVideoWrite(a,(u16)d); return; } // VDP
9037e45d 144
406c96c5 145// if (a == 0x800010) dump(d);
ef4eb506 146 if (a == 0x800010)
147 {
148 PicoPicohw.fifo_bytes += 2;
149
150 if (PicoPicohw.xpcm_ptr < PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
151 *PicoPicohw.xpcm_ptr++ = d >> 8;
152 *PicoPicohw.xpcm_ptr++ = d;
153 }
154 else if (PicoPicohw.xpcm_ptr == PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
155 elprintf(EL_ANOMALY, "xpcm_buffer overflow!");
156 PicoPicohw.xpcm_ptr++;
157 }
158 }
159 else if (a == 0x800012) PicoPicohw.r12 = d;
406c96c5 160
9037e45d 161 elprintf(EL_UIO, "w16: %06x, %04x", a&0xffffff, d);
162}
163
164static void PicoWritePico32(u32 a,u32 d)
165{
166 elprintf(EL_IO, "w32: %06x, %08x", a&0xffffff, d);
167
168 if ((a&0xe00000)==0xe00000)
169 {
170 // Ram:
171 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));
172 pm[0]=(u16)(d>>16); pm[1]=(u16)d;
173 return;
174 }
175
176 a&=0xfffffe;
d49b10c2 177 if ((a&0xfffff0)==0xc00000)
9037e45d 178 {
179 // VDP:
180 PicoVideoWrite(a, (u16)(d>>16));
181 PicoVideoWrite(a+2,(u16)d);
182 return;
183 }
184
185 elprintf(EL_UIO, "w32: %06x, %08x", a&0xffffff, d);
186}
187
188#ifdef EMU_M68K
189extern unsigned int (*pm68k_read_memory_8) (unsigned int address);
190extern unsigned int (*pm68k_read_memory_16)(unsigned int address);
191extern unsigned int (*pm68k_read_memory_32)(unsigned int address);
192extern void (*pm68k_write_memory_8) (unsigned int address, unsigned char value);
193extern void (*pm68k_write_memory_16)(unsigned int address, unsigned short value);
194extern void (*pm68k_write_memory_32)(unsigned int address, unsigned int value);
195extern unsigned int (*pm68k_read_memory_pcr_8) (unsigned int address);
196extern unsigned int (*pm68k_read_memory_pcr_16)(unsigned int address);
197extern unsigned int (*pm68k_read_memory_pcr_32)(unsigned int address);
198
199static unsigned int m68k_read_memory_pcrp_8(unsigned int a)
200{
201 if((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram
202 return 0;
203}
204
205static unsigned int m68k_read_memory_pcrp_16(unsigned int a)
206{
207 if((a&0xe00000)==0xe00000) return *(u16 *)(Pico.ram+(a&0xfffe)); // Ram
208 return 0;
209}
210
211static unsigned int m68k_read_memory_pcrp_32(unsigned int a)
212{
213 if((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); return (pm[0]<<16)|pm[1]; } // Ram
214 return 0;
215}
216#endif // EMU_M68K
217
218
219PICO_INTERNAL void PicoMemSetupPico(void)
220{
406c96c5 221#ifdef EMU_C68K
222 PicoCpuCM68k.checkpc=PicoCheckPc;
223 PicoCpuCM68k.fetch8 =PicoCpuCM68k.read8 =PicoReadPico8;
224 PicoCpuCM68k.fetch16=PicoCpuCM68k.read16=PicoReadPico16;
225 PicoCpuCM68k.fetch32=PicoCpuCM68k.read32=PicoReadPico32;
226 PicoCpuCM68k.write8 =PicoWritePico8;
227 PicoCpuCM68k.write16=PicoWritePico16;
228 PicoCpuCM68k.write32=PicoWritePico32;
229#endif
9037e45d 230#ifdef EMU_M68K
231 pm68k_read_memory_8 = PicoReadPico8;
232 pm68k_read_memory_16 = PicoReadPico16;
233 pm68k_read_memory_32 = PicoReadPico32;
234 pm68k_write_memory_8 = PicoWritePico8;
235 pm68k_write_memory_16 = PicoWritePico16;
236 pm68k_write_memory_32 = PicoWritePico32;
237 pm68k_read_memory_pcr_8 = m68k_read_memory_pcrp_8;
238 pm68k_read_memory_pcr_16 = m68k_read_memory_pcrp_16;
239 pm68k_read_memory_pcr_32 = m68k_read_memory_pcrp_32;
240#endif
241}
242