[xcc,pk,opcodes,sim] updated encoding/insn names
[riscv-isa-sim.git] / riscv / sim.cc
1 #include "sim.h"
2 #include "applink.h"
3 #include "common.h"
4 #include <sys/mman.h>
5 #include <map>
6 #include <iostream>
7 #include <climits>
8
9 sim_t::sim_t(int _nprocs, size_t _memsz, appserver_link_t* _applink)
10 : applink(_applink),
11 memsz(_memsz),
12 mem((char*)mmap64(NULL, memsz, PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0)),
13 procs(std::vector<processor_t>(_nprocs,processor_t(this,mem,memsz)))
14 {
15 demand(mem != MAP_FAILED, "couldn't allocate target machine's memory");
16
17 for(int i = 0; i < (int)procs.size(); i++)
18 procs[i].init(i);
19
20 applink->init(this);
21 }
22
23 sim_t::~sim_t()
24 {
25 }
26
27 void sim_t::set_tohost(reg_t val)
28 {
29 fromhost = 0;
30 tohost = val;
31 applink->wait_for_tohost();
32 }
33
34 reg_t sim_t::get_fromhost()
35 {
36 applink->wait_for_fromhost();
37 return fromhost;
38 }
39
40 void sim_t::run(bool debug)
41 {
42 applink->wait_for_start();
43
44 while(1)
45 {
46 if(!debug)
47 step_all(100,100,false);
48 else
49 {
50 putchar(':');
51 char s[128];
52 std::cin.getline(s,sizeof(s)-1);
53
54 char* p = strtok(s," ");
55 if(!p)
56 {
57 interactive_run_noisy(std::string("r"), std::vector<std::string>(1,"1"));
58 continue;
59 }
60 std::string cmd = p;
61
62 std::vector<std::string> args;
63 while((p = strtok(NULL," ")))
64 args.push_back(p);
65
66
67 typedef void (sim_t::*interactive_func)(const std::string&, const std::vector<std::string>&);
68 std::map<std::string,interactive_func> funcs;
69
70 funcs["r"] = &sim_t::interactive_run_noisy;
71 funcs["rs"] = &sim_t::interactive_run_silent;
72 funcs["rp"] = &sim_t::interactive_run_proc_noisy;
73 funcs["rps"] = &sim_t::interactive_run_proc_silent;
74 funcs["reg"] = &sim_t::interactive_reg;
75 funcs["fregs"] = &sim_t::interactive_fregs;
76 funcs["fregd"] = &sim_t::interactive_fregd;
77 funcs["mem"] = &sim_t::interactive_mem;
78 funcs["until"] = &sim_t::interactive_until;
79 funcs["while"] = &sim_t::interactive_until;
80 funcs["q"] = &sim_t::interactive_quit;
81
82 try
83 {
84 if(funcs.count(cmd))
85 (this->*funcs[cmd])(cmd, args);
86 }
87 catch(trap_t t) {}
88 }
89 }
90 }
91
92 void sim_t::step_all(size_t n, size_t interleave, bool noisy)
93 {
94 for(size_t j = 0; j < n; j+=interleave)
95 for(int i = 0; i < (int)procs.size(); i++)
96 procs[i].step(interleave,noisy);
97 }
98
99 void sim_t::interactive_run_noisy(const std::string& cmd, const std::vector<std::string>& args)
100 {
101 interactive_run(cmd,args,true);
102 }
103
104 void sim_t::interactive_run_silent(const std::string& cmd, const std::vector<std::string>& args)
105 {
106 interactive_run(cmd,args,false);
107 }
108
109 void sim_t::interactive_run(const std::string& cmd, const std::vector<std::string>& args, bool noisy)
110 {
111 if(args.size())
112 step_all(atoll(args[0].c_str()),1,noisy);
113 else
114 while(1) step_all(1,1,noisy);
115 }
116
117 void sim_t::interactive_run_proc_noisy(const std::string& cmd, const std::vector<std::string>& args)
118 {
119 interactive_run_proc(cmd,args,true);
120 }
121
122 void sim_t::interactive_run_proc_silent(const std::string& cmd, const std::vector<std::string>& args)
123 {
124 interactive_run_proc(cmd,args,false);
125 }
126
127 void sim_t::interactive_run_proc(const std::string& cmd, const std::vector<std::string>& a, bool noisy)
128 {
129 if(a.size() == 0)
130 return;
131
132 int p = atoi(a[0].c_str());
133 if(p >= (int)procs.size())
134 return;
135
136 if(a.size() == 2)
137 procs[p].step(atoi(a[1].c_str()),noisy);
138 else
139 while(1) procs[p].step(1,noisy);
140 }
141
142 void sim_t::interactive_quit(const std::string& cmd, const std::vector<std::string>& args)
143 {
144 exit(0);
145 }
146
147 reg_t sim_t::get_pc(const std::vector<std::string>& args)
148 {
149 if(args.size() != 1)
150 throw trap_illegal_instruction;
151
152 int p = atoi(args[0].c_str());
153 if(p >= (int)procs.size())
154 throw trap_illegal_instruction;
155
156 return procs[p].pc;
157 }
158
159 reg_t sim_t::get_reg(const std::vector<std::string>& args)
160 {
161 if(args.size() != 2)
162 throw trap_illegal_instruction;
163
164 int p = atoi(args[0].c_str());
165 int r = atoi(args[1].c_str());
166 if(p >= (int)procs.size() || r >= NXPR)
167 throw trap_illegal_instruction;
168
169 return procs[p].XPR[r];
170 }
171
172 reg_t sim_t::get_freg(const std::vector<std::string>& args)
173 {
174 if(args.size() != 2)
175 throw trap_illegal_instruction;
176
177 int p = atoi(args[0].c_str());
178 int r = atoi(args[1].c_str());
179 if(p >= (int)procs.size() || r >= NFPR)
180 throw trap_illegal_instruction;
181
182 return procs[p].FPR[r];
183 }
184
185 reg_t sim_t::get_tohost(const std::vector<std::string>& args)
186 {
187 if(args.size() != 1)
188 throw trap_illegal_instruction;
189
190 int p = atoi(args[0].c_str());
191 if(p >= (int)procs.size())
192 throw trap_illegal_instruction;
193
194 return procs[p].tohost;
195 }
196
197 void sim_t::interactive_reg(const std::string& cmd, const std::vector<std::string>& args)
198 {
199 printf("0x%016llx\n",(unsigned long long)get_reg(args));
200 }
201
202 union fpr
203 {
204 reg_t r;
205 float s;
206 double d;
207 };
208
209 void sim_t::interactive_fregs(const std::string& cmd, const std::vector<std::string>& args)
210 {
211 fpr f;
212 f.r = get_freg(args);
213 printf("%g\n",f.s);
214 }
215
216 void sim_t::interactive_fregd(const std::string& cmd, const std::vector<std::string>& args)
217 {
218 fpr f;
219 f.r = get_freg(args);
220 printf("%g\n",f.d);
221 }
222
223 reg_t sim_t::get_mem(const std::vector<std::string>& args)
224 {
225 if(args.size() != 1)
226 throw trap_illegal_instruction;
227
228 reg_t addr = strtol(args[0].c_str(),NULL,16), val;
229 if(addr == LONG_MAX)
230 addr = strtoul(args[0].c_str(),NULL,16);
231
232 mmu_t mmu(mem,memsz);
233 switch(addr % 8)
234 {
235 case 0:
236 val = mmu.load_uint64(addr);
237 break;
238 case 4:
239 val = mmu.load_uint32(addr);
240 break;
241 case 2:
242 case 6:
243 val = mmu.load_uint16(addr);
244 break;
245 default:
246 val = mmu.load_uint8(addr);
247 break;
248 }
249 return val;
250 }
251
252 void sim_t::interactive_mem(const std::string& cmd, const std::vector<std::string>& args)
253 {
254 printf("0x%016llx\n",(unsigned long long)get_mem(args));
255 }
256
257 void sim_t::interactive_until(const std::string& cmd, const std::vector<std::string>& args)
258 {
259 if(args.size() < 3)
260 return;
261
262 std::string scmd = args[0];
263 reg_t val = strtol(args[args.size()-1].c_str(),NULL,16);
264 if(val == LONG_MAX)
265 val = strtoul(args[args.size()-1].c_str(),NULL,16);
266
267 std::vector<std::string> args2;
268 args2 = std::vector<std::string>(args.begin()+1,args.end()-1);
269
270 while(1)
271 {
272 reg_t current;
273 if(scmd == "reg")
274 current = get_reg(args2);
275 else if(scmd == "pc")
276 current = get_pc(args2);
277 else if(scmd == "mem")
278 current = get_mem(args2);
279 else if(scmd == "tohost")
280 current = get_tohost(args2);
281 else
282 return;
283
284 if(cmd == "until" && current == val)
285 break;
286 if(cmd == "while" && current != val)
287 break;
288
289 step_all(1,1,false);
290 }
291 }