2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
11 #include "../fileio.h"
12 #if defined(_USE_AGAR) || defined(_USE_SDL) || defined(_USE_QT)
13 #include "agar_logger.h"
17 static const uint16 crc_table[256] = {
18 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
19 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
20 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
21 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
22 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
23 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
24 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
25 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
26 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
27 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
28 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
29 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
30 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
31 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
32 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
33 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
36 // teledisk decoder table
37 static const uint8 d_code[256] = {
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
41 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
42 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
43 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
44 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
45 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
46 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
47 0x0c, 0x0c, 0x0c, 0x0c, 0x0d, 0x0d, 0x0d, 0x0d, 0x0e, 0x0e, 0x0e, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f,
48 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
49 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
50 0x18, 0x18, 0x19, 0x19, 0x1a, 0x1a, 0x1b, 0x1b, 0x1c, 0x1c, 0x1d, 0x1d, 0x1e, 0x1e, 0x1f, 0x1f,
51 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
52 0x28, 0x28, 0x29, 0x29, 0x2a, 0x2a, 0x2b, 0x2b, 0x2c, 0x2c, 0x2d, 0x2d, 0x2e, 0x2e, 0x2f, 0x2f,
53 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
55 static const uint8 d_len[256] = {
56 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
57 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
58 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
59 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
60 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
61 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
62 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
63 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
64 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
65 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
66 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
67 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
68 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
69 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
70 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
71 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
73 static const int secsize[8] = {
74 128, 256, 512, 1024, 2048, 4096, 8192, 16384
77 static uint8 tmp_buffer[DISK_BUFFER_SIZE];
81 int ncyl, nside, nsec, size;
84 static const fd_format_t fd_formats[] = {
85 { MEDIA_TYPE_2D, 40, 1, 16, 256 }, // 1D 160KB
86 { MEDIA_TYPE_2D , 40, 2, 16, 256 }, // 2D 320KB
87 #if defined(_MZ80B) || defined(_MZ2000) || defined(_MZ2200) || defined(_MZ2500)
88 { MEDIA_TYPE_2DD, 80, 2, 16, 256 }, // 2DD 640KB (MZ-2500)
90 { MEDIA_TYPE_2DD, 80, 2, 8, 512 }, // 2DD 640KB
92 { MEDIA_TYPE_2DD, 80, 2, 9, 512 }, // 2DD 720KB
93 { MEDIA_TYPE_2HD, 80, 2, 15, 512 }, // 2HC 1.20MB
94 { MEDIA_TYPE_2HD, 77, 2, 8, 1024 }, // 2HD 1.25MB
95 { MEDIA_TYPE_144, 80, 2, 18, 512 }, // 2HD 1.44MB
96 { MEDIA_TYPE_144, 80, 2, 36, 512 }, // 2ED 2.88MB
100 #define IS_VALID_TRACK(offset) ((offset) >= 0x20 && (offset) < sizeof(buffer))
102 void DISK::open(_TCHAR path[], int bank)
104 // check current disk image
106 if(_tcsicmp(orig_path, path) == 0 && file_bank == bank) {
111 memset(buffer, 0, sizeof(buffer));
112 media_type = MEDIA_TYPE_UNK;
113 is_standard_image = is_fdi_image = false;
114 trim_required = false;
118 if(fi->Fopen(path, FILEIO_READ_BINARY)) {
119 bool converted = false;
121 _tcscpy_s(orig_path, _MAX_PATH, path);
122 _tcscpy_s(dest_path, _MAX_PATH, path);
123 _stprintf_s(temp_path, _MAX_PATH, _T("%s.$$$"), path);
126 // check if file protected
127 write_protected = fi->IsProtected(path);
129 // is this d88 format ?
130 if(check_file_extension(path, _T(".d88")) || check_file_extension(path, _T(".d77"))) {
132 for(int i = 0; i < bank; i++) {
133 fi->Fseek(offset + 0x1c, SEEK_SET);
134 offset += fi->FgetUint32_LE();
136 fi->Fseek(offset + 0x1c, FILEIO_SEEK_SET);
137 file_size.d = fi->FgetUint32_LE();
138 fi->Fseek(offset, FILEIO_SEEK_SET);
139 fi->Fread(buffer, file_size.d, 1);
141 inserted = changed = true;
142 // trim_required = true;
144 // fix sector number from big endian to little endian
145 for(int trkside = 0; trkside < 164; trkside++) {
147 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
149 if(!IS_VALID_TRACK(offset.d)) {
152 uint8* t = buffer + offset.d;
153 pair sector_num, data_size;
154 sector_num.read_2bytes_le_from(t + 4);
155 bool is_be = (sector_num.b.l == 0 && sector_num.b.h >= 4);
157 sector_num.read_2bytes_be_from(t + 4);
158 sector_num.write_2bytes_le_to(t + 4);
160 for(int i = 0; i < sector_num.sd; i++) {
162 sector_num.write_2bytes_le_to(t + 4);
164 data_size.read_2bytes_le_from(t + 14);
165 t += data_size.sd + 0x10;
171 fi->Fseek(0, FILEIO_SEEK_END);
172 file_size.d = fi->Ftell();
173 fi->Fseek(0, FILEIO_SEEK_SET);
176 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
177 // is this 2d format ?
178 if(check_file_extension(path, _T(".2d"))) {
179 if(standard_to_d88(MEDIA_TYPE_2D, 40, 2, 16, 256)) {
180 inserted = changed = is_standard_image = true;
183 fi->Fseek(0, FILEIO_SEEK_SET);
187 // check image file format
188 for(int i = 0;; i++) {
189 const fd_format_t *p = &fd_formats[i];
193 int len = p->ncyl * p->nside * p->nsec * p->size;
194 // 4096 bytes: FDI header ???
195 if(file_size.d == len || (file_size.d == (len + 4096) && (len == 655360 || len == 1261568))) {
196 if(file_size.d == len + 4096) {
198 fi->Fread(fdi_header, 4096, 1);
200 if(standard_to_d88(p->type, p->ncyl, p->nside, p->nsec, p->size)) {
201 inserted = changed = is_standard_image = true;
206 if(0 < file_size.d && file_size.d <= DISK_BUFFER_SIZE) {
207 memset(buffer, 0, sizeof(buffer));
208 fi->Fread(buffer, file_size.d, 1);
210 // check d88 format (temporary)
211 if(file_size.b.l == buffer[0x1c] && file_size.b.h == buffer[0x1d] && file_size.b.h2 == buffer[0x1e] && file_size.b.h3 == buffer[0x1f]) {
212 inserted = changed = true;
215 _stprintf_s(dest_path, _MAX_PATH, _T("%s.D88"), path);
219 if(memcmp(buffer, "TD", 2) == 0 || memcmp(buffer, "td", 2) == 0) {
220 // teledisk image file
221 inserted = changed = converted = teledisk_to_d88();
222 } else if(memcmp(buffer, "IMD", 3) == 0) {
223 // imagedisk image file
224 inserted = changed = converted = imagedisk_to_d88();
225 } else if(memcmp(buffer, "MV - CPC", 8) == 0) {
226 // standard cpdread image file
227 inserted = changed = converted = cpdread_to_d88(0);
228 } else if(memcmp(buffer, "EXTENDED", 8) == 0) {
229 // extended cpdread image file
230 inserted = changed = converted = cpdread_to_d88(1);
233 // failed to convert the disk image
241 fi->Remove(temp_path);
247 FILEIO* fio = new FILEIO();
248 if(fio->Fopen(dest_path, FILEIO_WRITE_BINARY)) {
249 fio->Fwrite(buffer, file_size.d, 1);
255 crc32 = getcrc32(buffer, file_size.d);
257 if(buffer[0x1a] != 0) {
258 write_protected = true;
260 if(media_type == MEDIA_TYPE_UNK) {
261 if((media_type = buffer[0x1b]) == MEDIA_TYPE_2HD) {
262 for(int trkside = 0; trkside < 164; trkside++) {
264 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
266 if(!IS_VALID_TRACK(offset.d)) {
270 uint8 *t = buffer + offset.d;
271 pair sector_num, data_size;
272 sector_num.read_2bytes_le_from(t + 4);
273 data_size.read_2bytes_le_from(t + 14);
275 if(sector_num.sd >= 18 && data_size.sd == 512) {
276 media_type = MEDIA_TYPE_144;
282 // FIXME: ugly patch for X1turbo ALPHA and Batten Tanuki
284 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
285 if(media_type == MEDIA_TYPE_2D) {
286 // check first sector
288 offset.read_4bytes_le_from(buffer + 0x20);
289 if(IS_VALID_TRACK(offset.d)) {
290 static const uint8 batten[] = {0xca, 0xde, 0xaf, 0xc3, 0xdd, 0x20, 0xc0, 0xc7, 0xb7};
291 uint8 *t = buffer + offset.d;
292 if(strncmp((char *)(t + 0x11), "turbo ALPHA", 11) == 0) {
293 is_special_disk = SPECIAL_DISK_X1_ALPHA;
294 } else if(memcmp((void *)(t + 0x11), batten, sizeof(batten)) == 0) {
295 is_special_disk = SPECIAL_DISK_X1_BATTEN;
310 trim_required = false;
312 if(!write_protected && file_size.d && getcrc32(buffer, file_size.d) != crc32) {
314 FILEIO* fio = new FILEIO();
315 int pre_size = 0, post_size = 0;
316 uint8 *pre_buffer = NULL, *post_buffer = NULL;
318 // is this d88 format ?
319 if(check_file_extension(dest_path, _T(".d88")) || check_file_extension(dest_path, _T(".d77"))) {
320 if(fio->Fopen(dest_path, FILEIO_READ_BINARY)) {
321 fio->Fseek(0, FILEIO_SEEK_END);
322 uint32 total_size = fio->Ftell(), offset = 0;
323 for(int i = 0; i < file_bank; i++) {
324 fio->Fseek(offset + 0x1c, SEEK_SET);
325 offset += fio->FgetUint32_LE();
327 if((pre_size = offset) > 0) {
328 pre_buffer = (uint8 *)malloc(pre_size);
329 fio->Fseek(0, FILEIO_SEEK_SET);
330 fio->Fread(pre_buffer, pre_size, 1);
332 fio->Fseek(offset + 0x1c, SEEK_SET);
333 offset += fio->FgetUint32_LE();
334 if((post_size = total_size - offset) > 0) {
335 post_buffer = (uint8 *)malloc(post_size);
336 fio->Fseek(offset, FILEIO_SEEK_SET);
337 fio->Fread(post_buffer, post_size, 1);
342 if(!fio->Fopen(dest_path, FILEIO_WRITE_BINARY)) {
343 _TCHAR tmp_path[_MAX_PATH];
344 _stprintf_s(tmp_path, _MAX_PATH, _T("temporary_saved_floppy_disk_#%d.d88"), drive_num);
345 fio->Fopen(emu->bios_path(tmp_path), FILEIO_WRITE_BINARY);
347 if(fio->IsOpened()) {
349 fio->Fwrite(pre_buffer, pre_size, 1);
351 if(is_standard_image) {
353 fio->Fwrite(fdi_header, 4096, 1);
355 for(int trkside = 0; trkside < 164; trkside++) {
357 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
359 if(!IS_VALID_TRACK(offset.d)) {
362 uint8* t = buffer + offset.d;
363 pair sector_num, data_size;
364 sector_num.read_2bytes_le_from(t + 4);
366 for(int i = 0; i < sector_num.sd; i++) {
367 data_size.read_2bytes_le_from(t + 14);
368 fio->Fwrite(t + 0x10, data_size.sd, 1);
369 t += data_size.sd + 0x10;
373 fio->Fwrite(buffer, file_size.d, 1);
376 fio->Fwrite(post_buffer, post_size, 1);
390 inserted = write_protected = false;
392 sector_size.sd = sector_num.sd = 0;
396 bool DISK::get_track(int trk, int side)
398 sector_size.sd = sector_num.sd = 0;
401 // disk not inserted or invalid media type
402 if(!(inserted && check_media_type())) {
407 int trkside = trk * 2 + (side & 1);
408 if(!(0 <= trkside && trkside < 164)) {
412 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
414 if(!IS_VALID_TRACK(offset.d)) {
419 sector = buffer + offset.d;
420 sector_num.read_2bytes_le_from(sector + 4);
422 // create each sector position in track
423 int sync_size = drive_mfm ? 12 : 6;
424 int am_size = drive_mfm ? 3 : 0;
425 int gap2_size = drive_mfm ? 22 : 11;
428 too_many_sectors = false;
431 int total = 0, gap3_size;
433 for(int i = 0; i < sector_num.sd; i++) {
435 data_size.read_2bytes_le_from(t + 14);
436 if((data_size.sd >> data_size_shift) < 0x80) {
437 too_many_sectors = true;
440 total += sync_size + (am_size + 1) + (4 + 2) + gap2_size + sync_size + (am_size + 1);
441 total += (data_size.sd >> data_size_shift) + 2;
442 t += data_size.sd + 0x10;
444 if(too_many_sectors) {
445 // Too many sectors in this track
448 } else if((gap3_size = (get_track_size() - total) / (sector_num.sd + 2)) < 12) {
454 total = gap3_size * 2;
456 for(int i = 0; i < sector_num.sd; i++) {
458 data_size.read_2bytes_le_from(t + 14);
459 if(too_many_sectors) {
460 total = gap3_size * 2 + (get_track_size() - gap3_size * 2) * i / sector_num.sd;
462 sync_position[i] = total;
463 total += sync_size + (am_size + 1);
464 id_position[i] = total;
465 total += (4 + 2) + gap2_size + sync_size + (am_size + 1);
466 data_position[i] = total;
467 total += (data_size.sd >> data_size_shift) + 2 + gap3_size;
471 t += data_size.sd + 0x10;
476 bool DISK::make_track(int trk, int side)
478 int track_size = get_track_size();
480 if(!get_track(trk, side)) {
481 // create a dummy track
482 for(int i = 0; i < track_size; i++) {
489 int sync_size = drive_mfm ? 12 : 6;
490 int am_size = drive_mfm ? 3 : 0;
491 int gap2_size = drive_mfm ? 22 : 11;
492 uint8 gap_data = drive_mfm ? 0x4e : 0xff;
495 memset(track, gap_data, track_size);
497 if(sync_position[0] >= (sync_size + am_size + 1) + (8 + 5)) {
498 int p = (sync_position[0] - (sync_size + am_size + 1)) * 8 / (8 + 5);
501 for(int i = 0; i < sync_size; i++) {
505 for(int i = 0; i < am_size; i++) {
514 for(int i = 0; i < sector_num.sd; i++) {
516 data_size.read_2bytes_le_from(t + 14);
517 int p = sync_position[i];
520 for(int j = 0; j < sync_size; j++) {
521 if(p < track_size) track[p++] = 0x00;
524 for(int j = 0; j < am_size; j++) {
525 if(p < track_size) track[p++] = 0xa1;
527 if(p < track_size) track[p++] = 0xfe;
529 if(p < track_size) track[p++] = t[0];
530 if(p < track_size) track[p++] = t[1];
531 if(p < track_size) track[p++] = t[2];
532 if(p < track_size) track[p++] = t[3];
534 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
535 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
536 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
537 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
538 if(p < track_size) track[p++] = (crc >> 8) & 0xff;
539 if(p < track_size) track[p++] = (crc >> 0) & 0xff;
541 for(int j = 0; j < gap2_size; j++) {
542 if(p < track_size) track[p++] = gap_data;
545 for(int j = 0; j < sync_size; j++) {
546 if(p < track_size) track[p++] = 0x00;
549 for(int j = 0; j < am_size; j++) {
550 if(p < track_size) track[p++] = 0xa1;
552 if(p < track_size) track[p++] = (t[7] != 0) ? 0xf8 : 0xfb;
555 for(int j = 0; j < (data_size.sd >> data_size_shift); j++) {
556 if(p < track_size) track[p++] = t[0x10 + j];
557 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0x10 + j]]);
559 if(p < track_size) track[p++] = (crc >> 8) & 0xff;
560 if(p < track_size) track[p++] = (crc >> 0) & 0xff;
562 t += data_size.sd + 0x10;
567 bool DISK::get_sector(int trk, int side, int index)
569 sector_size.sd = sector_num.sd = 0;
572 // disk not inserted or invalid media type
573 if(!(inserted && check_media_type())) {
578 int trkside = trk * 2 + (side & 1);
579 if(!(0 <= trkside && trkside < 164)) {
583 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
585 if(!IS_VALID_TRACK(offset.d)) {
590 uint8* t = buffer + offset.d;
591 sector_num.read_2bytes_le_from(t + 4);
593 if(index >= sector_num.sd) {
598 for(int i = 0; i < index; i++) {
600 data_size.read_2bytes_le_from(t + 14);
601 t += data_size.sd + 0x10;
607 void DISK::set_sector_info(uint8 *t)
615 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
616 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
617 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
618 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
619 id[4] = (crc >> 8) & 0xff;
620 id[5] = (crc >> 0) & 0xff;
622 deleted = (t[7] != 0);
623 crc_error = (t[8] != 0x00 && t[8] != 0x10);
625 sector_size.read_2bytes_le_from(t + 14);
628 void DISK::set_deleted(bool value)
631 uint8 *t = sector - 0x10;
632 t[7] = value ? 0x10 : 0;
633 if(t[8] == 0x00 || t[8] == 0x10) {
640 void DISK::set_crc_error(bool value)
643 uint8 *t = sector - 0x10;
644 t[8] = value ? 0xb0 : t[7];
649 bool DISK::format_track(int trk, int side)
651 // disk not inserted or invalid media type
652 if(!(inserted && check_media_type())) {
657 int trkside = trk * 2 + (side & 1);
658 if(!(0 <= trkside && trkside < 164)) {
662 // create new empty track
665 trim_required = false;
667 memset(buffer + DISK_BUFFER_SIZE, 0, sizeof(buffer) - DISK_BUFFER_SIZE);
669 offset.d = DISK_BUFFER_SIZE;
670 offset.write_4bytes_le_to(buffer + 0x20 + trkside * 4);
672 trim_required = true;
677 void DISK::insert_sector(uint8 c, uint8 h, uint8 r, uint8 n, bool deleted, bool crc_error, uint8 fill_data, int length)
679 uint8* t = buffer + DISK_BUFFER_SIZE;
682 for(int i = 0; i < (sector_num.sd - 1); i++) {
683 t[4] = sector_num.b.l;
684 t[5] = sector_num.b.h;
686 data_size.read_2bytes_le_from(t + 14);
687 t += data_size.sd + 0x10;
693 t[4] = sector_num.b.l;
694 t[5] = sector_num.b.h;
695 t[6] = drive_mfm ? 0 : 0x40;
696 t[7] = deleted ? 0x10 : 0;
697 t[8] = crc_error ? 0xb0 : t[7];
698 t[14] = (length >> 0) & 0xff;
699 t[15] = (length >> 8) & 0xff;
700 memset(t + 16, fill_data, length);
705 void DISK::trim_buffer()
707 int max_tracks = 164;
708 uint32 dest_offset = 0x2b0;
711 memset(tmp_buffer, 0, sizeof(tmp_buffer));
712 memcpy(tmp_buffer, buffer, 0x20);
715 for(int trkside = 0; trkside < 164; trkside++) {
717 src_trk_offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
718 if(src_trk_offset.d != 0) {
720 if(src_trk_offset.d < 0x2b0) {
721 max_tracks = (src_trk_offset.d - 0x20) >> 2;
724 if(src_trk_offset.d != 0x2b0) {
725 max_tracks = (src_trk_offset.d - 0x20) >> 2;
726 if(max_tracks > 164) {
727 dest_offset = 0x20 + max_tracks * 4);
736 for(int trkside = 0; trkside < max_tracks; trkside++) {
738 src_trk_offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
740 pair dest_trk_offset;
741 dest_trk_offset.d = 0;
743 if(IS_VALID_TRACK(src_trk_offset.d)) {
744 uint8* t = buffer + src_trk_offset.d;
745 pair sector_num, data_size;
746 sector_num.read_2bytes_le_from(t + 4);
747 if(sector_num.sd != 0) {
748 dest_trk_offset.d = dest_offset;
749 for(int i = 0; i < sector_num.sd; i++) {
750 data_size.read_2bytes_le_from(t + 14);
751 memcpy(tmp_buffer + dest_offset, t, data_size.sd + 0x10);
752 dest_offset += data_size.sd + 0x10;
753 t += data_size.sd + 0x10;
757 dest_trk_offset.write_4bytes_le_to(tmp_buffer + 0x20 + trkside * 4);
761 file_size.d = dest_offset;
762 file_size.write_4bytes_le_to(tmp_buffer + 0x1c);
764 memset(buffer, 0, sizeof(buffer));
765 memcpy(buffer, tmp_buffer, file_size.d);
772 } else if(inserted) {
773 return (media_type == MEDIA_TYPE_2HD) ? 360 : 300;
775 return (drive_type == DRIVE_TYPE_2HD) ? 360 : 300;
779 int DISK::get_track_size()
782 return media_type == MEDIA_TYPE_144 ? 12500 : media_type == MEDIA_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
784 return drive_type == DRIVE_TYPE_144 ? 12500 : drive_type == DRIVE_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
788 double DISK::get_usec_per_bytes(int bytes)
790 return 1000000.0 / (get_track_size() * (get_rpm() / 60.0)) * bytes;
793 bool DISK::check_media_type()
797 return (media_type == MEDIA_TYPE_2D);
799 return (media_type == MEDIA_TYPE_2D || media_type == MEDIA_TYPE_2DD);
801 return (media_type == MEDIA_TYPE_2HD);
803 return (media_type == MEDIA_TYPE_144);
805 return true; // always okay
810 // teledisk image decoder
813 this teledisk image decoder is based on:
815 LZHUF.C English version 1.0 based on Japanese version 29-NOV-1988
816 LZSS coded by Haruhiko OKUMURA
817 Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
818 Edited and translated to English by Kenji RIKITAKE
822 #define COPYBUFFER(src, size) { \
823 if(file_size.d + (size) > DISK_BUFFER_SIZE) { \
826 memcpy(buffer + file_size.d, (src), (size)); \
827 file_size.d += (size); \
830 bool DISK::teledisk_to_d88()
840 // check teledisk header
841 fi->Fseek(0, FILEIO_SEEK_SET);
842 fi->Fread(&hdr, sizeof(td_hdr_t), 1);
843 if(hdr.sig[0] == 't' && hdr.sig[1] == 'd') {
844 // decompress to the temporary file
845 FILEIO* fo = new FILEIO();
846 if(!fo->Fopen(temp_path, FILEIO_WRITE_BINARY)) {
853 if((rd = decode(obuf, 512)) > 0) {
854 fo->Fwrite(obuf, rd, 1);
862 // reopen the temporary file
864 if(!fi->Fopen(temp_path, FILEIO_READ_BINARY)) {
868 if(hdr.flag & 0x80) {
870 fi->Fread(&cmt, sizeof(td_cmt_t), 1);
871 fi->Fseek(cmt.len, FILEIO_SEEK_CUR);
878 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
879 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "TELEDISK");
880 d88_hdr.protect = 0; // non-protected
881 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
884 int trkcnt = 0, trkptr = sizeof(d88_hdr_t);
885 fi->Fread(&trk, sizeof(td_trk_t), 1);
886 while(trk.nsec != 0xff) {
887 d88_hdr.trkptr[trkcnt++] = trkptr;
889 d88_hdr.trkptr[trkcnt++] = trkptr;
892 // read sectors in this track
893 for(int i = 0; i < trk.nsec; i++) {
894 uint8 buf[2048], dst[2048];
895 memset(buf, 0, sizeof(buf));
896 memset(dst, 0, sizeof(dst));
898 // read sector header
899 fi->Fread(&sct, sizeof(td_sct_t), 1);
901 // create d88 sector header
902 memset(&d88_sct, 0, sizeof(d88_sct_t));
907 d88_sct.nsec = trk.nsec;
908 d88_sct.dens = (hdr.dens & 0x80) ? 0x40 : 0;
909 d88_sct.del = (sct.ctrl & 4) ? 0x10 : 0;
910 d88_sct.stat = (sct.ctrl & 2) ? 0xb0 : d88_sct.del;
911 d88_sct.size = secsize[sct.n & 3];
913 // create sector image
914 if(sct.ctrl != 0x10) {
915 // read sector source
916 int len = fi->Fgetc();
917 len += fi->Fgetc() * 256 - 1;
918 int flag = fi->Fgetc(), d = 0;
919 fi->Fread(buf, len, 1);
923 memcpy(dst, buf, len);
924 } else if(flag == 1) {
926 len2.read_2bytes_le_from(buf);
931 } else if(flag == 2) {
932 for(int s = 0; s < len;) {
939 } else if(type < 5) {
945 for(int j = 0; j < n; j++) {
949 for(int j = 0; j < n; j++) {
954 break; // unknown type
958 break; // unknown flag
965 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
966 COPYBUFFER(dst, d88_sct.size);
967 trkptr += sizeof(d88_sct_t) + d88_sct.size;
970 fi->Fread(&trk, sizeof(td_trk_t), 1);
972 d88_hdr.type = ((hdr.dens & 3) == 2) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
973 d88_hdr.size = trkptr;
974 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
978 int DISK::next_word()
980 if(ibufndx >= ibufcnt) {
981 ibufndx = ibufcnt = 0;
982 memset(inbuf, 0, 512);
983 for(int i = 0; i < 512; i++) {
996 getbuf |= inbuf[ibufndx++] << (8 - getlen);
1004 if(next_word() < 0) {
1010 return (i < 0) ? 1 : 0;
1013 int DISK::get_byte()
1015 if(next_word() != 0) {
1025 void DISK::start_huff()
1028 for(i = 0; i < N_CHAR; i++) {
1030 son[i] = i + TABLE_SIZE;
1031 prnt[i + TABLE_SIZE] = i;
1034 while(j <= ROOT_POSITION) {
1035 freq[j] = freq[i] + freq[i + 1];
1037 prnt[i] = prnt[i + 1] = j;
1040 freq[TABLE_SIZE] = 0xffff;
1041 prnt[ROOT_POSITION] = 0;
1044 void DISK::reconst()
1048 for(i = 0; i < TABLE_SIZE; i++) {
1049 if(son[i] >= TABLE_SIZE) {
1050 freq[j] = (freq[i] + 1) / 2;
1055 for(i = 0, j = N_CHAR; j < TABLE_SIZE; i += 2, j++) {
1057 f = freq[j] = freq[i] + freq[k];
1058 for(k = j - 1; f < freq[k]; k--);
1061 memmove(&freq[k + 1], &freq[k], l);
1063 memmove(&son[k + 1], &son[k], l);
1066 for(i = 0; i < TABLE_SIZE; i++) {
1067 if((k = son[i]) >= TABLE_SIZE) {
1070 prnt[k] = prnt[k + 1] = i;
1075 void DISK::update(int c)
1078 if(freq[ROOT_POSITION] == MAX_FREQ) {
1081 c = prnt[c + TABLE_SIZE];
1084 if(k > freq[l = c + 1]) {
1085 while(k > freq[++l]);
1091 if(i < TABLE_SIZE) {
1097 if(j < TABLE_SIZE) {
1104 while((c = prnt[c]) != 0);
1107 short DISK::decode_char()
1110 uint16 c = son[ROOT_POSITION];
1111 while(c < TABLE_SIZE) {
1112 if((ret = get_bit()) < 0) {
1123 short DISK::decode_position()
1127 if((bit = get_byte()) < 0) {
1131 c = (uint16)d_code[i] << 6;
1134 if((bit = get_bit()) < 0) {
1139 return (c | i & 0x3f);
1142 void DISK::init_decode()
1144 ibufcnt= ibufndx = bufcnt = getbuf = 0;
1147 for(int i = 0; i < STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE; i++) {
1150 ptr = STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE;
1153 int DISK::decode(uint8 *buf, int len)
1157 for(count = 0; count < len;) {
1159 if((c = decode_char()) < 0) {
1163 *(buf++) = (uint8)c;
1164 text_buf[ptr++] = (uint8)c;
1165 ptr &= (STRING_BUFFER_SIZE - 1);
1168 if((pos = decode_position()) < 0) {
1171 bufpos = (ptr - pos - 1) & (STRING_BUFFER_SIZE - 1);
1172 bufcnt = c - 255 + THRESHOLD;
1176 while(bufndx < bufcnt && count < len) {
1177 c = text_buf[(bufpos + bufndx) & (STRING_BUFFER_SIZE - 1)];
1178 *(buf++) = (uint8)c;
1180 text_buf[ptr++] = (uint8)c;
1181 ptr &= (STRING_BUFFER_SIZE - 1);
1184 if(bufndx >= bufcnt) {
1185 bufndx = bufcnt = 0;
1192 // imagedisk image decoder
1194 bool DISK::imagedisk_to_d88()
1201 fi->Fseek(0, FILEIO_SEEK_SET);
1203 while((tmp = fi->Fgetc()) != 0x1a) {
1212 // create d88 header
1213 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1214 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "IMAGEDISK");
1215 d88_hdr.protect = 0; // non-protected
1216 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1219 int trkptr = sizeof(d88_hdr_t);
1220 int trkcnt = 0, mode;
1222 for(int t = 0; t < 164; t++) {
1223 // check end of file
1224 if(fi->Fread(&trk, sizeof(imd_trk_t), 1) != 1) {
1229 // check track header
1231 mode = trk.mode % 3; // 0=500kbps, 1=300kbps, 2=250kbps
1236 d88_hdr.trkptr[t] = trkptr;
1239 uint8 c[64], h[64], r[64];
1240 fi->Fread(r, trk.nsec, 1);
1241 if(trk.head & 0x80) {
1242 fi->Fread(c, trk.nsec, 1);
1244 memset(c, trk.cyl, sizeof(c));
1246 if(trk.head & 0x40) {
1247 fi->Fread(h, trk.nsec, 1);
1249 memset(h, trk.head & 1, sizeof(h));
1252 // read sectors in this track
1253 for(int i = 0; i < trk.nsec; i++) {
1254 // create d88 sector header
1255 int sectype = fi->Fgetc();
1259 memset(&d88_sct, 0, sizeof(d88_sct_t));
1263 d88_sct.n = trk.size;
1264 d88_sct.nsec = trk.nsec;
1265 d88_sct.dens = (trk.mode < 3) ? 0x40 : 0;
1266 d88_sct.del = (sectype == 3 || sectype == 4 || sectype == 7 || sectype == 8) ? 0x10 : 0;
1267 d88_sct.stat = (sectype == 5 || sectype == 6 || sectype == 7 || sectype == 8) ? 0xb0 : d88_sct.del;
1268 d88_sct.size = secsize[trk.size & 7];
1270 // create sector image
1272 if(sectype == 1 || sectype == 3 || sectype == 5 || sectype == 7) {
1274 fi->Fread(dst, d88_sct.size, 1);
1275 } else if(sectype == 2 || sectype == 4 || sectype == 6 || sectype == 8) {
1277 int tmp = fi->Fgetc();
1278 memset(dst, tmp, d88_sct.size);
1284 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1285 COPYBUFFER(dst, d88_sct.size);
1286 trkptr += sizeof(d88_sct_t) + d88_sct.size;
1289 d88_hdr.type = (mode == 0) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
1290 d88_hdr.size = trkptr;
1291 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1295 // cpdread image decoder (from MESS formats/dsk_dsk.c)
1297 bool DISK::cpdread_to_d88(int extended)
1303 // get cylinder number and side number
1304 memcpy(tmp_buffer, buffer, file_size.d);
1305 int ncyl = tmp_buffer[0x30];
1306 int nside = tmp_buffer[0x31];
1311 // create d88 header
1312 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1313 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "CPDRead");
1314 d88_hdr.protect = 0; // non-protected
1315 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1318 int trkofs = 0x100, trkofs_ptr = 0x34;
1319 int trkptr = sizeof(d88_hdr_t);
1321 for(int c = 0; c < ncyl; c++) {
1322 for(int h = 0; h < nside; h++) {
1323 // read sectors in this track
1324 uint8 *track_info = tmp_buffer + trkofs;
1325 int cyl = track_info[0x10];
1326 int side = track_info[0x11];
1327 int nsec = track_info[0x15];
1328 int size = 1 << (track_info[0x14] + 7); // standard
1329 int sctofs = trkofs + 0x100;
1333 d88_hdr.trkptr[2 * cyl] = d88_hdr.trkptr[2 * cyl + 1] = trkptr;
1335 d88_hdr.trkptr[2 * cyl + side] = trkptr;
1337 for(int s = 0; s < nsec; s++) {
1339 uint8 *sector_info = tmp_buffer + trkofs + 0x18 + s * 8;
1341 size = sector_info[6] + sector_info[7] * 256;
1344 // create d88 sector header
1345 memset(&d88_sct, 0, sizeof(d88_sct_t));
1346 d88_sct.c = sector_info[0];
1347 d88_sct.h = sector_info[1];
1348 d88_sct.r = sector_info[2];
1349 d88_sct.n = sector_info[3];
1350 d88_sct.nsec = nsec;
1352 d88_sct.del = (sector_info[5] == 0xb2) ? 0x10 : 0;
1353 d88_sct.stat = (sector_info[5] == 0xb5) ? 0xb0 : d88_sct.del;
1354 d88_sct.size = size;
1357 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1358 COPYBUFFER(tmp_buffer + sctofs, size);
1359 trkptr += sizeof(d88_sct_t) + size;
1365 trkofs += tmp_buffer[trkofs_ptr++] * 256;
1367 trkofs += tmp_buffer[0x32] + tmp_buffer[0x33] * 256;
1371 d88_hdr.type = (total < (368640 + 655360) / 2) ? MEDIA_TYPE_2D : (total < (737280 + 1228800) / 2) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2HD;
1372 d88_hdr.size = trkptr;
1373 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1377 // standard image decoder
1379 bool DISK::standard_to_d88(int type, int ncyl, int nside, int nsec, int size)
1387 // create d88 header
1388 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1389 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "STANDARD");
1390 d88_hdr.protect = 0; // non-protected
1391 d88_hdr.type = (type == MEDIA_TYPE_144) ? MEDIA_TYPE_2HD : type;
1393 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1396 for(int i = 0; i < 8; i++) {
1397 if(size == (128 << i)) {
1404 int trkptr = sizeof(d88_hdr_t);
1405 for(int c = 0; c < ncyl; c++) {
1406 for(int h = 0; h < nside; h++) {
1407 d88_hdr.trkptr[t++] = trkptr;
1410 d88_hdr.trkptr[t++] = trkptr;
1413 // read sectors in this track
1414 for(int s = 0; s < nsec; s++) {
1415 // create d88 sector header
1416 memset(&d88_sct, 0, sizeof(d88_sct_t));
1421 d88_sct.nsec = nsec;
1425 d88_sct.size = size;
1427 // create sector image
1429 memset(dst, 0xe5, sizeof(dst));
1430 fi->Fread(dst, size, 1);
1433 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1434 COPYBUFFER(dst, size);
1435 trkptr += sizeof(d88_sct_t) + size;
1439 d88_hdr.size = trkptr;
1440 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1444 #define STATE_VERSION 3
1446 void DISK::save_state(FILEIO* state_fio)
1448 state_fio->FputUint32(STATE_VERSION);
1450 state_fio->Fwrite(buffer, sizeof(buffer), 1);
1451 state_fio->Fwrite(orig_path, sizeof(orig_path), 1);
1452 state_fio->Fwrite(dest_path, sizeof(dest_path), 1);
1453 state_fio->FputUint32(file_size.d);
1454 state_fio->FputInt32(file_bank);
1455 state_fio->FputUint32(crc32);
1456 state_fio->Fwrite(fdi_header, sizeof(fdi_header), 1);
1457 state_fio->FputBool(inserted);
1458 state_fio->FputBool(ejected);
1459 state_fio->FputBool(write_protected);
1460 state_fio->FputBool(changed);
1461 state_fio->FputUint8(media_type);
1462 state_fio->FputBool(is_standard_image);
1463 state_fio->FputBool(is_fdi_image);
1464 state_fio->FputInt32(is_special_disk);
1465 state_fio->Fwrite(track, sizeof(track), 1);
1466 state_fio->FputInt32(sector_num.sd);
1467 state_fio->FputInt32(data_size_shift);
1468 state_fio->FputBool(too_many_sectors);
1469 state_fio->FputBool(no_skew);
1470 state_fio->Fwrite(sync_position, sizeof(sync_position), 1);
1471 state_fio->Fwrite(id_position, sizeof(id_position), 1);
1472 state_fio->Fwrite(data_position, sizeof(data_position), 1);
1473 state_fio->FputInt32(sector ? (int)(sector - buffer) : -1);
1474 state_fio->FputInt32(sector_size.sd);
1475 state_fio->Fwrite(id, sizeof(id), 1);
1476 state_fio->FputUint8(density);
1477 state_fio->FputBool(deleted);
1478 state_fio->FputBool(crc_error);
1479 state_fio->FputUint8(drive_type);
1480 state_fio->FputInt32(drive_rpm);
1481 state_fio->FputBool(drive_mfm);
1484 bool DISK::load_state(FILEIO* state_fio)
1486 if(state_fio->FgetUint32() != STATE_VERSION) {
1489 state_fio->Fread(buffer, sizeof(buffer), 1);
1490 state_fio->Fread(orig_path, sizeof(orig_path), 1);
1491 state_fio->Fread(dest_path, sizeof(dest_path), 1);
1492 file_size.d = state_fio->FgetUint32();
1493 file_bank = state_fio->FgetInt32();
1494 crc32 = state_fio->FgetUint32();
1495 state_fio->Fread(fdi_header, sizeof(fdi_header), 1);
1496 inserted = state_fio->FgetBool();
1497 ejected = state_fio->FgetBool();
1498 write_protected = state_fio->FgetBool();
1499 changed = state_fio->FgetBool();
1500 media_type = state_fio->FgetUint8();
1501 is_standard_image = state_fio->FgetBool();
1502 is_fdi_image = state_fio->FgetBool();
1503 is_special_disk = state_fio->FgetInt32();
1504 state_fio->Fread(track, sizeof(track), 1);
1505 sector_num.sd = state_fio->FgetInt32();
1506 data_size_shift = state_fio->FgetInt32();
1507 too_many_sectors = state_fio->FgetBool();
1508 no_skew = state_fio->FgetBool();
1509 state_fio->Fread(sync_position, sizeof(sync_position), 1);
1510 state_fio->Fread(id_position, sizeof(id_position), 1);
1511 state_fio->Fread(data_position, sizeof(data_position), 1);
1512 int offset = state_fio->FgetInt32();
1513 sector = (offset != -1) ? buffer + offset : NULL;
1514 sector_size.sd = state_fio->FgetInt32();
1515 state_fio->Fread(id, sizeof(id), 1);
1516 density = state_fio->FgetUint8();
1517 deleted = state_fio->FgetBool();
1518 crc_error = state_fio->FgetBool();
1519 drive_type = state_fio->FgetUint8();
1520 drive_rpm = state_fio->FgetInt32();
1521 drive_mfm = state_fio->FgetBool();