00001 #define PP printf(
00002 #include <stdio.h>
00003 #include <unistd.h>
00004 #include <assert.h>
00005 #include <string.h>
00006 #include <arpa/inet.h>
00007
00008 #include "daqFormats.h"
00009 #include "rtsSystems.h"
00010
00011
00012 #include "evpSupport.h"
00013 #include "ssdReader.h"
00014 using namespace OLDEVP;
00015 namespace OLDEVP {
00016 struct ssd_t ssd ;
00017 }
00018
00019 static int unpackRaw(int rb, int mz, int what, char *mem) ;
00020
00021 ssd_t::ssd_t()
00022 {
00023 memset(this,0,sizeof(ssd_t));
00024 fenceA=1946;
00025 fenceZ=1946;
00026
00027 max_channels = 20*16*2*768 ;
00028 }
00029
00030 void ssd_t::reset()
00031 {
00032
00033 ssd.channels = 0 ;
00034 }
00035
00036 int ssd_t::check()
00037 {
00038 assert(fenceA==1946);
00039 assert(fenceZ==1946);
00040 return 0;
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 int OLDEVP::ssdReader(char *m)
00056 {
00057 int sec ;
00058 int rb, mz ;
00059 int len ;
00060 u_int off ;
00061 static int init=0 ;
00062
00063 struct TPCP *ssdp ;
00064 struct TPCSECP *secp ;
00065 struct TPCRBP *rbp ;
00066 struct TPCMZP *mzp ;
00067 struct TPCSEQD *seqd ;
00068 struct TPCADCD *adcd ;
00069 struct TPCPADK *padk ;
00070 struct TPCADCR_l *adcr ;
00071 struct TPCCPPR_l *cppr ;
00072 struct TPCPEDR *pedr ;
00073 struct TPCRMSR *rmsr ;
00074 struct DATAP *datap ;
00075 struct DATAPX *datapx ;
00076
00077
00078 ssd.check();
00079
00080 ssd.reset() ;
00081
00082
00083 if(m == NULL) return EVP_DATA_ERR ;
00084
00085 datap = (struct DATAP *)m ;
00086
00087
00088 len = l2h32(datap->det[EXT_ID].len) ;
00089 if(len == 0) return EVP_NO_DET ;
00090 assert(len>0&&len<99999);
00091
00092 off = l2h32(datap->det[EXT_ID].off) ;
00093 if(off == 0) return EVP_NO_DET ;
00094
00095 datapx = (struct DATAPX *)((u_int *)m + off) ;
00096
00097
00098 if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) assert(0);
00099
00100
00101 len = l2h32(datapx->det[SSD_ID-10].len) * 4 ;
00102 if(len == 0) return EVP_NO_DET ;
00103
00104
00105 if(datapx->bh.byte_order==0x01020304) off = b2h32(datapx->det[SSD_ID-10].off) ;
00106 else if(datapx->bh.byte_order==0x04030201) off = l2h32(datapx->det[SSD_ID-10].off) ;
00107 else assert(0);
00108 if(off == 0) return EVP_NO_DET ;
00109
00110
00111 ssdp = (struct TPCP *)((u_int *)datapx + off) ;
00112 if(checkBank((char *)ssdp,"SSDP") < 0) assert(0);
00113
00114
00115 if(!init) {
00116
00117 ssd.mode = 1 ;
00118
00119 init = 1 ;
00120 }
00121
00122
00123 memset(ssd.raw,0,sizeof(ssd.raw)) ;
00124
00125
00126 memset((char *)ssd.counts,0,sizeof(ssd.counts)) ;
00127
00128
00129 ssd.mode = 0 ;
00130
00131
00132 for(sec=0;sec<1;sec++) {
00133 int last, herb ;
00134
00135
00136 if(ssdp->sb[sec].len == 0) continue ;
00137
00138 { int herb=b2h32(ssdp->sb[sec].off); assert(herb>0&&herb<99999); }
00139 secp = (struct TPCSECP *) ((u_int *)ssdp + b2h32(ssdp->sb[sec].off)) ;
00140
00141 if(checkBank((char *)secp,"SSDSECP") < 0) assert(0);
00142
00143 last = 4 ;
00144
00145 for(rb=0;rb<last;rb++) {
00146
00147
00148 if(secp->rb[rb].len == 0) continue ;
00149
00150 herb=b2h32(secp->rb[rb].off); assert(herb>0&&herb<999999);
00151 rbp = (struct TPCRBP *) ((u_int *)secp + herb ) ;
00152 if(checkBank((char *)rbp,"SSDRBP") < 0) assert(0);
00153
00154
00155
00156 for(mz=0;mz<2;mz++) {
00157 if(rbp->mz[mz].len == 0) continue ;
00158
00159 { int herb=l2h32(rbp->mz[mz].off); assert(herb>0&&herb<99999); }
00160 mzp = (struct TPCMZP *)((u_int *)rbp + l2h32(rbp->mz[mz].off)) ;
00161 if(checkBank((char *)mzp,"SSDMZP") < 0) assert(0);
00162
00163
00164 if((mzp->banks[TPC_ADCD].len != 0) && (ssd.mode==0)) {
00165
00166
00167 { int herb=l2h32(mzp->banks[TPC_SEQD].off); assert(herb>0&&herb<99999); }
00168 seqd = (struct TPCSEQD *)((u_int *)mzp + l2h32(mzp->banks[TPC_SEQD].off)) ;
00169 { int herb=l2h32(mzp->banks[TPC_ADCD].off); assert(herb>0&&herb<99999); }
00170 adcd = (struct TPCADCD *)((u_int *)mzp + l2h32(mzp->banks[TPC_ADCD].off)) ;
00171
00172 if(checkBank((char *)seqd,"SSDSEQD") < 0) assert(0);
00173 if(checkBank((char *)adcd,"SSDADCD") < 0) assert(0);
00174
00175 int len = l2h32(seqd->bh.length) - 10 ;
00176 assert(len<99999);
00177 len *= 2 ;
00178
00179 int adccou = 0 ;
00180 int jj ;
00181
00182 int rr, pp ;
00183
00184 rr = pp = 1 ;
00185
00186 for(jj=0;jj<len;jj++) {
00187 int start, last, length, stop ;
00188 u_short ss, f8 ;
00189 int tbin ;
00190
00191 ss = l2h16(seqd->seq[jj]) ;
00192 f8 = ss & 0x8000 ;
00193
00194
00195 if(f8) {
00196 pp = (ss & 0x7FFF) % 256 ;
00197 rr = (ss & 0x7FFF) / 256 ;
00198
00199 if(pp == 0xff) break ;
00200 }
00201 else {
00202 last = ss & 0x0020 ;
00203 length = ss & 0x1F ;
00204 start = (ss & 0x7FC0) >> 6 ;
00205 stop = start + length ;
00206
00207 for(tbin=start;tbin<stop;tbin++) {
00208 u_char val ;
00209 int counter ;
00210
00211 val = adcd->adc[adccou++] ;
00212
00213 if(val==0) continue ;
00214
00215 counter = ssd.counts[rr-1][pp-1] ;
00216
00217 ssd.adc[rr-1][pp-1][counter] = val ;
00218 ssd.strip[rr-1][pp-1][counter] = tbin ;
00219 ssd.counts[rr-1][pp-1] += 1 ;
00220 ssd.channels++ ;
00221 }
00222
00223 if(last) pp++ ;
00224 }
00225
00226 }
00227
00228 continue ;
00229
00230
00231 }
00232
00233 padk = NULL ;
00234 if(mzp->banks[TPC_PADK].len != 0) {
00235 {
00236 int herb= l2h32(mzp->banks[TPC_PADK].off);
00237
00238 assert(herb>0&&herb<99999);
00239 }
00240 padk = (struct TPCPADK *)((u_int *)mzp + l2h32(mzp->banks[TPC_PADK].off)) ;
00241
00242 if(checkBank((char *)padk,"SSDPADK") < 0) assert(0);
00243
00244 }
00245
00246 cppr = NULL ;
00247 if(mzp->banks[TPC_CPPR].len != 0) {
00248
00249 cppr = (struct TPCCPPR_l *)((u_int *)mzp + l2h32(mzp->banks[TPC_CPPR].off)) ;
00250 if(checkBank((char *)cppr,"SSDCPPR") < 0) assert(0);
00251 }
00252
00253 if((mzp->banks[TPC_ADCR].len != 0) && (ssd.mode==0)) {
00254 adcr = (struct TPCADCR_l *)((u_int *)mzp + l2h32(mzp->banks[TPC_ADCR].off)) ;
00255
00256 if(checkBank((char *)adcr,"SSDADCR") < 0) assert(0);
00257
00258 if(unpackRaw(rb, mz, 0, (char *)adcr) < 0) {
00259
00260
00261 }
00262
00263
00264
00265 continue ;
00266 }
00267
00268 if(mzp->banks[TPC_PEDR].len != 0) {
00269 pedr = (struct TPCPEDR *)((u_int *)mzp + l2h32(mzp->banks[TPC_PEDR].off)) ;
00270
00271 if(checkBank((char *)pedr,"SSDPEDR") < 0) assert(0);
00272
00273 unpackRaw(rb, mz, 1,(char *)pedr) ;
00274 ssd.mode = 1 ;
00275 }
00276
00277
00278 if(mzp->banks[TPC_RMSR].len != 0) {
00279 rmsr = (struct TPCRMSR *)((u_int *)mzp + l2h32(mzp->banks[TPC_RMSR].off)) ;
00280
00281 if(checkBank((char *)rmsr,"SSDRMSR") < 0) assert(0);
00282 unpackRaw(rb, mz, 2, (char *)rmsr) ;
00283 ssd.mode = 1 ;
00284 }
00285
00286 }
00287
00288 }
00289
00290 }
00291
00292
00293 ssd.check();
00294 return len ;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304 static int unpackRaw(int rb, int mz, int what, char *mem)
00305 {
00306
00307 u_char *adcdata ;
00308 int as, ch, strip, cou ;
00309 u_char *dta ;
00310 int row ;
00311
00312 if(mem == NULL) {
00313
00314 return -1 ;
00315 }
00316
00317 if(mz == 2) {
00318
00319 }
00320
00321 adcdata = NULL ;
00322
00323 switch(what) {
00324 case 0 :
00325 adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
00326 ssd.raw[rb][mz] = adcdata ;
00327
00328 break ;
00329 case 1 :
00330 adcdata = ((struct TPCPEDR *)mem)->ped ;
00331 ssd.raw[rb][mz] = adcdata ;
00332 for(as=0;as<5;as++) {
00333 row = rb*10+mz*5+as ;
00334 for(ch=0;ch<64;ch++) {
00335 dta = adcdata + (as*64*512) + (ch*512) ;
00336 memcpy(ssd.adc[row][ch],dta,192) ;
00337 }
00338 }
00339 ssd.channels += 5*64*192 ;
00340 return 0;
00341 case 2 :
00342 adcdata = ((struct TPCRMSR *)mem)->rms ;
00343 for(as=0;as<5;as++) {
00344 row = rb*10+mz*5+as ;
00345 for(ch=0;ch<64;ch++) {
00346 dta = adcdata + (as*64*512) + (ch*512) ;
00347 memcpy(ssd.strip[row][ch],dta,192) ;
00348 }
00349 }
00350 return 0 ;
00351 default :printf("WARN,unknown case, do nothing \n");
00352 break;
00353
00354
00355 }
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388 return 0;
00389 }