peq_with_get.h revision 13513
113511Sgabeblack@google.com/*****************************************************************************
213511Sgabeblack@google.com
313511Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
413511Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
513511Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
613511Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
713511Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
813511Sgabeblack@google.com  License.  You may obtain a copy of the License at
913511Sgabeblack@google.com
1013511Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1113511Sgabeblack@google.com
1213511Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1313511Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1413511Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1513511Sgabeblack@google.com  implied.  See the License for the specific language governing
1613511Sgabeblack@google.com  permissions and limitations under the License.
1713511Sgabeblack@google.com
1813511Sgabeblack@google.com *****************************************************************************/
1913511Sgabeblack@google.com
2013511Sgabeblack@google.com// 12-Jan-2009  John Aynsley  Bug fix. sc_time argument to notify should be const
2113511Sgabeblack@google.com// 20-Mar-2009  John Aynsley  Add cancel_all() method
2213511Sgabeblack@google.com
2313511Sgabeblack@google.com
2413513Sgabeblack@google.com#ifndef __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__
2513513Sgabeblack@google.com#define __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__
2613511Sgabeblack@google.com
2713513Sgabeblack@google.com#include <map>
2813511Sgabeblack@google.com#include <systemc>
2913511Sgabeblack@google.com
3013513Sgabeblack@google.comnamespace tlm_utils
3113513Sgabeblack@google.com{
3213511Sgabeblack@google.com
3313511Sgabeblack@google.comtemplate <class PAYLOAD>
3413511Sgabeblack@google.comclass peq_with_get : public sc_core::sc_object
3513511Sgabeblack@google.com{
3613513Sgabeblack@google.com  public:
3713513Sgabeblack@google.com    typedef PAYLOAD transaction_type;
3813513Sgabeblack@google.com    typedef std::pair<const sc_core::sc_time, transaction_type *> pair_type;
3913511Sgabeblack@google.com
4013513Sgabeblack@google.com  public:
4113513Sgabeblack@google.com    peq_with_get(const char *name) : sc_core::sc_object(name) {}
4213511Sgabeblack@google.com
4313513Sgabeblack@google.com    void
4413513Sgabeblack@google.com    notify(transaction_type &trans, const sc_core::sc_time &t)
4513513Sgabeblack@google.com    {
4613513Sgabeblack@google.com        m_scheduled_events.insert(pair_type(t + sc_core::sc_time_stamp(),
4713513Sgabeblack@google.com                    &trans));
4813513Sgabeblack@google.com        m_event.notify(t);
4913511Sgabeblack@google.com    }
5013511Sgabeblack@google.com
5113513Sgabeblack@google.com    void
5213513Sgabeblack@google.com    notify(transaction_type &trans)
5313513Sgabeblack@google.com    {
5413513Sgabeblack@google.com        m_scheduled_events.insert(pair_type(sc_core::sc_time_stamp(), &trans));
5513513Sgabeblack@google.com        m_event.notify(); // Immediate notification.
5613511Sgabeblack@google.com    }
5713511Sgabeblack@google.com
5813513Sgabeblack@google.com    // Needs to be called until it returns NULL
5913513Sgabeblack@google.com    transaction_type *
6013513Sgabeblack@google.com    get_next_transaction()
6113513Sgabeblack@google.com    {
6213513Sgabeblack@google.com        if (m_scheduled_events.empty()) {
6313513Sgabeblack@google.com            return nullptr;
6413513Sgabeblack@google.com        }
6513511Sgabeblack@google.com
6613513Sgabeblack@google.com        sc_core::sc_time now = sc_core::sc_time_stamp();
6713513Sgabeblack@google.com        if (m_scheduled_events.begin()->first <= now) {
6813513Sgabeblack@google.com            transaction_type *trans = m_scheduled_events.begin()->second;
6913513Sgabeblack@google.com            m_scheduled_events.erase(m_scheduled_events.begin());
7013513Sgabeblack@google.com            return trans;
7113513Sgabeblack@google.com        }
7213511Sgabeblack@google.com
7313513Sgabeblack@google.com        m_event.notify(m_scheduled_events.begin()->first - now);
7413511Sgabeblack@google.com
7513513Sgabeblack@google.com        return nullptr;
7613513Sgabeblack@google.com    }
7713511Sgabeblack@google.com
7813513Sgabeblack@google.com    sc_core::sc_event &get_event() { return m_event; }
7913513Sgabeblack@google.com
8013513Sgabeblack@google.com    // Cancel all events from the event queue.
8113513Sgabeblack@google.com    void
8213513Sgabeblack@google.com    cancel_all()
8313513Sgabeblack@google.com    {
8413513Sgabeblack@google.com        m_scheduled_events.clear();
8513513Sgabeblack@google.com        m_event.cancel();
8613513Sgabeblack@google.com    }
8713513Sgabeblack@google.com
8813513Sgabeblack@google.com  private:
8913513Sgabeblack@google.com    std::multimap<const sc_core::sc_time, transaction_type *>
9013513Sgabeblack@google.com        m_scheduled_events;
9113513Sgabeblack@google.com    sc_core::sc_event m_event;
9213511Sgabeblack@google.com};
9313511Sgabeblack@google.com
9413513Sgabeblack@google.com} // namespace tlm_utils
9513511Sgabeblack@google.com
9613513Sgabeblack@google.com#endif /* __SYSTEMC_EXT_TLM_UTILS_PEQ_WITH_GET_H__ */
97