// SPDX-License-Identifier: GPL-3.0-or-later #import #import "../Bridge/VCCMachineController.h" #include #include #include #include @interface ControllerTests : XCTestCase @end @implementation ControllerTests - (void)waitForPrinterFrames:(VCCMachineController*)controller after:(uint64_t)frame attempts:(NSUInteger)attempts completion:(VCCMachineCompletion)completion { [controller snapshotWithCompletion:^(VCCMachineSnapshot* current) { if (current.frames >= frame + 3 || current.errorMessage || attempts == 0) { completion(current); return; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ [self waitForPrinterFrames:controller after:frame attempts:attempts - 1 completion:completion]; }); }]; } - (void)testJoystickInputSnapshotsAreImmutableAndReleaseOnPause { for (BOOL use6309 : {NO, YES}) { auto c = [VCCMachineController new]; auto initial = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWith6309:use6309 completion:done]; }]; XCTAssertEqual(initial.joystickInputs.count, 2u); XCTAssertEqual(initial.joystickInputs[0].x, 8191u); [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; [c setJoystick:0 x:0 y:16383 buttons:3 mode:0]; [c setJoystick:1 x:16383 y:0 buttons:2 mode:2]; [c setKeyboardMask:2]; auto held = [self diskCommand:^(VCCMachineCompletion done) { [c snapshotWithCompletion:done]; }]; XCTAssertEqual(held.joystickInputs[0].x, 0u); XCTAssertEqual(held.joystickInputs[0].y, 16383u); XCTAssertEqual(held.joystickInputs[0].buttons, 3u); XCTAssertTrue(held.keyboardInputHeld); XCTAssertEqual(held.joystickInputs[1].x, 16383u); XCTAssertEqual(held.joystickInputs[1].y, 0u); XCTAssertEqual(held.joystickInputs[1].buttons, 2u); XCTAssertEqual(held.joystickInputs[1].mode, 2u); auto paused = [self diskCommand:^(VCCMachineCompletion done) { [c pauseWithCompletion:done]; }]; for (VCCJoystickInputSnapshot *port in paused.joystickInputs) { XCTAssertEqual(port.x, 8191u); XCTAssertEqual(port.y, 8191u); XCTAssertEqual(port.buttons, 0u); } // Previously published observations remain immutable when live input changes. XCTAssertEqual(held.joystickInputs[0].x, 0u); XCTAssertEqual(held.joystickInputs[0].buttons, 3u); XCTAssertTrue(held.keyboardInputHeld); XCTAssertFalse(paused.keyboardInputHeld); XCTAssertEqual(paused.generation, held.generation); [c setJoystick:0 x:16383 y:0 buttons:1 mode:0]; auto ignored = [self diskCommand:^(VCCMachineCompletion done) { [c snapshotWithCompletion:done]; }]; XCTAssertEqual(ignored.joystickInputs[0].x, 8191u); XCTAssertEqual(ignored.joystickInputs[0].buttons, 0u); XCTAssertEqual(ignored.frames, paused.frames); XCTAssertEqual(ignored.cycles, paused.cycles); XCTAssertFalse(ignored.keyboardInputHeld); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } } - (void)testDriveWireOptInPreservesRunningMachine { auto controller = [VCCMachineController new]; auto initial = [self diskCommand:^(VCCMachineCompletion done) { [controller loadDiagnosticWithCompletion:done]; }]; XCTAssertFalse(initial.driveWireEnabled); auto running = [self diskCommand:^(VCCMachineCompletion done) { [controller resumeWithCompletion:done]; }]; for (BOOL enabled : {YES, NO}) { auto updated = [self diskCommand:^(VCCMachineCompletion done) { [controller setDriveWireEnabled:enabled completion:done]; }]; XCTAssertNil(updated.errorMessage); XCTAssertEqual(updated.driveWireEnabled, enabled); XCTAssertEqual(updated.state, VCCMachineStateRunning); XCTAssertEqual(updated.generation, running.generation); } [self diskCommand:^(VCCMachineCompletion done) { [controller shutdownWithCompletion:done]; }]; } - (void)testMacShellGrantEndsOnResetOrNetworkingOff { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; auto denied = [self diskCommand:^(VCCMachineCompletion done) { [c startMacShellWithCompletion:done]; }]; XCTAssertNotNil(denied.errorMessage); [self diskCommand:^(VCCMachineCompletion done) { [c setDriveWireEnabled:YES completion:done]; }]; for (bool reset : {true, false}) { auto armed = [self diskCommand:^(VCCMachineCompletion done) { [c startMacShellWithCompletion:done]; }]; XCTAssertEqualObjects(armed.macShellStatus, @"ready"); XCTAssertEqual(armed.macShellRequestID, 0u); auto ended = [self diskCommand:^(VCCMachineCompletion done) { if (reset) [c softResetWithCompletion:done]; else [c setDriveWireEnabled:NO completion:done]; }]; XCTAssertEqualObjects(ended.macShellStatus, @"idle"); auto stale = [self diskCommand:^(VCCMachineCompletion done) { [c decideMacShellRequest:42 allow:YES completion:done]; }]; XCTAssertNotNil(stale.errorMessage); XCTAssertEqualObjects(stale.macShellStatus, @"idle"); } [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testPrinterControlsKeepRunningAudioClock { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c setAudioEnabled:YES volume:0 completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; auto first = [self diskCommand:^(VCCMachineCompletion done) { [self waitForAudio:c attempts:250 completion:done]; }]; XCTAssertNil(first.errorMessage); XCTAssertGreaterThan(first.audioStarts, 0u); XCTAssertTrue(first.audioEnabled); XCTAssertEqual(first.state, VCCMachineStateRunning); for (unsigned i = 0; i < 6; ++i) { auto before = [self diskCommand:^(VCCMachineCompletion done) { [c snapshotWithCompletion:done]; }]; auto changed = [self diskCommand:^(VCCMachineCompletion done) { if (i % 3 == 2) [c clearPrinterOutputWithCompletion:done]; else [c setPrinterEnabled:i % 3 == 0 completion:done]; }]; XCTAssertNil(changed.errorMessage); XCTAssertEqual(changed.state, VCCMachineStateRunning); XCTAssertEqual(changed.generation, first.generation); XCTAssertEqual(changed.audioStarts, first.audioStarts); auto later = [self diskCommand:^(VCCMachineCompletion done) { [self waitForPrinterFrames:c after:changed.frames attempts:250 completion:done]; }]; XCTAssertNil(later.errorMessage); XCTAssertEqual(later.state, VCCMachineStateRunning); XCTAssertGreaterThan(later.frames, changed.frames); XCTAssertGreaterThan(later.cycles, before.cycles); XCTAssertEqual(later.generation, first.generation); XCTAssertEqual(later.audioStarts, first.audioStarts); XCTAssertGreaterThan(later.audioConsumed, before.audioConsumed); XCTAssertTrue(later.audioEnabled); } [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; auto rejected = [self diskCommand:^(VCCMachineCompletion done) { [c setPrinterEnabled:YES completion:done]; }]; XCTAssertNotNil(rejected.errorMessage); XCTAssertEqual(rejected.state, VCCMachineStateClosed); rejected = [self diskCommand:^(VCCMachineCompletion done) { [c clearPrinterOutputWithCompletion:done]; }]; XCTAssertNotNil(rejected.errorMessage); XCTAssertEqual(rejected.state, VCCMachineStateClosed); } - (void)testPrinterBridgeCapturesGuestAndPreservesSession { auto done = [self expectationWithDescription:@"Printer output is immutable and independent of machine lifetime"]; auto controller = [VCCMachineController new]; std::vector rom(0x8000, 0x12); std::vector code{0x86,0x04,0xb7,0xff,0x21}; auto sample = [&](unsigned bit) { code.insert(code.end(), {0x86,uint8_t(bit << 1),0xb7,0xff,0x20}); }; const uint8_t message[] = {'P','R','I','N','T',0x0d}; for (auto byte : message) { sample(1); sample(0); for (unsigned bit = 0; bit < 8; ++bit) sample((byte >> bit) & 1); sample(1); } code.insert(code.end(), {0x20,0xfe}); std::copy(code.begin(), code.end(), rom.begin()); rom[0x7ffe] = 0x80; rom[0x7fff] = 0; NSData* expected = [NSData dataWithBytes:message length:sizeof(message)]; [controller loadROM:[NSData dataWithBytes:rom.data() length:rom.size()] use6309:NO completion:^(VCCMachineSnapshot* loaded) { XCTAssertFalse(loaded.printerEnabled); XCTAssertEqual(loaded.printerByteCount, 0u); }]; [controller setPrinterEnabled:YES completion:^(VCCMachineSnapshot* enabled) { XCTAssertTrue(enabled.printerEnabled); XCTAssertEqual(enabled.state, VCCMachineStatePaused); }]; [controller resumeWithCompletion:^(VCCMachineSnapshot* running) { [self waitForFrame:controller attempts:200 completion:^(VCCMachineSnapshot* frame) { [controller pauseWithCompletion:^(VCCMachineSnapshot* paused) { XCTAssertEqual(paused.printerByteCount, sizeof(message)); XCTAssertEqual(paused.printerDroppedBytes, 0u); [controller printerOutputWithCompletion:^(VCCPrinterOutput* output, VCCMachineSnapshot* read) { XCTAssertTrue([NSThread isMainThread]); XCTAssertEqualObjects(output.bytes, expected); XCTAssertEqual(output.revision, read.printerRevision); XCTAssertTrue(output.enabled); XCTAssertEqual(output.capacity, 1024u * 1024u); XCTAssertEqual(read.generation, paused.generation); XCTAssertEqual(read.cycles, paused.cycles); XCTAssertEqual(read.audioStarts, paused.audioStarts); [controller clearPrinterOutputWithCompletion:^(VCCMachineSnapshot* cleared) { XCTAssertEqual(cleared.printerByteCount, 0u); XCTAssertTrue(cleared.printerEnabled); XCTAssertGreaterThan(cleared.printerRevision, read.printerRevision); XCTAssertEqual(cleared.generation, paused.generation); XCTAssertEqual(cleared.cycles, paused.cycles); XCTAssertEqualObjects(output.bytes, expected); [controller setPrinterEnabled:NO completion:^(VCCMachineSnapshot* stopped) { XCTAssertFalse(stopped.printerEnabled); [done fulfill]; }]; }]; }]; }]; }]; }]; [self waitForExpectations:@[done] timeout:10]; [self diskCommand:^(VCCMachineCompletion completion) { [controller shutdownWithCompletion:completion]; }]; } - (void)waitForFrame:(VCCMachineController *)controller attempts:(NSUInteger)attempts completion:(VCCMachineCompletion)completion { [controller snapshotWithCompletion:^(VCCMachineSnapshot *snapshot) { if (snapshot.frames > 0 || snapshot.errorMessage || attempts == 0) { completion(snapshot); return; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ [self waitForFrame:controller attempts:attempts - 1 completion:completion]; }); }]; } - (void)testControllerExecutionAndShutdown { auto done = [self expectationWithDescription:@"Ordered lifecycle and immutable frame delivery"]; auto controller = [VCCMachineController new]; [controller loadDiagnosticWithCompletion:^(VCCMachineSnapshot *loaded) { XCTAssertTrue([NSThread isMainThread]); XCTAssertEqual(loaded.state, VCCMachineStatePaused); XCTAssertNil(loaded.errorMessage); [controller resumeWithCompletion:^(VCCMachineSnapshot *running) { XCTAssertEqual(running.state, VCCMachineStateRunning); [self waitForFrame:controller attempts:200 completion:^(VCCMachineSnapshot *rendered) { XCTAssertGreaterThan(rendered.frames, 0u); XCTAssertNil(rendered.errorMessage); XCTAssertEqual(rendered.pixels.length, 640u * 480u * 4u); NSData *saved = rendered.pixels; [controller pauseWithCompletion:^(VCCMachineSnapshot *paused) { XCTAssertEqual(paused.state, VCCMachineStatePaused); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 50 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ [controller snapshotWithCompletion:^(VCCMachineSnapshot *still) { XCTAssertEqual(still.frames, paused.frames); XCTAssertEqual(still.cycles, paused.cycles); [controller resetWithCompletion:^(VCCMachineSnapshot *reset) { XCTAssertEqual(reset.state, VCCMachineStatePaused); XCTAssertEqual(reset.frames, 0u); XCTAssertNil(reset.pixels); XCTAssertGreaterThan(reset.generation, paused.generation); [controller shutdownWithCompletion:^(VCCMachineSnapshot *closed) { XCTAssertEqual(closed.state, VCCMachineStateClosed); XCTAssertNil(closed.pixels); const uint32_t *pixels = static_cast(saved.bytes); if (saved.length == 640 * 480 * 4) XCTAssertEqual(pixels[50 * 640], 0xff0000u); [controller resumeWithCompletion:^(VCCMachineSnapshot *rejected) { XCTAssertEqual(rejected.state, VCCMachineStateClosed); XCTAssertNotNil(rejected.errorMessage); [done fulfill]; }]; }]; }]; }]; }); }]; }]; }]; }]; [self waitForExpectations:@[done] timeout:10]; } - (void)testQueuedCommandsAndInvalidInput { auto done = [self expectationWithDescription:@"FIFO commands"]; auto controller = [VCCMachineController new]; NSMutableArray *order = [NSMutableArray new]; [controller loadDiagnosticWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertNil(s.errorMessage); [order addObject:@"load"]; }]; [controller resumeWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStateRunning); [order addObject:@"run"]; }]; [controller pauseWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStatePaused); [order addObject:@"pause"]; }]; [controller loadROM:[NSData data] use6309:NO completion:^(VCCMachineSnapshot *s) { XCTAssertNotNil(s.errorMessage); XCTAssertEqual(s.state, VCCMachineStatePaused); [order addObject:@"invalid"]; }]; [controller shutdownWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqualObjects(order, (@[@"load", @"run", @"pause", @"invalid"])); XCTAssertEqual(s.state, VCCMachineStateClosed); [done fulfill]; }]; [self waitForExpectations:@[done] timeout:5]; } - (void)testControllerDeallocationReleasesOwnership { auto loaded = [self expectationWithDescription:@"Load first"]; __block VCCMachineController *first = [VCCMachineController new]; [first loadDiagnosticWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertNil(s.errorMessage); [loaded fulfill]; }]; [self waitForExpectations:@[loaded] timeout:5]; first = nil; // No explicit shutdown; destructor must drain/release the machine. auto done = [self expectationWithDescription:@"Load replacement"]; auto next = [VCCMachineController new]; [next loadDiagnosticWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertNil(s.errorMessage); XCTAssertEqual(s.state, VCCMachineStatePaused); [next shutdownWithCompletion:^(VCCMachineSnapshot *closed) { [done fulfill]; }]; }]; [self waitForExpectations:@[done] timeout:5]; } - (void)verifySoftResetWith6309:(BOOL)use6309 { auto done = [self expectationWithDescription:@"Soft reset through serialized bridge"]; auto controller = [VCCMachineController new]; __block uint64_t generation = 0; [controller softResetWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStateEmpty); XCTAssertNotNil(s.errorMessage); }]; [controller loadDiagnosticWith6309:use6309 completion:^(VCCMachineSnapshot *s) { XCTAssertNil(s.errorMessage); generation = s.generation; }]; [controller resumeWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStateRunning); }]; [controller softResetWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStateRunning); XCTAssertNil(s.errorMessage); XCTAssertEqual(s.frames, 0u); XCTAssertEqual(s.cycles, 0u); XCTAssertNil(s.pixels); XCTAssertEqual(s.programCounter, 0x8000); XCTAssertGreaterThan(s.generation, generation); generation = s.generation; }]; [controller pauseWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStatePaused); }]; [controller softResetWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.state, VCCMachineStatePaused); XCTAssertEqual(s.cycles, 0u); XCTAssertGreaterThan(s.generation, generation); XCTAssertNil(s.errorMessage); }]; [controller resumeWithCompletion:^(VCCMachineSnapshot *s) { [self waitForFrame:controller attempts:200 completion:^(VCCMachineSnapshot *frame) { XCTAssertGreaterThan(frame.frames, 0u); XCTAssertNil(frame.errorMessage); [controller shutdownWithCompletion:^(VCCMachineSnapshot *closed) { [done fulfill]; }]; }]; }]; [self waitForExpectations:@[done] timeout:10]; } - (void)test6809SoftReset { [self verifySoftResetWith6309:NO]; } - (void)test6309SoftReset { [self verifySoftResetWith6309:YES]; } - (void)testCassetteBridgeTransportAndExport { auto done=[self expectationWithDescription:@"Cassette copy and transport"]; auto controller=[VCCMachineController new]; const uint8_t bytes[]={0x55,0x3c,0xa5}; NSData *source=[NSData dataWithBytes:bytes length:3]; [controller mountTape:source wav:NO completion:^(VCCMachineSnapshot *s) { XCTAssertTrue(s.tapeMounted); XCTAssertEqual(s.tapeLength,3u); XCTAssertNil(s.errorMessage); }]; [controller setTapeFast:YES completion:^(VCCMachineSnapshot *s) { XCTAssertTrue(s.tapeFast); }]; [controller tapeAction:1 completion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.tapeMode,1); }]; [controller tapeAction:2 completion:^(VCCMachineSnapshot *s) { XCTAssertEqual(s.tapeMode,0); XCTAssertEqual(s.tapePosition,0u); }]; [controller mountTape:source wav:YES completion:^(VCCMachineSnapshot *s) { XCTAssertNotNil(s.errorMessage); XCTAssertEqual(s.tapeLength,3u); }]; [controller exportTapeWithCompletion:^(NSData *data,VCCMachineSnapshot *s) { XCTAssertEqualObjects(data,source); XCTAssertNil(s.errorMessage); [controller discardTapeWithCompletion:^(VCCMachineSnapshot *discarded) { XCTAssertFalse(discarded.tapeMounted); XCTAssertFalse(discarded.tapeDirty); [controller shutdownWithCompletion:^(VCCMachineSnapshot *closed) { [done fulfill]; }]; }]; }]; [self waitForExpectations:@[done] timeout:5]; } - (void)waitForAudio:(VCCMachineController*)controller attempts:(NSUInteger)attempts completion:(VCCMachineCompletion)completion { [controller snapshotWithCompletion:^(VCCMachineSnapshot *s) { if(s.audioConsumed>=4410 || s.errorMessage || attempts==0) { completion(s); return; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW,20*NSEC_PER_MSEC),dispatch_get_main_queue(),^{ [self waitForAudio:controller attempts:attempts-1 completion:completion]; }); }]; } - (void)testVolumeChangesKeepAudioClockRunning { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c setAudioEnabled:YES volume:0 completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; auto first = [self diskCommand:^(VCCMachineCompletion done) { [self waitForAudio:c attempts:250 completion:done]; }]; XCTAssertNil(first.errorMessage); XCTAssertGreaterThan(first.audioStarts, 0u); for (unsigned i = 0; i < 20; ++i) { auto changed = [self diskCommand:^(VCCMachineCompletion done) { [c setAudioEnabled:YES volume:i % 2 ? 0 : 0.25 completion:done]; }]; XCTAssertNil(changed.errorMessage); XCTAssertEqual(changed.audioStarts, first.audioStarts); XCTAssertGreaterThanOrEqual(changed.audioConsumed, first.audioConsumed); } auto changedDisplay = [self diskCommand:^(VCCMachineCompletion done) { [c setVideoOutput:1 completion:done]; }]; XCTAssertEqual(changedDisplay.videoOutput, 1); XCTAssertNil(changedDisplay.errorMessage); XCTAssertEqual(changedDisplay.audioStarts, first.audioStarts); XCTAssertEqual(changedDisplay.generation, first.generation); auto liveSettings = [self diskCommand:^(VCCMachineCompletion done) { [c configureMachineWith6309:NO ram:1 overclock:3 frameInterval:4 fastForward:NO completion:done]; }]; XCTAssertNil(liveSettings.errorMessage); XCTAssertEqual(liveSettings.state, VCCMachineStateRunning); XCTAssertEqual(liveSettings.audioStarts, first.audioStarts); XCTAssertEqual(liveSettings.generation, first.generation); auto off = [self diskCommand:^(VCCMachineCompletion done) { [c setAudioEnabled:NO volume:0 completion:done]; }]; auto delay = [self expectationWithDescription:@"Silent pacing continues"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200*NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ [delay fulfill]; }); [self waitForExpectations:@[delay] timeout:5]; auto silent = [self diskCommand:^(VCCMachineCompletion done) { [c snapshotWithCompletion:done]; }]; XCTAssertGreaterThan(silent.frames, off.frames); XCTAssertLessThan(silent.frames - off.frames, 30u); XCTAssertEqual(silent.audioConsumed, off.audioConsumed); [self diskCommand:^(VCCMachineCompletion done) { [c setAudioEnabled:YES volume:0 completion:done]; }]; auto resumed = [self expectationWithDescription:@"Output restarts"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 200*NSEC_PER_MSEC), dispatch_get_main_queue(), ^{ [resumed fulfill]; }); [self waitForExpectations:@[resumed] timeout:5]; auto on = [self diskCommand:^(VCCMachineCompletion done) { [c snapshotWithCompletion:done]; }]; XCTAssertNil(on.errorMessage); XCTAssertTrue(on.audioEnabled); XCTAssertEqual(on.audioStarts, first.audioStarts + 1); XCTAssertGreaterThan(on.audioConsumed, silent.audioConsumed); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testNativeAudioPauseAndRestart { auto done=[self expectationWithDescription:@"Silent native output lifecycle"]; auto controller=[VCCMachineController new]; [controller setAudioEnabled:YES volume:0 completion:^(VCCMachineSnapshot *s) { XCTAssertTrue(s.audioEnabled); }]; [controller loadDiagnosticWithCompletion:^(VCCMachineSnapshot *s) { XCTAssertNil(s.errorMessage); }]; [controller resumeWithCompletion:^(VCCMachineSnapshot *s) { [self waitForAudio:controller attempts:250 completion:^(VCCMachineSnapshot *playing) { XCTAssertGreaterThanOrEqual(playing.audioConsumed,4410u, @"Audio error: %@; starts: %llu; enabled: %@", playing.errorMessage, (unsigned long long)playing.audioStarts, playing.audioEnabled ? @"yes" : @"no"); [controller pauseWithCompletion:^(VCCMachineSnapshot *paused) { XCTAssertNil(paused.errorMessage); XCTAssertTrue(paused.audioEnabled); XCTAssertGreaterThan(paused.frames,3u); dispatch_after(dispatch_time(DISPATCH_TIME_NOW,100*NSEC_PER_MSEC),dispatch_get_main_queue(),^{ [controller snapshotWithCompletion:^(VCCMachineSnapshot *still) { XCTAssertEqual(still.frames,paused.frames); XCTAssertEqual(still.audioUnderruns,paused.audioUnderruns); [controller resumeWithCompletion:^(VCCMachineSnapshot *s) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW,200*NSEC_PER_MSEC),dispatch_get_main_queue(),^{ [controller snapshotWithCompletion:^(VCCMachineSnapshot *running) { XCTAssertNil(running.errorMessage); XCTAssertGreaterThan(running.frames,paused.frames); [controller shutdownWithCompletion:^(VCCMachineSnapshot *closed) { [done fulfill]; }]; }]; }); }]; }]; }); }]; }]; }]; [self waitForExpectations:@[done] timeout:10]; } - (VCCMachineSnapshot*)diskCommand:(void (^)(VCCMachineCompletion))operation { auto done = [self expectationWithDescription:@"Disk command completion"]; __block VCCMachineSnapshot *result = nil; operation(^(VCCMachineSnapshot *snapshot) { result = snapshot; [done fulfill]; }); [self waitForExpectations:@[done] timeout:10]; return result; } - (VCCDebugGraphicsCapture*)graphics:(VCCMachineController*)controller current:(BOOL)current physical:(BOOL)physical address:(uint32_t)address mode:(NSInteger)mode { auto done = [self expectationWithDescription:@"Graphics capture on main queue"]; __block VCCDebugGraphicsCapture *result; [controller debuggerGraphicsCurrentPage:current address:address physical:physical mode:mode alternatePalette:NO completion:^(VCCDebugGraphicsCapture *capture) { XCTAssertTrue(NSThread.isMainThread); result = capture; [done fulfill]; }]; [self waitForExpectations:@[done] timeout:10]; return result; } - (void)testGraphicsPreviewCapturesImmutablePixelsAndByteLocations { auto c = [VCCMachineController new]; auto initial = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; auto current = [self graphics:c current:YES physical:NO address:0 mode:0]; XCTAssertNil(current.errorMessage); XCTAssertTrue(current.physical); XCTAssertEqual(current.machine.cycles, initial.cycles); XCTAssertEqual(current.machine.programCounter, initial.programCounter); XCTAssertEqual(current.machine.generation, initial.generation); auto writeByte = [&](uint16_t address, uint8_t value) { auto s = [self diskCommand:^(VCCMachineCompletion done) { [c debuggerWriteMemoryAtAddress:address data:[NSData dataWithBytes:&value length:1] completion:done]; }]; XCTAssertNil(s.errorMessage); }; writeByte(0x4001, 0xa0); auto before = [self graphics:c current:NO physical:NO address:0x4000 mode:5]; XCTAssertNil(before.errorMessage); XCTAssertFalse(before.physical); XCTAssertEqual(before.width, 256); XCTAssertEqual(before.height, 192); XCTAssertEqual(before.rgba.length, 256u * 192u * 4u); XCTAssertNil([before pixelAtX:-1 y:0]); XCTAssertNil([before pixelAtX:0 y:192]); auto high = [before pixelAtX:8 y:0]; XCTAssertEqual(high.address, 0x4001u); XCTAssertEqual(high.value, 0xa0); XCTAssertEqual(high.mask, 0x80); XCTAssertTrue(high.available); XCTAssertNil(high.attributeAddress); XCTAssertEqual([before pixelAtX:10 y:0].mask, 0x20); XCTAssertEqual([before pixelAtX:8 y:1].address, 0x4021u); writeByte(0x4001, 0x40); auto after = [self graphics:c current:NO physical:NO address:0x4000 mode:5]; XCTAssertTrue([after hasSameLayoutAs:before]); XCTAssertEqual([before pixelAtX:8 y:0].value, 0xa0); XCTAssertEqual([after pixelAtX:8 y:0].value, 0x40); auto ram = [self graphics:c current:NO physical:YES address:0x4000 mode:5]; XCTAssertNil(ram.errorMessage); XCTAssertFalse([after hasSameLayoutAs:ram]); [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; auto live = [self graphics:c current:YES physical:NO address:0 mode:0]; XCTAssertNil(live.errorMessage); XCTAssertEqual(live.machine.state, VCCMachineStateRunning); XCTAssertEqual(live.machine.generation, initial.generation); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testGraphicsPreviewRejectsOverflowAndMarksUnavailableMappedIO { auto c = [VCCMachineController new]; auto empty = [self graphics:c current:YES physical:NO address:0 mode:0]; XCTAssertNotNil(empty.errorMessage); XCTAssertNil([empty pixelAtX:0 y:0]); auto initial = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; for (BOOL physical : {NO, YES}) { auto invalid = [self graphics:c current:NO physical:physical address:UINT32_MAX mode:5]; XCTAssertNotNil(invalid.errorMessage); XCTAssertEqual(invalid.rgba.length, 0u); XCTAssertNil(invalid.machine.errorMessage); XCTAssertEqual(invalid.machine.cycles, initial.cycles); } auto invalidMode = [self graphics:c current:NO physical:YES address:0 mode:10]; XCTAssertNotNil(invalidMode.errorMessage); // A 128 × 96 one-bit page is 1536 bytes; this base puts $FF00 at y=80. auto io = [self graphics:c current:NO physical:NO address:0xfa00 mode:1]; XCTAssertNil(io.errorMessage); XCTAssertEqual(io.height, 96); XCTAssertEqual([io pixelAtX:0 y:80].address, 0xff00u); XCTAssertFalse([io pixelAtX:0 y:80].available); XCTAssertTrue([io pixelAtX:0 y:79].available); XCTAssertEqual(io.machine.cycles, initial.cycles); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testDiskBridgeLifecycleAndProtection { auto c = [VCCMachineController new]; NSData *rom = [NSMutableData dataWithLength:8192]; auto s = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; XCTAssertEqual(s.disks.count, 4u); XCTAssertFalse(s.diskEnabled); s = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSData data] name:@"bad" completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertFalse(s.diskEnabled); s = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:rom name:@"authored.rom" completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.diskEnabled); XCTAssertEqual(s.state, VCCMachineStatePaused); for (NSInteger drive = 0; drive < 4; ++drive) { s = [self diskCommand:^(VCCMachineCompletion done) { [c createDiskInDrive:drive completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.disks[drive].mounted); XCTAssertTrue(s.disks[drive].dirty); XCTAssertEqual(s.disks[drive].byteCount, 161280u); } VCCMachineSnapshot *immutable = s; s = [self diskCommand:^(VCCMachineCompletion done) { [c disableDisksWithCompletion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.diskEnabled); s = [self diskCommand:^(VCCMachineCompletion done) { [c ejectDiskInDrive:0 discard:NO completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[0].dirty); __block NSData *exported = nil; __block uint64_t token = 0; s = [self diskCommand:^(VCCMachineCompletion done) { [c exportDiskInDrive:0 completion:^(NSData *data, uint64_t value, VCCMachineSnapshot *snapshot) { exported = data; token = value; done(snapshot); }]; }]; XCTAssertEqual(exported.length, 161280u); XCTAssertTrue(s.disks[0].dirty); XCTAssertEqual(s.state,VCCMachineStatePaused); s = [self diskCommand:^(VCCMachineCompletion done) { [c markDiskSavedInDrive:0 token:token+1 name:@"wrong.dsk" completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[0].dirty); s = [self diskCommand:^(VCCMachineCompletion done) { [c markDiskSavedInDrive:0 token:token name:@"Disk é.DSK" completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertFalse(s.disks[0].dirty); XCTAssertEqualObjects(s.disks[0].name,@"Disk é.DSK"); XCTAssertTrue(immutable.disks[0].dirty, @"Published drive snapshots must not mutate"); s = [self diskCommand:^(VCCMachineCompletion done) { [c mountDisk:[NSData data] drive:0 name:@"broken.dsk" readOnly:NO completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertEqualObjects(s.disks[0].name,@"Disk é.DSK"); s = [self diskCommand:^(VCCMachineCompletion done) { [c setDiskWriteProtected:YES drive:0 completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.disks[0].writeProtected); s = [self diskCommand:^(VCCMachineCompletion done) { [c setDiskWriteProtected:NO drive:0 completion:done]; }]; XCTAssertFalse(s.disks[0].writeProtected); s = [self diskCommand:^(VCCMachineCompletion done) { [c mountDisk:exported drive:0 name:@"read-only.dsk" readOnly:YES completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.disks[0].sourceReadOnly); XCTAssertTrue(s.disks[0].writeProtected); s = [self diskCommand:^(VCCMachineCompletion done) { [c setDiskWriteProtected:NO drive:0 completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[0].writeProtected); s = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:rom name:@"replacement.rom" completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertEqualObjects(s.diskROMName,@"replacement.rom"); XCTAssertTrue(s.disks[0].mounted); XCTAssertTrue(s.disks[1].dirty); s = [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSData data] name:@"invalid" completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertEqualObjects(s.diskROMName,@"replacement.rom"); for (NSInteger drive : {-1,4}) { s = [self diskCommand:^(VCCMachineCompletion done) { [c createDiskInDrive:drive completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertEqual(s.disks.count,4u); } for (NSInteger drive = 0; drive < 4; ++drive) { s = [self diskCommand:^(VCCMachineCompletion done) { [c ejectDiskInDrive:drive discard:YES completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertFalse(s.disks[drive].mounted); } s = [self diskCommand:^(VCCMachineCompletion done) { [c disableDisksWithCompletion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertFalse(s.diskEnabled); XCTAssertEqual(s.state,VCCMachineStatePaused); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testVDKExtendedHeaderProtectionAndFailedDiskReplacement { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiagnosticWithCompletion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSMutableData dataWithLength:8192] name:@"authored.rom" completion:done]; }]; auto vdk = [NSMutableData dataWithLength:256 + 18 * 256]; const uint8_t header[] = {'d','k',0,1,0x10,0x10,0,0,1,1,1,0}; memcpy(vdk.mutableBytes, header, sizeof(header)); auto s = [self diskCommand:^(VCCMachineCompletion done) { [c mountDisk:vdk drive:2 name:@"Archive.DSK" readOnly:NO completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertEqualObjects(s.disks[2].floppyExtension, @"vdk"); XCTAssertTrue(s.disks[2].formatWriteProtected); XCTAssertTrue(s.disks[2].writeProtected); XCTAssertFalse(s.disks[2].sourceReadOnly); s = [self diskCommand:^(VCCMachineCompletion done) { [c setDiskWriteProtected:NO drive:2 completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[2].writeProtected); s = [self diskCommand:^(VCCMachineCompletion done) { [c mountDisk:vdk drive:2 name:@"Wrong.dmk" readOnly:NO completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertEqualObjects(s.disks[2].name, @"Archive.DSK"); [self diskCommand:^(VCCMachineCompletion done) { [c exportDiskInDrive:2 completion:^(NSData *data, uint64_t token, VCCMachineSnapshot *snapshot) { XCTAssertEqualObjects(data, vdk); done(snapshot); }]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (void)testCreateFloppyImageInSelectedFormat { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSMutableData dataWithLength:8192] name:@"authored.rom" completion:done]; }]; NSInteger drive = 0; for (VCCFloppyFormat format : {VCCFloppyFormatStandard, VCCFloppyFormatTrackDMK, VCCFloppyFormatSectorVDK}) { auto s = [self diskCommand:^(VCCMachineCompletion done) { [c createDiskInDrive:drive format:format completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.disks[drive].dirty); XCTAssertEqualObjects(s.disks[drive].floppyExtension, (@[@"dsk", @"dmk", @"vdk"])[drive]); XCTAssertEqual(s.disks[drive].tracks, 35u); ++drive; } [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } - (NSData*)diskWriterROM { auto rom = [NSMutableData dataWithLength:32768]; // Authored 6809/6309 program: write 256 bytes to sector 1, trigger deferred // commit with one final data-register access, then loop. No disk ROM needed. const uint8_t program[] = { 0x86,0x29,0xb7,0xff,0x40, 0x86,1,0xb7,0xff,0x4a, 0x86,0xa0,0xb7,0xff,0x48, 0x8e,1,0, 0x86,0x5a, 0xb7,0xff,0x4b,0x30,0x1f,0x26,0xf9,0xb7,0xff,0x4b,0x20,0xfe }; memcpy(rom.mutableBytes, program, sizeof(program)); auto* bytes = static_cast(rom.mutableBytes); bytes[32766] = 0x80; return rom; } - (void)testDiskExportRejectsNewWritesAndReplacement { for (BOOL use6309 : {NO,YES}) { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSMutableData dataWithLength:8192] name:@"authored" completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c createDiskInDrive:0 completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c loadROM:[self diskWriterROM] use6309:use6309 completion:done]; }]; __block uint64_t token = 0; __block NSData *before = nil; [self diskCommand:^(VCCMachineCompletion done) { [c exportDiskInDrive:0 completion:^(NSData *data,uint64_t value,VCCMachineSnapshot *s) { token=value; before=data; done(s); }]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [self waitForFrame:c attempts:200 completion:done]; }]; auto s = [self diskCommand:^(VCCMachineCompletion done) { [c pauseWithCompletion:done]; }]; XCTAssertFalse(s.diskBusy); XCTAssertTrue(s.disks[0].dirty); s = [self diskCommand:^(VCCMachineCompletion done) { [c markDiskSavedInDrive:0 token:token name:@"stale.dsk" completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[0].dirty); __block NSData *after = nil; [self diskCommand:^(VCCMachineCompletion done) { [c exportDiskInDrive:0 completion:^(NSData *data,uint64_t value,VCCMachineSnapshot *s) { token=value; after=data; done(s); }]; }]; XCTAssertNotEqualObjects(before,after); XCTAssertEqual(static_cast(after.bytes)[0],0x5a); [self diskCommand:^(VCCMachineCompletion done) { [c ejectDiskInDrive:0 discard:YES completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c mountDisk:after drive:0 name:@"replacement.dsk" readOnly:NO completion:done]; }]; s = [self diskCommand:^(VCCMachineCompletion done) { [c markDiskSavedInDrive:0 token:token name:@"wrong.dsk" completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertEqualObjects(s.disks[0].name,@"replacement.dsk"); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } } - (void)testDiskBusyControllerRejectsMediaChanges { auto c = [VCCMachineController new]; [self diskCommand:^(VCCMachineCompletion done) { [c loadDiskROM:[NSMutableData dataWithLength:8192] name:@"authored" completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c createDiskInDrive:0 completion:done]; }]; auto rom = [NSMutableData dataWithLength:32768]; const uint8_t program[] = {0x86,1,0xb7,0xff,0x40,0x86,0x10,0xb7,0xff,0x48,0x20,0xfe}; // motor off, pending seek memcpy(rom.mutableBytes,program,sizeof(program)); static_cast(rom.mutableBytes)[32766]=0x80; [self diskCommand:^(VCCMachineCompletion done) { [c loadROM:rom use6309:NO completion:done]; }]; [self diskCommand:^(VCCMachineCompletion done) { [c resumeWithCompletion:done]; }]; auto s = [self diskCommand:^(VCCMachineCompletion done) { [self waitForFrame:c attempts:200 completion:done]; }]; XCTAssertTrue(s.diskBusy); const auto generation = s.generation; s = [self diskCommand:^(VCCMachineCompletion done) { [c configureMachineWith6309:YES ram:0 overclock:1 frameInterval:1 fastForward:YES completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.diskBusy); XCTAssertFalse(s.fastForward); XCTAssertFalse(s.use6309); XCTAssertEqual(s.ramConfiguration, 1u); XCTAssertEqual(s.generation, generation); s = [self diskCommand:^(VCCMachineCompletion done) { [c pauseForDiskChangeWithCompletion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.diskBusy); s = [self diskCommand:^(VCCMachineCompletion done) { [c ejectDiskInDrive:0 discard:YES completion:done]; }]; XCTAssertNotNil(s.errorMessage); XCTAssertTrue(s.disks[0].mounted); [self diskCommand:^(VCCMachineCompletion done) { [c pauseWithCompletion:done]; }]; s = [self diskCommand:^(VCCMachineCompletion done) { [c softResetWithCompletion:done]; }]; XCTAssertFalse(s.diskBusy); XCTAssertTrue(s.disks[0].dirty); s = [self diskCommand:^(VCCMachineCompletion done) { [c configureMachineWith6309:YES ram:0 overclock:1 frameInterval:1 fastForward:NO completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertTrue(s.use6309); XCTAssertEqual(s.ramConfiguration, 0u); XCTAssertTrue(s.disks[0].mounted); XCTAssertTrue(s.disks[0].dirty); XCTAssertTrue(s.diskConnected); s = [self diskCommand:^(VCCMachineCompletion done) { [c ejectDiskInDrive:0 discard:YES completion:done]; }]; XCTAssertNil(s.errorMessage); XCTAssertFalse(s.disks[0].mounted); [self diskCommand:^(VCCMachineCompletion done) { [c shutdownWithCompletion:done]; }]; } @end