ruby: Re-enabled orion power models
[gem5.git] / src / mem / ruby / libruby.cc
1
2 #include <sys/wait.h>
3 #include <algorithm>
4
5 #include "config/gems_root.hh"
6 #include "mem/ruby/libruby_internal.hh"
7 #include "mem/ruby/system/RubyPort.hh"
8 #include "mem/ruby/system/System.hh"
9 #include "mem/ruby/eventqueue/RubyEventQueue.hh"
10 #include "mem/ruby/system/MemoryVector.hh"
11 #include "mem/ruby/common/Address.hh"
12 #include "mem/ruby/recorder/Tracer.hh"
13
14 string RubyRequestType_to_string(const RubyRequestType& obj)
15 {
16 switch(obj) {
17 case RubyRequestType_IFETCH:
18 return "IFETCH";
19 case RubyRequestType_LD:
20 return "LD";
21 case RubyRequestType_ST:
22 return "ST";
23 case RubyRequestType_Locked_Read:
24 return "Locked_Read";
25 case RubyRequestType_Locked_Write:
26 return "Locked_Write";
27 case RubyRequestType_RMW_Read:
28 return "RMW_Read";
29 case RubyRequestType_RMW_Write:
30 return "RMW_Write";
31 case RubyRequestType_NULL:
32 default:
33 assert(0);
34 return "";
35 }
36 }
37
38 RubyRequestType string_to_RubyRequestType(std::string str)
39 {
40 if (str == "IFETCH")
41 return RubyRequestType_IFETCH;
42 else if (str == "LD")
43 return RubyRequestType_LD;
44 else if (str == "ST")
45 return RubyRequestType_ST;
46 else if (str == "Locked_Read")
47 return RubyRequestType_Locked_Read;
48 else if (str == "Locked_Write")
49 return RubyRequestType_Locked_Write;
50 else if (str == "RMW_Read")
51 return RubyRequestType_RMW_Read;
52 else if (str == "RMW_Write")
53 return RubyRequestType_RMW_Write;
54 else
55 assert(0);
56 return RubyRequestType_NULL;
57 }
58
59 ostream& operator<<(ostream& out, const RubyRequestType& obj)
60 {
61 out << RubyRequestType_to_string(obj);
62 out << flush;
63 return out;
64 }
65
66 ostream& operator<<(std::ostream& out, const RubyRequest& obj)
67 {
68 out << hex << "0x" << obj.paddr << " data: 0x" << flush;
69 for (int i = 0; i < obj.len; i++) {
70 out << (int)obj.data[i];
71 }
72 out << dec << " type: " << RubyRequestType_to_string(obj.type) << endl;
73 return out;
74 }
75
76 vector<string> tokenizeString(string str, string delims)
77 {
78 vector<string> tokens;
79 char* pch;
80 char* tmp;
81 const char* c_delims = delims.c_str();
82 tmp = new char[str.length()+1];
83 strcpy(tmp, str.c_str());
84 pch = strtok(tmp, c_delims);
85 while (pch != NULL) {
86 string tmp_str(pch);
87 if (tmp_str == "null") tmp_str = "";
88 tokens.push_back(tmp_str);
89
90 pch = strtok(NULL, c_delims);
91 }
92 delete [] tmp;
93 return tokens;
94 }
95
96
97 /*
98 * The current state of M5/Ruby integration breaks the libruby
99 * interface. This code is ifdef'd out for now so that we can move
100 * forward with the integration process for non-libruby uses. We'll
101 * have to go back and resolve the libruby compatibility issue at a
102 * later date.
103 */
104 #if 0
105 void libruby_init(const char* cfg_filename)
106 {
107 ifstream cfg_output(cfg_filename);
108
109 vector<RubyObjConf> * sys_conf = new vector<RubyObjConf>;
110
111 string line;
112 getline(cfg_output, line) ;
113 while ( !cfg_output.eof() ) {
114 vector<string> tokens = tokenizeString(line, " ");
115 assert(tokens.size() >= 2);
116 vector<string> argv;
117 for (size_t i=2; i<tokens.size(); i++) {
118 std::replace(tokens[i].begin(), tokens[i].end(), '%', ' ');
119 std::replace(tokens[i].begin(), tokens[i].end(), '#', '\n');
120 argv.push_back(tokens[i]);
121 }
122 sys_conf->push_back(RubyObjConf(tokens[0], tokens[1], argv));
123 tokens.clear();
124 argv.clear();
125 getline(cfg_output, line);
126 }
127
128 RubySystem::create(*sys_conf);
129 delete sys_conf;
130 }
131 #endif
132
133 RubyPortHandle libruby_get_port(const char* port_name, void (*hit_callback)(int64_t access_id))
134 {
135 return static_cast<RubyPortHandle>(RubySystem::getPort(port_name, hit_callback));
136 }
137
138 RubyPortHandle libruby_get_port_by_name(const char* port_name)
139 {
140 return static_cast<RubyPortHandle>(RubySystem::getPortOnly(port_name));
141 }
142
143 void libruby_write_ram(uint64_t paddr, uint8_t* data, int len)
144 {
145 RubySystem::getMemoryVector()->write(Address(paddr), data, len);
146 }
147
148 void libruby_read_ram(uint64_t paddr, uint8_t* data, int len)
149 {
150 RubySystem::getMemoryVector()->read(Address(paddr), data, len);
151 }
152
153 int64_t libruby_issue_request(RubyPortHandle p, struct RubyRequest request)
154 {
155 return static_cast<RubyPort*>(p)->makeRequest(request);
156 }
157
158 int libruby_tick(int n)
159 {
160 RubySystem::getEventQueue()->triggerEvents(RubySystem::getEventQueue()->getTime() + n);
161 return 0;
162 }
163
164 void libruby_destroy()
165 {
166 }
167
168 const char* libruby_last_error()
169 {
170 return "";
171 }
172
173 void libruby_print_config(std::ostream & out)
174 {
175 RubySystem::printConfig(out);
176 }
177
178 void libruby_print_stats(std::ostream & out)
179 {
180 RubySystem::printStats(out);
181 }
182 void libruby_playback_trace(char * trace_filename)
183 {
184 RubySystem::getTracer()->playbackTrace(trace_filename);
185 }
186
187 void libruby_start_tracing(char * record_filename) {
188 // start the trace
189 RubySystem::getTracer()->startTrace(record_filename);
190 }
191
192 void libruby_stop_tracing() {
193 // start the trace
194 RubySystem::getTracer()->stopTrace();
195 }
196
197 uint64_t libruby_get_time() {
198 return RubySystem::getCycleCount(0);
199 }