Previous 15994 Revisions Next

r8145 Sunday 23rd May, 2010 at 05:16:22 UTC by Fabio Priuli
[SNES] Simplified cart loading code. This also fixes any possible crash experienced in 0.138 when loading from software lists.
[src/mess/machine]snescart.c

src/mess/machine/snescart.c
r8144r8145
706706   const address_space *space = cputag_get_address_space( machine, "maincpu", ADDRESS_SPACE_PROGRAM );
707707   int total_blocks, read_blocks, has_bsx_slot = 0, st_bios = 0;
708708   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");
711710
712711   if (image_software_entry(image) == NULL)
713712      state->cart_size = image_length(image);
r8144r8145
718717   offset = snes_skip_header(image, state->cart_size);
719718   
720719   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)
725720   {
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);
728723   }
729724   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);
733728
734729   /* 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);
736731
737732   /* Then, detect BS-X carts */
738733   // 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)
741736   {
742      UINT8 n15 = temp_buffer[int_header_offs + 0x15];
737      UINT8 n15 = ROM[int_header_offs + 0x15];
743738      if (n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c || n15 == 0xbc || n15 == 0xfc)
744739      {
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)
746741         {
747742            // BS-X Flash Cart
748743            state->cart[0].mode = SNES_MODE_BSX;
r8144r8145
751746   }
752747   
753748   // 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'))
755750   {
756      UINT8 n13 = temp_buffer[int_header_offs - 13];
751      UINT8 n13 = ROM[int_header_offs - 13];
757752      if ((n13 >= 'A' && n13 <= 'Z') || (n13 >= '0' && n13 <= '9'))
758753      {
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))
761756         {
762757            has_bsx_slot = 1;
763758         }
r8144r8145
767762   // If there is a BS-X connector, detect if it is the Base Cart or a compatible slotted cart
768763   if (has_bsx_slot)
769764   {
770      if (!memcmp(temp_buffer + int_header_offs, "Satellaview BS-X     ", 21))
765      if (!memcmp(ROM + int_header_offs, "Satellaview BS-X     ", 21))
771766      {
772767         //BS-X Base Cart
773768         state->cart[0].mode = SNES_MODE_BSX;
r8144r8145
781776   }
782777
783778   /* Then, detect Sufami Turbo carts */
784   if (!memcmp(temp_buffer, "BANDAI SFC-ADX", 14))
779   if (!memcmp(ROM, "BANDAI SFC-ADX", 14))
785780   {
786781      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))
788783         st_bios = 1;
789784   }
790785   
791786   if (SNES_CART_DEBUG) printf("mode %d\n", state->cart[0].mode);
792787   
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
803788   /* FIXME: Insert crc check here? */
804789
805790   /* How many blocks of data are available to be loaded? */
r8144r8145
827812         * range 0x8000 - 0xffff) of each bank is also mirrored in banks 0x00 to 0x3f and 0x80 to 0xbf.
828813         */
829814         /* 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))
831816         {
832817            while (read_blocks < 16 && read_blocks < total_blocks)
833818            {
r8144r8145
999984         * 0x7f are overwritten by WRAM). Each block is also mirrored in banks 0x80 to 0xff (up to 0xff for real)
1000985         */
1001986         /* 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))
1003988         {
1004989            while (read_blocks < 64 && read_blocks < total_blocks)
1005990            {
r8144r8145
10861071   int total_blocks, read_blocks;
10871072   int st_bios = 0, slot_id = 0;
10881073   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());
10911075
10921076   snes_ram = memory_region(machine, "maincpu");
10931077
r8144r8145
11121096   offset = snes_skip_header(image, state->cart_size);
11131097   
11141098   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)
11191099   {
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);
11221102   }
11231103   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);
11271105   
1106   if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset);
1107   
11281108   /* Detect Sufami Turbo carts */
1129   if (!memcmp(temp_buffer, "BANDAI SFC-ADX", 14))
1109   if (!memcmp(ROM, "BANDAI SFC-ADX", 14))
11301110   {
11311111      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))
11331113         st_bios = 1;
11341114   }
11351115   else
r8144r8145
11461126      return INIT_FAIL;
11471127   }
11481128
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   
11601129   /* FIXME: Insert crc check here? */
11611130   
11621131   /* How many blocks of data are available to be loaded? */
r8144r8145
11951164
11961165   state->cart[slot_id].slot_in_use = 1;   // aknowledge the cart in this slot, for saving sram at exit
11971166
1198   auto_free(image->machine, temp_buffer);
1167   auto_free(image->machine, ROM);
11991168   
12001169   return INIT_PASS;
12011170}
r8144r8145
12071176   int total_blocks, read_blocks;
12081177   int has_bsx_slot = 0;
12091178   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");
12121180   
12131181   if (image_software_entry(image) == NULL)
12141182      state->cart_size = image_length(image);
r8144r8145
12191187   offset = snes_skip_header(image, state->cart_size);
12201188   
12211189   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)
12261190   {
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);
12291193   }
12301194   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);
12341196   
1197   if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset);
1198   
12351199   /* 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);
12371201   
12381202   // 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'))
12401204   {
1241      UINT8 n13 = temp_buffer[int_header_offs - 13];
1205      UINT8 n13 = ROM[int_header_offs - 13];
12421206      if ((n13 >= 'A' && n13 <= 'Z') || (n13 >= '0' && n13 <= '9'))
12431207      {
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))
12461210         {
12471211            has_bsx_slot = 1;
12481212         }
r8144r8145
12521216   // If there is a BS-X connector, detect if it is the Base Cart or a compatible slotted cart
12531217   if (has_bsx_slot)
12541218   {
1255      if (!memcmp(temp_buffer + int_header_offs, "Satellaview BS-X     ", 21))
1219      if (!memcmp(ROM + int_header_offs, "Satellaview BS-X     ", 21))
12561220      {
12571221         //BS-X Base Cart
12581222         state->cart[0].mode = SNES_MODE_BSX;
r8144r8145
12711235      return INIT_FAIL;
12721236   }
12731237   
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
12851239   /* FIXME: Insert crc check here? */
12861240   
12871241   /* How many blocks of data are available to be loaded? */
12881242   total_blocks = (state->cart_size - offset) / 0x8000;
12891243   read_blocks = 0;
12901244   
1291//   if (SNES_CART_DEBUG)
1292      printf("blocks %d\n", total_blocks);
1245   if (SNES_CART_DEBUG) printf("blocks %d\n", total_blocks);
12931246   
12941247   // actually load the cart
12951248   while (read_blocks < 64 && read_blocks < total_blocks)
r8144r8145
13231276   running_machine *machine = image->machine;
13241277   snes_state *state = (snes_state *)machine->driver_data;
13251278   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");
13281280   
13291281   if (image_software_entry(image) == NULL)
13301282      state->cart_size = image_length(image);
r8144r8145
13351287   offset = snes_skip_header(image, state->cart_size);
13361288   
13371289   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)
13421290   {
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);
13451293   }
13461294   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);
13501296   
1297   if (SNES_CART_DEBUG) printf("size %08X\n", state->cart_size - offset);
1298   
13511299   /* 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);
13531301   
13541302   // 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)
13571305   {
1358      UINT8 n15 = temp_buffer[int_header_offs + 0x15];
1306      UINT8 n15 = ROM[int_header_offs + 0x15];
13591307      if (n15 == 0x00 || n15 == 0x80 || n15 == 0x84 || n15 == 0x9c || n15 == 0xbc || n15 == 0xfc)
13601308      {
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)
13621310         {
13631311            // BS-X Flash Cart
13641312            state->cart[1].mode = SNES_MODE_BSX;
r8144r8145
13721320      printf("This image cannot be loaded in the second cartslot of snesbsx.\n");
13731321      return INIT_FAIL;
13741322   }
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     
13841324   // actually load the cart
13851325   return INIT_PASS;
13861326}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team