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

📄 svf2loopsvf.c

📁 在嵌入式系统中对Lattice CPLD软件升级时所需的VME文件生成所需源代码。将一个链上的不同厂家的CPLD产生的SVF文件转换成VME文件
💻 C
📖 第 1 页 / 共 2 页
字号:

						if (getGRA) {

							direction = DetermineShiftStatus(tokenList1[tempTokenCounter],
										tokenList2[tempTokenCounter], &size); 
							/* if shift size is greater than one, return error
							   can only handle shift of one */
							if (size > 1) {
								return 1;
							}
							if (direction) {
								strcpy(globalRepeatAddress, "(SHR 1)\t");
								strcat(globalRepeatAddress, tokenList1[tempTokenCounter]);
							}
							else {
								strcpy(globalRepeatAddress, "(SHL 1)\t");
								strcat(globalRepeatAddress, tokenList1[tempTokenCounter]);
							}

							getGRA = 0;
						}
						else {

							direction = DetermineShiftStatus(tokenList1[tempTokenCounter],
											tokenList2[tempTokenCounter], &size);
							/* if shift size is greater than one, return error
							   can only handle shift of one */
							if (size > 1) {
								return 1;
							}
							if (direction) {
								strcpy(tempGlobalRepeatAddress, "(SHR 1)\t");
								strcat(tempGlobalRepeatAddress, tokenList1[tempTokenCounter]);
							}
							else {
								strcpy(tempGlobalRepeatAddress, "(SHL 1)\t");
								strcat(tempGlobalRepeatAddress, tokenList1[tempTokenCounter]);
							}
						}

						strcat(globalTemplate, "GRA"); /* globalRepeatAddress placeholder */
						strcpy(tokenList1[tempTokenCounter], "");
						strcpy(tokenList2[tempTokenCounter], "");
					}
					else {
				
						/* write VAR to hold place */
						if (strstr(tokenList1[tempTokenCounter], ";")) {
							strcat(globalTemplate, "(VAR);\t");
						}
						else {
							strcat(globalTemplate, "(VAR)\t");
						}
					}


				}
				else {
					strcat(globalTemplate, "(VAR)\t");
				}
			}
			else {
				strcpy(tokenList1[tempTokenCounter], "");
				strcpy(tokenList2[tempTokenCounter], "");	
				return 1;
			}
			
		}
		tempTokenCounter++;
	}
	newTokenCounter = tempTokenCounter;
	strcat(globalTemplate, "\n");
	
	return 0;
}

void WriteToGlobalValues(int offset) {
	int i = oldTokenCounter;

	while ((tokenList1[i] != NULL) && (tokenList2[i] != NULL)) {
		if (strcmp(tokenList1[i], tokenList2[i])) {
	
			if (offset) {
				globalValues[gvcount] = (char*)malloc(strlen(tokenList2[i]) + 2); 
				strcpy(globalValues[gvcount], tokenList2[i]);
				
				if (!strstr(tokenList2[i], ";")) {
					strcat(globalValues[gvcount], ";");
				}
				
				gvcount++;
			}
			else {
				globalValues[gvcount] = (char*)malloc(strlen(tokenList1[i]) + 2); 
				strcpy(globalValues[gvcount], tokenList1[i]);

				if (!strstr(tokenList2[i], ";")) {
					strcat(globalValues[gvcount], ";");
				}

				gvcount++;
			}
	
		}
		i++;
	}


}

void DetermineLoops( char * a_pSVFFilename )
{
	FILE *fptr, *headfptr, *tmpHeadFptr;
	char SIRLine[SIZE];
	char SIRCode[4096], tmpSIRCode1[SIZE], tmpSIRCode2[SIZE];
	char localPattern[SIZE];
	int i = 0;
	unsigned int matching_SIR_found = 0, pattern_found = 0, end_search = 0;
	long int headPos, fptrPos, startPos, tmpHeadPos;
	PatternNum = 0;

	for (i = 0; i < SIZE; i++) {
		startPositions[i] = 0;
	}

	if ((fptr = fopen( a_pSVFFilename , "r")) == NULL)
	{
		printf("\n%s File not found!\n", a_pSVFFilename );
		exit(1);
	}
	
	if ((headfptr = fopen( a_pSVFFilename, "r")) == NULL)
	{
		printf("\n%s File not found!\n", a_pSVFFilename );
		fclose( fptr );
		exit(1);
	}

	headPos = 0;
	i = 0;
	while( !end_search ) {

		/* Set fptr at next line after headfptr */
		startPos = GetNextSIRPos( headPos, a_pSVFFilename );
		
		fseek( headfptr, headPos, SEEK_SET );
		GetNextSIRLine( SIRLine, headfptr );

		/* If no SIR line then exit, else extract the SIR code */
		if ( !strcmp( SIRLine, "" ) ) {
			fclose( fptr );
			fclose( headfptr );
			return;
		}
		else {
			GetSIRCode( SIRCode, SIRLine );
		}

		tmpHeadPos = ftell( headfptr );
		headPos = tmpHeadPos;
		fseek( fptr, tmpHeadPos, SEEK_SET );

		while( !pattern_found ) {
			fptrPos = ftell( fptr );
			GetNextSIRLine( SIRLine, fptr );
			if ( !strcmp( SIRLine, "" ) ) {
				end_search = 1;
				break;
			}
			else {
				GetSIRCode( tmpSIRCode2, SIRLine );
			}

			while ( i < 7 ) {
				i++;
				if ( !strcmp( SIRCode, tmpSIRCode2 ) ) {
					strcpy( localPattern, SIRCode );
					strcat( localPattern, "\n" );
					matching_SIR_found = 1;
					break;
				}
				
				fptrPos = ftell( fptr );
				GetNextSIRLine( SIRLine, fptr );
				if ( !strcmp( SIRLine, "" ) ) {
					end_search = 1;
					break;
				}
				else {
					GetSIRCode( tmpSIRCode2, SIRLine );
				}
			}

			if ( i >= 7 && !matching_SIR_found ) {
				i = 0;
				break;
			}

			/* If matching SIR is found, there could be potentially a pattern
			   match.  Loop to see if pattern exist or if false alarm */
			if ( matching_SIR_found ) {
				
				if ( ( tmpHeadFptr = fopen( a_pSVFFilename, "r" ) ) == NULL ) {
					end_search = 1;
					break;
				}

				fseek( tmpHeadFptr, startPos, SEEK_SET );
				GetNextSIRLine( SIRLine, tmpHeadFptr );
				GetSIRCode( tmpSIRCode1, SIRLine );

				if ( !strcmp( tmpSIRCode1, tmpSIRCode2 ) ) {
					if ( tmpHeadPos == fptrPos ) {
						pattern_found = 1;
						fclose ( tmpHeadFptr );
						break;
					}
				}

				while( tmpHeadPos != fptrPos ) {
					pattern_found = 1;
					GetNextSIRLine( SIRLine, tmpHeadFptr );
					GetSIRCode( tmpSIRCode1, SIRLine );
					tmpHeadPos = ftell( tmpHeadFptr );

					GetNextSIRLine( SIRLine, fptr );
					if ( !strcmp( SIRLine, "" ) ) {
						pattern_found = 0;
						end_search = 1;
						fclose ( tmpHeadFptr );
						break;
					}
					else {
						GetSIRCode( tmpSIRCode2, SIRLine );
					}

					if ( strcmp( tmpSIRCode1, tmpSIRCode2 ) ) {

						/* Set to next SIR prior to entering matching state */
						fseek( fptr, fptrPos, SEEK_SET );
						GetNextSIRLine( SIRLine, fptr );
						
						if (i >=7) {
							i = 0;
						}

						matching_SIR_found = 0;
						pattern_found = 0;

						break;
					}
					
					strcat( localPattern, tmpSIRCode1 );
					strcat( localPattern, "\n" );

				}

				fclose( tmpHeadFptr );

			}
			

		} /* end of while(!pattern_found) */

		while( pattern_found ) {

			/* Count how many times pattern occurs
			   using headfptr, compare with pattern in PatternStr */
			int numOfPattern = 1, numOfToken = 0, count = 0;
			char tempPattern[ SIZE ];
			char *token;
			char **TokenArray;
			unsigned int end_matching = 0;


			/* If pattern is found, write pattern and start position
			   to global table */
			if ( pattern_found ) {
				startPositions[ PatternNum ] = startPos;
				Pattern[ PatternNum ] = (char*)malloc( strlen( localPattern ) + 1 ); 
				strcpy( Pattern[ PatternNum ], localPattern );
			}

			i = 0;
			strcpy(tempPattern, localPattern);
			token = strtok(tempPattern, "\n");
			while (token != NULL) { /* Count the number of tokens in PatternStr */
				token = strtok(NULL, "\n");
				numOfToken++;
			}

			strcpy(tempPattern, localPattern);
					
			TokenArray = (char **)calloc(numOfToken, sizeof(char*));
			if (TokenArray != NULL) {
				TokenArray[count]  = strtok(tempPattern, "\n");
				for (count = 1; count < numOfToken; count++) {
					TokenArray[count]  = strtok(NULL, "\n");
				}
			}
			else {
				printf("Cannot allocate memory for token array\n");
			}
			
			fseek( headfptr, tmpHeadPos, SEEK_SET );
			GetNextSIRLine( SIRLine, headfptr );
			if (!strcmp(SIRLine, "")) {
				end_search = 1;
				break;
			}
			else {
				GetSIRCode( SIRCode, SIRLine );
			}
			
			/* Loop to compare each token individually */
			count = 0;
			while(!strcmp(SIRCode, TokenArray[count])) {
				
				while(++count < numOfToken) {
					
					GetNextSIRLine( SIRLine, headfptr );
					if (!strcmp(SIRLine, "")) {
						end_search = 1;
						break;
					}
					else {
						GetSIRCode( SIRCode, SIRLine );
					}

					if (strcmp(SIRCode, TokenArray[count])) {
						pattern_found = 0;
						matching_SIR_found = 0;
						break;
					}
				}

				if (!pattern_found) {
					break;
				}

				count = 0;
				numOfPattern++;
				headPos = ftell( headfptr );
				GetNextSIRLine( SIRLine, headfptr );
				if (!strcmp(SIRLine, "")) {
					end_search = 1;
					break;
				}
				else {
					GetSIRCode( SIRCode, SIRLine );
				}
			}
			
			/* if the frequency of the pattern is less than 3, remove it because
			   it is likely to be non-loopable */
			if (numOfPattern < 3) {
				startPositions[PatternNum] = NULL;
				free(Pattern[PatternNum]);
				Pattern[PatternNum] = NULL;
				pattern_found = 0;
			}
			else {
				numOfOccurrences[PatternNum] = numOfPattern;
				PatternNum++;
				pattern_found = 0;
			}
			
		}

	}

	fclose(headfptr);
	fclose(fptr);
	BuildSDRTable( a_pSVFFilename );
}

int IsAllZeros(char *a_string) 
{
	char *string;
	char *token;
	unsigned int i = 0;

	string = (char*)malloc(strlen(a_string) + 1);
	strcpy(string, a_string);

	strtok(string, ")");
	token = strtok(string, "(");

	strcpy(string, token);

	while (i < strlen(string)) {
		
		if (string[i] != '0') {

			free(string);
			return 0;
		}
		i++;
	}
	
	free(string);
	string = NULL;
	return 1;
}

int SetGlobalSDRValue(char *array[], int size)
{
	while (size > 0) {
		
		globalSDR = atoi(array[size]);
		if ((globalSDR > 0) && (globalSDR < SDRValues[globalCounter])) {
			break;
		}
		globalSDR = 0;
		size--;
	}

	if (globalSDR == 0) {
		return 0;
	}

	return 1;
}

void FormatGlobalTemplate(char* patternTemplate)
{
	char *token;
	char *string;
	char seps[]   = " \t\n";
	int i = 0;
	
	string = (char*)malloc(strlen(patternTemplate) + 1);
	strcpy(string, patternTemplate);

	token = strtok(string, seps);
	strcpy(patternTemplate, token);
	strcat(patternTemplate, "\t");

	while (token != NULL) {
		
		token = strtok(NULL, seps);
		if (token == NULL) {
			break;
		}
		if (!strstr(token, "GRA")) {
			strcat(patternTemplate, token);
			strcat(patternTemplate, "\t");
		}
		else {
			strcat(patternTemplate, globalRepeatAddress);

			if (strstr(globalRepeatAddress, ";")) {
				strcat(patternTemplate, "\n");
			}
			else {
				strcat(patternTemplate, ";\n");
			}
		}

		if (strstr(token, ";")) {
			strcat(patternTemplate, "\n");
		}
		else if (strstr(token, ")") && !strstr(token, ";")) {
			strcat(patternTemplate, "\n\t\t");
		}

	}

	free(string);
	string = NULL;
}

void BuildSDRTable( char * a_pSVFFilename ) 
{
	int i;
	int SDR_val, tempSDR_val;
	long int start_pos, end_pos; 
	FILE *fptr;
	char Token[256];
	char pattern[256];
	char ReadInString[SIZE];
	char seps[]   = " \t\n";

	if ((fptr = fopen( a_pSVFFilename, "r")) == NULL)
	{
		printf("\n%s File Cannot Open!\n");
		exit(1);
	}

	for(i = 0; i < PatternNum; i++) {

		tempSDR_val = 0;
		SDR_val = 0;
		strcpy(pattern, Pattern[i]);
		strcpy(Token, strtok(pattern, "\n"));
		start_pos = startPositions[i];
		end_pos = GetNextSetPos(Token, a_pSVFFilename, start_pos);
		
		while (start_pos < end_pos) {

			fseek( fptr, start_pos, SEEK_SET );
			fgets(ReadInString, 255, fptr);
			start_pos = ftell( fptr );

			if (strstr(ReadInString, "SDR")) {
				strtok(ReadInString, seps);
				tempSDR_val = atoi(strtok(NULL, seps));

				if (tempSDR_val > SDR_val) {
					SDRValues[i] = tempSDR_val;
					SDR_val = tempSDR_val;
				}
			}
		}
	}

	fclose(fptr);
}

int DetermineShiftStatus(char *address1, char *address2, int *size)
{
	char *temp1;
	char *temp2;
	unsigned int i;
	int rcode = 0;
	char ch1;
	char ch2;
	int number1;
	int number2;
	int shift1;
	int shift2;
	*size = 0;

	temp1 = (char *)calloc(strlen(address1) + 1, sizeof(char));
	temp2 = (char *)calloc(strlen(address2) + 1, sizeof(char));

	for (i = 0; i < strlen(address1); i++) {
		temp1[i] = address1[i];
		temp2[i] = address2[i];
	}

	for (i = 1; i < SIZE; i++) {
		ch1 = temp1[i];
		ch2 = temp2[i];
		number1 = atoi(&ch1);
		number2 = atoi(&ch2);

		if (number1 > number2) {
			shift1 = log10(number1)/log10(2);
			if (!number2)
				shift2 = -1;
			else
				shift2 = log10(number2)/log10(2);
			*size = shift1 - shift2; 
			rcode = 1;
			break;
		}

		if (number1 < number2) {
			if (!number1)
				shift1 = -1;
			else
				shift1 = log10(number1)/log10(2);
			shift2 = log10(number2)/log10(2);
			*size = shift2 - shift1;
			rcode = 0;
			break;
		}
	}

	free(temp1);
	free(temp2);

	return rcode;
}

void FreeMem() {
	int i;
	int count;

	for (i = 0; i < gvcount; i++) {
		free(globalValues[i]);
		globalValues[i] = NULL;
	}
		
	count = 0;
	while(tokenList1[count]) {
		free(tokenList1[count]);
		tokenList1[count] = NULL;
		count++;
	}

	count = 0;
	while(tokenList2[count]) {
		free(tokenList2[count]);
		tokenList2[count] = NULL;
		count++;
	}

	free (tokenList1); 
	free (tokenList2);
	free (globalValues);

}

⌨️ 快捷键说明

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