fork(34) download
  1. //#include <iostream>
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5.  
  6. #include "rapidjson/filereadstream.h"
  7. #include "rapidjson/document.h"
  8. #include <cstdio>
  9.  
  10. using namespace rapidjson;
  11.  
  12.  
  13. //using namespace std;
  14.  
  15. #define MAXSIZE 16*1024
  16. #define MAXPORT 1024
  17.  
  18. int CCOUNT = 0;
  19.  
  20. char cellsPrefix[1024][32];
  21. /*
  22.  "INV",
  23.  "NAND",
  24.  "NOR",
  25.  "XNOR",
  26.  "AND",
  27.  "OR",
  28.  "XOR",
  29.  "AOI",
  30.  "MUX",
  31.  "CLKBUF",
  32.  "FAX",
  33.  "HAX",
  34.  "DFFSR",
  35.  "DFFPOS",
  36.  "DFFNEG"
  37.  };
  38.  */
  39.  
  40. typedef struct {char type[32]; char name[32];char pins[32][32]; char nets[32][32]; unsigned pCnt;} GATE;
  41. typedef struct {unsigned dir;char name[32];unsigned isBus;unsigned busSize;} PORT;
  42.  
  43. typedef enum {OUT=0, IN=1, CELL, FFD, FFQ} NODE_T;
  44. typedef struct node_struct {NODE_T type; int fo_instances[32]; int fo_cnt; int instance;} NODE;
  45. NODE graphNodes[MAXSIZE];
  46.  
  47. GATE circuitGates[MAXSIZE];
  48. PORT circuitPorts[MAXPORT];
  49.  
  50. unsigned int gCount = 0, nCount = 0, ffCount = 0, portCount=0;
  51. unsigned int nodeCount = 0;
  52.  
  53.  
  54. Document doc;
  55.  
  56.  
  57. /* Graph */
  58. int buildGraph()
  59. {
  60. int i;
  61. for(i=0;i<portCount;i++){
  62. graphNodes[nodeCount].type = (NODE_T) circuitPorts[i].dir;
  63. graphNodes[nodeCount].instance = i;
  64. nodeCount++;
  65. }
  66. for(i=0;i<gCount;i++){
  67. //graphNodes[nodeCount].type = (NODE_T) circuitPorts[i].dir;
  68. graphNodes[nodeCount].instance = i;
  69. }
  70.  
  71.  
  72. }
  73. /*
  74.  int findFO(int gate, char *net, int list[]){
  75.  int i, e;
  76.  // fnd all cnnected cells to that net
  77.  for(i=0; i<gCount; i++){
  78.  if(i!=g){
  79.  for(e=0; e<circuitGates[i].pCnt; e++){
  80.  //if()
  81.  }
  82.  }
  83.  }
  84.  }
  85.  
  86.  */
  87. //
  88.  
  89. int isCell(char *str)
  90. {
  91. int i;
  92. for(i=0; i<CCOUNT; i++){
  93. if(strstr(str, cellsPrefix[i]))
  94. return 1;
  95. }
  96. return 0;
  97. }
  98.  
  99. int getCell(FILE *fp, char *s)
  100. {
  101. char buff[512];
  102. while(1) {
  103. if(!fgets(buff, 511, fp)) return 0;
  104. strtok(buff, "\n");
  105. strcat(s, buff);
  106. if(strchr(buff,';')) break;
  107. }
  108. return 1;
  109. }
  110.  
  111. int getPinNet(char *s, char *n)
  112. {
  113. char *p=strchr(s,'(');
  114. if(!*p) return 0;
  115. p++;
  116. while(*p!=')') *n++=*p++;
  117. *n='\0';
  118. }
  119.  
  120. int getPinName(char *s, char *p)
  121. {
  122. s++;
  123. while(*s!='(') *p++=*s++;
  124. *p='\0';
  125. }
  126.  
  127. int getCellPins(char *s)
  128. {
  129. char *p=s;
  130. char net[32], pin[32];
  131. while(p=strchr(p,'.')){
  132. getPinName(p, pin);
  133. getPinNet(p, net);
  134. strcpy(circuitGates[gCount].pins[circuitGates[gCount].pCnt],pin);
  135. strcpy(circuitGates[gCount].nets[circuitGates[gCount].pCnt++],net);
  136. //printf("pin: %s\n", net);
  137. p++;
  138. }
  139. }
  140.  
  141. #define ISW(s) (*s==' ' || *s=='\t')
  142. int parseNameAndType(char *s, char *nm, char *type)
  143. {
  144. while(ISW(s)) s++;
  145. while(!ISW(s)) *type++=*s++;
  146. *type = '\0';
  147. while(ISW(s)) s++;
  148. while(!ISW(s)) *nm++=*s++;
  149. *nm = '\0';
  150. }
  151. /*
  152.  int parseToken (char *s, char *tok, char *sep)
  153.  {
  154.  while(ISW(*s)) s++;
  155.  while
  156.  }*/
  157. int parseRange(char *s, unsigned *r1, unsigned *r2){
  158. char num[32];
  159. char *p = num;
  160. s = strchr(s, '[');
  161. s++;
  162. while(*s!=':') *p++=*s++;
  163. *p = '\0';
  164. *r1=atoi(num);
  165. p = num;
  166. //s = strchr(s, ':');
  167. while(*s!=']') *p++=*s++;
  168. *p = '\0';
  169. *r2=atoi(num);
  170. }
  171.  
  172.  
  173. int parseGLV(FILE *fp)
  174. {
  175. char line[512], tmp[512];
  176. char pin[32];
  177. char gname[32], gtype[32], pname[32];
  178. unsigned r1, r2, e, s;
  179. char *p, *l;
  180.  
  181. while(fgets(line,511,fp)){
  182. if(isCell(line)) {
  183. strtok(line, "\n");
  184. if(getCell(fp, tmp))
  185. strcat(line, tmp);
  186. //printf("tmp: %s\n", tmp);
  187. //printf("found gate %s\n", line);
  188. parseNameAndType(line, gname, gtype);
  189. strcpy(circuitGates[gCount].type, gtype);
  190. strcpy(circuitGates[gCount].name, gname);
  191. getCellPins(line);
  192. gCount++;
  193. if(strstr(line,"DFF")) ffCount++;
  194. line[0]='\0';
  195. tmp[0]='\0';
  196. } else if(strstr(line, "wire")){
  197. // ignore internal wires inherited from the RTL
  198. if(!strstr(line, "]"))
  199. nCount++;
  200. } else if(strstr(line, "input")!=NULL || strstr(line, "output")!=NULL){
  201. if(strchr(line,'[')) {
  202. parseRange(line, &r1, &r2);
  203. if(r1>r2) {
  204. s = r2;
  205. e = r1;
  206. } else {
  207. s = r1;
  208. e = r2;
  209. }
  210. l = strchr(line, ']');
  211. l++;
  212. p=tmp;
  213. while(ISW(l)) l++;
  214. while(*l != ';') *p++=*l++;
  215. *p='\0';
  216. for(int i=s; i<=e; i++){
  217. sprintf(circuitPorts[portCount].name, "%s[%d]",tmp,i);
  218. if( strstr(line, "output"))
  219. circuitPorts[portCount].dir = 0;
  220. else
  221. circuitPorts[portCount].dir = 1;
  222. circuitPorts[portCount].isBus = 1;
  223. portCount++;
  224. }
  225. }
  226. else {
  227. circuitPorts[portCount].isBus = 0;
  228. l = strstr(line, "put");
  229. //if(l==NULL)
  230. //l=line;
  231. l+=3;
  232. p=tmp;
  233. while(ISW(l)) l++;
  234. while(*l != ';') *p++=*l++;
  235. *p='\0';
  236. strcpy(circuitPorts[portCount].name, tmp);
  237. //circuitPorts[portCount].dir = 1;
  238. if( strstr(line, "output"))
  239. circuitPorts[portCount].dir = 0;
  240. else
  241. circuitPorts[portCount].dir = 1;
  242. portCount++;
  243. }
  244.  
  245. }
  246. }
  247. }
  248.  
  249. /* LIST */
  250. typedef struct {int count; char items[256][32];} SLIST;
  251. SLIST * lstNew(){
  252.  
  253. SLIST *l = (SLIST *) malloc (sizeof(SLIST));
  254. l->count = 0;
  255. return l;
  256. }
  257. int lstAdd(SLIST *lst, char item[32]){
  258. strcpy(lst->items[lst->count], item);
  259. lst->count++;
  260. return (lst->count)-1;
  261. }
  262. int lstFind(SLIST *lst, char item[32]){
  263. int i;
  264. for(i=0; i<(lst->count); i++)
  265. if(!strcmp(lst->items[i],item)) return i;
  266. return -1;
  267. }
  268.  
  269. int lstFindAdd(SLIST *lst, char item[32]){
  270. int x = lstFind(lst, item);
  271. if((x>-1))
  272. return x;
  273. else
  274. return lstAdd(lst,item);
  275. }
  276. /* */
  277.  
  278.  
  279. #define SCL_COUNT 256
  280. int dumpGates()
  281. {
  282. int i, e, f;
  283. int stats[SCL_COUNT], areas[SCL_COUNT];
  284. int area = 0;
  285. SLIST *cells;
  286. cells = lstNew();
  287.  
  288. for(e=0; e<SCL_COUNT; e++) {stats[e]=0;areas[e]=0;}
  289.  
  290. for(i=0; i<gCount; i++) {
  291. //printf("%d: %s\n", i, circuitGates[i].type);
  292. if(strstr( circuitGates[i].type,"_DLATCH_P_")){
  293. printf("Design Exception: The design infers a latch! Instatnce name: %s\n", circuitGates[i].name);
  294. exit(-100);
  295. }
  296. f = lstFindAdd(cells, circuitGates[i].type);
  297. stats[f]++;
  298. areas[f] += doc["cells"][circuitGates[i].type]["area"].GetInt();
  299. area = area + doc["cells"][circuitGates[i].type]["area"].GetInt();
  300. }
  301.  
  302. /*
  303.   for(i=0; i<gCount; i++){
  304.   //printf("JSON : %d\n", doc["cells"][circuitGates[i].type]["area"].GetInt());
  305.   area = area + doc["cells"][circuitGates[i].type]["area"].GetInt();
  306.   for(e=0; e<CCOUNT; e++)
  307.   if(strstr(circuitGates[i].type,cellsPrefix[e])) {
  308.   stats[e]++;
  309.   break;
  310.   }
  311.   }
  312.   */
  313. for(e=0; e<cells->count; e++) {
  314. printf("%s\tCount:%d, Area: %d\n", cells->items[e], stats[e], areas[e]);
  315. }
  316. printf("Total Cell Area: %d \n", area);
  317. printf("Equivalent NAND2x1 Gates count: %d\n",area/doc["cells"]["NAND2X1"]["area"].GetInt());
  318.  
  319. }
  320.  
  321. /*
  322.  int getFO(int g, int fo[]){
  323.  int foc = 0;
  324.  int i;
  325.  
  326.  for(i=0; i<gCount; i++) {
  327.  if(!strcmp(circuitGates[g].pins[]))
  328.  }
  329.  
  330.  
  331.  }
  332.  */
  333.  
  334. int loadSCL(char *fn)
  335. {
  336. int size;
  337. char tmp[32];
  338. FILE* fp = fopen(fn, "r"); // non-Windows use "r"
  339. fseek(fp, 0, SEEK_END); // seek to end of file
  340. size = ftell(fp); // get current file pointer
  341. fseek(fp, 0, SEEK_SET); // seek back to beginning of file
  342.  
  343. char *readBuffer = (char *) malloc(size);
  344.  
  345. FileReadStream is(fp, readBuffer, size);
  346. doc.ParseStream(is);
  347.  
  348. //printf("\nJSON : %d\n", doc["cells"]["AND2X1"]["area"].GetInt());
  349.  
  350. for (Value::ConstMemberIterator itr = doc["cells"].MemberBegin();
  351. itr != doc["cells"].MemberEnd(); ++itr)
  352. {
  353. strcpy(tmp, itr->name.GetString());
  354. if(!strcmp(tmp,"input")) continue;
  355. if(!strcmp(tmp,"output")) continue;
  356. if(!strcmp(tmp,"gnd")) continue;
  357. if(!strcmp(tmp,"vdd")) continue;
  358. strcpy(cellsPrefix[CCOUNT],tmp);
  359. CCOUNT++;
  360.  
  361. printf("Type of member %s %s\n", itr->name.GetString(),cellsPrefix[CCOUNT-1]);
  362. }
  363.  
  364. fclose(fp);
  365. }
  366.  
  367. int main(int argc, char *argv[]) {
  368. FILE *fp;
  369.  
  370. loadSCL("osu350.json");
  371.  
  372. if((fp=fopen(argv[1],"r"))!=NULL){
  373. parseGLV(fp);
  374. }
  375. dumpGates();
  376. printf("total number of cells: %d (%d Flip FLops)\nTotal number of nets: %d\nTotal number of ports: %d\n", gCount, ffCount, nCount, portCount);
  377.  
  378. fclose(fp);
  379.  
  380. //loadSCL("osu350.json");
  381.  
  382. return 0;
  383. }
  384. Sharing settings
  385.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:38: fatal error: rapidjson/filereadstream.h: No such file or directory
 #include "rapidjson/filereadstream.h"
                                      ^
compilation terminated.
stdout
Standard output is empty