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