forked from RTimothyEdwards/qrouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
2980 lines (2640 loc) · 87.8 KB
/
maze.c
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------*/
/* maze.c -- general purpose maze router routines. */
/* */
/* This file contains the main cost evaluation routine, */
/* the route segment generator, and a routine to search */
/* the database for all parts of a routed network. */
/*--------------------------------------------------------------*/
/* Written by Tim Edwards, June 2011, based on code by Steve */
/* Beccue */
/*--------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#define MAZE
#include "qrouter.h"
#include "qconfig.h"
#include "point.h"
#include "node.h"
#include "maze.h"
#include "lef.h"
extern int TotalRoutes;
/*--------------------------------------------------------------*/
/* find_unrouted_node() -- */
/* */
/* On a power bus, the nodes are routed individually, using the */
/* entire power bus as the destination. So to find out if a */
/* node is already routed or not, the only way is to check the */
/* routes recorded for the net and determine if any net */
/* endpoint is on a node. */
/* */
/* Return the first node found that is not connected to any */
/* route endpoint. If all nodes are routed, then return NULL */
/*--------------------------------------------------------------*/
NODE find_unrouted_node(NET net)
{
int i, numroutes;
u_char *routednode;
NODE node;
ROUTE rt;
SEG seg1, seg2;
DPOINT tap;
// Quick check: If the number of routes == number of nodes,
// then return NULL and we're done.
numroutes = 0;
for (rt = net->routes; rt; rt = rt->next) numroutes++;
if (numroutes == net->numnodes) return NULL;
routednode = (u_char *)malloc(net->numnodes * sizeof(u_char));
for (i = 0; i < net->numnodes; i++) routednode[i] = 0;
// Otherwise, we don't know which nodes have been routed,
// so check each one individually.
for (rt = net->routes; rt; rt = rt->next) {
seg1 = rt->segments;
if (seg1 == NULL) continue;
seg2 = seg1;
while (seg2->next) seg2 = seg2->next;
for (node = net->netnodes; node; node = node->next) {
if (routednode[node->nodenum] == 1) continue;
for (tap = node->taps; tap; tap = tap->next) {
if (seg1->x1 == tap->gridx && seg1->y1 == tap->gridy
&& seg1->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg1->x2 == tap->gridx && seg1->y2 == tap->gridy
&& seg1->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg2->x1 == tap->gridx && seg2->y1 == tap->gridy
&& seg2->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg2->x2 == tap->gridx && seg2->y2 == tap->gridy
&& seg2->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
}
if (tap == NULL) {
for (tap = node->extend; tap; tap = tap->next) {
seg1 = rt->segments;
seg2 = seg1;
while (seg2->next) seg2 = seg2->next;
if (seg1->x1 == tap->gridx && seg1->y1 == tap->gridy
&& seg1->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg1->x2 == tap->gridx && seg1->y2 == tap->gridy
&& seg1->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg2->x1 == tap->gridx && seg2->y1 == tap->gridy
&& seg2->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
else if (seg2->x2 == tap->gridx && seg2->y2 == tap->gridy
&& seg2->layer == tap->layer) {
routednode[node->nodenum] = 1;
break;
}
}
}
}
}
for (node = net->netnodes; node; node = node->next) {
if (routednode[node->nodenum] == 0) {
free(routednode);
return node;
}
}
free(routednode);
return NULL; /* Statement should never be reached */
}
/*--------------------------------------------------------------*/
/* set_powerbus_to_net() */
/* If we have a power or ground net, go through the entire Obs */
/* array and mark all points matching the net as TARGET in Obs2 */
/* */
/* We do this after the call to PR_SOURCE, before the calls */
/* to set PR_TARGET. */
/* */
/* If any grid position was marked as TARGET, return 1, else */
/* return 0 (meaning the net has been routed already). */
/*--------------------------------------------------------------*/
int set_powerbus_to_net(int netnum)
{
int x, y, lay, rval;
PROUTE *Pr;
rval = 0;
if ((netnum == VDD_NET) || (netnum == GND_NET) || (netnum == ANTENNA_NET)) {
for (lay = 0; lay < Num_layers; lay++)
for (x = 0; x < NumChannelsX; x++)
for (y = 0; y < NumChannelsY; y++)
if ((OBSVAL(x, y, lay) & NETNUM_MASK) == netnum) {
Pr = &OBS2VAL(x, y, lay);
// Skip locations that have been purposefully disabled
if (!(Pr->flags & PR_COST) && (Pr->prdata.net == MAXNETNUM))
continue;
else if (!(Pr->flags & PR_SOURCE)) {
Pr->flags |= (PR_TARGET | PR_COST);
Pr->prdata.cost = MAXRT;
rval = 1;
}
}
}
return rval;
}
/*--------------------------------------------------------------*/
/* clear_non_source_targets -- */
/* */
/* Look at all target nodes of a net. For any that are not */
/* marked as SOURCE, but have terminal points marked as */
/* PROCESSED, remove the PROCESSED flag and put the position */
/* back on the stack for visiting on the next round. */
/*--------------------------------------------------------------*/
void clear_non_source_targets(NET net, POINT *pushlist)
{
NODE node;
DPOINT ntap;
PROUTE *Pr;
POINT gpoint;
int lay, x, y;
for (node = net->netnodes; node; node = node->next) {
for (ntap = node->taps; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_TARGET) {
if (Pr->flags & PR_PROCESSED) {
Pr->flags &= ~PR_PROCESSED;
if (~(Pr->flags & PR_ON_STACK)) {
Pr->flags |= PR_ON_STACK;
gpoint = allocPOINT();
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = *pushlist;
*pushlist = gpoint;
}
}
}
}
if (ntap == NULL) {
// Try extended tap areas
for (ntap = node->extend; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_TARGET) {
if (Pr->flags & PR_PROCESSED) {
Pr->flags &= ~PR_PROCESSED;
if (~(Pr->flags & PR_ON_STACK)) {
Pr->flags |= PR_ON_STACK;
gpoint = allocPOINT();
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = pushlist[1];
pushlist[1] = gpoint;
}
}
}
}
}
}
}
/*--------------------------------------------------------------*/
/* clear_target_node -- */
/* */
/* Remove PR_TARGET flags from all points belonging to a node */
/*--------------------------------------------------------------*/
void clear_target_node(NODE node)
{
int x, y, lay;
NODEINFO lnode;
DPOINT ntap;
PROUTE *Pr;
/* Process tap points of the node */
for (ntap = node->taps; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
if ((lay < Pinlayers) && (((lnode = NODEIPTR(x, y, lay)) == NULL)
|| (lnode->nodesav == NULL)))
continue;
Pr = &OBS2VAL(x, y, lay);
Pr->flags = 0;
Pr->prdata.net = node->netnum;
}
for (ntap = node->extend; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
if (lay < Pinlayers) {
lnode = NODEIPTR(x, y, lay);
if (lnode == NULL) continue;
if (lnode->nodesav != node) continue;
}
Pr = &OBS2VAL(x, y, lay);
Pr->flags = 0;
Pr->prdata.net = node->netnum;
}
}
/*--------------------------------------------------------------*/
/* count_targets() --- */
/* */
/* Count the number of nodes of a net that are still marked as */
/* TARGET. */
/*--------------------------------------------------------------*/
int
count_targets(NET net)
{
NODE node;
PROUTE *Pr;
DPOINT ntap;
int lay, x, y;
int count = 0;
for (node = net->netnodes; node; node = node->next) {
for (ntap = node->taps; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_TARGET) {
count++;
break;
}
}
if (ntap == NULL) {
// Try extended tap areas
for (ntap = node->extend; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_TARGET) {
count++;
break;
}
}
}
}
return count;
}
/*--------------------------------------------------------------*/
/* set_node_to_net() --- */
/* */
/* Change the Obs2[][] flag values to "newflags" for all tap */
/* positions of route terminal "node". */
/* */
/* Return value is 1 if at least one terminal of the node */
/* is already marked as PR_SOURCE, indicating that the node */
/* has already been routed. Otherwise, the return value is */
/* zero if no error occured, and -1 if any point was found to */
/* be unoccupied by any net, which should not happen. */
/* */
/* If "bbox" is non-null, record the grid extents of the node */
/* in the x1, x2, y1, y2 values */
/* */
/* If "stage" is 1 (rip-up and reroute), then don't let an */
/* existing route prevent us from adding terminals. However, */
/* the area will be first checked for any part of the terminal */
/* that is routable, only resorting to overwriting colliding */
/* nets if there are no other available taps. Defcon stage 3 */
/* indicates desperation due to a complete lack of routable */
/* taps. This happens if, for example, a port is offset from */
/* the routing grid and tightly boxed in by obstructions. In */
/* such case, we allow routing on an obstruction, but flag the */
/* point. In the output stage, the stub route information will */
/* be used to reposition the contact on the port and away from */
/* the obstruction. */
/* */
/* If we completely fail to find a tap point under any */
/* condition, then return -2. This is a fatal error; there */
/* will be no way to route the net. */
/*--------------------------------------------------------------*/
int set_node_to_net(NODE node, int newflags, POINT *pushlist,
SEG bbox, u_char stage)
{
int x, y, lay, rank, base, obsnet = 0;
int result = 0;
u_char found_one = FALSE;
NODEINFO lnode;
POINT gpoint;
DPOINT ntap;
PROUTE *Pr;
/* If called from set_routes_to_net, the node has no taps, and the */
/* net is a power bus, just return. */
if ((node->taps == NULL) && (node->extend == NULL)) {
if ((node->netnum == VDD_NET) || (node->netnum == GND_NET) ||
(node->netnum == ANTENNA_NET))
return result;
}
/* Process tap points of the node */
for (ntap = node->taps; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if ((Pr->flags & (newflags | PR_COST)) == PR_COST) {
Fprintf(stderr, "Error: Tap position %d, %d layer %d not "
"marked as source!\n", x, y, lay);
return -1; // This should not happen.
}
if (Pr->flags & PR_SOURCE) {
if (!found_one)
return 1; // Node is already connected!
else
continue; // May be duplicate tap position
}
else if ((Pr->flags & PR_TARGET) && (newflags & PR_TARGET)) {
if (!found_one)
return 1;
else
continue;
}
else if (((Pr->prdata.net == node->netnum) || (stage == (u_char)2))
&& !(Pr->flags & newflags)) {
// If we got here, we're on the rip-up stage, and there
// is an existing route completely blocking the terminal.
// So we will route over it and flag it as a collision.
if (Pr->prdata.net != node->netnum) {
if ((Pr->prdata.net == (NO_NET | OBSTRUCT_MASK)) ||
(Pr->prdata.net == NO_NET))
continue;
else
Pr->flags |= PR_CONFLICT;
}
// Do the source and dest nodes need to be marked routable?
Pr->flags |= (newflags == PR_SOURCE) ? newflags : (newflags | PR_COST);
Pr->prdata.cost = (newflags == PR_SOURCE) ? 0 : MAXRT;
// Rank the position according to how difficult it is to route to:
//
// 0: no restrictions
// 1: requires a stub route
// 2: inside the halo
// 3: requires an offset
// 4: requires an offset and a stub route
rank = 0;
if (lay < Pinlayers) {
if (lnode = NODEIPTR(x, y, lay)) {
if (lnode->flags & NI_OFFSET_MASK) rank = 2;
if (lnode->flags & NI_STUB_MASK) rank++;
}
}
// push this point on the stack to process
if (pushlist != NULL) {
if (~(Pr->flags & PR_ON_STACK)) {
Pr->flags |= PR_ON_STACK;
gpoint = allocPOINT();
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = pushlist[rank];
pushlist[rank] = gpoint;
}
}
found_one = TRUE;
// record extents
if (bbox) {
if (x < bbox->x1) bbox->x1 = x;
if (x > bbox->x2) bbox->x2 = x;
if (y < bbox->y1) bbox->y1 = y;
if (y > bbox->y2) bbox->y2 = y;
}
}
else if ((Pr->prdata.net < MAXNETNUM) && (Pr->prdata.net > 0)) obsnet++;
}
// Do the same for point in the halo around the tap, but only if
// they have been attached to the net during a past routing run.
for (ntap = node->extend; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
// Don't process extended areas if they coincide with other nodes,
// or those that are out-of-bounds
base = 0;
if (lay < Pinlayers) {
lnode = NODEIPTR(x, y, lay);
if (lnode == NULL) continue;
if (lnode->nodesav != node) continue;
// Otherwise, rank according to ease of reaching the tap.
if (lnode->flags & NI_OFFSET_MASK) base = 2;
if (lnode->flags & NI_STUB_MASK) base++;
}
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_SOURCE) {
if (!found_one)
return 1; // Node is already connected!
else
continue; // May be duplicate tap record
}
else if ((Pr->flags & PR_TARGET) && (newflags & PR_TARGET)) {
if (!found_one)
return 1;
else
continue;
}
else if ( !(Pr->flags & newflags) &&
((Pr->prdata.net == node->netnum) ||
(stage == (u_char)2 && Pr->prdata.net < MAXNETNUM) ||
(stage == (u_char)3))) {
if (Pr->prdata.net != node->netnum) Pr->flags |= PR_CONFLICT;
Pr->flags |= (newflags == PR_SOURCE) ? newflags : (newflags | PR_COST);
Pr->prdata.cost = (newflags == PR_SOURCE) ? 0 : MAXRT;
// push this point on the stack to process
if (pushlist != NULL) {
if (~(Pr->flags & PR_ON_STACK)) {
Pr->flags |= PR_ON_STACK;
gpoint = allocPOINT();
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
rank = (found_one == TRUE) ? 2 + base : base;
gpoint->next = pushlist[rank];
pushlist[rank] = gpoint;
}
}
found_one = TRUE;
// record extents
if (bbox) {
if (x < bbox->x1) bbox->x1 = x;
if (x > bbox->x2) bbox->x2 = x;
if (y < bbox->y1) bbox->y1 = y;
if (y > bbox->y2) bbox->y2 = y;
}
}
else if ((Pr->prdata.net < MAXNETNUM) && (Pr->prdata.net > 0)) obsnet++;
}
// In the case that no valid tap points were found, if we're on the
// rip-up and reroute section, try again, ignoring existing routes that
// are in the way of the tap point. If that fails, then we will
// route over obstructions and shift the contact when committing the
// route solution. And if that fails, we're basically hosed.
//
// Make sure to check for the case that the tap point is simply not
// reachable from any grid point, in the first stage, so we don't
// wait until the rip-up and reroute stage to route them.
if ((result == 0) && (!found_one)) {
if (stage == (u_char)1)
return set_node_to_net(node, newflags, pushlist, bbox, (u_char)2);
else if (stage == (u_char)2)
return set_node_to_net(node, newflags, pushlist, bbox, (u_char)3);
else if ((stage == (u_char)0) && (obsnet == 0))
return set_node_to_net(node, newflags, pushlist, bbox, (u_char)3);
else
return -2;
}
return result;
}
/*--------------------------------------------------------------*/
/* Set all taps of node "node" to MAXNETNUM, so that it will */
/* not be routed to. */
/*--------------------------------------------------------------*/
int disable_node_nets(NODE node)
{
int x, y, lay;
int result = 0;
DPOINT ntap;
PROUTE *Pr;
/* Process tap points of the node */
for (ntap = node->taps; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_SOURCE || Pr->flags & PR_TARGET || Pr->flags & PR_COST) {
result = 1;
}
else if (Pr->prdata.net == node->netnum) {
Pr->prdata.net = MAXNETNUM;
}
}
// Do the same for point in the halo around the tap, but only if
// they have been attached to the net during a past routing run.
for (ntap = node->extend; ntap; ntap = ntap->next) {
lay = ntap->layer;
x = ntap->gridx;
y = ntap->gridy;
Pr = &OBS2VAL(x, y, lay);
if (Pr->flags & PR_SOURCE || Pr->flags & PR_TARGET || Pr->flags & PR_COST) {
result = 1;
}
else if (Pr->prdata.net == node->netnum) {
Pr->prdata.net = MAXNETNUM;
}
}
return result;
}
/*--------------------------------------------------------------*/
/* That which is already routed (routes should be attached to */
/* source nodes) is routable by definition. . . */
/*--------------------------------------------------------------*/
int set_route_to_net(NET net, ROUTE rt, int newflags, POINT *pushlist,
SEG bbox, u_char stage)
{
int x, y, lay;
int result = 0;
NODEINFO lnode;
POINT gpoint;
SEG seg;
NODE n2;
PROUTE *Pr;
if (rt && rt->segments) {
for (seg = rt->segments; seg; seg = seg->next) {
lay = seg->layer;
x = seg->x1;
y = seg->y1;
while (1) {
Pr = &OBS2VAL(x, y, lay);
Pr->flags = (newflags == PR_SOURCE) ? newflags : (newflags | PR_COST);
// Conflicts should not happen (check for this?)
// if (Pr->prdata.net != node->netnum) Pr->flags |= PR_CONFLICT;
Pr->prdata.cost = (newflags == PR_SOURCE) ? 0 : MAXRT;
// push this point on the stack to process
if (pushlist != NULL) {
if (~(Pr->flags & PR_ON_STACK)) {
Pr->flags |= PR_ON_STACK;
gpoint = allocPOINT();
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = *pushlist;
*pushlist = gpoint;
}
}
// record extents
if (bbox) {
if (x < bbox->x1) bbox->x1 = x;
if (x > bbox->x2) bbox->x2 = x;
if (y < bbox->y1) bbox->y1 = y;
if (y > bbox->y2) bbox->y2 = y;
}
// If we found another node connected to the route,
// then process it, too.
lnode = (lay >= Pinlayers) ? NULL : NODEIPTR(x, y, lay);
n2 = (lnode) ? lnode->nodesav : NULL;
if ((n2 != (NODE)NULL) && (n2 != net->netnodes)) {
if (newflags == PR_SOURCE) clear_target_node(n2);
result = set_node_to_net(n2, newflags, pushlist, bbox, stage);
// On error, continue processing
}
// Process top part of via
if (seg->segtype & ST_VIA) {
if (lay != seg->layer) break;
lay++;
continue;
}
// Move to next grid position in segment
if (x == seg->x2 && y == seg->y2) break;
if (seg->x2 > seg->x1) x++;
else if (seg->x2 < seg->x1) x--;
if (seg->y2 > seg->y1) y++;
else if (seg->y2 < seg->y1) y--;
}
}
}
return result;
}
/*--------------------------------------------------------------*/
/* Process a route and all routes that connect to it. Works */
/* like the routine above, but searches the route endpoints for */
/* connecting nodes and routes, and then recursively calls */
/* itself on the connecting routes, and other routes that */
/* connect do the nodes. */
/*--------------------------------------------------------------*/
int set_route_to_net_recursive(NET net, ROUTE rt, int newflags,
POINT *pushlist, SEG bbox, u_char stage)
{
ROUTE route;
int result;
/* If route has been marked, return */
if (rt->flags & RT_VISITED) return 0;
rt->flags |= RT_VISITED;
/* First mark this route */
result = set_route_to_net(net, rt, newflags, pushlist, bbox, stage);
if (result < 0) return result;
/* Recursively mark the routes connected to the nodes of */
/* the endpoints or connected directly to the endpoints. */
if (rt->flags & RT_START_NODE) {
for (route = net->routes; route; route = route->next) {
if (!(route->flags & RT_START_NODE) && (route->start.route == rt)) {
result = set_route_to_net_recursive(net, route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
if (!(route->flags & RT_END_NODE) && (route->end.route == rt)) {
result = set_route_to_net_recursive(net, route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
}
}
else if (rt->start.route) {
result = set_route_to_net_recursive(net, rt->start.route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
else
Fprintf(stderr, "Error: Route start information not recorded, cannot walk.\n");
if (rt->flags & RT_END_NODE) {
for (route = net->routes; route; route = route->next) {
if (!(route->flags & RT_START_NODE) && (route->start.route == rt)) {
result = set_route_to_net_recursive(net, route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
if (!(route->flags & RT_END_NODE) && (route->end.route == rt)) {
result = set_route_to_net_recursive(net, route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
}
}
else if (rt->end.route) {
result = set_route_to_net_recursive(net, rt->end.route, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
else
Fprintf(stderr, "Error: Route end information not recorded, cannot walk.\n");
return result;
}
/*--------------------------------------------------------------*/
/* Process all routes of a net that are connected in some way */
/* node "node", and set their routed positions to the value of */
/* "newflags" (PR_SOURCE or PR_DEST) in Obs2[]. */
/*--------------------------------------------------------------*/
int set_routes_to_net(NODE node, NET net, int newflags, POINT *pushlist,
SEG bbox, u_char stage)
{
ROUTE rt;
int result = 0;
/* Clear marks on all routes */
for (rt = net->routes; rt; rt = rt->next) rt->flags &= ~RT_VISITED;
/* Find any route that has node as an endpoint */
for (rt = net->routes; rt; rt = rt->next) {
if ((rt->flags & RT_START_NODE) && (rt->start.node == node))
result = set_route_to_net_recursive(net, rt, newflags,
pushlist, bbox, stage);
else if ((rt->flags & RT_END_NODE) && (rt->end.node == node))
result = set_route_to_net_recursive(net, rt, newflags,
pushlist, bbox, stage);
if (result < 0) return result;
}
return result;
}
/*--------------------------------------------------------------*/
/* Used by find_colliding() (see below). Save net "netnum" */
/* to the list of colliding nets if it is not already in the */
/* list. Return 1 if the list got longer, 0 otherwise. */
/* Find the route of the net that includes the point of */
/* collision, and mark it for rip-up. */
/*--------------------------------------------------------------*/
static int
addcollidingnet(NETLIST *nlptr, int netnum, int x, int y, int lay)
{
ROUTE rt;
NETLIST cnl;
NET fnet;
SEG seg;
int i;
int sx, sy;
u_char found;
for (cnl = *nlptr; cnl; cnl = cnl->next)
if (cnl->net->netnum == netnum)
return 0;
for (i = 0; i < Numnets; i++) {
fnet = Nlnets[i];
if (fnet->netnum == netnum) {
cnl = (NETLIST)malloc(sizeof(struct netlist_));
cnl->net = fnet;
cnl->next = *nlptr;
*nlptr = cnl;
/* If there are no routes then we're done. */
if (fnet->routes == NULL) return 0;
/* If there is only one route then there is no need */
/* to search or shuffle. */
if (fnet->routes->next == NULL) {
fnet->routes->flags |= RT_RIP;
return 1;
}
for (rt = fnet->routes; rt; rt = rt->next) {
found = 0;
for (seg = rt->segments; seg; seg = seg->next) {
if ((seg->layer == lay) || ((seg->segtype & ST_VIA) &&
((seg->layer + 1) == lay))) {
sx = seg->x1;
sy = seg->y1;
while (1) {
if ((sx == x) && (sy == y)) {
found = 1;
break;
}
if ((sx == seg->x2) && (sy == seg->y2)) break;
if (sx < seg->x2) sx++;
else if (sx > seg->x2) sx--;
if (sy < seg->y2) sy++;
else if (sy > seg->y2) sy--;
}
if (found) break;
}
}
if (found) rt->flags |= RT_RIP;
}
return 1;
}
}
return 0;
}
/*--------------------------------------------------------------*/
/* Find nets that are colliding with the given net "net", and */
/* create and return a list of them. */
/*--------------------------------------------------------------*/
NETLIST find_colliding(NET net, int *ripnum)
{
NETLIST nl = (NETLIST)NULL, cnl;
ROUTE rt;
SEG seg;
int lay, x, y, orignet, rnum;
/* Scan the routed points for recorded collisions. */
rnum = 0;
for (rt = net->routes; rt; rt = rt->next) {
if (rt->segments) {
for (seg = rt->segments; seg; seg = seg->next) {
lay = seg->layer;
x = seg->x1;
y = seg->y1;
// The following skips over vias, which is okay, since
// those positions are covered by segments on both layers
// or are terminal positions that by definition can't
// belong to a different net.
while (1) {
orignet = OBSVAL(x, y, lay) & ROUTED_NET_MASK;
if ((orignet & DRC_BLOCKAGE) == DRC_BLOCKAGE) {
/* If original position was a DRC-related blockage, */
/* find out which net or nets would be in conflict. */
if (needblock[lay] & (ROUTEBLOCKX | VIABLOCKX)) {
if (x < NumChannelsX - 1) {
orignet = OBSVAL(x + 1, y, lay) & ROUTED_NET_MASK;
if (!(orignet & NO_NET)) {
orignet &= NETNUM_MASK;
if ((orignet != 0) && (orignet != net->netnum))
rnum += addcollidingnet(&nl, orignet, x, y, lay);
}
}
if (x > 0) {
orignet = OBSVAL(x - 1, y, lay) & ROUTED_NET_MASK;
if (!(orignet & NO_NET)) {
orignet &= NETNUM_MASK;
if ((orignet != 0) && (orignet != net->netnum))
rnum += addcollidingnet(&nl, orignet, x, y, lay);
}
}
}
if (needblock[lay] & (ROUTEBLOCKY | VIABLOCKY)) {
if (y < NumChannelsY - 1) {
orignet = OBSVAL(x, y + 1, lay) & ROUTED_NET_MASK;
if (!(orignet & NO_NET)) {
orignet &= NETNUM_MASK;
if ((orignet != 0) && (orignet != net->netnum))
rnum += addcollidingnet(&nl, orignet, x, y, lay);
}
}
if (y > 0) {
orignet = OBSVAL(x, y - 1, lay) & ROUTED_NET_MASK;
if (!(orignet & NO_NET)) {
orignet &= NETNUM_MASK;
if ((orignet != 0) && (orignet != net->netnum))
rnum += addcollidingnet(&nl, orignet, x, y, lay);
}
}
}
}
else {
orignet &= NETNUM_MASK;
if ((orignet != net->netnum) && (orignet != 0))
rnum += addcollidingnet(&nl, orignet, x, y, lay);
}
if ((x == seg->x2) && (y == seg->y2)) break;
if (x < seg->x2) x++;
else if (x > seg->x2) x--;
if (y < seg->y2) y++;
else if (y > seg->y2) y--;
}
}
}
}
/* Diagnostic */
if ((nl != NULL) && (Verbose > 0)) {
Fprintf(stdout, "Best route of %s collides with net%s: ",
net->netname, (rnum > 1) ? "s" : "");
for (cnl = nl; cnl; cnl = cnl->next) {
Fprintf(stdout, "%s ", cnl->net->netname);
}
Fprintf(stdout, "\n");
}
if (ripnum) *ripnum = rnum;
return nl;
}
/*--------------------------------------------------------------*/
/* ripup_dependent --- */
/* */
/* If a set of routes is being ripped out of a net (marked by */
/* RT_RIP in the flags), check if any routes below them have */
/* endpoints landing on a ripped-out net. If so, flag those */
/* nets for being ripped out as well. Repeat recursively. */
/*--------------------------------------------------------------*/
void ripup_dependent(NET net)
{
ROUTE rt, route;
u_char rerun = TRUE;
while (rerun) {
rerun = FALSE;
for (rt = net->routes; rt; rt = rt->next) {
if (rt->flags & RT_RIP) continue;
if (!(rt->flags & RT_START_NODE)) {
route = rt->start.route;
// route should not be NULL here. . .
if (route && (route->flags & RT_RIP)) {
rt->flags |= RT_RIP;
rerun = TRUE;
}
}
if (!(rt->flags & RT_END_NODE)) {
route = rt->end.route;
// route should not be NULL here. . .
if (route && (route->flags & RT_RIP)) {
rt->flags |= RT_RIP;
rerun = TRUE;
}
}
}
}
}