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

PROC Dec3(CHAN OF BUNDLE FromDec1D,ToDec1A,ToCtrl3A,ToCtrl3B,FromCtrl3A,
          CHAN OF DEB.MESSAGE screen, VAL INT report,evaluate )

  CHAN OF BUNDLE ToRegA1,ToRegA2,ToRegB1, ToRegB2, FromRegBA:
  
  --------------------------------------------------
  PROC  Dec3ctrl(CHAN OF BUNDLE FromDec1D,ToRegA1,ToRegA2,CHAN OF DEB.MESSAGE screen, VAL INT report )


    [MESGLEN]INT ALfs,C3,IN3:

    INT AluIn,AluOut, clock:

    [59][12]INT AluCmatch:
    [59][12]INT AluCmask:
    [59][27]INT AluCout:

    BOOL end:

    [12]BOOL AluInb:
    [32]BOOL IN3b:
    [10]BOOL ALfsb:
    [32]BOOL AluOutb:
    [32]BOOL C3b:
    ------------------------------------------------
    PROC AluC(INT In, Out)
      SEQ
   
        AluCmatch:=CAluCmatch
        AluCmask:=CAluCmask
        AluCout:=CAluCout
        decode(AluCout, AluCmatch, AluCmask, In, Out) 
    :
    -------------------------------------------------

    PROC mkaluIn() 
      SEQ
        [AluInb FROM 0 FOR 5]:=[IN3b FROM 3 FOR 5]
        [AluInb FROM 5 FOR 7]:=[IN3b FROM 10 FOR  7]
        init.hex(AluIn,AluInb)
    :
    ---------------------------------------------------
    PROC mkAlfs()
      SEQ     
        [ALfsb FROM 0 FOR 10]:=[AluOutb FROM 17 FOR 10]
        init.hex(ALfs[0], ALfsb)
    :
    ---------------------------------------------------
    PROC mkC3()
      SEQ
        [C3b FROM 0 FOR 3]:=[AluOutb FROM 0 FOR 3]
        C3b[3]:=IN3b[0]
        [C3b FROM 4 FOR 8] := [AluOutb FROM 3 FOR 8]
        [C3b FROM 12 FOR 2]:=[IN3b FROM 1 FOR 2]
        [C3b FROM 14 FOR 5]:=[AluOutb FROM 11 FOR 5]
        [C3b FROM 19 FOR 3]:=[IN3b FROM 8 FOR 3]
        C3b[22]:=AluOutb[16]
        [C3b FROM 23 FOR 3]:=[IN3b FROM 17 FOR 3]  --vect

        init.hex(C3[0], C3b )
    :
    ----------------------------------------------------
    SEQ
      debugmsg(report,screen, "DECODE3-DEC3: waiting for In3 from DEC1", NULL1)
      FromDec1D?IN3
      debugmsg(report,screen, "DECODE3-DEC3: Received In3 from DEC1",IN3)

      IF 
        IN3[0]=FINISH
          end:=TRUE
        TRUE
          SKIP 

      WHILE end=FALSE
        SEQ
          clock:=IN3[TIME.STAMP]
          int.to.bool(IN3[0], IN3b)
          mkaluIn() 
          AluC(AluIn, AluOut)
          int.to.bool(AluOut,AluOutb)
          debugmsg(report,screen, "DECODE3-DEC3: AluC output is ", [AluOut,0,0,0])

          mkC3()
          mkAlfs()
          debugmsg(report,screen, "DECODE3-DEC3: Waiting to send to Ctrl3 ",IN3)
          ALfs[TIME.STAMP]:=clock+ALUPLA.DELAY
          ALfs[TIME.IN]:=IN3[TIME.IN]
          ALfs[STALL]:=IN3[STALL]

          PAR
            ToRegA1!ALfs
            ToRegA2!C3
          debugmsg(report,screen, "DECODE3-DEC3: sent to Ctrl3 ALfs ",ALfs)
          debugmsg(report,screen, "DECODE3-DEC3: sent to Ctrl3  C3",C3)

          debugmsg(report,screen, "DECODE3-DEC3: waiting for In3 from DEC1", NULL1)
          FromDec1D?IN3
          debugmsg(report,screen, "DECODE3-DEC3: Received In3 from DEC1",IN3)

          IF 
            IN3[0]=FINISH
              end:=TRUE
            TRUE
              SKIP 
      -----------------------------------
      ToRegA1!IN3  --kill registers
      
  :
  ----------------------------------------------------------------
  PROC RegA(CHAN OF BUNDLE D1in, D2in,D1out, D2out, Ain, Aout)
    SEQ
      two.input.stage (D1in, D2in,D1out, D2out, Ain, Aout)
  :
  -----------------------------------------------------------------
  PROC RegB(CHAN OF BUNDLE D1in, D2in,D1out, D2out, Ain, Aout)
    SEQ
      last.two.input.stage (D1in, D2in,D1out, D2out, Ain, Aout)
  :
  ------------------------------------------------------------------
  SEQ
    PAR
      Dec3ctrl(FromDec1D,ToRegA1,ToRegA2, screen, report)

      RegA(ToRegA1,ToRegA2, ToRegB1, ToRegB2, ToDec1A, FromRegBA)

      RegB(ToRegB1, ToRegB2,ToCtrl3A,ToCtrl3B,FromRegBA, FromCtrl3A)
:


