mirror of
https://github.com/D-Modem/courier-emu.git
synced 2026-11-28 12:38:04 -08:00
The board's INT0 is most likely the C52's TOUT, which the ASIC routes to the CPU: in an x2 call the emulated C52's timer runs at 2,397 Hz on the 20.16 MHz time base, against 2,401 Hz counted on the live board. With COURIER_INT0_TOUT=1 each TOUT pulse latches one request, as the 80186's edge-triggered request bit does, whether or not IF is set, and the first service step that IF and the in-service bit allow takes it. This happens in the Python paths and in the native worker, which gains three fields. COURIER_TOUT_LOG samples the timer for diagnosis. At 0 dB and a service step of 256, a 54666 bit/s call carried 93 lines against 30 before. With a full-rate INT0 the Courier's own upstream signal degrades and 6 dB calls no longer train: see the write-up. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTieSnZY49zbAZzpUP
2863 lines
113 KiB
C++
2863 lines
113 KiB
C++
#include <cstdlib>
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
#include "c5x_core.h"
|
|
#include "bearer.hpp"
|
|
#include "resample.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <exception>
|
|
#include <stdexcept>
|
|
#include <cerrno>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
|
|
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';
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
// Port-level model of the analogue board's live data lanes, run inside the
|
|
// x86 engine so the CPU's polling of them does not leave native code. Only
|
|
// the lane regime lives here: it is a pure function of the DSP's I/O cells
|
|
// and the supervisor's window bytes, so it answers exactly what the harness's
|
|
// Python handlers answer. Everything else is declined and goes to Python.
|
|
struct LaneHostIo {
|
|
C5xCore *core = nullptr;
|
|
bool live = false;
|
|
uint16_t command_port = 0x18, ack_port = 0x1a;
|
|
uint16_t lane_first = 0x40, banks = 6, dsp_first = 0x58, dsp_status = 0x56;
|
|
// Window bytes behind ports lane_first + 2 * index, index < 2 * banks.
|
|
uint8_t *lane[16] = {};
|
|
// Accesses served here, to be folded into the harness's I/O tallies.
|
|
uint32_t in_count[256] = {}, out_count[256] = {};
|
|
uint16_t out_last[256] = {};
|
|
uint8_t out_seen[256] = {};
|
|
// The host-port status (0x1c) and overlay-strobe (0x1e) registers, for the
|
|
// accesses that are a pure function of the DSP's status latch and of the
|
|
// few bridge flags the harness publishes here (courier_laneio_configure_
|
|
// mailbox). Whatever else they do - commits, window strobes - is declined.
|
|
bool mb_runtime = false; // ROM protocol, runtime mode: 0x1c answers from the latch
|
|
bool mb_pending = false; // a host message is staged for delivery
|
|
bool mb_inbound = false; // a DSP-to-host message is queued
|
|
bool mb_overlay = false; // 0x1e reads answer the overlay status byte
|
|
bool mb_zero_ok = false; // a 0 written to 0x1e is not a strobe
|
|
uint8_t mb_overlay_status = 0;
|
|
uint16_t mb_status_cell = 0x57;
|
|
// Input ports whose answer the harness has worked out and published (the
|
|
// panel's latches, say), served until one of the ports it depends on is
|
|
// written. Any OUT to a port marked in `invalidates` drops them all; the
|
|
// harness publishes again once it has seen the state change.
|
|
uint8_t fixed_valid[256] = {}, fixed_value[256] = {}, invalidates[256] = {};
|
|
|
|
// A mid-call overlay's half-block strobes on 0x1e: bridge.py's
|
|
// DSP_COMMAND_PORT branch of `write`, for every strobe but the transfer's
|
|
// start/end. Each half is logged for the harness to replay through
|
|
// `_accumulate_overlay`; a `2` also commits the window's four words to the
|
|
// C51 resident loader here, as `_commit_overlay_group` does, because the
|
|
// loader has to have taken them before the supervisor's next OUT.
|
|
struct OverlayEvent {
|
|
uint8_t strobe, committed;
|
|
uint16_t base;
|
|
uint8_t half[4], block[8];
|
|
};
|
|
bool ov_strobes = false; // the harness's overlay preconditions hold
|
|
uint8_t ov_first = 1, ov_start = 4;
|
|
uint8_t ov_window_strobe[256] = {};
|
|
uint8_t *ov_window = nullptr; // the first transfer window's eight bytes
|
|
std::vector<OverlayEvent> ov_events;
|
|
std::string ov_error;
|
|
|
|
// The boot download through the C51's mask-ROM loader: bridge.py's
|
|
// command-port branch of `write` while `_rom_loader_armed`, for every
|
|
// strobe but the one that completes the bootstrap (activation stays with
|
|
// the harness), and the window bytes on 58..5e, which carry the transfer
|
|
// until the program is in and the mailbox only after. Each strobe is
|
|
// `_commit_rom_group`: four words at 58 or 5c, the strobe in @56, and the
|
|
// loader stepped until it writes @56 back. The window it sent is logged
|
|
// for the harness to append to the bootstrap.
|
|
struct BootEvent { uint8_t strobe, failed; uint8_t window[8]; };
|
|
bool boot_strobes = false, boot_stores = false;
|
|
uint8_t boot_first = 1;
|
|
int boot_checksum = -1;
|
|
uint8_t *boot_window[256] = {};
|
|
uint8_t *boot_lane[4] = {}; // ports 58, 5a, 5c, 5e
|
|
int64_t boot_remaining = 0; // bootstrap bytes still to come
|
|
std::vector<BootEvent> boot_events;
|
|
std::string boot_error;
|
|
|
|
bool serve_boot_strobe(uint8_t strobe)
|
|
{
|
|
if (!boot_strobes || !boot_window[strobe] || int(strobe) == boot_checksum
|
|
|| boot_remaining <= 8 || !boot_error.empty())
|
|
return false;
|
|
const uint16_t pc = core->program_counter();
|
|
if (pc < 0x0638 || pc >= 0x0653) return false;
|
|
BootEvent event{};
|
|
event.strobe = strobe;
|
|
std::memcpy(event.window, boot_window[strobe], 8);
|
|
try {
|
|
const uint64_t before = core->io_port_stat(0x56).writes;
|
|
const uint16_t base = uint16_t(0x58 + 4 * (strobe - boot_first));
|
|
for (unsigned index = 0; index < 4; ++index)
|
|
core->set_io(uint16_t(base + index), uint16_t(
|
|
event.window[2 * index] | (event.window[2 * index + 1] << 8)));
|
|
core->set_io(0x56, strobe);
|
|
bool acknowledged = false;
|
|
for (unsigned step = 0; step < 4096 && !acknowledged; ++step) {
|
|
core->run(1);
|
|
acknowledged = core->io_port_stat(0x56).writes > before;
|
|
}
|
|
if (!acknowledged) {
|
|
char message[80];
|
|
std::snprintf(message, sizeof message,
|
|
"C51 ROM loader did not acknowledge strobe %u", unsigned(strobe));
|
|
boot_error = message;
|
|
}
|
|
} catch (const std::exception &exception) {
|
|
boot_error = exception.what();
|
|
}
|
|
event.failed = !boot_error.empty();
|
|
boot_events.push_back(event);
|
|
boot_remaining -= 8;
|
|
return true;
|
|
}
|
|
|
|
// An opt-in record of the data lanes' traffic, for decoding what the two
|
|
// processors exchange: kind 1 a word the CPU committed to a bank, 2 the
|
|
// CPU's receive acknowledgement, 3 a lane byte the CPU read.
|
|
struct LaneRecord { uint64_t now; uint8_t kind, port; uint16_t value; };
|
|
bool cap_on = false;
|
|
std::vector<LaneRecord> cap;
|
|
void record(uint64_t now, uint8_t kind, uint8_t port, uint16_t value)
|
|
{
|
|
if (cap_on && cap.size() < (std::size_t(1) << 24))
|
|
cap.push_back({now, kind, port, value});
|
|
}
|
|
|
|
void ov_present(const uint8_t *block)
|
|
{
|
|
for (unsigned index = 0; index < 4; ++index)
|
|
core->set_io(uint16_t(0x58 + index),
|
|
uint16_t(block[2 * index] | (block[2 * index + 1] << 8)));
|
|
// _present_to_dsp(0x0200): the four-word-ready flag and the doorbell.
|
|
core->set_io(0x57, uint16_t(core->io(0x57) | 0x0200));
|
|
core->interrupt(1);
|
|
}
|
|
|
|
bool ov_settle(uint16_t base)
|
|
{
|
|
const uint16_t target = uint16_t(base + 4);
|
|
for (unsigned step = 0; step < 20000; ++step) {
|
|
core->run(1);
|
|
if (core->data(0xFF62) == target) {
|
|
core->run(32);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool ov_commit(OverlayEvent &event)
|
|
{
|
|
try {
|
|
const uint16_t destination = core->data(0xFF62);
|
|
ov_present(event.block);
|
|
mb_overlay_status &= uint8_t(~0x02);
|
|
uint16_t base = destination;
|
|
bool settled = ov_settle(destination);
|
|
if (!settled) {
|
|
const uint16_t repointed = core->data(0xFF62);
|
|
if (repointed != destination) {
|
|
ov_present(event.block);
|
|
base = repointed;
|
|
settled = ov_settle(repointed);
|
|
}
|
|
}
|
|
if (settled) {
|
|
mb_overlay_status |= 0x02;
|
|
event.committed = 1;
|
|
event.base = base;
|
|
return true;
|
|
}
|
|
char message[160];
|
|
std::snprintf(message, sizeof message,
|
|
"C51 resident loader did not acknowledge overlay block: "
|
|
"destination=%04x, ff62=%04x, pa7=%04x",
|
|
destination, core->data(0xFF62), core->io(0x57));
|
|
ov_error = message;
|
|
} catch (const std::exception &exception) {
|
|
ov_error = exception.what();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool serve_strobe(uint8_t strobe)
|
|
{
|
|
if (!ov_strobes || !ov_window || strobe == ov_start
|
|
|| !ov_window_strobe[strobe] || !ov_error.empty())
|
|
return false;
|
|
OverlayEvent event{};
|
|
event.strobe = strobe;
|
|
std::memcpy(event.half, ov_window + (strobe == ov_first ? 0 : 4), 4);
|
|
if (strobe == 2) {
|
|
std::memcpy(event.block, ov_window, 8);
|
|
ov_commit(event);
|
|
}
|
|
ov_events.push_back(event);
|
|
++out_count[0x1e];
|
|
out_last[0x1e] = strobe;
|
|
out_seen[0x1e] = 1;
|
|
return true;
|
|
}
|
|
|
|
// direction 1 = IN, 2 = OUT; true when served.
|
|
bool serve_mailbox(int direction, uint16_t port, uint16_t value, uint16_t *out)
|
|
{
|
|
if (direction == 1) {
|
|
uint16_t answer;
|
|
if (port == 0x1c) {
|
|
if (!mb_runtime) return false;
|
|
const uint16_t status = core->io(mb_status_cell);
|
|
answer = (status & 0x0001) || mb_pending ? 0 : 1;
|
|
if (mb_inbound || ((status ^ 0x0006) & 0x02)) answer |= 2;
|
|
// A word from the stream sender at 0x849e, which clears PA7
|
|
// bit 2 as the message sender clears bit 1: bridge.py's read.
|
|
if ((status ^ 0x0006) & 0x04) answer |= 4;
|
|
} else {
|
|
if (!mb_overlay) return false;
|
|
answer = mb_overlay_status;
|
|
}
|
|
++in_count[port];
|
|
*out = answer & 0xff;
|
|
return true;
|
|
}
|
|
if (value & 0xff)
|
|
return port == 0x1e && serve_strobe(uint8_t(value));
|
|
if (port == 0x1c ? !mb_runtime : !mb_zero_ok) return false;
|
|
++out_count[port];
|
|
out_last[port] = 0;
|
|
out_seen[port] = 1;
|
|
return true;
|
|
}
|
|
|
|
uint16_t lane_word(unsigned bank) const
|
|
{
|
|
uint16_t word = 0;
|
|
for (unsigned half = 0; half < 2; ++half) {
|
|
const uint8_t *byte = lane[2 * bank + half];
|
|
word |= uint16_t((byte ? *byte : 0xff) << (8 * half));
|
|
}
|
|
return word;
|
|
}
|
|
};
|
|
}
|
|
|
|
namespace {
|
|
// The interrupt controllers' registers, the 8254's wrap bookkeeping and the
|
|
// counters the poll keeps: the layout of PollState in poll_state.py.
|
|
struct PicChip {
|
|
uint8_t vector_base, mask, irr, isr, auto_eoi, read_isr, expect_icw4, single, init_words;
|
|
};
|
|
struct PitSlot {
|
|
uint64_t next_due, origin, reported;
|
|
uint32_t initial;
|
|
uint8_t programmed;
|
|
};
|
|
constexpr uint64_t PIT_INFINITE = ~uint64_t(0);
|
|
|
|
// The engine's registers and memory, lent to the poll for an interrupt entry
|
|
// (x86_interpreter.cpp's Core, in the order its register array has them).
|
|
struct PollCpu {
|
|
uint32_t *regs;
|
|
uint8_t *memory;
|
|
std::size_t memory_size;
|
|
const uint8_t *guard;
|
|
uint8_t *io_events;
|
|
uint32_t io_event_count;
|
|
uint8_t *mmio_events;
|
|
uint32_t mmio_count;
|
|
uint64_t batch_total;
|
|
};
|
|
enum { REG_SP = 4, REG_CS = 9, REG_SS = 10, REG_IP = 12, REG_FLAGS = 13 };
|
|
struct PollState {
|
|
PicChip pic[2];
|
|
uint64_t delivered;
|
|
PitSlot pit[3];
|
|
uint64_t timer_ticks;
|
|
uint64_t coalesced[3];
|
|
int8_t counter_irq[12];
|
|
uint64_t pit_clock_hz, ips;
|
|
int64_t clock_origin;
|
|
uint64_t next_rtos, next_mailbox, rtos_interval, mailbox_interval;
|
|
uint8_t rtos_on, mailbox_on;
|
|
uint8_t asserted_count, asserted[8];
|
|
uint64_t hardware_interrupts;
|
|
uint64_t last_poll;
|
|
uint64_t pic_in[4], pic_out[4];
|
|
// IRQ13 from the DSP's timer output: raised when the core's expiry count
|
|
// has moved past tout_seen (IsdnMachine.poll_timers does the same).
|
|
uint8_t tout_on;
|
|
uint64_t tout_seen;
|
|
};
|
|
|
|
// pic.py, line for line.
|
|
namespace pic {
|
|
constexpr unsigned CASCADE = 2;
|
|
inline void command(PicChip &c, uint8_t value)
|
|
{
|
|
if (value & 0x10) {
|
|
c.expect_icw4 = value & 1;
|
|
c.single = (value & 2) != 0;
|
|
c.init_words = 1;
|
|
c.isr = 0;
|
|
c.irr = 0;
|
|
return;
|
|
}
|
|
if (value & 0x08) {
|
|
if (value == 0x0B) c.read_isr = 1;
|
|
else if (value == 0x0A) c.read_isr = 0;
|
|
return;
|
|
}
|
|
if (value & 0x20) {
|
|
if (value & 0x40) {
|
|
c.isr &= uint8_t(~(1u << (value & 7)));
|
|
} else {
|
|
for (unsigned line = 0; line < 8; ++line)
|
|
if (c.isr & (1u << line)) { c.isr &= uint8_t(~(1u << line)); break; }
|
|
}
|
|
}
|
|
}
|
|
inline void data(PicChip &c, uint8_t value)
|
|
{
|
|
if (c.init_words == 1) {
|
|
c.vector_base = value & 0xF8;
|
|
if (!c.single) c.init_words = 2;
|
|
else c.init_words = c.expect_icw4 ? 3 : 0;
|
|
return;
|
|
}
|
|
if (c.init_words == 2) { c.init_words = c.expect_icw4 ? 3 : 0; return; }
|
|
if (c.init_words == 3) { c.auto_eoi = (value & 2) != 0; c.init_words = 0; return; }
|
|
c.mask = value;
|
|
}
|
|
inline int pending(const PicChip &c, uint8_t irr)
|
|
{
|
|
const uint8_t ready = irr & uint8_t(~c.mask);
|
|
if (!ready) return -1;
|
|
for (unsigned line = 0; line < 8; ++line) {
|
|
const uint8_t bit = uint8_t(1u << line);
|
|
if (c.isr & bit) return -1;
|
|
if (ready & bit) return int(line);
|
|
}
|
|
return -1;
|
|
}
|
|
inline void acknowledge(PicChip &c, unsigned line)
|
|
{
|
|
c.irr &= uint8_t(~(1u << line));
|
|
if (!c.auto_eoi) c.isr |= uint8_t(1u << line);
|
|
}
|
|
inline void raise(PollState &s, unsigned irq)
|
|
{
|
|
if (irq < 8) {
|
|
s.pic[0].irr |= uint8_t(1u << irq);
|
|
} else {
|
|
s.pic[1].irr |= uint8_t(1u << (irq - 8));
|
|
s.pic[0].irr |= uint8_t(1u << CASCADE);
|
|
}
|
|
}
|
|
// InterruptControllers.pending_vector: -1 when nothing is ready.
|
|
inline int pending_vector(PollState &s)
|
|
{
|
|
PicChip &master = s.pic[0], &slave = s.pic[1];
|
|
// IR2 is the slave's INT output: asserted again once an EOI unblocks a
|
|
// request that waited behind the one in service (pic.py).
|
|
if (pending(slave, slave.irr) >= 0) master.irr |= uint8_t(1u << CASCADE);
|
|
const int line = pending(master, master.irr);
|
|
if (line < 0) return -1;
|
|
if (line == int(CASCADE)) {
|
|
const int slave_line = pending(slave, slave.irr);
|
|
if (slave_line < 0) {
|
|
master.irr &= uint8_t(~(1u << CASCADE));
|
|
return -1;
|
|
}
|
|
acknowledge(master, CASCADE);
|
|
acknowledge(slave, unsigned(slave_line));
|
|
++s.delivered;
|
|
return slave.vector_base + slave_line;
|
|
}
|
|
acknowledge(master, unsigned(line));
|
|
++s.delivered;
|
|
return master.vector_base + line;
|
|
}
|
|
} // namespace pic
|
|
}
|
|
|
|
namespace {
|
|
constexpr std::size_t OV_LOG_CAPACITY = 8192;
|
|
// State the I-modem's mailbox endpoint shares with its native port model.
|
|
// Python reads and writes these fields in place through a ctypes mirror of
|
|
// this layout (dsp.py ImodemShared), so neither side marshals anything.
|
|
struct ImodemShared {
|
|
uint64_t dsp_instructions; // 386 instruction count the C5x has been run to
|
|
double cycle_debt; // C5x cycles owed and not yet run
|
|
uint64_t pcm_cursor; // PCM octets already exchanged with the DSC
|
|
uint64_t consumed;
|
|
uint64_t reply_writes;
|
|
uint64_t latch_writes;
|
|
uint64_t lane_writes; // lane stores served natively
|
|
uint8_t host_pending;
|
|
uint8_t tx_ready;
|
|
uint8_t rx_present;
|
|
uint8_t needs_service; // 1: the harness has work after a native advance
|
|
// Stop for the harness once this many transmitted PCM octets are waiting
|
|
// (0: at every finished frame, which is the unbatched behaviour).
|
|
uint32_t flush_octets;
|
|
uint8_t lanes[0x60]; // window bytes by port, 0x40..0x5e
|
|
uint32_t tx_pending; // transmitted octets the harness has not taken
|
|
uint32_t rx_pending; // receive octets queued ahead in the core
|
|
// Host-port 0x1e accesses served natively, in order, for the mailbox's
|
|
// diagnostic logs: (kind << 3) | value, kind 0 an IN's answer, kind 1 an
|
|
// OUT's low three bits. The harness replays them (ImodemDsp._drain_overlay)
|
|
// before it touches those logs itself; the model declines when this is full.
|
|
uint32_t ov_len;
|
|
uint8_t ov_log[OV_LOG_CAPACITY];
|
|
// What the harness publishes so the engine can skip its poll (poll()):
|
|
// until which 386 instruction count nothing in the harness is due, whether
|
|
// anything it depends on was touched since it last looked, and where the
|
|
// line clock stands (octets of B1 it has taken, octets waiting to go out,
|
|
// and the line frame that sends them).
|
|
uint64_t elide_until;
|
|
uint64_t clock_seen;
|
|
uint64_t clock_pending;
|
|
uint32_t clock_frame;
|
|
uint8_t poll_dirty;
|
|
uint8_t elide_on;
|
|
uint64_t polls_native, polls_resumed;
|
|
uint8_t elided_if, elided_any;
|
|
uint64_t poll_why[8];
|
|
};
|
|
|
|
// Port model for the I-modem's live data lanes and their DSP advance. It does
|
|
// what ImodemDsp.read/write and IsdnMachine._advance_dsp do for these ports,
|
|
// and stops, leaving `needs_service` set, whenever the harness has to act
|
|
// (a finished PCM frame, a reply to offer) so that work happens in Python in
|
|
// the same order it would have.
|
|
struct ImodemHostIo {
|
|
ImodemShared *shared = nullptr;
|
|
C5xCore *core = nullptr;
|
|
bool live = false;
|
|
// A live bearer clocks the C5x from wall time (ImodemDsp.pace_realtime),
|
|
// not from the 386's instruction count: the ports are then served from
|
|
// wherever that left the core, and advance() steps nothing.
|
|
bool wallclock = false;
|
|
// With wallclock, the core cycle the harness's wall clock has reached:
|
|
// advance() runs the C5x up to it, as it runs it up to `now` otherwise.
|
|
uint64_t wall_target = 0;
|
|
double cycles_per_instruction = 0;
|
|
uint32_t read_quantum = 0;
|
|
uint32_t in_count[256] = {}, out_count[256] = {};
|
|
courier::Bearer *bearer = nullptr;
|
|
PollState *ps = nullptr;
|
|
|
|
// The 8259 registers at 0xf020/0xf021 and 0xf0a0/0xf0a1.
|
|
bool serve_pic(int direction, uint16_t port, uint16_t value, uint16_t *out)
|
|
{
|
|
unsigned slot;
|
|
switch (port) {
|
|
case 0xF020: slot = 0; break;
|
|
case 0xF021: slot = 1; break;
|
|
case 0xF0A0: slot = 2; break;
|
|
case 0xF0A1: slot = 3; break;
|
|
default: return false;
|
|
}
|
|
PicChip &chip = ps->pic[slot >> 1];
|
|
if (direction == 1) {
|
|
*out = (slot & 1) ? chip.mask : (chip.read_isr ? chip.isr : chip.irr);
|
|
++ps->pic_in[slot];
|
|
} else {
|
|
if (slot & 1) pic::data(chip, uint8_t(value));
|
|
else pic::command(chip, uint8_t(value));
|
|
++ps->pic_out[slot];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
uint16_t word(uint16_t port) const
|
|
{
|
|
return uint16_t(shared->lanes[port] | (shared->lanes[port + 2] << 8));
|
|
}
|
|
|
|
// The host-port status (0x1c) and strobe (0x1e) registers, for the cases
|
|
// that need nothing from the harness: ImodemDsp.read/write do the same.
|
|
// Anything else - a command commit, a reply acknowledgement, a reply to
|
|
// offer, a full log - is declined and left to them.
|
|
int serve_host_port(int direction, uint16_t port, uint16_t value,
|
|
uint64_t now, uint16_t *out)
|
|
{
|
|
ImodemShared *sh = shared;
|
|
if (direction == 1) {
|
|
if (port == 0x1e && sh->ov_len >= OV_LOG_CAPACITY) return 0;
|
|
if (!advance(now, read_quantum)) return 0;
|
|
uint16_t answer;
|
|
if (port == 0x1e) {
|
|
answer = uint16_t(~(core->io(0x57) >> 8)) & 7;
|
|
sh->ov_log[sh->ov_len++] = uint8_t(answer);
|
|
} else {
|
|
// ImodemDsp._sync, then the mailbox's status word.
|
|
const uint16_t status = core->io(0x57);
|
|
if (sh->host_pending && !(status & 1)) {
|
|
++sh->consumed;
|
|
sh->host_pending = 0;
|
|
}
|
|
sh->tx_ready = !(status & 1);
|
|
if (!(status & 2) && !sh->rx_present
|
|
&& core->io_port_stat(0x5f).writes > sh->reply_writes)
|
|
return 0;
|
|
answer = uint16_t(sh->tx_ready | (sh->rx_present ? 2 : 0));
|
|
}
|
|
++in_count[port];
|
|
*out = answer;
|
|
return 1;
|
|
}
|
|
value &= 0xff;
|
|
if (port == 0x1c) {
|
|
// Bit 0 commits a command and bit 1 acknowledges a reply; with
|
|
// neither the write does nothing at all.
|
|
if (value & 3) return 0;
|
|
if (!advance(now, 0)) return 0;
|
|
} else {
|
|
if (sh->ov_len >= OV_LOG_CAPACITY) return 0;
|
|
if (!advance(now, 0)) return 0;
|
|
const unsigned masked = value & 7;
|
|
sh->ov_log[sh->ov_len++] = uint8_t(8 | masked);
|
|
if (value & 3) {
|
|
const uint16_t base = (value & 1) ? 0x40 : 0x48;
|
|
const uint16_t dsp_base = (value & 1) ? 0x58 : 0x5a;
|
|
for (unsigned i = 0; i < 2; ++i)
|
|
core->set_io(uint16_t(dsp_base + i), word(uint16_t(base + 4 * i)));
|
|
}
|
|
core->set_io(0x57, uint16_t(core->io(0x57) | (masked << 8)));
|
|
}
|
|
++out_count[port];
|
|
return 1;
|
|
}
|
|
|
|
// The octets the C5x has transmitted that nobody has exchanged: `waiting`
|
|
// of them when it is the native model's own count that is there, else all.
|
|
std::size_t untaken(std::size_t waiting) const
|
|
{
|
|
const auto &tx = core->g711_tx();
|
|
const std::size_t cursor = std::min<std::size_t>(shared->pcm_cursor, tx.size());
|
|
const std::size_t available = tx.size() - cursor;
|
|
return waiting && available >= waiting ? waiting : available;
|
|
}
|
|
|
|
// The part of ImodemDsp._sync_values the native path may do: a reply to
|
|
// offer is Python's.
|
|
bool reply_due(uint16_t status, uint64_t writes) const
|
|
{
|
|
return !(status & 2) && !shared->rx_present && writes > shared->reply_writes;
|
|
}
|
|
|
|
void sync_values(uint16_t status)
|
|
{
|
|
ImodemShared *sh = shared;
|
|
if (sh->host_pending && !(status & 1)) {
|
|
++sh->consumed;
|
|
sh->host_pending = 0;
|
|
}
|
|
sh->tx_ready = !(status & 1);
|
|
}
|
|
|
|
// Exchange `count` octets from the C5x's transmit buffer, which must be
|
|
// settled entirely, from frames fed ahead or a frame at a time (the
|
|
// caller has checked: see exchanges()).
|
|
void exchange_taken(std::size_t count)
|
|
{
|
|
ImodemShared *sh = shared;
|
|
const auto &tx = core->g711_tx();
|
|
std::vector<uint8_t> octets(tx.begin() + sh->pcm_cursor,
|
|
tx.begin() + sh->pcm_cursor + count);
|
|
sh->pcm_cursor += count;
|
|
uint8_t remainder[2 * courier::BEARER_SLOTS + 2];
|
|
bearer->exchange(core, octets.data(), octets.size(), remainder);
|
|
}
|
|
|
|
bool settles(std::size_t count) const
|
|
{
|
|
const std::size_t frames = ((bearer->partial_valid ? 1 : 0) + count) / courier::BEARER_SLOTS;
|
|
return bearer->ahead.size() >= frames;
|
|
}
|
|
|
|
// Whether `count` octets can be exchanged a frame at a time with nothing
|
|
// fed ahead (Bearer::clock_frame, as ImodemDsp._sync_pcm's exchange does
|
|
// it), none of them meeting an underrun, whose count is Python's to keep.
|
|
bool clocks(std::size_t count) const
|
|
{
|
|
const courier::Bearer &b = *bearer;
|
|
if (!b.ahead.empty() || b.frame_service) return false;
|
|
const std::size_t frames = ((b.partial_valid ? 1 : 0) + count) / courier::BEARER_SLOTS;
|
|
if (b.activated)
|
|
for (unsigned channel = 1; channel <= 2; ++channel)
|
|
if (b.fed[channel - 1] && b.channel_routed(channel)
|
|
&& b.rx[channel - 1].size() < frames)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
bool exchanges(std::size_t count) const { return settles(count) || clocks(count); }
|
|
|
|
// ImodemDsp.service_pending, for what the native path can do alone.
|
|
// 0: done. 2: nothing was changed and Python must do it. 3: the C5x ran on
|
|
// and the harness has to carry on from there (ImodemDsp.resume_service).
|
|
int service()
|
|
{
|
|
ImodemShared *sh = shared;
|
|
if (bearer->frame_service) return 2;
|
|
const uint16_t status = core->io(0x57);
|
|
const uint64_t writes = core->io_port_stat(0x5f).writes;
|
|
if (reply_due(status, writes)) return 2;
|
|
const std::size_t count = untaken(sh->tx_pending);
|
|
if (count && (!exchanges(count)
|
|
|| (!bearer->ahead.empty() && !bearer->lookahead_valid()))) return 2;
|
|
sh->needs_service = 0;
|
|
sh->tx_pending = 0;
|
|
sync_values(status);
|
|
if (count) exchange_taken(count);
|
|
bearer->prefeed(core);
|
|
// step_cycles(0): run what is still owed, one finished frame at a time.
|
|
if (sh->cycle_debt < 1) return 0;
|
|
int64_t remaining = int64_t(sh->cycle_debt);
|
|
int64_t elapsed = 0;
|
|
while (remaining > 0) {
|
|
const uint64_t before = core->cycle_count();
|
|
core->run_cycles(uint64_t(remaining), true);
|
|
const int64_t spent = int64_t(core->cycle_count() - before);
|
|
elapsed += spent;
|
|
const uint16_t now_status = core->io(0x57);
|
|
const std::size_t taken = untaken(0);
|
|
if (reply_due(now_status, core->io_port_stat(0x5f).writes)
|
|
|| (taken && !exchanges(taken))) {
|
|
sh->cycle_debt -= double(elapsed);
|
|
return 3;
|
|
}
|
|
sync_values(now_status);
|
|
if (taken) exchange_taken(taken);
|
|
remaining -= spent;
|
|
}
|
|
sh->cycle_debt -= double(elapsed);
|
|
return 0;
|
|
}
|
|
|
|
// The engine's poll hook: ImodemDsp's share of IsdnMachine.poll_timers
|
|
// (flush, prefeed, advance), when everything else in that poll is known to
|
|
// do nothing. 1: done; 0: run the whole poll in Python; 2/3: the poll was
|
|
// begun here and Python finishes it.
|
|
// IsdnMachine.push_far: take `vector` through the vector table. False for
|
|
// a null vector, with nothing changed.
|
|
static bool enter_interrupt(const PollCpu &cpu, unsigned vector)
|
|
{
|
|
uint32_t *r = cpu.regs;
|
|
uint8_t *mem = cpu.memory;
|
|
const std::size_t length = cpu.memory_size;
|
|
const uint32_t table = vector * 4;
|
|
const uint16_t offset = uint16_t(mem[table] | uint16_t(mem[table + 1]) << 8);
|
|
const uint16_t segment = uint16_t(mem[table + 2] | uint16_t(mem[table + 3]) << 8);
|
|
if (!offset && !segment) return false;
|
|
uint16_t sp = uint16_t(r[REG_SP]);
|
|
const uint16_t values[] = {uint16_t(r[REG_FLAGS]), uint16_t(r[REG_CS]), uint16_t(r[REG_IP])};
|
|
for (uint16_t value : values) {
|
|
sp = uint16_t(sp - 2);
|
|
const uint32_t address = uint32_t(((r[REG_SS] & 65535) * 16 + sp) % length);
|
|
mem[address] = uint8_t(value);
|
|
mem[(address + 1) % length] = uint8_t(value >> 8);
|
|
}
|
|
r[REG_SP] = sp;
|
|
r[REG_FLAGS] = uint16_t(r[REG_FLAGS]) & ~uint32_t(0x0200 | 0x0100);
|
|
r[REG_CS] = segment;
|
|
r[REG_IP] = offset;
|
|
return true;
|
|
}
|
|
|
|
// What is left of IsdnMachine.poll_timers once the DSP has been dealt with,
|
|
// and of its caller's tail: the services that raise a line when due, the
|
|
// sources that stay asserted, the 8254 wraps, and the interrupt the
|
|
// controller hands over if IF allows one.
|
|
bool tail(uint64_t now, uint32_t flags, const PollCpu &cpu)
|
|
{
|
|
PollState &s = *ps;
|
|
const uint64_t value = uint64_t(int64_t(now) - s.clock_origin);
|
|
if (s.rtos_on && value >= s.next_rtos) {
|
|
s.next_rtos = value + s.rtos_interval;
|
|
pic::raise(s, 11);
|
|
}
|
|
for (unsigned index = 0; index < s.asserted_count; ++index) pic::raise(s, s.asserted[index]);
|
|
if (s.mailbox_on && s.tout_on && core) {
|
|
// A fresh core (a reboot) starts its count again from zero.
|
|
const uint64_t expiries = core->timer_expiries();
|
|
if (expiries > s.tout_seen) pic::raise(s, 13);
|
|
s.tout_seen = expiries;
|
|
} else if (s.mailbox_on && value >= s.next_mailbox) {
|
|
s.next_mailbox = value + s.mailbox_interval;
|
|
pic::raise(s, 13);
|
|
}
|
|
const uint64_t ticks = value * s.pit_clock_hz / s.ips;
|
|
for (unsigned index = 0; index < 3; ++index) {
|
|
PitSlot &slot = s.pit[index];
|
|
if (ticks < slot.next_due) continue;
|
|
const uint64_t period = slot.initial ? slot.initial : 65536;
|
|
const uint64_t elapsed = ticks > slot.origin ? ticks - slot.origin : 0;
|
|
const uint64_t total = slot.programmed ? elapsed / period : 0;
|
|
const uint64_t fresh = total > slot.reported ? total - slot.reported : 0;
|
|
slot.reported = total;
|
|
slot.next_due = slot.programmed ? slot.origin + (total + 1) * period : PIT_INFINITE;
|
|
if (!fresh) continue;
|
|
s.timer_ticks += fresh;
|
|
for (unsigned entry = 0; entry < 4 && s.counter_irq[4 * index + entry] >= 0; ++entry) {
|
|
const unsigned line = unsigned(s.counter_irq[4 * index + entry]);
|
|
const PicChip &chip = s.pic[line >= 8 ? 1 : 0];
|
|
const bool already = (chip.irr >> (line % 8)) & 1;
|
|
s.coalesced[index] += fresh - (already ? 0 : 1);
|
|
pic::raise(s, line);
|
|
}
|
|
}
|
|
s.last_poll = now;
|
|
bool delivered = false;
|
|
if (flags & 0x200) {
|
|
const int vector = pic::pending_vector(s);
|
|
if (vector >= 0 && enter_interrupt(cpu, unsigned(vector))) {
|
|
++s.hardware_interrupts;
|
|
delivered = true;
|
|
}
|
|
}
|
|
return delivered;
|
|
}
|
|
|
|
int poll(uint64_t now, uint32_t flags, const PollCpu &cpu)
|
|
{
|
|
ImodemShared *sh = shared;
|
|
if (!sh || !core || !live || !bearer || !sh->elide_on) return 0;
|
|
if (sh->poll_dirty) { ++sh->poll_why[1]; return 0; }
|
|
if (sh->needs_service) { ++sh->poll_why[2]; return 0; }
|
|
if (now >= sh->elide_until) { ++sh->poll_why[3]; return 0; }
|
|
courier::Bearer &b = *bearer;
|
|
// No channel carries a call: nothing may be fed ahead, and each frame
|
|
// is settled as it finishes (see clocks()).
|
|
if (!b.activated || b.frame_service || (!b.channels && !b.ahead.empty())
|
|
|| !b.lookahead_valid()) {
|
|
++sh->poll_why[4];
|
|
return 0;
|
|
}
|
|
std::size_t flush = 0;
|
|
if (sh->tx_pending) {
|
|
flush = untaken(sh->tx_pending);
|
|
if (!exchanges(flush)) { ++sh->poll_why[5]; return 0; }
|
|
}
|
|
const std::size_t frames = ((b.partial_valid ? 1 : 0) + flush) / courier::BEARER_SLOTS;
|
|
// The line clock sends a frame when enough B1 octets have gathered.
|
|
if (b.tx[0].size() + frames + sh->clock_pending
|
|
>= sh->clock_seen + sh->clock_frame) { ++sh->poll_why[6]; return 0; }
|
|
if (sh->tx_pending) {
|
|
sh->tx_pending = 0;
|
|
if (flush) exchange_taken(flush);
|
|
b.prefeed(core);
|
|
}
|
|
if (b.ahead.size() * 2 <= b.lookahead_frames) b.prefeed(core);
|
|
int result = 0;
|
|
if (!advance(now, 0)) result = service();
|
|
if (result) {
|
|
++sh->polls_resumed;
|
|
return result;
|
|
}
|
|
const bool delivered = tail(now, flags, cpu);
|
|
++sh->polls_native;
|
|
// An interrupt taken costs the dispatch edge one retired instruction
|
|
// that executes nothing (see Core::poll_fn).
|
|
return delivered ? 4 : 1;
|
|
}
|
|
|
|
// true: the C5x is where the harness would have put it; false: the
|
|
// harness has to take over from here.
|
|
bool advance(uint64_t now, uint32_t quantum)
|
|
{
|
|
ImodemShared *sh = shared;
|
|
if (sh->needs_service) return false;
|
|
int64_t remaining;
|
|
if (wallclock) {
|
|
// The owed time is the wall clock's, not the instruction count's;
|
|
// a read's quantum still keeps a tight poll from stepping a
|
|
// handful of cycles at a time.
|
|
// Kept current, so the instruction-coupled advance resumes from
|
|
// here rather than owing the whole call once the bearer drops.
|
|
sh->dsp_instructions = now;
|
|
const uint64_t at = core->cycle_count();
|
|
if (wall_target <= at) return true;
|
|
remaining = int64_t(wall_target - at);
|
|
if (double(remaining) < double(quantum) * cycles_per_instruction) return true;
|
|
} else {
|
|
const int64_t elapsed = int64_t(now) - int64_t(sh->dsp_instructions);
|
|
if (elapsed < int64_t(quantum)) return true;
|
|
sh->dsp_instructions = now;
|
|
if (!elapsed) return true;
|
|
sh->cycle_debt += double(elapsed) * cycles_per_instruction;
|
|
if (sh->cycle_debt < 1) return true;
|
|
remaining = int64_t(sh->cycle_debt);
|
|
}
|
|
uint64_t spent_total = 0;
|
|
bool stop = false;
|
|
try {
|
|
while (remaining > 0) {
|
|
const uint64_t before = core->cycle_count();
|
|
core->run_cycles(uint64_t(remaining), true);
|
|
const uint64_t spent = core->cycle_count() - before;
|
|
const auto &octets = core->g711_tx();
|
|
const std::size_t cursor = std::min<std::size_t>(
|
|
sh->pcm_cursor, octets.size());
|
|
const std::size_t waiting = octets.size() - cursor;
|
|
const std::size_t queued = core->g711_rx_pending();
|
|
sh->tx_pending = uint32_t(waiting);
|
|
sh->rx_pending = uint32_t(queued);
|
|
// A finished frame is the harness's to exchange. With receive
|
|
// octets queued ahead the C5x can run on through several, up
|
|
// to flush_octets of transmit, and must stop before one that
|
|
// would find the queue empty.
|
|
const bool frame = waiting > 0
|
|
&& (sh->flush_octets == 0 || waiting >= sh->flush_octets
|
|
|| queued < 2);
|
|
const uint16_t status = core->io(0x57);
|
|
const uint64_t writes = core->io_port_stat(0x5f).writes;
|
|
if (sh->host_pending && !(status & 1)) {
|
|
++sh->consumed;
|
|
sh->host_pending = 0;
|
|
}
|
|
sh->tx_ready = !(status & 1);
|
|
const bool reply = !(status & 2) && !sh->rx_present
|
|
&& writes > sh->reply_writes;
|
|
spent_total += spent;
|
|
remaining -= int64_t(spent);
|
|
if (frame || reply) { stop = true; break; }
|
|
}
|
|
} catch (const std::exception &) {
|
|
stop = true;
|
|
}
|
|
if (!wallclock) sh->cycle_debt -= double(spent_total);
|
|
if (stop) sh->needs_service = 1;
|
|
return !stop;
|
|
}
|
|
};
|
|
}
|
|
|
|
extern "C" {
|
|
|
|
void *courier_laneio_create() { return new LaneHostIo(); }
|
|
|
|
void courier_laneio_destroy(void *context)
|
|
{
|
|
delete static_cast<LaneHostIo *>(context);
|
|
}
|
|
|
|
// `core` may be null, which declines every access.
|
|
void courier_laneio_configure(void *context, void *core, int live,
|
|
uint16_t command_port, uint16_t ack_port, uint16_t lane_first,
|
|
uint16_t banks, uint16_t dsp_first, uint16_t dsp_status)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
io->core = static_cast<C5xCore *>(core);
|
|
io->live = live != 0;
|
|
io->command_port = command_port;
|
|
io->ack_port = ack_port;
|
|
io->lane_first = lane_first;
|
|
io->banks = banks > 8 ? 8 : banks;
|
|
io->dsp_first = dsp_first;
|
|
io->dsp_status = dsp_status;
|
|
}
|
|
|
|
// What the harness knows about the mailbox ports that the model cannot read
|
|
// off the DSP's latch: see LaneHostIo::serve_mailbox.
|
|
void courier_laneio_configure_mailbox(void *context, int runtime, int pending,
|
|
int inbound, int overlay, unsigned overlay_status, int zero_ok,
|
|
unsigned status_cell)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
io->mb_runtime = runtime != 0;
|
|
io->mb_pending = pending != 0;
|
|
io->mb_inbound = inbound != 0;
|
|
io->mb_overlay = overlay != 0;
|
|
io->mb_overlay_status = uint8_t(overlay_status);
|
|
io->mb_zero_ok = zero_ok != 0;
|
|
io->mb_status_cell = uint16_t(status_cell);
|
|
}
|
|
|
|
// Publish (or withdraw) the answer to byte IN `port`; see LaneHostIo::fixed_valid.
|
|
void courier_laneio_set_fixed_input(void *context, unsigned port, int valid, unsigned value)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
if (port < 256) {
|
|
io->fixed_valid[port] = valid != 0;
|
|
io->fixed_value[port] = uint8_t(value);
|
|
}
|
|
}
|
|
|
|
void courier_laneio_set_invalidating_port(void *context, unsigned port)
|
|
{
|
|
if (port < 256) static_cast<LaneHostIo *>(context)->invalidates[port] = 1;
|
|
}
|
|
|
|
void courier_laneio_set_lane(void *context, unsigned index, uint8_t *byte)
|
|
{
|
|
if (index < 16) static_cast<LaneHostIo *>(context)->lane[index] = byte;
|
|
}
|
|
|
|
// The overlay strobes' fixed shape: the first window's bytes, its strobe, the
|
|
// start/end strobe, and every strobe that has a window. See serve_strobe.
|
|
void courier_laneio_set_overlay_window(void *context, uint8_t *window,
|
|
unsigned first, unsigned start, const uint8_t *strobes, unsigned count)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
io->ov_window = window;
|
|
io->ov_first = uint8_t(first);
|
|
io->ov_start = uint8_t(start);
|
|
std::memset(io->ov_window_strobe, 0, sizeof io->ov_window_strobe);
|
|
for (unsigned index = 0; index < count; ++index)
|
|
io->ov_window_strobe[strobes[index]] = 1;
|
|
}
|
|
|
|
// The boot download's fixed shape: the window behind each strobe, the first
|
|
// strobe, the checksum strobe (-1 for none), and the bytes behind 58..5e.
|
|
void courier_laneio_set_boot_window(void *context, unsigned strobe, uint8_t *window)
|
|
{
|
|
if (strobe < 256) static_cast<LaneHostIo *>(context)->boot_window[strobe] = window;
|
|
}
|
|
void courier_laneio_set_boot_lane(void *context, unsigned index, uint8_t *byte)
|
|
{
|
|
if (index < 4) static_cast<LaneHostIo *>(context)->boot_lane[index] = byte;
|
|
}
|
|
void courier_laneio_configure_boot(void *context, unsigned first, int checksum)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
io->boot_first = uint8_t(first);
|
|
io->boot_checksum = checksum;
|
|
}
|
|
// What the harness knows now: whether strobes and window stores may be served,
|
|
// and how many bootstrap bytes are still to come.
|
|
void courier_laneio_set_boot_state(void *context, int strobes, int stores, int64_t remaining)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
io->boot_strobes = strobes != 0;
|
|
io->boot_stores = stores != 0;
|
|
io->boot_remaining = remaining;
|
|
}
|
|
std::size_t courier_laneio_boot_pending(void *context)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
return io->boot_events.size() + (io->boot_error.empty() ? 0 : 1);
|
|
}
|
|
// The logged strobes, 10 bytes each: strobe, failed, window[8]. Returns the
|
|
// count; a loader failure's message goes to `error`.
|
|
std::size_t courier_laneio_take_boot(void *context, uint8_t *out, std::size_t capacity,
|
|
char *error, std::size_t error_size)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
const std::size_t count = std::min(capacity, io->boot_events.size());
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
const auto &event = io->boot_events[index];
|
|
out[10 * index] = event.strobe;
|
|
out[10 * index + 1] = event.failed;
|
|
std::memcpy(out + 10 * index + 2, event.window, 8);
|
|
}
|
|
io->boot_events.erase(io->boot_events.begin(), io->boot_events.begin() + count);
|
|
copy_error(error, error_size, io->boot_error.c_str());
|
|
io->boot_error.clear();
|
|
return count;
|
|
}
|
|
|
|
void courier_laneio_set_capture(void *context, int enabled)
|
|
{
|
|
static_cast<LaneHostIo *>(context)->cap_on = enabled != 0;
|
|
}
|
|
|
|
// The capture so far, 16 bytes a record (instruction count, kind, port or
|
|
// bank, value, all little-endian); returns the records copied and drops them.
|
|
std::size_t courier_laneio_take_capture(void *context, uint8_t *out, std::size_t capacity)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
const std::size_t count = std::min(capacity, io->cap.size());
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
const auto &entry = io->cap[index];
|
|
uint8_t *row = out + 16 * index;
|
|
std::memcpy(row, &entry.now, 8);
|
|
row[8] = entry.kind;
|
|
row[9] = entry.port;
|
|
row[10] = uint8_t(entry.value);
|
|
row[11] = uint8_t(entry.value >> 8);
|
|
std::memset(row + 12, 0, 4);
|
|
}
|
|
io->cap.erase(io->cap.begin(), io->cap.begin() + count);
|
|
return count;
|
|
}
|
|
|
|
std::size_t courier_laneio_capture_size(void *context)
|
|
{
|
|
return static_cast<LaneHostIo *>(context)->cap.size();
|
|
}
|
|
|
|
void courier_laneio_set_overlay_strobes(void *context, int enabled)
|
|
{
|
|
static_cast<LaneHostIo *>(context)->ov_strobes = enabled != 0;
|
|
}
|
|
|
|
std::size_t courier_laneio_overlay_pending(void *context)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
return io->ov_events.size() + (io->ov_error.empty() ? 0 : 1);
|
|
}
|
|
|
|
// Hand over the logged strobes, 16 bytes each: strobe, committed, base (LE),
|
|
// half[4], block[8]. *status is the overlay status byte as the strobes left it,
|
|
// and a commit failure's message goes to `error`. Returns the event count.
|
|
std::size_t courier_laneio_take_overlay(void *context, uint8_t *out,
|
|
std::size_t capacity, unsigned *status, char *error, std::size_t error_size)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
const std::size_t count = std::min(capacity, io->ov_events.size());
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
const auto &event = io->ov_events[index];
|
|
uint8_t *row = out + 16 * index;
|
|
row[0] = event.strobe;
|
|
row[1] = event.committed;
|
|
row[2] = uint8_t(event.base);
|
|
row[3] = uint8_t(event.base >> 8);
|
|
std::memcpy(row + 4, event.half, 4);
|
|
std::memcpy(row + 8, event.block, 8);
|
|
}
|
|
io->ov_events.erase(io->ov_events.begin(), io->ov_events.begin() + count);
|
|
*status = io->mb_overlay_status;
|
|
copy_error(error, error_size, io->ov_error.c_str());
|
|
io->ov_error.clear();
|
|
return count;
|
|
}
|
|
|
|
// direction 1 = IN, 2 = OUT. Returns 1 when served; the answer to an IN is in
|
|
// *out. Byte accesses only.
|
|
int courier_laneio_access(void *context, int direction, uint16_t port,
|
|
int size, uint16_t value, uint64_t now, uint16_t *out)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
if (!io->core || size != 1 || port >= 256) return 0;
|
|
if (direction == 1) {
|
|
if (io->fixed_valid[port]) {
|
|
++io->in_count[port];
|
|
*out = io->fixed_value[port];
|
|
return 1;
|
|
}
|
|
} else if (io->invalidates[port]) {
|
|
std::memset(io->fixed_valid, 0, sizeof io->fixed_valid);
|
|
}
|
|
if ((port == 0x1c || port == 0x1e) && io->serve_mailbox(direction, port, value, out))
|
|
return 1;
|
|
const uint16_t mask = uint16_t((1u << io->banks) - 1);
|
|
const uint16_t lane_last = uint16_t(io->lane_first + 4 * io->banks);
|
|
if (direction == 1) {
|
|
if (!io->live) return 0;
|
|
uint16_t answer;
|
|
if (port == io->command_port || port == io->ack_port) {
|
|
const uint16_t status = io->core->io(io->dsp_status);
|
|
answer = port == io->command_port
|
|
? uint16_t(0xC0 | (~status & mask))
|
|
: uint16_t(0xC0 | (~(status >> 8) & mask));
|
|
} else if (port >= io->lane_first && port < lane_last) {
|
|
const unsigned offset = port - io->lane_first;
|
|
const uint16_t word = io->core->io_output(
|
|
uint16_t(io->dsp_first + offset / 4));
|
|
answer = (word >> ((offset & 2) ? 8 : 0)) & 0xff;
|
|
io->record(now, 3, uint8_t(port), answer);
|
|
} else {
|
|
return 0;
|
|
}
|
|
++io->in_count[port];
|
|
*out = answer & 0xff;
|
|
return 1;
|
|
}
|
|
value &= 0xff;
|
|
if (!io->live && port == io->command_port && io->serve_boot_strobe(uint8_t(value))) {
|
|
++io->out_count[port];
|
|
io->out_last[port] = value;
|
|
io->out_seen[port] = 1;
|
|
return 1;
|
|
}
|
|
if (port >= 0x58 && port <= 0x5e && !(port & 1)) {
|
|
uint8_t *byte = io->boot_stores ? io->boot_lane[(port - 0x58) / 2] : nullptr;
|
|
if (!byte) return 0;
|
|
*byte = uint8_t(value);
|
|
++io->out_count[port];
|
|
io->out_last[port] = value;
|
|
io->out_seen[port] = 1;
|
|
return 1;
|
|
}
|
|
if (port == io->command_port || port == io->ack_port) {
|
|
if (!io->live) return 0;
|
|
uint16_t bits = value & mask;
|
|
if (port == io->command_port) {
|
|
for (unsigned bank = 0; bank < io->banks; ++bank)
|
|
if (bits & (1u << bank)) {
|
|
io->core->set_io(uint16_t(io->dsp_first + bank),
|
|
io->lane_word(bank));
|
|
io->record(now, 1, uint8_t(bank), io->lane_word(bank));
|
|
}
|
|
} else {
|
|
io->record(now, 2, uint8_t(port), bits);
|
|
bits = uint16_t(bits << 8);
|
|
}
|
|
io->core->set_io(io->dsp_status,
|
|
uint16_t(io->core->io(io->dsp_status) | bits));
|
|
} else if (port >= io->lane_first && port < lane_last
|
|
&& !((port - io->lane_first) & 1)) {
|
|
uint8_t *byte = io->lane[(port - io->lane_first) / 2];
|
|
if (!byte) return 0;
|
|
*byte = uint8_t(value);
|
|
} else {
|
|
return 0;
|
|
}
|
|
++io->out_count[port];
|
|
io->out_last[port] = value;
|
|
io->out_seen[port] = 1;
|
|
return 1;
|
|
}
|
|
|
|
void *courier_imodemio_create() { return new ImodemHostIo(); }
|
|
|
|
void courier_imodemio_destroy(void *context)
|
|
{
|
|
delete static_cast<ImodemHostIo *>(context);
|
|
}
|
|
|
|
// `core` may be null, which declines everything but lane stores.
|
|
void courier_imodemio_configure(void *context, void *shared, void *core,
|
|
int live, double cycles_per_instruction, unsigned read_quantum)
|
|
{
|
|
auto *io = static_cast<ImodemHostIo *>(context);
|
|
io->shared = static_cast<ImodemShared *>(shared);
|
|
io->core = static_cast<C5xCore *>(core);
|
|
io->live = live != 0;
|
|
io->cycles_per_instruction = cycles_per_instruction;
|
|
io->read_quantum = read_quantum;
|
|
}
|
|
|
|
// The harness's own advance, for passes that are not port accesses: 1 when the
|
|
// C5x is where the harness would have put it, 0 when it must call
|
|
// service_pending (needs_service is then set).
|
|
int courier_imodemio_advance(void *context, uint64_t now, unsigned quantum)
|
|
{
|
|
auto *io = static_cast<ImodemHostIo *>(context);
|
|
if (!io->shared || !io->core || !io->live) return 0;
|
|
return io->advance(now, quantum) ? 1 : 0;
|
|
}
|
|
|
|
// IsdnMachine._advance_dsp: the advance, and the service it hands back when
|
|
// the native model can do it. 1: done; 2: nothing changed and the harness
|
|
// runs ImodemDsp.service_pending; 3: begun here, ImodemDsp.resume_service
|
|
// finishes it.
|
|
int courier_imodemio_advance_serve(void *context, uint64_t now, unsigned quantum)
|
|
{
|
|
auto *io = static_cast<ImodemHostIo *>(context);
|
|
if (!io->shared || !io->core || !io->live) return 2;
|
|
if (io->advance(now, quantum)) return 1;
|
|
if (!io->bearer) return 2;
|
|
const int result = io->service();
|
|
return result ? result : 1;
|
|
}
|
|
|
|
// Serve the ports without stepping the C5x, which the harness is pacing
|
|
// against wall time (see ImodemHostIo::wallclock).
|
|
void courier_imodemio_set_wallclock(void *context, int enabled)
|
|
{
|
|
static_cast<ImodemHostIo *>(context)->wallclock = enabled != 0;
|
|
}
|
|
|
|
void courier_imodemio_set_wall_target(void *context, uint64_t cycles)
|
|
{
|
|
static_cast<ImodemHostIo *>(context)->wall_target = cycles;
|
|
}
|
|
|
|
void courier_imodemio_set_poll_state(void *context, void *state)
|
|
{
|
|
static_cast<ImodemHostIo *>(context)->ps = static_cast<PollState *>(state);
|
|
}
|
|
|
|
void courier_imodemio_set_bearer(void *context, void *bearer)
|
|
{
|
|
static_cast<ImodemHostIo *>(context)->bearer = static_cast<courier::Bearer *>(bearer);
|
|
}
|
|
|
|
int courier_imodemio_poll(void *context, uint64_t now, uint32_t flags, void *cpu)
|
|
{
|
|
return static_cast<ImodemHostIo *>(context)->poll(now, flags, *static_cast<PollCpu *>(cpu));
|
|
}
|
|
|
|
int courier_imodemio_access(void *context, int direction, uint16_t port,
|
|
int size, uint16_t value, uint64_t now, uint16_t *out)
|
|
{
|
|
auto *io = static_cast<ImodemHostIo *>(context);
|
|
ImodemShared *sh = io->shared;
|
|
if (io->ps && port >= 0xF020 && io->serve_pic(direction, port, value, out)) return 1;
|
|
if (!sh || size != 1 || port >= 0x60) return 0;
|
|
const bool lane = port >= 0x40 && port < 0x58 && !(port & 1);
|
|
if (direction == 2 && lane) {
|
|
sh->lanes[port] = uint8_t(value);
|
|
++sh->lane_writes;
|
|
++io->out_count[port];
|
|
return 1;
|
|
}
|
|
if (!io->core || !io->live) return 0;
|
|
if (port == 0x1c || port == 0x1e) return io->serve_host_port(direction, port, value, now, out);
|
|
if (direction == 1) {
|
|
if (port != 0x18 && port != 0x1a && !lane) return 0;
|
|
if (!io->advance(now, io->read_quantum)) return 0;
|
|
uint16_t answer;
|
|
if (port == 0x18 || port == 0x1a) {
|
|
const uint16_t status = io->core->io(0x56);
|
|
// 0x18 bit 7: a stream word, PA6 bit 7 cleared by the sender
|
|
// at 0x8980 (ImodemDsp.read).
|
|
answer = port == 0x18 ? uint16_t(0x40 | (~status & 0xbf))
|
|
: uint16_t(0xc0 | (~(status >> 8) & 0x3f));
|
|
} else {
|
|
const unsigned offset = port - 0x40;
|
|
const uint16_t word = io->core->io_output(uint16_t(0x58 + offset / 4));
|
|
answer = (word >> ((offset & 2) ? 8 : 0)) & 0xff;
|
|
}
|
|
++io->in_count[port];
|
|
*out = answer & 0xff;
|
|
return 1;
|
|
}
|
|
// The ack/command latches: 0xff on 0x18 is the loader's reset request.
|
|
if ((port != 0x18 && port != 0x1a) || (port == 0x18 && (value & 0xff) == 0xff))
|
|
return 0;
|
|
if (!io->advance(now, 0)) return 0;
|
|
uint16_t bits = value & 0x3f;
|
|
if (port == 0x18) {
|
|
bits |= value & 0x80; // a stream acknowledgement (ImodemDsp.write)
|
|
for (unsigned bank = 0; bank < 6; ++bank)
|
|
if (bits & (1u << bank))
|
|
io->core->set_io(uint16_t(0x58 + bank),
|
|
io->word(uint16_t(0x40 + 4 * bank)));
|
|
} else {
|
|
bits = uint16_t(bits << 8);
|
|
}
|
|
io->core->set_io(0x56, uint16_t(io->core->io(0x56) | bits));
|
|
++sh->latch_writes;
|
|
++io->out_count[port];
|
|
return 1;
|
|
}
|
|
|
|
void courier_imodemio_take_counts(void *context, uint32_t *in_counts,
|
|
uint32_t *out_counts)
|
|
{
|
|
auto *io = static_cast<ImodemHostIo *>(context);
|
|
std::memcpy(in_counts, io->in_count, sizeof io->in_count);
|
|
std::memcpy(out_counts, io->out_count, sizeof io->out_count);
|
|
std::memset(io->in_count, 0, sizeof io->in_count);
|
|
std::memset(io->out_count, 0, sizeof io->out_count);
|
|
}
|
|
|
|
// A write the harness handled itself supersedes the last one served here.
|
|
void courier_laneio_clear_seen(void *context, uint16_t port)
|
|
{
|
|
if (port < 256) static_cast<LaneHostIo *>(context)->out_seen[port] = 0;
|
|
}
|
|
|
|
// Copy out and clear the tallies of accesses served so far.
|
|
void courier_laneio_take_counts(void *context, uint32_t *in_counts,
|
|
uint32_t *out_counts, uint16_t *last, uint8_t *seen)
|
|
{
|
|
auto *io = static_cast<LaneHostIo *>(context);
|
|
std::memcpy(in_counts, io->in_count, sizeof io->in_count);
|
|
std::memcpy(out_counts, io->out_count, sizeof io->out_count);
|
|
std::memcpy(last, io->out_last, sizeof io->out_last);
|
|
std::memcpy(seen, io->out_seen, sizeof io->out_seen);
|
|
std::memset(io->in_count, 0, sizeof io->in_count);
|
|
std::memset(io->out_count, 0, sizeof io->out_count);
|
|
std::memset(io->out_seen, 0, sizeof io->out_seen);
|
|
}
|
|
|
|
void *courier_c5x_create()
|
|
{
|
|
try { return new C5xCore(); } catch (...) { return nullptr; }
|
|
}
|
|
|
|
// Keep the original constructor ABI for existing Courier clients.
|
|
void *courier_c5x_create_model(int model)
|
|
{
|
|
if (model != 51 && model != 52 && model != 53) return nullptr;
|
|
try { return new C5xCore(model == 53 ? C5xCore::Model::C53 : model == 52 ? C5xCore::Model::C52 : C5xCore::Model::C51); }
|
|
catch (...) { return nullptr; }
|
|
}
|
|
|
|
void courier_c5x_set_separate_global_memory(void *handle, int enabled)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_separate_global_memory(enabled != 0);
|
|
}
|
|
|
|
void courier_c5x_destroy(void *handle) { delete static_cast<C5xCore *>(handle); }
|
|
|
|
void courier_c5x_reset(void *handle)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->reset();
|
|
}
|
|
|
|
void courier_c5x_get_delay_move_state(void *handle, uint64_t *values)
|
|
{
|
|
if (!handle || !values) return;
|
|
const auto state = static_cast<C5xCore *>(handle)->delay_move_state();
|
|
std::copy(state.begin(), state.end(), values);
|
|
}
|
|
|
|
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_host_io_base(void *handle, uint16_t base)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_host_io_base(base);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// The core has always carried I/O callbacks; only the C entry point was
|
|
// missing, so a harness outside C++ could not install a device. The MICA board
|
|
// needs one: its host port is at forced MMRs 0x50..0x52, which this core
|
|
// already routes into the I/O space, and what answers there is a window onto
|
|
// i960 memory rather than anything the Courier has.
|
|
//
|
|
// A null callback restores the default, which is the core's own port storage.
|
|
// The read callback is asked about every port, so a device that claims only
|
|
// some of them should return the stored value for the rest -
|
|
// courier_c5x_get_io answers that.
|
|
void courier_c5x_set_io_hooks(
|
|
void *handle, uint16_t (*read)(void *user, uint16_t port),
|
|
void (*write)(void *user, uint16_t port, uint16_t value), void *user)
|
|
{
|
|
if (!handle) return;
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
C5xCore::IoRead on_read;
|
|
C5xCore::IoWrite on_write;
|
|
if (read)
|
|
on_read = [read, user](uint16_t port) { return read(user, port); };
|
|
if (write)
|
|
on_write = [write, user](uint16_t port, uint16_t value) { write(user, port, value); };
|
|
core->set_io_callbacks(std::move(on_read), std::move(on_write));
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
int courier_c5x_step_cycles(void *handle, uint64_t count,
|
|
uint64_t *instructions, uint64_t *cycles,
|
|
char *error, std::size_t error_size)
|
|
{
|
|
try {
|
|
if (!handle) throw std::runtime_error("null C5x handle");
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
const uint64_t before_instructions = core->instruction_count();
|
|
const uint64_t before_cycles = core->cycle_count();
|
|
core->run_cycles(count);
|
|
if (instructions)
|
|
*instructions = core->instruction_count() - before_instructions;
|
|
if (cycles) *cycles = core->cycle_count() - before_cycles;
|
|
return 0;
|
|
} catch (const std::exception &exception) {
|
|
copy_error(error, error_size, exception.what());
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
// BandLimitedResampler's retuned state in native memory (resample.Timed).
|
|
void *courier_timed_create(double norm)
|
|
{
|
|
auto *timed = new courier::resample::Timed();
|
|
timed->norm = norm;
|
|
return timed;
|
|
}
|
|
void courier_timed_destroy(void *handle) { delete static_cast<courier::resample::Timed *>(handle); }
|
|
void courier_timed_seed(void *handle, const double *history, std::size_t history_length,
|
|
const double *times, std::size_t times_length, double input_time, double next_time,
|
|
double input_rate, double output_rate)
|
|
{
|
|
auto &timed = *static_cast<courier::resample::Timed *>(handle);
|
|
timed.history.assign(history, history + history_length);
|
|
timed.times.assign(times, times + times_length);
|
|
timed.input_time = input_time;
|
|
timed.next_time = next_time;
|
|
timed.input_rate = input_rate;
|
|
timed.output_rate = output_rate;
|
|
}
|
|
// One conversion, committed; the samples are left for courier_timed_take.
|
|
std::size_t courier_timed_convert(void *handle, const int16_t *samples, std::size_t count,
|
|
double input_rate, double output_rate, const double *kernel)
|
|
{
|
|
auto &timed = *static_cast<courier::resample::Timed *>(handle);
|
|
timed.run(samples, count, input_rate, output_rate, kernel);
|
|
timed.commit();
|
|
return timed.out.size();
|
|
}
|
|
void courier_timed_take(void *handle, int16_t *out)
|
|
{
|
|
const auto &timed = *static_cast<courier::resample::Timed *>(handle);
|
|
std::copy(timed.out.begin(), timed.out.end(), out);
|
|
}
|
|
uint64_t courier_timed_converted(void *handle)
|
|
{
|
|
return static_cast<courier::resample::Timed *>(handle)->converted;
|
|
}
|
|
|
|
// resample.BandLimitedResampler._convert_retuned's per-sample loop; see
|
|
// resample.hpp. `kernel` is the PHASES x TAPS table, row after row.
|
|
std::size_t courier_resample_retuned(const double *history, std::size_t history_length,
|
|
const double *times, std::size_t times_length, const double *kernel,
|
|
double input_rate, double output_rate, double instant, double support,
|
|
double cutoff, double norm,
|
|
int16_t *out, std::size_t capacity, double *instant_out, int *status)
|
|
{
|
|
return courier::resample::retuned(history, history_length, times, times_length,
|
|
kernel, input_rate, output_rate, instant, support, cutoff, norm, out,
|
|
capacity, instant_out, status);
|
|
}
|
|
|
|
// Step one instruction at a time until a condition holds, at most `limit`
|
|
// times: mode 0, until data cell `a` reads `b`; mode 1, until the writes to
|
|
// I/O port `a` exceed `b`. Returns 1 when it held (and where the loop stopped
|
|
// is *steps), 0 when the limit ran out, -1 on an error.
|
|
int courier_c5x_step_until(void *handle, int mode, uint16_t a, uint64_t b,
|
|
uint64_t limit, uint64_t *steps, char *error, std::size_t error_size)
|
|
{
|
|
try {
|
|
if (!handle) throw std::runtime_error("null C5x handle");
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
for (uint64_t index = 0; index < limit; ++index) {
|
|
core->run(1);
|
|
const bool held = mode == 0 ? core->data(a) == b
|
|
: core->io_port_stat(a).writes > b;
|
|
if (held) { *steps = index + 1; return 1; }
|
|
}
|
|
*steps = limit;
|
|
return 0;
|
|
} catch (const std::exception &exception) {
|
|
copy_error(error, error_size, exception.what());
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
// One service's DSP work for the analog worker: run the cycles it is owed,
|
|
// then report what the harness looks at straight afterwards, so that it costs
|
|
// one call. out[0] instructions, out[1] cycles, out[2] writes to `port`,
|
|
// out[3] whether the call TDM is active.
|
|
int courier_c5x_service_step(void *handle, uint64_t count, uint16_t port,
|
|
uint64_t *out, char *error, std::size_t error_size)
|
|
{
|
|
try {
|
|
if (!handle) throw std::runtime_error("null C5x handle");
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
const uint64_t before_instructions = core->instruction_count();
|
|
const uint64_t before_cycles = core->cycle_count();
|
|
core->run_cycles(count);
|
|
out[0] = core->instruction_count() - before_instructions;
|
|
out[1] = core->cycle_count() - before_cycles;
|
|
out[2] = core->io_port_stat(port).writes;
|
|
out[3] = core->call_tdm_active() ? 1 : 0;
|
|
return 0;
|
|
} catch (const std::exception &exception) {
|
|
copy_error(error, error_size, exception.what());
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
std::size_t courier_c5x_advance_imodem(void *handle, uint64_t count,
|
|
std::size_t tx_start, uint8_t *tx, std::size_t tx_capacity,
|
|
uint64_t *values, std::size_t value_count,
|
|
char *error, std::size_t error_size)
|
|
{
|
|
try {
|
|
if (!handle || !values || value_count < 6)
|
|
throw std::runtime_error("invalid I-modem advance arguments");
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
const uint64_t before_instructions = core->instruction_count();
|
|
const uint64_t before_cycles = core->cycle_count();
|
|
core->run_cycles(count, true);
|
|
const auto &words = core->g711_tx();
|
|
tx_start = std::min(tx_start, words.size());
|
|
const std::size_t available = words.size() - tx_start;
|
|
if (tx)
|
|
std::copy_n(words.begin() + tx_start,
|
|
std::min(available, tx_capacity), tx);
|
|
const auto &port = core->io_port_stat(0x5f);
|
|
values[0] = core->instruction_count() - before_instructions;
|
|
values[1] = core->cycle_count() - before_cycles;
|
|
values[2] = core->io(0x57);
|
|
values[3] = core->io_output(0x5e);
|
|
values[4] = core->io_output(0x5f);
|
|
values[5] = port.writes;
|
|
return available;
|
|
} catch (const std::exception &exception) {
|
|
copy_error(error, error_size, exception.what());
|
|
return std::size_t(-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_queue_io_rx(
|
|
void *handle, uint16_t port, const uint16_t *words, std::size_t count)
|
|
{
|
|
if (handle && words) static_cast<C5xCore *>(handle)->queue_io_rx(port, words, count);
|
|
}
|
|
|
|
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_queue_line_rx(void *handle, const uint16_t *samples, std::size_t count)
|
|
{
|
|
if (handle && samples) static_cast<C5xCore *>(handle)->queue_line_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);
|
|
}
|
|
|
|
uint64_t courier_c5x_get_g711_rx_underruns(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->g711_rx_underruns() : 0;
|
|
}
|
|
|
|
uint64_t courier_c5x_get_timer_expiries(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->timer_expiries() : 0;
|
|
}
|
|
|
|
std::size_t courier_c5x_get_g711_rx_pending(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->g711_rx_pending() : 0;
|
|
}
|
|
|
|
std::size_t courier_c5x_drop_g711_rx_tail(void *handle, std::size_t count)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->drop_g711_rx_tail(count) : 0;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// What the I-modem harness reads from the core each time it services the DSP:
|
|
// the host-port status word, the reply registers and the count of writes to
|
|
// the reply strobe, and the transmitted G.711 octets since `tx_start`. With a
|
|
// non-zero `waiting` that is `waiting` octets when that many are there (the
|
|
// native port model's own count), otherwise everything since `tx_start`.
|
|
// Returns the number of octets copied, or SIZE_MAX when they did not fit.
|
|
std::size_t courier_c5x_imodem_collect(void *handle, std::size_t tx_start,
|
|
std::size_t waiting, uint8_t *out, std::size_t capacity, uint64_t *values)
|
|
{
|
|
if (!handle) return 0;
|
|
C5xCore *core = static_cast<C5xCore *>(handle);
|
|
values[0] = core->io(0x57);
|
|
values[1] = core->io_output(0x5e);
|
|
values[2] = core->io_output(0x5f);
|
|
values[3] = core->io_port_stat(0x5f).writes;
|
|
const auto &words = core->g711_tx();
|
|
tx_start = std::min(tx_start, words.size());
|
|
const std::size_t available = words.size() - tx_start;
|
|
const std::size_t count = waiting && available >= waiting ? waiting : available;
|
|
if (count > capacity) return SIZE_MAX;
|
|
std::copy_n(words.begin() + tx_start, count, out);
|
|
return count;
|
|
}
|
|
|
|
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_data_event_limit(void *handle, std::size_t limit)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_data_event_limit(limit);
|
|
}
|
|
|
|
void courier_c5x_set_data_trace_range(void *handle, unsigned first, unsigned last, int enabled)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_data_trace_range(first, last, enabled != 0);
|
|
}
|
|
|
|
void courier_c5x_set_step_probes(void *handle, int enabled)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_step_probes(enabled != 0);
|
|
}
|
|
|
|
void courier_c5x_set_coverage(void *handle, int enabled)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_coverage(enabled != 0);
|
|
}
|
|
|
|
uint64_t courier_c5x_get_first_exec(void *handle, unsigned pc)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->first_exec(uint16_t(pc)) : 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_trace_register(void *handle, int reg, std::size_t capacity)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->trace_register(reg, capacity);
|
|
}
|
|
|
|
// The traced writes, 12 bytes each: cycle (8), pc (2), value (2).
|
|
std::size_t courier_c5x_take_register_trace(void *handle, uint8_t *out, std::size_t capacity)
|
|
{
|
|
if (!handle) return 0;
|
|
const auto &trace = static_cast<C5xCore *>(handle)->register_trace();
|
|
const std::size_t count = std::min(capacity, trace.size());
|
|
for (std::size_t index = 0; index < count; ++index) {
|
|
std::memcpy(out + 12 * index, &trace[index].cycle, 8);
|
|
std::memcpy(out + 12 * index + 8, &trace[index].pc, 2);
|
|
std::memcpy(out + 12 * index + 10, &trace[index].value, 2);
|
|
}
|
|
return trace.size();
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
uint64_t courier_c5x_get_io_port_writes(void *handle, uint16_t port)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->io_port_stat(port).writes : 0;
|
|
}
|
|
|
|
// serial_state's queued - consumed: the codec pair when `codec` is set, else
|
|
// the line receive queue.
|
|
int64_t courier_c5x_serial_backlog(void *handle, int codec)
|
|
{
|
|
if (!handle) return 0;
|
|
const auto state = static_cast<C5xCore *>(handle)->serial_state();
|
|
return codec ? int64_t(state.codec_rx_queued - state.codec_rx_consumed)
|
|
: int64_t(state.rx_queued - state.rx_consumed);
|
|
}
|
|
|
|
uint16_t courier_c5x_get_pc(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->program_counter() : 0;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
uint64_t courier_c5x_get_line_tx_writes(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->serial_state().line_tx_writes : 0;
|
|
}
|
|
|
|
std::size_t courier_c5x_get_line_tx_samples(void *handle, std::size_t first,
|
|
uint16_t *values, std::size_t capacity)
|
|
{
|
|
if (!handle) return 0;
|
|
const auto &samples = static_cast<C5xCore *>(handle)->line_tx_samples();
|
|
if (first >= samples.size()) return 0;
|
|
const std::size_t count = std::min(capacity, samples.size() - first);
|
|
std::copy_n(samples.begin() + first, count, values);
|
|
return count;
|
|
}
|
|
|
|
std::size_t courier_c5x_get_line_tx_clock_events(void *handle, uint64_t *values, std::size_t count)
|
|
{
|
|
if (!handle) return 0;
|
|
const auto &events = static_cast<C5xCore *>(handle)->line_tx_clock_events();
|
|
if (values)
|
|
for (std::size_t i = 0; i < std::min(count, events.size()); ++i) {
|
|
values[2*i] = events[i][0];
|
|
values[2*i+1] = events[i][1];
|
|
}
|
|
return events.size();
|
|
}
|
|
|
|
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_set_pc_capture(void *handle, uint16_t pc, const uint16_t *addresses, std::size_t count)
|
|
{
|
|
if (!handle || count > 256 || (count && !addresses)) return;
|
|
std::vector<uint16_t> selected;
|
|
if (count) selected.assign(addresses, addresses + count);
|
|
static_cast<C5xCore *>(handle)->set_pc_capture(pc, selected);
|
|
}
|
|
|
|
std::size_t courier_c5x_get_pc_capture_count(void *handle)
|
|
{
|
|
return handle ? static_cast<C5xCore *>(handle)->pc_captures().size() : 0;
|
|
}
|
|
|
|
std::size_t courier_c5x_get_pc_capture(void *handle, std::size_t index, uint64_t *values, std::size_t count)
|
|
{
|
|
if (!handle || !values) return 0;
|
|
const auto &captures = static_cast<C5xCore *>(handle)->pc_captures();
|
|
if (index >= captures.size()) return 0;
|
|
const auto &capture = captures[index];
|
|
const auto copied = std::min(count, capture.size());
|
|
std::copy_n(capture.begin(), copied, values);
|
|
return copied;
|
|
}
|
|
|
|
void courier_c5x_clear_pc_captures(void *handle)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->clear_pc_captures();
|
|
}
|
|
|
|
void courier_c5x_clear_pc_trace(void *handle)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->clear_pc_trace();
|
|
}
|
|
|
|
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] >> 48;
|
|
values[1] = (trace[index] >> 32) & 0xffff;
|
|
if (count >= 3) values[2] = trace[index] & 0xffffffff;
|
|
}
|
|
|
|
void courier_c5x_get_serial_state(void *handle, uint64_t *values, std::size_t count)
|
|
{
|
|
if (!handle || !values || count < 65) 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_frame_suppressed,
|
|
serial.shadow_dp,
|
|
serial.last_dp_pc, serial.last_dp_value, serial.last_dp_source,
|
|
serial.stray_cala_pc, serial.stray_cala_target, serial.stray_cala_dp,
|
|
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_configure_si3034_codec(void *handle)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->configure_si3034_codec();
|
|
}
|
|
|
|
extern "C" void courier_c5x_set_si3034_line(void *handle, int connected, int off_hook, int ringing)
|
|
{
|
|
if (handle) static_cast<C5xCore *>(handle)->set_si3034_line(connected != 0, off_hook != 0, ringing != 0);
|
|
}
|
|
|
|
extern "C" void courier_c5x_get_codec_registers(void *handle, uint16_t *values, std::size_t count)
|
|
{
|
|
if (!handle || !values) return;
|
|
for (std::size_t index = 0; index < std::min(count, std::size_t(32)); ++index)
|
|
values[index] = static_cast<C5xCore *>(handle)->codec_register(unsigned(index));
|
|
}
|
|
|
|
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 < 36) 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)), codec.rx_empty_frames,
|
|
};
|
|
std::copy(std::begin(result), std::end(result), values);
|
|
}
|
|
|
|
// ---- the B-channel path (bearer.hpp); see native_bearer.py for the Python side ----
|
|
|
|
using courier::Bearer;
|
|
|
|
extern "C" {
|
|
|
|
void *courier_bearer_create() { return new Bearer(); }
|
|
void courier_bearer_destroy(void *handle) { delete static_cast<Bearer *>(handle); }
|
|
|
|
// Streams: kind 0 = rx queue (channel 0/1), 1 = tx, 2 = heard, 3 = pcm_tx (channel ignored).
|
|
std::size_t courier_bearer_length(void *handle, int kind, int channel)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
switch (kind) {
|
|
case 0: return b->rx[channel].size();
|
|
case 1: return b->tx[channel].size();
|
|
case 2: return b->heard[channel].size();
|
|
default: return b->pcm_tx.size();
|
|
}
|
|
}
|
|
|
|
// Copy [start, stop) of a tx/heard/pcm stream, or of the rx queue, to `out`.
|
|
std::size_t courier_bearer_read(void *handle, int kind, int channel,
|
|
std::size_t start, std::size_t stop, uint8_t *out)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
if (kind == 0) {
|
|
const auto &queue = b->rx[channel];
|
|
stop = std::min(stop, queue.size());
|
|
for (std::size_t index = start; index < stop; ++index) *out++ = queue[index];
|
|
return stop > start ? stop - start : 0;
|
|
}
|
|
const std::vector<uint8_t> &stream =
|
|
kind == 1 ? b->tx[channel] : kind == 2 ? b->heard[channel] : b->pcm_tx;
|
|
stop = std::min(stop, stream.size());
|
|
if (start >= stop) return 0;
|
|
std::memcpy(out, stream.data() + start, stop - start);
|
|
return stop - start;
|
|
}
|
|
|
|
// The stream's length, and a copy of everything from `start` on (up to `capacity`).
|
|
std::size_t courier_bearer_tail(void *handle, int kind, int channel,
|
|
std::size_t start, uint8_t *out, std::size_t capacity)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
const std::vector<uint8_t> &stream =
|
|
kind == 1 ? b->tx[channel] : kind == 2 ? b->heard[channel] : b->pcm_tx;
|
|
const std::size_t total = stream.size();
|
|
if (start < total)
|
|
std::memcpy(out, stream.data() + start, std::min(total - start, capacity));
|
|
return total;
|
|
}
|
|
|
|
void courier_bearer_append(void *handle, int kind, int channel,
|
|
const uint8_t *data, std::size_t count)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
switch (kind) {
|
|
case 0: b->rx[channel].insert(b->rx[channel].end(), data, data + count); break;
|
|
case 1: b->tx[channel].insert(b->tx[channel].end(), data, data + count); break;
|
|
case 2: b->heard[channel].insert(b->heard[channel].end(), data, data + count); break;
|
|
default: b->pcm_tx.insert(b->pcm_tx.end(), data, data + count); break;
|
|
}
|
|
}
|
|
|
|
// rx queue: pop the front (returns -1 when empty) or push to the front.
|
|
int courier_bearer_rx_popleft(void *handle, int channel)
|
|
{
|
|
auto &queue = static_cast<Bearer *>(handle)->rx[channel];
|
|
if (queue.empty()) return -1;
|
|
const int value = queue.front();
|
|
queue.pop_front();
|
|
return value;
|
|
}
|
|
void courier_bearer_rx_appendleft(void *handle, int channel, int value)
|
|
{
|
|
static_cast<Bearer *>(handle)->rx[channel].push_front(uint8_t(value));
|
|
}
|
|
void courier_bearer_rx_clear(void *handle, int channel)
|
|
{
|
|
static_cast<Bearer *>(handle)->rx[channel].clear();
|
|
}
|
|
|
|
// Scalars: 0 routed[0], 1 routed[1], 2 frames, 3 fed[0], 4 fed[1].
|
|
uint64_t courier_bearer_get(void *handle, int which)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
switch (which) {
|
|
case 0: return b->routed[0];
|
|
case 1: return b->routed[1];
|
|
case 2: return b->frames;
|
|
case 3: return b->fed[0];
|
|
default: return b->fed[1];
|
|
}
|
|
}
|
|
void courier_bearer_set(void *handle, int which, uint64_t value)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
switch (which) {
|
|
case 0: b->routed[0] = value; break;
|
|
case 1: b->routed[1] = value; break;
|
|
case 2: b->frames = value; break;
|
|
case 3: b->fed[0] = value != 0; break;
|
|
default: b->fed[1] = value != 0; break;
|
|
}
|
|
}
|
|
|
|
void courier_bearer_configure(void *handle, int activated, unsigned route_count,
|
|
const uint8_t *routes, const uint8_t *slots, uint64_t generation)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
b->activated = activated != 0;
|
|
b->route_count = std::min(route_count, 3u);
|
|
for (unsigned index = 0; index < b->route_count; ++index) {
|
|
b->routes[index][0] = routes[2 * index];
|
|
b->routes[index][1] = routes[2 * index + 1];
|
|
}
|
|
for (unsigned index = 0; index < courier::BEARER_SLOTS; ++index) b->slot[index] = slots[index];
|
|
b->generation = generation;
|
|
}
|
|
|
|
void courier_bearer_set_mode(void *handle, uint32_t channels, int frame_service,
|
|
unsigned lookahead_frames)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
b->channels = channels;
|
|
b->frame_service = frame_service != 0;
|
|
b->lookahead_frames = lookahead_frames;
|
|
}
|
|
|
|
std::size_t courier_bearer_ahead_count(void *handle)
|
|
{
|
|
return static_cast<Bearer *>(handle)->ahead.size();
|
|
}
|
|
int courier_bearer_lookahead_valid(void *handle)
|
|
{
|
|
return static_cast<Bearer *>(handle)->lookahead_valid() ? 1 : 0;
|
|
}
|
|
// Pop the oldest settled frame: in1, in2, taken, heard packed in a word.
|
|
// Returns -1 when none.
|
|
int64_t courier_bearer_ahead_popleft(void *handle)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
if (b->ahead.empty()) return -1;
|
|
const auto entry = b->ahead.front();
|
|
b->ahead.pop_front();
|
|
return int64_t(entry.in[0]) | int64_t(entry.in[1]) << 8
|
|
| int64_t(entry.taken) << 16 | int64_t(entry.heard) << 24;
|
|
}
|
|
void courier_bearer_ahead_clear(void *handle)
|
|
{
|
|
static_cast<Bearer *>(handle)->ahead.clear();
|
|
}
|
|
void courier_bearer_cancel(void *handle, void *core)
|
|
{
|
|
static_cast<Bearer *>(handle)->cancel_lookahead(static_cast<C5xCore *>(core));
|
|
}
|
|
void courier_bearer_prefeed(void *handle, void *core)
|
|
{
|
|
static_cast<Bearer *>(handle)->prefeed(static_cast<C5xCore *>(core));
|
|
}
|
|
// Settle what the C5x transmitted; returns how many octets are left in
|
|
// `remainder` for the harness (0: all done).
|
|
std::size_t courier_bearer_exchange(void *handle, void *core, const uint8_t *octets,
|
|
std::size_t count, uint8_t *remainder)
|
|
{
|
|
return static_cast<Bearer *>(handle)->exchange(
|
|
static_cast<C5xCore *>(core), octets, count, remainder);
|
|
}
|
|
std::size_t courier_bearer_partial(void *handle, uint8_t *out)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
if (!b->partial_valid) return 0;
|
|
*out = b->partial;
|
|
return 1;
|
|
}
|
|
void courier_bearer_set_partial(void *handle, const uint8_t *data, std::size_t count)
|
|
{
|
|
auto *b = static_cast<Bearer *>(handle);
|
|
b->partial_valid = count != 0;
|
|
if (count) b->partial = data[count - 1];
|
|
}
|
|
|
|
} // extern "C"
|
|
|
|
// The analog worker's service, run inside the x86 engine (see
|
|
// courier_emu/worker_poll.py, which mirrors this structure, and Machine.run's
|
|
// interpreter_service, fast_service and DspBridge.clock_x86_fast, which this
|
|
// does the work of). The harness arms it after a service of its own when
|
|
// nothing but the DSP, the clocks and the DSP's frame edge can move before
|
|
// `elide_until`; the engine then runs each service here until one needs
|
|
// Python, and the harness takes the fields back whenever it regains control.
|
|
namespace {
|
|
|
|
struct WorkerPollState {
|
|
void *core;
|
|
uint64_t armed, dirty, polls;
|
|
int64_t instruction_base;
|
|
uint64_t elide_until, last_service;
|
|
// The 80186 side: the timer poll, the frame edge and the interrupt
|
|
// controller's in-service stack.
|
|
int64_t timer_poll_owed;
|
|
uint64_t timer_poll_period;
|
|
int64_t last_frame;
|
|
uint64_t frame_instructions, int0_ok, int0_in_service, stack_depth;
|
|
uint8_t stack[64];
|
|
// DspBridge.clock_x86_fast's counters.
|
|
int64_t bridge_instructions, probe_last;
|
|
uint64_t codec_present;
|
|
int64_t codec_instructions, line_frame_instructions, x86_ticks, batch;
|
|
double debt, cpi, cycles_per_x86;
|
|
int64_t line_codec_last, line_skipped, line_min_samples, line_max_skip;
|
|
uint64_t boot_rom, mailbox_writes, overlay_active, host_port;
|
|
// Whether the C51 sat in its mask-ROM loader loop when armed (the lanes'
|
|
// configuration depends on it), and that loop.
|
|
uint64_t loader_in_loop, loader_first, loader_end;
|
|
// The lamp latch the batched port-0x14 writes must repeat (256: none).
|
|
uint64_t panel_latch;
|
|
// How far into the batch's logs this has looked. Arming happens just
|
|
// before a batch and every batch's end disarms, so the harness zeroes
|
|
// these when it arms.
|
|
uint64_t io_cursor, mmio_cursor;
|
|
// A service handed back after the DSP step (codes 2 and 3).
|
|
int64_t resume_elapsed;
|
|
uint64_t step_writes, step_tdm;
|
|
// The line service (DspBridge._service_line and _take_line_audio) for a
|
|
// batch of codec samples that does not fill a line frame: the resampler's
|
|
// retuned state (0: not retuned, the harness's), the line's rate and
|
|
// frame, how many samples the exchange buffer holds, and what the
|
|
// services done here added to it and to the statistics.
|
|
void *timed;
|
|
double line_rate;
|
|
uint64_t line_frame_samples;
|
|
int64_t tx_index, peak_codec, peak_line;
|
|
uint64_t line_calls, tx_consumed, resampled;
|
|
// The exchange buffer itself (_exchange_line_buffer), seeded when armed.
|
|
uint64_t line_buffer_count;
|
|
int16_t line_buffer[512];
|
|
// The frame exchange (_service_line_frame and LineLink.exchange) in a
|
|
// call that is through and steady: allowed when frame_ok, on the line's
|
|
// socket; the line's counters and its peer's state; the codec state the
|
|
// frame's register note must find unchanged; the receive side's peaks,
|
|
// the in-flight copy's length and the DAA's counts; what the recordings
|
|
// and the in-flight copy are owed. presend_error: a frame this sent and
|
|
// then took back (1: sent, else -errno or -1000 for a timeout), which
|
|
// LineLink.exchange must not send again.
|
|
uint64_t frame_ok;
|
|
int64_t socket_fd;
|
|
double loss_gain;
|
|
uint64_t frame_budget, line_frame_period;
|
|
uint64_t line_frames, frames_received, sent_frames, sent_samples, received_samples;
|
|
int64_t peer_instructions;
|
|
uint64_t peer_off_hook, peer_ringing, peer_call_state;
|
|
uint64_t codec_registers[9], codec_mclk, codec_empty_seen;
|
|
double codec_rate_seen;
|
|
uint64_t detector_present, daa_samples, daa_line_state;
|
|
int64_t line_rx_peak, codec_in_peak, codec_handed_peak, codec_handed_at,
|
|
codec_handed_ever, codec_queue_peak;
|
|
uint64_t in_flight_length, frames_done, record, presend_error;
|
|
uint64_t tx_record_count, rx_record_count, rx_log_count;
|
|
int16_t tx_record[4096], rx_record[4096], rx_log[4096];
|
|
char error[512];
|
|
// COURIER_INT0_TOUT: the frame edge is the DSP timer's output, one
|
|
// latched request for each pulse (Machine._int0_frame_due).
|
|
uint64_t int0_tout, tout_seen, int0_request;
|
|
};
|
|
|
|
constexpr uint32_t WORKER_IF = 0x0200;
|
|
constexpr unsigned WORKER_INT0 = 12;
|
|
|
|
// Machine.on_mmio_write's end-of-interrupt handling, for the writes the
|
|
// engine logged since this batch began; each is marked so the harness's replay
|
|
// leaves the stack alone.
|
|
void worker_apply_eois(WorkerPollState &s, const PollCpu &cpu)
|
|
{
|
|
if (!cpu.mmio_events) return;
|
|
for (uint64_t index = s.mmio_cursor; index < cpu.mmio_count; ++index) {
|
|
uint8_t *event = cpu.mmio_events + 8 * index;
|
|
const uint32_t address = uint32_t(event[0]) | uint32_t(event[1]) << 8
|
|
| uint32_t(event[2]) << 16 | uint32_t(event[3]) << 24;
|
|
const uint16_t value = uint16_t(event[4] | event[5] << 8);
|
|
if (address == 0xFF02 && s.stack_depth) {
|
|
unsigned completed;
|
|
if (value & 0x8000) completed = s.stack[--s.stack_depth];
|
|
else {
|
|
completed = value & 0x1f;
|
|
for (uint64_t at = 0; at < s.stack_depth; ++at)
|
|
if (s.stack[at] == completed) {
|
|
std::memmove(s.stack + at, s.stack + at + 1, s.stack_depth - at - 1);
|
|
--s.stack_depth;
|
|
break;
|
|
}
|
|
}
|
|
if (completed == WORKER_INT0) s.int0_in_service = 0;
|
|
s.dirty = 1;
|
|
}
|
|
event[7] = 1;
|
|
}
|
|
s.mmio_cursor = cpu.mmio_count;
|
|
}
|
|
|
|
bool worker_writable(const PollCpu &cpu, uint32_t address, uint32_t count)
|
|
{
|
|
if (address + count > cpu.memory_size) return false;
|
|
for (uint32_t at = address; at < address + count; ++at)
|
|
if (!(cpu.guard[at] & 2) || (cpu.guard[at] & 32)) return false;
|
|
return true;
|
|
}
|
|
|
|
// LineLink's socket has a timeout, which leaves it non-blocking: wait for it
|
|
// the way Python's socket does, 30 s a call. 0: done; an errno; -1000: timed
|
|
// out; -1 (receiving): the far end closed the line.
|
|
constexpr int WORKER_TIMEOUT = -1000, WORKER_CLOSED = -1;
|
|
constexpr int WORKER_SOCKET_MS = 30000;
|
|
|
|
int worker_wait(int fd, short events)
|
|
{
|
|
for (;;) {
|
|
pollfd entry{fd, events, 0};
|
|
const int ready = ::poll(&entry, 1, WORKER_SOCKET_MS);
|
|
if (ready > 0) return 0;
|
|
if (ready == 0) return WORKER_TIMEOUT;
|
|
if (errno != EINTR) return errno;
|
|
}
|
|
}
|
|
|
|
int worker_send(int fd, const uint8_t *data, std::size_t count)
|
|
{
|
|
std::size_t done = 0;
|
|
while (done < count) {
|
|
const ssize_t sent = ::send(fd, data + done, count - done, 0);
|
|
if (sent > 0) { done += std::size_t(sent); continue; }
|
|
if (sent < 0 && errno == EINTR) continue;
|
|
if (sent < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
|
if (const int failed = worker_wait(fd, POLLOUT)) return failed;
|
|
continue;
|
|
}
|
|
return errno;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int worker_receive(int fd, uint8_t *data, std::size_t count)
|
|
{
|
|
std::size_t done = 0;
|
|
while (done < count) {
|
|
const ssize_t got = ::recv(fd, data + done, count - done, 0);
|
|
if (got > 0) { done += std::size_t(got); continue; }
|
|
if (got == 0) return WORKER_CLOSED;
|
|
if (errno == EINTR) continue;
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
if (const int failed = worker_wait(fd, POLLIN)) return failed;
|
|
continue;
|
|
}
|
|
return errno;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool worker_readable(const PollCpu &cpu, uint32_t address, uint32_t count)
|
|
{
|
|
if (address + count > cpu.memory_size) return false;
|
|
for (uint32_t at = address; at < address + count; ++at)
|
|
if (!(cpu.guard[at] & 1) || (cpu.guard[at] & 16)) return false;
|
|
return true;
|
|
}
|
|
|
|
// What _service_line_frame does once the far end's frame is in: the frame
|
|
// leaves the exchange buffer, LineLink.exchange's bookkeeping and recordings,
|
|
// and the received audio into the codec's queue (_queue_line_audio on its
|
|
// clocked-input path), the peaks and the DAA.
|
|
void worker_commit_frame(WorkerPollState &s, C5xCore *core, const int16_t *wire,
|
|
const uint8_t *header, const std::vector<int16_t> &body)
|
|
{
|
|
#pragma clang fp contract(off)
|
|
const uint64_t frame = s.line_frame_samples;
|
|
std::memmove(s.line_buffer, s.line_buffer + frame,
|
|
std::size_t(s.line_buffer_count - frame) * sizeof s.line_buffer[0]);
|
|
s.line_buffer_count -= frame;
|
|
s.frames_done += 1;
|
|
s.sent_frames += 1;
|
|
if (s.record)
|
|
for (uint64_t k = 0; k < frame; ++k) s.tx_record[s.tx_record_count++] = wire[k];
|
|
s.frames_received += 1;
|
|
if (s.record)
|
|
for (const int16_t sample : body) s.rx_record[s.rx_record_count++] = sample;
|
|
s.line_frames += 1;
|
|
s.sent_samples += frame;
|
|
uint32_t stamp;
|
|
std::memcpy(&stamp, header, 4);
|
|
s.peer_instructions = stamp;
|
|
s.peer_off_hook = header[4] != 0;
|
|
s.peer_ringing = header[5] != 0;
|
|
s.peer_call_state = header[6];
|
|
std::vector<int64_t> incoming(body.size());
|
|
for (std::size_t k = 0; k < body.size(); ++k)
|
|
incoming[k] = s.loss_gain == 1.0 ? int64_t(body[k])
|
|
: int64_t(double(body[k]) * s.loss_gain);
|
|
s.received_samples += body.size();
|
|
if (!incoming.empty()) {
|
|
int64_t peak = 0;
|
|
for (const int64_t value : incoming) peak = std::max<int64_t>(peak, value < 0 ? -value : value);
|
|
s.line_rx_peak = std::max(s.line_rx_peak, peak);
|
|
s.codec_in_peak = std::max(s.codec_in_peak, peak);
|
|
if (peak) {
|
|
s.codec_handed_peak = std::max(s.codec_handed_peak, peak);
|
|
s.codec_handed_at = s.bridge_instructions;
|
|
s.codec_handed_ever = s.bridge_instructions;
|
|
}
|
|
std::vector<uint16_t> words(incoming.size());
|
|
for (std::size_t k = 0; k < incoming.size(); ++k) {
|
|
words[k] = uint16_t(incoming[k]);
|
|
s.rx_log[s.rx_log_count++] = int16_t(incoming[k]);
|
|
}
|
|
core->queue_line_rx(words.data(), words.size());
|
|
const uint64_t pending = core->codec_state().codec_rx_size;
|
|
s.in_flight_length = std::min<uint64_t>(s.in_flight_length + incoming.size(), pending);
|
|
s.codec_queue_peak = std::max(s.codec_queue_peak, peak);
|
|
}
|
|
// The socket is connected, so the line is quiet unless the far end rings.
|
|
s.daa_line_state = s.peer_ringing ? 2 : 1;
|
|
if (!body.empty() && s.detector_present) s.daa_samples += body.size();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
extern "C" int courier_worker_poll(void *context, uint64_t now, uint32_t flags, void *lent)
|
|
{
|
|
// Python's float arithmetic rounds after every operation.
|
|
#pragma clang fp contract(off)
|
|
WorkerPollState &s = *static_cast<WorkerPollState *>(context);
|
|
if (!s.armed) return 0;
|
|
const PollCpu &cpu = *static_cast<const PollCpu *>(lent);
|
|
worker_apply_eois(s, cpu);
|
|
const int64_t total = s.instruction_base + int64_t(now);
|
|
if (uint64_t(total) >= s.elide_until) { s.armed = 0; return 0; }
|
|
// Lamp writes waiting for the harness's replay: harmless only if they
|
|
// leave the latch as it is.
|
|
for (uint64_t index = s.io_cursor; index < cpu.io_event_count; ++index) {
|
|
const uint8_t *event = cpu.io_events + 4 * index;
|
|
if ((event[0] | event[1] << 8) != 0x14 || event[2] != s.panel_latch) { s.armed = 0; return 0; }
|
|
}
|
|
s.io_cursor = cpu.io_event_count;
|
|
C5xCore *core = static_cast<C5xCore *>(s.core);
|
|
const int64_t elapsed = total - int64_t(s.last_service);
|
|
// clock_x86_fast's tests that a service can change.
|
|
if (s.x86_ticks + elapsed < s.batch
|
|
|| (s.codec_present && s.codec_instructions + elapsed >= s.line_frame_instructions)) {
|
|
s.armed = 0; return 0;
|
|
}
|
|
const int64_t produced = int64_t(core->line_tx_count());
|
|
const int64_t fresh = produced - s.line_codec_last;
|
|
const bool line_skip = 0 < fresh && fresh < s.line_min_samples
|
|
&& s.line_skipped < s.line_max_skip;
|
|
if ((!line_skip && (fresh <= 0 || !s.timed))
|
|
|| s.bridge_instructions + elapsed - s.probe_last >= 200000) {
|
|
s.armed = 0; return 0;
|
|
}
|
|
// fast_service's frame edge, decided now: nothing it reads moves in the
|
|
// DSP step. An entry this cannot make the way dispatch_interrupt would is
|
|
// left to the harness.
|
|
const bool timer_poll = s.timer_poll_owed - elapsed <= 0;
|
|
bool frame_edge = false;
|
|
uint32_t *r = cpu.regs;
|
|
const uint32_t sp = r[REG_SP] & 0xFFFF, ss = r[REG_SS] & 0xFFFF;
|
|
uint32_t slots[3];
|
|
// local fix: COURIER_INT0_EXACT takes the frame edge when it is due,
|
|
// not only at a timer poll
|
|
static const bool int0_exact = getenv("COURIER_INT0_EXACT") && getenv("COURIER_INT0_EXACT")[0] != '0';
|
|
if (s.int0_tout) {
|
|
const uint64_t expiries = core->timer_expiries();
|
|
if (expiries != s.tout_seen) {
|
|
if (expiries > s.tout_seen) s.int0_request = 1;
|
|
s.tout_seen = expiries;
|
|
}
|
|
}
|
|
const bool frame_due = s.int0_tout ? s.int0_request != 0
|
|
: total - s.last_frame >= int64_t(s.frame_instructions);
|
|
if ((timer_poll || int0_exact || s.int0_tout) && s.int0_ok && !s.int0_in_service
|
|
&& (flags & WORKER_IF) && frame_due) {
|
|
if (!worker_readable(cpu, WORKER_INT0 * 4, 4)) { s.armed = 0; return 0; }
|
|
const uint8_t *vector = cpu.memory + WORKER_INT0 * 4;
|
|
if (vector[0] | vector[1] | vector[2] | vector[3]) {
|
|
for (unsigned k = 0; k < 3; ++k) {
|
|
slots[k] = (ss * 16 + ((sp - 2 * (k + 1)) & 0xFFFF)) & 0xFFFFF;
|
|
if (!worker_writable(cpu, slots[k], 2)) { s.armed = 0; return 0; }
|
|
}
|
|
if (s.stack_depth >= sizeof s.stack) { s.armed = 0; return 0; }
|
|
frame_edge = true;
|
|
}
|
|
}
|
|
// _service_line with a batch to take: _take_line_audio, converted on the
|
|
// resampler's own state and taken back unless it leaves no line frame due.
|
|
courier::resample::Timed *timed = static_cast<courier::resample::Timed *>(s.timed);
|
|
const auto &line_tx = core->line_tx_samples();
|
|
const int64_t line_start = s.tx_index;
|
|
bool frame = false;
|
|
if (!line_skip) {
|
|
if (line_start < 0 || produced <= line_start) { s.armed = 0; return 0; }
|
|
// The codec's rate, or the one in force at the batch's first sample;
|
|
// a clock change inside the batch is the harness's.
|
|
double rate = double(core->codec_sample_rate_millihz()) / 1000.0;
|
|
for (const auto &event : core->line_tx_clock_events()) {
|
|
if (int64_t(event[0]) <= line_start) rate = 40320000.0 / double(event[1]); // ASIC_DSP_CLOCK_HZ
|
|
else if (int64_t(event[0]) < produced) { s.armed = 0; return 0; }
|
|
}
|
|
if (!(rate > 0) || rate != timed->input_rate || s.line_rate != timed->output_rate
|
|
|| !timed->kernel) {
|
|
s.armed = 0; return 0;
|
|
}
|
|
std::vector<int16_t> samples(size_t(produced - line_start));
|
|
for (size_t k = 0; k < samples.size(); ++k)
|
|
samples[k] = int16_t(line_tx[size_t(line_start) + k]);
|
|
timed->run(samples.data(), samples.size(), rate, s.line_rate, timed->kernel);
|
|
const uint64_t buffered = s.line_buffer_count + timed->out.size();
|
|
if (buffered >= 2 * s.line_frame_samples || buffered > 512
|
|
|| (buffered >= s.line_frame_samples && !s.frame_ok)) {
|
|
timed->rollback();
|
|
s.armed = 0; return 0;
|
|
}
|
|
frame = buffered >= s.line_frame_samples;
|
|
}
|
|
// _service_line_frame, for the one frame the batch completes.
|
|
int16_t wire[512];
|
|
uint8_t header[9];
|
|
std::vector<int16_t> body;
|
|
if (frame) {
|
|
// _note_codec_registers must have nothing to note, the frame budget
|
|
// nothing to call, and the logs room for the frame.
|
|
const auto codec = core->codec_state();
|
|
bool same = codec.rx_empty_frames == s.codec_empty_seen && codec.mclk_hz == s.codec_mclk
|
|
&& double(codec.sample_rate_millihz) / 1000.0 == s.codec_rate_seen;
|
|
for (unsigned index = 0; index < 9; ++index)
|
|
same = same && codec.registers[index] == s.codec_registers[index];
|
|
if (!same || s.line_frames + 1 >= s.frame_budget
|
|
|| s.tx_record_count + s.line_frame_samples > 4096
|
|
|| s.rx_record_count + 2048 > 4096 || s.rx_log_count + 2048 > 4096) {
|
|
timed->rollback();
|
|
s.armed = 0; return 0;
|
|
}
|
|
const uint64_t from_buffer = std::min<uint64_t>(s.line_buffer_count, s.line_frame_samples);
|
|
for (uint64_t k = 0; k < from_buffer; ++k) wire[k] = s.line_buffer[k];
|
|
for (uint64_t k = from_buffer; k < s.line_frame_samples; ++k)
|
|
wire[k] = timed->out[size_t(k - from_buffer)];
|
|
// LineFrame.encode: <IBBBH and the samples. Off hook, not ringing,
|
|
// the call answered.
|
|
std::vector<uint8_t> packet(9 + 2 * s.line_frame_samples);
|
|
const uint32_t stamp = uint32_t(s.line_frames * s.line_frame_period);
|
|
std::memcpy(packet.data(), &stamp, 4);
|
|
packet[4] = 1; packet[5] = 0; packet[6] = 3;
|
|
const uint16_t count = uint16_t(s.line_frame_samples);
|
|
std::memcpy(packet.data() + 7, &count, 2);
|
|
std::memcpy(packet.data() + 9, wire, 2 * s.line_frame_samples);
|
|
const int fd = int(s.socket_fd);
|
|
if (const int failed = worker_send(fd, packet.data(), packet.size())) {
|
|
timed->rollback();
|
|
s.presend_error = failed == WORKER_TIMEOUT ? 5000 : 4000 + uint64_t(failed);
|
|
s.armed = 0; return 0;
|
|
}
|
|
int failed = worker_receive(fd, header, sizeof header);
|
|
if (!failed) {
|
|
uint16_t peer_count;
|
|
std::memcpy(&peer_count, header + 7, 2);
|
|
if (peer_count > 2048) std::abort(); // no far end sends frames this long
|
|
body.resize(peer_count);
|
|
failed = worker_receive(fd, reinterpret_cast<uint8_t *>(body.data()), 2 * std::size_t(peer_count));
|
|
}
|
|
if (failed) {
|
|
// Taken back: the harness runs this service again, and its
|
|
// exchange meets the same failure without sending a second time.
|
|
timed->rollback();
|
|
s.presend_error = failed == WORKER_CLOSED ? 1
|
|
: failed == WORKER_TIMEOUT ? 3000 : 2000 + uint64_t(failed);
|
|
s.armed = 0; return 0;
|
|
}
|
|
}
|
|
// interpreter_service's bookkeeping, then clock_x86_fast's commit.
|
|
s.dirty = 1;
|
|
s.last_service = uint64_t(total);
|
|
s.bridge_instructions += elapsed;
|
|
if (s.codec_present) s.codec_instructions += elapsed;
|
|
s.x86_ticks += elapsed;
|
|
s.debt += double(s.x86_ticks) * s.cycles_per_x86;
|
|
s.x86_ticks = 0;
|
|
if (line_skip) s.line_skipped += 1;
|
|
else {
|
|
timed->commit();
|
|
int64_t peak = 0;
|
|
for (size_t k = size_t(line_start); k < size_t(produced); ++k)
|
|
peak = std::max<int64_t>(peak, std::abs(int64_t(int16_t(line_tx[k]))));
|
|
s.peak_codec = std::max(s.peak_codec, peak);
|
|
peak = 0;
|
|
for (const int16_t sample : timed->out) {
|
|
peak = std::max<int64_t>(peak, std::abs(int64_t(sample)));
|
|
s.line_buffer[s.line_buffer_count++] = sample;
|
|
}
|
|
s.peak_line = std::max(s.peak_line, peak);
|
|
s.resampled += timed->out.size();
|
|
s.tx_consumed += uint64_t(produced - line_start);
|
|
s.tx_index = produced;
|
|
s.line_calls += 1;
|
|
s.line_codec_last = produced;
|
|
s.line_skipped = 0;
|
|
if (frame) worker_commit_frame(s, core, wire, header, body);
|
|
}
|
|
uint64_t writes;
|
|
bool tdm;
|
|
try {
|
|
if (s.debt < 1) {
|
|
writes = core->io_port_stat(uint16_t(s.host_port)).writes;
|
|
tdm = core->call_tdm_active();
|
|
} else {
|
|
const int64_t owed = int64_t(s.debt);
|
|
const uint64_t before_instructions = core->instruction_count();
|
|
const uint64_t before_cycles = core->cycle_count();
|
|
core->run_cycles(uint64_t(owed > 1 ? owed : 1));
|
|
const uint64_t ran = core->instruction_count() - before_instructions;
|
|
const uint64_t cycles = core->cycle_count() - before_cycles;
|
|
writes = core->io_port_stat(uint16_t(s.host_port)).writes;
|
|
tdm = core->call_tdm_active();
|
|
s.debt -= double(cycles);
|
|
if (ran > 0) {
|
|
const double cpi = double(cycles) / double(ran);
|
|
s.cpi = 0.9 * s.cpi + 0.1 * (cpi > 1.0 ? cpi : 1.0);
|
|
}
|
|
}
|
|
} catch (const std::exception &exception) {
|
|
copy_error(s.error, sizeof s.error, exception.what());
|
|
s.resume_elapsed = elapsed;
|
|
++s.polls;
|
|
s.armed = 0;
|
|
return 3;
|
|
}
|
|
const uint16_t pc = core->program_counter();
|
|
const bool in_loop = pc >= s.loader_first && pc < s.loader_end;
|
|
if ((s.boot_rom && writes > s.mailbox_writes) || (!s.overlay_active && tdm)
|
|
|| in_loop != bool(s.loader_in_loop)) {
|
|
// A DSP message to collect, the call's TDM starting, or the lanes'
|
|
// configuration changing: the harness finishes this service.
|
|
s.step_writes = writes;
|
|
s.step_tdm = tdm;
|
|
s.resume_elapsed = elapsed;
|
|
++s.polls;
|
|
s.armed = 0;
|
|
return 2;
|
|
}
|
|
if (s.boot_rom) s.mailbox_writes = writes;
|
|
// fast_service's tail. The 80186 timers are brought up to date lazily:
|
|
// every access to one advances it first, and the harness made sure none
|
|
// reaches a max count before elide_until.
|
|
s.timer_poll_owed -= elapsed;
|
|
if (s.timer_poll_owed <= 0)
|
|
s.timer_poll_owed += int64_t(s.timer_poll_period);
|
|
{
|
|
if (frame_edge) {
|
|
if (s.int0_tout) {
|
|
s.int0_request = 0;
|
|
} else {
|
|
s.last_frame += int64_t(s.frame_instructions);
|
|
if (total - s.last_frame >= int64_t(s.frame_instructions))
|
|
s.last_frame = total - int64_t(s.frame_instructions);
|
|
}
|
|
s.int0_in_service = 1;
|
|
// dispatch_interrupt(INT0_VECTOR, software=False)
|
|
const uint32_t flags16 = r[REG_FLAGS] & 0xFFFF;
|
|
const uint32_t values[3] = {flags16, r[REG_CS] & 0xFFFF, r[REG_IP] & 0xFFFF};
|
|
for (unsigned k = 0; k < 3; ++k) {
|
|
cpu.memory[slots[k]] = uint8_t(values[k]);
|
|
cpu.memory[slots[k] + 1] = uint8_t(values[k] >> 8);
|
|
}
|
|
r[REG_SP] = (sp - 6) & 0xFFFF;
|
|
const uint8_t *vector = cpu.memory + WORKER_INT0 * 4;
|
|
s.stack[s.stack_depth++] = WORKER_INT0;
|
|
r[REG_FLAGS] = flags16 & ~WORKER_IF;
|
|
r[REG_CS] = uint32_t(vector[2] | vector[3] << 8);
|
|
r[REG_IP] = uint32_t(vector[0] | vector[1] << 8);
|
|
}
|
|
}
|
|
// A far end that hangs up or rings changes the next frame's call state:
|
|
// that one is the harness's.
|
|
if (frame && !(s.peer_off_hook && !s.peer_ringing && s.peer_call_state <= 3)) s.armed = 0;
|
|
++s.polls;
|
|
return 1;
|
|
}
|
|
|
|
// For worker_poll.py to check its mirror of WorkerPollState against.
|
|
extern "C" void courier_worker_poll_layout(uint64_t *out)
|
|
{
|
|
out[0] = sizeof(WorkerPollState);
|
|
out[1] = offsetof(WorkerPollState, debt);
|
|
out[2] = offsetof(WorkerPollState, frame_ok);
|
|
out[3] = offsetof(WorkerPollState, error);
|
|
}
|