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