-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoroute.c
5324 lines (5075 loc) · 152 KB
/
autoroute.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
/* $Id$ */
/*
* COPYRIGHT
*
* PCB, interactive printed circuit board design
* Copyright (C) 1994,1995,1996 Thomas Nau
* Copyright (C) 1998,1999,2000,2001 harry eaton
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Contact addresses for paper mail and Email:
* harry eaton, 6697 Buttonhole Ct, Columbia, MD 21044 USA
* haceaton@aplcomm.jhuapl.edu
*
*/
/* this file, autoroute.c, was written and is
* Copyright (c) 2001 C. Scott Ananian
* Copyright (c) 2006 harry eaton
* Copyright (c) 2009 harry eaton
*/
/* functions used to autoroute nets.
*/
/*
*-------------------------------------------------------------------
* This file implements a rectangle-expansion router, based on
* "A Method for Gridless Routing of Printed Circuit Boards" by
* A. C. Finch, K. J. Mackenzie, G. J. Balsdon, and G. Symonds in the
* 1985 Proceedings of the 22nd ACM/IEEE Design Automation Conference.
* This reference is available from the ACM Digital Library at
* http://www.acm.org/dl for those with institutional or personal
* access to it. It's also available from your local engineering
* library.
*
* The code is much closer to what is described in the paper now,
* in that expansion areas can grow from corners and in all directions
* at once. Previously, these were emulated with discrete boxes moving
* in the cardinal directions. With the new method, there are fewer but
* larger expansion boxes that one might do a better job of routing in.
*--------------------------------------------------------------------
*/
#define NET_HEAP 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "global.h"
#include <assert.h>
#include <setjmp.h>
#include "data.h"
#include "macro.h"
#include "autoroute.h"
#include "box.h"
#include "create.h"
#include "draw.h"
#include "error.h"
#include "find.h"
#include "heap.h"
#include "rtree.h"
#include "misc.h"
#include "mtspace.h"
#include "mymem.h"
#include "polygon.h"
#include "rats.h"
#include "remove.h"
#include "thermal.h"
#include "undo.h"
#include "vector.h"
#ifdef HAVE_LIBDMALLOC
#include <dmalloc.h>
#endif
RCSID ("$Id$");
/* #defines to enable some debugging output */
/*
#define ROUTE_VERBOSE
*/
/*
#define ROUTE_DEBUG
//#define DEBUG_SHOW_ROUTE_BOXES
#define DEBUG_SHOW_EXPANSION_BOXES
//#define DEBUG_SHOW_EDGES
//#define DEBUG_SHOW_VIA_BOXES
#define DEBUG_SHOW_TARGETS
#define DEBUG_SHOW_SOURCES
//#define DEBUG_SHOW_ZIGZAG
*/
static direction_t
directionIncrement(direction_t dir)
{
switch(dir)
{
case NORTH:
dir = EAST;
break;
case EAST:
dir = SOUTH;
break;
case SOUTH:
dir = WEST;
break;
case WEST:
dir = NE;
break;
case NE:
dir = SE;
break;
case SE:
dir = SW;
break;
case SW:
dir = NW;
break;
case NW:
dir = ALL;
break;
case ALL:
dir = NORTH;
break;
}
return dir;
}
static hidGC ar_gc = 0;
#define EXPENSIVE 3e28
/* round up "half" thicknesses */
#define HALF_THICK(x) (((x)+1)/2)
/* a styles maximum bloat is its keepaway plus the larger of its via radius
* or line half-thickness. */
#define BLOAT(style)\
((style)->Keepaway + HALF_THICK((style)->Thick))
/* conflict penalty is less for traces laid down during previous pass than
* it is for traces already laid down in this pass. */
#define CONFLICT_LEVEL(rb)\
(((rb)->flags.is_odd==AutoRouteParameters.is_odd) ?\
HI_CONFLICT : LO_CONFLICT )
#define CONFLICT_PENALTY(rb)\
((CONFLICT_LEVEL(rb)==HI_CONFLICT ? \
AutoRouteParameters.ConflictPenalty : \
CONFLICT_LEVEL(rb)==LO_CONFLICT ? \
AutoRouteParameters.LastConflictPenalty : 1) * (rb)->pass)
#if !defined(ABS)
#define ABS(x) (((x)<0)?-(x):(x))
#endif
#define _NORTH 1
#define _EAST 2
#define _SOUTH 4
#define _WEST 8
#define LIST_LOOP(init, which, x) do {\
routebox_t *__next_one__ = (init);\
x = NULL;\
if (!__next_one__)\
assert(__next_one__);\
else\
while (!x || __next_one__ != (init)) {\
x = __next_one__;\
/* save next one first in case the command modifies or frees it */\
__next_one__ = x->which.next
#define FOREACH_SUBNET(net, p) do {\
routebox_t *_pp_;\
/* fail-fast: check subnet_processed flags */\
LIST_LOOP(net, same_net, p); \
assert(!p->flags.subnet_processed);\
END_LOOP;\
/* iterate through *distinct* subnets */\
LIST_LOOP(net, same_net, p); \
if (!p->flags.subnet_processed) {\
LIST_LOOP(p, same_subnet, _pp_);\
_pp_->flags.subnet_processed=1;\
END_LOOP
#define END_FOREACH(net, p) \
}; \
END_LOOP;\
/* reset subnet_processed flags */\
LIST_LOOP(net, same_net, p); \
p->flags.subnet_processed=0;\
END_LOOP;\
} while (0)
#define SWAP(t, f, s) do { t a=s; s=f; f=a; } while (0)
/* notes:
* all rectangles are assumed to be closed on the top and left and
* open on the bottom and right. That is, they include their top-left
* corner but don't include their bottom and right edges.
*
* expansion regions are always half-closed. This means that when
* tracing paths, you must steer clear of the bottom and right edges.,
* because these are not actually in the allowed box.
*
* All routeboxes *except* EXPANSION_AREAS now have their "box" bloated by
* their particular required keepaway. This simplifies the tree searching.
* the "sbox" contains the unbloated box.
*/
/* ---------------------------------------------------------------------------
* some local types
*/
/* enumerated type for conflict levels */
typedef enum
{ NO_CONFLICT = 0, LO_CONFLICT = 1, HI_CONFLICT = 2 }
conflict_t;
typedef struct routebox_list
{
struct routebox *next, *prev;
}routebox_list;
typedef enum etype
{ PAD, PIN, VIA, VIA_SHADOW, LINE, OTHER, EXPANSION_AREA, PLANE, THERMAL }
etype;
typedef struct routebox
{
BoxType box, sbox;
union
{
PadTypePtr pad;
PinTypePtr pin;
PinTypePtr via;
struct routebox *via_shadow; /* points to the via in r-tree which
* points to the PinType in the PCB. */
LineTypePtr line;
void *generic; /* 'other' is polygon, arc, text */
struct routebox *expansion_area; /* previous expansion area in search */
}
parent;
unsigned short group;
unsigned short layer;
etype type;
struct
{
unsigned nonstraight:1;
unsigned fixed:1;
/* for searches */
unsigned source:1;
unsigned target:1;
/* rects on same net as source and target don't need clearance areas */
unsigned nobloat:1;
/* mark circular pins, so that we be sure to connect them up properly */
unsigned circular:1;
/* we sometimes create routeboxen that don't actually belong to a
* r-tree yet -- make sure refcount of homelesss is set properly */
unsigned homeless:1;
/* was this nonfixed obstacle generated on an odd or even pass? */
unsigned is_odd:1;
/* fixed route boxes that have already been "routed through" in this
* search have their "touched" flag set. */
unsigned touched:1;
/* this is a status bit for iterating through *different* subnets */
unsigned subnet_processed:1;
/* some expansion_areas represent via candidates */
unsigned is_via:1;
/* mark non-straight lines which go from bottom-left to upper-right,
* instead of from upper-left to bottom-right. */
unsigned bl_to_ur:1;
/* mark polygons which are "transparent" for via-placement; that is,
* vias through the polygon will automatically be given a keepaway
* and will not electrically connect to the polygon. */
unsigned clear_poly:1;
/* this marks "conflicting" routes that must be torn up to obtain
* a correct routing. This flag allows us to return a correct routing
* even if the user cancels auto-route after a non-final pass. */
unsigned is_bad:1;
/* for assertion that 'box' is never changed after creation */
unsigned inited:1;
/* indicate this expansion ares is a thermal between the pin and plane */
unsigned is_thermal;
}
flags;
/* indicate the direction an expansion box came from */
cost_t cost;
CheapPointType cost_point;
/* reference count for homeless routeboxes; free when refcount==0 */
int refcount;
/* when routing with conflicts, we keep a record of what we're
* conflicting with.
*/
vector_t *conflicts_with;
/* route style of the net associated with this routebox */
RouteStyleType *style;
/* congestion values for the edges of an expansion box */
unsigned char n, e, s, w;
/* what pass this this track was laid down on */
unsigned char pass;
/* the direction this came from, if any */
direction_t came_from;
/* circular lists with connectivity information. */
routebox_list same_net, same_subnet, original_subnet, different_net;
}
routebox_t;
typedef struct routedata
{
/* one rtree per layer *group */
rtree_t *layergrouptree[MAX_LAYER]; /* no silkscreen layers here =) */
/* root pointer into connectivity information */
routebox_t *first_net;
/* default routing style */
RouteStyleType defaultstyle;
/* style structures */
RouteStyleType *styles[NUM_STYLES + 1];
/* what is the maximum bloat (keepaway+line half-width or
* keepaway+via_radius) for any style we've seen? */
BDimension max_bloat;
BDimension max_keep;
mtspace_t *mtspace;
}
routedata_t;
typedef struct edge_struct
{
routebox_t *rb; /* path expansion edges are real routeboxen. */
CheapPointType cost_point;
cost_t cost_to_point; /* from source */
cost_t cost; /* cached edge cost */
routebox_t *mincost_target; /* minimum cost from cost_point to any target */
vetting_t *work; /* for via search edges */
direction_t expand_dir;
struct
{
/* this indicates that this 'edge' is a via candidate. */
unsigned is_via:1;
/* record "conflict level" of via candidates, in case we need to split
* them later. */
conflict_t via_conflict_level:2;
/* when "routing with conflicts", sometimes edge is interior. */
unsigned is_interior:1;
/* this is a fake edge used to defer searching for via spaces */
unsigned via_search:1;
/* this is a via edge in a plane where the cost point moves for free */
unsigned in_plane:1;
}
flags;
}
edge_t;
static struct
{
/* net style parameters */
RouteStyleType *style;
/* the present bloat */
BDimension bloat;
/* cost parameters */
cost_t ViaCost, /* additional "length" cost for using a via */
LastConflictPenalty, /* length mult. for routing over last pass' trace */
ConflictPenalty, /* length multiplier for routing over another trace */
JogPenalty, /* additional "length" cost for changing direction */
CongestionPenalty, /* (rational) length multiplier for routing in */
NewLayerPenalty, /* penalty for routing on a previously unused layer */
MinPenalty; /* smallest Direction Penalty */
/* maximum conflict incidence before calling it "no path found" */
int hi_conflict;
/* are vias allowed? */
bool use_vias;
/* is this an odd or even pass? */
bool is_odd;
/* permit conflicts? */
bool with_conflicts;
/* is this a final "smoothing" pass? */
bool is_smoothing;
/* rip up nets regardless of conflicts? */
bool rip_always;
bool last_smooth;
unsigned char pass;
}
AutoRouteParameters;
struct routeone_state
{
/* heap of all candidate expansion edges */
heap_t *workheap;
/* information about the best path found so far. */
routebox_t *best_path, *best_target;
cost_t best_cost;
};
/* ---------------------------------------------------------------------------
* some local prototypes
*/
static routebox_t *CreateExpansionArea (const BoxType * area, Cardinal group,
routebox_t * parent,
bool relax_edge_requirements,
edge_t * edge);
static cost_t edge_cost (const edge_t * e, const cost_t too_big);
static void best_path_candidate (struct routeone_state *s,
edge_t * e, routebox_t * best_target);
static BoxType edge_to_box (const routebox_t * rb, direction_t expand_dir);
static void add_or_destroy_edge (struct routeone_state *s, edge_t * e);
static void
RD_DrawThermal (routedata_t * rd, LocationType X, LocationType Y,
Cardinal group, Cardinal layer, routebox_t * subnet,
bool is_bad);
static void ResetSubnet (routebox_t * net);
#ifdef ROUTE_DEBUG
static int showboxen = -2;
static int aabort = 0;
static void showroutebox (routebox_t * rb);
#endif
/* ---------------------------------------------------------------------------
* some local identifiers
*/
/* group number of groups that hold surface mount pads */
static Cardinal front, back;
static bool usedGroup[MAX_LAYER];
static int x_cost[MAX_LAYER], y_cost[MAX_LAYER];
static bool is_layer_group_active[MAX_LAYER];
static int ro = 0;
static int smoothes = 1;
static int passes = 12;
static int routing_layers = 0;
static float total_wire_length = 0;
static int total_via_count = 0;
/* assertion helper for routeboxen */
#ifndef NDEBUG
static int
__routebox_is_good (routebox_t * rb)
{
assert (rb && (rb->group < max_group) &&
(rb->box.X1 <= rb->box.X2) && (rb->box.Y1 <= rb->box.Y2) &&
(rb->flags.homeless ?
(rb->box.X1 != rb->box.X2) || (rb->box.Y1 != rb->box.Y2) :
(rb->box.X1 != rb->box.X2) && (rb->box.Y1 != rb->box.Y2)));
assert ((rb->flags.source ? rb->flags.nobloat : 1) &&
(rb->flags.target ? rb->flags.nobloat : 1) &&
(rb->flags.homeless ? !rb->flags.touched : rb->refcount == 0) &&
(rb->flags.touched ? rb->type != EXPANSION_AREA : 1));
assert ((rb->flags.is_odd ? (!rb->flags.fixed) &&
(rb->type == VIA || rb->type == VIA_SHADOW || rb->type == LINE
|| rb->type == PLANE) : 1));
assert (rb->flags.clear_poly ?
((rb->type == OTHER || rb->type == PLANE) && rb->flags.fixed
&& !rb->flags.homeless) : 1);
assert (rb->flags.inited);
/* run through conflict list showing none are homeless, targets or sources */
if (rb->conflicts_with)
{
int i;
for (i = 0; i < vector_size (rb->conflicts_with); i++)
{
routebox_t *c = vector_element (rb->conflicts_with, i);
assert (!c->flags.homeless && !c->flags.source && !c->flags.target
&& !c->flags.fixed);
}
}
assert (rb->style != NULL && rb->style != NULL);
assert (rb->type == EXPANSION_AREA
|| (rb->same_net.next && rb->same_net.prev && rb->same_subnet.next
&& rb->same_subnet.prev && rb->original_subnet.next
&& rb->original_subnet.prev && rb->different_net.next
&& rb->different_net.prev));
return 1;
}
static int
__edge_is_good (edge_t * e)
{
assert (e && e->rb && __routebox_is_good (e->rb));
assert ((e->rb->flags.homeless ? e->rb->refcount > 0 : 1));
assert ((0 <= e->expand_dir) && (e->expand_dir < 9)
&& (e->flags.
is_interior ? (e->expand_dir == ALL
&& e->rb->conflicts_with) : 1));
assert ((e->flags.is_via ? e->rb->flags.is_via : 1)
&& (e->flags.via_conflict_level >= 0
&& e->flags.via_conflict_level <= 2)
&& (e->flags.via_conflict_level != 0 ? e->flags.is_via : 1));
assert ((e->cost_to_point >= 0) && e->cost >= 0);
return 1;
}
int
no_planes (const BoxType * b, void *cl)
{
routebox_t *rb = (routebox_t *) b;
if (rb->type == PLANE)
return 0;
return 1;
}
#endif /* !NDEBUG */
/*---------------------------------------------------------------------
* route utility functions.
*/
enum boxlist
{ NET, SUBNET, ORIGINAL, DIFFERENT_NET };
static struct routebox_list *
__select_list (routebox_t * r, enum boxlist which)
{
assert (r);
switch (which)
{
default:
assert (0);
case NET:
return &(r->same_net);
case SUBNET:
return &(r->same_subnet);
case ORIGINAL:
return &(r->original_subnet);
case DIFFERENT_NET:
return &(r->different_net);
}
}
static void
InitLists (routebox_t * r)
{
static enum boxlist all[] =
{ NET, SUBNET, ORIGINAL, DIFFERENT_NET }
, *p;
for (p = all; p < all + (sizeof (all) / sizeof (*p)); p++)
{
struct routebox_list *rl = __select_list (r, *p);
rl->prev = rl->next = r;
}
}
static void
MergeNets (routebox_t * a, routebox_t * b, enum boxlist which)
{
struct routebox_list *al, *bl, *anl, *bnl;
routebox_t *an, *bn;
assert (a && b);
assert (a != b);
assert (a->type != EXPANSION_AREA);
assert (b->type != EXPANSION_AREA);
al = __select_list (a, which);
bl = __select_list (b, which);
assert (al && bl);
an = al->next;
bn = bl->next;
assert (an && bn);
anl = __select_list (an, which);
bnl = __select_list (bn, which);
assert (anl && bnl);
bl->next = an;
anl->prev = b;
al->next = bn;
bnl->prev = a;
}
static void
RemoveFromNet (routebox_t * a, enum boxlist which)
{
struct routebox_list *al, *anl, *apl;
routebox_t *an, *ap;
assert (a);
al = __select_list (a, which);
assert (al);
an = al->next;
ap = al->prev;
if (an == a || ap == a)
return; /* not on any list */
assert (an && ap);
anl = __select_list (an, which);
apl = __select_list (ap, which);
assert (anl && apl);
anl->prev = ap;
apl->next = an;
al->next = al->prev = a;
return;
}
static void
init_const_box (routebox_t * rb,
LocationType X1, LocationType Y1, LocationType X2,
LocationType Y2, BDimension keepaway)
{
BoxType *bp = (BoxType *) & rb->box; /* note discarding const! */
assert (!rb->flags.inited);
assert (X1 <= X2 && Y1 <= Y2);
bp->X1 = X1 - keepaway;
bp->Y1 = Y1 - keepaway;
bp->X2 = X2 + keepaway;
bp->Y2 = Y2 + keepaway;
bp = (BoxType *) & rb->sbox;
bp->X1 = X1;
bp->Y1 = Y1;
bp->X2 = X2;
bp->Y2 = Y2;
rb->flags.inited = 1;
}
static inline BoxType
shrink_routebox (const routebox_t * rb)
{
return rb->sbox;
}
static inline cost_t
box_area (const BoxType b)
{
cost_t ans = b.X2 - b.X1;
return ans * (b.Y2 - b.Y1);
}
static inline CheapPointType
closest_point_in_routebox (const CheapPointType * from, const routebox_t * rb)
{
return closest_point_in_box (from, &rb->sbox);
}
static inline bool
point_in_shrunk_box (const routebox_t * box, LocationType X, LocationType Y)
{
BoxType b = shrink_routebox (box);
return point_in_box (&b, X, Y);
}
/*---------------------------------------------------------------------
* routedata initialization functions.
*/
static routebox_t *
AddPin (PointerListType layergroupboxes[], PinTypePtr pin, bool is_via,
RouteStyleType * style)
{
routebox_t **rbpp, *lastrb = NULL;
int i, ht;
/* a pin cuts through every layer group */
for (i = 0; i < max_group; i++)
{
rbpp = (routebox_t **) GetPointerMemory (&layergroupboxes[i]);
*rbpp = (routebox_t *)malloc (sizeof (**rbpp));
memset ((void *) *rbpp, 0, sizeof (**rbpp));
(*rbpp)->group = i;
ht = HALF_THICK (MAX (pin->Thickness, pin->DrillingHole));
init_const_box (*rbpp,
/*X1 */ pin->X - ht,
/*Y1 */ pin->Y - ht,
/*X2 */ pin->X + ht,
/*Y2 */ pin->Y + ht, style->Keepaway);
/* set aux. properties */
if (is_via)
{
(*rbpp)->type = VIA;
(*rbpp)->parent.via = pin;
}
else
{
(*rbpp)->type = PIN;
(*rbpp)->parent.pin = pin;
}
(*rbpp)->flags.fixed = 1;
(*rbpp)->came_from = ALL;
(*rbpp)->style = style;
(*rbpp)->flags.circular = !TEST_FLAG (SQUAREFLAG, pin);
/* circular lists */
InitLists (*rbpp);
/* link together */
if (lastrb)
{
MergeNets (*rbpp, lastrb, NET);
MergeNets (*rbpp, lastrb, SUBNET);
MergeNets (*rbpp, lastrb, ORIGINAL);
}
lastrb = *rbpp;
}
return lastrb;
}
static routebox_t *
AddPad (PointerListType layergroupboxes[],
ElementTypePtr element, PadTypePtr pad, RouteStyleType * style)
{
BDimension halfthick;
routebox_t **rbpp;
int layergroup = (TEST_FLAG (ONSOLDERFLAG, pad) ? back : front);
assert (0 <= layergroup && layergroup < max_group);
assert (PCB->LayerGroups.Number[layergroup] > 0);
rbpp = (routebox_t **) GetPointerMemory (&layergroupboxes[layergroup]);
assert (rbpp);
*rbpp = (routebox_t *)malloc (sizeof (**rbpp));
assert (*rbpp);
memset (*rbpp, 0, sizeof (**rbpp));
(*rbpp)->group = layergroup;
halfthick = HALF_THICK (pad->Thickness);
init_const_box (*rbpp,
/*X1 */ MIN (pad->Point1.X, pad->Point2.X) - halfthick,
/*Y1 */ MIN (pad->Point1.Y, pad->Point2.Y) - halfthick,
/*X2 */ MAX (pad->Point1.X, pad->Point2.X) + halfthick,
/*Y2 */ MAX (pad->Point1.Y, pad->Point2.Y) + halfthick,
style->Keepaway);
/* kludge for non-manhattan pads (which are not allowed at present) */
if (pad->Point1.X != pad->Point2.X && pad->Point1.Y != pad->Point2.Y)
(*rbpp)->flags.nonstraight = 1;
/* set aux. properties */
(*rbpp)->type = PAD;
(*rbpp)->parent.pad = pad;
(*rbpp)->flags.fixed = 1;
(*rbpp)->came_from = ALL;
(*rbpp)->style = style;
/* circular lists */
InitLists (*rbpp);
return *rbpp;
}
static routebox_t *
AddLine (PointerListType layergroupboxes[], int layergroup, LineTypePtr line,
LineTypePtr ptr, RouteStyleType * style)
{
routebox_t **rbpp;
assert (layergroupboxes && line);
assert (0 <= layergroup && layergroup < max_group);
assert (PCB->LayerGroups.Number[layergroup] > 0);
rbpp = (routebox_t **) GetPointerMemory (&layergroupboxes[layergroup]);
*rbpp = (routebox_t *)malloc (sizeof (**rbpp));
memset (*rbpp, 0, sizeof (**rbpp));
(*rbpp)->group = layergroup;
init_const_box (*rbpp,
/*X1 */ MIN (line->Point1.X,
line->Point2.X) - HALF_THICK (line->Thickness),
/*Y1 */ MIN (line->Point1.Y,
line->Point2.Y) - HALF_THICK (line->Thickness),
/*X2 */ MAX (line->Point1.X,
line->Point2.X) + HALF_THICK (line->Thickness),
/*Y2 */ MAX (line->Point1.Y,
line->Point2.Y) +
HALF_THICK (line->Thickness), style->Keepaway);
/* kludge for non-manhattan lines */
if (line->Point1.X != line->Point2.X && line->Point1.Y != line->Point2.Y)
{
(*rbpp)->flags.nonstraight = 1;
(*rbpp)->flags.bl_to_ur =
(MIN (line->Point1.X, line->Point2.X) == line->Point1.X) !=
(MIN (line->Point1.Y, line->Point2.Y) == line->Point1.Y);
#if defined(ROUTE_DEBUG) && defined(DEBUG_SHOW_ZIGZAG)
showroutebox (*rbpp);
#endif
}
/* set aux. properties */
(*rbpp)->type = LINE;
(*rbpp)->parent.line = ptr;
(*rbpp)->flags.fixed = 1;
(*rbpp)->came_from = ALL;
(*rbpp)->style = style;
/* circular lists */
InitLists (*rbpp);
return *rbpp;
}
static routebox_t *
AddIrregularObstacle (PointerListType layergroupboxes[],
LocationType X1, LocationType Y1,
LocationType X2, LocationType Y2, Cardinal layergroup,
void *parent, RouteStyleType * style)
{
routebox_t **rbpp;
LocationType keep = style->Keepaway;
assert (layergroupboxes && parent);
assert (X1 <= X2 && Y1 <= Y2);
assert (0 <= layergroup && layergroup < max_group);
assert (PCB->LayerGroups.Number[layergroup] > 0);
rbpp = (routebox_t **) GetPointerMemory (&layergroupboxes[layergroup]);
*rbpp = (routebox_t *)malloc (sizeof (**rbpp));
memset (*rbpp, 0, sizeof (**rbpp));
(*rbpp)->group = layergroup;
init_const_box (*rbpp, X1, Y1, X2, Y2, keep);
(*rbpp)->flags.nonstraight = 1;
(*rbpp)->type = OTHER;
(*rbpp)->parent.generic = parent;
(*rbpp)->flags.fixed = 1;
(*rbpp)->style = style;
/* circular lists */
InitLists (*rbpp);
return *rbpp;
}
static routebox_t *
AddPolygon (PointerListType layergroupboxes[], Cardinal layer,
PolygonTypePtr polygon, RouteStyleType * style)
{
int is_not_rectangle = 1;
int layergroup = GetLayerGroupNumberByNumber (layer);
routebox_t *rb;
assert (0 <= layergroup && layergroup < max_group);
rb = AddIrregularObstacle (layergroupboxes,
polygon->BoundingBox.X1,
polygon->BoundingBox.Y1,
polygon->BoundingBox.X2,
polygon->BoundingBox.Y2,
layergroup, polygon, style);
if (polygon->PointN == 4 &&
polygon->HoleIndexN == 0 &&
(polygon->Points[0].X == polygon->Points[1].X ||
polygon->Points[0].Y == polygon->Points[1].Y) &&
(polygon->Points[1].X == polygon->Points[2].X ||
polygon->Points[1].Y == polygon->Points[2].Y) &&
(polygon->Points[2].X == polygon->Points[3].X ||
polygon->Points[2].Y == polygon->Points[3].Y) &&
(polygon->Points[3].X == polygon->Points[0].X ||
polygon->Points[3].Y == polygon->Points[0].Y))
is_not_rectangle = 0;
rb->flags.nonstraight = is_not_rectangle;
rb->layer = layer;
rb->came_from = ALL;
if (TEST_FLAG (CLEARPOLYFLAG, polygon))
{
rb->flags.clear_poly = 1;
if (!is_not_rectangle)
rb->type = PLANE;
}
return rb;
}
static void
AddText (PointerListType layergroupboxes[], Cardinal layergroup,
TextTypePtr text, RouteStyleType * style)
{
AddIrregularObstacle (layergroupboxes,
text->BoundingBox.X1, text->BoundingBox.Y1,
text->BoundingBox.X2, text->BoundingBox.Y2,
layergroup, text, style);
}
static routebox_t *
AddArc (PointerListType layergroupboxes[], Cardinal layergroup,
ArcTypePtr arc, RouteStyleType * style)
{
return AddIrregularObstacle (layergroupboxes,
arc->BoundingBox.X1, arc->BoundingBox.Y1,
arc->BoundingBox.X2, arc->BoundingBox.Y2,
layergroup, arc, style);
}
struct rb_info
{
BoxType query;
routebox_t *winner;
jmp_buf env;
};
static int
__found_one_on_lg (const BoxType * box, void *cl)
{
struct rb_info *inf = (struct rb_info *) cl;
routebox_t *rb = (routebox_t *) box;
BoxType sb;
if (rb->flags.nonstraight)
return 0;
sb = shrink_box (&rb->box, rb->style->Keepaway);
if (inf->query.X1 >= sb.X2 || inf->query.X2 <= sb.X1 ||
inf->query.Y1 >= sb.Y2 || inf->query.Y2 <= sb.Y1)
return 0;
inf->winner = rb;
if (rb->type == PLANE)
return 1; /* keep looking for something smaller if a plane was found */
longjmp (inf->env, 1);
return 0;
}
static routebox_t *
FindRouteBoxOnLayerGroup (routedata_t * rd,
LocationType X, LocationType Y, Cardinal layergroup)
{
struct rb_info info;
info.winner = NULL;
info.query = point_box (X, Y);
if (setjmp (info.env) == 0)
r_search (rd->layergrouptree[layergroup], &info.query, NULL,
__found_one_on_lg, &info);
return info.winner;
}
#ifdef ROUTE_DEBUG_VERBOSE
static void
DumpRouteBox (routebox_t * rb)
{
printf ("RB: (%d,%d)-(%d,%d) l%d; ",
rb->box.X1, rb->box.Y1, rb->box.X2, rb->box.Y2, (int) rb->group);
switch (rb->type)
{
case PAD:
printf ("PAD[%s %s] ", rb->parent.pad->Name, rb->parent.pad->Number);
break;
case PIN:
printf ("PIN[%s %s] ", rb->parent.pin->Name, rb->parent.pin->Number);
break;
case VIA:
if (!rb->parent.via)
break;
printf ("VIA[%s %s] ", rb->parent.via->Name, rb->parent.via->Number);
break;
case LINE:
printf ("LINE ");
break;
case OTHER:
printf ("OTHER ");
break;
case EXPANSION_AREA:
printf ("EXPAREA ");
break;
default:
printf ("UNKNOWN ");
break;
}
if (rb->flags.nonstraight)
printf ("(nonstraight) ");
if (rb->flags.fixed)
printf ("(fixed) ");
if (rb->flags.source)
printf ("(source) ");
if (rb->flags.target)
printf ("(target) ");
if (rb->flags.homeless)
printf ("(homeless) ");
printf ("\n");
}
#endif
static routedata_t *
CreateRouteData ()
{
NetListListType Nets;
PointerListType layergroupboxes[MAX_LAYER];
BoxType bbox;
routedata_t *rd;
int group, i;
/* check which layers are active first */
routing_layers = 0;
for (group = 0; group < max_group; group++)
{
for (i = 0; i < PCB->LayerGroups.Number[group]; i++)
/* layer must be 1) not silk (ie, < max_copper_layer) and 2) on */
if ((PCB->LayerGroups.Entries[group][i] < max_copper_layer) &&
PCB->Data->Layer[PCB->LayerGroups.Entries[group][i]].On)
{
routing_layers++;
is_layer_group_active[group] = true;
break;
}
else
is_layer_group_active[group] = false;
}
/* if via visibility is turned off, don't use them */
AutoRouteParameters.use_vias = routing_layers > 1 && PCB->ViaOn;
front = GetLayerGroupNumberByNumber (component_silk_layer);
back = GetLayerGroupNumberByNumber (solder_silk_layer);
/* determine preferred routing direction on each group */
for (i = 0; i < max_group; i++)
{
if (i != back && i != front)
{
x_cost[i] = (i & 1) ? 2 : 1;
y_cost[i] = (i & 1) ? 1 : 2;
}
else if (i == back)
{
x_cost[i] = 4;
y_cost[i] = 2;
}
else
{
x_cost[i] = 2;
y_cost[i] = 2;
}
}
/* create routedata */
rd = (routedata_t *)malloc (sizeof (*rd));
memset ((void *) rd, 0, sizeof (*rd));
/* create default style */
rd->defaultstyle.Thick = Settings.LineThickness;
rd->defaultstyle.Diameter = Settings.ViaThickness;
rd->defaultstyle.Hole = Settings.ViaDrillingHole;
rd->defaultstyle.Keepaway = Settings.Keepaway;
rd->max_bloat = BLOAT (&rd->defaultstyle);
rd->max_keep = Settings.Keepaway;
/* create styles structures */
bbox.X1 = bbox.Y1 = 0;
bbox.X2 = PCB->MaxWidth;
bbox.Y2 = PCB->MaxHeight;
for (i = 0; i < NUM_STYLES + 1; i++)
{
RouteStyleType *style =
(i < NUM_STYLES) ? &PCB->RouteStyle[i] : &rd->defaultstyle;