00001
00002
00003 #include <iostream>
00004 using namespace std;
00005
00006 #include <ctype.h>
00007
00008 #include "RodModule.h"
00009 #include "BaseException.h"
00010
00011 #include "primParams.h"
00012 #include "RCCVmeInterface.h"
00013 #include "parameters.h"
00014
00015 int main(int argc, char *argv[]) {
00016
00017 using namespace SctPixelRod;
00018
00019 const unsigned long mapSize=0xc00040;
00020 RodPrimList primList(1);
00021 long myTextLength;
00022 TEXT_BUFFER_TYPE myTextType;
00023 long numSlaves = 4;
00024 PrimState returnPState;
00025 TextBuffState returnTState;
00026
00027 std::string fileName(""), option;
00028 long dataLength = -1;
00029 int slot = -1;
00030 int repetitions = 1;
00031
00032 unsigned long baseAddress;
00033
00034 if (argc > 1) {
00035 for (int i=1; i<argc; i++) {
00036 option = argv[i];
00037 if (option[0] != '-') break;
00038 switch (option[1]) {
00039 case 's': {
00040 slot = atoi(option.substr(2).c_str());
00041 break;
00042 }
00043 case 'l': {
00044 dataLength = atoi(option.substr(2).c_str());
00045 break;
00046 }
00047 case 'r': {
00048 repetitions = atoi(option.substr(2).c_str());
00049 break;
00050 }
00051 default: {
00052 break;
00053 }
00054 }
00055 }
00056 }
00057
00058
00059 if (slot < 0 ) {
00060 cout << "Enter slot number (decimal):";
00061 cin >> slot;
00062 while ((slot < 1) || (slot > 21)) {
00063 cout << "Slot number out or range [1:21], re-enter: ";
00064 cin >> slot;
00065 }
00066 }
00067 baseAddress = slot << 24;
00068
00069
00070 RCCVmeInterface *vme1;
00071 try {
00072 vme1 = new RCCVmeInterface();
00073 }
00074 catch (VmeException &v) {
00075 cout << "VmeException creating VME interface." << endl;
00076 cout << "ErrorClass = " << v.getErrorClass() << endl;
00077 cout << "ErrorCode = " << v.getErrorCode() << endl;
00078
00079 return 1;
00080 }
00081
00082
00083 RodModule* rod0 = new RodModule(baseAddress, mapSize, *vme1, numSlaves);
00084 try{
00085 rod0->initialize();
00086 }
00087 catch (HpiException &h) {
00088 cout << h;
00089
00090 return 1;
00091 }
00092 catch (VmeException &v) {
00093 cout << "VmeException initialising RodModule." << endl;
00094 cout << "ErrorClass = " << v.getErrorClass() << endl;
00095 cout << "ErrorCode = " << v.getErrorCode() << endl;
00096
00097 return 1;
00098 }
00099 catch (RodException &r) {
00100 cout << r;
00101
00102 return 1;
00103 }
00104 catch (BaseException &b) {
00105 cout << b;
00106
00107 return 1;
00108 }
00109
00110
00111 char * myTextBuffer;
00112 try {
00113 myTextBuffer= new char[TEXT_BUFF_SIZE];
00114 }
00115 catch (std::bad_alloc) {
00116 cout << "Unable to allocate text buffer in main.";
00117 }
00118
00119
00120 long* echoData;
00121
00122 if(dataLength < 0) {
00123 cout << "Enter number of data words: ";
00124
00125 cin >> dataLength;
00126 }
00127
00128 try {
00129 echoData = new long[dataLength];
00130 }
00131 catch (std::bad_alloc) {
00132 cout << "Unable to allocate echoData in main.";
00133 return 1;
00134 }
00135
00136 for (long i=0; i<dataLength; i++) {
00137 echoData[i] = 0xbeef0000 | i;
00138 }
00139 RodPrimitive* echo;
00140 try {
00141 echo= new RodPrimitive(dataLength+4, 1, ECHO, R_ECHO, echoData);
00142 }
00143 catch (std::bad_alloc) {
00144 cout << "Unable to allocate ECHO primitive in main.";
00145
00146 return 1;
00147 }
00148
00149 primList.insert(primList.begin(), *echo);
00150 try {
00151 primList.bufferBuild();
00152 }
00153 catch (PrimListException &p) {
00154 cout << p.getDescriptor() << " ";
00155 cout << p.getData1() << ", " << p.getData2() << "\n";
00156 };
00157
00158 if(repetitions > 1) {
00159 cout << "Repeating " << repetitions << "times\n";
00160 }
00161
00162 for(int reps=0; reps<repetitions; reps++) {
00163 if(repetitions > 1) {
00164 cerr << ".";
00165 }
00166
00167 try {
00168 rod0->sendPrimList(&primList);
00169 }
00170 catch (VmeException &v) {
00171 cout << "VmeException in SendPrimList." << endl;
00172 cout << "ErrorClass = " << v.getErrorClass() << endl;
00173 cout << "ErrorCode = " << v.getErrorCode() << endl;
00174
00175 return 1;
00176 }
00177 catch (BaseException &h) {
00178 cout << h;
00179
00180 return 1;
00181 }
00182
00183
00184
00185
00186
00187 do {
00188 try {
00189 returnPState = rod0->primHandler();
00190 }
00191 catch (VmeException &v) {
00192 cout << "VmeException in first primHandler call." << endl;
00193 cout << "ErrorClass = " << v.getErrorClass() << endl;
00194 cout << "ErrorCode = " << v.getErrorCode() << endl;
00195
00196 return 1;
00197 }
00198 try {
00199 returnTState = rod0->textHandler();
00200 }
00201 catch (VmeException &v) {
00202 cout << "VmeException in first textHandler call." << endl;
00203 cout << "ErrorClass = " << v.getErrorClass() << endl;
00204 cout << "ErrorCode = " << v.getErrorCode() << endl;
00205
00206 return 1;
00207 }
00208 if (returnTState == TEXT_RQ_SET) {
00209 do {
00210 try {
00211 returnTState = rod0->textHandler();
00212 }
00213 catch (VmeException &v) {
00214 cout << "VmeException in second textHandler call." << endl;
00215 cout << "ErrorClass = " << v.getErrorClass() << endl;
00216 cout << "ErrorCode = " << v.getErrorCode() << endl;
00217
00218 return 1;
00219 }
00220 } while (returnTState != TEXT_READOUT);
00221 rod0->getTextBuffer(myTextBuffer, myTextLength, myTextType);
00222 rod0->clearTextBuffer();
00223 for (int i=0; i<myTextLength; i++) {
00224 cout << myTextBuffer[i];
00225 if (0==(i+1)%64) cout << endl;
00226 }
00227 cout << endl;
00228 }
00229 } while (returnPState != PRIM_EXECUTING);
00230 do {
00231 try {
00232 returnPState = rod0->primHandler();
00233 }
00234 catch (RodException &r) {
00235 cout << r;
00236 return 1;
00237 }
00238 catch (VmeException &v) {
00239 cout << "VmeException in second primHandler call." << endl;
00240 cout << "ErrorClass = " << v.getErrorClass() << endl;
00241 cout << "ErrorCode = " << v.getErrorCode() << endl;
00242
00243 return 1;
00244 }
00245 catch (BaseException &r) {
00246 cout << r;
00247 return 1;
00248 }
00249 } while (returnPState != PRIM_WAITING && returnPState != PRIM_IDLE);
00250
00251
00252 RodOutList* outList = rod0->getOutList();
00253
00254 if(outList) {
00255
00256 UINT32 outListLength = UINT32(outList->getLength());
00257 unsigned long* outBody = outList->getBody();
00258 UINT32 outLength = UINT32(outBody[0]);
00259
00260 if(outLength != outListLength) {
00261
00262
00263 cout << " ** Mismatch between RodOutList length and first word of RodOutList body!";
00264 cout << " outLength = 0x" << hex << outLength << " outListLength = 0x" << outListLength << dec << endl;
00265
00266
00267 continue;
00268 }
00269
00270 UINT32 outIndex = UINT32(outBody[1]);
00271 UINT32 outNumPrims = outBody[2];
00272 UINT32 outPrimVersion = outBody[3];
00273 if(reps == 0) {
00274 cout << "outLength = " << outLength << ", outIndex = " << outIndex <<
00275 ", outNumPrims = " << outNumPrims << ", outPrimVersion = " <<
00276 outPrimVersion <<'\n';
00277 }
00278 int outPtr = 4;
00279 for (UINT32 j=0; j<outNumPrims; j++) {
00280 UINT32 primLength = outBody[outPtr++];
00281 UINT32 primIndex = outBody[outPtr++];
00282 UINT32 primId = outBody[outPtr++];
00283 UINT32 primVersion = outBody[outPtr++];
00284
00285 if(reps>=1) {
00286 for(UINT32 i=0; i<primLength-4; i++) {
00287 if(outBody[i+outPtr] != echoData[i]) {
00288 cout << "Echo Check failed in word " << i << " of repetition " << reps << "\n";
00289 cout << "0x" << hex << outBody[i+outPtr] << " != 0x" << echoData[i] << dec << endl;
00290 return 1;
00291 }
00292 }
00293 } else {
00294 cout << "primLength = " << primLength << ", primIndex = " << primIndex <<
00295 ", primId = " << primId << ", primVersion = " << primVersion << '\n';
00296 cout << "ECHO data: " << endl;
00297 hex(cout);
00298
00299 for (UINT32 i=0; i<primLength-4; i++) {
00300 cout.width(8);
00301 cout << outBody[i+outPtr] <<" ";
00302 if (0 == (i+1)%8) cout << endl;
00303 };
00304 if (0 != (primLength-4)%8) cout << endl;
00305 dec(cout);
00306 }
00307 };
00308
00309
00310 unsigned long* myOutBody;
00311 try {
00312 myOutBody= new unsigned long[outLength];
00313 }
00314 catch (std::bad_alloc) {
00315 cout << "Unable to allocate myOutBody in main." << endl;
00316
00317 return 1;
00318 }
00319 for (UINT32 i=0; i<outLength; i++) {
00320 myOutBody[i] = outBody[i];
00321 };
00322
00323 delete [] myOutBody;
00324 rod0->deleteOutList();
00325 } else {
00326 cout << "No response from echo primitive!\n";
00327 }
00328 }
00329
00330 if(repetitions > 1)
00331 cerr << endl;
00332
00333
00334 cout << "Rod Status:" << endl << *rod0 << endl;
00335
00336 cout << "Check text buffers before exiting:\n";
00337
00338 try {
00339 returnTState = rod0->textHandler();
00340 }
00341 catch (VmeException &v) {
00342 cout << "VmeException in first textHandler call." << endl;
00343 cout << "ErrorClass = " << v.getErrorClass() << endl;
00344 cout << "ErrorCode = " << v.getErrorCode() << endl;
00345
00346 return 1;
00347 }
00348 if (returnTState == TEXT_RQ_SET) {
00349 do {
00350 try {
00351 returnTState = rod0->textHandler();
00352 }
00353 catch (VmeException &v) {
00354 cout << "VmeException in second textHandler call." << endl;
00355 cout << "ErrorClass = " << v.getErrorClass() << endl;
00356 cout << "ErrorCode = " << v.getErrorCode() << endl;
00357
00358 return 1;
00359 }
00360 } while (returnTState != TEXT_READOUT);
00361 rod0->getTextBuffer(myTextBuffer, myTextLength, myTextType);
00362 rod0->clearTextBuffer();
00363 for (int i=0; i<myTextLength; i++) {
00364 cout << myTextBuffer[i];
00365 if (0==(i+1)%64) cout << endl;
00366 }
00367 cout << endl;
00368 }
00369
00370
00371 cout << "Text buffers checked\n";
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 primList.clear();
00398 delete [] echoData;
00399 delete echo;
00400 delete [] myTextBuffer;
00401 rod0->deleteOutList();
00402
00403
00404 delete rod0;
00405 delete vme1;
00406
00407 return 0;
00408 }
00409
00410