src/mess/devices/mflopimg.c
| r5704 | r5705 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | mflopimg.c |
| 4 | | |
| 5 | | MESS interface to the floppy disk image abstraction code |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include "mame.h" |
| 10 | | #include "mflopimg.h" |
| 11 | | #include "flopdrv.h" |
| 12 | | #include "formats/flopimg.h" |
| 13 | | |
| 14 | | |
| 15 | | |
| 16 | | /*************************************************************************** |
| 17 | | CONSTANTS |
| 18 | | ***************************************************************************/ |
| 19 | | |
| 20 | | #define FLOPPY_TAG "floptag" |
| 21 | | #define LOG_FLOPPY 0 |
| 22 | | |
| 23 | | |
| 24 | | |
| 25 | | /*************************************************************************** |
| 26 | | TYPE DEFINITIONS |
| 27 | | ***************************************************************************/ |
| 28 | | |
| 29 | | typedef struct _mess_flopimg mess_flopimg; |
| 30 | | struct _mess_flopimg |
| 31 | | { |
| 32 | | floppy_image *floppy; |
| 33 | | int track; |
| 34 | | void (*load_proc)(const device_config *image); |
| 35 | | void (*unload_proc)(const device_config *image); |
| 36 | | int (*tracktranslate_proc)(const device_config *image, floppy_image *floppy, int physical_track); |
| 37 | | }; |
| 38 | | |
| 39 | | |
| 40 | | |
| 41 | | typedef struct _floppy_error_map floppy_error_map; |
| 42 | | struct _floppy_error_map |
| 43 | | { |
| 44 | | floperr_t ferr; |
| 45 | | image_error_t ierr; |
| 46 | | const char *message; |
| 47 | | }; |
| 48 | | |
| 49 | | |
| 50 | | |
| 51 | | /*************************************************************************** |
| 52 | | GLOBAL VARIABLES |
| 53 | | ***************************************************************************/ |
| 54 | | |
| 55 | | static const floppy_error_map errmap[] = |
| 56 | | { |
| 57 | | { FLOPPY_ERROR_SUCCESS, IMAGE_ERROR_SUCCESS }, |
| 58 | | { FLOPPY_ERROR_INTERNAL, IMAGE_ERROR_INTERNAL }, |
| 59 | | { FLOPPY_ERROR_UNSUPPORTED, IMAGE_ERROR_UNSUPPORTED }, |
| 60 | | { FLOPPY_ERROR_OUTOFMEMORY, IMAGE_ERROR_OUTOFMEMORY }, |
| 61 | | { FLOPPY_ERROR_INVALIDIMAGE, IMAGE_ERROR_INVALIDIMAGE } |
| 62 | | }; |
| 63 | | |
| 64 | | /*************************************************************************** |
| 65 | | IMPLEMENTATION |
| 66 | | ***************************************************************************/ |
| 67 | | |
| 68 | | static mess_flopimg *get_flopimg(const device_config *image) |
| 69 | | { |
| 70 | | return (mess_flopimg *) image_lookuptag(image, FLOPPY_TAG); |
| 71 | | } |
| 72 | | |
| 73 | | |
| 74 | | |
| 75 | | floppy_image *flopimg_get_image(const device_config *image) |
| 76 | | { |
| 77 | | return get_flopimg(image)->floppy; |
| 78 | | } |
| 79 | | |
| 80 | | |
| 81 | | |
| 82 | | static void flopimg_seek_callback(const device_config *image, int physical_track) |
| 83 | | { |
| 84 | | mess_flopimg *flopimg; |
| 85 | | |
| 86 | | flopimg = get_flopimg(image); |
| 87 | | if (!flopimg || !flopimg->floppy) |
| 88 | | return; |
| 89 | | |
| 90 | | /* translate the track number if necessary */ |
| 91 | | if (flopimg->tracktranslate_proc) |
| 92 | | physical_track = flopimg->tracktranslate_proc(image, flopimg->floppy, physical_track); |
| 93 | | |
| 94 | | flopimg->track = physical_track; |
| 95 | | } |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | static int flopimg_get_sectors_per_track(const device_config *image, int side) |
| 100 | | { |
| 101 | | mess_flopimg *flopimg; |
| 102 | | floperr_t err; |
| 103 | | int sector_count; |
| 104 | | |
| 105 | | flopimg = get_flopimg(image); |
| 106 | | if (!flopimg || !flopimg->floppy) |
| 107 | | return 0; |
| 108 | | |
| 109 | | err = floppy_get_sector_count(flopimg->floppy, side, flopimg->track, §or_count); |
| 110 | | if (err) |
| 111 | | return 0; |
| 112 | | return sector_count; |
| 113 | | } |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | static void flopimg_get_id_callback(const device_config *image, chrn_id *id, int id_index, int side) |
| 118 | | { |
| 119 | | mess_flopimg *flopimg; |
| 120 | | int cylinder, sector, N; |
| 121 | | unsigned long flags; |
| 122 | | UINT32 sector_length; |
| 123 | | |
| 124 | | flopimg = get_flopimg(image); |
| 125 | | if (!flopimg || !flopimg->floppy) |
| 126 | | return; |
| 127 | | |
| 128 | | floppy_get_indexed_sector_info(flopimg->floppy, side, flopimg->track, id_index, &cylinder, &side, §or, §or_length, &flags); |
| 129 | | |
| 130 | | N = compute_log2(sector_length); |
| 131 | | |
| 132 | | id->C = cylinder; |
| 133 | | id->H = side; |
| 134 | | id->R = sector; |
| 135 | | id->data_id = id_index; |
| 136 | | id->flags = flags; |
| 137 | | id->N = ((N >= 7) && (N <= 10)) ? N - 7 : 0; |
| 138 | | } |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | static void log_readwrite(const char *name, int head, int track, int sector, const char *buf, int length) |
| 143 | | { |
| 144 | | char membuf[1024]; |
| 145 | | int i; |
| 146 | | for (i = 0; i < length; i++) |
| 147 | | sprintf(membuf + i*2, "%02x", (int) (UINT8) buf[i]); |
| 148 | | logerror("%s: head=%i track=%i sector=%i buffer='%s'\n", name, head, track, sector, membuf); |
| 149 | | } |
| 150 | | |
| 151 | | |
| 152 | | |
| 153 | | static void flopimg_read_sector_data_into_buffer(const device_config *image, int side, int index1, char *ptr, int length) |
| 154 | | { |
| 155 | | mess_flopimg *flopimg; |
| 156 | | |
| 157 | | flopimg = get_flopimg(image); |
| 158 | | if (!flopimg || !flopimg->floppy) |
| 159 | | return; |
| 160 | | |
| 161 | | floppy_read_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length); |
| 162 | | |
| 163 | | if (LOG_FLOPPY) |
| 164 | | log_readwrite("sector_read", side, flopimg->track, index1, ptr, length); |
| 165 | | } |
| 166 | | |
| 167 | | |
| 168 | | |
| 169 | | static void flopimg_write_sector_data_from_buffer(const device_config *image, int side, int index1, const char *ptr, int length, int ddam) |
| 170 | | { |
| 171 | | mess_flopimg *flopimg; |
| 172 | | |
| 173 | | flopimg = get_flopimg(image); |
| 174 | | if (!flopimg || !flopimg->floppy) |
| 175 | | return; |
| 176 | | |
| 177 | | if (LOG_FLOPPY) |
| 178 | | log_readwrite("sector_write", side, flopimg->track, index1, ptr, length); |
| 179 | | |
| 180 | | floppy_write_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length, ddam); |
| 181 | | } |
| 182 | | |
| 183 | | |
| 184 | | |
| 185 | | static void flopimg_read_track_data_info_buffer(const device_config *image, int side, void *ptr, int *length) |
| 186 | | { |
| 187 | | mess_flopimg *flopimg; |
| 188 | | |
| 189 | | flopimg = get_flopimg(image); |
| 190 | | if (!flopimg || !flopimg->floppy) |
| 191 | | return; |
| 192 | | |
| 193 | | floppy_read_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 194 | | } |
| 195 | | |
| 196 | | |
| 197 | | |
| 198 | | static void flopimg_write_track_data_info_buffer(const device_config *image, int side, const void *ptr, int *length) |
| 199 | | { |
| 200 | | mess_flopimg *flopimg; |
| 201 | | |
| 202 | | flopimg = get_flopimg(image); |
| 203 | | if (!flopimg || !flopimg->floppy) |
| 204 | | return; |
| 205 | | |
| 206 | | floppy_write_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 207 | | } |
| 208 | | |
| 209 | | |
| 210 | | |
| 211 | | static const floppy_interface mess_floppy_interface = |
| 212 | | { |
| 213 | | flopimg_seek_callback, |
| 214 | | flopimg_get_sectors_per_track, |
| 215 | | flopimg_get_id_callback, |
| 216 | | flopimg_read_sector_data_into_buffer, |
| 217 | | flopimg_write_sector_data_from_buffer, |
| 218 | | flopimg_read_track_data_info_buffer, |
| 219 | | flopimg_write_track_data_info_buffer, |
| 220 | | NULL |
| 221 | | }; |
| 222 | | |
| 223 | | /* ----------------------------------------------------------------------- */ |
| 224 | | |
| 225 | | static int image_fseek_thunk(void *file, INT64 offset, int whence) |
| 226 | | { |
| 227 | | return image_fseek((const device_config *) file, offset, whence); |
| 228 | | } |
| 229 | | |
| 230 | | static size_t image_fread_thunk(void *file, void *buffer, size_t length) |
| 231 | | { |
| 232 | | return image_fread((const device_config *) file, buffer, length); |
| 233 | | } |
| 234 | | |
| 235 | | static size_t image_fwrite_thunk(void *file, const void *buffer, size_t length) |
| 236 | | { |
| 237 | | return image_fwrite((const device_config *) file, buffer, length); |
| 238 | | } |
| 239 | | |
| 240 | | static UINT64 image_fsize_thunk(void *file) |
| 241 | | { |
| 242 | | return image_length((const device_config *) file); |
| 243 | | } |
| 244 | | |
| 245 | | /* ----------------------------------------------------------------------- */ |
| 246 | | |
| 247 | | const struct io_procs mess_ioprocs = |
| 248 | | { |
| 249 | | NULL, |
| 250 | | image_fseek_thunk, |
| 251 | | image_fread_thunk, |
| 252 | | image_fwrite_thunk, |
| 253 | | image_fsize_thunk |
| 254 | | }; |
| 255 | | |
| 256 | | |
| 257 | | |
| 258 | | |
| 259 | | /* ----------------------------------------------------------------------- */ |
| 260 | | |
| 261 | | |
| 262 | | static DEVICE_START( floppy ) |
| 263 | | { |
| 264 | | image_alloctag(device, FLOPPY_TAG, sizeof(mess_flopimg)); |
| 265 | | floppy_drive_init(device, &mess_floppy_interface); |
| 266 | | } |
| 267 | | |
| 268 | | |
| 269 | | |
| 270 | | static int internal_floppy_device_load(const device_config *image, int create_format, option_resolution *create_args) |
| 271 | | { |
| 272 | | floperr_t err; |
| 273 | | mess_flopimg *flopimg; |
| 274 | | const mess_device_class *devclass; |
| 275 | | const struct FloppyFormat *floppy_options; |
| 276 | | int floppy_flags, i; |
| 277 | | const char *extension; |
| 278 | | int keep_geometry = 0; |
| 279 | | |
| 280 | | /* look up instance data */ |
| 281 | | flopimg = get_flopimg(image); |
| 282 | | |
| 283 | | /* figure out the floppy options */ |
| 284 | | devclass = mess_devclass_from_core_device(image); |
| 285 | | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 286 | | |
| 287 | | if (image_has_been_created(image)) |
| 288 | | { |
| 289 | | /* creating an image */ |
| 290 | | assert(create_format >= 0); |
| 291 | | err = floppy_create((void *) image, &mess_ioprocs, &floppy_options[create_format], create_args, &flopimg->floppy); |
| 292 | | if (err) |
| 293 | | goto error; |
| 294 | | } |
| 295 | | else |
| 296 | | { |
| 297 | | /* opening an image */ |
| 298 | | floppy_flags = image_is_writable(image) ? FLOPPY_FLAGS_READWRITE : FLOPPY_FLAGS_READONLY; |
| 299 | | extension = image_filetype(image); |
| 300 | | err = floppy_open_choices((void *) image, &mess_ioprocs, extension, floppy_options, floppy_flags, &flopimg->floppy); |
| 301 | | if (err) |
| 302 | | goto error; |
| 303 | | } |
| 304 | | |
| 305 | | /* if we can get head and track counts, then set the geometry accordingly |
| 306 | | However, at least the ti99 system family requires that medium track |
| 307 | | count and drive track count be handled separately. |
| 308 | | It is possible to insert a 40 track medium in an 80 track drive; the |
| 309 | | TI controllers read the track count from sector 0 and automatically |
| 310 | | apply double steps. Setting the track count of the drive to the |
| 311 | | medium track count will then lead to unreachable tracks. |
| 312 | | */ |
| 313 | | keep_geometry = (int)mess_device_get_info_int(mess_devclass_from_core_device(image), MESS_DEVINFO_INT_KEEP_DRIVE_GEOMETRY); |
| 314 | | |
| 315 | | if (!keep_geometry |
| 316 | | && floppy_callbacks(flopimg->floppy)->get_heads_per_disk |
| 317 | | && floppy_callbacks(flopimg->floppy)->get_tracks_per_disk) |
| 318 | | { |
| 319 | | floppy_drive_set_geometry_absolute(image, |
| 320 | | floppy_get_tracks_per_disk(flopimg->floppy), |
| 321 | | floppy_get_heads_per_disk(flopimg->floppy)); |
| 322 | | } |
| 323 | | return INIT_PASS; |
| 324 | | |
| 325 | | error: |
| 326 | | for (i = 0; i < sizeof(errmap) / sizeof(errmap[0]); i++) |
| 327 | | { |
| 328 | | if (err == errmap[i].ferr) |
| 329 | | image_seterror(image, errmap[i].ierr, errmap[i].message); |
| 330 | | } |
| 331 | | return INIT_FAIL; |
| 332 | | } |
| 333 | | |
| 334 | | |
| 335 | | |
| 336 | | static DEVICE_IMAGE_LOAD( floppy ) |
| 337 | | { |
| 338 | | mess_flopimg *flopimg; |
| 339 | | int retVal = internal_floppy_device_load(image, -1, NULL); |
| 340 | | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 341 | | if (retVal==INIT_PASS) { |
| 342 | | /* if we have one of our hacky unload procs, call it */ |
| 343 | | if (flopimg->load_proc) |
| 344 | | flopimg->load_proc(image); |
| 345 | | } |
| 346 | | return retVal; |
| 347 | | } |
| 348 | | |
| 349 | | |
| 350 | | |
| 351 | | static DEVICE_IMAGE_CREATE( floppy ) |
| 352 | | { |
| 353 | | return internal_floppy_device_load(image, create_format, create_args); |
| 354 | | } |
| 355 | | |
| 356 | | |
| 357 | | |
| 358 | | static DEVICE_IMAGE_UNLOAD( floppy ) |
| 359 | | { |
| 360 | | mess_flopimg *flopimg; |
| 361 | | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 362 | | |
| 363 | | /* if we have one of our hacky unload procs, call it */ |
| 364 | | if (flopimg->unload_proc) |
| 365 | | flopimg->unload_proc(image); |
| 366 | | |
| 367 | | floppy_close(flopimg->floppy); |
| 368 | | flopimg->floppy = NULL; |
| 369 | | } |
| 370 | | |
| 371 | | |
| 372 | | |
| 373 | | /************************************* |
| 374 | | * |
| 375 | | * Hacks for specific systems |
| 376 | | * |
| 377 | | *************************************/ |
| 378 | | |
| 379 | | void floppy_install_load_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 380 | | { |
| 381 | | mess_flopimg *flopimg; |
| 382 | | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 383 | | flopimg->load_proc = proc; |
| 384 | | } |
| 385 | | |
| 386 | | void floppy_install_unload_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 387 | | { |
| 388 | | mess_flopimg *flopimg; |
| 389 | | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 390 | | flopimg->unload_proc = proc; |
| 391 | | } |
| 392 | | |
| 393 | | |
| 394 | | |
| 395 | | void floppy_install_tracktranslate_proc(const device_config *image, int (*proc)(const device_config *image, floppy_image *floppy, int physical_track)) |
| 396 | | { |
| 397 | | mess_flopimg *flopimg; |
| 398 | | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 399 | | flopimg->tracktranslate_proc = proc; |
| 400 | | } |
| 401 | | |
| 402 | | /************************************* |
| 403 | | * |
| 404 | | * Device specification function |
| 405 | | * |
| 406 | | *************************************/ |
| 407 | | |
| 408 | | void floppy_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 409 | | { |
| 410 | | char *s; |
| 411 | | int i, count; |
| 412 | | const struct FloppyFormat *floppy_options; |
| 413 | | |
| 414 | | switch(state) |
| 415 | | { |
| 416 | | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 417 | | case MESS_DEVINFO_INT_TYPE: info->i = IO_FLOPPY; break; |
| 418 | | case MESS_DEVINFO_INT_READABLE: info->i = 1; break; |
| 419 | | case MESS_DEVINFO_INT_WRITEABLE: info->i = 1; break; |
| 420 | | case MESS_DEVINFO_INT_CREATABLE: |
| 421 | | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 422 | | info->i = floppy_options->param_guidelines ? 1 : 0; |
| 423 | | break; |
| 424 | | |
| 425 | | case MESS_DEVINFO_INT_CREATE_OPTCOUNT: |
| 426 | | /* count total floppy options */ |
| 427 | | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 428 | | for (count = 0; floppy_options[count].construct; count++) |
| 429 | | ; |
| 430 | | info->i = count; |
| 431 | | break; |
| 432 | | |
| 433 | | case MESS_DEVINFO_INT_KEEP_DRIVE_GEOMETRY: |
| 434 | | info->i = 0; |
| 435 | | break; |
| 436 | | |
| 437 | | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 438 | | case MESS_DEVINFO_STR_FILE_EXTENSIONS: |
| 439 | | /* retrieve the floppy options */ |
| 440 | | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 441 | | |
| 442 | | /* set up a temporary string */ |
| 443 | | s = device_temp_str(); |
| 444 | | info->s = s; |
| 445 | | s[0] = '\0'; |
| 446 | | |
| 447 | | /* append each of the extensions */ |
| 448 | | for (i = 0; floppy_options[i].construct; i++) |
| 449 | | specify_extension(s, 256, floppy_options[i].extensions); |
| 450 | | break; |
| 451 | | |
| 452 | | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 453 | | case MESS_DEVINFO_PTR_START: info->start = DEVICE_START_NAME(floppy); break; |
| 454 | | case MESS_DEVINFO_PTR_LOAD: info->load = DEVICE_IMAGE_LOAD_NAME(floppy); break; |
| 455 | | case MESS_DEVINFO_PTR_CREATE: info->create = DEVICE_IMAGE_CREATE_NAME(floppy); break; |
| 456 | | case MESS_DEVINFO_PTR_UNLOAD: info->unload = DEVICE_IMAGE_UNLOAD_NAME(floppy); break; |
| 457 | | case MESS_DEVINFO_PTR_CREATE_OPTGUIDE: info->p = (void *) floppy_option_guide; break; |
| 458 | | |
| 459 | | default: |
| 460 | | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 461 | | if ((state >= MESS_DEVINFO_STR_CREATE_OPTNAME) && (state < MESS_DEVINFO_STR_CREATE_OPTNAME + DEVINFO_CREATE_OPTMAX)) |
| 462 | | { |
| 463 | | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTNAME].name; |
| 464 | | } |
| 465 | | else if ((state >= MESS_DEVINFO_STR_CREATE_OPTDESC) && (state < MESS_DEVINFO_STR_CREATE_OPTDESC + DEVINFO_CREATE_OPTMAX)) |
| 466 | | { |
| 467 | | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTDESC].description; |
| 468 | | } |
| 469 | | else if ((state >= MESS_DEVINFO_STR_CREATE_OPTEXTS) && (state < MESS_DEVINFO_STR_CREATE_OPTEXTS + DEVINFO_CREATE_OPTMAX)) |
| 470 | | { |
| 471 | | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTEXTS].extensions; |
| 472 | | } |
| 473 | | else if ((state >= MESS_DEVINFO_PTR_CREATE_OPTSPEC) && (state < MESS_DEVINFO_PTR_CREATE_OPTSPEC + DEVINFO_CREATE_OPTMAX)) |
| 474 | | { |
| 475 | | info->p = (void *) floppy_options[state - MESS_DEVINFO_PTR_CREATE_OPTSPEC].param_guidelines; |
| 476 | | } |
| 477 | | break; |
| 478 | | } |
| 479 | | } |
| 480 | | |
src/mess/devices/flopdrv.c
| r5704 | r5705 | |
| 519 | 519 | drv->controller = controller; |
| 520 | 520 | } |
| 521 | 521 | |
| 522 | |
| 523 | |
| 524 | /*************************************************************************** |
| 525 | CONSTANTS |
| 526 | ***************************************************************************/ |
| 527 | |
| 528 | #define FLOPPY_TAG "floptag" |
| 529 | #define LOG_FLOPPY 0 |
| 530 | |
| 531 | |
| 532 | |
| 533 | /*************************************************************************** |
| 534 | TYPE DEFINITIONS |
| 535 | ***************************************************************************/ |
| 536 | |
| 537 | typedef struct _mess_flopimg mess_flopimg; |
| 538 | struct _mess_flopimg |
| 539 | { |
| 540 | floppy_image *floppy; |
| 541 | int track; |
| 542 | void (*load_proc)(const device_config *image); |
| 543 | void (*unload_proc)(const device_config *image); |
| 544 | int (*tracktranslate_proc)(const device_config *image, floppy_image *floppy, int physical_track); |
| 545 | }; |
| 546 | |
| 547 | |
| 548 | |
| 549 | typedef struct _floppy_error_map floppy_error_map; |
| 550 | struct _floppy_error_map |
| 551 | { |
| 552 | floperr_t ferr; |
| 553 | image_error_t ierr; |
| 554 | const char *message; |
| 555 | }; |
| 556 | |
| 557 | |
| 558 | |
| 559 | /*************************************************************************** |
| 560 | GLOBAL VARIABLES |
| 561 | ***************************************************************************/ |
| 562 | |
| 563 | static const floppy_error_map errmap[] = |
| 564 | { |
| 565 | { FLOPPY_ERROR_SUCCESS, IMAGE_ERROR_SUCCESS }, |
| 566 | { FLOPPY_ERROR_INTERNAL, IMAGE_ERROR_INTERNAL }, |
| 567 | { FLOPPY_ERROR_UNSUPPORTED, IMAGE_ERROR_UNSUPPORTED }, |
| 568 | { FLOPPY_ERROR_OUTOFMEMORY, IMAGE_ERROR_OUTOFMEMORY }, |
| 569 | { FLOPPY_ERROR_INVALIDIMAGE, IMAGE_ERROR_INVALIDIMAGE } |
| 570 | }; |
| 571 | |
| 572 | /*************************************************************************** |
| 573 | IMPLEMENTATION |
| 574 | ***************************************************************************/ |
| 575 | |
| 576 | static mess_flopimg *get_flopimg(const device_config *image) |
| 577 | { |
| 578 | return (mess_flopimg *) image_lookuptag(image, FLOPPY_TAG); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | |
| 583 | floppy_image *flopimg_get_image(const device_config *image) |
| 584 | { |
| 585 | return get_flopimg(image)->floppy; |
| 586 | } |
| 587 | |
| 588 | |
| 589 | |
| 590 | static void flopimg_seek_callback(const device_config *image, int physical_track) |
| 591 | { |
| 592 | mess_flopimg *flopimg; |
| 593 | |
| 594 | flopimg = get_flopimg(image); |
| 595 | if (!flopimg || !flopimg->floppy) |
| 596 | return; |
| 597 | |
| 598 | /* translate the track number if necessary */ |
| 599 | if (flopimg->tracktranslate_proc) |
| 600 | physical_track = flopimg->tracktranslate_proc(image, flopimg->floppy, physical_track); |
| 601 | |
| 602 | flopimg->track = physical_track; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | |
| 607 | static int flopimg_get_sectors_per_track(const device_config *image, int side) |
| 608 | { |
| 609 | mess_flopimg *flopimg; |
| 610 | floperr_t err; |
| 611 | int sector_count; |
| 612 | |
| 613 | flopimg = get_flopimg(image); |
| 614 | if (!flopimg || !flopimg->floppy) |
| 615 | return 0; |
| 616 | |
| 617 | err = floppy_get_sector_count(flopimg->floppy, side, flopimg->track, §or_count); |
| 618 | if (err) |
| 619 | return 0; |
| 620 | return sector_count; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | static void flopimg_get_id_callback(const device_config *image, chrn_id *id, int id_index, int side) |
| 626 | { |
| 627 | mess_flopimg *flopimg; |
| 628 | int cylinder, sector, N; |
| 629 | unsigned long flags; |
| 630 | UINT32 sector_length; |
| 631 | |
| 632 | flopimg = get_flopimg(image); |
| 633 | if (!flopimg || !flopimg->floppy) |
| 634 | return; |
| 635 | |
| 636 | floppy_get_indexed_sector_info(flopimg->floppy, side, flopimg->track, id_index, &cylinder, &side, §or, §or_length, &flags); |
| 637 | |
| 638 | N = compute_log2(sector_length); |
| 639 | |
| 640 | id->C = cylinder; |
| 641 | id->H = side; |
| 642 | id->R = sector; |
| 643 | id->data_id = id_index; |
| 644 | id->flags = flags; |
| 645 | id->N = ((N >= 7) && (N <= 10)) ? N - 7 : 0; |
| 646 | } |
| 647 | |
| 648 | |
| 649 | |
| 650 | static void log_readwrite(const char *name, int head, int track, int sector, const char *buf, int length) |
| 651 | { |
| 652 | char membuf[1024]; |
| 653 | int i; |
| 654 | for (i = 0; i < length; i++) |
| 655 | sprintf(membuf + i*2, "%02x", (int) (UINT8) buf[i]); |
| 656 | logerror("%s: head=%i track=%i sector=%i buffer='%s'\n", name, head, track, sector, membuf); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | |
| 661 | static void flopimg_read_sector_data_into_buffer(const device_config *image, int side, int index1, char *ptr, int length) |
| 662 | { |
| 663 | mess_flopimg *flopimg; |
| 664 | |
| 665 | flopimg = get_flopimg(image); |
| 666 | if (!flopimg || !flopimg->floppy) |
| 667 | return; |
| 668 | |
| 669 | floppy_read_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length); |
| 670 | |
| 671 | if (LOG_FLOPPY) |
| 672 | log_readwrite("sector_read", side, flopimg->track, index1, ptr, length); |
| 673 | } |
| 674 | |
| 675 | |
| 676 | |
| 677 | static void flopimg_write_sector_data_from_buffer(const device_config *image, int side, int index1, const char *ptr, int length, int ddam) |
| 678 | { |
| 679 | mess_flopimg *flopimg; |
| 680 | |
| 681 | flopimg = get_flopimg(image); |
| 682 | if (!flopimg || !flopimg->floppy) |
| 683 | return; |
| 684 | |
| 685 | if (LOG_FLOPPY) |
| 686 | log_readwrite("sector_write", side, flopimg->track, index1, ptr, length); |
| 687 | |
| 688 | floppy_write_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length, ddam); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | |
| 693 | static void flopimg_read_track_data_info_buffer(const device_config *image, int side, void *ptr, int *length) |
| 694 | { |
| 695 | mess_flopimg *flopimg; |
| 696 | |
| 697 | flopimg = get_flopimg(image); |
| 698 | if (!flopimg || !flopimg->floppy) |
| 699 | return; |
| 700 | |
| 701 | floppy_read_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 702 | } |
| 703 | |
| 704 | |
| 705 | |
| 706 | static void flopimg_write_track_data_info_buffer(const device_config *image, int side, const void *ptr, int *length) |
| 707 | { |
| 708 | mess_flopimg *flopimg; |
| 709 | |
| 710 | flopimg = get_flopimg(image); |
| 711 | if (!flopimg || !flopimg->floppy) |
| 712 | return; |
| 713 | |
| 714 | floppy_write_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 715 | } |
| 716 | |
| 717 | |
| 718 | |
| 719 | static const floppy_interface mess_floppy_interface = |
| 720 | { |
| 721 | flopimg_seek_callback, |
| 722 | flopimg_get_sectors_per_track, |
| 723 | flopimg_get_id_callback, |
| 724 | flopimg_read_sector_data_into_buffer, |
| 725 | flopimg_write_sector_data_from_buffer, |
| 726 | flopimg_read_track_data_info_buffer, |
| 727 | flopimg_write_track_data_info_buffer, |
| 728 | NULL |
| 729 | }; |
| 730 | |
| 731 | /* ----------------------------------------------------------------------- */ |
| 732 | |
| 733 | static int image_fseek_thunk(void *file, INT64 offset, int whence) |
| 734 | { |
| 735 | return image_fseek((const device_config *) file, offset, whence); |
| 736 | } |
| 737 | |
| 738 | static size_t image_fread_thunk(void *file, void *buffer, size_t length) |
| 739 | { |
| 740 | return image_fread((const device_config *) file, buffer, length); |
| 741 | } |
| 742 | |
| 743 | static size_t image_fwrite_thunk(void *file, const void *buffer, size_t length) |
| 744 | { |
| 745 | return image_fwrite((const device_config *) file, buffer, length); |
| 746 | } |
| 747 | |
| 748 | static UINT64 image_fsize_thunk(void *file) |
| 749 | { |
| 750 | return image_length((const device_config *) file); |
| 751 | } |
| 752 | |
| 753 | /* ----------------------------------------------------------------------- */ |
| 754 | |
| 755 | const struct io_procs mess_ioprocs = |
| 756 | { |
| 757 | NULL, |
| 758 | image_fseek_thunk, |
| 759 | image_fread_thunk, |
| 760 | image_fwrite_thunk, |
| 761 | image_fsize_thunk |
| 762 | }; |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | /* ----------------------------------------------------------------------- */ |
| 768 | |
| 769 | |
| 770 | static DEVICE_START( floppy ) |
| 771 | { |
| 772 | image_alloctag(device, FLOPPY_TAG, sizeof(mess_flopimg)); |
| 773 | floppy_drive_init(device, &mess_floppy_interface); |
| 774 | } |
| 775 | |
| 776 | |
| 777 | |
| 778 | static int internal_floppy_device_load(const device_config *image, int create_format, option_resolution *create_args) |
| 779 | { |
| 780 | floperr_t err; |
| 781 | mess_flopimg *flopimg; |
| 782 | const mess_device_class *devclass; |
| 783 | const struct FloppyFormat *floppy_options; |
| 784 | int floppy_flags, i; |
| 785 | const char *extension; |
| 786 | int keep_geometry = 0; |
| 787 | |
| 788 | /* look up instance data */ |
| 789 | flopimg = get_flopimg(image); |
| 790 | |
| 791 | /* figure out the floppy options */ |
| 792 | devclass = mess_devclass_from_core_device(image); |
| 793 | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 794 | |
| 795 | if (image_has_been_created(image)) |
| 796 | { |
| 797 | /* creating an image */ |
| 798 | assert(create_format >= 0); |
| 799 | err = floppy_create((void *) image, &mess_ioprocs, &floppy_options[create_format], create_args, &flopimg->floppy); |
| 800 | if (err) |
| 801 | goto error; |
| 802 | } |
| 803 | else |
| 804 | { |
| 805 | /* opening an image */ |
| 806 | floppy_flags = image_is_writable(image) ? FLOPPY_FLAGS_READWRITE : FLOPPY_FLAGS_READONLY; |
| 807 | extension = image_filetype(image); |
| 808 | err = floppy_open_choices((void *) image, &mess_ioprocs, extension, floppy_options, floppy_flags, &flopimg->floppy); |
| 809 | if (err) |
| 810 | goto error; |
| 811 | } |
| 812 | |
| 813 | /* if we can get head and track counts, then set the geometry accordingly |
| 814 | However, at least the ti99 system family requires that medium track |
| 815 | count and drive track count be handled separately. |
| 816 | It is possible to insert a 40 track medium in an 80 track drive; the |
| 817 | TI controllers read the track count from sector 0 and automatically |
| 818 | apply double steps. Setting the track count of the drive to the |
| 819 | medium track count will then lead to unreachable tracks. |
| 820 | */ |
| 821 | keep_geometry = (int)mess_device_get_info_int(mess_devclass_from_core_device(image), MESS_DEVINFO_INT_KEEP_DRIVE_GEOMETRY); |
| 822 | |
| 823 | if (!keep_geometry |
| 824 | && floppy_callbacks(flopimg->floppy)->get_heads_per_disk |
| 825 | && floppy_callbacks(flopimg->floppy)->get_tracks_per_disk) |
| 826 | { |
| 827 | floppy_drive_set_geometry_absolute(image, |
| 828 | floppy_get_tracks_per_disk(flopimg->floppy), |
| 829 | floppy_get_heads_per_disk(flopimg->floppy)); |
| 830 | } |
| 831 | return INIT_PASS; |
| 832 | |
| 833 | error: |
| 834 | for (i = 0; i < sizeof(errmap) / sizeof(errmap[0]); i++) |
| 835 | { |
| 836 | if (err == errmap[i].ferr) |
| 837 | image_seterror(image, errmap[i].ierr, errmap[i].message); |
| 838 | } |
| 839 | return INIT_FAIL; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | |
| 844 | static DEVICE_IMAGE_LOAD( floppy ) |
| 845 | { |
| 846 | mess_flopimg *flopimg; |
| 847 | int retVal = internal_floppy_device_load(image, -1, NULL); |
| 848 | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 849 | if (retVal==INIT_PASS) { |
| 850 | /* if we have one of our hacky unload procs, call it */ |
| 851 | if (flopimg->load_proc) |
| 852 | flopimg->load_proc(image); |
| 853 | } |
| 854 | return retVal; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | |
| 859 | static DEVICE_IMAGE_CREATE( floppy ) |
| 860 | { |
| 861 | return internal_floppy_device_load(image, create_format, create_args); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | |
| 866 | static DEVICE_IMAGE_UNLOAD( floppy ) |
| 867 | { |
| 868 | mess_flopimg *flopimg; |
| 869 | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 870 | |
| 871 | /* if we have one of our hacky unload procs, call it */ |
| 872 | if (flopimg->unload_proc) |
| 873 | flopimg->unload_proc(image); |
| 874 | |
| 875 | floppy_close(flopimg->floppy); |
| 876 | flopimg->floppy = NULL; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | |
| 881 | /************************************* |
| 882 | * |
| 883 | * Hacks for specific systems |
| 884 | * |
| 885 | *************************************/ |
| 886 | |
| 887 | void floppy_install_load_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 888 | { |
| 889 | mess_flopimg *flopimg; |
| 890 | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 891 | flopimg->load_proc = proc; |
| 892 | } |
| 893 | |
| 894 | void floppy_install_unload_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 895 | { |
| 896 | mess_flopimg *flopimg; |
| 897 | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 898 | flopimg->unload_proc = proc; |
| 899 | } |
| 900 | |
| 901 | |
| 902 | |
| 903 | void floppy_install_tracktranslate_proc(const device_config *image, int (*proc)(const device_config *image, floppy_image *floppy, int physical_track)) |
| 904 | { |
| 905 | mess_flopimg *flopimg; |
| 906 | flopimg = image_lookuptag(image, FLOPPY_TAG); |
| 907 | flopimg->tracktranslate_proc = proc; |
| 908 | } |
| 909 | |
| 910 | /************************************* |
| 911 | * |
| 912 | * Device specification function |
| 913 | * |
| 914 | *************************************/ |
| 915 | |
| 916 | void floppy_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 917 | { |
| 918 | char *s; |
| 919 | int i, count; |
| 920 | const struct FloppyFormat *floppy_options; |
| 921 | |
| 922 | switch(state) |
| 923 | { |
| 924 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 925 | case MESS_DEVINFO_INT_TYPE: info->i = IO_FLOPPY; break; |
| 926 | case MESS_DEVINFO_INT_READABLE: info->i = 1; break; |
| 927 | case MESS_DEVINFO_INT_WRITEABLE: info->i = 1; break; |
| 928 | case MESS_DEVINFO_INT_CREATABLE: |
| 929 | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 930 | info->i = floppy_options->param_guidelines ? 1 : 0; |
| 931 | break; |
| 932 | |
| 933 | case MESS_DEVINFO_INT_CREATE_OPTCOUNT: |
| 934 | /* count total floppy options */ |
| 935 | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 936 | for (count = 0; floppy_options[count].construct; count++) |
| 937 | ; |
| 938 | info->i = count; |
| 939 | break; |
| 940 | |
| 941 | case MESS_DEVINFO_INT_KEEP_DRIVE_GEOMETRY: |
| 942 | info->i = 0; |
| 943 | break; |
| 944 | |
| 945 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 946 | case MESS_DEVINFO_STR_FILE_EXTENSIONS: |
| 947 | /* retrieve the floppy options */ |
| 948 | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 949 | |
| 950 | /* set up a temporary string */ |
| 951 | s = device_temp_str(); |
| 952 | info->s = s; |
| 953 | s[0] = '\0'; |
| 954 | |
| 955 | /* append each of the extensions */ |
| 956 | for (i = 0; floppy_options[i].construct; i++) |
| 957 | specify_extension(s, 256, floppy_options[i].extensions); |
| 958 | break; |
| 959 | |
| 960 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 961 | case MESS_DEVINFO_PTR_START: info->start = DEVICE_START_NAME(floppy); break; |
| 962 | case MESS_DEVINFO_PTR_LOAD: info->load = DEVICE_IMAGE_LOAD_NAME(floppy); break; |
| 963 | case MESS_DEVINFO_PTR_CREATE: info->create = DEVICE_IMAGE_CREATE_NAME(floppy); break; |
| 964 | case MESS_DEVINFO_PTR_UNLOAD: info->unload = DEVICE_IMAGE_UNLOAD_NAME(floppy); break; |
| 965 | case MESS_DEVINFO_PTR_CREATE_OPTGUIDE: info->p = (void *) floppy_option_guide; break; |
| 966 | |
| 967 | default: |
| 968 | floppy_options = mess_device_get_info_ptr(devclass, MESS_DEVINFO_PTR_FLOPPY_OPTIONS); |
| 969 | if ((state >= MESS_DEVINFO_STR_CREATE_OPTNAME) && (state < MESS_DEVINFO_STR_CREATE_OPTNAME + DEVINFO_CREATE_OPTMAX)) |
| 970 | { |
| 971 | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTNAME].name; |
| 972 | } |
| 973 | else if ((state >= MESS_DEVINFO_STR_CREATE_OPTDESC) && (state < MESS_DEVINFO_STR_CREATE_OPTDESC + DEVINFO_CREATE_OPTMAX)) |
| 974 | { |
| 975 | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTDESC].description; |
| 976 | } |
| 977 | else if ((state >= MESS_DEVINFO_STR_CREATE_OPTEXTS) && (state < MESS_DEVINFO_STR_CREATE_OPTEXTS + DEVINFO_CREATE_OPTMAX)) |
| 978 | { |
| 979 | info->s = (void *) floppy_options[state - MESS_DEVINFO_STR_CREATE_OPTEXTS].extensions; |
| 980 | } |
| 981 | else if ((state >= MESS_DEVINFO_PTR_CREATE_OPTSPEC) && (state < MESS_DEVINFO_PTR_CREATE_OPTSPEC + DEVINFO_CREATE_OPTMAX)) |
| 982 | { |
| 983 | info->p = (void *) floppy_options[state - MESS_DEVINFO_PTR_CREATE_OPTSPEC].param_guidelines; |
| 984 | } |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | |