| 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 |
#include <stdarg.h> |
| 39 |
#include <stdint.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
|
| 45 |
#define SNAPSHOT_RAM_PAGES 16 |
| 46 |
|
| 47 |
#define libspectrum_new(type, count) \ |
| 48 |
((type *)libspectrum_malloc_n((count), sizeof(type))) |
| 49 |
|
| 50 |
#define libspectrum_renew(type, mem, count) \ |
| 51 |
((type *)libspectrum_realloc_n((void *)mem, (count), sizeof(type))) |
| 52 |
|
| 53 |
void *libspectrum_realloc_n(void *ptr, size_t nmemb, size_t size) |
| 54 |
{ |
| 55 |
if (nmemb > SIZE_MAX / size) |
| 56 |
abort(); |
| 57 |
|
| 58 |
return realloc(ptr, nmemb * size); |
| 59 |
} |
| 60 |
|
| 61 |
void *libspectrum_malloc(size_t size) |
| 62 |
{ |
| 63 |
void *ptr = malloc(size); |
| 64 |
|
| 65 |
|
| 66 |
if (size && !ptr) |
| 67 |
abort(); |
| 68 |
|
| 69 |
return ptr; |
| 70 |
} |
| 71 |
|
| 72 |
void *libspectrum_malloc_n(size_t nmemb, size_t size) |
| 73 |
{ |
| 74 |
if (nmemb > SIZE_MAX / size) |
| 75 |
abort(); |
| 76 |
|
| 77 |
return libspectrum_malloc(nmemb * size); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void libspectrum_make_room(uint8_t **dest, size_t requested, uint8_t **ptr, |
| 84 |
size_t *allocated) |
| 85 |
{ |
| 86 |
size_t current_length = 0; |
| 87 |
|
| 88 |
if (*allocated == 0) { |
| 89 |
|
| 90 |
(*allocated) = requested; |
| 91 |
*dest = libspectrum_new(uint8_t, requested); |
| 92 |
|
| 93 |
} else { |
| 94 |
current_length = *ptr - *dest; |
| 95 |
|
| 96 |
|
| 97 |
if (current_length + requested <= (*allocated)) |
| 98 |
return; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
(*allocated) = current_length + requested > 2 * (*allocated) |
| 103 |
? current_length + requested |
| 104 |
: 2 * (*allocated); |
| 105 |
|
| 106 |
*dest = libspectrum_renew(uint8_t, *dest, *allocated); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
*ptr = *dest + current_length; |
| 111 |
} |
| 112 |
|
| 113 |
struct libspectrum_snap { |
| 114 |
|
| 115 |
|
| 116 |
int machine; |
| 117 |
|
| 118 |
|
| 119 |
uint16_t pc; |
| 120 |
|
| 121 |
uint8_t *pages[SNAPSHOT_RAM_PAGES]; |
| 122 |
}; |
| 123 |
|
| 124 |
typedef struct libspectrum_snap libspectrum_snap; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
typedef enum libspectrum_error { |
| 130 |
|
| 131 |
LIBSPECTRUM_ERROR_NONE = 0, |
| 132 |
|
| 133 |
LIBSPECTRUM_ERROR_WARNING, |
| 134 |
LIBSPECTRUM_ERROR_MEMORY, |
| 135 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 136 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 137 |
LIBSPECTRUM_ERROR_SIGNATURE, |
| 138 |
LIBSPECTRUM_ERROR_SLT, |
| 139 |
LIBSPECTRUM_ERROR_INVALID, |
| 140 |
|
| 141 |
LIBSPECTRUM_ERROR_LOGIC = -1, |
| 142 |
|
| 143 |
} libspectrum_error; |
| 144 |
|
| 145 |
|
| 146 |
typedef enum libspectrum_machine { |
| 147 |
|
| 148 |
LIBSPECTRUM_MACHINE_48, |
| 149 |
LIBSPECTRUM_MACHINE_TC2048, |
| 150 |
LIBSPECTRUM_MACHINE_128, |
| 151 |
LIBSPECTRUM_MACHINE_PLUS2, |
| 152 |
LIBSPECTRUM_MACHINE_PENT, |
| 153 |
LIBSPECTRUM_MACHINE_PLUS2A, |
| 154 |
LIBSPECTRUM_MACHINE_PLUS3, |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
LIBSPECTRUM_MACHINE_UNKNOWN, |
| 159 |
|
| 160 |
LIBSPECTRUM_MACHINE_16, |
| 161 |
LIBSPECTRUM_MACHINE_TC2068, |
| 162 |
|
| 163 |
LIBSPECTRUM_MACHINE_SCORP, |
| 164 |
LIBSPECTRUM_MACHINE_PLUS3E, |
| 165 |
LIBSPECTRUM_MACHINE_SE, |
| 166 |
|
| 167 |
LIBSPECTRUM_MACHINE_TS2068, |
| 168 |
LIBSPECTRUM_MACHINE_PENT512, |
| 169 |
LIBSPECTRUM_MACHINE_PENT1024, |
| 170 |
LIBSPECTRUM_MACHINE_48_NTSC, |
| 171 |
|
| 172 |
LIBSPECTRUM_MACHINE_128E, |
| 173 |
|
| 174 |
} libspectrum_machine; |
| 175 |
|
| 176 |
typedef enum libspectrum_machine_capability { |
| 177 |
LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY = (1u << 0), |
| 178 |
LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_MEMORY = (1u << 1), |
| 179 |
LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY = (1u << 2), |
| 180 |
LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY = (1u << 3), |
| 181 |
LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY = (1u << 4), |
| 182 |
LIBSPECTRUM_MACHINE_CAPABILITY_PENT512_MEMORY = (1u << 5), |
| 183 |
LIBSPECTRUM_MACHINE_CAPABILITY_PENT1024_MEMORY = (1u << 6), |
| 184 |
} libspectrum_machine_capability; |
| 185 |
|
| 186 |
|
| 187 |
int libspectrum_machine_capabilities(libspectrum_machine type) |
| 188 |
{ |
| 189 |
int capabilities = 0; |
| 190 |
|
| 191 |
|
| 192 |
switch (type) { |
| 193 |
case LIBSPECTRUM_MACHINE_128: |
| 194 |
case LIBSPECTRUM_MACHINE_PLUS2: |
| 195 |
case LIBSPECTRUM_MACHINE_PLUS2A: |
| 196 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 197 |
case LIBSPECTRUM_MACHINE_PLUS3E: |
| 198 |
case LIBSPECTRUM_MACHINE_128E: |
| 199 |
case LIBSPECTRUM_MACHINE_PENT: |
| 200 |
case LIBSPECTRUM_MACHINE_PENT512: |
| 201 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 202 |
case LIBSPECTRUM_MACHINE_SCORP: |
| 203 |
|
| 204 |
|
| 205 |
case LIBSPECTRUM_MACHINE_SE: |
| 206 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY; |
| 207 |
break; |
| 208 |
default: |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
switch (type) { |
| 214 |
case LIBSPECTRUM_MACHINE_PLUS2A: |
| 215 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 216 |
case LIBSPECTRUM_MACHINE_PLUS3E: |
| 217 |
case LIBSPECTRUM_MACHINE_128E: |
| 218 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PLUS3_MEMORY; |
| 219 |
break; |
| 220 |
default: |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
switch (type) { |
| 226 |
case LIBSPECTRUM_MACHINE_TC2048: |
| 227 |
case LIBSPECTRUM_MACHINE_TC2068: |
| 228 |
case LIBSPECTRUM_MACHINE_TS2068: |
| 229 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_TIMEX_MEMORY; |
| 230 |
break; |
| 231 |
default: |
| 232 |
break; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
switch (type) { |
| 237 |
case LIBSPECTRUM_MACHINE_SCORP: |
| 238 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY; |
| 239 |
break; |
| 240 |
default: |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
switch (type) { |
| 246 |
case LIBSPECTRUM_MACHINE_SE: |
| 247 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_SE_MEMORY; |
| 248 |
break; |
| 249 |
default: |
| 250 |
break; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
switch (type) { |
| 255 |
case LIBSPECTRUM_MACHINE_PENT512: |
| 256 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 257 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PENT512_MEMORY; |
| 258 |
break; |
| 259 |
default: |
| 260 |
break; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
switch (type) { |
| 265 |
case LIBSPECTRUM_MACHINE_PENT1024: |
| 266 |
capabilities |= LIBSPECTRUM_MACHINE_CAPABILITY_PENT1024_MEMORY; |
| 267 |
break; |
| 268 |
default: |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
return capabilities; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
static const int LIBSPECTRUM_Z80_HEADER_LENGTH = 30; |
| 277 |
|
| 278 |
|
| 279 |
#define LIBSPECTRUM_Z80_V2_LENGTH 23 |
| 280 |
|
| 281 |
|
| 282 |
#define LIBSPECTRUM_Z80_V3_LENGTH 54 |
| 283 |
|
| 284 |
|
| 285 |
#define LIBSPECTRUM_Z80_V3X_LENGTH 55 |
| 286 |
|
| 287 |
|
| 288 |
enum { |
| 289 |
|
| 290 |
|
| 291 |
Z80_MACHINE_48_V2 = 0, |
| 292 |
Z80_MACHINE_48_IF1_V2 = 1, |
| 293 |
Z80_MACHINE_48_SAMRAM_V2 = 2, |
| 294 |
Z80_MACHINE_128_V2 = 3, |
| 295 |
Z80_MACHINE_128_IF1_V2 = 4, |
| 296 |
|
| 297 |
|
| 298 |
Z80_MACHINE_48 = 0, |
| 299 |
Z80_MACHINE_48_IF1 = 1, |
| 300 |
Z80_MACHINE_48_SAMRAM = 2, |
| 301 |
Z80_MACHINE_48_MGT = 3, |
| 302 |
Z80_MACHINE_128 = 4, |
| 303 |
Z80_MACHINE_128_IF1 = 5, |
| 304 |
Z80_MACHINE_128_MGT = 6, |
| 305 |
|
| 306 |
|
| 307 |
Z80_MACHINE_PLUS3 = 7, |
| 308 |
Z80_MACHINE_PLUS3_XZX_ERROR = 8, |
| 309 |
Z80_MACHINE_PENTAGON = 9, |
| 310 |
Z80_MACHINE_SCORPION = 10, |
| 311 |
Z80_MACHINE_PLUS2 = 12, |
| 312 |
Z80_MACHINE_PLUS2A = 13, |
| 313 |
Z80_MACHINE_TC2048 = 14, |
| 314 |
Z80_MACHINE_TC2068 = 15, |
| 315 |
Z80_MACHINE_TS2068 = 128, |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
Z80_MACHINE_FIRST_EXTENSION = Z80_MACHINE_PLUS3, |
| 320 |
}; |
| 321 |
|
| 322 |
static libspectrum_error read_header(const uint8_t *buffer, |
| 323 |
libspectrum_snap *snap, |
| 324 |
const uint8_t **data, int *version, |
| 325 |
int *compressed); |
| 326 |
static libspectrum_error get_machine_type(libspectrum_snap *snap, uint8_t type, |
| 327 |
int version); |
| 328 |
static libspectrum_error get_machine_type_v2(libspectrum_snap *snap, |
| 329 |
uint8_t type); |
| 330 |
static libspectrum_error get_machine_type_v3(libspectrum_snap *snap, |
| 331 |
uint8_t type); |
| 332 |
static libspectrum_error get_machine_type_extension(libspectrum_snap *snap, |
| 333 |
uint8_t type); |
| 334 |
|
| 335 |
static libspectrum_error read_blocks(const uint8_t *buffer, |
| 336 |
size_t buffer_length, |
| 337 |
libspectrum_snap *snap, int version, |
| 338 |
int compressed); |
| 339 |
static libspectrum_error read_block(const uint8_t *buffer, |
| 340 |
libspectrum_snap *snap, |
| 341 |
const uint8_t **next_block, |
| 342 |
const uint8_t *end, int version, |
| 343 |
int compressed); |
| 344 |
static libspectrum_error read_v1_block(const uint8_t *buffer, int is_compressed, |
| 345 |
uint8_t **uncompressed, |
| 346 |
const uint8_t **next_block, |
| 347 |
const uint8_t *end); |
| 348 |
static libspectrum_error read_v2_block(const uint8_t *buffer, uint8_t **block, |
| 349 |
size_t *length, int *page, |
| 350 |
const uint8_t **next_block, |
| 351 |
const uint8_t *end); |
| 352 |
|
| 353 |
static void uncompress_block(uint8_t **dest, size_t *dest_length, |
| 354 |
const uint8_t *src, size_t src_length); |
| 355 |
|
| 356 |
void libspectrum_snap_set_machine(libspectrum_snap *snap, int val) |
| 357 |
{ |
| 358 |
snap->machine = val; |
| 359 |
} |
| 360 |
|
| 361 |
uint8_t *libspectrum_snap_pages(libspectrum_snap *snap, int page) |
| 362 |
{ |
| 363 |
return snap->pages[page]; |
| 364 |
} |
| 365 |
|
| 366 |
void libspectrum_snap_set_pages(libspectrum_snap *snap, int page, |
| 367 |
uint8_t *buf) |
| 368 |
{ |
| 369 |
snap->pages[page] = buf; |
| 370 |
} |
| 371 |
|
| 372 |
void libspectrum_print_error(libspectrum_error error, const char *fmt, ...) |
| 373 |
{ |
| 374 |
va_list ap; |
| 375 |
char msg[2048]; |
| 376 |
|
| 377 |
int size = sizeof msg; |
| 378 |
|
| 379 |
va_start(ap, fmt); |
| 380 |
vsnprintf(msg, size, fmt, ap); |
| 381 |
va_end(ap); |
| 382 |
|
| 383 |
fprintf(stderr, "LibSpectrum "); |
| 384 |
switch (error) { |
| 385 |
case LIBSPECTRUM_ERROR_WARNING: |
| 386 |
fprintf(stderr, "warning"); |
| 387 |
break; |
| 388 |
case LIBSPECTRUM_ERROR_MEMORY: |
| 389 |
fprintf(stderr, "memory error"); |
| 390 |
break; |
| 391 |
case LIBSPECTRUM_ERROR_UNKNOWN: |
| 392 |
fprintf(stderr, "unknown error"); |
| 393 |
break; |
| 394 |
case LIBSPECTRUM_ERROR_CORRUPT: |
| 395 |
fprintf(stderr, "corruption error"); |
| 396 |
break; |
| 397 |
case LIBSPECTRUM_ERROR_SIGNATURE: |
| 398 |
fprintf(stderr, "signature error"); |
| 399 |
break; |
| 400 |
case LIBSPECTRUM_ERROR_SLT: |
| 401 |
fprintf(stderr, "SLT data in Z80 error"); |
| 402 |
break; |
| 403 |
case LIBSPECTRUM_ERROR_INVALID: |
| 404 |
fprintf(stderr, "invalid parameter error"); |
| 405 |
break; |
| 406 |
case LIBSPECTRUM_ERROR_LOGIC: |
| 407 |
fprintf(stderr, "logic error"); |
| 408 |
break; |
| 409 |
default: |
| 410 |
fprintf(stderr, "unhandled error"); |
| 411 |
break; |
| 412 |
} |
| 413 |
|
| 414 |
fprintf(stderr, ": %s\n", msg); |
| 415 |
} |
| 416 |
|
| 417 |
int libspectrum_snap_machine(libspectrum_snap *snap) { return snap->machine; } |
| 418 |
|
| 419 |
|
| 420 |
uint32_t libspectrum_read_dword(const uint8_t **buffer) |
| 421 |
{ |
| 422 |
uint32_t value; |
| 423 |
|
| 424 |
value = (*buffer)[0] + (*buffer)[1] * 0x100 + (*buffer)[2] * 0x10000 + (*buffer)[3] * 0x1000000; |
| 425 |
|
| 426 |
(*buffer) += 4; |
| 427 |
|
| 428 |
return value; |
| 429 |
} |
| 430 |
|
| 431 |
static libspectrum_error get_machine_type_extension(libspectrum_snap *snap, |
| 432 |
uint8_t type) |
| 433 |
{ |
| 434 |
switch (type) { |
| 435 |
|
| 436 |
case Z80_MACHINE_PLUS3: |
| 437 |
case Z80_MACHINE_PLUS3_XZX_ERROR: |
| 438 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS3); |
| 439 |
break; |
| 440 |
case Z80_MACHINE_PENTAGON: |
| 441 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PENT); |
| 442 |
break; |
| 443 |
case Z80_MACHINE_SCORPION: |
| 444 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_SCORP); |
| 445 |
break; |
| 446 |
case Z80_MACHINE_PLUS2: |
| 447 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2); |
| 448 |
break; |
| 449 |
case Z80_MACHINE_PLUS2A: |
| 450 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2A); |
| 451 |
break; |
| 452 |
case Z80_MACHINE_TC2048: |
| 453 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TC2048); |
| 454 |
break; |
| 455 |
case Z80_MACHINE_TC2068: |
| 456 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TC2068); |
| 457 |
break; |
| 458 |
case Z80_MACHINE_TS2068: |
| 459 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_TS2068); |
| 460 |
break; |
| 461 |
default: |
| 462 |
libspectrum_print_error( |
| 463 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 464 |
"%s:get_machine_type: unknown extension machine type %d", __FILE__, |
| 465 |
type); |
| 466 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 467 |
} |
| 468 |
|
| 469 |
return LIBSPECTRUM_ERROR_NONE; |
| 470 |
} |
| 471 |
|
| 472 |
libspectrum_error internal_z80_read(libspectrum_snap *snap, |
| 473 |
const uint8_t *buffer, |
| 474 |
size_t buffer_length); |
| 475 |
|
| 476 |
uint8_t *DecompressZ80(uint8_t *raw_data, size_t length) |
| 477 |
{ |
| 478 |
libspectrum_snap *snap = libspectrum_new(libspectrum_snap, 1); |
| 479 |
for (int i = 0; i < SNAPSHOT_RAM_PAGES; i++) |
| 480 |
libspectrum_snap_set_pages(snap, i, NULL); |
| 481 |
if (internal_z80_read(snap, raw_data, length) != LIBSPECTRUM_ERROR_NONE) { |
| 482 |
return NULL; |
| 483 |
} |
| 484 |
|
| 485 |
uint8_t *uncompressed = malloc(0xC000); |
| 486 |
if (uncompressed == NULL) |
| 487 |
return NULL; |
| 488 |
memcpy(uncompressed, snap->pages[5], 0x4000); |
| 489 |
memcpy(uncompressed + 0x4000, snap->pages[2], 0x4000); |
| 490 |
memcpy(uncompressed + 0x8000, snap->pages[0], 0x4000); |
| 491 |
|
| 492 |
for (int i = 0; i < SNAPSHOT_RAM_PAGES; i++) |
| 493 |
if (snap->pages[i] != NULL) |
| 494 |
free(snap->pages[i]); |
| 495 |
|
| 496 |
free(snap); |
| 497 |
return uncompressed; |
| 498 |
} |
| 499 |
|
| 500 |
libspectrum_error internal_z80_read(libspectrum_snap *snap, |
| 501 |
const uint8_t *buffer, |
| 502 |
size_t buffer_length) |
| 503 |
{ |
| 504 |
libspectrum_error error; |
| 505 |
const uint8_t *data; |
| 506 |
int version, compressed = 1; |
| 507 |
|
| 508 |
error = read_header(buffer, snap, &data, &version, &compressed); |
| 509 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 510 |
return error; |
| 511 |
|
| 512 |
error = read_blocks(data, buffer_length - (data - buffer), snap, version, |
| 513 |
compressed); |
| 514 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 515 |
return error; |
| 516 |
|
| 517 |
return LIBSPECTRUM_ERROR_NONE; |
| 518 |
} |
| 519 |
|
| 520 |
static libspectrum_error read_header(const uint8_t *buffer, |
| 521 |
libspectrum_snap *snap, |
| 522 |
const uint8_t **data, int *version, |
| 523 |
int *compressed) |
| 524 |
{ |
| 525 |
const uint8_t *header = buffer; |
| 526 |
libspectrum_error error; |
| 527 |
|
| 528 |
snap->pc = header[6] + header[7] * 0x100; |
| 529 |
|
| 530 |
if (snap->pc == 0) { |
| 531 |
|
| 532 |
size_t extra_length; |
| 533 |
const uint8_t *extra_header; |
| 534 |
|
| 535 |
extra_length = header[LIBSPECTRUM_Z80_HEADER_LENGTH] + header[LIBSPECTRUM_Z80_HEADER_LENGTH + 1] * 0x100; |
| 536 |
|
| 537 |
switch (extra_length) { |
| 538 |
case LIBSPECTRUM_Z80_V2_LENGTH: |
| 539 |
*version = 2; |
| 540 |
break; |
| 541 |
case LIBSPECTRUM_Z80_V3_LENGTH: |
| 542 |
case LIBSPECTRUM_Z80_V3X_LENGTH: |
| 543 |
*version = 3; |
| 544 |
break; |
| 545 |
default: |
| 546 |
libspectrum_print_error( |
| 547 |
LIBSPECTRUM_ERROR_UNKNOWN, |
| 548 |
"libspectrum_read_z80_header: unknown header length %d", |
| 549 |
(int)extra_length); |
| 550 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 551 |
} |
| 552 |
|
| 553 |
extra_header = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2; |
| 554 |
|
| 555 |
snap->pc = extra_header[0] + extra_header[1] * 0x100; |
| 556 |
|
| 557 |
error = get_machine_type(snap, extra_header[2], *version); |
| 558 |
if (error) |
| 559 |
return error; |
| 560 |
|
| 561 |
if (extra_header[5] & 0x80) { |
| 562 |
|
| 563 |
switch (libspectrum_snap_machine(snap)) { |
| 564 |
|
| 565 |
case LIBSPECTRUM_MACHINE_48: |
| 566 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_16); |
| 567 |
break; |
| 568 |
case LIBSPECTRUM_MACHINE_128: |
| 569 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2); |
| 570 |
break; |
| 571 |
case LIBSPECTRUM_MACHINE_PLUS3: |
| 572 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_PLUS2A); |
| 573 |
break; |
| 574 |
default: |
| 575 |
break; |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH + 2 + extra_length; |
| 580 |
|
| 581 |
} else { |
| 582 |
|
| 583 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 584 |
*version = 1; |
| 585 |
|
| 586 |
|
| 587 |
*compressed = (header[12] & 0x20) ? 1 : 0; |
| 588 |
|
| 589 |
(*data) = buffer + LIBSPECTRUM_Z80_HEADER_LENGTH; |
| 590 |
} |
| 591 |
|
| 592 |
return LIBSPECTRUM_ERROR_NONE; |
| 593 |
} |
| 594 |
|
| 595 |
static libspectrum_error get_machine_type(libspectrum_snap *snap, uint8_t type, |
| 596 |
int version) |
| 597 |
{ |
| 598 |
libspectrum_error error; |
| 599 |
|
| 600 |
if (type < Z80_MACHINE_FIRST_EXTENSION) { |
| 601 |
|
| 602 |
switch (version) { |
| 603 |
|
| 604 |
case 2: |
| 605 |
error = get_machine_type_v2(snap, type); |
| 606 |
if (error) |
| 607 |
return error; |
| 608 |
break; |
| 609 |
|
| 610 |
case 3: |
| 611 |
error = get_machine_type_v3(snap, type); |
| 612 |
if (error) |
| 613 |
return error; |
| 614 |
break; |
| 615 |
|
| 616 |
default: |
| 617 |
libspectrum_print_error(LIBSPECTRUM_ERROR_LOGIC, |
| 618 |
"%s:get_machine_type: unknown version %d", |
| 619 |
__FILE__, version); |
| 620 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 621 |
} |
| 622 |
|
| 623 |
} else { |
| 624 |
error = get_machine_type_extension(snap, type); |
| 625 |
if (error) |
| 626 |
return error; |
| 627 |
} |
| 628 |
|
| 629 |
return LIBSPECTRUM_ERROR_NONE; |
| 630 |
} |
| 631 |
|
| 632 |
static libspectrum_error get_machine_type_v2(libspectrum_snap *snap, |
| 633 |
uint8_t type) |
| 634 |
{ |
| 635 |
switch (type) { |
| 636 |
|
| 637 |
case Z80_MACHINE_48_V2: |
| 638 |
case Z80_MACHINE_48_SAMRAM_V2: |
| 639 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 640 |
break; |
| 641 |
case Z80_MACHINE_48_IF1_V2: |
| 642 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 643 |
break; |
| 644 |
case Z80_MACHINE_128_V2: |
| 645 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 646 |
break; |
| 647 |
case Z80_MACHINE_128_IF1_V2: |
| 648 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 649 |
break; |
| 650 |
default: |
| 651 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 652 |
"%s:get_machine_type: unknown v2 machine type %d", |
| 653 |
__FILE__, type); |
| 654 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 655 |
} |
| 656 |
|
| 657 |
return LIBSPECTRUM_ERROR_NONE; |
| 658 |
} |
| 659 |
|
| 660 |
static libspectrum_error get_machine_type_v3(libspectrum_snap *snap, |
| 661 |
uint8_t type) |
| 662 |
{ |
| 663 |
|
| 664 |
switch (type) { |
| 665 |
|
| 666 |
case Z80_MACHINE_48: |
| 667 |
case Z80_MACHINE_48_SAMRAM: |
| 668 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 669 |
break; |
| 670 |
case Z80_MACHINE_48_IF1: |
| 671 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 672 |
break; |
| 673 |
case Z80_MACHINE_48_MGT: |
| 674 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_48); |
| 675 |
break; |
| 676 |
case Z80_MACHINE_128: |
| 677 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 678 |
break; |
| 679 |
case Z80_MACHINE_128_MGT: |
| 680 |
libspectrum_snap_set_machine(snap, LIBSPECTRUM_MACHINE_128); |
| 681 |
break; |
| 682 |
default: |
| 683 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 684 |
"%s:get_machine_type: unknown v3 machine type %d", |
| 685 |
__FILE__, type); |
| 686 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 687 |
} |
| 688 |
|
| 689 |
return LIBSPECTRUM_ERROR_NONE; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
libspectrum_error libspectrum_split_to_48k_pages(libspectrum_snap *snap, |
| 695 |
const uint8_t *data) |
| 696 |
{ |
| 697 |
uint8_t *buffer[3]; |
| 698 |
size_t i; |
| 699 |
|
| 700 |
|
| 701 |
if (libspectrum_snap_pages(snap, 5) || libspectrum_snap_pages(snap, 2) || libspectrum_snap_pages(snap, 0)) { |
| 702 |
libspectrum_print_error( |
| 703 |
LIBSPECTRUM_ERROR_LOGIC, |
| 704 |
"libspectrum_split_to_48k_pages: RAM page already in use"); |
| 705 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 706 |
} |
| 707 |
|
| 708 |
for (i = 0; i < 3; i++) |
| 709 |
buffer[i] = libspectrum_new(uint8_t, 0x4000); |
| 710 |
|
| 711 |
libspectrum_snap_set_pages(snap, 5, buffer[0]); |
| 712 |
libspectrum_snap_set_pages(snap, 2, buffer[1]); |
| 713 |
libspectrum_snap_set_pages(snap, 0, buffer[2]); |
| 714 |
|
| 715 |
|
| 716 |
memcpy(libspectrum_snap_pages(snap, 5), &data[0x0000], 0x4000); |
| 717 |
memcpy(libspectrum_snap_pages(snap, 2), &data[0x4000], 0x4000); |
| 718 |
memcpy(libspectrum_snap_pages(snap, 0), &data[0x8000], 0x4000); |
| 719 |
|
| 720 |
return LIBSPECTRUM_ERROR_NONE; |
| 721 |
} |
| 722 |
|
| 723 |
static libspectrum_error read_blocks(const uint8_t *buffer, |
| 724 |
size_t buffer_length, |
| 725 |
libspectrum_snap *snap, int version, |
| 726 |
int compressed) |
| 727 |
{ |
| 728 |
const uint8_t *end, *next_block; |
| 729 |
|
| 730 |
end = buffer + buffer_length; |
| 731 |
next_block = buffer; |
| 732 |
|
| 733 |
while (next_block < end) { |
| 734 |
libspectrum_error error = read_block(next_block, snap, &next_block, end, version, compressed); |
| 735 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 736 |
return error; |
| 737 |
} |
| 738 |
|
| 739 |
return LIBSPECTRUM_ERROR_NONE; |
| 740 |
} |
| 741 |
|
| 742 |
static libspectrum_error read_block(const uint8_t *buffer, |
| 743 |
libspectrum_snap *snap, |
| 744 |
const uint8_t **next_block, |
| 745 |
const uint8_t *end, int version, |
| 746 |
int compressed) |
| 747 |
{ |
| 748 |
libspectrum_error error; |
| 749 |
uint8_t *uncompressed; |
| 750 |
|
| 751 |
int capabilities = libspectrum_machine_capabilities(libspectrum_snap_machine(snap)); |
| 752 |
|
| 753 |
if (version == 1) { |
| 754 |
|
| 755 |
error = read_v1_block(buffer, compressed, &uncompressed, next_block, end); |
| 756 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 757 |
return error; |
| 758 |
|
| 759 |
libspectrum_split_to_48k_pages(snap, uncompressed); |
| 760 |
|
| 761 |
free(uncompressed); |
| 762 |
|
| 763 |
} else { |
| 764 |
|
| 765 |
size_t length; |
| 766 |
int page; |
| 767 |
|
| 768 |
error = read_v2_block(buffer, &uncompressed, &length, &page, next_block, end); |
| 769 |
if (error != LIBSPECTRUM_ERROR_NONE) |
| 770 |
return error; |
| 771 |
|
| 772 |
if (page <= 0 || page > 18) { |
| 773 |
libspectrum_print_error(LIBSPECTRUM_ERROR_UNKNOWN, |
| 774 |
"read_block: unknown page %d", page); |
| 775 |
free(uncompressed); |
| 776 |
return LIBSPECTRUM_ERROR_UNKNOWN; |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
if (page < 3) { |
| 781 |
free(uncompressed); |
| 782 |
return LIBSPECTRUM_ERROR_NONE; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
if (page == 11 && !(capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_SCORP_MEMORY)) { |
| 788 |
free(uncompressed); |
| 789 |
return LIBSPECTRUM_ERROR_NONE; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if (!(capabilities & LIBSPECTRUM_MACHINE_CAPABILITY_128_MEMORY)) { |
| 795 |
|
| 796 |
switch (page) { |
| 797 |
|
| 798 |
case 3: |
| 799 |
free(uncompressed); |
| 800 |
return LIBSPECTRUM_ERROR_NONE; |
| 801 |
case 4: |
| 802 |
page = 5; |
| 803 |
break; |
| 804 |
case 5: |
| 805 |
page = 3; |
| 806 |
break; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
page -= 3; |
| 812 |
|
| 813 |
if (libspectrum_snap_pages(snap, page) == NULL) { |
| 814 |
libspectrum_snap_set_pages(snap, page, uncompressed); |
| 815 |
} else { |
| 816 |
free(uncompressed); |
| 817 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 818 |
"read_block: page %d duplicated", page); |
| 819 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
return LIBSPECTRUM_ERROR_NONE; |
| 824 |
} |
| 825 |
|
| 826 |
static libspectrum_error read_v1_block(const uint8_t *buffer, int is_compressed, |
| 827 |
uint8_t **uncompressed, |
| 828 |
const uint8_t **next_block, |
| 829 |
const uint8_t *end) |
| 830 |
{ |
| 831 |
if (is_compressed) { |
| 832 |
|
| 833 |
const uint8_t *ptr; |
| 834 |
int state; |
| 835 |
size_t uncompressed_length = 0; |
| 836 |
|
| 837 |
state = 0; |
| 838 |
ptr = buffer; |
| 839 |
|
| 840 |
while (state != 4) { |
| 841 |
|
| 842 |
if (ptr == end) { |
| 843 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 844 |
"read_v1_block: end marker not found"); |
| 845 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 846 |
} |
| 847 |
|
| 848 |
switch (state) { |
| 849 |
case 0: |
| 850 |
switch (*ptr++) { |
| 851 |
case 0x00: |
| 852 |
state = 1; |
| 853 |
break; |
| 854 |
default: |
| 855 |
state = 0; |
| 856 |
break; |
| 857 |
} |
| 858 |
break; |
| 859 |
case 1: |
| 860 |
switch (*ptr++) { |
| 861 |
case 0x00: |
| 862 |
state = 1; |
| 863 |
break; |
| 864 |
case 0xed: |
| 865 |
state = 2; |
| 866 |
break; |
| 867 |
default: |
| 868 |
state = 0; |
| 869 |
break; |
| 870 |
} |
| 871 |
break; |
| 872 |
case 2: |
| 873 |
switch (*ptr++) { |
| 874 |
case 0x00: |
| 875 |
state = 1; |
| 876 |
break; |
| 877 |
case 0xed: |
| 878 |
state = 3; |
| 879 |
break; |
| 880 |
default: |
| 881 |
state = 0; |
| 882 |
break; |
| 883 |
} |
| 884 |
break; |
| 885 |
case 3: |
| 886 |
switch (*ptr++) { |
| 887 |
case 0x00: |
| 888 |
state = 4; |
| 889 |
break; |
| 890 |
default: |
| 891 |
state = 0; |
| 892 |
break; |
| 893 |
} |
| 894 |
break; |
| 895 |
default: |
| 896 |
libspectrum_print_error(LIBSPECTRUM_ERROR_LOGIC, |
| 897 |
"read_v1_block: unknown state %d", state); |
| 898 |
return LIBSPECTRUM_ERROR_LOGIC; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
uncompress_block(uncompressed, &uncompressed_length, buffer, |
| 904 |
(ptr - buffer - 4)); |
| 905 |
|
| 906 |
|
| 907 |
if (uncompressed_length != 0xc000) { |
| 908 |
free(*uncompressed); |
| 909 |
libspectrum_print_error( |
| 910 |
LIBSPECTRUM_ERROR_CORRUPT, |
| 911 |
"read_v1_block: data does not uncompress to 48Kb"); |
| 912 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 913 |
} |
| 914 |
|
| 915 |
*next_block = ptr; |
| 916 |
|
| 917 |
} else { |
| 918 |
|
| 919 |
|
| 920 |
if (end - *next_block < 0xc000) { |
| 921 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 922 |
"read_v1_block: not enough data in buffer"); |
| 923 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 924 |
} |
| 925 |
|
| 926 |
*uncompressed = libspectrum_new(uint8_t, 0xc000); |
| 927 |
memcpy(*uncompressed, buffer, 0xc000); |
| 928 |
*next_block = buffer + 0xc000; |
| 929 |
} |
| 930 |
|
| 931 |
return LIBSPECTRUM_ERROR_NONE; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
static uint8_t slt_signature[] = "\0\0\0SLT"; |
| 936 |
static size_t slt_signature_length = 6; |
| 937 |
|
| 938 |
static libspectrum_error read_v2_block(const uint8_t *buffer, uint8_t **block, |
| 939 |
size_t *length, int *page, |
| 940 |
const uint8_t **next_block, |
| 941 |
const uint8_t *end) |
| 942 |
{ |
| 943 |
size_t length2; |
| 944 |
|
| 945 |
length2 = buffer[0] + buffer[1] * 0x100; |
| 946 |
(*page) = buffer[2]; |
| 947 |
|
| 948 |
if (length2 == 0 && *page == 0) { |
| 949 |
if (buffer + 8 < end && !memcmp(buffer, slt_signature, slt_signature_length)) { |
| 950 |
|
| 951 |
*next_block = buffer + 6; |
| 952 |
return LIBSPECTRUM_ERROR_SLT; |
| 953 |
} |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
if (length2 != 0xffff) { |
| 958 |
|
| 959 |
|
| 960 |
if (buffer + 3 + length2 > end) { |
| 961 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 962 |
"read_v2_block: not enough data in buffer"); |
| 963 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 964 |
} |
| 965 |
|
| 966 |
*length = 0; |
| 967 |
uncompress_block(block, length, buffer + 3, length2); |
| 968 |
|
| 969 |
*next_block = buffer + 3 + length2; |
| 970 |
|
| 971 |
} else { |
| 972 |
|
| 973 |
|
| 974 |
if (buffer + 3 + 0x4000 > end) { |
| 975 |
libspectrum_print_error(LIBSPECTRUM_ERROR_CORRUPT, |
| 976 |
"read_v2_block: not enough data in buffer"); |
| 977 |
return LIBSPECTRUM_ERROR_CORRUPT; |
| 978 |
} |
| 979 |
|
| 980 |
*block = libspectrum_new(uint8_t, 0x4000); |
| 981 |
memcpy(*block, buffer + 3, 0x4000); |
| 982 |
|
| 983 |
*length = 0x4000; |
| 984 |
*next_block = buffer + 3 + 0x4000; |
| 985 |
} |
| 986 |
|
| 987 |
return LIBSPECTRUM_ERROR_NONE; |
| 988 |
} |
| 989 |
|
| 990 |
static void uncompress_block(uint8_t **dest, size_t *dest_length, |
| 991 |
const uint8_t *src, size_t src_length) |
| 992 |
{ |
| 993 |
const uint8_t *in_ptr; |
| 994 |
uint8_t *out_ptr; |
| 995 |
|
| 996 |
|
| 997 |
if (*dest_length == 0) { |
| 998 |
*dest_length = src_length / 2; |
| 999 |
*dest = libspectrum_new(uint8_t, *dest_length); |
| 1000 |
} |
| 1001 |
|
| 1002 |
in_ptr = src; |
| 1003 |
out_ptr = *dest; |
| 1004 |
|
| 1005 |
while (in_ptr < src + src_length) { |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
if (in_ptr == src + src_length - 1) { |
| 1010 |
libspectrum_make_room(dest, 1, &out_ptr, dest_length); |
| 1011 |
*out_ptr++ = *in_ptr++; |
| 1012 |
continue; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
if (*in_ptr == 0xed && *(in_ptr + 1) == 0xed) { |
| 1018 |
|
| 1019 |
size_t run_length; |
| 1020 |
uint8_t repeated; |
| 1021 |
|
| 1022 |
in_ptr += 2; |
| 1023 |
run_length = *in_ptr++; |
| 1024 |
repeated = *in_ptr++; |
| 1025 |
|
| 1026 |
libspectrum_make_room(dest, run_length, &out_ptr, dest_length); |
| 1027 |
|
| 1028 |
while (run_length--) |
| 1029 |
*out_ptr++ = repeated; |
| 1030 |
|
| 1031 |
} else { |
| 1032 |
|
| 1033 |
libspectrum_make_room(dest, 1, &out_ptr, dest_length); |
| 1034 |
*out_ptr++ = *in_ptr++; |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
*dest_length = out_ptr - *dest; |
| 1039 |
} |