1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20// 12-Jan-2009  John Aynsley  Bug fix. sc_time argument to notify should be const
21// 20-Mar-2009  John Aynsley  Add cancel_all() method
22
23
24#ifndef __PEQ_WITH_GET_H__
25#define __PEQ_WITH_GET_H__
26
27#include <systemc>
28//#include <tlm>
29#include <map>
30
31namespace tlm_utils {
32
33template <class PAYLOAD>
34class peq_with_get : public sc_core::sc_object
35{
36public:
37  typedef PAYLOAD transaction_type;
38  typedef std::pair<const sc_core::sc_time, transaction_type*> pair_type;
39
40public:
41  peq_with_get(const char* name) : sc_core::sc_object(name)
42  {
43  }
44
45  void notify(transaction_type& trans, const sc_core::sc_time& t)
46  {
47    m_scheduled_events.insert(pair_type(t + sc_core::sc_time_stamp(), &trans));
48    m_event.notify(t);
49  }
50
51  void notify(transaction_type& trans)
52  {
53    m_scheduled_events.insert(pair_type(sc_core::sc_time_stamp(), &trans));
54    m_event.notify(); // immediate notification
55  }
56
57  // needs to be called until it returns 0
58  transaction_type* get_next_transaction()
59  {
60    if (m_scheduled_events.empty()) {
61      return 0;
62    }
63
64    sc_core::sc_time now = sc_core::sc_time_stamp();
65    if (m_scheduled_events.begin()->first <= now) {
66      transaction_type* trans = m_scheduled_events.begin()->second;
67      m_scheduled_events.erase(m_scheduled_events.begin());
68      return trans;
69    }
70
71    m_event.notify(m_scheduled_events.begin()->first - now);
72
73    return 0;
74  }
75
76  sc_core::sc_event& get_event()
77  {
78    return m_event;
79  }
80
81  // Cancel all events from the event queue
82  void cancel_all() {
83    m_scheduled_events.clear();
84    m_event.cancel();
85  }
86
87private:
88  std::multimap<const sc_core::sc_time, transaction_type*> m_scheduled_events;
89  sc_core::sc_event m_event;
90};
91
92}
93
94#endif
95