aboutsummaryrefslogtreecommitdiff
path: root/tools/pnr.sh
diff options
context:
space:
mode:
authorLouis <louis@dnge.de>2026-11-11 10:08:41 +0100
committerLouis <louis@dnge.de>2026-11-11 10:08:41 +0100
commit2e433fcd0d1afadd8ab786dd30d7d4546bdee43d (patch)
treef2a5603703d56b08c1eaa672eaf529ec4b2cc3fa /tools/pnr.sh
downloadhw-toolchain-2e433fcd0d1afadd8ab786dd30d7d4546bdee43d.tar.gz
hw-toolchain: placer, routers and KiCad helpers for agent-driven hardware work
Rust placer (simulated annealing) and negotiated-congestion router for 2-layer KiCad boards, pnr.sh to place, route, DRC-check and install candidates, Python KiCad helpers, repo conventions (AGENTS.md) and the tool documentation under knowledge/. Konnect (KiCad MCP, AGPL) as a submodule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tools/pnr.sh')
-rw-r--r--tools/pnr.sh60
1 files changed, 60 insertions, 0 deletions
diff --git a/tools/pnr.sh b/tools/pnr.sh
new file mode 100644
index 0000000..b21bde5
--- /dev/null
+++ b/tools/pnr.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+# Place and route a board with tools/placer + tools/router-rs, check the best candidates with
+# KiCad's DRC, and install one. See knowledge/tooling/pnr.md.
+#
+# sh tools/pnr.sh run BOARD_DIR [TOP] # 32 placements, all routed; DRC of the TOP (6) best
+# sh tools/pnr.sh install BOARD_DIR RANK # e.g. rank_00: checkpoint, references, pours, DRC, ERC
+#
+# BOARD_DIR holds <name>.kicad_pcb/.kicad_sch/.kicad_pro (name = the directory name) with
+# placement.toml and route.toml next to them. Output goes to BOARD_DIR/out/pnr/.
+# Close the board in KiCad first: an open editor rewrites .kicad_pro and would overwrite the
+# installed board on its next save. RUNS (32), JOBS (6), KICAD_PYTHON and KICAD_CLI are read
+# from the environment.
+set -e
+T=$(dirname "$0")
+. "$T/kicad_env.sh"
+CMD=$1; M=${2%/}; N=$(basename "$M"); O=$M/out/pnr
+[ -n "$CMD" ] && [ -d "$M" ] || { sed -n 5,6p "$0"; exit 1; }
+if ls "$M"/~"$N".kicad_p*.lck >/dev/null 2>&1; then echo "$N is open in KiCad; close it first" >&2; exit 1; fi
+
+case $CMD in
+run)
+ mkdir -p "$O"
+ cargo build --release -q --manifest-path "$T/placer/Cargo.toml"
+ $KC sch export netlist -o "$O/$N.net" "$M/$N.kicad_sch" >/dev/null
+ $KPY "$T/placer/export.py" "$M/$N.kicad_pcb" "$O/problem.json" "$O/$N.net" 2>/dev/null | tail -n 1
+ $KPY "$T/router/dump_board.py" "$M/$N.kicad_pcb" "$O/board.json" 2>/dev/null | tail -n 1
+ R=${RUNS:-32}
+ "$T/placer/target/release/placer" run "$O/problem.json" "$M/placement.toml" "$O/run" --runs "$R" --inner 10 \
+ --top "$R" --route "$O/board.json" "$M/route.toml" --route-top "$R" > "$O/placer.log" 2>&1
+ grep -A 5 'legal and complete' "$O/placer.log"
+ TOP=$(awk -v d="$O" -v n="${3:-6}" 'NR > 1 && NR <= n + 1 {print d "/run/rank_" $1 ".json"}' "$O/run/routed.tsv")
+ rm -rf "$O/eval"
+ # shellcheck disable=SC2086
+ E=$M BOARD=$N TOML=$M/route.toml JOBS=${JOBS:-6} sh "$T/pnr_eval.sh" "$O/eval" $TOP
+ ;;
+install)
+ R=$3; [ -f "$O/eval/$R/routed.kicad_pcb" ] || { echo "no $O/eval/$R/routed.kicad_pcb" >&2; exit 1; }
+ C=$M/out/before-pnr-$(date +%Y-%m-%d-%H%M); mkdir -p "$C"
+ cp "$M/$N.kicad_pcb" "$M/$N.kicad_pro" "$C/"
+ $KPY "$T/silk_refs.py" "$O/eval/$R/routed.kicad_pcb" "$O/silk.kicad_pcb" 2>/dev/null | tail -n 1
+ $KPY - "$O/silk.kicad_pcb" "$M/$N.kicad_pcb" <<'EOF' 2>/dev/null
+import sys, pcbnew
+b = pcbnew.LoadBoard(sys.argv[1])
+pcbnew.ZONE_FILLER(b).Fill(b.Zones())
+pcbnew.SaveBoard(sys.argv[2], b)
+EOF
+ cp "$C/$N.kicad_pro" "$M/$N.kicad_pro" # SaveBoard rewrites the project's net settings
+ $KC pcb drc --refill-zones --schematic-parity --format json -o "$O/drc.json" "$M/$N.kicad_pcb" >/dev/null
+ $KC sch erc --format json -o "$O/erc.json" "$M/$N.kicad_sch" >/dev/null
+ python3 - "$O/drc.json" "$O/erc.json" <<'EOF'
+import collections, json, sys
+d = json.load(open(sys.argv[1])); e = json.load(open(sys.argv[2]))
+c = collections.Counter((v['type'], v['severity']) for v in d['violations'])
+print('DRC %s unconnected=%d parity=%d | ERC %d' % (dict(c), len(d['unconnected_items']),
+ len(d.get('schematic_parity', [])), sum(len(s['violations']) for s in e['sheets'])))
+EOF
+ echo "checkpoint: $C"
+ ;;
+*) sed -n 5,6p "$0"; exit 1 ;;
+esac