| 1 | /*************************************************************************\r |
| 2 | * *\r |
| 3 | * $Id: sim_imd.c 1904 2008-05-21 06:57:57Z hharte $ *\r |
| 4 | * *\r |
| 5 | * Copyright (c) 2007-2008 Howard M. Harte. *\r |
| 6 | * http://www.hartetec.com *\r |
| 7 | * *\r |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining *\r |
| 9 | * a copy of this software and associated documentation files (the *\r |
| 10 | * "Software"), to deal in the Software without restriction, including *\r |
| 11 | * without limitation the rights to use, copy, modify, merge, publish, *\r |
| 12 | * distribute, sublicense, and/or sell copies of the Software, and to *\r |
| 13 | * permit persons to whom the Software is furnished to do so, subject to *\r |
| 14 | * the following conditions: *\r |
| 15 | * *\r |
| 16 | * The above copyright notice and this permission notice shall be *\r |
| 17 | * included in all copies or substantial portions of the Software. *\r |
| 18 | * *\r |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *\r |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *\r |
| 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *\r |
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL HOWARD M. HARTE BE LIABLE FOR ANY *\r |
| 23 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, *\r |
| 24 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *\r |
| 25 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *\r |
| 26 | * *\r |
| 27 | * Except as contained in this notice, the name of Howard M. Harte shall *\r |
| 28 | * not be used in advertising or otherwise to promote the sale, use or *\r |
| 29 | * other dealings in this Software without prior written authorization *\r |
| 30 | * Howard M. Harte. *\r |
| 31 | * *\r |
| 32 | * SIMH Interface based on altairz80_hdsk.c, by Peter Schorn. *\r |
| 33 | * *\r |
| 34 | * Module Description: *\r |
| 35 | * ImageDisk Disk Image File access module for SIMH, definitions. *\r |
| 36 | * see : *\r |
| 37 | * for details on the ImageDisk format and other utilities. *\r |
| 38 | * *\r |
| 39 | * Environment: *\r |
| 40 | * User mode only *\r |
| 41 | * *\r |
| 42 | *************************************************************************/\r |
| 43 | \r |
| 44 | #include "altairz80_defs.h"\r |
| 45 | #include "sim_imd.h"\r |
| 46 | \r |
| 47 | /* #define DBG_MSG */\r |
| 48 | \r |
| 49 | #ifdef DBG_MSG\r |
| 50 | #define DBG_PRINT(args) printf args\r |
| 51 | #else\r |
| 52 | #define DBG_PRINT(args)\r |
| 53 | #endif\r |
| 54 | \r |
| 55 | \r |
| 56 | DISK_INFO * diskOpen(FILE *fileref, int isVerbose)\r |
| 57 | {\r |
| 58 | char cBuf[256];\r |
| 59 | char sectorMap[256];\r |
| 60 | char sectorHeadMap[256];\r |
| 61 | char sectorCylMap[256];\r |
| 62 | unsigned int sectorSize, sectRecordType;\r |
| 63 | unsigned int i;\r |
| 64 | unsigned char start_sect;\r |
| 65 | DISK_INFO *myDisk = NULL;\r |
| 66 | \r |
| 67 | unsigned int TotalSectorCount = 0;\r |
| 68 | IMD_HEADER imd;\r |
| 69 | \r |
| 70 | myDisk = (DISK_INFO *)malloc(sizeof(DISK_INFO));\r |
| 71 | \r |
| 72 | myDisk->file = fileref;\r |
| 73 | \r |
| 74 | /* rewind to the beginning of the file. */\r |
| 75 | fseek(myDisk->file, 0, SEEK_SET);\r |
| 76 | \r |
| 77 | do {\r |
| 78 | cBuf[0] = fgetc(myDisk->file);\r |
| 79 | if((cBuf[0] != 0x1a) && isVerbose) putchar(cBuf[0]);\r |
| 80 | }\r |
| 81 | while (cBuf[0] != 0x1a);\r |
| 82 | \r |
| 83 | myDisk->nsides = 1;\r |
| 84 | myDisk->ntracks = 0;\r |
| 85 | myDisk->flags = 0; /* Make sure all flags are clear. */\r |
| 86 | \r |
| 87 | do {\r |
| 88 | DBG_PRINT(("start of track %d at file offset %d\n", myDisk->ntracks, ftell(myDisk->file)));\r |
| 89 | \r |
| 90 | fread(&imd, 1, 5, myDisk->file);\r |
| 91 | if(feof(myDisk->file)) break;\r |
| 92 | \r |
| 93 | sectorSize = (1 << imd.sectsize) * 128;\r |
| 94 | \r |
| 95 | DBG_PRINT(("Track %d:\n", myDisk->ntracks));\r |
| 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));\r |
| 97 | \r |
| 98 | if((imd.head + 1) > myDisk->nsides) {\r |
| 99 | myDisk->nsides = imd.head + 1;\r |
| 100 | }\r |
| 101 | \r |
| 102 | myDisk->track[imd.cyl][imd.head].mode = imd.mode;\r |
| 103 | myDisk->track[imd.cyl][imd.head].nsects = imd.nsects;\r |
| 104 | myDisk->track[imd.cyl][imd.head].sectsize = sectorSize;\r |
| 105 | \r |
| 106 | fread(sectorMap, 1, imd.nsects, myDisk->file);\r |
| 107 | myDisk->track[imd.cyl][imd.head].start_sector = imd.nsects;\r |
| 108 | DBG_PRINT(("\tSector Map: "));\r |
| 109 | for(i=0;i<imd.nsects;i++) {\r |
| 110 | DBG_PRINT(("%d ", sectorMap[i]));\r |
| 111 | if(sectorMap[i] < myDisk->track[imd.cyl][imd.head].start_sector) {\r |
| 112 | myDisk->track[imd.cyl][imd.head].start_sector = sectorMap[i];\r |
| 113 | }\r |
| 114 | }\r |
| 115 | DBG_PRINT((", Start Sector=%d", myDisk->track[imd.cyl][imd.head].start_sector));\r |
| 116 | \r |
| 117 | if(imd.head & IMD_FLAG_SECT_HEAD_MAP) {\r |
| 118 | fread(sectorHeadMap, 1, imd.nsects, myDisk->file);\r |
| 119 | DBG_PRINT(("\tSector Head Map: "));\r |
| 120 | for(i=0;i<imd.nsects;i++) {\r |
| 121 | DBG_PRINT(("%d ", sectorHeadMap[i]));\r |
| 122 | }\r |
| 123 | DBG_PRINT(("\n"));\r |
| 124 | } else {\r |
| 125 | memset(sectorHeadMap, 0, imd.nsects);\r |
| 126 | }\r |
| 127 | \r |
| 128 | if(imd.head & IMD_FLAG_SECT_CYL_MAP) {\r |
| 129 | fread(sectorCylMap, 1, imd.nsects, myDisk->file);\r |
| 130 | DBG_PRINT(("\tSector Cyl Map: "));\r |
| 131 | for(i=0;i<imd.nsects;i++) {\r |
| 132 | DBG_PRINT(("%d ", sectorCylMap[i]));\r |
| 133 | }\r |
| 134 | DBG_PRINT(("\n"));\r |
| 135 | } else {\r |
| 136 | memset(sectorCylMap, 0, imd.nsects);\r |
| 137 | }\r |
| 138 | \r |
| 139 | DBG_PRINT(("\nSector data at offset 0x%08x\n", ftell(myDisk->file)));\r |
| 140 | \r |
| 141 | /* Build the table with location 0 being the start sector. */\r |
| 142 | start_sect = myDisk->track[imd.cyl][imd.head].start_sector;\r |
| 143 | \r |
| 144 | /* Now read each sector */\r |
| 145 | for(i=0;i<imd.nsects;i++) {\r |
| 146 | TotalSectorCount++;\r |
| 147 | DBG_PRINT(("Sector Phys: %d/Logical: %d: %d bytes: ", i, sectorMap[i], sectorSize));\r |
| 148 | sectRecordType = fgetc(myDisk->file);\r |
| 149 | switch(sectRecordType) {\r |
| 150 | case SECT_RECORD_UNAVAILABLE: /* Data could not be read from the original media */\r |
| 151 | myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = 0xBADBAD;\r |
| 152 | break;\r |
| 153 | case SECT_RECORD_NORM: /* Normal Data */\r |
| 154 | case SECT_RECORD_NORM_DAM: /* Normal Data with deleted address mark */\r |
| 155 | case SECT_RECORD_NORM_ERR: /* Normal Data with read error */\r |
| 156 | case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */\r |
| 157 | /* DBG_PRINT(("Uncompressed Data\n")); */\r |
| 158 | myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = ftell(myDisk->file);\r |
| 159 | fseek(myDisk->file, sectorSize, SEEK_CUR);\r |
| 160 | break;\r |
| 161 | case SECT_RECORD_NORM_COMP: /* Compressed Normal Data */\r |
| 162 | case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */\r |
| 163 | case SECT_RECORD_NORM_COMP_ERR: /* Compressed Normal Data */\r |
| 164 | case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */\r |
| 165 | myDisk->track[imd.cyl][imd.head].sectorOffsetMap[sectorMap[i]-start_sect] = ftell(myDisk->file);\r |
| 166 | myDisk->flags |= FD_FLAG_WRITELOCK; /* Write-protect the disk if any sectors are compressed. */\r |
| 167 | #ifdef VERBOSE_DEBUG\r |
| 168 | DBG_PRINT(("Compressed Data = 0x%02x\n", fgetc(myDisk->file)));\r |
| 169 | #else\r |
| 170 | fgetc(myDisk->file);\r |
| 171 | #endif\r |
| 172 | break;\r |
| 173 | default:\r |
| 174 | printf("ERROR: unrecognized sector record type %d\n", sectRecordType);\r |
| 175 | break;\r |
| 176 | }\r |
| 177 | DBG_PRINT(("\n"));\r |
| 178 | }\r |
| 179 | \r |
| 180 | myDisk->ntracks++;\r |
| 181 | }\r |
| 182 | while (!feof(myDisk->file));\r |
| 183 | \r |
| 184 | DBG_PRINT(("Processed %d sectors\n", TotalSectorCount));\r |
| 185 | \r |
| 186 | #ifdef VERBOSE_DEBUG\r |
| 187 | for(i=0;i<myDisk->ntracks;i++) {\r |
| 188 | DBG_PRINT(("Track %02d: ", i));\r |
| 189 | for(j=0;j<imd.nsects;j++) {\r |
| 190 | DBG_PRINT(("0x%06x ", myDisk->track[i][0].sectorOffsetMap[j]));\r |
| 191 | }\r |
| 192 | DBG_PRINT(("\n"));\r |
| 193 | }\r |
| 194 | #endif\r |
| 195 | if(myDisk->flags & FD_FLAG_WRITELOCK) {\r |
| 196 | printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress.\n");\r |
| 197 | }\r |
| 198 | \r |
| 199 | return myDisk;\r |
| 200 | }\r |
| 201 | \r |
| 202 | unsigned int diskClose(DISK_INFO *myDisk)\r |
| 203 | {\r |
| 204 | unsigned int retval = 0;\r |
| 205 | \r |
| 206 | if(myDisk == NULL) {\r |
| 207 | return (-1);\r |
| 208 | }\r |
| 209 | \r |
| 210 | free(myDisk);\r |
| 211 | myDisk = NULL;\r |
| 212 | \r |
| 213 | return retval;\r |
| 214 | }\r |
| 215 | \r |
| 216 | unsigned int imdGetSides(DISK_INFO *myDisk)\r |
| 217 | {\r |
| 218 | if(myDisk != NULL) {\r |
| 219 | return(myDisk->nsides);\r |
| 220 | }\r |
| 221 | \r |
| 222 | return (0);\r |
| 223 | }\r |
| 224 | \r |
| 225 | unsigned int imdIsWriteLocked(DISK_INFO *myDisk)\r |
| 226 | {\r |
| 227 | if(myDisk != NULL) {\r |
| 228 | return((myDisk->flags & FD_FLAG_WRITELOCK) ? 1 : 0);\r |
| 229 | }\r |
| 230 | \r |
| 231 | return (0);\r |
| 232 | }\r |
| 233 | \r |
| 234 | /* Check that the given track/sector exists on the disk */\r |
| 235 | int sectSeek(DISK_INFO *myDisk, unsigned int Cyl, unsigned int Head)\r |
| 236 | {\r |
| 237 | if(myDisk->track[Cyl][Head].nsects == 0) {\r |
| 238 | DBG_PRINT(("%s: invalid track/head" NLP, __FUNCTION__));\r |
| 239 | return(-1);\r |
| 240 | }\r |
| 241 | \r |
| 242 | return(0);\r |
| 243 | }\r |
| 244 | \r |
| 245 | \r |
| 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)\r |
| 247 | {\r |
| 248 | unsigned int sectorFileOffset;\r |
| 249 | unsigned char sectRecordType;\r |
| 250 | unsigned char start_sect;\r |
| 251 | *readlen = 0;\r |
| 252 | *flags = 0;\r |
| 253 | \r |
| 254 | if(Sector > myDisk->track[Cyl][Head].nsects) {\r |
| 255 | DBG_PRINT(("%s: invalid sector" NLP, __FUNCTION__));\r |
| 256 | return(-1);\r |
| 257 | }\r |
| 258 | \r |
| 259 | if(buflen < myDisk->track[Cyl][Head].sectsize) {\r |
| 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);\r |
| 261 | return(-1);\r |
| 262 | }\r |
| 263 | \r |
| 264 | start_sect = myDisk->track[Cyl][Head].start_sector;\r |
| 265 | \r |
| 266 | sectorFileOffset = myDisk->track[Cyl][Head].sectorOffsetMap[Sector-start_sect];\r |
| 267 | \r |
| 268 | DBG_PRINT(("Reading C:%d/H:%d/S:%d, len=%d, offset=0x%08x" NLP, Cyl, Head, Sector, buflen, sectorFileOffset));\r |
| 269 | \r |
| 270 | fseek(myDisk->file, sectorFileOffset-1, 0);\r |
| 271 | \r |
| 272 | sectRecordType = fgetc(myDisk->file);\r |
| 273 | switch(sectRecordType) {\r |
| 274 | case SECT_RECORD_UNAVAILABLE: /* Data could not be read from the original media */\r |
| 275 | *flags |= IMD_DISK_IO_ERROR_GENERAL;\r |
| 276 | break;\r |
| 277 | case SECT_RECORD_NORM_ERR: /* Normal Data with read error */\r |
| 278 | case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */\r |
| 279 | *flags |= IMD_DISK_IO_ERROR_CRC;\r |
| 280 | case SECT_RECORD_NORM: /* Normal Data */\r |
| 281 | case SECT_RECORD_NORM_DAM: /* Normal Data with deleted address mark */\r |
| 282 | \r |
| 283 | /* DBG_PRINT(("Uncompressed Data" NLP)); */\r |
| 284 | fread(buf, 1, myDisk->track[Cyl][Head].sectsize, myDisk->file);\r |
| 285 | *readlen = myDisk->track[Cyl][Head].sectsize;\r |
| 286 | break;\r |
| 287 | case SECT_RECORD_NORM_COMP_ERR: /* Compressed Normal Data */\r |
| 288 | case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */\r |
| 289 | *flags |= IMD_DISK_IO_ERROR_CRC;\r |
| 290 | case SECT_RECORD_NORM_COMP: /* Compressed Normal Data */\r |
| 291 | case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */\r |
| 292 | /* DBG_PRINT(("Compressed Data" NLP)); */\r |
| 293 | memset(buf, fgetc(myDisk->file), myDisk->track[Cyl][Head].sectsize);\r |
| 294 | *readlen = myDisk->track[Cyl][Head].sectsize;\r |
| 295 | *flags |= IMD_DISK_IO_COMPRESSED;\r |
| 296 | break;\r |
| 297 | default:\r |
| 298 | printf("ERROR: unrecognized sector record type %d" NLP, sectRecordType);\r |
| 299 | break;\r |
| 300 | }\r |
| 301 | \r |
| 302 | /* Set flags for deleted address mark. */\r |
| 303 | switch(sectRecordType) {\r |
| 304 | case SECT_RECORD_NORM_DAM: /* Normal Data with deleted address mark */\r |
| 305 | case SECT_RECORD_NORM_DAM_ERR: /* Normal Data with deleted address mark with read error */\r |
| 306 | case SECT_RECORD_NORM_DAM_COMP: /* Compressed Normal Data with deleted address mark */\r |
| 307 | case SECT_RECORD_NORM_DAM_COMP_ERR: /* Compressed Normal Data with deleted address mark */\r |
| 308 | *flags |= IMD_DISK_IO_DELETED_ADDR_MARK;\r |
| 309 | default:\r |
| 310 | break;\r |
| 311 | }\r |
| 312 | \r |
| 313 | return(0);\r |
| 314 | }\r |
| 315 | \r |
| 316 | \r |
| 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)\r |
| 318 | {\r |
| 319 | unsigned int sectorFileOffset;\r |
| 320 | unsigned char sectRecordType;\r |
| 321 | unsigned char start_sect;\r |
| 322 | *writelen = 0;\r |
| 323 | *flags = 0;\r |
| 324 | \r |
| 325 | DBG_PRINT(("Writing C:%d/H:%d/S:%d, len=%d" NLP, Cyl, Head, Sector, buflen));\r |
| 326 | \r |
| 327 | if(myDisk->flags & FD_FLAG_WRITELOCK) {\r |
| 328 | printf("Disk write-protected because the image contains compressed sectors. Use IMDU to uncompress." NLP);\r |
| 329 | }\r |
| 330 | \r |
| 331 | if(Sector > myDisk->track[Cyl][Head].nsects) {\r |
| 332 | printf("%s: invalid sector [sector %i > # of sectors %i]" NLP,\r |
| 333 | __FUNCTION__, Sector, myDisk->track[Cyl][Head].nsects);\r |
| 334 | return(-1);\r |
| 335 | }\r |
| 336 | \r |
| 337 | if(buflen < myDisk->track[Cyl][Head].sectsize) {\r |
| 338 | printf("%s: user buffer too short [buflen %i < sectsize %i]" NLP,\r |
| 339 | __FUNCTION__, buflen, myDisk->track[Cyl][Head].sectsize);\r |
| 340 | return(-1);\r |
| 341 | }\r |
| 342 | \r |
| 343 | start_sect = myDisk->track[Cyl][Head].start_sector;\r |
| 344 | \r |
| 345 | sectorFileOffset = myDisk->track[Cyl][Head].sectorOffsetMap[Sector-start_sect];\r |
| 346 | \r |
| 347 | fseek(myDisk->file, sectorFileOffset-1, 0);\r |
| 348 | \r |
| 349 | if (*flags & IMD_DISK_IO_ERROR_GENERAL) {\r |
| 350 | sectRecordType = SECT_RECORD_UNAVAILABLE;\r |
| 351 | } else if (*flags & IMD_DISK_IO_ERROR_CRC) {\r |
| 352 | if (*flags & IMD_DISK_IO_DELETED_ADDR_MARK)\r |
| 353 | sectRecordType = SECT_RECORD_NORM_DAM_ERR;\r |
| 354 | else\r |
| 355 | sectRecordType = SECT_RECORD_NORM_ERR;\r |
| 356 | } else {\r |
| 357 | sectRecordType = SECT_RECORD_NORM;\r |
| 358 | }\r |
| 359 | \r |
| 360 | fputc(sectRecordType, myDisk->file);\r |
| 361 | fwrite(buf, 1, myDisk->track[Cyl][Head].sectsize, myDisk->file);\r |
| 362 | *writelen = myDisk->track[Cyl][Head].sectsize;\r |
| 363 | \r |
| 364 | return(0);\r |
| 365 | }\r |