#include #include #include #include #include #include #include #include #include #define Uses_TDR #define Uses_FileLoad #include "utils.h" #include "tdr_test.h" TTestApp::TTestApp() { Tdr = new TTdrControl(); portError = initParameters(); valueDAC = 0; } TTestApp::~TTestApp() { delete Tdr; delete code; } int TTestApp::initParameters() { code = new uchar[32000]; int status = Tdr->portStatus(); for(int boucle=0;boucle<32000;boucle++) code[boucle]=0; if ( status == stsNormal ) { fprintf(stderr,"EPP Port Found in %XH\nConnection is checked\n", Tdr->portBase()); return FALSE; } else if ( status == stsNotFound ) { fprintf(stderr,"There is no parallel port found in this computer.\n"); fprintf(stderr,"Please check your hardware and computer setup!\n"); } else if ( status == stsNotEPP ) { fprintf(stderr,"The parallel port (%XH) is not in EPP mode.\n", Tdr->portBase()); fprintf(stderr,"Please check your computer setup!\n"); } else if ( status == stsNotConnect ) { fprintf(stderr,"There is no connection on EPP port (%XH).\n", Tdr->portBase()); fprintf(stderr,"Please check your hardware!\n"); } else { fprintf(stderr,"Unknown status port = %XH.\n",Tdr->portBase()); fprintf(stderr,"Please check your hardware!\n"); }; return TRUE; } /* readFile type :: typMCSFile,typBNMFile,typ21KFile,typLDRFile,typEXEFile filename :: Name of file to read */ int TTestApp::readFile(int type,char *filename) { FileLoad *Load = new FileLoad(filename, type, code, &nbytes, &address); ushort error = Load->checkError(); delete Load; if ( !error ) { fprintf(stderr,"File <%s> loaded.\nTotal %d bytes starting from %x\n", filename, nbytes, address); } else if ( error == errFileNotExist ) fprintf(stderr,"File is not exist!\n"); else if ( error == errFileOpen ) fprintf(stderr,"File open error!\n"); else if ( error == errCheckSum ) fprintf(stderr,"Checksum error!\n"); else if ( error == errByteCount ) fprintf(stderr,"Data counter error!\n"); else if ( error == errAddress ) fprintf(stderr,"Data address error!\n"); return ((int) error); } char *TTestApp::GetName(uchar tdr) { switch (tdr) { case TDRS_A : return "TDRS A"; case TDRS_B : return "TDRS B"; case TDRK_A : return "TDRK A"; case TDRK_B : return "TDRK B"; case TDRS : return "TDRS A&B"; case TDRK : return "TDRK A&B"; case TDR_ALL : return "ALL TDRS & TDRK"; default : return "TDR ERROR"; }; }; /* loadXilinx tdr :: TDRS_A,TDRS_B,TDRK_A,TDRK_B,TDRS,TDRK,TDR_ALL filename :: Name of starting file. */ void TTestApp::loadXilinx(uchar tdr,char *filename) { int Type=typMCSFile; if ( !readFile(Type,filename) ) { if ( (nbytes & 1) ) nbytes++; if ( Tdr->bootXilinx(tdr, code, nbytes, address) ) { Tdr->writeData(portEppAddress, (ushort) 0); // Tdr->readData(portRamRegister, result, nbytes); if (Tdr->cmpData(portRamRegister,code,nbytes)) fprintf(stderr,"Code downloaded with errors.\n"); else fprintf(stderr,"Xilinx on %s have been booted.\n",GetName(tdr)); } else fprintf(stderr,"Timeout during booting Xilinx on %s!\n", GetName(tdr)); } } void TTestApp::loadDsp(uchar tdr,char *filename) { if ( !readFile(typBNMFile,filename) ) { if ( (nbytes & 1) ) nbytes++; if ( Tdr->bootDsp(tdr, code, nbytes, address) ) { Tdr->writeData(portEppAddress, (ushort) 2); // Tdr->readData(portRamRegister, result, nbytes+2); if (Tdr->cmpData(portRamRegister,code,nbytes)) fprintf(stderr,"Code downloaded with errors.\n"); else fprintf(stderr,"DSP on %s have been booted.\n", GetName(tdr)); } else fprintf(stderr,"Timeout during booting DSP on %s!\n", GetName(tdr)); } } void TTestApp::setCSR0(ushort data) { Tdr->setCSR0(data); fprintf(stderr,"CSR0 was set to %04XH\n", data); } void TTestApp::readCSR0() { fprintf(stderr,"Current CSR0 = %04XH\n", Tdr->getCSR0()); } void TTestApp::readCSR1() { fprintf(stderr,"Current CSR1 = %02XH\n", Tdr->getCSR1()); } void TTestApp::readCSR2() { fprintf(stderr,"Current CSR2 = %08lXH\n", Tdr->getCSR2()); } void TTestApp::readRam(int dnbytes,int daddress) { address=daddress; nbytes=dnbytes; Tdr->readRam(code, nbytes, address); } void TTestApp::dumpData() { DumpData *Dump = new DumpData(TRUE); Dump->dump(code, nbytes); fprintf(stderr,"Data have been dumpped to file .\n", Dump->currentFile()); delete Dump; } void TTestApp::setEppAddr(int addr) { Tdr->EppAddress(addr); fprintf(stderr,"New EPP address = %04XH\n", addr); } void TTestApp::readEppAddr() { fprintf(stderr,"Current EPP address = %04XH\n", Tdr->EppAddress()); } void TTestApp::setSlinkAddr(uchar addr) { Tdr->SlinkAddress(addr); fprintf(stderr,"New SLINK address = %04XH\n", addr); } void TTestApp::readSlinkAddr() { fprintf(stderr,"Current Slink address = %04XH\n", Tdr->SlinkAddress()); } void TTestApp::readBootLength() { fprintf(stderr,"Current BootLength = %04XH\n", Tdr->BootLength()); } void TTestApp::sendMessage(uchar tdr,ulong temp) { Tdr->sendMessage(tdr, temp); fprintf(stderr,"Message %08lXH has been sent to %s\n", temp, GetName(tdr)); } void TTestApp::getStatus(uchar tdr) { ushort data[16],*ptdata=data; int status = Tdr->getStatus(tdr,data); if ( status == FALSE ) fprintf(stderr,"Timeout during waiting status from %s\n", GetName(tdr)); else { fprintf(stderr,"Current Status from %s =\n", GetName(tdr)); for(int boucle=0;boucle<16;boucle++) printf(" %x\n",*ptdata++); }; } void TTestApp::checkMessage(int select) { fprintf(stderr,"Current Message Register %1d = %08lXH\n", select, Tdr->checkMessage(select)); } void TTestApp::loopMsg(uchar tdr) { ulong msg = 0x00020000L; int i = 0; while ( !kbhit() ) { Tdr->sendMessage(tdr, msg); if ( (i++ % 0x100) == 0 ) fprintf(stderr,"%X loops were passed.\n", i); for ( int j = 0; j < 10000; j++); } if ( getch() == 0 ) getch(); fprintf(stderr,"Loop is stopped.\n"); } void TTestApp::loopBoot(uchar tdr) { code[0] = 0x55; code[1] = 0xAA; nbytes = 2; address = 0; fprintf(stderr,"TDR selected is %02XH\n", tdr); int i = 0; while ( !kbhit() ) { if ( !Tdr->bootXilinx(tdr, code, nbytes, address) ) // if ( !Tdr->bootDsp(tdr, code, nbytes, address) ) fprintf(stderr,"Timeout during booting at %X\n", i); if ( (i++ % 0x100) == 0 ) fprintf(stderr,"%X loops were passed.\n", i); } if ( getch() == 0 ) getch(); fprintf(stderr,"Loop is stopped.\n"); } void TTestApp::sendTrigger() { Tdr->sendTrigger(); fprintf(stderr,"One trigger has been sent\n"); } void TTestApp::selectSource(uchar tdr) { Tdr->selectSource(tdr); fprintf(stderr,"%s was select as the source.\n", GetName(tdr)); } void TTestApp::getEvent(uchar tdr) { if ( (nbytes = Tdr->getEvent(tdr, code)) == 0 ) { fprintf(stderr,"Timeout during waiting for event from %s\n",GetName(tdr)); } else { address = 0; double sigmas[4]; double means[4]; sigma(sigmas, means); fprintf(stderr, "Nbytes = %d\n Means= %.0lf %.0lf %.0lf %.0lf\n sigmas= %lf %lf %lf %lf\n", nbytes,means[0], means[1], means[2], means[3], sigmas[0], sigmas[1], sigmas[2], sigmas[3]); } } void TTestApp::resetDsp(uchar tdr) { Tdr->resetDsp(tdr); fprintf(stderr,"DSP on %s was reset\n", GetName(tdr)); } void TTestApp::sendCmd(int cmd) { Tdr->sendCmd((uchar) (cmd & 0xFF)); fprintf(stderr,"Command %02XH has been sent.\n", cmd); } ulong TTestApp::SendPED(uchar tdr,ushort *ptdata) { return Tdr->sendPED(tdr,ptdata); }; ulong TTestApp::SendSIGMA(uchar tdr,ushort *ptdata) { return Tdr->sendSIGMA(tdr,ptdata); }; ulong TTestApp::SendTAB(uchar tdr,ushort *ptdata) { return Tdr->sendTAB(tdr,ptdata); }; ulong TTestApp::SendEvent(uchar tdr,ushort *ptdata) { return Tdr->sendEvent(tdr,ptdata); }; /* void TTestApp::dumpStatus(uchar tdr) { address = 0; nbytes = Tdr->dspStatus(tdr, code); if ( nbytes == 0 ) { fprintf(stderr,"Timeout on waiting for reply from DSP\n"); return; } } */ void TTestApp::sigma(double *s, double *m) { double s_sum[4]; double n_sum[4]; ushort *words = (ushort *) code; // Calculate sum and square sum int n_len = ((swapByte(words[0]) & 0xFFF) - 4) / 4; words += 2; for ( int adc = 0; adc < 4; adc++ ) { n_sum[adc] = 0.0; s_sum[adc] = 0.0; for ( int n = 0; n < n_len; n++ ) { ushort k = swapByte(*words++); n_sum[adc] += (double) k; s_sum[adc] += (double) k * (double) k; } m[adc] = n_sum[adc] / n_len; s[adc] = sqrt(n_len * s_sum[adc] - n_sum[adc] * n_sum[adc]) / n_len; } } void TTestApp::setDAC(uchar tdr,int valueDAC) { Tdr->setDAC(tdr, valueDAC); fprintf(stderr,"DAC value is set to %x\n", valueDAC); } void TTestApp::tdrkDelay(uchar tdrk,int delay) { ushort csr0 = Tdr->getCSR0(); if ( tdrk ) { if ( delay ) csr0 |= csr0DelayTdrkB; else csr0 &= (~csr0DelayTdrkB); } else { if ( delay ) csr0 |= csr0DelayTdrkA; else csr0 &= (~csr0DelayTdrkA); } Tdr->setCSR0(csr0); fprintf(stderr,"TDRK %c serial link delay is %d cycles\n", (tdrk ? 'B' : 'A'), (delay ? 3 : 2)); }