RubySlicc_Util.hh revision 9466:23e13ad7091f
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * These are the functions that exported to slicc from ruby.
31 */
32
33#ifndef __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
34#define __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
35
36#include <cassert>
37
38#include "debug/RubySlicc.hh"
39#include "mem/ruby/common/Address.hh"
40#include "mem/ruby/common/Global.hh"
41#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
42#include "mem/ruby/system/System.hh"
43
44inline int
45random(int n)
46{
47  return random() % n;
48}
49
50inline Time
51get_time()
52{
53    return g_system_ptr->getTime();
54}
55
56inline Time
57zero_time()
58{
59    return 0;
60}
61
62inline NodeID
63intToID(int nodenum)
64{
65    NodeID id = nodenum;
66    return id;
67}
68
69inline int
70IDToInt(NodeID id)
71{
72    int nodenum = id;
73    return nodenum;
74}
75
76inline Time
77getTimeModInt(Time time, int modulus)
78{
79    return time % modulus;
80}
81
82inline Time
83getTimePlusInt(Time addend1, int addend2)
84{
85    return (Time) addend1 + addend2;
86}
87
88inline Time
89getTimeMinusTime(Time t1, Time t2)
90{
91    assert(t1 >= t2);
92    return t1 - t2;
93}
94
95// Return type for time_to_int is "Time" and not "int" so we get a
96// 64-bit integer
97inline Time
98time_to_int(Time time)
99{
100    return time;
101}
102
103// Appends an offset to an address
104inline Address
105setOffset(Address addr, int offset)
106{
107    Address result = addr;
108    result.setOffset(offset);
109    return result;
110}
111
112// Makes an address into a line address
113inline Address
114makeLineAddress(Address addr)
115{
116    Address result = addr;
117    result.makeLineAddress();
118    return result;
119}
120
121inline int
122addressOffset(Address addr)
123{
124    return addr.getOffset();
125}
126
127inline int
128mod(int val, int mod)
129{
130    return val % mod;
131}
132
133inline int max_tokens()
134{
135  return 1024;
136}
137
138/**
139 * This function accepts an address, a data block and a packet. If the address
140 * range for the data block contains the address which the packet needs to
141 * read, then the data from the data block is written to the packet. True is
142 * returned if the data block was read, otherwise false is returned.
143 */
144inline bool
145testAndRead(Address addr, DataBlock& blk, Packet *pkt)
146{
147    Address pktLineAddr(pkt->getAddr());
148    pktLineAddr.makeLineAddress();
149
150    Address lineAddr = addr;
151    lineAddr.makeLineAddress();
152
153    if (pktLineAddr == lineAddr) {
154        uint8_t *data = pkt->getPtr<uint8_t>(true);
155        unsigned int size_in_bytes = pkt->getSize();
156        unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
157
158        for (unsigned i = 0; i < size_in_bytes; ++i) {
159            data[i] = blk.getByte(i + startByte);
160        }
161        return true;
162    }
163    return false;
164}
165
166/**
167 * This function accepts an address, a data block and a packet. If the address
168 * range for the data block contains the address which the packet needs to
169 * write, then the data from the packet is written to the data block. True is
170 * returned if the data block was written, otherwise false is returned.
171 */
172inline bool
173testAndWrite(Address addr, DataBlock& blk, Packet *pkt)
174{
175    Address pktLineAddr(pkt->getAddr());
176    pktLineAddr.makeLineAddress();
177
178    Address lineAddr = addr;
179    lineAddr.makeLineAddress();
180
181    if (pktLineAddr == lineAddr) {
182        uint8_t *data = pkt->getPtr<uint8_t>(true);
183        unsigned int size_in_bytes = pkt->getSize();
184        unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
185
186        for (unsigned i = 0; i < size_in_bytes; ++i) {
187            blk.setByte(i + startByte, data[i]);
188        }
189        return true;
190    }
191    return false;
192}
193
194#endif // __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
195