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

PROC WrtCtrl(CHAN OF BUNDLE FRReg1,FRReg2, FAddIntA,FAdInWlx,
                             FRegBA,FDatInta,FDatIntb,ToAddInta,
                             ToAddIntb,ToRegBa,ToRegBb,ToRRegA,ToDatIntA,
             []CHAN OF DEB.MESSAGE screen,VAL INT report,evaluate,preemption )  --3


  CHAN OF BUNDLE  FWrtC2,ToWrtC2a,ToWrtC2b,FWRegA,ToWreg1,ToWreg2,FWrtC:
  ---------------------------------------
  PROC Wlx.buffer(CHAN OF BUNDLE In, Out)
    SEQ
      buffer(In, Out)
  :
  -----------------------------------------------
  PROC WReg(CHAN OF BUNDLE Din1, Din2, Dout1,Dout2, Ain, Aout)
    SEQ
      two.input.stage(Din1, Din2, Dout1,Dout2, Ain, Aout)
  :
  ----------------------------------------------
  PROC WrtCtrl1(CHAN OF BUNDLE FRReg1,FRReg2,FWrtC2,ToWrtC2a,ToWrtC2b,
                 CHAN OF DEB.MESSAGE screen, VAL INT report,evaluate)


    [MESGLEN]INT WR, WRq,Wlxok:
    [32]BOOL WRqb:
    BOOL  end,first:
    INT clock:
    [5]INT delay:
    -----------------------------------------------

    PROC calculate.delays()
      SEQ
        delay[0]:=SELECT
        delay[1]:=MULLERC
        delay[2]:=XOR
    :
    -------------------------------------------------------------------------

    SEQ
      calculate.delays()
      Wlxok[TIME.STAMP] :=0  --initially
      debugmsg(report,screen, "WRTCTRL-WRTCTRL1: Waiting for data from Ctrl3 ",NULL1)

      FRReg1?WR                 --from rlatch data...

      debugmsg(report,screen, "WRTCTRL-WRTCTRL1:  Received data from Ctrl3 ",WR)
      IF
        WR[0]<>FINISH
          SEQ
            FRReg2? WRq   --...and control
        TRUE
          SEQ
            end:=TRUE
      first:=TRUE
      WHILE end=FALSE
        SEQ
          clock:=WR[TIME.STAMP] +delay[0]   --SELECT
          int.to.bool(WRq[0], WRqb)
          ------------------------------------------------------
          IF
            WRqb[10]=TRUE
              SEQ
                IF
                  first=TRUE
                    SEQ
                      first:=FALSE
                  first=FALSE
                    SEQ
                      debugmsg(report,screen, "WRTCTRL-WRTCTRL1: Waiting for Wlxok",WR)
                      FWrtC2? Wlxok
                      debugmsg(report,screen, "WRTCTRL-WRTCTRL1: Received Wlxok",WR)
                      clock:=max(clock,Wlxok[TIME.STAMP])+delay[1] --MULLERC

            WRqb[10]=FALSE
              SKIP
          -----------------------------------------------------
          WR[TIME.STAMP]:=clock+delay[2]  --XOR
          debugmsg(report,screen, "WRTCTRL-WRTCTRL1: Waiting to send to wrtctrl2 ",WR)

          PAR
            ToWrtC2a!WR
            ToWrtC2b!WRq


          debugmsg(report,screen, "WRTCTRL-WRTCTRL1: waiting for new data from Ctrl3",NULL1)
          FRReg1?WR                 --from rlatch data...
          IF
            WR[0]<>FINISH
              SEQ
                FRReg2? WRq   --...and control
                debugmsg(report,screen, "WRTCTRL-WRTCTRL1: Received data from Ctrl3",WR)
            TRUE
              SEQ
                end:=TRUE
      -------------------------------------------
      ToWrtC2a!WR   --kill wrtctrl2



  :
  -------------------------------------------------------------------

  PROC WrtCtrl2(CHAN OF BUNDLE FAddIntA,FAdInWlx,FRegBA,FWrtC1a,
                                FWrtC1b,FDatInta,ToAddInta,ToAddIntb,
                                ToWrtC1,ToRegBa,ToRegBb,ToRRegA,ToDatIntA,
                                []CHAN OF DEB.MESSAGE screen, VAL INT report, evaluate, preemption)

    [MESGLEN]INT WRq,Ackad,WR,Wlx,Regc,Ackrb,Ackrr,DWq,DW,WR.occ,DW.occ,DWc,Ackdi,Wlx1:
    [32]BOOL WRqb,DWqb:
    BOOL  end,PcPar,found,lookahead.table.empty,WR.received:
    TIMER clock5:
    VAL INT delay IS 2343:   --0.5 sec
    [2][OCCT.SIZE][OCCT.COL]INT occupancy.table:  --an array of occupancy tables!!!!
    [15]INT delay:
    INT clock0, clock1,clock2, clock, time, msgnum,lkahead.table.top, lkahead.table.base,contents:
    INT previous.report, preempt.frequency, preempt.magnitude,accum.preempt, instruction,num.of.ldmregs:
    VAL INT process.id IS WRTCTRL.ID:
    VAL INT lkahead.table.size IS 10:
    VAL INT lat.lookahead IS 0:
    VAL INT num.of.regs IS 1:

    [lkahead.table.size][2]INT lookahead.table:  

    ------------------------------------------------------------------------
    PROC calculate.delays()
      SEQ
        delay[0]:=ARBITER
        delay[1]:=SELECT
        delay[2]:=CALLR
        delay[3]:=DESWAIT
        delay[4]:=CALLA+XOR
        delay[5]:=MULLERC
        delay[6]:=SELECT
        delay[7]:=CALLR
        delay[8]:=SELECT
        delay[9]:=CALLR
        delay[10]:=(CALLA+XOR)+XOR
        delay[11]:=XOR
    :
    --------------------------------------------------------------
    PROC  initialize.occup.tables()
      SEQ
        initialize.occup.table(occupancy.table[0], DINPIPE, DINPIPE.SIZE,DIN.DELAY)
        initialize.occup.table(occupancy.table[1], CTRL3RREG, CTRL3RREG.SIZE,REGISTER.DELAY)
    :
    --------------------------------------------------------------

    PROC Wgo()

      SEQ
        int.to.bool(WRq[0], WRqb)
        PcPar:=WRqb[0]
        --IF
        --found=FALSE
        --SEQ
        clock1:=clock+delay[1] --SELECT
        --found=TRUE
        --SEQ
        --clock1:=clock


        IF    --1
          WRqb[10]=TRUE
            SEQ
              --IF
              --found=FALSE
              --SEQ
              clock1:=clock1+delay[2]  --CALL
              --found=TRUE
              --SEQ
              --SKIP

              WR[TIME.STAMP]:=clock1
              WR[STALL]:=-1
          TRUE
            SKIP

        --IF
        --found=FALSE
        --SEQ
        clock2:=clock+delay[1] --SELECT
        --found=TRUE
        --SEQ
        clock2:=clock

        IF        --2
          WRqb[11]=TRUE
            SEQ
              --IF
              --found=FALSE
              --SEQ
              clock2:=clock2+delay[2]  --CALL
              --found=TRUE
              --SEQ
              --SKIP

              WR[TIME.STAMP]:=clock2
              WR[STALL]:=-1
          TRUE
            SKIP
        --****************************************

        PAR
          --------------------------------------------------
          SEQ
            IF    --1
              WRqb[10]=TRUE
                SEQ
                  PAR
                    ----------------------------
                    SEQ
                      debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: waiting to send to AddIntInt",WR)
                      PAR
                        ToAddInta!WR
                        ToAddIntb!WRq
                      FAddIntA? Ackad
                      debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: sent to AddIntInt",WR)

                    ----------------------------
                    SEQ
                      FAdInWlx? Wlx
                      Wlx[TIME.STAMP]:=Wlx[TIME.STAMP]+delay[3]  --DESWAIT
                      ToWrtC1!Wlx
                    -----------------------------
                  clock1:= Ackad[TIME.STAMP] +delay[4]                 --CALL+XOR

                  debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Wlx received from AddInt OK",WR)
              WRqb[10]=FALSE
                SEQ
                  clock1:=clock1+delay[3]  --XOR

                  SKIP
          --------------------------------------------------
          SEQ

            IF        --2
              WRqb[11]=TRUE
                SEQ

                  debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: waiting to send to Regbank",WR)
                  PAR
                    ToRegBa!WR
                    SEQ
                      init.hex(Regc[0], [ TRUE, WRqb[2]])  --Wmux  [Ma, valid]
                      ToRegBb!Regc
                  debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: waiting for ack from Regbank for ",WR)

                  FRegBA? Ackrb
                  debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: sent to Regbank",WR)
                  clock2:= Ackrb[TIME.STAMP] +delay[4]                 --CALL+XOR

              WRqb[11]=FALSE
                SEQ
                  clock2:=clock2+delay[3]  --XOR

                  SKIP
        Ackrr[TIME.STAMP]:=max(clock1,clock2)+delay[5]  --MULLERC
        clock:=Ackrr[TIME.STAMP]                           --save current clock
        calculate.occupancy(evaluate, occupancy.table[1], Ackrr[TIME.STAMP],WR.occ )

        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: waiting to send  Ack to RRegister",WR)
        ToRRegA!Ackrr
        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: sent  Ack to RRegister",WR)

    :
    -----------------------------------------------------
    PROC Dgo()
      SEQ
        int.to.bool(DWq[0], DWqb)
        clock:=clock+delay[6] --SELECT

        IF
          --------------------------------------------------
          DWqb[0]=FALSE              --DestPC
            SEQ
              debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo waiting to send to RegBank",DW)
              --IF
              --found=FALSE
              --SEQ

              DW[TIME.STAMP]:=clock+delay[7]  --CALL
              --found=TRUE
              --SEQ
              --DW[TIME.STAMP]:=clock   --use the current clock do not update it!!!!!!!!!!!!

              PAR
                ToRegBa!DW
                SEQ
                  init.hex(Regc[0], [FALSE, DWqb[1]])  --Wmux   --[Ma, valid]
                  ToRegBb!Regc
              FRegBA? Ackrb
              clock:=Ackrb[TIME.STAMP] + delay[10]  --CALL+XOR+XOR      --OK for found  TRUE or FALSE
              debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2:Dgo sent to RegBank",DW)
          ----------------------------------------------------
          DWqb[0]=TRUE
            SEQ
              --IF
              --found=FALSE
              --SEQ
              clock:=clock+delay[8] --SELECT
              --found=TRUE
              --SEQ
              --SKIP
              IF
                DWqb[1]=TRUE        --valid
                  SEQ
                    PAR
                      ----------------------------
                      SEQ
                        debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo valid is TRUE  waiting to send to AddInt",DW)

                        init.hex(DWc[0], [PcPar,VSS,DWqb[1], VDD,VSS,DWqb[0],VSS,VDD,VSS,VSS])
                        --IF
                        --found=FALSE
                        --SEQ
                        DW[TIME.STAMP]:=clock+delay[9]  --CALL
                        --found=TRUE
                        --SEQ
                        --DW[TIME.STAMP]:=clock   --use current clock


                        DW[STALL]:=-1

                        PAR
                          ToAddInta!DW
                          ToAddIntb!DWc
                        FAddIntA? Ackad
                        clock:=Ackad[TIME.STAMP]+delay[10]    --CALL+XOR+XOR
                        debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo sent to AddInt",DW)

                      ----------------------------
                      SEQ
                        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: Waiting for Wlx ",NULL1)

                        FAdInWlx? Wlx
                        Wlx[TIME.STAMP]:=Wlx[TIME.STAMP]+delay[3]  --DESWAIT
                      -----------------------------
                DWqb[1]=FALSE
                  SEQ
                    debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo valid is FALSE not sending to AddInt",DW)
                    clock:=clock+delay[11]   --XOR

                    SKIP
        ----------------------------------------------------
        Ackdi[TIME.STAMP]:=clock
        calculate.occupancy(evaluate, occupancy.table[0], Ackdi[TIME.STAMP],DW.occ )
        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: Waiting to send Ack to DatInt",NULL1)
        ToDatIntA!Ackdi
        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: sent Ack to DatInt",NULL1)
    :
    --------------------------------------------------------------
    PROC WrtCtrl.preempt()
      ------------------------------------------------------------
      PROC update.lookahead.table(VAL INT action, num.of.ldmregs)
        SEQ
          IF
            action=APPEND
              SEQ
                IF
                  num.of.ldmregs<>(-1)   --LDM
                    SEQ
                      IF
                        num.of.ldmregs > 2
                          SEQ
                            lookahead.table[lkahead.table.top][lat.lookahead]:=clock+WRTCTRL.LAT.LKAHD --lookahead timestamp
                            lookahead.table[lkahead.table.top][num.of.regs]:=num.of.ldmregs-2
                            lkahead.table.top:=(lkahead.table.top+1) REM lkahead.table.size
                            contents:=contents+1
                            SEQ i=0 FOR 2
                              SEQ
                                lookahead.table[lkahead.table.top][lat.lookahead]:=((clock+ (((num.of.ldmregs-2)+i) * AV.STAGE.DELAY))+ WRTCTRL.LAT.LKAHD) --lookahead timestamp
                                lookahead.table[lkahead.table.top][num.of.regs]:=-1
                                lkahead.table.top:=(lkahead.table.top+1) REM lkahead.table.size
                                contents:=contents+1
                        num.of.ldmregs <= 2
                          SEQ
                            SEQ i=0 FOR num.of.ldmregs
                              SEQ
                                lookahead.table[lkahead.table.top][lat.lookahead]:=((clock+ (i * AV.STAGE.DELAY))+ WRTCTRL.LAT.LKAHD) --lookahead timestamp
                                lookahead.table[lkahead.table.top][num.of.regs]:=-1
                                lkahead.table.top:=(lkahead.table.top+1) REM lkahead.table.size
                                contents:=contents+1
                  num.of.ldmregs=(-1)   --NOT LDM
                    SEQ
                      lookahead.table[lkahead.table.top][lat.lookahead]:=clock+ WRTCTRL.LAT.LKAHD --lookahead timestamp
                      lookahead.table[lkahead.table.top][num.of.regs]:=-1
                      lkahead.table.top:=(lkahead.table.top+1) REM lkahead.table.size
                      contents:=contents+1
            action=DELETE
              SEQ
                lkahead.table.base:=(lkahead.table.base+1) REM lkahead.table.size  --delete
                contents:=contents-1
                IF
                  contents=0
                    SEQ
                      lookahead.table.empty:=TRUE
                  contents>0
                    SEQ
                      SKIP
      :
      ---------------------------------------------------
      PROC decode.instruction()
        INT temp:
        SEQ
          init.hex(temp, [WRqb[13],WRqb[14],WRqb[15],WRqb[16]])  
          IF
            temp<>0
              SEQ
                instruction:=LDM
                num.of.ldmregs:=temp
            temp=0
              SEQ
                instruction:=(-1)
                num.of.ldmregs:=(-1)
      :
      ---------------------------------------------------
      PROC Wgo.prmpt()
        SEQ
          int.to.bool(WRq[0], WRqb)
          decode.instruction()
          clock:=WR[TIME.STAMP]+delay[0]  --ARBITER
          Wgo()
          WR.received:=FALSE
          IF
            WRqb[10]=TRUE   --this was an address that was sent to AddInt
              SEQ
                IF
                  WRqb[3]=FALSE       --opc=false-this is a  data address
                    SEQ
                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Wgo.prmpt  data address found ",NULL1)

                      IF
                        instruction=LDM
                          SEQ
                            debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Wgo.prmpt  data address was LDM base",NULL1)

                            update.lookahead.table(APPEND, num.of.ldmregs)
                            --wait for second message from Ctrl3 as a
                            --result of the second (writeback) cycle
                            --of the LDM instruction
                            debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Wgo.prmpt waiting for a second message from Ctrl3",NULL1)
                            PAR
                              FWrtC1a?WR
                              FWrtC1b? WRq
                            ToRRegA!Ackrr
                            debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Wgo.prmpt received a second message from Ctrl3",WR)

                            clock:=WR[TIME.STAMP]+delay[0]  --ARBITER
                            WR.occ:=WR
                            Wgo()
                        instruction<>LDM
                          SEQ
                            update.lookahead.table(APPEND, -1)
                            ----------------------------------------
                  WRqb[3]=TRUE       --opc=true this was an instruction address
                    SEQ
                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Wgo.prmpt instruction address do not append ",NULL1)

                      SKIP
            WRqb[10]=FALSE
              SEQ
                SKIP
      :
      --------------------------------------------------
      PROC Dgo.prmpt()

        SEQ
          IF
            lookahead.table[lkahead.table.top][num.of.regs]<>(-1)
              SEQ  --LDM
                Dgo()
                debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Dgo.prmpt LDM entering loop",NULL1)

                SEQ i=0 FOR ((lookahead.table[lkahead.table.top][num.of.regs]) -1)
                  SEQ
                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Dgo.prmpt LDM waiting for DW",NULL1)

                    FDatInta? DW
                    FDatIntb? DWq
                    DW.occ:=DW
                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF  Dgo.prmpt LDM received DW",DW)

                    Dgo()
            lookahead.table[lkahead.table.top][num.of.regs]=(-1)
              SEQ
                Dgo()
      :
      --------------------------------------------------
      SEQ
        IF
          WR.received=FALSE
            SEQ
              debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF WR.received=FALSE",NULL1)
              debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting for WR",NULL1)
              FWrtC1a?WR
              debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Received WR",WR)

              SEQ
                SEQ
                  WR.occ:=WR
                  debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF received from Ctrl3 WR ",WR)
                  IF
                    WR[0]<>FINISH
                      SEQ
                        FWrtC1b? WRq
                        WR.received:=TRUE
                        debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF received from Ctrl3 WRq",WRq)
                    TRUE
                      SEQ
                        end:=TRUE
          WR.received=TRUE
            SEQ
              debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF WR.received=TRUE",NULL1)

              SKIP
        ---------------------------------------
        IF
          end=FALSE
            SEQ
              IF
                ----------------------------------------
                WR[0]=NULL.MESSAGE
                  SEQ
                    WR.received:=FALSE
                    PAR
                      SEQ
                        debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting to send to AddInt NULL MESSAGE",WR)

                        PAR
                          ToAddInta!WR
                          ToAddIntb!WRq
                        FAddIntA? Ackad
                        debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF sent to AddIntInt NULL MESSAGE",WR)

                      SEQ
                        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting for WLx from AddInt ",WR)

                        FAdInWlx? Wlx
                        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF received WLx from AddInt sending to WrtCtrl1 ",WR)

                        ToWrtC1!Wlx
                        debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF sent Wlx to WrtCtrl1 ",WR)

                ----------------------------------------
                WR[0]<>NULL.MESSAGE
                  SEQ
                    clock:=WR[TIME.STAMP]+delay[0]  --ARBITER
                    WR.occ:=WR
                    SEQ
                      SEQ
                        SEQ
                          IF
                            lookahead.table.empty=FALSE   -- a data address is expected
                              SEQ
                                debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF lookahead.table.empty=FALSE ",WR)

                                IF
                                  (lookahead.table[lkahead.table.base][lat.lookahead]  >= WR[TIME.STAMP])
                                    SEQ
                                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Queue >=  WR calling Wgo.prmpt",WR)

                                      Wgo.prmpt()
                                  (lookahead.table[lkahead.table.base][lat.lookahead] < WR[TIME.STAMP])
                                    SEQ
                                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Queue < WR waiting for DW",WR)

                                      FDatInta? DW
                                      FDatIntb? DWq
                                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF Queue < WR received DW",DW)

                                      DW.occ:=DW
                                      IF
                                        DW[TIME.STAMP] <= WR[TIME.STAMP]
                                          SEQ
                                            debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF DW[TIME.STAMP]<=WR[TIME.STAMP]",DW)

                                            update.lookahead.table(DELETE,0)
                                            DW.occ:=DW
                                            clock:=DW[TIME.STAMP]+delay[0]  --ARBITER
                                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: received from DatInt",DW)
                                            Dgo.prmpt()
                                        DW[TIME.STAMP] > WR[TIME.STAMP]
                                          SEQ
                                            WHILE ((((DW[TIME.STAMP]) > (WR[TIME.STAMP]))) AND (end=FALSE))
                                              SEQ
                                                debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF DW[TIME.STAMP]>WR[TIME.STAMP]",DW)

                                                Wgo.prmpt()
                                                SEQ
                                                  SEQ
                                                    SEQ
                                                      debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting for WR",NULL1)

                                                      FWrtC1a?WR
                                                      WR.occ:=WR
                                                      debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF received from Ctrl3 WR ",WR)
                                                      IF
                                                        WR[0]<>FINISH
                                                          SEQ
                                                            FWrtC1b? WRq
                                                            WR.received:=TRUE
                                                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF received from Ctrl3 WRq",WRq)
                                                            --------------------------------------------
                                                            WHILE ((WR[0]=NULL.MESSAGE)  AND (end=FALSE))   --ignore null messages just let them through
                                                              SEQ
                                                                WR.received:=FALSE
                                                                PAR

                                                                  SEQ
                                                                    debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting to send to AddIntInt NULL MESSAGE",WR)

                                                                    PAR
                                                                      ToAddInta!WR
                                                                      ToAddIntb!WRq
                                                                    FAddIntA? Ackad
                                                                    debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF sent to AddIntInt NULL MESSAGE",WR)
                                                                  SEQ
                                                                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting for WLx from AddInt ",WR)
                                                                    FAdInWlx? Wlx
                                                                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF received WLx from AddInt sending to WrtCtrl1 ",WR)
                                                                    ToWrtC1!Wlx 
                                                                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF sent Wlx to WrtCtrl1 ",WR)
                                                                    debugmsg(report,screen[1], "WRTCTRL-WRTCTRL2: PRMPT OFF waiting for WR ",NULL1)
                                                                FWrtC1a?WR
                                                                WR.occ:=WR
                                                                debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF received from Ctrl3 WR ",WR)
                                                                IF
                                                                  WR[0]<>FINISH
                                                                    SEQ
                                                                      FWrtC1b? WRq
                                                                      WR.received:=TRUE
                                                                  WR[0]=FINISH
                                                                    SEQ
                                                                      end:=TRUE
                                                            ----------------------------------------

                                                        TRUE
                                                          SEQ
                                                            end:=TRUE

                                            update.lookahead.table(DELETE,0)
                                            DW.occ:=DW
                                            clock:=DW[TIME.STAMP]+delay[0]  --ARBITER
                                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: received from DatInt",DW)
                                            Dgo.prmpt()

                            lookahead.table.empty=TRUE   -- no data address is expected
                              SEQ
                                debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: PRMPT OFF lookahead.table.empty=TRUE calling  Wgo.prmpt",WR)

                                Wgo.prmpt()

          TRUE
            SEQ
              SKIP


          
    :
    --------------------------------------------------------------------------------


    SEQ
      end:=FALSE
      msgnum:=0
      calculate.delays()
      initialize.occup.tables()
      clock:=0
      previous.report,preempt.frequency, preempt.magnitude,accum.preempt:=0,0,0,0

      WR.occ:=[0,0,0,0]
      DW.occ:=[0,0,0,0]

      lkahead.table.top, lkahead.table.base,contents:=0,0,0
      lookahead.table.empty:=TRUE

      WHILE end=FALSE
        SEQ

          IF
            (preemption=PREEMPTION.ON)
              --------------------------------------------------------------------------------------
              SEQ
                found:=FALSE
                debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: waiting from Ctrl3/wrtctrl1 or datint",WR)
                ALT
                  FDatInta? DW
                    SEQ
                      clock5?time
                      check.for.preemption(DW, WR.occ,clock, process.id, report, clock,
                                           previous.report, preempt.frequency,
                                           preempt.magnitude, msgnum,accum.preempt,screen[0],found)
                      DW.occ:=DW
                      IF
                        found=FALSE
                          SEQ
                            clock:=DW[TIME.STAMP]+delay[0]  --ARBITER
                        found=TRUE
                          SEQ
                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo-preemption found, current preempt.mag is", [preempt.magnitude,0,0,0])
                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Dgo-preemption found, current preempt.freq is", [preempt.frequency,0,0,0])

                            SKIP   --preemption found! use previous clock

                      FDatIntb? DWq
                      debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: received from DatInt",DW)
                      Dgo()

                  FWrtC1a?WR
                    SEQ
                      clock5?time

                      check.for.preemption(WR, DW.occ,clock, process.id,report,clock,
                           previous.report, preempt.frequency, preempt.magnitude,msgnum,accum.preempt,
                          screen[0],found)
                      IF
                        found=FALSE
                          SEQ
                            clock:=WR[TIME.STAMP]+delay[0]  --ARBITER


                        found=TRUE
                          SEQ
                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2:  Wgo -preemption found, current preempt.mag is", [preempt.magnitude,0,0,0])
                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: Wgo-preemption found, current preempt.freq is", [preempt.frequency,0,0,0])

                            SKIP   --preemption found! use previous clock


                      WR.occ:=WR

                      debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: received from Ctrl3 WR ",WR)
                      IF
                        WR[0]<>FINISH
                          SEQ
                            FWrtC1b? WRq
                            --clock? AFTER delay
                            debugmsg(report,screen[0], "WRTCTRL-WRTCTRL2: received from Ctrl3 WRq",WRq)

                            Wgo()
                        TRUE
                          SEQ
                            end:=TRUE
            TRUE               --preemption=PREEMPTION.OFF
              -----------------------------------------------------------
              SEQ
                --WrtCtrl.preempt()


      ----------------------------------------------------
      ToRegBa!WR
      terminate(evaluate, WR[TIME.STAMP], KILL,FLUSH, occupancy.table, screen[0], [ToAddInta,ToWrtC1])
  :
  ------------------------------------

  SEQ
    PAR
      WrtCtrl1(FRReg1,FRReg2,FWrtC2,ToWrtC2a,ToWrtC2b, screen[0], report,evaluate)

      WrtCtrl2(FWRegA,FAdInWlx,FRegBA,ToWrtC2a,ToWrtC2b,
               FDatInta,ToWreg1,ToWreg2,FWrtC,ToRegBa,ToRegBb,
               ToRRegA,ToDatIntA, [screen FROM 1 FOR 2], report,evaluate, preemption)

      Wlx.buffer(FWrtC,FWrtC2)

      WReg( ToWreg1,ToWreg2,ToAddInta,ToAddIntb,FWRegA, FAddIntA )

:


