00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "local.h"
00057 #include <assert.h>
00058 #include <stdio.h>
00059 #include <stdlib.h>
00060
00061 #include <errno.h>
00062 #include <fcntl.h>
00063 #include <sys/ioctl.h>
00064 #include <sys/mman.h>
00065 #include <sys/stat.h>
00066 #include <sys/time.h>
00067 #include <sys/types.h>
00068 #include <stdio.h>
00069 #include <unistd.h>
00070
00071 #include <ctype.h>
00072
00073 #include <popt.h>
00074
00075 typedef unsigned u32;
00076
00077 #include "acq32busprot.h"
00078 #include "llcontrol.h"
00079
00080
00081 struct LLC200_INIT *llc200_init;
00082 int llc200_init_count;
00083
00084 struct ULL_LUT {
00085 const char* key;
00086 unsigned long long mask;
00087 };
00088
00089 template<class T> class InputLineHandler {
00090 protected:
00091 T *array;
00092 int maxcount;
00093 int ix;
00094
00095
00096 public:
00097 InputLineHandler () :
00098 array(0), maxcount(0) {
00099 ix = 0;
00100 }
00101 virtual ~InputLineHandler() {}
00102
00103 virtual void handleLine(char myline[]) {
00104
00105 }
00106
00107 void setMaxCount(int _maxcount) { maxcount = _maxcount; }
00108 void setArray(T* _array) { array = _array; }
00109 int getCount(void) { return ix; }
00110 };
00111
00112
00113 class TextFileReader {
00114 FILE *fp;
00115 public:
00116 TextFileReader(const char* fname) {
00117 fp = fopen(fname, "r");
00118 assert(fp);
00119 }
00120 ~TextFileReader() { fclose(fp); }
00121
00122 char* getLine(char* myline, int maxline) {
00123 return fgets(myline, maxline, fp);
00124 }
00125 };
00126
00127 #define MAXLINE 256
00128
00129 template<class T> class DefinitionFileHandler {
00130 protected:
00131 const char* def_file;
00132
00133 int isComment(const char* my_line) {
00134 if (strlen(my_line) == 1){
00135 return 1;
00136 }else if (my_line[0] == '#'){
00137 return 1;
00138 }else if (strspn(my_line, " \n") == strlen(my_line)){
00139 return 1;
00140 }else{
00141 return 0;
00142 }
00143 }
00144 public:
00145 DefinitionFileHandler(const char* _def_file) : def_file(_def_file) {}
00146
00147 int getMaxLines(void) {
00148 char myline[MAXLINE];
00149 int line_count = 0;
00150
00151 TextFileReader fileReader(def_file);
00152
00153 while(fileReader.getLine(myline, MAXLINE) != 0){
00154 ++line_count;
00155 }
00156
00157 return line_count;
00158 }
00159
00160 void processFile(InputLineHandler<T> *handler) {
00161 char myline[MAXLINE];
00162 TextFileReader fileReader(def_file);
00163
00164 while(fileReader.getLine(myline, MAXLINE) != 0){
00165 if (!isComment(myline)){
00166 handler->handleLine(myline);
00167 }
00168 }
00169 }
00170 };
00171
00172 static unsigned char hexval(char hexchar)
00173
00174 {
00175 if (hexchar >= '0' && hexchar <= '9'){
00176 return hexchar - '0';
00177 }else if (hexchar >= 'A' && hexchar <= 'F'){
00178 return hexchar - 'A' + 10;
00179 }else if (hexchar >= 'a' && hexchar <= 'f'){
00180 return hexchar - 'a' + 10;
00181 }else{
00182 assert(0);
00183 }
00184 }
00185
00186
00187 static void initBuf(DefinitionFileHandler<struct LLC200_INIT>* fileHandler) {
00188 if (!llc200_init_count){
00189 llc200_init_count = fileHandler->getMaxLines();
00190 llc200_init = (LLC200_INIT *)
00191 calloc(llc200_init_count, LLC200_INIT_SZ);
00192
00193 assert(llc200_init);
00194 }
00195 }
00196
00197
00198
00199
00200 class FtwHandler : public InputLineHandler<struct LLC200_INIT> {
00201 public:
00202 FtwHandler() :
00203 InputLineHandler<struct LLC200_INIT>() {}
00204
00205 virtual void handleLine(char myline[]){
00206 if (ix == maxcount){
00207 return;
00208 }
00209 unsigned char ftw1[6];
00210
00211
00212
00213
00214 for (int ic = 0, iw = 0; iw < 6; ++ic){
00215 if (isxdigit(myline[ic])){
00216 if ((ic&1) == 0){
00217 ftw1[iw] = hexval(myline[ic]) << 4;
00218 }else{
00219 ftw1[iw++] |= hexval(myline[ic]);
00220 }
00221 }else{
00222 return;
00223 }
00224 }
00225
00226 memcpy(array[ix].dds_ftw, ftw1, sizeof(ftw1));
00227
00228 array[ix].mask |= LLC200_INIT_MASK_DDS_FTW;
00229 array[ix].marker = LLC200_INIT_MAGIC_MARKER;
00230 ix++;
00231 }
00232 };
00233
00234
00235
00236 class QdacHandler : public InputLineHandler<struct LLC200_INIT> {
00237 public:
00238 QdacHandler() :
00239 InputLineHandler<struct LLC200_INIT>() {}
00240
00241 virtual void handleLine(char myline[]){
00242 if (ix == maxcount){
00243 return;
00244 }
00245 short qvc = atoi(myline);
00246
00247 memcpy(&array[ix].dds_ftw[6], &qvc, sizeof(short));
00248
00249 array[ix].mask |= LLC200_INIT_MASK_DDS_QDAC;
00250 array[ix].marker = LLC200_INIT_MAGIC_MARKER;
00251 ix++;
00252 }
00253 };
00254
00255
00256
00257
00258 static void load(InputLineHandler<struct LLC200_INIT>* lineHandler, const char* def_file)
00259 {
00260 DefinitionFileHandler<struct LLC200_INIT>* fileHandler =
00261 new DefinitionFileHandler<struct LLC200_INIT> (def_file);
00262
00263 initBuf(fileHandler);
00264
00265 lineHandler->setMaxCount(llc200_init_count);
00266 lineHandler->setArray(llc200_init);
00267
00268 fileHandler->processFile(lineHandler);
00269
00270 llc200_init_count = lineHandler->getCount();
00271 delete lineHandler;
00272 }
00273
00274
00275
00276 class IntclkHandler : public InputLineHandler<struct LLC200_INIT> {
00277 public:
00278 IntclkHandler() {}
00279
00280 virtual void handleLine(char myline[]) {
00281 if (ix == maxcount){
00282 return;
00283 }
00284 unsigned int_clk;
00285
00286 if (sscanf(myline, "%u", &int_clk) == 1){
00287 array[ix].int_clk = int_clk;
00288 array[ix].mask |= LLC200_INIT_MASK_INTCLK;
00289 array[ix].marker = LLC200_INIT_MAGIC_MARKER;
00290 ix++;
00291 }
00292 }
00293 };
00294
00295
00296 int strsplit(char *str, char *argv[], int maxargs, const char* delim)
00297
00298 {
00299 int iarg;
00300 char *s1, *s2;
00301
00302 for (iarg = 0, s1 = str; iarg < maxargs && (s2 = strstr(s1, delim)); ){
00303 *s2 = '\0';
00304 argv[iarg++] = s1;
00305 s1 = s2 + strlen(delim);
00306 }
00307 if (s1 != 0){
00308 argv[iarg++] = s1;
00309 }
00310 return iarg;
00311 }
00312
00313
00314 class InputOffsetHandler : public InputLineHandler<struct LLC200_INIT> {
00315 public:
00316 virtual void handleLine(char myline[]) {
00317 if (ix == maxcount){
00318 return;
00319 }
00320 char* argv[16];
00321 int nargs = strsplit(myline, argv, 16, ",");
00322
00323 for (int ic = 0; ic != nargs; ++ic){
00324 array[ix].offsets[ic] = strtol(argv[ic], 0, 0);
00325 }
00326
00327 array[ix].mask |= LLC200_INIT_MASK_OFFSETS;
00328 array[ix].marker = LLC200_INIT_MAGIC_MARKER;
00329 ix++;
00330 }
00331 };
00332
00333 #include "CONSTS/M2-consts.h"
00334 #include "CONSTS/M5-consts.h"
00335 #include "CONSTS/M2-consts.c"
00336 #include "CONSTS/M5-consts.c"
00337
00338 static char *chomp(char *str) {
00339 if (str[strlen(str)-1] == '\n'){
00340 str[strlen(str)-1] = '\0';
00341 }
00342 return str;
00343 }
00344
00345
00346 class VRangeHandler : public InputLineHandler<struct LLC200_INIT> {
00347 const struct ULL_LUT* lut;
00348
00349 unsigned long long lookup(const char* key){
00350 const struct ULL_LUT* cursor = lut;
00351
00352 while(cursor->key != 0){
00353 if (strcmp(cursor->key, key) == 0){
00354 return cursor->mask;
00355 }
00356 cursor++;
00357 }
00358 fprintf(stderr, "WARNING: key \"%s\" NOT FOUND\n", key);
00359 return 0;
00360 }
00361 public:
00362 VRangeHandler(const struct ULL_LUT* _lut) : lut(_lut) {}
00363
00364 virtual void handleLine(char myline[]) {
00365 if (ix == maxcount){
00366 return;
00367 }
00368 unsigned long long vrange = 0ULL;
00369
00370
00371 for (char* ps = myline; (ps = strtok(ps, " ")) != 0; ps = 0){
00372 chomp(ps);
00373 vrange |= lookup(ps);
00374 }
00375
00376 array[ix].vranges.ull = vrange;
00377 array[ix].mask |= LLC200_INIT_MASK_RANGE;
00378 array[ix].marker = LLC200_INIT_MAGIC_MARKER;
00379 ix++;
00380 }
00381 };
00382
00383
00384
00385 static void writeOutput(const char* outfile)
00386 {
00387 FILE *fp = fopen(outfile, "w");
00388 assert(fp);
00389
00390 fwrite(llc200_init, LLC200_INIT_SZ, llc200_init_count, fp);
00391 fclose(fp);
00392 }
00393
00394
00395
00396
00397 int main( int argc, const char* argv[] )
00398 {
00399 char *def_file;
00400 const char *outfile = "build_prams_outfile";
00401 int rc;
00402
00403 struct poptOption opt_table[] = {
00404 { "dds_ftw", 0, POPT_ARG_STRING, &def_file, 'd' },
00405 { "dds_qdac",0, POPT_ARG_STRING, &def_file, 'q' },
00406 { "M5range", 0, POPT_ARG_STRING, &def_file, '5' },
00407 { "M2range", 0, POPT_ARG_STRING, &def_file, '2' },
00408 { "M5offset", 0, POPT_ARG_STRING, &def_file, 'O' },
00409 { "intclk", 0, POPT_ARG_STRING, &def_file, 'c' },
00410 { "output", 'o', POPT_ARG_STRING, &outfile, 0 },
00411 POPT_AUTOHELP
00412 POPT_TABLEEND
00413 };
00414
00415 poptContext opt_context =
00416 poptGetContext(argv[0], argc, argv, opt_table, 0);
00417
00418
00419 while((rc = poptGetNextOpt(opt_context)) > 0){
00420 switch(rc){
00421 case 'd':
00422 load(new FtwHandler(), def_file);
00423 break;
00424 case 'q':
00425 load(new QdacHandler(), def_file);
00426 case 'c':
00427 load(new IntclkHandler(), def_file);
00428 break;
00429 case '2':
00430 load(new VRangeHandler(M2_consts), def_file);
00431 break;
00432 case '5':
00433 load(new VRangeHandler(M5_consts), def_file);
00434 break;
00435 case 'O':
00436 load(new InputOffsetHandler(), def_file);
00437 break;
00438 }
00439 }
00440
00441 writeOutput(outfile);
00442 return 0;
00443 }
00444