]> Shamusworld >> Repos - wozmaker/blob - src/fileio.h
Flesh out the disk settings dialog.
[wozmaker] / src / fileio.h
1 #ifndef __FILEIO_H__
2 #define __FILEIO_H__
3
4 #include <stdint.h>
5
6 // N.B.: All 32/16-bit values are stored in little endian.  Which means, to
7 //       read/write them safely, we need to use translators as this code may or
8 //       may not be compiled on an architecture that supports little endian
9 //       natively.
10
11 struct Metadata
12 {
13         uint8_t metaTag[4];             // "META"
14         uint32_t metaSize;              // Size of the META chunk
15         uint8_t data[];                 // Variable length array of metadata
16 };
17
18 struct A2RStream
19 {
20         uint8_t location;               // Quarter track for this stream
21         uint8_t captureType;    // 1 = timing, 2 = bits, 3 = ext. timing
22         uint8_t dataLength[4];  // Length of the stream
23         uint8_t estLoopPoint[4];// Estimated loop point in ticks (125 ns/tick)
24         uint8_t data[];                 // Variable length array for stream data
25 };
26
27 struct A2R
28 {
29         // Header
30         uint8_t magic1[4];              // "A2R2"
31         uint8_t magic2[4];              // $FF $0A $0D $0A
32
33         // INFO chunk
34         uint8_t infoTag[4];             // "INFO"
35         uint32_t infoSize;              // Always 36 bytes long
36         uint8_t infoVersion;    // Currently 1
37         uint8_t creator[32];    // Software that made this image, padded with 0x20
38         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
39         uint8_t writeProtected; // 1 = write protected disk
40         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
41
42         // STRM chunk
43         uint8_t strmTag[4];             // "STRM"
44         uint32_t strmSize;              // Varies, depending on # of tracks imaged
45         uint8_t data[];                 // Variable length array for stream data proper
46 };
47
48 struct WOZTrack
49 {
50         uint8_t bits[6646];
51         uint16_t byteCount;
52         uint16_t bitCount;
53         uint16_t splicePoint;
54         uint8_t spliceNibble;
55         uint8_t spliceBitCount;
56         uint16_t reserved;
57 };
58
59 struct WOZ
60 {
61         // Header
62         uint8_t magic1[4];              // "WOZ1"
63         uint8_t magic2[4];              // $FF $0A $0D $0A
64         uint32_t crc32;                 // CRC32 of the remaining data in the file
65
66         // INFO chunk
67         uint8_t infoTag[4];             // "INFO"
68         uint32_t infoSize;              // Always 60 bytes long
69         uint8_t infoVersion;    // Currently 1
70         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
71         uint8_t writeProtected; // 1 = write protected disk
72         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
73         uint8_t cleaned;                // 1 = fake bits removed from image
74         uint8_t creator[32];    // Software that made this image, padded with 0x20
75         uint8_t pad1[23];               // Padding to 60 bytes
76
77         // TMAP chunk
78         uint8_t tmapTag[4];             // "TMAP"
79         uint32_t tmapSize;              // Always 160 bytes long
80         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
81
82         // TRKS chunk
83         uint8_t trksTag[4];             // "TRKS"
84         uint32_t trksSize;              // Varies, depending on # of tracks imaged
85         WOZTrack track[];               // Variable length array for the track data proper
86 };
87
88 struct WOZTrack2
89 {
90         uint16_t startingBlock; // 512 byte block # where this track starts (relative to the start of the file)
91         uint16_t blockCount;    // # of blocks in this track
92         uint32_t bitCount;              // # of bits in this track
93 };
94
95 struct WOZ2
96 {
97         // Header
98         uint8_t magic1[4];              // "WOZ1"
99         uint8_t magic2[4];              // $FF $0A $0D $0A
100         uint32_t crc32;                 // CRC32 of the remaining data in the file
101
102         // INFO chunk
103         uint8_t infoTag[4];             // "INFO"
104         uint32_t infoSize;              // Always 60 bytes long
105         uint8_t infoVersion;    // Currently 1
106         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
107         uint8_t writeProtected; // 1 = write protected disk
108         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
109         uint8_t cleaned;                // 1 = fake bits removed from image
110         uint8_t creator[32];    // Software that made this image, padded with 0x20
111         uint8_t diskSides;              // 5 1/4" disks always have 1 side (v2 from here on)
112         uint8_t bootSectorFmt;  // 5 1/4" only (0=unknown, 1=16 sector, 2=13 sector, 3=both)
113         uint8_t optimalBitTmg;  // In ticks, standard for 5 1/4" is 32 (4 µs)
114         uint16_t compatibleHW;  // Bitfield showing hardware compatibility (1=][, 2=][+, 4=//e (unenh), 8=//c, 16=//e (enh), 32=IIgs, 64=//c+, 128=///, 256=///+)
115         uint16_t requiredRAM;   // Minimum size in K, 0=unknown
116         uint16_t largestTrack;  // Number of 512 byte blocks used by largest track
117         uint8_t pad1[14];               // Padding to 60 bytes
118
119         // TMAP chunk
120         uint8_t tmapTag[4];             // "TMAP"
121         uint32_t tmapSize;              // Always 160 bytes long
122         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
123
124         // TRKS chunk
125         uint8_t trksTag[4];             // "TRKS"
126         uint32_t trksSize;              // Varies, depending on # of tracks imaged
127         WOZTrack2 track[160];   // Actual track info (corresponding to TMAP data)
128         uint8_t data[];                 // Variable length array for the track data proper
129 };
130
131
132 // Exported functions
133 uint32_t CRC32(const uint8_t * data, uint32_t length);
134 uint8_t * ReadFile(const char * filename, uint32_t * size);
135 bool LoadA2R(const char * filename);
136 bool WriteWOZFile(const char * filename);
137 void UnpackMetadata(Metadata * data);
138 uint8_t * GetMetadata(const char * keyword);
139 uint16_t GetRequiredMachineBits(void);
140 uint16_t GetRequiredRAMInK(void);
141 uint16_t GetRequiredRAMIndex(void);
142
143
144 // Inline functions ("get" functions--need to write "set" functions)
145 static inline uint16_t Uint16LE(uint16_t v)
146 {
147         uint8_t * w = (uint8_t *)&v;
148         return (w[1] << 8) | (w[0] << 0);
149 }
150
151 static inline uint32_t Uint32LE(uint32_t v)
152 {
153         uint8_t * w = (uint8_t *)&v;
154         return (w[3] << 24) | (w[2] << 16) | (w[1] << 8) | (w[0] << 0);
155 }
156
157 static inline uint32_t Uint32LE(uint8_t * v)
158 {
159         return (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | (v[0] << 0);
160 }
161
162 static inline uint32_t Uint32BE(uint8_t * v)
163 {
164         return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | (v[3] << 0);
165 }
166
167 static inline void SwapBytes32(uint8_t * b)
168 {
169         uint8_t temp = b[0];
170         b[0] = b[3];
171         b[3] = temp;
172         temp = b[1];
173         b[1] = b[2];
174         b[2] = temp;
175 }
176
177 #endif  // __FILEIO_H__
178