      subroutine filladc        !??? AGGIUSTARE TUTTO


      include '../common/commontracker.f'
      include '../common/level0.f'


      external nvk
      external nst

      real errflag

      integer*2 flag,tipo,info,prec_ind,word

      integer*2 newVAL,oldVAL
      data oldval/0/

      integer DSPn

c      real tempadc(nstrips_view) !??? per inversione vista 11


c     controllo DAQmode per capire se i dati sono compressi (=2) o full (=1)
c     se sono sia full che compressi (=3) prima vengono riempite le variabili
c     adc con la versione compressa, poi con quella full, che quindi e'
c     quella che viene poi effettivamente usata

      idata=0                   !indice dell'array datatracker!???

      do iv=1,nviews

         DSPn=DSPnumber(iv)

         
         if(iand(DAQmode(DSPn),z'0002').eq.z'0002') then ! compressed mode

            is = 0
            il = 0 
            prec_ind = 0
 222        continue
            idata = idata+1
            word=datatracker(idata)

            call compdecode(word,flag,tipo,info,errflag) !routine per scomprimere

            if(errflag.ne.0.) then
               print*,'ERROR on compdecode'
               return
            endif
            if(flag.eq.1) then  !   flag: fine messaggio
               if(info.ne.1.and.info.ne.2.and.info.ne.3) then 
                  print*,'ERROR on end of ladder',info,tipo
               endif
               il = info
               do js=is+1,1024*il
                  newVAL=oldVAL-pedestal_t(DSPn,nvk(is),nst(is))
     $                 +pedestal_t(DSPn,nvk(js),nst(js))
                  newVAL=max(0,newVAL)
                  newVAL=min(4095,newVAL)


c     print*,'comp1',nev0,iv,jss,idata !???
                  adc(DSPn,nvk(js),nst(js))=newVAL 

               enddo

               if(info.eq.3) goto 1000
               is=1024*il
               prec_ind=0       !il precedente non e' un indirizzo
            endif
            
            if(flag.eq.0) then  !  flag: dato o indirizzo
               if(tipo.eq.1) then ! tipo: indirizzo
                  iaddr = info + il*1024
                  if(iaddr.ge.is+1.and.iaddr.le.3072) then
                     do js = is+1,iaddr-1
                        newVAL=oldVAL-pedestal_t(DSPn,nvk(is),nst(is))
     $                       +pedestal_t(DSPn,nvk(js),nst(js))
                        newVAL=max(0,newVAL)
                        newVAL=min(4095,newVAL)



c     print*,'comp2',nev0,iv,jss,idata !???
                        adc(DSPn,nvk(js),nst(js))=newVAL

                     enddo

                     is = iaddr
                     prec_ind = 1
                  else
                     print*,'ERROR address 
     $                    out of range - iaddr,is',iaddr,is
                  endif
               endif
               if(tipo.eq.0) then ! tipo: dato   
                  if(prec_ind.eq.0) is=is+1
                  if(info.ge.0.and.info.le.4095) then
                     if(is.gt.3072) 
     $                    print*,
     $                    'ERROR strip out of range - DSPn,is',DSPn,is
                     newVAL=info

c     print*,'comp3',nev0,iv,iss,idata !???
                     adc(DSPn,nvk(is),nst(is))=newVAL

                     oldVAL=newVAL
                  else
                     print*,'ERROR datum out of range - info',info
                  endif
                  prec_ind=0
               endif
            endif
            goto 222
         endif

 1000    continue


c$$$  print*,' '
c$$$  do ik=1,nva1_view
c$$$  do is=1,nstrips_va1
c$$$  print*,iv,adc(iv,ik,is) !???
c$$$  enddo
c$$$  enddo
c$$$  


         if(iand(DAQmode(DSPn),z'0001').eq.z'0001') then ! full mode
            do i=1,3
               do j=1,1024
                  idata = idata+1
                  is=j+1024*(i-1)



c     print*,'full',nev0,iv,is,idata !???

                  adc(DSPn,nvk(is),nst(is)) = datatracker(idata)

c                  print*,iv,DSPn,nvk(is),nst(is),is,datatracker(idata) !???

               enddo
               idata = idata+1
               if(datatracker(idata).ne.ior(z'1800',i+3)) then
                  print*,'ERROR on ladder label end',datatracker(idata),
     $                 ior(z'1800',i+3)
               endif
            enddo
         endif



c$$$  print*,' '             !???
c$$$  do ik=1,nva1_view
c$$$  do is=1,nstrips_va1
c$$$  c               print*,iv,ik,is,adc(iv,ik,is),adc1(iv,ik,is) !???
c$$$  print*,iv,ik,is,adc(iv,ik,is),adc1(iv,ik,is)
c$$$  $              ,(adc(iv,ik,is)-adc1(iv,ik,is))/sigma_t !???
c$$$  enddo
c$$$  enddo


      enddo

c$$$C     
c$$$C     inversione vista 11 !???
c$$$C     
c$$$      do is=1,nstrips_view
c$$$         tempadc(is) = adc(11,nvk(is),nst(is))
c$$$      enddo
c$$$      
c$$$      
c$$$      do is=1,nstrips_view
c$$$         offset=5121
c$$$         if(is.le.2048) offset=3073
c$$$         if(is.le.1024) offset=1025
c$$$         iss=offset-is
c$$$
c$$$         adc(11,nvk(iss),nst(iss)) = tempadc(is)
c$$$      enddo
C     
C     fine inversione vista 11 !???
C     
      
      
      return
      end



c     qui o nelle functions.f???
      
      SUBROUTINE COMPDECODE(word,flag,tipo,info,errflag)
      INTEGER*2 word,flag,tipo,info

      errflag=0.
      flag=iand(word,z'f000')
      flag=ishft(flag,-12)

      if(flag.ne.0.and.flag.ne.1) then
         print*,'error on decompression: flag=',flag
         errflag=1.
      endif
      if(flag.eq.0) then        ! valore ADC
         tipo=0
         info=iand(word,z'0fff')
      endif
      if(flag.eq.1) then        ! indirizzo OR fine vista
         info=iand(word,z'03ff')
         tipo=iand(word,z'0c00')
         if(tipo.ne.0.and.tipo.ne.z'0800') then
            print*,'error on decompression: tipo=',tipo
            errflag=1.
         endif
         if(tipo.eq.0) then     ! indirizzo
            flag=0
            tipo=1
            info=info+1
         endif
         if(tipo.eq.z'0800') then ! fine vista
            flag=1
            if(info.eq.3.or.info.eq.6) then 
               tipo=1
            else
               tipo=0
            endif
         endif
      endif
      return
      end
