/*
 * Copyright (c) 2015, University of 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:
 *    Janik Schlemminger
 *    Matthias Jung
 *    Eder F. Zulian
 *    Felipe S. Prado
 */

#ifndef CONFIGURATION_H_
#define CONFIGURATION_H_

#include <systemc.h>
#include <string>
#include <cstdint>
#include "MemSpec.h"
#include "thermalSimConfig.h"
#include "../../../common/Utils.h"

#include "../../../error/eccbaseclass.h"

enum class StorageMode {NoStorage, Store, ErrorModel};

enum class EPowerDownMode {NoPowerDown, Staggered, TimeoutPDN, TimeoutSREF};

enum class ECCControllerMode {Disabled, Hamming};

struct  Configuration {
    static std::string memspecUri;
    static std::string mcconfigUri;
    std::string pathToResources;

    DEF_SINGLETON(Configuration);

    // MCConfig:
    bool BankwiseLogic = false;
    bool OpenPagePolicy = true;
    unsigned int MaxNrOfTransactions = 8;
    std::string Scheduler;
    unsigned int SJFProbability;
    unsigned int RequestBufferSize;
    unsigned int Capsize = 5;
    sc_time getPowerDownTimeout()
    {
        return powerDownTimeoutInClk * memSpec.clk;
    }
    EPowerDownMode PowerDownMode = EPowerDownMode::Staggered;
    bool ReadWriteGrouping = false;
    bool ReorderBuffer = false;

    // SimConfig
    std::string SimulationName = "default";
    bool DatabaseRecording = true;
    bool PowerAnalysis = false;
    bool EnableWindowing = false;
    unsigned int WindowSize = 1000;
    bool Debug = false;
    unsigned int NumberOfMemChannels = 1;
    bool ControllerCoreRefDisable = false;
    bool RowGranularRef = false;
    unsigned int trasbclk = 0;
    sc_time getTrasb();
    unsigned int trrdblclk = 0;
    sc_time getTrrdb_L();
    unsigned int trrdbsclk = 0;
    sc_time getTrrdb_S();
    unsigned int trpbclk = 0;
    sc_time getTrpb();
    unsigned int trcbclk = 0;
    sc_time getTrcb();
    unsigned int tfawbclk = 0;
    sc_time getTfawb();
    bool RGRB0 = true;
    bool RGRB1 = true;
    bool RGRB2 = true;
    bool RGRB3 = true;
    bool RGRB4 = true;
    bool RGRB5 = true;
    bool RGRB6 = true;
    bool RGRB7 = true;
    bool RGRB8 = true;
    bool RGRB9 = true;
    bool RGRB10 = true;
    bool RGRB11 = true;
    bool RGRB12 = true;
    bool RGRB13 = true;
    bool RGRB14 = true;
    bool RGRB15 = true;
    unsigned int NumAR = 8192;
    unsigned int RefMode = 1;
    unsigned int RowInc = 1;
    bool getRGRBank(unsigned int);
    unsigned int getNumAR(void);
    unsigned int getRowInc(void);
    unsigned int getRefMode(void);
    bool ControllerCoreRefForceMaxPostponeBurst = false;
    bool ControllerCoreRefEnablePostpone = false;
    bool ControllerCoreRefEnablePullIn = false;
    unsigned int ControllerCoreRefMaxPostponed = 8;
    unsigned int ControllerCoreRefMaxPulledIn = 8;
    bool ThermalSimulation = false;
    bool SimulationProgressBar = false;
    unsigned int NumberOfDevicesOnDIMM = 1;
    bool CheckTLM2Protocol = false;
    ECCControllerMode ECCMode = ECCControllerMode::Disabled;
    ECCBaseClass *pECC = nullptr;
    bool gem5 = false;
    bool UseMalloc = false;
    unsigned long long int AddressOffset = 0;

    // MemSpec (from DRAM-Power XML)
    MemSpec memSpec;

    void setParameter(std::string name, std::string value);
    void setParameters(std::map<std::string, std::string> parameterMap);

    //Configs for Seed, csv file and StorageMode
    unsigned int ErrorChipSeed;
    std::string ErrorCSVFile = "not defined.";
    StorageMode StoreMode;

    // Temperature Simulation related
    TemperatureSimConfig temperatureSim;

    std::uint64_t getSimMemSizeInBytes();
    unsigned int getDataBusWidth();
    unsigned int getBytesPerBurst();
    unsigned int adjustNumBytesAfterECC(unsigned bytes);
    void setPathToResources(std::string path);
    std::string getPathToResources();

private:
    Configuration();
    unsigned int powerDownTimeoutInClk = 3;
};

#endif /* CONFIGURATION_H_ */

