src/emu/imagedev/multcart.c
| r13375 | r13376 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | multcart.c |
| 4 | | |
| 5 | | Multi-cartridge handling code |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include "emu.h" |
| 10 | | #include "multcart.h" |
| 11 | | #include "pool.h" |
| 12 | | #include "unzip.h" |
| 13 | | #include "corestr.h" |
| 14 | | #include "xmlfile.h" |
| 15 | | #include "hash.h" |
| 16 | | #include "machine/ram.h" |
| 17 | | |
| 18 | | /*************************************************************************** |
| 19 | | TYPE DEFINITIONS |
| 20 | | ***************************************************************************/ |
| 21 | | |
| 22 | | struct _multicart_private |
| 23 | | { |
| 24 | | object_pool * pool; |
| 25 | | zip_file * zip; |
| 26 | | }; |
| 27 | | |
| 28 | | |
| 29 | | typedef struct _multicart_load_state multicart_load_state; |
| 30 | | struct _multicart_load_state |
| 31 | | { |
| 32 | | multicart_t * multicart; |
| 33 | | zip_file * zip; |
| 34 | | xml_data_node * layout_xml; |
| 35 | | xml_data_node * resources_node; |
| 36 | | xml_data_node * pcb_node; |
| 37 | | multicart_resource * resources; |
| 38 | | multicart_socket * sockets; |
| 39 | | }; |
| 40 | | |
| 41 | | static const char error_text[14][30] = |
| 42 | | { |
| 43 | | "no error", |
| 44 | | "not a multicart", |
| 45 | | "module definition corrupt", |
| 46 | | "out of memory", |
| 47 | | "xml format error", |
| 48 | | "invalid file reference", |
| 49 | | "zip file error", |
| 50 | | "missing ram length", |
| 51 | | "invalid ram specification", |
| 52 | | "unknown resource type", |
| 53 | | "invalid resource reference", |
| 54 | | "invalid file format", |
| 55 | | "missing layout", |
| 56 | | "no pcb or resource found" |
| 57 | | }; |
| 58 | | |
| 59 | | const char *multicart_error_text(multicart_open_error error) |
| 60 | | { |
| 61 | | return error_text[(int)error]; |
| 62 | | } |
| 63 | | |
| 64 | | /*************************************************************************** |
| 65 | | INLINE FUNCTIONS |
| 66 | | ***************************************************************************/ |
| 67 | | |
| 68 | | INLINE multicartslot_t *get_token(device_t *device) |
| 69 | | { |
| 70 | | assert(device != NULL); |
| 71 | | assert(device->type() == MULTICARTSLOT); |
| 72 | | return (multicartslot_t *) downcast<legacy_device_base *>(device)->token(); |
| 73 | | } |
| 74 | | |
| 75 | | |
| 76 | | INLINE const multicartslot_config *get_config(const device_t *device) |
| 77 | | { |
| 78 | | assert(device != NULL); |
| 79 | | assert(device->type() == MULTICARTSLOT); |
| 80 | | return (const multicartslot_config *) downcast<const legacy_device_base *>(device)->inline_config(); |
| 81 | | } |
| 82 | | |
| 83 | | |
| 84 | | /*************************************************************************** |
| 85 | | IMPLEMENTATION |
| 86 | | ***************************************************************************/ |
| 87 | | |
| 88 | | /*------------------------------------------------- |
| 89 | | cartslot_get_pcb |
| 90 | | -------------------------------------------------*/ |
| 91 | | |
| 92 | | device_t *cartslot_get_pcb(device_t *device) |
| 93 | | { |
| 94 | | multicartslot_t *cart = get_token(device); |
| 95 | | return cart->pcb_device; |
| 96 | | } |
| 97 | | |
| 98 | | |
| 99 | | /*------------------------------------------------- |
| 100 | | cartslot_get_socket |
| 101 | | -------------------------------------------------*/ |
| 102 | | |
| 103 | | void *cartslot_get_socket(device_t *device, const char *socket_name) |
| 104 | | { |
| 105 | | multicartslot_t *cart = get_token(device); |
| 106 | | device_image_interface *image = dynamic_cast<device_image_interface *>(device); |
| 107 | | void *result = NULL; |
| 108 | | |
| 109 | | if (cart->mc != NULL) |
| 110 | | { |
| 111 | | const multicart_socket *socket; |
| 112 | | for (socket = cart->mc->sockets; socket != NULL; socket = socket->next) |
| 113 | | { |
| 114 | | if (!strcmp(socket->id, socket_name)) |
| 115 | | break; |
| 116 | | } |
| 117 | | result = socket ? socket->ptr : NULL; |
| 118 | | } |
| 119 | | else if (socket_name[0] == '\0') |
| 120 | | { |
| 121 | | result = image->ptr(); |
| 122 | | } |
| 123 | | return result; |
| 124 | | } |
| 125 | | |
| 126 | | /*------------------------------------------------- |
| 127 | | cartslot_get_resource_length |
| 128 | | -------------------------------------------------*/ |
| 129 | | |
| 130 | | int cartslot_get_resource_length(device_t *device, const char *socket_name) |
| 131 | | { |
| 132 | | multicartslot_t *cart = get_token(device); |
| 133 | | int result = 0; |
| 134 | | |
| 135 | | if (cart->mc != NULL) |
| 136 | | { |
| 137 | | const multicart_socket *socket; |
| 138 | | |
| 139 | | for (socket = cart->mc->sockets; socket != NULL; socket = socket->next) |
| 140 | | { |
| 141 | | if (!strcmp(socket->id, socket_name)) { |
| 142 | | break; |
| 143 | | } |
| 144 | | } |
| 145 | | if (socket != NULL) |
| 146 | | result = socket->resource->length; |
| 147 | | } |
| 148 | | else |
| 149 | | result = 0; |
| 150 | | |
| 151 | | return result; |
| 152 | | } |
| 153 | | /*------------------------------------------------- |
| 154 | | identify_pcb |
| 155 | | -------------------------------------------------*/ |
| 156 | | |
| 157 | | static const multicartslot_pcb_type *identify_pcb(device_image_interface &image) |
| 158 | | { |
| 159 | | const multicartslot_config *config = get_config(&image.device()); |
| 160 | | astring pcb_name; |
| 161 | | const multicartslot_pcb_type *pcb_type = NULL; |
| 162 | | multicart_t *mc; |
| 163 | | int i; |
| 164 | | |
| 165 | | if (image.exists()) |
| 166 | | { |
| 167 | | /* try opening this as if it were a multicart */ |
| 168 | | multicart_open_error me = multicart_open(image.device().machine().options(), image.filename(), image.device().machine().system().name, MULTICART_FLAGS_DONT_LOAD_RESOURCES, &mc); |
| 169 | | if (me == MCERR_NONE) |
| 170 | | { |
| 171 | | /* this was a multicart - read from it */ |
| 172 | | astring_cpyc(&pcb_name, mc->pcb_type); |
| 173 | | multicart_close(image.device().machine().options(), mc); |
| 174 | | } |
| 175 | | else |
| 176 | | { |
| 177 | | if (me != MCERR_NOT_MULTICART) |
| 178 | | fatalerror("multicart error: %s", multicart_error_text(me)); |
| 179 | | } |
| 180 | | |
| 181 | | /* look for PCB type with matching name */ |
| 182 | | for (i = 0; (i < ARRAY_LENGTH(config->pcb_types)) && (config->pcb_types[i].name != NULL); i++) |
| 183 | | { |
| 184 | | if ((config->pcb_types[i].name[0] == '\0') || !strcmp(astring_c(&pcb_name), config->pcb_types[i].name)) |
| 185 | | { |
| 186 | | pcb_type = &config->pcb_types[i]; |
| 187 | | break; |
| 188 | | } |
| 189 | | } |
| 190 | | |
| 191 | | /* check for unknown PCB type */ |
| 192 | | if ((mc != NULL) && (pcb_type == NULL)) |
| 193 | | fatalerror("Unknown PCB type \"%s\"", astring_c(&pcb_name)); |
| 194 | | } |
| 195 | | else |
| 196 | | { |
| 197 | | /* no device loaded; use the default */ |
| 198 | | pcb_type = (config->pcb_types[0].name != NULL) ? &config->pcb_types[0] : NULL; |
| 199 | | } |
| 200 | | return pcb_type; |
| 201 | | } |
| 202 | | |
| 203 | | /*------------------------------------------------- |
| 204 | | DEVICE_IMAGE_GET_DEVICES(cartslot) |
| 205 | | -------------------------------------------------*/ |
| 206 | | static DEVICE_IMAGE_GET_DEVICES(multicartslot) |
| 207 | | { |
| 208 | | const multicartslot_pcb_type *pcb_type; |
| 209 | | device_t *device = &image.device(); |
| 210 | | |
| 211 | | pcb_type = identify_pcb(image); |
| 212 | | if (pcb_type != NULL) |
| 213 | | { |
| 214 | | image_add_device_with_subdevices(device,pcb_type->devtype,TAG_PCB,0); |
| 215 | | } |
| 216 | | } |
| 217 | | |
| 218 | | |
| 219 | | /*------------------------------------------------- |
| 220 | | find_file |
| 221 | | -------------------------------------------------*/ |
| 222 | | |
| 223 | | static const zip_file_header *find_file(zip_file *zip, const char *filename) |
| 224 | | { |
| 225 | | const zip_file_header *header; |
| 226 | | for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip)) |
| 227 | | { |
| 228 | | if (!core_stricmp(header->filename, filename)) |
| 229 | | return header; |
| 230 | | } |
| 231 | | return NULL; |
| 232 | | } |
| 233 | | |
| 234 | | static const zip_file_header *find_file_crc(zip_file *zip, const char *filename, UINT32 crc) |
| 235 | | { |
| 236 | | const zip_file_header *header; |
| 237 | | for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip)) |
| 238 | | { |
| 239 | | // if the CRC and name both match, we're good |
| 240 | | // if the CRC matches and the name doesn't, we're still good |
| 241 | | if ((!core_stricmp(header->filename, filename)) && (header->crc == crc)) |
| 242 | | { |
| 243 | | return header; |
| 244 | | } |
| 245 | | else if (header->crc == crc) |
| 246 | | { |
| 247 | | return header; |
| 248 | | } |
| 249 | | } |
| 250 | | return NULL; |
| 251 | | } |
| 252 | | |
| 253 | | /*------------------------------------------------- |
| 254 | | find_pcb_and_resource_nodes |
| 255 | | -------------------------------------------------*/ |
| 256 | | |
| 257 | | static int find_pcb_and_resource_nodes(xml_data_node *layout_xml, |
| 258 | | xml_data_node **pcb_node, xml_data_node **resource_node) |
| 259 | | { |
| 260 | | xml_data_node *romset_node; |
| 261 | | xml_data_node *configuration_node; |
| 262 | | |
| 263 | | *pcb_node = NULL; |
| 264 | | *resource_node = NULL; |
| 265 | | |
| 266 | | romset_node = xml_get_sibling(layout_xml->child, "romset"); |
| 267 | | if (romset_node == NULL) |
| 268 | | return FALSE; |
| 269 | | |
| 270 | | configuration_node = xml_get_sibling(romset_node->child, "configuration"); |
| 271 | | if (configuration_node == NULL) |
| 272 | | return FALSE; |
| 273 | | |
| 274 | | *pcb_node = xml_get_sibling(configuration_node->child, "pcb"); |
| 275 | | if (*pcb_node == NULL) |
| 276 | | return FALSE; |
| 277 | | |
| 278 | | *resource_node = xml_get_sibling(romset_node->child, "resources"); |
| 279 | | if (*resource_node == NULL) |
| 280 | | return FALSE; |
| 281 | | |
| 282 | | return TRUE; |
| 283 | | } |
| 284 | | |
| 285 | | |
| 286 | | /*------------------------------------------------- |
| 287 | | get_resource_type |
| 288 | | -------------------------------------------------*/ |
| 289 | | |
| 290 | | static multicart_resource_type get_resource_type(const char *s) |
| 291 | | { |
| 292 | | multicart_resource_type result; |
| 293 | | if (!strcmp(s, "rom")) |
| 294 | | result = MULTICART_RESOURCE_TYPE_ROM; |
| 295 | | else if (!strcmp(s, "ram")) |
| 296 | | result = MULTICART_RESOURCE_TYPE_RAM; |
| 297 | | else |
| 298 | | result = MULTICART_RESOURCE_TYPE_INVALID; |
| 299 | | return result; |
| 300 | | } |
| 301 | | |
| 302 | | |
| 303 | | /*------------------------------------------------- |
| 304 | | load_rom_resource |
| 305 | | -------------------------------------------------*/ |
| 306 | | |
| 307 | | static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node, |
| 308 | | multicart_resource *resource) |
| 309 | | { |
| 310 | | const char *file, *crcstr, *sha1; |
| 311 | | const zip_file_header *header; |
| 312 | | zip_error ziperr; |
| 313 | | UINT32 crc; |
| 314 | | |
| 315 | | /* locate the 'file' attribute */ |
| 316 | | file = xml_get_attribute_string(resource_node, "file", NULL); |
| 317 | | if (file == NULL) |
| 318 | | return MCERR_XML_ERROR; |
| 319 | | |
| 320 | | if (!(crcstr = xml_get_attribute_string(resource_node, "crc", NULL))) |
| 321 | | { |
| 322 | | /* locate the file in the ZIP file */ |
| 323 | | header = find_file(state->zip, file); |
| 324 | | } |
| 325 | | else /* CRC tag is present, use it */ |
| 326 | | { |
| 327 | | crc = strtoul(crcstr, NULL, 16); |
| 328 | | header = find_file_crc(state->zip, file, crc); |
| 329 | | } |
| 330 | | if (header == NULL) |
| 331 | | return MCERR_INVALID_FILE_REF; |
| 332 | | resource->length = header->uncompressed_length; |
| 333 | | |
| 334 | | /* allocate bytes for this resource */ |
| 335 | | resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 336 | | if (resource->ptr == NULL) |
| 337 | | return MCERR_OUT_OF_MEMORY; |
| 338 | | |
| 339 | | /* and decompress it */ |
| 340 | | ziperr = zip_file_decompress(state->zip, resource->ptr, resource->length); |
| 341 | | if (ziperr != ZIPERR_NONE) |
| 342 | | return MCERR_ZIP_ERROR; |
| 343 | | |
| 344 | | /* check SHA1 now */ |
| 345 | | if ((sha1 = xml_get_attribute_string(resource_node, "sha1", NULL))) |
| 346 | | { |
| 347 | | hash_collection actual_hashes; |
| 348 | | actual_hashes.compute((const UINT8 *)resource->ptr, resource->length, hash_collection::HASH_TYPES_CRC_SHA1); |
| 349 | | |
| 350 | | hash_collection expected_hashes; |
| 351 | | expected_hashes.add_from_string(hash_collection::HASH_SHA1, sha1, strlen(sha1)); |
| 352 | | |
| 353 | | if (actual_hashes != expected_hashes) |
| 354 | | { |
| 355 | | return MCERR_INVALID_FILE_REF; |
| 356 | | } |
| 357 | | } |
| 358 | | |
| 359 | | return MCERR_NONE; |
| 360 | | } |
| 361 | | |
| 362 | | |
| 363 | | /*------------------------------------------------- |
| 364 | | load_ram_resource |
| 365 | | -------------------------------------------------*/ |
| 366 | | |
| 367 | | static multicart_open_error load_ram_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, |
| 368 | | multicart_resource *resource) |
| 369 | | { |
| 370 | | const char *length_string; |
| 371 | | const char *ram_type; |
| 372 | | const char *ram_filename; |
| 373 | | |
| 374 | | astring *ram_pathname; |
| 375 | | |
| 376 | | /* locate the 'length' attribute */ |
| 377 | | length_string = xml_get_attribute_string(resource_node, "length", NULL); |
| 378 | | if (length_string == NULL) |
| 379 | | return MCERR_MISSING_RAM_LENGTH; |
| 380 | | |
| 381 | | /* ...and parse it */ |
| 382 | | resource->length = ram_device::parse_string(length_string); |
| 383 | | if (resource->length <= 0) |
| 384 | | return MCERR_INVALID_RAM_SPEC; |
| 385 | | |
| 386 | | /* allocate bytes for this resource */ |
| 387 | | resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 388 | | if (resource->ptr == NULL) |
| 389 | | return MCERR_OUT_OF_MEMORY; |
| 390 | | |
| 391 | | /* Is this a persistent RAM resource? Then try to load it. */ |
| 392 | | ram_type = xml_get_attribute_string(resource_node, "type", NULL); |
| 393 | | if (ram_type != NULL) |
| 394 | | { |
| 395 | | if (strcmp(ram_type, "persistent")==0) |
| 396 | | { |
| 397 | | astring tmp; |
| 398 | | |
| 399 | | /* Get the file name. */ |
| 400 | | ram_filename = xml_get_attribute_string(resource_node, "file", NULL); |
| 401 | | if (ram_filename==NULL) |
| 402 | | return MCERR_XML_ERROR; |
| 403 | | |
| 404 | | ram_pathname = astring_assemble_3(&tmp, state->multicart->gamedrv_name, PATH_SEPARATOR, ram_filename); |
| 405 | | |
| 406 | | /* Save the file name so that we can write the contents on unloading. |
| 407 | | If the RAM resource has no filename, we know that it was volatile only. */ |
| 408 | | resource->filename = pool_strdup_lib(state->multicart->data->pool, astring_c(ram_pathname)); |
| 409 | | |
| 410 | | if (resource->filename == NULL) |
| 411 | | return MCERR_OUT_OF_MEMORY; |
| 412 | | |
| 413 | | image_battery_load_by_name(options, resource->filename, resource->ptr, resource->length, 0x00); |
| 414 | | } |
| 415 | | /* else this type is volatile, in which case we just have |
| 416 | | a memory expansion */ |
| 417 | | } |
| 418 | | return MCERR_NONE; |
| 419 | | } |
| 420 | | |
| 421 | | |
| 422 | | /*------------------------------------------------- |
| 423 | | load_resource |
| 424 | | -------------------------------------------------*/ |
| 425 | | |
| 426 | | static multicart_open_error load_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, |
| 427 | | multicart_resource_type resource_type) |
| 428 | | { |
| 429 | | const char *id; |
| 430 | | multicart_open_error err; |
| 431 | | multicart_resource *resource; |
| 432 | | multicart_resource **next_resource; |
| 433 | | |
| 434 | | /* get the 'id' attribute; error if not present */ |
| 435 | | id = xml_get_attribute_string(resource_node, "id", NULL); |
| 436 | | if (id == NULL) |
| 437 | | return MCERR_XML_ERROR; |
| 438 | | |
| 439 | | /* allocate memory for the resource */ |
| 440 | | resource = (multicart_resource *)pool_malloc_lib(state->multicart->data->pool, sizeof(*resource)); |
| 441 | | if (resource == NULL) |
| 442 | | return MCERR_OUT_OF_MEMORY; |
| 443 | | memset(resource, 0, sizeof(*resource)); |
| 444 | | resource->type = resource_type; |
| 445 | | |
| 446 | | /* copy id */ |
| 447 | | resource->id = pool_strdup_lib(state->multicart->data->pool, id); |
| 448 | | if (resource->id == NULL) |
| 449 | | return MCERR_OUT_OF_MEMORY; |
| 450 | | |
| 451 | | switch(resource->type) |
| 452 | | { |
| 453 | | case MULTICART_RESOURCE_TYPE_ROM: |
| 454 | | err = load_rom_resource(state, resource_node, resource); |
| 455 | | if (err != MCERR_NONE) |
| 456 | | return err; |
| 457 | | break; |
| 458 | | |
| 459 | | case MULTICART_RESOURCE_TYPE_RAM: |
| 460 | | err = load_ram_resource(options, state, resource_node, resource); |
| 461 | | if (err != MCERR_NONE) |
| 462 | | return err; |
| 463 | | break; |
| 464 | | |
| 465 | | default: |
| 466 | | return MCERR_UNKNOWN_RESOURCE_TYPE; |
| 467 | | } |
| 468 | | |
| 469 | | /* append the resource */ |
| 470 | | for (next_resource = &state->resources; *next_resource; next_resource = &(*next_resource)->next) |
| 471 | | ; |
| 472 | | *next_resource = resource; |
| 473 | | |
| 474 | | return MCERR_NONE; |
| 475 | | } |
| 476 | | |
| 477 | | |
| 478 | | /*------------------------------------------------- |
| 479 | | load_all_resources |
| 480 | | -------------------------------------------------*/ |
| 481 | | |
| 482 | | static multicart_open_error load_all_resources(emu_options &options, multicart_load_state *state) |
| 483 | | { |
| 484 | | multicart_open_error err; |
| 485 | | xml_data_node *resource_node; |
| 486 | | multicart_resource_type resource_type; |
| 487 | | |
| 488 | | for (resource_node = state->resources_node->child; resource_node != NULL; resource_node = resource_node->next) |
| 489 | | { |
| 490 | | resource_type = get_resource_type(resource_node->name); |
| 491 | | if (resource_type != MULTICART_RESOURCE_TYPE_INVALID) |
| 492 | | { |
| 493 | | err = load_resource(options, state, resource_node, resource_type); |
| 494 | | if (err != MCERR_NONE) |
| 495 | | return err; |
| 496 | | } |
| 497 | | } |
| 498 | | |
| 499 | | state->multicart->resources = state->resources; |
| 500 | | return MCERR_NONE; |
| 501 | | } |
| 502 | | |
| 503 | | /*------------------------------------------------- |
| 504 | | save_ram_resources. This is important for persistent RAM. All |
| 505 | | resources were allocated within the memory pool of this device and will |
| 506 | | be freed on multicart_close. |
| 507 | | -------------------------------------------------*/ |
| 508 | | |
| 509 | | static multicart_open_error save_ram_resources(emu_options &options, multicart_t *cart) |
| 510 | | { |
| 511 | | const multicart_resource *resource; |
| 512 | | |
| 513 | | for (resource = cart->resources; resource != NULL; resource = resource->next) |
| 514 | | { |
| 515 | | if ((resource->type == MULTICART_RESOURCE_TYPE_RAM) && (resource->filename != NULL)) |
| 516 | | { |
| 517 | | image_battery_save_by_name(options, resource->filename, resource->ptr, resource->length); |
| 518 | | } |
| 519 | | } |
| 520 | | return MCERR_NONE; |
| 521 | | } |
| 522 | | |
| 523 | | /*------------------------------------------------- |
| 524 | | load_socket |
| 525 | | -------------------------------------------------*/ |
| 526 | | |
| 527 | | static multicart_open_error load_socket(multicart_load_state *state, xml_data_node *socket_node) |
| 528 | | { |
| 529 | | const char *id; |
| 530 | | const char *uses; |
| 531 | | const multicart_resource *resource; |
| 532 | | multicart_socket *socket; |
| 533 | | multicart_socket **next_socket; |
| 534 | | |
| 535 | | /* get the 'id' and 'uses' attributes; error if not present */ |
| 536 | | id = xml_get_attribute_string(socket_node, "id", NULL); |
| 537 | | uses = xml_get_attribute_string(socket_node, "uses", NULL); |
| 538 | | if ((id == NULL) || (uses == NULL)) |
| 539 | | return MCERR_XML_ERROR; |
| 540 | | |
| 541 | | /* find the resource */ |
| 542 | | for (resource = state->multicart->resources; resource != NULL; resource = resource->next) |
| 543 | | { |
| 544 | | if (!strcmp(uses, resource->id)) |
| 545 | | break; |
| 546 | | } |
| 547 | | if (resource == NULL) |
| 548 | | return MCERR_INVALID_RESOURCE_REF; |
| 549 | | |
| 550 | | /* create the socket */ |
| 551 | | socket = (multicart_socket *)pool_malloc_lib(state->multicart->data->pool, sizeof(*socket)); |
| 552 | | if (socket == NULL) |
| 553 | | return MCERR_OUT_OF_MEMORY; |
| 554 | | memset(socket, 0, sizeof(*socket)); |
| 555 | | socket->resource = resource; |
| 556 | | socket->ptr = resource->ptr; |
| 557 | | |
| 558 | | /* copy id */ |
| 559 | | socket->id = pool_strdup_lib(state->multicart->data->pool, id); |
| 560 | | if (socket->id == NULL) |
| 561 | | return MCERR_OUT_OF_MEMORY; |
| 562 | | |
| 563 | | /* which pointer should I use? */ |
| 564 | | if (resource->ptr != NULL) |
| 565 | | { |
| 566 | | /* use the resource's ptr */ |
| 567 | | socket->ptr = resource->ptr; |
| 568 | | } |
| 569 | | else |
| 570 | | { |
| 571 | | /* allocate bytes for this socket */ |
| 572 | | socket->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 573 | | if (socket->ptr == NULL) |
| 574 | | return MCERR_OUT_OF_MEMORY; |
| 575 | | |
| 576 | | /* ...and clear it */ |
| 577 | | memset(socket->ptr, 0xCD, resource->length); |
| 578 | | } |
| 579 | | |
| 580 | | /* append the resource */ |
| 581 | | for (next_socket = &state->sockets; *next_socket; next_socket = &(*next_socket)->next) |
| 582 | | ; |
| 583 | | *next_socket = socket; |
| 584 | | |
| 585 | | return MCERR_NONE; |
| 586 | | } |
| 587 | | |
| 588 | | |
| 589 | | /*------------------------------------------------- |
| 590 | | load_all_sockets |
| 591 | | -------------------------------------------------*/ |
| 592 | | |
| 593 | | static multicart_open_error load_all_sockets(multicart_load_state *state) |
| 594 | | { |
| 595 | | multicart_open_error err; |
| 596 | | xml_data_node *socket_node; |
| 597 | | |
| 598 | | for (socket_node = xml_get_sibling(state->pcb_node->child, "socket"); socket_node != NULL; |
| 599 | | socket_node = xml_get_sibling(socket_node->next, "socket")) |
| 600 | | { |
| 601 | | err = load_socket(state, socket_node); |
| 602 | | if (err != MCERR_NONE) |
| 603 | | return err; |
| 604 | | } |
| 605 | | |
| 606 | | state->multicart->sockets = state->sockets; |
| 607 | | return MCERR_NONE; |
| 608 | | } |
| 609 | | |
| 610 | | |
| 611 | | /*------------------------------------------------- |
| 612 | | multicart_open - opens a multicart |
| 613 | | -------------------------------------------------*/ |
| 614 | | |
| 615 | | multicart_open_error multicart_open(emu_options &options, const char *filename, const char *gamedrv, multicart_load_flags load_flags, multicart_t **cart) |
| 616 | | { |
| 617 | | multicart_open_error err; |
| 618 | | zip_error ziperr; |
| 619 | | object_pool *pool; |
| 620 | | multicart_load_state state = {0, }; |
| 621 | | const zip_file_header *header; |
| 622 | | const char *pcb_type; |
| 623 | | char *layout_text = NULL; |
| 624 | | |
| 625 | | /* allocate an object pool */ |
| 626 | | pool = pool_alloc_lib(NULL); |
| 627 | | if (pool == NULL) |
| 628 | | { |
| 629 | | err = MCERR_OUT_OF_MEMORY; |
| 630 | | goto done; |
| 631 | | } |
| 632 | | |
| 633 | | /* allocate the multicart */ |
| 634 | | state.multicart = (multicart_t*)pool_malloc_lib(pool, sizeof(*state.multicart)); |
| 635 | | if (state.multicart == NULL) |
| 636 | | { |
| 637 | | err = MCERR_OUT_OF_MEMORY; |
| 638 | | goto done; |
| 639 | | } |
| 640 | | memset(state.multicart, 0, sizeof(*state.multicart)); |
| 641 | | |
| 642 | | /* allocate the multicart's private data */ |
| 643 | | state.multicart->data = (multicart_private*)pool_malloc_lib(pool, sizeof(*state.multicart->data)); |
| 644 | | if (state.multicart->data == NULL) |
| 645 | | { |
| 646 | | err = MCERR_OUT_OF_MEMORY; |
| 647 | | goto done; |
| 648 | | } |
| 649 | | memset(state.multicart->data, 0, sizeof(*state.multicart->data)); |
| 650 | | state.multicart->data->pool = pool; |
| 651 | | pool = NULL; |
| 652 | | |
| 653 | | /* open the ZIP file */ |
| 654 | | ziperr = zip_file_open(filename, &state.zip); |
| 655 | | if (ziperr != ZIPERR_NONE) |
| 656 | | { |
| 657 | | err = MCERR_NOT_MULTICART; |
| 658 | | goto done; |
| 659 | | } |
| 660 | | |
| 661 | | /* find the layout.xml file */ |
| 662 | | header = find_file(state.zip, "layout.xml"); |
| 663 | | if (header == NULL) |
| 664 | | { |
| 665 | | err = MCERR_MISSING_LAYOUT; |
| 666 | | goto done; |
| 667 | | } |
| 668 | | |
| 669 | | /* reserve space for the layout text */ |
| 670 | | layout_text = (char*)malloc(header->uncompressed_length + 1); |
| 671 | | if (layout_text == NULL) |
| 672 | | { |
| 673 | | err = MCERR_OUT_OF_MEMORY; |
| 674 | | goto done; |
| 675 | | } |
| 676 | | |
| 677 | | /* uncompress the layout text */ |
| 678 | | ziperr = zip_file_decompress(state.zip, layout_text, header->uncompressed_length); |
| 679 | | if (ziperr != ZIPERR_NONE) |
| 680 | | { |
| 681 | | err = MCERR_ZIP_ERROR; |
| 682 | | goto done; |
| 683 | | } |
| 684 | | layout_text[header->uncompressed_length] = '\0'; |
| 685 | | |
| 686 | | /* parse the layout text */ |
| 687 | | state.layout_xml = xml_string_read(layout_text, NULL); |
| 688 | | if (state.layout_xml == NULL) |
| 689 | | { |
| 690 | | err = MCERR_XML_ERROR; |
| 691 | | goto done; |
| 692 | | } |
| 693 | | |
| 694 | | /* locate the PCB node */ |
| 695 | | if (!find_pcb_and_resource_nodes(state.layout_xml, &state.pcb_node, &state.resources_node)) |
| 696 | | { |
| 697 | | err = MCERR_NO_PCB_OR_RESOURCES; |
| 698 | | goto done; |
| 699 | | } |
| 700 | | |
| 701 | | /* get the PCB resource_type */ |
| 702 | | pcb_type = xml_get_attribute_string(state.pcb_node, "type", ""); |
| 703 | | state.multicart->pcb_type = pool_strdup_lib(state.multicart->data->pool, pcb_type); |
| 704 | | if (state.multicart->pcb_type == NULL) |
| 705 | | { |
| 706 | | err = MCERR_OUT_OF_MEMORY; |
| 707 | | goto done; |
| 708 | | } |
| 709 | | |
| 710 | | state.multicart->gamedrv_name = pool_strdup_lib(state.multicart->data->pool, gamedrv); |
| 711 | | if (state.multicart->gamedrv_name == NULL) |
| 712 | | { |
| 713 | | err = MCERR_OUT_OF_MEMORY; |
| 714 | | goto done; |
| 715 | | } |
| 716 | | |
| 717 | | /* do we have to load resources? */ |
| 718 | | if (load_flags & MULTICART_FLAGS_LOAD_RESOURCES) |
| 719 | | { |
| 720 | | err = load_all_resources(options, &state); |
| 721 | | if (err != MCERR_NONE) |
| 722 | | goto done; |
| 723 | | |
| 724 | | err = load_all_sockets(&state); |
| 725 | | if (err != MCERR_NONE) |
| 726 | | goto done; |
| 727 | | } |
| 728 | | |
| 729 | | err = MCERR_NONE; |
| 730 | | |
| 731 | | done: |
| 732 | | if (pool != NULL) |
| 733 | | pool_free_lib(pool); |
| 734 | | if (state.zip != NULL) |
| 735 | | zip_file_close(state.zip); |
| 736 | | if (layout_text != NULL) |
| 737 | | free(layout_text); |
| 738 | | if (state.layout_xml != NULL) |
| 739 | | xml_file_free(state.layout_xml); |
| 740 | | |
| 741 | | if ((err != MCERR_NONE) && (state.multicart != NULL)) |
| 742 | | { |
| 743 | | multicart_close(options, state.multicart); |
| 744 | | state.multicart = NULL; |
| 745 | | } |
| 746 | | *cart = state.multicart; |
| 747 | | return err; |
| 748 | | } |
| 749 | | |
| 750 | | |
| 751 | | /*------------------------------------------------- |
| 752 | | multicart_close - closes a multicart |
| 753 | | -------------------------------------------------*/ |
| 754 | | |
| 755 | | void multicart_close(emu_options &options, multicart_t *cart) |
| 756 | | { |
| 757 | | save_ram_resources(options, cart); |
| 758 | | pool_free_lib(cart->data->pool); |
| 759 | | } |
| 760 | | |
| 761 | | /*------------------------------------------------- |
| 762 | | DEVICE_START( multicartslot ) |
| 763 | | -------------------------------------------------*/ |
| 764 | | |
| 765 | | static DEVICE_START( multicartslot ) |
| 766 | | { |
| 767 | | const multicartslot_config *config = get_config(device); |
| 768 | | |
| 769 | | /* if this cartridge has a custom DEVICE_START, use it */ |
| 770 | | if (config->device_start != NULL) |
| 771 | | { |
| 772 | | (*config->device_start)(device); |
| 773 | | } |
| 774 | | } |
| 775 | | |
| 776 | | |
| 777 | | /*------------------------------------------------- |
| 778 | | DEVICE_IMAGE_LOAD( cartslot ) |
| 779 | | -------------------------------------------------*/ |
| 780 | | |
| 781 | | static DEVICE_IMAGE_LOAD( multicartslot ) |
| 782 | | { |
| 783 | | device_t *device = &image.device(); |
| 784 | | const multicartslot_config *config = get_config(device); |
| 785 | | |
| 786 | | /* if this cartridge has a custom DEVICE_IMAGE_LOAD, use it */ |
| 787 | | if (config->device_load != NULL) |
| 788 | | return (*config->device_load)(image); |
| 789 | | |
| 790 | | /* otherwise try the normal route */ |
| 791 | | return IMAGE_INIT_PASS; |
| 792 | | } |
| 793 | | |
| 794 | | |
| 795 | | /*------------------------------------------------- |
| 796 | | DEVICE_IMAGE_UNLOAD( multicartslot ) |
| 797 | | -------------------------------------------------*/ |
| 798 | | |
| 799 | | static DEVICE_IMAGE_UNLOAD( multicartslot ) |
| 800 | | { |
| 801 | | device_t *device = &image.device(); |
| 802 | | const multicartslot_config *config = get_config(device); |
| 803 | | |
| 804 | | /* if this cartridge has a custom DEVICE_IMAGE_UNLOAD, use it */ |
| 805 | | if (config->device_unload != NULL) |
| 806 | | { |
| 807 | | (*config->device_unload)(image); |
| 808 | | return; |
| 809 | | } |
| 810 | | } |
| 811 | | /*------------------------------------------------- |
| 812 | | DEVICE_GET_INFO( multicartslot ) |
| 813 | | -------------------------------------------------*/ |
| 814 | | |
| 815 | | DEVICE_GET_INFO( multicartslot ) |
| 816 | | { |
| 817 | | switch(state) |
| 818 | | { |
| 819 | | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 820 | | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(multicartslot_t); break; |
| 821 | | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = sizeof(multicartslot_config); break; |
| 822 | | case DEVINFO_INT_IMAGE_TYPE: info->i = IO_CARTSLOT; break; |
| 823 | | case DEVINFO_INT_IMAGE_READABLE: info->i = 1; break; |
| 824 | | case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 0; break; |
| 825 | | case DEVINFO_INT_IMAGE_CREATABLE: info->i = 0; break; |
| 826 | | case DEVINFO_INT_IMAGE_RESET_ON_LOAD: info->i = 1; break; |
| 827 | | case DEVINFO_INT_IMAGE_MUST_BE_LOADED: info->i = 0; break; |
| 828 | | |
| 829 | | /* --- the following bits of info are returned as pointers to functions --- */ |
| 830 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(multicartslot); break; |
| 831 | | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(multicartslot); break; |
| 832 | | case DEVINFO_FCT_IMAGE_UNLOAD: info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(multicartslot); break; |
| 833 | | case DEVINFO_FCT_IMAGE_GET_DEVICES: info->f = (genf *) DEVICE_IMAGE_GET_DEVICES_NAME(multicartslot); break; |
| 834 | | |
| 835 | | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 836 | | case DEVINFO_STR_NAME: strcpy(info->s, "MultiCartslot"); break; |
| 837 | | case DEVINFO_STR_FAMILY: strcpy(info->s, "MultiCartslot"); break; |
| 838 | | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 839 | | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: |
| 840 | | if ( device && downcast<const legacy_image_device_base *>(device)->inline_config() && get_config(device)->extensions ) |
| 841 | | { |
| 842 | | strcpy(info->s, get_config(device)->extensions); |
| 843 | | } |
| 844 | | else |
| 845 | | { |
| 846 | | strcpy(info->s, "bin"); |
| 847 | | } |
| 848 | | break; |
| 849 | | } |
| 850 | | } |
| 851 | | |
| 852 | | DEFINE_LEGACY_IMAGE_DEVICE(MULTICARTSLOT, multicartslot); |
| 853 | | |
| 854 | | //************************************************************************** |
| 855 | | // DEVICE CARTSLOT INTERFACE |
| 856 | | //************************************************************************** |
| 857 | | |
| 858 | | //------------------------------------------------- |
| 859 | | // device_cart_slot_interface - constructor |
| 860 | | //------------------------------------------------- |
| 861 | | |
| 862 | | device_cart_slot_interface::device_cart_slot_interface(const machine_config &mconfig, device_t &device) |
| 863 | | : device_interface(device) |
| 864 | | { |
| 865 | | } |
| 866 | | |
| 867 | | |
| 868 | | //------------------------------------------------- |
| 869 | | // ~device_cart_slot_interface - destructor |
| 870 | | //------------------------------------------------- |
| 871 | | |
| 872 | | device_cart_slot_interface::~device_cart_slot_interface() |
| 873 | | { |
| 874 | | } |
| 875 | | |
| 876 | | //************************************************************************** |
| 877 | | // LIVE LEGACY cart_slot DEVICE |
| 878 | | //************************************************************************** |
| 879 | | |
| 880 | | //------------------------------------------------- |
| 881 | | // legacy_cart_slot_device_base - constructor |
| 882 | | //------------------------------------------------- |
| 883 | | |
| 884 | | legacy_cart_slot_device_base::legacy_cart_slot_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, UINT32 clock, device_get_config_func get_config) |
| 885 | | : legacy_device_base(mconfig, type, tag, owner, clock, get_config), |
| 886 | | device_cart_slot_interface(mconfig, *this) |
| 887 | | { |
| 888 | | } |
src/mess/machine/ti99/gromport.c
| r13375 | r13376 | |
| 6 | 6 | |
| 7 | 7 | #include "emu.h" |
| 8 | 8 | #include "gromport.h" |
| 9 | | #include "imagedev/multcart.h" |
| 10 | 9 | #include "grom.h" |
| 10 | #include "pool.h" |
| 11 | #include "unzip.h" |
| 12 | #include "corestr.h" |
| 13 | #include "xmlfile.h" |
| 14 | #include "hash.h" |
| 15 | #include "machine/ram.h" |
| 11 | 16 | |
| 17 | /* start of multicart */ |
| 18 | #define TAG_PCB "pcb" |
| 19 | |
| 20 | DECLARE_LEGACY_IMAGE_DEVICE(MULTICARTSLOT, multicartslot); |
| 21 | |
| 22 | /*************************************************************************** |
| 23 | TYPE DEFINITIONS |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | enum _multicart_load_flags |
| 27 | { |
| 28 | MULTICART_FLAGS_DONT_LOAD_RESOURCES = 0x00, |
| 29 | MULTICART_FLAGS_LOAD_RESOURCES = 0x01 |
| 30 | }; |
| 31 | typedef enum _multicart_load_flags multicart_load_flags; |
| 32 | |
| 33 | enum _multicart_resource_type |
| 34 | { |
| 35 | MULTICART_RESOURCE_TYPE_INVALID, |
| 36 | MULTICART_RESOURCE_TYPE_ROM, |
| 37 | MULTICART_RESOURCE_TYPE_RAM |
| 38 | }; |
| 39 | typedef enum _multicart_resource_type multicart_resource_type; |
| 40 | |
| 41 | typedef struct _multicart_resource multicart_resource; |
| 42 | struct _multicart_resource |
| 43 | { |
| 44 | const char * id; |
| 45 | const char * filename; |
| 46 | multicart_resource * next; |
| 47 | multicart_resource_type type; |
| 48 | UINT32 length; |
| 49 | void * ptr; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | typedef struct _multicart_socket multicart_socket; |
| 54 | struct _multicart_socket |
| 55 | { |
| 56 | const char * id; |
| 57 | multicart_socket * next; |
| 58 | const multicart_resource * resource; |
| 59 | void * ptr; |
| 60 | }; |
| 61 | |
| 62 | |
| 63 | typedef struct _multicart_private multicart_private; |
| 64 | |
| 65 | typedef struct _multicart_t multicart_t; |
| 66 | struct _multicart_t |
| 67 | { |
| 68 | const multicart_resource * resources; |
| 69 | const multicart_socket * sockets; |
| 70 | const char * pcb_type; |
| 71 | const char * gamedrv_name; /* need this to find the path to the nvram files */ |
| 72 | multicart_private * data; |
| 73 | }; |
| 74 | |
| 75 | typedef struct _multicartslot_t multicartslot_t; |
| 76 | struct _multicartslot_t |
| 77 | { |
| 78 | device_t *pcb_device; |
| 79 | multicart_t *mc; |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | typedef struct _multicartslot_pcb_type multicartslot_pcb_type; |
| 84 | struct _multicartslot_pcb_type |
| 85 | { |
| 86 | const char * name; |
| 87 | device_type devtype; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | typedef struct _multicartslot_config multicartslot_config; |
| 92 | struct _multicartslot_config |
| 93 | { |
| 94 | const char * extensions; |
| 95 | device_start_func device_start; |
| 96 | device_image_load_func device_load; |
| 97 | device_image_unload_func device_unload; |
| 98 | multicartslot_pcb_type pcb_types[16]; |
| 99 | }; |
| 100 | |
| 101 | enum _multicart_open_error |
| 102 | { |
| 103 | MCERR_NONE, |
| 104 | MCERR_NOT_MULTICART, |
| 105 | MCERR_CORRUPT, |
| 106 | MCERR_OUT_OF_MEMORY, |
| 107 | MCERR_XML_ERROR, |
| 108 | MCERR_INVALID_FILE_REF, |
| 109 | MCERR_ZIP_ERROR, |
| 110 | MCERR_MISSING_RAM_LENGTH, |
| 111 | MCERR_INVALID_RAM_SPEC, |
| 112 | MCERR_UNKNOWN_RESOURCE_TYPE, |
| 113 | MCERR_INVALID_RESOURCE_REF, |
| 114 | MCERR_INVALID_FILE_FORMAT, |
| 115 | MCERR_MISSING_LAYOUT, |
| 116 | MCERR_NO_PCB_OR_RESOURCES |
| 117 | }; |
| 118 | typedef enum _multicart_open_error multicart_open_error; |
| 119 | |
| 120 | const char *multicart_error_text(multicart_open_error error); |
| 121 | |
| 122 | /*************************************************************************** |
| 123 | PROTOTYPES |
| 124 | ***************************************************************************/ |
| 125 | |
| 126 | /* opens a multicart */ |
| 127 | multicart_open_error multicart_open(emu_options &options, const char *filename, const char *drvname, multicart_load_flags load_flags, multicart_t **cart); |
| 128 | |
| 129 | /* closes a multicart */ |
| 130 | void multicart_close(emu_options &options, multicart_t *cart); |
| 131 | |
| 132 | /* accesses the PCB associated with this cartslot */ |
| 133 | device_t *cartslot_get_pcb(device_t *device); |
| 134 | |
| 135 | /* accesses a particular socket */ |
| 136 | void *cartslot_get_socket(device_t *device, const char *socket_name); |
| 137 | |
| 138 | /* accesses a particular socket; gets the length of the associated resource */ |
| 139 | int cartslot_get_resource_length(device_t *device, const char *socket_name); |
| 140 | |
| 141 | #define DECLARE_LEGACY_CART_SLOT_DEVICE(name, basename) _DECLARE_LEGACY_DEVICE(name, basename, basename##_device, legacy_cart_slot_device_base) |
| 142 | #define DEFINE_LEGACY_CART_SLOT_DEVICE(name, basename) _DEFINE_LEGACY_DEVICE(name, basename, basename##_device, legacy_cart_slot_device_base) |
| 143 | |
| 144 | #define MCFG_MULTICARTSLOT_ADD(_tag) \ |
| 145 | MCFG_DEVICE_ADD(_tag, MULTICARTSLOT, 0) \ |
| 146 | |
| 147 | #define MCFG_MULTICARTSLOT_MODIFY(_tag) \ |
| 148 | MCFG_DEVICE_MODIFY(_tag) \ |
| 149 | |
| 150 | #define MCFG_MULTICARTSLOT_PCBTYPE(_index, _pcb_type_name, _pcb_devtype) \ |
| 151 | MCFG_DEVICE_CONFIG_DATAPTR_ARRAY_MEMBER(multicartslot_config, pcb_types, _index, multicartslot_pcb_type, name, _pcb_type_name) \ |
| 152 | MCFG_DEVICE_CONFIG_DATAPTR_ARRAY_MEMBER(multicartslot_config, pcb_types, _index, multicartslot_pcb_type, devtype, _pcb_devtype) |
| 153 | |
| 154 | #define MCFG_MULTICARTSLOT_START(_start) \ |
| 155 | MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_start, DEVICE_START_NAME(_start)) |
| 156 | |
| 157 | #define MCFG_MULTICARTSLOT_LOAD(_load) \ |
| 158 | MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_load, DEVICE_IMAGE_LOAD_NAME(_load)) |
| 159 | |
| 160 | #define MCFG_MULTICARTSLOT_UNLOAD(_unload) \ |
| 161 | MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_unload, DEVICE_IMAGE_UNLOAD_NAME(_unload)) |
| 162 | |
| 163 | #define MCFG_MULTICARTSLOT_EXTENSION_LIST(_extensions) \ |
| 164 | MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, extensions, _extensions) |
| 165 | |
| 166 | |
| 167 | // ======================> device_cart_slot_interface |
| 168 | |
| 169 | // class representing interface-specific live cart_slot |
| 170 | class device_cart_slot_interface : public device_interface |
| 171 | { |
| 172 | public: |
| 173 | // construction/destruction |
| 174 | device_cart_slot_interface(const machine_config &mconfig, device_t &device); |
| 175 | virtual ~device_cart_slot_interface(); |
| 176 | }; |
| 177 | |
| 178 | |
| 179 | // ======================> legacy_cart_slot_device |
| 180 | |
| 181 | // legacy_cart_slot_device is a legacy_device_base with a cart_slot interface |
| 182 | class legacy_cart_slot_device_base : public legacy_device_base, |
| 183 | public device_cart_slot_interface |
| 184 | { |
| 185 | protected: |
| 186 | // construction/destruction |
| 187 | legacy_cart_slot_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, UINT32 clock, device_get_config_func get_config); |
| 188 | |
| 189 | public: |
| 190 | using legacy_device_base::get_legacy_int; |
| 191 | using legacy_device_base::get_legacy_fct; |
| 192 | using legacy_device_base::get_legacy_ptr; |
| 193 | |
| 194 | // device_cart_slot_interface overrides |
| 195 | }; |
| 196 | |
| 197 | /*************************************************************************** |
| 198 | TYPE DEFINITIONS |
| 199 | ***************************************************************************/ |
| 200 | |
| 201 | struct _multicart_private |
| 202 | { |
| 203 | object_pool * pool; |
| 204 | zip_file * zip; |
| 205 | }; |
| 206 | |
| 207 | |
| 208 | typedef struct _multicart_load_state multicart_load_state; |
| 209 | struct _multicart_load_state |
| 210 | { |
| 211 | multicart_t * multicart; |
| 212 | zip_file * zip; |
| 213 | xml_data_node * layout_xml; |
| 214 | xml_data_node * resources_node; |
| 215 | xml_data_node * pcb_node; |
| 216 | multicart_resource * resources; |
| 217 | multicart_socket * sockets; |
| 218 | }; |
| 219 | |
| 220 | static const char error_text[14][30] = |
| 221 | { |
| 222 | "no error", |
| 223 | "not a multicart", |
| 224 | "module definition corrupt", |
| 225 | "out of memory", |
| 226 | "xml format error", |
| 227 | "invalid file reference", |
| 228 | "zip file error", |
| 229 | "missing ram length", |
| 230 | "invalid ram specification", |
| 231 | "unknown resource type", |
| 232 | "invalid resource reference", |
| 233 | "invalid file format", |
| 234 | "missing layout", |
| 235 | "no pcb or resource found" |
| 236 | }; |
| 237 | |
| 238 | const char *multicart_error_text(multicart_open_error error) |
| 239 | { |
| 240 | return error_text[(int)error]; |
| 241 | } |
| 242 | |
| 243 | /*************************************************************************** |
| 244 | INLINE FUNCTIONS |
| 245 | ***************************************************************************/ |
| 246 | |
| 247 | INLINE multicartslot_t *get_token(device_t *device) |
| 248 | { |
| 249 | assert(device != NULL); |
| 250 | assert(device->type() == MULTICARTSLOT); |
| 251 | return (multicartslot_t *) downcast<legacy_device_base *>(device)->token(); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | INLINE const multicartslot_config *get_config(const device_t *device) |
| 256 | { |
| 257 | assert(device != NULL); |
| 258 | assert(device->type() == MULTICARTSLOT); |
| 259 | return (const multicartslot_config *) downcast<const legacy_device_base *>(device)->inline_config(); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /*************************************************************************** |
| 264 | IMPLEMENTATION |
| 265 | ***************************************************************************/ |
| 266 | |
| 267 | /*------------------------------------------------- |
| 268 | cartslot_get_pcb |
| 269 | -------------------------------------------------*/ |
| 270 | |
| 271 | device_t *cartslot_get_pcb(device_t *device) |
| 272 | { |
| 273 | multicartslot_t *cart = get_token(device); |
| 274 | return cart->pcb_device; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /*------------------------------------------------- |
| 279 | cartslot_get_socket |
| 280 | -------------------------------------------------*/ |
| 281 | |
| 282 | void *cartslot_get_socket(device_t *device, const char *socket_name) |
| 283 | { |
| 284 | multicartslot_t *cart = get_token(device); |
| 285 | device_image_interface *image = dynamic_cast<device_image_interface *>(device); |
| 286 | void *result = NULL; |
| 287 | |
| 288 | if (cart->mc != NULL) |
| 289 | { |
| 290 | const multicart_socket *socket; |
| 291 | for (socket = cart->mc->sockets; socket != NULL; socket = socket->next) |
| 292 | { |
| 293 | if (!strcmp(socket->id, socket_name)) |
| 294 | break; |
| 295 | } |
| 296 | result = socket ? socket->ptr : NULL; |
| 297 | } |
| 298 | else if (socket_name[0] == '\0') |
| 299 | { |
| 300 | result = image->ptr(); |
| 301 | } |
| 302 | return result; |
| 303 | } |
| 304 | |
| 305 | /*------------------------------------------------- |
| 306 | cartslot_get_resource_length |
| 307 | -------------------------------------------------*/ |
| 308 | |
| 309 | int cartslot_get_resource_length(device_t *device, const char *socket_name) |
| 310 | { |
| 311 | multicartslot_t *cart = get_token(device); |
| 312 | int result = 0; |
| 313 | |
| 314 | if (cart->mc != NULL) |
| 315 | { |
| 316 | const multicart_socket *socket; |
| 317 | |
| 318 | for (socket = cart->mc->sockets; socket != NULL; socket = socket->next) |
| 319 | { |
| 320 | if (!strcmp(socket->id, socket_name)) { |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | if (socket != NULL) |
| 325 | result = socket->resource->length; |
| 326 | } |
| 327 | else |
| 328 | result = 0; |
| 329 | |
| 330 | return result; |
| 331 | } |
| 332 | /*------------------------------------------------- |
| 333 | identify_pcb |
| 334 | -------------------------------------------------*/ |
| 335 | |
| 336 | static const multicartslot_pcb_type *identify_pcb(device_image_interface &image) |
| 337 | { |
| 338 | const multicartslot_config *config = get_config(&image.device()); |
| 339 | astring pcb_name; |
| 340 | const multicartslot_pcb_type *pcb_type = NULL; |
| 341 | multicart_t *mc; |
| 342 | int i; |
| 343 | |
| 344 | if (image.exists()) |
| 345 | { |
| 346 | /* try opening this as if it were a multicart */ |
| 347 | multicart_open_error me = multicart_open(image.device().machine().options(), image.filename(), image.device().machine().system().name, MULTICART_FLAGS_DONT_LOAD_RESOURCES, &mc); |
| 348 | if (me == MCERR_NONE) |
| 349 | { |
| 350 | /* this was a multicart - read from it */ |
| 351 | astring_cpyc(&pcb_name, mc->pcb_type); |
| 352 | multicart_close(image.device().machine().options(), mc); |
| 353 | } |
| 354 | else |
| 355 | { |
| 356 | if (me != MCERR_NOT_MULTICART) |
| 357 | fatalerror("multicart error: %s", multicart_error_text(me)); |
| 358 | } |
| 359 | |
| 360 | /* look for PCB type with matching name */ |
| 361 | for (i = 0; (i < ARRAY_LENGTH(config->pcb_types)) && (config->pcb_types[i].name != NULL); i++) |
| 362 | { |
| 363 | if ((config->pcb_types[i].name[0] == '\0') || !strcmp(astring_c(&pcb_name), config->pcb_types[i].name)) |
| 364 | { |
| 365 | pcb_type = &config->pcb_types[i]; |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* check for unknown PCB type */ |
| 371 | if ((mc != NULL) && (pcb_type == NULL)) |
| 372 | fatalerror("Unknown PCB type \"%s\"", astring_c(&pcb_name)); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | /* no device loaded; use the default */ |
| 377 | pcb_type = (config->pcb_types[0].name != NULL) ? &config->pcb_types[0] : NULL; |
| 378 | } |
| 379 | return pcb_type; |
| 380 | } |
| 381 | |
| 382 | /*------------------------------------------------- |
| 383 | DEVICE_IMAGE_GET_DEVICES(cartslot) |
| 384 | -------------------------------------------------*/ |
| 385 | static DEVICE_IMAGE_GET_DEVICES(multicartslot) |
| 386 | { |
| 387 | const multicartslot_pcb_type *pcb_type; |
| 388 | device_t *device = &image.device(); |
| 389 | |
| 390 | pcb_type = identify_pcb(image); |
| 391 | if (pcb_type != NULL) |
| 392 | { |
| 393 | image_add_device_with_subdevices(device,pcb_type->devtype,TAG_PCB,0); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /*------------------------------------------------- |
| 399 | find_file |
| 400 | -------------------------------------------------*/ |
| 401 | |
| 402 | static const zip_file_header *find_file(zip_file *zip, const char *filename) |
| 403 | { |
| 404 | const zip_file_header *header; |
| 405 | for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip)) |
| 406 | { |
| 407 | if (!core_stricmp(header->filename, filename)) |
| 408 | return header; |
| 409 | } |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | static const zip_file_header *find_file_crc(zip_file *zip, const char *filename, UINT32 crc) |
| 414 | { |
| 415 | const zip_file_header *header; |
| 416 | for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip)) |
| 417 | { |
| 418 | // if the CRC and name both match, we're good |
| 419 | // if the CRC matches and the name doesn't, we're still good |
| 420 | if ((!core_stricmp(header->filename, filename)) && (header->crc == crc)) |
| 421 | { |
| 422 | return header; |
| 423 | } |
| 424 | else if (header->crc == crc) |
| 425 | { |
| 426 | return header; |
| 427 | } |
| 428 | } |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | /*------------------------------------------------- |
| 433 | find_pcb_and_resource_nodes |
| 434 | -------------------------------------------------*/ |
| 435 | |
| 436 | static int find_pcb_and_resource_nodes(xml_data_node *layout_xml, |
| 437 | xml_data_node **pcb_node, xml_data_node **resource_node) |
| 438 | { |
| 439 | xml_data_node *romset_node; |
| 440 | xml_data_node *configuration_node; |
| 441 | |
| 442 | *pcb_node = NULL; |
| 443 | *resource_node = NULL; |
| 444 | |
| 445 | romset_node = xml_get_sibling(layout_xml->child, "romset"); |
| 446 | if (romset_node == NULL) |
| 447 | return FALSE; |
| 448 | |
| 449 | configuration_node = xml_get_sibling(romset_node->child, "configuration"); |
| 450 | if (configuration_node == NULL) |
| 451 | return FALSE; |
| 452 | |
| 453 | *pcb_node = xml_get_sibling(configuration_node->child, "pcb"); |
| 454 | if (*pcb_node == NULL) |
| 455 | return FALSE; |
| 456 | |
| 457 | *resource_node = xml_get_sibling(romset_node->child, "resources"); |
| 458 | if (*resource_node == NULL) |
| 459 | return FALSE; |
| 460 | |
| 461 | return TRUE; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /*------------------------------------------------- |
| 466 | get_resource_type |
| 467 | -------------------------------------------------*/ |
| 468 | |
| 469 | static multicart_resource_type get_resource_type(const char *s) |
| 470 | { |
| 471 | multicart_resource_type result; |
| 472 | if (!strcmp(s, "rom")) |
| 473 | result = MULTICART_RESOURCE_TYPE_ROM; |
| 474 | else if (!strcmp(s, "ram")) |
| 475 | result = MULTICART_RESOURCE_TYPE_RAM; |
| 476 | else |
| 477 | result = MULTICART_RESOURCE_TYPE_INVALID; |
| 478 | return result; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /*------------------------------------------------- |
| 483 | load_rom_resource |
| 484 | -------------------------------------------------*/ |
| 485 | |
| 486 | static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node, |
| 487 | multicart_resource *resource) |
| 488 | { |
| 489 | const char *file, *crcstr, *sha1; |
| 490 | const zip_file_header *header; |
| 491 | zip_error ziperr; |
| 492 | UINT32 crc; |
| 493 | |
| 494 | /* locate the 'file' attribute */ |
| 495 | file = xml_get_attribute_string(resource_node, "file", NULL); |
| 496 | if (file == NULL) |
| 497 | return MCERR_XML_ERROR; |
| 498 | |
| 499 | if (!(crcstr = xml_get_attribute_string(resource_node, "crc", NULL))) |
| 500 | { |
| 501 | /* locate the file in the ZIP file */ |
| 502 | header = find_file(state->zip, file); |
| 503 | } |
| 504 | else /* CRC tag is present, use it */ |
| 505 | { |
| 506 | crc = strtoul(crcstr, NULL, 16); |
| 507 | header = find_file_crc(state->zip, file, crc); |
| 508 | } |
| 509 | if (header == NULL) |
| 510 | return MCERR_INVALID_FILE_REF; |
| 511 | resource->length = header->uncompressed_length; |
| 512 | |
| 513 | /* allocate bytes for this resource */ |
| 514 | resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 515 | if (resource->ptr == NULL) |
| 516 | return MCERR_OUT_OF_MEMORY; |
| 517 | |
| 518 | /* and decompress it */ |
| 519 | ziperr = zip_file_decompress(state->zip, resource->ptr, resource->length); |
| 520 | if (ziperr != ZIPERR_NONE) |
| 521 | return MCERR_ZIP_ERROR; |
| 522 | |
| 523 | /* check SHA1 now */ |
| 524 | if ((sha1 = xml_get_attribute_string(resource_node, "sha1", NULL))) |
| 525 | { |
| 526 | hash_collection actual_hashes; |
| 527 | actual_hashes.compute((const UINT8 *)resource->ptr, resource->length, hash_collection::HASH_TYPES_CRC_SHA1); |
| 528 | |
| 529 | hash_collection expected_hashes; |
| 530 | expected_hashes.add_from_string(hash_collection::HASH_SHA1, sha1, strlen(sha1)); |
| 531 | |
| 532 | if (actual_hashes != expected_hashes) |
| 533 | { |
| 534 | return MCERR_INVALID_FILE_REF; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | return MCERR_NONE; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | /*------------------------------------------------- |
| 543 | load_ram_resource |
| 544 | -------------------------------------------------*/ |
| 545 | |
| 546 | static multicart_open_error load_ram_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, |
| 547 | multicart_resource *resource) |
| 548 | { |
| 549 | const char *length_string; |
| 550 | const char *ram_type; |
| 551 | const char *ram_filename; |
| 552 | |
| 553 | astring *ram_pathname; |
| 554 | |
| 555 | /* locate the 'length' attribute */ |
| 556 | length_string = xml_get_attribute_string(resource_node, "length", NULL); |
| 557 | if (length_string == NULL) |
| 558 | return MCERR_MISSING_RAM_LENGTH; |
| 559 | |
| 560 | /* ...and parse it */ |
| 561 | resource->length = ram_device::parse_string(length_string); |
| 562 | if (resource->length <= 0) |
| 563 | return MCERR_INVALID_RAM_SPEC; |
| 564 | |
| 565 | /* allocate bytes for this resource */ |
| 566 | resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 567 | if (resource->ptr == NULL) |
| 568 | return MCERR_OUT_OF_MEMORY; |
| 569 | |
| 570 | /* Is this a persistent RAM resource? Then try to load it. */ |
| 571 | ram_type = xml_get_attribute_string(resource_node, "type", NULL); |
| 572 | if (ram_type != NULL) |
| 573 | { |
| 574 | if (strcmp(ram_type, "persistent")==0) |
| 575 | { |
| 576 | astring tmp; |
| 577 | |
| 578 | /* Get the file name. */ |
| 579 | ram_filename = xml_get_attribute_string(resource_node, "file", NULL); |
| 580 | if (ram_filename==NULL) |
| 581 | return MCERR_XML_ERROR; |
| 582 | |
| 583 | ram_pathname = astring_assemble_3(&tmp, state->multicart->gamedrv_name, PATH_SEPARATOR, ram_filename); |
| 584 | |
| 585 | /* Save the file name so that we can write the contents on unloading. |
| 586 | If the RAM resource has no filename, we know that it was volatile only. */ |
| 587 | resource->filename = pool_strdup_lib(state->multicart->data->pool, astring_c(ram_pathname)); |
| 588 | |
| 589 | if (resource->filename == NULL) |
| 590 | return MCERR_OUT_OF_MEMORY; |
| 591 | |
| 592 | image_battery_load_by_name(options, resource->filename, resource->ptr, resource->length, 0x00); |
| 593 | } |
| 594 | /* else this type is volatile, in which case we just have |
| 595 | a memory expansion */ |
| 596 | } |
| 597 | return MCERR_NONE; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /*------------------------------------------------- |
| 602 | load_resource |
| 603 | -------------------------------------------------*/ |
| 604 | |
| 605 | static multicart_open_error load_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node, |
| 606 | multicart_resource_type resource_type) |
| 607 | { |
| 608 | const char *id; |
| 609 | multicart_open_error err; |
| 610 | multicart_resource *resource; |
| 611 | multicart_resource **next_resource; |
| 612 | |
| 613 | /* get the 'id' attribute; error if not present */ |
| 614 | id = xml_get_attribute_string(resource_node, "id", NULL); |
| 615 | if (id == NULL) |
| 616 | return MCERR_XML_ERROR; |
| 617 | |
| 618 | /* allocate memory for the resource */ |
| 619 | resource = (multicart_resource *)pool_malloc_lib(state->multicart->data->pool, sizeof(*resource)); |
| 620 | if (resource == NULL) |
| 621 | return MCERR_OUT_OF_MEMORY; |
| 622 | memset(resource, 0, sizeof(*resource)); |
| 623 | resource->type = resource_type; |
| 624 | |
| 625 | /* copy id */ |
| 626 | resource->id = pool_strdup_lib(state->multicart->data->pool, id); |
| 627 | if (resource->id == NULL) |
| 628 | return MCERR_OUT_OF_MEMORY; |
| 629 | |
| 630 | switch(resource->type) |
| 631 | { |
| 632 | case MULTICART_RESOURCE_TYPE_ROM: |
| 633 | err = load_rom_resource(state, resource_node, resource); |
| 634 | if (err != MCERR_NONE) |
| 635 | return err; |
| 636 | break; |
| 637 | |
| 638 | case MULTICART_RESOURCE_TYPE_RAM: |
| 639 | err = load_ram_resource(options, state, resource_node, resource); |
| 640 | if (err != MCERR_NONE) |
| 641 | return err; |
| 642 | break; |
| 643 | |
| 644 | default: |
| 645 | return MCERR_UNKNOWN_RESOURCE_TYPE; |
| 646 | } |
| 647 | |
| 648 | /* append the resource */ |
| 649 | for (next_resource = &state->resources; *next_resource; next_resource = &(*next_resource)->next) |
| 650 | ; |
| 651 | *next_resource = resource; |
| 652 | |
| 653 | return MCERR_NONE; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /*------------------------------------------------- |
| 658 | load_all_resources |
| 659 | -------------------------------------------------*/ |
| 660 | |
| 661 | static multicart_open_error load_all_resources(emu_options &options, multicart_load_state *state) |
| 662 | { |
| 663 | multicart_open_error err; |
| 664 | xml_data_node *resource_node; |
| 665 | multicart_resource_type resource_type; |
| 666 | |
| 667 | for (resource_node = state->resources_node->child; resource_node != NULL; resource_node = resource_node->next) |
| 668 | { |
| 669 | resource_type = get_resource_type(resource_node->name); |
| 670 | if (resource_type != MULTICART_RESOURCE_TYPE_INVALID) |
| 671 | { |
| 672 | err = load_resource(options, state, resource_node, resource_type); |
| 673 | if (err != MCERR_NONE) |
| 674 | return err; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | state->multicart->resources = state->resources; |
| 679 | return MCERR_NONE; |
| 680 | } |
| 681 | |
| 682 | /*------------------------------------------------- |
| 683 | save_ram_resources. This is important for persistent RAM. All |
| 684 | resources were allocated within the memory pool of this device and will |
| 685 | be freed on multicart_close. |
| 686 | -------------------------------------------------*/ |
| 687 | |
| 688 | static multicart_open_error save_ram_resources(emu_options &options, multicart_t *cart) |
| 689 | { |
| 690 | const multicart_resource *resource; |
| 691 | |
| 692 | for (resource = cart->resources; resource != NULL; resource = resource->next) |
| 693 | { |
| 694 | if ((resource->type == MULTICART_RESOURCE_TYPE_RAM) && (resource->filename != NULL)) |
| 695 | { |
| 696 | image_battery_save_by_name(options, resource->filename, resource->ptr, resource->length); |
| 697 | } |
| 698 | } |
| 699 | return MCERR_NONE; |
| 700 | } |
| 701 | |
| 702 | /*------------------------------------------------- |
| 703 | load_socket |
| 704 | -------------------------------------------------*/ |
| 705 | |
| 706 | static multicart_open_error load_socket(multicart_load_state *state, xml_data_node *socket_node) |
| 707 | { |
| 708 | const char *id; |
| 709 | const char *uses; |
| 710 | const multicart_resource *resource; |
| 711 | multicart_socket *socket; |
| 712 | multicart_socket **next_socket; |
| 713 | |
| 714 | /* get the 'id' and 'uses' attributes; error if not present */ |
| 715 | id = xml_get_attribute_string(socket_node, "id", NULL); |
| 716 | uses = xml_get_attribute_string(socket_node, "uses", NULL); |
| 717 | if ((id == NULL) || (uses == NULL)) |
| 718 | return MCERR_XML_ERROR; |
| 719 | |
| 720 | /* find the resource */ |
| 721 | for (resource = state->multicart->resources; resource != NULL; resource = resource->next) |
| 722 | { |
| 723 | if (!strcmp(uses, resource->id)) |
| 724 | break; |
| 725 | } |
| 726 | if (resource == NULL) |
| 727 | return MCERR_INVALID_RESOURCE_REF; |
| 728 | |
| 729 | /* create the socket */ |
| 730 | socket = (multicart_socket *)pool_malloc_lib(state->multicart->data->pool, sizeof(*socket)); |
| 731 | if (socket == NULL) |
| 732 | return MCERR_OUT_OF_MEMORY; |
| 733 | memset(socket, 0, sizeof(*socket)); |
| 734 | socket->resource = resource; |
| 735 | socket->ptr = resource->ptr; |
| 736 | |
| 737 | /* copy id */ |
| 738 | socket->id = pool_strdup_lib(state->multicart->data->pool, id); |
| 739 | if (socket->id == NULL) |
| 740 | return MCERR_OUT_OF_MEMORY; |
| 741 | |
| 742 | /* which pointer should I use? */ |
| 743 | if (resource->ptr != NULL) |
| 744 | { |
| 745 | /* use the resource's ptr */ |
| 746 | socket->ptr = resource->ptr; |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | /* allocate bytes for this socket */ |
| 751 | socket->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length); |
| 752 | if (socket->ptr == NULL) |
| 753 | return MCERR_OUT_OF_MEMORY; |
| 754 | |
| 755 | /* ...and clear it */ |
| 756 | memset(socket->ptr, 0xCD, resource->length); |
| 757 | } |
| 758 | |
| 759 | /* append the resource */ |
| 760 | for (next_socket = &state->sockets; *next_socket; next_socket = &(*next_socket)->next) |
| 761 | ; |
| 762 | *next_socket = socket; |
| 763 | |
| 764 | return MCERR_NONE; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | /*------------------------------------------------- |
| 769 | load_all_sockets |
| 770 | -------------------------------------------------*/ |
| 771 | |
| 772 | static multicart_open_error load_all_sockets(multicart_load_state *state) |
| 773 | { |
| 774 | multicart_open_error err; |
| 775 | xml_data_node *socket_node; |
| 776 | |
| 777 | for (socket_node = xml_get_sibling(state->pcb_node->child, "socket"); socket_node != NULL; |
| 778 | socket_node = xml_get_sibling(socket_node->next, "socket")) |
| 779 | { |
| 780 | err = load_socket(state, socket_node); |
| 781 | if (err != MCERR_NONE) |
| 782 | return err; |
| 783 | } |
| 784 | |
| 785 | state->multicart->sockets = state->sockets; |
| 786 | return MCERR_NONE; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /*------------------------------------------------- |
| 791 | multicart_open - opens a multicart |
| 792 | -------------------------------------------------*/ |
| 793 | |
| 794 | multicart_open_error multicart_open(emu_options &options, const char *filename, const char *gamedrv, multicart_load_flags load_flags, multicart_t **cart) |
| 795 | { |
| 796 | multicart_open_error err; |
| 797 | zip_error ziperr; |
| 798 | object_pool *pool; |
| 799 | multicart_load_state state = {0, }; |
| 800 | const zip_file_header *header; |
| 801 | const char *pcb_type; |
| 802 | char *layout_text = NULL; |
| 803 | |
| 804 | /* allocate an object pool */ |
| 805 | pool = pool_alloc_lib(NULL); |
| 806 | if (pool == NULL) |
| 807 | { |
| 808 | err = MCERR_OUT_OF_MEMORY; |
| 809 | goto done; |
| 810 | } |
| 811 | |
| 812 | /* allocate the multicart */ |
| 813 | state.multicart = (multicart_t*)pool_malloc_lib(pool, sizeof(*state.multicart)); |
| 814 | if (state.multicart == NULL) |
| 815 | { |
| 816 | err = MCERR_OUT_OF_MEMORY; |
| 817 | goto done; |
| 818 | } |
| 819 | memset(state.multicart, 0, sizeof(*state.multicart)); |
| 820 | |
| 821 | /* allocate the multicart's private data */ |
| 822 | state.multicart->data = (multicart_private*)pool_malloc_lib(pool, sizeof(*state.multicart->data)); |
| 823 | if (state.multicart->data == NULL) |
| 824 | { |
| 825 | err = MCERR_OUT_OF_MEMORY; |
| 826 | goto done; |
| 827 | } |
| 828 | memset(state.multicart->data, 0, sizeof(*state.multicart->data)); |
| 829 | state.multicart->data->pool = pool; |
| 830 | pool = NULL; |
| 831 | |
| 832 | /* open the ZIP file */ |
| 833 | ziperr = zip_file_open(filename, &state.zip); |
| 834 | if (ziperr != ZIPERR_NONE) |
| 835 | { |
| 836 | err = MCERR_NOT_MULTICART; |
| 837 | goto done; |
| 838 | } |
| 839 | |
| 840 | /* find the layout.xml file */ |
| 841 | header = find_file(state.zip, "layout.xml"); |
| 842 | if (header == NULL) |
| 843 | { |
| 844 | err = MCERR_MISSING_LAYOUT; |
| 845 | goto done; |
| 846 | } |
| 847 | |
| 848 | /* reserve space for the layout text */ |
| 849 | layout_text = (char*)malloc(header->uncompressed_length + 1); |
| 850 | if (layout_text == NULL) |
| 851 | { |
| 852 | err = MCERR_OUT_OF_MEMORY; |
| 853 | goto done; |
| 854 | } |
| 855 | |
| 856 | /* uncompress the layout text */ |
| 857 | ziperr = zip_file_decompress(state.zip, layout_text, header->uncompressed_length); |
| 858 | if (ziperr != ZIPERR_NONE) |
| 859 | { |
| 860 | err = MCERR_ZIP_ERROR; |
| 861 | goto done; |
| 862 | } |
| 863 | layout_text[header->uncompressed_length] = '\0'; |
| 864 | |
| 865 | /* parse the layout text */ |
| 866 | state.layout_xml = xml_string_read(layout_text, NULL); |
| 867 | if (state.layout_xml == NULL) |
| 868 | { |
| 869 | err = MCERR_XML_ERROR; |
| 870 | goto done; |
| 871 | } |
| 872 | |
| 873 | /* locate the PCB node */ |
| 874 | if (!find_pcb_and_resource_nodes(state.layout_xml, &state.pcb_node, &state.resources_node)) |
| 875 | { |
| 876 | err = MCERR_NO_PCB_OR_RESOURCES; |
| 877 | goto done; |
| 878 | } |
| 879 | |
| 880 | /* get the PCB resource_type */ |
| 881 | pcb_type = xml_get_attribute_string(state.pcb_node, "type", ""); |
| 882 | state.multicart->pcb_type = pool_strdup_lib(state.multicart->data->pool, pcb_type); |
| 883 | if (state.multicart->pcb_type == NULL) |
| 884 | { |
| 885 | err = MCERR_OUT_OF_MEMORY; |
| 886 | goto done; |
| 887 | } |
| 888 | |
| 889 | state.multicart->gamedrv_name = pool_strdup_lib(state.multicart->data->pool, gamedrv); |
| 890 | if (state.multicart->gamedrv_name == NULL) |
| 891 | { |
| 892 | err = MCERR_OUT_OF_MEMORY; |
| 893 | goto done; |
| 894 | } |
| 895 | |
| 896 | /* do we have to load resources? */ |
| 897 | if (load_flags & MULTICART_FLAGS_LOAD_RESOURCES) |
| 898 | { |
| 899 | err = load_all_resources(options, &state); |
| 900 | if (err != MCERR_NONE) |
| 901 | goto done; |
| 902 | |
| 903 | err = load_all_sockets(&state); |
| 904 | if (err != MCERR_NONE) |
| 905 | goto done; |
| 906 | } |
| 907 | |
| 908 | err = MCERR_NONE; |
| 909 | |
| 910 | done: |
| 911 | if (pool != NULL) |
| 912 | pool_free_lib(pool); |
| 913 | if (state.zip != NULL) |
| 914 | zip_file_close(state.zip); |
| 915 | if (layout_text != NULL) |
| 916 | free(layout_text); |
| 917 | if (state.layout_xml != NULL) |
| 918 | xml_file_free(state.layout_xml); |
| 919 | |
| 920 | if ((err != MCERR_NONE) && (state.multicart != NULL)) |
| 921 | { |
| 922 | multicart_close(options, state.multicart); |
| 923 | state.multicart = NULL; |
| 924 | } |
| 925 | *cart = state.multicart; |
| 926 | return err; |
| 927 | } |
| 928 | |
| 929 | |
| 930 | /*------------------------------------------------- |
| 931 | multicart_close - closes a multicart |
| 932 | -------------------------------------------------*/ |
| 933 | |
| 934 | void multicart_close(emu_options &options, multicart_t *cart) |
| 935 | { |
| 936 | save_ram_resources(options, cart); |
| 937 | pool_free_lib(cart->data->pool); |
| 938 | } |
| 939 | |
| 940 | /*------------------------------------------------- |
| 941 | DEVICE_START( multicartslot ) |
| 942 | -------------------------------------------------*/ |
| 943 | |
| 944 | static DEVICE_START( multicartslot ) |
| 945 | { |
| 946 | const multicartslot_config *config = get_config(device); |
| 947 | |
| 948 | /* if this cartridge has a custom DEVICE_START, use it */ |
| 949 | if (config->device_start != NULL) |
| 950 | { |
| 951 | (*config->device_start)(device); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | |
| 956 | /*------------------------------------------------- |
| 957 | DEVICE_IMAGE_LOAD( cartslot ) |
| 958 | -------------------------------------------------*/ |
| 959 | |
| 960 | static DEVICE_IMAGE_LOAD( multicartslot ) |
| 961 | { |
| 962 | device_t *device = &image.device(); |
| 963 | const multicartslot_config *config = get_config(device); |
| 964 | |
| 965 | /* if this cartridge has a custom DEVICE_IMAGE_LOAD, use it */ |
| 966 | if (config->device_load != NULL) |
| 967 | return (*config->device_load)(image); |
| 968 | |
| 969 | /* otherwise try the normal route */ |
| 970 | return IMAGE_INIT_PASS; |
| 971 | } |
| 972 | |
| 973 | |
| 974 | /*------------------------------------------------- |
| 975 | DEVICE_IMAGE_UNLOAD( multicartslot ) |
| 976 | -------------------------------------------------*/ |
| 977 | |
| 978 | static DEVICE_IMAGE_UNLOAD( multicartslot ) |
| 979 | { |
| 980 | device_t *device = &image.device(); |
| 981 | const multicartslot_config *config = get_config(device); |
| 982 | |
| 983 | /* if this cartridge has a custom DEVICE_IMAGE_UNLOAD, use it */ |
| 984 | if (config->device_unload != NULL) |
| 985 | { |
| 986 | (*config->device_unload)(image); |
| 987 | return; |
| 988 | } |
| 989 | } |
| 990 | /*------------------------------------------------- |
| 991 | DEVICE_GET_INFO( multicartslot ) |
| 992 | -------------------------------------------------*/ |
| 993 | |
| 994 | DEVICE_GET_INFO( multicartslot ) |
| 995 | { |
| 996 | switch(state) |
| 997 | { |
| 998 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 999 | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(multicartslot_t); break; |
| 1000 | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = sizeof(multicartslot_config); break; |
| 1001 | case DEVINFO_INT_IMAGE_TYPE: info->i = IO_CARTSLOT; break; |
| 1002 | case DEVINFO_INT_IMAGE_READABLE: info->i = 1; break; |
| 1003 | case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 0; break; |
| 1004 | case DEVINFO_INT_IMAGE_CREATABLE: info->i = 0; break; |
| 1005 | case DEVINFO_INT_IMAGE_RESET_ON_LOAD: info->i = 1; break; |
| 1006 | case DEVINFO_INT_IMAGE_MUST_BE_LOADED: info->i = 0; break; |
| 1007 | |
| 1008 | /* --- the following bits of info are returned as pointers to functions --- */ |
| 1009 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(multicartslot); break; |
| 1010 | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(multicartslot); break; |
| 1011 | case DEVINFO_FCT_IMAGE_UNLOAD: info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(multicartslot); break; |
| 1012 | case DEVINFO_FCT_IMAGE_GET_DEVICES: info->f = (genf *) DEVICE_IMAGE_GET_DEVICES_NAME(multicartslot); break; |
| 1013 | |
| 1014 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 1015 | case DEVINFO_STR_NAME: strcpy(info->s, "MultiCartslot"); break; |
| 1016 | case DEVINFO_STR_FAMILY: strcpy(info->s, "MultiCartslot"); break; |
| 1017 | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 1018 | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: |
| 1019 | if ( device && downcast<const legacy_image_device_base *>(device)->inline_config() && get_config(device)->extensions ) |
| 1020 | { |
| 1021 | strcpy(info->s, get_config(device)->extensions); |
| 1022 | } |
| 1023 | else |
| 1024 | { |
| 1025 | strcpy(info->s, "bin"); |
| 1026 | } |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | DEFINE_LEGACY_IMAGE_DEVICE(MULTICARTSLOT, multicartslot); |
| 1032 | |
| 1033 | //************************************************************************** |
| 1034 | // DEVICE CARTSLOT INTERFACE |
| 1035 | //************************************************************************** |
| 1036 | |
| 1037 | //------------------------------------------------- |
| 1038 | // device_cart_slot_interface - constructor |
| 1039 | //------------------------------------------------- |
| 1040 | |
| 1041 | device_cart_slot_interface::device_cart_slot_interface(const machine_config &mconfig, device_t &device) |
| 1042 | : device_interface(device) |
| 1043 | { |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | //------------------------------------------------- |
| 1048 | // ~device_cart_slot_interface - destructor |
| 1049 | //------------------------------------------------- |
| 1050 | |
| 1051 | device_cart_slot_interface::~device_cart_slot_interface() |
| 1052 | { |
| 1053 | } |
| 1054 | |
| 1055 | //************************************************************************** |
| 1056 | // LIVE LEGACY cart_slot DEVICE |
| 1057 | //************************************************************************** |
| 1058 | |
| 1059 | //------------------------------------------------- |
| 1060 | // legacy_cart_slot_device_base - constructor |
| 1061 | //------------------------------------------------- |
| 1062 | |
| 1063 | legacy_cart_slot_device_base::legacy_cart_slot_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, UINT32 clock, device_get_config_func get_config) |
| 1064 | : legacy_device_base(mconfig, type, tag, owner, clock, get_config), |
| 1065 | device_cart_slot_interface(mconfig, *this) |
| 1066 | { |
| 1067 | } |
| 1068 | |
| 1069 | /* end of multicart */ |
| 12 | 1070 | typedef UINT8 (*read8z_device_func) (ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT8 *value); |
| 13 | 1071 | |
| 14 | 1072 | /* We set the number of slots to 8, although we may have up to 16. From a |