]> Shamusworld >> Repos - apple2/blob - src/fileio.h
Added WOZ 2 support.
[apple2] / src / fileio.h
1 #ifndef __FILEIO_H__
2 #define __FILEIO_H__
3
4 #include <SDL.h>
5 #include <stdio.h>
6 #include <stdint.h>
7
8 // N.B.: All 32/16-bit values are stored in little endian.  Which means, to
9 //       read/write them safely, we need to use translators as this code may or
10 //       may not be compiled on an architecture that supports little endian
11 //       natively.
12
13 struct WOZ1Track
14 {
15         uint8_t bits[6646];
16         uint16_t byteCount;
17         uint16_t bitCount;
18         uint16_t splicePoint;
19         uint8_t spliceNibble;
20         uint8_t spliceBitCount;
21         uint16_t reserved;
22 };
23
24 struct WOZMetadata
25 {
26         uint8_t metaTag[4];             // "META"
27         uint32_t metaSize;              // Size of the META chunk
28         uint8_t data[];                 // Variable length array of metadata
29 };
30
31 struct WOZ1
32 {
33         // Header
34         uint8_t magic[8];               // "WOZ1" $FF $0A $0D $0A
35         uint32_t crc32;                 // CRC32 of the remaining data in the file
36
37         // INFO chunk
38         uint8_t infoTag[4];             // "INFO"
39         uint32_t infoSize;              // Always 60 bytes long
40         uint8_t infoVersion;    // Currently 1
41         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
42         uint8_t writeProtected; // 1 = write protected disk
43         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
44         uint8_t cleaned;                // 1 = fake bits removed from image
45         uint8_t creator[32];    // Software that made this image, padded with 0x20
46         uint8_t pad1[23];               // Padding to 60 bytes
47
48         // TMAP chunk
49         uint8_t tmapTag[4];             // "TMAP"
50         uint32_t tmapSize;              // Always 160 bytes long
51         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
52
53         // TRKS chunk
54         uint8_t trksTag[4];             // "TRKS"
55         uint32_t trksSize;              // Varies, depending on # of tracks imaged
56         WOZ1Track track[];              // Variable length array for the track data proper
57 };
58
59 struct WOZTrack2
60 {
61         uint16_t startingBlock; // 512 byte block # where this track starts (relative to the start of the file)
62         uint16_t blockCount;    // # of blocks in this track
63         uint32_t bitCount;              // # of bits in this track
64 };
65
66 struct WOZ2
67 {
68         // Header
69         uint8_t magic[8];               // "WOZ2" $FF $0A $0D $0A
70         uint32_t crc32;                 // CRC32 of the remaining data in the file
71
72         // INFO chunk
73         uint8_t infoTag[4];             // "INFO"
74         uint32_t infoSize;              // Always 60 bytes long
75         uint8_t infoVersion;    // Currently 1
76         uint8_t diskType;               // 1 = 5 1/4", 2 = 3 1/2"
77         uint8_t writeProtected; // 1 = write protected disk
78         uint8_t synchronized;   // 1 = cross-track sync was used during imaging
79         uint8_t cleaned;                // 1 = fake bits removed from image
80         uint8_t creator[32];    // Software that made this image, padded with 0x20
81         uint8_t diskSides;              // 5 1/4" disks always have 1 side (v2 from here on)
82         uint8_t bootSectorFmt;  // 5 1/4" only (0=unknown, 1=16 sector, 2=13 sector, 3=both)
83         uint8_t optimalBitTmg;  // In ticks, standard for 5 1/4" is 32 (4 µs)
84         uint16_t compatibleHW;  // Bitfield showing hardware compatibility (1=][, 2=][+, 4=//e (unenh), 8=//c, 16=//e (enh), 32=IIgs, 64=//c+, 128=///, 256=///+)
85         uint16_t requiredRAM;   // Minimum size in K, 0=unknown
86         uint16_t largestTrack;  // Number of 512 byte blocks used by largest track
87         uint8_t pad1[14];               // Padding to 60 bytes
88
89         // TMAP chunk
90         uint8_t tmapTag[4];             // "TMAP"
91         uint32_t tmapSize;              // Always 160 bytes long
92         uint8_t tmap[160];              // Track map, with empty tracks set to $FF
93
94         // TRKS chunk
95         uint8_t trksTag[4];             // "TRKS"
96         uint32_t trksSize;              // Varies, depending on # of tracks imaged
97         WOZTrack2 track[160];   // Actual track info (corresponding to TMAP data)
98         uint8_t data[];                 // Variable length array for the track data proper
99 };
100
101 // Exported functions
102 uint8_t * ReadFile(const char * filename, uint32_t * size);
103 void InitWOZ2Headers(WOZ2 &);
104 uint8_t * InitWOZ(uint32_t * pSize = NULL);
105 uint8_t * UpconvertWOZ1ToWOZ2(uint8_t * woz1Data, uint32_t woz1Size, uint32_t * newSize);
106 uint8_t CheckWOZType(const uint8_t * wozData, uint32_t wozSize);
107 bool CheckWOZIntegrity(const uint8_t * wozData, uint32_t wozSize);
108 bool SaveWOZ(const char * filename, WOZ2 * woz, uint32_t size);
109
110 // Static in-line functions, for clarity & speed, mostly for reading values out
111 // of the WOZ struct, which stores its data in LE; some for swapping variables
112 static inline uint16_t Uint16LE(uint16_t v)
113 {
114 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
115         return ((v & 0xFF) << 8) | ((v & 0xFF00) >> 8);
116 #else
117         return v;
118 #endif
119 }
120
121
122 static inline uint32_t Uint32LE(uint32_t v)
123 {
124 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
125         return ((v & 0xFF) << 24) | ((v & 0xFF00) << 8)
126                 | ((v & 0xFF0000) >> 8) | ((v & 0xFF000000) >> 24);
127 #else
128         return v;
129 #endif
130 }
131
132 #endif  // __FILEIO_H__
133