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

//#include "readwritegrouper.h"
//#include "../../common/DebugManager.h"

//namespace scheduler{

//using namespace tlm;
//using namespace std;

//ReadWriteGrouper::ReadWriteGrouper(ControllerCore& controllerCore): controllerCore(controllerCore)
//{
//    batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//    batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//}

//ReadWriteGrouper::~ReadWriteGrouper()
//{

//}

//void ReadWriteGrouper::schedule(gp *payload)
//{
//    tlm_command command = payload->get_command();

//    if(batches.size() > 2)
//    {
//        if(command == TLM_READ_COMMAND)
//        {
//            //printDebugMessage("Scheduling read");

//            if(schedulingReadCausesHazardWithQueuedWrite(payload))
//            {
//                printDebugMessage("Scheduling read causes hazard with queued write");
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//            }

//            getLatestReadBatch().schedule(payload);
//        }
//        else if(command == TLM_WRITE_COMMAND)
//        {
//            //printDebugMessage("Scheduling write");
//            getLatestWriteBatch().schedule(payload);
//        }
//    }
//    else if(batches.size() == 2)
//    {
//        if(command == TLM_READ_COMMAND)
//        {
//            //printDebugMessage("Scheduling read");

//            if(getLatestReadBatch().hasPayloads() && schedulingReadCausesHazardWithQueuedWrite(payload))
//            {
//                printDebugMessage("Scheduling read causes hazard with queued write");
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//            }
//            else if(!getLatestReadBatch().hasPayloads() && getLatestWriteBatch().hasPayloads())
//            {
//                printDebugMessage("Scheduling read, but there are writes to be processed first");
//                batches.erase(batches.begin());
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//                batches.push_back(shared_ptr<FR_FCFS>(new FR_FCFS(controllerCore,true,false)));
//            }
//            getLatestReadBatch().schedule(payload);

//        }
//        else if(command == TLM_WRITE_COMMAND)
//        {
//            //printDebugMessage("Scheduling write");
//            getLatestWriteBatch().schedule(payload);
//        }
//    }
//    else
//    {
//        sc_assert(false);
//    }
//}

//gp *ReadWriteGrouper::getNextPayload()
//{
//    if(batches.size() > 2)
//    {
//        return batches.front()->getNextPayload();
//    }
//    else if(batches.size() == 2)
//    {
//        if(getLatestReadBatch().hasPayloads())
//            return getLatestReadBatch().getNextPayload();
//        else if(getLatestWriteBatch().hasPayloads())
//            return getLatestWriteBatch().getNextPayload();
//        else
//            return NULL;
//    }
//    else
//    {
//        sc_assert(false);
//        return NULL;
//    }
//}


//void ReadWriteGrouper::removePayload(gp *payload)
//{
//    if(batches.size() > 2)
//    {
//        batches.front()->removePayload(payload);
//        if(!batches.front()->hasPayloads())
//            batches.erase(batches.begin());
//    }
//    else if(batches.size() == 2)
//    {
//        if(payload->is_read())
//            getLatestReadBatch().removePayload(payload);
//        else
//            getLatestWriteBatch().removePayload(payload);
//    }
//    else
//    {
//        sc_assert(false);
//    }
//}


//bool ReadWriteGrouper::hasPayloads()
//{
//    if(batches.size() > 2)
//        return true;
//    else if(batches.size() == 2)
//        return (getLatestReadBatch().hasPayloads() || getLatestWriteBatch().hasPayloads());
//    else
//    {
//        sc_assert(false);
//        return NULL;
//    }
//}


//bool ReadWriteGrouper::schedulingReadCausesHazardWithQueuedWrite(gp *payload)
//{
//    sc_assert(payload->is_read());
//    return getLatestWriteBatch().containsPayloadTragetingSameAddress(payload);
//}

//FR_FCFS &ReadWriteGrouper::getLatestWriteBatch()
//{
//    return *batches[batches.size()-1];
//}

//FR_FCFS &ReadWriteGrouper::getLatestReadBatch()
//{
//    return *batches[batches.size()-2];
//}

//}
