Previous 15994 Revisions Next

r4777 Thursday 21st May, 2009 at 06:25:57 UTC by Fabio Priuli
[CBM] Finally split c1551 & vc1541 emulation, reducing struct hell in vc1541.c. Added some VIA notes. Misc clean-ups.
[src/mess/includes]vc1541.h
[src/mess/machine]c16.c vc1541.c

src/mess/includes/vc1541.h
r4776r4777
99#ifndef VC1541_H_
1010#define VC1541_H_
1111
12#include "machine/6522via.h"
13
1412/*----------- defined in machine/vc1541.c -----------*/
1513
16extern const via6522_interface vc1541_via2;
17extern const via6522_interface vc1541_via3;
18
1914/* we currently have preliminary support for 1541 & 1551 only */
2015enum {
2116type_1541 = 0,
r4776r4777
5045
5146
5247int drive_config(running_machine *machine, int type, int id, int mode, const char *cputag, int devicenr);
48void c1551_config(running_machine *machine, const char *cputag);
5349void drive_reset(void);
5450
5551void vc1541_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info);
src/mess/machine/vc1541.c
r4776r4777
133133G64 & GCR - http://ist.uwaterloo.ca/~schepers/formats/G64.TXT
134134GCR - http://www.baltissen.org/newhtm/1541c.htm
135135
136Other info sources:
137   http://sta.c64.org/cbm1541mem.html
138
136139*/
137140
138141
r4776r4777
172175
173176*******************************************/
174177
178/* Legacy code. At a later stage, we should convert everything to devices.
179However, fixing the C64 / Floppy drive communications is a priority over
180cleaning up of this part */
181
182
175183/* 0 summarized, 1 computer, 2 vc1541 */
176184static struct
177185{
r4776r4777
179187}
180188serial;
181189
182/* Legacy code. At a later stage, we should really use code more similar
183to the MESS core floppy code. However, fixing the C64 / Floppy drive
184communications is a priority over the clean up of this part */
190typedef 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
185198typedef struct
186199{
187200   const char *cputag;
188201   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;
203202
203   struct {
204      int deviceid;
205      int serial_atn, serial_clock, serial_data;
206      int acka, data;
207   } vc1541_serial;
208
204209   int via0irq, via1irq;
205210
206211   int led, motor, frequency;
r4776r4777
211216
212217   void *timer;
213218
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;
221220
222static CBM_Drive_Emu drive_static= { 0 }, *drive = &drive_static;
221} vc1541_drive_emu;
223222
223typedef struct
224{
225   const char *cputag;
224226
227   UINT8 cpu_ddr, cpu_port;
228   void *irq_timer;
225229
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
239static vc1541_drive_emu vc1541_drive_static = { 0 }, *vc1541_drive = &vc1541_drive_static;
240static c1551_drive_emu c1551_drive_static = { 0 }, *c1551_drive = &c1551_drive_static;
241
242
243
226244/*******************************************
227245
228246   Format specific code
r4776r4777
595613
596614static TIMER_CALLBACK(drive_timer)
597615{
598   if (drive->clock == 0)
616   if (vc1541_drive->clock == 0)
599617   {
600      drive->gcr.ready = 0;
618      vc1541_drive->gcr.ready = 0;
601619      // why put sync back to 0 here?!? it would break the sync count, isn't it?
602620      // or is it the way the serial port reads the sync count?!?
603      drive->gcr.sync = 0;
621      vc1541_drive->gcr.sync = 0;
604622   
605      if (drive->type == type_1541)
623      if (vc1541_drive->type == type_1541)
606624      {
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");
608626
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);
611629      }
612630
613      drive->clock = 1;
631      vc1541_drive->clock = 1;
614632      return;
615633   }
616634
r4776r4777
618636   // size of the track is: (# sectors in the track) * (size of sector GCR endoded)
619637   // Here, size of sector is 368 because of our choices in the loading: 9 bytes of header gap & 14 bytes of sector gap
620638   // 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;
623641   // should we check if there is a change of track? to reset the pos in that case as well?
624642
625   drive->gcr.ready = 1;
643   vc1541_drive->gcr.ready = 1;
626644
627645   // notice that if there is a sequence of more than 40 1s it correctly does not reset the sync
628646   // counter until a 0 is found
629   if (drive->gcr.data[drive->pos] == 0xff)
647   if (vc1541_drive->gcr.data[vc1541_drive->pos] == 0xff)
630648   {
631      drive->gcr.ones_count++;
649      vc1541_drive->gcr.ones_count++;
632650
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;
636654   }
637655   else
638656   {
639      drive->gcr.ones_count = 0;
640      drive->gcr.sync = 0;
657      vc1541_drive->gcr.ones_count = 0;
658      vc1541_drive->gcr.sync = 0;
641659   }
642660
643   if (drive->type == type_1541)
661   if (vc1541_drive->type == type_1541)
644662   {
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);
648666   }
649667
650   drive->clock = 0;
668   vc1541_drive->clock = 0;
651669}
652670
653671
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 *
666710 * also ca1 (books says cb2)
667711 * irq to m6502 irq connected (or with second via irq)
668 */
712 *
713 **************************************************************************/
669714
670715static void vc1541_via0_irq(const device_config *device, int level)
671716{
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));
674719
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);
676721}
677722
678723static READ8_DEVICE_HANDLER( vc1541_via0_read_portb )
679724{
680725   int value = 0x7a;
681726
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;
684729
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;
687732
688   if (!serial.atn[0]) value |= 0x80;
733   if (!serial.atn[0])
734      value |= 0x80;
689735
690   switch (drive->drive.serial.deviceid)
736   switch (vc1541_drive->vc1541_serial.deviceid)
691737   {
692738      case 8:
693739         value &= ~0x60;
r4776r4777
712758                            data & 0x08 ? "CLOCK" : "clock",
713759                            data & 0x02 ? "DATA"  : "data"));
714760
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;
717763
718   if ((!(data & 0x02)) != drive->drive.serial.serial_data)
764   if ((!(data & 0x02)) != vc1541_drive->vc1541_serial.serial_data)
719765   {
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));
721767   }
722768
723   if ((!(data & 0x08)) != drive->drive.serial.serial_clock)
769   if ((!(data & 0x08)) != vc1541_drive->vc1541_serial.serial_clock)
724770   {
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));
726772   }
727773
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);
729775}
730776
777static 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};
731793
732/*
733 * via 6522 at 0x1c00
734 * port a
735    byte in gcr format from or to floppy
736794
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 *
752828 * ca1 byte ready input (also m6502 set overflow input)
753
754829 * ca2 set overflow enable for 6502
755830 * ca3 read/write
756831 *
757832 * irq to m6502 irq connected
758 */
833 *
834 **************************************************************************/
759835
836
760837static void vc1541_via1_irq (const device_config *device, int level)
761838{
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));
764841
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);
766843}
767844
768845static READ8_DEVICE_HANDLER( vc1541_via1_read_porta )
769846{
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);
771849
772850   DBG_LOG(device->machine, 2, "vc1541 drive", ("port a read %.2x\n", data));
773851   return data;
r4776r4777
787865      value &= ~0x10;
788866#endif
789867
790   if (drive->gcr.sync)
868   if (vc1541_drive->gcr.sync)
791869   {
792870      value &= ~0x80;
793871   }
r4776r4777
803881   {
804882      DBG_LOG(device->machine, 1, "vc1541 drive",("%.2x\n", data));
805883
884   /*   decrease value (%00-%11-%10-%01-%00...) to move head downwards; */
885   /*   increase value (%00-%01-%10-%11-%00...) to move head upwards.   */
806886      if ((old & 0x03) != (data & 0x03))
807887      {
808888         switch (old & 0x03)
809889         {
810890         case 0:
811891            if ((data & 0x03) == 1)
812               drive->track += 0.5;
892               vc1541_drive->track += 0.5;
813893            else if ((data & 0x03) == 3)
814               drive->track -= 0.5;
894               vc1541_drive->track -= 0.5;
815895            break;
816896
817897         case 1:
818898            if ((data & 0x03) == 2)
819               drive->track += 0.5;
899               vc1541_drive->track += 0.5;
820900            else if ((data & 0x03) == 0)
821               drive->track -= 0.5;
901               vc1541_drive->track -= 0.5;
822902            break;
823903
824904         case 2:
825905            if ((data & 0x03) == 3)
826               drive->track += 0.5;
906               vc1541_drive->track += 0.5;
827907            else if ((data & 0x03) == 1)
828               drive->track -= 0.5;
908               vc1541_drive->track -= 0.5;
829909            break;
830910
831911         case 3:
832912            if ((data & 0x03) == 0)
833               drive->track += 0.5;
913               vc1541_drive->track += 0.5;
834914            else if ((data & 0x03) == 2)
835               drive->track -= 0.5;
915               vc1541_drive->track -= 0.5;
836916            break;
837917         }
838918
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;
840924
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);
846926      }
847927
848      if ((drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60)))
928      if ((vc1541_drive->motor != (data & 0x04)) || (vc1541_drive->frequency != (data & 0x60)))
849929      {
850930         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];
854934
855         if (drive->motor)
935         if (vc1541_drive->motor)
856936         {
857            if (attotime_to_double(timer_timeelapsed(drive->timer)) > 1.0e29)
937            if (attotime_to_double(timer_timeelapsed(vc1541_drive->timer)) > 1.0e29)
858938            {
859               timer_reset(drive->timer, attotime_never);
939               timer_reset(vc1541_drive->timer, attotime_never);
860940            }
861941            else
862942            {
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);
864945            }
865946         }
866947         else
867948         {
868            timer_reset(drive->timer, attotime_never);
949            timer_reset(vc1541_drive->timer, attotime_never);
950            logerror("Motor turned off\n");
869951         }
870952      }
871953      old = data;
872954   }
873   drive->led = data & 0x08;
874}
875955
876static WRITE8_DEVICE_HANDLER( vc1541_via1_write_portca1 )
877{
956   vc1541_drive->led = data & 0x08;
878957}
879958
880static WRITE8_DEVICE_HANDLER( vc1541_via1_write_portca2 )
959static const via6522_interface vc1541_via1 =
881960{
882}
883
884const 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
901vc1541_via1 =
902{
903961   DEVCB_HANDLER(vc1541_via1_read_porta),
904962   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, */
909967   DEVCB_HANDLER(vc1541_via1_write_porta),
910968   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, */
915973   DEVCB_LINE(vc1541_via1_irq)
916974};
917975
r4776r4777
931989
932990void vc1541_serial_atn_write( running_machine *machine, int which, int level )
933991{
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");
935993#if 0
936994   int value;
937995#endif
r4776r4777
9531011                            activecpu_get_pc(),
9541012                            serial.atn[0]?"ATN":"atn"));
9551013         #endif
956            via_ca1_w (via_2, 0, !level);
1014            via_ca1_w (via_0, 0, !level);
9571015#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];
9641025               }
9651026            }
9661027#endif
r4776r4777
11001161 */
11011162static TIMER_CALLBACK(c1551_irq_timer)
11021163{
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);
11041165}
11051166
11061167/*
r4776r4777
11181179   if (offset)
11191180   {
11201181      DBG_LOG(space->machine, 1, "c1551 port",("write %.2x\n", data));
1121      drive->drive.c1551.cpu_port = data;
1182      c1551_drive->cpu_port = data;
11221183
11231184      if (data != old)
11241185      {
r4776r4777
11301191            {
11311192            case 0:
11321193               if ((data & 0x03) == 1)
1133                  drive->track += 0.5;
1194                  c1551_drive->track += 0.5;
11341195               else if ((data & 0x03) == 3)
1135                  drive->track -= 0.5;
1196                  c1551_drive->track -= 0.5;
11361197               break;
11371198
11381199            case 1:
11391200               if ((data & 0x03) == 2)
1140                  drive->track += 0.5;
1201                  c1551_drive->track += 0.5;
11411202               else if ((data & 0x03) == 0)
1142                  drive->track -= 0.5;
1203                  c1551_drive->track -= 0.5;
11431204               break;
11441205
11451206            case 2:
11461207               if ((data & 0x03) == 3)
1147                  drive->track += 0.5;
1208                  c1551_drive->track += 0.5;
11481209               else if ((data & 0x03) == 1)
1149                  drive->track -= 0.5;
1210                  c1551_drive->track -= 0.5;
11501211               break;
11511212
11521213            case 3:
11531214               if ((data & 0x03) == 0)
1154                  drive->track += 0.5;
1215                  c1551_drive->track += 0.5;
11551216               else if ((data & 0x03) == 2)
1156                  drive->track -= 0.5;
1217                  c1551_drive->track -= 0.5;
11571218               break;
11581219            }
11591220
11601221            /* 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;
11651226         }
11661227
1167         if ( (drive->motor != (data & 0x04)) || (drive->frequency != (data & 0x60)) )
1228         if ( (c1551_drive->motor != (data & 0x04)) || (c1551_drive->frequency != (data & 0x60)) )
11681229         {
11691230            double tme;
1170            drive->motor = data & 0x04;
1171            drive->frequency = data & 0x60;
1231            c1551_drive->motor = data & 0x04;
1232            c1551_drive->frequency = data & 0x60;
11721233
1173            tme = drive_times[drive->frequency >> 5];
1174            if (drive->motor)
1234            tme = drive_times[c1551_drive->frequency >> 5];
1235            if (c1551_drive->motor)
11751236            {
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);
11781239               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));
11801241            }
11811242            else
11821243            {
1183               timer_reset(drive->timer, attotime_never);
1244               timer_reset(c1551_drive->timer, attotime_never);
11841245            }
11851246         }
11861247         old = data;
11871248      }
1188      drive->led = data & 0x08;
1249      c1551_drive->led = data & 0x08;
11891250   }
11901251   else
11911252   {
1192      drive->drive.c1551.cpu_ddr = data;
1253      c1551_drive->cpu_ddr = data;
11931254      DBG_LOG(space->machine, 1, "c1551 ddr", ("write %.2x\n", data));
11941255   }
11951256}
r4776r4777
12051266      if (WRITEPROTECTED)
12061267         data &= ~0x10;
12071268#endif
1208      if (drive->gcr.ready)
1269      if (c1551_drive->gcr.ready)
12091270      {
12101271         data |= 0x80;
1211         drive->gcr.ready = 0;
1272         c1551_drive->gcr.ready = 0;
12121273      }
12131274
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;
12161277      DBG_LOG(space->machine, 3, "c1551 port",("read %.2x\n", data));
12171278   }
12181279   else
12191280   {
1220      data = drive->drive.c1551.cpu_ddr;
1281      data = c1551_drive->cpu_ddr;
12211282      DBG_LOG(space->machine, 3, "c1551 ddr",("read %.2x\n", data));
12221283   }
12231284   return data;
r4776r4777
12451306   
12461307   data &= ~0x20;
12471308
1248   if (drive->gcr.sync)
1309   if (c1551_drive->gcr.sync)
12491310      data &= ~0x40;
12501311
12511312   return data;
r4776r4777
12531314
12541315static READ8_DEVICE_HANDLER( c1551_port_b_r )
12551316{
1256   int data = drive->gcr.data[drive->pos];
1317   int data = c1551_drive->gcr.data[c1551_drive->pos];
12571318
12581319   DBG_LOG(device->machine, 2, "c1551 drive",("port a read %.2x\n", data));
12591320   return data;
r4776r4777
13331394
13341395int drive_config( running_machine *machine, int type, int id, int mode, const char *cputag, int devicenr )
13351396{
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);
13401401
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
13491402   return 0;
13501403}
13511404
1405void 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
13521415void drive_reset (void)
13531416{
13541417   int i;
13551418
1356   if (drive->type == type_1541)
1419   if (vc1541_drive->type == type_1541)
13571420   {
13581421      for (i = 0; i < sizeof (serial.atn) / sizeof (serial.atn[0]); i++)
13591422      {
r4776r4777
13611424      }
13621425   }
13631426
1364   drive->track = 1.0;
1427   vc1541_drive->track = 1.0;
13651428}
13661429
13671430
r4776r4777
14291492
14301493**************************************/
14311494
1432/* At a later stage we should move the loading and encoding part to an external function
1433for all formats / drive and then call it here and in similar DEVICE_IMAGE_LOADs for
1434the 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...      */
1498void 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
14351599static DEVICE_IMAGE_LOAD( _1541 )
14361600{
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;
14401603   UINT8 *temp_copy = NULL;
14411604   const char *filetype;
14421605
r4776r4777
15111674   format_id_1 = temp_copy[disk_id1(format)];   // can be modified by error code 29 -> ^ 0xff
15121675   format_id_2 = temp_copy[disk_id2(format)];
15131676
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 );
15231679
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}
15291683
1530         // first we set the position at which sector data starts in the image
1531         pos = image_tracksector2offset(k, l);
1684static 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;
15321690
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);
15411693
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);
15461725
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);
15551728
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;
15621731
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)];
15671735
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 );
15721738
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
16081739   logerror("Floppy image %s successfully loaded\n", image_filename(image));
16091740   return INIT_PASS;
16101741}
r4776r4777
16441775
16451776void c1551_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info)
16461777{
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   }
16481785}
16491786
16501787
src/mess/machine/c16.c
r4776r4777
457457   memset(mess_ram + (0xfd40 % mess_ram_size), 0xff, 0x20);
458458   
459459   if (has_c1551)      /* C1551 */
460      drive_config(machine, type_1551, 0, 0, "cpu_c1551", 8);
460      c1551_config(machine, "cpu_c1551");
461461
462462   if (has_vc1541)      /* VC1541 */
463463      drive_config(machine, type_1541, 0, 0, "cpu_vc1540", 8);
r4776r4777
561561      memory_install_read8_handler(space, 0xfec0, 0xfedf, 0, 0, SMH_NOP);
562562   }
563563
564   if (has_c1551 || has_vc1541)      /* c1551 or vc1541 */
564   if (has_vc1541)      /* vc1541 */
565565   {
566566      cbm_serial_config(machine, &cbm_fake_drive_interface);
567567      drive_reset();

Previous 15994 Revisions Next


© 1998-2018 The MESS Team