Files

167 lines
4.3 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
#include <vcc/orch90.h>
static const struct orch90_frame *firstFrame, *nextFrame, *endFrame;
static unsigned char playing, looping, previousClock, scanlines, fieldsToStart;
// Called with IRQ masked by the foreground or by hardware interrupt entry.
// Restore just the enable bit we borrowed. Preserve the current mux selection.
static asm void releasePlayback(void)
{
asm {
lda >$ff01
bita #1
beq @vector // An early stop can cancel before HSYNC is armed.
anda #$3e
sta >$ff01
@vector lda #$16 // Original LBRA $010C at the CoCo 3 IRQ vector.
sta >$fef7
ldd #$0212
std >$fef8
ldd #$8080 // The write-only DACs rest at their midpoint.
std >$ff7a
clr :playing
tst :previousClock
bne @done
clr >$ffd8 // Return a previously normal-speed CoCo to 0.89 MHz.
@done
}
}
// A full IRQ stacks D and X on both supported CPUs. Keep this handler in
// resident code, with PC-relative data independent of the foreground's DP.
static asm __norts__ void scanlineIRQ(void)
{
asm {
lda >$ff01
bpl @chain // A different source interrupted the foreground.
lda >$ff00 // Acknowledge the horizontal edge.
dec :scanlines
bne @vertical
lda #4
sta :scanlines
ldx :nextFrame
cmpx :endFrame
bne @sample
tst :looping
beq @end
ldx :firstFrame
@sample ldd ,x++ // Left and right are consecutive DAC addresses.
std >$ff7a
stx :nextFrame
@vertical
lda >$ff03 // BASIC's timer and keyboard still get VSYNC.
bpl @return
bita #1
beq @return
@chain jmp >$010c
@end lbsr releasePlayback
bra @vertical
@return rti
}
}
// Let a requested speed change settle before the first DAC sample. Some
// hosts apply it at their next frame boundary. Only count genuine BASIC
// vertical interrupts, even if another IRQ arrives while we are waiting.
static asm __norts__ void armAtVerticalIRQ(void)
{
asm {
lda >$ff03
bpl @chain
dec :fieldsToStart
bne @chain
leax scanlineIRQ
stx >$fef8
lda >$ff00 // Drop any horizontal edge before sampling.
lda >$ff01
anda #$3f
ora #1
sta >$ff01
@chain jmp >$010c
}
}
unsigned char orch90_start(const struct orch90_frame *frames, unsigned int count,
unsigned char loop, unsigned char clock)
{
unsigned char installed;
if (!frames || !count || count > (0u - (unsigned int)frames) / sizeof(*frames)
|| loop > 1 || clock > ORCH90_CLOCK_FAST)
return 0;
asm {
pshs cc
tfr cc,a
orcc #$50
bita #$10
bne @reject
tst :playing
bne @reject
ldd >$fff8 // Stock CoCo 3 IRQ trampoline: LBRA $010C.
cmpd #$fef7
bne @reject
lda >$fef7
cmpa #$16
bne @reject
ldd >$fef8
cmpd #$0212
bne @reject
lda >$ff01 // HSYNC must be free, with PIA data accessible.
anda #5
cmpa #4
bne @reject
lda >$ff03 // Arm from the ordinary enabled vertical IRQ.
anda #5
cmpa #5
bne @reject
ldx :frames
stx :firstFrame
stx :nextFrame
ldd :count
lslb
rola
leax d,x
stx :endFrame
lda :loop
sta :looping
lda :clock
sta :previousClock
bne @fast
clr >$ffd9 // Allow processing time between horizontal IRQs.
@fast lda #4
sta :scanlines
lda #$7e
sta >$fef7
leax armAtVerticalIRQ
stx >$fef8
lda #2
sta :fieldsToStart
lda #1
sta :playing
bra @done
@reject clra
@done sta :installed
puls cc
}
return installed;
}
asm unsigned char orch90_active(void)
{
asm {
ldb :playing
}
}
asm void orch90_stop(void)
{
asm {
pshs cc
orcc #$50
tst :playing
beq @done
lbsr releasePlayback
@done puls cc
}
}