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

Functs.cxx

00001 // #define IDRAM_BASE 0x80000000     // Rev C
00002 #define IDRAM_BASE 0x10000           // Rev E
00003 #define MDSP_IDRAM_BASE 0x80000000   // Both
00004 
00005 #define RW_REG_REV   105  // Was 103
00006 #define POLL_REG_REV 105  // Was 103
00007 
00008 void printBinary(unsigned int data, int length, int divisions = 0);
00009 
00010 void printSlave(int s, int add, int words, bool usePrim = true) {
00011   unsigned long *mem = readSlave(s, add, words, usePrim);
00012 
00013   if(mem) {
00014     int lines = words/8;
00015     int skipping = 0;
00016     for(int i=0; i<lines; i++) {
00017       int val = 0;
00018       for(int j=0; j<8; j++) {
00019         val+=mem[i*8+j];
00020       }
00021       if(val == 0) {
00022         if(!skipping) 
00023           printf(".");
00024         skipping++;
00025       } else {
00026         if(skipping) printf(" Skipped %d\n", skipping);
00027         skipping = 0;
00028 
00029         for(int j=0; j<8; j++) {
00030           printf("%08x ", mem[i*8+j]);
00031         }
00032         printf("\n");
00033       }
00034     }
00035     if(skipping) printf(" Skipped %d\n", skipping);
00036     if(lines*8 < words) {
00037       for(int i=lines*8; i<words; i++) {
00038         printf("%08x ", mem[i]);
00039       }
00040       printf("\n");
00041     }
00042   }
00043 }
00044 
00045 void printMaster(int add, int words) {
00046   tapi.dspBlockDump(0, 0, 0, add, words, -1);
00047 }
00048 
00049 void printConfig(int mid, int bank = 0) {
00050   // Print module configuration
00051   tapi.getABCDModule(mid, bank);
00052 
00053   unsigned long *config = tapi.retrieveModule(mid);
00054 
00055   for(int i=0; i<585; i++) {
00056     if(i && ((i%8) == 0)) printf("\n");
00057     printf("%08x ", config[i]);
00058   }
00059   printf("\n");
00060 }
00061 
00062 void printHostMem(unsigned long *add, int hostLength) {
00063   for(int i=0; i<hostLength; i++) {
00064     if(i && ((i%8) == 0)) printf("\n");
00065     printf("%08x ", add[i]);
00066   }
00067   printf("\n");
00068 }
00069 
00070 void readSlaveFile(int s, int add, int words, char *fileName, bool usePrim = true) {
00071   unsigned long *mem = readSlave(s, add, words, usePrim); 
00072 
00073   if(mem) {
00074     ofstream fout(fileName, ios::binary);
00075     fout.write((char*)&mem[0], words*4);
00076     // Clear prim list for next time
00077     tapi.createDebugPrimList();
00078   }
00079 }
00080 
00081 void readMasterFile(int add, int words, char *fileName) {
00082   unsigned int readlength
00083   unsigned long *mem = tapi.dspBlockRead(0, 0, 0, add, words, -1, &readlength);
00084 
00085 //    unsigned long *mem = readSlave(s, add, words, usePrim); 
00086 
00087   if(mem) {
00088     ofstream fout(fileName, ios::binary);
00089     fout.write((char*)&mem[0], words*4);
00090   }
00091 }
00092 
00093 void loadAndStartSlave(int s, bool usePrim = true) {
00094   writeSlaveFile(s, "../SlaveDspCode/slaveRun_IPRAM.bin", 0, 16384, usePrim);
00095   readSlaveFile(s, 0, 16384, "ipramS.bin", usePrim);
00096   writeSlaveFile(s, "../SlaveDspCode/slaveRun_IDRAM.bin", 0x80000000, 16384, usePrim);
00097   readSlaveFile(s, 0x80000000, 16384, "idramS.bin", usePrim);
00098   writeSlaveFile(s, "../SlaveDspCode/slaveRun_xcode.bin", 0x2000000, 65536, usePrim);
00099   readSlaveFile(s, 0x2000000, 65536, "xcodeS.bin", usePrim);
00100   startSlave(s);
00101 }
00102 
00103 void startSlave(int s) {
00104   unsigned long data[4];
00105   data[0] = s;     // Slave
00106   data[1] = 1;     // Comm on
00107   data[2] = 2;     // Mode
00108   data[3] = 0x2000000;    // Timeout
00109 
00110   tapi.createDebugPrimList();
00111   tapi.addDebugPrimList(8, 1, 8194, 104, data);
00112   tapi.sendDebugPrimList(0, 0, 0);
00113   tapi.awaitResponse(0, 0, 0);
00114   // Clear debug list
00115   tapi.createDebugPrimList();
00116 
00117   unsigned long *result = tapi.getResponse(0, 0, 0);
00118 
00119   if(result) {
00120     unsigned int repLength;
00121     repLength = result[0];
00122     if(repLength > 8) {
00123       if(result[8] == s) {
00124         cout << "Reply correct\n";
00125       } else {
00126         cout << "Slave started = " << result[8] << " not " << s << endl;
00127       } 
00128     } else {
00129       cout << "Response too short!\n"; 
00130     }
00131   } else {
00132     cout << "No response to slave start\n"; 
00133   }
00134 }
00135 
00136 void writeSlaveFile(int s, char *fileName, long add, int words, bool usePrim = true) {
00137   ifstream fin(fileName, ios::binary);
00138   unsigned long *buffer = new unsigned long[5+words];
00139 
00140   fin.read((char *)(buffer + 5), words*4);
00141 
00142   if(usePrim) {
00143     buffer[0] = s;
00144     buffer[1] = 0;
00145     buffer[2] = add;
00146     buffer[3] = 0xffffffff;
00147     buffer[4] = words;
00148 
00149     tapi.createDebugPrimList();
00150     tapi.addDebugPrimList(4 + 5 + words, 1, 8192, 100, buffer);
00151     tapi.sendDebugPrimList(0, 0, 0);
00152     tapi.awaitResponse(0, 0, 0);
00153     // Clear prim list for next time
00154     tapi.createDebugPrimList();
00155   } else {
00156     tapi.dspBlockWrite(0, 0, 0, buffer, add, words, s);
00157   }
00158  
00159   cout << "Slave data sent\n";
00160 }
00161 
00162 void clearSlaveMemory(int s, long add, int words) {
00163   unsigned long *buffer = new unsigned long[3];
00164 
00165   buffer[0] = add;
00166   buffer[1] = words;
00167   buffer[2] = 0;
00168 
00169   tapi.createDebugPrimList();
00170   tapi.addDebugPrimList(4 + 3, 1, 4, 100, buffer);
00171   tapi.sendDebugSlavePrimList(0, 0, 0, s, 1, 0);
00172   tapi.awaitResponse(0, 0, 0);
00173   // Clear prim list for next time
00174   tapi.createDebugPrimList();
00175 
00176   cout << "Slave data sent\n";
00177 }
00178 
00179 void clearScratch() {
00180   for(int i=0; i<10000; i++) {
00181     scratch[i] = 0;
00182   }
00183 }
00184 
00185 void checkInmem(int delay, int units) {
00186   // Put formatters into raw data mode
00187   writeRegister(0x60, 2, 1, 1);
00188   writeRegister(0x61, 2, 1, 1);
00189 
00190   // Reset INMEM fifo
00191   writeRegister(0x1c6, 3, 1, 1);
00192   writeRegister(0x1c6, 3, 1, 0);
00193 
00194   // Test bench mode select (output only)
00195   //  writeRegister(0x1d6, 12, 6, 0x18);
00196 
00197   // Setup fifo. 22 is delay in clock cycles. 1000 is length
00198   unsigned long val = (delay << 16) + units;
00199   writeRegister(0x1d5, 0, 32, val);
00200 
00201   // Calibration Trigger Signal Decoder Enable
00202   writeRegister(0x1c7, 18, 1, 1);
00203 
00204   // Not sure about this, some bits don't have meanings
00205   writeRegister(0x1c7, 24, 4, 0xa);
00206 
00207   // Configuration readback enable (I added this)
00208   writeRegister(0x1c7, 19, 1, 1);
00209 
00210   // Do something that might trigger a response
00211   sendL1A();
00212 
00213   // Poll "Configuration read back" done
00214   pollRegister(0x1c9, 3, 1, 1);
00215 
00216   int status = readRegister(0x1d7, false);
00217   cout << "Inmem status: " << hex << status << dec << endl;
00218 
00219   readFifo(0, 0, units);
00220   readFifo(0, 1, units);
00221 
00222   // Turn off configuration read back
00223   writeRegister(0x1c7, 19, 1, 0);
00224 
00225   // Put formatters back to normal data mode
00226   writeRegister(0x60, 2, 1, 0);
00227   writeRegister(0x61, 2, 1, 0);
00228 }
00229 
00230 // This one works
00231 void newLinkRead(int delay, int units) {
00232   // Reset INMEM fifo
00233   writeRegister(0x1c6, 3, 1, 1);
00234   writeRegister(0x1c6, 3, 1, 0);
00235 
00236   // Disable formatters
00237   for(int i=0; i<96; i++) {
00238     writeRegister(0x60 + i, 0, 4, 0x1);     // ???
00239   }
00240 
00241   // Enable FE Command Outputs with SP0
00242   writeRegister(0x1c7, 0, 2, 0x1);
00243 
00244   // Load masks
00245   writeRegister(0x1ca, 0, 32, 0xffffffff);
00246   writeRegister(0x1cb, 0, 16, 0xffff);
00247   writeRegister(0x1cc, 0, 32, 0xffffffff);
00248   writeRegister(0x1cd, 0, 16, 0xffff);
00249 
00250   // Setup fifo. 22 is delay in clock cycles. 1000 is length
00251   unsigned long val = (delay << 16) + units;
00252 
00253   writeRegister(0x1d5, 0, 32, val);
00254 
00255   // Load enable and load
00256   writeRegister(0x1c7, 20, 1, 0x1);
00257   writeRegister(0x1c7,  2, 1, 0x1);
00258 
00259   // Configuration read back mode
00260   writeRegister(0x1c7, 25, 3, 0x7);
00261 
00262   // Enable formatters
00263   for(int i=0; i<96; i++) {
00264     writeRegister(0x60 + i, 0, 4, 0x0);    //  ????
00265   }
00266 
00267   // Do something that might trigger a response
00268   sendL1A();
00269 
00270   // Poll "Configuration read back" done
00271   pollRegister(0x1c9, 3, 1, 1);
00272 
00273   int status = readRegister(0x1d7, false);
00274   cout << "Inmem status: " << hex << status << dec << endl;
00275 
00276   // Set MUX to read out
00277   writeRegister(0x1c7, 25, 3, 0x1);
00278 
00279   unsigned long *bufferA = readFifo(0, 0, units, false);
00280   unsigned long *bufferB = readFifo(0, 1, units, false);
00281 
00282   cout << hex;
00283   cout.fill('0');
00284   for(int i=0; i<units/2; i++) {
00285     cout.width(8); cout << bufferA[i*3 + 0] << " ";
00286     cout.width(8); cout << bufferA[i*3 + 1] << " ";
00287     cout.width(8); cout << bufferA[i*3 + 2] << " ";
00288     cout.width(8); cout << bufferB[i*3 + 0] << " ";
00289     cout.width(8); cout << bufferB[i*3 + 1] << " ";
00290     cout.width(8); cout << bufferB[i*3 + 2] << " ";
00291     cout << endl;
00292   }
00293   cout << dec;
00294   cout.fill(' ');
00295 
00296 //   // Turn off configuration read back
00297 //   writeRegister(0x1c7, 19, 1, 0);
00298 
00299 //   // Put formatters back to normal data mode
00300 //   writeRegister(0x60, 2, 1, 0);
00301 //   writeRegister(0x61, 2, 1, 0);
00302 }
00303 
00304 void readConfiguration(int delay, int units) {
00305   // Reset INMEM fifo
00306   writeRegister(0x1c6, 3, 1, 1);
00307   writeRegister(0x1c6, 3, 1, 0);
00308 
00309   // Disable formatters
00310   for(int i=0; i<96; i++) {
00311     writeRegister(0x60 + i, 0, 4, 0x1);
00312   }
00313 
00314   writeRegister(0x1c7, 0, 2, 0x1);
00315 
00316   // Set and load masks
00317 
00318   // Configuration read back mode
00319   writeRegister(0x1c7, 25, 3, 0x5);
00320 
00321   // Trans_serial_data
00322 
00323   // Do something that might trigger a response
00324   sendL1A();
00325 
00326   // Poll "Configuration read back" done
00327   pollRegister(0x1c9, 3, 1, 1);
00328 
00329   // Setup fifo. 22 is delay in clock cycles. 1000 is length
00330   unsigned long val = (delay << 16) + units;
00331 
00332   writeRegister(0x1d5, 0, 32, val);
00333 
00334   int status = readRegister(0x1d7, false);
00335   cout << "Inmem status: " << hex << status << dec << endl;
00336 
00337   readFifo(0, 0, units);
00338   readFifo(0, 1, units);
00339 
00340   // Turn off configuration read back
00341   writeRegister(0x1c7, 19, 1, 0);
00342 
00343   // Put formatters back to normal data mode
00344   writeRegister(0x60, 2, 1, 0);
00345   writeRegister(0x61, 2, 1, 0);
00346 }
00347 
00348 void readConfiguration2(int delay, int units) {
00349   // Reset INMEM fifo
00350   writeRegister(0x216, 3, 1, 1);
00351   writeRegister(0x216, 3, 1, 0);
00352 
00353   // Disable formatters
00354 //   for(int i=0; i<96; i++) {
00355 //     writeRegister(0x60 + i, 0, 4, 0x1);
00356 //   }
00357 
00358 //   0x22d?
00359 
00360   writeRegister(0x217, 0, 2, 0x1);
00361 
00362   // Setup fifo. 22 is delay in clock cycles. 1000 is length
00363   unsigned long val = (delay << 16) + units;
00364 
00365   writeRegister(0x225, 0, 32, val);
00366 
00367   // Configuration read back mode
00368 //   writeRegister(0x217, 25, 3, 0x5);
00369   writeRegister(0x217, 24, 3, 0xA);
00370 
00371   // Trans_serial_data
00372   // Do something that might trigger a response
00373   sendL1A(1);
00374 
00375   // Poll "Configuration read back" done
00376   pollRegister(0x218, 3, 1, 1);
00377 
00378   int status = readRegister(0x1d7, false);
00379   cout << "Inmem status: " << hex << status << dec << endl;
00380 
00381   readFifo(0, 0, units);
00382   readFifo(0, 1, units);
00383 
00384   // Turn off configuration read back
00385   writeRegister(0x217, 19, 1, 0);
00386 
00387 //   // Put formatters back to normal data mode
00388 //   writeRegister(0x60, 2, 1, 0);
00389 //   writeRegister(0x61, 2, 1, 0);
00390 }
00391 
00392 void readLinksToFifos(int delay, int units) {
00393   // Enable Test Bench IO!!!
00394   writeRegister(0x1c7, 15, 2, 2);
00395 
00396   // Test bench mode select (output only)
00397   writeRegister(0x1d6, 12, 6, 0x18);
00398 
00399   unsigned long val = (delay << 16) + units;
00400 
00401   //  cout << "val = " << hex << val << dec << endl;
00402 
00403   //  // Setup fifo. 22 is delay in clock cycles. 1000 is length
00404   writeRegister(0x1d3, 0, 32, val);
00405 
00406 //    readRegister(0x1d7);
00407 
00408   // Set up data link MUX       (Test bench)
00409   writeRegister(0x1c7, 25, 3, 0x2);
00410   // Test bench run bit
00411   writeRegister(0x1c7, 17, 1, 0x1);
00412 
00413   // Poll "Configuration read back" done
00414   pollRegister(0x1d7, 6, 2, 0x3);
00415 
00416   // Test bench idle
00417   writeRegister(0x1c7, 17, 1, 0x0);
00418   // Give DSP R/W control of debug FIFO
00419   writeRegister(0x1c7, 25, 3, 0x1);
00420 
00421   int status = readRegister(0x1d7, false);
00422   cout << "Inmem status: " << hex << status << dec << endl;
00423 
00424   cout << "Bank A\n";
00425   readFifo(0, 0, units);
00426   cout << "Bank B\n";
00427   readFifo(0, 1, units);
00428 
00429   cout << "TIM FIFO\n";
00430   readFifo(3, 0, units);
00431 }
00432 
00433 unsigned long *readFifo(int id, int bank, int elems, bool dumpFifo = true) {
00434   unsigned long fifo[] = {id, bank, 1, elems, 0xffffffff};
00435 
00436   tapi.createDebugPrimList();
00437   tapi.addDebugPrimList(4 + 5, 1, 8198, 104, fifo);
00438   tapi.sendDebugPrimList(0, 0, 0);
00439   tapi.awaitResponse(0, 0, 0);
00440 
00441   unsigned long *result = tapi.getResponse(0, 0, 0);
00442 
00443   if(result) {
00444     unsigned int fifoLength = result[0];
00445     cout << "Got " << result[8] << " bytes Length is " << fifoLength << endl;
00446 
00447     // Clear prim list for next time
00448     tapi.createDebugPrimList();
00449 
00450     if(dumpFifo) {
00451       cout << hex;
00452       cout.fill('0');
00453       for(int i=0; i<fifoLength-11; i++) {
00454         if(i && i%8 == 0) cout << endl;
00455         cout.width(8);
00456         cout << result[i + 9] << " ";
00457       }
00458       cout << endl;
00459       cout << dec;
00460       cout.fill(' ');
00461     }
00462 
00463     return result + 9;
00464   }
00465 }
00466 
00467 void pollRegister(int r, int off, int width, int val) {
00468   // Timeout in 1000000 usec
00469   unsigned long poll[] = {r, off, width, val, 1000000};
00470 
00471   tapi.createDebugPrimList();
00472   tapi.addDebugPrimList(4 + 5, 1, 8197, POLL_REG_REV, poll);
00473   tapi.sendDebugPrimList(0, 0, 0);
00474   tapi.awaitResponse(0, 0, 0);
00475 
00476   unsigned long *result = tapi.getResponse(0, 0, 0);
00477 
00478   if(result) {
00479     unsigned int pollLength = result[0];
00480     //    cout << "Got " << result[8] << " reply\n";
00481     if(pollLength>8) {
00482       if(result[8] == 1) {
00483         cout << "Success\n";
00484       } else {
00485         cout << "Failure (timeout)\n";
00486       }
00487     } else {
00488       cout << "Poll response too short\n";
00489     }
00490   }
00491 
00492   // Clear prim list for next time
00493   tapi.createDebugPrimList();
00494 }
00495 
00496 void taskOp(int typ, int op, int data, int dsp) {
00497   unsigned long tkOp[] = {typ, op, data};
00498 
00499   tapi.createDebugPrimList();
00500   tapi.addDebugPrimList(7, 1, 13, 100, tkOp);
00501   if(dsp == -1) {
00502     tapi.sendDebugPrimList(0, 0, 0);
00503   } else {
00504     tapi.sendDebugSlavePrimList(0, 0, 0, dsp, 1, 0);
00505   }
00506   tapi.awaitResponse(0, 0, 0);
00507 }
00508 
00509 unsigned int readRegister(int r, bool output = true) {
00510   unsigned long d[] = {r, 0, 32, 1, 0};
00511 
00512   tapi.createDebugPrimList();
00513   tapi.addDebugPrimList(9, 5, 8196, RW_REG_REV, d);
00514   tapi.sendDebugPrimList(0, 0, 0);
00515   tapi.awaitResponse(0, 0, 0);
00516 
00517   unsigned long *result = tapi.getResponse(0, 0, 0);
00518   unsigned int val = 0;
00519   if(result) {
00520     unsigned long myLength = result[0];
00521     if(output) {
00522       cout << "Result is 0x" << hex << result[8] << dec << endl;
00523     }
00524     val = result[8];
00525   } else {
00526     cout << "ReadRegister " << hex << r << dec << " failed\n";
00527   }
00528 
00529   // Clear prim list for next time
00530   tapi.createDebugPrimList();
00531 
00532   return val;
00533 }
00534 
00535 void writeRegister(int r, int off, int wid, int value) {
00536   unsigned long d[] = {r, off, wid, 0, value};
00537 
00538   tapi.createDebugPrimList();
00539   tapi.addDebugPrimList(9, 5, 8196, RW_REG_REV, d);
00540   tapi.sendDebugPrimList(0, 0, 0);
00541   tapi.awaitResponse(0, 0, 0);
00542 
00543   // Clear prim list for next time
00544   tapi.createDebugPrimList();
00545 }
00546 
00547 void sendL1A(bool capture = 0) {//1   0     3   2     5   4    0    1    2    3    4    5  
00548   unsigned long l1aStream[] = {0x640065, 0x640064, 0x640064, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 2, 1, 63, 3, 0}; // 0x0, 0x0, 0x0, 0x0, 2};
00549   tapi.createDebugPrimList();
00550   tapi.addDebugPrimList(4 + 14, 2, 8202, 102, l1aStream);
00551 
00552   unsigned long sendStream[] = {2, capture};
00553   tapi.addDebugPrimList(4 + 2, 3, 8203, 100, sendStream);
00554   tapi.sendDebugPrimList(0, 0, 0);
00555   tapi.awaitResponse(0, 0, 0);
00556 }
00557 
00558 void sendSR_L1A() {  //           1   0     3   2     5   4    0    1    2    3    4    5  
00559   unsigned long l1aStream[] = {0x650066, 0x640064, 0x640064, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 2, 1, 63, 3, 0}; // 0x0, 0x0, 0x0, 0x0, 2};
00560   tapi.createDebugPrimList();
00561   tapi.addDebugPrimList(4 + 14, 2, 8202, 102, l1aStream);
00562 
00563   unsigned long sendStream[] = {2, 0};
00564   tapi.addDebugPrimList(4 + 2, 3, 8203, 100, sendStream);
00565   tapi.sendDebugPrimList(0, 0, 0);
00566   tapi.awaitResponse(0, 0, 0);
00567 }
00568 
00569 // Modify this one and don't expect it to be the same next time
00570 void sendTrigger() { //           1   0     3   2     5   4    0     1    2    3    4    5  
00571   unsigned long l1aStream[] = {0x700066, 0x670064, 0x640064, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0, 1, 63, 2, 0}; // 0x0, 0x0, 0x0, 0x0, 2};
00572   tapi.createDebugPrimList();
00573   tapi.addDebugPrimList(4 + 14, 2, 8202, 102, l1aStream);
00574 
00575   unsigned long sendStream[] = {2, 0};
00576   tapi.addDebugPrimList(4 + 2, 3, 8203, 100, sendStream);
00577   tapi.sendDebugPrimList(0, 0, 0);
00578   tapi.awaitResponse(0, 0, 0);
00579 }
00580 
00581 void calib_init() {
00582 
00583   // Clear router transfer buffer
00584   //      80008000 2000 words of 0
00585 
00586   // Set link static mask on all inputs
00587   for(int a=0x60; a<=0xbf; a++) {
00588     writeRegister(a, 0, 4, 0x01);
00589   }
00590 
00591   // Mask FE Occ counters
00592   writeRegister(0x1df, 0, 32, 0xffffffff);
00593   writeRegister(0x1e0, 0, 32, 0xffffffff);
00594   writeRegister(0x1e1, 0, 32, 0xffffffff);
00595 
00596   // Set RRIF cmd 0&1
00597   writeRegister(0x1c6, 0, 8, 0xff);    // Reset everything
00598   writeRegister(0x1c6, 0, 8, 0x78);
00599   writeRegister(0x1c7, 0, 23, 0x494a9);
00600 
00601   writeRegister(0x1c7, 22, 1, 1);     // Set static L1
00602 
00603   // Read register 1c7
00604 
00605   writeRegister(0x229, 0, 24, 1);     // Set CAL_L1_ID_0
00606   writeRegister(0x22a, 0, 24, 1);     // Set CAL_L1_ID_0
00607 
00608   // Load mask
00609   writeRegister(0x1ca, 0, 32, 1);   // Unmask channel 1
00610   writeRegister(0x1cb, 0, 16, 0);
00611   writeRegister(0x1cc, 0, 32, 0);
00612   writeRegister(0x1cd, 0, 16, 0);
00613   writeRegister(0x1c7, 2, 1, 1);    // Load mask
00614 
00615   // Load default mode bits
00616   for(int a=0x1ec; a<=0x1f3; a++) {
00617     writeRegister(a, 0, 16, 0);
00618   }
00619 
00620   // Set ROD type
00621   writeRegister(0x20c, 0, 8, 0);
00622 
00623   // Load default dynamic mask
00624   for(int a=0x20d; a<=0x218; a++) {
00625     writeRegister(a, 0, 16, 0);
00626   }
00627 
00628   // "set_rb10xFC_htl0xF0"
00629   // Set header/trailer limit on all formatters
00630   for(int a=0xd0; a<=0xd7; a++) {
00631     writeRegister(a, 0, 8, 0xf0);
00632   }
00633   // Set ROD busy limit on all formatters
00634   for(int a=0xd8; a<=0xdf; a++) {
00635     writeRegister(a, 0, 8, 0xfc);
00636   }
00637 
00638   // Normal data path
00639   writeRegister(0x1c7, 25, 3, 0x4);
00640 
00641   // Set Form Link 01 on
00642   writeRegister(0x60, 0, 4, 2);
00643   writeRegister(0x61, 0, 4, 2);
00644   writeRegister(0x1df, 0, 32, 0xfffffffc);
00645   writeRegister(0x1e0, 0, 32, 0xffffffff);
00646   writeRegister(0x1e1, 0, 32, 0xffffffff);
00647 }
00648 
00649 // Different modes
00650 // 0    Normal (trap everything)
00651 // 1    Error (trap in error mode)
00652 // 2    Router distribution (slave 2 matches remainder 2)
00653 // 3    DSP distribution (slave 2 matches event type 2)
00654 void setupEvTrap(int s, int mode = 0) {
00655 //    long setup[24] = {0x1 << s, 0, 0x2000, 0, 
00656 //                      0x3, 0x1, 0, 0,
00657 //                      0, 0, 1, 2,
00658 //                      0x3, 0, 0, 1,                    // Config, exclusion
00659 //                      0x1, 0, s, 1,                    // function, match
00660 //                      1,   0, 0, 0};                   // modulus, remainder
00661 
00662   unsigned long setup[24] = {0x1 << s, 0, 0x2000, 0, 
00663                     0x3, 0x1, 0, 0,
00664                     0, 0, 0, 0,
00665                     0x3, 0, 0, 1,                    // Config, exclusion
00666                     0x1, 0, s, 1,                    // function, match
00667                     1,   0, 0, 0};                   // modulus, remainder
00668 
00669   unsigned long *realSetup = new unsigned long[24];
00670 
00671   for(int i=0; i<24; i++) realSetup[i] = setup[i];
00672 
00673   realSetup[0] = 1<<s;
00674 
00675 //    if(mode == 1) {
00676 //      realSetup[9] = 1;
00677 //    } else if(mode == 2) {
00678 //      realSetup[20] == 4;
00679 //      realSetup[22] == s;
00680 //    } else if(mode == 3) {
00681 //      realSetup[18] == s;
00682 //      realSetup[20] == 1;
00683 //      realSetup[22] == 0;
00684 //    } 
00685 
00686 //    realSetup[0] = 0x1 << s;
00687 //    realSetup[18] = s;
00688 
00689   tapi.createDebugPrimList();
00690   tapi.addDebugPrimList(28, 1, 3, 103, realSetup);
00691   tapi.sendDebugPrimList(0, 0, 0);
00692   tapi.awaitResponse(0, 0, 0, 1);
00693 
00694   unsigned long *result = tapi.getResponse(0, 0, 0);
00695 
00696   if(result) {
00697     cout << "ETS Error code " << result[8] << endl;
00698   } else {
00699     cout << "No response to ETS\n";
00700   }
00701 }
00702 
00703 void startEvTrap(int s) {
00704   unsigned long trap[0] = {};
00705 
00706   tapi.createDebugPrimList();
00707   
00708   tapi.addDebugPrimList(4, 0, 4096, 101, trap);
00709   tapi.sendDebugSlavePrimList(0, 0, 0, s, 1, 1);
00710   tapi.awaitResponse(0, 0, 0);
00711 }
00712 
00713 void stopEvTrap(int s) {
00714   unsigned long trap[0] = {};
00715 
00716   tapi.createDebugPrimList();
00717   
00718   tapi.addDebugPrimList(4, 0, 4097, 100, trap);
00719   tapi.sendDebugSlavePrimList(0, 0, 0, s, 1, 1);
00720   tapi.awaitResponse(0, 0, 0);
00721 }
00722 
00723 void decodeEvent(int sl, int index, bool extFlag = false, bool errorType = false) {
00724   const int eventSize = 0x400;
00725   long base;
00726   if(extFlag) 
00727     base = 0x2092000;
00728   else {
00729     // RevE
00730 //     base = 0x18000;
00731     // Rev C
00732     base = 0x80008000;
00733   }
00734 
00735   long add = base + eventSize * index;
00736   const int eventWords = eventSize / 4;
00737   int frameCount = 0;
00738 
00739   // Storage for 32 frames
00740   unsigned long frameBuffer[eventWords * 32];
00741 
00742   while(frameCount < 16) {
00743     unsigned long *mem = readSlave(sl, base + eventSize * ((index + frameCount) % 32), eventWords);
00744 
00745     if(mem) {
00746       for(int i=0; i<eventWords; i++) {
00747         frameBuffer[eventWords*frameCount + i] = mem[i];
00748       }
00749 
00750       frameCount ++;
00751     } else {
00752       break;
00753     }
00754 
00755     if((frameBuffer[eventWords*frameCount - 1] & 0xff0e0000) == 0x400e0000) {
00756       break;
00757     } 
00758   }
00759 
00760   if(frameCount == 0) {
00761     cout << "Couldn't load memory\n";
00762     return;
00763   } else {
00764     cout << "Found " << frameCount << " frames of event data\n";
00765   }
00766 
00767   if((frameBuffer[eventWords*frameCount - 1] & 0xff0e0000) != 0x400e0000) {
00768     cout << "Event trailer marker not found " << frameBuffer[eventWords*frameCount - 1] << endl;
00769     return;
00770   } 
00771 
00772   if(frameBuffer[0] == 0xb0f00000) { 
00773     cout << "Found header\n";
00774   } else {
00775     cout << "Bad header 0x" << hex << frameBuffer[0] << dec << endl;
00776     return;
00777   }
00778 
00779   if(frameBuffer[1] == 0xee1234ee) {
00780     cout << "Valid header\n";
00781   } else {
00782     cout << "Bad check 0x" << hex << frameBuffer[1] << dec << endl;
00783   }
00784 
00785   int headerLength;     // Including bof
00786   int trailerLength;    // Excluding eof
00787 
00788   if(errorType) {
00789     headerLength = 6;
00790     trailerLength = 8;
00791 
00792     cout << "L1ID = " << frameBuffer[2] << " BCID = " << frameBuffer[3] << endl;
00793     cout << "Trigger type = " << frameBuffer[4] << " Event type = " << frameBuffer[5] << endl;
00794   } else {
00795     headerLength = 9;
00796     trailerLength = 5;
00797 
00798     if(frameBuffer[2] != headerLength) {
00799       cout << "Unknown header length (" << frameBuffer[2] << ")\n";
00800     } 
00801 
00802     cout << "Version: " << frameBuffer[3] << " ID: " << frameBuffer[4] << endl;
00803     cout << "L1ID = " << frameBuffer[5] << " BCID = " << frameBuffer[6] 
00804          << " TType = " << frameBuffer[7] << " det type = " << frameBuffer[8] << endl;
00805 
00806   } 
00807 
00808   int eofAt = headerLength;
00809 
00810   while(frameBuffer[eofAt] != 0xe0f00000 && eofAt< (eventWords * frameCount)) eofAt ++;
00811 
00812   if(eofAt < (eventWords * frameCount)) {
00813     cout << "Found eof!\n";
00814   } else {
00815     cout << "EOF not found\n";
00816     return;
00817   }
00818 
00819   int eventLength = eofAt-headerLength-trailerLength;
00820 
00821   unsigned long *rawEvent = frameBuffer+headerLength;
00822 
00823   for(int i=0; i<eventLength * 2; i++) {
00824     UINT16 currWord;
00825     if(i&1) {
00826       currWord = rawEvent[i/2] & 0x00ffff;
00827     } else {
00828       currWord = (rawEvent[i/2] & 0xffff0000) >> 16;
00829     }
00830     cout << hex << currWord << dec;
00831 
00832     switch((currWord & 0xe000) >> 13) {
00833     case 0:
00834       {
00835         if(currWord & 0x1f80) 
00836           cout << " INVALID" << endl;
00837         else {
00838           // Flagged error
00839           cout << " Flagged error on " << ((currWord & 0x78) >> 3) 
00840                << " (" << ((currWord & 0x7)) << ")\n";
00841         }
00842       }
00843       break;
00844     case 1:
00845       {
00846         // Header
00847         cout << " Header: ";
00848         if(errorType) {
00849           cout << "L1 " << ((currWord & 0x0f00) >> 8) 
00850                << " BCID " << ((currWord & 0xff)) 
00851                << ((currWord & 0x1000)?" Preamble err ":"")
00852                << endl;
00853         } else {
00854           cout << "Link " << (currWord & 0x7f) << " " 
00855              << ((currWord & 0x100)?"Condensed ":"") 
00856              << ((currWord & 0x200)?"BC err ":"") 
00857              << ((currWord & 0x400)?"L1 err ":"") 
00858              << ((currWord & 0x800)?"Time out err ":"") 
00859              << ((currWord & 0x1000)?"Preamble err ":"")
00860              << endl;
00861         }
00862       }
00863       break;
00864     case 2:
00865       {
00866         if(currWord & 0x3ff) 
00867           cout << " INVALID\n";
00868         else {
00869           // Trailer
00870           cout << " Trailer: " 
00871                << ((currWord & 0x400)?"Data overflow err ":"") 
00872                << ((currWord & 0x800)?"H/T limit err ":"") 
00873                << ((currWord & 0x1000)?"Trailer bit err ":"");
00874           cout << endl;
00875         }
00876       }
00877       break;
00878     case 3:
00879       {
00880         if(currWord & 0x300) 
00881           cout << " INVALID\n";
00882         else {
00883           // Raw data
00884           cout << " Raw: " 
00885                << ((currWord & 0x1c00) >> 10) + 1 << " bits " 
00886                << hex << (currWord & 0xff) << dec;
00887           cout << endl;
00888         }
00889       }
00890       break;
00891     default:
00892       // Everything else
00893       //          cout << " Cluster or condensed\n";
00894       {
00895         if((currWord & 0x2) == 0) {
00896           cout << "  Condensed: " ;
00897           cout << "Chip " << ((currWord & 0x7e00) >> 11)
00898                << " address 0x" << hex << ((currWord & 0x7f0) >> 4) << dec;
00899           if(currWord & 1) {
00900             cout << " hits " << ((currWord & 0x4)?"1":"0") 
00901                  << ((currWord & 0x8)?"1":"0");
00902           } else {
00903             cout << " hit " << ((currWord & 0x4)?"1":"0"); 
00904           }
00905           cout << endl;
00906         }
00907 
00908         if(currWord & 0x8 == 0) {
00909           cout << "  1st hit clust exp: " 
00910                << "Chip " << ((currWord & 0x7e00) >> 11)
00911                << " address 0x" << hex << ((currWord & 0x7f0) >> 4) << dec;
00912           cout << hex << " 0x" << (currWord & 0x7) << dec << endl;
00913         } else {
00914           if((currWord & 0x7f00) == 0) {
00915             cout << "  Clust exp: ";
00916             cout << hex << " 0x" << (currWord & 0x7) << dec;
00917             if(currWord & 0x80) {
00918               cout << hex << " 0x" << ((currWord & 0x70) >> 4) << dec;
00919             }
00920             cout << endl;
00921           }
00922         }
00923       }
00924     }
00925   }
00926 
00927   cout << "Error count = " << frameBuffer[eofAt-trailerLength] << endl;
00928   cout << "Error flags = 0x" << hex << frameBuffer[eofAt-trailerLength + 1] << dec << endl;
00929 
00930   int flags = frameBuffer[eofAt-trailerLength + 1];
00931   if(flags != 0) {
00932     if(flags & 0x1) {
00933       cout << "HEADER ";
00934     }
00935     if(flags & 0x2) {
00936       cout << "TRAILER ";
00937     }
00938     if(flags & 0x4) {
00939       cout << "FLAGGED ";
00940     }
00941     if(flags & 0x8) {
00942       cout << "SYNC ";
00943     }
00944     if(flags & 0x10) {
00945       cout << "HIT PATTERN ";
00946     }
00947     if(flags & 0x20) {
00948       cout << "L1ID ";
00949     }
00950     if(flags & 0x40) {
00951       cout << "BCID ";
00952     }
00953     if(flags & 0x80) {
00954       cout << "TIMEOUT ";
00955     }
00956     if(flags & 0x100) {
00957       cout << "Almost Full ";
00958     }
00959     if(flags & 0x200) {
00960       cout << "OVERFLOW ";
00961     }
00962     cout << endl;
00963   }
00964 
00965   if(errorType) {
00966     cout << "Error Status: \n" << hex;
00967 
00968     for(int es = 0; es < 6; es++) {
00969       UINT16 esWord = frameBuffer[eofAt-trailerLength + 2 + es];
00970       for(int i=0; i<8; i++) {
00971         cout << (esWord & 0x3) << " ";
00972         esWord >>= 2;
00973       }
00974     }
00975 
00976     cout << dec << endl;
00977   } else {
00978     cout << "nData = " << frameBuffer[eofAt-2] << " words found = " << (eofAt-9-5) << endl;
00979   }
00980 
00981   cout << "EOF = 0x" << hex << frameBuffer[eofAt] << dec << endl;
00982 
00983   // Print out the raw data afterwards
00984   int wordsPerLine = 8;
00985   int lines = eventLength/wordsPerLine;
00986   int skipping = 0;
00987 
00988   for(int i=0; i<lines; i++) {
00989     int val = 0;
00990     for(int j=0; j<wordsPerLine; j++) {
00991       val+=rawEvent[i*wordsPerLine+j];
00992     }
00993     if(val == 0) {
00994       if(!skipping) 
00995         printf(".");
00996       skipping++;
00997     } else {
00998       if(skipping) printf(" Skipped %d\n", skipping);
00999       skipping = 0;
01000 
01001       for(int j=0; j<wordsPerLine; j++) {
01002         printf("%08x ", rawEvent[i*wordsPerLine+j]);
01003       }
01004       printf("\n");
01005     }
01006   }
01007   if(skipping) printf(" Skipped %d\n", skipping);
01008   if(lines*wordsPerLine < eventLength) {
01009     for(int i=lines*wordsPerLine; i<eventLength; i++) {
01010       printf("%08x ", rawEvent[i]);
01011     }
01012     printf("\n");
01013   }
01014 
01015   printf("%d frames finished in frame %d with 0x%08x\n", 
01016          frameCount, frameCount + index, frameBuffer[frameCount * eventWords - 1]);
01017 }
01018 
01019 void histoStatus(int dsp) {
01020   if(dsp == -1) {
01021     // Histogram command stat 0
01022     long reg = tapi.dspSingleRead(0, 0, 0, MDSP_IDRAM_BASE + 0x20, -1);
01023     cout << "Bin: " << (reg & 0xff) 
01024          << " Cal: " << ((reg & 0xff00) >> 8) 
01025          << " Errs: " << ((reg & 0xffff0000) >> 16) << endl;
01026 
01027     // Histogram command stat 1
01028     reg = tapi.dspSingleRead(0, 0, 0, MDSP_IDRAM_BASE + 0x24, -1);
01029     cout << "avg. trans " << (reg & 0xff)
01030          << " avg. len " << ((reg & 0xff00) >> 8)
01031          << " avg. proc. " << ((reg & 0xffff0000) >> 16) << endl;
01032 
01033     // Histogram status 1
01034     reg = tapi.dspSingleRead(0, 0, 0, MDSP_IDRAM_BASE + 0x2c, -1);
01035     cout << "Slave 0: " << (reg & 0xffff)
01036          << " Slave 1: " << ((reg & 0xffff0000) >> 16) << endl;
01037 
01038     // Histogram status 0
01039     reg = tapi.dspSingleRead(0, 0, 0, MDSP_IDRAM_BASE + 0x28, -1);
01040     cout << "Slave 2: " << (reg & 0xffff)
01041          << " Slave 3: " << ((reg & 0xffff0000) >> 16) << endl;
01042   } else {
01043     unsigned long *regs = readSlave(dsp, IDRAM_BASE + 0x10, 20);
01044 
01045     if(regs) {
01046       long reg;
01047 
01048       cout << "TrapStatus: \n";
01049 
01050       // Trap stat 0
01051       reg = regs[1];
01052       cout << "Event word count: " << ((reg &0xffff) >> 0);
01053       cout << " IFrame tail: " << ((reg & 0xff0000) >> 16);
01054       cout << " XFrame tail: " << ((reg & 0xff000000) >> 24) << endl;
01055 
01056       // Trap stat 1
01057       reg = regs[2];
01058       cout << " IFrame head: " << ((reg & 0xff0000) >> 16);
01059       cout << " XFrame head: " << ((reg & 0xff000000) >> 24) << endl;
01060 
01061       // Trap cmd stat
01062       reg = regs[17];
01063       cout << "Trap Command/Status: " 
01064            << ((reg & 0x1)?"Trailer ":"")
01065            << ((reg & 0x2)?"Transmit ":"")
01066            << ((reg & 0x4)?"Header ":"")
01067            << ((reg & 0x4)?"ISR active ":"");
01068       cout << ((reg & 0x10)?"\"Data Error\" ":"")
01069            << ((reg & 0x20)?"\"Header Error\" ":"")
01070            << ((reg & 0x40)?"\"Trailer Error\" ":"")
01071            << ((reg & 0x80)?"\"Link Error\" ":"");
01072       cout << ((reg & 0x100)?"\"Error\" ":"")
01073            << ((reg & 0x200)?"\"Overflow(old)\" ":"")
01074            << ((reg & 0x800)?"\"ISR pending\" ":"");
01075       cout << ((reg & 0x4000)?"\"Overflow error\" ":"")
01076            << ((reg & 0x8000)?"\"Overflow\" ":"");
01077       cout << endl;
01078 
01079       cout << "\"Error count\": " << ((reg >> 16) & 0xff) << endl;
01080       cout << "\"Event count\": " << ((reg >> 24) & 0xff) << endl;
01081 
01082       // Hcmd stat reg 0
01083       reg = regs[4];
01084       cout << "Bin: " << (reg & 0xff) 
01085            << " Cal: " << ((reg & 0x1f00) >> 8) 
01086            << ((reg & 0x800)?" Cal enabled":"")
01087            << ((reg & 0x1000)?" New bin":"")
01088            << ((reg & 0x20000)?" New cal":"") << endl;
01089 
01090       // Hcmd stat reg 1
01091       reg = regs[5];
01092       cout << "Num events: " << reg << endl;
01093 
01094       // H stat reg 0
01095       reg = regs[6];
01096       cout << "Histogram status: " << ((reg & 0x1)?"Ready ":"")
01097            << ((reg & 0x2)?"Expecting ":"")
01098            << ((reg & 0x4)?"Processing ":"")
01099            << ((reg & 0x8)?"Done ":"") << endl;
01100       cout << "Bin err: " << ((reg >> 8) & 0xff) 
01101            << " proc time: " << ((reg >> 16) & 0xffff) << endl;
01102 
01103       // H stat reg 1
01104       reg = regs[7];
01105       cout << "Events recvd: " << reg << endl;
01106     }
01107   }
01108 }
01109 
01110 unsigned long *readSlave(int s, int add, int words, bool usePrim = true) {
01111   unsigned long *mem = 0;
01112 
01113   if(usePrim) {
01114     // Read slave memory        address                 numwords
01115     unsigned long rwSlaveMem[5] = {s, 1, add, 0xffffffff, words};
01116 
01117     printf("Read slave %d: 0x%x\n", s, add);
01118     tapi.createDebugPrimList();
01119     tapi.addDebugPrimList(4 + 5, 1, 8192, 100, rwSlaveMem);
01120     tapi.sendDebugPrimList(0, 0, 0);
01121     tapi.awaitResponse(0, 0, 0);
01122 
01123     mem = tapi.getResponse(0, 0, 0);
01124     // Clear prim list for next time
01125     tapi.createDebugPrimList();
01126     
01127     if(mem) {
01128       return mem+8;
01129     }
01130   } else {
01131     // Turn off dsp accesses 
01132     tapi.dspSingleWrite(0, 0, 0, MDSP_IDRAM_BASE + 0xc, 0x400, -1);
01133 
01134     unsigned int readLength;
01135     unsigned long *block = tapi.dspBlockRead(0, 0, 0, add, words, s, &readLength);
01136     tapi.dspSingleWrite(0, 0, 0, MDSP_IDRAM_BASE + 0xc, 0x0, -1);
01137     return block;
01138   }
01139 
01140   return 0;
01141 }
01142 
01143 // Send config but turn off ROD processing of data stream
01144 // So it doesn't get confused by "L1A"s 
01145 void sendConfig(int module = 0, int bank = 0) {
01146   writeRegister(0x1c7, 18, 1, 0);
01147   tapi.sendABCDModule(module, bank);
01148   writeRegister(0x1c7, 18, 1, 1);
01149 }
01150 
01151 // Read histogram buffer to file
01152 // How to handle multiple slaves?
01153 void readHisto(int bins, char *fname = "histogram.bin", bool headers=false) {
01154   ofstream histoout(fname, ios::binary);
01155 
01156   // Not right
01157   if(headers) {
01158     unsigned int length = 22*4 + 584 * 4;
01159     histoout.put(1); histoout.put(0);                                           // Version
01160     histoout.put(length & 0xff); histoout.put((length >> 8) & 0xff);            // length 
01161     histoout.put(123); histoout.put(0); histoout.put(0); histoout.put(0);       // run
01162     histoout.put(221); histoout.put(0); histoout.put(0); histoout.put(0);       // Scan
01163     histoout << "Module 28......"; histoout.put(0);                             // 16 char name
01164     histoout << "Module 28......"; histoout.put(0);                             // 16 char start
01165     histoout << "Module 28......"; histoout.put(0);                             // 16 char end
01166     histoout.put(1); histoout.put(0);                                           // Type
01167     histoout.put(bins&0xff); histoout.put((bins&0xff00)>>8);                    // Points
01168 
01169     unsigned int size = 0x800 * bins * 2;
01170     histoout.put(size&0xff); histoout.put((size&0xff00)>>8);                   
01171             histoout.put((size&0xff0000)>>16); histoout.put((size&0xff000000)>>24);                    // Size
01172     histoout.put(2); histoout.put(0);                                           // Datatype
01173     histoout.put(16); histoout.put(0);                                          // width
01174 
01175     // Module config
01176     for(int i=0; i<584; i++) {
01177       histoout.put(i);
01178       histoout.put(i);
01179       histoout.put(i);
01180       histoout.put(i);
01181     }
01182 
01183     unsigned int pointer = length;
01184 
01185     histoout.put(((pointer) & 0xff));
01186     histoout.put(((pointer) & 0xff00) >> 8);
01187     histoout.put(((pointer) & 0xff0000) >> 16);
01188     histoout.put(((pointer) & 0xff000000) >> 24);
01189 
01190     // Other pointers
01191     for(int i=0; i<12; i++) {
01192       histoout.put(0);
01193     }
01194 
01195     for(float j=0.0; j<(float)bins; j+=1.0) {
01196       // X
01197       histoout.put(((*(int*)&j) & 0xff));
01198       histoout.put(((*(int*)&j) & 0xff00) >> 8);
01199       histoout.put(((*(int*)&j) & 0xff0000) >> 16);
01200       histoout.put(((*(int*)&j) & 0xff000000) >> 24);
01201     }
01202     for(int i=0; i<bins; i++) {
01203        // Events
01204       histoout.put(((1000) & 0xff));
01205       histoout.put(((1000) & 0xff) >> 8);
01206       histoout.put(((1000) & 0xff) >> 16);
01207       histoout.put(((1000) & 0xff) >> 24);
01208     }
01209     for(int i=0; i<bins; i++) {
01210       // Errors
01211       histoout.put(0);
01212       histoout.put(0);
01213       histoout.put(0);
01214       histoout.put(0);
01215     }
01216   }
01217 
01218   // The data!
01219   for(int i=0; i<bins/2; i++) {
01220     unsigned long *chunk = readSlave(0, 0xa00e2000 + 0x1000*4*i, 0x1000);
01221     histoout.write((char*)&chunk[0], 0x1000*4);
01222   }
01223 }
01224 
01225 void print_calib_old() {
01226   cout << "Formatters\n";
01227   for(int a=0x0; a<0x60; a++) {
01228     int data = readRegister(a, false) & 0xffff;
01229     cout.width(4);
01230     cout << hex;
01231     cout << data << " ";
01232 
01233     if(a % 12 == 11) cout << endl;
01234   }
01235 
01236   cout << "Link masks\n";
01237   for(int a=0x0; a<0x60; a++) {
01238     int link = readRegister(0x60 + a, false) & 0xf;
01239     cout.width(2);
01240     cout << link << " ";
01241 
01242     if(a % 12 == 11) cout << endl;
01243 
01244     // Status is 16 bits
01245 //      link = readRegister(a, false) & 0xffff;
01246 //      cout << link << " ";
01247   }
01248 //    cout << endl;
01249 
01250   cout << "Occupancy counter mask\n";
01251   readRegister(0x1df);
01252   readRegister(0x1e0);
01253   readRegister(0x1e1);
01254 
01255   cout << "Status regs\n";
01256   readRegister(0x1c6);
01257   readRegister(0x1c7);
01258   readRegister(0x1c8);
01259   readRegister(0x1c9);
01260 
01261   cout << "Output mask\n";
01262   readRegister(0x1ca);
01263   readRegister(0x1cb);
01264   readRegister(0x1cc);
01265   readRegister(0x1cd);
01266 
01267   cout << "Mode bits\n";
01268   for(int a=0x1ec; a<=0x1f3; a++) {
01269     int mode = readRegister(a, false) & 0xfff;
01270     cout.width(3);
01271     cout << hex << mode << dec << " ";
01272   }
01273   cout << endl;
01274 
01275   cout << "ROD event type\n";
01276   readRegister(0x20c);
01277 
01278   cout << "Dynamic mask\n";
01279   for(int a=0x20d; a<=0x218; a++) {
01280     int mask = readRegister(a, false) & 0xffff;
01281     cout.width(4);
01282     cout << hex << mask << dec << " ";
01283   }
01284   cout << endl;
01285 }
01286 
01287 void print_calib() {
01288   cout << "Formatters\n";
01289   cout << " Enables\n";
01290   for(int a=0; a<8; a++) {
01291     int data = readRegister(a, false) & 0xffff;
01292     cout.width(4);
01293     cout << hex;
01294     cout << data << " ";
01295   }
01296   cout << endl;
01297 
01298   cout << " Expanded\n";
01299   for(int a=0; a<8; a++) {
01300     int data = readRegister(a+8, false) & 0xffff;
01301     cout.width(4);
01302     cout << hex;
01303     cout << data << " ";
01304   }
01305   cout << endl;
01306 
01307   cout << " Config mode\n";
01308   for(int a=0; a<8; a++) {
01309     int data = readRegister(a+0x10, false) & 0xffff;
01310     cout.width(4);
01311     cout << hex;
01312     cout << data << " ";
01313   }
01314   cout << endl;
01315 
01316   cout << " Edge mode\n";
01317   for(int a=0; a<8; a++) {
01318     int data = readRegister(a+0x18, false) & 0xffff;
01319     cout.width(4);
01320     cout << hex;
01321     cout << data << " ";
01322   }
01323   cout << endl;
01324 
01325   cout << " Time out\n";
01326   for(int a=0; a<8; a++) {
01327     int data = readRegister(a+0x20, false) & 0xff;
01328     cout.width(4);
01329     cout << hex;
01330     cout << data << " ";
01331   }
01332   cout << endl;
01333 
01334   cout << " Overflow limit\n";
01335   for(int a=0; a<8; a++) {
01336     int data = readRegister(a+0x28, false) & 0x7ff;
01337     cout.width(4);
01338     cout << hex;
01339     cout << data << " ";
01340   }
01341   cout << endl;
01342 
01343   cout << " H/T limit\n";
01344   for(int a=0; a<8; a++) {
01345     int data = readRegister(a+0x30, false) & 0x3ff;
01346     cout.width(4);
01347     cout << hex;
01348     cout << data << " ";
01349   }
01350   cout << endl;
01351 
01352   cout << " ROD busy limit\n";
01353   for(int a=0; a<8; a++) {
01354     int data = readRegister(a+0x38, false) & 0x3ff;
01355     cout.width(4);
01356     cout << hex;
01357     cout << data << " ";
01358   }
01359   cout << endl;
01360 
01361   cout << " Diagnostic link to LEMO\n";
01362   for(int a=0; a<8; a++) {
01363     int data = readRegister(a+0x70, false) & 0xf;
01364     cout.width(4);
01365     cout << hex;
01366     cout << data << " ";
01367   }
01368   cout << endl;
01369 
01370   cout << " Diagnostic mode bits read enable\n";
01371   for(int a=0; a<8; a++) {
01372     int data = readRegister(a+0x78, false) & 0x1;
01373     cout.width(4);
01374     cout << hex;
01375     cout << data << " ";
01376   }
01377   cout << endl;
01378 
01379   cout << "Link occupancies\n";
01380   for(int a=0x0; a<96; a++) {
01381     int link = readRegister(0x80 + a, false) & 0xfff;
01382     cout.width(3);
01383     cout << link << " ";
01384 
01385     if(a % 12 == 11) cout << endl;
01386 
01387   }
01388   cout << endl;
01389 
01390   cout << " Timeout error\n";
01391   for(int a=0; a<8; a++) {
01392     int data = readRegister(a+0xe0, false) & 0xfff;
01393     cout.width(4);
01394     cout << hex;
01395     cout << data << " ";
01396   }
01397   cout << endl;
01398 
01399   cout << " Overflow error\n";
01400   for(int a=0; a<8; a++) {
01401     int data = readRegister(a+0xe8, false) & 0xfff;
01402     cout.width(4);
01403     cout << hex;
01404     cout << data << " ";
01405   }
01406   cout << endl;
01407 
01408   cout << " H/T error\n";
01409   for(int a=0; a<8; a++) {
01410     int data = readRegister(a+0xf0, false) & 0xfff;
01411     cout.width(4);
01412     cout << hex;
01413     cout << data << " ";
01414   }
01415   cout << endl;
01416 
01417   cout << " ROD busy error\n";
01418   for(int a=0; a<8; a++) {
01419     int data = readRegister(a+0xf8, false) & 0xfff;
01420     cout.width(4);
01421     cout << hex;
01422     cout << data << " ";
01423   }
01424   cout << endl;
01425 
01426   cout << " Parsing mode (0 = Raw, 1 = normal) \n";
01427   for(int a=0; a<8; a++) {
01428     int data = readRegister(a+0x100, false) & 0xfff;
01429     cout.width(4);
01430     cout << hex;
01431     cout << data << " ";
01432   }
01433   cout << endl;
01434 
01435   cout << " Formatter status\n";
01436   for(int a=0; a<8; a++) {
01437     int data = readRegister(a+0x108, false) & 0xffff;
01438     cout.width(4);
01439     cout << hex;
01440     cout << data << " ";
01441   }
01442   cout << endl;
01443 
01444   cout << " Formatter versions\n";
01445   for(int a=0; a<8; a++) {
01446     int data = readRegister(a+0x110, false) & 0xffff;
01447     cout.width(4);
01448     cout << hex;
01449     cout << data << " ";
01450   }
01451   cout << endl;
01452 
01453   cout << " Formatter mode bits\n";
01454   for(int a=0; a<8; a++) {
01455     int data = readRegister(a+0x118, false); // & 0xfff;
01456     cout.width(4);
01457     cout << hex;
01458     cout << data << " ";
01459 //     data = readRegister(a+0x120, false); //  & 0xfff;
01460 //     cout.width(4);
01461 //     cout << data << " ";
01462 //     cout << dec;
01463   }
01464   cout << endl;
01465 
01466   cout << "Formatter to front panel\n";
01467   readRegister(0x1a4);
01468 
01469   cout << "EFB version\n";
01470   readRegister(0x1b2);
01471 
01472   cout << "RTR version\n";
01473   readRegister(0x205);
01474 
01475   cout << "RRIF version\n";
01476   readRegister(0x216); // 0x27d);
01477   cout << "RRIF command 0\n";
01478   readRegister(0x218);
01479   cout << "RRIF command 1\n";
01480   readRegister(0x217);
01481   cout << "RRIF status 0\n";
01482   readRegister(0x21c);
01483   cout << "RRIF status 1\n";
01484   readRegister(0x21b);
01485 
01486   cout << "Output masks\n";
01487   readRegister(0x21f);
01488   readRegister(0x220);
01489   readRegister(0x221);
01490   readRegister(0x222);
01491 
01492   cout << "L1ID for ports 0 and 1\n";
01493   readRegister(0x229);
01494   readRegister(0x22a);
01495   cout << "BCID for ports 0 and 1\n";
01496   readRegister(0x22b);
01497 }
01498 
01499 void print_formatter() {
01500   cout << "Formatter status\n";
01501   for(int a=0; a<8; a++) {
01502     int data = readRegister(a+0x108, false) & 0xffff;
01503     cout.width(4);
01504     cout << hex;
01505     cout << " Formatter " << a << ": 0x" << data << ":\n";
01506     cout << dec; 
01507     cout << "  " << (data & 0x1f) << " triggers pending"
01508      << "     mode bits " << ((data & 0x40)?"empty":((data & 0x80)?"full":"not full"))
01509      << "         " << ((data & 0x4000)?"master":"slave") << endl;
01510     cout << "  active link ";
01511     cout.width(2);
01512     cout << ((data & 0xf00) >> 8)
01513      << "         " << ((data & 0x1000)?"chip has token  ":"token status idle") 
01514      << "       hold output " << ((data & 0x2000)?"hold":"OK")
01515      << "       DLL " << ((data & 0x8000)?"locked":"error") << endl;
01516   }
01517 }
01518 
01519 void print_mode_bits() {
01520   cout << "Mode bits LUT select\n";
01521 
01522   int data = tapi.dspSingleRead(0, 0, 0, 0x40441c, -1);
01523   cout.width(2);
01524   cout << data << " ";
01525   cout << endl;
01526 
01527   for(int m=0; m<8; m++) {
01528     cout << "LUT set: " << m << endl;
01529     for(int f=0; f<8; f++) {
01530       cout << "Formatter: " << f << endl;
01531       // Default mode bit 0
01532       data = tapi.dspSingleRead(0, 0, 0, 0x404800 + 8 * f + 0x80 * m, -1);
01533       printBinary(data, 12); cout << " ";
01534 
01535       // Default mode bit 1
01536       data = tapi.dspSingleRead(0, 0, 0, 0x404804 + 8 * f + 0x80 * m, -1);
01537       printBinary(data, 12); cout << " ";
01538 
01539 
01540       // Corrective mode bit 0
01541       data = tapi.dspSingleRead(0, 0, 0, 0x404840 + 8 * f + 0x80 * m, -1);
01542       printBinary(data, 12); cout << " ";
01543 
01544       // Corrective mode bit 1
01545       data = tapi.dspSingleRead(0, 0, 0, 0x404844 + 8 * f + 0x80 * m, -1);
01546       printBinary(data, 12); cout << " ";
01547       cout << endl;
01548     }
01549 
01550     // Mask bits 0-31
01551     data = tapi.dspSingleRead(0, 0, 0, 0x404600 + 0x10 * m, -1);
01552     printBinary(data, 32, 16);
01553 
01554     // SP0 bits 32-47
01555     data = tapi.dspSingleRead(0, 0, 0, 0x404604 + 0x10 * m, -1);
01556     printBinary(data, 16); cout << " ";
01557 
01558     // SP1 bits 0-31
01559     data = tapi.dspSingleRead(0, 0, 0, 0x404608 + 0x10 * m, -1);
01560     printBinary(data, 32, 16);
01561 
01562     data = tapi.dspSingleRead(0, 0, 0, 0x40460c + 0x10 * m, -1);
01563     printBinary(data, 16); cout << " ";
01564     cout << endl;
01565   }
01566 }
01567 
01568 void print_dsp_mode_bits(unsigned long mCD) {
01569   // CmdMask    4 * 2
01570   // DynMask    8 * 2
01571   // FmtMask    16
01572   // valid      1 * 2 * 2
01573   // lemo       4
01574   // DeltaMask  2 * 8
01575 
01576   int delta = 64;
01577 
01578   unsigned int readlength;
01579   unsigned long *mem = tapi.dspBlockRead(0, 0, 0, mCD, delta * 8, -1, &readlength);
01580 
01581   for(int m=0; m<8; m++) {
01582     cout << "Mask set: " << m << endl;
01583 
01584     // CmdMask
01585 
01586     cout << " CMD Mask 0: ";
01587 
01588     // Mask bits 0-31
01589     int data = mem[0 + delta * m]; printBinary(data, 32, 16);
01590 
01591     // SP0 bits 32-47
01592     data = mem[1 + delta * m]; printBinary(data, 16);
01593     cout << endl;
01594 
01595     cout << " CMD Mask 1: ";
01596     // SP1 bits 0-31
01597     data = mem[4 + delta * m]; printBinary(data, 32, 16);
01598 
01599     // SP1 bits 32-47
01600     data = mem[5 + delta * m]; printBinary(data, 16);
01601     cout << " \n";
01602 
01603     // DynMask
01604     for(int f=0; f<8; f++) {
01605       cout << " Formatter: " << f << " ";
01606       // Default mode bit 0 and 1
01607       data = mem[8 + f + delta * m];
01608       printBinary(data & 0xfff, 12); cout << " ";
01609       printBinary((data >> 16) & 0xfff, 12); cout << " ";
01610  
01611       // Corrective mode bit 0
01612       data = mem[16 + f + delta * m];
01613       printBinary(data & 0xfff, 12); cout << " ";
01614       printBinary((data >> 16) & 0xfff, 12); cout << " ";
01615 
01616       cout << endl;
01617 
01618       // FmtMask
01619       // Skip fmt Encable ( 24 )
01620       // Skip fmt Cfg     ( 28 )
01621     }
01622  
01623     // DataLinkMask
01624     cout << " Data link mask: ";
01625     data = mem[32 + delta * m];
01626     printBinary(data, 32, 16);// cout << " ";
01627     data = mem[33 + delta * m];
01628     printBinary(data, 32, 16);// cout << " ";
01629     data = mem[34 + delta * m];
01630     printBinary(data, 32, 16); cout << " \n";
01631 
01632     // Valid modules
01633     cout << " Modules:        ";
01634     data = mem[40 + delta * m];
01635     printBinary(data, 32, 16);
01636     data = mem[41 + delta * m];
01637     printBinary(data, 16); cout << " ";
01638     data = mem[42 + delta * m];
01639     printBinary(data, 32, 16);
01640     data = mem[43 + delta * m];
01641     printBinary(data, 16); cout << " ";
01642     cout << endl;
01643 
01644     // Lemo
01645     data = mem[44 + delta * m];
01646     cout << " Lemo link: " << data;
01647     data = mem[45 + delta * m];
01648     cout << " formatter: " << data << endl;
01649 
01650     // Delta masks
01651   }
01652 }
01653 
01654 void print_dsp_moduleConfig(int dsp) {
01655   unsigned int readlength;
01656 
01657   unsigned int address;
01658   if(dsp == -1) address = 0x02029b00;
01659   else address = 0xa0013420;
01660 
01661   unsigned long *mem = tapi.dspBlockRead(0, 0, 0, address, 2 * 48, dsp, &readlength);
01662   cout << hex;
01663 
01664   for(int i=0; i<48; i++) {
01665     cout << "tx: " << (mem[0 + i * 2] & 0xff) << " rx: ";
01666     int data = mem[1 + i * 2];
01667     for(int r=0; r<2; r++) {
01668       cout << ((data >> (r * 8)) & 0xff) << " ";
01669     }
01670     cout << endl;
01671   }
01672   cout << dec;
01673 }
01674 
01675 void print_rod_Status() {
01676   unsigned int reg;
01677 
01678   // Really a command register
01679 //   reg = readRegister(0x218);
01680 
01681   reg = readRegister(0x217);
01682 
01683   cout << "FE Command output data streams " << ((reg&1)?"on":"off") << endl;
01684   cout << "Command input source for mask 1 " << ((reg&2)?"TIM":"DSP SP0") << endl;
01685 
01686   cout << "Calibration Trigger Signal Decoder " << ((reg&0x40000)?"Enable":"Idle") << endl;
01687   cout << "Configuration readback " << ((reg&0x80000)?"Enable":"Idle") << endl;
01688   cout << "Config/Cal Mask Load " << ((reg&0x100000)?"Enable Load":"Idle") << endl;
01689   cout << "Static Callibration BCID " << ((reg&0x200000)?"Static BCID":"Dynamic BCID") << endl;
01690 
01691   cout << "ROD type " << ((reg&0x80000000)?"Pixel":"SCT") << endl;
01692 
01693   reg = readRegister(0x21c);
01694 
01695   cout << "ECR ID Counter Value " << ((reg & 0xff)) << endl;
01696   cout << "FE Trigger FIFO Counter Value " << ((reg & 0x7ff00) >> 8) << endl;
01697 
01698   reg = readRegister(0x21b);
01699 
01700   cout << "TIM Clock " << ((reg&1)?"OK":"***FAIL***") << endl;
01701   cout << "BOC Clock " << ((reg&2)?"OK":"***FAIL***") << endl;
01702   cout << "BOC " << ((reg&4)?"Busy":"Ready") << endl;
01703   cout << "Configuration read back/Trap link data " << ((reg&8)?"Done":"Ready") << endl;
01704 
01705   cout << "Cal test ready " << ((reg&0x10)?"Ready":"Running") << endl;
01706   cout << "FE Trigger FIFO " << ((reg&0x20)?"Empty":"Not Empty") << endl;
01707   cout << "FE Trigger FIFO " << ((reg&0x40)?"Full":"Not Full") << endl;
01708   cout << "Formatter A Mode bits FIFO " << ((reg&0x80)?"Empty":"Not Empty") << endl;
01709 
01710   cout << "Formatter A Mode bits FIFO " << ((reg&0x100)?"Full":"Not Full") << endl;
01711   cout << "Formatter B Mode bits FIFO " << ((reg&0x200)?"Empty":"Not Empty") << endl;
01712   cout << "Formatter B Mode bits FIFO " << ((reg&0x400)?"Full":"Not Full") << endl;
01713   cout << "Header/Trailer limit formatter bank A " << ((reg&0x800)?"Limit":"OK") << endl;
01714 
01715   cout << "Header/Trailer limit formatter bank B " << ((reg&0x1000)?"Limit":"OK") << endl;
01716   cout << "ROD Busy limit Formatter Bank A " << ((reg&0x2000)?"Busy":"OK") << endl;
01717   cout << "ROD Busy limit Formatter Bank B " << ((reg&0x4000)?"Busy":"OK") << endl;
01718   cout << "EFB Dynamic Mask Bits FIFO " << ((reg&0x8000)?"Empty":"Not Empty") << endl;
01719 
01720   cout << "EFB Dynamic Mask Bits FIFO " << ((reg&0x10000)?"Full":"Not Full") << endl;
01721   cout << "EFB Event ID Empty " << ((reg&0x20000)?"Error":"OK") << endl;
01722   cout << "Event memory A FIFO " << ((reg&0x40000)?"Empty":"Not Empty") << endl;
01723   cout << "Event memory A FIFO " << ((reg&0x80000)?"Full":"Not Full") << endl;
01724 
01725   cout << "Event memory B FIFO " << ((reg&0x100000)?"Empty":"Not Empty") << endl;
01726   cout << "Event memory B FIFO " << ((reg&0x200000)?"Full":"Not Full") << endl;
01727 
01728   cout << "FE Command Pulse Counter 0x" << hex << ((reg&0x3fc00000) >> 22) << dec << endl;
01729 
01730   cout << "FE Occupancy counter " << ((reg&0x40000000)?"All Zero":"Not Zero") << endl;
01731   cout << "Mode Bits " << ((reg&0x80000000)?"Error":"OK") << endl;
01732 
01733 }
01734 
01735 void changeVariable(int module, int bank, int chip, int type, float val) {
01736   cout << "Change variable macro\n";
01737   tapi.createDebugPrimList();
01738 
01739   unsigned long *buffer = new unsigned long[7];
01740 
01741   buffer[0] = 0;         // _not_ write
01742   buffer[1] = bank;
01743   buffer[2] = 8;         // ALL_GROUPS
01744   buffer[3] = module;
01745   buffer[4] = chip;
01746   buffer[5] = type;
01747   buffer[6] = 1;         // Send text message confirming what was done
01748   buffer[7] = 1;
01749   unsigned int intVal = (unsigned int*)(&val);
01750   buffer[8] = (MDAT32*)intVal;
01751 
01752   tapi.addDebugPrimList(4 + 8, 1, 8208, 102, buffer);
01753   tapi.sendDebugPrimList(0, 0, 0);
01754   tapi.awaitResponse(0, 0, 0);
01755 
01756 //    body = tapi.getResponse(0, 0, 0);
01757   // Clear prim list for next time
01758   tapi.createDebugPrimList();
01759 }
01760 
01761 void setModuleMask(int port, int cfg, unsigned long mask[2], int type, int storage, int maskSet) {
01762   cout << "Set module mask variable macro\n";
01763   tapi.createDebugPrimList();
01764 
01765   unsigned long *buffer = new unsigned long[16];
01766   buffer[0] = 0;
01767   buffer[1] = port;
01768   buffer[2] = 0;
01769   buffer[3] = 0;
01770   buffer[4] = 0;
01771   buffer[5] = 0;
01772   buffer[6] = 0;
01773   buffer[7] = 0;
01774   buffer[8] = 0;
01775   buffer[9] = 0;
01776   buffer[10] = cfg;
01777   buffer[11] = mask[0];
01778   buffer[12] = mask[1];
01779   buffer[13] = type;
01780   buffer[14] = storage;
01781   buffer[15] = maskSet;
01782 
01783   tapi.addDebugPrimList(4 + 16, 1, 10, 101, buffer);
01784   tapi.sendDebugPrimList(0, 0, 0);
01785   tapi.awaitResponse(0, 0, 0);
01786 
01787 //    body = tapi.getResponse(0, 0, 0);
01788   // Clear prim list for next time
01789   tapi.createDebugPrimList();
01790 }
01791 
01792 void initMaskSet(int maskSet) {
01793   unsigned long masks[2] = {0, 0};
01794   setModuleMask(2, 1, masks, 0xfff, 0, maskSet); 
01795   // int port, int cfg, unsigned long mask[2], int type, int storage, int maskSet) {
01796 }
01797 
01798 void doEyePlot() {
01799   int histo[25][2][2];
01800 
01801   for(int delay = 0; delay < 25; delay++) {
01802     // Set BOC delay
01803 
01804     for(int event = 0; event < 100; event++) {
01805       readLinksToFifos(25, 100);
01806 
01807       unsigned long mask = 0x1;
01808 
01809       // Examine clock by two
01810       for (int i=0; i<100/2; i++) {
01811         if(scratch[i*6 + 0] & mask) {
01812           histo[delay][0][0] ++;
01813         } else {
01814           histo[delay][0][1] ++;
01815         }
01816 
01817         if(scratch[i*6 + 3] & mask) {
01818           histo[delay][1][0] ++;
01819         } else {
01820           histo[delay][1][1] ++;
01821         }
01822       }
01823     }
01824     cout << histo[delay][0][0] << " ";
01825     cout << histo[delay][0][1] << " ";
01826     cout << histo[delay][1][0] << " ";
01827     cout << histo[delay][1][1] << endl;
01828   }
01829 }
01830 
01831 void rawScan() {
01832   int histo[25][1536];
01833 
01834   for(int delay = 0; delay < 25; delay++) {
01835     tapi.modifyBOCParam(0); // Set BOC delay
01836 
01837     for(int event = 0; event < 20; event++) {
01838       readLinksToFifos(25, 1536);
01839 
01840       unsigned long mask = 0x1;
01841 
01842       // Examine clock by two
01843       for (int i=0; i<1536; i++) {
01844         if(scratch[i*3 + 0] & mask) {
01845           histo[delay][i] ++;
01846         }
01847       }
01848     }
01849   }
01850 
01851   for(int i=0; i<25; i++) {
01852     cout << histo[i][0] << " ";
01853     cout << histo[i][1] << " ";
01854     cout << histo[i][2] << " ";
01855     cout << histo[i][3] << endl;
01856   }
01857 }
01858 
01859 void printEventTrap() {
01860   cout << "Trap command:\n";
01861   for(int i=0; i<4; i++) {
01862     int command = readRegister(0x172+i, false) & 0x7f;
01863     cout << command << " ";
01864     command = readRegister(0x176+i, false) & 0x7;
01865     cout << command << "  ";
01866   }
01867   cout << endl;
01868 
01869   cout << "Trap status:\n";
01870   for(int i=0; i<4; i++) {
01871     int status = readRegister((0x17e)+i, false) & 0xff;
01872     cout << status << " ";
01873   }
01874   cout << endl;
01875 
01876   cout << "Trap match:\n";
01877   for(int i=0; i<4; i++) {
01878     int match = readRegister(0x182+i, false) & 0xff;
01879     cout << match << " ";
01880     match = readRegister(0x18a+i, false) & 0xff;
01881     cout << match << " ";
01882   }
01883   cout << endl;
01884 
01885   cout << "Trap rem/mod:\n";
01886   for(int i=0; i<4; i++) {
01887     int modrem = readRegister(0x186+i, false) & 0xffff;
01888     cout << modrem << " ";
01889     modrem = readRegister((0x18e)+i, false) & 0xffff;
01890     cout << modrem << " ";
01891   }
01892   cout << endl;
01893 
01894   cout << "Trap FIFO count:\n";
01895   for(int i=0; i<4; i++) {
01896     int count = readRegister(0x196+i, false) & 0x3ff;
01897     cout << count << " ";
01898   }
01899   cout << endl;
01900 
01901   cout << "Trap mask delay:\n";
01902   for(int i=0; i<4; i++) {
01903     int delay = readRegister(0x1a2+i, false) & 0x3f;
01904     cout << delay << " ";
01905   }
01906   cout << endl;
01907 }
01908 
01909 void printEventTrap2() {
01910   cout << "Trap command:\n";
01911   for(int i=0; i<4; i++) {
01912     int command = readRegister(0x1c2+i, false) & 0x7f;
01913     cout << command << " ";
01914     command = readRegister(0x1c6+i, false) & 0x7;
01915     cout << command << "  ";
01916   }
01917   cout << endl;
01918 
01919   cout << "Trap status:\n";
01920   for(int i=0; i<4; i++) {
01921     int status = readRegister((0x1ce)+i, false) & 0xff;
01922     cout << status << " ";
01923   }
01924   cout << endl;
01925 
01926   cout << "Trap match:\n";
01927   for(int i=0; i<4; i++) {
01928     int match = readRegister(0x1d2+i, false) & 0xff;
01929     cout << match << " ";
01930     match = readRegister(0x1da+i, false) & 0xff;
01931     cout << match << " ";
01932   }
01933   cout << endl;
01934 
01935   cout << "Trap rem/mod:\n";
01936   for(int i=0; i<4; i++) {
01937     int modrem = readRegister(0x1d6+i, false) & 0xffff;
01938     cout << modrem << " ";
01939     modrem = readRegister((0x1de)+i, false) & 0xffff;
01940     cout << modrem << " ";
01941   }
01942   cout << endl;
01943 
01944   cout << "Trap FIFO count:\n";
01945   for(int i=0; i<4; i++) {
01946     int count = readRegister(0x1e6+i, false) & 0x3ff;
01947     cout << count << " ";
01948   }
01949   cout << endl;
01950 
01951   cout << "Trap mask delay:\n";
01952   for(int i=0; i<4; i++) {
01953     int delay = readRegister(0x1f2+i, false) & 0x3f;
01954     cout << delay << " ";
01955   }
01956   cout << endl;
01957 }
01958 
01959 void rodModeCapture(int delay, int units) {
01960   tapi.rodMode(0, 0, 0, 0x00040002, 0, 1, units, delay, 1);
01961   sendL1A();
01962 
01963   unsigned long *bufferA = readFifo(0, 0, units, false);
01964   unsigned long *bufferB = readFifo(0, 1, units, false);
01965 
01966   cout << hex;
01967   cout.fill('0');
01968   for(int i=0; i<units/2; i++) {
01969     cout.width(8); cout << bufferA[i*3 + 0] << " ";
01970     cout.width(8); cout << bufferA[i*3 + 1] << " ";
01971     cout.width(8); cout << bufferA[i*3 + 2] << " ";
01972     cout.width(8); cout << bufferB[i*3 + 0] << " ";
01973     cout.width(8); cout << bufferB[i*3 + 1] << " ";
01974     cout.width(8); cout << bufferB[i*3 + 2] << " ";
01975     cout << endl;
01976   }
01977   cout << dec;
01978   cout.fill(' ');
01979 }
01980 
01981 void decodeConfig() {
01982   unsigned int readlength;
01983   unsigned long *config = tapi.dspBlockRead(0, 0, 0, 0x2102000, 270 * 1, -1, &readlength);
01984 
01985   int pos = 0;
01986   while(pos < readlength) {
01987     if((config[pos] & 0x5760000) != 0x5700000) {
01988       cout << "Strange data: ";
01989       cout << hex << " " << pos << " " << config[pos] << dec << endl;
01990       break;
01991     } 
01992 
01993     unsigned int f3 = (config[pos] & 0xff000) >> 12;
01994     unsigned int f4 = (config[pos] & 0x00fc0) >> 6;
01995     unsigned int f5 = (config[pos] & 0x0003f) >> 0;
01996 
01997     pos ++;
01998 
01999     cout << "Chip address " << f4 << ": ";
02000     cout << hex << f3 << "/" << f5 << dec << ": ";
02001 
02002     switch(f3) {
02003     case 0x1c:
02004       unsigned short data = (0xffff0000 & config[pos]) >> 16;
02005       switch(f5) {
02006       case 0:
02007         // Configuration reg
02008         cout << "Config: " << hex << data << dec;
02009         cout << " comp " << (data & 3) 
02010              << " cal "  << ((data & 0xc) >> 2)
02011              << " trim "  << ((data & 0x30) >> 4)
02012              << " edge "  << ((data & 0x40) >> 6)
02013              << " mask "  << ((data & 0x80) >> 7)
02014              << " acc "  << ((data & 0x100) >> 8)
02015              << " in_b " << ((data & 0x200) >> 9)
02016              << " out_b " << ((data & 0x400) >> 10)
02017              << " mast " << ((data & 0x800) >> 11)
02018              << " end " << ((data & 0x1000) >> 12)
02019              << " feed " << ((data & 0x2000) >> 13)
02020              << endl;
02021         break;
02022       case 0x10:
02023         cout << "Strobe delay: " << hex 
02024              << (data & 0x3f) << dec << endl;
02025         break;
02026       case 0x18:
02027         cout << "Threshold/Cal: " << hex
02028              << ((data & 0xff00) >> 8) << "/" 
02029              << (data & 0xff) << dec << endl;
02030         break;
02031       case 0x38:
02032         cout << "preamp/shaper: " << hex
02033              << ((data & 0xff00) >> 8) << "/" 
02034              << (data & 0xff) << dec << endl;
02035         break;
02036       case 0x04:
02037         cout << "\r";
02038 //         cout << "TRIM DAC: " << hex
02039 //              << (data & 0xf) << " " 
02040 //              << ((data & 0x7f0) >> 4) << dec << endl;
02041         break;
02042       }
02043       pos ++;
02044       break;
02045     case 0x8c:
02046       // Mask reg
02047       cout << "Mask ";
02048       cout << hex;
02049       for(int i=0; i<4; i++) {
02050         cout << config[pos++] << " ";
02051       }
02052       cout << dec;
02053       cout << endl;
02054       pos++;
02055       break;
02056     case 0x0c:
02057       cout << "Something else\n";
02058       break;
02059     }
02060   }
02061 }
02062 
02063 void testConfigVariable(float start, float stop, float step, int var) {
02064   for(float val = start; val < stop; val += step) {
02065     tapi.modifyABCDVarROD(var, val, 0);
02066     tapi.sendABCDModule(0, 0);
02067     tapi.decodeConfig(0, 0, 0, true, false);
02068   }
02069 }
02070 
02071 void setSlaveMemory(int s, long add, int words, int value) {
02072   unsigned long *buffer = new unsigned long[3];
02073 
02074   buffer[0] = add;
02075   buffer[1] = words;
02076   buffer[2] = value;
02077 
02078   tapi.createDebugPrimList();
02079   tapi.addDebugPrimList(4 + 3, 1, 4, 100, buffer);
02080   tapi.sendDebugSlavePrimList(0, 0, 0, s, 1, 1);
02081   tapi.awaitResponse(0, 0, 0);
02082   // Clear prim list for next time
02083   tapi.createDebugPrimList();
02084 
02085   cout << "Slave data sent\n";
02086 }
02087 
02088 void setNmask(int n) {
02089   // Put all modules in send all mask mode
02090   tapi.modifyABCDVar(11, 1.0);
02091   tapi.modifyABCDVar(13, 0.0);
02092   tapi.modifyABCDVar(14, 1.0);   
02093   tapi.modifyABCDVar(9, (double)n);
02094   tapi.setABCDModules(0);
02095   tapi.sendABCDModules(0);
02096 }
02097 
02098 void printBinary(unsigned int data, int length, int divisions) {
02099   // Actually -ve length not used...
02100   if(length < 0) {
02101     for(int i=-length-1; i>=0; i--) {
02102       int val = data & (1<<i);
02103       cout << (val?'1':'0');
02104       if(divisions && ((i%divisions) == 0)) cout << " ";
02105     }
02106   } else {
02107     for(int i=0; i<length; i++) {
02108       int val = data & (1<<i);
02109       cout << (val?'1':'0');
02110       if(divisions && ((i%divisions) == divisions-1)) cout << " ";
02111     }
02112   }
02113 }
02114 
02115 void setTrigger(int bin, int slave) {
02116   unsigned long *buffer = new unsigned long[30];
02117 
02118   buffer[18] = 0xf;  // slvbits
02119   
02120   buffer[19] = 0;
02121   buffer[20] = 0;
02122   buffer[21] = bin;
02123   buffer[22] = 0;  // Set
02124   buffer[23] = 0;  // Reps
02125   buffer[24] = 0;  // Interval
02126   buffer[25] = 0;  // preBDO
02127   buffer[26] = 0;
02128   buffer[27] = 0;  // incCmd
02129   buffer[28] = 0;  // incData
02130   buffer[29] = 0;  // incData
02131 
02132   tapi.createDebugPrimList();
02133   tapi.addDebugPrimList(4 + 30, 1, 11, 102, buffer);
02134   tapi.sendDebugSlavePrimList(0, 0, 0, slave, 1, 0);
02135   tapi.awaitResponse(0, 0, 0);
02136   // Clear prim list for next time
02137   tapi.createDebugPrimList();
02138 
02139   cout << "Set bin to " << bin << endl;
02140 }
02141 
02142 void sendData(int type, int dsp) {
02143   unsigned long data[] = {type, 0, 0, 0};
02144   tapi.createDebugPrimList();
02145   tapi.addDebugPrimList(4 + 4, 2, 9, 103, data);
02146 
02147   if(dsp == -1) {
02148     tapi.sendDebugPrimList(0, 0, 0);
02149   } else {
02150     tapi.sendDebugSlavePrimList(0, 0, 0, dsp, 1, 1);
02151   }
02152   tapi.awaitResponse(0, 0, 0);
02153 
02154   unsigned long *result = tapi.getResponse(0, 0, 0);
02155 
02156   if(result) {
02157     if(dsp == -1) {
02158       cout << "pointer " << (unsigned long)(result[8]) << endl;
02159       cout << "length " << (unsigned long)(result[9]) << endl;
02160 
02161       tapi.dspBlockDumpFile(0, 0, 0, (unsigned long)(result[8]), (unsigned long)(result[9]), -1, "SendDataDump.bin");
02162     } else {
02163       cout << "pointer " << (unsigned long)(result[16]) << endl;
02164       cout << "length " << (unsigned long)(result[17]) << endl;
02165       tapi.dspBlockDumpFile(0, 0, 0, (unsigned long)(result[16]), (unsigned long)(result[17]), dsp, "SendDataDump.bin");
02166     }
02167   }
02168 }

Generated on Fri Sep 16 18:01:51 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5