1 // See LICENSE for license details.
21 DECLARE_TRAP(-1, interactive
)
23 processor_t
*sim_t::get_core(const std::string
& i
)
26 unsigned long p
= strtoul(i
.c_str(), &ptr
, 10);
27 if (*ptr
|| p
>= procs
.size())
28 throw trap_interactive();
32 static std::string
readline(int fd
)
35 bool noncanonical
= tcgetattr(fd
, &tios
) == 0 && (tios
.c_lflag
& ICANON
) == 0;
38 for (char ch
; read(fd
, &ch
, 1) == 1; )
46 if (noncanonical
&& write(fd
, "\b \b", 3) != 3)
49 else if (noncanonical
&& write(fd
, &ch
, 1) != 1)
60 void sim_t::interactive()
62 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
63 std::map
<std::string
,interactive_func
> funcs
;
65 funcs
["run"] = &sim_t::interactive_run_noisy
;
66 funcs
["r"] = funcs
["run"];
67 funcs
["rs"] = &sim_t::interactive_run_silent
;
68 funcs
["reg"] = &sim_t::interactive_reg
;
69 funcs
["freg"] = &sim_t::interactive_freg
;
70 funcs
["fregs"] = &sim_t::interactive_fregs
;
71 funcs
["fregd"] = &sim_t::interactive_fregd
;
72 funcs
["pc"] = &sim_t::interactive_pc
;
73 funcs
["mem"] = &sim_t::interactive_mem
;
74 funcs
["str"] = &sim_t::interactive_str
;
75 funcs
["until"] = &sim_t::interactive_until
;
76 funcs
["while"] = &sim_t::interactive_until
;
77 funcs
["quit"] = &sim_t::interactive_quit
;
78 funcs
["q"] = funcs
["quit"];
79 funcs
["help"] = &sim_t::interactive_help
;
80 funcs
["h"] = funcs
["help"];
84 std::cerr
<< ": " << std::flush
;
85 std::string s
= readline(2);
87 std::stringstream
ss(s
);
89 std::vector
<std::string
> args
;
93 set_procs_debug(true);
104 (this->*funcs
[cmd
])(cmd
, args
);
106 fprintf(stderr
, "Unknown command %s\n", cmd
.c_str());
110 ctrlc_pressed
= false;
113 void sim_t::interactive_help(const std::string
& cmd
, const std::vector
<std::string
>& args
)
116 "Interactive commands:\n"
117 "reg <core> [reg] # Display [reg] (all if omitted) in <core>\n"
118 "fregs <core> <reg> # Display single precision <reg> in <core>\n"
119 "fregd <core> <reg> # Display double precision <reg> in <core>\n"
120 "pc <core> # Show current PC in <core>\n"
121 "mem <hex addr> # Show contents of physical memory\n"
122 "str <hex addr> # Show NUL-terminated C string\n"
123 "until reg <core> <reg> <val> # Stop when <reg> in <core> hits <val>\n"
124 "until pc <core> <val> # Stop when PC in <core> hits <val>\n"
125 "until mem <addr> <val> # Stop when memory <addr> becomes <val>\n"
126 "while reg <core> <reg> <val> # Run while <reg> in <core> is <val>\n"
127 "while pc <core> <val> # Run while PC in <core> is <val>\n"
128 "while mem <addr> <val> # Run while memory <addr> is <val>\n"
129 "run [count] # Resume noisy execution (until CTRL+C, or [count] insns)\n"
130 "r [count] Alias for run\n"
131 "rs [count] # Resume silent execution (until CTRL+C, or [count] insns)\n"
132 "quit # End the simulation\n"
134 "help # This screen!\n"
136 "Note: Hitting enter is the same as: run 1\n"
140 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
142 interactive_run(cmd
,args
,true);
145 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
147 interactive_run(cmd
,args
,false);
150 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
152 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
153 ctrlc_pressed
= false;
154 set_procs_debug(noisy
);
155 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
&& !done(); i
++)
159 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
164 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
167 throw trap_interactive();
169 processor_t
*p
= get_core(args
[0]);
173 void sim_t::interactive_pc(const std::string
& cmd
, const std::vector
<std::string
>& args
)
175 fprintf(stderr
, "0x%016" PRIx64
"\n", get_pc(args
));
178 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
181 throw trap_interactive();
183 processor_t
*p
= get_core(args
[0]);
185 unsigned long r
= std::find(xpr_name
, xpr_name
+ NXPR
, args
[1]) - xpr_name
;
188 r
= strtoul(args
[1].c_str(), &ptr
, 10);
190 #define DECLARE_CSR(name, number) if (args[1] == #name) return p->get_csr(number);
191 #include "encoding.h" // generates if's for all csrs
192 r
= NXPR
; // else case (csr name not found)
198 throw trap_interactive();
200 return p
->state
.XPR
[r
];
203 freg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
206 throw trap_interactive();
208 processor_t
*p
= get_core(args
[0]);
209 int r
= std::find(fpr_name
, fpr_name
+ NFPR
, args
[1]) - fpr_name
;
211 r
= atoi(args
[1].c_str());
213 throw trap_interactive();
215 return p
->state
.FPR
[r
];
218 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
220 if (args
.size() == 1) {
221 // Show all the regs!
222 processor_t
*p
= get_core(args
[0]);
224 for (int r
= 0; r
< NXPR
; ++r
) {
225 fprintf(stderr
, "%-4s: 0x%016" PRIx64
" ", xpr_name
[r
], p
->state
.XPR
[r
]);
226 if ((r
+ 1) % 4 == 0)
227 fprintf(stderr
, "\n");
230 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
240 void sim_t::interactive_freg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
242 fprintf(stderr
, "0x%016" PRIx64
"\n", get_freg(args
).v
);
245 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
248 f
.r
= get_freg(args
);
249 fprintf(stderr
, "%g\n",f
.s
);
252 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
255 f
.r
= get_freg(args
);
256 fprintf(stderr
, "%g\n",f
.d
);
259 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
261 if(args
.size() != 1 && args
.size() != 2)
262 throw trap_interactive();
264 std::string addr_str
= args
[0];
265 mmu_t
* mmu
= debug_mmu
;
268 processor_t
*p
= get_core(args
[0]);
273 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
275 addr
= strtoul(addr_str
.c_str(),NULL
,16);
280 val
= mmu
->load_uint64(addr
);
283 val
= mmu
->load_uint32(addr
);
287 val
= mmu
->load_uint16(addr
);
290 val
= mmu
->load_uint8(addr
);
296 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
298 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
301 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
304 throw trap_interactive();
306 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
309 while((ch
= debug_mmu
->load_uint8(addr
++)))
315 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
317 bool cmd_until
= cmd
== "until";
322 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
324 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
326 std::vector
<std::string
> args2
;
327 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
329 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
330 args
[0] == "pc" ? &sim_t::get_pc
:
331 args
[0] == "mem" ? &sim_t::get_mem
:
337 ctrlc_pressed
= false;
343 reg_t current
= (this->*func
)(args2
);
345 if (cmd_until
== (current
== val
))
352 set_procs_debug(false);