lowercasing filenames, part1; makefile adjustments
[picodrive.git] / Pico / carthw / carthw.c
CommitLineData
a12b1b29 1/*
65ca3034 2 * Support for a few cart mappers.
3 *
4 * (c) Copyright 2008, Grazvydas "notaz" Ignotas
5 * Free for non-commercial use.
6 *
7 *
8 * I should better do some pointer stuff here. But as none of these bankswitch
a12b1b29 9 * while the game runs, memcpy will suffice.
10 */
11
efcba75f 12#include "../pico_int.h"
757f8dae 13
a12b1b29 14
65ca3034 15/* 12-in-1 and 4-in-1. Assuming >= 2MB ROMs here. */
bdec53c9 16static unsigned int carthw_12in1_baddr = 0;
17
18static carthw_state_chunk carthw_12in1_state[] =
19{
20 { CHUNK_CARTHW, sizeof(carthw_12in1_baddr), &carthw_12in1_baddr },
21 { 0, 0, NULL }
22};
23
757f8dae 24static unsigned int carthw_12in1_read16(unsigned int a, int realsize)
25{
26 // ??
27 elprintf(EL_UIO, "12-in-1: read [%06x] @ %06x", a, SekPc);
28 return 0;
29}
30
31static void carthw_12in1_write8(unsigned int a, unsigned int d, int realsize)
32{
33 int len;
34
35 if (a < 0xA13000 || a >= 0xA13040) {
a12b1b29 36 /* 4-in-1 has Real Deal Boxing, which uses serial eeprom,
37 * but I really doubt that pirate cart had it */
38 if (a != 0x200001)
39 elprintf(EL_ANOMALY, "12-in-1: unexpected write [%06x] %02x @ %06x", a, d, SekPc);
40 return;
757f8dae 41 }
42
bdec53c9 43 carthw_12in1_baddr = a;
757f8dae 44 a &= 0x3f; a <<= 16;
45 len = Pico.romsize - a;
46 if (len <= 0) {
0b35350d 47 elprintf(EL_ANOMALY|EL_STATUS, "12-in-1: missing bank @ %06x", a);
757f8dae 48 return;
49 }
50
a12b1b29 51 memcpy(Pico.rom, Pico.rom + Pico.romsize + a, len);
757f8dae 52}
53
54static void carthw_12in1_reset(void)
55{
56 carthw_12in1_write8(0xA13000, 0, 0);
57}
58
bdec53c9 59static void carthw_12in1_statef(void)
60{
61 carthw_12in1_write8(carthw_12in1_baddr, 0, 0);
62}
63
757f8dae 64void carthw_12in1_startup(void)
65{
66 void *tmp;
67
68 elprintf(EL_STATUS, "12-in-1 mapper detected");
69
a12b1b29 70 tmp = realloc(Pico.rom, Pico.romsize * 2);
757f8dae 71 if (tmp == NULL)
72 {
73 elprintf(EL_STATUS, "OOM");
74 return;
75 }
a12b1b29 76 Pico.rom = tmp;
77 memcpy(Pico.rom + Pico.romsize, Pico.rom, Pico.romsize);
757f8dae 78
79 PicoRead16Hook = carthw_12in1_read16;
80 PicoWrite8Hook = carthw_12in1_write8;
81 PicoResetHook = carthw_12in1_reset;
bdec53c9 82 PicoLoadStateHook = carthw_12in1_statef;
83 carthw_chunks = carthw_12in1_state;
757f8dae 84}
85
a12b1b29 86
87/* Realtec, based on TascoDLX doc
88 * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt
89 */
90static int realtec_bank = 0x80000000, realtec_size = 0x80000000;
91static int realtec_romsize = 0;
92
93static void carthw_realtec_write8(unsigned int a, unsigned int d, int realsize)
94{
95 int i, bank_old = realtec_bank, size_old = realtec_size;
96
97 if (a == 0x400000)
98 {
99 realtec_bank &= 0x0e0000;
100 realtec_bank |= 0x300000 & (d << 19);
101 if (realtec_bank != bank_old)
102 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
103 }
104 else if (a == 0x402000)
105 {
106 realtec_size = (d << 17) & 0x3e0000;
107 if (realtec_size != size_old)
108 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
109 }
110 else if (a == 0x404000)
111 {
112 realtec_bank &= 0x300000;
113 realtec_bank |= 0x0e0000 & (d << 17);
114 if (realtec_bank != bank_old)
115 elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc);
116 }
117 else
118 elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc);
65ca3034 119
a12b1b29 120 if (realtec_bank >= 0 && realtec_size >= 0 &&
121 (realtec_bank != bank_old || realtec_size != size_old))
122 {
123 elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc);
124 if (realtec_size > realtec_romsize - realtec_bank || realtec_bank >= realtec_romsize)
125 {
126 elprintf(EL_ANOMALY, "realtec: bank too large / out of range?");
127 return;
128 }
129
130 for (i = 0; i < 0x400000; i += realtec_size)
131 memcpy(Pico.rom + i, Pico.rom + 0x400000 + realtec_bank, realtec_size);
132 }
133}
134
0b35350d 135static void carthw_realtec_reset(void)
a12b1b29 136{
137 int i;
138 /* map boot code */
139 for (i = 0; i < 0x400000; i += 0x2000)
140 memcpy(Pico.rom + i, Pico.rom + 0x400000 + realtec_romsize - 0x2000, 0x2000);
141 realtec_bank = realtec_size = 0x80000000;
142}
143
144void carthw_realtec_startup(void)
145{
146 void *tmp;
147
148 elprintf(EL_STATUS, "Realtec mapper detected");
149
150 realtec_romsize = Pico.romsize;
151 Pico.romsize = 0x400000;
152 tmp = realloc(Pico.rom, 0x400000 + realtec_romsize);
153 if (tmp == NULL)
154 {
155 elprintf(EL_STATUS, "OOM");
156 return;
157 }
158 Pico.rom = tmp;
159 memcpy(Pico.rom + 0x400000, Pico.rom, realtec_romsize);
160
161 PicoWrite8Hook = carthw_realtec_write8;
162 PicoResetHook = carthw_realtec_reset;
163}
164
0b35350d 165/* Radica mapper, based on DevSter's info
166 * http://devster.monkeeh.com/sega/radica/
167 */
168static unsigned int carthw_radica_baddr = 0;
169
170static carthw_state_chunk carthw_radica_state[] =
171{
172 { CHUNK_CARTHW, sizeof(carthw_radica_baddr), &carthw_radica_baddr },
173 { 0, 0, NULL }
174};
175
176static unsigned int carthw_radica_read16(unsigned int a, int realsize)
177{
178 if ((a & 0xffff80) != 0xa13000) {
179 elprintf(EL_UIO, "radica: r16 %06x", a);
180 return 0;
181 }
182
183 carthw_radica_baddr = a;
184 a = (a & 0x7e) << 15;
185 if (a >= Pico.romsize) {
186 elprintf(EL_ANOMALY|EL_STATUS, "radica: missing bank @ %06x", a);
187 return 0;
188 }
189 memcpy(Pico.rom, Pico.rom + Pico.romsize + a, Pico.romsize - a);
190
191 return 0;
192}
193
194static void carthw_radica_statef(void)
195{
196 carthw_radica_read16(carthw_radica_baddr, 0);
197}
198
199static void carthw_radica_reset(void)
200{
201 memcpy(Pico.rom, Pico.rom + Pico.romsize, Pico.romsize);
202}
203
204void carthw_radica_startup(void)
205{
206 void *tmp;
207
208 elprintf(EL_STATUS, "Radica mapper detected");
209
210 tmp = realloc(Pico.rom, Pico.romsize * 2);
211 if (tmp == NULL)
212 {
213 elprintf(EL_STATUS, "OOM");
214 return;
215 }
216 Pico.rom = tmp;
217 memcpy(Pico.rom + Pico.romsize, Pico.rom, Pico.romsize);
218
219 PicoRead16Hook = carthw_radica_read16;
220 PicoResetHook = carthw_radica_reset;
221 PicoLoadStateHook = carthw_radica_statef;
222 carthw_chunks = carthw_radica_state;
223}
224
225