forked from jhallen/atari-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimd2atr.c
executable file
·410 lines (371 loc) · 9.4 KB
/
imd2atr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Convert Nick Kennedy's .ATR (Atari) disk image file format to
* Dave Dunfield's .IMD (ImageDisk) file format
*
* Copyright
* (C) 2011 Joseph H. Allen
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 1, or (at your option) any later version.
*
* It is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this software; see the file COPYING. If not, write to the Free Software Foundation,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
/* A track */
struct track {
struct track *next;
int mode; /*
0 = 500 kbps FM
1 = 300 kbps FM
2 = 250 kbps FM
3 = 500 kbps MFM
4 = 300 kbps MFM
5 = 250 kbps MFM */
int sec_size;
int head;
int cyl;
int sects;
unsigned char *map;
unsigned char *data;
};
/* A loaded .IMD file */
struct imd {
char *comment;
struct track *tracks;
int ntracks;
};
void free_imd(struct imd *imd)
{
struct track *t;
while ((t = imd->tracks)) {
imd->tracks = t->next;
if (t->map)
free(t->map);
if (t->data)
free(t->data);
free(t);
}
if (imd->comment)
free(imd->comment);
free(imd);
}
struct imd *read_imd(char *name)
{
struct imd *imd;
struct track *track;
struct track *last;
char buf[1024];
int x;
int c;
FILE *f = fopen(name, "rb");
last = 0;
if (!f) {
fprintf(stderr, "Couldn't open %s\n", name);
return 0;
}
printf("Converting %s\n", name);
/* Read header */
x = 0;
while ((c = fgetc(f)), (c != -1 && c != 0x1A)) {
if (x < sizeof(buf) - 1)
buf[x++] = c;
}
buf[x] = 0;
if (!x) {
fprintf(stderr, "No header?\n");
fclose(f);
return 0;
}
imd = (struct imd *)malloc(sizeof(struct imd));
imd->comment = strdup(buf);
imd->tracks = 0;
imd->ntracks = 0;
/* Read tracks */
while ((c = fgetc(f)), (c != -1)) {
int x;
if (c < 0 || c > 5) {
fprintf(stderr,"Invalid mode byte?\n");
fclose(f);
free_imd(imd);
return 0;
}
// printf("track\n");
track = (struct track *)malloc(sizeof(struct track));
track->data = 0;
track->map = 0;
track->next = 0;
track->mode = c;
c = fgetc(f);
if (c < 0 || c > 80) {
fprintf(stderr,"Invalid cylinder number\n");
fclose(f);
free_imd(imd);
return 0;
}
track->cyl = c;
c = fgetc(f);
if (c < 0 || c > 1) {
fprintf(stderr,"Invalid head number\n");
fclose(f);
free_imd(imd);
return 0;
}
track->head = c;
c = fgetc(f);
if (c < 1) {
fprintf(stderr,"Invalid number of sectors\n");
fclose(f);
free_imd(imd);
return 0;
}
track->sects = c;
c = fgetc(f);
if (c < 0 || c > 6) {
fprintf(stderr,"Invalid sector size\n");
fclose(f);
free_imd(imd);
return 0;
}
track->sec_size = (128 << c);
track->map = (unsigned char *)malloc(track->sects);
if (1 != fread(track->map, track->sects, 1, f)) {
fprintf(stderr,"Couldn't read sector map\n");
fclose(f);
free_imd(imd);
return 0;
}
track->data = (unsigned char *)malloc(track->sects * track->sec_size);
for (x = 0; x != track->sects; ++x) {
c = fgetc(f);
if (c < 0 || c > 8) {
fprintf(stderr,"Invalid sector type\n");
fclose(f);
free_imd(imd);
return 0;
}
if (c & 1) {
if (1 != fread(track->data + x * track->sec_size, track->sec_size, 1, f)) {
fprintf(stderr,"Couldn't read sectors\n");
fclose(f);
free_imd(imd);
return 0;
}
} else if (c == 0) {
memset(track->data + x * track->sec_size, 0, track->sec_size);
} else {
c = fgetc(f);
if (c < 0) {
fprintf(stderr,"Couldn't compressed sector\n");
fclose(f);
free_imd(imd);
return 0;
}
memset(track->data + x * track->sec_size, c, track->sec_size);
}
}
if (last) {
last->next = track;
last = track;
} else {
last = imd->tracks = track;
}
++imd->ntracks;
}
fclose(f);
return imd;
}
char *modes[] =
{
"0 (500 kbps FM)",
"1 (300 kbps FM)",
"2 (250 kbps FM)",
"3 (500 kbps MFM)",
"4 (300 kbps MFM)",
"5 (250 kbps MFM)"
};
void dump_imd(struct imd *imd)
{
struct track *t;
printf("Comment = %s\n", imd->comment);
printf("%d tracks\n", imd->ntracks);
for (t = imd->tracks; t; t = t->next) {
int x;
printf("Cyl=%d Head=%d Sects=%d Sec_size=%d Mode=%s\n Map:",
t->cyl, t->head, t->sects, t->sec_size, modes[t->mode]);
for(x = 0; x != t->sects; ++x)
printf(" %d", t->map[x]);
printf("\n");
}
}
long imd_size(struct imd *imd)
{
long size = 0;
struct track *t;
for (t = imd->tracks; t; t = t->next) {
size += t->sec_size * t->sects;
}
return size;
}
int write_atr(struct imd *imd, char *dest_name, int logical, int sio)
{
FILE *f;
struct track *t;
unsigned char header[16];
unsigned char buf[8192];
long size;
int sec_size;
int count;
count = 0;
sec_size = imd->tracks->sec_size;
size = imd_size(imd);
printf("Sector size is %d\n", sec_size);
if (sec_size == 256) {
if (logical)
printf(" Using logical\n");
else if (sio)
printf(" Using sio\n");
else
printf(" Using physical\n");
}
if (size & (sec_size - 1)) {
fprintf(stderr,"Invalid .imd file size = %ld bytes\n", size);
fprintf(stderr,"It must be multiple of %d\n", sec_size);
return 1;
} else {
printf("Disk size is %ldK\n", size / 1024);
}
f = fopen(dest_name, "rb");
if (f) {
char buf[80];
fclose(f);
printf("%s already exists. Overwrite (y,n)?", dest_name);
fgets(buf,sizeof(buf)-1,stdin);
if (buf[0] != 'y' && buf[0] != 'Y') {
printf("Skipping...\n");
return 0;
}
}
f = fopen(dest_name, "wb");
if (!f) {
fprintf(stderr,"Couldn't open %s\n", dest_name);
return 1;
}
if (logical && sec_size == 256 && size >= 768)
size -= 384;
memset(header, 0, 16);
header[0] = 0x96;
header[1] = 0x02;
header[4] = sec_size;
header[5] = (sec_size >> 8);
header[2] = (size >> 4);
header[3] = (size >> 12);
header[6] = (size >> 20);
fwrite(header, 16, 1, f);
for (t = imd->tracks; t; t = t->next) {
int x;
for (x = 1; x != t->sects + 1; ++x) {
int y;
for (y = 0; y != t->sects; ++y)
if (t->map[y] == x)
break;
memcpy(buf, t->data + t->sec_size * y, t->sec_size);
for (y = 0; y != t->sec_size; ++y)
buf[y] ^= 0xFF;
if ((logical || sio) && sec_size == 256 && count < 3)
fwrite(buf, 128, 1, f);
else
fwrite(buf, t->sec_size, 1, f);
++count;
if (sio && count == 3) {
memset(buf, 0, 384);
fwrite(buf, 384, 1, f);
}
}
}
fclose(f);
return 0;
}
int main(int argc, char *argv[])
{
int dump = 0;
int logical = 1;
int sio = 0;
int x;
int err = 0;
int did = 0;
/* Parse args */
for (x = 1; argv[x]; ++x) {
if (argv[x][0] == '-') {
if (!strcmp(argv[x], "--dump"))
dump = 1;
else if (!strcmp(argv[x], "--logical")) {
logical = 1;
sio = 0;
} else if (!strcmp(argv[x], "--sio")) {
sio = 1;
logical = 0;
} else if (!strcmp(argv[x], "--physical")) {
sio = 0;
logical = 0;
} else
err = 1;
} else {
char *source_name = argv[x];
char dest_name[1024];
struct imd *imd;
char *p;
/* Create destination name based on source name */
strcpy(dest_name, source_name);
if ((p = strrchr(dest_name, '.')))
*p = 0;
strcat(dest_name, ".atr");
/* Read imd file */
if (!(imd = read_imd(source_name)))
return 1;
if (dump)
dump_imd(imd);
/* Write atr file */
if (write_atr(imd, dest_name, logical, sio))
return 1;
did = 1;
}
}
if (!did || err) {
fprintf(stderr,"Convert Dave Dunfield's .IMD (ImageDisk) file format to\n");
fprintf(stderr,"Nick Kennedy's .ATR (ATARI) disk image file format.\n");
fprintf(stderr,"\n");
fprintf(stderr," version 1.0\n");
fprintf(stderr," by: Joe Allen (2011)\n");
fprintf(stderr,"\n");
fprintf(stderr,"imd2atr [options] filename\n");
fprintf(stderr,"\n");
fprintf(stderr," --dump Show tracks\n");
fprintf(stderr,"\n");
fprintf(stderr,"The following options control how we deal with first three sectors of a 256-byte\n");
fprintf(stderr,"sector disk. Such disks store 256 bytes on the disk for these sectors, but the\n");
fprintf(stderr,"Atari makes use of only the first 128 bytes of them.\n");
fprintf(stderr,"\n");
fprintf(stderr," --physical Write all 256 bytes of these first three sectors.\n");
fprintf(stderr,"\n");
fprintf(stderr," --logical Write only 128 bytes each for first three sectors.\n");
fprintf(stderr,"\n");
fprintf(stderr," --sio Write only 128 bytes each for first three sectors,\n");
fprintf(stderr," then write 384 bytes of zeros (followed by rest of disk).\n");
fprintf(stderr,"\n");
fprintf(stderr,"Default format is '--logical', but emulators can deal with\n");
fprintf(stderr,"all three of them and '--physical' preserves all data actually read.\n");
return 1;
}
return 0;
}