summaryrefslogtreecommitdiff
path: root/scripts/arrangepins.tcl.in
blob: 48175ee41980384c859a548e79bacd56199d6b9f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
#!TCLSH_PATH
#-----------------------------------------------------------------------
# arrangepins.tcl ---
#
# Pin reassignment surgery.  Your health plan does not cover this.
#
# Because graywolf has a truly horrific handling of pin placement, some
# redistribution of pins is needed after running placement.  graywolf
# does a generally good job of placing pins around the border but tends
# to cluster them unnecessarily, sometimes placing them far away from
# the pin to which they're connected.
#
# This routine parses the .cel2 file for pin hints, and then
# redistributes the pins around the layout perimeter, trying to maintain
# double spacing between pins when possible and not overlapping vectors
# as much as possible, weighed against the distance from pin to cell.
# Also avoids putting pins directly under power bus stripes.
#
# What it does:  Reads the (unrouted) DEF file for existing pin
# placements, adjusts the pin positions, and writes out a modified DEF
# file with new pin positions.
#
# Written by Tim Edwards, Open Circuit Design, 5/16/2018
#-----------------------------------------------------------------------

namespace path {::tcl::mathop ::tcl::mathfunc}

if {$argc < 1} {
   puts stdout "Usage:  arrangepins \[<options>\] <project_name>"
   puts stdout "Options:"
   puts stdout "   (none at present)"
   exit 0
}

set argidx 0

# Option parsing:  Take code from addspacers.tcl
set debug false

set topname [file rootname [lindex $argv $argidx]]

set defname ${topname}.def
set defoutname ${topname}_mod.def

set infoname ${topname}.info

set units 100           ;# write centimicron units into the DEF file (default)

#-----------------------------------------------------------------
# Open all files for reading and writing
#-----------------------------------------------------------------

if [catch {open $defname r} fdef] {
    puts stderr "Error: can't open DEF file $defname for input"
    return
}

# The .info file is optional;  get track pitch and width information
# for each layer.

set layersrec [dict create]

if {![catch {open $infoname r} finfo]} {
    puts stdout "Reading info file ${infoname}. . ."
    while {[gets $finfo line] >= 0} {
	if [regexp {[ \t]*#} $line lmatch] {
            continue
	} elseif [regexp {[ \t]*qrouter[ \t]+([^ \t]+)} $line lmatch qversion] {
	    continue
	} elseif [regexp {[ \t]*([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t\n]+)} $line lmatch layername pitch offset width orient] {
	    # Note:  Could make use of pitch information here, relying on DEF file
	    # TRACKS section instead.

	    set layerrec [dict create]
	    if {$orient == "vertical"} {
		dict set layerrec xpitch $pitch
	    } else {
		dict set layerrec ypitch $pitch
	    }
	    dict set layerrec offset $offset
	    dict set layerrec width $width
	    dict set layersrec $layername $layerrec
	}
    }
    close $finfo
}

puts stdout "Reading DEF file ${defname}. . ."
flush stdout

set pinsrec [dict create]
set specialrec [dict create]
set compsrec [dict create]
set inspecial false
set incomps false
set inpins false
set innets false

while {[gets $fdef line] >= 0} {
    if [regexp {[ \t]*#} $line lmatch] {
        continue
    } elseif [regexp {[ \t]*UNITS[ \t]+DISTANCE[ \t]+MICRONS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
	set units $number
    } elseif [regexp {[ \t]*SPECIALNETS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
        # Parse the "SPECIALNETS" statement
	if $debug {puts stdout "start specialnets section"}
	set inspecial true
    } elseif [regexp {[ \t]*NETS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
        # Parse the "NETS" statement
	if $debug {puts stdout "start nets section"}
	set innets true
    } elseif [regexp {[ \t]*PINS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
        set rows [dict create]
        # Parse the "PINS" statement
	if $debug {puts stdout "start pins section"}
	set inpins true
    } elseif [regexp {[ \t]*COMPONENTS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
        # Parse the "COMPONENTS" statement
	if $debug {puts stdout "start components section"}
	set incomps true
    }
    if $innets {
	if [regexp {[ \t]*END[ \t]+NETS} $line lmatch] {
	    if $debug {puts stdout "end nets section"}
	    set innets false
	} elseif [regexp {[ \t]*-[ \t]+([^ \t]+)} $line lmatch netname] {
	    if [catch {set pinrec [dict get $pinsrec $netname]}] {
		set netname ""
	    }
	    incr number -1
	} elseif {($netname != "") && [regexp {[ \t]*\([ \t]*([^ \t]+)} $line lmatch instname]} {
	    # Pull the pin record (NOTE:  This assumes the pin name = the net name.
	    # This is true for qflow.  Otherwise, would need a cross-referencing
	    # dictionary indexed by net name).
	    if {![catch {set instbounds [dict get $compsrec $instname]}]} {
		# Dictionary lookup will fail for instance PIN, which is the
		# behavior we want.  Process only instance connections internal
		# to the digital core.

		set instx [lindex $instbounds 0]
		set insty [lindex $instbounds 1]

		# Use the sorted record of instance X, Y positions to determine
		# the center position of the instance.

		set yidx [lsearch $yrows $insty]
		set topy [lindex $yrows [+ $yidx 1]]

		set xidx [lsearch [subst \$row$insty] $instx]
		set topx [lindex [subst \$row$insty] [+ $xidx 1]]

		# puts stdout "yidx = $yidx, topy = $topy"
		# puts stdout "xidx = $xidx, topx = $topx"

		set instx [/ [+ $instx $topx] 2]
		set insty [/ [+ $insty $topy] 2]
		
		if {![catch {set bbox [dict get $pinrec bbox]}]} {
		    set llx [lindex $bbox 0]
		    set lly [lindex $bbox 1]
		    set urx [lindex $bbox 2]
		    set ury [lindex $bbox 3]
		    if {$instx < $llx} {set llx $instx}
		    if {$insty < $lly} {set lly $insty}
		    if {$instx > $urx} {set urx $instx}
		    if {$insty > $ury} {set ury $insty}
		    set bbox [list $llx $lly $urx $ury]
		    dict set pinrec bbox $bbox
		} else {
		    dict set pinrec bbox [list $instx $insty $instx $insty]
		}
		# Update the pinrec entry
		dict set pinsrec $netname $pinrec
	    }
	}
    } elseif $incomps {
	if [regexp {[ \t]*END[ \t]+COMPONENTS} $line lmatch] {
	    set incomps false
	    if $debug {puts stdout "end components section"}

	    # Sort the row Y values
	    set yrows [lsort -integer -unique $yrows]

	    # Find the unique set of rows
	    set numrows [llength $yrows]
	    set lastyrow [lindex $yrows [- $numrows 1]]
	    set prevyrow [lindex $yrows [- $numrows 2]]
	    set rowheight [- $lastyrow $prevyrow]

	    # Sort the X value ranges in each row
	    foreach yval $yrows {
		set row$yval [lsort -integer -unique [subst \$row$yval]]
		lappend row$yval $dieurx
	    }

	    # Add one row to list for the top side of the topmost row
	    lappend yrows [+ $lastyrow $rowheight]

	    # puts stdout "Length of yrows is [llength $yrows]"
	    # puts stdout "Next-to-last yrow is [lindex $yrows [- [llength $yrows] 2]]"
	    # puts stdout "Last yrow is [lindex $yrows [- [llength $yrows] 1]]"

	} elseif [regexp {[ \t]*-[ \t]+([^ \t]+)[ \t]+[^ \t]+[ \t]+\+[ \t]+PLACED[ \t]+\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)} $line lmatch instname instx insty] {
	    # Record the position of this instance for matching to pins.  Only
	    # need placement X and Y for a rough estimate of all pin positions.
	    dict set compsrec $instname [list $instx $insty]

	    # Record the X and Y positions in arrays that can be used to
	    # track down the cell centerpoints, a better estimate of the
	    # optimal route position than the lower left hand corner.

	    lappend yrows $insty
	    lappend row$insty $instx
	}
    } elseif $inspecial {
	if [regexp {[ \t]*END[ \t]+SPECIALNETS} $line lmatch] {
	    set inspecial false
	    if $debug {puts stdout "end specialnets section"}
	} elseif [regexp {[ \t]*-[ \t]+([^ \t]+)} $line lmatch netname] {
	    # Remove specialnets from the pin list and place them in
	    # a different record.
	    if {![catch {set pinrec [dict get $pinsrec $netname]}]} {
                if $debug {puts stdout "Removing $netname from pinsrec dict:"}
		dict unset pinsrec $netname
		dict set pinrec xranges []
		dict set specialrec $netname $pinrec
	    }
	    incr number -1
	} elseif [regexp {[ \t]*NEW[ \t]+[^ \t]+[ \t]+([^ \t]+)[ \t]+\([ \t]*(-?[0-9]+)[ \t]+-?[0-9]+[ \t]*\)[ \t]*\([ \t]*\*[ \t]+-?[0-9]+[ \t]*\)} $line lmatch width xmid] {
	    # Find the vertical stripes and register the X range of each
	    catch {
	        set xranges [dict get $pinrec xranges]
	        set hwid [/ $width 2] 
	        set xlow [- $xmid $hwid]
	        set xhigh [+ $xmid $hwid]
	        lappend xranges [list $xlow $xhigh]
		dict set pinrec xranges $xranges
		dict set specialrec $netname $pinrec
	    }
	}
    } elseif $inpins {
	if [regexp {[ \t]*END[ \t]+PINS} $line lmatch] {
	    set inpins false
	    if $debug {puts stdout "end pins section"}
	} elseif [regexp {[ \t]*-[ \t]+([^ \t]+)[ \t]+\+[ \t]+NET[ \t]+([^ \t]+)} $line lmatch pinname netname] {
	    set pinrec [dict create]
	    dict set pinrec net $netname
	    dict set pinsrec $pinname $pinrec
	    incr number -1
	} elseif [regexp {[ \t]*\+[ \t]+PLACED[ \t]+\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)[ \t]+([^ \t]+)[ \t]*;} $line lmatch pinx piny pino] {
	    dict set pinrec x $pinx
	    dict set pinrec y $piny
	    dict set pinrec o $pino
	    dict set pinsrec $pinname $pinrec
	} elseif [regexp {[ \t]*\+[ \t]+LAYER[ \t]+([^ \t]+)[ \t]+\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)[ \t]*\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)} $line lmatch layer llx lly urx ury] {
	    dict set pinrec llx $llx
	    dict set pinrec lly $lly
	    dict set pinrec urx $urx
	    dict set pinrec ury $ury
	    dict set pinrec layer $layer
	    dict set pinsrec $pinname $pinrec
	}
    } else {
	if [regexp {[ \t]*TRACKS[ \t]+([XY])[ \t]+[^ \t]+[ \t]+DO[ \t]+[^ \t]+[ \t]+STEP[ \t]+([^ \t]+)[ \t]+LAYER[ \t]+([^ \t;]+)[ \t]*;} $line lmatch orient pitch layer] {
	    if {[dict exists $layersrec $layer]} {
		set layerrec [dict get $layersrec $layer]
	    } else {
		set layerrec [dict create]
	    }
	    if {$orient == "X"} {
		dict set layerrec xpitch $pitch
	    } else {
		dict set layerrec ypitch $pitch
	    }
	    dict set layersrec $layer $layerrec

 	} elseif [regexp {[ \t]*DIEAREA[ \t]+\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)[ \t]*\([ \t]*(-?[0-9]+)[ \t]+(-?[0-9]+)[ \t]*\)} $line lmatch diellx dielly dieurx dieury] {
	    # Initialize the pin bounding box to the die area center point
	    set pinllx [/ [+ $diellx $dieurx] 2]
	    set pinlly [/ [+ $dielly $dieury] 2]
	    set pinurx $pinllx
	    set pinury $pinlly
	}
    }
}

# Check if all layer records have an xpitch and a ypitch.  If one is
# missing, pull it from the next layer above, or failing that, below.

# Note that "dict keys" preserves order
set layernames [dict keys $layersrec]

# Run once forwards to make sure xpitch and ypitch are seeded with
# the values for the highest layer for which each one is defined.

for {set i 0} {$i < [llength $layernames]} {incr i} {
   set layername [lindex $layernames $i]
   set layerrec [dict get $layersrec $layername]
   catch {set xpitch [dict get $layerrec xpitch]}
   catch {set ypitch [dict get $layerrec ypitch]}
}

# Now run backwards, so that each layer gets xpitch and ypitch
# defined, taking the value from the closest layer above it, if
# it is not defined.

for {set i [- [llength $layernames] 1]} {$i >= 0} {incr i -1} {
   set layername [lindex $layernames $i]
   set layerrec [dict get $layersrec $layername]

   # Get layer's xpitch and ypitch, otherwise set it to the current value.
   if [catch {set xpitch [dict get $layerrec xpitch]}] {
      dict set layerrec xpitch $xpitch
   }
   if [catch {set ypitch [dict get $layerrec ypitch]}] {
      dict set layerrec ypitch $ypitch
   }

   # Write modified entry back to master dictionary
   dict set layersrec $layername $layerrec
}

close $fdef

# Expand the pin bounding box to contain all the core pins

if $debug {puts stdout "Pin area bounds: lly = $pinlly"}
dict for {pinname pinrec} $pinsrec {
    set pinx [dict get $pinrec x]
    set piny [dict get $pinrec y]
    if {$pinx < $pinllx} {set pinllx $pinx}
    if {$pinx > $pinurx} {set pinurx $pinx}
    if {$piny < $pinlly} {
	if $debug {puts stdout "Pin $pinname new area bounds: lly = $piny"}
	set pinlly $piny
    }
    if {$piny > $pinury} {set pinury $piny}
}

# Diagnostic
if $debug {
    puts stdout "Analysis of DEF file:"
    puts stdout "Layout bounding box:"
    puts stdout "($diellx $dielly) ($dieurx $dieury)"
    puts stdout ""
    puts stdout "Pin bounding box:"
    puts stdout "($pinllx $pinlly) ($pinurx $pinury)"
    puts stdout ""
    puts stdout "Pins:"
    dict for {pinname pinrec} $pinsrec {
	puts -nonewline stdout "pin=$pinname "
	dict for {key value} $pinrec {
	    puts -nonewline stdout "$key=$value "
	}
	puts stdout ""
    }
    puts stdout ""

    puts stdout "Specialnets:"
    dict for {pinname pinrec} $specialrec {
	puts -nonewline stdout "pin=$pinname "
	dict for {key value} $pinrec {
	    puts -nonewline stdout "$key=$value "
	}
	puts stdout ""
    }
    puts stdout ""

    puts stdout "Layers:"
    dict for {layername layerspec} $layersrec {
	puts -nonewline stdout "layer=$layername "
	dict for {key value} $layerspec {
	    puts -nonewline stdout "$key=$value "
	}
	puts stdout ""
    }
    puts stdout ""

    puts stdout "Components:"
    dict for {instname instpos} $compsrec {
	set x [lindex $instpos 0]
	set y [lindex $instpos 1]
	puts stdout "instance=$instname position=($x $y)"
    }
    puts stdout ""
}

#-----------------------------------------------------------------
# Use STEP size from TRACKS and existing pin bounding box to
# define available slots for pins on each side
#-----------------------------------------------------------------

# What layer is used for each side?  If one side has no pins, assume
# it uses the same layer as the opposite side.

dict for {pinname pinrec} $pinsrec {
    set pinx [dict get $pinrec x]
    set piny [dict get $pinrec y]
    set pinl [dict get $pinrec layer]
    if {$pinx == $pinllx} {
	set leftlayer $pinl
	if [catch {set rightlayer}] {set rightlayer $pinl}
    } elseif {$pinx == $pinurx} {
	set rightlayer $pinl
	if [catch {set leftlayer}] {set leftlayer $pinl}
    } elseif {$piny == $pinlly} {
	set botlayer $pinl
	if [catch {set toplayer}] {set toplayer $pinl}
    } elseif {$piny == $pinury} {
	set toplayer $pinl
	if [catch {set botlayer}] {set botlayer $pinl}
    }
}

if [catch {set layerspec [dict get $layersrec $leftlayer]}] {
    # Left and right sides unoccupied---set to a placeholder value
    set leftpitch 1
} else {
    set leftpitch [dict get $layerspec ypitch]
    # (Not used)
    set leftslots [/ [- $pinury $pinlly] $leftpitch]
    set leftbottrack [/ $pinlly $leftpitch]
}

if [catch {set layerspec [dict get $layersrec $rightlayer]}] {
    # Left and right sides unoccupied---set to a placeholder value
    set rightpitch 1
} else {
    set rightpitch [dict get $layerspec ypitch]
    # (Not used)
    set rightslots [/ [- $pinury $pinlly] $rightpitch]
    set rightbottrack [/ $pinlly $rightpitch]
}

if [catch {set layerspec [dict get $layersrec $botlayer]}] {
    # Top and bottom sides unoccupied---set to a placeholder value
    set botpitch 1
} else {
    set botpitch [dict get $layerspec xpitch]
    # (Not used)
    set botslots [/ [- $pinurx $pinllx] $botpitch]
    set botlefttrack [/ $pinllx $botpitch]
}

if [catch {set layerspec [dict get $layersrec $toplayer]}] {
    # Top and bottom sides unoccupied---set to a placeholder value
    set toppitch 1
} else {
    set toppitch [dict get $layerspec xpitch]
    # (Not used)
    set topslots [/ [- $pinurx $pinllx] $toppitch]
    set toplefttrack [/ $pinllx $toppitch]
}

# Diagnostic
if $debug {
    puts stdout "Slot pitches:"
    puts stdout ""
    puts stdout "Left   = $leftpitch"
    puts stdout "Right  = $rightpitch"
    puts stdout "Top    = $toppitch"
    puts stdout "Bottom = $botpitch"
    puts stdout ""
}

#--------------------------------------------------------------------
# Place existing pins in rows, either left to right or bottom to top
# Generate list with pin name and relevant axis position, then sort
# by position.  Also divide position by relevant pitch and take
# integer to get a track value rather than a physical distance.
#--------------------------------------------------------------------

set toprow {}
set botrow {}
set leftrow {}
set rightrow {}

dict for {pinname pinrec} $pinsrec {
    set pinx [dict get $pinrec x]
    set piny [dict get $pinrec y]
    set pinl [dict get $pinrec layer]

    # If a pin has more than one match to the pin bounds, then check
    # which side is closest to a die area bound.

    set matchleft false
    set matchright false
    set matchtop false
    set matchbottom false

    if {$pinx <= $pinllx} {
        set matchleft true
    }
    if {$pinx >= $pinurx} {
        set matchright true
    }
    if {$piny <= $pinlly} {
        set matchbottom true
    }
    if {$piny >= $pinury} {
        set matchtop true
    }

    # Corner cases, so to speak.
	
    if {$matchleft && $matchtop} {
	set leftdist [abs [- $diellx $pinx]]
	set topdist [abs [- $dieury $piny]]
	if {$topdist < $leftdist} {
	    set matchleft false
	} else {
	    set matchtop false
	}
    } elseif {$matchleft && $matchbottom} {
	set leftdist [abs [- $diellx $pinx]]
	set bottomdist [abs [- $dielly $piny]]
	if {$bottomdist < $leftdist} {
	    set matchleft false
	} else {
	    set matchbottom false
	}
    } elseif {$matchright && $matchtop} {
	set rightdist [abs [- $dieurx $pinx]]
	set topdist [abs [- $dieury $piny]]
	if {$topdist < $rightdist} {
	    set matchright false
	} else {
	    set matchtop false
	}
    } elseif {$matchright && $matchbottom} {
	set rightdist [abs [- $dieurx $pinx]]
	set bottomdist [abs [- $dielly $piny]]
	if {$bottomdist < $rightdist} {
	    set matchright false
	} else {
	    set matchbottom false
	}
    }

    if {$matchleft} {
	lappend leftrow [list $pinname [/ $piny $leftpitch]]
    } elseif {$matchright} {
	lappend rightrow [list $pinname [/ $piny $rightpitch]]
    } elseif {$matchbottom} {
	lappend botrow [list $pinname [/ $pinx $botpitch]]
    } elseif {$matchtop} {
	lappend toprow [list $pinname [/ $pinx $toppitch]]
    } else {
	# Check against DIEAREA bounds.  Determine whether to adjust pin bounds
        # to match the DIEAREA bound.  This corrects for problems where the
	# placement tool puts pins out-of-bounds.  Arrangepins will pull them
	# back in.

	if {$pinx <= $diellx} {
	    lappend leftrow [list $pinname [/ $piny $leftpitch]]
	    if {$pinx > $pinllx} {
		set pinllx $pinx
	    }
	} elseif {$pinx >= $dieurx} {
	    lappend rightrow [list $pinname [/ $piny $rightpitch]]
	    if {$pinx < $pinurx} {
		set pinurx $pinx
	    }
	} elseif {$piny <= $dielly} {
	    lappend botrow [list $pinname [/ $pinx $botpitch]]
	    if {$piny > $pinlly} {
		set pinlly $piny
	    }
	} elseif {$piny >= $dieury} {
	    lappend toprow [list $pinname [/ $pinx $toppitch]]
	    if {$piny < $pinury} {
		set pinury $piny
	    }
	} else {
	    puts stderr "Warning!  Pin $pinname did not match any border position!"
	    puts stderr "Pin area bounds are ($pinllx $pinlly) to ($pinurx $pinury)"
	    puts stderr "Pin $pinname position is ($pinx $piny)"
	    puts stderr ""
	}
    }
}

# Sort rows

set leftrow [lsort -integer -index 1 $leftrow]
set rightrow [lsort -integer -index 1 $rightrow]
set botrow [lsort -integer -index 1 $botrow]
set toprow [lsort -integer -index 1 $toprow]

# Diagnostic!
if $debug {
    puts stdout "Sorted rows:"
    puts stdout "Left row:"
    foreach entry $leftrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Right row:"
    foreach entry $rightrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Top row:"
    foreach entry $toprow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Bottom row:"
    foreach entry $botrow {
	puts stdout $entry
    }
    puts stdout ""
}

# Now for each pin in each row, find the positions that are inside
# the route target instances' bounding box, and append those
# positions to the list entry

set newleftrow {}
foreach entry $leftrow {
    set pinrec [dict get $pinsrec [lindex $entry 0]]
    if {![catch {set pinbbox [dict get $pinrec bbox]}]} {
	set pinbot [/ [lindex $pinbbox 1] $leftpitch]
	set pintop [/ [lindex $pinbbox 3] $leftpitch]
    } else {
	puts stdout "Warning: pin [lindex $entry 0] has no bounding box."
	set pinbot [- [/ [dict get $pinrec y] $leftpitch] 1]
	set pintop [+ [/ [dict get $pinrec y] $leftpitch] 1]
    }
    lappend newleftrow [concat $entry $pinbot $pintop]
}

set newrightrow {}
foreach entry $rightrow {
    set pinrec [dict get $pinsrec [lindex $entry 0]]
    if {![catch {set pinbbox [dict get $pinrec bbox]}]} {
	set pinbot [/ [lindex $pinbbox 1] $rightpitch]
	set pintop [/ [lindex $pinbbox 3] $rightpitch]
    } else {
	puts stdout "Warning: pin [lindex $entry 0] has no bounding box."
	set pinbot [- [/ [dict get $pinrec y] $rightpitch] 1]
	set pintop [+ [/ [dict get $pinrec y] $rightpitch] 1]
    }
    lappend newrightrow [concat $entry $pinbot $pintop]
}

set newtoprow {}
foreach entry $toprow {
    set pinrec [dict get $pinsrec [lindex $entry 0]]
    if {![catch {set pinbbox [dict get $pinrec bbox]}]} {
	set pinleft [/ [lindex $pinbbox 0] $toppitch]
	set pinright [/ [lindex $pinbbox 2] $toppitch]
    } else {
	puts stdout "Warning: pin [lindex $entry 0] has no bounding box."
	set pinleft [- [/ [dict get $pinrec x] $toppitch] 1]
	set pinright [+ [/ [dict get $pinrec x] $toppitch] 1]
    }
    lappend newtoprow [concat $entry $pinleft $pinright]
}

set newbotrow {}
foreach entry $botrow {
    set pinrec [dict get $pinsrec [lindex $entry 0]]
    if {![catch {set pinbbox [dict get $pinrec bbox]}]} {
	set pinleft [/ [lindex $pinbbox 0] $botpitch]
	set pinright [/ [lindex $pinbbox 2] $botpitch]
    } else {
	puts stdout "Warning: pin [lindex $entry 0] has no bounding box."
	set pinleft [- [/ [dict get $pinrec x] $botpitch] 1]
	set pinright [+ [/ [dict get $pinrec x] $botpitch] 1]
    }
    lappend newbotrow [concat $entry $pinleft $pinright]
}

set topmax [/ $pinurx $toppitch]
set topmin [/ $pinllx $toppitch]
set botmax [/ $pinurx $botpitch]
set botmin [/ $pinllx $botpitch]
set leftmax [/ $pinury $leftpitch]
set leftmin [/ $pinlly $leftpitch]
set rightmax [/ $pinury $rightpitch]
set rightmin [/ $pinlly $rightpitch]

if $debug {
    puts stdout "Pin slots:"
    puts stdout "  Top, $topmin to $topmax"
    puts stdout "  Bottom, $botmin to $botmax"
    puts stdout "  Left, $leftmin to $leftmax"
    puts stdout "  Right, $rightmin to $rightmax"
}

# Diagnostic!
if $debug {
    puts stdout "Left row:"
    foreach entry $newleftrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Right row:"
    foreach entry $newrightrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Top row:"
    foreach entry $newtoprow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "Bottom row:"
    foreach entry $newbotrow {
	puts stdout $entry
    }
    puts stdout ""
}

#-----------------------------------------------------------------
# Find the X ranges along top and bottom where we want to avoid
# placing pins due to the presence of power supply stripe posts,
# which tend to get in the way of routes.
#-----------------------------------------------------------------

set xbranges {}
set xtranges {}
dict for {pinname pinrec} $specialrec {
    catch {
	set locxranges [dict get $pinrec xranges]
	foreach xpair $locxranges {
	    set xblow [/ [lindex $xpair 0] $botpitch]
	    set xbhigh [/ [lindex $xpair 1] $botpitch]
	    set xtlow [/ [lindex $xpair 0] $toppitch]
	    set xthigh [/ [lindex $xpair 1] $toppitch]
	    lappend xbranges [list $xblow $xbhigh]
	    lappend xtranges [list $xtlow $xthigh]
	}
    }
}

set xbranges [lsort -integer -index 0 $xbranges]
set xtranges [lsort -integer -index 0 $xtranges]

if $debug {
    puts stdout "X pin slot forbidden ranges (bottom):"
    foreach xpair $xbranges {
	set xlow [lindex $xpair 0]
	set xhigh [lindex $xpair 1]
	puts stdout "($xlow $xhigh)"
    }
}

# For speed it is worth making a mask array of the forbidden ranges
# Each value is either -1 for no obstruction, or the value is the
# index of the X-range pair, so that the pair can be looked up
# quickly to find the bottom and top of the range.  Note that the
# mask position is offset by $topmin or $botmin.

set i $botmin
set j 0
foreach xpair $xbranges {
    set xlow [lindex $xpair 0]
    set xhigh [lindex $xpair 1]

    for {} {$i < $xlow} {incr i} {
	lappend xbmask -1 
    }
    for {} {$i <= $xhigh} {incr i} {
	lappend xbmask $j
    }
    incr j
}
for {} {$i <= $botmax} {incr i} {
    lappend xbmask -1 
}

if $debug {
    puts stdout "xbmask ="
    for {set i 0} {$i < [llength $xbmask]} {incr i} {
        set mval [lindex $xbmask $i]
	if {$mval == -1} {
	    puts stdout "$i = $mval"
	} else {
	    puts stdout "$i = $mval ([lindex $xbranges $mval])"
	}
    }
}

set i $topmin
set j 0
foreach xpair $xtranges {
    set xlow [lindex $xpair 0]
    set xhigh [lindex $xpair 1]

    for {} {$i < $xlow} {incr i} {
	lappend xtmask -1
    }
    for {} {$i <= $xhigh} {incr i} {
	lappend xtmask $j
    }
    incr j
}
for {} {$i <= $topmax} {incr i} {
    lappend xtmask -1
}

#-----------------------------------------------------------------
# Pin adjustment
#-----------------------------------------------------------------

puts stdout "Recalculating pin positions"
set iterations 1	;# More than one iteration has little effect

set testleftrow $newleftrow
for {set i 0} {$i < $iterations} {incr i} {
    for {set j 0} {$j < [llength $testleftrow]} {incr j} {

	# Each pin has a position and a range of positions that are
	# inside the bounding box of the cells to which the pin connects.
	# Adjust pins to keep all pins as close to their bounding boxes
	# as possible while maintaining the order of the pins, and while
	# keeping pins two tracks away (preferably).  This is an iterative
	# process, so run 20 times (ad hoc value).

	# To do:  If pins are too crowded, move up two metal layers if
	# possible.

	set below [lindex $testleftrow [- $j 1]]
	if {$below == {}} {
	    set lbound 0
	} else {
	    set lbound [+ [lindex $below 1] 2]
	}
	set above [lindex $testleftrow [+ $j 1]]
	if {$above == {}} {
	    set ubound $leftmax
	} else {
	    set ubound [- [lindex $above 1] 2]
	}

	# By expanding to a spacing of 2 grids per pin, occasionally
	# the bounds get squeezed.  If ubound is less than lbound,
	# then renumber everything downward.
	
	if {$ubound < $lbound} {
	    set nval [- $ubound 2]
	    set lbound $ubound
	    for {set k [- $j 1]} {$k >= 0} {incr k -1} {
		set below [lindex $testleftrow $k]
	        set bval [lindex $below 1]
		if {$bval <= $nval} {
		    break
		}
		set below [lreplace $below 1 1 $nval]
		set testleftrow [lreplace $testleftrow $k $k $below]
		set nval [- $nval 2]
	    }
	}

	set here  [lindex $testleftrow $j]
	set rangel [lindex $here 2]
	set rangeu [lindex $here 3]
	set curval [lindex $here 1]

	# If the current position is inside the bounds, do nothing.
	# Otherwise, move to the nearest range bound if the nearest
	# range bound is between the pin and the next pin.  If not,
	# move the pin as close to the range bound as possible while
	# maintaining a distance of 2 from the next pin.

	if {$curval < $rangel} {
	    if {$rangel < $ubound} {
		if {$rangel > $lbound} {
		    set newval $rangel
		} else {
		    set newval $lbound
		}
	    } else {
		set newval $ubound
	    }
	    set newentry [lreplace $here 1 2 $newval [- $rangel 4]]
	    set testleftrow [lreplace $testleftrow $j $j $newentry]
	} elseif {$curval > $rangeu} {
	    if {$rangeu > $lbound} {
		if {$rangeu < $ubound} {
		    set newval $rangeu
		} else {
		    set newval $ubound
		}
	    } else {
		set newval $lbound
	    }
	    set newentry [lreplace $here 1 3 $newval $rangel [+ $rangeu 4]]
	    set testleftrow [lreplace $testleftrow $j $j $newentry]
	}
    }
}

# Same procedure for the other sides.

set testrightrow $newrightrow
for {set i 0} {$i < $iterations} {incr i} {
    for {set j 0} {$j < [llength $testrightrow]} {incr j} {

	set below [lindex $testrightrow [- $j 1]]
	if {$below == {}} {
	    set lbound 0
	} else {
	    set lbound [+ [lindex $below 1] 2]
	}
	set above [lindex $testrightrow [+ $j 1]]
	if {$above == {}} {
	    set ubound $rightmax
	} else {
	    set ubound [- [lindex $above 1] 2]
	}

	if {$ubound < $lbound} {
	    set nval [- $ubound 2]
	    set lbound $ubound
	    for {set k [- $j 1]} {$k >= 0} {incr k -1} {
		set below [lindex $testrightrow $k]
	        set bval [lindex $below 1]
		if {$bval <= $nval} {
		    break
		}
		set below [lreplace $below 1 1 $nval]
		set testrightrow [lreplace $testrightrow $k $k $below]
		set nval [- $nval 2]
	    }
	}

	set here  [lindex $testrightrow $j]
	set rangel [lindex $here 2]
	set rangeu [lindex $here 3]
	set curval [lindex $here 1]

	if {$curval < $rangel} {
	    if {$rangel < $ubound} {
		if {$rangel > $lbound} {
		    set newval $rangel
		} else {
		    set newval $lbound
		}
	    } else {
		set newval $ubound
	    }
	    set newentry [lreplace $here 1 2 $newval [- $rangel 4]]
	    set testrightrow [lreplace $testrightrow $j $j $newentry]
	} elseif {$curval > $rangeu} {
	    if {$rangeu > $lbound} {
		if {$rangeu < $ubound} {
		    set newval $rangeu
		} else {
		    set newval $ubound
		}
	    } else {
		set newval $lbound
	    }
	    set newentry [lreplace $here 1 3 $newval $rangel [+ $rangeu 4]]
	    set testrightrow [lreplace $testrightrow $j $j $newentry]
	}
    }
}

# NOTE:  Top and bottom row calculations differ from left and right
# by checking the position of power posts in lists xtranges and
# xbranges, and avoiding those values

set testtoprow $newtoprow
for {set i 0} {$i < $iterations} {incr i} {
    for {set j 0} {$j < [llength $testtoprow]} {incr j} {

	set below [lindex $testtoprow [- $j 1]]
	if {$below == {}} {
	    set lbound 0
	} else {
	    set lbound [+ [lindex $below 1] 2]
	}
	set above [lindex $testtoprow [+ $j 1]]
	if {$above == {}} {
	    set ubound $topmax
	} else {
	    set ubound [- [lindex $above 1] 2]
	}
	if {$ubound < $lbound} {
	    set nval [- $ubound 2]
	    set lbound $ubound
	    for {set k [- $j 1]} {$k >= 0} {incr k -1} {
		set below [lindex $testtoprow $k]
	        set bval [lindex $below 1]
		if {$bval <= $nval} {
		    break
		}
		set below [lreplace $below 1 1 $nval]
		set testtoprow [lreplace $testtoprow $k $k $below]
		set nval [- $nval 2]
	    }
	}

	set here  [lindex $testtoprow $j]
	set rangel [lindex $here 2]
	set rangeu [lindex $here 3]
	set curval [lindex $here 1]

	if {$curval < $rangel} {
	    if {$rangel < $ubound} {
		if {$rangel > $lbound} {
		    set newval $rangel
		} else {
		    set newval $lbound
		}
	    } else {
		set newval $ubound
	    }
	    set newentry [lreplace $here 1 2 $newval [- $rangel 4]]
	    set testtoprow [lreplace $testtoprow $j $j $newentry]
	} elseif {$curval > $rangeu} {
	    if {$rangeu > $lbound} {
		if {$rangeu < $ubound} {
		    set newval $rangeu
		} else {
		    set newval $ubound
		}
	    } else {
		set newval $lbound
	    }
	    set newentry [lreplace $here 1 3 $newval $rangel [+ $rangeu 4]]
	    set testtoprow [lreplace $testtoprow $j $j $newentry]
	} else {
	    set newval $curval
	}

        # Check for power post conflicts and move up if needed
	set rindex [- $newval $topmin]
	set pindex [lindex $xtmask $rindex]
	if {$pindex >= 0} {
	    set pair [lindex $xtranges $pindex]
	    set newval [+ [lindex $pair 1] 1]
	    set newentry [lreplace $here 1 1 $newval]
	    set testtoprow [lreplace $testtoprow $j $j $newentry]

	    # Move everything above further up if needed.
	    set nval [+ $newval 2]
	    for {set k [+ $j 1]} {$k < [llength $testtoprow]} {incr k} {
		set above [lindex $testtoprow $k]
	        set aval [lindex $above 1]
		if {$aval >= $nval} {
		    break
		}
		set above [lreplace $above 1 1 $nval]
		set testtoprow [lreplace $testtoprow $k $k $above]
		set nval [+ $nval 2]
	    }
	}
    }
}

set testbotrow $newbotrow
for {set i 0} {$i < $iterations} {incr i} {
    for {set j 0} {$j < [llength $testbotrow]} {incr j} {

	set below [lindex $testbotrow [- $j 1]]
	if {$below == {}} {
	    set lbound 0
	} else {
	    set lbound [+ [lindex $below 1] 2]
	}
	set above [lindex $testbotrow [+ $j 1]]
	if {$above == {}} {
	    set ubound $botmax
	} else {
	    set ubound [- [lindex $above 1] 2]
	}
	if {$ubound < $lbound} {
	    set nval [- $ubound 2]
	    set lbound $ubound
	    for {set k [- $j 1]} {$k >= 0} {incr k -1} {
		set below [lindex $testbotrow $k]
	        set bval [lindex $below 1]
		if {$bval <= $nval} {
		    break
		}
		set below [lreplace $below 1 1 $nval]
		set testbotrow [lreplace $testbotrow $k $k $below]
		set nval [- $nval 2]
	    }
	}

	set here  [lindex $testbotrow $j]
	set rangel [lindex $here 2]
	set rangeu [lindex $here 3]
	set curval [lindex $here 1]

	if {$curval < $rangel} {
	    if {$rangel < $ubound} {
		if {$rangel > $lbound} {
		    set newval $rangel
		} else {
		    set newval $lbound
		}
	    } else {
		set newval $ubound
	    }
	    set newentry [lreplace $here 1 2 $newval [- $rangel 4]]
	    set testbotrow [lreplace $testbotrow $j $j $newentry]
	} elseif {$curval > $rangeu} {
	    if {$rangeu > $lbound} {
		if {$rangeu < $ubound} {
		    set newval $rangeu
		} else {
		    set newval $ubound
		}
	    } else {
		set newval $lbound
	    }
	    set newentry [lreplace $here 1 3 $newval $rangel [+ $rangeu 4]]
	    set testbotrow [lreplace $testbotrow $j $j $newentry]
	} else {
	    set newval $curval
	}

        # Check for power post conflicts and move up if needed
	set rindex [- $newval $botmin]
	set pindex [lindex $xbmask $rindex]
	if {$pindex >= 0} {
	    set pair [lindex $xbranges $pindex]
	    set newval [+ [lindex $pair 1] 1]
	    set newentry [lreplace $here 1 1 $newval]
	    set testbotrow [lreplace $testbotrow $j $j $newentry]

	    # Move everything above further up if needed.
	    set nval [+ $newval 2]
	    for {set k [+ $j 1]} {$k < [llength $testbotrow]} {incr k} {
		set above [lindex $testbotrow $k]
	        set aval [lindex $above 1]
		if {$aval >= $nval} {
		    break
		}
		set above [lreplace $above 1 1 $nval]
		set testbotrow [lreplace $testbotrow $k $k $above]
		set nval [+ $nval 2]
	    }
	}
    }
}

# Diagnostic!
if $debug {
    puts stdout "New left row:"
    foreach entry $testleftrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "New right row:"
    foreach entry $testrightrow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "New top row:"
    foreach entry $testtoprow {
	puts stdout $entry
    }
    puts stdout ""

    puts stdout "New bottom row:"
    foreach entry $testbotrow {
	puts stdout $entry
    }
    puts stdout ""
}

#-------------------------------------------------------------------
# Recalculate X and Y position for each pin and put back in pinsrec
#-------------------------------------------------------------------

foreach entry $testleftrow {
    set pinname [lindex $entry 0]
    set sloty [lindex $entry 1]
    set piny [* $sloty $leftpitch]

    set pinrec [dict get $pinsrec $pinname]
    dict set pinrec y $piny
    dict set pinsrec $pinname $pinrec
}

foreach entry $testrightrow {
    set pinname [lindex $entry 0]
    set sloty [lindex $entry 1]
    set piny [* $sloty $rightpitch]

    set pinrec [dict get $pinsrec $pinname]
    dict set pinrec y $piny
    dict set pinsrec $pinname $pinrec
}

foreach entry $testtoprow {
    set pinname [lindex $entry 0]
    set slotx [lindex $entry 1]
    set pinx [* $slotx $toppitch]

    set pinrec [dict get $pinsrec $pinname]
    dict set pinrec x $pinx
    dict set pinsrec $pinname $pinrec
}

foreach entry $testbotrow {
    set pinname [lindex $entry 0]
    set slotx [lindex $entry 1]
    set pinx [* $slotx $botpitch]

    set pinrec [dict get $pinsrec $pinname]
    dict set pinrec x $pinx
    dict set pinsrec $pinname $pinrec
}

#-----------------------------------------------------------------
# Reopen the DEF file for reading
set fdef [open $defname r]

# Open the output DEF file for writing
if [catch {open $defoutname w} fmod] {
    puts stderr "Error: can't open file $defoutname for output"
    return
}

puts stdout "Writing DEF file ${defoutname}. . ."

#-----------------------------------------------------------------
# Read the DEF file a second time and output the adjusted pin list
# of each row
#-----------------------------------------------------------------

while {[gets $fdef line] >= 0} {
    if [regexp {[ \t]*#} $line lmatch] {
	puts $fmod $line
    } elseif $inpins {
	if [regexp {[ \t]*END[ \t]+PINS} $line lmatch] {
	    puts $fmod $line
	    set inpins false
	}
    } elseif [regexp {[ \t]*PINS[ \t]+([^ \t]+)[ \t]*;} $line lmatch number] {
	set inpins true
	puts $fmod $line
	dict for {pinname pinrec} $specialrec {
	    set netname [dict get $pinrec net]
	    set layer [dict get $pinrec layer]
	    set llx [dict get $pinrec llx]
	    set lly [dict get $pinrec lly]
	    set urx [dict get $pinrec urx]
	    set ury [dict get $pinrec ury]

	    set pinx [dict get $pinrec x]
	    set piny [dict get $pinrec y]
	    set pino [dict get $pinrec o]
	    puts $fmod "- $pinname + NET $netname"
	    puts $fmod "  + LAYER $layer ( $llx $lly ) ( $urx $ury )"
	    puts $fmod "  + PLACED ( $pinx $piny ) $pino ;"
	}
	dict for {pinname pinrec} $pinsrec {
	    set netname [dict get $pinrec net]
	    set layer [dict get $pinrec layer]
	    set llx [dict get $pinrec llx]
	    set lly [dict get $pinrec lly]
	    set urx [dict get $pinrec urx]
	    set ury [dict get $pinrec ury]
	    if {$llx == 0 && $urx == 1 && $lly == 0 && $ury == 1} {
		# Change unit size pins to be a square of the route
		# width.  This prevents unconnected pins from losing
		# all underlying metal when read into a layout tool.
		set layerrec [dict get $layersrec $layer]
		if {[dict exists $layerrec width]} {
		    set width [dict get $layerrec width]
		    set hwidth [expr {int([/ [* $units $width] 2])}]
		    set llx -$hwidth
		    set lly -$hwidth
		    set urx  $hwidth
		    set ury  $hwidth
		}
	    }
	    set pinx [dict get $pinrec x]
	    set piny [dict get $pinrec y]
	    set pino [dict get $pinrec o]
	    puts $fmod "- $pinname + NET $netname"
	    puts $fmod "  + LAYER $layer ( $llx $lly ) ( $urx $ury )"
	    puts $fmod "  + PLACED ( $pinx $piny ) $pino ;"
	}
    } else {
	puts $fmod $line
    }
}

close $fdef
close $fmod

#-----------------------------------------------------------------
puts stdout "Done with arrangepins.tcl"
exit 0