Debian Codegraph @

terps/plus/atari8detect.c

raw symbols SCIP
1 //
2 // atari8detect.c
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 //
5 // Created by Petter Sjölund on 2022-01-30.
6 //
7
8 #include <ctype.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "common.h"
14 #include "companionfile.h"
15 #include "definitions.h"
16 #include "gameinfo.h"
17 #include "graphics.h"
18 #include "loaddatabase.h"
19
20 #include "atari8detect.h"
21
22 typedef struct imglist {
23 char *filename;
24 size_t offset;
25 } imglist;
26
27 static const struct imglist listFantastic[] = {
28 { "R001", 0x0297 },
29 { "R002", 0x0a8e },
30 { "R003", 0x10e48 },
31 { "R004", 0xdac },
32 { "R005", 0x690e },
33 { "R006", 0x70ba },
34 { "R007", 0x7687 },
35 { "R008", 0x1b8e },
36 { "R009", 0x9dd6 },
37 { "R010", 0x7d9e },
38 { "R011", 0x2333 },
39 { "R012", 0x853a },
40 { "R013", 0x339e },
41 { "R014", 0x8879 },
42 { "R015", 0x3c48 },
43 { "R016", 0x4533 },
44 { "R017", 0x48f9 },
45 { "R018", 0x9080 },
46 { "R019", 0x966b },
47 { "R020", 0x532c },
48 { "R021", 0x5fa5 },
49 { "R022", 0xaa0e },
50 { "B020", 0xc10e },
51 { "B002", 0xdd0e },
52 { "B003", 0xb800 },
53 { "B004", 0xeb87 },
54 { "B005", 0xbb97 },
55 { "B006", 0xc01e },
56 { "B007", 0xe1eb },
57 { "B008", 0xd180 },
58 { "B009", 0xc3c8 },
59 { "B010", 0xdbf2 },
60 { "B011", 0xc241 },
61 { "B012", 0xc31e },
62 { "B014", 0xf380 },
63 { "B015", 0xcc72 },
64 { "B016", 0xcfa5 },
65 { "B017", 0xd500 },
66 { "B018", 0xd5e4 },
67 { "B019", 0xe2f2 },
68 { "B013", 0xe625 },
69 { "B021", 0xbfac },
70 { "B022", 0xbf80 },
71 { "B023", 0xea41 },
72 { "S000", 0x10e80 },
73 { "S001", 0xfe80 },
74 { "S002", 0x1016b },
75 { "S003", 0x1032c },
76 { "S004", 0x104e4 },
77 { "S005", 0x10610 },
78 { "S006", 0x10717 },
79 { "S007", 0x1083a },
80 { "S008", 0x10e48 },
81 { "S009", 0x11d07 },
82 { "S010", 0x11d48 },
83 { "S011", 0x11ec8 },
84 { "S012", 0x12000 },
85 { "S013", 0x120f2 },
86 { "S014", 0x12307 },
87 { "S015", 0x12417 },
88 { "S016", 0x1252c },
89 { "S017", 0x125cf },
90 { "S018", 0x12617 },
91 { "S019", 0x1263a },
92 { "S020", 0x126c1 },
93 { NULL, 0 }
94 };
95
96 static const struct imglist listSpidey[] = {
97 { "R001", 0x0297 },
98 { "R002", 0x078e },
99 { "R003", 0x0b64 },
100 { "R005", 0x1741 },
101 { "R006", 0x1df9 },
102 { "R007", 0x2a17 },
103 { "R008", 0x34e4 },
104 { "R013", 0x3c97 },
105 { "R015", 0x463a },
106 { "R017", 0x4dd6 },
107 { "R018", 0x5633 },
108 { "R019", 0x5cac },
109 { "R022", 0x6a00 },
110 { "R024", 0x7464 },
111 { "R025", 0x81ac },
112 { "R034", 0x875d },
113 { "R037", 0x9280 },
114 { "R040", 0xa16b },
115 { "B021", 0xadf2 },
116 { "B015", 0xb1c8 },
117 { "B016", 0xb56b },
118 { "B023", 0xb88e },
119 { "B025", 0xba07 },
120 { "B027", 0xbac8 },
121 { "B028", 0xbc87 },
122 { "B031", 0xc248 },
123 { "B033", 0xc633 },
124 { "B034", 0xc9dd },
125 { "B035", 0xcdeb },
126 { "B038", 0xcf9e },
127 { "B051", 0xd91e },
128 { "B052", 0xdb97 },
129 { "B058", 0xe048 },
130 { "B059", 0xe172 },
131 { "B060", 0xe4c8 },
132 { "B062", 0xe5eb },
133 { "B071", 0xeb0e },
134 { "B049", 0xeb48 },
135 { "B044", 0xee2c },
136 { "B053", 0xf090 },
137 { "B056", 0xf49e },
138 { "B055", 0xf6f9 },
139 { "B050", 0xf8dd },
140 { "S001", 0xff64 },
141 { "S002", 0x10197 },
142 { "S003", 0x103ac },
143 { "S004", 0x105ac },
144 { "S005", 0x10833 },
145 { "S007", 0x11141 },
146 { "S008", 0x11541 },
147 { "S009", 0x11f90 },
148 { "S010", 0x12c79 },
149 { "S011", 0x13590 },
150 { "S012", 0x13e64 },
151 { "S013", 0x14348 },
152 { "S014", 0x149e4 },
153 { "S015", 0x14ddd },
154 { "S016", 0x14e97 },
155 { "S017", 0x14f3a },
156 { "S018", 0x15064 },
157 { "S020", 0x151cf },
158 { "S021", 0x15687 },
159 { "S022", 0x15b64 },
160 { "S023", 0x16179 },
161 // { "S024", 0x16682 },
162 { "S026", 0x16179 },
163 { NULL, 0 }
164 };
165
166 //{ "S000", 0, Disk image A at offset 6d97
167 // 0x0e94 },
168
169 static const struct imglist listBanzai[] = {
170 { "R001", 0x0297 },
171 { "R002", 0x083a },
172 { "R003", 0xb072 },
173 { "R004", 0x115d },
174 { "R005", 0x1c2c },
175 { "R006", 0x264f },
176 { "R008", 0xb7f2 },
177 { "R009", 0x2dc1 },
178 { "R011", 0xc2b3 },
179 { "R012", 0x351e },
180 { "R013", 0x9d56 },
181 { "R014", 0x3b6b },
182 { "R015", 0x1196b },
183 { "R016", 0xc817 },
184 { "R017", 0x41cf },
185 { "R018", 0x4ce4 },
186 { "R019", 0x11bc8 },
187 { "R020", 0x583a },
188 { "R021", 0x5eb3 },
189 { "R022", 0x6556 },
190 { "R023", 0x6b07 },
191 { "R024", 0x7080 },
192 { "R025", 0xa641 },
193 { "R026", 0x7941 },
194 { "R028", 0x8048 },
195 { "R029", 0x856b },
196 { "R035", 0x8b33 },
197 { "S000", 0x92ac },
198 { "B027", 0xca33 },
199 { "B004", 0xcf9e },
200 { "B005", 0xd017 },
201 { "B006", 0xd479 },
202 { "B019", 0xd81e },
203 { "B007", 0xd9cf },
204 { "B030", 0xda5d },
205 { "B025", 0xdbc1 },
206 { "B026", 0xdc25 },
207 { "B051", 0xdcc8 },
208 { "B023", 0xddc8 },
209 { "B055", 0xdeb3 },
210 { "B056", 0xe2b3 },
211 { "B070", 0xe7d6 },
212 { "B071", 0xe956 },
213 { "B073", 0xe9d6 },
214 { "B074", 0xea90 },
215 { "B037", 0xd479 },
216 { "B034", 0xeaf9 },
217 { "B035", 0xed5d },
218 { "S001", 0xf000 },
219 { "S002", 0xf807 },
220 { "S003", 0xf85d },
221 { "S004", 0xfacf },
222 { "S005", 0xfcd6 },
223 { "S006", 0xff56 },
224 { "S007", 0x10180 },
225 { "S008", 0x103cf },
226 { "S009", 0x1064f },
227 { "S010", 0x1088e },
228 { "S011", 0x10f17 },
229 { "S012", 0x11141 },
230 { "S013", 0x11433 },
231 { "S014", 0x11617 },
232 { "S015", 0x1176b },
233 { "S016", 0x11841 },
234 { "S017", 0x118e4 },
235
236 { NULL, 0 }
237 };
238
239 static int ExtractImagesFromAtariCompanionFile(uint8_t *data, size_t datasize, uint8_t *otherdisk, size_t othersize)
240 {
241 size_t size;
242
243 const struct imglist *list = listSpidey;
244 /* The number of images is 65 in all three Atari 8-bit Saga plus games!? */
245 /* (Not counting the title image on disk A in Spider-Man.) */
246 int count = 65;
247 if (CurrentGame == BANZAI)
248 list = listBanzai;
249 else if (CurrentGame == FANTASTIC4)
250 list = listFantastic;
251
252 Images = MemAlloc((count + 2) * sizeof(struct imgrec));
253
254 int outpic;
255
256 // Now loop round for each image
257 for (outpic = 0; outpic < count; outpic++) {
258 uint8_t *ptr = data + list[outpic].offset;
259
260 size = *ptr++;
261 size += *ptr * 256 + 4;
262
263 if (list[outpic].offset >= datasize) {
264 fprintf(stderr, "Image %d (%s) offset %zx out of bounds. Size: %zx\n", outpic, list[outpic].filename, list[outpic].offset, datasize);
265 continue;
266 }
267 if (list[outpic].offset + size >= datasize) {
268 fprintf(stderr, "Image %d (%s) offset %zx out of bounds. image size: %zx, disk size: %zx\n", outpic, list[outpic].filename, list[outpic].offset, size, datasize);
269 size = datasize - list[outpic].offset + 2;
270 }
271
272 Images[outpic].Filename = list[outpic].filename;
273 Images[outpic].Data = MemAlloc(size);
274 Images[outpic].Size = size;
275 memcpy(Images[outpic].Data, data + list[outpic].offset - 2, size);
276 if (list[outpic].offset < 0xb390 && list[outpic].offset + Images[outpic].Size > 0xb390) {
277 memcpy(Images[outpic].Data + 0xb390 - list[outpic].offset + 2, data + 0xb410, size - 0xb390 + list[outpic].offset - 2);
278 }
279 }
280
281 Images[outpic].Filename = NULL;
282
283 if (CurrentGame == SPIDERMAN && otherdisk) {
284 Images[outpic].Filename = "S000";
285 Images[outpic].Size = 0x0e96;
286 Images[outpic].Data = MemAlloc(Images[outpic].Size);
287 memcpy(Images[outpic].Data, otherdisk + 0x6d95, Images[outpic].Size);
288 Images[outpic + 1].Filename = NULL;
289 }
290 return 1;
291 }
292
293 static const uint8_t atrheader[6] = { 0x96, 0x02, 0x80, 0x16, 0x80, 0x00 };
294
295 int DetectAtari8(uint8_t **sf, size_t *extent)
296 {
297 uint8_t *main_disk = *sf;
298 size_t main_size = *extent;
299 int result = 0;
300 size_t data_start = 0xff8e;
301 // Header actually starts at offset 0xffc0 (0xff8e + 0x32).
302 // We add 50 bytes at the head to match the C64 files.
303
304 if (main_size > MAX_LENGTH || main_size < data_start)
305 return 0;
306
307 for (int i = 0; i < 6; i++)
308 if ((*sf)[i] != atrheader[i])
309 return 0;
310
311 size_t companionsize;
312 uint8_t *companionfile = GetCompanionFile(&companionsize);
313
314 ImageWidth = 280;
315 ImageHeight = 158;
316 mem = main_disk + data_start;
317 memlen = main_size - data_start;
318 result = LoadDatabaseBinary();
319 if (!result && companionfile != NULL && companionsize > data_start) {
320 debug_print("Could not find database in this file, trying the companion file\n");
321 mem = companionfile + data_start;
322 memlen = companionsize - data_start;
323 result = LoadDatabaseBinary();
324 if (result) {
325 debug_print("Found database in companion file. Switching files.\n");
326 uint8_t *temp = companionfile;
327 size_t tempsize = companionsize;
328 companionfile = main_disk;
329 companionsize = main_size;
330 main_disk = temp;
331 main_size = tempsize;
332 }
333 }
334
335 if (result) {
336 CurrentSys = SYS_ATARI8;
337 if (companionfile) {
338 ExtractImagesFromAtariCompanionFile(companionfile, companionsize, main_disk, main_size);
339 free(companionfile);
340 }
341 } else
342 debug_print("Failed loading database\n");
343
344 return result;
345 }