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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
|
/* sna.c: Routines for handling .sna snapshots
Copyright (c) 2001-2002 Philip Kendall
Copyright (c) 2016 Fredrick Meunier
This program 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 2 of the License, or
(at your option) any later version.
This program 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 program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <string.h>
#include "internals.h"
#define LIBSPECTRUM_SNA_HEADER_LENGTH 27
#define LIBSPECTRUM_SNA_128_HEADER_LENGTH 4
static int identify_machine( size_t buffer_length, libspectrum_snap *snap );
static int libspectrum_sna_read_header( const libspectrum_byte *buffer,
size_t buffer_length,
libspectrum_snap *snap );
static int libspectrum_sna_read_data( const libspectrum_byte *buffer,
size_t buffer_length,
libspectrum_snap *snap );
static int libspectrum_sna_read_128_header( const libspectrum_byte *buffer,
size_t buffer_length,
libspectrum_snap *snap );
static int libspectrum_sna_read_128_data( const libspectrum_byte *buffer,
size_t buffer_length,
libspectrum_snap *snap );
static void
write_header( libspectrum_buffer *buffer, libspectrum_snap *snap,
libspectrum_word sp );
static libspectrum_error
write_48k_sna( libspectrum_buffer *buffer, libspectrum_snap *snap,
libspectrum_word *new_sp );
static void
write_128k_sna( libspectrum_buffer *buffer, libspectrum_snap *snap );
static void
write_page( libspectrum_buffer *buffer, libspectrum_snap *snap, int page );
libspectrum_error
internal_sna_read( libspectrum_snap *snap,
const libspectrum_byte *buffer, size_t buffer_length )
{
int error;
error = identify_machine( buffer_length, snap );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
error = libspectrum_sna_read_header( buffer, buffer_length, snap );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
error = libspectrum_sna_read_data(
&buffer[LIBSPECTRUM_SNA_HEADER_LENGTH],
buffer_length - LIBSPECTRUM_SNA_HEADER_LENGTH, snap );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
return LIBSPECTRUM_ERROR_NONE;
}
static int
identify_machine( size_t buffer_length, libspectrum_snap *snap )
{
switch( buffer_length ) {
case 49179:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_48 );
break;
case 131103:
case 147487:
libspectrum_snap_set_machine( snap, LIBSPECTRUM_MACHINE_PENT );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_identify: unknown length" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
return LIBSPECTRUM_ERROR_NONE;
}
static int
libspectrum_sna_read_header( const libspectrum_byte *buffer,
size_t buffer_length, libspectrum_snap *snap )
{
int iff;
if( buffer_length < LIBSPECTRUM_SNA_HEADER_LENGTH ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_header: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
libspectrum_snap_set_a ( snap, buffer[22] );
libspectrum_snap_set_f ( snap, buffer[21] );
libspectrum_snap_set_bc ( snap, buffer[13] + buffer[14]*0x100 );
libspectrum_snap_set_de ( snap, buffer[11] + buffer[12]*0x100 );
libspectrum_snap_set_hl ( snap, buffer[ 9] + buffer[10]*0x100 );
libspectrum_snap_set_a_ ( snap, buffer[ 8] );
libspectrum_snap_set_f_ ( snap, buffer[ 7] );
libspectrum_snap_set_bc_( snap, buffer[ 5] + buffer[ 6]*0x100 );
libspectrum_snap_set_de_( snap, buffer[ 3] + buffer[ 4]*0x100 );
libspectrum_snap_set_hl_( snap, buffer[ 1] + buffer[ 2]*0x100 );
libspectrum_snap_set_ix ( snap, buffer[17] + buffer[18]*0x100 );
libspectrum_snap_set_iy ( snap, buffer[15] + buffer[16]*0x100 );
libspectrum_snap_set_i ( snap, buffer[ 0] );
libspectrum_snap_set_r ( snap, buffer[20] );
libspectrum_snap_set_pc ( snap, buffer[ 6] + buffer[ 7]*0x100 );
libspectrum_snap_set_sp ( snap, buffer[23] + buffer[24]*0x100 );
iff = ( buffer[19] & 0x04 ) ? 1 : 0;
libspectrum_snap_set_iff1( snap, iff );
libspectrum_snap_set_iff2( snap, iff );
libspectrum_snap_set_im( snap, buffer[25] & 0x03 );
libspectrum_snap_set_out_ula( snap, buffer[26] & 0x07 );
return LIBSPECTRUM_ERROR_NONE;
}
static int
libspectrum_sna_read_data( const libspectrum_byte *buffer,
size_t buffer_length, libspectrum_snap *snap )
{
int error, page, i;
libspectrum_word sp, offset;
if( buffer_length < 0xc000 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_data: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
switch( libspectrum_snap_machine( snap ) ) {
case LIBSPECTRUM_MACHINE_48:
sp = libspectrum_snap_sp( snap );
if( sp < 0x4000 || sp == 0xffff ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_data: SP invalid (0x%04x)", sp
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Rescue PC from the stack */
offset = sp - 0x4000;
libspectrum_snap_set_pc( snap, buffer[offset] + 0x100 * buffer[offset+1] );
/* Increase SP as PC has been unstacked */
libspectrum_snap_set_sp( snap, libspectrum_snap_sp( snap ) + 2 );
/* And split the pages up */
error = libspectrum_split_to_48k_pages( snap, buffer );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
break;
case LIBSPECTRUM_MACHINE_PENT:
for( i=0; i<8; i++ ) {
libspectrum_byte *ram = libspectrum_new( libspectrum_byte, 0x4000 );
libspectrum_snap_set_pages( snap, i, ram );
}
memcpy( libspectrum_snap_pages( snap, 5 ), &buffer[0x0000], 0x4000 );
memcpy( libspectrum_snap_pages( snap, 2 ), &buffer[0x4000], 0x4000 );
error = libspectrum_sna_read_128_header( buffer + 0xc000,
buffer_length - 0xc000, snap );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
page = libspectrum_snap_out_128_memoryport( snap ) & 0x07;
if( page == 5 || page == 2 ) {
if( memcmp( libspectrum_snap_pages( snap, page ),
&buffer[0x8000], 0x4000 ) ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_data: duplicated page not identical"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
} else {
memcpy( libspectrum_snap_pages( snap, page ), &buffer[0x8000], 0x4000 );
}
buffer += 0xc000 + LIBSPECTRUM_SNA_128_HEADER_LENGTH;
buffer_length -= 0xc000 + LIBSPECTRUM_SNA_128_HEADER_LENGTH;
error = libspectrum_sna_read_128_data( buffer, buffer_length, snap );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_sna_read_data: unknown machine" );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static int
libspectrum_sna_read_128_header( const libspectrum_byte *buffer,
size_t buffer_length, libspectrum_snap *snap )
{
if( buffer_length < 4 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_128_header: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
libspectrum_snap_set_pc( snap, buffer[0] + 0x100 * buffer[1] );
libspectrum_snap_set_out_128_memoryport( snap, buffer[2] );
return LIBSPECTRUM_ERROR_NONE;
}
static int
libspectrum_sna_read_128_data( const libspectrum_byte *buffer,
size_t buffer_length, libspectrum_snap *snap )
{
int i, page;
page = libspectrum_snap_out_128_memoryport( snap ) & 0x07;
for( i=0; i<=7; i++ ) {
if( i==2 || i==5 || i==page ) continue; /* Already got this page */
/* Check we've still got some data to read */
if( buffer_length < 0x4000 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_sna_read_128_data: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Copy the data across */
memcpy( libspectrum_snap_pages( snap, i ), buffer, 0x4000 );
/* And update what we're looking at here */
buffer += 0x4000; buffer_length -= 0x4000;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_sna_write( libspectrum_buffer *buffer, int *out_flags,
libspectrum_snap *snap, int in_flags GCC_UNUSED )
{
libspectrum_word snap_sp;
libspectrum_buffer *buffer_mem;
libspectrum_error error = LIBSPECTRUM_ERROR_NONE;
/* Minor info loss already due to things like tstate count, halted state,
etc which are not stored in .sna format */
*out_flags = LIBSPECTRUM_FLAG_SNAPSHOT_MINOR_INFO_LOSS;
/* We don't store +D info at all */
if( libspectrum_snap_plusd_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't store Beta info at all */
if( libspectrum_snap_beta_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't store Opus info at all */
if( libspectrum_snap_opus_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save IDE interface info at all */
if( libspectrum_snap_zxatasp_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
if( libspectrum_snap_zxcf_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
if( libspectrum_snap_simpleide_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
if( libspectrum_snap_divide_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Interface 2 ROM at all */
if( libspectrum_snap_interface2_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Timex Dock at all */
if( libspectrum_snap_dock_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save custom ROMs at all */
if( libspectrum_snap_custom_rom( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save AY interfaces at all */
if( libspectrum_snap_fuller_box_active( snap ) ||
libspectrum_snap_melodik_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Specdrum state at all */
if( libspectrum_snap_specdrum_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Spectranet state at all */
if( libspectrum_snap_spectranet_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the uSource state at all */
if( libspectrum_snap_usource_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the DISCiPLE state at all */
if( libspectrum_snap_disciple_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Didaktik80 state at all */
if( libspectrum_snap_didaktik80_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Covox state at all */
if( libspectrum_snap_covox_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the Multiface state at all */
if( libspectrum_snap_multiface_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the MMC state at all */
if( libspectrum_snap_divmmc_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
if( libspectrum_snap_zxmmc_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* We don't save the TTX2000S state at all */
if( libspectrum_snap_ttx2000s_active( snap ) )
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
buffer_mem = libspectrum_buffer_alloc();
switch( libspectrum_snap_machine( snap ) ) {
case LIBSPECTRUM_MACHINE_48_NTSC:
case LIBSPECTRUM_MACHINE_TC2048:
case LIBSPECTRUM_MACHINE_TC2068:
case LIBSPECTRUM_MACHINE_TS2068:
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* Fall through */
case LIBSPECTRUM_MACHINE_16:
case LIBSPECTRUM_MACHINE_48:
error = write_48k_sna( buffer_mem, snap, &snap_sp );
break;
case LIBSPECTRUM_MACHINE_128:
case LIBSPECTRUM_MACHINE_128E:
case LIBSPECTRUM_MACHINE_PENT512:
case LIBSPECTRUM_MACHINE_PENT1024:
case LIBSPECTRUM_MACHINE_PLUS2:
case LIBSPECTRUM_MACHINE_PLUS2A:
case LIBSPECTRUM_MACHINE_PLUS3:
case LIBSPECTRUM_MACHINE_PLUS3E:
case LIBSPECTRUM_MACHINE_SCORP:
case LIBSPECTRUM_MACHINE_SE:
*out_flags |= LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS;
/* Fall through */
case LIBSPECTRUM_MACHINE_PENT:
snap_sp = libspectrum_snap_sp ( snap );
write_128k_sna( buffer_mem, snap );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"Emulated machine type is set to 'unknown'!" );
libspectrum_buffer_free( buffer_mem );
return LIBSPECTRUM_ERROR_LOGIC;
}
if( error ) {
libspectrum_buffer_free( buffer_mem );
return error;
}
write_header( buffer, snap, snap_sp );
libspectrum_buffer_write_buffer( buffer, buffer_mem );
libspectrum_buffer_free( buffer_mem );
return LIBSPECTRUM_ERROR_NONE;
}
static void
write_header( libspectrum_buffer *buffer, libspectrum_snap *snap,
libspectrum_word sp )
{
libspectrum_buffer_write_byte( buffer, libspectrum_snap_i ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_hl_( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_de_( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_bc_( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_f_ ( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_a_ ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_hl ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_de ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_bc ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_iy ( snap ) );
libspectrum_buffer_write_word( buffer, libspectrum_snap_ix ( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_iff2( snap ) ? 0x04 : 0x00 );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_r ( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_f ( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_a ( snap ) );
libspectrum_buffer_write_word( buffer, sp );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_im( snap ) );
libspectrum_buffer_write_byte( buffer, libspectrum_snap_out_ula( snap ) & 0x07 );
}
static libspectrum_error
write_48k_sna( libspectrum_buffer *buffer, libspectrum_snap *snap,
libspectrum_word *new_sp )
{
libspectrum_byte *stack, *memory_base;
size_t offset;
libspectrum_word new_stack_address;
/* Must have somewhere in RAM to store PC */
if( libspectrum_snap_sp( snap ) < 0x4002 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_INVALID,
"SP is too low (0x%04x) to stack PC",
libspectrum_snap_sp( snap ) );
return LIBSPECTRUM_ERROR_INVALID;
}
offset = libspectrum_buffer_get_data_size( buffer );
write_page( buffer, snap, 5 );
write_page( buffer, snap, 2 );
write_page( buffer, snap, 0 );
memory_base = libspectrum_buffer_get_data( buffer ) + offset;
/* Place PC on the stack */
new_stack_address = libspectrum_snap_sp( snap ) - 2;
stack = &( memory_base[ new_stack_address - 0x4000 ] );
libspectrum_write_word( &stack, libspectrum_snap_pc( snap ) );
/* Store the new value of SP */
*new_sp = new_stack_address;
return LIBSPECTRUM_ERROR_NONE;
}
static void
write_128k_sna( libspectrum_buffer *buffer, libspectrum_snap *snap )
{
size_t i, page;
page = libspectrum_snap_out_128_memoryport( snap ) & 0x07;
write_page( buffer, snap, 5 );
write_page( buffer, snap, 2 );
write_page( buffer, snap, page );
libspectrum_buffer_write_word( buffer, libspectrum_snap_pc( snap ) );
libspectrum_buffer_write_byte( buffer,
libspectrum_snap_out_128_memoryport( snap ) );
libspectrum_buffer_write_byte( buffer, '\0' );
for( i = 0; i < 8; i++ ) {
/* Already written pages 5, 2 and whatever's paged in */
if( i == 5 || i == 2 || i == page ) continue;
write_page( buffer, snap, i );
}
}
static void
write_page( libspectrum_buffer *buffer, libspectrum_snap *snap, int page )
{
libspectrum_byte *ram;
ram = libspectrum_snap_pages( snap, page );
if( ram ) {
libspectrum_buffer_write( buffer, ram, 0x4000 );
} else {
libspectrum_buffer_set( buffer, 0xff, 0x4000 );
}
}
|