Files
FPGA-Neural/tools/netasm/frames.py
T
micheleandClaude Sonnet 5 55c827bedf feat: PSRAM page-mode reads + graph engine (Type #2) + real pinout/IRQ pins
PSRAM page-mode read burst support in psram_controller.v: enables the
ISSI IS66WVE4M16EBLL-70BLI's page mode via its configuration-register
software-access sequence at boot (disabled by default on the real
chip), then keeps CE#/OE# asserted after a read so a same-page
continuation only pays tAPA (20ns) instead of a full tAA (70ns)
random access, with automatic tCEM-safe session closing. Only a WRITE
closes the page -- byte-enable changes do not, since
int8_memory_access.v alternates them on nearly every access and an
early implementation attempt that treated them as a close condition
measured a real regression (53.25->61.25 cycles/edge) before being
corrected (53.25->37.53 cycles/edge, +42% gather bandwidth).
sim/psram_model.v gained independent tAPA/tAA and tCEM enforcement
(with a real Verilog same-timestep event-ordering race found and
fixed via a #0 sync) so the regression proves real timing compliance,
not just data correctness. New sim/psram_page_mode_tb.v; full 26-file
regression suite re-run clean. Real nextpnr-ecp5 Fmax re-measured on
the full spi_neuron_top system: 75.73MHz (P2, up from 55.59MHz) and
65.13MHz (P8) -- still under the 80MHz target but not regressed, with
the critical path confirmed (not assumed) to remain entirely inside
neuron_parallel's accumulate chain, never psram_controller.

Also includes this session's other already-validated work: the graph
engine (Type #2 sparse-graph network: act_buffer, graph_engine,
netasm host assembler), real CABGA381 pinout (.lpf, place&route
verified) and physical IRQ_N/DATA_READY_N pins, and Phase 7 timing
closure logs -- all previously uncommitted, documented in WORKLOG.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LH3jPeJ3eFMfF2v8SQhpkk
2026-09-03 17:12:05 +02:00

132 lines
3.3 KiB
Python

"""
SPI frame encoding matching rtl/spi_engine.v's opcode set (§5 of the
spec). A "frame" is the exact sequence of bytes shifted over MOSI
during one CS-low transaction -- opcode byte first, then whatever
fixed-size payload that opcode expects. This module only builds
byte sequences; it does not talk to real hardware.
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import List
OP_NOP = 0x00
OP_WRITE_RAM = 0x01
OP_READ_RAM = 0x02
OP_RESET = 0x0F
OP_SET_BASE = 0x10
OP_SET_NET_TYPE = 0x11
OP_START = 0x20
OP_STATUS = 0x21
OP_READ_OUTPUT = 0x22
OP_RUN_NETWORK = 0x23
OP_READ_CONFIG = 0x30
SEL_X_BASE = 0x00
SEL_W_BASE = 0x01
SEL_BIAS_ADDR = 0x02
SEL_TABLE_BASE = 0x03
SEL_BUF_A_BASE = 0x04
SEL_BUF_B_BASE = 0x05
SEL_ACTIVATION = 0x06
SEL_N_INPUTS = 0x07
SEL_N_NEURONS = 0x08
SEL_NUM_NEURONS_GRAPH = 0x09
SEL_N_OUT = 0x0A
NET_TYPE_DENSE = 0x01
NET_TYPE_GRAPH = 0x02
ACT_NONE = 0
ACT_RELU = 1
def _u24(v: int) -> bytes:
if not (0 <= v < (1 << 24)):
raise ValueError(f"address 0x{v:x} does not fit in 24 bits")
return bytes([(v >> 16) & 0xFF, (v >> 8) & 0xFF, v & 0xFF])
def _u16(v: int) -> bytes:
if not (0 <= v < (1 << 16)):
raise ValueError(f"value 0x{v:x} does not fit in 16 bits")
return bytes([(v >> 8) & 0xFF, v & 0xFF])
def _i8(v: int) -> int:
if not (-128 <= v <= 127):
raise ValueError(f"value {v} does not fit in a signed byte")
return v & 0xFF
@dataclass
class Frame:
label: str
data: bytes
def reset() -> Frame:
return Frame("RESET", bytes([OP_RESET]))
def set_net_type(net_type: int) -> Frame:
return Frame(f"SET_NET_TYPE({net_type:#04x})", bytes([OP_SET_NET_TYPE, net_type & 0xFF]))
def set_base(sel: int, addr: int) -> Frame:
return Frame(
f"SET_BASE(sel={sel:#04x}, addr={addr:#08x})",
bytes([OP_SET_BASE, sel & 0xFF]) + _u24(addr),
)
def write_ram(addr: int, data: bytes) -> Frame:
return Frame(
f"WRITE_RAM(addr={addr:#08x}, len={len(data)})",
bytes([OP_WRITE_RAM]) + _u24(addr) + _u16(len(data)) + bytes(data),
)
def read_ram(addr: int, length: int) -> Frame:
return Frame(
f"READ_RAM(addr={addr:#08x}, len={length})",
bytes([OP_READ_RAM]) + _u24(addr) + _u16(length),
)
def run_network(payload_byte: int = 0) -> Frame:
return Frame(f"RUN_NETWORK(payload={payload_byte:#04x})", bytes([OP_RUN_NETWORK, payload_byte & 0xFF]))
def start() -> Frame:
return Frame("START", bytes([OP_START]))
def status() -> Frame:
return Frame("STATUS", bytes([OP_STATUS, 0x00]))
def read_config() -> Frame:
return Frame("READ_CONFIG", bytes([OP_READ_CONFIG] + [0x00] * 10))
def dump_frames(frames: List[Frame], path: str) -> None:
"""Length-prefixed binary dump: for each frame, a 2-byte
big-endian length followed by that many payload bytes. A simple
host driver replays this by, for each record, asserting CS,
shifting out the bytes, then deasserting CS."""
with open(path, "wb") as f:
for fr in frames:
n = len(fr.data)
f.write(bytes([(n >> 8) & 0xFF, n & 0xFF]))
f.write(fr.data)
def frames_as_hex(frames: List[Frame]) -> str:
lines = []
for fr in frames:
hexbytes = " ".join(f"{b:02x}" for b in fr.data)
lines.append(f"{fr.label:40s} : {hexbytes}")
return "\n".join(lines)