1 // See LICENSE for license details.
21 static std::string
readline(int fd
)
24 bool noncanonical
= tcgetattr(fd
, &tios
) == 0 && (tios
.c_lflag
& ICANON
) == 0;
27 for (char ch
; read(fd
, &ch
, 1) == 1; )
35 if (noncanonical
&& write(fd
, "\b \b", 3) != 3)
38 else if (noncanonical
&& write(fd
, &ch
, 1) != 1)
49 void sim_t::interactive()
51 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
52 std::map
<std::string
,interactive_func
> funcs
;
54 funcs
["run"] = &sim_t::interactive_run_noisy
;
55 funcs
["r"] = funcs
["run"];
56 funcs
["rs"] = &sim_t::interactive_run_silent
;
57 funcs
["reg"] = &sim_t::interactive_reg
;
58 funcs
["fregs"] = &sim_t::interactive_fregs
;
59 funcs
["fregd"] = &sim_t::interactive_fregd
;
60 funcs
["mem"] = &sim_t::interactive_mem
;
61 funcs
["str"] = &sim_t::interactive_str
;
62 funcs
["until"] = &sim_t::interactive_until
;
63 funcs
["while"] = &sim_t::interactive_until
;
64 funcs
["quit"] = &sim_t::interactive_quit
;
65 funcs
["q"] = funcs
["quit"];
66 funcs
["help"] = &sim_t::interactive_help
;
67 funcs
["h"] = funcs
["help"];
71 std::cerr
<< ": " << std::flush
;
72 std::string s
= readline(2);
74 std::stringstream
ss(s
);
76 std::vector
<std::string
> args
;
80 set_procs_debug(true);
91 (this->*funcs
[cmd
])(cmd
, args
);
95 ctrlc_pressed
= false;
98 void sim_t::interactive_help(const std::string
& cmd
, const std::vector
<std::string
>& args
)
101 "Interactive commands:\n"
102 "reg <core> <reg> # Display <reg> in <core>\n"
103 "fregs <core> <reg> # Display single precision <reg> in <core>\n"
104 "fregd <core> <reg> # Display double precision <reg> in <core>\n"
105 "mem <hex addr> # Show contents of physical memory\n"
106 "str <hex addr> # Show NUL-terminated C string\n"
107 "until reg <core> <reg> <val> # Stop when <reg> in <core> hits <val>\n"
108 "until pc <core> <val> # Stop when PC in <core> hits <val>\n"
109 "until mem <addr> <val> # Stop when memory <addr> becomes <val>\n"
110 "while reg <core> <reg> <val> # Run while <reg> in <core> is <val>\n"
111 "while pc <core> <val> # Run while PC in <core> is <val>\n"
112 "while mem <addr> <val> # Run while memory <addr> is <val>\n"
113 "run [count] # Resume noisy execution (until CTRL+C, or [count] insns)\n"
114 "r [count] Alias for run\n"
115 "rs [count] # Resume silent execution (until CTRL+C, or [count] insns)\n"
116 "quit # End the simulation\n"
118 "help # This screen!\n"
120 "Note: Hitting enter is the same as: run 1\n"
124 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
126 interactive_run(cmd
,args
,true);
129 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
131 interactive_run(cmd
,args
,false);
134 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
136 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
137 ctrlc_pressed
= false;
138 set_procs_debug(noisy
);
139 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
&& !htif
->done(); i
++)
143 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
148 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
151 throw trap_illegal_instruction();
153 int p
= atoi(args
[0].c_str());
154 if(p
>= (int)num_cores())
155 throw trap_illegal_instruction();
157 return procs
[p
]->state
.pc
;
160 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
163 throw trap_illegal_instruction();
166 unsigned long p
= strtoul(args
[0].c_str(), &ptr
, 10);
167 if (*ptr
|| p
>= num_cores())
168 throw trap_illegal_instruction();
170 unsigned long r
= std::find(xpr_name
, xpr_name
+ NXPR
, args
[1]) - xpr_name
;
172 r
= strtoul(args
[1].c_str(), &ptr
, 10);
174 #define DECLARE_CSR(name, number) if (args[1] == #name) return procs[p]->get_csr(number);
176 #include "encoding.h"
183 throw trap_illegal_instruction();
185 return procs
[p
]->state
.XPR
[r
];
188 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
191 throw trap_illegal_instruction();
193 int p
= atoi(args
[0].c_str());
194 int r
= std::find(fpr_name
, fpr_name
+ NFPR
, args
[1]) - fpr_name
;
196 r
= atoi(args
[1].c_str());
197 if(p
>= (int)num_cores() || r
>= NFPR
)
198 throw trap_illegal_instruction();
200 return procs
[p
]->state
.FPR
[r
];
203 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
205 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
215 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
218 f
.r
= get_freg(args
);
219 fprintf(stderr
, "%g\n",f
.s
);
222 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
225 f
.r
= get_freg(args
);
226 fprintf(stderr
, "%g\n",f
.d
);
229 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
231 if(args
.size() != 1 && args
.size() != 2)
232 throw trap_illegal_instruction();
234 std::string addr_str
= args
[0];
235 mmu_t
* mmu
= debug_mmu
;
238 int p
= atoi(args
[0].c_str());
239 if(p
>= (int)num_cores())
240 throw trap_illegal_instruction();
241 mmu
= procs
[p
]->get_mmu();
245 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
247 addr
= strtoul(addr_str
.c_str(),NULL
,16);
252 val
= mmu
->load_uint64(addr
);
255 val
= mmu
->load_uint32(addr
);
259 val
= mmu
->load_uint16(addr
);
262 val
= mmu
->load_uint8(addr
);
268 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
270 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
273 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
276 throw trap_illegal_instruction();
278 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
281 while((ch
= debug_mmu
->load_uint8(addr
++)))
287 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
289 bool cmd_until
= cmd
== "until";
294 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
296 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
298 std::vector
<std::string
> args2
;
299 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
301 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
302 args
[0] == "pc" ? &sim_t::get_pc
:
303 args
[0] == "mem" ? &sim_t::get_mem
:
309 ctrlc_pressed
= false;
315 reg_t current
= (this->*func
)(args2
);
317 if (cmd_until
== (current
== val
))
324 set_procs_debug(false);