RubySlicc_Util.hh (10086:bd1089db3a88) RubySlicc_Util.hh (10302:0e9e99e6369a)
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/DataBlock.hh"
41#include "mem/packet.hh"
42
43inline int
44random(int n)
45{
46 return random() % n;
47}
48
49inline Cycles zero_time() { return Cycles(0); }
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/DataBlock.hh"
41#include "mem/packet.hh"
42
43inline int
44random(int n)
45{
46 return random() % n;
47}
48
49inline Cycles zero_time() { return Cycles(0); }
50inline Cycles TimeToCycles(Time t) { return Cycles(t); }
51
52inline NodeID
53intToID(int nodenum)
54{
55 NodeID id = nodenum;
56 return id;
57}
58
59inline int
60IDToInt(NodeID id)
61{
62 int nodenum = id;
63 return nodenum;
64}
65
66// Appends an offset to an address
67inline Address
68setOffset(Address addr, int offset)
69{
70 Address result = addr;
71 result.setOffset(offset);
72 return result;
73}
74
75// Makes an address into a line address
76inline Address
77makeLineAddress(Address addr)
78{
79 Address result = addr;
80 result.makeLineAddress();
81 return result;
82}
83
84inline int
85addressOffset(Address addr)
86{
87 return addr.getOffset();
88}
89
90inline int
91mod(int val, int mod)
92{
93 return val % mod;
94}
95
96inline int max_tokens()
97{
98 return 1024;
99}
100
101/**
102 * This function accepts an address, a data block and a packet. If the address
103 * range for the data block contains the address which the packet needs to
104 * read, then the data from the data block is written to the packet. True is
105 * returned if the data block was read, otherwise false is returned.
106 */
107inline bool
108testAndRead(Address addr, DataBlock& blk, Packet *pkt)
109{
110 Address pktLineAddr(pkt->getAddr());
111 pktLineAddr.makeLineAddress();
112
113 Address lineAddr = addr;
114 lineAddr.makeLineAddress();
115
116 if (pktLineAddr == lineAddr) {
117 uint8_t *data = pkt->getPtr<uint8_t>(true);
118 unsigned int size_in_bytes = pkt->getSize();
119 unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
120
121 for (unsigned i = 0; i < size_in_bytes; ++i) {
122 data[i] = blk.getByte(i + startByte);
123 }
124 return true;
125 }
126 return false;
127}
128
129/**
130 * This function accepts an address, a data block and a packet. If the address
131 * range for the data block contains the address which the packet needs to
132 * write, then the data from the packet is written to the data block. True is
133 * returned if the data block was written, otherwise false is returned.
134 */
135inline bool
136testAndWrite(Address addr, DataBlock& blk, Packet *pkt)
137{
138 Address pktLineAddr(pkt->getAddr());
139 pktLineAddr.makeLineAddress();
140
141 Address lineAddr = addr;
142 lineAddr.makeLineAddress();
143
144 if (pktLineAddr == lineAddr) {
145 uint8_t *data = pkt->getPtr<uint8_t>(true);
146 unsigned int size_in_bytes = pkt->getSize();
147 unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
148
149 for (unsigned i = 0; i < size_in_bytes; ++i) {
150 blk.setByte(i + startByte, data[i]);
151 }
152 return true;
153 }
154 return false;
155}
156
157#endif // __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__
50
51inline NodeID
52intToID(int nodenum)
53{
54 NodeID id = nodenum;
55 return id;
56}
57
58inline int
59IDToInt(NodeID id)
60{
61 int nodenum = id;
62 return nodenum;
63}
64
65// Appends an offset to an address
66inline Address
67setOffset(Address addr, int offset)
68{
69 Address result = addr;
70 result.setOffset(offset);
71 return result;
72}
73
74// Makes an address into a line address
75inline Address
76makeLineAddress(Address addr)
77{
78 Address result = addr;
79 result.makeLineAddress();
80 return result;
81}
82
83inline int
84addressOffset(Address addr)
85{
86 return addr.getOffset();
87}
88
89inline int
90mod(int val, int mod)
91{
92 return val % mod;
93}
94
95inline int max_tokens()
96{
97 return 1024;
98}
99
100/**
101 * This function accepts an address, a data block and a packet. If the address
102 * range for the data block contains the address which the packet needs to
103 * read, then the data from the data block is written to the packet. True is
104 * returned if the data block was read, otherwise false is returned.
105 */
106inline bool
107testAndRead(Address addr, DataBlock& blk, Packet *pkt)
108{
109 Address pktLineAddr(pkt->getAddr());
110 pktLineAddr.makeLineAddress();
111
112 Address lineAddr = addr;
113 lineAddr.makeLineAddress();
114
115 if (pktLineAddr == lineAddr) {
116 uint8_t *data = pkt->getPtr<uint8_t>(true);
117 unsigned int size_in_bytes = pkt->getSize();
118 unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
119
120 for (unsigned i = 0; i < size_in_bytes; ++i) {
121 data[i] = blk.getByte(i + startByte);
122 }
123 return true;
124 }
125 return false;
126}
127
128/**
129 * This function accepts an address, a data block and a packet. If the address
130 * range for the data block contains the address which the packet needs to
131 * write, then the data from the packet is written to the data block. True is
132 * returned if the data block was written, otherwise false is returned.
133 */
134inline bool
135testAndWrite(Address addr, DataBlock& blk, Packet *pkt)
136{
137 Address pktLineAddr(pkt->getAddr());
138 pktLineAddr.makeLineAddress();
139
140 Address lineAddr = addr;
141 lineAddr.makeLineAddress();
142
143 if (pktLineAddr == lineAddr) {
144 uint8_t *data = pkt->getPtr<uint8_t>(true);
145 unsigned int size_in_bytes = pkt->getSize();
146 unsigned startByte = pkt->getAddr() - lineAddr.getAddress();
147
148 for (unsigned i = 0; i < size_in_bytes; ++i) {
149 blk.setByte(i + startByte, data[i]);
150 }
151 return true;
152 }
153 return false;
154}
155
156#endif // __MEM_RUBY_SLICC_INTERFACE_RUBYSLICCUTIL_HH__