← Commit history

route: a rip-up no longer erases the copper of the routes it keeps, and the via check sees the whole of a long track

Drew Owens ·c3563e04a2 · ·parent 9b5be5d
rip_keeping and rip_cluster keep some routes of the net they rip (the decoupling pair routes; the other clusters' routes) but unstamp the rest cell by cell, which also cleared the cells those share with the kept routes. The kept routes (not a Kelvin tap, which keeps its own id) are stamped again after the unstamp; a restamp names its vias once. via_clear_exact skipped a segment when both its ends were more than 3 mm away in x, so a via could sit on the middle of a long straight track; it now skips a segment only when its whole box is out of reach. Co-Authored-By: Claude Opus 5.5 <[email protected]>
3 files changed +130−2
crates/aiflow-router/src/drive.rs+18−1
@@ -621,6 +621,7 @@ impl Router {                 keep.push(r);             }         }+        self.restamp_kept(net, &keep);         self.nets[net].routes = keep;         self.nets[net].used_escapes.retain(|(ci, _)| *ci != cluster);         self.nets[net].clusters[cluster].connected = false;@@ -648,6 +649,19 @@ impl Router {         self.rip_keeping(net, true);     } +    /// The routes a rip keeps are copper still: stamp them again. Unstamping a ripped route clears every+    /// cell and via disk it covered, and a kept route of the same net (a decoupling pair route, kept+    /// through rip-up; another cluster's route in `rip_cluster`) often shares cells with it, at the pin+    /// they both reach or where the rail ran along the pair. Without this those cells read as free, and a+    /// later net could put a via or a track on the kept copper: a short the plan reports as routed.+    /// A Kelvin tap is left as it is: its cells carry the tap's own id (foreign to the rest of its net), and+    /// the rail never shares them.+    fn restamp_kept(&mut self, net: usize, kept: &[Route]) {+        for r in kept.iter().filter(|r| !r.kelvin) {+            self.stamp_route(net, r, 1);+        }+    }+     pub fn rip_keeping(&mut self, net: usize, keep_pairs: bool) {         let routes = std::mem::take(&mut self.nets[net].routes);         let mut kept = Vec::new();@@ -661,6 +675,7 @@ impl Router {                 self.bump_history(c);             }         }+        self.restamp_kept(net, &kept);         self.nets[net].routes = kept;         self.nets[net].used_escapes.clear();         let n = self.nets[net].clusters.len();@@ -859,7 +874,9 @@ impl Router {             for r in &other.routes {                 let need = rv + self.rules.clearance + r.width / 2.0;                 for (ax, ay, bx, by) in route_segments(&g, &r.cells) {-                    if (ax - x).abs() > 3.0 && (bx - x).abs() > 3.0 {+                    // skip a segment only when its whole box is out of reach: a long straight run passes the+                    // point although both its ends are far from it+                    if x < ax.min(bx) - need || x > ax.max(bx) + need || y < ay.min(by) - need || y > ay.max(by) + need {                         continue;                     }                     if geom::seg_distance(x, y, ax, ay, bx, by) < need {
crates/aiflow-router/src/lib.rs+4−1
@@ -502,7 +502,10 @@ impl Router {             }         }         if sign > 0 {-            self.vias.push((i, j, nid, via, drill));+            // a kept route stamped again after a rip (restamp_kept) names its vias once+            if !self.vias.iter().any(|v| v.0 == i && v.1 == j && v.2 == nid) {+                self.vias.push((i, j, nid, via, drill));+            }         } else {             self.vias.retain(|v| !(v.0 == i && v.1 == j && v.2 == nid));         }
crates/aiflow-router/tests/pair_copper.rsadded+108
@@ -0,0 +1,108 @@+//! A decoupling pair route is kept through rip-up (decoupling.rs). Ripping the other routes of its net+//! must leave the pair's copper stamped: they share grid cells at the pin they both reach, and a cell+//! cleared there reads as free to every later net (a via or track of another net landed on a kept+//! 1V1 decoupling track, and the plan still counted both nets routed).+use aiflow_board::Board;+use aiflow_router::decoupling::Pair;+use aiflow_router::{End, Route, Router, Rules, Term};+use serde_json::json;++fn board() -> Board {+    Board::from_text("pair.kicad_pcb", r#"(kicad_pcb+      (layers (0 "F.Cu" signal) (31 "B.Cu" signal))+      (net 1 "1V1") (net 2 "3V3")+      (gr_rect (start 0 0) (end 30 14) (layer "Edge.Cuts"))+      (footprint "C" (layer "F.Cu") (at 5 7) (property "Reference" "C1")+        (pad "1" smd rect (at 0 0) (size 1 1) (layers "F.Cu") (net 1 "1V1")))+      (footprint "U" (layer "F.Cu") (at 15 7) (property "Reference" "U1")+        (pad "1" smd rect (at 0 0) (size 1 1) (layers "F.Cu") (net 1 "1V1")))+      (footprint "R" (layer "F.Cu") (at 25 7) (property "Reference" "R1")+        (pad "1" smd rect (at 0 0) (size 1 1) (layers "F.Cu") (net 1 "1V1")))+      (footprint "J" (layer "F.Cu") (at 15 12) (property "Reference" "J1")+        (pad "1" smd rect (at -6 0) (size 1 1) (layers "F.Cu") (net 2 "3V3"))+        (pad "2" smd rect (at 6 0) (size 1 1) (layers "F.Cu") (net 2 "3V3"))))"#.into()).unwrap()+}++#[test]+fn ripping_a_net_keeps_its_decoupling_pair_copper() {+    let mut rules = Rules::default();+    rules.decoupling = Pair::from_spec(&json!({"decoupling": [{"cap": "C1.1", "pin": "U1.1"}]})).unwrap();+    let mut r = Router::new(board(), rules, false);+    let net = r.net_ids["1V1"];+    let other = r.net_ids["3V3"];+    r.route_decoupling();+    let pair = r.nets[net].routes.iter().find(|x| x.pair.is_some()).expect("the pair is routed directly").clone();+    let nid = r.nets[net].idx as i16;+    let stamped = |r: &Router, cells: &[aiflow_router::Term]| cells.iter().filter(|c| r.route_raw[c.layer()][c.j as usize * r.grid.w + c.i as usize] == nid).count();+    assert_eq!(stamped(&r, &pair.cells), pair.cells.len());+    // the rail joins U1 and R1 to the pair; its route leaves U1.1, where the pair arrives+    r.route_net(net);+    assert!(r.nets[net].routes.iter().any(|x| x.pair.is_none()), "the rail is routed");+    // a rip-up of the rail keeps the pair route, and must keep its copper+    r.rip(net);+    assert!(r.nets[net].routes.iter().all(|x| x.pair.is_some()) && !r.nets[net].routes.is_empty());+    assert_eq!(stamped(&r, &pair.cells), pair.cells.len(), "every cell of the kept pair route is still copper of 1V1");+    // and no via of another net may sit on the pair's track+    let mid = pair.cells[pair.cells.len() / 2];+    let (x, y) = r.grid.to_xy(mid.i as i64, mid.j as i64);+    assert!(!r.via_clear_exact(other, x, y), "a 3V3 via on the kept 1V1 pair track");+    // the pair's own end at U1.1: stamped too+    let last = *pair.cells.last().unwrap();+    assert_eq!(r.route_raw[last.layer()][last.j as usize * r.grid.w + last.i as usize], nid);+}++/// A straight run of another net, long enough that both its ends are more than 3 mm from the via:+/// the via check skipped such a segment by its ends' x alone and let the via sit on the track.+#[test]+fn no_via_on_the_middle_of_a_long_straight_track() {+    let mut r = Router::new(board(), Rules::default(), false);+    let (net, other) = (r.net_ids["1V1"], r.net_ids["3V3"]);+    let (i0, j) = r.grid.to_cell(5.0, 4.0);+    let (i1, _) = r.grid.to_cell(25.0, 4.0);+    let cells: Vec<Term> = (i0..=i1).map(|i| Term::new(0, i, j)).collect();+    r.commit(net, Route::new(cells, 0.25, End::Copper, End::Copper));+    assert!(!r.via_clear_exact(other, 15.0, 4.0), "a 3V3 via on the middle of a 20 mm 1V1 track");+    assert!(!r.via_clear_exact(other, 15.0, 4.4), "a 3V3 via within clearance of it");+    assert!(r.via_clear_exact(other, 15.0, 9.5), "clear of it, a via is fine");+}++/// A route stamped again (a kept route after a rip) names each of its vias once.+#[test]+fn a_restamped_route_names_its_vias_once() {+    let mut r = Router::new(board(), Rules::default(), false);+    let net = r.net_ids["1V1"];+    let (i, j) = r.grid.to_cell(20.0, 4.0);+    let (i, j) = (i, j);+    let cells = vec![Term::new(0, i - 2, j), Term::new(0, i, j), Term::new(1, i, j), Term::new(1, i + 2, j)];+    let route = Route::new(cells, 0.25, End::Copper, End::Copper);+    r.commit(net, route.clone());+    assert_eq!(r.vias.len(), 1);+    r.stamp_route(net, &route, 1);+    assert_eq!(r.vias.len(), 1, "{:?}", r.vias);+}++/// A Kelvin tap kept by `rip_cluster` keeps its own id on its cells (foreign copper to the rest of its net):+/// stamping the kept routes again after a rip must not turn it into plain copper of the net.+#[test]+fn a_kept_kelvin_tap_stays_foreign_to_its_net() {+    let mut r = Router::new(board(), Rules::default(), false);+    let net = r.net_ids["1V1"];+    let (i0, j) = r.grid.to_cell(6.0, 10.0);+    let (i1, _) = r.grid.to_cell(14.0, 10.0);+    let mut tap = Route::new((i0..=i1).map(|i| Term::new(0, i, j)).collect(), 0.2, End::Copper, End::Copper);+    tap.kelvin = true;+    r.commit(net, tap.clone());+    // the tap's cells carry its own id, as route_kelvin stamps them+    let kid: i16 = 30001;+    for c in &tap.cells { let k = c.j as usize * r.grid.w + c.i as usize; r.route_raw[0][k] = kid; }+    // a route of another cluster of the net, ripped: the tap is kept+    let c = r.nets[net].clusters.len() - 1;+    let (a, _) = r.grid.to_cell(20.0, 4.0);+    let (b, jj) = r.grid.to_cell(26.0, 4.0);+    let mut other = Route::new((a..=b).map(|i| Term::new(0, i, jj)).collect(), 0.25, End::Copper, End::Copper);+    other.start = End::Pad(c, Term::new(0, a, jj));+    r.commit(net, other);+    r.rip_cluster(net, c);+    assert!(r.nets[net].routes.iter().any(|x| x.kelvin));+    assert!(tap.cells.iter().all(|c| r.route_raw[0][c.j as usize * r.grid.w + c.i as usize] == kid), "the tap is still its own copper");+}