⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netlist.cpp

📁 uploading the file , the system will delete the file when time expires
💻 CPP
字号:
#include "netlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

bool ReadFromFile(char * filename, Netlist * nl) {
	
	FILE * infile = fopen(filename,"r");
	if(infile==NULL) return false;

	char buff_in[1024];
	char * token;

	// read the first line--it contains the 
	// maximum row length, the number of cells,
	// the number of nets, the cell height,
	// and the channel height
	fgets(buff_in,1024,infile);
	token = strtok(buff_in,"\n\t ");

	if(token==NULL) return false;
	nl->nRowMax = atoi(token);
	if(nl->nRowMax==0) return false;

	token = strtok(NULL," \t\n");

	if(token==NULL) return false;
	nl->nCells = atoi(token);
	if(nl->nCells==0) return false;

	token = strtok(NULL," \t\n");
	if(token==NULL) return false;
	nl->nNets = atoi(token);
	if(nl->nNets==0) return false;

	token = strtok(NULL," \t\n");
	if(token==NULL) return false;
	nl->cell_height = atoi(token);
	if(nl->cell_height==0) return false;

	token = strtok(NULL," \t\n");
	if(token==NULL) return false;
	nl->channel_height = atoi(token);
	// don't error-check this one, zero is ok

	// rowlen, ncells, and nnets now known--allocate space
	nl->cell_widths = new int[nl->nCells];
	nl->nconnections = new int[nl->nNets];
	nl->net_weights = new int[nl->nNets];
	nl->net_connections = new int*[nl->nNets];

	int net_number;
	int i;

	// start getting net weights
	for( i=0;i<nl->nNets;i++) {
		fgets(buff_in,1024,infile);
		token = strtok(buff_in," \t\n");
		if(token==NULL) return false;
		net_number = atoi(&token[1]);
		if(net_number<1 || net_number>nl->nNets) return false;
		token = strtok(NULL," \t\n");
		if(token==NULL) return false;
		nl->net_weights[net_number-1] = atoi(token);

	}

	// these are temporary
	int ** cell_connections = new int*[nl->nCells];
	int * cconnections = new int[nl->nCells];

	int cell_number, token_count; char c;

	// next rows should contain
	for(int j=0;j<nl->nCells;j++) {
		// C<number> <width> N<number> ...

		fgets(buff_in,1024,infile);
		
		// count tokens
		token_count = 0;
		i = 0;

		while((c = buff_in[i])!='\n' && buff_in[i]!=(char)NULL) {
			// start of potential token or non-token
			if(c==' '||c=='\t') {
				i++;
				continue;
			}

			// start of a token...seek through the token
			token_count++;
			while(buff_in[i]!=' '&&buff_in[i]!='\t'&&buff_in[i]!='\n'&&buff_in[i]!=(char)NULL) {
				i++;
			}
		}

		// net_count = token_count - 2;
		// expect token_count - 2 nets
	    if(token_count<=2) return false;

		cconnections[j] = token_count - 2;
		cell_connections[j] = new int[cconnections[j]];

		i = 0;

		token = strtok(buff_in," \t\n");
		if(token==NULL) return false;

		// first token is the cell name
		cell_number = atoi(&token[1]);
		if(cell_number<=0 || cell_number>nl->nCells) return false;

		// second token is cell size
		token = strtok(NULL," \t\n");
		if(token==NULL) return false;
		nl->cell_widths[cell_number-1] = atoi(token);

		// subsequent tokens are connections to nets
		token = strtok(NULL," \t\n");
		while(token!=NULL) {
			cell_connections[j][i] = atoi(&token[1]) - 1;
			if(cell_connections[j][i]==-1) return false;
			i++;
//			printf("NET %i (str=%s) on CELL %i\n",cell_connections[j][i-1]+1,
//				&token[1],j+1);
			token = strtok(NULL," \t\n");
		}

	}

	// now we have a cell-connection list
	// temporary code
/*	printf("Cell connection list:\n");
	for(cell_number=0;cell_number<nl->nCells;cell_number++) {
		printf("C%i:  ",cell_number+1);
		for(net_number=0;net_number<cconnections[cell_number];net_number++) {
			printf("N%i  ",cell_connections[cell_number][net_number]+1);
		}
		printf("\n");
	} 
*/

	// okay, now figure out how many connections each net has
	for(cell_number = 0; cell_number<nl->nCells;cell_number++) {
		for(net_number=0;net_number<cconnections[cell_number];net_number++){
			nl->nconnections[cell_connections[cell_number][net_number]]++;
		}
	}

	// allocate space for each net connection
	for(net_number=0;net_number<nl->nNets;net_number++) {
		nl->net_connections[net_number] = new int[nl->nconnections[net_number]];
		for(int j=0;j<nl->nconnections[net_number];j++) {
			// initially set all connections to -1
			nl->net_connections[net_number][j] = -1;
		}
	}

	int net_name;

	// Convert a cell-connection list to a net-connection list
	for(cell_number=0;cell_number<nl->nCells;cell_number++) {
//		printf("Working on Cell #%i\n",cell_number+1);
		// for each cell, iterate over all the cell's connections
		for(net_number=0;net_number<cconnections[cell_number];net_number++) {
//			printf("Working on Net #%i\n",net_number+1);
			// for each net connection at each cell, add that
			// connection (the cell) to the first open spot (a -1)
			// available in net_connections
			net_name = cell_connections[cell_number][net_number];
			for(int k=0;k<nl->nconnections[net_name];k++) {
				if(nl->net_connections[net_name][k]==-1) {
					nl->net_connections[net_name][k] = cell_number;
//					printf("Adding: NET %i has connection #%i set to %i\n",
//						net_name,k+1,cell_number+1);
					break;
				}
			}
		}

	}

	// now deallocate the memory in the temporary connection list
	for(cell_number=0;cell_number<nl->nCells;cell_number++) {
		delete cell_connections[cell_number];
	}
	delete cell_connections;
	delete cconnections;

	fclose(infile);

	return true;
}

void dump(Netlist * nl) {
	int i;
	printf("Rowmax = %i\tnCells = %i\tnNets = %i\n",
			nl->nRowMax,nl->nCells,nl->nNets);
	printf("Net weights:\n");
	for(i=0;i<nl->nNets;i++)
		printf("%i ",nl->net_weights[i]);
	printf("\nCell widths:\n");
	for(i=0;i<nl->nCells;i++)
		printf("%i ",nl->cell_widths[i]);

	printf("\nConnection Matrix:\n");

	// dump the connection matrix!!!!!!
	for(i=0;i<nl->nNets;i++) {
		printf("N%i: ",i+1);
		for(int j=0;j<nl->nconnections[i];j++) {
			printf("C%i ",nl->net_connections[i][j]+1);
		}
		printf("\n");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -