BF tidy wiring laid a wire along another signal's row

When a target sat level with another wire's source (Y3 next to S2), the
column order counted that as free, and ties went to the leftmost column:
S3 stepped left of S2, its last run shared S2's row, and the overlap pass
pushed it onto a side lane with a jog before the pin. Now a shared row
costs more than a crossing, ties keep top-to-bottom order, and a tidy
route that would still run along another signal goes to the usual router.
Tests: the reported layout, and no shared lines in tidy mode for every
example circuit.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-21 10:31:13 +02:00
parent f1b72b3e3c
commit 183edc784f
2 changed files with 48 additions and 6 deletions

View File

@ -353,10 +353,20 @@ interface TidyNet {
const inside = (y: number, n: TidyNet) => y > n.min + EPS && y < n.max - EPS
/** Crossings between two signals in one gap when `a` takes the column left of `b`. */
/** Cost of a wire lying along another signal's line: far worse than a crossing. */
const SHARED_LINE = 10
/**
* Crossings between two signals in one gap when `a` takes the column left
* of `b`. If `a` ends on the row where `b` starts, `b`'s first run and
* `a`'s last run would share that row between the two columns.
*/
function crossings(a: TidyNet, b: TidyNet): number {
let n = inside(b.y1, a) ? 1 : 0
for (const y of a.ys) if (inside(y, b)) n++
for (const y of a.ys) {
if (inside(y, b)) n++
if (Math.abs(y - b.y1) < EPS) n += SHARED_LINE
}
return n
}
@ -373,7 +383,8 @@ function order(nets: TidyNet[]): TidyNet[] {
for (let at = 0; at <= out.length; at++) {
let c = 0
for (let i = 0; i < out.length; i++) c += i < at ? crossings(out[i]!, n) : crossings(n, out[i]!)
if (c < bestCost) {
// On a tie the later place wins, so signals keep their top-to-bottom order.
if (c <= bestCost) {
bestCost = c
best = at
}
@ -461,9 +472,12 @@ function tidyRoutes(inputs: RouteInput[], boxes: Box[], occ: Occupancy, bodies:
const x = cols[i]!
for (const w of n.wires) {
const route = simplify([w.a, [x, w.a[1]], [x, w.b[1]], w.b])
// Through a part, or along another signal's line: the usual router finds a way around.
let blocked = false
for (let s = 0; s < route.length - 1 && !blocked; s++) blocked = bodies.hits(route[s]!, route[s + 1]!) > 0
if (blocked) continue // the usual router finds a way around
for (let s = 0; s < route.length - 1 && !blocked; s++) {
blocked = bodies.hits(route[s]!, route[s + 1]!) > 0 || occ.overlap(route[s]!, route[s + 1]!, w.net) > 0
}
if (blocked) continue
grid.set(w.id, route)
occ.add(route, w.net)
}

View File

@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { shapeOf } from '../app/lib/logic/geometry'
import { shapeOf, type Pt } from '../app/lib/logic/geometry'
import { createProject, lookup } from '../app/lib/logic/ops'
import { circuitRoutes, findCrossings, routeAll, type Routed } from '../app/lib/logic/routing'
@ -105,3 +105,31 @@ describe('routing', () => {
expect(c.map(x => [routes[x.wire]!.id, x.x])).toEqual([['h', 5], ['v2', 5]])
})
})
describe('tidy routing', () => {
// An adder's outputs S0..S3 (rows 0..3) to output pins Y0..Y3 placed higher
// up (rows -4, -2, 0, 2), so Y3 sits level with S2. Reported after "Tidy all wires".
const inputs = [0, 1, 2, 3].map(i => ({ id: `w${i}`, net: `S${i}`, a: [0, i] as Pt, b: [14, -4 + 2 * i] as Pt }))
it('steps the wires in source order and never lays one on another signal’s row', () => {
const routes = routeAll(inputs, [], [], { tidy: true })
expect(overlaps(routes)).toEqual([])
// Drawn exactly as routed: nothing had to be moved onto a side lane.
for (const r of routes) expect(r.draw).toEqual(r.grid)
const cols = routes.map(r => r.grid[1]![0])
expect(cols).toEqual([...cols].sort((a, b) => a - b))
expect(new Set(cols).size).toBe(4)
})
})
describe('tidy routing on every example', () => {
const p = createProject({ examples: true })
const get = lookup(p)
for (const def of p.components) {
it(`${def.name}: tidy wiring keeps every signal on its own lines`, () => {
const shapes = new Map(def.nodes.map(n => [n.id, shapeOf(n, get)]))
const routes = circuitRoutes({ ...def, wires: def.wires.map(w => ({ ...w, pts: undefined })) }, shapes, { tidy: true })
expect(overlaps(routes)).toEqual([])
})
}
})