/*
 * 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:
 *    Robert Gernhardt
 *    Matthias Jung
 */

#include "ScheduledCommand.h"
#include "../TimingCalculation.h"
#include "../../../common/Utils.h"
#include "../configuration/Configuration.h"

bool ScheduledCommand::isNoCommand() const
{
    return (command == Command::NOP && start == SC_ZERO_TIME
            && executionTime == SC_ZERO_TIME && end == SC_ZERO_TIME);
}

bool ScheduledCommand::isValidCommand() const
{
    return !isNoCommand();
}

const sc_time ScheduledCommand::getStart() const
{
    return start;
}

void ScheduledCommand::setStart(sc_time newStart)
{
    start = newStart;
    end = newStart + executionTime;
}

void ScheduledCommand::delayStart(sc_time delay)
{
    setStart(start + delay);
}

// Delays the command so that its start is at least value(constraint) from timepoint previous apart.
// If passed a constraint of 0ns, method will make sure that command starts no earlier than at timepoint previous
void ScheduledCommand::establishMinDistanceFromStart(sc_time previous,
                                                     sc_time constraint)
{
    delayStart(getDelayToMeetConstraint(previous, start, constraint));
}

sc_time ScheduledCommand::getEnd() const
{
    return end;
}

Command ScheduledCommand::getCommand() const
{
    return command;
}

sc_time ScheduledCommand::getExecutionTime() const
{
    return executionTime;
}

Bank ScheduledCommand::getBank() const
{
    return extension.getBank();
}

BankGroup ScheduledCommand::getBankGroup() const
{
    return extension.getBankGroup();
}

Row ScheduledCommand::getRow() const
{
    return extension.getRow();
}

unsigned int ScheduledCommand::getBurstLength() const
{
    return extension.getBurstlength();
}

bool ScheduledCommand::operator ==(const ScheduledCommand &b) const
{
    return b.command == command && b.start == start
           && b.executionTime == executionTime && b.end == end;
}

bool ScheduledCommand::commandIsIn(const std::vector<Command> &commandSet) const
{
    return isIn(command, commandSet);
}

TimeInterval ScheduledCommand::getIntervalOnDataStrobe() const
{
    //only read and write commands have an assoicated time on the data strobe
    sc_assert(getCommand() == Command::Read || getCommand() == Command::ReadA
              || getCommand() == Command::Write
              || getCommand() == Command::WriteA);

    MemSpec &timings = Configuration::getInstance().memSpec;

    if (getCommand() == Command::Read || getCommand() == Command::ReadA) {
        return TimeInterval(getStart() + timings.tRL,
                            getStart() + timings.tRL + getReadAccessTime());
    } else {
        return TimeInterval(getStart() + timings.tWL - timings.clk / 2,
                            getStart() + timings.tWL + getWriteAccessTime() - timings.clk / 2);
    }
}
