OSDN Git Service

[General] Merge from upstream version, 2015-01-28.
[csp-qt/common_source_project-fm7.git] / source / src / vm / disk.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.09.16-
6
7         [ d88 handler ]
8 */
9
10 #include "disk.h"
11 #include "../fileio.h"
12 #if defined(_USE_AGAR) || defined(_USE_SDL)
13 #include "agar_logger.h"
14 #endif
15
16 // crc table
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
34 };
35
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
54 };
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
72 };
73 static const int secsize[8] = {
74         128, 256, 512, 1024, 2048, 4096, 8192, 16384
75 };
76
77 static uint8 tmp_buffer[DISK_BUFFER_SIZE];
78
79 typedef struct {
80         int type;
81         int ncyl, nside, nsec, size;
82 } fd_format_t;
83
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)
89 #else
90         { MEDIA_TYPE_2DD, 80, 2,  8,  512 },    // 2DD  640KB
91 #endif
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
97         { -1, 0, 0, 0, 0 },
98 };
99
100 void DISK::open(_TCHAR path[], int offset)
101 {
102         // check current disk image
103         if(inserted) {
104 #if defined(_USE_AGAR) || defined(_USE_SDL)
105            AGAR_DebugLog(AGAR_LOG_INFO, "Open disk: %s", path);
106 #endif
107            if(_tcsicmp(orig_path, path) == 0 && file_offset == offset) {
108                         return;
109                 }
110                 close();
111         }
112         memset(buffer, 0, sizeof(buffer));
113         media_type = MEDIA_TYPE_UNK;
114         is_standard_image = is_fdi_image = false;
115 #if defined(_USE_AGAR) || defined(_USE_SDL)
116         AGAR_DebugLog(AGAR_LOG_INFO, "Open disk: %s", path);
117 #endif  
118         // open disk image
119         fi = new FILEIO();
120         if(fi->Fopen(path, FILEIO_READ_BINARY)) {
121                 bool converted = false;
122                 
123                 _tcscpy_s(orig_path, _MAX_PATH, path);
124                 _tcscpy_s(dest_path, _MAX_PATH, path);
125                 _stprintf_s(temp_path, _MAX_PATH, _T("%s.$$$"), path);
126                 temporary = false;
127                 
128                 // check if file protected
129                 write_protected = fi->IsProtected(path);
130                 
131                 // is this d88 format ?
132                 if(check_file_extension(path, _T(".d88")) || check_file_extension(path, _T(".d77")) ||
133                    check_file_extension(path, _T(".D88")) || check_file_extension(path, _T(".D77"))) {
134                         fi->Fseek(offset + 0x1c, FILEIO_SEEK_SET);
135                         file_size = fi->Fgetc();
136                         file_size |= fi->Fgetc() << 8;
137                         file_size |= fi->Fgetc() << 16;
138                         file_size |= fi->Fgetc() << 24;
139                         fi->Fseek(offset, FILEIO_SEEK_SET);
140                         fi->Fread(buffer, file_size, 1);
141                         file_offset = offset;
142                         inserted = changed = true;
143                         goto file_loaded;
144                 }
145                 
146                 fi->Fseek(0, FILEIO_SEEK_END);
147                 file_size = fi->Ftell();
148                 fi->Fseek(0, FILEIO_SEEK_SET);
149                 file_offset = 0;
150                 
151 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
152                 // is this 2d format ?
153                 if(check_file_extension(path, _T(".2d"))) {
154                         if(standard_to_d88(MEDIA_TYPE_2D, 40, 2, 16, 256)) {
155                                 inserted = changed = is_standard_image = true;
156                                 goto file_loaded;
157                         }
158                         fi->Fseek(0, FILEIO_SEEK_SET);
159                 }
160 #endif
161                 
162                 // check image file format
163                 for(int i = 0;; i++) {
164                         const fd_format_t *p = &fd_formats[i];
165                         if(p->type == -1) {
166                                 break;
167                         }
168                         int len = p->ncyl * p->nside * p->nsec * p->size;
169                         // 4096 bytes: FDI header ???
170                         if(file_size == len || (file_size == (len + 4096) && (len == 655360 || len == 1261568))) {
171                                 if(file_size == len + 4096) {
172                                         is_fdi_image = true;
173                                         fi->Fread(fdi_header, 4096, 1);
174                                 }
175                                 if(standard_to_d88(p->type, p->ncyl, p->nside, p->nsec, p->size)) {
176                                         inserted = changed = is_standard_image = true;
177                                         goto file_loaded;
178                                 }
179                         }
180                 }
181                 if(0 < file_size && file_size <= DISK_BUFFER_SIZE) {
182                         memset(buffer, 0, sizeof(buffer));
183                         fi->Fread(buffer, file_size, 1);
184                         
185                         // check d88 format (temporary)
186                         if(*(uint32 *)(buffer + 0x1c) == file_size) {
187                                 inserted = changed = true;
188                                 goto file_loaded;
189                         }
190                         _stprintf_s(dest_path, _MAX_PATH, _T("%s.D88"), path);
191                         
192                         // check file header
193                         try {
194                                 if(memcmp(buffer, "TD", 2) == 0 || memcmp(buffer, "td", 2) == 0) {
195                                         // teledisk image file
196                                         inserted = changed = converted = teledisk_to_d88();
197                                 } else if(memcmp(buffer, "IMD", 3) == 0) {
198                                         // imagedisk image file
199                                         inserted = changed = converted = imagedisk_to_d88();
200                                 } else if(memcmp(buffer, "MV - CPC", 8) == 0) {
201                                         // standard cpdread image file
202                                         inserted = changed = converted = cpdread_to_d88(0);
203                                 } else if(memcmp(buffer, "EXTENDED", 8) == 0) {
204                                         // extended cpdread image file
205                                         inserted = changed = converted = cpdread_to_d88(1);
206                                 }
207                         }
208                         catch(...) {
209                                 // failed to convert the disk image
210 #if defined(_USE_AGAR) || defined(_USE_SDL)
211                                 AGAR_DebugLog(AGAR_LOG_INFO, "EE: disk.cpp : Failed to convert disk image.");
212 #endif
213                         }
214                 }
215 file_loaded:
216                 if(fi->IsOpened()) {
217                         fi->Fclose();
218                 }
219                 if(temporary) {
220                         fi->Remove(temp_path);
221                 }
222                 if(inserted) {
223 #if 0
224                         if(converted) {
225                                 // write image
226                                 FILEIO* fio = new FILEIO();
227                                 if(fio->Fopen(dest_path, FILEIO_WRITE_BINARY)) {
228                                         fio->Fwrite(buffer, file_size, 1);
229                                         fio->Fclose();
230                                 }
231                                 delete fio;
232                         }
233 #endif
234                         crc32 = getcrc32(buffer, file_size);
235                 }
236                 if(buffer[0x1a] != 0) {
237                         write_protected = true;
238                 }
239                 if(media_type == MEDIA_TYPE_UNK) {
240                         if((media_type = buffer[0x1b]) == MEDIA_TYPE_2HD) {
241                                 for(int trkside = 0; trkside < 164; trkside++) {
242                                         uint32 offset = buffer[0x20 + trkside * 4 + 0];
243                                         offset |= buffer[0x20 + trkside * 4 + 1] << 8;
244                                         offset |= buffer[0x20 + trkside * 4 + 2] << 16;
245                                         offset |= buffer[0x20 + trkside * 4 + 3] << 24;
246                                         
247                                         if(!offset) {
248                                                 continue;
249                                         }
250                                         // track found
251                                         uint8 *t = buffer + offset;
252                                         int sector_num = t[4] | (t[5] << 8);
253                                         int data_size = t[14] | (t[15] << 8);
254                                         
255                                         if(sector_num >= 18 && data_size == 512) {
256                                                 media_type = MEDIA_TYPE_144;
257                                         }
258                                         break;
259                                 }
260                         }
261                 }
262                 // FIXME: ugly patch for X1turbo ALPHA and Batten Tanuki
263                 is_alpha = is_batten = false;
264 #if defined(_X1) || defined(_X1TWIN) || defined(_X1TURBO) || defined(_X1TURBOZ)
265                 if(media_type == MEDIA_TYPE_2D) {
266                         static const uint8 batten[] = {0xca, 0xde, 0xaf, 0xc3, 0xdd, 0x20, 0xc0, 0xc7, 0xb7};
267                         uint32 offset = buffer[0x20] | (buffer[0x21] << 8) | (buffer[0x22] << 16) | (buffer[0x23] << 24);
268                         uint8 *t = buffer + offset;
269                         is_alpha = (strncmp((char *)(t + 0x11), "turbo ALPHA", 11) == 0);
270                         is_batten = (memcmp((void *)(t + 0x11), batten, sizeof(batten)) == 0);
271                 }
272 #endif
273         }
274         delete fi;
275 }
276
277 void DISK::close()
278 {
279         // write disk image
280         if(inserted) {
281                 if(!write_protected && file_size && getcrc32(buffer, file_size) != crc32) {
282                         // write image
283                         FILEIO* fio = new FILEIO();
284                         if(fio->Fopen(dest_path, FILEIO_READ_WRITE_BINARY)) {
285                                 fio->Fseek(file_offset, FILEIO_SEEK_SET);
286                         } else {
287                                 _TCHAR tmp_path[_MAX_PATH];
288                                 _stprintf_s(tmp_path, _MAX_PATH, _T("temporary_saved_floppy_disk_#%d.d88"), drive_num);
289                                 fio->Fopen(emu->bios_path(tmp_path), FILEIO_WRITE_BINARY);
290                         }
291                         if(fio->IsOpened()) {
292                                 if(is_standard_image) {
293                                         if(is_fdi_image) {
294                                                 fio->Fwrite(fdi_header, 4096, 1);
295                                         }
296                                         for(int trkside = 0; trkside < 164; trkside++) {
297                                                 uint32 offset = buffer[0x20 + trkside * 4 + 0];
298                                                 offset |= buffer[0x20 + trkside * 4 + 1] << 8;
299                                                 offset |= buffer[0x20 + trkside * 4 + 2] << 16;
300                                                 offset |= buffer[0x20 + trkside * 4 + 3] << 24;
301                                                 
302                                                 if(!offset) {
303                                                         break;
304                                                 }
305                                                 uint8* t = buffer + offset;
306                                                 int sector_num = t[4] | (t[5] << 8);
307                                                 
308                                                 for(int i = 0; i < sector_num; i++) {
309                                                         int data_size = t[14] | (t[15] << 8);
310                                                         fio->Fwrite(t + 0x10, data_size, 1);
311                                                         t += data_size + 0x10;
312                                                 }
313                                         }
314                                 } else {
315                                         fio->Fwrite(buffer, file_size, 1);
316                                 }
317                                 fio->Fclose();
318                         }
319                         delete fio;
320                 }
321                 ejected = true;
322         }
323         inserted = write_protected = false;
324         file_size = 0;
325         sector_size = sector_num = 0;
326         sector = NULL;
327 }
328
329 bool DISK::insert_sector(uint32 trk, uint32 side, int secnum, uint8 *chrn, uint8 *buf)
330 {
331         uint8 *from;
332         uint8 *to;
333         int n_size;
334         int s_size;
335         int right;
336         
337         if(secnum < 0) return false;
338         
339         // This function is not implemented yet.
340         return true;
341 }
342     
343 bool DISK::get_track(int trk, int side)
344 {
345         sector_size = sector_num = 0;
346         no_skew = true;
347         
348         // disk not inserted or invalid media type
349         if(!(inserted && check_media_type())) {
350                 return false;
351         }
352         
353         // search track
354         int trkside = trk * 2 + (side & 1);
355         if(!(0 <= trkside && trkside < 164)) {
356                 return false;
357         }
358         uint32 offset = buffer[0x20 + trkside * 4 + 0];
359         offset |= buffer[0x20 + trkside * 4 + 1] << 8;
360         offset |= buffer[0x20 + trkside * 4 + 2] << 16;
361         offset |= buffer[0x20 + trkside * 4 + 3] << 24;
362         
363         if(!offset) {
364                 return false;
365         }
366         
367         // track found
368         sector = buffer + offset;
369         sector_num = sector[4] | (sector[5] << 8);
370         
371         // create each sector position in track
372         int sync_size  = drive_mfm ? 12 : 6;
373         int am_size = drive_mfm ? 3 : 0;
374         int gap2_size = drive_mfm ? 22 : 11;
375         
376         data_size_shift = 0;
377         too_many_sectors = false;
378 retry:
379         uint8* t = sector;
380         int total = 0, gap3_size;
381         
382         for(int i = 0; i < sector_num; i++) {
383                 int data_size = t[14] | (t[15] << 8);
384                 
385                 if((data_size >> data_size_shift) < 0x80) {
386                         too_many_sectors = true;
387                         break;
388                 }
389                 total += sync_size + (am_size + 1) + (4 + 2) + gap2_size + sync_size + (am_size + 1);
390                 total += (data_size >> data_size_shift) + 2;
391                 
392                 t += data_size + 0x10;
393         }
394         if(too_many_sectors) {
395                 // Too many sectors in this track
396                 gap3_size = 32;
397                 data_size_shift = 0;
398         } else if((gap3_size = (get_track_size() - total) / (sector_num + 2)) < 12) {
399                 // ID:N is modified
400                 data_size_shift++;
401                 goto retry;
402         }
403         t = sector;
404         total = gap3_size * 2;
405         
406         for(int i = 0; i < sector_num; i++) {
407                 int data_size = t[14] | (t[15] << 8);
408                 
409                 if(too_many_sectors) {
410                         total = gap3_size * 2 + (get_track_size() - gap3_size * 2) * i / sector_num;
411                 }
412                 sync_position[i] = total;
413                 total += sync_size + (am_size + 1);
414                 id_position[i] = total;
415                 total += (4 + 2) + gap2_size + sync_size + (am_size + 1);
416                 data_position[i] = total;
417                 total += (data_size >> data_size_shift) + 2 + gap3_size;
418                 
419                 if(t[2] != i + 1) {
420                         no_skew = false;
421                 }
422                 t += data_size + 0x10;
423         }
424         return true;
425 }
426
427 bool DISK::make_track(int trk, int side)
428 {
429         int track_size = get_track_size();
430         
431         if(!get_track(trk, side)) {
432                 // create a dummy track
433                 for(int i = 0; i < track_size; i++) {
434                         track[i] = rand();
435                 }
436                 return false;
437         }
438         
439         // make track image
440         int sync_size  = drive_mfm ? 12 : 6;
441         int am_size = drive_mfm ? 3 : 0;
442         int gap2_size = drive_mfm ? 22 : 11;
443         uint8 gap_data = drive_mfm ? 0x4e : 0xff;
444         
445         // preamble
446         memset(track, gap_data, track_size);
447         
448         if(sync_position[0] >= (sync_size + am_size + 1) + (8 + 5)) {
449                 int p = (sync_position[0] - (sync_size + am_size + 1)) * 8 / (8 + 5);
450                 
451                 // sync
452                 for(int i = 0; i < sync_size; i++) {
453                         track[p++] = 0x00;
454                 }
455                 // index mark
456                 for(int i = 0; i < am_size; i++) {
457                         track[p++] = 0xc2;
458                 }
459                 track[p++] = 0xfc;
460         }
461         
462         // sectors
463         uint8 *t = sector;
464         
465         for(int i = 0; i < sector_num; i++) {
466                 int data_size = t[14] | (t[15] << 8);
467                 int p = sync_position[i];
468                 
469                 // sync
470                 for(int j = 0; j < sync_size; j++) {
471                         if(p < track_size) track[p++] = 0x00;
472                 }
473                 // am1
474                 for(int j = 0; j < am_size; j++) {
475                         if(p < track_size) track[p++] = 0xa1;
476                 }
477                 if(p < track_size) track[p++] = 0xfe;
478                 // id
479                 if(p < track_size) track[p++] = t[0];
480                 if(p < track_size) track[p++] = t[1];
481                 if(p < track_size) track[p++] = t[2];
482                 if(p < track_size) track[p++] = t[3];
483                 uint16 crc = 0;
484                 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
485                 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
486                 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
487                 crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
488                 if(p < track_size) track[p++] = crc >> 8;
489                 if(p < track_size) track[p++] = crc & 0xff;
490                 // gap2
491                 for(int j = 0; j < gap2_size; j++) {
492                         if(p < track_size) track[p++] = gap_data;
493                 }
494                 // sync
495                 for(int j = 0; j < sync_size; j++) {
496                         if(p < track_size) track[p++] = 0x00;
497                 }
498                 // am2
499                 for(int j = 0; j < am_size; j++) {
500                         if(p < track_size) track[p++] = 0xa1;
501                 }
502                 if(p < track_size) track[p++] = (t[7] != 0) ? 0xf8 : 0xfb;
503                 // data
504                 crc = 0;
505                 for(int j = 0; j < (data_size >> data_size_shift); j++) {
506                         if(p < track_size) track[p++] = t[0x10 + j];
507                         crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0x10 + j]]);
508                 }
509                 if(p < track_size) track[p++] = crc >> 8;
510                 if(p < track_size) track[p++] = crc & 0xff;
511                 
512                 t += data_size + 0x10;
513         }
514         return true;
515 }
516
517 bool DISK::get_sector(int trk, int side, int index)
518 {
519         sector_size = sector_num = 0;
520         sector = NULL;
521         
522         // disk not inserted or invalid media type
523         if(!(inserted && check_media_type())) {
524                 return false;
525         }
526         
527         // search track
528         int trkside = trk * 2 + (side & 1);
529         if(!(0 <= trkside && trkside < 164)) {
530                 return false;
531         }
532         uint32 offset = buffer[0x20 + trkside * 4 + 0];
533         offset |= buffer[0x20 + trkside * 4 + 1] << 8;
534         offset |= buffer[0x20 + trkside * 4 + 2] << 16;
535         offset |= buffer[0x20 + trkside * 4 + 3] << 24;
536         
537         if(!offset) {
538                 return false;
539         }
540         
541         // track found
542         uint8* t = buffer + offset;
543         sector_num = t[4] | (t[5] << 8);
544         
545         if(index >= sector_num) {
546                 return false;
547         }
548         
549         // skip sector
550         for(int i = 0; i < index; i++) {
551                 t += (t[14] | (t[15] << 8)) + 0x10;
552         }
553         
554         // header info
555         id[0] = t[0];
556         id[1] = t[1];
557         id[2] = t[2];
558         id[3] = t[3];
559         uint16 crc = 0;
560         crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[0]]);
561         crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[1]]);
562         crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[2]]);
563         crc = (uint16)((crc << 8) ^ crc_table[(uint8)(crc >> 8) ^ t[3]]);
564         id[4] = crc >> 8;
565         id[5] = crc & 0xff;
566         density = t[6];
567         deleted = t[7];
568         status = t[8];
569         sector = t + 0x10;
570         sector_size = t[14] | (t[15] << 8);
571         
572         return true;
573 }
574
575 int DISK::get_rpm()
576 {
577         if(drive_rpm != 0) {
578                 return drive_rpm;
579         } else if(inserted) {
580                 return (media_type == MEDIA_TYPE_2HD) ? 360 : 300;
581         } else {
582                 return (drive_type == DRIVE_TYPE_2HD) ? 360 : 300;
583         }
584 }
585
586 int DISK::get_track_size()
587 {
588         if(inserted) {
589                 return media_type == MEDIA_TYPE_144 ? 12500 : media_type == MEDIA_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
590         } else {
591                 return drive_type == DRIVE_TYPE_144 ? 12500 : drive_type == DRIVE_TYPE_2HD ? 10410 : drive_mfm ? 6250 : 3100;
592         }
593 }
594
595 double DISK::get_usec_per_bytes(int bytes)
596 {
597         return 1000000.0 / (get_track_size() * (get_rpm() / 60.0)) * bytes;
598 }
599
600 bool DISK::check_media_type()
601 {
602         switch(drive_type) {
603         case DRIVE_TYPE_2D:
604                 return (media_type == MEDIA_TYPE_2D);
605         case DRIVE_TYPE_2DD:
606                 return (media_type == MEDIA_TYPE_2D || media_type == MEDIA_TYPE_2DD);
607         case DRIVE_TYPE_2HD:
608                 return (media_type == MEDIA_TYPE_2HD);
609         case DRIVE_TYPE_144:
610                 return (media_type == MEDIA_TYPE_144);
611         case DRIVE_TYPE_UNK:
612                 return true; // always okay
613         }
614         return false;
615 }
616
617 // teledisk image decoder
618
619 /*
620         this teledisk image decoder is based on:
621         
622                 LZHUF.C English version 1.0 based on Japanese version 29-NOV-1988
623                 LZSS coded by Haruhiko OKUMURA
624                 Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI
625                 Edited and translated to English by Kenji RIKITAKE
626                 TDLZHUF.C by WTK
627 */
628
629 #define COPYBUFFER(src, size) { \
630         if(file_size + (size) > DISK_BUFFER_SIZE) { \
631                 return false; \
632         } \
633         memcpy(buffer + file_size, (src), (size)); \
634         file_size += (size); \
635 }
636
637 bool DISK::teledisk_to_d88()
638 {
639         td_hdr_t hdr;
640         td_cmt_t cmt;
641         td_trk_t trk;
642         td_sct_t sct;
643         d88_hdr_t d88_hdr;
644         d88_sct_t d88_sct;
645         uint8 obuf[512];
646         
647         // check teledisk header
648         fi->Fseek(0, FILEIO_SEEK_SET);
649         fi->Fread(&hdr, sizeof(td_hdr_t), 1);
650         if(hdr.sig[0] == 't' && hdr.sig[1] == 'd') {
651                 // decompress to the temporary file
652                 FILEIO* fo = new FILEIO();
653                 if(!fo->Fopen(temp_path, FILEIO_WRITE_BINARY)) {
654                         delete fo;
655                         return false;
656                 }
657                 int rd = 1;
658                 init_decode();
659                 do {
660                         if((rd = decode(obuf, 512)) > 0) {
661                                 fo->Fwrite(obuf, rd, 1);
662                         }
663                 }
664                 while(rd > 0);
665                 fo->Fclose();
666                 delete fo;
667                 temporary = true;
668                 
669                 // reopen the temporary file
670                 fi->Fclose();
671                 if(!fi->Fopen(temp_path, FILEIO_READ_BINARY)) {
672                         return false;
673                 }
674         }
675         if(hdr.flag & 0x80) {
676                 // skip comment
677                 fi->Fread(&cmt, sizeof(td_cmt_t), 1);
678                 fi->Fseek(cmt.len, FILEIO_SEEK_CUR);
679         }
680         
681         // create d88 image
682         file_size = 0;
683         
684         // create d88 header
685         memset(&d88_hdr, 0, sizeof(d88_hdr_t));
686         _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "TELEDISK");
687         d88_hdr.protect = 0; // non-protected
688         COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
689         
690         // create tracks
691         int trkcnt = 0, trkptr = sizeof(d88_hdr_t);
692         fi->Fread(&trk, sizeof(td_trk_t), 1);
693         while(trk.nsec != 0xff) {
694                 d88_hdr.trkptr[trkcnt++] = trkptr;
695                 if(hdr.sides == 1) {
696                         d88_hdr.trkptr[trkcnt++] = trkptr;
697                 }
698                 
699                 // read sectors in this track
700                 for(int i = 0; i < trk.nsec; i++) {
701                         uint8 buf[2048], dst[2048];
702                         memset(buf, 0, sizeof(buf));
703                         memset(dst, 0, sizeof(dst));
704                         
705                         // read sector header
706                         fi->Fread(&sct, sizeof(td_sct_t), 1);
707                         
708                         // create d88 sector header
709                         memset(&d88_sct, 0, sizeof(d88_sct_t));
710                         d88_sct.c = sct.c;
711                         d88_sct.h = sct.h;
712                         d88_sct.r = sct.r;
713                         d88_sct.n = sct.n;
714                         d88_sct.nsec = trk.nsec;
715                         d88_sct.dens = (hdr.dens & 0x80) ? 0x40 : 0;
716                         d88_sct.del = (sct.ctrl & 4) ? 0x10 : 0;
717                         d88_sct.stat = (sct.ctrl & 2) ? 0x10 : 0; // crc?
718                         d88_sct.size = secsize[sct.n & 3];
719                         
720                         // create sector image
721                         if(sct.ctrl != 0x10) {
722                                 // read sector source
723                                 int len = fi->Fgetc();
724                                 len += fi->Fgetc() * 256 - 1;
725                                 int flag = fi->Fgetc(), d = 0;
726                                 fi->Fread(buf, len, 1);
727                                 
728                                 // convert
729                                 if(flag == 0) {
730                                         memcpy(dst, buf, len);
731                                 } else if(flag == 1) {
732                                         int len2 = buf[0] | (buf[1] << 8);
733                                         while(len2--) {
734                                                 dst[d++] = buf[2];
735                                                 dst[d++] = buf[3];
736                                         }
737                                 } else if(flag == 2) {
738                                         for(int s = 0; s < len;) {
739                                                 int type = buf[s++];
740                                                 int len2 = buf[s++];
741                                                 if(type == 0) {
742                                                         while(len2--) {
743                                                                 dst[d++] = buf[s++];
744                                                         }
745                                                 } else if(type < 5) {
746                                                         uint8 pat[256];
747                                                         int n = 2;
748                                                         while(type-- > 1) {
749                                                                 n *= 2;
750                                                         }
751                                                         for(int j = 0; j < n; j++) {
752                                                                 pat[j] = buf[s++];
753                                                         }
754                                                         while(len2--) {
755                                                                 for(int j = 0; j < n; j++) {
756                                                                         dst[d++] = pat[j];
757                                                                 }
758                                                         }
759                                                 } else {
760                                                         break; // unknown type
761                                                 }
762                                         }
763                                 } else {
764                                         break; // unknown flag
765                                 }
766                         } else {
767                                 d88_sct.size = 0;
768                         }
769                         
770                         // copy to d88
771                         COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
772                         COPYBUFFER(dst, d88_sct.size);
773                         trkptr += sizeof(d88_sct_t) + d88_sct.size;
774                 }
775                 // read next track
776                 fi->Fread(&trk, sizeof(td_trk_t), 1);
777         }
778         d88_hdr.type = ((hdr.dens & 3) == 2) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
779         d88_hdr.size = trkptr;
780         memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
781         return true;
782 }
783
784 int DISK::next_word()
785 {
786         if(ibufndx >= ibufcnt) {
787                 ibufndx = ibufcnt = 0;
788                 memset(inbuf, 0, 512);
789                 for(int i = 0; i < 512; i++) {
790                         int d = fi->Fgetc();
791                         if(d == EOF) {
792                                 if(i) {
793                                         break;
794                                 }
795                                 return(-1);
796                         }
797                         inbuf[i] = d;
798                         ibufcnt = i + 1;
799                 }
800         }
801         while(getlen <= 8) {
802                 getbuf |= inbuf[ibufndx++] << (8 - getlen);
803                 getlen += 8;
804         }
805         return 0;
806 }
807
808 int DISK::get_bit()
809 {
810         if(next_word() < 0) {
811                 return -1;
812         }
813         short i = getbuf;
814         getbuf <<= 1;
815         getlen--;
816         return (i < 0) ? 1 : 0;
817 }
818
819 int DISK::get_byte()
820 {
821         if(next_word() != 0) {
822                 return -1;
823         }
824         uint16 i = getbuf;
825         getbuf <<= 8;
826         getlen -= 8;
827         i >>= 8;
828         return (int)i;
829 }
830
831 void DISK::start_huff()
832 {
833         int i, j;
834         for(i = 0; i < N_CHAR; i++) {
835                 freq[i] = 1;
836                 son[i] = i + TABLE_SIZE;
837                 prnt[i + TABLE_SIZE] = i;
838         }
839         i = 0; j = N_CHAR;
840         while(j <= ROOT_POSITION) {
841                 freq[j] = freq[i] + freq[i + 1];
842                 son[j] = i;
843                 prnt[i] = prnt[i + 1] = j;
844                 i += 2; j++;
845         }
846         freq[TABLE_SIZE] = 0xffff;
847         prnt[ROOT_POSITION] = 0;
848 }
849
850 void DISK::reconst()
851 {
852         short i, j = 0, k;
853         uint16 f, l;
854         for(i = 0; i < TABLE_SIZE; i++) {
855                 if(son[i] >= TABLE_SIZE) {
856                         freq[j] = (freq[i] + 1) / 2;
857                         son[j] = son[i];
858                         j++;
859                 }
860         }
861         for(i = 0, j = N_CHAR; j < TABLE_SIZE; i += 2, j++) {
862                 k = i + 1;
863                 f = freq[j] = freq[i] + freq[k];
864                 for(k = j - 1; f < freq[k]; k--);
865                 k++;
866                 l = (j - k) * 2;
867                 memmove(&freq[k + 1], &freq[k], l);
868                 freq[k] = f;
869                 memmove(&son[k + 1], &son[k], l);
870                 son[k] = i;
871         }
872         for(i = 0; i < TABLE_SIZE; i++) {
873                 if((k = son[i]) >= TABLE_SIZE) {
874                         prnt[k] = i;
875                 } else {
876                         prnt[k] = prnt[k + 1] = i;
877                 }
878         }
879 }
880
881 void DISK::update(int c)
882 {
883         int i, j, k, l;
884         if(freq[ROOT_POSITION] == MAX_FREQ) {
885                 reconst();
886         }
887         c = prnt[c + TABLE_SIZE];
888         do {
889                 k = ++freq[c];
890                 if(k > freq[l = c + 1]) {
891                         while(k > freq[++l]);
892                         l--;
893                         freq[c] = freq[l];
894                         freq[l] = k;
895                         i = son[c];
896                         prnt[i] = l;
897                         if(i < TABLE_SIZE) {
898                                 prnt[i + 1] = l;
899                         }
900                         j = son[l];
901                         son[l] = i;
902                         prnt[j] = c;
903                         if(j < TABLE_SIZE) {
904                                 prnt[j + 1] = c;
905                         }
906                         son[c] = j;
907                         c = l;
908                 }
909         }
910         while((c = prnt[c]) != 0);
911 }
912
913 short DISK::decode_char()
914 {
915         int ret;
916         uint16 c = son[ROOT_POSITION];
917         while(c < TABLE_SIZE) {
918                 if((ret = get_bit()) < 0) {
919                         return -1;
920                 }
921                 c += (unsigned)ret;
922                 c = son[c];
923         }
924         c -= TABLE_SIZE;
925         update(c);
926         return c;
927 }
928
929 short DISK::decode_position()
930 {
931         short bit;
932         uint16 i, j, c;
933         if((bit = get_byte()) < 0) {
934                 return -1;
935         }
936         i = (uint16)bit;
937         c = (uint16)d_code[i] << 6;
938         j = d_len[i] - 2;
939         while(j--) {
940                 if((bit = get_bit()) < 0) {
941                          return -1;
942                 }
943                 i = (i << 1) + bit;
944         }
945         return (c | i & 0x3f);
946 }
947
948 void DISK::init_decode()
949 {
950         ibufcnt= ibufndx = bufcnt = getbuf = 0;
951         getlen = 0;
952         start_huff();
953         for(int i = 0; i < STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE; i++) {
954                 text_buf[i] = ' ';
955         }
956         ptr = STRING_BUFFER_SIZE - LOOKAHEAD_BUFFER_SIZE;
957 }
958
959 int DISK::decode(uint8 *buf, int len)
960 {
961         short c, pos;
962         int  count;
963         for(count = 0; count < len;) {
964                 if(bufcnt == 0) {
965                         if((c = decode_char()) < 0) {
966                                 return count;
967                         }
968                         if(c < 256) {
969                                 *(buf++) = (uint8)c;
970                                 text_buf[ptr++] = (uint8)c;
971                                 ptr &= (STRING_BUFFER_SIZE - 1);
972                                 count++;
973                         } else {
974                                 if((pos = decode_position()) < 0) {
975                                         return count;
976                                 }
977                                 bufpos = (ptr - pos - 1) & (STRING_BUFFER_SIZE - 1);
978                                 bufcnt = c - 255 + THRESHOLD;
979                                 bufndx = 0;
980                         }
981                 } else {
982                         while(bufndx < bufcnt && count < len) {
983                                 c = text_buf[(bufpos + bufndx) & (STRING_BUFFER_SIZE - 1)];
984                                 *(buf++) = (uint8)c;
985                                 bufndx++;
986                                 text_buf[ptr++] = (uint8)c;
987                                 ptr &= (STRING_BUFFER_SIZE - 1);
988                                 count++;
989                         }
990                         if(bufndx >= bufcnt) {
991                                 bufndx = bufcnt = 0;
992                         }
993                 }
994         }
995         return count;
996 }
997
998 // imagedisk image decoder
999
1000 bool DISK::imagedisk_to_d88()
1001 {
1002         imd_trk_t trk;
1003         d88_hdr_t d88_hdr;
1004         d88_sct_t d88_sct;
1005         
1006         // skip comment
1007         fi->Fseek(0, FILEIO_SEEK_SET);
1008         int tmp;
1009         while((tmp = fi->Fgetc()) != 0x1a) {
1010                 if(tmp == EOF) {
1011                         return false;
1012                 }
1013         }
1014         
1015         // create d88 image
1016         file_size = 0;
1017         
1018         // create d88 header
1019         memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1020         _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "IMAGEDISK");
1021         d88_hdr.protect = 0; // non-protected
1022         COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1023         
1024         // create tracks
1025         int trkptr = sizeof(d88_hdr_t);
1026         int trkcnt = 0, mode;
1027         
1028         for(int t = 0; t < 164; t++) {
1029                 // check end of file
1030                 if(fi->Fread(&trk, sizeof(imd_trk_t), 1) != 1) {
1031                         break;
1032                 }
1033                 trkcnt = t;
1034                 
1035                 // check track header
1036                 if(t == 0) {
1037                         mode = trk.mode % 3; // 0=500kbps, 1=300kbps, 2=250kbps
1038                 }
1039                 if(!trk.nsec) {
1040                         continue;
1041                 }
1042                 d88_hdr.trkptr[t] = trkptr;
1043                 
1044                 // setup sector id
1045                 uint8 c[64], h[64], r[64];
1046                 fi->Fread(r, trk.nsec, 1);
1047                 if(trk.head & 0x80) {
1048                         fi->Fread(c, trk.nsec, 1);
1049                 } else {
1050                         memset(c, trk.cyl, sizeof(c));
1051                 }
1052                 if(trk.head & 0x40) {
1053                         fi->Fread(h, trk.nsec, 1);
1054                 } else {
1055                         memset(h, trk.head & 1, sizeof(h));
1056                 }
1057                 
1058                 // read sectors in this track
1059                 for(int i = 0; i < trk.nsec; i++) {
1060                         // create d88 sector header
1061                         static const uint8 del[] = {0, 0, 0, 0x10, 0x10, 0, 0, 0x10, 0x10};
1062                         static const uint8 err[] = {0, 0, 0, 0, 0, 0x10, 0x10, 0x10, 0x10};
1063                         int sectype = fi->Fgetc();
1064                         if(sectype > 8) {
1065                                 return false;
1066                         }
1067                         memset(&d88_sct, 0, sizeof(d88_sct_t));
1068                         d88_sct.c = c[i];
1069                         d88_sct.h = h[i];
1070                         d88_sct.r = r[i];
1071                         d88_sct.n = trk.size;
1072                         d88_sct.nsec = trk.nsec;
1073                         d88_sct.dens = (trk.mode < 3) ? 0x40 : 0;
1074                         d88_sct.del = del[sectype];
1075                         d88_sct.stat = err[sectype];
1076                         d88_sct.size = secsize[trk.size & 7];
1077                         
1078                         // create sector image
1079                         uint8 dst[8192];
1080                         if(sectype == 1 || sectype == 3 || sectype == 5 || sectype == 7) {
1081                                 // uncompressed
1082                                 fi->Fread(dst, d88_sct.size, 1);
1083                         } else if(sectype == 2 || sectype == 4 || sectype == 6 || sectype == 8) {
1084                                 // compressed
1085                                 int tmp = fi->Fgetc();
1086                                 memset(dst, tmp, d88_sct.size);
1087                         } else {
1088                                 d88_sct.size = 0;
1089                         }
1090                         
1091                         // copy to d88
1092                         COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1093                         COPYBUFFER(dst, d88_sct.size);
1094                         trkptr += sizeof(d88_sct_t) + d88_sct.size;
1095                 }
1096         }
1097         d88_hdr.type = (mode == 0) ? MEDIA_TYPE_2HD : ((trkcnt >> 1) > 60) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2D;
1098         d88_hdr.size = trkptr;
1099         memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1100         return true;
1101 }
1102
1103 // cpdread image decoder (from MESS formats/dsk_dsk.c)
1104
1105 bool DISK::cpdread_to_d88(int extended)
1106 {
1107         d88_hdr_t d88_hdr;
1108         d88_sct_t d88_sct;
1109         int total = 0;
1110         
1111         // get cylinder number and side number
1112         memcpy(tmp_buffer, buffer, file_size);
1113         int ncyl = tmp_buffer[0x30];
1114         int nside = tmp_buffer[0x31];
1115         
1116         // create d88 image
1117         file_size = 0;
1118         
1119         // create d88 header
1120         memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1121         _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "CPDRead");
1122         d88_hdr.protect = 0; // non-protected
1123         COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1124         
1125         // create tracks
1126         int trkofs = 0x100, trkofs_ptr = 0x34;
1127         int trkptr = sizeof(d88_hdr_t);
1128         
1129         for(int c = 0; c < ncyl; c++) {
1130                 for(int h = 0; h < nside; h++) {
1131                         // read sectors in this track
1132                         uint8 *track_info = tmp_buffer + trkofs;
1133                         int cyl = track_info[0x10];
1134                         int side = track_info[0x11];
1135                         int nsec = track_info[0x15];
1136                         int size = 1 << (track_info[0x14] + 7); // standard
1137                         int sctofs = trkofs + 0x100;
1138                         
1139                         if(nside == 1) {
1140                                 // double side
1141                                 d88_hdr.trkptr[2 * cyl] = d88_hdr.trkptr[2 * cyl + 1] = trkptr;
1142                         } else {
1143                                 d88_hdr.trkptr[2 * cyl + side] = trkptr;
1144                         }
1145                         for(int s = 0; s < nsec; s++) {
1146                                 // get sector size
1147                                 uint8 *sector_info = tmp_buffer + trkofs + 0x18 + s * 8;
1148                                 if(extended) {
1149                                         size = sector_info[6] + sector_info[7] * 256;
1150                                 }
1151                                 
1152                                 // create d88 sector header
1153                                 memset(&d88_sct, 0, sizeof(d88_sct_t));
1154                                 d88_sct.c = sector_info[0];
1155                                 d88_sct.h = sector_info[1];
1156                                 d88_sct.r = sector_info[2];
1157                                 d88_sct.n = sector_info[3];
1158                                 d88_sct.nsec = nsec;
1159                                 d88_sct.dens = 0;
1160                                 d88_sct.del = (sector_info[5] & 0x40) ? 0x10 : 0;
1161                                 d88_sct.stat = 0;
1162                                 d88_sct.size = size;
1163                                 
1164                                 // copy to d88
1165                                 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1166                                 COPYBUFFER(tmp_buffer + sctofs, size);
1167                                 trkptr += sizeof(d88_sct_t) + size;
1168                                 sctofs += size;
1169                                 total += size;
1170                         }
1171                         
1172                         if(extended) {
1173                                 trkofs += tmp_buffer[trkofs_ptr++] * 256;
1174                         } else {
1175                                 trkofs += tmp_buffer[0x32] + tmp_buffer[0x33] * 256;
1176                         }
1177                 }
1178         }
1179         d88_hdr.type = (total < (368640 + 655360) / 2) ? MEDIA_TYPE_2D : (total < (737280 + 1228800) / 2) ? MEDIA_TYPE_2DD : MEDIA_TYPE_2HD;
1180         d88_hdr.size = trkptr;
1181         memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1182         return true;
1183 }
1184
1185 // standard image decoder
1186
1187 bool DISK::standard_to_d88(int type, int ncyl, int nside, int nsec, int size)
1188 {
1189         d88_hdr_t d88_hdr;
1190         d88_sct_t d88_sct;
1191         int n = 0, t = 0;
1192         
1193         file_size = 0;
1194         
1195         // create d88 header
1196         memset(&d88_hdr, 0, sizeof(d88_hdr_t));
1197         _strcpy_s(d88_hdr.title, sizeof(d88_hdr.title), "STANDARD");
1198         d88_hdr.protect = 0; // non-protected
1199         d88_hdr.type = (type == MEDIA_TYPE_144) ? MEDIA_TYPE_2HD : type;
1200         media_type = type;
1201         COPYBUFFER(&d88_hdr, sizeof(d88_hdr_t));
1202         
1203         // sector length
1204         for(int i = 0; i < 8; i++) {
1205                 if(size == (128 << i)) {
1206                         n = i;
1207                         break;
1208                 }
1209         }
1210         
1211         // create tracks
1212         int trkptr = sizeof(d88_hdr_t);
1213         for(int c = 0; c < ncyl; c++) {
1214                 for(int h = 0; h < nside; h++) {
1215                         d88_hdr.trkptr[t++] = trkptr;
1216                         if(nside == 1) {
1217                                 // double side
1218                                 d88_hdr.trkptr[t++] = trkptr;
1219                         }
1220                         
1221                         // read sectors in this track
1222                         for(int s = 0; s < nsec; s++) {
1223                                 // create d88 sector header
1224                                 memset(&d88_sct, 0, sizeof(d88_sct_t));
1225                                 d88_sct.c = c;
1226                                 d88_sct.h = h;
1227                                 d88_sct.r = s + 1;
1228                                 d88_sct.n = n;
1229                                 d88_sct.nsec = nsec;
1230                                 d88_sct.dens = 0;
1231                                 d88_sct.del = 0;
1232                                 d88_sct.stat = 0;
1233                                 d88_sct.size = size;
1234                                 
1235                                 // create sector image
1236                                 uint8 dst[16384];
1237                                 memset(dst, 0xe5, sizeof(dst));
1238                                 fi->Fread(dst, size, 1);
1239                                 
1240                                 // copy to d88
1241                                 COPYBUFFER(&d88_sct, sizeof(d88_sct_t));
1242                                 COPYBUFFER(dst, size);
1243                                 trkptr += sizeof(d88_sct_t) + size;
1244                         }
1245                 }
1246         }
1247         d88_hdr.size = trkptr;
1248         memcpy(buffer, &d88_hdr, sizeof(d88_hdr_t));
1249         return true;
1250 }
1251
1252 #define STATE_VERSION   2
1253
1254 void DISK::save_state(FILEIO* state_fio)
1255 {
1256         state_fio->FputUint32(STATE_VERSION);
1257         
1258         state_fio->Fwrite(buffer, sizeof(buffer), 1);
1259         state_fio->Fwrite(orig_path, sizeof(orig_path), 1);
1260         state_fio->Fwrite(dest_path, sizeof(dest_path), 1);
1261         state_fio->FputInt32(file_size);
1262         state_fio->FputInt32(file_offset);
1263         state_fio->FputUint32(crc32);
1264         state_fio->Fwrite(fdi_header, sizeof(fdi_header), 1);
1265         state_fio->FputBool(inserted);
1266         state_fio->FputBool(ejected);
1267         state_fio->FputBool(write_protected);
1268         state_fio->FputBool(changed);
1269         state_fio->FputUint8(media_type);
1270         state_fio->FputBool(is_standard_image);
1271         state_fio->FputBool(is_fdi_image);
1272         state_fio->FputBool(is_alpha);
1273         state_fio->FputBool(is_batten);
1274         state_fio->Fwrite(track, sizeof(track), 1);
1275         state_fio->FputInt32(sector_num);
1276         state_fio->FputInt32(data_size_shift);
1277         state_fio->FputBool(too_many_sectors);
1278         state_fio->FputBool(no_skew);
1279         state_fio->Fwrite(sync_position, sizeof(sync_position), 1);
1280         state_fio->Fwrite(id_position, sizeof(id_position), 1);
1281         state_fio->Fwrite(data_position, sizeof(data_position), 1);
1282         state_fio->FputInt32(sector ? (int)(sector - buffer) : -1);
1283         state_fio->FputInt32(sector_size);
1284         state_fio->Fwrite(id, sizeof(id), 1);
1285         state_fio->FputUint8(density);
1286         state_fio->FputUint8(deleted);
1287         state_fio->FputUint8(status);
1288         state_fio->FputUint8(drive_type);
1289         state_fio->FputInt32(drive_rpm);
1290         state_fio->FputBool(drive_mfm);
1291 }
1292
1293 bool DISK::load_state(FILEIO* state_fio)
1294 {
1295         if(state_fio->FgetUint32() != STATE_VERSION) {
1296                 return false;
1297         }
1298         state_fio->Fread(buffer, sizeof(buffer), 1);
1299         state_fio->Fread(orig_path, sizeof(orig_path), 1);
1300         state_fio->Fread(dest_path, sizeof(dest_path), 1);
1301         file_size = state_fio->FgetInt32();
1302         file_offset = state_fio->FgetInt32();
1303         crc32 = state_fio->FgetUint32();
1304         state_fio->Fread(fdi_header, sizeof(fdi_header), 1);
1305         inserted = state_fio->FgetBool();
1306         ejected = state_fio->FgetBool();
1307         write_protected = state_fio->FgetBool();
1308         changed = state_fio->FgetBool();
1309         media_type = state_fio->FgetUint8();
1310         is_standard_image = state_fio->FgetBool();
1311         is_fdi_image = state_fio->FgetBool();
1312         is_alpha = state_fio->FgetBool();
1313         is_batten = state_fio->FgetBool();
1314         state_fio->Fread(track, sizeof(track), 1);
1315         sector_num = state_fio->FgetInt32();
1316         data_size_shift = state_fio->FgetInt32();
1317         too_many_sectors = state_fio->FgetBool();
1318         no_skew = state_fio->FgetBool();
1319         state_fio->Fread(sync_position, sizeof(sync_position), 1);
1320         state_fio->Fread(id_position, sizeof(id_position), 1);
1321         state_fio->Fread(data_position, sizeof(data_position), 1);
1322         int offset = state_fio->FgetInt32();
1323         sector = (offset != -1) ? buffer + offset : NULL;
1324         sector_size = state_fio->FgetInt32();
1325         state_fio->Fread(id, sizeof(id), 1);
1326         density = state_fio->FgetUint8();
1327         deleted = state_fio->FgetUint8();
1328         status = state_fio->FgetUint8();
1329         drive_type = state_fio->FgetUint8();
1330         drive_rpm = state_fio->FgetInt32();
1331         drive_mfm = state_fio->FgetBool();
1332         return true;
1333 }
1334