/* Load a snap to start the current tape autoloading */ static int tape_autoload( libspectrum_machine hardware ) { int error; const char *id; compat_fd fd; char filename[80]; utils_file snap; libspectrum_id_t type; id = machine_get_id( hardware ); if( !id ) { ui_error( UI_ERROR_ERROR, "Unknown machine type %d!", hardware ); return 1; } /* Look for an autoload snap. Try .szx first, then .z80 */ type = LIBSPECTRUM_ID_SNAPSHOT_SZX; snprintf( filename, sizeof(filename), "tape_%s.szx", id ); fd = utils_find_auxiliary_file( filename, UTILS_AUXILIARY_LIB ); if( fd == COMPAT_FILE_OPEN_FAILED ) { type = LIBSPECTRUM_ID_SNAPSHOT_Z80; snprintf( filename, sizeof(filename), "tape_%s.z80", id ); fd = utils_find_auxiliary_file( filename, UTILS_AUXILIARY_LIB ); } /* If we couldn't find either, give up */ if( fd == COMPAT_FILE_OPEN_FAILED ) { ui_error( UI_ERROR_ERROR, "Couldn't find autoload snap for machine type '%s'", id ); return 1; } error = utils_read_fd( fd, filename, &snap ); if( error ) return error; error = snapshot_read_buffer( snap.buffer, snap.length, type ); if( error ) { utils_close_file( &snap ); return error; } if( utils_close_file( &snap ) ) { ui_error( UI_ERROR_ERROR, "Couldn't close '%s': %s", filename, strerror( errno ) ); return 1; } return 0; }
/* Open `filename' and do something sensible with it; autoload tapes if `autoload' is true and return the type of file found in `type' */ int utils_open_file( const char *filename, int autoload, libspectrum_id_t *type_ptr) { utils_file file; libspectrum_id_t type; libspectrum_class_t class; int error; error = 0; if( rzx_recording ) error = rzx_stop_recording(); if( rzx_playback ) error = rzx_stop_playback( 1 ); if( error ) return error; /* Read the file into a buffer */ if( utils_read_file( filename, &file ) ) return 1; /* See if we can work out what it is */ if( libspectrum_identify_file_with_class( &type, &class, filename, file.buffer, file.length ) ) { utils_close_file( &file ); return 1; } switch( class ) { case LIBSPECTRUM_CLASS_UNKNOWN: ui_error( UI_ERROR_ERROR, "utils_open_file: couldn't identify `%s'", filename ); utils_close_file( &file ); return 1; case LIBSPECTRUM_CLASS_RECORDING: error = rzx_start_playback_from_buffer( file.buffer, file.length ); break; case LIBSPECTRUM_CLASS_SNAPSHOT: error = snapshot_read_buffer( file.buffer, file.length, type ); pokemem_find_pokfile( filename ); break; case LIBSPECTRUM_CLASS_TAPE: error = tape_read_buffer( file.buffer, file.length, type, filename, autoload ); pokemem_find_pokfile( filename ); break; case LIBSPECTRUM_CLASS_DISK_PLUS3: if( !( machine_current->capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_DISK ) ) { error = machine_select( LIBSPECTRUM_MACHINE_PLUS3 ); if( error ) break; } error = specplus3_disk_insert( SPECPLUS3_DRIVE_A, filename, autoload ); break; case LIBSPECTRUM_CLASS_DISK_DIDAKTIK: error = didaktik80_disk_insert( DIDAKTIK80_DRIVE_A, filename, autoload ); break; case LIBSPECTRUM_CLASS_DISK_PLUSD: if( periph_is_active( PERIPH_TYPE_DISCIPLE ) ) error = disciple_disk_insert( DISCIPLE_DRIVE_1, filename, autoload ); else error = plusd_disk_insert( PLUSD_DRIVE_1, filename, autoload ); break; case LIBSPECTRUM_CLASS_DISK_OPUS: error = opus_disk_insert( OPUS_DRIVE_1, filename, autoload ); break; case LIBSPECTRUM_CLASS_DISK_TRDOS: if( !( machine_current->capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TRDOS_DISK ) && !periph_is_active( PERIPH_TYPE_BETA128 ) ) { error = machine_select( LIBSPECTRUM_MACHINE_PENT ); if( error ) break; } /* Check that we actually got a Beta capable machine to insert the disk */ if( ( machine_current->capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TRDOS_DISK ) || periph_is_active( PERIPH_TYPE_BETA128 ) ) { error = beta_disk_insert( BETA_DRIVE_A, filename, autoload ); } break; case LIBSPECTRUM_CLASS_DISK_GENERIC: if( machine_current->machine == LIBSPECTRUM_MACHINE_PLUS3 || machine_current->machine == LIBSPECTRUM_MACHINE_PLUS2A ) error = specplus3_disk_insert( SPECPLUS3_DRIVE_A, filename, autoload ); else if( machine_current->machine == LIBSPECTRUM_MACHINE_PENT || machine_current->machine == LIBSPECTRUM_MACHINE_PENT512 || machine_current->machine == LIBSPECTRUM_MACHINE_PENT1024 || machine_current->machine == LIBSPECTRUM_MACHINE_SCORP ) error = beta_disk_insert( BETA_DRIVE_A, filename, autoload ); else if( periph_is_active( PERIPH_TYPE_BETA128 ) ) error = beta_disk_insert( BETA_DRIVE_A, filename, autoload ); else if( periph_is_active( PERIPH_TYPE_DISCIPLE ) ) error = disciple_disk_insert( DISCIPLE_DRIVE_1, filename, autoload ); else if( periph_is_active( PERIPH_TYPE_PLUSD ) ) error = plusd_disk_insert( PLUSD_DRIVE_1, filename, autoload ); break; case LIBSPECTRUM_CLASS_CARTRIDGE_IF2: error = if2_insert( filename ); break; case LIBSPECTRUM_CLASS_MICRODRIVE: error = if1_mdr_insert( -1, filename ); break; case LIBSPECTRUM_CLASS_CARTRIDGE_TIMEX: if( !( machine_current->capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_DOCK ) ) { error = machine_select( LIBSPECTRUM_MACHINE_TC2068 ); if( error ) break; } /* Check that we actually got a Dock capable machine to insert the cart */ if( machine_current->capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_DOCK ) { error = dck_insert( filename ); } break; case LIBSPECTRUM_CLASS_HARDDISK: if( !settings_current.simpleide_active && !settings_current.zxatasp_active && !settings_current.divide_enabled && !settings_current.zxcf_active ) { settings_current.zxcf_active = 1; periph_update(); } if( settings_current.zxcf_active ) { error = zxcf_insert( filename ); } else if( settings_current.zxatasp_active ) { error = zxatasp_insert( filename, LIBSPECTRUM_IDE_MASTER ); } else if( settings_current.simpleide_active ) { error = simpleide_insert( filename, LIBSPECTRUM_IDE_MASTER ); } else { error = divide_insert( filename, LIBSPECTRUM_IDE_MASTER ); } if( error ) return error; break; case LIBSPECTRUM_CLASS_AUXILIARY: if( type == LIBSPECTRUM_ID_AUX_POK ) { ui_pokemem_selector( filename ); } break; default: ui_error( UI_ERROR_ERROR, "utils_open_file: unknown class %d", type ); error = 1; break; } if( error ) { utils_close_file( &file ); return error; } utils_close_file( &file ); if( type_ptr ) *type_ptr = type; return 0; }