#!/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 .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