/*
 * 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
 */

#ifndef IPOWERDOWNMANAGER_H_
#define IPOWERDOWNMANAGER_H_

#include <systemc.h>
#include "../../../common/dramExtension.h"
#include "../../Command.h"


enum class PowerDownState {
    Awake, AwakeForRefresh, PDNActive, PDNPrecharge, PDNSelfRefresh
};

class IPowerDownManager
{
public:
    virtual ~IPowerDownManager() {}

    virtual void sleep(Bank bank, sc_time time) = 0;
    virtual void triggerSleep(Bank bank, sc_time time) = 0;

    virtual void wakeUp(Bank bank, sc_time time) = 0;
    virtual void wakeUpForRefresh(Bank bank, sc_time time) = 0;

    virtual bool isInSelfRefresh(Bank bank) = 0;

protected:
    Command getSleepCommand(PowerDownState state);
    Command getWakeUpCommand(PowerDownState state);

};

inline Command IPowerDownManager::getSleepCommand(PowerDownState state)
{
    Command cmd(Command::NOP);
    switch (state) {
    case PowerDownState::PDNActive:
        cmd = Command::PDNA;
        break;
    case PowerDownState::PDNPrecharge:
        cmd = Command::PDNP;
        break;
    case PowerDownState::PDNSelfRefresh:
        cmd = Command::SREF;
        break;
    default:
        SC_REPORT_FATAL("In PowerDownManager sendPowerdownBegin",
                        "invalid powerDownState");
        break;
    }
    return cmd;
}

inline Command IPowerDownManager::getWakeUpCommand(PowerDownState state)

{
    Command cmd(Command::NOP);
    switch (state) {
    case PowerDownState::PDNActive:
        cmd = Command::PDNAX;
        break;
    case PowerDownState::PDNPrecharge:
        cmd = Command::PDNPX;
        break;
    case PowerDownState::PDNSelfRefresh:
        cmd = Command::SREFX;
        break;
    default:
        SC_REPORT_FATAL("In PowerDownManager sendPowerdownEnd",
                        "invalid powerDownState");
    }
    return cmd;
}

inline std::string powerDownStateToString(PowerDownState powerDownState)
{
    switch (powerDownState) {
    case PowerDownState::Awake:
        return "Awake";
    case PowerDownState::AwakeForRefresh:
        return "Awake for refresh";
    case PowerDownState::PDNActive:
        return "PDN Active";
    case PowerDownState::PDNPrecharge:
        return "PDN Precharged";
    case PowerDownState::PDNSelfRefresh:
        return "PDN Self refresh";
    default:
        return "unknown state";
    }
}


#endif /* IPOWERDOWNMANAGER_H_ */
