Previous 15994 Revisions Next

r9561 Saturday 13th November, 2010 at 00:05:21 UTC by Michael Zapf
Fixed CRC calculation in write_sector
[src/mess/formats]ti99_dsk.c

src/mess/formats/ti99_dsk.c
r9560r9561
55 * TI99 and Geneve disk images
66 * Sector Dump and Track Dump format
77 *
8 * used by TI-99/4, TI-99/4A, TI-99/8, TI-99/4P, and Geneve
8 * used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve
99 *
1010 * The Sector Dump Format is also known as v9t9 (named after the first TI
1111 * emulator to use this format). It is a contiguous sequence of sector contents
r9560r9561
6363 *
6464 * TODO: Link to imgtool. imgtool still uses its own format creation
6565 *
66 * TODO: Test with diverse disks, inserted at the start and in the meantime
67 *       Check delay
68 *
69 *
7066 * FIXME: If image is broken with good first track and defect higher tracks,
7167 *        tdf_guess_geometry fails to define a geometry. The guessing should
7268 *        always be done.
r9560r9561
104100   int heads;
105101   int tracks;
106102   int sectors;
107   int track_size;  /* Complete track; to be calculated */
103   int track_size;  // Complete track; to be calculated
108104   int format;
109105   int first_idam;
110106   int dam_offset;
r9560r9561
124120}
125121
126122#define TI99_DSK_TAG   "ti99dsktag"
127
128#if 0
129static void dump_contents(UINT8 *buffer, int length)
130{
131   for (int i=0; i < length; i+=16)
132   {
133      for (int j=0; j < 16; j+=2)
134      {
135         printf("%02x%02x ", buffer[i+j], buffer[i+j+1]);
136      }
137      printf("\n");
138   }
139}
140#endif
141
142123#define TI99DSK_BLOCKNOTFOUND -1
143124
144125/*
r9560r9561
174155*/
175156static int ti99_get_heads_per_disk(floppy_image *floppy)
176157{
177/*  struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
178    return tag->heads; */
158//  struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
159//  return tag->heads;
179160   return 2;
180161}
181162
r9560r9561
187168{
188169   int drivetracks = 42;
189170
190   /* This may fail on startup; therefore we use a special function in
191       99_dsk to explicitly set the geometry. */
171   // This may fail on startup; therefore we use a special function in
172   // in the floppy controller implementations to explicitly set the geometry.
192173   if (use_80_track_drives)
193174   {
194175      drivetracks = 83;
r9560r9561
296277   int totsecs;
297278   typedef struct ti99_vib
298279   {
299      char   name[10];      /* volume name (10 characters, pad with spaces) */
300      UINT8   totsecsMSB;      /* disk length in sectors (big-endian) (usually 360, 720 or 1440) */
280      char   name[10];      // volume name (10 characters, pad with spaces)
281      UINT8   totsecsMSB;      // disk length in sectors (big-endian) (usually 360, 720 or 1440)
301282      UINT8   totsecsLSB;
302      UINT8   secspertrack;      /* sectors per track (usually 9 (FM) or 18 (MFM)) */
303      UINT8   id[3];         /* String "DSK" */
304      UINT8   protection;      /* 'P' if disk is protected, ' ' otherwise. */
305      UINT8   tracksperside;      /* tracks per side (usually 40) */
306      UINT8   sides;         /* sides (1 or 2) */
307      UINT8   density;      /* 0,1 (FM) or 2,3,4 (MFM) */
308      UINT8   res[36];      /* Empty for traditional disks, or up to 3 directory pointers */
309      UINT8   abm[200];      /* allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0, sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.) */
283      UINT8   secspertrack;   // sectors per track (usually 9 (FM) or 18 (MFM))
284      UINT8   id[3];         // String "DSK"
285      UINT8   protection;      // 'P' if disk is protected, ' ' otherwise.
286      UINT8   tracksperside;   // tracks per side (usually 40)
287      UINT8   sides;         // sides (1 or 2)
288      UINT8   density;      // 0,1 (FM) or 2,3,4 (MFM)
289      UINT8   res[36];      // Empty for traditional disks, or up to 3 directory pointers
290      UINT8   abm[200];      // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0,
291                        // sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.)
310292   } ti99_vib;
311293
312294   ti99_vib vib;
r9560r9561
323305
324306   file_size = (UINT32) size;
325307
326   /* Read the Volume Information Block */
308   // Read the Volume Information Block
327309   floppy_image_read(floppy, &vib, 0, sizeof(vib));
328310
329   /* If we have read the sector successfully, let us parse it */
311   // If we have read the sector successfully, let us parse it
330312   totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB;
331313   geometry->secspertrack = vib.secspertrack;
332314   if (geometry->secspertrack == 0)
333      /* Some images might be like this, because the original SSSD TI controller always assumes 9. */
315      // Some images might be like this, because the original SSSD
316      // TI controller always assumes 9.
334317      geometry->secspertrack = 9;
335318   geometry->tracksperside = vib.tracksperside;
336319   if (geometry->tracksperside == 0)
337      /* Some images are like this, because the original SSSD TI controller always assumes 40. */
320      // Some images are like this, because the original SSSD TI controller
321      // always assumes 40.
338322      geometry->tracksperside = 40;
339323   geometry->sides = vib.sides;
340324   if (geometry->sides == 0)
341      /* Some images are like this, because the original SSSD TI controller always assumes that tracks beyond 40 are on side 2. */
325      // Some images are like this, because the original SSSD TI controller
326      // always assumes that tracks beyond 40 are on side 2. */
342327      geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside);
343328   geometry->density = vib.density;
344   /* check that the format makes sense */
329   // check that the format makes sense
345330   if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs)
346331      && (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3))
347332      && (file_size == totsecs*256))
r9560r9561
351336   }
352337   logerror("SDF/VIB not consistent; guessing format\n");
353338
354   /* So that was not consistent. We guess the size from the file size
355       and assume that the VIB did not contain reliable data. For the
356       ambiguous case we choose the most common format.
357    */
339   // So that was not consistent. We guess the size from the file size
340   // and assume that the VIB did not contain reliable data. For the
341   // ambiguous case we choose the most common format.
342
358343   switch (file_size)
359344   {
360345   case 1*40*9*256:
361      /* 90kbytes: SSSD */
346      // 90kbytes: SSSD
362347   case 0:
363348   /*default:*/
364349      geometry->sides = 1;
r9560r9561
368353      break;
369354
370355   case 2*40*9*256:
371      /* 180kbytes: either DSSD or 18-sector-per-track SSDD.
372        We assume DSSD since DSSD is more common and is supported by
373        the original TI SD disk controller. */
356      // 180kbytes: either DSSD or 18-sector-per-track SSDD.
357      // We assume DSSD since DSSD is more common and is supported by
358      // the original TI SD disk controller.
374359      geometry->sides = 2;
375360      geometry->tracksperside = 40;
376361      geometry->secspertrack = 9;
r9560r9561
378363      break;
379364
380365   case 1*40*16*256:
381      /* 160kbytes: 16-sector-per-track SSDD (standard format for TI
382        DD disk controller prototype, and the TI hexbus disk
383        controller?) */
366      // 160kbytes: 16-sector-per-track SSDD (standard format for TI
367      // DD disk controller prototype, and the TI hexbus disk
368      // controller?) */
384369      geometry->sides = 1;
385370      geometry->tracksperside = 40;
386371      geometry->secspertrack = 16;
r9560r9561
388373      break;
389374
390375   case 2*40*16*256:
391      /* 320kbytes: 16-sector-per-track DSDD (standard format for TI
392        DD disk controller prototype, and TI hexbus disk
393        controller?) */
376      // 320kbytes: 16-sector-per-track DSDD (standard format for TI
377      // DD disk controller prototype, and TI hexbus disk
378      // controller?)
394379      geometry->sides = 2;
395380      geometry->tracksperside = 40;
396381      geometry->secspertrack = 16;
r9560r9561
398383      break;
399384
400385   case 2*40*18*256:
401      /* 360kbytes: 18-sector-per-track DSDD (standard format for most
402        third-party DD disk controllers, but reportedly not supported by
403        the original TI DD disk controller prototype) */
386      //  360kbytes: 18-sector-per-track DSDD (standard format for most
387      // third-party DD disk controllers, but reportedly not supported by
388      // the original TI DD disk controller prototype)
404389      geometry->sides = 2;
405390      geometry->tracksperside = 40;
406391      geometry->secspertrack = 18;
r9560r9561
408393      break;
409394
410395   case 2*80*18*256:
411      /* 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only) */
396      // 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only)
412397      geometry->sides = 2;
413398      geometry->tracksperside = 80;
414399      geometry->secspertrack = 18;
r9560r9561
416401      break;
417402
418403   case 2*80*36*256:
419      /* 1.44Mbytes: DSHD (Myarc only) */
404      // 1.44Mbytes: DSHD (Myarc only)
420405      geometry->sides = 2;
421406      geometry->tracksperside = 80;
422407      geometry->secspertrack = 36;
r9560r9561
510495   /* Determine from the image geometry whether we have FM or MFM */
511496   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
512497
513   /* Do we have a 40 track disk in an 80 track drive? In that case we
514       double the "width" of the tracks. */
498   // Do we have a 40 track disk in an 80 track drive? In that case we
499   // double the "width" of the tracks. */
515500   if (use_80_track_drives && tag->tracks<=40)
516501   {
517502      imgtrack = track / 2;
r9560r9561
646631   struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
647632   int imgtrack = track;
648633
649   /* Find out if we are going to write an FM or MFM track. Caution: The
650    original TI controller is known to have a bug in the formatting
651    routine (using a MOVB instead of a MOV instruction when setting the
652    pointer to the track data), outputting false bytes at the
653    beginning of the track. So we don't rely on absolute positions
654    (neither does the controller).
655    */
634   // Find out if we are going to write an FM or MFM track. Note that the
635   // original TI controller is known to have a bug in the formatting
636   // routine (using a MOVB instead of a MOV instruction when setting the
637   // pointer to the track data), outputting false bytes at the
638   // beginning of the track. So we do not rely on absolute positions
639   // (and neither does the controller).
656640   track_image = (UINT8*)buffer;
657641
658642   if (use_80_track_drives && tag->tracks<=40)
r9560r9561
668652
669653   current_pos = find_block(track_image, 0, 100, 0x4e, leadin);
670654
671   /* In case of defect formats, we continue as far as possible. This
672    may lead to sectors not being written. */
655   // In case of defect formats, we continue as far as possible. This
656   // may lead to sectors not being written. */
673657   if (current_pos==TI99DSK_BLOCKNOTFOUND)
674658   {
675659      /* Not MFM, possibly FM image */
r9560r9561
741725         }
742726         else
743727         {
744            /* else the sector head data do not match the
745                current track and head. Ignore the sector. */
728            // else the sector head data do not match the
729            // current track and head. Ignore the sector.
746730            logerror("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head);
747731         }
748732      }
r9560r9561
11251109   if (format==TI99_FM)
11261110   {
11271111//      printf("Trying FM\n");
1128      /* We are seeking the lead-in and pre-id gap from
1129        position 0 to position 50. This is just to make sure that
1130        if there are bad bytes at the beginning (original TI controller)
1131        we will skip them.*/
1112      // We are seeking the lead-in and pre-id gap from
1113      // position 0 to position 50. This is just to make sure that
1114      // if there are bad bytes at the beginning (original TI controller)
1115      // we will skip them.
11321116      current_pos = find_block(track, 0, 50, 0x00, 22);
11331117      if (current_pos==TI99DSK_BLOCKNOTFOUND)
11341118      {
r9560r9561
11391123      {
11401124         current_pos += 22;
11411125         if (track[current_pos]==0xfe /* IDAM */ &&
1142             track[current_pos+4]==0x01 /* sector length, always 256 for TI */ &&
1143                 track[current_pos+24]==0xfb) /* DAM */
1126            track[current_pos+4]==0x01 /* sector length, always 256 for TI */ &&
1127            track[current_pos+24]==0xfb) /* DAM */
11441128         {
11451129            /* We're pretty sure this is the beginning. */
11461130            *offset = current_pos;
r9560r9561
11761160         {
11771161            current_pos += 10;
11781162            if (track[current_pos] == 0xa1 /* First sync */ &&
1179                track[current_pos+3] == 0xfe /* IDAM */ &&
1180                track[current_pos+44] == 0xa1 /* First sync */ &&
1181                track[current_pos+47] == 0xfb) /* DAM */
1163               track[current_pos+3] == 0xfe /* IDAM */ &&
1164               track[current_pos+44] == 0xa1 /* First sync */ &&
1165               track[current_pos+47] == 0xfb) /* DAM */
11821166            {
11831167            /* We're pretty sure this is the beginning. */
11841168               *offset = current_pos + 3;
r9560r9561
12461230   if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks))
12471231      return FLOPPY_ERROR_SEEKERROR;
12481232
1249   /* For head 0, tracks increase from 0 to tracks-1; then for head 1,
1250       tracks also increase from 0 to tracks-1. */
1233   // For head 0, tracks increase from 0 to tracks-1; then for head 1,
1234   // tracks also increase from 0 to tracks-1. */
12511235   *offset = head * (tag->tracks * tag->track_size) +  imgtrack * tag->track_size;
12521236
12531237   return FLOPPY_ERROR_SUCCESS;
r9560r9561
12741258
12751259   floppy_image_read(floppy, buffer, track_offset, buflen);
12761260
1277   /* Rebuild the CRCs. PC99 did not store the CRC but put
1278    F7F7 in its place.
1279    The first CRC is at position IDAM + 5
1280    The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM)
1281    All this is repeated for each sector in the track
1282    */
1261   // Rebuild the CRCs. PC99 did not store the CRC but put
1262   // F7F7 in its place.
1263   // The first CRC is at position IDAM + 5
1264   // The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM)
1265   // All this is repeated for each sector in the track
1266
12831267   if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
12841268      return FLOPPY_ERROR_SEEKERROR;
12851269
r9560r9561
12891273      byte = read_byte(tag->format, first_idam, track_data, i);
12901274      if (byte == TI99_IDAM)
12911275      {
1292         /* Do we have a valid CRCs already? Then this image
1293            already contains proper CRCs handling. Do not
1294            recreate them. */
1276         // Do we have a valid CRCs already? Then this image
1277         // already contains proper CRCs handling. Do not recreate them.
12951278         if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7)
12961279            return FLOPPY_ERROR_SUCCESS;
12971280
r9560r9561
13491332   if (err)
13501333      return err;
13511334
1352   /* According to the TDF format, we don't need the CRC but replace it
1353    with F7F7. For now we keep it and see whether PC99 can cope with
1354    that. (Otherwise we would have to copy the const buffer). */
1335   // According to the TDF format, we don't need the CRC but replace it
1336   // with F7F7. For now we keep it and see whether PC99 can cope with
1337   // that. (Otherwise we would have to copy the const buffer). */
13551338   floppy_image_write(floppy, buffer, offset + track_offset, buflen);
13561339   return FLOPPY_ERROR_SUCCESS;
13571340}
r9560r9561
14971480
14981481   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
14991482
1500   /* Recalculate CRC */
1501   crc = ccitt_crc16(0xffff, (UINT8*)buffer, SECTOR_SIZE);
1483   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1484   // already included; would be ffff without)
1485   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
15021486   crc_field[0] = (crc>>8) & 0xff;
15031487   crc_field[1] = (crc & 0xff);
15041488   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
r9560r9561
15971581   /* Write the sector data at that position. */
15981582   floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
15991583
1600   /* Recalculate CRC */
1601   crc = ccitt_crc16(0xffff, (UINT8*)buffer, SECTOR_SIZE);
1584   // Recalculate CRC. We init the CRC with bf84 (DAM fb is
1585   // already included; would be ffff without)
1586   crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
16021587   crc_field[0] = (crc>>8) & 0xff;
16031588   crc_field[1] = (crc & 0xff);
16041589   floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
r9560r9561
17801765      if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
17811766      {
17821767         logerror("IDAM not found. Unformatted disk.\n");
1783         /* FM failed as well. Disk is not formatted. We assume
1784               a format that fits into the space provided by the
1785               file.
1786               We only give a moderate vote in this case, so SDF
1787               make take it.
1788            */
1768
1769         // FM failed as well. Disk is not formatted. We assume
1770         // a format that fits into the space provided by the file.
1771         // We only give a moderate vote in this case, so SDF
1772         // make take it.
1773
17891774         free(track_data);
17901775         if (size < 130120 || size > 2078720)
17911776         {
r9560r9561
19051890   }
19061891   logerror("Determined %d sectors\n", idamcnt);
19071892
1908   /* Now calculate the geometry
1909       The track size, in theory, may change due to reformatting,
1910       and it may also vary between tracks, but the TDF does not support
1911       varying track lengths. So every track has the same length as track 0.
1912       Also, we don't yet know whether the drive and the disk have the
1913       same track count. For 80 track drives and 40 track disks, we need
1914       double-stepping; this will be known as soon as the controller is
1915       initialized; we need to check that in the access methods.
1916    */
1893   // Now calculate the geometry
1894   // The track size, in theory, may change due to reformatting,
1895   // and it may also vary between tracks, but the TDF does not support
1896   // varying track lengths. So every track has the same length as track 0.
1897   // Also, we don't yet know whether the drive and the disk have the
1898   // same track count. For 80 track drives and 40 track disks, we need
1899   // double-stepping; this will be known as soon as the controller is
1900   // initialized; we need to check that in the access methods.
19171901
19181902   tracklength = idamcnt * totalseclen + trackadd;
19191903   /*offset = 0;*/
19201904
1921   /* Read the last track. We assume that all tracks are properly
1922       formatted; otherwise, the above calculations would already fail.
1923       That is, the last track in the image tells us whether it is for
1924       head 0 or for head 1.
1925    */
1905   // Read the last track. We assume that all tracks are properly
1906   // formatted; otherwise, the above calculations would already fail.
1907   // That is, the last track in the image tells us whether it is for
1908   // head 0 or for head 1.
19261909
19271910   geometry->sides = 1;
19281911   floppy_image_read(floppy, track_data, size-tracklength, tracklength);

Previous 15994 Revisions Next


© 1998-2018 The MESS Team