Previous 15994 Revisions Next

r6702 Thursday 17th December, 2009 at 21:43:03 UTC by Curt Coder
[C1551] Added floppy reading code.
[src/mess/machine]c1551.c

src/mess/machine/c1551.c
r6701r6702
2626
2727#define FLOPPY_TAG      "c1551_floppy"
2828
29static const double C1551_BITRATE[4] =
30{
31   XTAL_16MHz/13.0,   /* tracks 1-17 */
32   XTAL_16MHz/14.0,   /* tracks 18-24 */
33   XTAL_16MHz/15.0,    /* tracks 25-30 */
34   XTAL_16MHz/16.0      /* tracks 31-42 */
35};
36
37#define SYNC_MARK         0x3ff      /* 10 consecutive 1-bits */
38
39#define TRACK_BUFFER_SIZE   8194      /* 2 bytes track length + maximum of 8192 bytes of GCR encoded data */
40#define TRACK_DATA_START   2
41
2942/***************************************************************************
3043    TYPE DEFINITIONS
3144***************************************************************************/
r6701r6702
3346typedef struct _c1551_t c1551_t;
3447struct _c1551_t
3548{
49   UINT8 track_buffer[TRACK_BUFFER_SIZE];            /* track data buffer */
50   int track_len;                  /* track length */
51   int buffer_pos;                  /* current byte position within track buffer */
52   int bit_pos;                  /* current bit position within track buffer byte */
53   int bit_count;                  /* current data byte bit counter */
54   UINT16 data;                  /* data shift register */
55
56   /* signals */
57   UINT8 yb;                     /* GCR data byte to write */
58   int byte;                     /* byte ready */
59   int atna;                     /* attention acknowledge */
60   int ds;                        /* density select */
61   int stp;                     /* stepping motor */
62   int soe;                     /* s? output enable */
63   int mode;                     /* mode (0 = write, 1 = read) */
64
3665   /* devices */
3766   const device_config *cpu;
38   const device_config *tpi;
67   const device_config *tpi0;
68   const device_config *tpi1;
3969   const device_config *image;
70
71   /* timers */
72   emu_timer *bit_timer;
4073};
4174
4275/***************************************************************************
r6701r6702
5588    IMPLEMENTATION
5689***************************************************************************/
5790
58static READ8_HANDLER( c1551_port_r )
91/*-------------------------------------------------
92    TIMER_CALLBACK( bit_tick )
93-------------------------------------------------*/
94
95static TIMER_CALLBACK( bit_tick )
5996{
97   const device_config *device = (device_config *)ptr;
98   c1551_t *c1551 = get_safe_token(device);
99   int byte = 0;
100
101   /* shift in data from the read head */
102   c1551->data <<= 1;
103   c1551->data |= BIT(c1551->track_buffer[c1551->buffer_pos], c1551->bit_pos);
104   c1551->bit_pos--;
105   c1551->bit_count++;
106
107   if (c1551->bit_pos < 0)
108   {
109      c1551->bit_pos = 7;
110      c1551->buffer_pos++;
111
112      if (c1551->buffer_pos > c1551->track_len + 1)
113      {
114         /* loop to the start of the track */
115         c1551->buffer_pos = TRACK_DATA_START;
116      }
117   }
118
119   if ((c1551->data & SYNC_MARK) == SYNC_MARK)
120   {
121      /* SYNC detected */
122      c1551->bit_count = 0;
123   }
124
125   if (c1551->bit_count > 7)
126   {
127      /* byte ready */
128      c1551->bit_count = 0;
129      byte = 1;
130
131      if (!(c1551->data & 0xff))
132      {
133         /* simulate weak bits with randomness */
134         c1551->data = (c1551->data & 0xff00) | (mame_rand(machine) & 0xff);
135      }
136   }
137
138   c1551->byte = byte;
139}
140
141/*-------------------------------------------------
142    c1551_port_r - M6510T port read
143-------------------------------------------------*/
144
145static READ8_DEVICE_HANDLER( c1551_port_r )
146{
60147   /*
61148
62149        bit     description
r6701r6702
68155        P4      WPRT
69156        P5      DS0
70157        P6      DS1
71        P7      ATN
158        P7      BYTE READY
72159
73160    */
74161
75   return 0;
162   c1551_t *c1551 = get_safe_token(device->owner);
163   UINT8 data = 0;
164
165   /* write protect sense */
166   data |= !floppy_wpt_r(c1551->image) << 4;
167   
168   /* byte ready */
169   data |= !(c1551->soe && c1551->byte) << 7;
170
171   return data;
76172}
77173
78static WRITE8_HANDLER( c1551_port_w )
174/*-------------------------------------------------
175    c1551_port_w - M6510T port write
176-------------------------------------------------*/
177
178static WRITE8_DEVICE_HANDLER( c1551_port_w )
79179{
80180   /*
81181
r6701r6702
88188        P4      WPRT
89189        P5      DS0
90190        P6      DS1
91        P7      ATN
191        P7      BYTE READY
92192
93193    */
194
195   c1551_t *c1551 = get_safe_token(device->owner);
196
197   int stp = data & 0x03;
198   int ds = (data >> 5) & 0x03;
199   int mtr = BIT(data, 2);
200
201   /* stepper motor */
202   if (c1551->stp != stp)
203   {
204      int tracks = 0;
205
206      switch (c1551->stp)
207      {
208      case 0:   if (stp == 1) tracks++; else if (stp == 3) tracks--; break;
209      case 1:   if (stp == 2) tracks++; else if (stp == 0) tracks--; break;
210      case 2: if (stp == 3) tracks++; else if (stp == 1) tracks--; break;
211      case 3: if (stp == 0) tracks++; else if (stp == 2) tracks--; break;
212      }
213
214      if (tracks != 0)
215      {
216         c1551->track_len = TRACK_BUFFER_SIZE;
217         c1551->buffer_pos = TRACK_DATA_START;
218         c1551->bit_pos = 7;
219         c1551->bit_count = 0;
220
221         /* step read/write head */
222         floppy_drive_seek(c1551->image, tracks);
223
224         /* read track data */
225         floppy_drive_read_track_data_info_buffer(c1551->image, 0, c1551->track_buffer, &c1551->track_len);
226
227         /* extract track length */
228         c1551->track_len = (c1551->track_buffer[1] << 8) | c1551->track_buffer[0];
229      }
230
231      c1551->stp = stp;
232   }
233
234   /* spindle motor */
235   floppy_mon_w(c1551->image, !mtr);
236   timer_enable(c1551->bit_timer, mtr);
237
238   /* activity LED */
239
240   /* density select */
241   if (c1551->ds != ds)
242   {
243      timer_adjust_periodic(c1551->bit_timer, attotime_zero, 0, ATTOTIME_IN_HZ(C1551_BITRATE[ds]/4));
244      c1551->ds = ds;
245   }
94246}
95247
96
97248/*-------------------------------------------------
98249    ADDRESS_MAP( c1551_map )
99250-------------------------------------------------*/
100251
101252static ADDRESS_MAP_START( c1551_map, ADDRESS_SPACE_PROGRAM, 8 )
102   AM_RANGE(0x0000, 0x0001) AM_READWRITE(c1551_port_r, c1551_port_w)
253   AM_RANGE(0x0000, 0x0001) AM_DEVREADWRITE(M6510T_TAG, c1551_port_r, c1551_port_w)
103254   AM_RANGE(0x0002, 0x07ff) AM_RAM
104255   AM_RANGE(0x4000, 0x4007) AM_DEVREADWRITE(M6525_TAG, tpi6525_r, tpi6525_w)
105256   AM_RANGE(0xc000, 0xffff) AM_ROM AM_REGION("c1551", 0)
r6701r6702
164315
165316    */
166317
167   return 0;
318   c1551_t *c1551 = get_safe_token(device->owner);
319
320   return c1551->data & 0xff;
168321}
169322
170323static WRITE8_DEVICE_HANDLER( c1551_tpi_pb_w )
r6701r6702
183336        PB7     YB7
184337
185338    */
339
340   c1551_t *c1551 = get_safe_token(device->owner);
341
342   c1551->yb = data;
186343}
187344
188345static READ8_DEVICE_HANDLER( c1551_tpi_pc_r )
r6701r6702
202359
203360    */
204361
205   return 0;
362   c1551_t *c1551 = get_safe_token(device->owner);
363   UINT8 data = 0;
364
365   /* SYNC detect line */
366   data |= !(c1551->mode && ((c1551->data & SYNC_MARK) == SYNC_MARK)) << 6;
367
368   return data;
206369}
207370
208371static WRITE8_DEVICE_HANDLER( c1551_tpi_pc_w )
r6701r6702
221384        PC7     6523 phi2
222385
223386    */
387
388   c1551_t *c1551 = get_safe_token(device->owner);
389
390   /* SOE */
391   c1551->soe = BIT(data, 4);
224392}
225393
226394static const tpi6525_interface c1551_tpi_intf =
r6701r6702
241409-------------------------------------------------*/
242410
243411static FLOPPY_OPTIONS_START( c1551 )
244   FLOPPY_OPTION( c1551, "g64", "Commodore 1541 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
245//  FLOPPY_OPTION( c1551, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
412//  FLOPPY_OPTION( c1551, "d64", "Commodore 1551 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
413   FLOPPY_OPTION( c1551, "g64", "Commodore 1551 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
246414FLOPPY_OPTIONS_END
247415
248416/*-------------------------------------------------
r6701r6702
290458
291459static DEVICE_START( c1551 )
292460{
461   c1551_t *c1551 = get_safe_token(device);
462
463   /* find our CPU */
464   c1551->cpu = device_find_child_by_tag(device, M6510T_TAG);
465
466   /* find devices */
467   c1551->tpi0 = device_find_child_by_tag(device, M6525_TAG);
468   c1551->tpi1 = device_find_child_by_tag(device, M6523_TAG);
469   c1551->image = device_find_child_by_tag(device, FLOPPY_0);
470
471   /* allocate track buffer */
472//  c1551->track_buffer = auto_alloc_array(device->machine, UINT8, TRACK_BUFFER_SIZE);
473
474   /* allocate data timer */
475   c1551->bit_timer = timer_alloc(device->machine, bit_tick, (void *)device);
476
477   /* register for state saving */
478//  state_save_register_device_item_pointer(device, 0, c1551->track_buffer, TRACK_BUFFER_SIZE);
479   state_save_register_device_item(device, 0, c1551->track_len);
480   state_save_register_device_item(device, 0, c1551->buffer_pos);
481   state_save_register_device_item(device, 0, c1551->bit_pos);
482   state_save_register_device_item(device, 0, c1551->bit_count);
483   state_save_register_device_item(device, 0, c1551->data);
484   state_save_register_device_item(device, 0, c1551->yb);
485   state_save_register_device_item(device, 0, c1551->byte);
486   state_save_register_device_item(device, 0, c1551->atna);
487   state_save_register_device_item(device, 0, c1551->ds);
488   state_save_register_device_item(device, 0, c1551->stp);
489   state_save_register_device_item(device, 0, c1551->soe);
490   state_save_register_device_item(device, 0, c1551->mode);
293491}
294492
295493/*-------------------------------------------------

Previous 15994 Revisions Next


© 1998-2018 The MESS Team