be2c4208 |
1 | #include "../pico_int.h" |
2 | #include "../memory.h" |
3 | |
4 | static const char str_mars[] = "MARS"; |
5 | |
974fdb5b |
6 | struct Pico32xMem *Pico32xMem; |
7 | |
8 | // SH2 faking |
9 | static const u16 comm_fakevals[] = { |
10 | 0x4d5f, 0x4f4b, // M_OK |
11 | 0x535f, 0x4f4b, // S_OK |
12 | 0x0002, // Mortal Kombat |
13 | 0, // pad |
be2c4208 |
14 | }; |
15 | |
be2c4208 |
16 | static u32 p32x_reg_read16(u32 a) |
17 | { |
18 | a &= 0x3e; |
19 | |
974fdb5b |
20 | // SH2 faker |
21 | if ((a & 0x30) == 0x20) |
22 | { |
23 | static int f = 0, csum_faked = 0; |
24 | if (a == 0x28 && !csum_faked) { |
25 | csum_faked = 1; |
26 | return *(unsigned short *)(Pico.rom + 0x18e); |
27 | } |
28 | if (f >= sizeof(comm_fakevals) / sizeof(comm_fakevals[0])) |
29 | f = 0; |
30 | return comm_fakevals[f++]; |
31 | } |
32 | |
be2c4208 |
33 | return Pico32x.regs[a / 2]; |
34 | } |
35 | |
36 | static void p32x_reg_write16(u32 a, u32 d) |
37 | { |
38 | a &= 0x3e; |
39 | |
40 | if (a == 0 && !(Pico32x.regs[0] & 1)) { |
41 | Pico32x.regs[0] |= 1; |
42 | Pico32xStartup(); |
43 | return; |
44 | } |
45 | } |
46 | |
47 | static void p32x_reg_write8(u32 a, u32 d) |
48 | { |
49 | a &= 0x3f; |
50 | |
51 | if (a == 1 && !(Pico32x.regs[0] & 1)) { |
52 | Pico32x.regs[0] |= 1; |
53 | Pico32xStartup(); |
54 | return; |
55 | } |
56 | } |
57 | |
58 | // VDP regs |
59 | static u32 p32x_vdp_read16(u32 a) |
60 | { |
61 | a &= 0x0e; |
62 | |
63 | return Pico32x.vdp_regs[a / 2]; |
64 | } |
65 | |
be2c4208 |
66 | static void p32x_vdp_write8(u32 a, u32 d) |
67 | { |
974fdb5b |
68 | u16 *r = Pico32x.vdp_regs; |
be2c4208 |
69 | a &= 0x0f; |
70 | |
974fdb5b |
71 | // TODO: verify what's writeable |
be2c4208 |
72 | switch (a) { |
974fdb5b |
73 | case 0x01: |
74 | if (((r[0] & 3) == 0) != ((d & 3) == 0)) { // forced blanking changed |
75 | if (Pico.video.status & 8) |
76 | r[0x0a/2] |= P32XV_VBLK; |
77 | else |
78 | r[0x0a/2] &= ~P32XV_VBLK; |
79 | } |
80 | r[0] = (r[0] & P32XV_nPAL) | (d & 0xff); |
81 | break; |
be2c4208 |
82 | case 0x0b: |
974fdb5b |
83 | d &= 1; |
84 | Pico32x.pending_fb = d; |
85 | // if we are blanking and FS bit is changing |
86 | if ((r[0x0a/2] & P32XV_VBLK) && ((r[0x0a/2] ^ d) & P32XV_FS)) { |
87 | r[0x0a/2] ^= 1; |
88 | Pico32xSwapDRAM(d ^ 1); |
be2c4208 |
89 | } |
90 | break; |
91 | } |
92 | } |
93 | |
974fdb5b |
94 | static void p32x_vdp_write16(u32 a, u32 d) |
95 | { |
96 | p32x_vdp_write8(a | 1, d); |
97 | } |
98 | |
be2c4208 |
99 | // default 32x handlers |
100 | u32 PicoRead8_32x(u32 a) |
101 | { |
102 | u32 d = 0; |
103 | if ((a & 0xffc0) == 0x5100) { // a15100 |
104 | d = p32x_reg_read16(a); |
105 | goto out_16to8; |
106 | } |
107 | |
974fdb5b |
108 | if (!(Pico32x.regs[0] & 1)) |
109 | goto no_vdp; |
110 | |
111 | if ((a & 0xfff0) == 0x5180) { // a15180 |
be2c4208 |
112 | d = p32x_vdp_read16(a); |
113 | goto out_16to8; |
114 | } |
115 | |
974fdb5b |
116 | if ((a & 0xfe00) == 0x5200) { // a15200 |
117 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
118 | goto out_16to8; |
119 | } |
120 | |
121 | no_vdp: |
be2c4208 |
122 | if ((a & 0xfffc) == 0x30ec) { // a130ec |
123 | d = str_mars[a & 3]; |
124 | goto out; |
125 | } |
126 | |
127 | elprintf(EL_UIO, "m68k unmapped r8 [%06x] @%06x", a, SekPc); |
128 | return d; |
129 | |
130 | out_16to8: |
131 | if (a & 1) |
132 | d &= 0xff; |
133 | else |
134 | d >>= 8; |
135 | |
136 | out: |
137 | elprintf(EL_32X, "m68k 32x r8 [%06x] %02x @%06x", a, d, SekPc); |
138 | return d; |
139 | } |
140 | |
141 | u32 PicoRead16_32x(u32 a) |
142 | { |
143 | u32 d = 0; |
144 | if ((a & 0xffc0) == 0x5100) { // a15100 |
145 | d = p32x_reg_read16(a); |
146 | goto out; |
147 | } |
148 | |
974fdb5b |
149 | if (!(Pico32x.regs[0] & 1)) |
150 | goto no_vdp; |
151 | |
152 | if ((a & 0xfff0) == 0x5180) { // a15180 |
be2c4208 |
153 | d = p32x_vdp_read16(a); |
154 | goto out; |
155 | } |
156 | |
974fdb5b |
157 | if ((a & 0xfe00) == 0x5200) { // a15200 |
158 | d = Pico32xMem->pal[(a & 0x1ff) / 2]; |
159 | goto out; |
160 | } |
161 | |
162 | no_vdp: |
be2c4208 |
163 | if ((a & 0xfffc) == 0x30ec) { // a130ec |
164 | d = !(a & 2) ? ('M'<<8)|'A' : ('R'<<8)|'S'; |
165 | goto out; |
166 | } |
167 | |
168 | elprintf(EL_UIO, "m68k unmapped r16 [%06x] @%06x", a, SekPc); |
169 | return d; |
170 | |
171 | out: |
172 | elprintf(EL_32X, "m68k 32x r16 [%06x] %04x @%06x", a, d, SekPc); |
173 | return d; |
174 | } |
175 | |
176 | void PicoWrite8_32x(u32 a, u32 d) |
177 | { |
178 | if ((a & 0xfc00) == 0x5000) |
179 | elprintf(EL_32X, "m68k 32x w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
180 | |
181 | if ((a & 0xffc0) == 0x5100) { // a15100 |
182 | p32x_reg_write8(a, d); |
183 | return; |
184 | } |
185 | |
974fdb5b |
186 | if (!(Pico32x.regs[0] & 1)) |
187 | goto no_vdp; |
188 | |
189 | if ((a & 0xfff0) == 0x5180) { // a15180 |
be2c4208 |
190 | p32x_vdp_write8(a, d); |
191 | return; |
192 | } |
193 | |
974fdb5b |
194 | // TODO: verify |
195 | if ((a & 0xfe00) == 0x5200) { // a15200 |
196 | elprintf(EL_32X|EL_ANOMALY, "m68k 32x PAL w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
197 | ((u8 *)Pico32xMem->pal)[(a & 0x1ff) ^ 1] = d; |
198 | Pico32x.dirty_pal = 1; |
199 | return; |
200 | } |
201 | |
202 | no_vdp: |
be2c4208 |
203 | elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
204 | } |
205 | |
206 | void PicoWrite16_32x(u32 a, u32 d) |
207 | { |
208 | if ((a & 0xfc00) == 0x5000) |
209 | elprintf(EL_UIO, "m68k 32x w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
210 | |
211 | if ((a & 0xffc0) == 0x5100) { // a15100 |
212 | p32x_reg_write16(a, d); |
213 | return; |
214 | } |
215 | |
974fdb5b |
216 | if (!(Pico32x.regs[0] & 1)) |
217 | goto no_vdp; |
218 | |
219 | if ((a & 0xfff0) == 0x5180) { // a15180 |
be2c4208 |
220 | p32x_vdp_write16(a, d); |
221 | return; |
222 | } |
223 | |
974fdb5b |
224 | if ((a & 0xfe00) == 0x5200) { // a15200 |
225 | Pico32xMem->pal[(a & 0x1ff) / 2] = d; |
226 | Pico32x.dirty_pal = 1; |
227 | return; |
228 | } |
229 | |
230 | no_vdp: |
be2c4208 |
231 | elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
232 | } |
233 | |
234 | // hint vector is writeable |
235 | static void PicoWrite8_hint(u32 a, u32 d) |
236 | { |
237 | if ((a & 0xfffc) == 0x0070) { |
238 | Pico32xMem->m68k_rom[a ^ 1] = d; |
239 | return; |
240 | } |
241 | |
242 | elprintf(EL_UIO, "m68k unmapped w8 [%06x] %02x @%06x", a, d & 0xff, SekPc); |
243 | } |
244 | |
245 | static void PicoWrite16_hint(u32 a, u32 d) |
246 | { |
247 | if ((a & 0xfffc) == 0x0070) { |
248 | ((u16 *)Pico32xMem->m68k_rom)[a/2] = d; |
249 | return; |
250 | } |
251 | |
252 | elprintf(EL_UIO, "m68k unmapped w16 [%06x] %04x @%06x", a, d & 0xffff, SekPc); |
253 | } |
254 | |
974fdb5b |
255 | void Pico32xSwapDRAM(int b) |
256 | { |
257 | cpu68k_map_set(m68k_read8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
258 | cpu68k_map_set(m68k_read16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
259 | cpu68k_map_set(m68k_write8_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
260 | cpu68k_map_set(m68k_write16_map, 0x840000, 0x85ffff, Pico32xMem->dram[b], 0); |
261 | } |
262 | |
be2c4208 |
263 | #define HWSWAP(x) (((x) << 16) | ((x) >> 16)) |
264 | void PicoMemSetup32x(void) |
265 | { |
266 | unsigned short *ps; |
267 | unsigned int *pl; |
268 | unsigned int rs, rs1; |
269 | int i; |
270 | |
271 | Pico32xMem = calloc(1, sizeof(*Pico32xMem)); |
272 | if (Pico32xMem == NULL) { |
273 | elprintf(EL_STATUS, "OOM"); |
274 | return; |
275 | } |
276 | |
277 | // generate 68k ROM |
278 | ps = (unsigned short *)Pico32xMem->m68k_rom; |
279 | pl = (unsigned int *)Pico32xMem->m68k_rom; |
280 | for (i = 1; i < 0xc0/4; i++) |
974fdb5b |
281 | pl[i] = HWSWAP(0x880200 + (i - 1) * 6); |
be2c4208 |
282 | |
283 | // fill with nops |
284 | for (i = 0xc0/2; i < 0x100/2; i++) |
285 | ps[i] = 0x4e71; |
286 | |
287 | ps[0xc0/2] = 0x46fc; |
288 | ps[0xc2/2] = 0x2700; // move #0x2700,sr |
289 | ps[0xfe/2] = 0x60fe; // jump to self |
290 | |
291 | // fill remaining mem with ROM |
974fdb5b |
292 | memcpy(Pico32xMem->m68k_rom + 0x100, Pico.rom + 0x100, sizeof(Pico32xMem->m68k_rom) - 0x100); |
be2c4208 |
293 | |
294 | // cartridge area becomes unmapped |
295 | // XXX: we take the easy way and don't unmap ROM, |
296 | // so that we can avoid handling the RV bit. |
297 | // m68k_map_unmap(0x000000, 0x3fffff); |
298 | |
299 | // MD ROM area |
974fdb5b |
300 | rs = sizeof(Pico32xMem->m68k_rom); |
301 | cpu68k_map_set(m68k_read8_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0); |
302 | cpu68k_map_set(m68k_read16_map, 0x000000, rs - 1, Pico32xMem->m68k_rom, 0); |
303 | cpu68k_map_set(m68k_write8_map, 0x000000, rs - 1, PicoWrite8_hint, 1); // TODO verify |
304 | cpu68k_map_set(m68k_write16_map, 0x000000, rs - 1, PicoWrite16_hint, 1); |
305 | |
306 | // DRAM area |
307 | Pico32xSwapDRAM(1); |
be2c4208 |
308 | |
309 | // 32X ROM (unbanked, XXX: consider mirroring?) |
310 | rs1 = rs = (Pico.romsize + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
311 | if (rs1 > 0x80000) |
312 | rs1 = 0x80000; |
313 | cpu68k_map_set(m68k_read8_map, 0x880000, 0x880000 + rs1 - 1, Pico.rom, 0); |
314 | cpu68k_map_set(m68k_read16_map, 0x880000, 0x880000 + rs1 - 1, Pico.rom, 0); |
315 | |
316 | // 32X ROM (banked) |
317 | if (rs > 0x100000) |
318 | rs = 0x100000; |
319 | cpu68k_map_set(m68k_read8_map, 0x900000, 0x900000 + rs - 1, Pico.rom, 0); |
320 | cpu68k_map_set(m68k_read16_map, 0x900000, 0x900000 + rs - 1, Pico.rom, 0); |
321 | } |
322 | |