// parser.cpp : Defines the entry point for the console application.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Parsing a Netlist File
// Author : Digvijay Rajurkar
typedef struct{
char name[40];
unsigned int outputs;//no. of output to the gate
unsigned int inputs;// no. of inputs to the gate
unsigned int index;
unsigned int child[90];
unsigned int parents[20];
unsigned char type;// primary out or intermediate
unsigned int parent;
unsigned int max_delay;
unsigned char visited; // arrival time
unsigned char visited1; // arrival time
unsigned int path_delay;
} Gate;
Gate tempgate[10000];
unsigned int i, j, k,sum=0,max=0;
unsigned int gate_count = 0,m=0,x=0,w=0;
unsigned int input_count = 0, output_count = 0;
unsigned int loopbreaker = 0;
int read_netlist(char* tp)
{
char templine[200], commentline, newline=1;
FILE *inputnetlist = 0;
char *token = 0, *success = 0;
i = 0;
inputnetlist = fopen(tp, "r");
if (inputnetlist == NULL)
{
printf("Error opening netlist\n");
return 0;
}
// read and discard the header line
//Assume headers and comments are inserted only at the top of file
commentline = 1; // Assuming First line is always header
do
{
success = fgets(templine, sizeof(templine), inputnetlist);
if (success == NULL) // end of file?
break;
if (templine[0] == '#' || templine[0] == '\n'|| templine[0] == '\r')
commentline = 1;
else
{
commentline = 0;
token = strtok(templine, "\t,()= ");
}
} while (commentline == 1);
while (!feof(inputnetlist))
{
if (i != 0)
{
success = fgets(templine, sizeof(templine), inputnetlist);
if (success == NULL) // end of file?
break;
if (templine[0] == '\n' || templine[0]== '\r')
{
newline = 0;
}
else
{
newline = 1;
}
token = strtok(templine, "\t,()= ");
}
if (token != NULL && newline == 1) // blank line or end of line
{
// case sensitive check and separate from interconnects
if ((token[0] == 'I') || (token[0] == 'i'))
{
tempgate[i].inputs=0;
tempgate[i].type=5;
tempgate[i].index = gate_count;
token = strtok(NULL, "\t,()= "); // name
strcpy((char*)tempgate[i].name,token);
i++;
gate_count++;
input_count++;
}
else if ((token[0] == 'O') || (token[0] == 'o'))
{
//defining output pin
tempgate[i].outputs = 0;
tempgate[i].type=2;
token = strtok(NULL, "\t,()= "); // name
strcpy((char*)tempgate[i].name, token);
tempgate[i].index = gate_count;
tempgate[i].parent=0;
tempgate[i].max_delay=0;
j = 0; loopbreaker = 0;
while (j < input_count && loopbreaker == 0)
{
if (strcmp((char*)tempgate[j].name, token) == 0)
{
loopbreaker = 1;
}
j++;
}
if (loopbreaker == 1) //new
{
tempgate[j-1].child[tempgate[j-1].outputs]=tempgate[i].index;
tempgate[j-1].outputs=1;
tempgate[i].parents[tempgate[i].inputs]=tempgate[j-1].index;
tempgate[i].inputs=1;
}
i++;
gate_count++;
output_count++;
}
else
{
j = input_count; loopbreaker = 0;
while (j < gate_count && loopbreaker == 0)
{
if (strcmp((char*)tempgate[j].name, token) == 0)
{
loopbreaker = 1;
}
j++;
}
if (loopbreaker == 0) //new
{
tempgate[i].outputs=0;
tempgate[i].type=1;
tempgate[i].index = gate_count;
strcpy((char*)tempgate[i].name, token);
token = strtok(NULL, "\t,()= ");//function
newline = 1;
while (newline == 1)
{
token = strtok(NULL, "\t,()= ");//input
if (token[0] == '\n' || token [0]== '\r')
{
newline = 0;
loopbreaker = 0;
i=gate_count;
i++;
gate_count++;
}
else
{
j = 0; loopbreaker = 0;
while (j < gate_count && loopbreaker == 0)
{
if (strcmp((char*)tempgate[j].name, token) == 0)
{
loopbreaker = 1;
}
j++;
}
if (loopbreaker == 1) // input name exists
{
if(tempgate[j-1].type == 2)//name is existing output
{
if(tempgate[j-1].parent == 0)
{
//defining output gate
m=gate_count;
tempgate[m+1].child[tempgate[m+1].outputs]= tempgate[j-1].index;
tempgate[m+1].outputs = 1;
tempgate[m+1].type = 3; // output gate
strcpy(tempgate[m+1].name, tempgate[j-1].name);
tempgate[m+1].index = gate_count+1;
tempgate[j-1].parent = tempgate[m+1].index;
tempgate[j-1].parents[tempgate[j-1].inputs]=tempgate[m+1].index;
tempgate[j-1].inputs=1;
//i++;
gate_count++;
tempgate[m+1].child[tempgate[m+1].outputs] = tempgate[i].index;
tempgate[m+1].outputs++;
tempgate[i].parents[tempgate[i].inputs]=tempgate[m+1].index;
tempgate[i].inputs++;
}
else
{
tempgate[tempgate[j-1].parent].child[tempgate[tempgate[j-1].parent].outputs] = tempgate[i].index;
tempgate[tempgate[j-1].parent].outputs++;
tempgate[i].parents[tempgate[i].inputs]=tempgate[tempgate[j-1].parent].index;
tempgate[i].inputs++;
}
}
else
{
tempgate[j - 1].child[tempgate[j - 1].outputs] = tempgate[i].index;
tempgate[j - 1].outputs++;
tempgate[i].parents[tempgate[i].inputs]=tempgate[j-1].index;
tempgate[i].inputs++;
}
}
else // new input name
{
m=gate_count;
strcpy((char*)tempgate[m + 1].name, token);
tempgate[m + 1].index = gate_count + 1;
tempgate[m + 1].child[tempgate[m + 1].outputs] = tempgate[i].index;
tempgate[m + 1].outputs=1;
tempgate[i].parents[tempgate[i].inputs]=tempgate[m+1].index;
tempgate[i].inputs++;
gate_count++;
}
}//else
}//while
} //if
else if (loopbreaker == 1)//existing name
{
if(tempgate[j-1].type == 2)//name is existing output
{
if(tempgate[j-1].parent == 0)
{
//defining output gate
m=gate_count;
tempgate[m].child[tempgate[m].outputs]= tempgate[j-1].index;
tempgate[m].outputs = 1;
tempgate[m].type = 3; // output gate
strcpy(tempgate[m].name, tempgate[j-1].name);
tempgate[m].index = gate_count;
tempgate[j-1].parent = tempgate[m].index;
//i++;
tempgate[j-1].parents[tempgate[j-1].inputs]=tempgate[m].index;
tempgate[j-1].inputs++;
gate_count++;
i=gate_count;
}
j=tempgate[j-1].parent + 1;
}
token = strtok(NULL, "\t,()= ");//function
newline = 1;
while (newline == 1)
{
token = strtok(NULL, "\t,()= ");//input
if (token[0] == '\n' || token[0]=='\r')
{
newline = 0;
}
else
{
k = 0; loopbreaker = 0;
while (k < gate_count && loopbreaker == 0)
{
if (strcmp(tempgate[k].name, token) == 0)
{
loopbreaker = 1;
}
k++;
}
if (loopbreaker == 1)
{
if(tempgate[k-1].type == 2)//name is existing output
{
if(tempgate[k-1].parent == 0)
{
//defining output gate
m=gate_count;
tempgate[m].child[tempgate[m].outputs]= tempgate[k-1].index;
tempgate[m].outputs = 1;
tempgate[m].type = 3; // output gate
strcpy(tempgate[m].name, tempgate[k-1].name);
tempgate[m].index = gate_count;
tempgate[k-1].parent = tempgate[m].index;
tempgate[k-1].parents[tempgate[k-1].inputs]=tempgate[m].index;
tempgate[k-1].inputs++;
gate_count++;
i=gate_count;
tempgate[m].child[tempgate[m].outputs] = tempgate[j-1].index;
tempgate[m].outputs++;
tempgate[j-1].parents[tempgate[j-