Pico fixes
[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
213c16ad 41 case 0x05: d = (PicoPicohw.pen_pos[0] >> 8); 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); break;
44 case 0x0b: d = PicoPicohw.pen_pos[1] & 0xff; break;
d49b10c2 45 case 0x0d: d = (1 << (PicoPicohw.page & 7)) - 1; break;
213c16ad 46 case 0x12: d = PicoPicohw.fifo_bytes == 0 ? 0x80 : 0; break; // guess
406c96c5 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
213c16ad 75 if (a == 0x800010)
d49b10c2 76 d = (PicoPicohw.fifo_bytes > 0x3f) ? 0 : (0x3f - PicoPicohw.fifo_bytes);
213c16ad 77 else if (a == 0x800012)
78 d = PicoPicohw.fifo_bytes == 0 ? 0x8000 : 0; // guess
406c96c5 79
80 elprintf(EL_UIO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 81
82end:
406c96c5 83 elprintf(EL_IO, "r16: %06x, %04x @%06x", a&0xffffff, d, SekPc);
9037e45d 84 return d;
85}
86
87static u32 PicoReadPico32(u32 a)
88{
89 u32 d=0;
90
91 if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); d = (pm[0]<<16)|pm[1]; goto end; } // Ram
92
93 a&=0xfffffe;
94
95 if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); d = (pm[0]<<16)|pm[1]; goto end; } // Rom
96
d49b10c2 97 if ((a&0xfffff0)==0xc00000) {
9037e45d 98 d = (PicoVideoRead(a)<<16)|PicoVideoRead(a+2);
99 goto end;
100 }
101
102 elprintf(EL_UIO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
103
104end:
105 elprintf(EL_IO, "r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);
106 return d;
107}
108
109// -----------------------------------------------------------------
110// Write Ram
111
406c96c5 112void dump(u16 w)
113{
114 FILE *f = fopen("dump.bin", "a");
115 fwrite(&w, 1, 2, f);
116 fclose(f);
117}
118
119
9037e45d 120static void PicoWritePico8(u32 a,u8 d)
121{
122 elprintf(EL_IO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
123
124 if ((a&0xe00000)==0xe00000) { *(u8 *)(Pico.ram+((a^1)&0xffff))=d; return; } // Ram
125
126 a&=0xffffff;
d49b10c2 127 if ((a&0xfffff9)==0xc00011) { if (PicoOpt&2) SN76496Write(d); return; } // PSG Sound
128
129 if ((a&0xfffff0)==0xc00000) { // VDP
9037e45d 130 d&=0xff;
131 PicoVideoWrite(a,(u16)(d|(d<<8))); // Byte access gets mirrored
132 return;
133 }
134
135 elprintf(EL_UIO, "w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc);
136}
137
138static void PicoWritePico16(u32 a,u16 d)
139{
140 elprintf(EL_IO, "w16: %06x, %04x", a&0xffffff, d);
141
142 if ((a&0xe00000)==0xe00000) { *(u16 *)(Pico.ram+(a&0xfffe))=d; return; } // Ram
143
144 a&=0xfffffe;
d49b10c2 145 if ((a&0xfffff0)==0xc00000) { PicoVideoWrite(a,(u16)d); return; } // VDP
9037e45d 146
406c96c5 147// if (a == 0x800010) dump(d);
ef4eb506 148 if (a == 0x800010)
149 {
150 PicoPicohw.fifo_bytes += 2;
151
152 if (PicoPicohw.xpcm_ptr < PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
153 *PicoPicohw.xpcm_ptr++ = d >> 8;
154 *PicoPicohw.xpcm_ptr++ = d;
155 }
156 else if (PicoPicohw.xpcm_ptr == PicoPicohw.xpcm_buffer + XPCM_BUFFER_SIZE) {
157 elprintf(EL_ANOMALY, "xpcm_buffer overflow!");
158 PicoPicohw.xpcm_ptr++;
159 }
160 }
213c16ad 161 else if (a == 0x800012) {
162 int r12_old = PicoPicohw.r12;
163 PicoPicohw.r12 = d;
164 if (r12_old != d)
165 PicoReratePico();
166 }
406c96c5 167
9037e45d 168 elprintf(EL_UIO, "w16: %06x, %04x", a&0xffffff, d);
169}
170
171static void PicoWritePico32(u32 a,u32 d)
172{
173 elprintf(EL_IO, "w32: %06x, %08x", a&0xffffff, d);
174
175 if ((a&0xe00000)==0xe00000)
176 {
177 // Ram:
178 u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));
179 pm[0]=(u16)(d>>16); pm[1]=(u16)d;
180 return;
181 }
182
183 a&=0xfffffe;
d49b10c2 184 if ((a&0xfffff0)==0xc00000)
9037e45d 185 {
186 // VDP:
187 PicoVideoWrite(a, (u16)(d>>16));
188 PicoVideoWrite(a+2,(u16)d);
189 return;
190 }
191
192 elprintf(EL_UIO, "w32: %06x, %08x", a&0xffffff, d);
193}
194
195#ifdef EMU_M68K
196extern unsigned int (*pm68k_read_memory_8) (unsigned int address);
197extern unsigned int (*pm68k_read_memory_16)(unsigned int address);
198extern unsigned int (*pm68k_read_memory_32)(unsigned int address);
199extern void (*pm68k_write_memory_8) (unsigned int address, unsigned char value);
200extern void (*pm68k_write_memory_16)(unsigned int address, unsigned short value);
201extern void (*pm68k_write_memory_32)(unsigned int address, unsigned int value);
202extern unsigned int (*pm68k_read_memory_pcr_8) (unsigned int address);
203extern unsigned int (*pm68k_read_memory_pcr_16)(unsigned int address);
204extern unsigned int (*pm68k_read_memory_pcr_32)(unsigned int address);
205
206static unsigned int m68k_read_memory_pcrp_8(unsigned int a)
207{
208 if((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram
209 return 0;
210}
211
212static unsigned int m68k_read_memory_pcrp_16(unsigned int a)
213{
214 if((a&0xe00000)==0xe00000) return *(u16 *)(Pico.ram+(a&0xfffe)); // Ram
215 return 0;
216}
217
218static unsigned int m68k_read_memory_pcrp_32(unsigned int a)
219{
220 if((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); return (pm[0]<<16)|pm[1]; } // Ram
221 return 0;
222}
223#endif // EMU_M68K
224
225
226PICO_INTERNAL void PicoMemSetupPico(void)
227{
406c96c5 228#ifdef EMU_C68K
229 PicoCpuCM68k.checkpc=PicoCheckPc;
230 PicoCpuCM68k.fetch8 =PicoCpuCM68k.read8 =PicoReadPico8;
231 PicoCpuCM68k.fetch16=PicoCpuCM68k.read16=PicoReadPico16;
232 PicoCpuCM68k.fetch32=PicoCpuCM68k.read32=PicoReadPico32;
233 PicoCpuCM68k.write8 =PicoWritePico8;
234 PicoCpuCM68k.write16=PicoWritePico16;
235 PicoCpuCM68k.write32=PicoWritePico32;
236#endif
9037e45d 237#ifdef EMU_M68K
238 pm68k_read_memory_8 = PicoReadPico8;
239 pm68k_read_memory_16 = PicoReadPico16;
240 pm68k_read_memory_32 = PicoReadPico32;
241 pm68k_write_memory_8 = PicoWritePico8;
242 pm68k_write_memory_16 = PicoWritePico16;
243 pm68k_write_memory_32 = PicoWritePico32;
244 pm68k_read_memory_pcr_8 = m68k_read_memory_pcrp_8;
245 pm68k_read_memory_pcr_16 = m68k_read_memory_pcrp_16;
246 pm68k_read_memory_pcr_32 = m68k_read_memory_pcrp_32;
247#endif
248}
249