Merge pull request #23 from vapier/master
[riscv-isa-sim.git] / riscv / interactive.cc
1 // See LICENSE for license details.
2
3 #include "decode.h"
4 #include "disasm.h"
5 #include "sim.h"
6 #include "htif.h"
7 #include <sys/mman.h>
8 #include <termios.h>
9 #include <map>
10 #include <iostream>
11 #include <climits>
12 #include <cinttypes>
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 #include <algorithm>
20
21 static std::string readline(int fd)
22 {
23 struct termios tios;
24 bool noncanonical = tcgetattr(fd, &tios) == 0 && (tios.c_lflag & ICANON) == 0;
25
26 std::string s;
27 for (char ch; read(fd, &ch, 1) == 1; )
28 {
29 if (ch == '\x7f')
30 {
31 if (s.empty())
32 continue;
33 s.erase(s.end()-1);
34
35 if (noncanonical && write(fd, "\b \b", 3) != 3)
36 ; // shut up gcc
37 }
38 else if (noncanonical && write(fd, &ch, 1) != 1)
39 ; // shut up gcc
40
41 if (ch == '\n')
42 break;
43 if (ch != '\x7f')
44 s += ch;
45 }
46 return s;
47 }
48
49 void sim_t::interactive()
50 {
51 typedef void (sim_t::*interactive_func)(const std::string&, const std::vector<std::string>&);
52 std::map<std::string,interactive_func> funcs;
53
54 funcs["r"] = &sim_t::interactive_run_noisy;
55 funcs["rs"] = &sim_t::interactive_run_silent;
56 funcs["reg"] = &sim_t::interactive_reg;
57 funcs["fregs"] = &sim_t::interactive_fregs;
58 funcs["fregd"] = &sim_t::interactive_fregd;
59 funcs["mem"] = &sim_t::interactive_mem;
60 funcs["str"] = &sim_t::interactive_str;
61 funcs["until"] = &sim_t::interactive_until;
62 funcs["while"] = &sim_t::interactive_until;
63 funcs["q"] = &sim_t::interactive_quit;
64
65 while (!htif->done())
66 {
67 std::cerr << ": " << std::flush;
68 std::string s = readline(2);
69
70 std::stringstream ss(s);
71 std::string cmd, tmp;
72 std::vector<std::string> args;
73
74 if (!(ss >> cmd))
75 {
76 set_procs_debug(true);
77 step(1);
78 continue;
79 }
80
81 while (ss >> tmp)
82 args.push_back(tmp);
83
84 try
85 {
86 if(funcs.count(cmd))
87 (this->*funcs[cmd])(cmd, args);
88 }
89 catch(trap_t t) {}
90 }
91 ctrlc_pressed = false;
92 }
93
94 void sim_t::interactive_run_noisy(const std::string& cmd, const std::vector<std::string>& args)
95 {
96 interactive_run(cmd,args,true);
97 }
98
99 void sim_t::interactive_run_silent(const std::string& cmd, const std::vector<std::string>& args)
100 {
101 interactive_run(cmd,args,false);
102 }
103
104 void sim_t::interactive_run(const std::string& cmd, const std::vector<std::string>& args, bool noisy)
105 {
106 size_t steps = args.size() ? atoll(args[0].c_str()) : -1;
107 ctrlc_pressed = false;
108 set_procs_debug(noisy);
109 for (size_t i = 0; i < steps && !ctrlc_pressed && !htif->done(); i++)
110 step(1);
111 }
112
113 void sim_t::interactive_quit(const std::string& cmd, const std::vector<std::string>& args)
114 {
115 exit(0);
116 }
117
118 reg_t sim_t::get_pc(const std::vector<std::string>& args)
119 {
120 if(args.size() != 1)
121 throw trap_illegal_instruction();
122
123 int p = atoi(args[0].c_str());
124 if(p >= (int)num_cores())
125 throw trap_illegal_instruction();
126
127 return procs[p]->state.pc;
128 }
129
130 reg_t sim_t::get_reg(const std::vector<std::string>& args)
131 {
132 if(args.size() != 2)
133 throw trap_illegal_instruction();
134
135 char* ptr;
136 unsigned long p = strtoul(args[0].c_str(), &ptr, 10);
137 if (*ptr || p >= num_cores())
138 throw trap_illegal_instruction();
139
140 unsigned long r = std::find(xpr_name, xpr_name + NXPR, args[1]) - xpr_name;
141 if (r == NXPR) {
142 r = strtoul(args[1].c_str(), &ptr, 10);
143 if (*ptr) {
144 #define DECLARE_CSR(name, number) if (args[1] == #name) return procs[p]->get_csr(number);
145 if (0) ;
146 #include "encoding.h"
147 else r = NXPR;
148 #undef DECLARE_CSR
149 }
150 }
151
152 if (r >= NXPR)
153 throw trap_illegal_instruction();
154
155 return procs[p]->state.XPR[r];
156 }
157
158 reg_t sim_t::get_freg(const std::vector<std::string>& args)
159 {
160 if(args.size() != 2)
161 throw trap_illegal_instruction();
162
163 int p = atoi(args[0].c_str());
164 int r = std::find(fpr_name, fpr_name + NFPR, args[1]) - fpr_name;
165 if (r == NFPR)
166 r = atoi(args[1].c_str());
167 if(p >= (int)num_cores() || r >= NFPR)
168 throw trap_illegal_instruction();
169
170 return procs[p]->state.FPR[r];
171 }
172
173 void sim_t::interactive_reg(const std::string& cmd, const std::vector<std::string>& args)
174 {
175 fprintf(stderr, "0x%016" PRIx64 "\n", get_reg(args));
176 }
177
178 union fpr
179 {
180 reg_t r;
181 float s;
182 double d;
183 };
184
185 void sim_t::interactive_fregs(const std::string& cmd, const std::vector<std::string>& args)
186 {
187 fpr f;
188 f.r = get_freg(args);
189 fprintf(stderr, "%g\n",f.s);
190 }
191
192 void sim_t::interactive_fregd(const std::string& cmd, const std::vector<std::string>& args)
193 {
194 fpr f;
195 f.r = get_freg(args);
196 fprintf(stderr, "%g\n",f.d);
197 }
198
199 reg_t sim_t::get_mem(const std::vector<std::string>& args)
200 {
201 if(args.size() != 1 && args.size() != 2)
202 throw trap_illegal_instruction();
203
204 std::string addr_str = args[0];
205 mmu_t* mmu = debug_mmu;
206 if(args.size() == 2)
207 {
208 int p = atoi(args[0].c_str());
209 if(p >= (int)num_cores())
210 throw trap_illegal_instruction();
211 mmu = procs[p]->get_mmu();
212 addr_str = args[1];
213 }
214
215 reg_t addr = strtol(addr_str.c_str(),NULL,16), val;
216 if(addr == LONG_MAX)
217 addr = strtoul(addr_str.c_str(),NULL,16);
218
219 switch(addr % 8)
220 {
221 case 0:
222 val = mmu->load_uint64(addr);
223 break;
224 case 4:
225 val = mmu->load_uint32(addr);
226 break;
227 case 2:
228 case 6:
229 val = mmu->load_uint16(addr);
230 break;
231 default:
232 val = mmu->load_uint8(addr);
233 break;
234 }
235 return val;
236 }
237
238 void sim_t::interactive_mem(const std::string& cmd, const std::vector<std::string>& args)
239 {
240 fprintf(stderr, "0x%016" PRIx64 "\n", get_mem(args));
241 }
242
243 void sim_t::interactive_str(const std::string& cmd, const std::vector<std::string>& args)
244 {
245 if(args.size() != 1)
246 throw trap_illegal_instruction();
247
248 reg_t addr = strtol(args[0].c_str(),NULL,16);
249
250 char ch;
251 while((ch = debug_mmu->load_uint8(addr++)))
252 putchar(ch);
253
254 putchar('\n');
255 }
256
257 void sim_t::interactive_until(const std::string& cmd, const std::vector<std::string>& args)
258 {
259 bool cmd_until = cmd == "until";
260
261 if(args.size() < 3)
262 return;
263
264 reg_t val = strtol(args[args.size()-1].c_str(),NULL,16);
265 if(val == LONG_MAX)
266 val = strtoul(args[args.size()-1].c_str(),NULL,16);
267
268 std::vector<std::string> args2;
269 args2 = std::vector<std::string>(args.begin()+1,args.end()-1);
270
271 auto func = args[0] == "reg" ? &sim_t::get_reg :
272 args[0] == "pc" ? &sim_t::get_pc :
273 args[0] == "mem" ? &sim_t::get_mem :
274 NULL;
275
276 if (func == NULL)
277 return;
278
279 ctrlc_pressed = false;
280
281 while (1)
282 {
283 try
284 {
285 reg_t current = (this->*func)(args2);
286
287 if (cmd_until == (current == val))
288 break;
289 if (ctrlc_pressed)
290 break;
291 }
292 catch (trap_t t) {}
293
294 set_procs_debug(false);
295 step(1);
296 }
297 }