forked from jhallen/atari-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atr2imd.c
executable file
·394 lines (346 loc) · 10.1 KB
/
atr2imd.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
/* 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 loaded .ATR image */
struct atr {
long size; /* Size of data in bytes */
int type; /* 0 = 128 byte sectors, 1 = 256 byte sectors. */
unsigned char *data; /* Sector data */
int cyls; /* No. of tracks */
int sects; /* No. of sectors per track */
int sec_size; /* Bytes per sector */
int dd; /* Disk type to use:
0 = 90 K disk: 18 128-byte sectors / track
1 = 130 K disk: 26 128-byte sectors / track
2 = 180 K disk: 18 256-byte sectors / track
*/
int *map; /* Interleave map */
};
/* Free .atr image */
void free_atr(struct atr *atr)
{
if (atr->data)
free(atr->data);
free(atr);
}
/* Interleave map for 90K disks */
int sd_map[] =
{ 1, 3, 5, 7, 9, 11, 13, 15, 17, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
/* Interleave map for 130K disks */
int dd_map[] =
{ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26 };
/* Interleave map for 180K disks */
int hd_map[] =
{ 1, 3, 5, 7, 9, 11, 13, 15, 17, 2, 4, 6, 8, 10, 12, 14, 16, 18 };
/* Read .atr image */
struct atr *read_atr(char *name, int force_ed, int force_dd)
{
struct atr *atr;
FILE *f = fopen(name, "rb");
unsigned char header[16];
int sec_size; /* Sector size from header */
if (!f) {
fprintf(stderr, "Couldn't open %s\n", name);
return 0;
}
if (1 != fread(header, 16, 1, f)) {
fprintf(stderr, "Header missing from %s\n", name);
fclose(f);
return 0;
}
if (header[0] != 0x96 || header[1] != 0x02) {
fprintf(stderr, "Warning.. magic number is not 0x0296\n");
}
atr = (struct atr *)malloc(sizeof(struct atr));
atr->map = 0;
atr->data =0;
/* Get image size from header */
atr->size = ((long)header[2] + ((long)header[3] << 8) + ((long)header[6] << 16)) * 0x10;
/* Get sector size from header */
sec_size = header[4] + (header[5] << 8);
/* Check sector size */
if (sec_size == 0x80)
atr->type = 0;
else if (sec_size == 0x100)
atr->type = 1;
else {
fprintf(stderr, "Unknown sector size %d\n", sec_size);
free_atr(atr);
fclose(f);
return 0;
}
/* Get actual image size, don't trust size from header */
fseek(f, 0, SEEK_END);
atr->size = ftell(f) - 16;
fseek(f, 16, SEEK_SET);
/* Allocate space for image: give extra space to expand boot sectors */
atr->data = (unsigned char *)malloc(atr->size + 3 * 128);
if (!atr->data) {
fprintf(stderr, "Couldn't allocate space for image\n");
free_atr(atr);
fclose(f);
return 0;
}
/* Read image */
if (1 != fread(atr->data, atr->size, 1, f)) {
fprintf(stderr, "Error reading from file\n");
free_atr(atr);
fclose(f);
return 0;
}
/* Success! */
fclose(f);
/* Deal with boot sectors */
if (sec_size == 256) {
if ((atr->size >> 7) & 1) {
/* It has an odd number of 128 bytes chunks.. first
three sectors are 128 bytes. Expand image to make
physical sectors. */
memmove(atr->data + 768, atr->data + 384, atr->size - 384);
memcpy(atr->data + 512, atr->data + 256, 128);
memset(atr->data + 640, 0, 128);
memcpy(atr->data + 256, atr->data + 128, 128);
memset(atr->data + 384, 0, 128);
memset(atr->data + 128, 0, 128);
atr->size += 384;
} else {
int x;
int flg = 0;
for (x = 384; x != 768; ++x)
if (atr->data[x])
flg = 1;
if (!flg) {
/* Bytes 384 - 768 are all zeros. SIO2PC does this */
memcpy(atr->data + 512, atr->data + 256, 128);
memset(atr->data + 640, 0, 128);
memcpy(atr->data + 256, atr->data + 128, 128);
memset(atr->data + 384, 0, 128);
memset(atr->data + 128, 0, 128);
} else {
/* We already have physical sectors */
}
}
}
printf("Converting %s (%ld %dB sectors) ", name, atr->size/sec_size, sec_size);
/* Decide on best disk format to use */
if (sec_size == 128 && atr->size <= 128 * 18 * 40 && !force_ed && !force_dd) {
printf("=> 90K disk\n");
atr->cyls = 40;
atr->sec_size = 128;
atr->sects = 18;
atr->dd = 0;
atr->map = sd_map;
} else if (sec_size == 128 && atr->size <= 128 * 26 * 40 && !force_dd) {
printf("=> 130K disk\n");
atr->cyls = 40;
atr->sec_size = 128;
atr->sects = 26;
atr->dd = 1;
atr->map = dd_map;
} else if (sec_size == 256 && atr->size <= 256 * 18 * 40) {
printf("=> 180K disk\n");
atr->cyls = 40;
atr->sec_size = 256;
atr->sects = 18;
atr->dd = 2;
atr->map = hd_map;
} else {
printf("\n");
fprintf(stderr,"Unknown format\n");
free(atr->data);
free(atr);
return 0;
}
return atr;
}
/* True if all bytes are the same */
int is_same(unsigned char *data, int len)
{
int c = data[0];
int x;
for (x = 1; x != len; ++x)
if (data[x] != c)
return 0;
return 1;
}
/* Convert IMD file */
int write_imd(struct atr *atr, char *dest_name, char *comment)
{
FILE *f;
time_t t = time(NULL);
struct tm *tm = localtime(&t);
int cyl;
int sect;
int x;
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 for writing\n", dest_name);
return 1;
}
/* Write timestamp */
fprintf(f, "ATR2IMD 1.0: %2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d\n",
tm->tm_mday,tm->tm_mon + 1,tm->tm_year + 1900,tm->tm_hour,
tm->tm_min,tm->tm_sec);
/* Write comment */
fprintf(f, "%s\n\x1a", comment);
/* Write tracks */
for (cyl = 0; cyl != atr->cyls; ++cyl) {
if (atr->dd)
fputc(5, f); /* 250 Kbps MFM */
else
fputc(2, f); /* 250 Kbps FM */
fputc(cyl, f); /* Cylinder */
fputc(0, f); /* Head */
fputc(atr->sects, f); /* Number of sectors */
if (atr->sec_size == 256)
fputc(1, f); /* Bytes per sector 0 = 128 */
else
fputc(0, f); /* Bytes per sector 1 = 256 */
/* Sector map */
for (x = 0; x != atr->sects; ++x) {
fputc(atr->map[x], f);
}
/* Cylinder map (empty) */
/* Head map (empty) */
/* Sectors */
for (x = 0; x != atr->sects; ++x) {
int ofst;
int dbl = 0;
sect = atr->map[x] - 1;
ofst = atr->sec_size * (cyl * atr->sects + sect);
if (ofst >= atr->size) {
fputc(2, f);
fputc(~0, f);
} else if (is_same(atr->data + ofst, atr->sec_size)) {
fputc(2, f);
fputc(~atr->data[ofst], f);
} else {
int y;
fputc(1, f);
if (dbl) {
for (y = 0; y != 128; ++y) {
fputc(~atr->data[ofst + y], f);
}
for (y = 0; y != 128; ++y) {
fputc(~0, f);
}
} else
for (y = 0; y != atr->sec_size; ++y) {
fputc(~atr->data[ofst + y], f);
}
}
}
}
fclose(f);
return 0;
}
int main(int argc, char *argv[])
{
int x;
int err = 0;
int did = 0;
char *comment = 0;
int force_ed = 0;
int force_dd = 0;
/* Parse args */
for (x = 1; argv[x]; ++x) {
if (argv[x][0] == '-') {
/* Some kind of option */
if (!strcmp(argv[x], "--comment") && argv[x + 1])
comment = argv[++x];
else if (!strcmp(argv[x], "--sd")) {
force_ed = 0;
force_dd = 0;
} else if (!strcmp(argv[x], "--ed")) {
force_ed = 1;
force_dd = 0;
} else if (!strcmp(argv[x], "--dd")) {
force_ed = 0;
force_dd = 1;
} else {
err = 1;
break;
}
} else {
char *p;
struct atr *atr;
char dest_name[1024];
char cmnt[1024];
char *source_name = argv[x];
/* Create destination name based on source name */
strcpy(dest_name, source_name);
if ((p = strrchr(dest_name, '.')))
*p = 0;
strcat(dest_name, ".imd");
/* Create comment if none provided */
if (!comment) {
sprintf(cmnt, "Converted from file %s", source_name);
comment = cmnt;
}
/* Read .atr file */
if (!(atr = read_atr(source_name, force_ed, force_dd)))
return 1;
/* Write .imd file */
if (write_imd(atr, dest_name, comment))
return 1;
/* Free .atr image */
free_atr(atr);
/* Reset options */
comment = 0;
did = 1;
}
}
if (!did || err) {
fprintf(stderr,"Convert Nick Kennedy's .ATR (ATARI) disk image file format to\n");
fprintf(stderr,"Dave Dunfield's .IMD (ImageDisk) file format.\n");
fprintf(stderr,"\n");
fprintf(stderr," version 1.0\n");
fprintf(stderr," by: Joe Allen (2011)\n");
fprintf(stderr,"\n");
fprintf(stderr,"atr2imd [options] filename\n");
fprintf(stderr,"\n");
fprintf(stderr," --comment <comment> Comment to put in .IMD file (otherwise file name\n");
fprintf(stderr," is used as the comment)\n");
fprintf(stderr,"\n");
fprintf(stderr,"atr2imd creates the smallest disk image needed to fit the .atr file.\n");
fprintf(stderr,"These options can be used to create a larger than necessary disk image:\n");
fprintf(stderr,"\n");
fprintf(stderr," --sd Force single density (90K disk, 128 byte FM sectors)\n");
fprintf(stderr," --ed Force medium density (130K disk, 128 byte MFM sectors)\n");
fprintf(stderr," --dd Force double density (180K disk, 256 byte MFM sectors)\n");
return 1;
}
return 0;
}