Skip to content

Commit

Permalink
Added OPT_C64_SNAP_COMPRESS (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Aug 31, 2024
1 parent bdd424e commit 9bba70f
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions Emulator/Base/Defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Defaults::Defaults()

setFallback(OPT_C64_SNAP_AUTO, false);
setFallback(OPT_C64_SNAP_DELAY, 10);
setFallback(OPT_C64_SNAP_COMPRESS, true);

setFallback(OPT_POWER_GRID, GRID_STABLE_50HZ);

Expand Down
2 changes: 1 addition & 1 deletion Emulator/Base/MsgQueueTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ enum_long(MSG_TYPE)
MSG_SHAKING, ///< A shaking mouse has been detected

// Snapshots
MSG_SNAPSHOT_TAKEN, ///< A snapshot has been taken (see OPT_SNAPSHOTS)
MSG_SNAPSHOT_TAKEN, ///< A snapshot has been taken (see OPT_C64_SNAP_AUTO)
MSG_SNAPSHOT_RESTORED, ///< A snapshot has been restored

// Screen recording
Expand Down
1 change: 1 addition & 0 deletions Emulator/Base/Option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OptionParser::create(Option opt, i64 arg)

case OPT_C64_SNAP_AUTO: return boolParser();
case OPT_C64_SNAP_DELAY: return numParser(" sec");
case OPT_C64_SNAP_COMPRESS: return boolParser();

case OPT_VICII_REVISION: return enumParser.template operator()<VICIIRevisionEnum>();
case OPT_VICII_GRAY_DOT_BUG: return boolParser();
Expand Down
3 changes: 3 additions & 0 deletions Emulator/Base/OptionTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum_long(OPT)
// Snapshots
OPT_C64_SNAP_AUTO, ///< Automatically take a snapshots
OPT_C64_SNAP_DELAY, ///< Delay between two snapshots in seconds
OPT_C64_SNAP_COMPRESS, ///< Compress snapshot data

// VICII
OPT_VICII_REVISION, ///< Chip revision
Expand Down Expand Up @@ -219,6 +220,7 @@ struct OptionEnum : util::Reflection<OptionEnum, Option> {

case OPT_C64_SNAP_AUTO: return "C64.SNAP_AUTO";
case OPT_C64_SNAP_DELAY: return "C64.SNAP_DELAY";
case OPT_C64_SNAP_COMPRESS: return "C64.SNAP_COMPRESS";

case OPT_VICII_REVISION: return "VICII.REVISION";
case OPT_VICII_GRAY_DOT_BUG: return "VICII.GRAY_DOT_BUG";
Expand Down Expand Up @@ -372,6 +374,7 @@ struct OptionEnum : util::Reflection<OptionEnum, Option> {

case OPT_C64_SNAP_AUTO: return "Automatically take snapshots";
case OPT_C64_SNAP_DELAY: return "Time span between two snapshots";
case OPT_C64_SNAP_COMPRESS: return "Compress snapshot data";

case OPT_VICII_REVISION: return "Chip revision";
case OPT_VICII_GRAY_DOT_BUG: return "Emulate gray-dot bug";
Expand Down
19 changes: 15 additions & 4 deletions Emulator/Components/C64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1249,18 +1249,29 @@ C64::clearFlag(u32 flag)
MediaFile *
C64::takeSnapshot()
{
{ SUSPENDED
Snapshot *result;

return new Snapshot(*this);
}
// Take the snapshot
{ SUSPENDED result = new Snapshot(*this); }

// Compress the snapshot if requested
if (config.compressSnapshots) result->compress();

return result;
}

void
C64::loadSnapshot(const MediaFile &file)
{
try {

const Snapshot &snapshot = dynamic_cast<const Snapshot &>(file);
const Snapshot &snap = dynamic_cast<const Snapshot &>(file);

// Make a copy so we can modify the snapshot
Snapshot snapshot(snap);

// Uncompress the snapshot
snapshot.uncompress();

{ SUSPENDED

Expand Down
1 change: 1 addition & 0 deletions Emulator/Components/C64.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class C64 final : public CoreComponent, public Inspectable<C64Info> {
OPT_C64_RUN_AHEAD,
OPT_C64_SNAP_AUTO,
OPT_C64_SNAP_DELAY,
OPT_C64_SNAP_COMPRESS
};

private:
Expand Down
14 changes: 12 additions & 2 deletions Emulator/Components/C64Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ C64::getOption(Option opt) const
case OPT_C64_RUN_AHEAD: return config.runAhead;
case OPT_C64_SNAP_AUTO: return config.snapshots;
case OPT_C64_SNAP_DELAY: return config.snapshotDelay;
case OPT_C64_SNAP_COMPRESS: return config.compressSnapshots;

default:
fatalError;
Expand Down Expand Up @@ -242,6 +243,10 @@ C64::checkOption(Option opt, i64 value)
}
return;

case OPT_C64_SNAP_COMPRESS:

return;

default:
throw Error(VC64ERROR_OPT_UNSUPPORTED);
}
Expand Down Expand Up @@ -275,6 +280,11 @@ C64::setOption(Option opt, i64 value)
updateClockFrequency();
return;

case OPT_C64_RUN_AHEAD:

config.runAhead = isize(value);
return;

case OPT_C64_SNAP_AUTO:

config.snapshots = bool(value);
Expand All @@ -287,9 +297,9 @@ C64::setOption(Option opt, i64 value)
scheduleNextSNPEvent();
return;

case OPT_C64_RUN_AHEAD:
case OPT_C64_SNAP_COMPRESS:

config.runAhead = isize(value);
config.compressSnapshots = bool(value);
return;

default:
Expand Down
2 changes: 1 addition & 1 deletion Emulator/Components/C64Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ typedef struct
isize snapshotDelay;

//! Indicates whether snapshots should be stored in compressed form
bool snapshotCompress;
bool compressSnapshots;
}
C64Config;

Expand Down
7 changes: 5 additions & 2 deletions Emulator/Media/MediaFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class MediaFile {
// Return a preview image (only available for snapshot files)
virtual const u32 *previewImageData() const { return nullptr; }

// Handels data compression (only implemented by snapshot files)
virtual bool isCompressed() const { return false; }
virtual void compress() { }
virtual void uncompress() { }

//
virtual void flash(u8 *buf, isize offset = 0) const = 0;

Expand All @@ -85,8 +90,6 @@ class MediaFile {

public:

// virtual isize readFromStream(std::istream &stream) = 0;
// virtual isize readFromFile(const std::filesystem::path &path) = 0;
virtual isize readFromBuffer(const u8 *buf, isize len) = 0;

virtual isize writeToStream(std::ostream &stream) = 0;
Expand Down
4 changes: 0 additions & 4 deletions Emulator/Media/Snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ Snapshot::Snapshot(C64 &c64) : Snapshot(c64.size())

if (SNP_DEBUG) c64.dump(Category::State);
c64.save(getSnapshotData());

// REMOVE ASAP (TEST CODE FOR THE RUN-LENGTH ENCODER)
compress();
uncompress();
}

void
Expand Down
6 changes: 3 additions & 3 deletions Emulator/Media/Snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ class Snapshot : public AnyFile {
//

// Indicates whether the snapshot is compressed
bool isCompressed() { return getHeader()->compressed; }
bool isCompressed() const override { return getHeader()->compressed; }

// Compresses or uncompresses the snapshot
void compress();
void uncompress();
void compress() override;
void uncompress() override;
};

}
2 changes: 1 addition & 1 deletion Emulator/Utilities/IOUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ bool matchingBufferHeader(const u8 *buf, isize blen, const string &header, isize

bool matchingBufferHeader(const u8 *buf, const string &header, isize offset)
{
auto blen = isize(std::numeric_limits<isize>::max);
auto blen = std::numeric_limits<isize>::max();
return matchingBufferHeader(buf, blen, header, offset);
}

Expand Down
3 changes: 2 additions & 1 deletion GUI/Panels/Captures/SnapshotViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ class SnapshotViewer: DialogController {

if let snapshot = myDocument.snapshots.element(at: currentItem) {
let takenAt = snapshot.timeStamp
let compressed = snapshot.compressed ? "(Compressed)" : ""
text1.stringValue = "Taken at " + timeInfo(time: takenAt)
text2.stringValue = Date.elapsed(time: takenAt)
text3.stringValue = "\(snapshot.size / 1024) KB"
text3.stringValue = "\(snapshot.size / 1024) KB " + compressed
message.stringValue = ""
} else {
nr.stringValue = "No snapshots taken"
Expand Down
1 change: 1 addition & 0 deletions Proxy/EmulatorProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ struct GuardInfo {
@property (readonly) FileType type;
@property (readonly) u64 fnv;
@property (readonly) NSInteger size;
@property (readonly) BOOL compressed;

- (void)writeToFile:(NSString *)path exception:(ExceptionWrapper *)ex;

Expand Down
5 changes: 5 additions & 0 deletions Proxy/EmulatorProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,11 @@ - (NSInteger)size
return [self file]->getSize();
}

- (BOOL)compressed
{
return [self file]->isCompressed();
}

- (void)writeToFile:(NSString *)path exception:(ExceptionWrapper *)ex
{
try { [self file]->writeToFile(string([path fileSystemRepresentation])); }
Expand Down

0 comments on commit 9bba70f

Please sign in to comment.