/* set currently active cart in "Main Slot" as default */ void cartridge_set_default(void) { int type = CARTRIDGE_NONE; if (cartfile != NULL) { if (util_file_exists(cartfile)) { if (crt_getid(cartfile) > 0) { type = CARTRIDGE_CRT; } else { type = c64cart_type; } } else { DBG(("cartridge_set_default: file does not exist: '%s'\n", cartfile)); } } else { DBG(("cartridge_set_default: no filename\n")); } DBG(("cartridge_set_default: id %d '%s'\n", type, cartfile)); if (type == CARTRIDGE_NONE) { util_string_set(&cartridge_file, ""); /* resource value modified */ } else { util_string_set(&cartridge_file, cartfile); /* resource value modified */ } cartridge_type = type; /* resource value modified */ }
static int expert_load_image(void) { int res = 0; FILE *fd; if (crt_getid(expert_filename) == CARTRIDGE_EXPERT) { fd = fopen(expert_filename, MODE_READ); res = expert_crt_load(fd, expert_ram); fclose(fd); } else { res = expert_bin_load(expert_filename, expert_ram); } return res; }
static int try_cartridge_attach(int type, const char *filename) { int crtid; if (filename) { if (util_file_exists(filename)) { if ((crtid = crt_getid(filename)) > 0) { cartridge_type = CARTRIDGE_CRT; /* resource value modified */ return cartridge_attach_image(CARTRIDGE_CRT, filename); } else if ((type != CARTRIDGE_NONE) && (type != CARTRIDGE_CRT)) { cartridge_type = type; /* resource value modified */ return cartridge_attach_image(type, filename); } } else { DBG(("cartridge_file does not exist: '%s'\n", filename)); } } return 0; }
/* attach cartridge image type == -1 NONE type == 0 CRT format returns -1 on error, 0 on success */ int cartridge_attach_image(int type, const char *filename) { BYTE *rawcart; char *abs_filename; int carttype = CARTRIDGE_NONE; int cartid = CARTRIDGE_NONE; int oldmain = CARTRIDGE_NONE; int slotmain = 0; if (filename == NULL) { return -1; } /* Attaching no cartridge always works. */ if (type == CARTRIDGE_NONE || *filename == '\0') { return 0; } if (archdep_path_is_relative(filename)) { archdep_expand_path(&abs_filename, filename); } else { abs_filename = lib_stralloc(filename); } if (type == CARTRIDGE_CRT) { carttype = crt_getid(abs_filename); if (carttype == -1) { log_message(LOG_DEFAULT, "CART: '%s' is not a valid CRT file.", abs_filename); lib_free(abs_filename); return -1; } } else { carttype = type; } DBG(("CART: cartridge_attach_image type: %d ID: %d\n", type, carttype)); /* allocate temporary array */ rawcart = lib_malloc(C64CART_IMAGE_LIMIT); /* cart should always be detached. there is no reason for doing fancy checks here, and it will cause problems incase a cart MUST be detached before attaching another, or even itself. (eg for initialization reasons) most obvious reason: attaching a different ROM (software) for the same cartridge (hardware) */ slotmain = cart_is_slotmain(carttype); if (slotmain) { /* if the cart to be attached is in the "Main Slot", detach whatever cart currently is in the "Main Slot" */ oldmain = cart_getid_slotmain(); if (oldmain != CARTRIDGE_NONE) { DBG(("CART: detach slot main ID: %d\n", oldmain)); cartridge_detach_image(oldmain); } } if (oldmain != carttype) { DBG(("CART: detach %s ID: %d\n", slotmain ? "slot main" : "other slot", carttype)); cartridge_detach_image(carttype); } if (type == CARTRIDGE_CRT) { DBG(("CART: attach CRT ID: %d '%s'\n", carttype, filename)); cartid = crt_attach(abs_filename, rawcart); if (cartid == CARTRIDGE_NONE) { goto exiterror; } if (type < 0) { DBG(("CART: attach generic CRT ID: %d\n", type)); } } else { DBG(("CART: attach BIN ID: %d '%s'\n", carttype, filename)); cartid = carttype; if (cart_bin_attach(carttype, abs_filename, rawcart) < 0) { goto exiterror; } } if (cart_is_slotmain(cartid)) { DBG(("cartridge_attach MAIN ID: %d\n", cartid)); mem_cartridge_type = cartid; cart_romhbank_set_slotmain(0); cart_romlbank_set_slotmain(0); } else { DBG(("cartridge_attach (other) ID: %d\n", cartid)); } DBG(("CART: attach RAW ID: %d\n", cartid)); cart_attach(cartid, rawcart); cart_power_off(); if (cart_is_slotmain(cartid)) { /* "Main Slot" */ DBG(("CART: set main slot ID: %d type: %d\n", carttype, type)); c64cart_type = type; if (type == CARTRIDGE_CRT) { crttype = carttype; } util_string_set(&cartfile, abs_filename); } DBG(("CART: cartridge_attach_image type: %d ID: %d done.\n", type, carttype)); lib_free(rawcart); log_message(LOG_DEFAULT, "CART: attached '%s' as ID %d.", abs_filename, carttype); lib_free(abs_filename); return 0; exiterror: DBG(("CART: error\n")); lib_free(rawcart); log_message(LOG_DEFAULT, "CART: could not attach '%s'.", abs_filename); lib_free(abs_filename); return -1; }