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 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
|
/*--------------------------------------------------------------*/
/* 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 <stdlib.h>
#define MAZE
#include "qrouter.h"
#include "qconfig.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;
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)) {
for (lay = 0; lay < Num_layers; lay++)
for (x = 0; x < NumChannelsX[lay]; x++)
for (y = 0; y < NumChannelsY[lay]; y++)
if ((Obs[lay][OGRID(x, y, lay)] & NETNUM_MASK) == netnum) {
Pr = &Obs2[lay][OGRID(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 = &Obs2[lay][OGRID(x, y, lay)];
if (Pr->flags & PR_TARGET) {
if (Pr->flags & PR_PROCESSED) {
Pr->flags &= ~PR_PROCESSED;
gpoint = (POINT)malloc(sizeof(struct point_));
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 = &Obs2[lay][OGRID(x, y, lay)];
if (Pr->flags & PR_TARGET) {
if (Pr->flags & PR_PROCESSED) {
Pr->flags &= ~PR_PROCESSED;
gpoint = (POINT)malloc(sizeof(struct point_));
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = *pushlist;
*pushlist = gpoint;
}
}
}
}
}
}
/*--------------------------------------------------------------*/
/* clear_target_node -- */
/* */
/* Remove PR_TARGET flags from all points belonging to a node */
/*--------------------------------------------------------------*/
int clear_target_node(NODE node)
{
int x, y, lay;
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 (Nodeloc[lay][OGRID(x, y, lay)] == (NODE)NULL)
continue;
Pr = &Obs2[lay][OGRID(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 (Nodesav[lay][OGRID(x, y, lay)] == (NODE)NULL ||
Nodesav[lay][OGRID(x, y, lay)] != node)
continue;
Pr = &Obs2[lay][OGRID(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 = &Obs2[lay][OGRID(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 = &Obs2[lay][OGRID(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". Then follow all routes */
/* connected to "node", updating their positions. Where those */
/* routes connect to other nodes, repeat recursively. */
/* */
/* 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 of 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, k, obsnet = 0;
int result = 0;
u_char found_one = (u_char)0;
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))
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 = &Obs2[lay][OGRID(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) {
result = 1; // Node is already connected!
}
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;
// push this point on the stack to process
if (pushlist != NULL) {
gpoint = (POINT)malloc(sizeof(struct point_));
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = *pushlist;
*pushlist = gpoint;
}
found_one = (u_char)1;
// 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.
// if (Nodeloc[lay][OGRID(x, y, lay)] != (NODE)NULL &&
// Nodeloc[lay][OGRID(x, y, lay)] != node)
// continue;
if (Nodesav[lay][OGRID(x, y, lay)] == (NODE)NULL ||
Nodesav[lay][OGRID(x, y, lay)] != node)
continue;
Pr = &Obs2[lay][OGRID(x, y, lay)];
if (Pr->flags & PR_SOURCE) {
result = 1; // Node is already connected!
}
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) {
gpoint = (POINT)malloc(sizeof(struct point_));
gpoint->x1 = x;
gpoint->y1 = y;
gpoint->layer = lay;
gpoint->next = *pushlist;
*pushlist = gpoint;
}
found_one = (u_char)1;
// 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 == (u_char)0)) {
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 = &Obs2[lay][OGRID(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 = &Obs2[lay][OGRID(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, k;
int result = 0;
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 = &Obs2[lay][OGRID(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) {
gpoint = (POINT)malloc(sizeof(struct point_));
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.
n2 = Nodeloc[lay][OGRID(x, y, lay)];
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 all routes of a net, and set their routed positions */
/* to SOURCE in Obs2[] */
/*--------------------------------------------------------------*/
int set_routes_to_net(NET net, int newflags, POINT *pushlist, SEG bbox,
u_char stage)
{
ROUTE rt;
int result;
for (rt = net->routes; rt; rt = rt->next)
result = set_route_to_net(net, rt, newflags, pushlist, bbox, stage);
return result;
}
/*--------------------------------------------------------------*/
/* Find nets that are colliding with the given net "net", and */
/* create and return a list of them. */
/*--------------------------------------------------------------*/
NETLIST find_colliding(NET net)
{
NETLIST nl = (NETLIST)NULL, cnl;
NET fnet;
ROUTE rt;
SEG seg;
int lay, i, x, y, orignet;
/* Scan the routed points for recorded collisions. */
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 = Obs[lay][OGRID(x, y, lay)] & NETNUM_MASK;
if (orignet != net->netnum) {
/* Route collision. Save this net if it is */
/* not already in the list of colliding nets. */
for (cnl = nl; cnl; cnl = cnl->next) {
if (cnl->net->netnum == orignet)
break;
}
if (cnl == NULL) {
for (i = 0; i < Numnets; i++) {
fnet = Nlnets[i];
if (fnet->netnum == orignet) {
cnl = (NETLIST)malloc(sizeof(struct netlist_));
cnl->net = fnet;
cnl->next = nl;
nl = cnl;
break;
}
}
}
}
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 nets: ",
net->netname);
for (cnl = nl; cnl; cnl = cnl->next) {
Fprintf(stdout, "%s ", cnl->net->netname);
}
Fprintf(stdout, "\n");
}
return nl;
}
/*--------------------------------------------------------------*/
/* ripup_net --- */
/* */
/* Rip up the entire network located at position x, y, lay. */
/* */
/* If argument "restore" is TRUE, then at each node, restore */
/* the crossover cost by attaching the node back to the Nodeloc */
/* array. */
/*--------------------------------------------------------------*/
u_char ripup_net(NET net, u_char restore)
{
int thisnet, oldnet, x, y, lay, dir;
double sreq;
NODE node;
ROUTE rt;
SEG seg;
DPOINT ntap;
thisnet = net->netnum;
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;
while (1) {
oldnet = Obs[lay][OGRID(x, y, lay)] & NETNUM_MASK;
if ((oldnet > 0) && (oldnet < MAXNETNUM)) {
if (oldnet != thisnet) {
Fprintf(stderr, "Error: position %d %d layer %d has net "
"%d not %d!\n", x, y, lay, oldnet, thisnet);
return FALSE; // Something went wrong
}
// Reset the net number to zero along this route for
// every point that is not a node tap. Points that
// were routed over obstructions to reach off-grid
// taps are returned to obstructions.
if (Nodesav[lay][OGRID(x, y, lay)] == (NODE)NULL) {
dir = Obs[lay][OGRID(x, y, lay)] & PINOBSTRUCTMASK;
if (dir == 0)
Obs[lay][OGRID(x, y, lay)] = 0;
else
Obs[lay][OGRID(x, y, lay)] = NO_NET | dir;
}
// Routes which had blockages added on the sides due
// to spacing constraints have (NO_NET | ROUTED_NET)
// set; these flags should be removed.
if (needblock[lay] & (ROUTEBLOCKX | VIABLOCKX)) {
if ((x > 0) && ((Obs[lay][OGRID(x - 1, y, lay)] &
(NO_NET | ROUTED_NET)) == (NO_NET | ROUTED_NET)))
Obs[lay][OGRID(x - 1, y, lay)] &= ~(NO_NET | ROUTED_NET);
else if ((x < NumChannelsX[lay] - 1) &&
((Obs[lay][OGRID(x + 1, y, lay)] &
(NO_NET | ROUTED_NET)) == (NO_NET | ROUTED_NET)))
Obs[lay][OGRID(x + 1, y, lay)] &= ~(NO_NET | ROUTED_NET);
}
if (needblock[lay] & (ROUTEBLOCKY | VIABLOCKY)) {
if ((y > 0) && ((Obs[lay][OGRID(x, y - 1, lay)] &
(NO_NET | ROUTED_NET)) == (NO_NET | ROUTED_NET)))
Obs[lay][OGRID(x, y - 1, lay)] &= ~(NO_NET | ROUTED_NET);
else if ((y < NumChannelsY[lay] - 1) &&
((Obs[lay][OGRID(x, y + 1, lay)] &
(NO_NET | ROUTED_NET)) == (NO_NET | ROUTED_NET)))
Obs[lay][OGRID(x, y + 1, lay)] &= ~(NO_NET | ROUTED_NET);
}
}
// This break condition misses via ends, but those are
// terminals and don't get ripped out.
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--;
}
}
}
}
// For each net node tap, restore the node pointer on Nodeloc so
// that crossover costs are again applied to routes over this
// node tap.
if (restore != 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;
Nodeloc[lay][OGRID(x, y, lay)] = Nodesav[lay][OGRID(x, y, lay)];
}
}
}
/* Remove all routing information from this net */
while (net->routes) {
rt = net->routes;
net->routes = rt->next;
while (rt->segments) {
seg = rt->segments->next;
free(rt->segments);
rt->segments = seg;
}
free(rt);
}
return TRUE;
}
/*--------------------------------------------------------------*/
/* eval_pt - evaluate cost to get from given point to */
/* current point. Current point is passed in "ept", and */
/* the direction from the new point to the current point */
/* is indicated by "flags". */
/* */
/* ONLY consider the cost of the single step itself. */
/* */
/* If "stage" is nonzero, then this is a second stage */
/* routing, where we should consider other nets to be a */
/* high cost to short to, rather than a blockage. This */
/* will allow us to finish the route, but with a minimum */
/* number of collisions with other nets. Then, we rip up */
/* those nets, add them to the "failed" stack, and re- */
/* route this one. */
/* */
/* ARGS: none */
/* RETURNS: 1 if node needs to be (re)processed, 0 if not. */
/* SIDE EFFECTS: none (get this right or else) */
/*--------------------------------------------------------------*/
int eval_pt(GRIDP *ept, u_char flags, u_char stage)
{
int thiscost = 0;
NODE node;
NETLIST nl;
PROUTE *Pr, *Pt;
GRIDP newpt;
newpt = *ept;
switch (flags) {
case PR_PRED_N:
newpt.y--;
break;
case PR_PRED_S:
newpt.y++;
break;
case PR_PRED_E:
newpt.x--;
break;
case PR_PRED_W:
newpt.x++;
break;
case PR_PRED_U:
newpt.lay--;
break;
case PR_PRED_D:
newpt.lay++;
break;
}
Pr = &Obs2[newpt.lay][OGRID(newpt.x, newpt.y, newpt.lay)];
if (!(Pr->flags & (PR_COST | PR_SOURCE))) {
// 2nd stage allows routes to cross existing routes
if (stage && (Pr->prdata.net < MAXNETNUM)) {
// if (Nodeloc[newpt.lay][OGRID(newpt.x, newpt.y, newpt.lay)] != NULL)
if (Nodesav[newpt.lay][OGRID(newpt.x, newpt.y, newpt.lay)] != NULL)
return 0; // But cannot route over terminals!
// Is net k in the "noripup" list? If so, don't route it */
for (nl = CurNet->noripup; nl; nl = nl->next) {
if (nl->net->netnum == Pr->prdata.net)
return 0;
}
// In case of a collision, we change the grid point to be routable
// but flag it as a point of collision so we can later see what
// were the net numbers of the interfering routes by cross-referencing
// the Obs[][] array.
Pr->flags |= (PR_CONFLICT | PR_COST);
Pr->prdata.cost = MAXRT;
thiscost = ConflictCost;
}
else
return 0; // Position is not routeable
}
// Compute the cost to step from the current point to the new point.
// "BlockCost" is used if the node has only one point to connect to,
// so that routing over it could block it entirely.
if (newpt.lay > 0) {
if ((node = Nodeloc[newpt.lay - 1][OGRID(newpt.x, newpt.y, newpt.lay - 1)])
!= (NODE)NULL) {
Pt = &Obs2[newpt.lay - 1][OGRID(newpt.x, newpt.y, newpt.lay - 1)];
if (!(Pt->flags & PR_TARGET) && !(Pt->flags & PR_SOURCE)) {
if (node->taps && (node->taps->next == NULL))
thiscost += BlockCost; // Cost to block out a tap
else if (node->taps == NULL) {
if (node->extend != NULL) {
if (node->extend->next == NULL)
// Node has only one extended access point: Try
// very hard to avoid routing over it
thiscost += 10 * BlockCost;
else
thiscost += BlockCost;
}
// If both node->taps and node->extend are NULL, then
// the node has no access and will never be routed, so
// don't bother costing it.
}
else
thiscost += XverCost; // Cross-under cost
}
}
}
if (newpt.lay < Num_layers - 1) {
if ((node = Nodeloc[newpt.lay + 1][OGRID(newpt.x, newpt.y, newpt.lay + 1)])
!= (NODE)NULL) {
Pt = &Obs2[newpt.lay + 1][OGRID(newpt.x, newpt.y, newpt.lay + 1)];
if (!(Pt->flags & PR_TARGET) && !(Pt->flags & PR_SOURCE)) {
if (node->taps && (node->taps->next == NULL))
thiscost += BlockCost; // Cost to block out a tap
else
thiscost += XverCost; // Cross-over cost
}
}
}
if (ept->lay != newpt.lay) thiscost += ViaCost;
if (ept->x != newpt.x) thiscost += (Vert[newpt.lay] * JogCost +
(1 - Vert[newpt.lay]) * SegCost);
if (ept->y != newpt.y) thiscost += (Vert[newpt.lay] * SegCost +
(1 - Vert[newpt.lay]) * JogCost);
// Add the cost to the cost of the original position
thiscost += ept->cost;
// Replace node information if cost is minimum
if (Pr->flags & PR_CONFLICT)
thiscost += ConflictCost; // For 2nd stage routes
if (thiscost < Pr->prdata.cost) {
Pr->flags &= ~PR_PRED_DMASK;
Pr->flags |= flags;
Pr->prdata.cost = thiscost;
Pr->flags &= ~PR_PROCESSED; // Need to reprocess this node
if (Verbose > 3) {
Fprintf(stdout, "New cost %d at (%d %d %d)\n", thiscost,
newpt.x, newpt.y, newpt.lay);
}
return 1;
}
return 0; // New position did not get a lower cost
} /* eval_pt() */
/*------------------------------------------------------*/
/* writeback_segment() --- */
/* */
/* Copy information from a single segment back */
/* the Obs[] array. */
/* */
/* NOTE: "needblock" is used to handle */
/* cases where the existence of a route prohibits any */
/* routing on adjacent tracks on the same plane due to */
/* DRC restrictions (i.e., metal is too wide for single */
/* track spacing). Be sure to check the value of */
/* adjacent positions in Obs against the mask */
/* NETNUM_MASK, because NETNUM_MASK includes NO_NET. */
/* By replacing only empty and routable positions with */
/* the unique flag combination NO_NET | ROUTED_NET, it */
/* is possible to detect and remove the same if that */
/* net is ripped up, without touching any position */
/* originally marked NO_NET. */
/* */
/* Another NOTE: Tap offsets can cause the position */
/* in front of the offset to be unroutable. So if the */
/* segment is on a tap offset, mark the position in */
/* front as unroutable. If the segment neighbors an */
/* offset tap, then mark the tap unroutable. */
/*------------------------------------------------------*/
void writeback_segment(SEG seg, int netnum)
{
double dist;
int i, layer;
u_int sobs;
NODE node;
if (seg->segtype == ST_VIA) {
Obs[seg->layer + 1][OGRID(seg->x1, seg->y1, seg->layer + 1)] = netnum;
if (needblock[seg->layer + 1] & VIABLOCKX) {
if ((seg->x1 < (NumChannelsX[seg->layer + 1] - 1)) &&
(Obs[seg->layer + 1][OGRID(seg->x1 + 1, seg->y1, seg->layer + 1)]
& NETNUM_MASK) == 0)
Obs[seg->layer + 1][OGRID(seg->x1 + 1, seg->y1, seg->layer + 1)] =
(NO_NET | ROUTED_NET);
if ((seg->x1 > 0) &&
(Obs[seg->layer + 1][OGRID(seg->x1 - 1, seg->y1, seg->layer + 1)]
& NETNUM_MASK) == 0)
Obs[seg->layer + 1][OGRID(seg->x1 - 1, seg->y1, seg->layer + 1)] =
(NO_NET | ROUTED_NET);
}
if (needblock[seg->layer + 1] & VIABLOCKY) {
if ((seg->y1 < (NumChannelsY[seg->layer + 1] - 1)) &&
(Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 + 1, seg->layer + 1)]
& NETNUM_MASK) == 0)
Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 + 1, seg->layer + 1)] =
(NO_NET | ROUTED_NET);
if ((seg->y1 > 0) &&
(Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 - 1, seg->layer + 1)]
& NETNUM_MASK) == 0)
Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 - 1, seg->layer + 1)] =
(NO_NET | ROUTED_NET);
}
// Check position on each side for an offset tap on a different net, and
// mark the position unroutable.
//
// NOTE: This is a bit conservative, as it will block positions next to
// an offset tap without checking if the offset distance is enough to
// cause a DRC error. Could be refined. . .
layer = (seg->layer == 0) ? 0 : seg->layer - 1;
if (seg->x1 < (NumChannelsX[layer] - 1)) {
sobs = Obs[layer][OGRID(seg->x1 + 1, seg->y1, layer)];
if ((sobs & OFFSET_TAP) && !(sobs & ROUTED_NET)) {
if (sobs & STUBROUTE_EW) {
dist = Stub[layer][OGRID(seg->x1 + 1, seg->y1, layer)];
if (dist > 0) {
Obs[layer][OGRID(seg->x1 + 1, seg->y1, layer)] |=
(NO_NET | ROUTED_NET);
}
}
}
}
if (seg->x1 > 0) {
sobs = Obs[layer][OGRID(seg->x1 - 1, seg->y1, layer)];
if ((sobs & OFFSET_TAP) && !(sobs & ROUTED_NET)) {
if (sobs & STUBROUTE_EW) {
dist = Stub[layer][OGRID(seg->x1 - 1, seg->y1, layer)];
if (dist < 0) {
Obs[layer][OGRID(seg->x1 - 1, seg->y1, layer)] |=
(NO_NET | ROUTED_NET);
}
}
}
}
if (seg->y1 < (NumChannelsY[layer] - 1)) {
sobs = Obs[layer][OGRID(seg->x1, seg->y1 + 1, layer)];
if ((sobs & OFFSET_TAP) && !(sobs & ROUTED_NET)) {
if (sobs & STUBROUTE_NS) {
dist = Stub[layer][OGRID(seg->x1, seg->y1 + 1, layer)];
if (dist > 0) {
Obs[layer][OGRID(seg->x1, seg->y1 + 1, layer)] |=
(NO_NET | ROUTED_NET);
}
}
}
}
if (seg->y1 > 0) {
sobs = Obs[layer][OGRID(seg->x1, seg->y1 - 1, layer)];
if ((sobs & OFFSET_TAP) && !(sobs & ROUTED_NET)) {
if (sobs & STUBROUTE_NS) {
dist = Stub[layer][OGRID(seg->x1, seg->y1 - 1, layer)];
if (dist < 0) {
Obs[layer][OGRID(seg->x1, seg->y1 - 1, layer)] |=
(NO_NET | ROUTED_NET);
}
}
}
}
// If position itself is an offset route, then make the route position
// on the forward side of the offset unroutable, on both via layers.
// (Like the above code, there is no check for whether the offset
// distance is enough to cause a DRC violation.)
sobs = Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)];
if (sobs & OFFSET_TAP) {
dist = Stub[layer][OGRID(seg->x1, seg->y1, seg->layer)];
if (sobs & STUBROUTE_EW) {
if ((dist > 0) && (seg->x1 < (NumChannelsX[seg->layer] - 1))) {
Obs[seg->layer][OGRID(seg->x1 + 1, seg->y1, seg->layer)] |=
(NO_NET | ROUTED_NET);
Obs[seg->layer + 1][OGRID(seg->x1 + 1, seg->y1, seg->layer + 1)] |=
(NO_NET | ROUTED_NET);
}
if ((dist < 0) && (seg->x1 > 0)) {
Obs[seg->layer][OGRID(seg->x1 - 1, seg->y1, seg->layer)] |=
(NO_NET | ROUTED_NET);
Obs[seg->layer + 1][OGRID(seg->x1 - 1, seg->y1, seg->layer + 1)] |=
(NO_NET | ROUTED_NET);
}
}
else if (sobs & STUBROUTE_NS) {
if ((dist > 0) && (seg->y1 < (NumChannelsY[seg->layer] - 1))) {
Obs[seg->layer][OGRID(seg->x1, seg->y1 + 1, seg->layer)] |=
(NO_NET | ROUTED_NET);
Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 + 1, seg->layer + 1)] |=
(NO_NET | ROUTED_NET);
}
if ((dist < 0) && (seg->y1 > 0)) {
Obs[seg->layer][OGRID(seg->x1, seg->y1 - 1, seg->layer)] |=
(NO_NET | ROUTED_NET);
Obs[seg->layer + 1][OGRID(seg->x1, seg->y1 - 1, seg->layer + 1)] |=
(NO_NET | ROUTED_NET);
}
}
}
}
for (i = seg->x1; ; i += (seg->x2 > seg->x1) ? 1 : -1) {
Obs[seg->layer][OGRID(i, seg->y1, seg->layer)] = netnum;
if (needblock[seg->layer] & ROUTEBLOCKY) {
if ((seg->y1 < (NumChannelsY[seg->layer] - 1)) &&
(Obs[seg->layer][OGRID(i, seg->y1 + 1, seg->layer)]
& NETNUM_MASK) == 0)
Obs[seg->layer][OGRID(i, seg->y1 + 1, seg->layer)] =
(NO_NET | ROUTED_NET);
if ((seg->y1 > 0) &&
(Obs[seg->layer][OGRID(i, seg->y1 - 1, seg->layer)]
& NETNUM_MASK) == 0)
Obs[seg->layer][OGRID(i, seg->y1 - 1, seg->layer)] =
(NO_NET | ROUTED_NET);
}
if (i == seg->x2) break;
}
for (i = seg->y1; ; i += (seg->y2 > seg->y1) ? 1 : -1) {
Obs[seg->layer][OGRID(seg->x1, i, seg->layer)] = netnum;
if (needblock[seg->layer] & ROUTEBLOCKX) {
if ((seg->x1 < (NumChannelsX[seg->layer] - 1)) &&
(Obs[seg->layer][OGRID(seg->x1 + 1, i, seg->layer)]
& NETNUM_MASK) == 0)
Obs[seg->layer][OGRID(seg->x1 + 1, i, seg->layer)] =
(NO_NET | ROUTED_NET);
if ((seg->x1 > 0) &&
(Obs[seg->layer][OGRID(seg->x1 - 1, i, seg->layer)]
& NETNUM_MASK) == 0)
Obs[seg->layer][OGRID(seg->x1 - 1, i, seg->layer)] =
(NO_NET | ROUTED_NET);
}
if (i == seg->y2) break;
}
}
/*--------------------------------------------------------------*/
/* commit_proute - turn the potential route into an actual */
/* route by generating the route segments */
/* */
/* ARGS: route structure to fill; "stage" is 1 if we're on */
/* second stage routing, in which case we fill the */
/* route structure but don't modify the Obs array. */
/* */
/* RETURNS: 1 on success, 0 on stacked via failure, and */
/* -1 on failure to find a terminal. On a stacked */
/* via failure, the route is committed anyway. */
/* */
/* SIDE EFFECTS: Obs update, RT llseg added */
/*--------------------------------------------------------------*/
int commit_proute(ROUTE rt, GRIDP *ept, u_char stage)
{
SEG seg, lseg;
int i, j, k, lay, lay2, rval;
int x, y;
int dx, dy, dl;
u_int netnum, netobs1, netobs2, dir1, dir2;
u_char first = (u_char)1;
u_char dmask;
u_char pflags, p2flags;
PROUTE *Pr;
POINT newlr, newlr2, lrtop, lrend, lrnext, lrcur, lrprev;
double sreq;
if (Verbose > 1) {
Flush(stdout);
Fprintf(stdout, "\nCommit: TotalRoutes = %d\n", TotalRoutes);
}
netnum = rt->netnum;
Pr = &Obs2[ept->lay][OGRID(ept->x, ept->y, ept->lay)];
if (!(Pr->flags & PR_COST)) {
Fprintf(stderr, "commit_proute(): impossible - terminal is not routable!\n");
return -1;
}
// Generate an indexed route, recording the series of predecessors and their
// positions.
lrtop = (POINT)malloc(sizeof(struct point_));
lrtop->x1 = ept->x;
lrtop->y1 = ept->y;
lrtop->layer = ept->lay;
lrtop->next = NULL;
lrend = lrtop;
while (1) {
Pr = &Obs2[lrend->layer][OGRID(lrend->x1, lrend->y1, lrend->layer)];
dmask = Pr->flags & PR_PRED_DMASK;
if (dmask == PR_PRED_NONE) break;
newlr = (POINT)malloc(sizeof(struct point_));
newlr->x1 = lrend->x1;
newlr->y1 = lrend->y1;
newlr->layer = lrend->layer;
lrend->next = newlr;
newlr->next = NULL;
switch (dmask) {
case PR_PRED_N:
(newlr->y1)++;
break;
case PR_PRED_S:
(newlr->y1)--;
break;
case PR_PRED_E:
(newlr->x1)++;
break;
case PR_PRED_W:
(newlr->x1)--;
break;
case PR_PRED_U:
(newlr->layer)++;
break;
case PR_PRED_D:
(newlr->layer)--;
break;
}
lrend = newlr;
}
lrend = lrtop;
// TEST: Walk through the solution, and look for stacked vias. When
// found, look for an alternative path that avoids the stack.
rval = 1;
if (StackedContacts < (Num_layers - 1)) {
POINT lrppre;
POINT a, b;
PROUTE *pri, *pri2;
int stacks = 1, stackheight;
int cx, cy, cl;
int mincost, minx, miny, ci, ci2, collide, cost;
while (stacks != 0) { // Keep doing until all illegal stacks are gone
stacks = 0;
lrcur = lrend;
lrprev = lrend->next;
while (lrprev != NULL) {
lrppre = lrprev->next;
if (lrppre == NULL) break;
stackheight = 0;
a = lrcur;
b = lrprev;
while (a->layer != b->layer) {
stackheight++;
a = b;
b = a->next;
if (b == NULL) break;
}
collide = FALSE;
while (stackheight > StackedContacts) { // Illegal stack found
stacks++;
// Try to move the second contact in the path
cx = lrprev->x1;
cy = lrprev->y1;
cl = lrprev->layer;
mincost = MAXRT;
dl = lrppre->layer;
// Check all four positions around the contact for the
// lowest cost, and make sure the position below that
// is available.
dx = cx + 1; // Check to the right
pri = &Obs2[cl][OGRID(dx, cy, cl)];
pflags = pri->flags;
cost = pri->prdata.cost;
if (collide && !(pflags & (PR_COST | PR_SOURCE)) &&
(pri->prdata.net < MAXNETNUM)) {
pflags = 0;
cost = ConflictCost;
}
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE && cost < mincost) {
pri2 = &Obs2[dl][OGRID(dx, cy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = cost;
minx = dx;
miny = cy;
}
}
else if (collide && !(p2flags & (PR_COST | PR_SOURCE)) &&
(pri2->prdata.net < MAXNETNUM) &&
((cost + ConflictCost) < mincost)) {
mincost = cost + ConflictCost;
minx = dx;
miny = dy;
}
}
}
dx = cx - 1; // Check to the left
pri = &Obs2[cl][OGRID(dx, cy, cl)];
pflags = pri->flags;
cost = pri->prdata.cost;
if (collide && !(pflags & (PR_COST | PR_SOURCE)) &&
(pri->prdata.net < MAXNETNUM)) {
pflags = 0;
cost = ConflictCost;
}
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE && cost < mincost) {
pri2 = &Obs2[dl][OGRID(dx, cy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = cost;
minx = dx;
miny = cy;
}
}
else if (collide && !(p2flags & (PR_COST | PR_SOURCE)) &&
(pri2->prdata.net < MAXNETNUM) &&
((cost + ConflictCost) < mincost)) {
mincost = cost + ConflictCost;
minx = dx;
miny = dy;
}
}
}
dy = cy + 1; // Check north
pri = &Obs2[cl][OGRID(cx, dy, cl)];
pflags = pri->flags;
cost = pri->prdata.cost;
if (collide && !(pflags & (PR_COST | PR_SOURCE)) &&
(pri->prdata.net < MAXNETNUM)) {
pflags = 0;
cost = ConflictCost;
}
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE && cost < mincost) {
pri2 = &Obs2[dl][OGRID(cx, dy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = cost;
minx = cx;
miny = dy;
}
}
else if (collide && !(p2flags & (PR_COST | PR_SOURCE)) &&
(pri2->prdata.net < MAXNETNUM) &&
((cost + ConflictCost) < mincost)) {
mincost = cost + ConflictCost;
minx = dx;
miny = dy;
}
}
}
dy = cy - 1; // Check south
pri = &Obs2[cl][OGRID(cx, dy, cl)];
pflags = pri->flags;
cost = pri->prdata.cost;
if (collide && !(pflags & (PR_COST | PR_SOURCE)) &&
(pri->prdata.net < MAXNETNUM)) {
pflags = 0;
cost = ConflictCost;
}
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if (pflags & PR_PRED_DMASK != PR_PRED_NONE && cost < mincost) {
pri2 = &Obs2[dl][OGRID(cx, dy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = cost;
minx = cx;
miny = dy;
}
}
else if (collide && !(p2flags & (PR_COST | PR_SOURCE)) &&
(pri2->prdata.net < MAXNETNUM) &&
((cost + ConflictCost) < mincost)) {
mincost = cost + ConflictCost;
minx = dx;
miny = dy;
}
}
}
// Was there an available route? If so, modify
// records to route through this alternate path. If not,
// then try to move the first contact instead.
if (mincost < MAXRT) {
pri = &Obs2[cl][OGRID(minx, miny, cl)];
newlr = (POINT)malloc(sizeof(struct point_));
newlr->x1 = minx;
newlr->y1 = miny;
newlr->layer = cl;
pri2 = &Obs2[dl][OGRID(minx, miny, dl)];
newlr2 = (POINT)malloc(sizeof(struct point_));
newlr2->x1 = minx;
newlr2->y1 = miny;
newlr2->layer = dl;
lrprev->next = newlr;
newlr->next = newlr2;
// Check if point at pri2 is equal to position of
// lrppre->next. If so, bypass lrppre.
if (lrnext = lrppre->next) {
if (lrnext->x1 == minx && lrnext->y1 == miny &&
lrnext->layer == dl) {
newlr->next = lrnext;
free(lrppre);
free(newlr2);
lrppre = lrnext; // ?
}
else
newlr2->next = lrppre;
}
else
newlr2->next = lrppre;
break; // Found a solution; we're done.
}
else {
// If we couldn't offset lrprev position, then try
// offsetting lrcur.
cx = lrcur->x1;
cy = lrcur->y1;
cl = lrcur->layer;
mincost = MAXRT;
dl = lrprev->layer;
dx = cx + 1; // Check to the right
pri = &Obs2[cl][OGRID(dx, cy, cl)];
pflags = pri->flags;
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri->prdata.cost < mincost) {
pri2 = &Obs2[dl][OGRID(dx, cy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = pri->prdata.cost;
minx = dx;
miny = cy;
}
}
}
}
dx = cx - 1; // Check to the left
pri = &Obs2[cl][OGRID(dx, cy, cl)];
pflags = pri->flags;
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri->prdata.cost < mincost) {
pri2 = &Obs2[dl][OGRID(dx, cy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = pri->prdata.cost;
minx = dx;
miny = cy;
}
}
}
}
dy = cy + 1; // Check north
pri = &Obs2[cl][OGRID(cx, dy, cl)];
pflags = pri->flags;
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri->prdata.cost < mincost) {
pri2 = &Obs2[dl][OGRID(cx, dy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = pri->prdata.cost;
minx = cx;
miny = dy;
}
}
}
}
dy = cy - 1; // Check south
pri = &Obs2[cl][OGRID(cx, dy, cl)];
pflags = pri->flags;
if (pflags & PR_COST) {
pflags &= ~PR_COST;
if ((pflags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri->prdata.cost < mincost) {
pri2 = &Obs2[dl][OGRID(cx, dy, dl)];
p2flags = pri2->flags;
if (p2flags & PR_COST) {
p2flags &= ~PR_COST;
if ((p2flags & PR_PRED_DMASK) != PR_PRED_NONE &&
pri2->prdata.cost < MAXRT) {
mincost = pri->prdata.cost;
minx = cx;
miny = dy;
}
}
}
}
if (mincost < MAXRT) {
newlr = (POINT)malloc(sizeof(struct point_));
newlr->x1 = minx;
newlr->y1 = miny;
newlr->layer = cl;
newlr2 = (POINT)malloc(sizeof(struct point_));
newlr2->x1 = minx;
newlr2->y1 = miny;
newlr2->layer = dl;
// If newlr is a source or target, then make it
// the endpoint, because we have just moved the
// endpoint along the source or target, and the
// original endpoint position is not needed.
pri = &Obs2[cl][OGRID(minx, miny, cl)];
pri2 = &Obs2[lrcur->layer][OGRID(lrcur->x1,
lrcur->y1, lrcur->layer)];
if (((pri->flags & PR_SOURCE) && (pri2->flags & PR_SOURCE)) ||
((pri->flags & PR_TARGET) &&
(pri2->flags & PR_TARGET)) && (lrcur == lrtop)) {
lrtop = newlr;
lrend = newlr;
free(lrcur);
lrcur = newlr;
}
else
lrcur->next = newlr;
newlr->next = newlr2;
// Check if point at pri2 is equal to position of
// lrprev->next. If so, bypass lrprev.
if (lrppre->x1 == minx && lrppre->y1 == miny &&
lrppre->layer == dl) {
newlr->next = lrppre;
free(lrprev);
free(newlr2);
lrprev = lrcur;
}
else
newlr2->next = lrprev;
break; // Found a solution; we're done.
}
else if (stage == 0) {
// On the first stage, we call it an error and move
// on to the next net. This is a bit conservative,
// but it works because failing to remove a stacked
// via is a rare occurrance.
if (Verbose > 0)
Fprintf(stderr, "Failed to remove stacked via "
"at grid point %d %d.\n",
lrcur->x1, lrcur->y1);
stacks = 0;
rval = 0;
goto cleanup;
}
else {
if (collide == TRUE) {
Fprintf(stderr, "Failed to remove stacked via "
"at grid point %d %d; position may "
"not be routable.\n",
lrcur->x1, lrcur->y1);
stacks = 0;
rval = 0;
goto cleanup;
}
// On the second stage, we will run through the
// search again, but allow overwriting other
// nets, which will be treated like other colliding
// nets in the regular route path search.
collide = TRUE;
}
}
}
lrcur = lrprev;
lrprev = lrppre;
}
}
}
lrend = lrtop;
lrcur = lrtop;
lrprev = lrcur->next;
lseg = (SEG)NULL;
while (1) {
seg = (SEG)malloc(sizeof(struct seg_));
seg->next = NULL;
seg->segtype = (lrcur->layer == lrprev->layer) ? ST_WIRE : ST_VIA;
seg->x1 = lrcur->x1;
seg->y1 = lrcur->y1;
seg->layer = MIN(lrcur->layer, lrprev->layer);
seg->x2 = lrprev->x1;
seg->y2 = lrprev->y1;
dx = seg->x2 - seg->x1;
dy = seg->y2 - seg->y1;
// segments are in order---place final segment at end of list
if (rt->segments == NULL)
rt->segments = seg;
else
lseg->next = seg;
// Continue processing predecessors as long as the direction is the same,
// so we get a single long wire segment. This minimizes the number of
// segments produced. Vias have to be handled one at a time, as we make
// no assumptions about stacked vias.
if (seg->segtype == ST_WIRE) {
while ((lrnext = lrprev->next) != NULL) {
lrnext = lrprev->next;
if (((lrnext->x1 - lrprev->x1) == dx) &&
((lrnext->y1 - lrprev->y1) == dy) &&
(lrnext->layer == lrprev->layer)) {
lrcur = lrprev;
lrprev = lrnext;
seg->x2 = lrprev->x1;
seg->y2 = lrprev->y1;
}
else
break;
}
}
if (Verbose > 3) {
Fprintf(stdout, "commit: index = %d, net = %d\n",
Pr->prdata.net, netnum);
if (seg->segtype == ST_WIRE) {
Fprintf(stdout, "commit: wire layer %d, (%d,%d) to (%d,%d)\n",
seg->layer, seg->x1, seg->y1, seg->x2, seg->y2);
}
else {
Fprintf(stdout, "commit: via %d to %d\n", seg->layer,
seg->layer + 1);
}
Flush(stdout);
}
// now fill in the Obs structure with this route....
lay2 = (seg->segtype & ST_VIA) ? seg->layer + 1 : seg->layer;
netobs1 = Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)];
netobs2 = Obs[lay2][OGRID(seg->x2, seg->y2, lay2)];
dir1 = netobs1 & PINOBSTRUCTMASK;
dir2 = netobs2 & PINOBSTRUCTMASK;
netobs1 &= NETNUM_MASK;
netobs2 &= NETNUM_MASK;
netnum |= ROUTED_NET;
// If Obs shows this position as an obstruction, then this was a port with
// no taps in reach of a grid point. This will be dealt with by moving
// the via off-grid and onto the port position in emit_routes().
if (stage == (u_char)0) {
writeback_segment(seg, netnum);
if (first && dir1) {
first = (u_char)0;
}
else if (first && dir2 && (seg->segtype & ST_VIA) && lrprev &&
(lrprev->layer != lay2)) {
// This also applies to vias at the beginning of a route
// if the path goes down instead of up (can happen on pins,
// in particular)
Obs[lay2][OGRID(seg->x1, seg->y1, lay2)] |= dir2;
}
}
// Keep stub information on obstructions that have been routed
// over, so that in the rip-up stage, we can return them to obstructions.
Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)] |= dir1;
Obs[lay2][OGRID(seg->x2, seg->y2, lay2)] |= dir2;
// An offset route end on the previous segment, if it is a via, needs
// to carry over to this one, if it is a wire route.
if (lseg && ((lseg->segtype & (ST_VIA | ST_OFFSET_END)) ==
(ST_VIA | ST_OFFSET_END)))
if (seg->segtype != ST_VIA)
seg->segtype |= ST_OFFSET_START;
// Check if the route ends are offset. If so, add flags. The segment
// entries are integer grid points, so offsets need to be made when
// the location is output.
if (dir1 & OFFSET_TAP) {
seg->segtype |= ST_OFFSET_START;
// An offset on a via needs to be applied to the previous route
// segment as well, if that route is a wire.
if (lseg && (seg->segtype & ST_VIA) && !(lseg->segtype & ST_VIA))
lseg->segtype |= ST_OFFSET_END;
}
if (dir2 & OFFSET_TAP) seg->segtype |= ST_OFFSET_END;
lrend = lrcur; // Save the last route position
lrend->x1 = lrcur->x1;
lrend->y1 = lrcur->y1;
lrend->layer = lrcur->layer;
lrcur = lrprev; // Move to the next route position
lrcur->x1 = seg->x2;
lrcur->y1 = seg->y2;
lrprev = lrcur->next;
if (lrprev == NULL) {
if (dir2 && (stage == (u_char)0)) {
Obs[lay2][OGRID(seg->x2, seg->y2, lay2)] |= dir2;
}
else if (dir1 && (seg->segtype & ST_VIA)) {
// This also applies to vias at the end of a route
Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)] |= dir1;
}
// Before returning, set *ept to the endpoint
// position. This is for diagnostic purposes only.
ept->x = lrend->x1;
ept->y = lrend->y1;
ept->lay = lrend->layer;
// Clean up allocated memory for the route. . .
while (lrtop != NULL) {
lrnext = lrtop->next;
free(lrtop);
lrtop = lrnext;
}
return rval; // Success
}
lseg = seg; // Move to next segment position
}
cleanup:
while (lrtop != NULL) {
lrnext = lrtop->next;
free(lrtop);
lrtop = lrnext;
}
return 0;
} /* commit_proute() */
/*------------------------------------------------------*/
/* writeback_route() --- */
/* */
/* This routine is the last part of the routine */
/* above. It copies the net defined by the segments */
/* in the route structure "rt" into the Obs array. */
/* This is used only for stage 2, when the writeback */
/* is not done by commit_proute because we want to */
/* rip up nets first, and also done prior to routing */
/* for any pre-defined net routes. */
/*------------------------------------------------------*/
int writeback_route(ROUTE rt)
{
SEG seg;
int i, lay2;
u_int netnum, dir1, dir2;
u_char first = (u_char)1;
netnum = rt->netnum | ROUTED_NET;
for (seg = rt->segments; seg; seg = seg->next) {
/* Save stub route information at segment ends. */
/* NOTE: Where segment end is a via, make sure we are */
/* placing the segment end on the right metal layer! */
lay2 = (seg->segtype & ST_VIA) ? seg->layer + 1 : seg->layer;
dir1 = Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)] & PINOBSTRUCTMASK;
dir2 = Obs[lay2][OGRID(seg->x2, seg->y2, lay2)] & PINOBSTRUCTMASK;
writeback_segment(seg, netnum);
if (first) {
first = (u_char)0;
if (dir1)
Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)] |= dir1;
else if (dir2)
Obs[lay2][OGRID(seg->x2, seg->y2, lay2)] |= dir2;
}
else if (!seg->next) {
if (dir1)
Obs[seg->layer][OGRID(seg->x1, seg->y1, seg->layer)] |= dir1;
else if (dir2)
Obs[lay2][OGRID(seg->x2, seg->y2, lay2)] |= dir2;
}
}
return TRUE;
}
/*------------------------------------------------------*/
/* Writeback all routes belonging to a net */
/*------------------------------------------------------*/
int writeback_all_routes(NET net)
{
ROUTE rt;
int result = TRUE;
for (rt = net->routes; rt; rt = rt->next) {
if (writeback_route(rt) == FALSE)
result = FALSE;
}
return result;
}
/* end of maze.c */
|