merged Tushar's bug fix with public repository changes
[gem5.git] / src / mem / ruby / system / TimerTable.cc
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 "mem/ruby/common/Global.hh"
35 #include "mem/ruby/system/TimerTable.hh"
36 #include "mem/ruby/eventqueue/RubyEventQueue.hh"
37
38 TimerTable::TimerTable()
39 {
40 m_consumer_ptr = NULL;
41 m_next_valid = false;
42 m_next_address = Address(0);
43 m_next_time = 0;
44 }
45
46
47 bool TimerTable::isReady() const
48 {
49 if (m_map.size() == 0) {
50 return false;
51 }
52
53 if (!m_next_valid) {
54 updateNext();
55 }
56 assert(m_next_valid);
57 return (g_eventQueue_ptr->getTime() >= m_next_time);
58 }
59
60 const Address& TimerTable::readyAddress() const
61 {
62 assert(isReady());
63
64 if (!m_next_valid) {
65 updateNext();
66 }
67 assert(m_next_valid);
68 return m_next_address;
69 }
70
71 void TimerTable::set(const Address& address, Time relative_latency)
72 {
73 assert(address == line_address(address));
74 assert(relative_latency > 0);
75 assert(m_map.exist(address) == false);
76 Time ready_time = g_eventQueue_ptr->getTime() + relative_latency;
77 m_map.add(address, ready_time);
78 assert(m_consumer_ptr != NULL);
79 g_eventQueue_ptr->scheduleEventAbsolute(m_consumer_ptr, ready_time);
80 m_next_valid = false;
81
82 // Don't always recalculate the next ready address
83 if (ready_time <= m_next_time) {
84 m_next_valid = false;
85 }
86 }
87
88 void TimerTable::unset(const Address& address)
89 {
90 assert(address == line_address(address));
91 assert(m_map.exist(address) == true);
92 m_map.remove(address);
93
94 // Don't always recalculate the next ready address
95 if (address == m_next_address) {
96 m_next_valid = false;
97 }
98 }
99
100 void TimerTable::print(ostream& out) const
101 {
102 }
103
104
105 void TimerTable::updateNext() const
106 {
107 if (m_map.size() == 0) {
108 assert(m_next_valid == false);
109 return;
110 }
111
112 Vector<Address> addresses = m_map.keys();
113 m_next_address = addresses[0];
114 m_next_time = m_map.lookup(m_next_address);
115
116 // Search for the minimum time
117 int size = addresses.size();
118 for (int i=1; i<size; i++) {
119 Address maybe_next_address = addresses[i];
120 Time maybe_next_time = m_map.lookup(maybe_next_address);
121 if (maybe_next_time < m_next_time) {
122 m_next_time = maybe_next_time;
123 m_next_address= maybe_next_address;
124 }
125 }
126 m_next_valid = true;
127 }