/* VIDARR AMB/APB logical to physical channel mapper */ /* Input: text database. Output: AMB physical channel # (argument) for FPGA logical channel (index) */ #include /* Standard input/output definitions */ #include /* Standard libraries definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #include #define channels 64 int ahex2int(char a, char b) { a = (a <= '9') ? a - '0' : (a & 0x7) + 9; b = (b <= '9') ? b - '0' : (b & 0x7) + 9; return (a << 4) + b; } int main(void) { int ncol = 3, nrow = channels+1, wSize = 4; /* nrow larger by 1 for labels */ char map[ncol][nrow][wSize+1]; /* additional NULL character to terminate string */ for (int i = 0; i < ncol; i++) for (int j = 0; j < nrow; j++) strncpy(map[i][j], "XXXX", wSize+1); int conv[channels], trim[channels]; for (int i = 0; i < channels; i++) {conv[i] = 99; trim[i] = 0;} /* Open file for read */ FILE *fp = fopen("frontend.txt", "r"); if (fp == NULL) {printf("no ini file found\n"); return (0);} fseek (fp, 0, SEEK_END); long tSize = ftell(fp); rewind(fp); // printf("file size in bytes %ld\n", tSize); /* allocate buffer for the entire file */ char *text = (char *) malloc(sizeof(char) *tSize); /* memory for the entire file */ char **lines = (char **)malloc(sizeof(void *)*tSize); /* memory for lines */ char **words = (char **)malloc(sizeof(void *)*tSize); /* memory for words */ if (text == NULL || lines == NULL || words == NULL) {perror("Unable to allocate memory for data\n"); exit(1);} /* copy the entire file into buffer */ fread(text, 1, tSize, fp); fclose(fp); /* get rid of these pesky CRs (this will result in empty lines) */ char *index = text; for (int i = 0; i < tSize; i++) if (*(index + i) == '\r') *(index + i) = '\n'; /* int i will be casted to long */ /* split text into lines and get rid of empty ones */ int lindex = 0, nscan = 0; while ((( *(lines + lindex) = strsep(&text, "\n")) != NULL)) { char *ltoken = strsep((lines + lindex), "\n"); if (*ltoken != 0x00 && nscan < nrow) { const char delimiters[] = " ,;:\t"; int windex = 0, nrun = 0; while ((( *(words + windex) = strsep(<oken, delimiters)) != NULL)) { char *wtoken = strsep((words + windex), delimiters); if (*wtoken != 0x00 && nrun < ncol) { // printf("col %02d row %02d %s\n", nrun, nscan, wtoken); strncpy(map[nrun][nscan], wtoken, wSize+1); nrun++; } /* if wtoken */ windex++; } /* while windex */ if (nrun < ncol) ncol = nrun; nscan++; } /* if ltoken */ lindex++; } /* while lindex */ if (nscan < nrow) nrow = nscan; /* free up memory */ free (text); free (lines); free (words); /* wrap nrow values to avoid segmentation fault */ // printf("columns %d rows %d\n", ncol, nrow); if (ncol > 3) ncol = 3; if (nrow > channels + 1) nrow = channels + 1; /* fill output array */ for (int i = 1; i < nrow; i++) { char *ind = map[0][i]; char a = ind[wSize/2]; char b = ind[wSize/2+1]; int ambn = ahex2int(a, b); // char *arg = map[1][i]; int apbn = atoi(map[1][i]); int addr = atoi(map[2][i]); // printf("%02d %02d %02d\n", ambn, apbn, trim); /* test range for channels */ if (ambn < 0 || ambn > channels - 1 || apbn < 0 || apbn > channels - 1) printf("values out of range %02d %02d\n", ambn, apbn); else conv[ambn] = apbn; if (ambn < 0 || ambn > channels - 1 || addr < 512 || addr > 575) printf("values out of range %02d %03d\n", ambn, addr); else trim[ambn] = addr; printf("Logical %02d Physical %02d Trim address %04X\n", i-1, conv[i-1], trim[i-1]); } /* for i */ return 1; } /* main */