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;
143 // trim_required = false;
147 fi->Fseek(0, FILEIO_SEEK_END);
148 file_size.d = fi->Ftell();
149 fi->Fseek(0, FILEIO_SEEK_SET);
152 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
153 // is this 2d format ?
154 if(check_file_extension(path, _T(".2d"))) {
155 if(standard_to_d88(MEDIA_TYPE_2D, 40, 2, 16, 256)) {
156 inserted = changed = is_standard_image = true;
159 fi->Fseek(0, FILEIO_SEEK_SET);
163 // check image file format
164 for(int i = 0;; i++) {
165 const fd_format_t *p = &fd_formats[i];
169 int len = p->ncyl * p->nside * p->nsec * p->size;
170 // 4096 bytes: FDI header ???
171 if(file_size.d == len || (file_size.d == (len + 4096) && (len == 655360 || len == 1261568))) {
172 if(file_size.d == len + 4096) {
174 fi->Fread(fdi_header, 4096, 1);
176 if(standard_to_d88(p->type, p->ncyl, p->nside, p->nsec, p->size)) {
177 inserted = changed = is_standard_image = true;
182 if(0 < file_size.d && file_size.d <= DISK_BUFFER_SIZE) {
183 memset(buffer, 0, sizeof(buffer));
184 fi->Fread(buffer, file_size.d, 1);
186 // check d88 format (temporary)
187 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]) {
188 inserted = changed = true;
191 _stprintf_s(dest_path, _MAX_PATH, _T("%s.D88"), path);
195 if(memcmp(buffer, "TD", 2) == 0 || memcmp(buffer, "td", 2) == 0) {
196 // teledisk image file
197 inserted = changed = converted = teledisk_to_d88();
198 } else if(memcmp(buffer, "IMD", 3) == 0) {
199 // imagedisk image file
200 inserted = changed = converted = imagedisk_to_d88();
201 } else if(memcmp(buffer, "MV - CPC", 8) == 0) {
202 // standard cpdread image file
203 inserted = changed = converted = cpdread_to_d88(0);
204 } else if(memcmp(buffer, "EXTENDED", 8) == 0) {
205 // extended cpdread image file
206 inserted = changed = converted = cpdread_to_d88(1);
209 // failed to convert the disk image
217 fi->Remove(temp_path);
223 FILEIO* fio = new FILEIO();
224 if(fio->Fopen(dest_path, FILEIO_WRITE_BINARY)) {
225 fio->Fwrite(buffer, file_size.d, 1);
231 crc32 = getcrc32(buffer, file_size.d);
233 if(buffer[0x1a] != 0) {
234 write_protected = true;
236 if(media_type == MEDIA_TYPE_UNK) {
237 if((media_type = buffer[0x1b]) == MEDIA_TYPE_2HD) {
238 for(int trkside = 0; trkside < 164; trkside++) {
240 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
242 if(!IS_VALID_TRACK(offset.d)) {
246 uint8 *t = buffer + offset.d;
247 pair sector_num, data_size;
248 sector_num.read_2bytes_le_from(t + 4);
249 data_size.read_2bytes_le_from(t + 14);
251 if(sector_num.sd >= 18 && data_size.sd == 512) {
252 media_type = MEDIA_TYPE_144;
258 // FIXME: ugly patch for X1turbo ALPHA and Batten Tanuki
260 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
261 if(media_type == MEDIA_TYPE_2D) {
262 // check first sector
264 offset.read_4bytes_le_from(buffer + 0x20);
265 if(IS_VALID_TRACK(offset.d)) {
266 static const uint8 batten[] = {0xca, 0xde, 0xaf, 0xc3, 0xdd, 0x20, 0xc0, 0xc7, 0xb7};
267 uint8 *t = buffer + offset.d;
268 if(strncmp((char *)(t + 0x11), "turbo ALPHA", 11) == 0) {
269 is_special_disk = SPECIAL_DISK_X1_ALPHA;
270 } else if(memcmp((void *)(t + 0x11), batten, sizeof(batten)) == 0) {
271 is_special_disk = SPECIAL_DISK_X1_BATTEN;
286 trim_required = false;
288 if(!write_protected && file_size.d && getcrc32(buffer, file_size.d) != crc32) {
290 FILEIO* fio = new FILEIO();
291 int pre_size = 0, post_size = 0;
292 uint8 *pre_buffer = NULL, *post_buffer = NULL;
294 // is this d88 format ?
295 if(check_file_extension(dest_path, _T(".d88")) || check_file_extension(dest_path, _T(".d77"))) {
296 if(fio->Fopen(dest_path, FILEIO_READ_BINARY)) {
297 fio->Fseek(0, FILEIO_SEEK_END);
298 uint32 total_size = fio->Ftell(), offset = 0;
299 for(int i = 0; i < file_bank; i++) {
300 fio->Fseek(offset + 0x1c, SEEK_SET);
301 offset += fio->FgetUint32_LE();
303 if((pre_size = offset) > 0) {
304 pre_buffer = (uint8 *)malloc(pre_size);
305 fio->Fseek(0, FILEIO_SEEK_SET);
306 fio->Fread(pre_buffer, pre_size, 1);
308 fio->Fseek(offset + 0x1c, SEEK_SET);
309 offset += fio->FgetUint32_LE();
310 if((post_size = total_size - offset) > 0) {
311 post_buffer = (uint8 *)malloc(post_size);
312 fio->Fseek(offset, FILEIO_SEEK_SET);
313 fio->Fread(post_buffer, post_size, 1);
318 if(!fio->Fopen(dest_path, FILEIO_WRITE_BINARY)) {
319 _TCHAR tmp_path[_MAX_PATH];
320 _stprintf_s(tmp_path, _MAX_PATH, _T("temporary_saved_floppy_disk_#%d.d88"), drive_num);
321 fio->Fopen(emu->bios_path(tmp_path), FILEIO_WRITE_BINARY);
323 if(fio->IsOpened()) {
325 fio->Fwrite(pre_buffer, pre_size, 1);
327 if(is_standard_image) {
329 fio->Fwrite(fdi_header, 4096, 1);
331 for(int trkside = 0; trkside < 164; trkside++) {
333 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
335 if(!IS_VALID_TRACK(offset.d)) {
338 uint8* t = buffer + offset.d;
339 pair sector_num, data_size;
340 sector_num.read_2bytes_le_from(t + 4);
342 for(int i = 0; i < sector_num.sd; i++) {
343 data_size.read_2bytes_le_from(t + 14);
344 fio->Fwrite(t + 0x10, data_size.sd, 1);
345 t += data_size.sd + 0x10;
349 fio->Fwrite(buffer, file_size.d, 1);
352 fio->Fwrite(post_buffer, post_size, 1);
366 inserted = write_protected = false;
368 sector_size.sd = sector_num.sd = 0;
372 bool DISK::get_track(int trk, int side)
374 sector_size.sd = sector_num.sd = 0;
377 // disk not inserted or invalid media type
378 if(!(inserted && check_media_type())) {
383 int trkside = trk * 2 + (side & 1);
384 if(!(0 <= trkside && trkside < 164)) {
388 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
390 if(!IS_VALID_TRACK(offset.d)) {
395 sector = buffer + offset.d;
396 sector_num.read_2bytes_le_from(sector + 4);
398 // create each sector position in track
399 int sync_size = drive_mfm ? 12 : 6;
400 int am_size = drive_mfm ? 3 : 0;
401 int gap2_size = drive_mfm ? 22 : 11;
404 too_many_sectors = false;
407 int total = 0, gap3_size;
409 for(int i = 0; i < sector_num.sd; i++) {
411 data_size.read_2bytes_le_from(t + 14);
412 if((data_size.sd >> data_size_shift) < 0x80) {
413 too_many_sectors = true;
416 total += sync_size + (am_size + 1) + (4 + 2) + gap2_size + sync_size + (am_size + 1);
417 total += (data_size.sd >> data_size_shift) + 2;
418 t += data_size.sd + 0x10;
420 if(too_many_sectors) {
421 // Too many sectors in this track
424 } else if((gap3_size = (get_track_size() - total) / (sector_num.sd + 2)) < 12) {
430 total = gap3_size * 2;
432 for(int i = 0; i < sector_num.sd; i++) {
434 data_size.read_2bytes_le_from(t + 14);
435 if(too_many_sectors) {
436 total = gap3_size * 2 + (get_track_size() - gap3_size * 2) * i / sector_num.sd;
438 sync_position[i] = total;
439 total += sync_size + (am_size + 1);
440 id_position[i] = total;
441 total += (4 + 2) + gap2_size + sync_size + (am_size + 1);
442 data_position[i] = total;
443 total += (data_size.sd >> data_size_shift) + 2 + gap3_size;
447 t += data_size.sd + 0x10;
452 bool DISK::make_track(int trk, int side)
454 int track_size = get_track_size();
456 if(!get_track(trk, side)) {
457 // create a dummy track
458 for(int i = 0; i < track_size; i++) {
465 int sync_size = drive_mfm ? 12 : 6;
466 int am_size = drive_mfm ? 3 : 0;
467 int gap2_size = drive_mfm ? 22 : 11;
468 uint8 gap_data = drive_mfm ? 0x4e : 0xff;
471 memset(track, gap_data, track_size);
473 if(sync_position[0] >= (sync_size + am_size + 1) + (8 + 5)) {
474 int p = (sync_position[0] - (sync_size + am_size + 1)) * 8 / (8 + 5);
477 for(int i = 0; i < sync_size; i++) {
481 for(int i = 0; i < am_size; i++) {
490 for(int i = 0; i < sector_num.sd; i++) {
492 data_size.read_2bytes_le_from(t + 14);
493 int p = sync_position[i];
496 for(int j = 0; j < sync_size; j++) {
497 if(p < track_size) track[p++] = 0x00;
500 for(int j = 0; j < am_size; j++) {
501 if(p < track_size) track[p++] = 0xa1;
503 if(p < track_size) track[p++] = 0xfe;
505 if(p < track_size) track[p++] = t[0];
506 if(p < track_size) track[p++] = t[1];
507 if(p < track_size) track[p++] = t[2];
508 if(p < track_size) track[p++] = t[3];
510 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
511 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
512 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
513 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
514 if(p < track_size) track[p++] = (crc >> 8) & 0xff;
515 if(p < track_size) track[p++] = (crc >> 0) & 0xff;
517 for(int j = 0; j < gap2_size; j++) {
518 if(p < track_size) track[p++] = gap_data;
521 for(int j = 0; j < sync_size; j++) {
522 if(p < track_size) track[p++] = 0x00;
525 for(int j = 0; j < am_size; j++) {
526 if(p < track_size) track[p++] = 0xa1;
528 if(p < track_size) track[p++] = (t[7] != 0) ? 0xf8 : 0xfb;
531 for(int j = 0; j < (data_size.sd >> data_size_shift); j++) {
532 if(p < track_size) track[p++] = t[0x10 + j];
533 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0x10 + j]]);
535 if(p < track_size) track[p++] = (crc >> 8) & 0xff;
536 if(p < track_size) track[p++] = (crc >> 0) & 0xff;
538 t += data_size.sd + 0x10;
543 bool DISK::get_sector(int trk, int side, int index)
545 sector_size.sd = sector_num.sd = 0;
548 // disk not inserted or invalid media type
549 if(!(inserted && check_media_type())) {
554 int trkside = trk * 2 + (side & 1);
555 if(!(0 <= trkside && trkside < 164)) {
559 offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
561 if(!IS_VALID_TRACK(offset.d)) {
566 uint8* t = buffer + offset.d;
567 sector_num.read_2bytes_le_from(t + 4);
569 if(index >= sector_num.sd) {
574 for(int i = 0; i < index; i++) {
576 data_size.read_2bytes_le_from(t + 14);
577 t += data_size.sd + 0x10;
583 void DISK::set_sector_info(uint8 *t)
591 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
592 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
593 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
594 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
595 id[4] = (crc >> 8) & 0xff;
596 id[5] = (crc >> 0) & 0xff;
598 deleted = (t[7] != 0);
599 crc_error = (t[8] != 0x00 && t[8] != 0x10);
601 sector_size.read_2bytes_le_from(t + 14);
604 void DISK::set_deleted(bool value)
607 uint8 *t = sector - 0x10;
608 t[7] = value ? 0x10 : 0;
609 if(t[8] == 0x00 || t[8] == 0x10) {
616 void DISK::set_crc_error(bool value)
619 uint8 *t = sector - 0x10;
620 t[8] = value ? 0xb0 : t[7];
625 bool DISK::format_track(int trk, int side)
627 // disk not inserted or invalid media type
628 if(!(inserted && check_media_type())) {
633 int trkside = trk * 2 + (side & 1);
634 if(!(0 <= trkside && trkside < 164)) {
638 // create new empty track
641 trim_required = false;
643 memset(buffer + DISK_BUFFER_SIZE, 0, sizeof(buffer) - DISK_BUFFER_SIZE);
645 offset.d = DISK_BUFFER_SIZE;
646 offset.write_4bytes_le_to(buffer + 0x20 + trkside * 4);
648 trim_required = true;
653 void DISK::insert_sector(uint8 c, uint8 h, uint8 r, uint8 n, bool deleted, bool crc_error, uint8 fill_data, int length)
655 uint8* t = buffer + DISK_BUFFER_SIZE;
658 for(int i = 0; i < (sector_num.sd - 1); i++) {
659 t[4] = sector_num.b.l;
660 t[5] = sector_num.b.h;
662 data_size.read_2bytes_le_from(t + 14);
663 t += data_size.sd + 0x10;
669 t[4] = sector_num.b.l;
670 t[5] = sector_num.b.h;
671 t[6] = drive_mfm ? 0 : 0x40;
672 t[7] = deleted ? 0x10 : 0;
673 t[8] = crc_error ? 0xb0 : t[7];
674 t[14] = (length >> 0) & 0xff;
675 t[15] = (length >> 8) & 0xff;
676 memset(t + 16, fill_data, length);
681 void DISK::trim_buffer()
683 int max_tracks = 164;
684 uint32 dest_offset = 0x2b0;
687 memset(tmp_buffer, 0, sizeof(tmp_buffer));
688 memcpy(tmp_buffer, buffer, 0x20);
691 for(int trkside = 0; trkside < 164; trkside++) {
693 src_trk_offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
694 if(src_trk_offset.d != 0) {
696 if(src_trk_offset.d < 0x2b0) {
697 max_tracks = (src_trk_offset.d - 0x20) >> 2;
700 if(src_trk_offset.d != 0x2b0) {
701 max_tracks = (src_trk_offset.d - 0x20) >> 2;
702 if(max_tracks > 164) {
703 dest_offset = 0x20 + max_tracks * 4);
712 for(int trkside = 0; trkside < max_tracks; trkside++) {
714 src_trk_offset.read_4bytes_le_from(buffer + 0x20 + trkside * 4);
716 pair dest_trk_offset;
717 dest_trk_offset.d = 0;
719 if(IS_VALID_TRACK(src_trk_offset.d)) {
720 uint8* t = buffer + src_trk_offset.d;
721 pair sector_num, data_size;
722 sector_num.read_2bytes_le_from(t + 4);
723 if(sector_num.sd != 0) {
724 dest_trk_offset.d = dest_offset;
725 for(int i = 0; i < sector_num.sd; i++) {
726 data_size.read_2bytes_le_from(t + 14);
727 memcpy(tmp_buffer + dest_offset, t, data_size.sd + 0x10);
728 dest_offset += data_size.sd + 0x10;
729 t += data_size.sd + 0x10;
733 dest_trk_offset.write_4bytes_le_to(tmp_buffer + 0x20 + trkside * 4);
737 file_size.d = dest_offset;
738 file_size.write_4bytes_le_to(tmp_buffer + 0x1c);
740 memset(buffer, 0, sizeof(buffer));
741 memcpy(buffer, tmp_buffer, file_size.d);
748 } else if(inserted) {
749 return (media_type == MEDIA_TYPE_2HD) ? 360 : 300;
751 return (drive_type == DRIVE_TYPE_2HD) ? 360 : 300;
755 int DISK::get_track_size()
758 return media_type == MEDIA_TYPE_144 ? 12500 : media_type == MEDIA_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
760 return drive_type == DRIVE_TYPE_144 ? 12500 : drive_type == DRIVE_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
764 double DISK::get_usec_per_bytes(int bytes)
766 return 1000000.0 / (get_track_size() * (get_rpm() / 60.0)) * bytes;
769 bool DISK::check_media_type()
773 return (media_type == MEDIA_TYPE_2D);
775 return (media_type == MEDIA_TYPE_2D || media_type == MEDIA_TYPE_2DD);
777 return (media_type == MEDIA_TYPE_2HD);
779 return (media_type == MEDIA_TYPE_144);
781 return true; // always okay
786 // teledisk image decoder
789 this teledisk image decoder is based on:
791 LZHUF.C English version 1.0 based on Japanese version 29-NOV-1988
792 LZSS coded by Haruhiko OKUMURA
793 Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
794 Edited and translated to English by Kenji RIKITAKE
798 #define COPYBUFFER(src, size) { \
799 if(file_size.d + (size) > DISK_BUFFER_SIZE) { \
802 memcpy(buffer + file_size.d, (src), (size)); \
803 file_size.d += (size); \
806 bool DISK::teledisk_to_d88()
816 // check teledisk header
817 fi->Fseek(0, FILEIO_SEEK_SET);
818 fi->Fread(&hdr, sizeof(td_hdr_t), 1);
819 if(hdr.sig[0] == 't' && hdr.sig[1] == 'd') {
820 // decompress to the temporary file
821 FILEIO* fo = new FILEIO();
822 if(!fo->Fopen(temp_path, FILEIO_WRITE_BINARY)) {
829 if((rd = decode(obuf, 512)) > 0) {
830 fo->Fwrite(obuf, rd, 1);
838 // reopen the temporary file
840 if(!fi->Fopen(temp_path, FILEIO_READ_BINARY)) {
844 if(hdr.flag & 0x80) {
846 fi->Fread(&cmt, sizeof(td_cmt_t), 1);
847 fi->Fseek(cmt.len, FILEIO_SEEK_CUR);
854 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
855 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "TELEDISK");
856 d88_hdr.protect = 0; // non-protected
857 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
860 int trkcnt = 0, trkptr = sizeof(d88_hdr_t);
861 fi->Fread(&trk, sizeof(td_trk_t), 1);
862 while(trk.nsec != 0xff) {
863 d88_hdr.trkptr[trkcnt++] = trkptr;
865 d88_hdr.trkptr[trkcnt++] = trkptr;
868 // read sectors in this track
869 for(int i = 0; i < trk.nsec; i++) {
870 uint8 buf[2048], dst[2048];
871 memset(buf, 0, sizeof(buf));
872 memset(dst, 0, sizeof(dst));
874 // read sector header
875 fi->Fread(&sct, sizeof(td_sct_t), 1);
877 // create d88 sector header
878 memset(&d88_sct, 0, sizeof(d88_sct_t));
883 d88_sct.nsec = trk.nsec;
884 d88_sct.dens = (hdr.dens & 0x80) ? 0x40 : 0;
885 d88_sct.del = (sct.ctrl & 4) ? 0x10 : 0;
886 d88_sct.stat = (sct.ctrl & 2) ? 0xb0 : d88_sct.del;
887 d88_sct.size = secsize[sct.n & 3];
889 // create sector image
890 if(sct.ctrl != 0x10) {
891 // read sector source
892 int len = fi->Fgetc();
893 len += fi->Fgetc() * 256 - 1;
894 int flag = fi->Fgetc(), d = 0;
895 fi->Fread(buf, len, 1);
899 memcpy(dst, buf, len);
900 } else if(flag == 1) {
902 len2.read_2bytes_le_from(buf);
907 } else if(flag == 2) {
908 for(int s = 0; s < len;) {
915 } else if(type < 5) {
921 for(int j = 0; j < n; j++) {
925 for(int j = 0; j < n; j++) {
930 break; // unknown type
934 break; // unknown flag
941 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
942 COPYBUFFER(dst, d88_sct.size);
943 trkptr += sizeof(d88_sct_t) + d88_sct.size;
946 fi->Fread(&trk, sizeof(td_trk_t), 1);
948 d88_hdr.type = ((hdr.dens & 3) == 2) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
949 d88_hdr.size = trkptr;
950 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
954 int DISK::next_word()
956 if(ibufndx >= ibufcnt) {
957 ibufndx = ibufcnt = 0;
958 memset(inbuf, 0, 512);
959 for(int i = 0; i < 512; i++) {
972 getbuf |= inbuf[ibufndx++] << (8 - getlen);
980 if(next_word() < 0) {
986 return (i < 0) ? 1 : 0;
991 if(next_word() != 0) {
1001 void DISK::start_huff()
1004 for(i = 0; i < N_CHAR; i++) {
1006 son[i] = i + TABLE_SIZE;
1007 prnt[i + TABLE_SIZE] = i;
1010 while(j <= ROOT_POSITION) {
1011 freq[j] = freq[i] + freq[i + 1];
1013 prnt[i] = prnt[i + 1] = j;
1016 freq[TABLE_SIZE] = 0xffff;
1017 prnt[ROOT_POSITION] = 0;
1020 void DISK::reconst()
1024 for(i = 0; i < TABLE_SIZE; i++) {
1025 if(son[i] >= TABLE_SIZE) {
1026 freq[j] = (freq[i] + 1) / 2;
1031 for(i = 0, j = N_CHAR; j < TABLE_SIZE; i += 2, j++) {
1033 f = freq[j] = freq[i] + freq[k];
1034 for(k = j - 1; f < freq[k]; k--);
1037 memmove(&freq[k + 1], &freq[k], l);
1039 memmove(&son[k + 1], &son[k], l);
1042 for(i = 0; i < TABLE_SIZE; i++) {
1043 if((k = son[i]) >= TABLE_SIZE) {
1046 prnt[k] = prnt[k + 1] = i;
1051 void DISK::update(int c)
1054 if(freq[ROOT_POSITION] == MAX_FREQ) {
1057 c = prnt[c + TABLE_SIZE];
1060 if(k > freq[l = c + 1]) {
1061 while(k > freq[++l]);
1067 if(i < TABLE_SIZE) {
1073 if(j < TABLE_SIZE) {
1080 while((c = prnt[c]) != 0);
1083 short DISK::decode_char()
1086 uint16 c = son[ROOT_POSITION];
1087 while(c < TABLE_SIZE) {
1088 if((ret = get_bit()) < 0) {
1099 short DISK::decode_position()
1103 if((bit = get_byte()) < 0) {
1107 c = (uint16)d_code[i] << 6;
1110 if((bit = get_bit()) < 0) {
1115 return (c | i & 0x3f);
1118 void DISK::init_decode()
1120 ibufcnt= ibufndx = bufcnt = getbuf = 0;
1123 for(int i = 0; i < STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE; i++) {
1126 ptr = STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE;
1129 int DISK::decode(uint8 *buf, int len)
1133 for(count = 0; count < len;) {
1135 if((c = decode_char()) < 0) {
1139 *(buf++) = (uint8)c;
1140 text_buf[ptr++] = (uint8)c;
1141 ptr &= (STRING_BUFFER_SIZE - 1);
1144 if((pos = decode_position()) < 0) {
1147 bufpos = (ptr - pos - 1) & (STRING_BUFFER_SIZE - 1);
1148 bufcnt = c - 255 + THRESHOLD;
1152 while(bufndx < bufcnt && count < len) {
1153 c = text_buf[(bufpos + bufndx) & (STRING_BUFFER_SIZE - 1)];
1154 *(buf++) = (uint8)c;
1156 text_buf[ptr++] = (uint8)c;
1157 ptr &= (STRING_BUFFER_SIZE - 1);
1160 if(bufndx >= bufcnt) {
1161 bufndx = bufcnt = 0;
1168 // imagedisk image decoder
1170 bool DISK::imagedisk_to_d88()
1177 fi->Fseek(0, FILEIO_SEEK_SET);
1179 while((tmp = fi->Fgetc()) != 0x1a) {
1188 // create d88 header
1189 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1190 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "IMAGEDISK");
1191 d88_hdr.protect = 0; // non-protected
1192 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1195 int trkptr = sizeof(d88_hdr_t);
1196 int trkcnt = 0, mode;
1198 for(int t = 0; t < 164; t++) {
1199 // check end of file
1200 if(fi->Fread(&trk, sizeof(imd_trk_t), 1) != 1) {
1205 // check track header
1207 mode = trk.mode % 3; // 0=500kbps, 1=300kbps, 2=250kbps
1212 d88_hdr.trkptr[t] = trkptr;
1215 uint8 c[64], h[64], r[64];
1216 fi->Fread(r, trk.nsec, 1);
1217 if(trk.head & 0x80) {
1218 fi->Fread(c, trk.nsec, 1);
1220 memset(c, trk.cyl, sizeof(c));
1222 if(trk.head & 0x40) {
1223 fi->Fread(h, trk.nsec, 1);
1225 memset(h, trk.head & 1, sizeof(h));
1228 // read sectors in this track
1229 for(int i = 0; i < trk.nsec; i++) {
1230 // create d88 sector header
1231 int sectype = fi->Fgetc();
1235 memset(&d88_sct, 0, sizeof(d88_sct_t));
1239 d88_sct.n = trk.size;
1240 d88_sct.nsec = trk.nsec;
1241 d88_sct.dens = (trk.mode < 3) ? 0x40 : 0;
1242 d88_sct.del = (sectype == 3 || sectype == 4 || sectype == 7 || sectype == 8) ? 0x10 : 0;
1243 d88_sct.stat = (sectype == 5 || sectype == 6 || sectype == 7 || sectype == 8) ? 0xb0 : d88_sct.del;
1244 d88_sct.size = secsize[trk.size & 7];
1246 // create sector image
1248 if(sectype == 1 || sectype == 3 || sectype == 5 || sectype == 7) {
1250 fi->Fread(dst, d88_sct.size, 1);
1251 } else if(sectype == 2 || sectype == 4 || sectype == 6 || sectype == 8) {
1253 int tmp = fi->Fgetc();
1254 memset(dst, tmp, d88_sct.size);
1260 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1261 COPYBUFFER(dst, d88_sct.size);
1262 trkptr += sizeof(d88_sct_t) + d88_sct.size;
1265 d88_hdr.type = (mode == 0) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
1266 d88_hdr.size = trkptr;
1267 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1271 // cpdread image decoder (from MESS formats/dsk_dsk.c)
1273 bool DISK::cpdread_to_d88(int extended)
1279 // get cylinder number and side number
1280 memcpy(tmp_buffer, buffer, file_size.d);
1281 int ncyl = tmp_buffer[0x30];
1282 int nside = tmp_buffer[0x31];
1287 // create d88 header
1288 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1289 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "CPDRead");
1290 d88_hdr.protect = 0; // non-protected
1291 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1294 int trkofs = 0x100, trkofs_ptr = 0x34;
1295 int trkptr = sizeof(d88_hdr_t);
1297 for(int c = 0; c < ncyl; c++) {
1298 for(int h = 0; h < nside; h++) {
1299 // read sectors in this track
1300 uint8 *track_info = tmp_buffer + trkofs;
1301 int cyl = track_info[0x10];
1302 int side = track_info[0x11];
1303 int nsec = track_info[0x15];
1304 int size = 1 << (track_info[0x14] + 7); // standard
1305 int sctofs = trkofs + 0x100;
1309 d88_hdr.trkptr[2 * cyl] = d88_hdr.trkptr[2 * cyl + 1] = trkptr;
1311 d88_hdr.trkptr[2 * cyl + side] = trkptr;
1313 for(int s = 0; s < nsec; s++) {
1315 uint8 *sector_info = tmp_buffer + trkofs + 0x18 + s * 8;
1317 size = sector_info[6] + sector_info[7] * 256;
1320 // create d88 sector header
1321 memset(&d88_sct, 0, sizeof(d88_sct_t));
1322 d88_sct.c = sector_info[0];
1323 d88_sct.h = sector_info[1];
1324 d88_sct.r = sector_info[2];
1325 d88_sct.n = sector_info[3];
1326 d88_sct.nsec = nsec;
1328 d88_sct.del = (sector_info[5] == 0xb2) ? 0x10 : 0;
1329 d88_sct.stat = (sector_info[5] == 0xb5) ? 0xb0 : d88_sct.del;
1330 d88_sct.size = size;
1333 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1334 COPYBUFFER(tmp_buffer + sctofs, size);
1335 trkptr += sizeof(d88_sct_t) + size;
1341 trkofs += tmp_buffer[trkofs_ptr++] * 256;
1343 trkofs += tmp_buffer[0x32] + tmp_buffer[0x33] * 256;
1347 d88_hdr.type = (total < (368640 + 655360) / 2) ? MEDIA_TYPE_2D : (total < (737280 + 1228800) / 2) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2HD;
1348 d88_hdr.size = trkptr;
1349 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1353 // standard image decoder
1355 bool DISK::standard_to_d88(int type, int ncyl, int nside, int nsec, int size)
1363 // create d88 header
1364 memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1365 _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "STANDARD");
1366 d88_hdr.protect = 0; // non-protected
1367 d88_hdr.type = (type == MEDIA_TYPE_144) ? MEDIA_TYPE_2HD : type;
1369 COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1372 for(int i = 0; i < 8; i++) {
1373 if(size == (128 << i)) {
1380 int trkptr = sizeof(d88_hdr_t);
1381 for(int c = 0; c < ncyl; c++) {
1382 for(int h = 0; h < nside; h++) {
1383 d88_hdr.trkptr[t++] = trkptr;
1386 d88_hdr.trkptr[t++] = trkptr;
1389 // read sectors in this track
1390 for(int s = 0; s < nsec; s++) {
1391 // create d88 sector header
1392 memset(&d88_sct, 0, sizeof(d88_sct_t));
1397 d88_sct.nsec = nsec;
1401 d88_sct.size = size;
1403 // create sector image
1405 memset(dst, 0xe5, sizeof(dst));
1406 fi->Fread(dst, size, 1);
1409 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1410 COPYBUFFER(dst, size);
1411 trkptr += sizeof(d88_sct_t) + size;
1415 d88_hdr.size = trkptr;
1416 memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1420 #define STATE_VERSION 3
1422 void DISK::save_state(FILEIO* state_fio)
1424 state_fio->FputUint32(STATE_VERSION);
1426 state_fio->Fwrite(buffer, sizeof(buffer), 1);
1427 state_fio->Fwrite(orig_path, sizeof(orig_path), 1);
1428 state_fio->Fwrite(dest_path, sizeof(dest_path), 1);
1429 state_fio->FputUint32(file_size.d);
1430 state_fio->FputInt32(file_bank);
1431 state_fio->FputUint32(crc32);
1432 state_fio->Fwrite(fdi_header, sizeof(fdi_header), 1);
1433 state_fio->FputBool(inserted);
1434 state_fio->FputBool(ejected);
1435 state_fio->FputBool(write_protected);
1436 state_fio->FputBool(changed);
1437 state_fio->FputUint8(media_type);
1438 state_fio->FputBool(is_standard_image);
1439 state_fio->FputBool(is_fdi_image);
1440 state_fio->FputInt32(is_special_disk);
1441 state_fio->Fwrite(track, sizeof(track), 1);
1442 state_fio->FputInt32(sector_num.sd);
1443 state_fio->FputInt32(data_size_shift);
1444 state_fio->FputBool(too_many_sectors);
1445 state_fio->FputBool(no_skew);
1446 state_fio->Fwrite(sync_position, sizeof(sync_position), 1);
1447 state_fio->Fwrite(id_position, sizeof(id_position), 1);
1448 state_fio->Fwrite(data_position, sizeof(data_position), 1);
1449 state_fio->FputInt32(sector ? (int)(sector - buffer) : -1);
1450 state_fio->FputInt32(sector_size.sd);
1451 state_fio->Fwrite(id, sizeof(id), 1);
1452 state_fio->FputUint8(density);
1453 state_fio->FputBool(deleted);
1454 state_fio->FputBool(crc_error);
1455 state_fio->FputUint8(drive_type);
1456 state_fio->FputInt32(drive_rpm);
1457 state_fio->FputBool(drive_mfm);
1460 bool DISK::load_state(FILEIO* state_fio)
1462 if(state_fio->FgetUint32() != STATE_VERSION) {
1465 state_fio->Fread(buffer, sizeof(buffer), 1);
1466 state_fio->Fread(orig_path, sizeof(orig_path), 1);
1467 state_fio->Fread(dest_path, sizeof(dest_path), 1);
1468 file_size.d = state_fio->FgetUint32();
1469 file_bank = state_fio->FgetInt32();
1470 crc32 = state_fio->FgetUint32();
1471 state_fio->Fread(fdi_header, sizeof(fdi_header), 1);
1472 inserted = state_fio->FgetBool();
1473 ejected = state_fio->FgetBool();
1474 write_protected = state_fio->FgetBool();
1475 changed = state_fio->FgetBool();
1476 media_type = state_fio->FgetUint8();
1477 is_standard_image = state_fio->FgetBool();
1478 is_fdi_image = state_fio->FgetBool();
1479 is_special_disk = state_fio->FgetInt32();
1480 state_fio->Fread(track, sizeof(track), 1);
1481 sector_num.sd = state_fio->FgetInt32();
1482 data_size_shift = state_fio->FgetInt32();
1483 too_many_sectors = state_fio->FgetBool();
1484 no_skew = state_fio->FgetBool();
1485 state_fio->Fread(sync_position, sizeof(sync_position), 1);
1486 state_fio->Fread(id_position, sizeof(id_position), 1);
1487 state_fio->Fread(data_position, sizeof(data_position), 1);
1488 int offset = state_fio->FgetInt32();
1489 sector = (offset != -1) ? buffer + offset : NULL;
1490 sector_size.sd = state_fio->FgetInt32();
1491 state_fio->Fread(id, sizeof(id), 1);
1492 density = state_fio->FgetUint8();
1493 deleted = state_fio->FgetBool();
1494 crc_error = state_fio->FgetBool();
1495 drive_type = state_fio->FgetUint8();
1496 drive_rpm = state_fio->FgetInt32();
1497 drive_mfm = state_fio->FgetBool();