| 1 | /* |
| 2 | * Support for a few cart mappers. |
| 3 | * |
| 4 | * (c) Copyright 2008-2009, Grazvydas "notaz" Ignotas |
| 5 | * Free for non-commercial use. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "../pico_int.h" |
| 10 | #include "../memory.h" |
| 11 | |
| 12 | |
| 13 | /* Common *-in-1 pirate mapper. |
| 14 | * Switches banks based on addr lines when /TIME is set. |
| 15 | * TODO: verify |
| 16 | */ |
| 17 | static unsigned int carthw_Xin1_baddr = 0; |
| 18 | |
| 19 | static void carthw_Xin1_do(u32 a, int mask, int shift) |
| 20 | { |
| 21 | int len; |
| 22 | |
| 23 | carthw_Xin1_baddr = a; |
| 24 | a &= mask; |
| 25 | a <<= shift; |
| 26 | len = Pico.romsize - a; |
| 27 | if (len <= 0) { |
| 28 | elprintf(EL_ANOMALY|EL_STATUS, "X-in-1: missing bank @ %06x", a); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | len = (len + M68K_BANK_MASK) & ~M68K_BANK_MASK; |
| 33 | cpu68k_map_set(m68k_read8_map, 0x000000, len - 1, Pico.rom + a, 0); |
| 34 | cpu68k_map_set(m68k_read16_map, 0x000000, len - 1, Pico.rom + a, 0); |
| 35 | } |
| 36 | |
| 37 | static carthw_state_chunk carthw_Xin1_state[] = |
| 38 | { |
| 39 | { CHUNK_CARTHW, sizeof(carthw_Xin1_baddr), &carthw_Xin1_baddr }, |
| 40 | { 0, 0, NULL } |
| 41 | }; |
| 42 | |
| 43 | // TODO: test a0, reads, w16 |
| 44 | static void carthw_Xin1_write8(u32 a, u32 d) |
| 45 | { |
| 46 | if ((a & 0xffff00) != 0xa13000) { |
| 47 | PicoWrite8_io(a, d); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | carthw_Xin1_do(a, 0x3f, 16); |
| 52 | } |
| 53 | |
| 54 | static void carthw_Xin1_mem_setup(void) |
| 55 | { |
| 56 | cpu68k_map_set(m68k_write8_map, 0xa10000, 0xa1ffff, carthw_Xin1_write8, 1); |
| 57 | } |
| 58 | |
| 59 | static void carthw_Xin1_reset(void) |
| 60 | { |
| 61 | carthw_Xin1_write8(0xa13000, 0); |
| 62 | } |
| 63 | |
| 64 | static void carthw_Xin1_statef(void) |
| 65 | { |
| 66 | carthw_Xin1_write8(carthw_Xin1_baddr, 0); |
| 67 | } |
| 68 | |
| 69 | void carthw_Xin1_startup(void) |
| 70 | { |
| 71 | elprintf(EL_STATUS, "X-in-1 mapper startup"); |
| 72 | |
| 73 | PicoCartMemSetup = carthw_Xin1_mem_setup; |
| 74 | PicoResetHook = carthw_Xin1_reset; |
| 75 | PicoLoadStateHook = carthw_Xin1_statef; |
| 76 | carthw_chunks = carthw_Xin1_state; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* Realtec, based on TascoDLX doc |
| 81 | * http://www.sharemation.com/TascoDLX/REALTEC%20Cart%20Mapper%20-%20description%20v1.txt |
| 82 | */ |
| 83 | static int realtec_bank = 0x80000000, realtec_size = 0x80000000; |
| 84 | |
| 85 | static void carthw_realtec_write8(u32 a, u32 d) |
| 86 | { |
| 87 | int i, bank_old = realtec_bank, size_old = realtec_size; |
| 88 | |
| 89 | if (a == 0x400000) |
| 90 | { |
| 91 | realtec_bank &= 0x0e0000; |
| 92 | realtec_bank |= 0x300000 & (d << 19); |
| 93 | if (realtec_bank != bank_old) |
| 94 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
| 95 | } |
| 96 | else if (a == 0x402000) |
| 97 | { |
| 98 | realtec_size = (d << 17) & 0x3e0000; |
| 99 | if (realtec_size != size_old) |
| 100 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
| 101 | } |
| 102 | else if (a == 0x404000) |
| 103 | { |
| 104 | realtec_bank &= 0x300000; |
| 105 | realtec_bank |= 0x0e0000 & (d << 17); |
| 106 | if (realtec_bank != bank_old) |
| 107 | elprintf(EL_ANOMALY, "write [%06x] %02x @ %06x", a, d, SekPc); |
| 108 | } |
| 109 | else |
| 110 | elprintf(EL_ANOMALY, "realtec: unexpected write [%06x] %02x @ %06x", a, d, SekPc); |
| 111 | |
| 112 | if (realtec_bank >= 0 && realtec_size >= 0 && |
| 113 | (realtec_bank != bank_old || realtec_size != size_old)) |
| 114 | { |
| 115 | elprintf(EL_ANOMALY, "realtec: new bank %06x, size %06x", realtec_bank, realtec_size, SekPc); |
| 116 | if (realtec_size > Pico.romsize - realtec_bank) |
| 117 | { |
| 118 | elprintf(EL_ANOMALY, "realtec: bank too large / out of range?"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | for (i = 0; i < 0x400000; i += realtec_size) { |
| 123 | cpu68k_map_set(m68k_read8_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
| 124 | cpu68k_map_set(m68k_read16_map, i, realtec_size - 1, Pico.rom + realtec_bank, 0); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void carthw_realtec_reset(void) |
| 130 | { |
| 131 | int i; |
| 132 | |
| 133 | /* map boot code */ |
| 134 | for (i = 0; i < 0x400000; i += M68K_BANK_SIZE) { |
| 135 | cpu68k_map_set(m68k_read8_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
| 136 | cpu68k_map_set(m68k_read16_map, i, i + M68K_BANK_SIZE - 1, Pico.rom + Pico.romsize, 0); |
| 137 | } |
| 138 | cpu68k_map_set(m68k_write8_map, 0x400000, 0x400000 + M68K_BANK_SIZE - 1, carthw_realtec_write8, 1); |
| 139 | realtec_bank = realtec_size = 0x80000000; |
| 140 | } |
| 141 | |
| 142 | void carthw_realtec_startup(void) |
| 143 | { |
| 144 | void *tmp; |
| 145 | int i; |
| 146 | |
| 147 | elprintf(EL_STATUS, "Realtec mapper startup"); |
| 148 | |
| 149 | // allocate additional bank for boot code |
| 150 | // (we know those ROMs have aligned size) |
| 151 | tmp = realloc(Pico.rom, Pico.romsize + M68K_BANK_SIZE); |
| 152 | if (tmp == NULL) |
| 153 | { |
| 154 | elprintf(EL_STATUS, "OOM"); |
| 155 | return; |
| 156 | } |
| 157 | Pico.rom = tmp; |
| 158 | |
| 159 | // create bank for boot code |
| 160 | for (i = 0; i < M68K_BANK_SIZE; i += 0x2000) |
| 161 | memcpy(Pico.rom + Pico.romsize + i, Pico.rom + Pico.romsize - 0x2000, 0x2000); |
| 162 | |
| 163 | PicoResetHook = carthw_realtec_reset; |
| 164 | } |
| 165 | |
| 166 | /* Radica mapper, based on DevSter's info |
| 167 | * http://devster.monkeeh.com/sega/radica/ |
| 168 | * XXX: mostly the same as X-in-1, merge? |
| 169 | */ |
| 170 | static u32 carthw_radica_read16(u32 a) |
| 171 | { |
| 172 | if ((a & 0xffff00) != 0xa13000) |
| 173 | return PicoRead16_io(a); |
| 174 | |
| 175 | carthw_Xin1_do(a, 0x7e, 15); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void carthw_radica_mem_setup(void) |
| 181 | { |
| 182 | cpu68k_map_set(m68k_read16_map, 0xa10000, 0xa1ffff, carthw_radica_read16, 1); |
| 183 | } |
| 184 | |
| 185 | static void carthw_radica_statef(void) |
| 186 | { |
| 187 | carthw_radica_read16(carthw_Xin1_baddr); |
| 188 | } |
| 189 | |
| 190 | static void carthw_radica_reset(void) |
| 191 | { |
| 192 | carthw_radica_read16(0xa13000); |
| 193 | } |
| 194 | |
| 195 | void carthw_radica_startup(void) |
| 196 | { |
| 197 | elprintf(EL_STATUS, "Radica mapper startup"); |
| 198 | |
| 199 | PicoCartMemSetup = carthw_radica_mem_setup; |
| 200 | PicoResetHook = carthw_radica_reset; |
| 201 | PicoLoadStateHook = carthw_radica_statef; |
| 202 | carthw_chunks = carthw_Xin1_state; |
| 203 | } |
| 204 | |