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#ifndef __TLM_GLOBAL_QUANTUM_H__
21#define __TLM_GLOBAL_QUANTUM_H__
22
23#include <systemc>
24
25namespace tlm {
26
27//
28// tlm_global_quantum class
29//
30// The global quantum is the maximum time an initiator can run ahead of
31// systemC time. All initiators should synchronize on timingpoints that
32// are multiples of the global quantum value.
33//
34// sc_set_time_resolution can only be called before the first
35// sc_time object is created. This means that after setting the
36// global quantum it will not be possible to call sc_set_time_resolution.
37// If sc_set_time_resolution must be called this must be done before
38// the global quantum is set.
39//
40
41class tlm_global_quantum
42{
43public:
44  //
45  // Returns a reference to the tlm_global_quantum singleton
46  //
47  static tlm_global_quantum& instance()
48  {
49    static tlm_global_quantum instance_;
50    return instance_;
51  }
52
53public:
54
55  //
56  // Setter/getter for the global quantum
57  //
58  void set(const sc_core::sc_time& t)
59  {
60    m_global_quantum = t;
61  }
62
63  const sc_core::sc_time& get() const
64  {
65    return m_global_quantum;
66  }
67
68  //
69  // This function will calculate the maximum value for the next local
70  // quantum for an initiator. All initiators should synchronize on
71  // integer multiples of the global quantum value. The value for the
72  // local quantum of an initiator can be smaller, but should never be
73  // greater than the value returned by this method.
74  //
75  sc_core::sc_time compute_local_quantum()
76  {
77    if (m_global_quantum != sc_core::SC_ZERO_TIME) {
78      const sc_core::sc_time current = sc_core::sc_time_stamp();
79      const sc_core::sc_time g_quant = m_global_quantum;
80      return g_quant - (current % g_quant);
81    } else {
82      return sc_core::SC_ZERO_TIME;
83    }
84  }
85
86protected:
87  tlm_global_quantum() : m_global_quantum(sc_core::SC_ZERO_TIME)
88  {
89  }
90
91protected:
92  sc_core::sc_time m_global_quantum;
93};
94
95} // namespace tlm
96
97#endif
98