home *** CD-ROM | disk | FTP | other *** search
/ gdead.berkeley.edu / gdead.berkeley.edu.tar / gdead.berkeley.edu / pub / cad-tools / ciftomann.tar / flatten.c < prev    next >
C/C++ Source or Header  |  1988-01-28  |  6KB  |  324 lines

  1. #include "ciftomann.h"
  2. #include "fd.h"
  3. #include <signal.h>
  4. #undef INFINITY
  5. #include "cd.h"
  6.  
  7. #define HORIZONTAL    0
  8. #define VERTICAL    1
  9. #define IDENTICAL    2
  10. #define NON_MANHATTEN    3
  11.  
  12. char *dirname = "fltXXXXXX";
  13. char cmd[60];
  14.  
  15. char *cif_file_name;
  16.  
  17.     /*
  18.      *    flatten uses Ken Keller's CD package to recursively 
  19.      *    expand each call of a cell into the actual geometry.
  20.      *  The result is written onto standard output for edger
  21.      *    to break up into edges
  22.      */
  23.  
  24. main(argc,argv) 
  25. int argc;
  26. char **argv;
  27. {
  28.     
  29.     char *tempname = "fltXXXXXX";
  30.     int layer, status;
  31.     struct s *SymbolDesc;
  32.  
  33.     cif_file_name = argv[1];
  34.  
  35.     catch_signals();
  36.     TInit();
  37.     CDInit();
  38.  
  39.     mktemp(tempname);
  40.  
  41.     /* convert the cif into CD's file format */
  42.  
  43.     if (CDParseCIF(tempname, cif_file_name, 'n') != True) {
  44.     fprintf(stderr,"flatten : Error in cif file : %s : %s\n",
  45.         cif_file_name, CDStatusString);
  46.     terminate();
  47.     }
  48.  
  49.     CDOpen(tempname,&SymbolDesc,'r');
  50.  
  51.     if (CDStatusInt == CDNEWSYMBOL) {
  52.     fprintf(stderr,
  53.         "flatten : Parse of the cif failed: CD got confused\n");
  54.     terminate();
  55.     }
  56.  
  57.     if (CDStatusInt == CDPARSEFAILED) {
  58.     fprintf(stderr,"flatten : Error in cif file (%d) : %s\n",
  59.         CDStatusInt, CDStatusString);
  60.     terminate();
  61.     }
  62.  
  63.     for (layer = 1; layer <= CDNUMLAYERS; layer++) {
  64.  
  65.     if (CDLayer[layer].lTechnology != ' ') {
  66.  
  67.         int i;
  68.  
  69.         printf("L %c",CDLayer[layer].lTechnology);
  70.         for(i=0;CDLayer[layer].lMask[i] != ' ' && i < 3;i++) {
  71.         putchar(CDLayer[layer].lMask[i]);
  72.         }
  73.         printf(";\n");
  74.  
  75.         Flatten(tempname,layer);
  76.     }
  77.     }
  78.  
  79.     printf("E\n");
  80.  
  81.     exit(0);
  82. }
  83.  
  84.     /*
  85.      * this is the main routine, called recursively on each cell
  86.      * call
  87.      */
  88. Flatten(SymbolName,layer)
  89. char * SymbolName;
  90. int layer;
  91. {
  92.     struct s *SymbolDesc;
  93.     char *Master;
  94.     char Type;
  95.     int junk;
  96.     int X,Y;
  97.     struct g *GenDesc;
  98.     struct o *Pointer;
  99.     struct t *TGen;
  100.  
  101.  
  102.     CDOpen(SymbolName,&SymbolDesc,'w');
  103.  
  104.     if (CDStatusInt == CDPARSEFAILED) 
  105.     fprintf(stderr,"flatten : Error in cell \"%s\" : %s\n",
  106.         SymbolName, CDStatusString);
  107.  
  108.     CDInitGen(SymbolDesc,0,-INFINITY,-INFINITY,INFINITY,INFINITY
  109.           ,&GenDesc);
  110.  
  111.     /* handle all of the cell calls */
  112.  
  113.     for(;;) {
  114.     CDGen(SymbolDesc,GenDesc,&Pointer);
  115.     if (Pointer == NULL)
  116.         break;
  117.     TPush();
  118.     TIdentity();
  119.     CDCall(Pointer,&Master,&junk,&junk,&junk,&junk);
  120.     CDInitTGen(Pointer,&TGen);
  121.  
  122.     for(;;) {
  123.         CDTGen(&TGen,&Type,&X,&Y);
  124.         if (TGen == NULL) 
  125.         break;
  126.  
  127.         switch (Type) {
  128.         
  129.         case 't' :
  130.             TTranslate(X,Y);
  131.             break;
  132.         
  133.         case 'r' :
  134.             TRotate(X,Y);
  135.             break;
  136.  
  137.         case 'x' :
  138.             TMX();
  139.             break;
  140.  
  141.         case 'y' :
  142.             TMY();
  143.             break;
  144.         
  145.         }/*switch*/
  146.     }
  147.     TPremultiply();
  148.     Flatten(Master,layer);
  149.     TPop();
  150.     } /* instances */
  151.     /* handle all the geometry in the cell */
  152.     ProcessLayer(SymbolDesc,layer);
  153. }
  154.  
  155. #define Scale(value) (*(value) = (CDDesc.dDSA*(*(value)))/CDDesc.dDSB)
  156.  
  157. ProcessLayer(SymbolDesc,layer)
  158. struct s *SymbolDesc;
  159. int layer;
  160. {
  161.     
  162.     struct p *Path;
  163.     struct o *Pointer;
  164.     struct g *GenDesc;
  165.     int junk;
  166.     int Width;
  167.     int X,Y,Xdir,Ydir,H,W;
  168.     int ZeroX,ZeroY;
  169.     char Type;
  170.  
  171.     if (CDInitGen(SymbolDesc,layer,(int)-INFINITY,(int)-INFINITY,
  172.         (int)INFINITY,(int)INFINITY,&GenDesc) == False) {
  173.     fprintf(stderr, "CDInitGen, malloc failed\n");
  174.     }
  175.     for(;;) {
  176.     CDGen(SymbolDesc,GenDesc,&Pointer);
  177.     if (Pointer == NULL)
  178.         break;
  179.     CDType(Pointer,&Type);
  180.  
  181.     switch (Type) {
  182.         
  183.         case CDBOX :
  184.         CDBox(Pointer,&junk,&W,&H,&X,&Y);
  185.         TPoint(&X,&Y);
  186.         ZeroX = ZeroY = 0;
  187.         TPoint(&ZeroX,&ZeroY);
  188.         Xdir = 1; Ydir = 0;
  189.         TPoint(&Xdir,&Ydir);
  190.         Xdir -= ZeroX;
  191.         Ydir -= ZeroY;
  192.         Scale(&X); Scale(&Y); Scale(&W); Scale(&H);
  193.         MakeBox(X,Y,W,H,Xdir,Ydir);
  194.         break;
  195.         
  196.         case CDWIRE :
  197.         CDWire(Pointer,&junk,&Width,&Path);
  198.         Scale(&Width);
  199.         MakeWire(Width,Path);
  200.         break;
  201.         
  202.         case CDROUNDFLASH :
  203.         CDRoundFlash(Pointer,&junk,&Width, &X,&Y);
  204.         TPoint(&X,&Y);
  205.         Scale(&X); Scale(&Y); Scale(&Width);
  206.         MakeCircle(Width,X,Y);
  207.         break;
  208.  
  209.         case CDLABEL :
  210.         break;
  211.         
  212.         case CDPOLYGON :
  213.         CDPolygon(Pointer,&junk,&Path);
  214.         MakePolygon(Path);
  215.         break;
  216.  
  217.         default :
  218.         fprintf(stderr,"Flattener : Unknown type %c\n",
  219.             Type);
  220.     }
  221.     } /* geoms */
  222. }
  223.  
  224.     /*
  225.      * all of the following routines convert the data returned by
  226.      * CD into the various (ASCII) CIF constructs
  227.      */
  228.  
  229. MakeBox(X,Y,W,H,Xdir,Ydir)
  230. int X,Y,W,H,Xdir,Ydir;
  231. {
  232.     /* throw away zero width boxes */
  233.     if (W == 0 || H == 0) {
  234.     return;
  235.     }
  236.  
  237.     printf("B %d %d %d %d %d %d;\n",W,H,X,Y,Xdir,Ydir);
  238. }
  239.  
  240. MakePolygon(pathlist)
  241. struct p *pathlist;
  242. {
  243.     
  244.     struct p path;
  245.  
  246.     path = *pathlist;
  247.  
  248.     TPoint(&(path.pX),&(path.pY));
  249.  
  250.     Scale(&(path.pX)); Scale(&(path.pY));
  251.  
  252.     printf("P %d %d",path.pX,path.pY);
  253.  
  254.     while (path.pSucc != 0) {
  255.     path = *path.pSucc;
  256.     TPoint(&(path.pX),&(path.pY));
  257.     Scale(&(path.pX)); Scale(&(path.pY));
  258.     printf(" %d %d",path.pX,path.pY);
  259.     }
  260.  
  261.     printf(";\n");
  262.  
  263. }
  264.  
  265. MakeWire(Width,pathlist)
  266. int Width;
  267. struct p *pathlist;
  268. {
  269.     struct p path;
  270.  
  271.     if (Width == 0) {
  272.     return;
  273.     }
  274.  
  275.     path = *pathlist;
  276.  
  277.     TPoint(&(path.pX),&(path.pY));
  278.  
  279.     Scale(&(path.pX)); Scale(&(path.pY));
  280.  
  281.     if (path.pSucc == 0) {
  282.     MakeCircle(Width,path.pX,path.pY);
  283.     return;
  284.     }
  285.  
  286.     printf("W %d %d %d",Width,path.pX,path.pY);
  287.  
  288.     while (path.pSucc != 0) {
  289.     path = *path.pSucc;
  290.     TPoint(&(path.pX),&(path.pY));
  291.     Scale(&(path.pX)); Scale(&(path.pY));
  292.     printf(" %d %d",path.pX,path.pY);
  293.     }
  294.  
  295.     printf(";\n");
  296.  
  297. }
  298.  
  299. MakeCircle(Diameter,X,Y)
  300. int Diameter,X,Y;
  301. {
  302.     printf("R %d %d %d;\n",Diameter,X,Y);
  303. }
  304.  
  305. terminate()
  306. {
  307.     exit(1);
  308. }
  309.  
  310. catch_signals()
  311. {
  312.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
  313.     signal(SIGHUP, terminate);
  314.     }
  315.  
  316.     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
  317.     signal(SIGINT, terminate);
  318.     }
  319.  
  320.     signal(SIGPIPE, terminate);
  321.  
  322.     signal(SIGTERM, terminate);
  323. }
  324.