Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

loadClist.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #ifndef WIN32
00006 #include <unistd.h>
00007 #endif
00008 #include <sys/stat.h>
00009 
00010 #include "primNames.h"
00011 #include "commandListDefinitions.h"
00012 #include "primListTableDefinitions.h"
00013 #include "primParams.h"
00014 
00015 char *prefix = "d:rod";
00016 
00017 #if WIN32
00018 bool swapSlash = false;
00019 #else
00020 bool swapSlash = true;
00021 #endif
00022 
00023 void loadClist(const char *fname, int depth);
00024 void loadPlist(const char *fname, int depth);
00025 char *spaces(int num);
00026 char *translateName(const char *oldFname);
00027 char *getName(int id);
00028 char *getTaskName(int id);
00029 void printDataChunk(unsigned int *ptr, unsigned int length, int depth);
00030 void printFile(char *fname, int depth);
00031 
00032 int main(int argc, char **argv) {
00033   if(argc == 2) {
00034     int len = strlen(argv[1]);
00035     // In windows, cygwin sees the files as .cli
00036     if(argv[1][len-3] == 'c' || argv[1][len-3] == 'C') {
00037       loadClist(argv[1], 0);
00038     } else if(argv[1][len-5] == 'c' || argv[1][len-5] == 'C') {
00039       loadClist(argv[1], 0);
00040     } else {
00041       loadPlist(argv[1], 0);
00042     }
00043   } else {
00044     printf("Usage: loadClist clist\n");
00045   }
00046   getc(stdin); 
00047 
00048   return 0;
00049 }
00050 
00051 void loadClist(const char *fname, int depth) {
00052   printf("%sLoading clist %s\n", spaces(depth), fname);
00053 
00054   struct stat fileInfo;
00055 
00056   stat(fname, &fileInfo);
00057 
00058   int fileSize;
00059 
00060 #if WIN32
00061   fileSize = 1000000;
00062 #else
00063   if(!S_ISREG(fileInfo.st_mode)) {
00064     printf("Not a regular file!\n");
00065     return;
00066   }
00067 
00068   fileSize = fileInfo.st_size;
00069 #endif
00070 
00071   char *buffer;
00072   buffer = (char *)malloc(fileSize);
00073 
00074   FILE *fin = fopen(fname, "rb");
00075 
00076   if(!fin) {
00077     printf("Bad file %s\n", fname);
00078     return;
00079   }
00080 
00081   int ret = fread(buffer, 1, fileSize, fin);
00082 
00083   if(ret == fileSize) 
00084     printf ("%sRead file of size %d\n", spaces(depth), (int)fileSize);
00085   else {
00086     printf("Read failed\n");
00087     return;
00088   }
00089 
00090   char *endOfFile = buffer + ret;
00091 
00092   COMMAND_LIST *clist = (COMMAND_LIST *)buffer;
00093 
00094   printf("%sThere are %d commands\n", spaces(depth), clist->commandCount);
00095 
00096   int noCommands = clist->commandCount;
00097 
00098   for(int command = 0; command < noCommands; command++) {
00099     printf("%s%d:\n", spaces(depth), command);
00100 
00101     COMMAND *commStruct = &clist->command[command];
00102     // Command size if 624
00103     if((char *)commStruct > endOfFile) {
00104       printf("Oops %p %p\n", commStruct, endOfFile);
00105       break;
00106     }
00107 
00108     int type = commStruct->id;
00109 
00110     printf(" %sRep: %d, Cont?: %d\n", spaces(depth), commStruct->numRepetitions, commStruct->continueIfError);
00111 
00112     //  #define RW_VME_COMMAND_ID              0  
00113     //  #define RW_MASTER_COMMAND_ID           1
00114     //  #define RW_SLAVE_COMMAND_ID            2
00115     //  #define SEND_PRIM_LIST_COMMAND_ID      3
00116     //  #define COMPARE_FILES_COMMAND_ID       4
00117     //  #define BUS_TEST_ID                    5
00118     //  #define RUN_CHILD_COMMAND_LIST_ID      6
00119     //  #define RW_FPGA_FLASH_ID               7
00120     //  #define WRITE_MDSP_FLASH_ID            8
00121     //  #define BOC_SETUP_ID                   9
00122     //  #define RUN_EXTERNAL_PROGRAM_ID       10
00123     //  #define DELAY_COMMAND_ID              11
00124 
00125     char *params = (char *)&commStruct->params; // buffer+index+12;
00126 
00127     switch(type) {
00128     case RW_VME_COMMAND_ID:
00129       {
00130         RW_VME_PARAMS *data = (RW_VME_PARAMS*)params;
00131         printf(" %sRW VME %s 0x%x bytes, block? %d start 0x%x length 0x%x\n", spaces(depth), 
00132                data->readNotWrite?"READ":"WRITE", data->elementSize, data->noBlockAccess, data->startAddress, data->length);
00133         break;
00134       }
00135     case RW_MASTER_COMMAND_ID:
00136       {
00137         RW_MASTER_PARAMS *data = (RW_MASTER_PARAMS*)params;
00138         printf(" %sRW MASTER %s start 0x%x length 0x%x\n", spaces(depth),
00139                data->readNotWrite?"Read":"Write", data->startAddress, data->length);
00140         if(data->readNotWrite) {
00141           printf(" %s comp? %d, filename %s\n", spaces(depth),
00142                  data->read.compareWithData, data->read.fileName);
00143         } else {
00144           if(data->write.dataNotFile) {
00145             printf(" %s Immediate 0x%x\n", spaces(depth),
00146                    data->write.data);
00147           } else {
00148             printf(" %s File %s\n", spaces(depth),
00149                    data->write.fileName);
00150           }
00151         }
00152         break;
00153       }
00154     case RW_SLAVE_COMMAND_ID:
00155       {
00156         RW_SLAVE_PARAMS *data = (RW_SLAVE_PARAMS*)params;
00157         printf(" %sRW SLAVE %s slave %d, start %p, length 0x%x\n", spaces(depth),
00158                data->readNotWrite?"READ":"WRITE", data->slaveNumber, (void *)data->startAddress, data->length);
00159         if(data->readNotWrite) {
00160           printf(" %s compare %d, 0x%08x\n", spaces(depth), data->read.compareWithData, data->read.compData);
00161           printf(" %s outFile %s\n", spaces(depth), data->read.fileName);
00162         } else {
00163           if(data->write.dataNotFile) {
00164             printf(" %s data 0x%08x\n", spaces(depth), data->write.data);
00165           } else {
00166             printf(" %s inFile %s\n", spaces(depth), data->write.fileName);
00167           }
00168         }
00169         break;
00170       }
00171     case SEND_PRIM_LIST_COMMAND_ID:
00172       printf(" %sRun prim list (no rebuild? %d) %s\n", spaces(depth), 
00173              *(int*)(params), params+4);
00174 
00175       char plName[300];
00176       strcpy(plName, translateName(params+4));
00177       //      printf("Converted name %s\n", plName);
00178       loadPlist(plName, depth + 1);
00179 
00180       break;
00181     case COMPARE_FILES_COMMAND_ID:
00182       {
00183         COMPARE_FILES_PARAMS *data = (COMPARE_FILES_PARAMS*)params;
00184         printf(" %sCOMPARE FILES %s %s\n", spaces(depth),
00185                data->commonFile, data->rodFile);
00186         break;
00187       }
00188     case BUS_TEST_ID:
00189       printf(" %sBUS TEST\n", spaces(depth));
00190       break;
00191     case RUN_CHILD_COMMAND_LIST_ID:
00192       printf(" %sSub command list %s\n", spaces(depth), params);
00193 
00194       char clName[300];
00195       strcpy(clName, translateName(params));
00196       //printf("Converted name %s\n", clName);
00197       loadClist(clName, depth + 1);
00198 
00199       break;
00200     case RW_FPGA_FLASH_ID:
00201       printf(" %sRW_FPGA_FLASH\n", spaces(depth));
00202       break;
00203     case WRITE_MDSP_FLASH_ID:
00204       printf(" %sWRITE_MDSP_FLASH\n", spaces(depth));
00205       break;
00206     case BOC_SETUP_ID:
00207       printf(" %sBOC_SETUP\n", spaces(depth));
00208       break;
00209     case RUN_EXTERNAL_PROGRAM_ID:
00210       {
00211         RUN_EXTERNAL_PROGRAM_PARAMS *data = (RUN_EXTERNAL_PROGRAM_PARAMS *)params;
00212         printf(" %sRUN EXTERNAL %s on (%s)\n", spaces(depth),
00213                data->externalProgram, data->rodDataFile);
00214       }
00215       break;
00216     case DELAY_COMMAND_ID:
00217       printf(" %sDELAY %f seconds\n", spaces(depth), *(float *)params);
00218       break;
00219     default:
00220       printf(" %sUnknown Command type is %d\n", spaces(depth), type);
00221     }
00222   }
00223 
00224   free(buffer);
00225 }
00226 
00227 void loadPlist(const char *fname, int depth) {
00228   printf("%sLoading plist %s\n", spaces(depth), fname);
00229 
00230   struct stat fileInfo;
00231 
00232   stat(fname, &fileInfo);
00233 
00234   int fileSize;
00235 
00236 #if WIN32
00237   fileSize = 1000000;
00238 #else
00239   if(!S_ISREG(fileInfo.st_mode)) {
00240     printf("Not a regular file!\n");
00241     return;
00242   }
00243 
00244   fileSize = fileInfo.st_size;
00245 #endif
00246 
00247   char *buffer;
00248   buffer = (char *)malloc(fileSize);
00249 
00250   FILE *fin = fopen(fname, "rb");
00251 
00252   if(!fin) {
00253     printf("Bad file %s\n", fname);
00254     return;
00255   }
00256 
00257   int ret = fread(buffer, 1, fileSize, fin);
00258 
00259   if(ret == fileInfo.st_size) 
00260     printf ("%sRead file of size %d\n", spaces(depth), (int)fileSize);
00261   else {
00262     printf("Read failed\n");
00263     return;
00264   }
00265 
00266   char *endOfFile = buffer + ret;
00267 
00268   LIST_TABLE* primitiveList = (LIST_TABLE*)buffer;
00269 
00270   //  int noCommands = *(int*)(buffer);
00271 
00272   printf("%sThere are %d primitives\n", spaces(depth), primitiveList->allPrimCount);
00273 
00274   //  char *basePrim = buffer+4*100+16+16;
00275 
00276   for(unsigned int prim = 0; prim < primitiveList->allPrimCount; prim++) {
00277     printf("%s%d:\n", spaces(depth), prim);
00278 
00279     PRIM_TABLE *primStruct = &primitiveList->primTable[prim];
00280 
00281     // Primitive size is 168 !
00282     int index = prim*168 + 4;
00283     if(index > fileInfo.st_size) {
00284       printf("File size Oops %d\n", (int)fileInfo.st_size);
00285       break;
00286     }
00287 
00288     // MSG_HEAD inPrimHeader
00289     MSG_HEAD *msg = &primStruct->inPrimHeader;
00290     printf(" %sIn  -- Len: %d, Index: %d, Id: %d, Rev: %d, (%s)\n", spaces(depth), 
00291            msg->length, msg->index, msg->id, msg->primRevision,
00292            getName(msg->id));
00293     //     int type = *(int*)(basePrim+index);
00294 
00295     // MSG_HEAD outPrim
00296     msg = &primStruct->outPrimHeader;
00297     printf(" %sOut -- Len: %d, Index: %d, Id: %d, Rev: %d, (%s)\n", spaces(depth), 
00298            msg->length, msg->index, msg->id, msg->primRevision,
00299            getName(msg->id));
00300 
00301     unsigned int *baseParams = (unsigned int *)&primStruct->params; // basePrim+index+32;
00302 
00303     switch(primStruct->outPrimHeader.id) { // *(int*)(basePrim+index + 24)) {
00304     case ECHO:
00305       {
00306         int type = (*(int*)baseParams+0);
00307         printf("%s  ECHO primitive type (%d) %s\n", spaces(depth), type, (type==0)?"Test pattern":"From File");
00308         int length = (*(int*)(baseParams + 104));
00309         if(type == 0) {
00310           printf("%s  Pattern: 0x%08x length %d\n", spaces(depth), (*(int*)(baseParams + 4)), length);
00311         } else {
00312           printf("%s  File: %s length %d\n", spaces(depth), (char*)(baseParams + 4), length);
00313         }
00314       }
00315       break;
00316     case RW_REG_FIELD:
00317       {
00318         RW_REG_FIELD_IN *data = (RW_REG_FIELD_IN *)baseParams;
00319         printf("%s  %s register 0x%x, offset 0x%x, width %d, value 0x%x\n", spaces(depth), 
00320                data->readNotWrite?"Read":"Write",
00321                data->registerID, data->offset, data->width, 
00322                data->dataIn);
00323       }
00324       break;
00325     case START_SLAVE_EXECUTING:
00326       {
00327         START_SLAVE_EXECUTING_IN *data = (START_SLAVE_EXECUTING_IN *)baseParams;
00328         printf("%s  Start executing slave %d, on/off %d, type %d, timeout %dus\n", spaces(depth), 
00329                data->slaveNumber, data->commOnOff, data->slaveType, data->timeoutInUsec);
00330       }
00331       break;
00332     case BUILD_STREAM:
00333       {
00334         BUILD_STREAM_IN *data = (BUILD_STREAM_IN *)baseParams;
00335         printf("%s  Cmd Buffer %d, reset %d, ChipAddress %d, data %d %d %d %d, fibre %d\n", spaces(depth),
00336                data->cmdBuff, data->reset, data->chipAddress, data->data[0], data->data[1], data->data[2], data->data[3], data->fibre);
00337         printf("%s   ", spaces(depth));
00338         for(int i=0; i<N_CMD_LIST_CMDS; i++) {
00339           printf("(%d %d), ", data->cmdList.cmd[i], data->cmdList.data[i]);
00340         }
00341         printf("\b\b\n");
00342       }
00343       break;
00344     case SEND_STREAM:
00345       printf("%s  Cmd Buffer %d, capture serial? %d\n", spaces(depth), 
00346              baseParams[0], baseParams[1]);
00347       break;
00348     case SEND_SLAVE_LIST:
00349       {
00350         PRIM_SEND_SLAVE_LIST_PARAMS *params = (PRIM_SEND_SLAVE_LIST_PARAMS *)baseParams;
00351         SEND_SLAVE_LIST_IN *data = &params->primParams;
00352         printf("%s  Send to slave %d, %d, (%p, %p)\n", spaces(depth), 
00353                data->slaveNumber, data->listLength, data->slavePrimList, data->slaveRepData);
00354 
00355         printf("%s  ToROD length %d, index %d, prims %d, rev %d\n", spaces(depth+1),
00356                params->inSlaveListHeader.length, 
00357                params->inSlaveListHeader.index, 
00358                params->inSlaveListHeader.numMsgs, 
00359                params->inSlaveListHeader.primListRevision);
00360         printf("%s  FromROD length %d, index %d, prims %d, rev %d\n", spaces(depth+1),
00361                params->outSlaveListHeader.length, 
00362                params->outSlaveListHeader.index, 
00363                params->outSlaveListHeader.numMsgs, 
00364                params->outSlaveListHeader.primListRevision);
00365 
00366         printf("%s   See following primitives for contents\n", spaces(depth);
00367       }
00368       break;
00369     case START_SLAVE_LIST:
00370       {
00371         START_SLAVE_LIST_IN *data = (START_SLAVE_LIST_IN *)baseParams;
00372         printf("%s  Start on slave %d, pause %d, reply %d\n", spaces(depth), 
00373                data->slaveNumber, data->pauseMasterList, data->getSlaveReply);
00374       }
00375       break;
00376     case EVENT_TRAP_SETUP:
00377       {
00378         //        EVENT_TRAP_SETUP_IN *data = (EVENT_TRAP_SETUP_IN *)baseParams;
00379         printf("%s  Event trap setup (lots of parameters)\n", spaces(depth));
00380         printDataChunk(baseParams, sizeof(EVENT_TRAP_SETUP_IN)/4, depth);
00381       }
00382       break;
00383     case RW_MODULE_DATA:
00384       { 
00385         RW_MODULE_DATA_PARAMS *params = (RW_MODULE_DATA_PARAMS *)baseParams;
00386         RW_MODULE_DATA_IN *data = &(params->inputParams);
00387         printf("%s  %s module data Struct %d moduleNum %d from: \n%s   %s\n", spaces(depth), 
00388                data->readNotWrite?("READ"):("WRITE"), data->structId, data->moduleNum, spaces(depth), 
00389                params->inFileName);
00390      }
00391       break;
00392     case START_TASK:
00393       {
00394         START_TASK_PARAMS *params = (START_TASK_PARAMS *) baseParams;
00395         START_TASK_IN_NOHIST *data = &(params->inputParams);
00396         printf("%s  Task %d (%s), Rev %d, Pri %d, Comp %d\n", spaces(depth),
00397                data->taskType, getTaskName(data->taskType), data->taskRevision, data->priority, data->completionFlag);
00398 
00399         if(data->taskType == HISTOGRAM_CTRL_TASK) {
00400           printf("%s  Input file name %s\n", spaces(depth), params->inDataFileName);
00401           printFile(translateName(params->inDataFileName), depth);
00402         } else {
00403           printDataChunk((unsigned int*)&(data->taskStruct), primStruct->inPrimHeader.length - 8, depth);
00404         }
00405       }
00406       break;
00407       // These have params so can't just print from file
00408     case RW_SLAVE_MEMORY:
00409       {
00410         PRIM_RW_SLAVE_MEMORY_PARAMS *params = (PRIM_RW_SLAVE_MEMORY_PARAMS *) baseParams;
00411         RW_SLAVE_MEMORY_IN *data = &(params->inputParams);
00412         printf("%s  Slave %d, %s, sAdd %p, mAdd %p, words %d\n", spaces(depth),
00413                data->slaveNumber, data->readNotWrite?"READ":"WRITE", data->slaveAddress, data->masterAddress, data->numWords);
00414         printf("%s  Filename %s\n", spaces(depth), params->inDataFileName); // outDataFileName is same address
00415       }
00416       break;
00417     case SEND_DATA:
00418       {
00419         char *types[] = {"Mirror", "Router", "Stream"};
00420         SEND_DATA_PARAMS *params = (SEND_DATA_PARAMS*) baseParams;
00421         SEND_DATA_IN *data = &(params->inputParams);
00422         printf("%s  Type %s (%d) \"AuxVal\" %d, repBuffer %d, timeout %d\n", spaces(depth),
00423                types[data->dataType], data->dataType, data->auxVal, data->repBufferFlag, data->timeout);
00424         if(data->repBufferFlag) {
00425           printf("%s   Write to: %s\n", spaces(depth),
00426                  params->outDataFileName);
00427         }
00428       }
00429       break;
00430     case POLL_REG_FIELD:
00431       {
00432         PRIM_POLL_REG_FIELD_PARAMS *params = (PRIM_POLL_REG_FIELD_PARAMS*) baseParams;
00433         POLL_REG_FIELD_IN *data = &(params->inputParams);
00434         printf("%s  Reg 0x%x, offset %d, width %d, desiredVal %d, timeout %dus\n", spaces(depth),
00435                data->registerID, data->offset, data->width, data->desiredValue, data->timeoutInUsec);
00436       }
00437       break;
00438     case RW_FIFO:
00439       {
00440         char *fifoNames[] = {"INPUT", "DEBUG", "EVENT", "TIM"};
00441         char *bankNames[] = {"BANK_A", "BANK_B", "BANK_C"};
00442         PRIM_RWFIFO_PARAMS *params = (PRIM_RWFIFO_PARAMS*) baseParams;
00443         RW_FIFO_IN *data = &(params->inputParams);
00444         printf("%s  id %s (%d), bank %s (%d), %s, elements %d, addr %p\n", spaces(depth),
00445                fifoNames[data->fifoId], data->fifoId, bankNames[data->bank], data->bank, 
00446                data->readNotWrite?"READ":"WRITE", data->numElements, data->dataBaseAdr);
00447         if(data->readNotWrite) {
00448           printf("%s   Read %d from: %s\n", spaces(depth),
00449                  params->inDataFile.size, params->inDataFile.name);
00450         } else {
00451           printf("%s   Write to: %s\n", spaces(depth),
00452                  params->outDataFileName);
00453         }
00454       }
00455       break;
00456     case TEST:
00457     case TRANS_SERIAL_DATA:
00458       break;
00459     default:
00460       if(primStruct->inPrimHeader.length > 4) {
00461         printDataChunk(baseParams, primStruct->inPrimHeader.length - 4, depth);
00462       }
00463       break;
00464     }
00465     
00466   }
00467 
00468   free(buffer);
00469 }
00470 
00471 char *spaces(int num) {
00472   static char buf[100];
00473 
00474   for(int i=0; i<num*2; i++) {
00475     buf[i] = ' ';
00476   }
00477 
00478   buf[num*2] = '\0';
00479 
00480   return buf;
00481 }
00482 
00483 // Swap slashes and remove :
00484 char *translateName(const char *oldFname) {
00485   static char clName[300];
00486   char *nptr = clName;
00487   const char *optr = oldFname;
00488 
00489   for(; *optr!=0; optr++) {
00490     if(*optr == '\\') {
00491       if(swapSlash) {
00492         *nptr++ = '/';
00493       } else {
00494         *nptr++ = '\\';
00495       }
00496     } else if(*optr == ':') {
00497       for(int i=0; i<strlen(prefix); i++) {
00498         nptr[i-1] = prefix[i];
00499       }
00500       nptr += strlen(prefix) - 1;
00501     } else {
00502       *nptr++ = *optr;
00503     }
00504   }
00505 
00506   *nptr = 0;
00507 
00508   return clName;
00509 }
00510 
00511 char *getName(int id) {
00512   for(int i=0; primNames[i].id != -1; i++) {
00513     if(primNames[i].id == id) {
00514       return primNames[i].name;
00515     }
00516   }
00517 
00518   return 0;
00519 }
00520 
00521 char *getTaskName(int id) {
00522   for(int i=0; taskNames[i].id != -1; i++) {
00523     if(taskNames[i].id == id) {
00524       return taskNames[i].name;
00525     }
00526   }
00527 
00528   return 0;
00529 }
00530 
00531 void printDataChunk(unsigned int *ptr, unsigned int length, int depth) {
00532   for(unsigned int i=0; i<length; i++) {
00533     if(i%4==0) printf("%s%s   ", i?"\n":"", spaces(depth));
00534     printf("0x%08x ", ptr[i]);
00535   }
00536   printf("\n");
00537 }
00538 
00539 
00540 void printFile(char *fname, int depth) {
00541   printf("%sLoading File %s\n", spaces(depth), fname);
00542 
00543   struct stat fileInfo;
00544 
00545   stat(fname, &fileInfo);
00546 
00547   int fileSize;
00548 
00549 #if WIN32
00550   fileSize = 1000000;
00551 #else
00552   if(!S_ISREG(fileInfo.st_mode)) {
00553     printf("Not a regular file!\n");
00554     return;
00555   }
00556 
00557   fileSize = fileInfo.st_size;
00558 #endif
00559 
00560   long *buffer;
00561   buffer = (long *)malloc(fileSize);
00562 
00563   FILE *fin = fopen(fname, "rb");
00564 
00565   if(!fin) {
00566     printf("Bad file %s\n", fname);
00567     return;
00568   }
00569 
00570   int ret = fread((char *)buffer, 1, fileSize, fin);
00571 
00572   if(ret == fileSize) 
00573     printf ("%sRead file of size %d\n", spaces(depth), (int)fileSize);
00574   else {
00575     printf("Read failed\n");
00576     return;
00577   }
00578 
00579   printf("%s", spaces(depth));
00580   for(int i=0; i<fileInfo.st_size/4; i++) {
00581     if(i && i%8 == 0) printf("\n%s", spaces(depth));
00582     printf("%08lx ", buffer[i]);
00583   }
00584   printf("\n");
00585 }

Generated on Thu Jul 15 09:50:48 2004 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5