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();