Previous 15994 Revisions Next

r7123 Friday 22nd January, 2010 at 07:54:55 UTC by Curt Coder
[C64] Added read-only access to D64 floppies.
[docs]messnew.txt
[src/mess]mess.mak
[src/mess/formats]d64_dsk.c* d64_dsk.h*
[src/mess/machine]c1541.c c1551.c c1571.c

src/mess/machine/c1541.c
r7122r7123
141141#include "c1541.h"
142142#include "cpu/m6502/m6502.h"
143143#include "devices/flopdrv.h"
144#include "formats/d64_dsk.h"
144145#include "formats/g64_dsk.h"
145146#include "machine/6522via.h"
146147#include "machine/cbmiec.h"
r7122r7123
945946-------------------------------------------------*/
946947
947948static FLOPPY_OPTIONS_START( c1541 )
948//  FLOPPY_OPTION( c1541, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
949949   FLOPPY_OPTION( c1541, "g64", "Commodore 1541 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
950   FLOPPY_OPTION( c1541, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
950951FLOPPY_OPTIONS_END
951952
952953/*-------------------------------------------------
src/mess/machine/c1551.c
r7122r7123
2121#include "c1551.h"
2222#include "cpu/m6502/m6502.h"
2323#include "devices/flopdrv.h"
24#include "formats/d64_dsk.h"
2425#include "formats/g64_dsk.h"
2526#include "machine/6525tpi.h"
2627
r7122r7123
419420-------------------------------------------------*/
420421
421422static FLOPPY_OPTIONS_START( c1551 )
422//  FLOPPY_OPTION( c1551, "d64", "Commodore 1551 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
423   FLOPPY_OPTION( c1551, "g64", "Commodore 1551 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
423   FLOPPY_OPTION( c1551, "g64", "Commodore 1541 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
424   FLOPPY_OPTION( c1551, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
424425FLOPPY_OPTIONS_END
425426
426427/*-------------------------------------------------
src/mess/machine/c1571.c
r7122r7123
2525#include "c1571.h"
2626#include "cpu/m6502/m6502.h"
2727#include "devices/flopdrv.h"
28#include "formats/d64_dsk.h"
2829#include "formats/g64_dsk.h"
2930#include "machine/6522via.h"
3031#include "machine/6526cia.h"
r7122r7123
725726
726727static FLOPPY_OPTIONS_START( c1571 )
727728   FLOPPY_OPTION( c1571, "g64", "Commodore 1541 GCR Disk Image", g64_dsk_identify, g64_dsk_construct, NULL )
728//  FLOPPY_OPTION( c1571, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
729   FLOPPY_OPTION( c1571, "d64", "Commodore 1541 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
729730//  FLOPPY_OPTION( c1571, "d71", "Commodore 1571 Disk Image", d64_dsk_identify, d64_dsk_construct, NULL )
730731FLOPPY_OPTIONS_END
731732
src/mess/formats/d64_dsk.c
r0r7123
1/*
2
3 Commodore GCR format
4
5Original    Encoded
64 bits      5 bits
7
80000    ->  01010 = 0x0a
90001    ->  01011 = 0x0b
100010    ->  10010 = 0x12
110011    ->  10011 = 0x13
120100    ->  01110 = 0x0e
130101    ->  01111 = 0x0f
140110    ->  10110 = 0x16
150111    ->  10111 = 0x17
161000    ->  01001 = 0x09
171001    ->  11001 = 0x19
181010    ->  11010 = 0x1a
191011    ->  11011 = 0x1b
201100    ->  01101 = 0x0d
211101    ->  11101 = 0x1d
221110    ->  11110 = 0x1e
231111    ->  10101 = 0x15
24
25We use the encoded values in bytes because we use them to encode
26groups of 4 bytes into groups of 5 bytes, below.
27
28*/
29
30/*
31
32    TODO:
33
34   - cleanup
35    - disk errors
36   - D71
37   - D80/D82
38   - variable gaps
39
40*/
41
42#include "emu.h"
43#include "formats/flopimg.h"
44#include "formats/d64_dsk.h"
45#include "devices/flopdrv.h"
46
47#define LOG 1
48
49#define HEADER_LENGTH   0x2ac       /* standard length for 84 half tracks */
50#define HALF_TRACKS      84
51#define OFFSET_INVALID   0xbadbad
52
53#define D64_MAX_TRACKS         35 * 2
54#define D64_40T_MAX_TRACKS     40 * 2
55#define D71_MAX_TRACKS         70
56#define D81_MAX_TRACKS 80
57#define D80_MAX_TRACKS 77
58/* Still have to investigate if these are read as 154 or as 77 in the drive... */
59/* For now we assume it's 154, but I'm not sure */
60#define D82_MAX_TRACKS 154
61
62static const int d64_sectors_per_track[] =
63{
64   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
65   19, 19, 19, 19, 19, 19, 19,
66   18, 18, 18, 18, 18, 18,
67   17, 17, 17, 17, 17,
68   17, 17, 17, 17, 17,      /* only for 40 tracks d64 */
69   17, 17               /* in principle the drive could read 42 tracks */
70};
71
72#if 0
73static const int d71_sectors_per_track[] =
74{
75   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
76   19, 19, 19, 19, 19, 19, 19,
77   18, 18, 18, 18, 18, 18,
78   17, 17, 17, 17, 17,
79   21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
80   19, 19, 19, 19, 19, 19, 19,
81   18, 18, 18, 18, 18, 18,
82   17, 17, 17, 17, 17
83};
84
85/* Each track in a d81 file has 40 sectors, so no need of a sectors_per_track table */
86
87static const int d80_sectors_per_track[] =
88{
89   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
90   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
91   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
92   25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
93   23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23
94};
95
96static const int d82_sectors_per_track[] =
97{
98   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
99   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
100   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
101   25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
102   23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
103   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
104   29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
105   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
106   25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
107   23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23
108};
109
110
111static const int max_tracks_per_format[] =
112{
113   D64_MAX_TRACKS,
114   D64_MAX_TRACKS,
115   D64_40T_MAX_TRACKS,
116   D64_40T_MAX_TRACKS,
117   D64_MAX_TRACKS,
118   D71_MAX_TRACKS,
119   D71_MAX_TRACKS,
120   D81_MAX_TRACKS,
121   D80_MAX_TRACKS,
122   D82_MAX_TRACKS,
123   0
124};
125
126#endif
127
128struct d64dsk_tag
129{
130   int heads;
131   int tracks;
132   int track_offset[HALF_TRACKS];         /* offset within data for each half track */
133   UINT32 speed_zone_offset[HALF_TRACKS];   /* speed zone for each half track */
134   int error[HALF_TRACKS];               /* DOS error code for each half track */
135
136   int id1, id2;
137};
138
139INLINE float get_track_index(int track)
140{
141   return ((float)track / 2) + 1;
142}
143
144static struct d64dsk_tag *get_tag(floppy_image *floppy)
145{
146   struct d64dsk_tag *tag;
147   tag = (d64dsk_tag *)floppy_tag(floppy);
148   return tag;
149}
150
151static int d64_get_heads_per_disk(floppy_image *floppy)
152{
153   return get_tag(floppy)->heads;
154}
155
156static int d64_get_tracks_per_disk(floppy_image *floppy)
157{
158   return get_tag(floppy)->tracks;
159}
160
161static floperr_t get_track_offset(floppy_image *floppy, int track, UINT64 *offset)
162{
163   struct d64dsk_tag *tag = get_tag(floppy);
164   UINT64 offs = 0;
165
166   if ((track < 0) || (track >= tag->tracks))
167      return FLOPPY_ERROR_SEEKERROR;
168
169   offs = tag->track_offset[track];
170
171   if (offset)
172      *offset = offs;
173
174   return FLOPPY_ERROR_SUCCESS;
175}
176
177static const int bin_2_gcr[] =
178{
179   0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17,
180   0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15
181};
182
183/* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */
184/*
185static const int gcr_2_bin[] =
186{
187    -1, -1,   -1,   -1,
188    -1, -1,   -1,   -1,
189    -1, 0x08, 0x00, 0x01,
190    -1, 0x0c, 0x04, 0x05,
191    -1, -1,   0x02, 0x03,
192    -1, 0x0f, 0x06, 0x07,
193    -1, 0x09, 0x0a, 0x0b,
194    -1, 0x0d, 0x0e, -1
195};
196*/
197
198/* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */
199/* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d)
200 * with res being 5 bytes long and enc(x) being the GCR encode of x.
201 * In fact, we store the result as five separate bytes in the dest argument
202 */
203static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest)
204{
205   UINT8 gcr[8];
206
207   /* Encode each nibble to 5 bits */
208   gcr[0] = bin_2_gcr[a >> 4];
209   gcr[1] = bin_2_gcr[a & 0x0f];
210   gcr[2] = bin_2_gcr[b >> 4];
211   gcr[3] = bin_2_gcr[b & 0x0f];
212   gcr[4] = bin_2_gcr[c >> 4];
213   gcr[5] = bin_2_gcr[c & 0x0f];
214   gcr[6] = bin_2_gcr[d >> 4];
215   gcr[7] = bin_2_gcr[d & 0x0f];
216
217   /* Re-order the encoded data to only keep the 5 lower bits of each byte */
218   dest[0] = (gcr[0] << 3) | (gcr[1] >> 2);
219   dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4);
220   dest[2] = (gcr[3] << 4) | (gcr[4] >> 1);
221   dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3);
222   dest[4] = (gcr[6] << 5) | gcr[7];
223}
224
225static floperr_t d64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
226{
227   floperr_t err;
228   UINT64 track_offset;
229
230   /* get track offset */
231   err = get_track_offset(floppy, track, &track_offset);
232
233   if (err)
234      return err;
235
236   if (track_offset != OFFSET_INVALID)
237   {
238      int gcr_pos = 2, i, sector, d64_pos;
239      int id1 = get_tag(floppy)->id1;
240      int id2 = get_tag(floppy)->id2;
241      int full_track = track / 2;
242      int dos_track = full_track + 1;
243      int sectors_per_track = d64_sectors_per_track[full_track];
244
245      UINT16 d64_track_size = sectors_per_track * 256;
246      UINT16 gcr_track_size = 2 + (sectors_per_track * 368);
247
248      UINT8 d64_track_data[d64_track_size];
249      UINT8 gcr_track_data[gcr_track_size];
250
251      UINT8 sector_checksum;
252
253      if (buflen < gcr_track_size) fatalerror("D64 track buffer too small: %u!\n", (UINT32)buflen);
254
255      /* set GCR track size to buffer */
256      gcr_track_data[0] = gcr_track_size & 0xff;
257      gcr_track_data[1] = gcr_track_size >> 8;
258
259      floppy_image_read(floppy, d64_track_data, track_offset, d64_track_size);
260
261      for (sector = 0; sector < sectors_per_track; sector++)
262      {
263         // here we convert the sector data to gcr directly!
264         // IMPORTANT: we shall implement errors in reading sectors!
265         // these can modify e.g. header info $01 & $05
266
267         // first we set the position at which sector data starts in the image
268         d64_pos = sector * 256;
269
270         /*
271                1. Header sync       FF FF FF FF FF (40 'on' bits, not GCR encoded)
272                2. Header info       52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes)
273                3. Header gap        55 55 55 55 55 55 55 55 55 (9 bytes, never read)
274                4. Data sync         FF FF FF FF FF (40 'on' bits, not GCR encoded)
275                5. Data block        55...4A (325 GCR bytes)
276                6. Inter-sector gap  55 55 55 55...55 55 (4 to 19 bytes, never read)
277            */
278
279         /* Header sync */
280         for (i = 0; i < 5; i++)
281            gcr_track_data[gcr_pos + i] = 0xff;
282         gcr_pos += 5;
283
284         /* Header info */
285         /* These are 8 bytes unencoded, which become 10 bytes encoded */
286         // $00 - header block ID ($08)                      // this byte can be modified by error code 20 -> 0xff
287         // $01 - header block checksum (EOR of $02-$05)     // this byte can be modified by error code 27 -> ^ 0xff
288         // $02 - Sector# of data block
289         // $03 - Track# of data block
290         gcr_double_2_gcr(0x08, sector ^ dos_track ^ id2 ^ id1, sector, dos_track, gcr_track_data + gcr_pos);
291         gcr_pos += 5;
292
293         // $04 - Format ID byte #2
294         // $05 - Format ID byte #1
295         // $06 - $0F ("off" byte)
296         // $07 - $0F ("off" byte)
297         gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr_track_data + gcr_pos);
298         gcr_pos += 5;
299
300         /* Header gap */
301         for (i = 0; i < 9; i++)
302            gcr_track_data[gcr_pos + i] = 0x55;
303         gcr_pos += 9;
304
305         /* Data sync */
306         for (i = 0; i < 5; i++)
307            gcr_track_data[gcr_pos + i] = 0xff;
308         gcr_pos += 5;
309
310         /* Data block */
311         // we first need to calculate the checksum of the 256 bytes of the sector
312         sector_checksum = d64_track_data[d64_pos];
313         for (i = 1; i < 256; i++)
314            sector_checksum ^= d64_track_data[d64_pos + i];
315
316         /*
317                $00      - data block ID ($07)
318                $01-100  - 256 bytes sector data
319                $101     - data block checksum (EOR of $01-100)
320                $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5)
321            */
322         gcr_double_2_gcr(0x07, d64_track_data[d64_pos], d64_track_data[d64_pos + 1], d64_track_data[d64_pos + 2], gcr_track_data + gcr_pos);
323         gcr_pos += 5;
324
325         for (i = 1; i < 64; i++)
326         {
327            gcr_double_2_gcr(d64_track_data[d64_pos + 4 * i - 1], d64_track_data[d64_pos + 4 * i],
328                           d64_track_data[d64_pos + 4 * i + 1], d64_track_data[d64_pos + 4 * i + 2], gcr_track_data + gcr_pos);
329            gcr_pos += 5;
330         }
331
332         gcr_double_2_gcr(d64_track_data[d64_pos + 255], sector_checksum, 0x00, 0x00, gcr_track_data + gcr_pos);
333         gcr_pos += 5;
334
335         /* Inter-sector gap */
336         // "In tests that the author conducted on a real 1541 disk, gap sizes of 8  to  19 bytes were seen."
337         // Here we put 14 as an average...
338         for (i = 0; i < 14; i++)
339            gcr_track_data[gcr_pos + i] = 0x55;
340         gcr_pos += 14;
341      }
342
343      /* copy GCR track data to buffer */
344      memcpy(buffer, gcr_track_data, gcr_track_size);
345   }
346   else   /* half tracks */
347   {
348      /* set track length to 0 */
349      memset(buffer, 0, buflen);
350   }
351
352   return FLOPPY_ERROR_SUCCESS;
353}
354
355static floperr_t d64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
356{
357   return FLOPPY_ERROR_UNSUPPORTED;
358}
359
360FLOPPY_IDENTIFY( d64_dsk_identify )
361{
362   UINT64 size = floppy_image_size(floppy);
363
364   if ((size == 0x2ab00) || (size == 0x2ab00 + 0x2ab) || (size == 0x30000) || (size == 0x30000 + 0x300))
365   {
366      *vote = 100;
367   }
368   else
369   {
370      *vote = 0;
371   }
372
373   return FLOPPY_ERROR_SUCCESS;
374}
375
376FLOPPY_CONSTRUCT( d64_dsk_construct )
377{
378   struct FloppyCallbacks *callbacks;
379   struct d64dsk_tag *tag;
380   UINT8 id[2];
381   UINT64 size = floppy_image_size(floppy);   // needed to set track #
382   int track_offset = 0;
383   int track_count = 0;
384   int i;
385
386   if (params)
387   {
388      /* create not supported */
389      return FLOPPY_ERROR_UNSUPPORTED;
390   }
391
392   tag = (struct d64dsk_tag *) floppy_create_tag(floppy, sizeof(struct d64dsk_tag));
393
394   if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;
395
396   /* number of half tracks */
397   if ((size == 0x2ab00) || (size == 0x2ab00 + 0x2ab))
398      track_count = D64_MAX_TRACKS;
399
400   if ((size == 0x30000) || (size == 0x30000 + 0x300))
401      track_count = D64_40T_MAX_TRACKS;
402
403   if (LOG) logerror("D64 tracks: %u\n", track_count / 2);
404
405   tag->heads = 1;
406   tag->tracks = 84;
407
408   /* data offsets */
409   for (i = 0; i < tag->tracks; i++)
410   {
411      if ((i % 2) || (i >= track_count))
412      {
413         /* half track */
414         tag->track_offset[i] = OFFSET_INVALID;
415      }
416      else
417      {
418         /* full track */
419         tag->track_offset[i] = track_offset;
420         track_offset += d64_sectors_per_track[i / 2] * 0x100;
421      }
422
423      if (LOG) logerror("D64 track %.1f data offset: %04x\n", get_track_index(i), tag->track_offset[i]);
424   }
425
426   /* speed zone offsets */
427   for (i = 0; i < tag->tracks; i++)
428   {
429      if (i < 0x11)
430         tag->speed_zone_offset[2 * i] = 3;
431      else if (i < 0x18)
432         tag->speed_zone_offset[2 * i] = 2;
433      else if (i < 0x1e)
434         tag->speed_zone_offset[2 * i] = 1;
435      else
436         tag->speed_zone_offset[2 * i] = 0;
437
438      tag->speed_zone_offset[2 * i + 1] = 0;
439
440      if (LOG) logerror("D64 track %.1f speed zone: %u\n", get_track_index(i), tag->speed_zone_offset[i]);
441   }
442
443   floppy_image_read(floppy, id, get_tag(floppy)->track_offset[34] + 0xa2, 2);
444
445   get_tag(floppy)->id1 = id[0];
446   get_tag(floppy)->id2 = id[1];
447
448   /* set callbacks */
449   callbacks = floppy_callbacks(floppy);
450
451   callbacks->read_track = d64_read_track;
452   callbacks->write_track = d64_write_track;
453   callbacks->get_heads_per_disk = d64_get_heads_per_disk;
454   callbacks->get_tracks_per_disk = d64_get_tracks_per_disk;
455
456   return FLOPPY_ERROR_SUCCESS;
457}
458
459/*
460id1, id2 are the same for extended d64 (i.e. with error tables), for d67 and for d71
461
462for d81 they are at track 40 bytes 0x17 & 0x18
463for d80 & d82 they are at track 39 bytes 0x18 & 0x19
464*/
src/mess/formats/d64_dsk.h
r0r7123
1/*********************************************************************
2
3    formats/d64_dsk.h
4
5    Floppy format code for Commodore 1541 D64 disk images
6
7*********************************************************************/
8
9#ifndef __D64_DSK__
10#define __D64_DSK__
11
12#include "formats/flopimg.h"
13
14FLOPPY_IDENTIFY( d64_dsk_identify );
15FLOPPY_CONSTRUCT( d64_dsk_construct );
16
17#endif
src/mess/mess.mak
r7122r7123
707707   $(MESS_MACHINE)/c1571.o      \
708708   $(MESS_MACHINE)/c1581.o      \
709709   $(MESS_MACHINE)/c2040.o      \
710   $(MESS_FORMATS)/d64_dsk.o   \
710711   $(MESS_FORMATS)/g64_dsk.o   \
711712   $(MESS_DRIVERS)/clcd.o      \
712713
docs/messnew.txt
r7122r7123
2424
2525System Driver Changes:
2626----------------------
27- [C64] Added read-only access to D64 floppies. [Fabio Priuli, Curt Coder]
28
2729- [C128] Fast IEC serial bus is now supported with a 1571. [Curt Coder]
2830
2931- [MIKROMIK] Fixed memory banking regression. [Curt Coder]

Previous 15994 Revisions Next


© 1998-2018 The MESS Team