1 /*************************************************************************
3 * $Id: sim_imd.c 1904 2008-05-21 06:57:57Z hharte $ *
5 * Copyright (c) 2007-2008 Howard M. Harte. *
6 * http://www.hartetec.com *
8 * Permission is hereby granted, free of charge, to any person obtaining *
9 * a copy of this software and associated documentation files (the *
10 * "Software"), to deal in the Software without restriction, including *
11 * without limitation the rights to use, copy, modify, merge, publish, *
12 * distribute, sublicense, and/or sell copies of the Software, and to *
13 * permit persons to whom the Software is furnished to do so, subject to *
14 * the following conditions: *
16 * The above copyright notice and this permission notice shall be *
17 * included in all copies or substantial portions of the Software. *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
22 * NONINFRINGEMENT. IN NO EVENT SHALL HOWARD M. HARTE BE LIABLE FOR ANY *
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, *
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
27 * Except as contained in this notice, the name of Howard M. Harte shall *
28 * not be used in advertising or otherwise to promote the sale, use or *
29 * other dealings in this Software without prior written authorization *
32 * SIMH Interface based on altairz80_hdsk.c, by Peter Schorn. *
34 * Module Description: *
35 * ImageDisk Disk Image File access module for SIMH, definitions. *
37 * for details on the ImageDisk format and other utilities. *
42 *************************************************************************/
44 #include "altairz80_defs.h"
50 #define DBG_PRINT(args) printf args
52 #define DBG_PRINT(args)
56 DISK_INFO
* diskOpen(FILE *fileref
, int isVerbose
)
60 char sectorHeadMap
[256];
61 char sectorCylMap
[256];
62 unsigned int sectorSize
, sectRecordType
;
64 unsigned char start_sect
;
65 DISK_INFO
*myDisk
= NULL
;
67 unsigned int TotalSectorCount
= 0;
70 myDisk
= (DISK_INFO
*)malloc(sizeof(DISK_INFO
));
72 myDisk
->file
= fileref
;
74 /* rewind to the beginning of the file. */
75 fseek(myDisk
->file
, 0, SEEK_SET
);
78 cBuf
[0] = fgetc(myDisk
->file
);
79 if((cBuf
[0] != 0x1a) && isVerbose
) putchar(cBuf
[0]);
81 while (cBuf
[0] != 0x1a);
85 myDisk
->flags
= 0; /* Make sure all flags are clear. */
88 DBG_PRINT(("start of track %d at file offset %d\n", myDisk
->ntracks
, ftell(myDisk
->file
)));
90 fread(&imd
, 1, 5, myDisk
->file
);
91 if(feof(myDisk
->file
)) break;
93 sectorSize
= (1 << imd
.sectsize
) * 128;
95 DBG_PRINT(("Track %d:\n", myDisk
->ntracks
));
96 DBG_PRINT(("\tMode=%d, Cyl=%d, Head=%d, #sectors=%d, sectsize=%d (%d bytes)\n", imd
.mode
, imd
.cyl
, imd
.head
, imd
.nsects
, imd
.sectsize
, sectorSize
));
98 if((imd
.head
+ 1) > myDisk
->nsides
) {
99 myDisk
->nsides
= imd
.head
+ 1;
102 myDisk
->track
[imd
.cyl
][imd
.head
].mode
= imd
.mode
;
103 myDisk
->track
[imd
.cyl
][imd
.head
].nsects
= imd
.nsects
;
104 myDisk
->track
[imd
.cyl
][imd
.head
].sectsize
= sectorSize
;
106 fread(sectorMap
, 1, imd
.nsects
, myDisk
->file
);
107 myDisk
->track
[imd
.cyl
][imd
.head
].start_sector
= imd
.nsects
;
108 DBG_PRINT(("\tSector Map: "));
109 for(i
=0;i
<imd
.nsects
;i
++) {
110 DBG_PRINT(("%d ", sectorMap
[i
]));
111 if(sectorMap
[i
] < myDisk
->track
[imd
.cyl
][imd
.head
].start_sector
) {
112 myDisk
->track
[imd
.cyl
][imd
.head
].start_sector
= sectorMap
[i
];
115 DBG_PRINT((", Start Sector=%d", myDisk
->track
[imd
.cyl
][imd
.head
].start_sector
));
117 if(imd
.head
& IMD_FLAG_SECT_HEAD_MAP
) {
118 fread(sectorHeadMap
, 1, imd
.nsects
, myDisk
->file
);
119 DBG_PRINT(("\tSector Head Map: "));
120 for(i
=0;i
<imd
.nsects
;i
++) {
121 DBG_PRINT(("%d ", sectorHeadMap
[i
]));
125 memset(sectorHeadMap
, 0, imd
.nsects
);
128 if(imd
.head
& IMD_FLAG_SECT_CYL_MAP
) {
129 fread(sectorCylMap
, 1, imd
.nsects
, myDisk
->file
);
130 DBG_PRINT(("\tSector Cyl Map: "));
131 for(i
=0;i
<imd
.nsects
;i
++) {
132 DBG_PRINT(("%d ", sectorCylMap
[i
]));
136 memset(sectorCylMap
, 0, imd
.nsects
);
139 DBG_PRINT(("\nSector data at offset 0x%08x\n", ftell(myDisk
->file
)));
141 /* Build the table with location 0 being the start sector. */
142 start_sect
= myDisk
->track
[imd
.cyl
][imd
.head
].start_sector
;
144 /* Now read each sector */
145 for(i
=0;i
<imd
.nsects
;i
++) {
147 DBG_PRINT(("Sector Phys: %d/Logical: %d: %d bytes: ", i
, sectorMap
[i
], sectorSize
));
148 sectRecordType
= fgetc(myDisk
->file
);
149 switch(sectRecordType
) {
150 case SECT_RECORD_UNAVAILABLE
: /* Data could not be read from the original media */
151 myDisk
->track
[imd
.cyl
][imd
.head
].sectorOffsetMap
[sectorMap
[i
]-start_sect
] = 0xBADBAD;
153 case SECT_RECORD_NORM
: /* Normal Data */
154 case SECT_RECORD_NORM_DAM
: /* Normal Data with deleted address mark */
155 case SECT_RECORD_NORM_ERR
: /* Normal Data with read error */
156 case SECT_RECORD_NORM_DAM_ERR
: /* Normal Data with deleted address mark with read error */
157 /* DBG_PRINT(("Uncompressed Data\n")); */
158 myDisk
->track
[imd
.cyl
][imd
.head
].sectorOffsetMap
[sectorMap
[i
]-start_sect
] = ftell(myDisk
->file
);
159 fseek(myDisk
->file
, sectorSize
, SEEK_CUR
);
161 case SECT_RECORD_NORM_COMP
: /* Compressed Normal Data */
162 case SECT_RECORD_NORM_DAM_COMP
: /* Compressed Normal Data with deleted address mark */
163 case SECT_RECORD_NORM_COMP_ERR
: /* Compressed Normal Data */
164 case SECT_RECORD_NORM_DAM_COMP_ERR
: /* Compressed Normal Data with deleted address mark */
165 myDisk
->track
[imd
.cyl
][imd
.head
].sectorOffsetMap
[sectorMap
[i
]-start_sect
] = ftell(myDisk
->file
);
166 myDisk
->flags
|= FD_FLAG_WRITELOCK
; /* Write-protect the disk if any sectors are compressed. */
168 DBG_PRINT(("Compressed Data = 0x%02x\n", fgetc(myDisk
->file
)));
174 printf("ERROR: unrecognized sector record type %d\n", sectRecordType
);
182 while (!feof(myDisk
->file
));
184 DBG_PRINT(("Processed %d sectors\n", TotalSectorCount
));
187 for(i
=0;i
<myDisk
->ntracks
;i
++) {
188 DBG_PRINT(("Track %02d: ", i
));
189 for(j
=0;j
<imd
.nsects
;j
++) {
190 DBG_PRINT(("0x%06x ", myDisk
->track
[i
][0].sectorOffsetMap
[j
]));
195 if(myDisk
->flags
& FD_FLAG_WRITELOCK
) {
196 printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress.\n");
202 unsigned int diskClose(DISK_INFO
*myDisk
)
204 unsigned int retval
= 0;
216 unsigned int imdGetSides(DISK_INFO
*myDisk
)
219 return(myDisk
->nsides
);
225 unsigned int imdIsWriteLocked(DISK_INFO
*myDisk
)
228 return((myDisk
->flags
& FD_FLAG_WRITELOCK
) ? 1 : 0);
234 /* Check that the given track/sector exists on the disk */
235 int sectSeek(DISK_INFO
*myDisk
, unsigned int Cyl
, unsigned int Head
)
237 if(myDisk
->track
[Cyl
][Head
].nsects
== 0) {
238 DBG_PRINT(("%s: invalid track/head" NLP
, __FUNCTION__
));
246 int sectRead(DISK_INFO
*myDisk
, unsigned int Cyl
, unsigned int Head
, unsigned int Sector
, unsigned char *buf
, unsigned int buflen
, unsigned int *flags
, unsigned int *readlen
)
248 unsigned int sectorFileOffset
;
249 unsigned char sectRecordType
;
250 unsigned char start_sect
;
254 if(Sector
> myDisk
->track
[Cyl
][Head
].nsects
) {
255 DBG_PRINT(("%s: invalid sector" NLP
, __FUNCTION__
));
259 if(buflen
< myDisk
->track
[Cyl
][Head
].sectsize
) {
260 printf("%s: Reading C:%d/H:%d/S:%d, len=%d: user buffer too short, need %d" NLP
, __FUNCTION__
, Cyl
, Head
, Sector
, buflen
, myDisk
->track
[Cyl
][Head
].sectsize
);
264 start_sect
= myDisk
->track
[Cyl
][Head
].start_sector
;
266 sectorFileOffset
= myDisk
->track
[Cyl
][Head
].sectorOffsetMap
[Sector
-start_sect
];
268 DBG_PRINT(("Reading C:%d/H:%d/S:%d, len=%d, offset=0x%08x" NLP
, Cyl
, Head
, Sector
, buflen
, sectorFileOffset
));
270 fseek(myDisk
->file
, sectorFileOffset
-1, 0);
272 sectRecordType
= fgetc(myDisk
->file
);
273 switch(sectRecordType
) {
274 case SECT_RECORD_UNAVAILABLE
: /* Data could not be read from the original media */
275 *flags
|= IMD_DISK_IO_ERROR_GENERAL
;
277 case SECT_RECORD_NORM_ERR
: /* Normal Data with read error */
278 case SECT_RECORD_NORM_DAM_ERR
: /* Normal Data with deleted address mark with read error */
279 *flags
|= IMD_DISK_IO_ERROR_CRC
;
280 case SECT_RECORD_NORM
: /* Normal Data */
281 case SECT_RECORD_NORM_DAM
: /* Normal Data with deleted address mark */
283 /* DBG_PRINT(("Uncompressed Data" NLP)); */
284 fread(buf
, 1, myDisk
->track
[Cyl
][Head
].sectsize
, myDisk
->file
);
285 *readlen
= myDisk
->track
[Cyl
][Head
].sectsize
;
287 case SECT_RECORD_NORM_COMP_ERR
: /* Compressed Normal Data */
288 case SECT_RECORD_NORM_DAM_COMP_ERR
: /* Compressed Normal Data with deleted address mark */
289 *flags
|= IMD_DISK_IO_ERROR_CRC
;
290 case SECT_RECORD_NORM_COMP
: /* Compressed Normal Data */
291 case SECT_RECORD_NORM_DAM_COMP
: /* Compressed Normal Data with deleted address mark */
292 /* DBG_PRINT(("Compressed Data" NLP)); */
293 memset(buf
, fgetc(myDisk
->file
), myDisk
->track
[Cyl
][Head
].sectsize
);
294 *readlen
= myDisk
->track
[Cyl
][Head
].sectsize
;
295 *flags
|= IMD_DISK_IO_COMPRESSED
;
298 printf("ERROR: unrecognized sector record type %d" NLP
, sectRecordType
);
302 /* Set flags for deleted address mark. */
303 switch(sectRecordType
) {
304 case SECT_RECORD_NORM_DAM
: /* Normal Data with deleted address mark */
305 case SECT_RECORD_NORM_DAM_ERR
: /* Normal Data with deleted address mark with read error */
306 case SECT_RECORD_NORM_DAM_COMP
: /* Compressed Normal Data with deleted address mark */
307 case SECT_RECORD_NORM_DAM_COMP_ERR
: /* Compressed Normal Data with deleted address mark */
308 *flags
|= IMD_DISK_IO_DELETED_ADDR_MARK
;
317 int sectWrite(DISK_INFO
*myDisk
, unsigned int Cyl
, unsigned int Head
, unsigned int Sector
, unsigned char *buf
, unsigned int buflen
, unsigned int *flags
, unsigned int *writelen
)
319 unsigned int sectorFileOffset
;
320 unsigned char sectRecordType
;
321 unsigned char start_sect
;
325 DBG_PRINT(("Writing C:%d/H:%d/S:%d, len=%d" NLP
, Cyl
, Head
, Sector
, buflen
));
327 if(myDisk
->flags
& FD_FLAG_WRITELOCK
) {
328 printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress." NLP
);
331 if(Sector
> myDisk
->track
[Cyl
][Head
].nsects
) {
332 printf("%s: invalid sector [sector %i > # of sectors %i]" NLP
,
333 __FUNCTION__
, Sector
, myDisk
->track
[Cyl
][Head
].nsects
);
337 if(buflen
< myDisk
->track
[Cyl
][Head
].sectsize
) {
338 printf("%s: user buffer too short [buflen %i < sectsize %i]" NLP
,
339 __FUNCTION__
, buflen
, myDisk
->track
[Cyl
][Head
].sectsize
);
343 start_sect
= myDisk
->track
[Cyl
][Head
].start_sector
;
345 sectorFileOffset
= myDisk
->track
[Cyl
][Head
].sectorOffsetMap
[Sector
-start_sect
];
347 fseek(myDisk
->file
, sectorFileOffset
-1, 0);
349 if (*flags
& IMD_DISK_IO_ERROR_GENERAL
) {
350 sectRecordType
= SECT_RECORD_UNAVAILABLE
;
351 } else if (*flags
& IMD_DISK_IO_ERROR_CRC
) {
352 if (*flags
& IMD_DISK_IO_DELETED_ADDR_MARK
)
353 sectRecordType
= SECT_RECORD_NORM_DAM_ERR
;
355 sectRecordType
= SECT_RECORD_NORM_ERR
;
357 sectRecordType
= SECT_RECORD_NORM
;
360 fputc(sectRecordType
, myDisk
->file
);
361 fwrite(buf
, 1, myDisk
->track
[Cyl
][Head
].sectsize
, myDisk
->file
);
362 *writelen
= myDisk
->track
[Cyl
][Head
].sectsize
;