-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcpe.c
More file actions
311 lines (275 loc) · 8.15 KB
/
Copy pathcpe.c
File metadata and controls
311 lines (275 loc) · 8.15 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
#if 0
/*===========================================================================
C P E . C
CPE - variable length binary stream data as used on the Sony
playstation. Each record is identified by a leading byte in the
range 0 to 8 indicating the record type. The types are defined
as:
0 = EOF (End of File)
1 = n bytes of data to be loaded at m address.
the address (m) is 4 bytes following immediately
after the type byte, lsb first (little endian);
the count (n) is 4 bytes following immediately
after the address, lsb first (little endian);
the data follows immediately after the count.
2 = xfer address. 4 bytes, lsb first, follows immediately
after the type byte.
3 = set register n to int32_t value v. Register number is 2 bytes,
lsb first, following immediately after the type byte;
value (v) is 4 bytes, lsb first, following immediately
after the register number.
4 = set register n to word value v. Register number is 2 bytes,
lsb first, following immediately after the type byte;
value (v) is 2 bytes, lsb first, following immediately
after the register number.
5 = set register n to byte value v. Register number is 2 bytes,
lsb first, following immediately after the type byte;
value (v) is 1 byte following immediately after the
register number.
6 = set register n to 3-byte value v. Register number is 2 bytes,
lsb first, following immediately after the type byte;
value (v) is 3 bytes, lsb first, following immediately
after the register number.
7 = workspace address is 4 bytes, lsb first, following immediately
after the type byte.
8 = unit number is 1 byte following immediately after the type
byte.
Copyright 1996 Atari Games. All rights reserved.
Author: David Shepperd
---------------------------------------------------------------------------
Revision history:
---------------------------------------------------------------------------
Known bugs/features/limitations:
===========================================================================*/
#endif
#include "mixit.h"
static int readl(uint32_t *ans, FILE *fp)
{
unsigned char buf[4];
int sts;
sts = fread(buf, 1, sizeof(buf), fp);
if ( sts != sizeof(buf) )
return sts;
if ( ans )
*ans = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
return 0;
}
static int readw(uint32_t *ans, FILE *fp)
{
unsigned char buf[2];
int sts;
sts = fread(buf, 1, sizeof(buf), fp);
if ( sts != sizeof(buf) )
return sts;
if ( ans )
*ans = (buf[1] << 8) | buf[0];
return 0;
}
static int read3(uint32_t *ans, FILE *fp)
{
unsigned char buf[3];
int sts;
sts = fread(buf, 1, sizeof(buf), fp);
if ( sts != sizeof(buf) )
return sts;
if ( ans )
*ans = (buf[2] << 16) | (buf[1] << 8) | buf[0];
return 0;
}
static int writel(uint32_t val, FILE *fp)
{
unsigned char buf[4];
int sts;
buf[0] = val;
buf[1] = val >> 8;
buf[2] = val >> 16;
buf[3] = val >> 24;
sts = fwrite(buf, 1, sizeof(buf), fp);
if ( sts != sizeof(buf) )
return sts;
return 0;
}
static int writew(uint32_t val, FILE *fp)
{
unsigned char buf[2];
size_t sts;
buf[0] = val;
buf[1] = val >> 8;
sts = fwrite(buf, 1, sizeof(buf), fp);
if ( sts != sizeof(buf) )
return sts;
return 0;
}
/*==========================================================================*/
static int GetHead_cpe(FILE *fp)
{
uint8_t head[4];
int sts;
sts = fread(head, 1, sizeof(head), fp);
if ( sts == sizeof(head) )
{
if ( head[0] == 'C' && head[1] == 'P' && head[2] == 'E' && head[3] == 1 )
return 0;
}
return 1;
}
/*==========================================================================*/
int GetRec_cpe(InRecord *record)
{
int cnt;
GPF *gpf = record->gpfPtr;
record->recData = record->recBuf;
if ( !record->recPrivate )
{
if ( GetHead_cpe(record->recFile) )
{
fprintf(errFile, "Not a CPE file format\n");
return record->recType = REC_ERR;
}
record->recPrivate = (void *)1;
gpf->recordOffset = 4;
}
if ( (cnt = getc(record->recFile)) == EOF )
{
read_error:
return record->recType = feof(record->recFile) ? REC_EOF : REC_ERR;
}
switch (cnt)
{
case 0:
gpf->recordOffset += 1;
return record->recType = REC_EOF;
case 1:
{
uint32_t addr=0;
uint32_t len=0;
int sts;
if ( readl(&addr, record->recFile) )
goto read_error;
if ( readl(&len, record->recFile) )
goto read_error;
if ( (size_t)len > record->recBufLen )
{
record->recData = record->recBuf = realloc(record->recBuf, (size_t)len);
if ( !record->recBuf )
{
fprintf(errFile, "Record too int32_t: %d > %" FMT_SZ "d. Out of memory.\n", len, record->recBufLen);
record->recBufLen = 0;
return record->recType = REC_ERR;
}
record->recBufLen = len;
}
sts = fread(record->recBuf, 1, len, record->recFile);
if ( sts != len )
goto read_error;
record->recSAddr = addr;
record->recEAddr = addr+len-1;
record->recLen = len;
gpf->recordOffset += 4*2 + len;
return record->recType = REC_DATA;
}
case 2:
if ( readl(&record->recSAddr, record->recFile) )
goto read_error;
record->recLen = 0;
gpf->recordOffset += 4;
return record->recType = REC_XFER;
case 3:
{
uint32_t reg=0;
if ( readw(®, record->recFile) )
goto read_error;
if ( readl(&record->recSAddr, record->recFile) )
goto read_error;
gpf->recordOffset += 2+4;
if ( reg == 144 )
{
record->recLen = 0;
return record->recType = REC_XFER;
}
break;
}
case 4:
if ( readw(0, record->recFile) )
goto read_error;
if ( readw(0, record->recFile) )
goto read_error;
gpf->recordOffset += 2+2;
break;
case 5:
if ( readw(0, record->recFile) )
goto read_error;
if ( fgetc(record->recFile) < 0 )
goto read_error;
gpf->recordOffset += 2+1;
break;
case 6:
if ( readw(0, record->recFile) )
goto read_error;
if ( read3(0, record->recFile) )
goto read_error;
gpf->recordOffset += 2+3;
break;
case 7:
if ( readl(0, record->recFile) )
goto read_error;
gpf->recordOffset += 4;
break;
case 8:
if ( fgetc(record->recFile) < 0 )
goto read_error;
gpf->recordOffset += 1;
break;
default:
err_exit("cpe.c: Unknown record type: %02X(%d). Infile offset: %ld (0x%lX)", cnt & 0xFF, cnt & 0xFF, gpf->recordOffset, gpf->recordOffset);
}
return record->recType = REC_UNKNOWN;
} /* end GetRec_cpe */
/*==========================================================================*
* Outputs a single record in CPE format.
*==========================================================================*/
int PutRec_cpe(FILE *file, uint8_t *data, int recsize, uint32_t recstart)
{
uint8_t maddr[4];
maddr[0] = recstart;
maddr[1] = recstart >> 8;
maddr[2] = recstart >> 16;
maddr[3] = recstart >> 24;
fputc(1, file); /* type 1 record */
fwrite(maddr, 1, sizeof(maddr), file); /* followed with address */
maddr[0] = recsize;
maddr[1] = recsize >> 8;
maddr[2] = recsize >> 16;
maddr[3] = recsize >> 24;
fwrite(maddr, 1, sizeof(maddr), file); /* followed with count */
fwrite(data, 1, recsize, file); /* followed by the data */
/* Now the count field */
return 1;
} /* end PutRec_cpe */
/*==========================================================================*
* Outputs all header information required by a CPE format file.
*==========================================================================*/
int PutHead_cpe(FILE *file, uint32_t addr, uint32_t hi)
{
fwrite("CPE\001", 1, 4, file);
fwrite("\010\000", 1, 2, file); /* set unit number to 0 */
return 0;
} /* end PutHead_cpe */
/*==========================================================================*
* Outputs transfer address
*==========================================================================*/
int PutXfer_cpe(FILE *file, uint32_t addr)
{
fputc(3, file); /* set register */
writew(144, file); /* 144 to */
writel(addr, file); /* xfer addr */
return 0;
} /* end PutXfer_cpe */
/*==========================================================================*
* Outputs all footer information required by a CPE format file.
*==========================================================================*/
int PutFoot_cpe(FILE *file)
{
fputc(0, file);
return 0;
} /* end PutFoot_cpe */