From 20133c60eec1d440489b3ed2b2fc8ee495678c4d Mon Sep 17 00:00:00 2001 From: Cryan209 Date: Wed, 16 Sep 2026 22:12:04 +1200 Subject: [PATCH] Do not service interrupts inside a delayed branch's delay slots op_clrc_intm() calls check_interrupts() inline, which is right at an instruction boundary and wrong inside a delay slot: it pushed the return address and set the PC to the vector, and then the enclosing op_calld's CHANGE_PC(pma) overwrote the PC and left the push behind. One orphaned stack entry per execution. SPRU056D 4.9 has a delayed branch and its delay slots complete as a unit, with no interrupt serviced between them, so the pending interrupt should simply be taken at the next boundary. This firmware guards every host-port access with the same idiom - setc intm ; calld 80e8 ; lar ar1, #xx ; clrc intm at 80eb, 8103, 82c6, 82d5, 8323 and 83c4, so the stack drifted a level on every port read. The visible damage was in the datapump's receive delivery: 8320's RETD at 8330 popped the stale 8327 its own CALLD had pushed, re- entering the routine at 8327 with ARP still 1 from the `sacl *+, ar1`. AR1 holds 0x6f there, so the delivery wrote the assembled character over @6f - its own flag word - clearing bit 3 and disabling receive delivery, after reading that same cell as if it were the host status port. With the FSK loopback running the firmware's own loop, that is the difference between 14 characters delivered and 1211, and @6f now reaches 008c - carrier, receive delivery and transmit data all enabled - instead of being knocked back to 0007. Also adds stack_entry()/NativeC5x.stack(), which is what made the orphaned push visible. Co-Authored-By: Claude Opus 5 --- courier_emu/dsp.py | 7 +++++++ native/c5x_capi.cpp | 5 +++++ native/c5x_core.cpp | 16 +++++++++++++++- native/c5x_core.h | 9 +++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/courier_emu/dsp.py b/courier_emu/dsp.py index d6281e7..674557f 100644 --- a/courier_emu/dsp.py +++ b/courier_emu/dsp.py @@ -261,6 +261,8 @@ class NativeC5x: lib.courier_c5x_get_io.restype = ctypes.c_uint16 lib.courier_c5x_get_program.argtypes = [ctypes.c_void_p, ctypes.c_uint16] lib.courier_c5x_get_program.restype = ctypes.c_uint16 + lib.courier_c5x_get_stack.argtypes = [ctypes.c_void_p, ctypes.c_uint] + lib.courier_c5x_get_stack.restype = ctypes.c_uint16 lib.courier_c5x_get_register.argtypes = [ctypes.c_void_p, ctypes.c_uint16] lib.courier_c5x_get_register.restype = ctypes.c_uint16 lib.courier_c5x_get_data.argtypes = [ctypes.c_void_p, ctypes.c_uint16] @@ -527,6 +529,11 @@ class NativeC5x: def set_data(self, address: int, value: int) -> None: self.library.courier_c5x_set_data(self.handle, address, value) + def stack(self) -> tuple[int, ...]: + """The 8-deep hardware stack, newest first.""" + return tuple(int(self.library.courier_c5x_get_stack(self.handle, n)) + for n in range(8)) + def register(self, offset: int) -> int: """A memory-mapped register, read without disturbing it. diff --git a/native/c5x_capi.cpp b/native/c5x_capi.cpp index 78abd80..e531156 100644 --- a/native/c5x_capi.cpp +++ b/native/c5x_capi.cpp @@ -259,6 +259,11 @@ uint16_t courier_c5x_get_data(void *handle, uint16_t address) return handle ? static_cast(handle)->data(address) : 0xffff; } +uint16_t courier_c5x_get_stack(void *handle, unsigned index) +{ + return handle ? static_cast(handle)->stack_entry(index) : 0xffff; +} + uint16_t courier_c5x_get_register(void *handle, uint16_t offset) { return handle ? static_cast(handle)->register_value(offset) : 0xffff; diff --git a/native/c5x_core.cpp b/native/c5x_core.cpp index e0d3529..18bfcd5 100644 --- a/native/c5x_core.cpp +++ b/native/c5x_core.cpp @@ -527,6 +527,11 @@ void C5xCore::set_data(uint16_t address, uint16_t value) else m_data[address] = value; } +uint16_t C5xCore::stack_entry(unsigned index) const +{ + return index < 8 ? m_pcstack[(m_pcstack_ptr + index) & 7] : 0; +} + uint16_t C5xCore::register_value(uint16_t offset) const { switch (offset) { @@ -971,8 +976,17 @@ void C5xCore::op_group_bf() { (this->*s_opcode_table_bf[m_op & 0xff])(); } void C5xCore::delay_slot(uint16_t startpc) { + // CLRC INTM services interrupts inline. In a delay slot that pushed the + // return address and vectored, and then the enclosing delayed branch + // overwrote the PC - orphaning one stack entry per execution. The firmware + // guards every host-port access with `setc intm ; calld ... ; clrc intm`, + // so the stack drifted a level each time and a later RET returned into the + // middle of the routine with the wrong ARP. + const bool outer = m_in_delay_slot; + m_in_delay_slot = true; m_op = ROPCODE(); (this->*s_opcode_table[m_op >> 8])(); while (uint16_t(m_pc - startpc) < 2) { m_op = ROPCODE(); (this->*s_opcode_table[m_op >> 8])(); } + m_in_delay_slot = outer; } void C5xCore::save_interrupt_context() @@ -989,7 +1003,7 @@ void C5xCore::restore_interrupt_context() } void C5xCore::check_interrupts() { - if (m_st0.intm || !m_ifr) return; + if (m_in_delay_slot || m_st0.intm || !m_ifr) return; for (unsigned irq = 0; irq < 16; ++irq) if (m_ifr & (1u << irq)) { m_st0.intm = 1; PUSH_STACK(m_pc); uint16_t vector = m_interrupt_vectors[irq]; diff --git a/native/c5x_core.h b/native/c5x_core.h index 804c6f2..a8a2ad7 100644 --- a/native/c5x_core.h +++ b/native/c5x_core.h @@ -257,6 +257,12 @@ public: // cpuregs_r it has no side effects: DRR does not pop the codec queue and // TRCV does not clear its ready flag. uint16_t register_value(uint16_t offset) const; + // The 8-deep hardware stack, newest first. The firmware manipulates it + // directly (d876 is `pop ; samm @70`, a coroutine yield that discards a + // return address), so a harness that reaches an entry by a different call + // depth than the firmware does leaves it skewed, and a later RET underflows + // into whatever the C5x's shift-register stack replicates at the bottom. + uint16_t stack_entry(unsigned index) const; void set_data(uint16_t address, uint16_t value); void interrupt(unsigned irq); void nmi(); @@ -514,6 +520,9 @@ private: void save_interrupt_context(); void restore_interrupt_context(); void delay_slot(uint16_t startpc); + // SPRU056D 4.9: a delayed branch and its delay slots complete as a + // unit; no interrupt is serviced between them. + bool m_in_delay_slot = false; void op_invalid(); void op_abs(); void op_adcb(); void op_add_mem(); void op_add_simm(); void op_add_limm(); void op_add_s16_mem(); void op_addb();