/*
 * Copyright (c) 2022, Technische Universität Kaiserslautern
 * All rights reserved.
 *
 * 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.
 *
 * Authors:
 *    Iron Prando da Silva
 */

#include "TimeDependenciesInfoDDR3.h"

using namespace std;

TimeDependenciesInfoDDR3::TimeDependenciesInfoDDR3(const QJsonObject& memspec, const uint tCK) : DRAMTimeDependenciesBase(memspec, tCK) {
  mInitializeValues();
}

void TimeDependenciesInfoDDR3::mInitializeValues() {
  burstLength = mMemspecJson["memarchitecturespec"].toObject()["burstLength"].toInt();
  dataRate = mMemspecJson["memarchitecturespec"].toObject()["dataRate"].toInt();

  tCCD = tCK * mMemspecJson["memtimingspec"].toObject()["CCD"].toInt();
  tRCD = tCK * mMemspecJson["memtimingspec"].toObject()["RCD"].toInt();
  tRP = tCK * mMemspecJson["memtimingspec"].toObject()["RP"].toInt();
  tRAS = tCK * mMemspecJson["memtimingspec"].toObject()["RAS"].toInt();
  tCL = tCK * mMemspecJson["memtimingspec"].toObject()["CL"].toInt();
  tCWL = tCK * mMemspecJson["memtimingspec"].toObject()["CWL"].toInt();
  tAL = tCK * mMemspecJson["memtimingspec"].toObject()["AL"].toInt();
  tRL = tCK * mMemspecJson["memtimingspec"].toObject()["RL"].toInt();
  tWL = tCK * mMemspecJson["memtimingspec"].toObject()["WL"].toInt();
  tRTP = tCK * mMemspecJson["memtimingspec"].toObject()["RTP"].toInt();
  tWTR = tCK * mMemspecJson["memtimingspec"].toObject()["WTR"].toInt();
  tRRD = tCK * mMemspecJson["memtimingspec"].toObject()["RRD"].toInt();
  tWR = tCK * mMemspecJson["memtimingspec"].toObject()["WR"].toInt();
  tFAW = tCK * mMemspecJson["memtimingspec"].toObject()["FAW"].toInt();
  tRFC = tCK * mMemspecJson["memtimingspec"].toObject()["RFC"].toInt();
  tRC = tCK * mMemspecJson["memtimingspec"].toObject()["RC"].toInt();
  tXP = tCK * mMemspecJson["memtimingspec"].toObject()["XP"].toInt();
  tXS = tCK * mMemspecJson["memtimingspec"].toObject()["XS"].toInt();
  tXSDLL = tCK * mMemspecJson["memtimingspec"].toObject()["XSDLL"].toInt();
  tCKE = tCK * mMemspecJson["memtimingspec"].toObject()["CKE"].toInt();
  tCKESR = tCK * mMemspecJson["memtimingspec"].toObject()["CKESR"].toInt();
  tREFPDEN = tCK * mMemspecJson["memtimingspec"].toObject()["REFPDEN"].toInt();
  tACTPDEN = tCK * mMemspecJson["memtimingspec"].toObject()["ACTPDEN"].toInt();
  tPRPDEN = tCK * mMemspecJson["memtimingspec"].toObject()["PRPDEN"].toInt();
  tRTRS = tCK * mMemspecJson["memtimingspec"].toObject()["RTRS"].toInt();

  tPD = tCKE;

  tBURST = (uint) (burstLength / (float) dataRate) * tCK;
  tRDWR = tRL + tBURST + 2 * tCK - tWL;
  tRDWR_R = tRL + tBURST + tRTRS - tWL;
  tWRRD = tWL + tBURST + tWTR - tAL;
  tWRPRE = tWL + tBURST + tWR;
  tWRRD_R = tWL + tBURST + tRTRS - tRL;
  tRDPDEN = tRL + tBURST + tCK;
  tWRPDEN = tWL + tBURST + tWR;
  tWRAPDEN = tWL + tBURST + tWR + tCK;

  mPools.insert({
    "CMD_BUS", {
      1, {
        {"ACT", tCK},
        {"RD", tCK},
        {"WR", tCK},
        {"PREPB", tCK},
        {"RDA", tCK},
        {"WRA", tCK},
        {"REFAB", tCK},
        {"PREAB", tCK},
        {"PDEP", tCK},
        {"PDXP", tCK},
        {"SREFEN", tCK},
        {"SREFEX", tCK},
        {"PDEA", tCK},
        {"PDXA", tCK},
      }
    }
  });

  mPools.insert({
    "NAW", {
      4, {
        {"ACT", tFAW},
      }
    }
  });
  
}

const std::vector<QString> TimeDependenciesInfoDDR3::getPossiblePhases() {
  return {
    "ACT",
    "RD",
    "WR",
    "PREPB",
    "RDA",
    "WRA",
    "REFAB",
    "PREAB",
    "PDEP",
    "PDXP",
    "SREFEN",
    "SREFEX",
    "PDEA",
    "PDXA",
  };
}

DependencyMap TimeDependenciesInfoDDR3::mSpecializedGetDependencies() const {
  DependencyMap dmap;

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("ACT"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRC, "ACT", DependencyType::IntraBank, "tRC"},
        {tRRD, "ACT", DependencyType::IntraRank, "tRRD"},
        {tAL + tRTP + tRP, "RDA", DependencyType::IntraBank, "tAL + tRTP + tRP"},
        {tWRPRE + tRP, "WRA", DependencyType::IntraBank, "tWRPRE + tRP"},
        {tRP, "PREPB", DependencyType::IntraBank, "tRP"},
        {tRP, "PREAB", DependencyType::IntraRank, "tRP"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tXP, "PDXP", DependencyType::IntraRank, "tXP"},
        {tRFC, "REFAB", DependencyType::IntraRank, "tRFC"},
        {tXS, "SREFEX", DependencyType::IntraRank, "tXS"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
        {tFAW, "NAW", DependencyType::IntraRank, "tFAW"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("RD"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRCD - tAL, "ACT", DependencyType::IntraBank, "tRCD - tAL"},
        {tCCD, "RD", DependencyType::IntraBank, "tCCD"},
        {tCCD, "RD", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "RD", DependencyType::InterRank, "tBURST + tRTRS"},
        {tCCD, "RDA", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "RDA", DependencyType::InterRank, "tBURST + tRTRS"},
        {tWRRD, "WR", DependencyType::IntraBank, "tWRRD"},
        {tWRRD, "WR", DependencyType::IntraRank, "tWRRD"},
        {tWRRD_R, "WR", DependencyType::InterRank, "tWRRD_R"},
        {tWRRD, "WRA", DependencyType::IntraRank, "tWRRD"},
        {tWRRD_R, "WRA", DependencyType::InterRank, "tWRRD_R"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tXSDLL, "SREFEX", DependencyType::IntraRank, "tXSDLL"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("WR"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRCD - tAL, "ACT", DependencyType::IntraBank, "tRCD - tAL"},
        {tRDWR, "RD", DependencyType::IntraBank, "tRDWR"},
        {tRDWR, "RD", DependencyType::IntraRank, "tRDWR"},
        {tRDWR_R, "RD", DependencyType::InterRank, "tRDWR_R"},
        {tRDWR, "RDA", DependencyType::IntraRank, "tRDWR"},
        {tRDWR_R, "RDA", DependencyType::InterRank, "tRDWR_R"},
        {tCCD, "WR", DependencyType::IntraBank, "tCCD"},
        {tCCD, "WR", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "WR", DependencyType::InterRank, "tBURST + tRTRS"},
        {tCCD, "WRA", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "WRA", DependencyType::InterRank, "tBURST + tRTRS"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tXSDLL, "SREFEX", DependencyType::IntraRank, "tXSDLL"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PREPB"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRAS, "ACT", DependencyType::IntraBank, "tRAS"},
        {tAL + tRTP, "RD", DependencyType::IntraBank, "tAL + tRTP"},
        {tWRPRE, "WR", DependencyType::IntraBank, "tWRPRE"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("RDA"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRCD - tAL, "ACT", DependencyType::IntraBank, "tRCD - tAL"},
        {tCCD, "RD", DependencyType::IntraBank, "tCCD"},
        {tCCD, "RD", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "RD", DependencyType::InterRank, "tBURST + tRTRS"},
        {tCCD, "RDA", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "RDA", DependencyType::InterRank, "tBURST + tRTRS"},
        {max({tWRRD, tWRPRE - tRTP - tAL}), "WR", DependencyType::IntraBank, "max(tWRRD, tWRPRE - tRTP - tAL)"},
        {tWRRD, "WR", DependencyType::IntraRank, "tWRRD"},
        {tWRRD_R, "WR", DependencyType::InterRank, "tWRRD_R"},
        {tWRRD, "WRA", DependencyType::IntraRank, "tWRRD"},
        {tWRRD_R, "WRA", DependencyType::InterRank, "tWRRD_R"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tXSDLL, "SREFEX", DependencyType::IntraRank, "tXSDLL"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("WRA"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRCD - tAL, "ACT", DependencyType::IntraBank, "tRCD - tAL"},
        {tRDWR, "RD", DependencyType::IntraBank, "tRDWR"},
        {tRDWR, "RD", DependencyType::IntraRank, "tRDWR"},
        {tRDWR_R, "RD", DependencyType::InterRank, "tRDWR_R"},
        {tRDWR, "RDA", DependencyType::IntraRank, "tRDWR"},
        {tRDWR_R, "RDA", DependencyType::InterRank, "tRDWR_R"},
        {tCCD, "WR", DependencyType::IntraBank, "tCCD"},
        {tCCD, "WR", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "WR", DependencyType::InterRank, "tBURST + tRTRS"},
        {tCCD, "WRA", DependencyType::IntraRank, "tCCD"},
        {tBURST + tRTRS, "WRA", DependencyType::InterRank, "tBURST + tRTRS"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tXSDLL, "SREFEX", DependencyType::IntraRank, "tXSDLL"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("REFAB"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRC, "ACT", DependencyType::IntraRank, "tRC"},
        {tAL + tRTP + tRP, "RDA", DependencyType::IntraRank, "tAL + tRTP + tRP"},
        {tWRPRE + tRP, "WRA", DependencyType::IntraRank, "tWRPRE + tRP"},
        {tRP, "PREPB", DependencyType::IntraRank, "tRP"},
        {tRP, "PREAB", DependencyType::IntraRank, "tRP"},
        {tXP, "PDXP", DependencyType::IntraRank, "tXP"},
        {tRFC, "REFAB", DependencyType::IntraRank, "tRFC"},
        {tXS, "SREFEX", DependencyType::IntraRank, "tXS"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PREAB"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRAS, "ACT", DependencyType::IntraRank, "tRAS"},
        {tAL + tRTP, "RD", DependencyType::IntraRank, "tAL + tRTP"},
        {tAL + tRTP, "RDA", DependencyType::IntraRank, "tAL + tRTP"},
        {tWRPRE, "WR", DependencyType::IntraRank, "tWRPRE"},
        {tWRPRE, "WRA", DependencyType::IntraRank, "tWRPRE"},
        {tXP, "PDXA", DependencyType::IntraRank, "tXP"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PDEP"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRDPDEN, "RD", DependencyType::IntraRank, "tRDPDEN"},
        {tRDPDEN, "RDA", DependencyType::IntraRank, "tRDPDEN"},
        {tWRAPDEN, "WRA", DependencyType::IntraRank, "tWRAPDEN"},
        {tPRPDEN, "PREPB", DependencyType::IntraRank, "tPRPDEN"},
        {tPRPDEN, "PREAB", DependencyType::IntraRank, "tPRPDEN"},
        {tCKE, "PDXP", DependencyType::IntraRank, "tCKE"},
        {tREFPDEN, "REFAB", DependencyType::IntraRank, "tREFPDEN"},
        {tXS, "SREFEX", DependencyType::IntraRank, "tXS"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PDXP"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tPD, "PDEP", DependencyType::IntraRank, "tPD"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("SREFEN"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tRC, "ACT", DependencyType::IntraRank, "tRC"},
        {max({tRDPDEN, tAL + tRTP + tRP}), "RDA", DependencyType::IntraRank, "max(tRDPDEN, tAL + tRTP + tRP)"},
        {max({tWRAPDEN, tWRPRE + tRP}), "WRA", DependencyType::IntraRank, "max(tWRAPDEN, tWRPRE + tRP)"},
        {tRP, "PREPB", DependencyType::IntraRank, "tRP"},
        {tRP, "PREAB", DependencyType::IntraRank, "tRP"},
        {tXP, "PDXP", DependencyType::IntraRank, "tXP"},
        {tRFC, "REFAB", DependencyType::IntraRank, "tRFC"},
        {tXS, "SREFEX", DependencyType::IntraRank, "tXS"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("SREFEX"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tCKESR, "SREFEN", DependencyType::IntraRank, "tCKESR"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PDEA"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tACTPDEN, "ACT", DependencyType::IntraRank, "tACTPDEN"},
        {tRDPDEN, "RD", DependencyType::IntraRank, "tRDPDEN"},
        {tRDPDEN, "RDA", DependencyType::IntraRank, "tRDPDEN"},
        {tWRPDEN, "WR", DependencyType::IntraRank, "tWRPDEN"},
        {tWRAPDEN, "WRA", DependencyType::IntraRank, "tWRAPDEN"},
        {tPRPDEN, "PREPB", DependencyType::IntraRank, "tPRPDEN"},
        {tCKE, "PDXA", DependencyType::IntraRank, "tCKE"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  dmap.emplace(
    piecewise_construct,
    forward_as_tuple("PDXA"),
    forward_as_tuple(
      initializer_list<TimeDependency>{
        {tPD, "PDEA", DependencyType::IntraRank, "tPD"},
        {tCK, "CMD_BUS", DependencyType::InterRank, "CommandBus"},
      }
    )
  );

  return dmap;
}
