📄 readlib.cpp
字号:
#include "dagon.h"main(int argc, char *argv[]) { lib *cir; liblist *cell_lib; if (argc!=3) // unmatched argument number printf("Usage:dagon pattern_file subject_file\n"); else { cell_lib=read_pat(argv[1]); // read pattern file, NULL=failed cir=read_sub(argv[2]); // read subject file, NULL=failed if (cell_lib && cir) { dagon(cir->netlist,cell_lib); printf("CIR_BEGIN\n"); printf("%s\n",cir->name); printf("%d\n",cir->netlist->mcost); printf("%s\n",cir->netlist->covlib->name); print_ckt(cir->netlist,cir->netlist->covlib->netlist); printf("CIR_END\n"); } /* free used memory */ free_liblist(cell_lib); free_gate(cir->netlist); delete cir->name; delete cir; } return 0;}liblist *read_pat(char *pattern_file){ FILE *fp; int cost,stleng; char cirname[256]; gate *gtemp; lib *newlib; liblist *cell_lib,*newlist,*listend; cell_lib=NULL; if ((fp=fopen(pattern_file,"r"))==NULL) { printf("Pattern file open error!!\n"); return NULL; } else { listend=NULL; while (!feof(fp)) { if ((gtemp=read_cir(fp,cirname,&cost)) != NULL) { /* add a new cell in the library */ newlib=new lib; stleng=strlen(cirname); newlib->name=new char[stleng+1]; strcpy(newlib->name,cirname); newlib->cost=cost; newlib->netlist=gtemp; /* put this cell into link list */ newlist=new liblist; newlist->cell=newlib; newlist->next=NULL; if (listend==NULL) cell_lib=newlist; else listend->next=newlist; listend=newlist; } } fclose(fp); } return cell_lib;}lib *read_sub(char *subject_file){ FILE *fp; int cost,stleng; char cirname[256]; gate *gtemp; lib *cir; cir=NULL; if ((fp=fopen(subject_file,"r"))==NULL) { printf("Subject file open error!!\n"); return NULL; } else { if ((gtemp=read_cir(fp,cirname,&cost)) != NULL) { /* add a new cell of the circuit */ cir=new lib; stleng=strlen(cirname); cir->name=new char[stleng+1]; strcpy(cir->name,cirname); cir->cost=cost; cir->netlist=gtemp; } fclose(fp); } return cir;} gate *read_cir(FILE *fp, char *cirname, int *cost){ int i,stleng,gano,dir; char temp[256],str[256]; gate *garray[256]; // assume max number of gates in a cell is 256 gate *ptemp,*root; if (!fp) return NULL; while((fgets(temp,256,fp))!=NULL) { stleng=strlen(temp); for (i=0;i<stleng;i++) // skip the space and tab at the begining if (temp[i]!=' ' && temp[i]!=9) break; if (i>=stleng || temp[i]=='\n') continue; // empty line else { sscanf(temp+i,"%s",str); if ((strcmp(str,"CIR_BEGIN"))==0) { fgets(cirname,256,fp); /* circuit name in the next line */ for (i=0;i<256;i++) if (cirname[i]=='\n') break; // find new_line symbol cirname[i]=0; // replace by string_end symbol fgets(temp,256,fp); /* cell cost in the next line */ sscanf(temp,"%d",cost); break; } else { printf("Wrong Netlist Format !!\n"); break; } } } gano=0; // the indicator about the connection waiting queue root=NULL; // the root of VLR tree, representing netlist output dir=RIGHT; // read gates from output (right) to input (left) while((fgets(temp,256,fp))!=NULL) { // read sub-circuits in this cell stleng=strlen(temp); for (i=0;i<stleng;i++) // skip the space and tab at the begining if (temp[i]!=' ' && temp[i]!=9) break; if (i>=stleng || temp[i]=='\n') continue; else { sscanf(temp+i,"%s",str); if ((strcmp(str,"CIR_END"))==0) break; // sub_circuit finish else if ((strcmp(str,"NAND2"))==0) { ptemp=new gate; ptemp->type=NAND; ptemp->rin=NULL; ptemp->lin=NULL; ptemp->mcost=0; ptemp->covlib=NULL; if (root==NULL) root=ptemp; // the first gate, output node if (gano > 0) { if (dir == RIGHT) { /* link the right pointer of previous gate to current gate */ (garray[gano])->rin = ptemp; /* if previous gate is inverter, its only input has been connected. then remove it from the waiting list by assigning the same queue indicator to current gate */ if ((garray[gano])->type==INV) gano--; } else { /* link the left pointer of previous gate to current gate */ (garray[gano])->lin = ptemp; /* and remove it from the waiting list */ gano--; } } garray[++gano] = ptemp; // increase the queue indicator dir = RIGHT; } else if ((strcmp(str,"INV"))==0) { ptemp=new gate; ptemp->type=INV; ptemp->rin=NULL; ptemp->lin=NULL; ptemp->mcost=0; ptemp->covlib=NULL; if (root==NULL) root=ptemp; if (gano > 0) { if (dir == RIGHT) { (garray[gano])->rin = ptemp; if ((garray[gano])->type==INV) gano--; } else { (garray[gano])->lin = ptemp; gano--; } } garray[++gano] = ptemp; dir = RIGHT; } else if ((strcmp(str,"INPUT"))==0) { // begin read netlist backward if (gano > 0) { if (dir == RIGHT) { // the first time to reach boundary (garray[gano])->rin = NULL; // no more gate at right side if ((garray[gano])->type==INV) gano--; } else { // traversing left side (garray[gano])->lin = NULL; // no more gate at left side gano--; // two inputs used, remove it from the waiting list } } dir = LEFT; } else { printf("Wrong Netlist Format !!\n"); break; } } } return root;}void print_ckt(gate *cur_gate, gate *cell_gate){ if (cell_gate==NULL && cur_gate!=NULL) { printf("%s\n",cur_gate->covlib->name); cell_gate=cur_gate->covlib->netlist; } if (cur_gate==NULL) printf("INPUT\n"); else { /* recursively traverse the right child of the tree */ print_ckt(cur_gate->rin,cell_gate->rin); if (cur_gate->type != INV) print_ckt(cur_gate->lin,cell_gate->lin); }}void free_liblist(liblist *cell_lib){ liblist *cur_node, *next; cur_node=cell_lib; while (cur_node) { next=cur_node->next; free_gate(cur_node->cell->netlist); delete cur_node->cell->name; delete cur_node->cell; delete cur_node; cur_node=next; }}void free_gate(gate *cur_gate){ if (!cur_gate) return; /* recursively traverse the right child of the tree */ free_gate(cur_gate->lin); free_gate(cur_gate->rin); delete cur_gate;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -