Research-Stack/4-Infrastructure/hardware/build_fractal.sh
Brandon Schneider 9f304abab0 feat: fractal dimension — DBC algorithm (Python + FPGA)
Paper: 'Ultra-fast computation of fractal dimension for RGB images'
  (Pattern Analysis and Applications, 2025)

Python (fractal_dimension.py):
- DBC algorithm with numpy vectorization (29x faster than scalar)
- fd_compress_hint: FD → voltage mode (STORE/COMPUTE/APPROX/MORPHIC)
- 7/7 tests pass (Sierpinski, random, gradient, checkerboard, fBm, constant, RGB)
- Q16_16 integer arithmetic internally

FPGA (fractal_box_counter.v + fractal_fd_selector.v):
- 5-state FSM: IDLE → COLLECT → FINALIZE → STORE_LOG → REGRESS → DONE
- 8 power-of-two scales (2, 4, 8, ..., 256)
- Linear regression via Q16_16 64-bit arithmetic
- FD clamped to [1.0, 3.0] in Q16_16
- Selector: FD < 2.3 → STORE, < 2.6 → COMPUTE, < 2.9 → APPROX, >= 2.9 → MORPHIC
- Integrated into research_stack_top.v

FD drives adaptive compression:
  Low FD (smooth) → STORE mode (minimal compression)
  High FD (rough) → MORPHIC mode (aggressive compression)
2026-05-29 20:45:21 -05:00

64 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
# Fractal Box Counter — standalone build for Tang Nano 9K
# Verifies synthesis, P&R, and bitstream generation for the
# fractal dimension computation modules.
TOP="fractal_box_counter"
CST="research_stack_tangnano9k.cst"
JSON="fractal_box_counter.json"
PNR="fractal_box_counter_pnr.json"
FS="fractal_box_counter.fs"
DEVICE="GW1NR-LV9QN88PC6/I5"
FAMILY="GW1N-9C"
FREQ=27
echo "=== Fractal Box Counter Build ==="
echo "Top: ${TOP}"
echo "Device: ${DEVICE}"
echo "CST: ${CST}"
echo ""
# ── Step 1: Synthesis ─────────────────────────────────────────────
echo "=== Step 1: Synthesis (Yosys) ==="
yosys -p "
read_verilog -sv \
fractal_box_counter.v \
fractal_fd_selector.v;
synth_gowin -top ${TOP} -json ${JSON};
stat
"
# ── Step 2: Place & Route ─────────────────────────────────────────
echo ""
echo "=== Step 2: Place & Route (nextpnr-himbaechel) ==="
nextpnr-himbaechel --device "${DEVICE}" --json "${JSON}" --write "${PNR}" \
--freq "${FREQ}" --vopt "family=${FAMILY}" --vopt "cst=${CST}"
# ── Step 3: Bitstream ─────────────────────────────────────────────
echo ""
echo "=== Step 3: Bitstream (gowin_pack) ==="
gowin_pack -d "${FAMILY}" -o "${FS}" "${PNR}"
# ── Step 4: Resource Report ───────────────────────────────────────
echo ""
echo "=== Step 4: Resource Usage Report ==="
if [ -f "${JSON}" ]; then
echo "Synthesis JSON: ${JSON}"
# Extract cell counts from yosys stat output (already printed above)
fi
if [ -f "${PNR}" ]; then
echo "P&R JSON: ${PNR}"
fi
if [ -f "${FS}" ]; then
FS_SIZE=$(stat -c%s "${FS}" 2>/dev/null || stat -f%z "${FS}" 2>/dev/null || echo "unknown")
echo "Bitstream: ${FS} (${FS_SIZE} bytes)"
else
echo "WARNING: Bitstream not generated!"
fi
echo ""
echo "=== Build complete: ${FS} ==="
ls -lh "${FS}" 2>/dev/null || true