      REAL FUNCTION decode()
      IMPLICIT NONE

*********************************************************
*           LeCroy CAMAC 2277 TDC                       *
*                                                       *
*********************************************************

      include ?
*
*--   Enter user code here
*
      INTEGER i
      INTEGER tdcdat,ch,nch(0:1), fch(0:1,0:15),edge
      INTEGER tdcfirst(0:1,0:15)
      INTEGER tdclast(0:1,0:15)

      
      call vzero(fch,2*16)
      call vzero(tdcfirst,2*16)
      call vzero(tdclast ,2*16)

      
      DO i=1,ntdc
         tdcdat=ibits(tdc(i),0,16)
         ch = ibits(tdc(i),17,5)
         edge = ibits (tdc(i),16,1)
c         PRINT*,tdcdat,ch,edge
         if (ch.le.16) then
            tdcfirst(edge,ch)=tdcdat
            if (tdclast(edge,ch).eq.0) then
               tdclast(edge,ch)=tdcdat
            endif  
            fch(edge,ch)=fch(edge,ch)+1
            call hf1(100+edge,float(tdcdat),1.0)
            call hf1(200+ch+edge*20,float(tdcdat),1.0)
         endif
         
      ENDDO
      call vzero(nch,2)
      do ch=0,15
        do edge=0,1
           call hf1(102+edge,float(tdcfirst(edge,ch)),1.0)
           call hf1(104+edge,float(tdclast(edge,ch)),1.0)
           call hf1(110+ch+edge*20,float(fch(edge,ch)),1.0)
           if ( fch(edge,ch).gt.0) then 
              nch(edge)=nch(edge)+1
           endif
        enddo
      enddo
      
      
      call hf1(300,float(nch(0)),1.0)
      call hf1(301,float(nch(1)),1.0)
      
      decode = 1.
*
      END

















