Sync source edition with VCC Plus 3.7.0 build 63

This commit is contained in:
Brian Holman committed 2026-11-14 01:43:46 -07:00
1 parent c652a5276d
commit b2ecec4d87
86 files changed
+10519 -472

No files matched your search

+100 -6
View File
@@ -17,12 +17,21 @@
#include <chrono>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <exception>
#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
@interface VCCVideoPacket ()
@property(nonatomic, readwrite, copy) NSData *pixels, *samples;
@property(nonatomic, readwrite) CGRect activeArea;
@property(nonatomic, readwrite) uint64_t sampleOffset;
@end
@implementation VCCVideoPacket
@end
@interface VCCTraceRecord ()
@property(nonatomic, readwrite) NSUInteger index;
@property(nonatomic, readwrite) uint64_t clock, frame;
@@ -291,7 +300,7 @@ static std::vector<uint16_t> DebugAddresses(NSArray<NSNumber*>* values, unsigned
@property(nonatomic, readwrite) uint64_t generation, frames, cycles;
@property(nonatomic, readwrite, copy) NSArray<VCCJoystickInputSnapshot *> *joystickInputs;
@property(nonatomic, readwrite) BOOL keyboardInputHeld;
@property(nonatomic, readwrite) uint64_t debuggerProgramRevision, debuggerMappingFingerprint, assistantMediaRevision;
@property(nonatomic, readwrite) uint64_t debuggerProgramRevision, debuggerMappingFingerprint, debuggerResidencyFingerprint, assistantMediaRevision;
@property(nonatomic, readwrite) BOOL debuggerDevelopmentAttached, debuggerAssemblyAttached, debuggerSourceStepping;
@property(nonatomic, readwrite) BOOL debuggerBankedDevelopment;
@property(nonatomic, readwrite) BOOL basicDebugAttached, assistantCompiledReturned;
@@ -384,6 +393,11 @@ struct Worker {
VCC::Mac::MachineSession machine;
VCC::Mac::MacAudioOutput audio;
bool audioEnabled = false, fastForward = false;
bool recording = false;
NSMutableArray<VCCVideoPacket *> *recordingPackets = nil;
std::chrono::steady_clock::time_point recordingOrigin{}, recordingLast{};
uint64_t recordingSample = 0;
std::string recordingError;
VCC::Mac::FramePacer pacer;
VCC::Mac::AudioProgress audioProgress;
std::chrono::steady_clock::time_point previousAudioTick{};
@@ -500,6 +514,19 @@ std::vector<VCC::Mac::DebugBankedModule> BankedModules(NSArray<NSDictionary<NSSt
VCC::Mac::DebugBankedModule parsed;
parsed.identifier = identifier.UTF8String;
parsed.guards = CodeGuards(addresses, bytes);
id overlayGroup = module[@"overlay_group"], overlayOwner = module[@"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("Overlay modules require bounded group and owner identities");
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("Overlay identities must not contain embedded NUL bytes");
parsed.overlayGroup = groupText; parsed.overlayOwner = ownerText;
}
if (id qualified = module[@"breakpoints"]) {
if (![qualified isKindOfClass:NSArray.class]) throw std::invalid_argument("Module breakpoints must be an array");
for (id point in qualified)
@@ -528,6 +555,7 @@ VCC::Mac::Snapshot AssemblyIdentity(VCCMachineSnapshot *snapshot) {
expected.generation = snapshot.generation;
expected.debuggerProgramRevision = snapshot.debuggerProgramRevision;
expected.debuggerMappingFingerprint = snapshot.debuggerMappingFingerprint;
expected.debuggerResidencyFingerprint = snapshot.debuggerResidencyFingerprint;
expected.cpu.PC = snapshot.programCounter;
expected.cycles = snapshot.cycles;
return expected;
@@ -664,6 +692,7 @@ VCCMachineSnapshot *Capture(Worker& worker) {
if (worker.closed) result.state = VCCMachineStateClosed;
result.generation = value.generation; result.frames = value.frames;
result.debuggerProgramRevision = value.debuggerProgramRevision; result.debuggerMappingFingerprint = value.debuggerMappingFingerprint;
result.debuggerResidencyFingerprint = value.debuggerResidencyFingerprint;
result.debuggerDevelopmentAttached = value.debuggerDevelopmentAttached;
result.debuggerBankedDevelopment = value.debuggerBankedDevelopment;
result.debuggerAssemblyAttached = value.debuggerAssemblyAttached;
@@ -714,6 +743,32 @@ void CheckCompiledRun(Worker& worker, VCCCompiledRunLease *lease, VCCMachineSnap
if (!worker.machine.sharedCompiledCallActive())
throw std::logic_error(worker.machine.sharedCompiledCallReturned() ? "run_finished" : "unverified_program_entry");
}
void RecordVideoPacket(Worker& worker, const std::vector<VCC::Mac::StereoSample>& samples) {
if (!worker.recording) return;
const auto value = worker.machine.snapshot();
if (!value.frame || value.frame->pixels.size() != 640 * 480) return;
if (worker.recordingPackets.count >= 8) {
worker.recording = false;
worker.recordingError = "Recording input stopped keeping up. The working movie has been retained; the CoCo keeps running.";
return;
}
const auto now = std::chrono::steady_clock::now();
const auto elapsed = std::chrono::duration<double>(now - worker.recordingOrigin).count();
const uint64_t position = std::max(worker.recordingSample, uint64_t(elapsed * 44100));
const auto& frame = *value.frame;
auto packet = [VCCVideoPacket new];
packet.pixels = [NSData dataWithBytes:frame.pixels.data() length:frame.pixels.size() * sizeof(uint32_t)];
packet.activeArea = CGRectMake(frame.activeArea.x, frame.activeArea.y, frame.activeArea.width, frame.activeArea.height);
packet.samples = [NSData dataWithBytes:samples.data() length:samples.size() * sizeof(VCC::Mac::StereoSample)];
packet.sampleOffset = position;
worker.recordingSample = position + samples.size(); worker.recordingLast = now;
[worker.recordingPackets addObject:packet];
}
std::vector<VCC::Mac::StereoSample> DrainWorkerAudio(Worker& worker) {
auto samples = worker.machine.takeAudio();
RecordVideoPacket(worker, samples);
return samples;
}
void AdvanceWorker(Worker& worker) {
if (worker.compiledKeyLease) {
if (worker.machine.pasteRemaining()) {
@@ -749,6 +804,40 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
dispatch_source_t _timer;
std::shared_ptr<Worker> _worker;
}
- (void)beginVideoCaptureWithCompletion:(VCCMachineCompletion)completion {
[self beginVideoCaptureOnQueue:dispatch_get_main_queue() completion:completion];
}
- (void)beginVideoCaptureOnQueue:(dispatch_queue_t)replyQueue completion:(VCCMachineCompletion)completion {
auto worker = _worker;
dispatch_async(_queue, ^{
try {
worker->error.clear();
worker->requireNoDiskEditor();
if (worker->closed || !worker->machine.snapshot().frame) throw std::logic_error("No CoCo picture is available to record.");
if (worker->fastForward) throw std::logic_error("Turn off fast-forward before recording video.");
if (worker->recording) throw std::logic_error("A CoCo movie is already recording.");
worker->recordingPackets = [NSMutableArray new]; worker->recordingError.clear();
worker->recordingSample = 0;
worker->recordingOrigin = worker->recordingLast = std::chrono::steady_clock::now();
worker->recording = true;
} catch (const std::exception& error) { worker->error = error.what(); }
VCCMachineSnapshot *snapshot = Capture(*worker);
dispatch_async(replyQueue, ^{ completion(snapshot); });
});
}
- (void)takeVideoCapturePackets:(BOOL)finish completion:(void (^)(NSArray<VCCVideoPacket *> *, NSString *))completion {
[self takeVideoCapturePackets:finish replyQueue:dispatch_get_main_queue() completion:completion];
}
- (void)takeVideoCapturePackets:(BOOL)finish replyQueue:(dispatch_queue_t)replyQueue completion:(void (^)(NSArray<VCCVideoPacket *> *, NSString *))completion {
auto worker = _worker;
dispatch_async(_queue, ^{
if (finish) worker->recording = false;
NSArray *packets = [worker->recordingPackets copy] ?: @[];
[worker->recordingPackets removeAllObjects];
NSString *error = worker->recordingError.empty() ? nil : [NSString stringWithUTF8String:worker->recordingError.c_str()];
dispatch_async(replyQueue, ^{ completion(packets, error); });
});
}
- (instancetype)init {
if ((self = [super init])) {
_worker = std::make_shared<Worker>();
@@ -765,6 +854,8 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
try {
if (worker->machine.snapshot().state != VCC::Mac::RunState::Running) {
if (worker->audio.running()) worker->audio.stop();
if (worker->recording && std::chrono::steady_clock::now() - worker->recordingLast >= std::chrono::microseconds(16667))
RecordVideoPacket(*worker, std::vector<VCC::Mac::StereoSample>(735));
worker->pacer.reset(); worker->audioProgress.reset(); return;
}
if (worker->fastForward) {
@@ -772,7 +863,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
// input, pause, and automatic media saves keep getting turns.
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(8);
for (unsigned i = 0; i < 4; ++i) {
AdvanceWorker(*worker); worker->machine.takeAudio();
AdvanceWorker(*worker); DrainWorkerAudio(*worker);
if (std::chrono::steady_clock::now() >= deadline) break;
}
} else if (worker->audioEnabled) {
@@ -784,7 +875,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
// Roughly 100 ms ahead, at most four frames per queue turn.
// The physical audio clock paces production; guest clocks stay fixed.
for(unsigned i=0;i<4 && worker->audio.buffer().size()<VCC::Mac::audioTargetSamples;++i) {
AdvanceWorker(*worker); auto samples=worker->machine.takeAudio();
AdvanceWorker(*worker); auto samples=DrainWorkerAudio(*worker);
worker->audio.buffer().write(samples.data(), samples.size());
if (worker->machine.snapshot().state != VCC::Mac::RunState::Running) break;
}
@@ -805,7 +896,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
throw std::runtime_error("Audio output stopped consuming samples. Emulation continues without sound; turn Sound on to retry.");
} else {
if (worker->pacer.ready(std::chrono::steady_clock::now())) {
AdvanceWorker(*worker); worker->machine.takeAudio();
AdvanceWorker(*worker); DrainWorkerAudio(*worker);
}
}
} catch (const std::exception& error) {
@@ -842,6 +933,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
VCCMachineProfile* p = [profile copy];
__block BOOL applied = NO;
[self perform:^(Worker& worker) {
if (worker.recording) throw std::logic_error("Stop the CoCo movie before replacing the machine profile.");
if (worker.closed) throw std::logic_error("The machine controller has shut down");
if (p.ram < 0 || p.ram > 3 || p.overclock < 1 || p.overclock > 16 || p.frameInterval < 1 || p.frameInterval > 6 ||
p.videoOutput < 0 || p.videoOutput > 2 || p.physicalSlot < 1 || p.physicalSlot > 4 ||
@@ -943,6 +1035,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
value.ram = unsigned(ram); value.overclock = unsigned(overclock); value.frameInterval = unsigned(interval);
const auto previous = worker.machine.snapshot().configuration;
const bool restart = previous.processor != value.processor || previous.ram != value.ram;
if (worker.recording && (restart || fastForward)) throw std::logic_error("Stop the CoCo movie before changing CPU/RAM or enabling fast-forward.");
if (restart) {
if (auto* disk = worker.floppy(); disk && disk->snapshot().busy)
throw std::logic_error("Wait for the disk operation to finish before changing CPU or memory.");
@@ -976,6 +1069,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
- (void)loadROM:(NSData *)rom use6309:(BOOL)use6309 completion:(VCCMachineCompletion)completion {
NSData *owned = [rom copy];
[self command:^(Worker& worker) {
if (worker.recording) throw std::logic_error("Stop the CoCo movie before replacing the system ROM.");
if (owned.length != 0x8000) throw std::invalid_argument("A 32 KiB CoCo 3 ROM image is required");
const auto *begin = static_cast<const std::uint8_t *>(owned.bytes);
worker.machine.load({begin, begin + owned.length}, use6309 ? VCC::Mac::Processor::HD6309 : VCC::Mac::Processor::MC6809);
@@ -1414,7 +1508,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
generation, programRevision, mappingFingerprint, programCounter);
} completion:completion];
}
- (void)debuggerStepBankedSource:(NSInteger)mode addresses:(NSArray<NSNumber *> *)addresses locations:(NSArray<NSNumber *> *)locations bytes:(NSArray<NSData *> *)bytes moduleIdentifiers:(NSArray<NSString *> *)moduleIdentifiers generation:(uint64_t)generation programRevision:(uint64_t)programRevision mappingFingerprint:(uint64_t)mappingFingerprint programCounter:(uint16_t)programCounter completion:(VCCMachineCompletion)completion {
- (void)debuggerStepBankedSource:(NSInteger)mode addresses:(NSArray<NSNumber *> *)addresses locations:(NSArray<NSNumber *> *)locations bytes:(NSArray<NSData *> *)bytes moduleIdentifiers:(NSArray<NSString *> *)moduleIdentifiers generation:(uint64_t)generation programRevision:(uint64_t)programRevision mappingFingerprint:(uint64_t)mappingFingerprint residencyFingerprint:(uint64_t)residencyFingerprint programCounter:(uint16_t)programCounter completion:(VCCMachineCompletion)completion {
NSArray<NSNumber *> *ownedAddresses = [addresses copy], *ownedLocations = [locations copy];
NSArray<NSData *> *ownedBytes = [[NSArray alloc] initWithArray:bytes copyItems:YES];
NSArray<NSString *> *identifiers = [[NSArray alloc] initWithArray:moduleIdentifiers copyItems:YES];
@@ -1433,7 +1527,7 @@ void CheckAssistantMachine(Worker& worker, VCCMachineSnapshot *expected, NSStrin
instructions.push_back({parsed[i], uint32_t(location), OwnedBytes(ownedBytes[i], 8), identifiers[i].UTF8String});
}
worker.machine.debuggerStepSource(static_cast<VCC::Mac::SourceStepMode>(mode), instructions,
generation, programRevision, mappingFingerprint, programCounter);
generation, programRevision, mappingFingerprint, programCounter, 1000000, 600, residencyFingerprint);
} completion:completion];
}
- (void)debuggerSetPC:(uint16_t)address completion:(VCCMachineCompletion)completion {