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


PROC Ctrl2(CHAN OF BUNDLE FromRegA,FromRegB,FromApipe,FromDatInt,
                           FromDec2,FromNGen,FromCPsrA,FromOplat,FromImm,
                           ToApipe,ToDatIntBw,ToDatIntMb,ToDec2,ToCPsr,ToOplatB,
                           ToOplatA,ToNGen,ToDec2a,ToReg,ToImm,
                           FromDec2A,Ctrl3Alx ,[]CHAN OF DEB.MESSAGE screen, VAL INT report)--6
  ------------------------------------------------------------------------
  [MESGLEN]INT  D2,Da,Db,MLa,MLa1,MLb,MLb1,AckAp,BObW,AckDI,Rs,AckD2,
        Imm,Sh,Cp,Ng,AckCp,OPdata,AckNg,Acktd2,Ackop,
        Ackrb,AckImm, ALx:
  BOOL  end, DObw, first:
  INT SHop,cin,cout,sel, mla,DObwi,temp:
  [32]BOOL D2b : 
  [8]BOOL  C2b:  
  [10]BOOL SHopb :  
  [7]BOOL Imdb :
  [8]BOOL Cpb:
  -------------------------------------------------------------------------

  PROC  MUltiplier(VAL INT Da, Db, INT Psm, Pca)
    INT sum, carry:
    SEQ
      sum:=((Da)*(Db))                    
      carry:=(sum>>1)
      sum:=sum-carry
      Psm:=sum
      Pca:=(~carry)
  :
  ---------------------------------------
  
  PROC SHifter(VAL BOOL sel, VAL INT Db, Imm,op,cin, INT Sh, cout,CHAN OF DEB.MESSAGE screen )

    INT b0,b31,uval,val,n,m,carry,switch:

    SEQ 

       

      IF
        sel=TRUE
          val:=(Db)
        sel=FALSE
          val:=(Imm)

      b0:= (val/\1)
      b31:=((val>>31)/\1)
      uval:=val
      -------------------------------------------------------------------- 
      --uval, val::: In the C implementation, val is signed while uval 
      -----is unsigned. The difference is that the right shift of  the unsigned
      -----guarantees that the holes will be filled with 0's; right shift of
      -----a signed could fill the holes with 0's (logical shift) or with
      -----the sign bit(arithmetic shift). Occam fills holes with 0's in all cases.
      -----*************THIS MUST BE CHECKED AGAIN**************************
      ------------------------------------------------------------------------- 


      n:=(op/\ #1F)
      --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER--- n", n )

      m:=((n-1) /\ #1F)
      --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER--- m ", m )
      carry:=((val>>m)/\1)               --carry for right shift
      --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER--- carry:=((val>>m)/\1)", carry ) 

      ----------------------------------------------------------
      switch:=((op>>5)/\3)
      CASE switch
        0                                    --LSL                
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:=((op>>5)/\3) ", 0 )

            IF
              n=0
                carry:=cin
              TRUE
                carry:=((val>>(32-n))/\1)
            val:=val<<n
        1                                     --LSR                               
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:=((op>>5)/\3) ", 1 )
            IF
              n=0
                val:=0
              TRUE
                val:=(val>>n)
        2                                      --ASR
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:=((op>>5)/\3) ", 2 )
            IF
              n=0
                val:=(val>>31)
              TRUE
                val:=(val>>n)
        3                                     --ROR
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:=((op>>5)/\3) ", 3 )
            IF
              n=0
                SEQ
                  carry:=b0
                  val:=((cin<<31)\/(uval>>1))
              TRUE
                SEQ
                  val:=((uval>>n)\/(val<<(32-n)))
      ----------------------------------------------------------
      switch:= ((op>>7) /\7)
      CASE switch
        6
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:= ((op>>7) /\7) ", 6 )
            SKIP              --carry as above
        7 
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:= ((op>>7) /\7) ", 7 )
            carry:=0
        5
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:= ((op>>7) /\7) ", 5 )
            carry:=b0
        3
          SEQ
            --debugmsg(report,screen, "DECODE2-CTRL2: SHIFTER---switch:= ((op>>7) /\7) ", 3 )
            carry:=b31
      --------------------------------------------------------
      Sh:=val
      cout:=carry

  :  

  --------------------------------------
  PROC  mkcpsr()
    BOOL temp:
    SEQ
      [Cpb FROM 0 FOR 7]:=Imdb
      IF
        cout=1
          SEQ
            temp:=TRUE
        cout=0
          SEQ
            temp:=FALSE
      Cpb[7]:=temp
      init.hex(Cp[0], Cpb)
  :
  --------------------------------------
  PROC mkopdata()
    INT temp:
    SEQ
      temp:= (Ng[0]/\#3F)  -- 111111  (Ng[5:0])
      init.hex(sel, [C2b[2]]) 
      --debugmsg(report,screen[4], "DECODE2-CTRL2: SELECTING 1=NG ", sel )     
      mux2(sel,MLa[0],temp , OPdata[0])
  :
  ---------------------------------------

  SEQ
    first:=TRUE
    cout:=0
    ----------------------------------------------------------------
    SEQ
      debugmsg(report,screen[0], "DECODE2-CTRL2: Waiting for data from Dec2 ", 0 )
      FromDec2?D2
      debugmsg(report,screen[0], "DECODE2-CTRL2: Received for data from Dec2 ", D2[0] )

      IF
        D2[0]<>FINISH  -- D2 = (c2[7:0],DObw,SHop[9:0],Imd[6:0])  --from Dec2
          SEQ
            debugmsg(report,screen[0], "DECODE2-CTRL2: Waiting for data from Reg Bank ", 0 )
            PAR
              FromRegA? Da    --from register bank
              FromRegB? Db
            debugmsg(report,screen[0], "DECODE2-CTRL2: Received data from Regbank ", Da[0])
            debugmsg(report,screen[0], "DECODE2-CTRL2: Received data from Regbank ", Db[0] )
        TRUE
          end:=TRUE
    ----------------------------------------------------------------
    WHILE end=FALSE
      SEQ
        MLa[0]:=Da[0]
        MLa1[0]:=Da[0]
        MLb:=Db
        MLb1:=Db
        Sh[0]:=0    --THIS IS THE OUTPUT OF THE SHIFTER IF THE SHIFTER IS NOT ACTIVATED
        int.to.bool(D2[0], D2b)
        C2b:=[D2b FROM 0 FOR 8]  --initialize C2b
        DObw:=D2b[8]
        SHopb:=[D2b FROM 9 FOR 10]
        Imdb:=[D2b FROM 19 FOR 7]

        ------------------------------------------------------------
        PAR  --1
          ----------------------------------------------------------
          --------------------------------------------------------
          IF
            C2b[3]=FALSE                   --To/From Apipe(AddInt)
              SEQ
                debugmsg(report,screen[0], "DECODE2-CTRL2: Waiting to send to Apipe ", 0 )
                ToApipe!Da
                FromApipe? AckAp
                debugmsg(report,screen[0], "DECODE2-CTRL2: sent to Apipe ", Da[0] )

            TRUE
              SKIP
          -------------------------------------------------------
          IF          
            C2b[6]=FALSE                  --To/From Dec2
              SEQ
                Rs[0]:=Da[0]/\#000000FF   --Rs=Da[7:0]
                debugmsg(report,screen[1], "DECODE2-CTRL2: Waiting to send to Dec2 ", 0 )
                ToDec2!Rs
                FromDec2A? AckD2
                debugmsg(report,screen[1], "DECODE2-CTRL2: sent to Dec2 ", Rs[0] )

            TRUE
              SKIP

          --------------------------------------------------------
          IF
            C2b[4]=FALSE                 --To/From Data Interface 
              SEQ
                --DObwi:=INT DObw
                init.hex(BObW[0], [DObw])
                IF
                  (((C2b[0]=TRUE) AND (C2b[7]=TRUE)) AND (C2b[1]=FALSE))
                    SEQ
                      MUltiplier(MLa1[0], Db[0], mla, MLb1[0]) -- mla not used
                  TRUE
                    SKIP
                debugmsg(report,screen[2], "DECODE2-CTRL2: Waiting to send to DatInt", 0 )
                PAR                                    
                  ToDatIntBw!BObW
                  ToDatIntMb!MLb1
                FromDatInt? AckDI
                debugmsg(report,screen[2], "DECODE2-CTRL2: sent to DatInt, MLb1 ", MLb1[0] )

            TRUE
              SKIP

                            
          ---------------------------------------------------------
          SEQ  --1.2
            -------------------------------------------------------
            IF
              C2b[0]=TRUE
                SEQ
                  debugmsg(report,screen[3], "DECODE2-CTRL2: C2b is  TRUE", 0 )
                  ---------------------------------------------------
                  IF
                    C2b[7]=FALSE
                      SKIP
                    C2b[7]=TRUE
                      SEQ
                        PAR
                          ----------------------------------------
                          SEQ
                            -------------------------------------
                            IF
                              C2b[1]=FALSE
                                SEQ
                                  temp:=MLa[0]
                                  MUltiplier(temp, Db[0], MLa[0], MLb[0])
                                  
                              TRUE
                                SKIP
                             
                            init.hex(SHop, SHopb)
                            IF
                              first=TRUE
                                SEQ
                                  first:=FALSE
                                  cin:=0           --initial value of the carry
                              first=FALSE
                                SEQ
                                  debugmsg(report,screen[3], "DECODE2-CTRL2: Waiting for ALx", 0 )
                                  Ctrl3Alx? ALx
                                  debugmsg(report,screen[3], "DECODE2-CTRL2: Received ALx", ALx[0] )
                                  cin:=ALx[0] 

                            debugmsg(report,screen[3], "DECODE2-CTRL2: C2b[7] TRUE--MLb[0] is ", MLb[0] ) 
                            debugmsg(report,screen[3], "DECODE2-CTRL2: C2b[7] TRUE--Imm[0] is ", Imm[0] ) 

                            debugmsg(report,screen[3], "DECODE2-CTRL2: C2b[7] TRUE--SHop is ",  SHop)  

                            debugmsg(report,screen[3], "DECODE2-CTRL2: C2b[7] TRUE--cin is ",  cin) 
 

                            SHifter(C2b[0],MLb[0],Imm[0], SHop, cin, Sh[0], cout, screen[3])
                            debugmsg(report,screen[3], "DECODE2-CTRL2: C2b[7] TRUE ", 0 )

                             ----------------------------------------

                          SEQ
                             -----------------------------------------
                            IF
                              C2b[2]=TRUE
                                SEQ
                                  debugmsg(report,screen[4], "DECODE2-CTRL2: Waiting for Ng", 0 )
                                  FromNGen? Ng
                                  debugmsg(report,screen[4], "DECODE2-CTRL2: Received Ng", Ng[0] )

                              TRUE
                                SKIP
                             
              ----------------------------------------------------------
              C2b[0]=FALSE
                SEQ
                  

                  ----------------------------------------------------
                  PAR
                    ------------------------------------------------
                    SEQ
                      ----------------------------------------------
                      PAR
                        SEQ
                          debugmsg(report,screen[3], "DECODE2-CTRL2: Waiting from IMMpipe", 0 )
                          FromImm? Imm
                          debugmsg(report,screen[3], "DECODE2-CTRL2: Received from IMMpipe", Imm[0] )                        
                        init.hex(SHop, SHopb)
                        IF
                          first=TRUE
                            SEQ
                              first:=FALSE
                              cin:=0            --initial value of the carry
                          first=FALSE
                            SEQ
                              --debugmsg(report,screen[4], "DECODE2-CTRL2: Waiting for ALx", 0 )
                              Ctrl3Alx? ALx
                              --debugmsg(report,screen[4], "DECODE2-CTRL2: Received ALx", 0 )
                              cin:=ALx[0]  
                      debugmsg(report,screen[4], "DECODE2-CTRL2: C2b[7] FALSE--MLb[0] is ", MLb[0] ) 
                      debugmsg(report,screen[4], "DECODE2-CTRL2: C2b[7] FALSE--Imm[0] is ", Imm[0] ) 
                      debugmsg(report,screen[4], "DECODE2-CTRL2: C2b[7] FALSE--cin is ",  cin)
                      debugmsg(report,screen[4], "DECODE2-CTRL2: C2b[7] FALSE--SHop is ",  SHop)                      
                      SHifter(C2b[0],MLb[0],Imm[0], SHop, cin, Sh[0], cout,screen[4])

                    SEQ
                      -----------------------------------------------
                      IF
                        C2b[2]=TRUE
                          SEQ
                            --debugmsg(report,screen[5], "DECODE2-CTRL2: Waiting for Ng", 0 )
                            FromNGen? Ng
                            --debugmsg(report,screen[5], "DECODE2-CTRL2: Received Ng", 0 )
                        TRUE
                          SKIP
            ----------------------------------------------------------
            SEQ
              PAR
                ------------------------------------------------------
                SEQ
                  IF
                    C2b[5]=TRUE
                      SEQ
                        debugmsg(report,screen[5], "DECODE2-CTRL2: waiting to send cpsr ", Cp[0] )
                        mkcpsr()
                        ToCPsr!Cp
                        FromCPsrA? AckCp
                        debugmsg(report,screen[5], "DECODE2-CTRL2: sent cpsr ", Cp[0] )

                    TRUE
                      SEQ
                        debugmsg(report,screen[5], "DECODE2-CTRL2: not sending cpsr.... ", Cp[0] )
                        SKIP
                -----------------------------------------------------
                SEQ
                  mkopdata()
                  debugmsg(report,screen[4], "DECODE2-CTRL2: Waiting to send to OPreg Sh", Sh[0])
                  debugmsg(report,screen[4], "DECODE2-CTRL2: Waiting to send to OPreg OPdata",OPdata[0])
                  PAR
                    ToOplatB!Sh
                    ToOplatA!OPdata   --from multiplexer
         
                  FromOplat? Ackop
                  debugmsg(report,screen[4], "DECODE2-CTRL2: sent to OPreg ",Sh[0] )
                  debugmsg(report,screen[4], "DECODE2-CTRL2: sent to OPreg ", OPdata[0])

            ----------------------------------------------------------

        PAR
          ------------------------------------------------------------
          SEQ
            IF
              C2b[2]=TRUE
                SEQ
                  ToNGen!AckNg
              TRUE
                SKIP
          -------------------------------------------------------------
          ToDec2a!Acktd2
          ToReg!Ackrb
          -------------------------------------------------------------
          --SEQ
           -- IF
             -- C2b[1]=FALSE
               -- SEQ
                --  TOMLa!Mla
             -- TRUE
               -- SKIP
          -----------------------------------------------------------
          SEQ
            IF
              C2b[0]=FALSE
                SEQ
                  ToImm!AckImm
              TRUE
                SKIP
          ---------------------------------------------------------
        -----------------------------------------------------------
       
        ----------------------------------------------------------------
        SEQ
          debugmsg(report,screen[0], "DECODE2-CTRL2: Waiting for data from Dec2 ", 0 )
          FromDec2?D2
          debugmsg(report,screen[0], "DECODE2-CTRL2: Received data from Dec2 ", D2[0] )

          IF
            D2[0]<>FINISH  -- D2 = (c2[7:0],DObw,SHop[9:0],Imd[6:0]) --from Dec2
              SEQ
                debugmsg(report,screen[0], "DECODE2-CTRL2: Waiting for data from RegBank ", 0 )

                PAR
                  FromRegA? Da    --from register bank
                  FromRegB? Db
                debugmsg(report,screen[0], "DECODE2-CTRL2: Received data from RegBank ", Da[0] )
                debugmsg(report,screen[0], "DECODE2-CTRL2: Received data from RegBank ", Db[0] )
            TRUE
              end:=TRUE
    ----------------------------------------------------------------
:


