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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
|
/* rzx.c: .rzx files
Copyright (c) 2002-2003 Philip Kendall
$Id: rzx.c 4159 2010-09-13 11:51:13Z fredm $
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 <math.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#endif /* #ifdef WIN32 */
#include "debugger/debugger.h"
#include "event.h"
#include "fuse.h"
#include "machine.h"
#include "rzx.h"
#include "settings.h"
#include "snapshot.h"
#include "timer/timer.h"
#include "ui/ui.h"
#include "ula.h"
#include "utils.h"
#include "z80/z80.h"
#include "z80/z80_macros.h"
#define RZX_SENTINEL_TIME ( ULA_CONTENTION_SIZE - 1000 )
#define RZX_SENTINEL_TIME_REDUCE 8000
/* The offset used to get the count of instructions from the R register;
(instruction count) = R + rzx_instructions_offset */
int rzx_instructions_offset;
/* The number of bytes read via IN during the current frame */
size_t rzx_in_count;
/* The number of frames we've recorded in this RZX file */
static size_t autosave_frame_count;
/* And the values of those bytes */
libspectrum_byte *rzx_in_bytes;
/* How big is the above array? */
size_t rzx_in_allocated;
/* Are we currently recording a .rzx file? */
int rzx_recording;
/* Is the .rzx file being recorded in competition mode? */
int rzx_competition_mode;
/* The filename we'll save this recording into */
static char *rzx_filename;
/* Are we currently playing back a .rzx file? */
int rzx_playback;
/* The number of instructions in the current .rzx playback frame */
size_t rzx_instruction_count;
/* The current RZX data */
libspectrum_rzx *rzx;
/* Fuse's DSA key */
libspectrum_rzx_dsa_key rzx_key = {
"A9E3BD74E136A9ABD41E614383BB1B01EB24B2CD7B920ED6A62F786A879AC8B00F2FF318BF96F81654214B1A064889FF6D8078858ED00CF61D2047B2AAB7888949F35D166A2BBAAE23A331BD4728A736E76901D74B195B68C4A2BBFB9F005E3655BDE8256C279A626E00C7087A2D575F78D7DC5CA6E392A535FFE47A816BA503", /* p */
"FE8D540EED2CAE1983690E2886259F8956FB5A19", /* q */
"9680ABFFB98EF2021945ADDF86C21D6EE3F7C8777FB0A0220AB59E9DFA3A3338611B32CFD1F22F8F26547858754ED93BFBDD87DC13C09F42B42A36B2024467D98EB754DEB2847FCA7FC60C81A99CF95133847EA38AD9D037AFE9DD189E9F0EE47624848CEE840D7E3724A39681E71B97ECF777383DC52A48C0A2C93BADA93F4C", /* g */
"46605F0514D56BC0B4207A350367A5038DBDD4DD62B7C997D26D0ADC5BE42D01F852C199E34553BCBCE5955FF80E3B402B55316606D7E39C0F500AE5EE41A7B7A4DCE78EC19072C21FCC7BA48DFDC830C17B72BCAA2B2D70D9DFC0AAD9B7E73F7AEB6241E54D55C33E41AB749CAAFBE7AB00F2D74C500E5F5DD63BD299C65778", /* y */
"948744AA7A1D1BE9EE65150B0A95A678B4181F0E" /* x */
};
/* By how much is the speed allowed to deviate from 100% whilst recording
a competition mode RZX file */
static const float SPEED_TOLERANCE = 5;
/* How often will we create an autosave file */
static const size_t AUTOSAVE_INTERVAL = 5 * 50;
/* Debugger events */
static const char *event_type_string = "rzx";
static const char *end_event_detail_string = "end";
int end_event;
static int start_playback( libspectrum_rzx *rzx );
static int recording_frame( void );
static int playback_frame( void );
static int counter_reset( void );
static void rzx_sentinel( libspectrum_dword ts, int type,
void *user_data );
static int sentinel_event;
int rzx_init( void )
{
rzx_recording = rzx_playback = 0;
rzx_in_bytes = NULL;
rzx_in_allocated = 0;
sentinel_event = event_register( rzx_sentinel, "RZX sentinel" );
if( sentinel_event == -1 ) return 1;
end_event = debugger_event_register( event_type_string, end_event_detail_string );
if( end_event == -1 ) return 1;
return 0;
}
int rzx_start_recording( const char *filename, int embed_snapshot )
{
int error;
if( rzx_playback ) return 1;
rzx = libspectrum_rzx_alloc();
/* Store the filename */
rzx_filename = strdup( filename );
if( rzx_filename == NULL ) {
ui_error( UI_ERROR_ERROR, "out of memory in rzx_start_recording" );
return 1;
}
/* If we're embedding a snapshot, create it now */
if( embed_snapshot ) {
libspectrum_snap *snap = libspectrum_snap_alloc();
error = snapshot_copy_to( snap );
if( error ) {
libspectrum_snap_free( snap );
return 1;
}
error = libspectrum_rzx_add_snap( rzx, snap, 0 );
if( error ) {
libspectrum_snap_free( snap );
return error;
}
}
libspectrum_rzx_start_input( rzx, tstates );
counter_reset();
rzx_in_count = 0;
autosave_frame_count = 0;
rzx_recording = 1;
ui_menu_activate( UI_MENU_ITEM_RECORDING, 1 );
if( settings_current.competition_mode ) {
if( !libspectrum_gcrypt_version() )
ui_error( UI_ERROR_WARNING,
"gcrypt not available: recording will NOT be signed" );
settings_current.emulation_speed = 100;
rzx_competition_mode = 1;
} else {
ui_menu_activate( UI_MENU_ITEM_RECORDING_ROLLBACK, 1 );
rzx_competition_mode = 0;
}
return 0;
}
int rzx_stop_recording( void )
{
libspectrum_byte *buffer; size_t length;
libspectrum_error libspec_error; int error;
if( !rzx_recording ) return 0;
/* Stop recording data */
rzx_recording = 0;
ui_menu_activate( UI_MENU_ITEM_RECORDING, 0 );
ui_menu_activate( UI_MENU_ITEM_RECORDING_ROLLBACK, 0 );
libspectrum_creator_set_competition_code(
fuse_creator, settings_current.competition_code
);
length = 0;
libspec_error = libspectrum_rzx_write(
&buffer, &length, rzx, LIBSPECTRUM_ID_UNKNOWN, fuse_creator,
settings_current.rzx_compression, rzx_competition_mode ? &rzx_key : NULL
);
if( libspec_error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_rzx_free( rzx );
return libspec_error;
}
error = utils_write_file( rzx_filename, buffer, length );
free( rzx_filename );
if( error ) { free( buffer ); libspectrum_rzx_free( rzx ); return error; }
free( buffer );
libspec_error = libspectrum_rzx_free( rzx );
if( libspec_error != LIBSPECTRUM_ERROR_NONE ) return libspec_error;
return 0;
}
libspectrum_snap*
rzx_get_initial_snapshot( void )
{
libspectrum_rzx_iterator it;
for( it = libspectrum_rzx_iterator_begin( rzx );
it;
it = libspectrum_rzx_iterator_next( it ) ) {
libspectrum_rzx_block_id id = libspectrum_rzx_iterator_get_type( it );
switch( id ) {
case LIBSPECTRUM_RZX_INPUT_BLOCK:
/* If we get this then there can't have been an initial snap to start
from */
return NULL;
break;
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
/* Got initial snap */
return libspectrum_rzx_iterator_get_snap( it );
break;
default:
continue;
}
}
return NULL;
}
int rzx_start_playback( const char *filename, int check_snapshot )
{
utils_file file;
libspectrum_error libspec_error; int error;
libspectrum_snap* snap;
if( rzx_recording ) return 1;
rzx = libspectrum_rzx_alloc();
error = utils_read_file( filename, &file );
if( error ) return error;
libspec_error = libspectrum_rzx_read( rzx, file.buffer, file.length );
if( libspec_error != LIBSPECTRUM_ERROR_NONE ) {
utils_close_file( &file );
return libspec_error;
}
if( utils_close_file( &file ) ) {
libspectrum_rzx_free( rzx );
return 1;
}
snap = rzx_get_initial_snapshot();
if( !snap && check_snapshot ) {
/* We need to load an external snapshot. Could be skipped if the snapshot
is preloaded from command line */
error = utils_open_snap();
if( error ) return error;
}
error = start_playback( rzx );
if( error ) {
libspectrum_rzx_free( rzx );
return error;
}
return 0;
}
int
rzx_start_playback_from_buffer( const unsigned char *buffer, size_t length )
{
int error;
libspectrum_snap* snap;
if( rzx_recording ) return 0;
rzx = libspectrum_rzx_alloc();
error = libspectrum_rzx_read( rzx, buffer, length );
if( error ) return error;
snap = rzx_get_initial_snapshot();
if( !snap ) {
error = utils_open_snap();
if( error ) {
libspectrum_rzx_free( rzx );
return error;
}
}
error = start_playback( rzx );
if( error ) {
libspectrum_rzx_free( rzx );
return error;
}
return 0;
}
static int
start_playback( libspectrum_rzx *rzx )
{
int error;
libspectrum_snap *snap;
error = libspectrum_rzx_start_playback( rzx, 0, &snap );
if( error ) return error;
if( snap ) {
error = snapshot_copy_from( snap );
if( error ) return error;
}
/* End of frame will now be generated by the RZX code */
event_remove_type( spectrum_frame_event );
/* Add a sentinel event to prevent tstates overrun (bug #1057471) */
error = event_add( RZX_SENTINEL_TIME, sentinel_event );
if( error ) return error;
tstates = libspectrum_rzx_tstates( rzx );
rzx_instruction_count = libspectrum_rzx_instructions( rzx );
rzx_playback = 1;
counter_reset();
ui_menu_activate( UI_MENU_ITEM_RECORDING, 1 );
ui_menu_activate( UI_MENU_ITEM_RECORDING_ROLLBACK, 0 );
return 0;
}
int rzx_stop_playback( int add_interrupt )
{
libspectrum_error libspec_error; int error;
rzx_playback = 0;
ui_menu_activate( UI_MENU_ITEM_RECORDING, 0 );
ui_menu_activate( UI_MENU_ITEM_RECORDING_ROLLBACK, 0 );
event_remove_type( sentinel_event );
/* We've now finished with the RZX file, so add an end of frame
event if we've been requested to do so; we don't if we just run
out of frames, as this occurs just before a normal end of frame
and everything works normally as rzx_playback is now zero again */
if( add_interrupt ) {
error = event_add( machine_current->timings.tstates_per_frame,
spectrum_frame_event );
if( error ) return error;
/* We're no longer doing RZX playback, so tstates now be <= the
normal frame count */
if( tstates > machine_current->timings.tstates_per_frame )
tstates = machine_current->timings.tstates_per_frame;
} else {
/* Ensure that tstates will be zero after it is reduced in
spectrum_frame() */
tstates = machine_current->timings.tstates_per_frame;
}
libspec_error = libspectrum_rzx_free( rzx );
if( libspec_error != LIBSPECTRUM_ERROR_NONE ) return libspec_error;
debugger_event( end_event );
return 0;
}
int rzx_frame( void )
{
if( rzx_recording ) return recording_frame();
if( rzx_playback ) return playback_frame();
return 0;
}
typedef struct prune_info_t {
libspectrum_rzx_iterator it;
size_t frames;
} prune_info_t;
static void
autosave_prune( void )
{
GArray *autosaves = g_array_new( FALSE, FALSE, sizeof( prune_info_t ) );
libspectrum_rzx_iterator it;
size_t i, frames = 0;
for( it = libspectrum_rzx_iterator_begin( rzx );
it;
it = libspectrum_rzx_iterator_next( it ) ) {
libspectrum_rzx_block_id id = libspectrum_rzx_iterator_get_type( it );
switch( id ) {
case LIBSPECTRUM_RZX_INPUT_BLOCK:
frames += libspectrum_rzx_iterator_get_frames( it ); break;
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
if( libspectrum_rzx_iterator_snap_is_automatic( it ) ) {
prune_info_t info = { it, frames };
g_array_append_val( autosaves, info );
}
break;
default:
break;
}
}
/* Convert 'time from start' into 'time before now' */
for( i = 0; i < autosaves->len; i++ ) {
prune_info_t *info = &( g_array_index( autosaves, prune_info_t, i ) );
info->frames = frames - info->frames;
}
for( i = autosaves->len - 1; i > 0; i-- ) {
prune_info_t save1 = g_array_index( autosaves, prune_info_t, i ),
save2 = g_array_index( autosaves, prune_info_t, i - 1 );
if( ( save1.frames == 15 * 50 ||
save1.frames == 60 * 50 ||
save1.frames == 300 * 50 ) &&
save2.frames < 2 * save1.frames
)
/* FIXME: could possibly merge adjacent IRBs here */
libspectrum_rzx_iterator_delete( rzx, save1.it );
}
g_array_set_size( autosaves, 0 );
}
static void
autosave_frame( void )
{
libspectrum_snap *snap;
if( ++autosave_frame_count % AUTOSAVE_INTERVAL ) return;
snap = libspectrum_snap_alloc();
snapshot_copy_to( snap );
libspectrum_rzx_stop_input( rzx );
libspectrum_rzx_add_snap( rzx, snap, 1 );
libspectrum_rzx_start_input( rzx, tstates );
autosave_prune();
}
static int recording_frame( void )
{
libspectrum_error error;
error = libspectrum_rzx_store_frame( rzx, R + rzx_instructions_offset,
rzx_in_count, rzx_in_bytes );
if( error ) {
rzx_stop_recording();
return error;
}
/* Reset the instruction counter */
rzx_in_count = 0; counter_reset();
/* If we're in competition mode, check we're running at close to 100%
speed */
if( rzx_competition_mode &&
fabs( current_speed - 100.0 ) > SPEED_TOLERANCE ) {
rzx_stop_recording();
ui_error(
UI_ERROR_INFO,
"emulator speed is %d%%: stopping competition mode RZX recording",
(int)( current_speed )
);
}
if( !rzx_competition_mode && settings_current.rzx_autosaves )
autosave_frame();
return 0;
}
static int playback_frame( void )
{
int error, finished;
libspectrum_snap *snap;
error = libspectrum_rzx_playback_frame( rzx, &finished, &snap );
if( error ) return rzx_stop_playback( 0 );
if( finished ) {
ui_error( UI_ERROR_INFO, "Finished RZX playback" );
return rzx_stop_playback( 0 );
}
/* Move the RZX sentinel back out to 79000 tstates; the addition of
the frame length is because everything is reduced by that in
event_frame() */
event_remove_type( sentinel_event );
event_add( RZX_SENTINEL_TIME + tstates, sentinel_event );
if( snap ) {
error = snapshot_copy_from( snap );
if( error ) return rzx_stop_playback( 0 );
}
/* If we've got another frame to do, fetch the new instruction count and
continue */
rzx_instruction_count = libspectrum_rzx_instructions( rzx );
counter_reset();
return 0;
}
/* Reset the RZX counter; also, take this opportunity to normalise the
R register */
static int counter_reset( void )
{
R &= 0x7f; /* Clear all but the 7 lowest bits of the R register */
rzx_instructions_offset = -R; /* Gives us a zero count */
return 0;
}
int rzx_store_byte( libspectrum_byte value )
{
/* Get more space if we need it; allocate twice as much as we currently
have, with a minimum of 50 */
if( rzx_in_count == rzx_in_allocated ) {
libspectrum_byte *ptr; size_t new_allocated;
new_allocated = rzx_in_allocated >= 25 ? 2 * rzx_in_allocated : 50;
ptr =
(libspectrum_byte*)realloc(
rzx_in_bytes, new_allocated * sizeof(libspectrum_byte)
);
if( ptr == NULL ) {
ui_error( UI_ERROR_ERROR, "Out of memory in rzx_store_byte" );
return 1;
}
rzx_in_bytes = ptr;
rzx_in_allocated = new_allocated;
}
rzx_in_bytes[ rzx_in_count++ ] = value;
return 0;
}
int rzx_end( void )
{
if( rzx_recording ) return rzx_stop_recording();
if( rzx_playback ) return rzx_stop_playback( 0 );
return 0;
}
static GSList*
get_rollback_list( libspectrum_rzx *rzx )
{
libspectrum_rzx_iterator it;
GSList *rollback_points;
size_t frames;
it = libspectrum_rzx_iterator_begin( rzx );
rollback_points = NULL;
frames = 0;
while( it ) {
libspectrum_rzx_block_id id = libspectrum_rzx_iterator_get_type( it );
switch( id ) {
case LIBSPECTRUM_RZX_INPUT_BLOCK:
frames += libspectrum_rzx_iterator_get_frames( it ); break;
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
rollback_points = g_slist_append( rollback_points,
GINT_TO_POINTER( frames ) );
break;
default:
break;
}
it = libspectrum_rzx_iterator_next( it );
}
/* Add the final IRB in, if any */
if( frames )
rollback_points = g_slist_append( rollback_points,
GINT_TO_POINTER( frames ) );
return rollback_points;
}
static int
start_after_rollback( libspectrum_snap *snap )
{
int error;
error = snapshot_copy_from( snap );
if( error ) return error;
libspectrum_rzx_start_input( rzx, tstates );
error = counter_reset();
if( error ) return error;
return 0;
}
int
rzx_rollback( void )
{
libspectrum_snap *snap;
int error;
error = libspectrum_rzx_rollback( rzx, &snap );
if( error ) return error;
error = start_after_rollback( snap );
if( error ) return error;
return 0;
}
int
rzx_rollback_to( void )
{
GSList *rollback_points;
libspectrum_snap *snap;
int which, error;
rollback_points = get_rollback_list( rzx );
which = ui_get_rollback_point( rollback_points );
if( which == -1 ) return 1;
error = libspectrum_rzx_rollback_to( rzx, &snap, which );
if( error ) return error;
error = start_after_rollback( snap );
if( error ) return error;
return 0;
}
static void
rzx_sentinel( libspectrum_dword ts, int type, void *user_data )
{
int error;
ui_error( UI_ERROR_WARNING, "RZX frame is longer than %u tstates",
RZX_SENTINEL_TIME );
tstates -= RZX_SENTINEL_TIME_REDUCE;
z80.interrupts_enabled_at -= RZX_SENTINEL_TIME_REDUCE;
/* Add another sentinel event in case this frame continues a lot more after
this */
error = event_add( RZX_SENTINEL_TIME, sentinel_event );
if( error ) return;
}
|