Sync source edition with VCC Plus 3.7.0 build 63
This commit is contained in:
1 parent
c652a5276d
commit
b2ecec4d87
86 files changed
+10519
-472
No files matched your search
@@ -11,9 +11,16 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
// The class is implemented in Swift and shares CoCoVideoEncoder with the app.
|
||||
@interface VCCPlaytestVideoWriter : NSObject
|
||||
- (instancetype)initWithDirectory:(NSURL *)directory includeAudio:(BOOL)includeAudio error:(NSError **)error;
|
||||
- (BOOL)appendPixels:(NSData *)pixels area:(CGRect)area samples:(NSData *)samples error:(NSError **)error;
|
||||
- (NSURL *)finishAndReturnError:(NSError **)error;
|
||||
@end
|
||||
using namespace VCC::Mac;
|
||||
namespace {
|
||||
constexpr size_t wireLimit = 1024 * 1024;
|
||||
@@ -21,6 +28,7 @@ constexpr uint64_t fieldBudget = 216000;
|
||||
constexpr size_t maximumDiskBytes = 512 * 1024;
|
||||
constexpr unsigned audioFieldLimit = 120;
|
||||
constexpr size_t audioSampleFrameLimit = 131072;
|
||||
constexpr uint64_t videoByteLimit = 64 * 1024 * 1024;
|
||||
void little16(std::vector<uint8_t>& bytes, uint16_t value) {
|
||||
bytes.push_back(value & 255); bytes.push_back(value >> 8);
|
||||
}
|
||||
@@ -274,6 +282,55 @@ class Playtest {
|
||||
void armStop() { armEvidence(stops, stopModule); }
|
||||
void armInspection() { armEvidence({}, {}); }
|
||||
uint64_t consumed = 0, fieldClock = 0, bootNumber = 0, mediaGeneration = 0;
|
||||
VCCPlaytestVideoWriter *videoWriter = nil;
|
||||
NSMutableDictionary *videoInfo = nil;
|
||||
NSURL *videoDirectory = nil;
|
||||
unsigned videoMaximumFields = 0;
|
||||
uint64_t videoFirstField = 0, videoSamples = 0;
|
||||
void finishVideo(NSString *reason) {
|
||||
if (!videoWriter) return;
|
||||
NSError *error = nil;
|
||||
NSURL *output = nil;
|
||||
if (videoSamples) output = [videoWriter finishAndReturnError:&error];
|
||||
videoWriter = nil;
|
||||
videoInfo[@"field_end"] = @(fieldClock);
|
||||
videoInfo[@"fields_recorded"] = @(fieldClock - videoFirstField);
|
||||
videoInfo[@"sample_frames"] = @(videoSamples);
|
||||
videoInfo[@"truncated"] = @(![reason isEqual:@"stopped"] && ![reason isEqual:@"run_stopped"]);
|
||||
videoInfo[@"reason"] = reason;
|
||||
videoInfo[@"status"] = @"failed"; videoInfo[@"completed"] = @NO;
|
||||
if (!output) { videoInfo[@"error"] = error.localizedDescription ?: @"No emulated audio/video interval was recorded."; return; }
|
||||
NSData *data = [NSData dataWithContentsOfURL:output options:NSDataReadingMappedIfSafe error:&error];
|
||||
if (!data || data.length == 0 || data.length > videoByteLimit) {
|
||||
videoInfo[@"error"] = error.localizedDescription ?: @"playtest_video_byte_limit"; return;
|
||||
}
|
||||
std::vector<uint8_t> bytes(static_cast<const uint8_t *>(data.bytes), static_cast<const uint8_t *>(data.bytes) + data.length);
|
||||
videoInfo[@"status"] = @"complete"; videoInfo[@"completed"] = @YES;
|
||||
videoInfo[@"byte_count"] = @(data.length); videoInfo[@"sha256"] = sha256(bytes);
|
||||
videoInfo[@"mime_type"] = @"video/mp4"; videoInfo[@"filename"] = @"Clip.mp4";
|
||||
}
|
||||
void recordVideo(const std::vector<StereoSample>& samples) {
|
||||
if (!videoWriter || samples.empty()) return;
|
||||
const auto snapshot = machine->snapshot();
|
||||
const auto frame = snapshot.frame;
|
||||
if (!frame || frame->pixels.size() != Frame::width * Frame::height) {
|
||||
finishVideo(@"picture_unavailable"); return;
|
||||
}
|
||||
const auto area = frame->activeArea;
|
||||
NSError *error = nil;
|
||||
const BOOL ok = [videoWriter appendPixels:[NSData dataWithBytes:frame->pixels.data() length:frame->pixels.size() * sizeof(uint32_t)]
|
||||
area:CGRectMake(area.x, area.y, area.width, area.height)
|
||||
samples:[NSData dataWithBytes:samples.data() length:samples.size() * sizeof(StereoSample)] error:&error];
|
||||
if (!ok) {
|
||||
videoWriter = nil; videoInfo[@"status"] = @"failed"; videoInfo[@"completed"] = @NO;
|
||||
videoInfo[@"error"] = error.localizedDescription ?: @"playtest_video_writer_failed"; return;
|
||||
}
|
||||
videoSamples += samples.size();
|
||||
videoInfo[@"field_end"] = @(fieldClock); videoInfo[@"fields_recorded"] = @(fieldClock - videoFirstField);
|
||||
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:[videoDirectory URLByAppendingPathComponent:@"Working.mov"].path error:nil];
|
||||
if ([attributes fileSize] >= videoByteLimit - 1024 * 1024) finishVideo(@"byte_limit");
|
||||
else if (fieldClock - videoFirstField >= videoMaximumFields) finishVideo(@"field_limit");
|
||||
}
|
||||
Processor processor = Processor::MC6809;
|
||||
NSString *originalDiskHash = nil, *currentMediaID = nil;
|
||||
NSString *diskHash() const {
|
||||
@@ -332,8 +389,9 @@ class Playtest {
|
||||
if (consumed + n > fieldBudget) throw std::runtime_error("playtest_field_limit");
|
||||
for (unsigned i = 0; i < n && machine->snapshot().state == RunState::Running; ++i) {
|
||||
const auto before = machine->snapshot().frames;
|
||||
machine->advance(); machine->takeAudio(); ++consumed;
|
||||
machine->advance(); ++consumed;
|
||||
fieldClock += machine->snapshot().frames - before;
|
||||
auto samples = machine->takeAudio(); recordVideo(samples);
|
||||
}
|
||||
}
|
||||
void boot() {
|
||||
@@ -354,7 +412,7 @@ class Playtest {
|
||||
auto s = machine->snapshot();
|
||||
const bool hd6309 = s.configuration.processor == Processor::HD6309;
|
||||
const auto *image = floppy->image(0);
|
||||
return @{ @"cpu": hd6309 ? @"6309" : @"6809", @"processor": hd6309 ? @"HD6309" : @"MC6809",
|
||||
NSMutableDictionary *out = [@{ @"cpu": hd6309 ? @"6309" : @"6809", @"processor": hd6309 ? @"HD6309" : @"MC6809",
|
||||
@"execution_mode": hd6309 ? (s.cpu.IsNative6309 ? @"native" : @"emulation") : @"not_applicable",
|
||||
@"md_register": hd6309 ? (id)@(s.cpu.MD) : NSNull.null, @"ram_kb": @512,
|
||||
@"fields": @(fieldClock), @"field_clock": @(fieldClock), @"boot": @(bootNumber), @"machine_frame": @(s.frames),
|
||||
@@ -366,7 +424,9 @@ class Playtest {
|
||||
@"disk_present": @(image != nullptr), @"media_id": currentMediaID ?: (id)NSNull.null,
|
||||
@"media_generation": @(mediaGeneration), @"floppy_busy": @(floppy->snapshot().busy),
|
||||
@"disk_dirty": @(image && image->dirty()), @"disk_reads": @(floppy->readEvents(0)),
|
||||
@"disk_writes": @(floppy->writeEvents(0)), @"isolated": @YES, @"input":inputMetadata(s) };
|
||||
@"disk_writes": @(floppy->writeEvents(0)), @"isolated": @YES, @"input":inputMetadata(s) } mutableCopy];
|
||||
if (videoInfo) out[@"video_recording"] = [videoInfo copy];
|
||||
return out;
|
||||
}
|
||||
NSDictionary *capture() const {
|
||||
NSMutableDictionary *out = [status() mutableCopy];
|
||||
@@ -396,6 +456,7 @@ class Playtest {
|
||||
auto next = machine->takeAudio();
|
||||
if (next.size() > audioSampleFrameLimit - samples.size()) throw std::runtime_error("playtest_audio_limit");
|
||||
samples.insert(samples.end(), next.begin(), next.end());
|
||||
recordVideo(next);
|
||||
};
|
||||
// Discard audio from earlier input/steps. A capture owns only this
|
||||
// explicit interval, including any normal leave-breakpoint CPU slice.
|
||||
@@ -452,6 +513,19 @@ public:
|
||||
for (NSDictionary *item in declared) {
|
||||
if (![item isKindOfClass:NSDictionary.class] || ![item[@"id"] isKindOfClass:NSString.class]) throw std::invalid_argument("invalid_modules");
|
||||
DebugBankedModule module; module.identifier = [item[@"id"] UTF8String];
|
||||
id overlayGroup = item[@"overlay_group"], overlayOwner = item[@"overlay_owner"];
|
||||
if (overlayGroup || overlayOwner) {
|
||||
if (![overlayGroup isKindOfClass:NSString.class] || ![overlayOwner isKindOfClass:NSString.class] ||
|
||||
![overlayGroup length] || ![overlayOwner length] ||
|
||||
[overlayGroup lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 128 ||
|
||||
[overlayOwner lengthOfBytesUsingEncoding:NSUTF8StringEncoding] > 128)
|
||||
throw std::invalid_argument("invalid_overlay_identity");
|
||||
const char *groupText = [overlayGroup UTF8String], *ownerText = [overlayOwner UTF8String];
|
||||
if (!groupText || !ownerText || std::strlen(groupText) != [overlayGroup lengthOfBytesUsingEncoding:NSUTF8StringEncoding] ||
|
||||
std::strlen(ownerText) != [overlayOwner lengthOfBytesUsingEncoding:NSUTF8StringEncoding])
|
||||
throw std::invalid_argument("invalid_overlay_identity");
|
||||
module.overlayGroup = groupText; module.overlayOwner = ownerText;
|
||||
}
|
||||
NSArray *pages = item[@"pages"], *guards = item[@"guards"];
|
||||
if (![pages isKindOfClass:NSArray.class] || pages.count == 0 || pages.count > 8 ||
|
||||
![guards isKindOfClass:NSArray.class] || guards.count == 0 || guards.count > 8192) throw std::invalid_argument("invalid_modules");
|
||||
@@ -543,6 +617,32 @@ public:
|
||||
return exportMedia();
|
||||
}
|
||||
if ([op isEqual:@"exchange_media"]) return exchangeMedia(r);
|
||||
if ([op isEqual:@"video_start"]) {
|
||||
if (r.count != 5 || ![r[@"clip_id"] isKindOfClass:NSString.class] ||
|
||||
![r[@"directory"] isKindOfClass:NSString.class] || !r[@"include_audio"] ||
|
||||
CFGetTypeID((__bridge CFTypeRef)r[@"include_audio"]) != CFBooleanGetTypeID()) throw std::invalid_argument("invalid_arguments");
|
||||
if (videoWriter) throw std::runtime_error("playtest_video_active");
|
||||
NSUUID *identifier = [[NSUUID alloc] initWithUUIDString:r[@"clip_id"]];
|
||||
NSURL *directory = [NSURL fileURLWithPath:r[@"directory"] isDirectory:YES];
|
||||
if (!identifier || ![directory.lastPathComponent isEqual:identifier.UUIDString] ||
|
||||
![NSFileManager.defaultManager fileExistsAtPath:directory.path]) throw std::invalid_argument("invalid_arguments");
|
||||
videoMaximumFields = number(r[@"max_fields"], 1, 3600);
|
||||
NSError *error = nil;
|
||||
videoWriter = [[VCCPlaytestVideoWriter alloc] initWithDirectory:directory includeAudio:[r[@"include_audio"] boolValue] error:&error];
|
||||
if (!videoWriter) throw std::runtime_error((error.localizedDescription ?: @"playtest_video_start_failed").UTF8String);
|
||||
videoDirectory = directory; videoFirstField = fieldClock; videoSamples = 0;
|
||||
machine->takeAudio();
|
||||
videoInfo = [@{@"clip_id":identifier.UUIDString, @"status":@"recording", @"include_audio":r[@"include_audio"],
|
||||
@"max_fields":@(videoMaximumFields), @"field_start":@(fieldClock), @"field_end":@(fieldClock),
|
||||
@"fields_recorded":@0, @"completed":@NO, @"truncated":@NO, @"boot":@(bootNumber),
|
||||
@"ui_cues_included":@NO, @"microphone_included":@NO, @"host_audio_included":@NO} mutableCopy];
|
||||
return status();
|
||||
}
|
||||
if ([op isEqual:@"video_stop"]) {
|
||||
if (r.count != 1 || !videoInfo) throw std::runtime_error("playtest_video_unavailable");
|
||||
finishVideo(@"stopped");
|
||||
NSMutableDictionary *out = [status() mutableCopy]; out[@"video"] = [videoInfo copy]; return out;
|
||||
}
|
||||
if ([op isEqual:@"input"]) {
|
||||
if (r.count != 2) throw std::invalid_argument("invalid_arguments");
|
||||
unsigned total = 0;
|
||||
@@ -563,6 +663,7 @@ public:
|
||||
applied = machine->resumeWithInput(step.keys, step.joysticks);
|
||||
const auto advanced = machine->snapshot().frames - before.frames;
|
||||
fieldClock += advanced; consumed += advanced;
|
||||
auto samples = machine->takeAudio(); recordVideo(samples);
|
||||
if (advanced > step.fields) throw std::runtime_error("playtest_input_timing");
|
||||
} else {
|
||||
machine->setKeyboard(step.keys);
|
||||
@@ -621,15 +722,16 @@ public:
|
||||
return out;
|
||||
}
|
||||
if ([op isEqual:@"restart"]) {
|
||||
if (videoWriter) throw std::runtime_error("playtest_video_active: stop the recording before restarting");
|
||||
if (floppy->snapshot().busy) throw std::runtime_error("disk_busy");
|
||||
boot(); return status();
|
||||
}
|
||||
if ([op isEqual:@"continue"]) { armInspection(); machine->resume(); return status(); }
|
||||
if ([op isEqual:@"continue"]) { armInspection(); const auto before = machine->snapshot().frames; machine->resume(); fieldClock += machine->snapshot().frames - before; auto samples = machine->takeAudio(); recordVideo(samples); return status(); }
|
||||
if ([op isEqual:@"pause"]) { machine->releaseInputs(); machine->pause(); return status(); }
|
||||
if ([op isEqual:@"step"]) {
|
||||
if (machine->snapshot().state != RunState::Paused) throw std::runtime_error("playtest_not_stopped");
|
||||
const auto before = machine->snapshot().frames;
|
||||
machine->debuggerStep(); fieldClock += machine->snapshot().frames - before; return status();
|
||||
machine->debuggerStep(); fieldClock += machine->snapshot().frames - before; auto samples = machine->takeAudio(); recordVideo(samples); return status();
|
||||
}
|
||||
if ([op isEqual:@"stop_at"]) {
|
||||
if (modules.empty() && flatGuards.empty()) throw std::runtime_error("unverified_stop");
|
||||
@@ -643,7 +745,8 @@ public:
|
||||
}
|
||||
armEvidence({nextPoint}, nextModule);
|
||||
stops = {nextPoint}; stopModule = nextModule;
|
||||
machine->resume(); return status();
|
||||
const auto before = machine->snapshot().frames; machine->resume(); fieldClock += machine->snapshot().frames - before;
|
||||
auto samples = machine->takeAudio(); recordVideo(samples); return status();
|
||||
}
|
||||
if ([op isEqual:@"debug"]) {
|
||||
if (machine->snapshot().state != RunState::Paused) throw std::runtime_error("playtest_not_stopped");
|
||||
@@ -658,6 +761,7 @@ public:
|
||||
out[@"registers"] = registers;
|
||||
out[@"mapping_fingerprint"] = [NSString stringWithFormat:@"%llu",(unsigned long long)machine->snapshot().debuggerMappingFingerprint];
|
||||
out[@"program_revision"] = @(machine->snapshot().debuggerProgramRevision);
|
||||
out[@"residency_fingerprint"] = [NSString stringWithFormat:@"%llu",(unsigned long long)machine->snapshot().debuggerResidencyFingerprint];
|
||||
out[@"attached"] = @(machine->snapshot().debuggerDevelopmentAttached);
|
||||
out[@"paused"] = @YES;
|
||||
out[@"logical_memory"] = [[NSData dataWithBytes:d.memory.data() length:d.memory.size()] base64EncodedStringWithOptions:0];
|
||||
@@ -684,7 +788,9 @@ public:
|
||||
}
|
||||
if ([op isEqual:@"stop"]) {
|
||||
machine->releaseInputs(); machine->pause();
|
||||
finishVideo(@"run_stopped");
|
||||
NSMutableDictionary *out = [status() mutableCopy];
|
||||
if (videoInfo) out[@"video"] = [videoInfo copy];
|
||||
if (const auto *image = floppy->image(0)) {
|
||||
const auto& disk = image->bytes();
|
||||
out[@"disk_base64"] = [[NSData dataWithBytes:disk.data() length:disk.size()] base64EncodedStringWithOptions:0];
|
||||
|
||||
Reference in new issue
Block a user