]> Shamusworld >> Repos - wozmaker/blob - src/fileio.h
Small bugfix & fix for cross-platform compilation.
[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 A2RStream
12 {
13         uint8_t location;               // Quarter track for this stream
14         uint8_t captureType;    // 1 = timing, 2 = bits, 3 = ext. timing
15         uint8_t dataLength[4];  // Length of the stream
16         uint8_t estLoopPoint[4];// Estimated loop point in ticks (125 ns/tick)
17         uint8_t data[];                 // Variable length array for stream data
18 };
19
20 struct A2RMetadata
21 {
22         uint8_t metaTag[4];             // "META"
23         uint32_t metaSize;              // Size of the META chunk
24         uint8_t data[];                 // Variable length array of metadata
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 WOZMetadata
60 {
61         uint8_t metaTag[4];             // "META"
62         uint32_t metaSize;              // Size of the META chunk
63         uint8_t data[];                 // Variable length array of metadata
64 };
65
66 struct WOZ
67 {
68         // Header
69         uint8_t magic1[4];              // "WOZ1"
70         uint8_t magic2[4];              // $FF $0A $0D $0A
71         uint32_t crc32;                 // CRC32 of the remaining data in the file
72
73         // INFO chunk
74         uint8_t infoTag[4];             // "INFO"
75         uint32_t infoSize;              // Always 60 bytes long
76         uint8_t infoVersion;    // Currently 1
77         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
78         uint8_t writeProtected; // 1 = write protected disk
79         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
80         uint8_t cleaned;                // 1 = fake bits removed from image
81         uint8_t creator[32];    // Software that made this image, padded with 0x20
82         uint8_t pad1[23];               // Padding to 60 bytes
83
84         // TMAP chunk
85         uint8_t tmapTag[4];             // "TMAP"
86         uint32_t tmapSize;              // Always 160 bytes long
87         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
88
89         // TRKS chunk
90         uint8_t trksTag[4];             // "TRKS"
91         uint32_t trksSize;              // Varies, depending on # of tracks imaged
92         WOZTrack track[];               // Variable length array for the track data proper
93 };
94
95 struct WOZTrack2
96 {
97         uint16_t startingBlock; // 512 byte block # where this track starts (relative to the start of the file)
98         uint16_t blockCount;    // # of blocks in this track
99         uint32_t bitCount;              // # of bits in this track
100 };
101
102 struct WOZ2
103 {
104         // Header
105         uint8_t magic1[4];              // "WOZ1"
106         uint8_t magic2[4];              // $FF $0A $0D $0A
107         uint32_t crc32;                 // CRC32 of the remaining data in the file
108
109         // INFO chunk
110         uint8_t infoTag[4];             // "INFO"
111         uint32_t infoSize;              // Always 60 bytes long
112         uint8_t infoVersion;    // Currently 1
113         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
114         uint8_t writeProtected; // 1 = write protected disk
115         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
116         uint8_t cleaned;                // 1 = fake bits removed from image
117         uint8_t creator[32];    // Software that made this image, padded with 0x20
118         uint8_t diskSides;              // 5 1/4" disks always have 1 side (v2 from here on)
119         uint8_t bootSectorFmt;  // 5 1/4" only (0=unknown, 1=16 sector, 2=13 sector, 3=both)
120         uint8_t optimalBitTmg;  // In ticks, standard for 5 1/4" is 32 (4 µs)
121         uint16_t compatibleHW;  // Bitfield showing hardware compatibility (1=][, 2=][+, 4=//e (unenh), 8=//c, 16=//e (enh), 32=IIgs, 64=//c+, 128=///, 256=///+)
122         uint16_t requiredRAM;   // Minimum size in K, 0=unknown
123         uint16_t largestTrack;  // Number of 512 byte blocks used by largest track
124         uint8_t pad1[14];               // Padding to 60 bytes
125
126         // TMAP chunk
127         uint8_t tmapTag[4];             // "TMAP"
128         uint32_t tmapSize;              // Always 160 bytes long
129         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
130
131         // TRKS chunk
132         uint8_t trksTag[4];             // "TRKS"
133         uint32_t trksSize;              // Varies, depending on # of tracks imaged
134         WOZTrack2 track[160];   // Actual track info (corresponding to TMAP data)
135         uint8_t data[];                 // Variable length array for the track data proper
136 };
137
138
139 // Exported functions
140 uint32_t CRC32(const uint8_t * data, uint32_t length);
141 uint8_t * ReadFile(const char * filename, uint32_t * size);
142 bool LoadA2R(const char * filename);
143 bool WriteWOZFile(const char * filename);
144
145
146 // Inline functions ("get" functions--need to write "set" functions)
147 static inline uint16_t Uint16LE(uint16_t v)
148 {
149         uint8_t * w = (uint8_t *)&v;
150         return (w[1] << 8) | (w[0] << 0);
151 }
152
153 static inline uint32_t Uint32LE(uint32_t v)
154 {
155         uint8_t * w = (uint8_t *)&v;
156         return (w[3] << 24) | (w[2] << 16) | (w[1] << 8) | (w[0] << 0);
157 }
158
159 static inline uint32_t Uint32LE(uint8_t * v)
160 {
161         return (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | (v[0] << 0);
162 }
163
164 static inline uint32_t Uint32BE(uint8_t * v)
165 {
166         return (v[0] << 24) | (v[1] << 16) | (v[2] << 8) | (v[3] << 0);
167 }
168
169 static inline void SwapBytes32(uint8_t * b)
170 {
171         uint8_t temp = b[0];
172         b[0] = b[3];
173         b[3] = temp;
174         temp = b[1];
175         b[1] = b[2];
176         b[2] = temp;
177 }
178
179 #endif  // __FILEIO_H__
180