/*
 * Copyright (c) 2010-2015 Advanced Micro Devices, Inc.
 * All rights reserved.
 *
 * For use for simulation and test purposes only
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Lisa Hsu
 */

machine(MachineType:L3Cache, "L3")
 : CacheMemory * L3cache;
   WireBuffer * reqToDir;
   WireBuffer * respToDir;
   WireBuffer * l3UnblockToDir;
   WireBuffer * reqToL3;
   WireBuffer * probeToL3;
   WireBuffer * respToL3;
   Cycles l3_request_latency := 1;
   Cycles l3_response_latency := 35;

  // To the general response network
  MessageBuffer * responseFromL3, network="To", virtual_network="2", ordered="false", vnet_type="response";

  // From the general response network
  MessageBuffer * responseToL3, network="From", virtual_network="2", ordered="false", vnet_type="response";

{
  // EVENTS
  enumeration(Event, desc="L3 Events") {
    // Requests coming from the Cores
    RdBlk,                  desc="CPU RdBlk event";
    RdBlkM,                 desc="CPU RdBlkM event";
    RdBlkS,                 desc="CPU RdBlkS event";
    CtoD,                   desc="Change to Dirty request";
    WrVicBlk,               desc="L2 Victim (dirty)";
    WrVicBlkShared,               desc="L2 Victim (dirty)";
    ClVicBlk,               desc="L2 Victim (clean)";
    ClVicBlkShared,               desc="L2 Victim (clean)";

    CPUData,                      desc="WB data from CPU";
    CPUDataShared,                desc="WB data from CPU, NBReqShared 1";
    StaleWB,                desc="WB stale; no data";

    L3_Repl,             desc="L3 Replacement";

    // Probes
    PrbInvData,         desc="Invalidating probe, return dirty data";
    PrbInv,             desc="Invalidating probe, no need to return data";
    PrbShrData,         desc="Downgrading probe, return data";

    // Coming from Memory Controller
    WBAck,                     desc="ack from memory";

    CancelWB,                   desc="Cancel WB from L2";
  }

  // STATES
  // Base States:
  state_declaration(State, desc="L3 State", default="L3Cache_State_I") {
    M, AccessPermission:Read_Write, desc="Modified";  // No other cache has copy, memory stale
    O, AccessPermission:Read_Only, desc="Owned";     // Correct most recent copy, others may exist in S
    E, AccessPermission:Read_Write, desc="Exclusive"; // Correct, most recent, and only copy (and == Memory)
    S, AccessPermission:Read_Only, desc="Shared";    // Correct, most recent. If no one in O, then == Memory
    I, AccessPermission:Invalid, desc="Invalid";

    I_M, AccessPermission:Busy, desc="Invalid, received WrVicBlk, sent Ack, waiting for Data";
    I_O, AccessPermission:Busy, desc="Invalid, received WrVicBlk, sent Ack, waiting for Data";
    I_E, AccessPermission:Busy, desc="Invalid, receive ClVicBlk, sent Ack, waiting for Data";
    I_S, AccessPermission:Busy, desc="Invalid, receive ClVicBlk, sent Ack, waiting for Data";
    S_M, AccessPermission:Busy, desc="received WrVicBlk, sent Ack, waiting for Data, then go to M";
    S_O, AccessPermission:Busy, desc="received WrVicBlkShared, sent Ack, waiting for Data, then go to O";
    S_E, AccessPermission:Busy, desc="Shared, received ClVicBlk, sent Ack, waiting for Data, then go to E";
    S_S, AccessPermission:Busy, desc="Shared, received ClVicBlk, sent Ack, waiting for Data, then go to S";
    E_M, AccessPermission:Busy, desc="received WrVicBlk, sent Ack, waiting for Data, then go to O";
    E_O, AccessPermission:Busy, desc="received WrVicBlkShared, sent Ack, waiting for Data, then go to O";
    E_E, AccessPermission:Busy, desc="received WrVicBlk, sent Ack, waiting for Data, then go to O";
    E_S, AccessPermission:Busy, desc="Shared, received WrVicBlk, sent Ack, waiting for Data";
    O_M, AccessPermission:Busy, desc="...";
    O_O, AccessPermission:Busy, desc="...";
    O_E, AccessPermission:Busy, desc="...";
    O_S, AccessPermission:Busy, desc="...";
    M_M, AccessPermission:Busy, desc="...";
    M_O, AccessPermission:Busy, desc="...";
    M_E, AccessPermission:Busy, desc="...";
    M_S, AccessPermission:Busy, desc="...";
    D_I, AccessPermission:Invalid,  desc="drop WB data on the floor when receive";
    MOD_I, AccessPermission:Busy, desc="drop WB data on the floor, waiting for WBAck from Mem";
    MO_I, AccessPermission:Busy, desc="M or O, received L3_Repl, waiting for WBAck from Mem";
    I_I, AccessPermission:Busy, desc="I_MO received L3_Repl";
    I_CD, AccessPermission:Busy, desc="I_I received WBAck, now just waiting for CPUData";
    I_C, AccessPermission:Invalid, desc="sent cancel, just waiting to receive mem wb ack so nothing gets confused";
  }

  enumeration(RequestType, desc="To communicate stats from transitions to recordStats") {
    DataArrayRead,    desc="Read the data array";
    DataArrayWrite,   desc="Write the data array";
    TagArrayRead,     desc="Read the data array";
    TagArrayWrite,    desc="Write the data array";
  }

  // STRUCTURES

  structure(Entry, desc="...", interface="AbstractCacheEntry") {
    State CacheState,           desc="cache state";
    bool Dirty,                 desc="Is the data dirty (diff from memory?)";
    DataBlock DataBlk,          desc="Data for the block";
  }

  structure(TBE, desc="...") {
    State TBEState,     desc="Transient state";
    DataBlock DataBlk,  desc="data for the block";
    bool Dirty,         desc="Is the data dirty?";
    bool Shared,        desc="Victim hit by shared probe";
    MachineID From,     desc="Waiting for writeback from...";
  }

  structure(TBETable, external="yes") {
    TBE lookup(Addr);
    void allocate(Addr);
    void deallocate(Addr);
    bool isPresent(Addr);
  }

  TBETable TBEs, template="<L3Cache_TBE>", constructor="m_number_of_TBEs";

  void set_cache_entry(AbstractCacheEntry b);
  void unset_cache_entry();
  void set_tbe(TBE b);
  void unset_tbe();
  void wakeUpAllBuffers();
  void wakeUpBuffers(Addr a);


  // FUNCTION DEFINITIONS
  Tick clockEdge();
  Tick cyclesToTicks(Cycles c);

  Entry getCacheEntry(Addr addr), return_by_pointer="yes" {
    return static_cast(Entry, "pointer", L3cache.lookup(addr));
  }

  DataBlock getDataBlock(Addr addr), return_by_ref="yes" {
    return getCacheEntry(addr).DataBlk;
  }

  bool presentOrAvail(Addr addr) {
    return L3cache.isTagPresent(addr) || L3cache.cacheAvail(addr);
  }

  State getState(TBE tbe, Entry cache_entry, Addr addr) {
    if (is_valid(tbe)) {
      return tbe.TBEState;
    } else if (is_valid(cache_entry)) {
      return cache_entry.CacheState;
    }
    return State:I;
  }

  void setState(TBE tbe, Entry cache_entry, Addr addr, State state) {
    if (is_valid(tbe)) {
        tbe.TBEState := state;
    }

    if (is_valid(cache_entry)) {
        cache_entry.CacheState := state;
    }
  }

  void functionalRead(Addr addr, Packet *pkt) {
    TBE tbe := TBEs.lookup(addr);
    if(is_valid(tbe)) {
      testAndRead(addr, tbe.DataBlk, pkt);
    } else {
      functionalMemoryRead(pkt);
    }
  }

  int functionalWrite(Addr addr, Packet *pkt) {
    int num_functional_writes := 0;

    TBE tbe := TBEs.lookup(addr);
    if(is_valid(tbe)) {
      num_functional_writes := num_functional_writes +
            testAndWrite(addr, tbe.DataBlk, pkt);
    }

    num_functional_writes := num_functional_writes +
        functionalMemoryWrite(pkt);
    return num_functional_writes;
  }

  AccessPermission getAccessPermission(Addr addr) {
    TBE tbe := TBEs.lookup(addr);
    if(is_valid(tbe)) {
      return L3Cache_State_to_permission(tbe.TBEState);
    }

    Entry cache_entry := getCacheEntry(addr);
    if(is_valid(cache_entry)) {
      return L3Cache_State_to_permission(cache_entry.CacheState);
    }

    return AccessPermission:NotPresent;
  }

  void setAccessPermission(Entry cache_entry, Addr addr, State state) {
    if (is_valid(cache_entry)) {
      cache_entry.changePermission(L3Cache_State_to_permission(state));
    }
  }

  void recordRequestType(RequestType request_type, Addr addr) {

  }

  bool checkResourceAvailable(RequestType request_type, Addr addr) {
    return true;
  }


  // OUT PORTS
  out_port(requestNetwork_out, CPURequestMsg, reqToDir);
  out_port(L3Resp_out, ResponseMsg, respToDir);
  out_port(responseNetwork_out, ResponseMsg, responseFromL3);
  out_port(unblockNetwork_out, UnblockMsg, l3UnblockToDir);

  // IN PORTS
  in_port(NBResponse_in, ResponseMsg, respToL3) {
    if (NBResponse_in.isReady(clockEdge())) {
      peek(NBResponse_in, ResponseMsg) {
        Entry cache_entry := getCacheEntry(in_msg.addr);
        TBE tbe := TBEs.lookup(in_msg.addr);
        if (in_msg.Type == CoherenceResponseType:NBSysWBAck) {
          trigger(Event:WBAck, in_msg.addr, cache_entry, tbe);
        } else {
          DPRINTF(RubySlicc, "%s\n", in_msg);
          error("Error on NBResponse Type");
        }
      }
    }
  }

  // Response Network
  in_port(responseNetwork_in, ResponseMsg, responseToL3) {
    if (responseNetwork_in.isReady(clockEdge())) {
      peek(responseNetwork_in, ResponseMsg) {
        Entry cache_entry := getCacheEntry(in_msg.addr);
        TBE tbe := TBEs.lookup(in_msg.addr);
        if (in_msg.Type == CoherenceResponseType:CPUData) {
          if (in_msg.NbReqShared) {
            trigger(Event:CPUDataShared, in_msg.addr, cache_entry, tbe);
          } else {
            trigger(Event:CPUData, in_msg.addr, cache_entry, tbe);
          }
        } else if (in_msg.Type == CoherenceResponseType:StaleNotif) {
            trigger(Event:StaleWB, in_msg.addr, cache_entry, tbe);
        } else {
          DPRINTF(RubySlicc, "%s\n", in_msg);
          error("Error on NBResponse Type");
        }
      }
    }
  }

  // probe network
  in_port(probeNetwork_in, NBProbeRequestMsg, probeToL3) {
    if (probeNetwork_in.isReady(clockEdge())) {
      peek(probeNetwork_in, NBProbeRequestMsg) {
        Entry cache_entry := getCacheEntry(in_msg.addr);
        TBE tbe := TBEs.lookup(in_msg.addr);
        if (in_msg.Type == ProbeRequestType:PrbInv) {
          if (in_msg.ReturnData) {
            trigger(Event:PrbInvData, in_msg.addr, cache_entry, tbe);
          } else {
            trigger(Event:PrbInv, in_msg.addr, cache_entry, tbe);
          }
        } else if (in_msg.Type == ProbeRequestType:PrbDowngrade) {
          if (in_msg.ReturnData) {
            trigger(Event:PrbShrData, in_msg.addr, cache_entry, tbe);
          } else {
            error("Don't think I should get any of these");
          }
        }
      }
    }
  }

  // Request Network
  in_port(requestNetwork_in, CPURequestMsg, reqToL3) {
    if (requestNetwork_in.isReady(clockEdge())) {
      peek(requestNetwork_in, CPURequestMsg) {
        assert(in_msg.Destination.isElement(machineID));
        Entry cache_entry := getCacheEntry(in_msg.addr);
        TBE tbe := TBEs.lookup(in_msg.addr);
        if (in_msg.Type == CoherenceRequestType:RdBlk) {
          trigger(Event:RdBlk, in_msg.addr, cache_entry, tbe);
        } else if (in_msg.Type == CoherenceRequestType:RdBlkS) {
          trigger(Event:RdBlkS, in_msg.addr, cache_entry, tbe);
        } else if (in_msg.Type == CoherenceRequestType:RdBlkM) {
          trigger(Event:RdBlkM, in_msg.addr, cache_entry, tbe);
        } else if (in_msg.Type == CoherenceRequestType:VicClean) {
          if (presentOrAvail(in_msg.addr)) {
            if (in_msg.Shared) {
              trigger(Event:ClVicBlkShared, in_msg.addr, cache_entry, tbe);
            } else {
              trigger(Event:ClVicBlk, in_msg.addr, cache_entry, tbe);
            }
          } else {
            Addr victim :=  L3cache.cacheProbe(in_msg.addr);
            trigger(Event:L3_Repl, victim, getCacheEntry(victim), TBEs.lookup(victim));
          }
        } else if (in_msg.Type == CoherenceRequestType:VicDirty) {
          if (presentOrAvail(in_msg.addr)) {
            if (in_msg.Shared) {
              trigger(Event:WrVicBlkShared, in_msg.addr, cache_entry, tbe);
            } else {
              trigger(Event:WrVicBlk, in_msg.addr, cache_entry, tbe);
            }
          } else {
            Addr victim := L3cache.cacheProbe(in_msg.addr);
            trigger(Event:L3_Repl, victim, getCacheEntry(victim), TBEs.lookup(victim));
          }
        } else if (in_msg.Type == CoherenceRequestType:WrCancel) {
          if (is_valid(tbe) && tbe.From == in_msg.Requestor) {
            trigger(Event:CancelWB, in_msg.addr, cache_entry, tbe);
          } else {
            requestNetwork_in.recycle(clockEdge(), cyclesToTicks(recycle_latency));
          }
        }
      }
    }
  }

  // BEGIN ACTIONS

  action(i_invL3, "i", desc="invalidate L3 cache block") {
    if (is_valid(cache_entry)) {
        L3cache.deallocate(address);
    }
    unset_cache_entry();
  }

  action(rm_sendResponseM, "rm", desc="send Modified response") {
    peek(requestNetwork_in, CPURequestMsg) {
      enqueue(responseNetwork_out, ResponseMsg, l3_response_latency) {
        out_msg.addr := address;
        out_msg.Type := CoherenceResponseType:NBSysResp;
        out_msg.Sender := machineID;
        out_msg.Destination.add(in_msg.Requestor);
        out_msg.DataBlk := cache_entry.DataBlk;
        out_msg.MessageSize := MessageSizeType:Response_Data;
        out_msg.Dirty := cache_entry.Dirty;
        out_msg.State := CoherenceState:Modified;
        DPRINTF(RubySlicc, "%s\n", out_msg);
      }
    }
  }

  action(rs_sendResponseS, "rs", desc="send Shared response") {
    peek(requestNetwork_in, CPURequestMsg) {
      enqueue(responseNetwork_out, ResponseMsg, l3_response_latency) {
        out_msg.addr := address;
        out_msg.Type := CoherenceResponseType:NBSysResp;
        out_msg.Sender := machineID;
        out_msg.Destination.add(in_msg.Requestor);
        out_msg.DataBlk := cache_entry.DataBlk;
        out_msg.MessageSize := MessageSizeType:Response_Data;
        out_msg.Dirty := cache_entry.Dirty;
        out_msg.State := CoherenceState:Shared;
        DPRINTF(RubySlicc, "%s\n", out_msg);
      }
    }
  }


  action(r_requestToMem, "r", desc="Miss in L3, pass on") {
    peek(requestNetwork_in, CPURequestMsg) {
      enqueue(requestNetwork_out, CPURequestMsg, l3_request_latency) {
        out_msg.addr := address;
        out_msg.Type := in_msg.Type;
        out_msg.Requestor := in_msg.Requestor;
        out_msg.Destination.add(map_Address_to_Directory(address));
        out_msg.Shared := false; // unneeded for this request
        out_msg.MessageSize := in_msg.MessageSize;
        DPRINTF(RubySlicc, "%s\n", out_msg);
      }
    }
  }

  action(t_allocateTBE, "t", desc="allocate TBE Entry") {
    TBEs.allocate(address);
    set_tbe(TBEs.lookup(address));
    if (is_valid(cache_entry)) {
      tbe.DataBlk := cache_entry.DataBlk; // Data only for WBs
      tbe.Dirty := cache_entry.Dirty;
    }
    tbe.From := machineID;
  }

  action(dt_deallocateTBE, "dt", desc="deallocate TBE Entry") {
    TBEs.deallocate(address);
    unset_tbe();
  }

  action(vd_vicDirty, "vd", desc="Victimize dirty L3 data") {
    enqueue(requestNetwork_out, CPURequestMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceRequestType:VicDirty;
      out_msg.Requestor := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address));
      out_msg.MessageSize := MessageSizeType:Request_Control;
    }
  }

  action(w_sendResponseWBAck, "w", desc="send WB Ack") {
    peek(requestNetwork_in, CPURequestMsg) {
      enqueue(responseNetwork_out, ResponseMsg, l3_response_latency) {
        out_msg.addr := address;
        out_msg.Type := CoherenceResponseType:NBSysWBAck;
        out_msg.Destination.add(in_msg.Requestor);
        out_msg.Sender := machineID;
        out_msg.MessageSize := MessageSizeType:Writeback_Control;
      }
    }
  }

  action(pi_sendProbeResponseInv, "pi", desc="send probe ack inv, no data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUPrbResp;  // L3 and CPUs respond in same way to probes
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address)); // will this always be ok? probably not for multisocket
      out_msg.Dirty := false;
      out_msg.Hit := false;
      out_msg.Ntsl := true;
      out_msg.State := CoherenceState:NA;
      out_msg.MessageSize := MessageSizeType:Response_Control;
    }
  }

  action(ph_sendProbeResponseHit, "ph", desc="send probe ack, no data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUPrbResp;  // L3 and CPUs respond in same way to probes
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address)); // will this always be ok? probably not for multisocket
      out_msg.Dirty := false;
      out_msg.Hit := true;
      out_msg.Ntsl := false;
      out_msg.State := CoherenceState:NA;
      out_msg.MessageSize := MessageSizeType:Response_Control;
    }
  }

  action(pm_sendProbeResponseMiss, "pm", desc="send probe ack, no data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUPrbResp;  // L3 and CPUs respond in same way to probes
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address)); // will this always be ok? probably not for multisocket
      out_msg.Dirty := false;
      out_msg.Hit := false;
      out_msg.Ntsl := false;
      out_msg.State := CoherenceState:NA;
      out_msg.MessageSize := MessageSizeType:Response_Control;
    }
  }

  action(pd_sendProbeResponseData, "pd", desc="send probe ack, with data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUPrbResp;  // L3 and CPUs respond in same way to probes
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address)); // will this always be ok? probably not for multisocket
      out_msg.DataBlk := cache_entry.DataBlk;
      assert(cache_entry.Dirty);
      out_msg.Dirty := true;
      out_msg.Hit := true;
      out_msg.State := CoherenceState:NA;
      out_msg.MessageSize := MessageSizeType:Response_Data;
    }
  }

  action(pdt_sendProbeResponseDataFromTBE, "pdt", desc="send probe ack with data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUPrbResp;
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address));
      out_msg.DataBlk := tbe.DataBlk;
      assert(tbe.Dirty);
      out_msg.Dirty := true;
      out_msg.Hit := true;
      out_msg.MessageSize := MessageSizeType:Response_Data;
      out_msg.State := CoherenceState:NA;
      DPRINTF(RubySlicc, "%s\n", out_msg);
    }
  }

  action(mc_cancelMemWriteback, "mc", desc="send writeback cancel to memory") {
    enqueue(requestNetwork_out, CPURequestMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceRequestType:WrCancel;
      out_msg.Requestor := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address));
      out_msg.MessageSize := MessageSizeType:Request_Control;
    }
  }

  action(a_allocateBlock, "a", desc="allocate L3 block") {
    if (is_invalid(cache_entry)) {
      set_cache_entry(L3cache.allocate(address, new Entry));
    }
  }

  action(d_writeData, "d", desc="write data to L3") {
    peek(responseNetwork_in, ResponseMsg) {
      if (in_msg.Dirty) {
        cache_entry.Dirty := in_msg.Dirty;
      }
      cache_entry.DataBlk := in_msg.DataBlk;
      DPRINTF(RubySlicc, "Writing to L3: %s\n", in_msg);
    }
  }

  action(rd_copyDataFromRequest, "rd", desc="write data to L3") {
    peek(requestNetwork_in, CPURequestMsg) {
      cache_entry.DataBlk := in_msg.DataBlk;
      cache_entry.Dirty := true;
    }
  }

  action(f_setFrom, "f", desc="set who WB is expected to come from") {
    peek(requestNetwork_in, CPURequestMsg) {
      tbe.From := in_msg.Requestor;
    }
  }

  action(rf_resetFrom, "rf", desc="reset From") {
    tbe.From := machineID;
  }

  action(wb_data, "wb", desc="write back data") {
    enqueue(L3Resp_out, ResponseMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Type := CoherenceResponseType:CPUData;
      out_msg.Sender := machineID;
      out_msg.Destination.add(map_Address_to_Directory(address));
      out_msg.DataBlk := tbe.DataBlk;
      out_msg.Dirty := tbe.Dirty;
      if (tbe.Shared) {
        out_msg.NbReqShared := true;
      } else {
        out_msg.NbReqShared := false;
      }
      out_msg.State := CoherenceState:Shared; // faux info
      out_msg.MessageSize := MessageSizeType:Writeback_Data;
      DPRINTF(RubySlicc, "%s\n", out_msg);
    }
  }

  action(wt_writeDataToTBE, "wt", desc="write WB data to TBE") {
    peek(responseNetwork_in, ResponseMsg) {
      tbe.DataBlk := in_msg.DataBlk;
      tbe.Dirty := in_msg.Dirty;
    }
  }

  action(uu_sendUnblock, "uu", desc="state changed, unblock") {
    enqueue(unblockNetwork_out, UnblockMsg, l3_request_latency) {
      out_msg.addr := address;
      out_msg.Destination.add(map_Address_to_Directory(address));
      out_msg.MessageSize := MessageSizeType:Unblock_Control;
      DPRINTF(RubySlicc, "%s\n", out_msg);
    }
  }

  action(ut_updateTag, "ut", desc="update Tag (i.e. set MRU)") {
    L3cache.setMRU(address);
  }

  action(p_popRequestQueue, "p", desc="pop request queue") {
    requestNetwork_in.dequeue(clockEdge());
  }

  action(pr_popResponseQueue, "pr", desc="pop response queue") {
    responseNetwork_in.dequeue(clockEdge());
  }

  action(pn_popNBResponseQueue, "pn", desc="pop NB response queue") {
    NBResponse_in.dequeue(clockEdge());
  }

  action(pp_popProbeQueue, "pp", desc="pop probe queue") {
    probeNetwork_in.dequeue(clockEdge());
  }

  action(zz_recycleRequestQueue, "\z", desc="recycle request queue") {
    requestNetwork_in.recycle(clockEdge(), cyclesToTicks(recycle_latency));
  }


  // END ACTIONS

  // BEGIN TRANSITIONS

  // transitions from base

  transition({I, I_C}, {RdBlk, RdBlkS, RdBlkM, CtoD}) {TagArrayRead} {
    r_requestToMem;
    p_popRequestQueue;
  }

  transition(O, RdBlk ) {TagArrayRead, DataArrayRead} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }
  transition(M, RdBlk, O) {TagArrayRead, DataArrayRead, TagArrayWrite} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }

  transition(S, RdBlk) {TagArrayRead, DataArrayRead} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }
  transition(E, RdBlk, S) {TagArrayRead, DataArrayRead, TagArrayWrite} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }

  transition({M, O}, RdBlkS, O) {TagArrayRead, DataArrayRead, TagArrayWrite} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }

  transition({E, S}, RdBlkS, S) {TagArrayRead, DataArrayRead, TagArrayWrite} {
    rs_sendResponseS;
    ut_updateTag;
    p_popRequestQueue;
  }

  transition(M, RdBlkM, I) {TagArrayRead, TagArrayWrite, DataArrayRead} {
    rm_sendResponseM;
    i_invL3;
    p_popRequestQueue;
  }

  transition({O, S}, {RdBlkM, CtoD}) {TagArrayRead} {
    r_requestToMem;  // can't handle this, just forward
    p_popRequestQueue;
  }

  transition(E, RdBlkM, I) {TagArrayRead, TagArrayWrite, DataArrayRead} {
    rm_sendResponseM;
    i_invL3;
    p_popRequestQueue;
  }

  transition({I}, WrVicBlk, I_M) {TagArrayRead, TagArrayWrite} {
    a_allocateBlock;
    t_allocateTBE;
    f_setFrom;
//    rd_copyDataFromRequest;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(I_C, {WrVicBlk, WrVicBlkShared, ClVicBlk, ClVicBlkShared}) {} {
    zz_recycleRequestQueue;
  }

  transition({I}, WrVicBlkShared, I_O) {TagArrayRead, TagArrayWrite} {
    a_allocateBlock;
    t_allocateTBE;
    f_setFrom;
//    rd_copyDataFromRequest;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(S, WrVicBlkShared, S_O) {TagArrayRead, TagArrayWrite} {
//    rd_copyDataFromRequest;
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(S, WrVicBlk, S_M) {TagArrayRead, TagArrayWrite} { // should be technically not possible, but assume the data comes back with shared bit flipped
//    rd_copyDataFromRequest;
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(E, WrVicBlk, E_M) {TagArrayRead, TagArrayWrite}  {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(E, WrVicBlkShared, E_O) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(O, WrVicBlk, O_M) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(O, WrVicBlkShared, O_O) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(M, WrVicBlk, M_M) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(M, WrVicBlkShared, M_O) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition({I}, ClVicBlk, I_E) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    a_allocateBlock;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition({I}, ClVicBlkShared, I_S) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    a_allocateBlock;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(S, ClVicBlk, S_E) {TagArrayRead, TagArrayWrite} { // technically impossible, assume data comes back with shared bit flipped
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(S, ClVicBlkShared, S_S) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(E, ClVicBlk, E_E) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(E, ClVicBlkShared, E_S) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(O, ClVicBlk, O_E) {TagArrayRead, TagArrayWrite} { // technically impossible, but assume data comes back with shared bit flipped
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(O, ClVicBlkShared, O_S) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(M, ClVicBlk, M_E) {TagArrayRead, TagArrayWrite}  {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(M, ClVicBlkShared, M_S) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition({MO_I}, {RdBlk, RdBlkS, RdBlkM, CtoD}) {} {
    r_requestToMem;
    p_popRequestQueue;
  }

  transition(MO_I, {WrVicBlkShared, WrVicBlk, ClVicBlk, ClVicBlkShared}, MOD_I) {TagArrayWrite} {
    f_setFrom;
    w_sendResponseWBAck;
    p_popRequestQueue;
  }

  transition(I_M, CPUData, M) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(I_M, CPUDataShared, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(I_O, {CPUData, CPUDataShared}, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(I_E, CPUData, E) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(I_E, CPUDataShared, S) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(I_S, {CPUData, CPUDataShared}, S) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    pr_popResponseQueue;
  }

  transition(S_M, CPUDataShared, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition(S_O, {CPUData, CPUDataShared}, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition(S_E, CPUDataShared, S) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition(S_S, {CPUData, CPUDataShared}, S) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition(O_E, CPUDataShared, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition(O_S, {CPUData, CPUDataShared}, O) {DataArrayWrite, TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    d_writeData;
    ut_updateTag;  // update tag on writeback hits.
    pr_popResponseQueue;
  }

  transition({D_I}, {CPUData, CPUDataShared}, I) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    pr_popResponseQueue;
  }

  transition(MOD_I, {CPUData, CPUDataShared}, MO_I) {TagArrayWrite} {
    uu_sendUnblock;
    rf_resetFrom;
    pr_popResponseQueue;
  }

  transition(I_I, {CPUData, CPUDataShared}, MO_I) {TagArrayWrite, DataArrayRead} {
    uu_sendUnblock;
    wt_writeDataToTBE;
    rf_resetFrom;
    pr_popResponseQueue;
  }

  transition(I_CD, {CPUData, CPUDataShared}, I) {DataArrayRead, TagArrayWrite} {
    uu_sendUnblock;
    wt_writeDataToTBE;
    wb_data;
    dt_deallocateTBE;
    pr_popResponseQueue;
  }

  transition({M, O}, L3_Repl, MO_I) {TagArrayRead, TagArrayWrite} {
    t_allocateTBE;
    vd_vicDirty;
    i_invL3;
  }

  transition({E, S,}, L3_Repl, I) {TagArrayRead, TagArrayWrite} {
    i_invL3;
  }

  transition({I_M, I_O, S_M, S_O, E_M, E_O}, L3_Repl) {} {
    zz_recycleRequestQueue;
  }

  transition({O_M, O_O, O_E, O_S, M_M, M_O, M_E, M_S}, L3_Repl) {} {
    zz_recycleRequestQueue;
  }

  transition({I_E, I_S, S_E, S_S, E_E, E_S}, L3_Repl) {} {
    zz_recycleRequestQueue;
  }

  transition({M, O}, PrbInvData, I) {TagArrayRead, TagArrayWrite, DataArrayRead} {
    pd_sendProbeResponseData;
    i_invL3;
    pp_popProbeQueue;
  }

  transition({E, S, I}, PrbInvData, I) {TagArrayRead, TagArrayWrite} {
    pi_sendProbeResponseInv;
    i_invL3;  // nothing will happen in I
    pp_popProbeQueue;
  }

  transition({M, O, E, S, I}, PrbInv, I) {TagArrayRead, TagArrayWrite} {
    pi_sendProbeResponseInv;
    i_invL3; // nothing will happen in I
    pp_popProbeQueue;
  }

  transition({M, O}, PrbShrData, O) {TagArrayRead, DataArrayRead, TagArrayWrite} {
    pd_sendProbeResponseData;
    pp_popProbeQueue;
  }

  transition({E, S}, PrbShrData, S) {TagArrayRead, TagArrayWrite} {
    ph_sendProbeResponseHit;
    pp_popProbeQueue;
  }

  transition(I, PrbShrData) {TagArrayRead} {
    pm_sendProbeResponseMiss;
    pp_popProbeQueue;
  }

  transition(MO_I, PrbInvData, I_C) {TagArrayWrite, DataArrayRead} {
    pdt_sendProbeResponseDataFromTBE;
    mc_cancelMemWriteback;
    pp_popProbeQueue;
  }

  transition(MO_I, PrbInv, I_C) {TagArrayWrite} {
    pi_sendProbeResponseInv;
    mc_cancelMemWriteback;
    pp_popProbeQueue;
  }

  transition(MO_I, PrbShrData) {DataArrayRead} {
    pdt_sendProbeResponseDataFromTBE;
    pp_popProbeQueue;
  }

  transition(I_C, {PrbInvData, PrbInv}) {} {
    pi_sendProbeResponseInv;
    pp_popProbeQueue;
  }

  transition(I_C, PrbShrData) {} {
    pm_sendProbeResponseMiss;
    pp_popProbeQueue;
  }

  transition(I_I, {WBAck}, I_CD) {TagArrayWrite} {
    pn_popNBResponseQueue;
  }

  transition(MOD_I, WBAck, D_I) {DataArrayRead} {
    wb_data;
    pn_popNBResponseQueue;
  }

  transition(MO_I, WBAck, I) {DataArrayRead, TagArrayWrite} {
    wb_data;
    dt_deallocateTBE;
    pn_popNBResponseQueue;
  }

  transition(I_C, {WBAck}, I) {TagArrayWrite} {
    dt_deallocateTBE;
    pn_popNBResponseQueue;
  }

  transition({I_M, I_O, I_E, I_S}, CancelWB, I) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    i_invL3;
    p_popRequestQueue;
  }

  transition({S_S, S_O, S_M, S_E}, CancelWB, S) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    p_popRequestQueue;
  }

  transition({E_M, E_O, E_E, E_S}, CancelWB, E) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    p_popRequestQueue;
  }

  transition({O_M, O_O, O_E, O_S}, CancelWB, O) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    p_popRequestQueue;
  }

  transition({M_M, M_O, M_E, M_S}, CancelWB, M) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    p_popRequestQueue;
  }

  transition(D_I, CancelWB, I) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    p_popRequestQueue;
  }

  transition(MOD_I, CancelWB, MO_I) {TagArrayWrite} {
    uu_sendUnblock;
    rf_resetFrom;
    p_popRequestQueue;
  }

  transition(I_I, CancelWB, I_C) {TagArrayWrite} {
    uu_sendUnblock;
    rf_resetFrom;
    mc_cancelMemWriteback;
    p_popRequestQueue;
  }

  transition(I_CD, CancelWB, I) {TagArrayWrite} {
    uu_sendUnblock;
    dt_deallocateTBE;
    mc_cancelMemWriteback;
    p_popRequestQueue;
  }

}
