-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdio.c
More file actions
358 lines (299 loc) · 9.65 KB
/
dio.c
File metadata and controls
358 lines (299 loc) · 9.65 KB
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
#if 0
/* ===========================================================================
D I O . C
Data I/O - Raw data. There are three formats of DIO data.
1) An initial RUBOUT (FF) character begins the data, followed by
sequential bytes of data starting from address 0. The data ends at EOF.
2) 16-bit mode, where the format is:
08,1C,2A,49,08,00,cccc,FF,data...,00,00,ssss
where cccc is the 2-byte count of data,
and ssss is the 2-byte checksum.
3) 32-bit mode, where the format is:
08,1C,3E,6B,08,00,0c,0c,0c,0c,0c,0c,0c,0c,FF,data...,00,00,ss,ss
where cccccccc is the 32 bit count of data split into the low 4 bit nibbles of 8 bytes, big endian (total of a 15 byte header)
and ss,ss is the 2-byte checksum, big endian
On all systems the DIO files begin with a CR (0x0D) followed by the 15-byte header,
etc.
Copyright 1993 Atari Games. All rights reserved.
Author: Jim Petrick
---------------------------------------------------------------------------
Revision history:
2-oct-1993 DMS Would produce incorrect file if PutRec was called
with recsize not a multiple of 512. Also it didn't
pad the file to match the length specified in the
header. Made it a little faster.
25-sep-2024 TG Fixed checksum handling on both input and output.
25-sep-2024 DMS Fixed the comments above.
---------------------------------------------------------------------------
Known bugs/features/limitations:
===========================================================================*/
#endif
#include "mixit.h"
#ifndef SEEK_SET
#define SEEK_SET 0 /* Set file pointer to "offset" */
#define SEEK_CUR 1 /* Set file pointer to current plus "offset" */
#define SEEK_END 2 /* Set file pointer to EOF plus "offset" */
#endif
extern uint8_t *out_buf;
extern int out_bufsize;
static uint8_t buffer[512];
static uint16_t bufIndex;
static uint32_t checkSum;
static uint32_t amtWritten, amtToWrite;
#if 0
static uint8_t zeroes[512];
extern GPF *gpf;
#endif
/*==========================================================================*/
int GetRec_dio(InRecord *rec)
{
uint8_t *bp;
static uint16_t file_cksum = 0;
static uint16_t checksum = 0;
static int32_t byteCount = 0;
size_t len, lclRecLen, dataSize;
int i;
uint8_t byte, arrow[5],
a16[] = { 0x1C, 0x2A, 0x49, 0x08, 0x00 },
a32[] = { 0x1C, 0x3E, 0x6B, 0x08, 0x00 },
count[9];
/* Skip header info until a rubout is reached */
if ( !rec->recPrivate )
{
checksum = 0;
if ( fread(&byte, 1, 1, rec->recFile) == 0 )
goto problem;
/* Short form of DIO format */
if ( byte == 0xFF )
byteCount = 0x7FFFFFFFL;
/* Check for CR, skip if found */
else if ( byte == 0x0D )
if ( fread(&byte, 1, 1, rec->recFile) == 0 )
goto problem;
/* Check for arrow start */
if ( byte == 0x08 )
{
/* Got Arrow, but which type */
if ( fread(&arrow, 1, 5, rec->recFile) == 0 )
goto problem;
if ( !strncmp((char *)arrow, (char *)a16, 5) )
len = 4;
else if ( !strncmp((char *)arrow, (char *)a32, 5) )
len = 8;
else
goto problem;
/* Read the count bytes */
if ( fread(&count, 1, len + 1, rec->recFile) == 0 )
goto problem;
/* Save the count of bytes to read in */
for ( dataSize = i = 0; i < 8 /*(int)len*/; ++i )
dataSize = (dataSize << 4) + (count[i] & 0xF);
/* Check for the RUBOUT byte */
if ( count[i] != 0xFF )
goto problem;
byteCount = dataSize;
rec->recPrivate = (void *)1;
}
}
/* End of data reached, check out the checksum */
if ( byteCount <= 0 )
{
if ( fread(count, 1, 4, rec->recFile) == 0 || count[0] || count[1] )
goto problem;
/* file_cksum now contains checksum */
file_cksum = (count[2] << 8) | count[3];
if ( file_cksum != checksum )
{
printf("Checksum Error - File Checksum = %X Calculated Checksum = %X\n", file_cksum, checksum);
rec->recLen = 0;
checksum = 0;
file_cksum = 0;
byteCount = 0;
return (rec->recType = REC_ERR);
}
rec->recLen = 0;
return (rec->recType = REC_EOF);
}
rec->recData = rec->recBuf;
lclRecLen = rec->recBufLen;
if ( lclRecLen > byteCount )
lclRecLen = byteCount; /* Don't read more 'data' than is in the file (i.e. don't include the 4 checksum bytes here) */
if ( (len = fread(rec->recBuf, 1, lclRecLen, rec->recFile)) > 0 )
{
rec->recSAddr += rec->recLen; /* Advance address from last read size */
rec->recLen = len; /* remember current read size for next time */
rec->recEAddr = rec->recSAddr+rec->recLen-1; /* new last address */
/* Compute the checksum */
for ( bp = rec->recBuf, i = 0; i < len; ++i )
checksum += *bp++;
if ( byteCount != 0x7FFFFFFFL )
byteCount -= len;
#if 0 /* Should never get here now */
if ( byteCount < 0 )
{
for ( i = 0; i < 4; ++i )
checksum -= *--bp;
file_cksum = (*(bp+2) << 8) | *(bp+3);
if ( file_cksum != checksum )
{
printf("Checksum Error - File Checksum = %X Calculated Checksum = %X\n", file_cksum, checksum);
rec->recLen = 0;
checksum = 0;
file_cksum = 0;
byteCount = 0;
return (rec->recType = REC_ERR);
}
}
#endif
return (rec->recType = REC_DATA);
}
problem:
byteCount = 0;
checksum = 0;
rec->recLen = 0;
return (rec->recType = feof(rec->recFile) ? REC_EOF : REC_ERR);
} /* end GetRec_dio */
/*==========================================================================*
* Outputs a single record in DIO format.
*==========================================================================*/
int PutRec_dio(FILE *file, uint8_t *data, int recsize, uint32_t recstart)
{
int i;
uint8_t *bp;
#if defined(VMS)
int amount;
amtWritten += recsize; /* record for posterity */
for (; recsize > 0; recsize -= amount, data += amount )
{
if ( bufIndex > 0 )
{
amount = min(sizeof(buffer) - bufIndex, recsize);
memcpy(buffer, data, amount);
bufIndex += amount;
if ( bufIndex == sizeof(buffer) )
{
/* Compute the checksum */
for ( bp = buffer, i = 0; i < recsize; ++i )
checkSum += *bp++;
if ( fwrite(buffer, sizeof(buffer), 1, file) != 1 )
return perr_return(0, "Error writing data");
bufIndex = 0;
}
continue;
}
if ( recsize < sizeof(buffer) )
{
amount = recsize;
memcpy(buffer, data, amount);
memset(buffer + amount, 0, sizeof(buffer) - amount);
}
else
{
amount = recsize - (recsize % sizeof(buffer));
/* Compute the checksum */
for ( bp = data, i = 0; i < amount; ++i )
checkSum += *bp++;
if ( fwrite(data, sizeof(buffer), amount / sizeof(buffer), file) != amount / sizeof(buffer) )
return perr_return(0, "Error writing data");
}
}
#else
if ( amtWritten != recstart )
{
if ( fseek(file, recstart + 0x10, SEEK_SET) < 0 )
return perr_return(0, "Error seeking to position in DIO file");
}
if ( amtWritten <= recstart )
{
amtWritten += recstart - amtWritten + recsize;
}
else
{
char emsg[132];
sprintf(emsg, "ERROR: Not allowed to backpatch a .DIO file (%08X-%08X)\n",
recstart, recstart + recsize - 1);
return perr_return(0, emsg);
}
for ( bp = data, i = 0; i < recsize; ++i )
checkSum += *bp++;
if ( fwrite(data, recsize, 1, file) != 1 )
return perr_return(0, "Error writing data");
#endif
return 1;
} /* end PutRec_dio */
/*==========================================================================
* Puts out any header info for the file.
*==========================================================================*/
int PutHead_dio(FILE *file, uint32_t addr, uint32_t hi)
{
amtToWrite = hi - addr + 1;
amtWritten = 0; /* say how much has been written */
bufIndex = 0; /* overflow buffer index */
checkSum = 0; /* Initialize the checksum */
memset(buffer, 0, 16);
if ( fwrite(buffer, 16, 1, file) != 1 )
return perr_return(0, "Error writing header");
return 1;
} /* end PutHead_dio */
/*==========================================================================
* Puts out any footer info for the file.
*==========================================================================*/
int PutFoot_dio(FILE *file)
{
uint8_t csbuf[4], *bp;
int sts;
#if defined(VMS)
int amount;
if ( bufIndex )
{
memset(buffer + bufIndex, 0, sizeof(buffer) - bufIndex);
amount = min(amtToWrite - amtWritten, sizeof(buffer));
if ( fwrite(buffer, amount, 1, file) != 1 )
return perr_return(0, "Error flushing before writing footer");
bufIndex = 0;
amtWritten += amount;
}
#endif
bp = csbuf;
*bp++ = 0;
*bp++ = 0;
*bp++ = (checkSum >> 8) & 0xFF;
*bp++ = checkSum & 0xFF;
if ( noisy )
fprintf(stdout, "DIO Checksum = (%04X)%04X\n",
(int)((checkSum >> 16) & 0xFFFF), (int)(checkSum & 0xFFFF));
if ( fwrite(csbuf, sizeof(csbuf), 1, file) != 1 )
return perr_return(0, "Error writing footer");
fflush(file);
if ( fseek(file, 0, SEEK_SET) < 0 )
return perr_return(0, "Error seeking to 0 to re-read header");
if ( (sts = fread(buffer, 16, 1, file)) != 1 )
{
char msg[132];
sprintf(msg, "Error %d re-reading header", sts);
return perr_return(0, msg);
}
bp = buffer;
*bp++ = 0x0D;
*bp++ = 0x08;
*bp++ = 0x1C;
*bp++ = 0x3E;
*bp++ = 0x6B;
*bp++ = 0x08;
*bp++ = 0x00;
*bp++ = (uint8_t)((amtWritten >> 28) & 15);
*bp++ = (uint8_t)((amtWritten >> 24) & 15);
*bp++ = (uint8_t)((amtWritten >> 20) & 15);
*bp++ = (uint8_t)((amtWritten >> 16) & 15);
*bp++ = (uint8_t)((amtWritten >> 12) & 15);
*bp++ = (uint8_t)((amtWritten >> 8) & 15);
*bp++ = (uint8_t)((amtWritten >> 4) & 15);
*bp++ = (uint8_t)((amtWritten >> 0) & 15);
*bp++ = 0xFF;
/* fprintf(errFile, "Wrote a %d (%08lX) byte dio file\n", amtWritten, amtWritten); */
if ( fseek(file, 0, SEEK_SET) < 0 )
return perr_return(0, "Error seeking to 0 to re-write header");
if ( fwrite(buffer, 16, 1, file) != 1 )
return perr_return(0, "Error re-writing dio header");
return 1;
} /* end PutFoot_dio */