src/mess/devices/flopdrv.c
| r5708 | r5709 | |
| 1 | 1 | /* |
| 2 | | This code handles the floppy drives. |
| 3 | | All FDD actions should be performed using these functions. |
| 2 | This code handles the floppy drives. |
| 3 | All FDD actions should be performed using these functions. |
| 4 | 4 | |
| 5 | | The functions are emulated and a disk image is used. |
| 5 | The functions are emulated and a disk image is used. |
| 6 | 6 | |
| 7 | 7 | Real disk operation: |
| 8 | 8 | - set unit id |
| 9 | 9 | |
| 10 | 10 | TODO: |
| 11 | | - Disk change handling. |
| 12 | | - Override write protect if disk image has been opened in read mode |
| 11 | - Disk change handling. |
| 12 | - Override write protect if disk image has been opened in read mode |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | 15 | #include "driver.h" |
| r5708 | r5709 | |
| 17 | 17 | |
| 18 | 18 | #define VERBOSE 0 |
| 19 | 19 | #define LOG(x) do { if (VERBOSE) logerror x; } while (0) |
| 20 | | |
| 20 | |
| 21 | 21 | /*************************************************************************** |
| 22 | 22 | CONSTANTS |
| 23 | 23 | ***************************************************************************/ |
| r5708 | r5709 | |
| 39 | 39 | /* num sides */ |
| 40 | 40 | int num_sides; |
| 41 | 41 | /* current track - this may or may not relate to the present cylinder number |
| 42 | | stored by the fdc */ |
| 42 | stored by the fdc */ |
| 43 | 43 | int current_track; |
| 44 | 44 | |
| 45 | 45 | /* index pulse timer */ |
| r5708 | r5709 | |
| 91 | 91 | /*************************************************************************** |
| 92 | 92 | IMPLEMENTATION |
| 93 | 93 | ***************************************************************************/ |
| 94 | | |
| 95 | 94 | static floppy_drive *get_drive(const device_config *img) |
| 96 | 95 | { |
| 97 | 96 | return image_lookuptag(img, FLOPDRVTAG); |
| r5708 | r5709 | |
| 102 | 101 | return get_drive(image)->floppy; |
| 103 | 102 | } |
| 104 | 103 | |
| 105 | | |
| 106 | | |
| 107 | 104 | static void flopimg_seek_callback(const device_config *image, int physical_track) |
| 108 | 105 | { |
| 109 | | floppy_drive *flopimg; |
| 110 | | |
| 111 | | flopimg = get_drive(image); |
| 106 | floppy_drive *flopimg = get_drive(image); |
| 112 | 107 | if (!flopimg || !flopimg->floppy) |
| 113 | 108 | return; |
| 114 | 109 | |
| r5708 | r5709 | |
| 119 | 114 | flopimg->track = physical_track; |
| 120 | 115 | } |
| 121 | 116 | |
| 122 | | |
| 123 | | |
| 124 | 117 | static int flopimg_get_sectors_per_track(const device_config *image, int side) |
| 125 | 118 | { |
| 126 | | floppy_drive *flopimg; |
| 127 | 119 | floperr_t err; |
| 128 | 120 | int sector_count; |
| 121 | floppy_drive *flopimg = get_drive(image); |
| 129 | 122 | |
| 130 | | flopimg = get_drive(image); |
| 131 | 123 | if (!flopimg || !flopimg->floppy) |
| 132 | 124 | return 0; |
| 133 | 125 | |
| r5708 | r5709 | |
| 137 | 129 | return sector_count; |
| 138 | 130 | } |
| 139 | 131 | |
| 140 | | |
| 141 | | |
| 142 | 132 | static void flopimg_get_id_callback(const device_config *image, chrn_id *id, int id_index, int side) |
| 143 | 133 | { |
| 144 | 134 | floppy_drive *flopimg; |
| r5708 | r5709 | |
| 162 | 152 | id->N = ((N >= 7) && (N <= 10)) ? N - 7 : 0; |
| 163 | 153 | } |
| 164 | 154 | |
| 165 | | |
| 166 | | |
| 167 | 155 | static void log_readwrite(const char *name, int head, int track, int sector, const char *buf, int length) |
| 168 | 156 | { |
| 169 | 157 | char membuf[1024]; |
| r5708 | r5709 | |
| 206 | 194 | image_fsize_thunk |
| 207 | 195 | }; |
| 208 | 196 | |
| 209 | | /************************************* |
| 210 | | * |
| 211 | | * Hacks for specific systems |
| 212 | | * |
| 213 | | *************************************/ |
| 214 | 197 | |
| 215 | | void floppy_install_load_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 216 | | { |
| 217 | | floppy_drive *flopimg = get_drive(image); |
| 218 | | flopimg->load_proc = proc; |
| 219 | | } |
| 220 | | |
| 221 | | void floppy_install_unload_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 222 | | { |
| 223 | | floppy_drive *flopimg = get_drive(image); |
| 224 | | flopimg->unload_proc = proc; |
| 225 | | } |
| 226 | | |
| 227 | | |
| 228 | | |
| 229 | | void floppy_install_tracktranslate_proc(const device_config *image, int (*proc)(const device_config *image, floppy_image *floppy, int physical_track)) |
| 230 | | { |
| 231 | | floppy_drive *flopimg = get_drive(image); |
| 232 | | flopimg->tracktranslate_proc = proc; |
| 233 | | } |
| 234 | | |
| 235 | 198 | static TIMER_CALLBACK(floppy_drive_index_callback); |
| 236 | 199 | |
| 237 | 200 | /* this is called on device init */ |
| r5708 | r5709 | |
| 264 | 227 | pDrive->controller = NULL; |
| 265 | 228 | } |
| 266 | 229 | |
| 267 | | |
| 268 | | |
| 269 | 230 | /* index pulses at rpm/60 Hz, and stays high 1/20th of time */ |
| 270 | 231 | static void floppy_drive_index_func(const device_config *img) |
| 271 | 232 | { |
| r5708 | r5709 | |
| 288 | 249 | pDrive->index_pulse_callback(pDrive->controller, img, pDrive->index); |
| 289 | 250 | } |
| 290 | 251 | |
| 291 | | |
| 292 | | |
| 293 | 252 | static TIMER_CALLBACK(floppy_drive_index_callback) |
| 294 | 253 | { |
| 295 | 254 | const device_config *image = (const device_config *) ptr; |
| 296 | 255 | floppy_drive_index_func(image); |
| 297 | 256 | } |
| 298 | 257 | |
| 299 | | |
| 300 | | |
| 301 | | /* set the callback for the index pulse */ |
| 302 | | void floppy_drive_set_index_pulse_callback(const device_config *img, void (*callback)(const device_config *controller,const device_config *image, int state)) |
| 303 | | { |
| 304 | | floppy_drive *pDrive = get_drive(img); |
| 305 | | pDrive->index_pulse_callback = callback; |
| 306 | | } |
| 307 | | |
| 308 | | |
| 309 | | void floppy_drive_set_ready_state_change_callback(const device_config *img, void (*callback)(const device_config *controller,const device_config *img, int state)) |
| 310 | | { |
| 311 | | floppy_drive *pDrive = get_drive(img); |
| 312 | | pDrive->ready_state_change_callback = callback; |
| 313 | | } |
| 314 | | |
| 315 | 258 | /*************************************************************************/ |
| 316 | 259 | /* IO_FLOPPY device functions */ |
| 317 | 260 | |
| r5708 | r5709 | |
| 327 | 270 | if (new_status!=-1) |
| 328 | 271 | { |
| 329 | 272 | /* we don't set the flags directly. |
| 330 | | The flags are "cooked" when we do a floppy_drive_get_flag_state depending on |
| 331 | | if drive is connected etc. So if we wrote the flags back it would |
| 332 | | corrupt this information. Therefore we update the flags depending on new_status */ |
| 273 | The flags are "cooked" when we do a floppy_drive_get_flag_state depending on |
| 274 | if drive is connected etc. So if we wrote the flags back it would |
| 275 | corrupt this information. Therefore we update the flags depending on new_status */ |
| 333 | 276 | |
| 334 | 277 | floppy_drive_set_flag_state(img, FLOPPY_DRIVE_DISK_WRITE_PROTECTED, (new_status & FLOPPY_DRIVE_DISK_WRITE_PROTECTED)); |
| 335 | 278 | } |
| r5708 | r5709 | |
| 338 | 281 | return floppy_drive_get_flag_state(img,0x0ff); |
| 339 | 282 | } |
| 340 | 283 | |
| 341 | | |
| 342 | 284 | /* set flag state */ |
| 343 | 285 | void floppy_drive_set_flag_state(const device_config *img, int flag, int state) |
| 344 | 286 | { |
| r5708 | r5709 | |
| 432 | 374 | if (flag) |
| 433 | 375 | { |
| 434 | 376 | /* set ready only if drive is present, disk is in the drive, |
| 435 | | and disk motor is on - for Amstrad, Spectrum and PCW*/ |
| 377 | and disk motor is on - for Amstrad, Spectrum and PCW*/ |
| 436 | 378 | |
| 437 | 379 | /* drive present? */ |
| 438 | 380 | if (image_slotexists(img)) |
| r5708 | r5709 | |
| 442 | 384 | { |
| 443 | 385 | if (floppy_drive_get_flag_state(img, FLOPPY_DRIVE_MOTOR_ON)) |
| 444 | 386 | { |
| 445 | | |
| 446 | 387 | /* set state */ |
| 447 | 388 | floppy_drive_set_flag_state(img, FLOPPY_DRIVE_READY, state); |
| 448 | 389 | return; |
| 449 | 390 | } |
| 450 | 391 | } |
| 451 | 392 | } |
| 452 | | |
| 453 | 393 | floppy_drive_set_flag_state(img, FLOPPY_DRIVE_READY, 0); |
| 454 | 394 | } |
| 455 | 395 | else |
| r5708 | r5709 | |
| 457 | 397 | /* force ready state - for PC driver */ |
| 458 | 398 | floppy_drive_set_flag_state(img, FLOPPY_DRIVE_READY, state); |
| 459 | 399 | } |
| 460 | | |
| 461 | 400 | } |
| 462 | 401 | |
| 463 | | |
| 464 | 402 | /* get flag state */ |
| 465 | 403 | int floppy_drive_get_flag_state(const device_config *img, int flag) |
| 466 | 404 | { |
| r5708 | r5709 | |
| 524 | 462 | floppy_drive_set_geometry_absolute(img, max_track, num_sides); |
| 525 | 463 | } |
| 526 | 464 | |
| 527 | | void floppy_drive_set_geometry_absolute(const device_config *img, int tracks, int sides) |
| 528 | | { |
| 529 | | floppy_drive *pDrive = get_drive(img); |
| 530 | | pDrive->max_track = tracks; |
| 531 | | pDrive->num_sides = sides; |
| 532 | | } |
| 533 | | |
| 534 | 465 | void floppy_drive_seek(const device_config *img, signed int signed_tracks) |
| 535 | 466 | { |
| 536 | 467 | floppy_drive *pDrive; |
| r5708 | r5709 | |
| 604 | 535 | return (spt == 0) ? 0 : 1; |
| 605 | 536 | } |
| 606 | 537 | |
| 607 | | int floppy_drive_get_current_track(const device_config *img) |
| 608 | | { |
| 609 | | floppy_drive *drv = get_drive(img); |
| 610 | | return drv->current_track; |
| 611 | | } |
| 612 | | |
| 613 | 538 | void floppy_drive_read_track_data_info_buffer(const device_config *img, int side, void *ptr, int *length ) |
| 614 | 539 | { |
| 615 | 540 | floppy_drive *flopimg; |
| 616 | 541 | if (image_exists(img)) |
| 617 | | { |
| 542 | { |
| 618 | 543 | flopimg = get_drive(img); |
| 619 | 544 | if (!flopimg || !flopimg->floppy) |
| 620 | 545 | return; |
| 621 | | |
| 546 | |
| 622 | 547 | floppy_read_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 623 | 548 | } |
| 624 | 549 | } |
| r5708 | r5709 | |
| 631 | 556 | flopimg = get_drive(img); |
| 632 | 557 | if (!flopimg || !flopimg->floppy) |
| 633 | 558 | return; |
| 634 | | |
| 559 | |
| 635 | 560 | floppy_write_track_data(flopimg->floppy, side, flopimg->track, ptr, *length); |
| 636 | 561 | } |
| 637 | 562 | } |
| r5708 | r5709 | |
| 640 | 565 | { |
| 641 | 566 | if (image_exists(img)) |
| 642 | 567 | { |
| 643 | | /* if (drv->interface_.format_sector) |
| 644 | | drv->interface_.format_sector(img, side, sector_index,c, h, r, n, filler);*/ |
| 568 | /* if (drv->interface_.format_sector) |
| 569 | drv->interface_.format_sector(img, side, sector_index,c, h, r, n, filler);*/ |
| 645 | 570 | } |
| 646 | 571 | } |
| 647 | 572 | |
| r5708 | r5709 | |
| 653 | 578 | flopimg = get_drive(img); |
| 654 | 579 | if (!flopimg || !flopimg->floppy) |
| 655 | 580 | return; |
| 656 | | |
| 581 | |
| 657 | 582 | floppy_read_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length); |
| 658 | | |
| 583 | |
| 659 | 584 | if (LOG_FLOPPY) |
| 660 | 585 | log_readwrite("sector_read", side, flopimg->track, index1, ptr, length); |
| 661 | | |
| 586 | |
| 662 | 587 | } |
| 663 | 588 | } |
| 664 | 589 | |
| r5708 | r5709 | |
| 666 | 591 | { |
| 667 | 592 | floppy_drive *flopimg; |
| 668 | 593 | if (image_exists(img)) |
| 669 | | { |
| 594 | { |
| 670 | 595 | flopimg = get_drive(img); |
| 671 | 596 | if (!flopimg || !flopimg->floppy) |
| 672 | 597 | return; |
| 673 | | |
| 598 | |
| 674 | 599 | if (LOG_FLOPPY) |
| 675 | 600 | log_readwrite("sector_write", side, flopimg->track, index1, ptr, length); |
| 676 | | |
| 601 | |
| 677 | 602 | floppy_write_indexed_sector(flopimg->floppy, side, flopimg->track, index1, 0, ptr, length, ddam); |
| 678 | 603 | } |
| 679 | 604 | } |
| r5708 | r5709 | |
| 713 | 638 | return usecs; |
| 714 | 639 | } |
| 715 | 640 | |
| 641 | void floppy_install_load_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 642 | { |
| 643 | floppy_drive *flopimg = get_drive(image); |
| 644 | flopimg->load_proc = proc; |
| 645 | } |
| 716 | 646 | |
| 647 | void floppy_install_unload_proc(const device_config *image, void (*proc)(const device_config *image)) |
| 648 | { |
| 649 | floppy_drive *flopimg = get_drive(image); |
| 650 | flopimg->unload_proc = proc; |
| 651 | } |
| 717 | 652 | |
| 653 | void floppy_install_tracktranslate_proc(const device_config *image, int (*proc)(const device_config *image, floppy_image *floppy, int physical_track)) |
| 654 | { |
| 655 | floppy_drive *flopimg = get_drive(image); |
| 656 | flopimg->tracktranslate_proc = proc; |
| 657 | } |
| 658 | |
| 659 | /* set the callback for the index pulse */ |
| 660 | void floppy_drive_set_index_pulse_callback(const device_config *img, void (*callback)(const device_config *controller,const device_config *image, int state)) |
| 661 | { |
| 662 | floppy_drive *pDrive = get_drive(img); |
| 663 | pDrive->index_pulse_callback = callback; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | void floppy_drive_set_ready_state_change_callback(const device_config *img, void (*callback)(const device_config *controller,const device_config *img, int state)) |
| 668 | { |
| 669 | floppy_drive *pDrive = get_drive(img); |
| 670 | pDrive->ready_state_change_callback = callback; |
| 671 | } |
| 672 | |
| 673 | void floppy_drive_set_geometry_absolute(const device_config *img, int tracks, int sides) |
| 674 | { |
| 675 | floppy_drive *pDrive = get_drive(img); |
| 676 | pDrive->max_track = tracks; |
| 677 | pDrive->num_sides = sides; |
| 678 | } |
| 679 | |
| 680 | int floppy_drive_get_current_track(const device_config *img) |
| 681 | { |
| 682 | floppy_drive *drv = get_drive(img); |
| 683 | return drv->current_track; |
| 684 | } |
| 685 | |
| 718 | 686 | void floppy_drive_set_rpm(const device_config *img, float rpm) |
| 719 | 687 | { |
| 720 | 688 | floppy_drive *drv = get_drive(img); |
| r5708 | r5709 | |
| 728 | 696 | } |
| 729 | 697 | |
| 730 | 698 | /* ----------------------------------------------------------------------- */ |
| 731 | | |
| 732 | | |
| 733 | 699 | static DEVICE_START( floppy ) |
| 734 | 700 | { |
| 735 | 701 | floppy_drive_init(device); |
| 736 | 702 | } |
| 737 | 703 | |
| 738 | | |
| 739 | | |
| 740 | 704 | static int internal_floppy_device_load(const device_config *image, int create_format, option_resolution *create_args) |
| 741 | 705 | { |
| 742 | 706 | floperr_t err; |
| r5708 | r5709 | |
| 771 | 735 | if (err) |
| 772 | 736 | goto error; |
| 773 | 737 | } |
| 774 | | |
| 738 | |
| 775 | 739 | /* if we can get head and track counts, then set the geometry accordingly |
| 776 | | However, at least the ti99 system family requires that medium track |
| 777 | | count and drive track count be handled separately. |
| 778 | | It is possible to insert a 40 track medium in an 80 track drive; the |
| 779 | | TI controllers read the track count from sector 0 and automatically |
| 780 | | apply double steps. Setting the track count of the drive to the |
| 781 | | medium track count will then lead to unreachable tracks. |
| 782 | | */ |
| 740 | However, at least the ti99 system family requires that medium track |
| 741 | count and drive track count be handled separately. |
| 742 | It is possible to insert a 40 track medium in an 80 track drive; the |
| 743 | TI controllers read the track count from sector 0 and automatically |
| 744 | apply double steps. Setting the track count of the drive to the |
| 745 | medium track count will then lead to unreachable tracks. |
| 746 | */ |
| 783 | 747 | keep_geometry = (int)mess_device_get_info_int(mess_devclass_from_core_device(image), MESS_DEVINFO_INT_KEEP_DRIVE_GEOMETRY); |
| 784 | 748 | |
| 785 | | if (!keep_geometry |
| 749 | if (!keep_geometry |
| 786 | 750 | && floppy_callbacks(flopimg->floppy)->get_heads_per_disk |
| 787 | 751 | && floppy_callbacks(flopimg->floppy)->get_tracks_per_disk) |
| 788 | 752 | { |
| r5708 | r5709 | |
| 816 | 780 | return retVal; |
| 817 | 781 | } |
| 818 | 782 | |
| 819 | | |
| 820 | | |
| 821 | 783 | static DEVICE_IMAGE_CREATE( floppy ) |
| 822 | 784 | { |
| 823 | | return internal_floppy_device_load(image, create_format, create_args); |
| 785 | return internal_floppy_device_load(image, create_format, create_args); |
| 824 | 786 | } |
| 825 | 787 | |
| 826 | | |
| 827 | | |
| 828 | 788 | static DEVICE_IMAGE_UNLOAD( floppy ) |
| 829 | 789 | { |
| 830 | 790 | floppy_drive *flopimg = get_drive(image); |
| r5708 | r5709 | |
| 835 | 795 | flopimg->floppy = NULL; |
| 836 | 796 | } |
| 837 | 797 | |
| 838 | | |
| 839 | | |
| 840 | 798 | /************************************* |
| 841 | 799 | * |
| 842 | | * Device specification function |
| 800 | * Device specification function |
| 843 | 801 | * |
| 844 | 802 | *************************************/ |
| 845 | 803 | |