Previous 15994 Revisions Next

r6608 Friday 11th December, 2009 at 13:36:00 UTC by Curt Coder
[C1541] Added bit timer, serial bus reset, write protect sensor, SYNC detection, stepper/spindle motors, serial bus address selection.
[src/mess/machine]c1541.c

src/mess/machine/c1541.c
r6607r6608
118118
119119   TODO:
120120
121   - serial bus
121   - mechanical track 0 sensing
122   - get floppy image to driver state
123   - allocate track buffer
122124   - activity led
123   - bus reset
124   - spindle motor
125   - stepper motor
126125   - read G64
127126   - write G64
127   - D64 to G64 conversion
128128   - accurate timing
129129
130130*/
r6607r6608
141141    PARAMETERS
142142***************************************************************************/
143143
144#define LOG 1
145
144146#define M6502_TAG      "ucd5"
145147#define M6522_0_TAG      "uab1"
146148#define M6522_1_TAG      "ucd4"
149#define TIMER_BIT_TAG   "ue7"
147150
151static const double C1541_BITRATE[4] =
152{
153   XTAL_16MHz/13.0, XTAL_16MHz/14.0, XTAL_16MHz/15.0, XTAL_16MHz/16.0
154};
155
156#define C1541_TRACK_BUFFER_SIZE      0x2000
157
148158/***************************************************************************
149159    TYPE DEFINITIONS
150160***************************************************************************/
r6607r6608
152162typedef struct _c1541_t c1541_t;
153163struct _c1541_t
154164{
155   UINT8 yb;
156   int atna;
165   /* abstractions */
166   int address;                  /* serial bus address - 8 */
167   UINT8 *track_buffer;            /* track data buffer */
168   int track;                     /* current position of the actuator (half track) */
169   int buffer_pos;                  /* current byte position within track buffer */
170   int bit_pos;                  /* current bit position within track buffer byte */
157171
172   /* signals */
173   UINT8 yb;                     /* GCR data byte */
174   int byte;                     /* byte ready */
175   int atna;                     /* attention acknowledge */
176   int ds;                        /* data speed */
177   int stp;                     /* stepper motor phase */
178   int soe;                     /* s? output enable */
179   int mode;                     /* mode (0 = write, 1 = read) */
180
158181   /* devices */
159182   const device_config *cpu;
160183   const device_config *via0;
161184   const device_config *via1;
162185   const device_config *serial_bus;
186   const device_config *image;
187   
188   /* timers */
189   emu_timer *bit_timer;
163190};
164191
165192/***************************************************************************
r6607r6608
181208   return (c1541_config *)device->inline_config;
182209}
183210
211INLINE void set_byte_ready(c1541_t *c1541, int byte)
212{
213   if (c1541->byte != byte)
214   {
215      int byte_ready = !(c1541->soe && byte_ready);
216
217      cpu_set_input_line(c1541->cpu, M6502_SET_OVERFLOW, byte_ready);
218      via_ca1_w(c1541->via1, 0, byte_ready);
219     
220      c1541->byte = byte;
221   }
222}
223
184224/***************************************************************************
185225    IMPLEMENTATION
186226***************************************************************************/
187227
188228/*-------------------------------------------------
229    TIMER_CALLBACK( bit_tick )
230-------------------------------------------------*/
231
232static TIMER_CALLBACK( bit_tick )
233{
234   const device_config *device = (device_config *) param;
235   c1541_t *c1541 = get_safe_token(device);
236   int byte_ready = 0;
237
238   if (c1541->bit_pos == 0x07)
239   {
240      c1541->yb = c1541->track_buffer[c1541->buffer_pos];
241      c1541->buffer_pos++;
242      c1541->bit_pos = 0;
243
244      byte_ready = 1;
245   }
246   else
247   {
248      c1541->bit_pos++;
249   }
250
251   set_byte_ready(c1541, byte_ready);
252}
253
254/*-------------------------------------------------
189255    c1541_atn_w - serial bus attention
190256-------------------------------------------------*/
191257
r6607r6608
193259{
194260   c1541_t *c1541 = get_safe_token(device);
195261
196   via_ca1_w(c1541->via1, 0, state);
262   via_ca1_w(c1541->via0, 0, state);
197263}
198264
199265/*-------------------------------------------------
r6607r6608
202268
203269static WRITE_LINE_DEVICE_HANDLER( c1541_reset_w )
204270{
205   // reset cpu and vias
271   c1541_t *c1541 = get_safe_token(device);
272
273   if (state)
274   {
275      device_reset(c1541->cpu);
276      device_reset(c1541->via0);
277      device_reset(c1541->via1);
278   }
206279}
207280
208281/*-------------------------------------------------
r6607r6608
274347      PB1      STP0
275348      PB2      MTR
276349      PB3      ACT
277      PB4      WPS
278      PB5     
279      PB6     
280      PB7      SYNC
350      PB4      WPS input   write protect sensor
351      PB5      DS0
352      PB6      DS1
353      PB7      SYNC input
281354
282355   */
283356
284   return 0;
357   c1541_t *c1541 = get_safe_token(device);
358   UINT8 data = 0;
359
360   /* write protect sensor */
361   data |= (floppy_drive_get_flag_state(c1541->image, FLOPPY_DRIVE_DISK_WRITE_PROTECTED)) << 4;
362
363   /* SYNC detected */
364   data |= !(c1541->mode && (c1541->yb == 0xff)) << 7;
365
366   return data;
285367}
286368
287369static WRITE8_DEVICE_HANDLER( via0_pb_w )
r6607r6608
294376      PB1      STP0
295377      PB2      MTR
296378      PB3      ACT
297      PB4      WPS
298      PB5     
299      PB6     
300      PB7      SYNC
379      PB4      WPS input
380      PB5      DS0
381      PB6      DS1
382      PB7      SYNC input
301383
302384   */
385
386   c1541_t *c1541 = get_safe_token(device);
387   int stp = data & 0x03;
388   int ds = (data >> 5) & 0x03;
389
390   /* stepper motor */
391   if (c1541->stp != stp)
392   {
393      int tracks = 0;
394      int length;
395
396      switch (c1541->stp)
397      {
398      case 0:   if (stp == 1) tracks++; else if (stp == 3) tracks--; break;
399      case 1:   if (stp == 2) tracks++; else if (stp == 0) tracks--; break;
400      case 2: if (stp == 3) tracks++; else if (stp == 1) tracks--; break;
401      case 3: if (stp == 0) tracks++; else if (stp == 2) tracks--; break;
402      }
403
404      floppy_drive_seek(c1541->image, tracks);
405
406//      if (c1541->track < 0) c1541->track = 0; // BUMP!
407//      if (c1541->track > 83) c1541->track = 83;
408
409//      if (LOG) logerror("C1541 Track: %f\n", (float)c1541->track / 2);
410
411      floppy_drive_read_track_data_info_buffer(c1541->image, 0, c1541->track_buffer, &length);
412
413      if (LOG) logerror("C1541 Track length %u\n", length);
414
415      c1541->stp = stp;
416   }
417
418   /* spindle motor */
419   floppy_drive_set_motor_state(c1541->image, BIT(data, 2) ? FLOPPY_DRIVE_MOTOR_ON : 0);
420   timer_enable(c1541->bit_timer, BIT(data, 2));
421
422   /* activity LED */
423
424   /* data speed */
425   if (c1541->ds != ds)
426   {
427      timer_adjust_periodic(c1541->bit_timer, attotime_zero, 0, ATTOTIME_IN_HZ(C1541_BITRATE[ds]));
428      c1541->ds = ds;
429   }
303430}
304431
305432static WRITE_LINE_DEVICE_HANDLER( soe_w )
306433{
434   c1541_t *c1541 = get_safe_token(device);
435   int byte_ready = !(state && c1541->byte);
436
437   c1541->soe = state;
438
439   cpu_set_input_line(c1541->cpu, M6502_SET_OVERFLOW, byte_ready);
440   via_ca1_w(c1541->via0, 0, byte_ready);
307441}
308442
309443static WRITE_LINE_DEVICE_HANDLER( mode_w )
310444{
445   c1541_t *c1541 = get_safe_token(device);
446
447   c1541->mode = state;
311448}
312449
313450static const via6522_interface c1541_via0_intf =
r6607r6608
344481      PB2      CLK IN
345482      PB3      CLK OUT
346483      PB4      ATNA
347      PB5     
348      PB6     
484      PB5      J1
485      PB6      J2
349486      PB7      ATN IN
350487
351488   */
r6607r6608
359496   /* clock in */
360497   data |= cbmserial_clk_r(c1541->serial_bus) << 2;
361498
499   /* serial bus address */
500   data |= c1541->address << 5;
501
362502   /* attention in */
363503   data |= cbmserial_atn_r(c1541->serial_bus) << 7;
364504
r6607r6608
376516      PB2      CLK IN
377517      PB3      CLK OUT
378518      PB4      ATNA
379      PB5     
380      PB6     
519      PB5      J1
520      PB6      J2
381521      PB7      ATN IN
382522
383523   */
r6607r6608
539679   c1541_t *c1541 = get_safe_token(device);
540680   const c1541_config *config = get_safe_config(device);
541681
682   /* set serial address */
683   assert((config->address > 7) && (config->address < 12));
684   c1541->address = config->address - 8;
685
542686   /* find our CPU */
543687   c1541->cpu = device_find_child_by_tag(device, M6502_TAG);
544688
r6607r6608
547691   c1541->via1 = device_find_child_by_tag(device, M6522_1_TAG);
548692   c1541->serial_bus = devtag_get_device(device->machine, config->serial_bus_tag);
549693
694   /* allocate data timer */
695   c1541->bit_timer = timer_alloc(device->machine, bit_tick, (void *)device);
696   timer_adjust_periodic(c1541->bit_timer, attotime_zero, 0, ATTOTIME_IN_HZ(C1541_BITRATE[0]));
697
550698   /* register for state saving */
551699//   state_save_register_device_item(device, 0, c1541->);
552700}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team