src/mame/machine/snesdsp2.c
| r0 | r3406 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | snesdsp2.c |
| 4 | |
| 5 | File to handle emulation of the SNES "DSP-2" add-on chip. |
| 6 | |
| 7 | Original C++ code by byuu, based on research by Overload |
| 8 | |
| 9 | MAME/MESS C conversion by etabeta |
| 10 | |
| 11 | ***************************************************************************/ |
| 12 | |
| 13 | int DSP2_waiting_for_command = 0; |
| 14 | int DSP2_command = 0; |
| 15 | int DSP2_in_count = 0, DSP2_in_index = 0; |
| 16 | int DSP2_out_count = 0, DSP2_out_index = 0; |
| 17 | |
| 18 | UINT8 DSP2_parameters[512] = {0}; |
| 19 | UINT8 DSP2_output[512] = {0}; |
| 20 | |
| 21 | UINT8 DSP2_op05transparent = 0; |
| 22 | int DSP2_op05haslen = 0; |
| 23 | int DSP2_op05len = 0; |
| 24 | int DSP2_op06haslen = 0; |
| 25 | int DSP2_op06len = 0; |
| 26 | UINT16 DSP2_op09word1 = 0; |
| 27 | UINT16 DSP2_op09word2 = 0; |
| 28 | int DSP2_op0dhaslen = 0; |
| 29 | int DSP2_op0doutlen = 0; |
| 30 | int DSP2_op0dinlen = 0; |
| 31 | |
| 32 | |
| 33 | //convert bitmap to bitplane tile |
| 34 | void DSP2_op01( void ) |
| 35 | { |
| 36 | //op01 size is always 32 bytes input and output |
| 37 | //the hardware does strange things if you vary the size |
| 38 | |
| 39 | unsigned char c0, c1, c2, c3; |
| 40 | unsigned char *p1 = DSP2_parameters; |
| 41 | unsigned char *p2a = DSP2_output; |
| 42 | unsigned char *p2b = DSP2_output + 16; //halfway |
| 43 | int j; |
| 44 | |
| 45 | //process 8 blocks of 4 bytes each |
| 46 | for (j = 0; j < 8; j++) |
| 47 | { |
| 48 | c0 = *p1++; |
| 49 | c1 = *p1++; |
| 50 | c2 = *p1++; |
| 51 | c3 = *p1++; |
| 52 | |
| 53 | *p2a++ = (c0 & 0x10) << 3 | |
| 54 | (c0 & 0x01) << 6 | |
| 55 | (c1 & 0x10) << 1 | |
| 56 | (c1 & 0x01) << 4 | |
| 57 | (c2 & 0x10) >> 1 | |
| 58 | (c2 & 0x01) << 2 | |
| 59 | (c3 & 0x10) >> 3 | |
| 60 | (c3 & 0x01); |
| 61 | |
| 62 | *p2a++ = (c0 & 0x20) << 2 | |
| 63 | (c0 & 0x02) << 5 | |
| 64 | (c1 & 0x20) | |
| 65 | (c1 & 0x02) << 3 | |
| 66 | (c2 & 0x20) >> 2 | |
| 67 | (c2 & 0x02) << 1 | |
| 68 | (c3 & 0x20) >> 4 | |
| 69 | (c3 & 0x02) >> 1; |
| 70 | |
| 71 | *p2b++ = (c0 & 0x40) << 1 | |
| 72 | (c0 & 0x04) << 4 | |
| 73 | (c1 & 0x40) >> 1 | |
| 74 | (c1 & 0x04) << 2 | |
| 75 | (c2 & 0x40) >> 3 | |
| 76 | (c2 & 0x04) | |
| 77 | (c3 & 0x40) >> 5 | |
| 78 | (c3 & 0x04) >> 2; |
| 79 | |
| 80 | *p2b++ = (c0 & 0x80) | |
| 81 | (c0 & 0x08) << 3 | |
| 82 | (c1 & 0x80) >> 2 | |
| 83 | (c1 & 0x08) << 1 | |
| 84 | (c2 & 0x80) >> 4 | |
| 85 | (c2 & 0x08) >> 1 | |
| 86 | (c3 & 0x80) >> 6 | |
| 87 | (c3 & 0x08) >> 3; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | //set transparent color |
| 92 | void DSP2_op03( void ) |
| 93 | { |
| 94 | DSP2_op05transparent = DSP2_parameters[0]; |
| 95 | } |
| 96 | |
| 97 | //replace bitmap using transparent color |
| 98 | void DSP2_op05( void ) |
| 99 | { |
| 100 | UINT8 color; |
| 101 | // Overlay bitmap with transparency. |
| 102 | // Input: |
| 103 | // |
| 104 | // Bitmap 1: i[0] <=> i[size-1] |
| 105 | // Bitmap 2: i[size] <=> i[2*size-1] |
| 106 | // |
| 107 | // Output: |
| 108 | // |
| 109 | // Bitmap 3: o[0] <=> o[size-1] |
| 110 | // |
| 111 | // Processing: |
| 112 | // |
| 113 | // Process all 4-bit pixels (nibbles) in the bitmap |
| 114 | // |
| 115 | // if ( BM2_pixel == transparent_color ) |
| 116 | // pixelout = BM1_pixel |
| 117 | // else |
| 118 | // pixelout = BM2_pixel |
| 119 | |
| 120 | // The max size bitmap is limited to 255 because the size parameter is a byte |
| 121 | // I think size=0 is an error. The behavior of the chip on size=0 is to |
| 122 | // return the last value written to DR if you read DR on Op05 with |
| 123 | // size = 0. I don't think it's worth implementing this quirk unless it's |
| 124 | // proven necessary. |
| 125 | |
| 126 | unsigned char c1, c2; |
| 127 | unsigned char *p1 = DSP2_parameters; |
| 128 | unsigned char *p2 = DSP2_parameters + DSP2_op05len; |
| 129 | unsigned char *p3 = DSP2_output; |
| 130 | int n; |
| 131 | |
| 132 | color = DSP2_op05transparent & 0x0f; |
| 133 | |
| 134 | for (n = 0; n < DSP2_op05len; n++) |
| 135 | { |
| 136 | c1 = *p1++; |
| 137 | c2 = *p2++; |
| 138 | *p3++ = ( ((c2 >> 4) == color ) ? c1 & 0xf0 : c2 & 0xf0 ) | |
| 139 | ( ((c2 & 0x0f) == color ) ? c1 & 0x0f : c2 & 0x0f ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | //reverse bitmap |
| 144 | void DSP2_op06( void ) |
| 145 | { |
| 146 | // Input: |
| 147 | // size |
| 148 | // bitmap |
| 149 | int i, j; |
| 150 | for (i = 0, j = DSP2_op06len - 1; i < DSP2_op06len; i++, j--) |
| 151 | { |
| 152 | DSP2_output[j] = (DSP2_parameters[i] << 4) | (DSP2_parameters[i] >> 4); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | //multiply |
| 157 | void DSP2_op09( void ) |
| 158 | { |
| 159 | UINT32 r = 0; |
| 160 | DSP2_out_count = 4; |
| 161 | |
| 162 | DSP2_op09word1 = DSP2_parameters[0] | (DSP2_parameters[1] << 8); |
| 163 | DSP2_op09word2 = DSP2_parameters[2] | (DSP2_parameters[3] << 8); |
| 164 | |
| 165 | r = DSP2_op09word1 * DSP2_op09word2; |
| 166 | DSP2_output[0] = r; |
| 167 | DSP2_output[1] = r >> 8; |
| 168 | DSP2_output[2] = r >> 16; |
| 169 | DSP2_output[3] = r >> 24; |
| 170 | } |
| 171 | |
| 172 | //scale bitmap |
| 173 | void DSP2_op0d( void ) |
| 174 | { |
| 175 | // Bit accurate hardware algorithm - uses fixed point math |
| 176 | // This should match the DSP2 Op0D output exactly |
| 177 | // I wouldn't recommend using this unless you're doing hardware debug. |
| 178 | // In some situations it has small visual artifacts that |
| 179 | // are not readily apparent on a TV screen but show up clearly |
| 180 | // on a monitor. Use Overload's scaling instead. |
| 181 | // This is for hardware verification testing. |
| 182 | // |
| 183 | // One note: the HW can do odd byte scaling but since we divide |
| 184 | // by two to get the count of bytes this won't work well for |
| 185 | // odd byte scaling (in any of the current algorithm implementations). |
| 186 | // So far I haven't seen Dungeon Master use it. |
| 187 | // If it does we can adjust the parameters and code to work with it |
| 188 | |
| 189 | UINT32 multiplier; // Any size int >= 32-bits |
| 190 | UINT32 pixloc; // match size of multiplier |
| 191 | int i, j; |
| 192 | UINT8 pixelarray[512]; |
| 193 | if (DSP2_op0dinlen <= DSP2_op0doutlen) |
| 194 | { |
| 195 | multiplier = 0x10000; // In our self defined fixed point 0x10000 == 1 |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | multiplier = (DSP2_op0dinlen << 17) / ((DSP2_op0doutlen << 1) + 1); |
| 200 | } |
| 201 | |
| 202 | pixloc = 0; |
| 203 | for (i = 0; i < DSP2_op0doutlen * 2; i++) |
| 204 | { |
| 205 | j = pixloc >> 16; |
| 206 | |
| 207 | if (j & 1) |
| 208 | pixelarray[i] = (DSP2_parameters[j >> 1] & 0x0f); |
| 209 | else |
| 210 | pixelarray[i] = (DSP2_parameters[j >> 1] & 0xf0) >> 4; |
| 211 | |
| 212 | pixloc += multiplier; |
| 213 | } |
| 214 | |
| 215 | for (i = 0; i < DSP2_op0doutlen; i++) |
| 216 | { |
| 217 | DSP2_output[i] = (pixelarray[i << 1] << 4) | pixelarray[(i << 1) + 1]; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | |
| 222 | void DSP2_reset( void ) |
| 223 | { |
| 224 | DSP2_waiting_for_command = 1; |
| 225 | DSP2_in_count = 0; |
| 226 | DSP2_in_index = 0; |
| 227 | DSP2_out_count = 0; |
| 228 | DSP2_out_index = 0; |
| 229 | |
| 230 | DSP2_op05transparent = 0; |
| 231 | DSP2_op05haslen = 0; |
| 232 | DSP2_op05len = 0; |
| 233 | DSP2_op06haslen = 0; |
| 234 | DSP2_op06len = 0; |
| 235 | DSP2_op09word1 = 0; |
| 236 | DSP2_op09word2 = 0; |
| 237 | DSP2_op0dhaslen = 0; |
| 238 | DSP2_op0doutlen = 0; |
| 239 | DSP2_op0dinlen = 0; |
| 240 | } |
| 241 | |
| 242 | UINT8 DSP2_read( void ) |
| 243 | { |
| 244 | UINT8 r = 0xff; |
| 245 | if (DSP2_out_count) |
| 246 | { |
| 247 | r = DSP2_output[DSP2_out_index++]; |
| 248 | DSP2_out_index &= 511; |
| 249 | if (DSP2_out_count == DSP2_out_index) |
| 250 | DSP2_out_count = 0; |
| 251 | } |
| 252 | return r; |
| 253 | } |
| 254 | |
| 255 | void DSP2_write(UINT8 data) |
| 256 | { |
| 257 | if(DSP2_waiting_for_command) |
| 258 | { |
| 259 | DSP2_command = data; |
| 260 | DSP2_in_index = 0; |
| 261 | DSP2_waiting_for_command = 0; |
| 262 | |
| 263 | switch (data) |
| 264 | { |
| 265 | case 0x01: DSP2_in_count = 32; break; |
| 266 | case 0x03: DSP2_in_count = 1; break; |
| 267 | case 0x05: DSP2_in_count = 1; break; |
| 268 | case 0x06: DSP2_in_count = 1; break; |
| 269 | case 0x07: break; |
| 270 | case 0x08: break; |
| 271 | case 0x09: DSP2_in_count = 4; break; |
| 272 | case 0x0d: DSP2_in_count = 2; break; |
| 273 | case 0x0f: DSP2_in_count = 0; break; |
| 274 | } |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | DSP2_parameters[DSP2_in_index++] = data; |
| 279 | DSP2_in_index &= 511; |
| 280 | } |
| 281 | |
| 282 | if (DSP2_in_count == DSP2_in_index) |
| 283 | { |
| 284 | DSP2_waiting_for_command = 1; |
| 285 | DSP2_out_index = 0; |
| 286 | switch (DSP2_command) |
| 287 | { |
| 288 | case 0x01: |
| 289 | DSP2_out_count = 32; |
| 290 | DSP2_op01(); |
| 291 | break; |
| 292 | |
| 293 | case 0x03: |
| 294 | DSP2_op03(); |
| 295 | break; |
| 296 | |
| 297 | case 0x05: |
| 298 | if (DSP2_op05haslen) |
| 299 | { |
| 300 | DSP2_op05haslen = 0; |
| 301 | DSP2_out_count = DSP2_op05len; |
| 302 | DSP2_op05(); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | DSP2_op05len = DSP2_parameters[0]; |
| 307 | DSP2_in_index = 0; |
| 308 | DSP2_in_count = DSP2_op05len * 2; |
| 309 | DSP2_op05haslen = 1; |
| 310 | if (data) |
| 311 | DSP2_waiting_for_command = 0; |
| 312 | } |
| 313 | break; |
| 314 | |
| 315 | case 0x06: |
| 316 | if (DSP2_op06haslen) |
| 317 | { |
| 318 | DSP2_op06haslen = 0; |
| 319 | DSP2_out_count = DSP2_op06len; |
| 320 | DSP2_op06(); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | DSP2_op06len = DSP2_parameters[0]; |
| 325 | DSP2_in_index = 0; |
| 326 | DSP2_in_count = DSP2_op06len; |
| 327 | DSP2_op06haslen = 1; |
| 328 | if (data) |
| 329 | DSP2_waiting_for_command = 0; |
| 330 | } |
| 331 | break; |
| 332 | |
| 333 | case 0x07: break; |
| 334 | case 0x08: break; |
| 335 | |
| 336 | case 0x09: |
| 337 | DSP2_op09(); |
| 338 | break; |
| 339 | |
| 340 | case 0x0d: |
| 341 | if (DSP2_op0dhaslen) |
| 342 | { |
| 343 | DSP2_op0dhaslen = 0; |
| 344 | DSP2_out_count = DSP2_op0doutlen; |
| 345 | DSP2_op0d(); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | DSP2_op0dinlen = DSP2_parameters[0]; |
| 350 | DSP2_op0doutlen = DSP2_parameters[1]; |
| 351 | DSP2_in_index = 0; |
| 352 | DSP2_in_count = (DSP2_op0dinlen + 1) >> 1; |
| 353 | DSP2_op0dhaslen = 1; |
| 354 | if(data) |
| 355 | DSP2_waiting_for_command = 0; |
| 356 | } |
| 357 | break; |
| 358 | |
| 359 | case 0x0f: |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | } |
src/mame/machine/snes.c
| r3405 | r3406 | |
| 18 | 18 | |
| 19 | 19 | // add-on chip emulators |
| 20 | 20 | #include "machine/snesdsp1.c" |
| 21 | #include "machine/snesdsp2.c" |
| 22 | #include "machine/snesobc1.c" |
| 21 | 23 | |
| 22 | 24 | /* -- Globals -- */ |
| 23 | 25 | UINT8 *snes_ram = NULL; /* 65816 ram */ |
| r3405 | r3406 | |
| 39 | 41 | static emu_timer *snes_hirq_timer; |
| 40 | 42 | static UINT16 hblank_offset; |
| 41 | 43 | static UINT16 snes_htmult; /* in 512 wide, we run HTOTAL double and halve it on latching */ |
| 42 | | static UINT8 has_dsp1; |
| 44 | UINT8 has_addon_chip; |
| 43 | 45 | |
| 44 | 46 | // full graphic variables |
| 45 | 47 | static UINT16 vram_fgr_high, vram_fgr_increment, vram_fgr_count, vram_fgr_mask, vram_fgr_shift, vram_read_buffer; |
| r3405 | r3406 | |
| 275 | 277 | } |
| 276 | 278 | |
| 277 | 279 | |
| 278 | | |
| 279 | 280 | /************************************* |
| 280 | 281 | |
| 281 | 282 | Input Handlers |
| r3405 | r3406 | |
| 1097 | 1098 | /* |
| 1098 | 1099 | There are at least 4 different kind of boards for SNES carts, which we denote with |
| 1099 | 1100 | mode_20, mode_21, mode_22 and mode_25. Below is a layout of the memory for each of |
| 1100 | | them, as described on the SNES dev manual. Notice we mirror ROM at loading time |
| 1101 | | where necessary (e.g. banks 0x80 to 0xff at address 0x8000 for mode_20). |
| 1101 | them. Notice we mirror ROM at loading time where necessary (e.g. banks 0x80 to 0xff |
| 1102 | at address 0x8000 for mode_20). |
| 1102 | 1103 | |
| 1103 | 1104 | MODE_20 |
| 1104 | 1105 | banks 0x00 0x20 0x40 0x60 0x70 0x7e 0x80 0xc0 0xff |
| r3405 | r3406 | |
| 1117 | 1118 | 0x0000 ------------------------------------------------------------------------------- |
| 1118 | 1119 | |
| 1119 | 1120 | MODE_22 is the same, but banks 0x40 to 0x7e at address 0x000 to 0x7fff contain ROM (of |
| 1120 | | course mirrored also at 0xc0 to 0xff). Mode 22 is quite similar to the board SHVC-2P3B |
| 1121 | | shown on SNES Dev manual. It is used also in SDD-1 games (only for the first blocks of |
| 1122 | | data). DSP data & status can be either at banks 0x20 to 0x40 at address 0x8000 or at |
| 1123 | | banks 0x60 to 0x6f at address 0x0000. |
| 1121 | course mirrored also at 0xc0 to 0xff). Mode 22 is quite similar to the board SHVC-2P3B. |
| 1122 | It is used also in SDD-1 games (only for the first blocks of data). DSP data & status |
| 1123 | can be either at banks 0x20 to 0x40 at address 0x8000 or at banks 0x60 to 0x6f at address |
| 1124 | 0x0000. |
| 1124 | 1125 | |
| 1125 | 1126 | |
| 1126 | 1127 | MODE_21 |
| r3405 | r3406 | |
| 1163 | 1164 | |
| 1164 | 1165 | */ |
| 1165 | 1166 | |
| 1166 | | |
| 1167 | 1167 | /* 0x000000 - 0x2fffff */ |
| 1168 | 1168 | READ8_HANDLER( snes_r_bank1 ) |
| 1169 | 1169 | { |
| r3405 | r3406 | |
| 1176 | 1176 | value = snes_r_io(machine, address); |
| 1177 | 1177 | else if (address < 0x8000) |
| 1178 | 1178 | { |
| 1179 | | if ((snes_cart.mode == SNES_MODE_21) && has_dsp1 && (offset < 0x100000)) |
| 1179 | if (has_addon_chip == HAS_OBC1) |
| 1180 | value = obc1_read(machine, offset); |
| 1181 | else if ((has_addon_chip == HAS_DSP2) && (offset >= 0x200000)) |
| 1182 | value = (address < 0x7000) ? DSP2_read() : 0x00; |
| 1183 | else if ((snes_cart.mode == SNES_MODE_21) && (has_addon_chip == HAS_DSP1) && (offset < 0x100000)) |
| 1180 | 1184 | value = (address < 0x7000) ? DSP1_getDr() : DSP1_getSr(); |
| 1181 | 1185 | else |
| 1182 | 1186 | value = 0xff; /* Reserved */ |
| 1183 | 1187 | } |
| 1184 | | else if ((snes_cart.mode == SNES_MODE_20) && has_dsp1 && (offset >= 0x200000)) |
| 1185 | | value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr(); /* This is not mentioned in the SNES dev manual, fwiw */ |
| 1188 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1) && (offset >= 0x200000)) |
| 1189 | value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr(); |
| 1190 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2) && (offset >= 0x200000)) |
| 1191 | value = (address < 0xc000) ? DSP2_read() : 0x00; |
| 1186 | 1192 | else |
| 1187 | 1193 | value = snes_ram[offset]; |
| 1188 | 1194 | |
| r3405 | r3406 | |
| 1201 | 1207 | value = snes_r_io(machine, address); |
| 1202 | 1208 | else if (address < 0x8000) /* SRAM for mode_21, Reserved othewise */ |
| 1203 | 1209 | { |
| 1204 | | if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0)) |
| 1210 | if (has_addon_chip == HAS_OBC1) |
| 1211 | value = obc1_read (machine, offset); |
| 1212 | else if (has_addon_chip == HAS_DSP2) |
| 1213 | value = (address < 0x7000) ? DSP2_read() : 0x00; |
| 1214 | else if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0)) |
| 1205 | 1215 | { |
| 1206 | 1216 | int mask = ((snes_cart.sram * 1024) - 1); /* Limit SRAM size to what's actually present */ |
| 1207 | 1217 | offset -= 0x6000; |
| r3405 | r3406 | |
| 1211 | 1221 | value = 0xff; |
| 1212 | 1222 | } |
| 1213 | 1223 | /* some dsp1 games use these banks 0x30 to 0x3f at address 0x8000 */ |
| 1214 | | else if ((snes_cart.mode == SNES_MODE_20) && has_dsp1) |
| 1224 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1)) |
| 1215 | 1225 | value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr(); |
| 1226 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2)) |
| 1227 | value = (address < 0xc000) ? DSP2_read() : 0x00; |
| 1216 | 1228 | else |
| 1217 | 1229 | value = snes_ram[0x300000 + offset]; |
| 1218 | 1230 | |
| r3405 | r3406 | |
| 1241 | 1253 | /* 0x600000 - 0x6fffff */ |
| 1242 | 1254 | READ8_HANDLER( snes_r_bank4 ) |
| 1243 | 1255 | { |
| 1244 | | UINT8 value = 0; |
| 1256 | UINT8 value = 0xff; |
| 1245 | 1257 | UINT16 address = offset & 0xffff; |
| 1246 | 1258 | |
| 1247 | 1259 | if (snes_cart.mode & 5) /* Mode 20 & 22 */ |
| r3405 | r3406 | |
| 1249 | 1261 | if (address >= 0x8000) |
| 1250 | 1262 | value = snes_ram[0x600000 + offset]; |
| 1251 | 1263 | /* some other dsp1 games use these banks 0x60 to 0x6f at address 0x0000 */ |
| 1252 | | else if (has_dsp1) |
| 1264 | else if (has_addon_chip == HAS_DSP1) |
| 1253 | 1265 | value = (address >= 0x4000) ? DSP1_getSr() : DSP1_getDr(); |
| 1254 | 1266 | else |
| 1255 | 1267 | value = 0xff; /* Reserved */ |
| r3405 | r3406 | |
| 1306 | 1318 | value = 0xff; |
| 1307 | 1319 | } |
| 1308 | 1320 | } |
| 1321 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1) && (offset >= 0x200000)) |
| 1322 | value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr(); |
| 1323 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2) && (offset >= 0x200000)) |
| 1324 | value = (address < 0xc000) ? DSP2_read() : 0x00; |
| 1309 | 1325 | else |
| 1310 | 1326 | value = snes_ram[0x800000 + offset]; |
| 1311 | 1327 | |
| r3405 | r3406 | |
| 1343 | 1359 | snes_w_io(machine, address, data); |
| 1344 | 1360 | else if (address < 0x8000) |
| 1345 | 1361 | { |
| 1346 | | if ((snes_cart.mode == SNES_MODE_21) && has_dsp1 && (offset < 0x100000)) |
| 1362 | if (has_addon_chip == HAS_OBC1) |
| 1363 | obc1_write(machine, offset, data); |
| 1364 | else if ((has_addon_chip == HAS_DSP2) && (offset >= 0x200000)) |
| 1365 | DSP2_write(data); |
| 1366 | else if ((snes_cart.mode == SNES_MODE_21) && (has_addon_chip == HAS_DSP1) && (offset < 0x100000)) |
| 1347 | 1367 | DSP1_setDr(data); |
| 1348 | 1368 | else |
| 1349 | 1369 | logerror( "Attempt to write to reserved address: %X\n", offset ); |
| 1350 | 1370 | } |
| 1351 | | else if ((snes_cart.mode == SNES_MODE_20) && has_dsp1 && (offset >= 0x200000)) |
| 1352 | | DSP1_setDr(data); /* This is not mentioned in the SNES dev manual, fwiw */ |
| 1371 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1) && (offset >= 0x200000)) |
| 1372 | DSP1_setDr(data); |
| 1373 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2) && (offset >= 0x200000) && (address < 0xc000)) |
| 1374 | DSP2_write(data); |
| 1353 | 1375 | else |
| 1354 | 1376 | logerror( "Attempt to write to ROM address: %X\n", offset ); |
| 1355 | 1377 | } |
| r3405 | r3406 | |
| 1365 | 1387 | snes_w_io(machine, address, data); |
| 1366 | 1388 | else if (address < 0x8000) /* SRAM for mode_21, Reserved othewise */ |
| 1367 | 1389 | { |
| 1368 | | if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0)) |
| 1390 | if (has_addon_chip == HAS_OBC1) |
| 1391 | obc1_write(machine, offset, data); |
| 1392 | else if (has_addon_chip == HAS_DSP2) |
| 1393 | DSP2_write(data); |
| 1394 | else if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0)) |
| 1369 | 1395 | { |
| 1370 | 1396 | int mask = ((snes_cart.sram * 1024) - 1); /* Limit SRAM size to what's actually present */ |
| 1371 | 1397 | offset -= 0x6000; |
| r3405 | r3406 | |
| 1373 | 1399 | } |
| 1374 | 1400 | else |
| 1375 | 1401 | logerror("Attempt to write to reserved address: %X\n", offset + 0x300000); |
| 1376 | | } |
| 1402 | } |
| 1377 | 1403 | /* some dsp1 games use these banks 0x30 to 0x3f at address 0x8000 */ |
| 1378 | | else if ((snes_cart.mode == SNES_MODE_20) && has_dsp1) |
| 1404 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1)) |
| 1379 | 1405 | DSP1_setDr(data); |
| 1406 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2) && (address < 0xc000)) |
| 1407 | DSP2_write(data); |
| 1380 | 1408 | else |
| 1381 | 1409 | logerror("Attempt to write to ROM address: %X\n", offset + 0x300000); |
| 1382 | 1410 | } |
| r3405 | r3406 | |
| 1390 | 1418 | { |
| 1391 | 1419 | if (address >= 0x8000) |
| 1392 | 1420 | logerror("Attempt to write to ROM address: %X\n", offset + 0x600000); |
| 1393 | | else if (has_dsp1) |
| 1421 | else if (has_addon_chip == HAS_DSP1) |
| 1394 | 1422 | DSP1_setDr(data); |
| 1395 | 1423 | else |
| 1396 | 1424 | logerror("Attempt to write to reserved address: %X\n", offset + 0x600000); |
| r3405 | r3406 | |
| 1430 | 1458 | { |
| 1431 | 1459 | if (offset < 0x300000) |
| 1432 | 1460 | snes_w_bank1(machine, offset, data); |
| 1433 | | else |
| 1461 | else |
| 1434 | 1462 | snes_w_bank2(machine, offset - 0x300000, data); |
| 1435 | 1463 | } |
| 1436 | 1464 | else /* Mode 25 has SRAM not mirrored from lower banks */ |
| r3405 | r3406 | |
| 1439 | 1467 | { |
| 1440 | 1468 | if (offset < 0x300000) |
| 1441 | 1469 | snes_w_bank1(machine, offset, data); |
| 1442 | | else |
| 1470 | else |
| 1443 | 1471 | snes_w_bank2(machine, offset - 0x300000, data); |
| 1444 | 1472 | } |
| 1445 | 1473 | else if ((offset >= 0x300000) && (snes_cart.sram > 0)) |
| r3405 | r3406 | |
| 1449 | 1477 | snes_ram[0xb06000 + (offset & mask)] = data; |
| 1450 | 1478 | } |
| 1451 | 1479 | else /* Area in 0x6000-0x8000 && offset < 0x300000 is Reserved! */ |
| 1452 | | logerror("Attempt to write to reserved address: %X\n", offset + 0x800000); |
| 1480 | logerror("Attempt to write to reserved address: %X\n", offset + 0x800000); |
| 1453 | 1481 | } |
| 1454 | 1482 | } |
| 1483 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP1) && (offset >= 0x200000)) |
| 1484 | DSP1_setDr(data); |
| 1485 | else if ((snes_cart.mode == SNES_MODE_20) && (has_addon_chip == HAS_DSP2) && (offset >= 0x200000) && (address < 0xc000)) |
| 1486 | DSP2_write(data); |
| 1455 | 1487 | else |
| 1456 | 1488 | logerror("Attempt to write to ROM address: %X\n", offset + 0x800000); |
| 1457 | 1489 | } |
| r3405 | r3406 | |
| 1483 | 1515 | |
| 1484 | 1516 | /************************************* |
| 1485 | 1517 | |
| 1486 | | Driver Init |
| 1518 | Driver Init |
| 1487 | 1519 | |
| 1488 | 1520 | *************************************/ |
| 1489 | 1521 | |
| r3405 | r3406 | |
| 1543 | 1575 | hblank_offset = 268; |
| 1544 | 1576 | timer_adjust_oneshot(snes_hblank_timer, video_screen_get_time_until_pos(machine->primary_screen, ((snes_ram[STAT78] & 0x10) == SNES_NTSC) ? SNES_VTOTAL_NTSC-1 : SNES_VTOTAL_PAL-1, hblank_offset), 0); |
| 1545 | 1577 | |
| 1546 | | // check if DSP1 is present (maybe not 100%?) |
| 1547 | | has_dsp1 = ((snes_r_bank1(machine,0xffd6) >= 3) && (snes_r_bank1(machine,0xffd6) <= 5)) ? 1 : 0; |
| 1578 | switch (has_addon_chip) |
| 1579 | { |
| 1580 | case HAS_DSP2: |
| 1581 | DSP2_reset(); |
| 1582 | break; |
| 1583 | |
| 1584 | case HAS_OBC1: |
| 1585 | obc1_init(); |
| 1586 | break; |
| 1548 | 1587 | |
| 1588 | default: |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1549 | 1592 | // init frame counter so first line is 0 |
| 1550 | 1593 | if( ATTOSECONDS_TO_HZ(video_screen_get_frame_period(machine->primary_screen).attoseconds) >= 59 ) |
| 1551 | 1594 | { |
| r3405 | r3406 | |
| 1638 | 1681 | /* all NSS games seem to use MODE 20 */ |
| 1639 | 1682 | snes_cart.mode = SNES_MODE_20; |
| 1640 | 1683 | snes_cart.sram_max = 0x40000; |
| 1684 | has_addon_chip = HAS_NONE; |
| 1641 | 1685 | |
| 1642 | 1686 | /* Find the number of blocks in this ROM */ |
| 1643 | 1687 | total_blocks = (memory_region_length(machine, "user3") / 0x8000); |
| r3405 | r3406 | |
| 1701 | 1745 | |
| 1702 | 1746 | snes_cart.mode = SNES_MODE_21; |
| 1703 | 1747 | snes_cart.sram_max = 0x40000; |
| 1748 | has_addon_chip = HAS_NONE; |
| 1704 | 1749 | |
| 1705 | 1750 | /* Find the number of blocks in this ROM */ |
| 1706 | 1751 | total_blocks = (memory_region_length(machine, "user3") / 0x10000); |
src/mess/machine/snescart.c
| r3405 | r3406 | |
| 11 | 11 | #include "devices/cartslot.h" |
| 12 | 12 | |
| 13 | 13 | |
| 14 | | /****** Cart types ******/ |
| 15 | | |
| 16 | | static int cart_type; |
| 17 | | |
| 18 | | enum |
| 19 | | { |
| 20 | | STANDARD = 0, |
| 21 | | STANDARD_RAM, |
| 22 | | STANDARD_SRAM, |
| 23 | | DSP1, |
| 24 | | DSP1_RAM, |
| 25 | | DSP1_SRAM, |
| 26 | | DSP2, |
| 27 | | DSP3, |
| 28 | | DSP4, |
| 29 | | SUPERFX, |
| 30 | | SUPERFX_SRAM, |
| 31 | | SA1, |
| 32 | | SA1_SRAM, |
| 33 | | SDD1, |
| 34 | | SDD1_SRAM, |
| 35 | | OBC1, |
| 36 | | RTC, |
| 37 | | SGB, |
| 38 | | C4, |
| 39 | | ST010, |
| 40 | | ST011, |
| 41 | | ST018, |
| 42 | | SPC7110, |
| 43 | | SPC7110_RTC, |
| 44 | | UNKNOWN, |
| 45 | | }; |
| 46 | | |
| 47 | | |
| 48 | 14 | /****** Strings for Cart Header Logging ******/ |
| 49 | 15 | |
| 50 | 16 | #define UNK "Unknown" |
| r3405 | r3406 | |
| 53 | 19 | static const char *types[] = |
| 54 | 20 | { |
| 55 | 21 | "ROM", |
| 56 | | "ROM, RAM", |
| 57 | | "ROM, SRAM", |
| 58 | 22 | "ROM, DSP-1", |
| 59 | | "ROM, RAM, DSP-1", |
| 60 | | "ROM, SRAM, DSP-1", |
| 61 | | "ROM, SRAM, DSP-2", |
| 62 | | "ROM, SRAM, DSP-3", |
| 23 | "ROM, DSP-2", |
| 24 | "ROM, DSP-3", |
| 63 | 25 | "ROM, DSP-4", |
| 64 | 26 | "ROM, Super FX / FX2", |
| 65 | | "ROM, SRAM, Super FX / FX2", |
| 66 | 27 | "ROM, SA-1", |
| 67 | | "ROM, SRAM, SA-1", |
| 68 | 28 | "ROM, S-DD1", |
| 69 | | "ROM, SRAM, S-DD1", |
| 70 | | "ROM, SRAM, OBC-1", |
| 71 | | "ROM, SRAM, S-RTC", |
| 29 | "ROM, OBC-1", |
| 30 | "ROM, S-RTC", |
| 72 | 31 | "ROM, Z80GB (Super Game Boy)", |
| 73 | 32 | "ROM, C4", |
| 74 | | "ROM, SRAM, Seta ST-010", |
| 75 | | "ROM, SRAM, Seta ST-011", |
| 76 | | "ROM, SRAM, Seta ST-018", |
| 77 | | "ROM, SRAM, SPC7110", |
| 78 | | "ROM, SRAM, SPC7110, RTC", |
| 33 | "ROM, Seta ST-010", |
| 34 | "ROM, Seta ST-011", |
| 35 | "ROM, Seta ST-018", |
| 36 | "ROM, SPC7110", |
| 37 | "ROM, SPC7110, RTC", |
| 79 | 38 | UNK, // to add: Satellaview BS-X detection |
| 80 | 39 | }; |
| 81 | 40 | |
| r3405 | r3406 | |
| 413 | 372 | /* No header found so go back to the start of the file */ |
| 414 | 373 | logerror("No header found.\n"); |
| 415 | 374 | offset = 0; |
| 416 | | image_fseek( image, offset, SEEK_SET ); |
| 375 | image_fseek(image, offset, SEEK_SET); |
| 417 | 376 | } |
| 418 | 377 | |
| 419 | 378 | /* We need to take a sample to test what mode we need to be in (the |
| r3405 | r3406 | |
| 628 | 587 | switch (snes_r_bank1(machine, 0x00ffd6)) |
| 629 | 588 | { |
| 630 | 589 | case 0x00: |
| 631 | | cart_type = STANDARD; |
| 632 | | supported_type = 1; |
| 633 | | break; |
| 634 | | |
| 635 | 590 | case 0x01: |
| 636 | | cart_type = STANDARD_RAM; |
| 637 | | supported_type = 1; |
| 638 | | break; |
| 639 | | |
| 640 | 591 | case 0x02: |
| 641 | | cart_type = STANDARD_SRAM; |
| 592 | has_addon_chip = HAS_NONE; |
| 642 | 593 | supported_type = 1; |
| 643 | 594 | break; |
| 644 | 595 | |
| 645 | 596 | case 0x03: |
| 646 | 597 | if (snes_r_bank1(machine, 0x00ffd5) == 0x30) |
| 647 | | cart_type = DSP4; |
| 598 | has_addon_chip = HAS_DSP4; |
| 648 | 599 | else |
| 649 | 600 | { |
| 650 | | cart_type = DSP1; |
| 601 | has_addon_chip = HAS_DSP1; |
| 651 | 602 | supported_type = 1; |
| 652 | 603 | } |
| 653 | 604 | break; |
| 654 | 605 | |
| 655 | 606 | case 0x04: |
| 656 | | cart_type = DSP1_RAM; |
| 607 | has_addon_chip = HAS_DSP1; |
| 657 | 608 | supported_type = 1; |
| 658 | 609 | break; |
| 659 | 610 | |
| 660 | 611 | case 0x05: |
| 661 | 612 | if (snes_r_bank1(machine, 0x00ffd5) == 0x20) |
| 662 | | cart_type = DSP2; |
| 613 | { |
| 614 | has_addon_chip = HAS_DSP2; |
| 615 | supported_type = 1; |
| 616 | } |
| 663 | 617 | /* DSP-3 is hard to detect. We exploit the fact that the only game has been manufactured by Bandai */ |
| 664 | 618 | else if ((snes_r_bank1(machine, 0x00ffd5) == 0x30) && (snes_r_bank1(machine, 0x00ffda) == 0xb2)) |
| 665 | | cart_type = DSP3; |
| 619 | has_addon_chip = HAS_DSP3; |
| 666 | 620 | else |
| 667 | 621 | { |
| 668 | | cart_type = DSP1_SRAM; |
| 622 | has_addon_chip = HAS_DSP1; |
| 669 | 623 | supported_type = 1; |
| 670 | 624 | } |
| 671 | 625 | break; |
| 672 | 626 | |
| 673 | 627 | case 0x13: // Mario Chip 1 |
| 674 | 628 | case 0x14: // GSU-x |
| 675 | | if (snes_r_bank1(machine, 0x00ffd5) == 0x20) |
| 676 | | cart_type = SUPERFX; |
| 677 | | break; |
| 678 | | |
| 679 | 629 | case 0x15: // GSU-x |
| 680 | 630 | case 0x1a: // GSU-1 (21 Mhz at start) |
| 681 | 631 | if (snes_r_bank1(machine, 0x00ffd5) == 0x20) |
| 682 | | cart_type = SUPERFX_SRAM; |
| 632 | has_addon_chip = HAS_SUPERFX; |
| 683 | 633 | break; |
| 684 | 634 | |
| 685 | 635 | case 0x25: |
| 686 | | cart_type = OBC1; |
| 636 | has_addon_chip = HAS_OBC1; |
| 637 | supported_type = 1; |
| 687 | 638 | break; |
| 688 | 639 | |
| 689 | | case 0x34: |
| 690 | | if (snes_r_bank1(machine, 0x00ffd5) == 0x23) |
| 691 | | cart_type = SA1; |
| 692 | | break; |
| 693 | | |
| 694 | 640 | case 0x32: // needed by a Sample game (according to ZSNES) |
| 641 | case 0x34: |
| 695 | 642 | case 0x35: |
| 696 | 643 | if (snes_r_bank1(machine, 0x00ffd5) == 0x23) |
| 697 | | cart_type = SA1_SRAM; |
| 644 | has_addon_chip = HAS_SA1; |
| 698 | 645 | break; |
| 699 | 646 | |
| 700 | 647 | case 0x43: |
| 701 | | if (snes_r_bank1(machine, 0x00ffd5) == 0x32) |
| 702 | | cart_type = SDD1; |
| 703 | | break; |
| 704 | | |
| 705 | 648 | case 0x45: |
| 706 | 649 | if (snes_r_bank1(machine, 0x00ffd5) == 0x32) |
| 707 | | cart_type = SDD1_SRAM; |
| 650 | has_addon_chip = HAS_SDD1; |
| 708 | 651 | break; |
| 709 | 652 | |
| 710 | 653 | case 0x55: |
| 711 | 654 | if (snes_r_bank1(machine, 0x00ffd5) == 0x35) |
| 712 | | cart_type = RTC; |
| 655 | has_addon_chip = HAS_RTC; |
| 713 | 656 | break; |
| 714 | 657 | |
| 715 | 658 | case 0xe3: |
| 716 | | cart_type = SGB; |
| 659 | has_addon_chip = HAS_Z80GB; |
| 717 | 660 | break; |
| 718 | 661 | |
| 719 | 662 | case 0xf3: |
| 720 | | cart_type = C4; |
| 663 | has_addon_chip = HAS_C4; |
| 721 | 664 | break; |
| 722 | 665 | |
| 723 | 666 | case 0xf5: |
| 724 | 667 | if (snes_r_bank1(machine, 0x00ffd5) == 0x30) |
| 725 | | cart_type = ST018; |
| 668 | has_addon_chip = HAS_ST018; |
| 726 | 669 | else if (snes_r_bank1(machine, 0x00ffd5) == 0x3a) |
| 727 | | cart_type = SPC7110; |
| 670 | has_addon_chip = HAS_SPC7110; |
| 728 | 671 | break; |
| 729 | 672 | |
| 730 | 673 | case 0xf6: |
| 731 | 674 | /* These Seta ST-01X chips have both 0x30 at 0x00ffd5, |
| 732 | 675 | they only differ for the 'size' at 0x00ffd7 */ |
| 733 | 676 | if (snes_r_bank1(machine, 0x00ffd7) < 0x0a) |
| 734 | | cart_type = ST011; |
| 677 | has_addon_chip = HAS_ST011; |
| 735 | 678 | else |
| 736 | | cart_type = ST010; |
| 679 | has_addon_chip = HAS_ST010; |
| 737 | 680 | break; |
| 738 | 681 | |
| 739 | 682 | case 0xf9: |
| 740 | 683 | if (snes_r_bank1(machine, 0x00ffd5) == 0x3a) |
| 741 | | cart_type = SPC7110_RTC; |
| 684 | has_addon_chip = HAS_SPC7110_RTC; |
| 742 | 685 | break; |
| 743 | 686 | |
| 744 | 687 | default: |
| 745 | | cart_type = UNKNOWN; |
| 688 | has_addon_chip = HAS_UNK; |
| 746 | 689 | break; |
| 747 | 690 | } |
| 748 | 691 | |
| 749 | 692 | /* Log snes_cart information */ |
| 750 | 693 | { |
| 751 | 694 | char title[21], rom_id[4], company_id[2]; |
| 752 | | int company; |
| 695 | int company, has_ram = 0, has_sram = 0; |
| 753 | 696 | |
| 754 | 697 | /* Company */ |
| 755 | 698 | for( i = 0; i < 2; i++ ) |
| r3405 | r3406 | |
| 766 | 709 | for( i = 0; i < 21; i++ ) |
| 767 | 710 | title[i] = snes_r_bank1(machine, 0x00ffc0 + i); |
| 768 | 711 | |
| 712 | /* RAM */ |
| 713 | if (((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 1) || |
| 714 | ((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 2) || |
| 715 | ((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 4) || |
| 716 | ((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 5)) |
| 717 | has_ram = 1; |
| 718 | |
| 719 | /* SRAM */ |
| 720 | if (((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 2) || |
| 721 | ((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 5) || |
| 722 | ((snes_r_bank1(machine, 0x00ffd6) & 0xf) == 6)) |
| 723 | has_sram = 1; |
| 724 | |
| 769 | 725 | logerror( "ROM DETAILS\n" ); |
| 770 | 726 | logerror( "===========\n\n" ); |
| 771 | 727 | logerror( "\tTotal blocks: %d (%dmb)\n", total_blocks, total_blocks / (snes_cart.mode & 5 ? 32 : 16) ); |
| r3405 | r3406 | |
| 780 | 736 | logerror( "\tName: %.21s\n", title ); |
| 781 | 737 | logerror( "\tSpeed: %s [%d]\n", ((snes_r_bank1(machine, 0x00ffd5) & 0xf0)) ? "FastROM" : "SlowROM", (snes_r_bank1(machine, 0x00ffd5) & 0xf0) >> 4 ); |
| 782 | 738 | logerror( "\tBank size: %s [%d]\n", (snes_r_bank1(machine, 0x00ffd5) & 0xf) ? "HiROM" : "LoROM", snes_r_bank1(machine, 0x00ffd5) & 0xf ); |
| 783 | | logerror( "\tType: %s [%d]\n", types[cart_type], snes_r_bank1(machine, 0x00ffd6) ); |
| 739 | |
| 740 | logerror( "\tType: %s", types[has_addon_chip]); |
| 741 | if (has_ram) |
| 742 | logerror( ", RAM"); |
| 743 | if (has_sram) |
| 744 | logerror( ", SRAM"); |
| 745 | logerror( " [%d]\n", snes_r_bank1(machine, 0x00ffd6) ); |
| 746 | |
| 784 | 747 | logerror( "\tSize: %d megabits [%d]\n", 1 << (snes_r_bank1(machine, 0x00ffd7) - 7), snes_r_bank1(machine, 0x00ffd7) ); |
| 785 | 748 | logerror( "\tSRAM: %d kilobits [%d]\n", snes_cart.sram * 8, snes_ram[0xffd8] ); |
| 786 | 749 | logerror( "\tCountry: %s [%d]\n", countries[snes_r_bank1(machine, 0x00ffd9)], snes_r_bank1(machine, 0x00ffd9) ); |
| r3405 | r3406 | |
| 792 | 755 | logerror( "\tStart Address: %2X%2Xh\n\n", snes_r_bank1(machine, 0x00fffd), snes_r_bank1(machine, 0x00fffc) ); |
| 793 | 756 | |
| 794 | 757 | if (!supported_type) |
| 795 | | logerror("WARNING: This cart type \"%s\" is not supported yet!\n", types[cart_type]); |
| 758 | logerror("WARNING: This cart type \"%s\" is not supported yet!\n", types[has_addon_chip]); |
| 796 | 759 | } |
| 797 | 760 | |
| 798 | 761 | /* Load SRAM */ |