1 /*************************************************************************
2  *                                                                       *
3  * $Id: sim_imd.h 1987 2008-07-08 03:25: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: http://www.classiccmp.org/dunfield/img/index.htm             *
37  *     for details on the ImageDisk format and other utilities.          *
38  *                                                                       *
39  * Environment:                                                          *
40  *     User mode only                                                    *
41  *                                                                       *
42  *************************************************************************/
43 
44 typedef struct {
45     uint8 mode;
46     uint8 cyl;
47     uint8 head;
48     uint8 nsects;
49     uint8 sectsize;
50 } IMD_HEADER;
51 
52 
53 #define IMD_FLAG_SECT_HEAD_MAP  (1 << 6)
54 #define IMD_FLAG_SECT_CYL_MAP   (1 << 7)
55 
56 #define SECT_RECORD_UNAVAILABLE         0   /* Data could not be read from the original media */
57 #define SECT_RECORD_NORM                1   /* Normal Data */
58 #define SECT_RECORD_NORM_COMP           2   /* Compressed Normal Data */
59 #define SECT_RECORD_NORM_DAM            3   /* Normal Data with deleted address mark */
60 #define SECT_RECORD_NORM_DAM_COMP       4   /* Compressed Normal Data with deleted address mark */
61 #define SECT_RECORD_NORM_ERR            5   /* Normal Data */
62 #define SECT_RECORD_NORM_COMP_ERR       6   /* Compressed Normal Data */
63 #define SECT_RECORD_NORM_DAM_ERR        7   /* Normal Data with deleted address mark */
64 #define SECT_RECORD_NORM_DAM_COMP_ERR   8   /* Compressed Normal Data with deleted address mark */
65 
66 #define MAX_CYL     80
67 #define MAX_HEAD    2
68 #define MAX_SPT     26
69 
70 #define FD_FLAG_WRITELOCK   1
71 
72 #define IMD_DISK_IO_ERROR_GENERAL       (1 << 0)    /* General data error. */
73 #define IMD_DISK_IO_ERROR_CRC           (1 << 1)    /* Data read/written, but got a CRC error. */
74 #define IMD_DISK_IO_DELETED_ADDR_MARK   (1 << 2)    /* Sector had a deleted address mark */
75 #define IMD_DISK_IO_COMPRESSED          (1 << 3)    /* Sector is compressed in the IMD file (Read Only) */
76 #define IMD_DISK_IO_ERROR_WPROT         (1 << 4)    /* Disk is write protected */
77 
78 #define IMD_MODE_500K_FM        0
79 #define IMD_MODE_300K_FM        1
80 #define IMD_MODE_250K_FM        2
81 #define IMD_MODE_500K_MFM       3
82 #define IMD_MODE_300K_MFM       4
83 #define IMD_MODE_250K_MFM       5
84 
85 #define IMD_MODE_FM(x)      (x <= IMD_MODE_250K_FM)
86 #define IMD_MODE_MFM(x)     (x >= IMD_MODE_500K_MFM)
87 
88 #define IMAGE_TYPE_DSK          1               /* Flat binary "DSK" image file.            */
89 #define IMAGE_TYPE_IMD          2               /* ImageDisk "IMD" image file.              */
90 #define IMAGE_TYPE_CPT          3               /* CP/M Transfer "CPT" image file.          */
91 
92 typedef struct {
93     uint8 mode;
94     uint8 nsects;
95     uint32 sectsize;
96     uint32 sectorOffsetMap[MAX_SPT];
97     uint8 start_sector;
98     uint8 logicalHead[MAX_SPT];
99     uint8 logicalCyl[MAX_SPT];
100 } TRACK_INFO;
101 
102 typedef struct {
103     FILE *file;
104     uint32 ntracks;
105     uint8 nsides;
106     uint8 flags;
107     TRACK_INFO track[MAX_CYL][MAX_HEAD];
108 } DISK_INFO;
109 
110 extern DISK_INFO *diskOpen(FILE *fileref, uint32 isVerbose);
111 extern t_stat diskClose(DISK_INFO **myDisk);
112 extern t_stat diskCreate(FILE *fileref, char *ctlr_comment);
113 extern uint32 imdGetSides(DISK_INFO *myDisk);
114 extern uint32 imdIsWriteLocked(DISK_INFO *myDisk);
115 
116 extern t_stat sectSeek(DISK_INFO *myDisk, uint32 Cyl, uint32 Head);
117 extern t_stat sectRead(DISK_INFO *myDisk, uint32 Cyl, uint32 Head, uint32 Sector, uint8 *buf, uint32 buflen, uint32 *flags, uint32 *readlen);
118 extern t_stat sectWrite(DISK_INFO *myDisk, uint32 Cyl, uint32 Head, uint32 Sector, uint8 *buf, uint32 buflen, uint32 *flags, uint32 *writelen);
119 extern t_stat trackWrite(DISK_INFO *myDisk,
120                uint32 Cyl,
121                uint32 Head,
122                uint32 numSectors,
123                uint32 sectorLen,
124                uint8 *sectorMap,
125                uint8 mode,
126                uint8 fillbyte,
127                uint32 *flags);
128 extern t_stat assignDiskType(UNIT *uptr);
129