Clock the EXI bit engine from the EXI pin: timing closes on all 8 seeds

Replaces the PLL-fed 54 MHz `capture` domain, which oversampled the GC's
27 MHz EXI clock 2x and so put BIT-level timing across a CDC boundary: the
engine's view of the clock lagged the pin by 2 FFSynchronizer stages plus a
register (~55 ns) against a 37 ns bit time, so MISO came out ~2 bits late and
the GC read 0x17 as 05 C0.  No amount of oversampling fixes that -- 54 MHz
gives only 2 ticks per bit and the part cannot close the 81 MHz that 3x needs.

The engine is now clocked by the pin itself (pin 44 = global buffer input net
6, no board change), making MISO clock-to-Q.  ExiCapture creates four domains:
exiclk (posedge, samples MOSI), exiclk_n (negedge, drives MISO), both
async-reset by CS, plus reset-less exififo_w/exififo_r for the byte FIFO sides
-- reset-less because resetting one side of an AsyncFIFO desynchronises its
gray pointers, and posedge because AsyncFIFO has a posedge-only reset CDC
internally and cannot host a negedge side.

Also removes the SPRAM response FIFO.  The DMA read was one byte late because
the round trip did not fit the protocol's turnaround byte; the arbiter already
exposed valid/ready, so the FIFO bought a cycle of latency and nothing else.
The response is handled outside the FSM (the reply lands in RHDR_PAD, not
SPRAM_STREAM) with a one-entry hold for when tx_fifo is momentarily full.

The PLL was left instantiated but unused after the rewrite; removing it frees
the hard macro and pin 35 (which stays unconnected only because the board is
already built that way).  This is what lifted the pass rate:

  domain            target    before        after
  clk (sync)        24 MHz    28.1-30.2     26.4-32.2   8/8
  capture_clk       54 MHz    2 of 8 PASS   (gone)
  EXI pin domain    27 MHz    --            45.6-64.4   8/8

Best seed 3: clk 32.18 MHz, EXI 55.86 MHz.  The binding constraint has FLIPPED
-- `clk` (1.10-1.34x) is now tighter than the EXI domain (1.69-2.39x) -- so
synth.py ranks seeds by worst RELATIVE margin, not by the EXI number.

synth.py could not have scored this design at all: it required the vanished
capture_clk, and its regex '(\w+)' cannot match nextpnr's exi_clk_$glb_clk
(no $ in \w).  add_clock_constraint was also landing on the wrong net -- under
EXI_J4 the clock is the AND of two pads, not either pad -- so nextpnr silently
fell back to a 12 MHz default and under-optimised the whole EXI domain.

Testbenches:
- spi_mode3_slave had lost its testbench entirely in the rewrite.  Restored
  with a strict Mode 3 master (changes MOSI on the falling edge, samples MISO
  in the clock-low phase) and MUTATION-TESTED: flipping either clock edge in
  the RTL makes it fail.  Both of those bugs previously reached silicon
  against a lax master that parked MOSI well before the edge.
- exi_j4 T2/T3 returned [0,4,2,2].  Real bug: the engine latches the FIFO head
  at each byte boundary, so a 2-byte command header leaves only ONE drain
  slot.  DEVID streamed zeros until CS and left two stale bytes -- one too
  many -- so the NEXT enumeration came back a slot late.  DEVID now pushes
  exactly four bytes and stops.  Register-space reads may still over-push:
  their 4-byte header gives three drain slots.
- Dead ports removed from SPIMode3Slave (hdr_bytes, frame_start, cs_active,
  byte_idx) and the mode2 parameter is gone -- there is one correct edge
  convention and it is now enforced by tests.

All 14 module suites pass, plus bba_top at EXI_HALF=1/2/8.

CLAUDE.md: rewrote the clocking, CDC inventory and module tables; marked the
2026-09-30 capture_clk measurements historical; and corrected the line in
"Critical Protocol Notes" that still stated the INVERTED edge convention
("MOSI sampled on falling, MISO driven on rising" is Mode 2) -- the original
implementation followed that doc error.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 17:10:11 +02:00
parent 02239466fb
commit 91c3bf0ece
7 changed files with 611 additions and 408 deletions
+137 -17
View File
@@ -53,10 +53,10 @@ class SPIMode3Slave(Elaboratable):
rx_byte : last complete received byte (valid while rx_valid is high)
rx_valid : 1-cycle pulse in `exiclk` when rx_byte holds a new byte
tx_byte : byte to transmit; sampled at each byte boundary
tx_load : 1-cycle pulse in `exiclk_n` asking upstream for the next byte
hdr_bytes : how many leading bytes are header (MISO don't-care). Pops are
suppressed until past them, so the first response byte the register
file pushes lands in data slot 0 rather than being eaten by the header.
tx_load : 1-cycle pulse asking upstream for the next byte, one per byte
boundary. No header-length or slot-threshold logic: see the TX comment
in elaborate() for why unconditional pops are both sufficient and
necessary.
"""
def __init__(self):
@@ -71,29 +71,16 @@ class SPIMode3Slave(Elaboratable):
self.tx_byte = Signal(8)
self.tx_load = Signal()
# Header length for this transaction, in bytes. The real protocol has
# two forms selected by bit 7 of byte 0 (see docs/exi_protocol_real.md):
# register space = 4-byte header, command space = 2-byte.
self.hdr_bytes = Signal(3, init=4)
# Transaction framing, generated in the slow domain by the parent.
self.frame_start = Signal()
self.cs_active = Signal()
def elaborate(self, platform):
m = Module()
# ── RX: sample MOSI on the RISING edge (Mode 3 samples on edge 2) ──
rx_shift = Signal(8)
rx_bit = Signal(3)
byte_idx = Signal(3, init=0) # bytes seen this transaction (saturating)
nxt = Cat(self.spi_mosi, rx_shift[:-1]) # MSB-first: shift left, new bit at LSB
m.d.exiclk += rx_shift.eq(nxt)
m.d.exiclk += rx_bit.eq(rx_bit + 1)
with m.If(rx_bit == 7):
with m.If(byte_idx != 7):
m.d.exiclk += byte_idx.eq(byte_idx + 1)
# rx_byte/rx_valid are COMBINATIONAL, not registered. The clock stops
# and CS rises immediately after a transaction's final bit, so a
@@ -138,3 +125,136 @@ class SPIMode3Slave(Elaboratable):
m.d.comb += self.spi_miso.eq(Mux(self.spi_cs_n, 1, tx_shift[7]))
return m
# ── Testbench ─────────────────────────────────────────────────────────────
class _Harness(Elaboratable):
"""Wraps the slave with the two pin-driven domains its parent normally
creates, so the engine can be exercised standalone."""
def __init__(self):
self.dut = SPIMode3Slave()
def elaborate(self, platform):
m = Module()
m.domains.exiclk = cd_p = ClockDomain(async_reset=True)
m.domains.exiclk_n = cd_n = ClockDomain(async_reset=True,
clk_edge="neg")
d = self.dut
m.d.comb += [
cd_p.clk.eq(d.spi_clk), cd_p.rst.eq(d.spi_cs_n),
cd_n.clk.eq(d.spi_clk), cd_n.rst.eq(d.spi_cs_n),
]
m.submodules.dut = d
return m
if __name__ == "__main__":
import sys
from amaranth.sim import Simulator, Period
HP = Period(MHz=27) / 2 # one EXI half-period at the hardware rate
top = _Harness()
dut = top.dut
failures = []
def check(name, got, want):
ok = got == want
print(f" {'ok ' if ok else 'FAIL'} {name}: got {got}, want {want}")
if not ok:
failures.append(name)
# A STRICT Mode 3 master. Unlike a convenience model that parks MOSI long
# before the edge, this one changes MOSI exactly ON the falling edge and
# samples MISO in the CLK-low phase. That is what makes the edge
# convention observable: a slave that sampled MOSI on the falling edge
# would capture the previous bit, and one that drove MISO on the rising
# edge would come out a bit late. Both bugs shipped to hardware against a
# lax master that could not see them.
async def spi_byte(ctx, mosi_val, probe_rx=False):
miso = 0
rx = None
for bit in range(7, -1, -1):
ctx.set(dut.spi_clk, 0) # falling edge
ctx.set(dut.spi_mosi, (mosi_val >> bit) & 1)
await ctx.delay(HP)
miso = (miso << 1) | ctx.get(dut.spi_miso) # sample in CLK-low
if probe_rx and bit == 0:
rx = (ctx.get(dut.rx_valid), ctx.get(dut.rx_byte))
ctx.set(dut.spi_clk, 1) # rising edge: slave samples
await ctx.delay(HP)
return (miso, rx) if probe_rx else miso
async def cs_low(ctx):
ctx.set(dut.spi_clk, 1)
ctx.set(dut.spi_cs_n, 0)
await ctx.delay(HP)
async def cs_high(ctx):
ctx.set(dut.spi_clk, 1)
await ctx.delay(HP)
ctx.set(dut.spi_cs_n, 1)
await ctx.delay(HP * 4)
async def tb(ctx):
ctx.set(dut.spi_cs_n, 1)
ctx.set(dut.spi_clk, 1)
ctx.set(dut.tx_byte, 0)
await ctx.delay(HP * 4)
# T1 — RX samples MOSI on the RISING edge, MSB first.
# rx_byte/rx_valid are combinational and peak during the final bit,
# which is the instant the parent's FIFO captures them.
await cs_low(ctx)
seen = []
for b in (0x80, 0x00, 0x31, 0x00):
_, (valid, byte) = await spi_byte(ctx, b, probe_rx=True)
seen.append((valid, byte))
await cs_high(ctx)
check("T1 rx bytes", [b for _, b in seen], [0x80, 0x00, 0x31, 0x00])
check("T1 rx_valid", [v for v, _ in seen], [1, 1, 1, 1])
# T2 — TX: the byte latched at the end of slot N appears in slot N+1,
# MSB first. Slot 0 is the reset value, before any load.
await cs_low(ctx)
ctx.set(dut.tx_byte, 0xA5)
s0 = await spi_byte(ctx, 0x00)
ctx.set(dut.tx_byte, 0x3C)
s1 = await spi_byte(ctx, 0x00)
ctx.set(dut.tx_byte, 0x00)
s2 = await spi_byte(ctx, 0x00)
await cs_high(ctx)
check("T2 miso slot0 (pre-load)", hex(s0), hex(0x00))
check("T2 miso slot1", hex(s1), hex(0xA5))
check("T2 miso slot2", hex(s2), hex(0x3C))
# T3 — tx_load pulses exactly once per byte, during the final bit.
await cs_low(ctx)
loads = 0
for _ in range(3):
for bit in range(8):
ctx.set(dut.spi_clk, 0)
await ctx.delay(HP)
loads += ctx.get(dut.tx_load)
ctx.set(dut.spi_clk, 1)
await ctx.delay(HP)
await cs_high(ctx)
check("T3 tx_load pulses", loads, 3)
# T4 — MISO releases HIGH when CS is deasserted, so it never fights
# another EXI device sharing the bus.
check("T4 miso idles high", ctx.get(dut.spi_miso), 1)
sim = Simulator(top)
sim.add_testbench(tb)
with sim.write_vcd("SPIMode3Slave.vcd"):
sim.run()
if failures:
print("\nFAILURES:")
for f in failures:
print(" -", f)
sys.exit(1)
print("\nAll tests passed.")