z80/ym2612 reset handling improved
[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
9761a7d0 25 d=PicoVideoRead8(a);
9037e45d 26 goto end;
27 }
28
406c96c5 29 if ((a&0xffffe0)==0x800000) // Pico I/O
30 {
31 switch (a & 0x1f)
32 {
1e6b5e39 33 case 0x01: d = PicoPicohw.r1; break;
406c96c5 34 case 0x03:
d49b10c2 35 d = PicoPad[0]&0x1f; // d-pad
42989e7d 36 d |= (PicoPad[0]&0x20) << 2; // pen push -> C
406c96c5 37 d = ~d;
38 break;
39
213c16ad 40 case 0x05: d = (PicoPicohw.pen_pos[0] >> 8); break; // what is MS bit for? Games read it..
41 case 0x07: d = PicoPicohw.pen_pos[0] & 0xff; break;
42 case 0x09: d = (PicoPicohw.pen_pos[1] >> 8); break;
43 case 0x0b: d = PicoPicohw.pen_pos[1] & 0xff; break;
d49b10c2 44 case 0x0d: d = (1 << (PicoPicohw.page & 7)) - 1; break;
213c16ad 45 case 0x12: d = PicoPicohw.fifo_bytes == 0 ? 0x80 : 0; break; // guess
406c96c5 46 default:
47 elprintf(EL_UIO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
48 break;
49 }
50 }
51
fa22af4c 52 //elprintf(EL_UIO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
9037e45d 53
54end:
55 elprintf(EL_IO, "r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc);
56 return d;
57}
58
59static u32 PicoReadPico16(u32 a)
60{
61 u32 d=0;
62
63 if ((a&0xe00000)==0xe00000) { d=*(u16 *)(Pico.ram+(a&0xfffe)); goto end; } // Ram
64
65 a&=0xfffffe;
66
67 if (a<Pico.romsize) { d = *(u16 *)(Pico.rom+a); goto end; } // Rom
68
d49b10c2 69 if ((a&0xfffff0)==0xc00000) {
9037e45d 70 d = PicoVideoRead(a);
71 goto end;
72 }
73
213c16ad 74 if (a == 0x800010)
d49b10c2 75 d = (PicoPicohw.fifo_bytes > 0x3f) ? 0 : (0x3f - PicoPicohw.fifo_bytes);
213c16ad 76 else if (a == 0x800012)
77 d = PicoPicohw.fifo_bytes == 0 ? 0x8000 : 0; // guess
fa22af4c 78 else
79 elprintf(EL_UIO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
406c96c5 80
fa22af4c 81 //elprintf(EL_UIO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 82
83end:
406c96c5 84 elprintf(EL_IO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 85 return d;
86}
87
88static u32 PicoReadPico32(u32 a)
89{
90 u32 d=0;
91
92 if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); d = (pm[0]<<16)|pm[1]; goto end; } // Ram
93
94 a&=0xfffffe;
95
96 if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); d = (pm[0]<<16)|pm[1]; goto end; } // Rom
97
d49b10c2 98 if ((a&0xfffff0)==0xc00000) {
9037e45d 99 d = (PicoVideoRead(a)<<16)|PicoVideoRead(a+2);
100 goto end;
101 }
102
103 elprintf(EL_UIO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
104
105end:
106 elprintf(EL_IO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
107 return d;
108}
109
110// -----------------------------------------------------------------
111// Write Ram
fa22af4c 112/*
406c96c5 113void dump(u16 w)
114{
fa22af4c 115 static FILE *f[0x10] = { NULL, };
116 char fname[32];
117 int num = PicoPicohw.r12 & 0xf;
118
582890c0 119 w = (w << 8) | (w >> 8);
fa22af4c 120 sprintf(fname, "ldump%i.bin", num);
121 if (f[num] == NULL)
122 f[num] = fopen(fname, "wb");
123 fwrite(&w, 1, 2, f[num]);
124 //fclose(f);
406c96c5 125}
fa22af4c 126*/
406c96c5 127
9037e45d 128static void PicoWritePico8(u32 a,u8 d)
129{
130 elprintf(EL_IO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
131
132 if ((a&0xe00000)==0xe00000) { *(u8 *)(Pico.ram+((a^1)&0xffff))=d; return; } // Ram
133
134 a&=0xffffff;
d49b10c2 135 if ((a&0xfffff9)==0xc00011) { if (PicoOpt&2) SN76496Write(d); return; } // PSG Sound
136
137 if ((a&0xfffff0)==0xc00000) { // VDP
9037e45d 138 d&=0xff;
139 PicoVideoWrite(a,(u16)(d|(d<<8))); // Byte access gets mirrored
140 return;
141 }
142
fa22af4c 143 switch (a & 0x1f) {
144 case 0x19: case 0x1b: case 0x1d: case 0x1f: break; // 'S' 'E' 'G' 'A'
145 default:
146 elprintf(EL_UIO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
147 break;
148 }
149 //elprintf(EL_UIO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
9037e45d 150}
151
152static void PicoWritePico16(u32 a,u16 d)
153{
154 elprintf(EL_IO, "w16: %06x, %04x", a&0xffffff, d);
155
156 if ((a&0xe00000)==0xe00000) { *(u16 *)(Pico.ram+(a&0xfffe))=d; return; } // Ram
157
158 a&=0xfffffe;
d49b10c2 159 if ((a&0xfffff0)==0xc00000) { PicoVideoWrite(a,(u16)d); return; } // VDP
9037e45d 160
582890c0 161 //if (a == 0x800010) dump(d);
ef4eb506 162 if (a == 0x800010)
163 {
164 PicoPicohw.fifo_bytes += 2;
165
166 if (PicoPicohw.xpcm_ptr < PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
167 *PicoPicohw.xpcm_ptr++ = d >> 8;
168 *PicoPicohw.xpcm_ptr++ = d;
169 }
170 else if (PicoPicohw.xpcm_ptr == PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
fa22af4c 171 elprintf(EL_ANOMALY|EL_PICOHW, "xpcm_buffer overflow!");
ef4eb506 172 PicoPicohw.xpcm_ptr++;
173 }
174 }
213c16ad 175 else if (a == 0x800012) {
176 int r12_old = PicoPicohw.r12;
177 PicoPicohw.r12 = d;
178 if (r12_old != d)
179 PicoReratePico();
180 }
fa22af4c 181 else
182 elprintf(EL_UIO, "w16: %06x, %04x", a&0xffffff, d);
406c96c5 183
fa22af4c 184 //elprintf(EL_UIO, "w16: %06x, %04x", a&0xffffff, d);
9037e45d 185}
186
187static void PicoWritePico32(u32 a,u32 d)
188{
189 elprintf(EL_IO, "w32: %06x, %08x", a&0xffffff, d);
190
191 if ((a&0xe00000)==0xe00000)
192 {
193 // Ram:
194 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));
195 pm[0]=(u16)(d>>16); pm[1]=(u16)d;
196 return;
197 }
198
199 a&=0xfffffe;
d49b10c2 200 if ((a&0xfffff0)==0xc00000)
9037e45d 201 {
202 // VDP:
203 PicoVideoWrite(a, (u16)(d>>16));
204 PicoVideoWrite(a+2,(u16)d);
205 return;
206 }
207
208 elprintf(EL_UIO, "w32: %06x, %08x", a&0xffffff, d);
209}
210
211#ifdef EMU_M68K
212extern unsigned int (*pm68k_read_memory_8) (unsigned int address);
213extern unsigned int (*pm68k_read_memory_16)(unsigned int address);
214extern unsigned int (*pm68k_read_memory_32)(unsigned int address);
215extern void (*pm68k_write_memory_8) (unsigned int address, unsigned char value);
216extern void (*pm68k_write_memory_16)(unsigned int address, unsigned short value);
217extern void (*pm68k_write_memory_32)(unsigned int address, unsigned int value);
218extern unsigned int (*pm68k_read_memory_pcr_8) (unsigned int address);
219extern unsigned int (*pm68k_read_memory_pcr_16)(unsigned int address);
220extern unsigned int (*pm68k_read_memory_pcr_32)(unsigned int address);
221
222static unsigned int m68k_read_memory_pcrp_8(unsigned int a)
223{
224 if((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram
225 return 0;
226}
227
228static unsigned int m68k_read_memory_pcrp_16(unsigned int a)
229{
230 if((a&0xe00000)==0xe00000) return *(u16 *)(Pico.ram+(a&0xfffe)); // Ram
231 return 0;
232}
233
234static unsigned int m68k_read_memory_pcrp_32(unsigned int a)
235{
236 if((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); return (pm[0]<<16)|pm[1]; } // Ram
237 return 0;
238}
239#endif // EMU_M68K
240
241
242PICO_INTERNAL void PicoMemSetupPico(void)
243{
406c96c5 244#ifdef EMU_C68K
245 PicoCpuCM68k.checkpc=PicoCheckPc;
246 PicoCpuCM68k.fetch8 =PicoCpuCM68k.read8 =PicoReadPico8;
247 PicoCpuCM68k.fetch16=PicoCpuCM68k.read16=PicoReadPico16;
248 PicoCpuCM68k.fetch32=PicoCpuCM68k.read32=PicoReadPico32;
249 PicoCpuCM68k.write8 =PicoWritePico8;
250 PicoCpuCM68k.write16=PicoWritePico16;
251 PicoCpuCM68k.write32=PicoWritePico32;
252#endif
9037e45d 253#ifdef EMU_M68K
254 pm68k_read_memory_8 = PicoReadPico8;
255 pm68k_read_memory_16 = PicoReadPico16;
256 pm68k_read_memory_32 = PicoReadPico32;
257 pm68k_write_memory_8 = PicoWritePico8;
258 pm68k_write_memory_16 = PicoWritePico16;
259 pm68k_write_memory_32 = PicoWritePico32;
260 pm68k_read_memory_pcr_8 = m68k_read_memory_pcrp_8;
261 pm68k_read_memory_pcr_16 = m68k_read_memory_pcrp_16;
262 pm68k_read_memory_pcr_32 = m68k_read_memory_pcrp_32;
263#endif
67c81ee2 264#ifdef EMU_F68K
265 // use standard setup, only override handlers
266 PicoMemSetup();
267 PicoCpuFM68k.read_byte =PicoReadPico8;
268 PicoCpuFM68k.read_word =PicoReadPico16;
269 PicoCpuFM68k.read_long =PicoReadPico32;
270 PicoCpuFM68k.write_byte=PicoWritePico8;
271 PicoCpuFM68k.write_word=PicoWritePico16;
272 PicoCpuFM68k.write_long=PicoWritePico32;
273#endif
9037e45d 274}
275