📄 svf2loopsvf.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include "Utilities.h"
#include "svf2loopsvf.h"
int iLoopVMEOption = 0; /* Set default to no loop */
int shiftsize = 0;
int iLoopIndex = 0;
int totalbytecount = 0; /*counts the number of bytes in the VME file*/
int ** Repeat = NULL;
int * PatternNumArray = NULL;
int ** SDRValueArray = NULL;
int heapsize = 0; /*counts the number of bytes in each repeat loop*/
unsigned int inRepeat = 0;
int gvcount = 0; /* global values count */
int PatternNum = 0; /*tracks the number of patterns*/
void GetSIRCode( char * a_pszSIRCode, const char * a_pszSIRLine ) {
char * pcToken;
char * pszSIRLine;
pszSIRLine = ( char * ) malloc( strlen( a_pszSIRLine ) + 1 );
strcpy( pszSIRLine, a_pszSIRLine );
pcToken = strtok( pszSIRLine, "(" );
pcToken = strtok( NULL, ")" );
if ( pcToken ) {
strcpy( a_pszSIRCode, pcToken );
}
else {
strcpy( a_pszSIRCode, "" );
}
free( pszSIRLine );
pszSIRLine = NULL;
}
void GetNextSIRLine( char * a_pszSIRLine, FILE * a_fptr )
{
int iSIRIndex = 0;
char cReadChar;
if ( !a_pszSIRLine ) {
return;
}
strcpy( a_pszSIRLine, "" );
while( !feof( a_fptr ) ) {
iSIRIndex = 0;
cReadChar = fgetc( a_fptr );
if ( cReadChar == 'S' || cReadChar == 's' ) {
a_pszSIRLine[ iSIRIndex++ ] = cReadChar;
cReadChar = fgetc( a_fptr );
if ( cReadChar == 'I' || cReadChar == 'i' ) {
a_pszSIRLine[ iSIRIndex++ ] = cReadChar;
cReadChar = fgetc( a_fptr );
if ( cReadChar == 'R' || cReadChar == 'r' ) {
a_pszSIRLine[ iSIRIndex++ ] = cReadChar;
while ( ( cReadChar = fgetc( a_fptr ) ) != ';' ) {
a_pszSIRLine[ iSIRIndex++ ] = cReadChar;
}
a_pszSIRLine[ iSIRIndex++ ] = cReadChar;
a_pszSIRLine[ iSIRIndex ] = '\0';
break;
}
}
}
}
if ( !iSIRIndex ) {
strcpy( a_pszSIRLine, "" );
}
}
long int GetNextSIRPos( long int a_liPosition, char * a_pSVFFilename )
{
char TmpStr[256];
long int liReturnPosition = -1L;
FILE * fptrReadSVF = fopen( a_pSVFFilename, "r" );
fseek( fptrReadSVF, a_liPosition, SEEK_SET );
while( !feof( fptrReadSVF ) ) {
liReturnPosition = ftell( fptrReadSVF );
fgets( TmpStr, 255, fptrReadSVF );
TrimLeft( TmpStr );
if ( !strncmp( TmpStr, "SIR", 3 ) ) {
break;
}
}
if ( !feof( fptrReadSVF ) ) {
fclose( fptrReadSVF );
return liReturnPosition;
}
else {
fclose( fptrReadSVF );
return -1L;
}
}
int CreateLoopFile( char * a_pSVFFilename )
{
int iIndex;
int iRetCode;
DetermineLoops( a_pSVFFilename );
iRetCode = CompareData( a_pSVFFilename );
PatternNumArray[ iLoopIndex ] = PatternNum;
if ( iRetCode ) {
SDRValueArray[ iLoopIndex ] = ( int * ) calloc ( PatternNum, sizeof ( int ) );
for ( iIndex = 0; iIndex < PatternNum; iIndex++ ) {
SDRValueArray[ iLoopIndex ] [ iIndex ] = SDRValues[ iIndex ];
}
Repeat[ iLoopIndex ] = ( int * ) calloc( PatternNum, sizeof( int ) );
for ( iIndex = 0; iIndex < PatternNum; iIndex++ ) {
Repeat[ iLoopIndex ] [ iIndex ] = RepeatArray[ iIndex ];
}
}
/* Free memory for global variables */
for ( iIndex = 0; iIndex < PatternNum; iIndex++ ) {
free( Pattern[ iIndex ] );
}
return iRetCode;
}
long int GetNextSetPos( const char * a_pszBegPattern, const char * a_pszSVFFilename, long int a_liStartPosition ) {
FILE * f = fopen( a_pszSVFFilename, "r");
char TmpStr[SIZE];
char Token[SIZE];
long int returnPos;
strcpy(TmpStr, "(");
strcat(TmpStr, a_pszBegPattern);
strcat(TmpStr, ")");
strcpy(Token, TmpStr);
strcpy(TmpStr, "");
fseek(f, a_liStartPosition, SEEK_SET );
fgets(TmpStr, 255, f);
while(!strstr(TmpStr, ";")) {
fgets(TmpStr, 255, f);
}
fgets(TmpStr, 255, f); /* increment file pointer */
while(!strstr(TmpStr, Token)) {
fgets(TmpStr, 255, f);
}
returnPos = ftell( f );
fgets(TmpStr, 255, f);
TrimLeft(TmpStr);
while(!strstr(TmpStr, "SIR")) {
returnPos--;
fseek( f, returnPos, SEEK_SET );
fgets(TmpStr, 255, f);
TrimLeft(TmpStr);
}
fclose(f);
return returnPos;
}
int CompareData( const char * a_pszSVFFilename )
{
FILE *fp1, *fp2;
int i;
int count;
int repeatCounter;
long int pos1 = 0, pos2 = 0, nonUniformPos = 0;
char pattern[SIZE], TmpStr[MAXSIZE], TmpStr1[MAXSIZE], TmpStr2[MAXSIZE];
char tempGlobalTemplate[MAXSIZE]; /*temporary global template*/
char Token[256];
int rcode = 0;
int previousrcode = 0;
int numOfRepeats;
if (PatternNum == 0) {
/* if no patterns were found, return immediately*/
return 0;
}
if ((fp1 = fopen(a_pszSVFFilename, "r")) == NULL)
{
printf("\n%s File not found!\n", a_pszSVFFilename);
exit(1);
}
if ((fp2 = fopen(a_pszSVFFilename, "r")) == NULL)
{
printf("\n%s File not found!\n", a_pszSVFFilename);
fclose( fp1 );
exit(1);
}
/* Print beginning non-looping information*/
while(pos1 != startPositions[0]) {
fgets(TmpStr, 255, fp1);
fprintf(loopfptr, TmpStr);
pos1 = ftell( fp1 );
}
for (i = 0; i < PatternNum; i++) {
newTokenCounter = 0;
oldTokenCounter = 0;
repeatCounter = 0;
numOccur = numOfOccurrences[i];
pos1 = startPositions[i];
fseek( fp1, pos1, SEEK_SET );
strcpy(globalTemplate, "");
gvcount = 0;
count = gvcount;
globalCounter = i;
globalSDR = 0;
getGRA = 1;
numOfRepeats = 0;
fprintf(loopfptr, "\n! Begin Pattern %d\n\n", i + 1);
/* Gets the first token of the pattern to locate
the next position of the matching pattern*/
strcpy(pattern, Pattern[i]);
strcpy(Token, strtok(pattern, "\n"));
pos2 = GetNextSetPos(Token, a_pszSVFFilename, pos1);
fseek( fp2, pos2, SEEK_SET );
/* reset fp1 to SIR */
pos1 = startPositions[i];
fseek( fp1, pos1, SEEK_SET );
/* allocate memory for tokenlists and global values */
tokenList1 = (char**)calloc(numOfOccurrences[i] * 15 * 5, sizeof(char*));
tokenList2 = (char**)calloc(numOfOccurrences[i] * 15 * 5, sizeof(char*));
globalValues = (char**)calloc(numOfOccurrences[i] * 40, sizeof(char*));
/* Determine template and comapre with the rest of the matching sets */
while (numOccur > 0) {
strcpy(globalTemplate, "");
oldTokenCounter = newTokenCounter;
fseek( fp1, pos1, SEEK_SET );
pos2 = GetNextSetPos(Token, a_pszSVFFilename, pos1);
fseek( fp2, pos2, SEEK_SET );
fseek( fp1, pos1, SEEK_SET );
nonUniformPos = pos1;
while (pos2 > pos1) {
fgets(TmpStr1, 255, fp1);
fgets(TmpStr2, 255, fp2);
/* eliminate white lines and comments from the template */
if (strcmp(TmpStr1, "\n") && strcmp(TmpStr2, "\n")) {
if (!strstr(TmpStr1, "!") && !strstr(TmpStr2, "!")) {
/* Get line of first set */
if (!IsLineComplete(TmpStr1)) {
GetCompleteLine(fp1, TmpStr1);
}
/* Get line of second set */
if (!IsLineComplete(TmpStr2)) {
GetCompleteLine(fp2, TmpStr2);
}
/*Compare first set line with second set line*/
if (strcmp(TmpStr1, TmpStr2)) {
/* Checks to see if strings can be arranged as a
template. If not then print the whole pattern
section without loops */
rcode = DetermineTemplateLine(TmpStr1, TmpStr2);
if (rcode) {
/* non-uniform SVF */
strcpy(TmpStr, "");
fseek( fp1, nonUniformPos, SEEK_SET );
while(pos2 > nonUniformPos) {
fgets(TmpStr1, 255, fp1);
strcat(TmpStr, TmpStr1);
if (strlen(TmpStr) > MAXSIZE) {
/* return loop error, resume flat VME */
fclose(fp1);
fclose(fp2);
FreeMem();
return 0;
}
nonUniformPos = ftell( fp1 );
}
/* tnt 5/22/02/: delete tokenList to prevent
writing unwanted data */
count = 0;
while(tokenList1[count]) {
free(tokenList1[count]);
tokenList1[count] = NULL;
count++;
}
count = 0;
while(tokenList2[count]) {
free(tokenList2[count]);
tokenList2[count] = NULL;
count++;
}
count = 0;
pos1 = ftell( fp1 );
break;
}
}
else {
/* if match, continue loop */
strcat(globalTemplate, TmpStr1);
}
}
}
pos1 = ftell( fp1 );
}
if (numOccur == numOfOccurrences[i]) {
strcpy(tempGlobalTemplate, globalTemplate);
numOccur--;
}
/* if previousrcode exist, then tempGlobalTemplate
is junk so copy over new globalTemplate */
if (previousrcode) {
strcpy(tempGlobalTemplate, globalTemplate);
previousrcode = 0;
}
if (rcode) {
if(numOccur + 1 == numOfOccurrences[i]) {
fprintf(loopfptr, TmpStr);
fprintf(loopfptr, "\n");
previousrcode = 1;
rcode = 0;
}
else {
/* return loop error, resume flat VME */
fclose(fp1);
fclose(fp2);
FreeMem();
return 0;
}
}
else {
/* if subloops exist */
if (strcmp(tempGlobalTemplate, globalTemplate)) {
fprintf(loopfptr, "REPEAT %d;\n", repeatCounter);
FormatGlobalTemplate(tempGlobalTemplate);
fprintf(loopfptr, tempGlobalTemplate);
fprintf(loopfptr, "ENDLOOP;\n");
while(count < gvcount) {
fprintf(loopfptr, globalValues[count]);
fprintf(loopfptr, "\n");
count++;
}
strcpy(globalRepeatAddress, tempGlobalRepeatAddress);
count = gvcount;
repeatCounter = 1;
WriteToGlobalValues(0);
fprintf(loopfptr, "\n");
strcpy(tempGlobalTemplate, globalTemplate);
numOfRepeats++;
}
else {
WriteToGlobalValues(0);
repeatCounter++;
}
}
numOccur--;
}
repeatCounter++;
fprintf(loopfptr, "REPEAT %d;\n", repeatCounter);
FormatGlobalTemplate(globalTemplate);
fprintf(loopfptr, globalTemplate);
fprintf(loopfptr, "ENDLOOP;\n");
numOfRepeats++;
RepeatArray[i] = numOfRepeats;
WriteToGlobalValues(1);
while (count < gvcount) {
fprintf(loopfptr, globalValues[count]);
fprintf(loopfptr, "\n");
count++;
}
FreeMem();
fprintf(loopfptr, "\n");
gvcount = 0;
fprintf(loopfptr, "\n! End Pattern %d\n\n", i + 1);
/* Print non-looping information */
pos1 = ftell( fp2 );
while((pos1 < startPositions[i+1]) && (startPositions[i+1] != 0)) {
fgets(TmpStr, 255, fp2);
fprintf(loopfptr, TmpStr);
pos1 = ftell( fp2 );
}
}
/* Print the rest of non-looping information */
while(!feof(fp2)) {
fgets(TmpStr, 255, fp2);
/* eliminate duplicate last lines */
if (feof(fp2)) {
break;
}
fprintf(loopfptr, TmpStr);
}
fclose(fp1);
fclose(fp2);
return 1;
}
int IsLineComplete( const char * a_pszSVFLine )
{
if ( !a_pszSVFLine ) {
return 0;
}
if ( !strstr( a_pszSVFLine, ";" ) ) {
return 0;
}
else {
return 1;
}
}
void GetCompleteLine( FILE * a_fptrRead, char * a_pszSVFLine )
{
char szTmp[ SIZE ] = { 0 };
TrimRight( a_pszSVFLine );
while( !strstr( a_pszSVFLine, ";" ) ) {
fgets( szTmp, SIZE, a_fptrRead );
TrimRight( szTmp );
if (!strstr(szTmp, "TDO") && !strstr(szTmp, "MASK") && !strstr(szTmp, "TDI")
&& !strstr(szTmp, "DMASK")) {
TrimLeft(szTmp);
}
else {
strcat(a_pszSVFLine, "\n");
}
strcat(a_pszSVFLine, szTmp);
}
strcat(a_pszSVFLine, "\n");
}
int DetermineTemplateLine(char *string1, char*string2)
{
char *token;
char seps[] = " \t\n";
int tempTokenCounter;
int tempTokenCounter1;
int tempTokenCounter2;
int inSDRLine = 0;
int address = 0;
int size;
int direction;
tempTokenCounter1 = newTokenCounter;
token = strtok(string1, seps);
while(token != NULL) {
tokenList1[tempTokenCounter1] = (char*)malloc(strlen(token) + 1);
strcpy(tokenList1[tempTokenCounter1], token);
token = strtok(NULL, seps);
tempTokenCounter1++;
}
tempTokenCounter2 = newTokenCounter;
token = strtok(string2, seps);
while(token != NULL) {
tokenList2[tempTokenCounter2] = (char*)malloc(strlen(token) + 1);
strcpy(tokenList2[tempTokenCounter2], token);
token = strtok(NULL, seps);
tempTokenCounter2++;
}
/* If the number of tokens are different, then this section is
unloop-able. */
if (tempTokenCounter1 != tempTokenCounter2) {
return 1;
}
tempTokenCounter = newTokenCounter;
if (globalSDR) {
if (!strcmp(tokenList1[tempTokenCounter], "SDR")) {
inSDRLine = 1; /* We're in an SDR Line */
if (globalSDR == atoi(tokenList1[tempTokenCounter + 1])) {
address = 1; /* The address in these lines should not be
written out to globalvalues table */
}
}
}
while ((tokenList1[tempTokenCounter] != NULL) &&
(tokenList2[tempTokenCounter] != NULL)) {
if (!strcmp(tokenList1[tempTokenCounter], tokenList2[tempTokenCounter])) {
/* write out to globalTemplate */
if (strstr(tokenList1[tempTokenCounter], "TDO") ||
strstr(tokenList1[tempTokenCounter], "MASK")) {
strcat(globalTemplate, "\n\t\t");
}
strcat(globalTemplate, tokenList1[tempTokenCounter]);
strcat(globalTemplate, "\t");
}
else {
if (strstr(tokenList1[tempTokenCounter], "(")
&& strstr(tokenList1[tempTokenCounter], ")")) {
if (strstr(tokenList1[tempTokenCounter], ";") ||
strstr(tokenList1[tempTokenCounter + 1], ";")) {
/* if globalSDR is zero then set globalSDR */
if (!globalSDR) {
if (IsAllZeros(tokenList1[tempTokenCounter])) {
/* if address is all zeros, then set globalSDR and
exit, cannot shift all zero address */
if (SetGlobalSDRValue(tokenList1, tempTokenCounter)) {
return 1;
}
else {
strcat(globalTemplate, "(VAR)\t");
}
}
else {
/* if address is not all zeros, then set globalSDR and
set globalRepeatAddress and write placeholder */
if (SetGlobalSDRValue(tokenList1, tempTokenCounter)) {
direction = DetermineShiftStatus(tokenList1[tempTokenCounter],
tokenList2[tempTokenCounter], &size);
getGRA = 0;
/* 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]);
}
strcat(globalTemplate, "GRA"); /* globalRepeatAddress placeholder */
strcpy(tokenList1[tempTokenCounter], "");
strcpy(tokenList2[tempTokenCounter], "");
}
else {
/* write VAR to hold place */
strcat(globalTemplate, "(VAR);\t");
}
}
}
else if (address) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -