Files
courier-emu/native/c5x_capi.cpp
T
Cryan209andClaude Opus 5 23b41322f4 The call has one state, and it belongs to the line rather than to either end
Three corrections to the sound path.

The switched call now has a single state that both subscribers share.  Each
end used to decide for itself whether the path was through, from a counter
only it could have: the calling end watched the rings it was sending, the
answering end watched the ring bursts it was receiving, and a leased pair
matched neither rule on the answering side, so B never cut through at all.
The two ends could therefore disagree, which is a half-connected call - one
end transmitting into a cut loop.  `call_state` now crosses the line in every
frame; each end advertises the furthest point it knows of and adopts the later
of its own view and the peer's.  Every transition has exactly one end that can
make it - only the calling end can seize and ring, only the called end can
answer - so this converges instead of oscillating, and `_cut_through` is now a
derived property rather than a per-end flag.

RS is shared with the AC01, and a reset pin cannot change the board.  The
reset already returned the codec's registers to power-up but took the MCLK
back to the 2.88 MHz default with them, so every generation after the first
ran at a clock the board never presents; it also left the frame period at the
rate the previous generation had programmed, while the divider registers it
was supposedly derived from had gone back to A = B = 18.  Carry the configured
MCLK across and restart the frame period with the registers.

IRQ5 is the serial port's receive interrupt, not the frame scheduler's.
SPRU056D 5.3: RINT is raised when RSR transfers into DRR, and a receiver held
in reset shifts nothing and raises nothing.  The frame clock itself is
genuinely external - the ASIC is the frame master and keeps running while the
DSP is inside an overlay - but all four sites that load DRR now end at one
port-level entry point that applies the RRST gate.  Suppressions are counted
and exported as `serial_rint_suppressed`, because a receiver left in reset is
otherwise indistinguishable from a frame clock that stopped.  The gate has not
been exercised against a completed call: the C51 ROM loader does not reach its
ASIC poll on either the 302 or the 403 image, which is a fault ahead of it and
predates this change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 10:54:46 +12:00

494 lines
17 KiB
C++

// SPDX-License-Identifier: BSD-3-Clause
#include "c5x_core.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <exception>
using courier::C5xCore;
namespace {
void copy_error(char *buffer, std::size_t size, const char *message)
{
if (!buffer || !size) return;
std::strncpy(buffer, message, size - 1);
buffer[size - 1] = '\0';
}
}
extern "C" {
void *courier_c5x_create()
{
try { return new C5xCore(); } catch (...) { return nullptr; }
}
void courier_c5x_destroy(void *handle) { delete static_cast<C5xCore *>(handle); }
void courier_c5x_reset(void *handle)
{
if (handle) static_cast<C5xCore *>(handle)->reset();
}
int courier_c5x_load_program(
void *handle, uint16_t origin, const uint8_t *bytes, std::size_t byte_count,
char *error, std::size_t error_size)
{
try {
if (!handle || !bytes || (byte_count & 1)) throw std::runtime_error("invalid C5x program segment");
std::vector<uint16_t> words(byte_count / 2);
for (std::size_t i = 0; i < words.size(); ++i)
words[i] = uint16_t(bytes[i * 2] | (uint16_t(bytes[i * 2 + 1]) << 8));
static_cast<C5xCore *>(handle)->load_program(words.data(), words.size(), origin);
return 0;
} catch (const std::exception &exception) {
copy_error(error, error_size, exception.what());
return -1;
}
}
int courier_c5x_schedule_call_overlay(
void *handle, uint16_t origin, const uint8_t *bytes, std::size_t byte_count,
uint16_t entry, const uint16_t *registers, uint16_t selector,
char *error, std::size_t error_size)
{
try {
if (!handle || !bytes || !registers || (byte_count & 1))
throw std::runtime_error("invalid C5x call overlay");
std::vector<uint16_t> words(byte_count / 2);
for (std::size_t i = 0; i < words.size(); ++i)
words[i] = uint16_t(bytes[i * 2] | (uint16_t(bytes[i * 2 + 1]) << 8));
static_cast<C5xCore *>(handle)->schedule_call_overlay(
origin, words.data(), words.size(), entry, registers, selector);
return 0;
} catch (const std::exception &exception) {
copy_error(error, error_size, exception.what());
return -1;
}
}
int courier_c5x_load_rom(
void *handle, uint16_t origin, const uint8_t *bytes, std::size_t byte_count,
char *error, std::size_t error_size)
{
try {
if (!handle || !bytes || (byte_count & 1)) throw std::runtime_error("invalid C5x ROM image");
std::vector<uint16_t> words(byte_count / 2);
for (std::size_t i = 0; i < words.size(); ++i)
words[i] = uint16_t(bytes[i * 2] | (uint16_t(bytes[i * 2 + 1]) << 8));
static_cast<C5xCore *>(handle)->load_rom(words.data(), words.size(), origin);
return 0;
} catch (const std::exception &exception) {
copy_error(error, error_size, exception.what());
return -1;
}
}
void courier_c5x_set_mpmc_pin(void *handle, int level)
{
if (handle) static_cast<C5xCore *>(handle)->set_mpmc_pin(uint16_t(level));
}
void courier_c5x_set_shared_window(void *handle, uint16_t first, uint16_t last)
{
if (handle) static_cast<C5xCore *>(handle)->set_shared_window(first, last);
}
void courier_c5x_get_memory_map(void *handle, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 21) return;
auto map = static_cast<C5xCore *>(handle)->memory_map();
uint64_t result[] = {
map.mpmc_pin, map.mpmc, map.ovly, map.ram, map.cnf, map.iptr,
map.pdwsr, map.iowsr, map.cwsr, map.rom_present ? 1u : 0u,
map.program_rom, map.program_daram, map.program_saram,
map.program_external,
map.data_registers, map.data_daram, map.data_saram,
map.data_reserved, map.data_shared, map.data_external, map.rom_holes,
};
std::copy(std::begin(result), std::end(result), values);
}
int courier_c5x_step(void *handle, uint64_t count, char *error, std::size_t error_size)
{
try {
if (!handle) throw std::runtime_error("null C5x handle");
static_cast<C5xCore *>(handle)->run(count);
return 0;
} catch (const std::exception &exception) {
copy_error(error, error_size, exception.what());
return -1;
}
}
uint16_t courier_c5x_get_io_output(void *handle, uint16_t port)
{
return handle ? static_cast<C5xCore *>(handle)->io_output(port) : 0;
}
void courier_c5x_set_io(void *handle, uint16_t port, uint16_t value)
{
if (handle) static_cast<C5xCore *>(handle)->set_io(port, value);
}
void courier_c5x_configure_host_mailbox(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->configure_host_mailbox(enabled != 0);
}
uint64_t courier_c5x_get_xf_falling_edges(void *handle)
{
return handle ? static_cast<C5xCore *>(handle)->xf_falling_edges() : 0;
}
void courier_c5x_host_write(void *handle, uint16_t address, uint16_t value)
{
if (handle) static_cast<C5xCore *>(handle)->host_write(address, value);
}
void courier_c5x_queue_serial_rx(void *handle, const uint16_t *samples, std::size_t count)
{
if (handle && samples) static_cast<C5xCore *>(handle)->queue_serial_rx(samples, count);
}
void courier_c5x_queue_codec_boot(void *handle, const uint16_t *words, std::size_t count)
{
if (handle && words) static_cast<C5xCore *>(handle)->queue_codec_boot(words, count);
}
void courier_c5x_queue_codec_rx(void *handle, const uint16_t *samples, std::size_t count)
{
if (handle && samples) static_cast<C5xCore *>(handle)->queue_codec_rx(samples, count);
}
void courier_c5x_set_hybrid_return(void *handle, uint32_t return_scale,
uint32_t delay)
{
if (handle) static_cast<C5xCore *>(handle)->set_hybrid_return(
return_scale, delay);
}
void courier_c5x_configure_digital_pcm(void *handle, int enabled,
uint16_t idle_codeword, uint32_t clock_hz)
{
if (handle) static_cast<C5xCore *>(handle)->configure_digital_pcm(
enabled != 0, idle_codeword, clock_hz);
}
void courier_c5x_queue_g711_rx(void *handle, const uint8_t *codewords, std::size_t count)
{
if (handle && codewords) static_cast<C5xCore *>(handle)->queue_g711_rx(codewords, count);
}
std::size_t courier_c5x_get_g711_tx(void *handle, uint8_t *out, std::size_t capacity)
{
if (!handle) return 0;
const auto &codewords = static_cast<C5xCore *>(handle)->g711_tx();
const std::size_t count = std::min(codewords.size(), capacity);
if (out) std::copy(codewords.begin(), codewords.begin() + count, out);
return codewords.size();
}
std::size_t courier_c5x_get_g711_tx_since(void *handle, std::size_t start,
uint8_t *out, std::size_t capacity)
{
if (!handle) return 0;
const auto &words = static_cast<C5xCore *>(handle)->g711_tx();
start = std::min(start, words.size());
const auto available = words.size() - start;
if (out) std::copy_n(words.begin() + start, std::min(available, capacity), out);
return available;
}
void courier_c5x_set_data_trace_filter(void *handle, unsigned address, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->set_data_trace_filter(
static_cast<uint16_t>(address), enabled != 0);
}
void courier_c5x_set_pc_trace_range(void *handle, unsigned first, unsigned last)
{
if (handle) static_cast<C5xCore *>(handle)->set_pc_trace_range(
static_cast<uint16_t>(first), static_cast<uint16_t>(last));
}
void courier_c5x_set_v8_dispatch_pcs(void *handle, const uint16_t *pcs,
std::size_t count)
{
if (handle) static_cast<C5xCore *>(handle)->set_v8_dispatch_pcs(pcs, count);
}
void courier_c5x_set_v8_calling(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->set_v8_calling(enabled != 0);
}
void courier_c5x_set_v8_answering(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->set_v8_answering(enabled != 0);
}
std::size_t courier_c5x_get_line_phase_samples(void *handle, unsigned phase,
uint16_t *out, std::size_t capacity)
{
if (!handle) return 0;
const auto &samples = static_cast<C5xCore *>(handle)->line_phase_samples(phase);
std::size_t count = samples.size() < capacity ? samples.size() : capacity;
if (out) std::copy(samples.begin(), samples.begin() + count, out);
return samples.size();
}
void courier_c5x_set_line_dac_slot(void *handle, uint16_t slot)
{
if (handle) static_cast<C5xCore *>(handle)->set_line_dac_slot(slot);
}
void courier_c5x_set_bio_low(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->set_bio_low(enabled != 0);
}
uint16_t courier_c5x_get_io(void *handle, uint16_t port)
{
return handle ? static_cast<C5xCore *>(handle)->io(port) : 0xffff;
}
uint16_t courier_c5x_get_program(void *handle, uint16_t address)
{
return handle ? static_cast<C5xCore *>(handle)->program(address) : 0xffff;
}
uint16_t courier_c5x_get_data(void *handle, uint16_t address)
{
return handle ? static_cast<C5xCore *>(handle)->data(address) : 0xffff;
}
uint16_t courier_c5x_get_stack(void *handle, unsigned index)
{
return handle ? static_cast<C5xCore *>(handle)->stack_entry(index) : 0xffff;
}
uint16_t courier_c5x_get_register(void *handle, uint16_t offset)
{
return handle ? static_cast<C5xCore *>(handle)->register_value(offset) : 0xffff;
}
void courier_c5x_set_data(void *handle, uint16_t address, uint16_t value)
{
if (handle) static_cast<C5xCore *>(handle)->set_data(address, value);
}
uint64_t courier_c5x_get_data_write_count(void *handle, uint16_t address)
{
return handle ? static_cast<C5xCore *>(handle)->data_write_count(address) : 0;
}
void courier_c5x_interrupt(void *handle, unsigned irq)
{
if (handle) static_cast<C5xCore *>(handle)->interrupt(irq);
}
void courier_c5x_nmi(void *handle)
{
if (handle) static_cast<C5xCore *>(handle)->nmi();
}
void courier_c5x_configure_line_frame_interrupt(void *handle, unsigned irq, uint16_t vector)
{
if (handle) static_cast<C5xCore *>(handle)->configure_line_frame_interrupt(irq, vector);
}
void courier_c5x_set_call_tdm_active(void *handle, int active)
{
if (handle) static_cast<C5xCore *>(handle)->set_call_tdm_active(active != 0);
}
int courier_c5x_call_tdm_active(void *handle)
{
return handle && static_cast<C5xCore *>(handle)->call_tdm_active() ? 1 : 0;
}
void courier_c5x_schedule_line_frame_entry(void *handle, uint16_t address)
{
if (handle) static_cast<C5xCore *>(handle)->schedule_line_frame_entry(address);
}
void courier_c5x_set_pc(void *handle, uint16_t address)
{
if (handle) static_cast<C5xCore *>(handle)->set_pc(address);
}
void courier_c5x_get_state(void *handle, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 22) return;
auto state = static_cast<C5xCore *>(handle)->state();
uint64_t result[] = {
state.pc, state.op, uint32_t(state.acc), uint32_t(state.accb), uint32_t(state.preg),
state.dp, state.arp, state.flags, state.idle ? 1u : 0u,
state.instructions, state.cycles,
static_cast<C5xCore *>(handle)->io_events().size(),
state.ar[0], state.ar[1], state.ar[2], state.ar[3],
state.ar[4], state.ar[5], state.ar[6], state.ar[7],
state.arcr, state.indx,
};
std::copy(std::begin(result), std::end(result), values);
}
void courier_c5x_set_data_trace(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->set_data_trace(enabled != 0);
}
void courier_c5x_clear_data_events(void *handle)
{
if (handle) static_cast<C5xCore *>(handle)->clear_data_events();
}
std::size_t courier_c5x_get_data_event_count(void *handle)
{
return handle ? static_cast<C5xCore *>(handle)->data_events().size() : 0;
}
std::size_t courier_c5x_get_io_event_count(void *handle)
{
return handle ? static_cast<C5xCore *>(handle)->io_events().size() : 0;
}
void courier_c5x_get_io_port_stats(
void *handle, uint16_t port, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 6) return;
const auto &stat = static_cast<C5xCore *>(handle)->io_port_stat(port);
uint64_t result[] = {
stat.reads, stat.writes, stat.last_read, stat.last_write,
stat.last_read_pc, stat.last_write_pc,
};
std::copy(std::begin(result), std::end(result), values);
}
void courier_c5x_get_io_event(void *handle, std::size_t index, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 5) return;
const auto &events = static_cast<C5xCore *>(handle)->io_events();
if (index >= events.size()) return;
const auto &event = events[index];
uint64_t result[] = {event.write ? 1u : 0u, event.port, event.value, event.pc, event.instruction};
std::copy(std::begin(result), std::end(result), values);
}
std::size_t courier_c5x_get_mailbox_event_count(void *handle)
{
return handle ? static_cast<C5xCore *>(handle)->mailbox_events().size() : 0;
}
void courier_c5x_get_mailbox_event(
void *handle, std::size_t index, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 5) return;
const auto &events = static_cast<C5xCore *>(handle)->mailbox_events();
if (index >= events.size()) return;
const auto &event = events[index];
uint64_t result[] = {event.write ? 1u : 0u, event.port, event.value, event.pc, event.instruction};
std::copy(std::begin(result), std::end(result), values);
}
uint16_t courier_c5x_get_line_tx_sample(void *handle, std::size_t index)
{
if (!handle) return 0;
const auto &samples = static_cast<C5xCore *>(handle)->line_tx_samples();
return index < samples.size() ? samples[index] : 0;
}
void courier_c5x_get_data_event(void *handle, std::size_t index, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 4) return;
const auto &events = static_cast<C5xCore *>(handle)->data_events();
if (index >= events.size()) return;
const auto &event = events[index];
uint64_t result[] = {event.address, event.value, event.pc, event.instruction};
std::copy(std::begin(result), std::end(result), values);
}
std::size_t courier_c5x_get_pc_trace_count(void *handle)
{
return handle ? static_cast<C5xCore *>(handle)->pc_trace().size() : 0;
}
void courier_c5x_get_pc_trace(void *handle, std::size_t index, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 2) return;
const auto &trace = static_cast<C5xCore *>(handle)->pc_trace();
if (index >= trace.size()) return;
values[0] = trace[index] >> 16;
values[1] = trace[index] & 0xffff;
}
void courier_c5x_get_serial_state(void *handle, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 58) return;
auto serial = static_cast<C5xCore *>(handle)->serial_state();
uint64_t result[] = {
serial.drr, serial.dxr, serial.spc,
serial.drr_reads, serial.dxr_writes, serial.spc_writes,
serial.rx_consumed, serial.rx_queued,
serial.codec_rx_consumed, serial.codec_rx_queued,
serial.last_drr_pc, serial.last_dxr_pc, serial.last_spc_pc,
serial.trcv, serial.tdxr, serial.tspc,
serial.trcv_reads, serial.tdxr_writes, serial.tspc_writes,
serial.last_trcv_pc, serial.last_tdxr_pc, serial.last_tspc_pc,
serial.line_tx_writes, serial.line_tx_nonzero, serial.line_frame_interrupts,
serial.serial_rint_suppressed,
serial.line_dac_writes, serial.line_dac_frames,
serial.line_tx_last, serial.line_tx_last_pc, serial.imr,
serial.v8_rx_state, serial.v8_rx_peak, serial.codec_rx_peak,
serial.negotiation_loop_entries, serial.negotiation_loop_pc, serial.negotiation_source, serial.negotiation_pair,
serial.negotiation_source_value, serial.negotiation_pair_value,
uint32_t(serial.negotiation_acc),
serial.v8_dispatches, serial.v8_record, serial.v8_handler,
serial.v8_countdown, serial.v8_flags, serial.v8_dispatch_pc,
serial.v8_dispatch_dp,
serial.negotiation_d76, serial.negotiation_d77,
serial.negotiation_d78, serial.negotiation_d79,
serial.negotiation_d26, serial.negotiation_indx,
serial.negotiation_arp, serial.negotiation_pm,
serial.hybrid_frames, serial.hybrid_peak,
};
std::copy(std::begin(result), std::end(result), values);
}
} // extern "C"
extern "C" void courier_c5x_configure_rom_codec(void *handle, int enabled)
{
if (handle) static_cast<C5xCore *>(handle)->configure_rom_codec(enabled != 0);
}
extern "C" void courier_c5x_set_codec_mclk(void *handle, uint32_t hz)
{
if (handle) static_cast<C5xCore *>(handle)->set_codec_mclk(hz);
}
extern "C" void courier_c5x_get_codec_state(void *handle, uint64_t *values, std::size_t count)
{
if (!handle || !values || count < 35) return;
auto codec = static_cast<C5xCore *>(handle)->codec_state();
uint64_t result[] = {
codec.registers[0], codec.registers[1], codec.registers[2],
codec.registers[3], codec.registers[4], codec.registers[5],
codec.registers[6], codec.registers[7], codec.registers[8],
codec.mclk_hz, codec.sample_rate_millihz, codec.frame_period,
codec.secondary_frames, codec.register_writes, codec.register_reads,
codec.phase_shifts, codec.primary_frames, codec.frames_clocked,
codec.last_control_word,
codec.rate_programmed, codec.secondary_pending, codec.force_secondary,
codec.free_run, codec.high_pass_enabled, codec.loopback,
codec.sixteen_bit, codec.input_gain, codec.output_gain,
codec.monitor_gain, codec.input_select,
codec.codec_rx_size, codec.line_frame_next_cycle, codec.cycles,
uint64_t(int64_t(codec.line_frame_irq)),
};
std::copy(std::begin(result), std::end(result), values);
}