ruby: style pass
[gem5.git] / src / mem / ruby / libruby.cc
1 /*
2 * Copyright (c) 2009 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 #include <sys/wait.h>
30 #include <algorithm>
31
32 #include "config/gems_root.hh"
33 #include "mem/ruby/common/Address.hh"
34 #include "mem/ruby/eventqueue/RubyEventQueue.hh"
35 #include "mem/ruby/libruby_internal.hh"
36 #include "mem/ruby/recorder/Tracer.hh"
37 #include "mem/ruby/system/MemoryVector.hh"
38 #include "mem/ruby/system/RubyPort.hh"
39 #include "mem/ruby/system/System.hh"
40
41 string
42 RubyRequestType_to_string(const RubyRequestType& obj)
43 {
44 switch(obj) {
45 case RubyRequestType_IFETCH:
46 return "IFETCH";
47 case RubyRequestType_LD:
48 return "LD";
49 case RubyRequestType_ST:
50 return "ST";
51 case RubyRequestType_Locked_Read:
52 return "Locked_Read";
53 case RubyRequestType_Locked_Write:
54 return "Locked_Write";
55 case RubyRequestType_RMW_Read:
56 return "RMW_Read";
57 case RubyRequestType_RMW_Write:
58 return "RMW_Write";
59 case RubyRequestType_NULL:
60 default:
61 assert(0);
62 return "";
63 }
64 }
65
66 RubyRequestType
67 string_to_RubyRequestType(std::string str)
68 {
69 if (str == "IFETCH")
70 return RubyRequestType_IFETCH;
71 else if (str == "LD")
72 return RubyRequestType_LD;
73 else if (str == "ST")
74 return RubyRequestType_ST;
75 else if (str == "Locked_Read")
76 return RubyRequestType_Locked_Read;
77 else if (str == "Locked_Write")
78 return RubyRequestType_Locked_Write;
79 else if (str == "RMW_Read")
80 return RubyRequestType_RMW_Read;
81 else if (str == "RMW_Write")
82 return RubyRequestType_RMW_Write;
83 else
84 assert(0);
85 return RubyRequestType_NULL;
86 }
87
88 ostream&
89 operator<<(ostream& out, const RubyRequestType& obj)
90 {
91 out << RubyRequestType_to_string(obj);
92 out << flush;
93 return out;
94 }
95
96 ostream&
97 operator<<(std::ostream& out, const RubyRequest& obj)
98 {
99 out << hex << "0x" << obj.paddr << " data: 0x" << flush;
100 for (int i = 0; i < obj.len; i++) {
101 out << (int)obj.data[i];
102 }
103 out << dec << " type: " << RubyRequestType_to_string(obj.type) << endl;
104 return out;
105 }
106
107 vector<string>
108 tokenizeString(string str, string delims)
109 {
110 vector<string> tokens;
111 char* pch;
112 char* tmp;
113 const char* c_delims = delims.c_str();
114 tmp = new char[str.length()+1];
115 strcpy(tmp, str.c_str());
116 pch = strtok(tmp, c_delims);
117 while (pch != NULL) {
118 string tmp_str(pch);
119 if (tmp_str == "null") tmp_str = "";
120 tokens.push_back(tmp_str);
121
122 pch = strtok(NULL, c_delims);
123 }
124 delete [] tmp;
125 return tokens;
126 }
127
128 /*
129 * The current state of M5/Ruby integration breaks the libruby
130 * interface. This code is ifdef'd out for now so that we can move
131 * forward with the integration process for non-libruby uses. We'll
132 * have to go back and resolve the libruby compatibility issue at a
133 * later date.
134 */
135 #if 0
136 void
137 libruby_init(const char* cfg_filename)
138 {
139 ifstream cfg_output(cfg_filename);
140
141 vector<RubyObjConf> * sys_conf = new vector<RubyObjConf>;
142
143 string line;
144 getline(cfg_output, line) ;
145 while ( !cfg_output.eof() ) {
146 vector<string> tokens = tokenizeString(line, " ");
147 assert(tokens.size() >= 2);
148 vector<string> argv;
149 for (size_t i=2; i<tokens.size(); i++) {
150 std::replace(tokens[i].begin(), tokens[i].end(), '%', ' ');
151 std::replace(tokens[i].begin(), tokens[i].end(), '#', '\n');
152 argv.push_back(tokens[i]);
153 }
154 sys_conf->push_back(RubyObjConf(tokens[0], tokens[1], argv));
155 tokens.clear();
156 argv.clear();
157 getline(cfg_output, line);
158 }
159
160 RubySystem::create(*sys_conf);
161 delete sys_conf;
162 }
163 #endif
164
165 RubyPortHandle
166 libruby_get_port(const char* port_name,
167 void (*hit_callback)(int64_t access_id))
168 {
169 //
170 // Fix me: Hit callback is now a non-static member function pointer of
171 // RubyPort and cannot be set to an arbitrary global function
172 //
173 return NULL;//static_cast<RubyPortHandle>(RubySystem::getPort(port_name, hit_callback));
174 }
175
176 RubyPortHandle libruby_get_port_by_name(const char* port_name)
177 {
178 //
179 // Fix me: Ports should now be initialized using the python configuration
180 // system
181 //
182 return NULL;//static_cast<RubyPortHandle>(RubySystem::getPortOnly(port_name));
183 }
184
185 void
186 libruby_write_ram(uint64_t paddr, uint8_t* data, int len)
187 {
188 RubySystem::getMemoryVector()->write(Address(paddr), data, len);
189 }
190
191 void
192 libruby_read_ram(uint64_t paddr, uint8_t* data, int len)
193 {
194 RubySystem::getMemoryVector()->read(Address(paddr), data, len);
195 }
196
197 int64_t
198 libruby_issue_request(RubyPortHandle p, struct RubyRequest request)
199 {
200 //
201 // Fix me: Ports should now be accessed using the python configuration
202 // system
203 //
204 return 0;//return static_cast<RubyPort*>(p)->makeRequest(request);
205 }
206
207 int
208 libruby_tick(int n)
209 {
210 RubyEventQueue *eventq = RubySystem::getEventQueue();
211 eventq->triggerEvents(eventq->getTime() + n);
212 return 0;
213 }
214
215 void
216 libruby_destroy()
217 {
218 }
219
220 const char*
221 libruby_last_error()
222 {
223 return "";
224 }
225
226 void
227 libruby_print_config(std::ostream & out)
228 {
229 RubySystem::printConfig(out);
230 }
231
232 void
233 libruby_print_stats(std::ostream & out)
234 {
235 RubySystem::printStats(out);
236 }
237 void
238 libruby_playback_trace(char * trace_filename)
239 {
240 RubySystem::getTracer()->playbackTrace(trace_filename);
241 }
242
243 void
244 libruby_start_tracing(char * record_filename)
245 {
246 // start the trace
247 RubySystem::getTracer()->startTrace(record_filename);
248 }
249
250 void
251 libruby_stop_tracing()
252 {
253 // start the trace
254 RubySystem::getTracer()->stopTrace();
255 }
256
257 uint64_t
258 libruby_get_time()
259 {
260 return RubySystem::getCycleCount(0);
261 }