src/mess/machine/vc1541.c
| r4776 | r4777 | |
| 133 | 133 | G64 & GCR - http://ist.uwaterloo.ca/~schepers/formats/G64.TXT |
| 134 | 134 | GCR - http://www.baltissen.org/newhtm/1541c.htm |
| 135 | 135 | |
| 136 | Other info sources: |
| 137 | http://sta.c64.org/cbm1541mem.html |
| 138 | |
| 136 | 139 | */ |
| 137 | 140 | |
| 138 | 141 | |
| r4776 | r4777 | |
| 172 | 175 | |
| 173 | 176 | *******************************************/ |
| 174 | 177 | |
| 178 | /* Legacy code. At a later stage, we should convert everything to devices. |
| 179 | However, fixing the C64 / Floppy drive communications is a priority over |
| 180 | cleaning up of this part */ |
| 181 | |
| 182 | |
| 175 | 183 | /* 0 summarized, 1 computer, 2 vc1541 */ |
| 176 | 184 | static struct |
| 177 | 185 | { |
| r4776 | r4777 | |
| 179 | 187 | } |
| 180 | 188 | serial; |
| 181 | 189 | |
| 182 | | /* Legacy code. At a later stage, we should really use code more similar |
| 183 | | to the MESS core floppy code. However, fixing the C64 / Floppy drive |
| 184 | | communications is a priority over the clean up of this part */ |
| 190 | typedef struct |
| 191 | { |
| 192 | int sync; // active after > 9 '1's |
| 193 | int ones_count; |
| 194 | int ready; // needed for 1551 |
| 195 | UINT8 data[0x500000]; // entire d64 image encoded at loading time |
| 196 | } GCR_data; |
| 197 | |
| 185 | 198 | typedef struct |
| 186 | 199 | { |
| 187 | 200 | const char *cputag; |
| 188 | 201 | int type; |
| 189 | | union { |
| 190 | | struct { |
| 191 | | int deviceid; |
| 192 | | int serial_atn, serial_clock, serial_data; |
| 193 | | int acka, data; |
| 194 | | } serial; |
| 195 | | struct { |
| 196 | | int deviceid; |
| 197 | | } ieee488; |
| 198 | | struct { |
| 199 | | UINT8 cpu_ddr, cpu_port; |
| 200 | | void *irq_timer; |
| 201 | | } c1551; |
| 202 | | } drive; |
| 203 | 202 | |
| 203 | struct { |
| 204 | int deviceid; |
| 205 | int serial_atn, serial_clock, serial_data; |
| 206 | int acka, data; |
| 207 | } vc1541_serial; |
| 208 | |
| 204 | 209 | int via0irq, via1irq; |
| 205 | 210 | |
| 206 | 211 | int led, motor, frequency; |
| r4776 | r4777 | |
| 211 | 216 | |
| 212 | 217 | void *timer; |
| 213 | 218 | |
| 214 | | struct { |
| 215 | | int sync; // active after > 9 '1's |
| 216 | | int ones_count; |
| 217 | | int ready; // needed for 1551 |
| 218 | | UINT8 data[0x500000]; // entire d64 image encoded at loading time |
| 219 | | } gcr; |
| 220 | | } CBM_Drive_Emu; |
| 219 | GCR_data gcr; |
| 221 | 220 | |
| 222 | | static CBM_Drive_Emu drive_static= { 0 }, *drive = &drive_static; |
| 221 | } vc1541_drive_emu; |
| 223 | 222 | |
| 223 | typedef struct |
| 224 | { |
| 225 | const char *cputag; |
| 224 | 226 | |
| 227 | UINT8 cpu_ddr, cpu_port; |
| 228 | void *irq_timer; |
| 225 | 229 | |
| 230 | int led, motor, frequency; |
| 231 | double track; |
| 232 | int pos; // from the beginning of the current track! |
| 233 | void *timer; |
| 234 | |
| 235 | GCR_data gcr; |
| 236 | |
| 237 | } c1551_drive_emu; |
| 238 | |
| 239 | static vc1541_drive_emu vc1541_drive_static = { 0 }, *vc1541_drive = &vc1541_drive_static; |
| 240 | static c1551_drive_emu c1551_drive_static = { 0 }, *c1551_drive = &c1551_drive_static; |
| 241 | |
| 242 | |
| 243 | |
| 226 | 244 | /******************************************* |
| 227 | 245 | |
| 228 | 246 | Format specific code |
| r4776 | r4777 | |
| 595 | 613 | |
| 596 | 614 | static TIMER_CALLBACK(drive_timer) |
| 597 | 615 | { |
| 598 | | if (drive->clock == 0) |
| 616 | if (vc1541_drive->clock == 0) |
| 599 | 617 | { |
| 600 | | drive->gcr.ready = 0; |
| 618 | vc1541_drive->gcr.ready = 0; |
| 601 | 619 | // why put sync back to 0 here?!? it would break the sync count, isn't it? |
| 602 | 620 | // or is it the way the serial port reads the sync count?!? |
| 603 | | drive->gcr.sync = 0; |
| 621 | vc1541_drive->gcr.sync = 0; |
| 604 | 622 | |
| 605 | | if (drive->type == type_1541) |
| 623 | if (vc1541_drive->type == type_1541) |
| 606 | 624 | { |
| 607 | | const device_config *via_3 = devtag_get_device(machine, "_1541_via_1"); |
| 625 | const device_config *via_1 = devtag_get_device(machine, "_1541_via_1"); |
| 608 | 626 | |
| 609 | | cputag_set_input_line(machine, drive->cputag, M6502_SET_OVERFLOW, 1); |
| 610 | | via_ca1_w(via_3, 0, 1); |
| 627 | cputag_set_input_line(machine, vc1541_drive->cputag, M6502_SET_OVERFLOW, 1); |
| 628 | via_ca1_w(via_1, 0, 1); |
| 611 | 629 | } |
| 612 | 630 | |
| 613 | | drive->clock = 1; |
| 631 | vc1541_drive->clock = 1; |
| 614 | 632 | return; |
| 615 | 633 | } |
| 616 | 634 | |
| r4776 | r4777 | |
| 618 | 636 | // size of the track is: (# sectors in the track) * (size of sector GCR endoded) |
| 619 | 637 | // Here, size of sector is 368 because of our choices in the loading: 9 bytes of header gap & 14 bytes of sector gap |
| 620 | 638 | // We will need to set the size at loading time when adding support for G64 images |
| 621 | | if (++(drive->pos) >= (d64_sectors_per_track[(int)drive->track - 1] * 368)) |
| 622 | | drive->pos = 0; |
| 639 | if (++(vc1541_drive->pos) >= (d64_sectors_per_track[(int)vc1541_drive->track - 1] * 368)) |
| 640 | vc1541_drive->pos = 0; |
| 623 | 641 | // should we check if there is a change of track? to reset the pos in that case as well? |
| 624 | 642 | |
| 625 | | drive->gcr.ready = 1; |
| 643 | vc1541_drive->gcr.ready = 1; |
| 626 | 644 | |
| 627 | 645 | // notice that if there is a sequence of more than 40 1s it correctly does not reset the sync |
| 628 | 646 | // counter until a 0 is found |
| 629 | | if (drive->gcr.data[drive->pos] == 0xff) |
| 647 | if (vc1541_drive->gcr.data[vc1541_drive->pos] == 0xff) |
| 630 | 648 | { |
| 631 | | drive->gcr.ones_count++; |
| 649 | vc1541_drive->gcr.ones_count++; |
| 632 | 650 | |
| 633 | | // if (drive->gcr.ones_count == 2) // 9 ones should already mark it as a sync, even if CBM chose to use 40 of them |
| 634 | | if (drive->gcr.ones_count == 5) |
| 635 | | drive->gcr.sync = 1; |
| 651 | // if (vc1541_drive->gcr.ones_count == 2) // 9 ones should already mark it as a sync, even if CBM chose to use 40 of them |
| 652 | if (vc1541_drive->gcr.ones_count == 5) |
| 653 | vc1541_drive->gcr.sync = 1; |
| 636 | 654 | } |
| 637 | 655 | else |
| 638 | 656 | { |
| 639 | | drive->gcr.ones_count = 0; |
| 640 | | drive->gcr.sync = 0; |
| 657 | vc1541_drive->gcr.ones_count = 0; |
| 658 | vc1541_drive->gcr.sync = 0; |
| 641 | 659 | } |
| 642 | 660 | |
| 643 | | if (drive->type == type_1541) |
| 661 | if (vc1541_drive->type == type_1541) |
| 644 | 662 | { |
| 645 | | const device_config *via_3 = devtag_get_device(machine, "_1541_via_1"); |
| 646 | | cputag_set_input_line(machine, drive->cputag, M6502_SET_OVERFLOW, 0); |
| 647 | | via_ca1_w(via_3, 0, 0); |
| 663 | const device_config *via_1 = devtag_get_device(machine, "_1541_via_1"); |
| 664 | cputag_set_input_line(machine, vc1541_drive->cputag, M6502_SET_OVERFLOW, 0); |
| 665 | via_ca1_w(via_1, 0, 0); |
| 648 | 666 | } |
| 649 | 667 | |
| 650 | | drive->clock = 0; |
| 668 | vc1541_drive->clock = 0; |
| 651 | 669 | } |
| 652 | 670 | |
| 653 | 671 | |
| 654 | | /* |
| 655 | | * via 6522 at 0x1800 |
| 656 | | * port b |
| 657 | | * 0 inverted serial data in |
| 658 | | * 1 inverted serial data out |
| 659 | | * 2 inverted serial clock in |
| 660 | | * 3 inverted serial clock out |
| 661 | | * 4 inverted serial atn out |
| 662 | | * 5 input device id 1 |
| 663 | | * 6 input device id 2 |
| 664 | | * id 2+id 1/0+0 devicenumber 8/0+1 9/1+0 10/1+1 11 |
| 665 | | * 7 inverted serial atn in |
| 672 | /************************************************************************** |
| 673 | * VIA 6522 at 0x1800 |
| 674 | * |
| 675 | * 0x1800 port B |
| 676 | * bit 0 : data in (inverted: 0 = Low, 1 = High) |
| 677 | * bit 1 : data out (inverted: 0 = Low, 1 = High) |
| 678 | * bit 2 : clock in (inverted: 0 = Low, 1 = High) |
| 679 | * bit 3 : clock out (inverted: 0 = Low, 1 = High) |
| 680 | * bit 4 : atna out (inverted: 0 = Low, 1 = High) |
| 681 | * bit 5 : device id 1 |
| 682 | * bit 6 : device id 2 |
| 683 | * device ID number is set by a jumper: |
| 684 | * 00 = 8 (default), 01 = 9, 10 = 10, 11 = 11 |
| 685 | * bit 7 : atn in (inverted: 0 = Low, 1 = High) |
| 686 | * 0x1801 port A - Read to acknowledge interrupt generated by ATN IN going high. |
| 687 | * 0x1802 port B data direction register. |
| 688 | * if a bit here is 0, then the corresponding bit of portB can only be read |
| 689 | * if a bit here is 1, then the corresponding bit of portB can be read and written |
| 690 | * default value 0x1a |
| 691 | * 0x1803 port A data direction register. |
| 692 | * default value 0xff |
| 693 | * 0x1804 timer 1 counter (low) |
| 694 | * 0x1805 timer 1 counter (high) / Timer one counter |
| 695 | * Read low byte or write high byte to start timer or restart timer upon underflow. |
| 696 | * 0x1806 timer 1 latch (low) |
| 697 | * 0x1807 timer 1 latch (high) |
| 698 | * read/write starting value of timer from/to here. |
| 699 | * 0x1808 timer 2 counter (low) |
| 700 | * 0x1809 timer 2 counter (high) |
| 701 | * 0x180a shift register |
| 702 | * 0x180b auxiliary control register. bit 6: 0 = Stop timer; 1 = Start timer. |
| 703 | * 0x180c peripheral control register |
| 704 | * 0x180d interrupt flag register. bit 1: 1 = ATN IN went high, bit 6: 1 = Timer underflow occurred. |
| 705 | * 0x180e interrupt enable register. |
| 706 | * bit 1: write - 1 = Enable interrupts generated by ATN IN going high |
| 707 | * bit 7: read - interrupt has been generated. |
| 708 | * bit 7: write - Fill bit: bits 0-6, that are set to 1, get their values from this bit; bits 0-6, that are set to 0, are left unchanged |
| 709 | * |
| 666 | 710 | * also ca1 (books says cb2) |
| 667 | 711 | * irq to m6502 irq connected (or with second via irq) |
| 668 | | */ |
| 712 | * |
| 713 | **************************************************************************/ |
| 669 | 714 | |
| 670 | 715 | static void vc1541_via0_irq(const device_config *device, int level) |
| 671 | 716 | { |
| 672 | | drive->via0irq = level; |
| 673 | | DBG_LOG(device->machine, 2, "vc1541 via0 irq", ("level %d %d\n", drive->via0irq, drive->via1irq)); |
| 717 | vc1541_drive->via0irq = level; |
| 718 | DBG_LOG(device->machine, 2, "vc1541 via0 irq", ("level %d %d\n", vc1541_drive->via0irq, vc1541_drive->via1irq)); |
| 674 | 719 | |
| 675 | | cputag_set_input_line(device->machine, drive->cputag, M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 720 | cputag_set_input_line(device->machine, vc1541_drive->cputag, M6502_IRQ_LINE, vc1541_drive->via1irq || vc1541_drive->via0irq); |
| 676 | 721 | } |
| 677 | 722 | |
| 678 | 723 | static READ8_DEVICE_HANDLER( vc1541_via0_read_portb ) |
| 679 | 724 | { |
| 680 | 725 | int value = 0x7a; |
| 681 | 726 | |
| 682 | | if (!drive->drive.serial.serial_data || !serial.data[0]) |
| 683 | | value |= 1; |
| 727 | if (!vc1541_drive->vc1541_serial.serial_data || !serial.data[0]) |
| 728 | value |= 0x01; |
| 684 | 729 | |
| 685 | | if (!drive->drive.serial.serial_clock || !serial.clock[0]) |
| 686 | | value |= 4; |
| 730 | if (!vc1541_drive->vc1541_serial.serial_clock || !serial.clock[0]) |
| 731 | value |= 0x04; |
| 687 | 732 | |
| 688 | | if (!serial.atn[0]) value |= 0x80; |
| 733 | if (!serial.atn[0]) |
| 734 | value |= 0x80; |
| 689 | 735 | |
| 690 | | switch (drive->drive.serial.deviceid) |
| 736 | switch (vc1541_drive->vc1541_serial.deviceid) |
| 691 | 737 | { |
| 692 | 738 | case 8: |
| 693 | 739 | value &= ~0x60; |
| r4776 | r4777 | |
| 712 | 758 | data & 0x08 ? "CLOCK" : "clock", |
| 713 | 759 | data & 0x02 ? "DATA" : "data")); |
| 714 | 760 | |
| 715 | | drive->drive.serial.data = data & 0x02 ? 0 : 1; |
| 716 | | drive->drive.serial.acka = data & 0x10 ? 1 : 0; |
| 761 | vc1541_drive->vc1541_serial.data = data & 0x02 ? 0 : 1; |
| 762 | vc1541_drive->vc1541_serial.acka = data & 0x10 ? 1 : 0; |
| 717 | 763 | |
| 718 | | if ((!(data & 0x02)) != drive->drive.serial.serial_data) |
| 764 | if ((!(data & 0x02)) != vc1541_drive->vc1541_serial.serial_data) |
| 719 | 765 | { |
| 720 | | vc1541_serial_data_write(device->machine, 1, drive->drive.serial.serial_data = !(data & 0x02)); |
| 766 | vc1541_serial_data_write(device->machine, 1, vc1541_drive->vc1541_serial.serial_data = !(data & 0x02)); |
| 721 | 767 | } |
| 722 | 768 | |
| 723 | | if ((!(data & 0x08)) != drive->drive.serial.serial_clock) |
| 769 | if ((!(data & 0x08)) != vc1541_drive->vc1541_serial.serial_clock) |
| 724 | 770 | { |
| 725 | | vc1541_serial_clock_write(device->machine, 1, drive->drive.serial.serial_clock = !(data & 0x08)); |
| 771 | vc1541_serial_clock_write(device->machine, 1, vc1541_drive->vc1541_serial.serial_clock = !(data & 0x08)); |
| 726 | 772 | } |
| 727 | 773 | |
| 728 | | vc1541_serial_atn_write(device->machine, 1, drive->drive.serial.serial_atn = 1); |
| 774 | vc1541_serial_atn_write(device->machine, 1, vc1541_drive->vc1541_serial.serial_atn = 1); |
| 729 | 775 | } |
| 730 | 776 | |
| 777 | static const via6522_interface vc1541_via0 = |
| 778 | { |
| 779 | DEVCB_NULL, /*via0_read_porta, */ |
| 780 | DEVCB_HANDLER(vc1541_via0_read_portb), |
| 781 | DEVCB_NULL, /*via0_read_ca1, */ |
| 782 | DEVCB_NULL, /*via0_read_cb1, */ |
| 783 | DEVCB_NULL, /*via0_read_ca2, */ |
| 784 | DEVCB_NULL, /*via0_read_cb2, */ |
| 785 | DEVCB_NULL, /*via0_write_porta, */ |
| 786 | DEVCB_HANDLER(vc1541_via0_write_portb), |
| 787 | DEVCB_NULL, /*via0_write_ca1, */ |
| 788 | DEVCB_NULL, /*via0_write_cb1, */ |
| 789 | DEVCB_NULL, /*via0_write_ca2, */ |
| 790 | DEVCB_NULL, /*via0_write_cb2, */ |
| 791 | DEVCB_LINE(vc1541_via0_irq) |
| 792 | }; |
| 731 | 793 | |
| 732 | | /* |
| 733 | | * via 6522 at 0x1c00 |
| 734 | | * port a |
| 735 | | byte in gcr format from or to floppy |
| 736 | 794 | |
| 737 | | * port b |
| 738 | | * 0 output steppermotor |
| 739 | | * 1 output steppermotor |
| 740 | | 10: 00->01->10->11->00 move head to higher tracks |
| 741 | | * 2 output motor (rotation) (300 revolutions per minute) |
| 742 | | * 3 output led |
| 743 | | * 4 input disk not write protected |
| 744 | | * 5 timer adjustment |
| 745 | | * 6 timer adjustment |
| 746 | | * 4 different speed zones (track dependend) |
| 747 | | frequency select? |
| 748 | | 3 slowest |
| 749 | | 0 highest |
| 750 | | * 7 input sync signal when reading from disk (more then 9 1 bits) |
| 751 | | |
| 795 | /************************************************************************** |
| 796 | * VIA 6522 at 0x1c00 |
| 797 | * |
| 798 | * 0x1c00 port B - Disk controller i/o control line |
| 799 | * bit 0 : step head motor output 1 |
| 800 | * bit 1 : step head motor output 2 |
| 801 | * bit 2 : motor on output (0 = Off, 1 = On) - 300 revolutions per minute |
| 802 | * bit 3 : LED control output (0 = Off, 1 = On) |
| 803 | * bit 4 : write protect sense (0 = protected, 1 = unprotected) |
| 804 | * bit 5 : density select 0 |
| 805 | * bit 6 : density select 1 |
| 806 | * data density (track dependend): 00 = lowest (faster time), 11 = highest (slower timer) |
| 807 | * bit 7 : sync signal (more then nine 1 bits) detect |
| 808 | * 0x1c01 Port A - byte in gcr format from or to floppy |
| 809 | * 0x1c02 Port B data direction register + LED output - default 0x6f |
| 810 | * 0x1c03 Port A data direction register - 0x00 read from disk, 0xff write to disk |
| 811 | * 0x1c04 Timer 1 counter (low) |
| 812 | * 0x1c05 Timer 1 counter (high) |
| 813 | * Read low byte or write high byte to start timer or restart timer upon underflow. |
| 814 | * 0x1c06 Timer 1 latch (low) |
| 815 | * 0x1c07 Timer 1 latch (high) |
| 816 | * read/write starting value of timer from/to here. |
| 817 | * 0x1c08 Timer 2 latch (low) |
| 818 | * 0x1c09 Timer 2 latch (high) |
| 819 | * 0x1c0a Shift register |
| 820 | * 0x1c0b Auxiliary control register. bit 6: 0 = Stop timer; 1 = Start timer. |
| 821 | * 0x1c0c Peripheral control register |
| 822 | * 0x1c0d Interrupt flag register. bit 6: 1 = Timer underflow occurred. |
| 823 | * 0x1c0e Interrupt enable register |
| 824 | * bit 6: write - 1 = Enable interrupts generated by timer underflow |
| 825 | * bit 7: read - interrupt has been generated. |
| 826 | * bit 7: write - Fill bit: bits 0-6, that are set to 1, get their values from this bit; bits 0-6, that are set to 0, are left unchanged |
| 827 | * |
| 752 | 828 | * ca1 byte ready input (also m6502 set overflow input) |
| 753 | | |
| 754 | 829 | * ca2 set overflow enable for 6502 |
| 755 | 830 | * ca3 read/write |
| 756 | 831 | * |
| 757 | 832 | * irq to m6502 irq connected |
| 758 | | */ |
| 833 | * |
| 834 | **************************************************************************/ |
| 759 | 835 | |
| 836 | |
| 760 | 837 | static void vc1541_via1_irq (const device_config *device, int level) |
| 761 | 838 | { |
| 762 | | drive->via1irq = level; |
| 763 | | DBG_LOG(device->machine, 2, "vc1541 via1 irq", ("level %d %d\n", drive->via0irq, drive->via1irq)); |
| 839 | vc1541_drive->via1irq = level; |
| 840 | DBG_LOG(device->machine, 2, "vc1541 via1 irq", ("level %d %d\n", vc1541_drive->via0irq, vc1541_drive->via1irq)); |
| 764 | 841 | |
| 765 | | cputag_set_input_line(device->machine, drive->cputag, M6502_IRQ_LINE, drive->via1irq || drive->via0irq); |
| 842 | cputag_set_input_line(device->machine, vc1541_drive->cputag, M6502_IRQ_LINE, vc1541_drive->via1irq || vc1541_drive->via0irq); |
| 766 | 843 | } |
| 767 | 844 | |
| 768 | 845 | static READ8_DEVICE_HANDLER( vc1541_via1_read_porta ) |
| 769 | 846 | { |
| 770 | | int data = drive->gcr.data[drive->pos]; |
| 847 | int data = vc1541_drive->gcr.data[vc1541_drive->pos]; |
| 848 | logerror("Data read: Track %.1f Pos %d\n", vc1541_drive->track, vc1541_drive->pos); |
| 771 | 849 | |
| 772 | 850 | DBG_LOG(device->machine, 2, "vc1541 drive", ("port a read %.2x\n", data)); |
| 773 | 851 | return data; |
| r4776 | r4777 | |
| 787 | 865 | value &= ~0x10; |
| 788 | 866 | #endif |
| 789 | 867 | |
| 790 | | if (drive->gcr.sync) |
| 868 | if (vc1541_drive->gcr.sync) |
| 791 | 869 | { |
| 792 | 870 | value &= ~0x80; |
| 793 | 871 | } |
| r4776 | r4777 | |
| 803 | 881 | { |
| 804 | 882 | DBG_LOG(device->machine, 1, "vc1541 drive",("%.2x\n", data)); |
| 805 | 883 | |
| 884 | /* decrease value (%00-%11-%10-%01-%00...) to move head downwards; */ |
| 885 | /* increase value (%00-%01-%10-%11-%00...) to move head upwards. */ |
| 806 | 886 | if ((old & 0x03) != (data & 0x03)) |
| 807 | 887 | { |
| 808 | 888 | switch (old & 0x03) |
| 809 | 889 | { |
| 810 | 890 | case 0: |
| 811 | 891 | if ((data & 0x03) == 1) |
| 812 | | drive->track += 0.5; |
| 892 | vc1541_drive->track += 0.5; |
| 813 | 893 | else if ((data & 0x03) == 3) |
| 814 | | drive->track -= 0.5; |
| 894 | vc1541_drive->track -= 0.5; |
| 815 | 895 | break; |
| 816 | 896 | |
| 817 | 897 | case 1: |
| 818 | 898 | if ((data & 0x03) == 2) |
| 819 | | drive->track += 0.5; |
| 899 | vc1541_drive->track += 0.5; |
| 820 | 900 | else if ((data & 0x03) == 0) |
| 821 | | drive->track -= 0.5; |
| 901 | vc1541_drive->track -= 0.5; |
| 822 | 902 | break; |
| 823 | 903 | |
| 824 | 904 | case 2: |
| 825 | 905 | if ((data & 0x03) == 3) |
| 826 | | drive->track += 0.5; |
| 906 | vc1541_drive->track += 0.5; |
| 827 | 907 | else if ((data & 0x03) == 1) |
| 828 | | drive->track -= 0.5; |
| 908 | vc1541_drive->track -= 0.5; |
| 829 | 909 | break; |
| 830 | 910 | |
| 831 | 911 | case 3: |
| 832 | 912 | if ((data & 0x03) == 0) |
| 833 | | drive->track += 0.5; |
| 913 | vc1541_drive->track += 0.5; |
| 834 | 914 | else if ((data & 0x03) == 2) |
| 835 | | drive->track -= 0.5; |
| 915 | vc1541_drive->track -= 0.5; |
| 836 | 916 | break; |
| 837 | 917 | } |
| 838 | 918 | |
| 839 | | // logerror("Track #: %f\n", drive->track); |
| 919 | /* If we have reached the first or last track, we cannot proceed further! */ |
| 920 | if (vc1541_drive->track < 1) |
| 921 | vc1541_drive->track = 1.0; |
| 922 | if (vc1541_drive->track > 35) |
| 923 | vc1541_drive->track = 35.0; |
| 840 | 924 | |
| 841 | | /* If we have reached the first or last track, we cannot proceed further! */ |
| 842 | | if (drive->track < 1) |
| 843 | | drive->track = 1.0; |
| 844 | | if (drive->track > 35) |
| 845 | | drive->track = 35.0; |
| 925 | logerror("Track #: %.1f\n", vc1541_drive->track); |
| 846 | 926 | } |
| 847 | 927 | |
| 848 | | if ((drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60))) |
| 928 | if ((vc1541_drive->motor != (data & 0x04)) || (vc1541_drive->frequency != (data & 0x60))) |
| 849 | 929 | { |
| 850 | 930 | double tme; |
| 851 | | drive->motor = data & 0x04; |
| 852 | | drive->frequency = data & 0x60; |
| 853 | | tme = drive_times[drive->frequency >> 5]; |
| 931 | vc1541_drive->motor = data & 0x04; |
| 932 | vc1541_drive->frequency = data & 0x60; |
| 933 | tme = drive_times[vc1541_drive->frequency >> 5]; |
| 854 | 934 | |
| 855 | | if (drive->motor) |
| 935 | if (vc1541_drive->motor) |
| 856 | 936 | { |
| 857 | | if (attotime_to_double(timer_timeelapsed(drive->timer)) > 1.0e29) |
| 937 | if (attotime_to_double(timer_timeelapsed(vc1541_drive->timer)) > 1.0e29) |
| 858 | 938 | { |
| 859 | | timer_reset(drive->timer, attotime_never); |
| 939 | timer_reset(vc1541_drive->timer, attotime_never); |
| 860 | 940 | } |
| 861 | 941 | else |
| 862 | 942 | { |
| 863 | | timer_adjust_periodic(drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 943 | timer_adjust_periodic(vc1541_drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 944 | logerror("Freq changed: %f\n", tme); |
| 864 | 945 | } |
| 865 | 946 | } |
| 866 | 947 | else |
| 867 | 948 | { |
| 868 | | timer_reset(drive->timer, attotime_never); |
| 949 | timer_reset(vc1541_drive->timer, attotime_never); |
| 950 | logerror("Motor turned off\n"); |
| 869 | 951 | } |
| 870 | 952 | } |
| 871 | 953 | old = data; |
| 872 | 954 | } |
| 873 | | drive->led = data & 0x08; |
| 874 | | } |
| 875 | 955 | |
| 876 | | static WRITE8_DEVICE_HANDLER( vc1541_via1_write_portca1 ) |
| 877 | | { |
| 956 | vc1541_drive->led = data & 0x08; |
| 878 | 957 | } |
| 879 | 958 | |
| 880 | | static WRITE8_DEVICE_HANDLER( vc1541_via1_write_portca2 ) |
| 959 | static const via6522_interface vc1541_via1 = |
| 881 | 960 | { |
| 882 | | } |
| 883 | | |
| 884 | | const via6522_interface vc1541_via0 = |
| 885 | | { |
| 886 | | DEVCB_NULL, /*vc1541_via0_read_porta, */ |
| 887 | | DEVCB_HANDLER(vc1541_via0_read_portb), |
| 888 | | DEVCB_NULL, /*via2_read_ca1, */ |
| 889 | | DEVCB_NULL, /*via2_read_cb1, */ |
| 890 | | DEVCB_NULL, /*via2_read_ca2, */ |
| 891 | | DEVCB_NULL, /*via2_read_cb2, */ |
| 892 | | DEVCB_NULL, /*via2_write_porta, */ |
| 893 | | DEVCB_HANDLER(vc1541_via0_write_portb), |
| 894 | | DEVCB_NULL, /*via2_write_ca1, */ |
| 895 | | DEVCB_NULL, /*via2_write_cb1, */ |
| 896 | | DEVCB_NULL, /*via2_write_ca2, */ |
| 897 | | DEVCB_NULL, /*via2_write_cb2, */ |
| 898 | | DEVCB_LINE(vc1541_via0_irq) |
| 899 | | }, |
| 900 | | |
| 901 | | vc1541_via1 = |
| 902 | | { |
| 903 | 961 | DEVCB_HANDLER(vc1541_via1_read_porta), |
| 904 | 962 | DEVCB_HANDLER(vc1541_via1_read_portb), |
| 905 | | DEVCB_NULL, /*via3_read_ca1, */ |
| 906 | | DEVCB_NULL, /*via3_read_cb1, */ |
| 907 | | DEVCB_NULL, /*via3_read_ca2, */ |
| 908 | | DEVCB_NULL, /*via3_read_cb2, */ |
| 963 | DEVCB_NULL, /*via3_read_ca1, */ |
| 964 | DEVCB_NULL, /*via3_read_cb1, */ |
| 965 | DEVCB_NULL, /*via3_read_ca2, */ |
| 966 | DEVCB_NULL, /*via3_read_cb2, */ |
| 909 | 967 | DEVCB_HANDLER(vc1541_via1_write_porta), |
| 910 | 968 | DEVCB_HANDLER(vc1541_via1_write_portb), |
| 911 | | DEVCB_HANDLER(vc1541_via1_write_portca1), /*via3_write_ca1, */ |
| 912 | | DEVCB_NULL, /*via3_write_cb1, */ |
| 913 | | DEVCB_HANDLER(vc1541_via1_write_portca2), /*via3_write_ca2, */ |
| 914 | | DEVCB_NULL, /*via3_write_cb2, */ |
| 969 | DEVCB_NULL, /*via3_write_ca1, */ |
| 970 | DEVCB_NULL, /*via3_write_cb1, */ |
| 971 | DEVCB_NULL, /*via3_write_ca2, */ |
| 972 | DEVCB_NULL, /*via3_write_cb2, */ |
| 915 | 973 | DEVCB_LINE(vc1541_via1_irq) |
| 916 | 974 | }; |
| 917 | 975 | |
| r4776 | r4777 | |
| 931 | 989 | |
| 932 | 990 | void vc1541_serial_atn_write( running_machine *machine, int which, int level ) |
| 933 | 991 | { |
| 934 | | const device_config *via_2 = devtag_get_device(machine, "_1541_via_0"); |
| 992 | const device_config *via_0 = devtag_get_device(machine, "_1541_via_0"); |
| 935 | 993 | #if 0 |
| 936 | 994 | int value; |
| 937 | 995 | #endif |
| r4776 | r4777 | |
| 953 | 1011 | activecpu_get_pc(), |
| 954 | 1012 | serial.atn[0]?"ATN":"atn")); |
| 955 | 1013 | #endif |
| 956 | | via_ca1_w (via_2, 0, !level); |
| 1014 | via_ca1_w (via_0, 0, !level); |
| 957 | 1015 | #if 0 |
| 958 | | value=drive->drive.serial.data; |
| 959 | | if (drive->drive.serial.acka!=!level) value=0; |
| 960 | | if (value!=serial.data[2]) { |
| 961 | | serial.data[2]=value; |
| 962 | | if (serial.data[0]!=value) { |
| 963 | | serial.data[0]=serial.data[1] && serial.data[2]; |
| 1016 | value = vc1541_drive->vc1541_serial.data; |
| 1017 | if (vc1541_drive->vc1541_serial.acka != !level) |
| 1018 | value = 0; |
| 1019 | if (value != serial.data[2]) |
| 1020 | { |
| 1021 | serial.data[2] = value; |
| 1022 | if (serial.data[0] != value) |
| 1023 | { |
| 1024 | serial.data[0] = serial.data[1] && serial.data[2]; |
| 964 | 1025 | } |
| 965 | 1026 | } |
| 966 | 1027 | #endif |
| r4776 | r4777 | |
| 1100 | 1161 | */ |
| 1101 | 1162 | static TIMER_CALLBACK(c1551_irq_timer) |
| 1102 | 1163 | { |
| 1103 | | cputag_set_input_line(machine, drive->cputag, M6502_IRQ_LINE, ASSERT_LINE); |
| 1164 | cputag_set_input_line(machine, c1551_drive->cputag, M6502_IRQ_LINE, ASSERT_LINE); |
| 1104 | 1165 | } |
| 1105 | 1166 | |
| 1106 | 1167 | /* |
| r4776 | r4777 | |
| 1118 | 1179 | if (offset) |
| 1119 | 1180 | { |
| 1120 | 1181 | DBG_LOG(space->machine, 1, "c1551 port",("write %.2x\n", data)); |
| 1121 | | drive->drive.c1551.cpu_port = data; |
| 1182 | c1551_drive->cpu_port = data; |
| 1122 | 1183 | |
| 1123 | 1184 | if (data != old) |
| 1124 | 1185 | { |
| r4776 | r4777 | |
| 1130 | 1191 | { |
| 1131 | 1192 | case 0: |
| 1132 | 1193 | if ((data & 0x03) == 1) |
| 1133 | | drive->track += 0.5; |
| 1194 | c1551_drive->track += 0.5; |
| 1134 | 1195 | else if ((data & 0x03) == 3) |
| 1135 | | drive->track -= 0.5; |
| 1196 | c1551_drive->track -= 0.5; |
| 1136 | 1197 | break; |
| 1137 | 1198 | |
| 1138 | 1199 | case 1: |
| 1139 | 1200 | if ((data & 0x03) == 2) |
| 1140 | | drive->track += 0.5; |
| 1201 | c1551_drive->track += 0.5; |
| 1141 | 1202 | else if ((data & 0x03) == 0) |
| 1142 | | drive->track -= 0.5; |
| 1203 | c1551_drive->track -= 0.5; |
| 1143 | 1204 | break; |
| 1144 | 1205 | |
| 1145 | 1206 | case 2: |
| 1146 | 1207 | if ((data & 0x03) == 3) |
| 1147 | | drive->track += 0.5; |
| 1208 | c1551_drive->track += 0.5; |
| 1148 | 1209 | else if ((data & 0x03) == 1) |
| 1149 | | drive->track -= 0.5; |
| 1210 | c1551_drive->track -= 0.5; |
| 1150 | 1211 | break; |
| 1151 | 1212 | |
| 1152 | 1213 | case 3: |
| 1153 | 1214 | if ((data & 0x03) == 0) |
| 1154 | | drive->track += 0.5; |
| 1215 | c1551_drive->track += 0.5; |
| 1155 | 1216 | else if ((data & 0x03) == 2) |
| 1156 | | drive->track -= 0.5; |
| 1217 | c1551_drive->track -= 0.5; |
| 1157 | 1218 | break; |
| 1158 | 1219 | } |
| 1159 | 1220 | |
| 1160 | 1221 | /* If we have reached the first or last track, we cannot proceed further! */ |
| 1161 | | if (drive->track < 1) |
| 1162 | | drive->track = 1.0; |
| 1163 | | if (drive->track > 35) |
| 1164 | | drive->track = 35.0; |
| 1222 | if (c1551_drive->track < 1) |
| 1223 | c1551_drive->track = 1.0; |
| 1224 | if (c1551_drive->track > 35) |
| 1225 | c1551_drive->track = 35.0; |
| 1165 | 1226 | } |
| 1166 | 1227 | |
| 1167 | | if ( (drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60)) ) |
| 1228 | if ( (c1551_drive->motor != (data & 0x04)) || (c1551_drive->frequency != (data & 0x60)) ) |
| 1168 | 1229 | { |
| 1169 | 1230 | double tme; |
| 1170 | | drive->motor = data & 0x04; |
| 1171 | | drive->frequency = data & 0x60; |
| 1231 | c1551_drive->motor = data & 0x04; |
| 1232 | c1551_drive->frequency = data & 0x60; |
| 1172 | 1233 | |
| 1173 | | tme = drive_times[drive->frequency >> 5]; |
| 1174 | | if (drive->motor) |
| 1234 | tme = drive_times[c1551_drive->frequency >> 5]; |
| 1235 | if (c1551_drive->motor) |
| 1175 | 1236 | { |
| 1176 | | if (attotime_to_double(timer_timeelapsed(drive->timer)) > 1.0e29) |
| 1177 | | timer_reset(drive->timer, attotime_never); |
| 1237 | if (attotime_to_double(timer_timeelapsed(c1551_drive->timer)) > 1.0e29) |
| 1238 | timer_reset(c1551_drive->timer, attotime_never); |
| 1178 | 1239 | else |
| 1179 | | timer_adjust_periodic(drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 1240 | timer_adjust_periodic(c1551_drive->timer, attotime_zero, 0, ATTOTIME_IN_HZ(tme)); |
| 1180 | 1241 | } |
| 1181 | 1242 | else |
| 1182 | 1243 | { |
| 1183 | | timer_reset(drive->timer, attotime_never); |
| 1244 | timer_reset(c1551_drive->timer, attotime_never); |
| 1184 | 1245 | } |
| 1185 | 1246 | } |
| 1186 | 1247 | old = data; |
| 1187 | 1248 | } |
| 1188 | | drive->led = data & 0x08; |
| 1249 | c1551_drive->led = data & 0x08; |
| 1189 | 1250 | } |
| 1190 | 1251 | else |
| 1191 | 1252 | { |
| 1192 | | drive->drive.c1551.cpu_ddr = data; |
| 1253 | c1551_drive->cpu_ddr = data; |
| 1193 | 1254 | DBG_LOG(space->machine, 1, "c1551 ddr", ("write %.2x\n", data)); |
| 1194 | 1255 | } |
| 1195 | 1256 | } |
| r4776 | r4777 | |
| 1205 | 1266 | if (WRITEPROTECTED) |
| 1206 | 1267 | data &= ~0x10; |
| 1207 | 1268 | #endif |
| 1208 | | if (drive->gcr.ready) |
| 1269 | if (c1551_drive->gcr.ready) |
| 1209 | 1270 | { |
| 1210 | 1271 | data |= 0x80; |
| 1211 | | drive->gcr.ready = 0; |
| 1272 | c1551_drive->gcr.ready = 0; |
| 1212 | 1273 | } |
| 1213 | 1274 | |
| 1214 | | data &= ~drive->drive.c1551.cpu_ddr; |
| 1215 | | data |= drive->drive.c1551.cpu_ddr & drive->drive.c1551.cpu_port; |
| 1275 | data &= ~c1551_drive->cpu_ddr; |
| 1276 | data |= c1551_drive->cpu_ddr & c1551_drive->cpu_port; |
| 1216 | 1277 | DBG_LOG(space->machine, 3, "c1551 port",("read %.2x\n", data)); |
| 1217 | 1278 | } |
| 1218 | 1279 | else |
| 1219 | 1280 | { |
| 1220 | | data = drive->drive.c1551.cpu_ddr; |
| 1281 | data = c1551_drive->cpu_ddr; |
| 1221 | 1282 | DBG_LOG(space->machine, 3, "c1551 ddr",("read %.2x\n", data)); |
| 1222 | 1283 | } |
| 1223 | 1284 | return data; |
| r4776 | r4777 | |
| 1245 | 1306 | |
| 1246 | 1307 | data &= ~0x20; |
| 1247 | 1308 | |
| 1248 | | if (drive->gcr.sync) |
| 1309 | if (c1551_drive->gcr.sync) |
| 1249 | 1310 | data &= ~0x40; |
| 1250 | 1311 | |
| 1251 | 1312 | return data; |
| r4776 | r4777 | |
| 1253 | 1314 | |
| 1254 | 1315 | static READ8_DEVICE_HANDLER( c1551_port_b_r ) |
| 1255 | 1316 | { |
| 1256 | | int data = drive->gcr.data[drive->pos]; |
| 1317 | int data = c1551_drive->gcr.data[c1551_drive->pos]; |
| 1257 | 1318 | |
| 1258 | 1319 | DBG_LOG(device->machine, 2, "c1551 drive",("port a read %.2x\n", data)); |
| 1259 | 1320 | return data; |
| r4776 | r4777 | |
| 1333 | 1394 | |
| 1334 | 1395 | int drive_config( running_machine *machine, int type, int id, int mode, const char *cputag, int devicenr ) |
| 1335 | 1396 | { |
| 1336 | | drive->type = type; |
| 1337 | | drive->cputag = cputag; |
| 1338 | | drive->drive.serial.deviceid = devicenr; |
| 1339 | | drive->timer = timer_alloc(machine, drive_timer, NULL); |
| 1397 | vc1541_drive->type = type; |
| 1398 | vc1541_drive->cputag = cputag; |
| 1399 | vc1541_drive->vc1541_serial.deviceid = devicenr; |
| 1400 | vc1541_drive->timer = timer_alloc(machine, drive_timer, NULL); |
| 1340 | 1401 | |
| 1341 | | if (type == type_1551) |
| 1342 | | { |
| 1343 | | /* time should be small enough to allow quitting of the irq |
| 1344 | | line before the next interrupt is triggered */ |
| 1345 | | drive->drive.c1551.irq_timer = timer_alloc(machine, c1551_irq_timer, NULL); |
| 1346 | | timer_adjust_periodic(drive->drive.c1551.irq_timer, attotime_zero, 0, ATTOTIME_IN_HZ(60)); |
| 1347 | | } |
| 1348 | | |
| 1349 | 1402 | return 0; |
| 1350 | 1403 | } |
| 1351 | 1404 | |
| 1405 | void c1551_config( running_machine *machine, const char *cputag ) |
| 1406 | { |
| 1407 | c1551_drive->cputag = cputag; |
| 1408 | |
| 1409 | /* time should be small enough to allow quitting of the irq |
| 1410 | line before the next interrupt is triggered */ |
| 1411 | c1551_drive->irq_timer = timer_alloc(machine, c1551_irq_timer, NULL); |
| 1412 | timer_adjust_periodic(c1551_drive->irq_timer, attotime_zero, 0, ATTOTIME_IN_HZ(60)); |
| 1413 | } |
| 1414 | |
| 1352 | 1415 | void drive_reset (void) |
| 1353 | 1416 | { |
| 1354 | 1417 | int i; |
| 1355 | 1418 | |
| 1356 | | if (drive->type == type_1541) |
| 1419 | if (vc1541_drive->type == type_1541) |
| 1357 | 1420 | { |
| 1358 | 1421 | for (i = 0; i < sizeof (serial.atn) / sizeof (serial.atn[0]); i++) |
| 1359 | 1422 | { |
| r4776 | r4777 | |
| 1361 | 1424 | } |
| 1362 | 1425 | } |
| 1363 | 1426 | |
| 1364 | | drive->track = 1.0; |
| 1427 | vc1541_drive->track = 1.0; |
| 1365 | 1428 | } |
| 1366 | 1429 | |
| 1367 | 1430 | |
| r4776 | r4777 | |
| 1429 | 1492 | |
| 1430 | 1493 | **************************************/ |
| 1431 | 1494 | |
| 1432 | | /* At a later stage we should move the loading and encoding part to an external function |
| 1433 | | for all formats / drive and then call it here and in similar DEVICE_IMAGE_LOADs for |
| 1434 | | the other drive types with 'image' as a parameter */ |
| 1495 | /* Floppy loading clean-up, part 1: with cbm_floppy_image_loading we can finally split c1551 from vc1541 |
| 1496 | Both struct have a GCR_data gcr component where we store the floppy data, but remaining parts are finally separated! |
| 1497 | Later, everything should be changed to use devices for the drives... */ |
| 1498 | void cbm_floppy_image_loading( int format, int id1, int id2, UINT8 *copy, GCR_data gcr ) |
| 1499 | { |
| 1500 | UINT8 sector_checksum; |
| 1501 | int i = 0, j, k, l, pos; |
| 1502 | |
| 1503 | for (k = 1; k <= max_tracks_per_format[format]; k++) |
| 1504 | { |
| 1505 | /* logerror("decoding track %d offset %d %d %d %d %d\n", k, |
| 1506 | image_tracksector2offset(k, 0) / (16*16*16*16), |
| 1507 | (image_tracksector2offset(k, 0) % (16*16*16*16)) / (16*16*16), |
| 1508 | (image_tracksector2offset(k, 0) % (16*16*16)) / (16*16), |
| 1509 | (image_tracksector2offset(k, 0) % (16*16)) / 16, |
| 1510 | image_tracksector2offset(k, 0) % 16); |
| 1511 | */ |
| 1512 | |
| 1513 | for (l = 0; l < d64_sectors_per_track[k - 1]; l++) |
| 1514 | { |
| 1515 | // here we convert the sector data to gcr directly! |
| 1516 | // IMPORTANT: we shall implement errors in reading sectors! |
| 1517 | // these can modify e.g. header info $01 & $05 |
| 1518 | |
| 1519 | // first we set the position at which sector data starts in the image |
| 1520 | pos = image_tracksector2offset(k, l); |
| 1521 | |
| 1522 | /* |
| 1523 | 1. Header sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1524 | 2. Header info 52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes) |
| 1525 | 3. Header gap 55 55 55 55 55 55 55 55 55 (9 bytes, never read) |
| 1526 | 4. Data sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1527 | 5. Data block 55...4A (325 GCR bytes) |
| 1528 | 6. Inter-sector gap 55 55 55 55...55 55 (4 to 19 bytes, never read) |
| 1529 | */ |
| 1530 | |
| 1531 | /* Header sync */ |
| 1532 | for (j = 0; j < 5; j++) |
| 1533 | gcr.data[i + j] = 0xff; |
| 1534 | i += 5; |
| 1535 | |
| 1536 | /* Header info */ |
| 1537 | /* These are 8 bytes unencoded, which become 10 bytes encoded */ |
| 1538 | // $00 - header block ID ($08) // this byte can be modified by error code 20 -> 0xff |
| 1539 | // $01 - header block checksum (EOR of $02-$05) // this byte can be modified by error code 27 -> ^ 0xff |
| 1540 | // $02 - Sector# of data block |
| 1541 | // $03 - Track# of data block |
| 1542 | gcr_double_2_gcr(0x08, l ^ k ^ id2 ^ id1, l, k, gcr.data + i); |
| 1543 | i += 5; |
| 1544 | |
| 1545 | // $04 - Format ID byte #2 |
| 1546 | // $05 - Format ID byte #1 |
| 1547 | // $06 - $0F ("off" byte) |
| 1548 | // $07 - $0F ("off" byte) |
| 1549 | gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr.data + i); |
| 1550 | i += 5; |
| 1551 | |
| 1552 | /* Header gap */ |
| 1553 | for (j = 0; j < 9; j++) |
| 1554 | gcr.data[i + j] = 0x55; |
| 1555 | i += 9; |
| 1556 | |
| 1557 | /* Data sync */ |
| 1558 | for (j = 0; j < 5; j++) |
| 1559 | gcr.data[i + j] = 0xff; |
| 1560 | i += 5; |
| 1561 | |
| 1562 | /* Data block */ |
| 1563 | // we first need to calculate the checksum of the 256 bytes of the sector |
| 1564 | sector_checksum = copy[pos]; |
| 1565 | for (j = 1; j < 256; j++) |
| 1566 | sector_checksum ^= copy[pos + j]; |
| 1567 | |
| 1568 | /* |
| 1569 | $00 - data block ID ($07) |
| 1570 | $01-100 - 256 bytes sector data |
| 1571 | $101 - data block checksum (EOR of $01-100) |
| 1572 | $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5) |
| 1573 | */ |
| 1574 | gcr_double_2_gcr(0x07, copy[pos], copy[pos + 1], |
| 1575 | copy[pos + 2], gcr.data + i); |
| 1576 | i += 5; |
| 1577 | |
| 1578 | for (j = 1; j < 64; j++) |
| 1579 | { |
| 1580 | gcr_double_2_gcr(copy[pos + 4 * j - 1], copy[pos + 4 * j], |
| 1581 | copy[pos + 4 * j + 1], copy[pos + 4 * j + 2], gcr.data + i); |
| 1582 | i += 5; |
| 1583 | } |
| 1584 | |
| 1585 | gcr_double_2_gcr(copy[pos + 255], sector_checksum, 0x00, 0x00, gcr.data + i); |
| 1586 | i += 5; |
| 1587 | |
| 1588 | /* Inter-sector gap */ |
| 1589 | // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen." |
| 1590 | // Here we put 14 as an average... |
| 1591 | for (j = 0; j < 14; j++) |
| 1592 | gcr.data[i + j] = 0x55; |
| 1593 | i += 14; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | } |
| 1598 | |
| 1435 | 1599 | static DEVICE_IMAGE_LOAD( _1541 ) |
| 1436 | 1600 | { |
| 1437 | | UINT8 sector_checksum, format_id_1, format_id_2; |
| 1438 | | int i = 0, j, k, l; |
| 1439 | | int format = 0, pos, filesize; |
| 1601 | UINT8 format_id_1, format_id_2; |
| 1602 | int format = 0, filesize; |
| 1440 | 1603 | UINT8 *temp_copy = NULL; |
| 1441 | 1604 | const char *filetype; |
| 1442 | 1605 | |
| r4776 | r4777 | |
| 1511 | 1674 | format_id_1 = temp_copy[disk_id1(format)]; // can be modified by error code 29 -> ^ 0xff |
| 1512 | 1675 | format_id_2 = temp_copy[disk_id2(format)]; |
| 1513 | 1676 | |
| 1514 | | for (k = 1; k <= max_tracks_per_format[format]; k++) |
| 1515 | | { |
| 1516 | | /* logerror("decoding track %d offset %d %d %d %d %d\n", k, |
| 1517 | | image_tracksector2offset(k, 0) / (16*16*16*16), |
| 1518 | | (image_tracksector2offset(k, 0) % (16*16*16*16)) / (16*16*16), |
| 1519 | | (image_tracksector2offset(k, 0) % (16*16*16)) / (16*16), |
| 1520 | | (image_tracksector2offset(k, 0) % (16*16)) / 16, |
| 1521 | | image_tracksector2offset(k, 0) % 16); |
| 1522 | | */ |
| 1677 | /* Disk is encoded and stored in xxx_drive->gcr.data */ |
| 1678 | cbm_floppy_image_loading( format, format_id_1, format_id_2, temp_copy, vc1541_drive->gcr ); |
| 1523 | 1679 | |
| 1524 | | for (l = 0; l < d64_sectors_per_track[k - 1]; l++) |
| 1525 | | { |
| 1526 | | // here we convert the sector data to gcr directly! |
| 1527 | | // IMPORTANT: we shall implement errors in reading sectors! |
| 1528 | | // these can modify e.g. header info $01 & $05 |
| 1680 | logerror("Floppy image %s successfully loaded\n", image_filename(image)); |
| 1681 | return INIT_PASS; |
| 1682 | } |
| 1529 | 1683 | |
| 1530 | | // first we set the position at which sector data starts in the image |
| 1531 | | pos = image_tracksector2offset(k, l); |
| 1684 | static DEVICE_IMAGE_LOAD( _1551 ) |
| 1685 | { |
| 1686 | UINT8 format_id_1, format_id_2; |
| 1687 | int format = 0, filesize; |
| 1688 | UINT8 *temp_copy = NULL; |
| 1689 | const char *filetype; |
| 1532 | 1690 | |
| 1533 | | /* |
| 1534 | | 1. Header sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1535 | | 2. Header info 52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes) |
| 1536 | | 3. Header gap 55 55 55 55 55 55 55 55 55 (9 bytes, never read) |
| 1537 | | 4. Data sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 1538 | | 5. Data block 55...4A (325 GCR bytes) |
| 1539 | | 6. Inter-sector gap 55 55 55 55...55 55 (4 to 19 bytes, never read) |
| 1540 | | */ |
| 1691 | filesize = image_length(image); |
| 1692 | filetype = image_filetype(image); |
| 1541 | 1693 | |
| 1542 | | /* Header sync */ |
| 1543 | | for (j = 0; j < 5; j++) |
| 1544 | | drive->gcr.data[i + j] = 0xff; |
| 1545 | | i += 5; |
| 1694 | /* Find out which type of image we are loading */ |
| 1695 | if (!mame_stricmp (filetype, "d64")) |
| 1696 | { |
| 1697 | switch (filesize) |
| 1698 | { |
| 1699 | case 0x2ab00: |
| 1700 | format = format_d64; |
| 1701 | break; |
| 1702 | case (0x2ab00 + 0x2ab): |
| 1703 | format = format_d64_err; |
| 1704 | break; |
| 1705 | case 0x30000: |
| 1706 | format = format_d64_40t; |
| 1707 | break; |
| 1708 | case (0x30000 + 0x300): |
| 1709 | format = format_d64_40t_err; |
| 1710 | break; |
| 1711 | default: |
| 1712 | logerror("The image you're trying to load has not the regular size of a .d64 image: \n"); |
| 1713 | logerror("Image %s size %d\n", image_filename(image), filesize); |
| 1714 | return INIT_FAIL; |
| 1715 | } |
| 1716 | } |
| 1717 | else |
| 1718 | { |
| 1719 | logerror("Unsupported format\n"); |
| 1720 | return INIT_FAIL; |
| 1721 | } |
| 1722 | |
| 1723 | /* Set up track / sector map according to the format of the image */ |
| 1724 | offset_init (format); |
| 1546 | 1725 | |
| 1547 | | /* Header info */ |
| 1548 | | /* These are 8 bytes unencoded, which become 10 bytes encoded */ |
| 1549 | | // $00 - header block ID ($08) // this byte can be modified by error code 20 -> 0xff |
| 1550 | | // $01 - header block checksum (EOR of $02-$05) // this byte can be modified by error code 27 -> ^ 0xff |
| 1551 | | // $02 - Sector# of data block |
| 1552 | | // $03 - Track# of data block |
| 1553 | | gcr_double_2_gcr(0x08, l ^ k ^ format_id_2 ^ format_id_1, l, k, drive->gcr.data + i); |
| 1554 | | i += 5; |
| 1726 | /* Claim memory */ |
| 1727 | temp_copy = auto_alloc_array(image->machine, UINT8, filesize); |
| 1555 | 1728 | |
| 1556 | | // $04 - Format ID byte #2 |
| 1557 | | // $05 - Format ID byte #1 |
| 1558 | | // $06 - $0F ("off" byte) |
| 1559 | | // $07 - $0F ("off" byte) |
| 1560 | | gcr_double_2_gcr(format_id_2, format_id_1, 0x0f, 0x0f, drive->gcr.data + i); |
| 1561 | | i += 5; |
| 1729 | if ((image_fread( image, temp_copy, filesize ) != filesize) || !filesize) |
| 1730 | return INIT_FAIL; |
| 1562 | 1731 | |
| 1563 | | /* Header gap */ |
| 1564 | | for (j = 0; j < 9; j++) |
| 1565 | | drive->gcr.data[i + j] = 0x55; |
| 1566 | | i += 9; |
| 1732 | /* Start GCR encoding */ |
| 1733 | format_id_1 = temp_copy[disk_id1(format)]; // can be modified by error code 29 -> ^ 0xff |
| 1734 | format_id_2 = temp_copy[disk_id2(format)]; |
| 1567 | 1735 | |
| 1568 | | /* Data sync */ |
| 1569 | | for (j = 0; j < 5; j++) |
| 1570 | | drive->gcr.data[i + j] = 0xff; |
| 1571 | | i += 5; |
| 1736 | /* Disk is encoded and stored in xxx_drive->gcr.data */ |
| 1737 | cbm_floppy_image_loading( format, format_id_1, format_id_2, temp_copy, c1551_drive->gcr ); |
| 1572 | 1738 | |
| 1573 | | /* Data block */ |
| 1574 | | // we first need to calculate the checksum of the 256 bytes of the sector |
| 1575 | | sector_checksum = temp_copy[pos]; |
| 1576 | | for (j = 1; j < 256; j++) |
| 1577 | | sector_checksum ^= temp_copy[pos + j]; |
| 1578 | | |
| 1579 | | /* |
| 1580 | | $00 - data block ID ($07) |
| 1581 | | $01-100 - 256 bytes sector data |
| 1582 | | $101 - data block checksum (EOR of $01-100) |
| 1583 | | $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5) |
| 1584 | | */ |
| 1585 | | gcr_double_2_gcr(0x07, temp_copy[pos], temp_copy[pos + 1], |
| 1586 | | temp_copy[pos + 2], drive->gcr.data + i); |
| 1587 | | i += 5; |
| 1588 | | |
| 1589 | | for (j = 1; j < 64; j++) |
| 1590 | | { |
| 1591 | | gcr_double_2_gcr(temp_copy[pos + 4 * j - 1], temp_copy[pos + 4 * j], |
| 1592 | | temp_copy[pos + 4 * j + 1], temp_copy[pos + 4 * j + 2], drive->gcr.data + i); |
| 1593 | | i += 5; |
| 1594 | | } |
| 1595 | | |
| 1596 | | gcr_double_2_gcr(temp_copy[pos + 255], sector_checksum, 0x00, 0x00, drive->gcr.data + i); |
| 1597 | | i += 5; |
| 1598 | | |
| 1599 | | /* Inter-sector gap */ |
| 1600 | | // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen." |
| 1601 | | // Here we put 14 as an average... |
| 1602 | | for (j = 0; j < 14; j++) |
| 1603 | | drive->gcr.data[i + j] = 0x55; |
| 1604 | | i += 14; |
| 1605 | | } |
| 1606 | | } |
| 1607 | | |
| 1608 | 1739 | logerror("Floppy image %s successfully loaded\n", image_filename(image)); |
| 1609 | 1740 | return INIT_PASS; |
| 1610 | 1741 | } |
| r4776 | r4777 | |
| 1644 | 1775 | |
| 1645 | 1776 | void c1551_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 1646 | 1777 | { |
| 1647 | | vc1541_device_getinfo(devclass, state, info); |
| 1778 | switch(state) |
| 1779 | { |
| 1780 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 1781 | case MESS_DEVINFO_PTR_LOAD: info->load = DEVICE_IMAGE_LOAD_NAME(_1551); break; |
| 1782 | |
| 1783 | default: vc1541_device_getinfo(devclass, state, info); |
| 1784 | } |
| 1648 | 1785 | } |
| 1649 | 1786 | |
| 1650 | 1787 | |