// SPDX-License-Identifier: GPL-3.0-or-later #include #include #include #define COUNT 512 #define CHECK(test) do { if (!(test)) { printf("FAIL LINE %u\n", (unsigned)__LINE__); return 1; } } while (0) static struct orch90_frame source[COUNT]; static byte savedVector[3]; asm byte interruptMasks(void) { asm { tfr cc,b andb #$50 } } // Same timed instruction loop before and after playback, starting at a // vertical sync. This detects returning to BASIC at twice/half its old speed. asm unsigned int speedProbe(void) { asm { sync ldd >$0112 addd #3 ldx #0 @count leax 1,x cmpd >$0112 bne @count tfr x,d } } static byte refusedForeignVector(byte clock) { // JMP $010C reaches the same handler as BASIC's stock LBRA $010C. // It is safe to execute yet a visibly different owner's vector. byte result; asm("orcc", "#$10"); *(byte *)0xfef7 = 0x7e; *(unsigned *)0xfef8 = 0x010c; asm("andcc", "#$ef"); result = !orch90_start(source, COUNT, 0, clock); asm("orcc", "#$10"); memcpy((void *)0xfef7, savedVector, sizeof(savedVector)); asm("andcc", "#$ef"); return result; } asm byte control(void) { asm { ldb >$ff01 andb #$3f } } // Keep the stock IRQ entry, but give the already enabled HSYNC source a // temporary consumer. A refused start must leave this user's PIA bit alone. static asm __norts__ void occupiedHS(void) { asm { tst >$ff00 tst >$ff02 rti } } static byte refusedBusyHS(byte clock) { byte normalControl = control(), jump[3], result; setHighSpeed(TRUE); asm { sync } asm { sync } asm("orcc", "#$10"); memcpy(jump, (const void *)0x010c, 3); *(byte *)0x010c = 0x7e; *(unsigned *)0x010d = (unsigned)occupiedHS; *(byte *)0xff01 = normalControl | 1; asm("andcc", "#$ef"); result = !orch90_start(source, COUNT, 0, ORCH90_CLOCK_FAST); asm("orcc", "#$10"); if (!(control() & 1)) result = 0; *(byte *)0xff01 = normalControl; memcpy((void *)0x010c, jump, 3); asm("andcc", "#$ef"); setHighSpeed(clock == ORCH90_CLOCK_FAST); return result; } int main(void) { byte clock, masks, initial, vertical, changed, status; unsigned i, before, after, ticks, work = 0; printf("SELECT CLOCK\n"); clock = waitkey(FALSE) == '2' ? ORCH90_CLOCK_FAST : ORCH90_CLOCK_NORMAL; setHighSpeed(clock == ORCH90_CLOCK_FAST); before = speedProbe(); initial = control(); masks = interruptMasks(); memcpy(savedVector, (const void *)0xfef7, sizeof(savedVector)); for (i = 0; i < COUNT; ++i) { byte level = (i & 8) ? 64 : 192; source[i].left = i < COUNT / 2 ? level : 128; source[i].right = i >= COUNT / 2 ? level : 128; } orch90_write(19, 235); CHECK(!orch90_active()); orch90_stop(); // Idle stop must leave another producer's DACs alone. CHECK(!orch90_start(0, 1, 0, clock)); CHECK(!orch90_start(source, 0, 0, clock)); CHECK(!orch90_start((const struct orch90_frame *)0xffff, 1, 0, clock)); CHECK(!orch90_start(source, 1, 2, clock)); CHECK(!orch90_start(source, 1, 0, 2)); asm("orcc", "#$10"); status = orch90_start(source, COUNT, 0, clock); asm("andcc", "#$ef"); CHECK(!status); CHECK(refusedForeignVector(clock)); CHECK(refusedBusyHS(clock)); *(byte *)0xff01 = initial & 0x3b; // The data address now selects a DDR. status = orch90_start(source, COUNT, 0, clock); *(byte *)0xff01 = initial; CHECK(!status); vertical = *(byte *)0xff03 & 63; *(byte *)0xff03 = vertical & 0x3e; // VSYNC disabled: arming would hang. status = orch90_start(source, COUNT, 0, clock); *(byte *)0xff03 = vertical; CHECK(!status); CHECK(!orch90_active()); CHECK(control() == initial && interruptMasks() == masks); CHECK(!memcmp(savedVector, (const void *)0xfef7, sizeof(savedVector))); // Before the second VSYNC arms HSYNC, cancellation releases the vector // without reading any sample or leaving the clock at the borrowed speed. CHECK(orch90_start(source, COUNT, 1, clock)); CHECK(orch90_active()); orch90_stop(); CHECK(!orch90_active() && control() == initial); CHECK(!memcmp(savedVector, (const void *)0xfef7, sizeof(savedVector))); after = speedProbe(); CHECK(after > before - before / 8 && after < before + before / 8); ticks = getTimer(); CHECK(orch90_start(source, COUNT, 0, clock)); CHECK(!orch90_start(source, 1, 1, clock)); while (orch90_active()) ++work; CHECK(work > 16 && (unsigned)(getTimer() - ticks) >= 4); CHECK(!memcmp(savedVector, (const void *)0xfef7, sizeof(savedVector))); CHECK(control() == initial && interruptMasks() == masks); after = speedProbe(); CHECK(after > before - before / 8 && after < before + before / 8); // A one-frame clip completes and a subsequent loop can be stopped at will. CHECK(orch90_start(source, 1, 0, clock)); while (orch90_active()) {} CHECK(orch90_start(source, 16, 1, clock)); ticks = getTimer(); while ((unsigned)(getTimer() - ticks) < 6) ++work; CHECK(orch90_active()); asm("orcc", "#$50"); changed = initial ^ 8; *(byte *)0xff01 = changed | 1; // Foreground changed a PIA field it still owns. orch90_stop(); CHECK(interruptMasks() == 0x50 && control() == changed); *(byte *)0xff01 = initial; asm { lda :masks tfr a,cc } orch90_stop(); CHECK(!orch90_active()); CHECK(!memcmp(savedVector, (const void *)0xfef7, sizeof(savedVector))); CHECK(control() == initial && interruptMasks() == masks); after = speedProbe(); CHECK(after > before - before / 8 && after < before + before / 8); orch90_write(21, 233); setHighSpeed(FALSE); printf("RESTORED\n"); return 0; }