-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdiff_pair_loop.py
More file actions
741 lines (666 loc) · 42.9 KB
/
diff_pair_loop.py
File metadata and controls
741 lines (666 loc) · 42.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
"""
Differential pair routing loop.
This module contains the main loop for routing differential pairs,
extracted from route.py for better maintainability.
"""
import time
from typing import List, Tuple
from routing_state import RoutingState
from routing_config import DiffPairNet
from memory_debug import get_process_memory_mb, estimate_track_proximity_cache_mb
from obstacle_costs import compute_track_proximity_for_net
from net_queries import calculate_route_length
from pcb_modification import add_route_to_pcb_data
from diff_pair_routing import route_diff_pair_with_obstacles, get_diff_pair_endpoints
from blocking_analysis import analyze_frontier_blocking, print_blocking_analysis, filter_rippable_blockers, invalidate_obstacle_cache
from rip_up_reroute import rip_up_net, restore_net
from polarity_swap import apply_polarity_swap, get_canonical_net_id
from layer_swap_fallback import try_fallback_layer_swap, add_own_stubs_as_obstacles_for_diff_pair
from routing_context import build_diff_pair_obstacles, restore_ripped_net
from terminal_colors import RED, GREEN, RESET
def route_diff_pairs(
state: RoutingState,
diff_pair_ids_to_route: List[Tuple[str, DiffPairNet]],
) -> Tuple[int, int, float, int, int]:
"""
Route all differential pairs.
Args:
state: The routing state object containing all shared state
diff_pair_ids_to_route: List of (pair_name, pair) tuples to route
Returns:
Tuple of (successful, failed, total_time, total_iterations, route_index)
"""
# Extract frequently-used state fields as local references
pcb_data = state.pcb_data
config = state.config
routed_net_ids = state.routed_net_ids
routed_net_paths = state.routed_net_paths
routed_results = state.routed_results
diff_pair_by_net_id = state.diff_pair_by_net_id
track_proximity_cache = state.track_proximity_cache
layer_map = state.layer_map
reroute_queue = state.reroute_queue
queued_net_ids = state.queued_net_ids
polarity_swapped_pairs = state.polarity_swapped_pairs
rip_and_retry_history = state.rip_and_retry_history
ripup_success_pairs = state.ripup_success_pairs
remaining_net_ids = state.remaining_net_ids
results = state.results
pad_swaps = state.pad_swaps
all_unrouted_net_ids = state.all_unrouted_net_ids
gnd_net_id = state.gnd_net_id
base_obstacles = state.base_obstacles
diff_pair_base_obstacles = state.diff_pair_base_obstacles
diff_pair_extra_clearance = state.diff_pair_extra_clearance
enable_layer_switch = state.enable_layer_switch
all_swap_vias = state.all_swap_vias
all_segment_modifications = state.all_segment_modifications
target_swaps = state.target_swaps
total_routes = state.total_routes
# Counters
successful = 0
failed = 0
total_time = 0.0
total_iterations = 0
route_index = 0
# Cache for obstacle cells - persists across retry iterations for performance
obstacle_cache = {}
user_cancelled = False
for pair_name, pair in diff_pair_ids_to_route:
# Check for cancellation request
if state.cancel_check is not None and state.cancel_check():
print("\nRouting cancelled by user")
user_cancelled = True
break
route_index += 1
failed_str = f" ({failed} failed)" if failed > 0 else ""
print(f"\n[{route_index}/{total_routes}{failed_str}] Routing diff pair {pair_name}")
print(f" P: {pair.p_net_name} (id={pair.p_net_id})")
print(f" N: {pair.n_net_name} (id={pair.n_net_id})")
print("-" * 40)
# Update progress
if state.progress_callback is not None:
msg = pair_name
if failed > 0:
msg += f" ({failed} failed)"
state.progress_callback(route_index, total_routes, msg)
# Periodic memory reporting (every 5 diff pairs)
if config.debug_memory and (route_index % 5 == 1 or route_index == len(diff_pair_ids_to_route)):
current_mem = get_process_memory_mb()
prox_cache_mb = estimate_track_proximity_cache_mb(track_proximity_cache)
print(f"[MEMORY] Diff pair {route_index}/{len(diff_pair_ids_to_route)}: {current_mem:.1f} MB total, "
f"track_proximity_cache: {prox_cache_mb:.1f} MB ({len(track_proximity_cache)} nets)")
start_time = time.time()
# Build complete obstacle map for diff pair routing
obstacles, unrouted_stubs = build_diff_pair_obstacles(
diff_pair_base_obstacles, pcb_data, config, routed_net_ids, remaining_net_ids,
all_unrouted_net_ids, pair.p_net_id, pair.n_net_id, gnd_net_id,
track_proximity_cache, layer_map, diff_pair_extra_clearance,
add_own_stubs_func=add_own_stubs_as_obstacles_for_diff_pair,
ripped_route_layer_costs=state.ripped_route_layer_costs,
ripped_route_via_positions=state.ripped_route_via_positions
)
# Get source/target coordinates for blocking analysis (center of P/N endpoints)
sources, targets, _ = get_diff_pair_endpoints(pcb_data, pair.p_net_id, pair.n_net_id, config)
source_xy = None
target_xy = None
if sources:
src = sources[0]
source_xy = ((src[5] + src[7]) / 2, (src[6] + src[8]) / 2)
if targets:
tgt = targets[0]
target_xy = ((tgt[5] + tgt[7]) / 2, (tgt[6] + tgt[8]) / 2)
# Route the differential pair
result = route_diff_pair_with_obstacles(pcb_data, pair, config, obstacles, base_obstacles,
unrouted_stubs)
# Handle probe_blocked: try progressive rip-up before full route
probe_ripup_attempts = 0
probe_ripped_items = []
while result and result.get('probe_blocked') and probe_ripup_attempts < config.max_rip_up_count:
blocked_at = result.get('blocked_at', 'unknown')
blocked_cells = result.pop('blocked_cells', [])
probe_direction = result.get('direction', 'unknown')
if not blocked_cells:
print(f" Probe {probe_direction} blocked at {blocked_at} but no blocked cells to analyze")
break
# Analyze which routed nets are blocking
blockers = analyze_frontier_blocking(
blocked_cells, pcb_data, config, routed_net_paths,
exclude_net_ids={pair.p_net_id, pair.n_net_id},
extra_clearance=config.diff_pair_gap / 2,
target_xy=target_xy,
source_xy=source_xy,
obstacle_cache=obstacle_cache
)
# Filter to rippable blockers (only those we've routed)
rippable_blockers = []
seen_canonical_ids = set()
for b in blockers:
canonical = get_canonical_net_id(b.net_id, diff_pair_by_net_id)
if canonical not in seen_canonical_ids and b.net_id in routed_results:
seen_canonical_ids.add(canonical)
rippable_blockers.append(b)
if not rippable_blockers:
print(f" Probe {probe_direction} blocked at {blocked_at} but no rippable blockers found")
# Restore any nets that were ripped in previous attempts
if probe_ripped_items:
print(f" Restoring {len(probe_ripped_items)} previously ripped net(s)")
for ripped_blocker, ripped_saved, ripped_ids_item, ripped_was_in_results in reversed(probe_ripped_items):
restore_ripped_net(
pcb_data, ripped_saved, ripped_ids_item, ripped_was_in_results,
routed_net_ids, remaining_net_ids, routed_results, results,
config, track_proximity_cache, layer_map
)
probe_ripped_items.clear()
# Convert to failed result so normal failure handling takes over
result = {
'failed': True,
'iterations': result.get('iterations', 0),
'blocked_cells_forward': blocked_cells if probe_direction == 'forward' else [],
'blocked_cells_backward': blocked_cells if probe_direction == 'backward' else [],
}
break
probe_ripup_attempts += 1
print(f" Probe blocked at {blocked_at}, rip-up attempt {probe_ripup_attempts}/{config.max_rip_up_count}")
# Rip up the top blocker
blocker = rippable_blockers[0]
print(f" Ripping up {blocker.net_name} ({blocker.blocked_count} blocked cells)")
saved_result, ripped_ids, was_in_results = rip_up_net(
blocker.net_id, pcb_data, routed_net_ids, routed_net_paths,
routed_results, diff_pair_by_net_id, remaining_net_ids,
results, config, track_proximity_cache,
state.working_obstacles, state.net_obstacles_cache,
state.ripped_route_layer_costs, state.ripped_route_via_positions,
layer_map
)
probe_ripped_items.append((blocker, saved_result, ripped_ids, was_in_results))
# Rebuild obstacles without ripped net
retry_obstacles, _ = build_diff_pair_obstacles(
diff_pair_base_obstacles, pcb_data, config, routed_net_ids, remaining_net_ids,
all_unrouted_net_ids, pair.p_net_id, pair.n_net_id, gnd_net_id,
track_proximity_cache, layer_map, diff_pair_extra_clearance,
add_own_stubs_func=add_own_stubs_as_obstacles_for_diff_pair,
ripped_route_layer_costs=state.ripped_route_layer_costs,
ripped_route_via_positions=state.ripped_route_via_positions
)
# Retry the route
result = route_diff_pair_with_obstacles(pcb_data, pair, config, retry_obstacles, base_obstacles,
unrouted_stubs)
# If retry succeeded, queue ALL ripped nets for re-routing
if result and not result.get('failed') and not result.get('probe_blocked'):
for ripped_blocker, ripped_saved, ripped_ids_item, ripped_was_in_results in probe_ripped_items:
if ripped_was_in_results:
successful -= 1
if ripped_blocker.net_id in diff_pair_by_net_id:
ripped_pair_name, ripped_pair = diff_pair_by_net_id[ripped_blocker.net_id]
canonical_id = ripped_pair.p_net_id
if canonical_id not in queued_net_ids:
reroute_queue.append(('diff_pair', ripped_pair_name, ripped_pair))
queued_net_ids.add(canonical_id)
else:
if ripped_blocker.net_id not in queued_net_ids:
reroute_queue.append(('single', ripped_blocker.net_name, ripped_blocker.net_id))
queued_net_ids.add(ripped_blocker.net_id)
ripup_success_pairs.add(pair_name)
print(f" Probe rip-up succeeded, {len(probe_ripped_items)} net(s) queued for re-routing")
elif result and result.get('probe_blocked'):
print(f" Still blocked after rip-up, trying next...")
else:
# Route failed completely - restore ALL ripped nets
print(f" Probe rip-up didn't help, restoring {len(probe_ripped_items)} net(s)")
for ripped_blocker, ripped_saved, ripped_ids_item, ripped_was_in_results in reversed(probe_ripped_items):
restore_ripped_net(
pcb_data, ripped_saved, ripped_ids_item, ripped_was_in_results,
routed_net_ids, remaining_net_ids, routed_results, results,
config, track_proximity_cache, layer_map
)
probe_ripped_items.clear()
break
# If probe rip-up exhausted attempts without success, restore all and try full route
if result and result.get('probe_blocked'):
print(f" Probe blocked after {probe_ripup_attempts} rip-up attempts, restoring {len(probe_ripped_items)} net(s), trying full route...")
for ripped_blocker, ripped_saved, ripped_ids_item, ripped_was_in_results in reversed(probe_ripped_items):
restore_ripped_net(
pcb_data, ripped_saved, ripped_ids_item, ripped_was_in_results,
routed_net_ids, remaining_net_ids, routed_results, results,
config, track_proximity_cache, layer_map
)
result = {
'failed': True,
'iterations': result.get('iterations', 0),
'blocked_cells_forward': result.get('blocked_cells', []) if result.get('direction') == 'forward' else [],
'blocked_cells_backward': result.get('blocked_cells', []) if result.get('direction') == 'backward' else [],
}
elapsed = time.time() - start_time
total_time += elapsed
if result and not result.get('failed'):
# Calculate actual routed length from segments (includes connectors and via barrels)
new_segments = result.get('new_segments', [])
new_vias = result.get('new_vias', [])
p_segments = [s for s in new_segments if s.net_id == pair.p_net_id]
n_segments = [s for s in new_segments if s.net_id == pair.n_net_id]
p_vias = [v for v in new_vias if v.net_id == pair.p_net_id]
n_vias = [v for v in new_vias if v.net_id == pair.n_net_id]
p_routed_length = calculate_route_length(p_segments, p_vias, pcb_data)
n_routed_length = calculate_route_length(n_segments, n_vias, pcb_data)
centerline_length = (p_routed_length + n_routed_length) / 2
# Calculate stub lengths for pad-to-pad measurement
# Use pre-calculated stub lengths from routing, accounting for polarity swap
p_src_stub = result.get('p_src_stub_length', 0.0)
p_tgt_stub = result.get('p_tgt_stub_length', 0.0)
n_src_stub = result.get('n_src_stub_length', 0.0)
n_tgt_stub = result.get('n_tgt_stub_length', 0.0)
polarity_fixed = result.get('polarity_fixed', False)
if polarity_fixed:
# After swap: P gets P_source + N_target, N gets N_source + P_target
p_stub_length = p_src_stub + n_tgt_stub
n_stub_length = n_src_stub + p_tgt_stub
else:
p_stub_length = p_src_stub + p_tgt_stub
n_stub_length = n_src_stub + n_tgt_stub
avg_stub_length = (p_stub_length + n_stub_length) / 2
# Store lengths for length matching
result['centerline_length'] = centerline_length
result['stub_length'] = avg_stub_length
result['p_stub_length'] = p_stub_length
result['n_stub_length'] = n_stub_length
result['p_routed_length'] = p_routed_length
result['n_routed_length'] = n_routed_length
# For inter-pair matching: use max(P,N) total if intra-pair will equalize them, else use average
if config.diff_pair_intra_match:
p_total = p_routed_length + p_stub_length
n_total = n_routed_length + n_stub_length
result['route_length'] = max(p_total, n_total)
else:
result['route_length'] = centerline_length + avg_stub_length
print(f" SUCCESS: {len(result['new_segments'])} segments, {len(result['new_vias'])} vias, {result['iterations']} iterations ({elapsed:.2f}s)")
print(f" Centerline length: {centerline_length:.3f}mm, stub length: {avg_stub_length:.3f}mm, total: {result['route_length']:.3f}mm")
results.append(result)
successful += 1
total_iterations += result['iterations']
# Check if polarity was fixed
apply_polarity_swap(pcb_data, result, pad_swaps, pair_name, polarity_swapped_pairs)
add_route_to_pcb_data(pcb_data, result, debug_lines=config.debug_lines)
if pair.p_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.p_net_id)
if pair.n_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.n_net_id)
routed_net_ids.append(pair.p_net_id)
routed_net_ids.append(pair.n_net_id)
track_proximity_cache[pair.p_net_id] = compute_track_proximity_for_net(pcb_data, pair.p_net_id, config, layer_map)
track_proximity_cache[pair.n_net_id] = compute_track_proximity_for_net(pcb_data, pair.n_net_id, config, layer_map)
if result.get('p_path'):
routed_net_paths[pair.p_net_id] = result['p_path']
if result.get('n_path'):
routed_net_paths[pair.n_net_id] = result['n_path']
routed_results[pair.p_net_id] = result
routed_results[pair.n_net_id] = result
diff_pair_by_net_id[pair.p_net_id] = (pair_name, pair)
diff_pair_by_net_id[pair.n_net_id] = (pair_name, pair)
# Invalidate cache entries since we added segments
invalidate_obstacle_cache(obstacle_cache, pair.p_net_id)
invalidate_obstacle_cache(obstacle_cache, pair.n_net_id)
else:
iterations = result['iterations'] if result else 0
print(f" FAILED: Could not find route ({elapsed:.2f}s)")
total_iterations += iterations
# Try rip-up and reroute with progressive N+1
ripped_up = False
if routed_net_paths and result:
# Find the direction that failed faster
fwd_iters = result.get('iterations_forward', 0)
bwd_iters = result.get('iterations_backward', 0)
fwd_cells = result.pop('blocked_cells_forward', [])
bwd_cells = result.pop('blocked_cells_backward', [])
# Check for setback failure
is_setback_failure = (fwd_iters == 0 and bwd_iters == 0 and (fwd_cells or bwd_cells))
if is_setback_failure:
blocked_cells = list(set(fwd_cells + bwd_cells))
if blocked_cells:
print(f" Setback blocked ({len(blocked_cells)} blocked cells)")
elif fwd_iters > 0 and (bwd_iters == 0 or fwd_iters <= bwd_iters):
fastest_dir = 'forward'
blocked_cells = fwd_cells
dir_iters = fwd_iters
if blocked_cells:
print(f" {fastest_dir.capitalize()} direction failed fastest ({dir_iters} iterations, {len(blocked_cells)} blocked cells)")
else:
fastest_dir = 'backward'
blocked_cells = bwd_cells
dir_iters = bwd_iters
if blocked_cells:
print(f" {fastest_dir.capitalize()} direction failed fastest ({dir_iters} iterations, {len(blocked_cells)} blocked cells)")
# Analyze blocking
if blocked_cells:
blockers = analyze_frontier_blocking(
blocked_cells, pcb_data, config, routed_net_paths,
exclude_net_ids={pair.p_net_id, pair.n_net_id},
extra_clearance=diff_pair_extra_clearance,
target_xy=target_xy,
source_xy=source_xy,
obstacle_cache=obstacle_cache
)
print_blocking_analysis(blockers)
rippable_blockers, seen_canonical_ids = filter_rippable_blockers(
blockers, routed_results, diff_pair_by_net_id, get_canonical_net_id
)
# Progressive rip-up: try N=1, then N=2, etc
current_canonical = pair.p_net_id
ripped_items = []
ripped_canonical_ids = set()
retry_succeeded = False
last_retry_blocked_cells = blocked_cells
for N in range(1, config.max_rip_up_count + 1):
if N > 1 and last_retry_blocked_cells:
print(f" Re-analyzing {len(last_retry_blocked_cells)} blocked cells from N={N-1} retry:")
fresh_blockers = analyze_frontier_blocking(
last_retry_blocked_cells, pcb_data, config, routed_net_paths,
exclude_net_ids={pair.p_net_id, pair.n_net_id},
extra_clearance=diff_pair_extra_clearance,
target_xy=target_xy,
source_xy=source_xy,
obstacle_cache=obstacle_cache
)
print_blocking_analysis(fresh_blockers, prefix=" ")
next_blocker = None
for b in fresh_blockers:
if b.net_id in routed_results:
canonical = get_canonical_net_id(b.net_id, diff_pair_by_net_id)
if canonical not in ripped_canonical_ids:
next_blocker = b
break
if next_blocker is None:
print(f" No additional rippable blockers from retry analysis")
break
next_canonical = get_canonical_net_id(next_blocker.net_id, diff_pair_by_net_id)
if next_canonical not in seen_canonical_ids:
seen_canonical_ids.add(next_canonical)
rippable_blockers.append(next_blocker)
for idx, b in enumerate(rippable_blockers):
if get_canonical_net_id(b.net_id, diff_pair_by_net_id) == next_canonical:
if idx != N - 1 and N - 1 < len(rippable_blockers):
rippable_blockers[idx], rippable_blockers[N-1] = rippable_blockers[N-1], rippable_blockers[idx]
break
if N > len(rippable_blockers):
break
blocker_canonicals = frozenset(
get_canonical_net_id(rippable_blockers[i].net_id, diff_pair_by_net_id)
for i in range(N)
)
if (current_canonical, blocker_canonicals) in rip_and_retry_history:
blocker_names = []
for i in range(N):
b = rippable_blockers[i]
if b.net_id in diff_pair_by_net_id:
blocker_names.append(diff_pair_by_net_id[b.net_id][0])
else:
blocker_names.append(b.net_name)
if len(blocker_names) == 1:
blockers_str = blocker_names[0]
else:
blockers_str = "{" + ", ".join(blocker_names) + "}"
print(f" Skipping N={N}: already tried ripping {blockers_str} for {pair_name}")
continue
rip_successful = True
new_ripped_this_level = []
if N == 1:
blocker = rippable_blockers[0]
if blocker.net_id in diff_pair_by_net_id:
ripped_pair_name_tmp, _ = diff_pair_by_net_id[blocker.net_id]
print(f" Ripping up diff pair {ripped_pair_name_tmp} (P and N) to retry...")
else:
print(f" Ripping up {blocker.net_name} to retry...")
else:
blocker = rippable_blockers[N-1]
if blocker.net_id in diff_pair_by_net_id:
ripped_pair_name_tmp, _ = diff_pair_by_net_id[blocker.net_id]
print(f" Extending to N={N}: ripping diff pair {ripped_pair_name_tmp}...")
else:
print(f" Extending to N={N}: ripping {blocker.net_name}...")
for i in range(len(ripped_items), N):
blocker = rippable_blockers[i]
if blocker.net_id not in routed_results:
continue
saved_result, ripped_ids, was_in_results = rip_up_net(
blocker.net_id, pcb_data, routed_net_ids, routed_net_paths,
routed_results, diff_pair_by_net_id, remaining_net_ids,
results, config, track_proximity_cache,
state.working_obstacles, state.net_obstacles_cache,
state.ripped_route_layer_costs, state.ripped_route_via_positions,
layer_map
)
if saved_result is None:
rip_successful = False
break
ripped_items.append((blocker.net_id, saved_result, ripped_ids, was_in_results))
new_ripped_this_level.append((blocker.net_id, saved_result, ripped_ids, was_in_results))
ripped_canonical_ids.add(get_canonical_net_id(blocker.net_id, diff_pair_by_net_id))
# Invalidate obstacle cache for ripped nets
for rid in ripped_ids:
invalidate_obstacle_cache(obstacle_cache, rid)
if was_in_results:
successful -= 1
if blocker.net_id in diff_pair_by_net_id:
ripped_pair_name_tmp, _ = diff_pair_by_net_id[blocker.net_id]
print(f" Ripped diff pair {ripped_pair_name_tmp}")
else:
print(f" Ripped {blocker.net_name}")
if not rip_successful:
for net_id, saved_result, ripped_ids, was_in_results in reversed(new_ripped_this_level):
restore_net(net_id, saved_result, ripped_ids, was_in_results,
pcb_data, routed_net_ids, routed_net_paths,
routed_results, diff_pair_by_net_id, remaining_net_ids,
results, config, track_proximity_cache, layer_map,
state.working_obstacles, state.net_obstacles_cache,
state.ripped_route_layer_costs, state.ripped_route_via_positions)
if was_in_results:
successful += 1
ripped_items.pop()
continue
# Rebuild obstacles and retry
retry_obstacles, unrouted_stubs = build_diff_pair_obstacles(
diff_pair_base_obstacles, pcb_data, config, routed_net_ids, remaining_net_ids,
all_unrouted_net_ids, pair.p_net_id, pair.n_net_id, gnd_net_id,
track_proximity_cache, layer_map, diff_pair_extra_clearance,
add_own_stubs_func=add_own_stubs_as_obstacles_for_diff_pair,
ripped_route_layer_costs=state.ripped_route_layer_costs,
ripped_route_via_positions=state.ripped_route_via_positions
)
retry_result = route_diff_pair_with_obstacles(pcb_data, pair, config, retry_obstacles, base_obstacles, unrouted_stubs)
if retry_result and not retry_result.get('failed') and not retry_result.get('probe_blocked'):
# Calculate actual routed length from segments (includes connectors and via barrels)
new_segments = retry_result.get('new_segments', [])
new_vias = retry_result.get('new_vias', [])
p_segments = [s for s in new_segments if s.net_id == pair.p_net_id]
n_segments = [s for s in new_segments if s.net_id == pair.n_net_id]
p_vias = [v for v in new_vias if v.net_id == pair.p_net_id]
n_vias = [v for v in new_vias if v.net_id == pair.n_net_id]
p_routed_length = calculate_route_length(p_segments, p_vias, pcb_data)
n_routed_length = calculate_route_length(n_segments, n_vias, pcb_data)
centerline_length = (p_routed_length + n_routed_length) / 2
# Use pre-calculated stub lengths from routing, accounting for polarity swap
p_src_stub = retry_result.get('p_src_stub_length', 0.0)
p_tgt_stub = retry_result.get('p_tgt_stub_length', 0.0)
n_src_stub = retry_result.get('n_src_stub_length', 0.0)
n_tgt_stub = retry_result.get('n_tgt_stub_length', 0.0)
polarity_fixed = retry_result.get('polarity_fixed', False)
if polarity_fixed:
p_stub_length = p_src_stub + n_tgt_stub
n_stub_length = n_src_stub + p_tgt_stub
else:
p_stub_length = p_src_stub + p_tgt_stub
n_stub_length = n_src_stub + n_tgt_stub
avg_stub_length = (p_stub_length + n_stub_length) / 2
retry_result['centerline_length'] = centerline_length
retry_result['stub_length'] = avg_stub_length
retry_result['p_stub_length'] = p_stub_length
retry_result['n_stub_length'] = n_stub_length
retry_result['p_routed_length'] = p_routed_length
retry_result['n_routed_length'] = n_routed_length
# For inter-pair matching: use max(P,N) total if intra-pair will equalize them
if config.diff_pair_intra_match:
p_total = p_routed_length + p_stub_length
n_total = n_routed_length + n_stub_length
retry_result['route_length'] = max(p_total, n_total)
else:
retry_result['route_length'] = centerline_length + avg_stub_length
print(f" RETRY SUCCESS (N={N}): {len(retry_result['new_segments'])} segments, {len(retry_result['new_vias'])} vias")
print(f" Centerline length: {centerline_length:.3f}mm, total: {retry_result['route_length']:.3f}mm")
results.append(retry_result)
successful += 1
total_iterations += retry_result['iterations']
apply_polarity_swap(pcb_data, retry_result, pad_swaps, pair_name, polarity_swapped_pairs)
add_route_to_pcb_data(pcb_data, retry_result, debug_lines=config.debug_lines)
if pair.p_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.p_net_id)
if pair.n_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.n_net_id)
routed_net_ids.append(pair.p_net_id)
routed_net_ids.append(pair.n_net_id)
track_proximity_cache[pair.p_net_id] = compute_track_proximity_for_net(pcb_data, pair.p_net_id, config, layer_map)
track_proximity_cache[pair.n_net_id] = compute_track_proximity_for_net(pcb_data, pair.n_net_id, config, layer_map)
if retry_result.get('p_path'):
routed_net_paths[pair.p_net_id] = retry_result['p_path']
if retry_result.get('n_path'):
routed_net_paths[pair.n_net_id] = retry_result['n_path']
routed_results[pair.p_net_id] = retry_result
routed_results[pair.n_net_id] = retry_result
diff_pair_by_net_id[pair.p_net_id] = (pair_name, pair)
diff_pair_by_net_id[pair.n_net_id] = (pair_name, pair)
# Allow re-queuing if this pair gets ripped again later
queued_net_ids.discard(pair.p_net_id)
queued_net_ids.discard(pair.n_net_id)
# Invalidate cache entries since we added segments
invalidate_obstacle_cache(obstacle_cache, pair.p_net_id)
invalidate_obstacle_cache(obstacle_cache, pair.n_net_id)
rip_and_retry_history.add((current_canonical, blocker_canonicals))
for net_id, saved_result, ripped_ids, was_in_results in ripped_items:
if was_in_results:
successful -= 1
if net_id in diff_pair_by_net_id:
ripped_pair_name_tmp, ripped_pair_tmp = diff_pair_by_net_id[net_id]
canonical_id = ripped_pair_tmp.p_net_id
if canonical_id not in queued_net_ids:
reroute_queue.append(('diff_pair', ripped_pair_name_tmp, ripped_pair_tmp))
queued_net_ids.add(canonical_id)
else:
if net_id not in queued_net_ids:
net = pcb_data.nets.get(net_id)
net_name = net.name if net else f"Net {net_id}"
reroute_queue.append(('single', net_name, net_id))
queued_net_ids.add(net_id)
ripped_up = True
retry_succeeded = True
ripup_success_pairs.add(pair_name)
break
else:
print(f" RETRY FAILED (N={N})")
if retry_result:
if retry_result.get('probe_blocked'):
last_retry_blocked_cells = retry_result.pop('blocked_cells', [])
else:
retry_fwd_cells = retry_result.pop('blocked_cells_forward', [])
retry_bwd_cells = retry_result.pop('blocked_cells_backward', [])
last_retry_blocked_cells = list(set(retry_fwd_cells + retry_bwd_cells))
del retry_fwd_cells, retry_bwd_cells # Free memory immediately
if last_retry_blocked_cells:
print(f" Retry had {len(last_retry_blocked_cells)} blocked cells")
else:
print(f" No blocked cells from retry to analyze")
# If all N levels failed, restore all ripped nets
if not retry_succeeded and ripped_items:
print(f" {RED}All rip-up attempts failed: Restoring {len(ripped_items)} net(s){RESET}")
for net_id, saved_result, ripped_ids, was_in_results in reversed(ripped_items):
restore_net(net_id, saved_result, ripped_ids, was_in_results,
pcb_data, routed_net_ids, routed_net_paths,
routed_results, diff_pair_by_net_id, remaining_net_ids,
results, config, track_proximity_cache, layer_map,
state.working_obstacles, state.net_obstacles_cache,
state.ripped_route_layer_costs, state.ripped_route_via_positions)
if was_in_results:
successful += 1
# Fallback layer swap for setback failures
if not ripped_up and is_setback_failure and enable_layer_switch:
print(f" Trying fallback layer swap for setback failure...")
swap_success, swap_result, _, _ = try_fallback_layer_swap(
pcb_data, pair, pair_name, config,
fwd_cells, bwd_cells,
diff_pair_base_obstacles, base_obstacles,
routed_net_ids, remaining_net_ids,
all_unrouted_net_ids, gnd_net_id,
track_proximity_cache, diff_pair_extra_clearance,
all_swap_vias, all_segment_modifications,
None, None,
routed_net_paths, routed_results, diff_pair_by_net_id, layer_map,
target_swaps, results=results, obstacle_cache=obstacle_cache)
if swap_success and swap_result:
# Calculate actual routed length from segments (includes connectors and via barrels)
new_segments = swap_result.get('new_segments', [])
new_vias = swap_result.get('new_vias', [])
p_segments = [s for s in new_segments if s.net_id == pair.p_net_id]
n_segments = [s for s in new_segments if s.net_id == pair.n_net_id]
p_vias = [v for v in new_vias if v.net_id == pair.p_net_id]
n_vias = [v for v in new_vias if v.net_id == pair.n_net_id]
p_routed_length = calculate_route_length(p_segments, p_vias, pcb_data)
n_routed_length = calculate_route_length(n_segments, n_vias, pcb_data)
centerline_length = (p_routed_length + n_routed_length) / 2
# Use pre-calculated stub lengths from routing, accounting for polarity swap
p_src_stub = swap_result.get('p_src_stub_length', 0.0)
p_tgt_stub = swap_result.get('p_tgt_stub_length', 0.0)
n_src_stub = swap_result.get('n_src_stub_length', 0.0)
n_tgt_stub = swap_result.get('n_tgt_stub_length', 0.0)
polarity_fixed = swap_result.get('polarity_fixed', False)
if polarity_fixed:
p_stub_length = p_src_stub + n_tgt_stub
n_stub_length = n_src_stub + p_tgt_stub
else:
p_stub_length = p_src_stub + p_tgt_stub
n_stub_length = n_src_stub + n_tgt_stub
avg_stub_length = (p_stub_length + n_stub_length) / 2
swap_result['centerline_length'] = centerline_length
swap_result['stub_length'] = avg_stub_length
swap_result['p_stub_length'] = p_stub_length
swap_result['n_stub_length'] = n_stub_length
swap_result['p_routed_length'] = p_routed_length
swap_result['n_routed_length'] = n_routed_length
# For inter-pair matching: use max(P,N) total if intra-pair will equalize them
if config.diff_pair_intra_match:
p_total = p_routed_length + p_stub_length
n_total = n_routed_length + n_stub_length
swap_result['route_length'] = max(p_total, n_total)
else:
swap_result['route_length'] = centerline_length + avg_stub_length
print(f" {GREEN}FALLBACK LAYER SWAP SUCCESS{RESET}")
print(f" Centerline length: {centerline_length:.3f}mm, total: {swap_result['route_length']:.3f}mm")
results.append(swap_result)
successful += 1
total_iterations += swap_result['iterations']
apply_polarity_swap(pcb_data, swap_result, pad_swaps, pair_name, polarity_swapped_pairs)
add_route_to_pcb_data(pcb_data, swap_result, debug_lines=config.debug_lines)
if pair.p_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.p_net_id)
if pair.n_net_id in remaining_net_ids:
remaining_net_ids.remove(pair.n_net_id)
routed_net_ids.append(pair.p_net_id)
routed_net_ids.append(pair.n_net_id)
track_proximity_cache[pair.p_net_id] = compute_track_proximity_for_net(pcb_data, pair.p_net_id, config, layer_map)
track_proximity_cache[pair.n_net_id] = compute_track_proximity_for_net(pcb_data, pair.n_net_id, config, layer_map)
if swap_result.get('p_path'):
routed_net_paths[pair.p_net_id] = swap_result['p_path']
if swap_result.get('n_path'):
routed_net_paths[pair.n_net_id] = swap_result['n_path']
routed_results[pair.p_net_id] = swap_result
routed_results[pair.n_net_id] = swap_result
diff_pair_by_net_id[pair.p_net_id] = (pair_name, pair)
diff_pair_by_net_id[pair.n_net_id] = (pair_name, pair)
# Allow re-queuing if this pair gets ripped again later
queued_net_ids.discard(pair.p_net_id)
queued_net_ids.discard(pair.n_net_id)
# Invalidate cache entries since we added segments
invalidate_obstacle_cache(obstacle_cache, pair.p_net_id)
invalidate_obstacle_cache(obstacle_cache, pair.n_net_id)
ripped_up = True
if not ripped_up:
print(f" {RED}ROUTE FAILED - no rippable blockers found{RESET}")
failed += 1
return successful, failed, total_time, total_iterations, route_index