TimerTable.cc revision 6145
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * $Id$
32 */
33
34#include "Global.hh"
35#include "TimerTable.hh"
36#include "EventQueue.hh"
37
38TimerTable::TimerTable(Chip* chip_ptr)
39{
40  assert(chip_ptr != NULL);
41  m_consumer_ptr  = NULL;
42  m_chip_ptr = chip_ptr;
43  m_next_valid = false;
44  m_next_address = Address(0);
45  m_next_time = 0;
46}
47
48
49bool TimerTable::isReady() const
50{
51  if (m_map.size() == 0) {
52    return false;
53  }
54
55  if (!m_next_valid) {
56    updateNext();
57  }
58  assert(m_next_valid);
59  return (g_eventQueue_ptr->getTime() >= m_next_time);
60}
61
62const Address& TimerTable::readyAddress() const
63{
64  assert(isReady());
65
66  if (!m_next_valid) {
67    updateNext();
68  }
69  assert(m_next_valid);
70  return m_next_address;
71}
72
73void TimerTable::set(const Address& address, Time relative_latency)
74{
75  assert(address == line_address(address));
76  assert(relative_latency > 0);
77  assert(m_map.exist(address) == false);
78  Time ready_time = g_eventQueue_ptr->getTime() + relative_latency;
79  m_map.add(address, ready_time);
80  assert(m_consumer_ptr != NULL);
81  g_eventQueue_ptr->scheduleEventAbsolute(m_consumer_ptr, ready_time);
82  m_next_valid = false;
83
84  // Don't always recalculate the next ready address
85  if (ready_time <= m_next_time) {
86    m_next_valid = false;
87  }
88}
89
90void TimerTable::unset(const Address& address)
91{
92  assert(address == line_address(address));
93  assert(m_map.exist(address) == true);
94  m_map.remove(address);
95
96  // Don't always recalculate the next ready address
97  if (address == m_next_address) {
98    m_next_valid = false;
99  }
100}
101
102void TimerTable::print(ostream& out) const
103{
104}
105
106
107void TimerTable::updateNext() const
108{
109  if (m_map.size() == 0) {
110    assert(m_next_valid == false);
111    return;
112  }
113
114  Vector<Address> addresses = m_map.keys();
115  m_next_address = addresses[0];
116  m_next_time = m_map.lookup(m_next_address);
117
118  // Search for the minimum time
119  int size = addresses.size();
120  for (int i=1; i<size; i++) {
121    Address maybe_next_address = addresses[i];
122    Time maybe_next_time = m_map.lookup(maybe_next_address);
123    if (maybe_next_time < m_next_time) {
124      m_next_time = maybe_next_time;
125      m_next_address= maybe_next_address;
126    }
127  }
128  m_next_valid = true;
129}
130