#INCLUDE "/home/amu5/users/georget/Phd/Simul/Defs/defs.inc"
#USE "/home/amu5/users/georget/Phd/Simul/library"

PROC RdGen(CHAN OF BUNDLE LSMP0,LSMP1,RdGenr,RdGA,RdGena1)

  [MESGLEN]INT IRd,Ntrmpc,Ack,Pout :
  BOOL  done, end ,Ntrm,Dpc:
  INT  mask,p, i,clock :
  VAL INT delay IS RDGEN.DELAY:
  BOOL first:

  SEQ
    RdGenr?IRd
    IF
      IRd[0]<>FINISH
        SEQ
          done:=FALSE
          mask:=#FFFF
      TRUE 
        end:=TRUE
    WHILE end=FALSE
      SEQ
        first:=TRUE
        WHILE done=FALSE
          SEQ
            IF
              first=TRUE
                SEQ
                  clock:=IRd[TIME.STAMP]
              TRUE
                SEQ
                  clock:=Ack[TIME.STAMP] 
            p:=15
            i:=15
            WHILE i > (-1)
              SEQ
                IF
                  (((mask /\ IRd[0])/\(1<<i))>>i) <> 0
                    SEQ
                      p:=i
                  TRUE
                    SKIP
                i:=i-1

            mask:=(mask /\(~(1<<p)))
            IF
              ((mask/\IRd[0])=0)
                SEQ
                  done:=TRUE
              TRUE
                SKIP
            PAR
              SEQ
                Pout[0]:=p
                Pout[TIME.STAMP]:=clock+delay                
                LSMP0!Pout
              SEQ
                Ntrm:=(NOT done)
                IF
                  (p=#F)
                    SEQ
                      Dpc:=TRUE
                  TRUE
                    Dpc:=FALSE
               
                init.hex(Ntrmpc[0], [Ntrm,Dpc])
                LSMP1!Ntrmpc
            first:=FALSE
            RdGena1? Ack
        Ack[TIME.STAMP]:=clock+delay        
        RdGA!Ack
        RdGenr? IRd
        IF
          IRd[0]<>FINISH
            SEQ
              done:=FALSE
              
              mask:=#FFFF
          TRUE
            end:=TRUE
:
