src/mess/machine/snescart.c
| r8144 | r8145 | |
| 706 | 706 | const address_space *space = cputag_get_address_space( machine, "maincpu", ADDRESS_SPACE_PROGRAM ); |
| 707 | 707 | int total_blocks, read_blocks, has_bsx_slot = 0, st_bios = 0; |
| 708 | 708 | UINT32 offset, int_header_offs; |
| 709 | | UINT8 *temp_buffer = auto_alloc_array(machine, UINT8, 0x410000); |
| 710 | | UINT8 *ROM; |
| 709 | UINT8 *ROM = memory_region(image->machine, "cart"); |
| 711 | 710 | |
| 712 | 711 | if (image_software_entry(image) == NULL) |
| 713 | 712 | state->cart_size = image_length(image); |
| r8144 | r8145 | |
| 718 | 717 | offset = snes_skip_header(image, state->cart_size); |
| 719 | 718 | |
| 720 | 719 | if (image_software_entry(image) == NULL) |
| 721 | | image_fseek(image, offset, SEEK_SET); |
| 722 | | |
| 723 | | /* We need to take a sample to test what mode we need to be in (the sample has to be quite large to cope with large carts in ExHiRom) */ |
| 724 | | if (image_software_entry(image) == NULL) |
| 725 | 720 | { |
| 726 | | image_fread(image, temp_buffer, 0x40ffff); |
| 727 | | image_fseek(image, offset, SEEK_SET); /* Rewind */ |
| 721 | image_fseek(image, offset, SEEK_SET); |
| 722 | image_fread(image, ROM, state->cart_size - offset); |
| 728 | 723 | } |
| 729 | 724 | else |
| 730 | | { |
| 731 | | memcpy(temp_buffer, image_get_software_region(image, "rom") + offset, 0x40ffff); |
| 732 | | } |
| 725 | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 726 | |
| 727 | if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset); |
| 733 | 728 | |
| 734 | 729 | /* First, look if the cart is HiROM or LoROM (and set snes_cart accordingly) */ |
| 735 | | int_header_offs = snes_find_hilo_mode(image, temp_buffer, offset, 0); |
| 730 | int_header_offs = snes_find_hilo_mode(image, ROM, offset, 0); |
| 736 | 731 | |
| 737 | 732 | /* Then, detect BS-X carts */ |
| 738 | 733 | // Detect presence of BS-X Flash Cart |
| 739 | | if ((temp_buffer[int_header_offs + 0x13] == 0x00 || temp_buffer[int_header_offs + 0x13] == 0xff) && |
| 740 | | temp_buffer[int_header_offs + 0x14] == 0x00) |
| 734 | if ((ROM[int_header_offs + 0x13] == 0x00 || ROM[int_header_offs + 0x13] == 0xff) && |
| 735 | ROM[int_header_offs + 0x14] == 0x00) |
| 741 | 736 | { |
| 742 | | UINT8 n15 = temp_buffer[int_header_offs + 0x15]; |
| 737 | UINT8 n15 = ROM[int_header_offs + 0x15]; |
| 743 | 738 | if (n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c || n15 == 0xbc || n15 == 0xfc) |
| 744 | 739 | { |
| 745 | | if (temp_buffer[int_header_offs + 0x1a] == 0x33 || temp_buffer[int_header_offs + 0x1a] == 0xff) |
| 740 | if (ROM[int_header_offs + 0x1a] == 0x33 || ROM[int_header_offs + 0x1a] == 0xff) |
| 746 | 741 | { |
| 747 | 742 | // BS-X Flash Cart |
| 748 | 743 | state->cart[0].mode = SNES_MODE_BSX; |
| r8144 | r8145 | |
| 751 | 746 | } |
| 752 | 747 | |
| 753 | 748 | // Detect presence of BS-X flash cartridge connector |
| 754 | | if ((temp_buffer[int_header_offs - 14] == 'Z') && (temp_buffer[int_header_offs - 11] == 'J')) |
| 749 | if ((ROM[int_header_offs - 14] == 'Z') && (ROM[int_header_offs - 11] == 'J')) |
| 755 | 750 | { |
| 756 | | UINT8 n13 = temp_buffer[int_header_offs - 13]; |
| 751 | UINT8 n13 = ROM[int_header_offs - 13]; |
| 757 | 752 | if ((n13 >= 'A' && n13 <= 'Z') || (n13 >= '0' && n13 <= '9')) |
| 758 | 753 | { |
| 759 | | if (temp_buffer[int_header_offs + 0x1a] == 0x33 || |
| 760 | | (temp_buffer[int_header_offs - 10] == 0x00 && temp_buffer[int_header_offs - 4] == 0x00)) |
| 754 | if (ROM[int_header_offs + 0x1a] == 0x33 || |
| 755 | (ROM[int_header_offs - 10] == 0x00 && ROM[int_header_offs - 4] == 0x00)) |
| 761 | 756 | { |
| 762 | 757 | has_bsx_slot = 1; |
| 763 | 758 | } |
| r8144 | r8145 | |
| 767 | 762 | // If there is a BS-X connector, detect if it is the Base Cart or a compatible slotted cart |
| 768 | 763 | if (has_bsx_slot) |
| 769 | 764 | { |
| 770 | | if (!memcmp(temp_buffer + int_header_offs, "Satellaview BS-X ", 21)) |
| 765 | if (!memcmp(ROM + int_header_offs, "Satellaview BS-X ", 21)) |
| 771 | 766 | { |
| 772 | 767 | //BS-X Base Cart |
| 773 | 768 | state->cart[0].mode = SNES_MODE_BSX; |
| r8144 | r8145 | |
| 781 | 776 | } |
| 782 | 777 | |
| 783 | 778 | /* Then, detect Sufami Turbo carts */ |
| 784 | | if (!memcmp(temp_buffer, "BANDAI SFC-ADX", 14)) |
| 779 | if (!memcmp(ROM, "BANDAI SFC-ADX", 14)) |
| 785 | 780 | { |
| 786 | 781 | state->cart[0].mode = SNES_MODE_ST; |
| 787 | | if (!memcmp(temp_buffer + 16, "SFC-ADX BACKUP", 14)) |
| 782 | if (!memcmp(ROM + 16, "SFC-ADX BACKUP", 14)) |
| 788 | 783 | st_bios = 1; |
| 789 | 784 | } |
| 790 | 785 | |
| 791 | 786 | if (SNES_CART_DEBUG) printf("mode %d\n", state->cart[0].mode); |
| 792 | 787 | |
| 793 | | /* Finally, load data */ |
| 794 | | ROM = memory_region(image->machine, "cart"); |
| 795 | | |
| 796 | | if (image_software_entry(image) == NULL) |
| 797 | | image_fread(image, ROM, state->cart_size - offset); |
| 798 | | else |
| 799 | | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 800 | | |
| 801 | | if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset); |
| 802 | | |
| 803 | 788 | /* FIXME: Insert crc check here? */ |
| 804 | 789 | |
| 805 | 790 | /* How many blocks of data are available to be loaded? */ |
| r8144 | r8145 | |
| 827 | 812 | * range 0x8000 - 0xffff) of each bank is also mirrored in banks 0x00 to 0x3f and 0x80 to 0xbf. |
| 828 | 813 | */ |
| 829 | 814 | /* SPC7110 games needs this different loading routine */ |
| 830 | | if ((temp_buffer[0x00ffd6] == 0xf9 || temp_buffer[0x00ffd6] == 0xf5) && (temp_buffer[0x00ffd5] == 0x3a)) |
| 815 | if ((ROM[0x00ffd6] == 0xf9 || ROM[0x00ffd6] == 0xf5) && (ROM[0x00ffd5] == 0x3a)) |
| 831 | 816 | { |
| 832 | 817 | while (read_blocks < 16 && read_blocks < total_blocks) |
| 833 | 818 | { |
| r8144 | r8145 | |
| 999 | 984 | * 0x7f are overwritten by WRAM). Each block is also mirrored in banks 0x80 to 0xff (up to 0xff for real) |
| 1000 | 985 | */ |
| 1001 | 986 | /* SuperFX games needs this different loading routine */ |
| 1002 | | if (((temp_buffer[0x007fd6] >= 0x13 && temp_buffer[0x007fd6] <= 0x15) || temp_buffer[0x007fd6] == 0x1a) && (temp_buffer[0x007fd5] == 0x20)) |
| 987 | if (((ROM[0x007fd6] >= 0x13 && ROM[0x007fd6] <= 0x15) || ROM[0x007fd6] == 0x1a) && (ROM[0x007fd5] == 0x20)) |
| 1003 | 988 | { |
| 1004 | 989 | while (read_blocks < 64 && read_blocks < total_blocks) |
| 1005 | 990 | { |
| r8144 | r8145 | |
| 1086 | 1071 | int total_blocks, read_blocks; |
| 1087 | 1072 | int st_bios = 0, slot_id = 0; |
| 1088 | 1073 | UINT32 offset, st_data_offset = 0; |
| 1089 | | UINT8 *temp_buffer = auto_alloc_array(machine, UINT8, 0x410000); |
| 1090 | | UINT8 *ROM; |
| 1074 | UINT8 *ROM = memory_region(image->machine, image->tag()); |
| 1091 | 1075 | |
| 1092 | 1076 | snes_ram = memory_region(machine, "maincpu"); |
| 1093 | 1077 | |
| r8144 | r8145 | |
| 1112 | 1096 | offset = snes_skip_header(image, state->cart_size); |
| 1113 | 1097 | |
| 1114 | 1098 | if (image_software_entry(image) == NULL) |
| 1115 | | image_fseek(image, offset, SEEK_SET); |
| 1116 | | |
| 1117 | | /* We need to take a sample to test what mode we need to be in (the sample has to be quite large to cope with large carts in ExHiRom) */ |
| 1118 | | if (image_software_entry(image) == NULL) |
| 1119 | 1099 | { |
| 1120 | | image_fread(image, temp_buffer, 0x40ffff); |
| 1121 | | image_fseek(image, offset, SEEK_SET); /* Rewind */ |
| 1100 | image_fseek(image, offset, SEEK_SET); |
| 1101 | image_fread(image, ROM, state->cart_size - offset); |
| 1122 | 1102 | } |
| 1123 | 1103 | else |
| 1124 | | { |
| 1125 | | memcpy(temp_buffer, image_get_software_region(image, "rom") + offset, 0x40ffff); |
| 1126 | | } |
| 1104 | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1127 | 1105 | |
| 1106 | if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset); |
| 1107 | |
| 1128 | 1108 | /* Detect Sufami Turbo carts */ |
| 1129 | | if (!memcmp(temp_buffer, "BANDAI SFC-ADX", 14)) |
| 1109 | if (!memcmp(ROM, "BANDAI SFC-ADX", 14)) |
| 1130 | 1110 | { |
| 1131 | 1111 | state->cart[slot_id].mode = SNES_MODE_ST; |
| 1132 | | if (!memcmp(temp_buffer + 16, "SFC-ADX BACKUP", 14)) |
| 1112 | if (!memcmp(ROM + 16, "SFC-ADX BACKUP", 14)) |
| 1133 | 1113 | st_bios = 1; |
| 1134 | 1114 | } |
| 1135 | 1115 | else |
| r8144 | r8145 | |
| 1146 | 1126 | return INIT_FAIL; |
| 1147 | 1127 | } |
| 1148 | 1128 | |
| 1149 | | /* Loading data */ |
| 1150 | | ROM = memory_region(image->machine, image->tag()); |
| 1151 | | |
| 1152 | | if (image_software_entry(image) == NULL) |
| 1153 | | image_fread(image, ROM, state->cart_size - offset); |
| 1154 | | else |
| 1155 | | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1156 | | |
| 1157 | | if (SNES_CART_DEBUG) |
| 1158 | | printf("size %08X\n", state->cart_size - offset); |
| 1159 | | |
| 1160 | 1129 | /* FIXME: Insert crc check here? */ |
| 1161 | 1130 | |
| 1162 | 1131 | /* How many blocks of data are available to be loaded? */ |
| r8144 | r8145 | |
| 1195 | 1164 | |
| 1196 | 1165 | state->cart[slot_id].slot_in_use = 1; // aknowledge the cart in this slot, for saving sram at exit |
| 1197 | 1166 | |
| 1198 | | auto_free(image->machine, temp_buffer); |
| 1167 | auto_free(image->machine, ROM); |
| 1199 | 1168 | |
| 1200 | 1169 | return INIT_PASS; |
| 1201 | 1170 | } |
| r8144 | r8145 | |
| 1207 | 1176 | int total_blocks, read_blocks; |
| 1208 | 1177 | int has_bsx_slot = 0; |
| 1209 | 1178 | UINT32 offset, int_header_offs; |
| 1210 | | UINT8 *temp_buffer = auto_alloc_array(machine, UINT8, 0x410000); |
| 1211 | | UINT8 *ROM; |
| 1179 | UINT8 *ROM = memory_region(image->machine, "cart"); |
| 1212 | 1180 | |
| 1213 | 1181 | if (image_software_entry(image) == NULL) |
| 1214 | 1182 | state->cart_size = image_length(image); |
| r8144 | r8145 | |
| 1219 | 1187 | offset = snes_skip_header(image, state->cart_size); |
| 1220 | 1188 | |
| 1221 | 1189 | if (image_software_entry(image) == NULL) |
| 1222 | | image_fseek(image, offset, SEEK_SET); |
| 1223 | | |
| 1224 | | /* We need to take a sample to test what mode we need to be in (the sample has to be quite large to cope with large carts in ExHiRom) */ |
| 1225 | | if (image_software_entry(image) == NULL) |
| 1226 | 1190 | { |
| 1227 | | image_fread(image, temp_buffer, 0x40ffff); |
| 1228 | | image_fseek(image, offset, SEEK_SET); /* Rewind */ |
| 1191 | image_fseek(image, offset, SEEK_SET); |
| 1192 | image_fread(image, ROM, state->cart_size - offset); |
| 1229 | 1193 | } |
| 1230 | 1194 | else |
| 1231 | | { |
| 1232 | | memcpy(temp_buffer, image_get_software_region(image, "rom") + offset, 0x40ffff); |
| 1233 | | } |
| 1195 | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1234 | 1196 | |
| 1197 | if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset); |
| 1198 | |
| 1235 | 1199 | /* First, look if the cart is HiROM or LoROM (and set snes_cart accordingly) */ |
| 1236 | | int_header_offs = snes_find_hilo_mode(image, temp_buffer, offset, 0); |
| 1200 | int_header_offs = snes_find_hilo_mode(image, ROM, offset, 0); |
| 1237 | 1201 | |
| 1238 | 1202 | // Detect presence of BS-X flash cartridge connector |
| 1239 | | if ((temp_buffer[int_header_offs - 14] == 'Z') && (temp_buffer[int_header_offs - 11] == 'J')) |
| 1203 | if ((ROM[int_header_offs - 14] == 'Z') && (ROM[int_header_offs - 11] == 'J')) |
| 1240 | 1204 | { |
| 1241 | | UINT8 n13 = temp_buffer[int_header_offs - 13]; |
| 1205 | UINT8 n13 = ROM[int_header_offs - 13]; |
| 1242 | 1206 | if ((n13 >= 'A' && n13 <= 'Z') || (n13 >= '0' && n13 <= '9')) |
| 1243 | 1207 | { |
| 1244 | | if (temp_buffer[int_header_offs + 0x1a] == 0x33 || |
| 1245 | | (temp_buffer[int_header_offs - 10] == 0x00 && temp_buffer[int_header_offs - 4] == 0x00)) |
| 1208 | if (ROM[int_header_offs + 0x1a] == 0x33 || |
| 1209 | (ROM[int_header_offs - 10] == 0x00 && ROM[int_header_offs - 4] == 0x00)) |
| 1246 | 1210 | { |
| 1247 | 1211 | has_bsx_slot = 1; |
| 1248 | 1212 | } |
| r8144 | r8145 | |
| 1252 | 1216 | // If there is a BS-X connector, detect if it is the Base Cart or a compatible slotted cart |
| 1253 | 1217 | if (has_bsx_slot) |
| 1254 | 1218 | { |
| 1255 | | if (!memcmp(temp_buffer + int_header_offs, "Satellaview BS-X ", 21)) |
| 1219 | if (!memcmp(ROM + int_header_offs, "Satellaview BS-X ", 21)) |
| 1256 | 1220 | { |
| 1257 | 1221 | //BS-X Base Cart |
| 1258 | 1222 | state->cart[0].mode = SNES_MODE_BSX; |
| r8144 | r8145 | |
| 1271 | 1235 | return INIT_FAIL; |
| 1272 | 1236 | } |
| 1273 | 1237 | |
| 1274 | | /* Loading data */ |
| 1275 | | ROM = memory_region(image->machine, "cart"); |
| 1276 | | |
| 1277 | | if (image_software_entry(image) == NULL) |
| 1278 | | image_fread(image, ROM, state->cart_size - offset); |
| 1279 | | else |
| 1280 | | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1281 | | |
| 1282 | | // if (SNES_CART_DEBUG) |
| 1283 | | printf("size %08X\n", state->cart_size - offset); |
| 1284 | | |
| 1238 | |
| 1285 | 1239 | /* FIXME: Insert crc check here? */ |
| 1286 | 1240 | |
| 1287 | 1241 | /* How many blocks of data are available to be loaded? */ |
| 1288 | 1242 | total_blocks = (state->cart_size - offset) / 0x8000; |
| 1289 | 1243 | read_blocks = 0; |
| 1290 | 1244 | |
| 1291 | | // if (SNES_CART_DEBUG) |
| 1292 | | printf("blocks %d\n", total_blocks); |
| 1245 | if (SNES_CART_DEBUG) printf("blocks %d\n", total_blocks); |
| 1293 | 1246 | |
| 1294 | 1247 | // actually load the cart |
| 1295 | 1248 | while (read_blocks < 64 && read_blocks < total_blocks) |
| r8144 | r8145 | |
| 1323 | 1276 | running_machine *machine = image->machine; |
| 1324 | 1277 | snes_state *state = (snes_state *)machine->driver_data; |
| 1325 | 1278 | UINT32 offset, int_header_offs; |
| 1326 | | UINT8 *temp_buffer = auto_alloc_array(machine, UINT8, 0x410000); |
| 1327 | | UINT8 *ROM; |
| 1279 | UINT8 *ROM = memory_region(image->machine, "flash"); |
| 1328 | 1280 | |
| 1329 | 1281 | if (image_software_entry(image) == NULL) |
| 1330 | 1282 | state->cart_size = image_length(image); |
| r8144 | r8145 | |
| 1335 | 1287 | offset = snes_skip_header(image, state->cart_size); |
| 1336 | 1288 | |
| 1337 | 1289 | if (image_software_entry(image) == NULL) |
| 1338 | | image_fseek(image, offset, SEEK_SET); |
| 1339 | | |
| 1340 | | /* We need to take a sample to test what mode we need to be in (the sample has to be quite large to cope with large carts in ExHiRom) */ |
| 1341 | | if (image_software_entry(image) == NULL) |
| 1342 | 1290 | { |
| 1343 | | image_fread(image, temp_buffer, 0x40ffff); |
| 1344 | | image_fseek(image, offset, SEEK_SET); /* Rewind */ |
| 1291 | image_fseek(image, offset, SEEK_SET); |
| 1292 | image_fread(image, ROM, state->cart_size - offset); |
| 1345 | 1293 | } |
| 1346 | 1294 | else |
| 1347 | | { |
| 1348 | | memcpy(temp_buffer, image_get_software_region(image, "rom") + offset, 0x40ffff); |
| 1349 | | } |
| 1295 | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1350 | 1296 | |
| 1297 | if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset); |
| 1298 | |
| 1351 | 1299 | /* First, look if the cart is HiROM or LoROM (and set snes_cart accordingly) */ |
| 1352 | | int_header_offs = snes_find_hilo_mode(image, temp_buffer, offset, 1); |
| 1300 | int_header_offs = snes_find_hilo_mode(image, ROM, offset, 1); |
| 1353 | 1301 | |
| 1354 | 1302 | // Detect presence of BS-X Flash Cart |
| 1355 | | if ((temp_buffer[int_header_offs + 0x13] == 0x00 || temp_buffer[int_header_offs + 0x13] == 0xff) && |
| 1356 | | temp_buffer[int_header_offs + 0x14] == 0x00) |
| 1303 | if ((ROM[int_header_offs + 0x13] == 0x00 || ROM[int_header_offs + 0x13] == 0xff) && |
| 1304 | ROM[int_header_offs + 0x14] == 0x00) |
| 1357 | 1305 | { |
| 1358 | | UINT8 n15 = temp_buffer[int_header_offs + 0x15]; |
| 1306 | UINT8 n15 = ROM[int_header_offs + 0x15]; |
| 1359 | 1307 | if (n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c || n15 == 0xbc || n15 == 0xfc) |
| 1360 | 1308 | { |
| 1361 | | if (temp_buffer[int_header_offs + 0x1a] == 0x33 || temp_buffer[int_header_offs + 0x1a] == 0xff) |
| 1309 | if (ROM[int_header_offs + 0x1a] == 0x33 || ROM[int_header_offs + 0x1a] == 0xff) |
| 1362 | 1310 | { |
| 1363 | 1311 | // BS-X Flash Cart |
| 1364 | 1312 | state->cart[1].mode = SNES_MODE_BSX; |
| r8144 | r8145 | |
| 1372 | 1320 | printf("This image cannot be loaded in the second cartslot of snesbsx.\n"); |
| 1373 | 1321 | return INIT_FAIL; |
| 1374 | 1322 | } |
| 1375 | | |
| 1376 | | /* Loading data */ |
| 1377 | | ROM = memory_region(image->machine, "flash"); |
| 1378 | | |
| 1379 | | if (image_software_entry(image) == NULL) |
| 1380 | | image_fread(image, ROM, state->cart_size - offset); |
| 1381 | | else |
| 1382 | | memcpy(ROM, image_get_software_region(image, "rom") + offset, state->cart_size - offset); |
| 1383 | | |
| 1323 | |
| 1384 | 1324 | // actually load the cart |
| 1385 | 1325 | return INIT_PASS; |
| 1386 | 1326 | } |