Skip to content

Commit

Permalink
Work on CoreComponent (loading and saving)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Aug 2, 2024
1 parent f4bb769 commit 252c534
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 63 deletions.
83 changes: 49 additions & 34 deletions Emulator/Base/CoreComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,70 +236,85 @@ CoreComponent::unfocus()
}

isize
CoreComponent::size()
CoreComponent::size(bool recursive)
{
// Count elements
SerCounter counter;
*this << counter;
isize result = counter.count;

// Add 8 bytes for the checksum
result += 8;

for (CoreComponent *c : subComponents) { result += c->size(); }
// Add size of subcomponents if requested
if (recursive) for (CoreComponent *c : subComponents) { result += c->size(); }

return result;
}

isize
CoreComponent::load(const u8 *buffer)
{
assert(!isRunning());

const u8 *ptr = buffer;

// Load internal state of all subcomponents
for (CoreComponent *c : subComponents) {
ptr += c->load(ptr);
}
isize result = 0;

// Load the checksum for this component
auto hash = read64(ptr);
postorderWalk([this, buffer, &result](CoreComponent *c) {

// Load internal state of this component
SerReader reader(ptr);
*this << reader;
ptr = reader.ptr;
const u8 *ptr = buffer + result;

// Check integrity
if (hash != checksum(false) || FORCE_SNAP_CORRUPTED) {
throw Error(ERROR_SNAP_CORRUPTED);
}
// Load the checksum for this component
auto hash = read64(ptr);

// Load the internal state of this component
SerReader reader(ptr); *c << reader;

// Determine the number of loaded bytes
isize count = (isize)(reader.ptr - (buffer + result));

// Check integrity
if (hash != c->checksum(false) || FORCE_SNAP_CORRUPTED) {
if (SNP_DEBUG) { fatalError; } else { throw Error(ERROR_SNAP_CORRUPTED); }
}

debug(SNP_DEBUG, "Loaded %ld bytes (expected %ld)\n", count, c->size(false));
result += count;
});

postorderWalk([](CoreComponent *c) { c->_didLoad(); });

isize result = (isize)(ptr - buffer);
debug(SNP_DEBUG, "Loaded %ld bytes (expected %ld)\n", result, size());
return result;
}

isize
CoreComponent::save(u8 *buffer)
{
u8 *ptr = buffer;
isize result = 0;

// Save internal state of all subcomponents
for (CoreComponent *c : subComponents) {
ptr += c->save(ptr);
}
postorderWalk([this, buffer, &result](CoreComponent *c) {

u8 *ptr = buffer + result;

// Save the checksum for this component
write64(ptr, c->checksum(false));

// Save the internal state of this component
SerWriter writer(ptr); *c << writer;

// Save the checksum for this component
write64(ptr, checksum(false));
// Determine the number of written bytes
isize count = (isize)(writer.ptr - (buffer + result));

// Check integrity
if (count != c->size(false) || FORCE_SNAP_CORRUPTED) {
if (SNP_DEBUG) { fatalError; } else { throw Error(ERROR_SNAP_CORRUPTED); }
}

debug(SNP_DEBUG, "Saved %ld bytes (expected %ld)\n", count, c->size(false));
result += count;
});

// Save the internal state of this component
SerWriter writer(ptr);
*this << writer;
ptr = writer.ptr;
postorderWalk([](CoreComponent *c) { c->_didSave(); });

isize result = (isize)(ptr - buffer);
debug(SNP_DEBUG, "Saved %ld bytes (expected %ld)\n", result, size());
assert(result == size());
return result;
}

Expand Down
12 changes: 7 additions & 5 deletions Emulator/Base/CoreComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,23 @@ public CoreObject, public Serializable, public Suspendable, public Synchronizabl
public:

// Returns the size of the internal state in bytes
isize size();
isize size(bool recursive = true);

// Resets the internal state
void hardReset() { reset(true); }
void softReset() { reset(false); }
void reset(bool hard);
virtual void _willReset(bool hard) { }
virtual void _didReset(bool hard) { }

// Convenience wrappers
void hardReset() { reset(true); }
void softReset() { reset(false); }

// Loads the internal state from a memory buffer
virtual isize load(const u8 *buf) throws;
isize load(const u8 *buf) throws;
virtual void _didLoad() { }

// Saves the internal state to a memory buffer
virtual isize save(u8 *buf);
isize save(u8 *buf);
virtual void _didSave() { }


Expand Down
19 changes: 0 additions & 19 deletions Emulator/Components/Amiga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,25 +591,6 @@ Amiga::_trackOff()
msgQueue.put(MSG_TRACK, 0);
}

isize
Amiga::load(const u8 *buffer)
{
auto result = CoreComponent::load(buffer);
postorderWalk([](CoreComponent *c) { c->_didLoad(); });

return result;
}

isize
Amiga::save(u8 *buffer)
{
auto result = CoreComponent::save(buffer);
postorderWalk([](CoreComponent *c) { c->_didSave(); });

return result;
}


void
Amiga::computeFrame()
{
Expand Down
4 changes: 0 additions & 4 deletions Emulator/Components/Amiga.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ class Amiga final : public CoreComponent, public Inspectable<AmigaInfo> {

public:

isize load(const u8 *buffer) override;
isize save(u8 *buffer) override;


void prefix() const override;

private:
Expand Down
2 changes: 1 addition & 1 deletion Emulator/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ debugflag QUEUE_DEBUG = 0;
debugflag SNP_DEBUG = 0;

// Run ahead
debugflag RUA_DEBUG = 1;
debugflag RUA_DEBUG = 0;
debugflag RUA_CHECKSUM = 0;
debugflag RUA_ON_STEROIDS = 0;

Expand Down

0 comments on commit 252c534

Please sign in to comment.