First Commit of my working state
[simh.git] / AltairZ80 / sim_imd.c
1 /*************************************************************************
2 * *
3 * $Id: sim_imd.c 1904 2008-05-21 06:57:57Z hharte $ *
4 * *
5 * Copyright (c) 2007-2008 Howard M. Harte. *
6 * http://www.hartetec.com *
7 * *
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: *
15 * *
16 * The above copyright notice and this permission notice shall be *
17 * included in all copies or substantial portions of the Software. *
18 * *
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. *
26 * *
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 *
30 * Howard M. Harte. *
31 * *
32 * SIMH Interface based on altairz80_hdsk.c, by Peter Schorn. *
33 * *
34 * Module Description: *
35 * ImageDisk Disk Image File access module for SIMH, definitions. *
36 * see : *
37 * for details on the ImageDisk format and other utilities. *
38 * *
39 * Environment: *
40 * User mode only *
41 * *
42 *************************************************************************/
43
44 #include "altairz80_defs.h"
45 #include "sim_imd.h"
46
47 /* #define DBG_MSG */
48
49 #ifdef DBG_MSG
50 #define DBG_PRINT(args) printf args
51 #else
52 #define DBG_PRINT(args)
53 #endif
54
55
56 DISK_INFO * diskOpen(FILE *fileref, int isVerbose)
57 {
58 char cBuf[256];
59 char sectorMap[256];
60 char sectorHeadMap[256];
61 char sectorCylMap[256];
62 unsigned int sectorSize, sectRecordType;
63 unsigned int i;
64 unsigned char start_sect;
65 DISK_INFO *myDisk = NULL;
66
67 unsigned int TotalSectorCount = 0;
68 IMD_HEADER imd;
69
70 myDisk = (DISK_INFO *)malloc(sizeof(DISK_INFO));
71
72 myDisk->file = fileref;
73
74 /* rewind to the beginning of the file. */
75 fseek(myDisk->file, 0, SEEK_SET);
76
77 do {
78 cBuf[0] = fgetc(myDisk->file);
79 if((cBuf[0] != 0x1a) && isVerbose) putchar(cBuf[0]);
80 }
81 while (cBuf[0] != 0x1a);
82
83 myDisk->nsides = 1;
84 myDisk->ntracks = 0;
85 myDisk->flags = 0; /* Make sure all flags are clear. */
86
87 do {
88 DBG_PRINT(("start of track %d at file offset %d\n", myDisk->ntracks, ftell(myDisk->file)));
89
90 fread(&imd, 1, 5, myDisk->file);
91 if(feof(myDisk->file)) break;
92
93 sectorSize = (1 << imd.sectsize) * 128;
94
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));
97
98 if((imd.head + 1) > myDisk->nsides) {
99 myDisk->nsides = imd.head + 1;
100 }
101
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;
105
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];
113 }
114 }
115 DBG_PRINT((", Start Sector=%d", myDisk->track[imd.cyl][imd.head].start_sector));
116
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]));
122 }
123 DBG_PRINT(("\n"));
124 } else {
125 memset(sectorHeadMap, 0, imd.nsects);
126 }
127
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]));
133 }
134 DBG_PRINT(("\n"));
135 } else {
136 memset(sectorCylMap, 0, imd.nsects);
137 }
138
139 DBG_PRINT(("\nSector data at offset 0x%08x\n", ftell(myDisk->file)));
140
141 /* Build the table with location 0 being the start sector. */
142 start_sect = myDisk->track[imd.cyl][imd.head].start_sector;
143
144 /* Now read each sector */
145 for(i=0;i<imd.nsects;i++) {
146 TotalSectorCount++;
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;
152 break;
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);
160 break;
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. */
167 #ifdef VERBOSE_DEBUG
168 DBG_PRINT(("Compressed Data = 0x%02x\n", fgetc(myDisk->file)));
169 #else
170 fgetc(myDisk->file);
171 #endif
172 break;
173 default:
174 printf("ERROR: unrecognized sector record type %d\n", sectRecordType);
175 break;
176 }
177 DBG_PRINT(("\n"));
178 }
179
180 myDisk->ntracks++;
181 }
182 while (!feof(myDisk->file));
183
184 DBG_PRINT(("Processed %d sectors\n", TotalSectorCount));
185
186 #ifdef VERBOSE_DEBUG
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]));
191 }
192 DBG_PRINT(("\n"));
193 }
194 #endif
195 if(myDisk->flags & FD_FLAG_WRITELOCK) {
196 printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress.\n");
197 }
198
199 return myDisk;
200 }
201
202 unsigned int diskClose(DISK_INFO *myDisk)
203 {
204 unsigned int retval = 0;
205
206 if(myDisk == NULL) {
207 return (-1);
208 }
209
210 free(myDisk);
211 myDisk = NULL;
212
213 return retval;
214 }
215
216 unsigned int imdGetSides(DISK_INFO *myDisk)
217 {
218 if(myDisk != NULL) {
219 return(myDisk->nsides);
220 }
221
222 return (0);
223 }
224
225 unsigned int imdIsWriteLocked(DISK_INFO *myDisk)
226 {
227 if(myDisk != NULL) {
228 return((myDisk->flags & FD_FLAG_WRITELOCK) ? 1 : 0);
229 }
230
231 return (0);
232 }
233
234 /* Check that the given track/sector exists on the disk */
235 int sectSeek(DISK_INFO *myDisk, unsigned int Cyl, unsigned int Head)
236 {
237 if(myDisk->track[Cyl][Head].nsects == 0) {
238 DBG_PRINT(("%s: invalid track/head" NLP, __FUNCTION__));
239 return(-1);
240 }
241
242 return(0);
243 }
244
245
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)
247 {
248 unsigned int sectorFileOffset;
249 unsigned char sectRecordType;
250 unsigned char start_sect;
251 *readlen = 0;
252 *flags = 0;
253
254 if(Sector > myDisk->track[Cyl][Head].nsects) {
255 DBG_PRINT(("%s: invalid sector" NLP, __FUNCTION__));
256 return(-1);
257 }
258
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);
261 return(-1);
262 }
263
264 start_sect = myDisk->track[Cyl][Head].start_sector;
265
266 sectorFileOffset = myDisk->track[Cyl][Head].sectorOffsetMap[Sector-start_sect];
267
268 DBG_PRINT(("Reading C:%d/H:%d/S:%d, len=%d, offset=0x%08x" NLP, Cyl, Head, Sector, buflen, sectorFileOffset));
269
270 fseek(myDisk->file, sectorFileOffset-1, 0);
271
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;
276 break;
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 */
282
283 /* DBG_PRINT(("Uncompressed Data" NLP)); */
284 fread(buf, 1, myDisk->track[Cyl][Head].sectsize, myDisk->file);
285 *readlen = myDisk->track[Cyl][Head].sectsize;
286 break;
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;
296 break;
297 default:
298 printf("ERROR: unrecognized sector record type %d" NLP, sectRecordType);
299 break;
300 }
301
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;
309 default:
310 break;
311 }
312
313 return(0);
314 }
315
316
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)
318 {
319 unsigned int sectorFileOffset;
320 unsigned char sectRecordType;
321 unsigned char start_sect;
322 *writelen = 0;
323 *flags = 0;
324
325 DBG_PRINT(("Writing C:%d/H:%d/S:%d, len=%d" NLP, Cyl, Head, Sector, buflen));
326
327 if(myDisk->flags & FD_FLAG_WRITELOCK) {
328 printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress." NLP);
329 }
330
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);
334 return(-1);
335 }
336
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);
340 return(-1);
341 }
342
343 start_sect = myDisk->track[Cyl][Head].start_sector;
344
345 sectorFileOffset = myDisk->track[Cyl][Head].sectorOffsetMap[Sector-start_sect];
346
347 fseek(myDisk->file, sectorFileOffset-1, 0);
348
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;
354 else
355 sectRecordType = SECT_RECORD_NORM_ERR;
356 } else {
357 sectRecordType = SECT_RECORD_NORM;
358 }
359
360 fputc(sectRecordType, myDisk->file);
361 fwrite(buf, 1, myDisk->track[Cyl][Head].sectsize, myDisk->file);
362 *writelen = myDisk->track[Cyl][Head].sectsize;
363
364 return(0);
365 }