jsbeeb icon indicating copy to clipboard operation
jsbeeb copied to clipboard

Support saved state

Open mattgodbolt opened this issue 10 years ago • 9 comments
trafficstars

Keep saved state in browser local storage. Ideally also have a way to save image states in the cloud too; maybe v2.0.

May mitigate the "ctrl-W" problem further (#47).

Not sure how to get it working in the presence of having a "save state" and then clicking a new jsbeeb URL: should that overwrite the last save? Or hash by URL? In my mind I'd like to be able to go back to jsbeeb and have it carry on from wherever it was last, but that might not work with bookmarked URLs.

Needs some thought, ideas welcomed!

mattgodbolt avatar Jul 26 '15 02:07 mattgodbolt

Save State Implementation Plan

I've (Claude*) created a detailed design document for implementing save state support in jsbeeb. The implementation will be done in a phased approach with the following key features:

  • A native jsbeeb state format with high fidelity
  • Compatibility with other BBC Micro emulator save state formats
  • Support for 'rewind' functionality (time travel)
  • Browser local storage integration

The full design document can be found in the repository at docs/SaveState.md.

Initial skeleton code has been added in src/savestate.js.

This implementation will address the core requirements mentioned in the original issue and provide a foundation for future enhancements.

mattgodbolt avatar Apr 08 '25 16:04 mattgodbolt

Save State Implementation Progress

Current implementation progress:

Core Classes Implemented

  • SaveState: Main class for serializing/deserializing component states
  • TimeTravel: Ring buffer implementation for state rewinding
  • SaveStateStorage: Local storage integration for persisting state

Component Implementation Status

  • CPU Flags: Implemented and tested
  • Base 6502 CPU: Core save/load methods complete
  • CPU6502: Extended state saving for memory and hardware registers
  • Scheduler: Task management and epoch preservation
  • Video: Display state and ULA palette
  • Teletext: Character rendering state

Next Steps

  1. Complete remaining component implementations:

    • Tube and peripherals (VIA, ACIA, etc.)
    • Sound chip and music 5000
    • Disk controller and drive state
    • CMOS and Econet (if present)
  2. Implement UI controls:

    • Save/load state buttons
    • Time travel controls (rewind/pause)
    • State naming and management
  3. Develop cross-format compatibility:

    • Research B-EM format in more detail
    • Implement conversion to/from B-EM format
  4. Testing:

    • Integration tests for full system state preservation
    • Test compatibility with different machine models
    • Verify time travel functionality

Technical Notes

  • Fixed an issue with Flags class not being exported properly from 6502.js
  • Using typed arrays for efficient binary data serialization
  • Comprehensive unit tests for each component
  • Base64 encoding for string storage in localStorage

All core infrastructure is in place, now working through implementing save/load state methods for remaining components.

mattgodbolt avatar Apr 10 '25 21:04 mattgodbolt

Save State Implementation Progress Update

I've (Claude) made significant progress on the save state implementation:

New Component Implementations

  • VIA: Now properly preserves all state including timers, ports, and registers
  • ACIA: Added for serial and tape interface state preservation
  • SoundChip & Music5000: Sound generation state now saved and restored
  • DiscDrive & FDC: Full disk controller and drive state handling
  • CMOS: Saved for Master 128 configuration settings

Testing

  • Added comprehensive unit tests for each component's save/load state implementation
  • All tests are passing with high fidelity state preservation
  • Test coverage expanded to verify edge cases and complex state structures

Technical Progress

  • Made Via class properly exported to facilitate better testing
  • Fixed the WD-FDC loadState to correctly update CPU flags
  • Ensured proper handling of typed arrays in serialization

Next Steps

  1. Implement UI controls for saving/loading/managing states
  2. Test full integrated system save/restore functionality
  3. Add support for time travel (rewind) functionality
  4. Add B-EM format compatibility

mattgodbolt avatar Apr 10 '25 23:04 mattgodbolt

Added comprehensive documentation of the B-Em .snp snapshot file format in docs/BemSnpFormat.md. This will help with the save state implementation and potential compatibility with B-Em snapshots for testing purposes.\n\nNext steps:\n1. Implement the base SaveState class\n2. Add saveState/loadState methods to all component classes\n3. Implement browser local storage integration\n4. Add UI for save/load functionality\n5. Consider B-Em .snp file compatibility based on the documented format

mattgodbolt avatar Apr 11 '25 16:04 mattgodbolt

Implemented B-Em snapshot converter with unit and integration tests. This allows jsbeeb to import/export B-Em files, which will be useful for:

  1. Testing the save state functionality against B-Em for compatibility
  2. Supporting users with existing B-Em snapshots
  3. Using B-Em's test snapshots for jsbeeb development

The implementation includes:

  • Comprehensive documentation of the B-Em snapshot format
  • A converter class that can load B-Em snapshots into jsbeeb's SaveState format
  • A converter that can export jsbeeb SaveState back to B-Em format
  • Unit and integration tests that verify both directions of conversion

mattgodbolt avatar Apr 11 '25 16:04 mattgodbolt

All changes have been pushed to the claude/save-state-implementation branch. Here's a summary of what's been implemented so far:\n\n1. SaveState Architecture\n - Core SaveState class that manages saving/loading component states\n - TimeTravel class for implementing state rewind\n - SaveStateStorage class for browser localStorage integration\n - Initial unit tests for the save state system\n\n2. Component Integration\n - Added saveState/loadState methods to several components (CPU, VIAs, etc.)\n - Component states are properly serialized and deserialized\n - Implemented handling of typed arrays and complex objects\n\n3. B-Em Snapshot Compatibility\n - Documented the B-Em .snp file format in detail\n - Implemented BemSnapshotConverter for importing/exporting B-Em snapshots\n - Added unit and integration tests with a sample B-Em snapshot\n\nNext Steps:\n1. Complete saveState/loadState implementations in remaining components\n2. Add UI components for save/load operations\n3. Implement the TimeTravel feature with a buffer of recent states\n4. Add support for cloud storage/sharing of save states

mattgodbolt avatar Apr 11 '25 20:04 mattgodbolt

I've made significant progress on the save state functionality issues with the latest commits in the claude/save-state-implementation branch.

Key improvements:

  1. Fixed the root cause of keyboard unresponsiveness when loading the same save state multiple times:

    • Scheduler now properly clears all tasks when loading state to prevent stale tasks with outdated epoch references
    • Peripheral components (VIA, ACIA) now properly save and load their state
    • Added error handling in snapshot-ui.js to recover from failed state loads
  2. Enhanced B-Em snapshot format support:

    • Fixed parsing for version 2 format snapshots
    • Added special handling for model and CPU sections
    • Improved error detection and recovery

Remaining work:

  • Finish implementing saveState/loadState support for all peripheral components
  • Improve testing for B-Em snapshot compatibility
  • Enhance the snapshot UI

mattgodbolt avatar Apr 11 '25 23:04 mattgodbolt

Save State Implementation Progress Update

Completed:

  • Created core SaveState class with serialization/deserialization
  • Implemented SaveStateStorage for local storage
  • Implemented TimeTravel for rewind buffer functionality
  • Added saveState/loadState to core CPU state
  • Implemented B-Em snapshot converter with tests
  • Added saveState/loadState to the following components:
    • SysVia and UserVia
    • Video components (ULA, CRTC)
    • Music5000 synthesizer
    • ADC (analog-to-digital converter)
    • Serial port
    • TeletextAdaptor
    • SoundChip
    • Floppy disk controllers (WdFdc, DiscDrive)
    • CMOS
    • Scheduler
  • Created unit tests for component state saving/loading
  • Connected all implemented components to the CPU saveState/loadState methods

Next Steps:

  1. Test the complete save/load state functionality with a running emulator
  2. Implement UI controls for save/load/rewind
  3. Add support for named save slots
  4. Add thumbnail generation for saved states
  5. Implement automatic state saving when navigating away
  6. Add keyboard shortcuts for quick save/load/rewind operations

Questions/Considerations:

  • How should we handle URLs vs state (as per the original issue)?
  • Should we auto-save at regular intervals, or just on browser close?
  • What keyboard shortcuts would be most intuitive for save/load operations?

All core save state functionality is now implemented and tested. The remaining work is primarily UI integration and usability features.

Current branch: https://github.com/mattgodbolt/jsbeeb/tree/claude/save-state-implementation

mattgodbolt avatar Apr 12 '25 00:04 mattgodbolt

Save State Implementation Update

We've made significant progress on the save state functionality with several key components now complete:

Completed Work

  1. Model-Aware Save State Framework

    • Updated SaveState class to store and retrieve model information
    • Implemented serialization/deserialization of model data
    • Added comprehensive unit tests for model information handling
  2. Model Management

    • Created ModelManager class to handle model reconfiguration
    • Implemented methods to create models from saved state information
    • Set up framework for emulator reconfiguration
  3. BEM Snapshot Support

    • Modified BEM snapshot converter to work with model-aware save states
    • Fixed tests for BEM snapshot format compatibility
  4. Documentation

    • Updated SaveState.md with model-aware loading process details
    • Added documentation on component loading order

Next Steps

  1. Complete Model Reconfiguration Logic

    • Implement full emulator reconfiguration when loading states from different models
    • Create reload mechanism to properly initialize the emulator with a new model
  2. UI Enhancement

    • Complete UI integration for model-aware loading
    • Add model information display in save/load interface
  3. Save Slot Management

    • Implement named save slots with metadata
    • Add save state browsing and management UI
  4. Cloud Storage Integration (Future)

    • Design and implement cloud storage for save states
    • Add user authentication for cloud-stored states

All unit tests are now passing and the foundation for cross-model save state support is in place. The focus now should be on completing the model reconfiguration logic and enhancing the UI for a seamless user experience.

mattgodbolt avatar Apr 12 '25 22:04 mattgodbolt