1 // See LICENSE for license details.
22 processor_t
*sim_t::get_core(const std::string
& i
)
25 unsigned long p
= strtoul(i
.c_str(), &ptr
, 10);
26 if (*ptr
|| p
>= num_cores())
27 throw trap_illegal_instruction();
31 static std::string
readline(int fd
)
34 bool noncanonical
= tcgetattr(fd
, &tios
) == 0 && (tios
.c_lflag
& ICANON
) == 0;
37 for (char ch
; read(fd
, &ch
, 1) == 1; )
45 if (noncanonical
&& write(fd
, "\b \b", 3) != 3)
48 else if (noncanonical
&& write(fd
, &ch
, 1) != 1)
59 void sim_t::interactive()
61 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
62 std::map
<std::string
,interactive_func
> funcs
;
64 funcs
["run"] = &sim_t::interactive_run_noisy
;
65 funcs
["r"] = funcs
["run"];
66 funcs
["rs"] = &sim_t::interactive_run_silent
;
67 funcs
["reg"] = &sim_t::interactive_reg
;
68 funcs
["fregs"] = &sim_t::interactive_fregs
;
69 funcs
["fregd"] = &sim_t::interactive_fregd
;
70 funcs
["pc"] = &sim_t::interactive_pc
;
71 funcs
["mem"] = &sim_t::interactive_mem
;
72 funcs
["str"] = &sim_t::interactive_str
;
73 funcs
["until"] = &sim_t::interactive_until
;
74 funcs
["while"] = &sim_t::interactive_until
;
75 funcs
["quit"] = &sim_t::interactive_quit
;
76 funcs
["q"] = funcs
["quit"];
77 funcs
["help"] = &sim_t::interactive_help
;
78 funcs
["h"] = funcs
["help"];
82 std::cerr
<< ": " << std::flush
;
83 std::string s
= readline(2);
85 std::stringstream
ss(s
);
87 std::vector
<std::string
> args
;
91 set_procs_debug(true);
102 (this->*funcs
[cmd
])(cmd
, args
);
104 fprintf(stderr
, "Unknown command %s\n", cmd
.c_str());
108 ctrlc_pressed
= false;
111 void sim_t::interactive_help(const std::string
& cmd
, const std::vector
<std::string
>& args
)
114 "Interactive commands:\n"
115 "reg <core> [reg] # Display [reg] (all if omitted) in <core>\n"
116 "fregs <core> <reg> # Display single precision <reg> in <core>\n"
117 "fregd <core> <reg> # Display double precision <reg> in <core>\n"
118 "pc <core> # Show current PC in <core>\n"
119 "mem <hex addr> # Show contents of physical memory\n"
120 "str <hex addr> # Show NUL-terminated C string\n"
121 "until reg <core> <reg> <val> # Stop when <reg> in <core> hits <val>\n"
122 "until pc <core> <val> # Stop when PC in <core> hits <val>\n"
123 "until mem <addr> <val> # Stop when memory <addr> becomes <val>\n"
124 "while reg <core> <reg> <val> # Run while <reg> in <core> is <val>\n"
125 "while pc <core> <val> # Run while PC in <core> is <val>\n"
126 "while mem <addr> <val> # Run while memory <addr> is <val>\n"
127 "run [count] # Resume noisy execution (until CTRL+C, or [count] insns)\n"
128 "r [count] Alias for run\n"
129 "rs [count] # Resume silent execution (until CTRL+C, or [count] insns)\n"
130 "quit # End the simulation\n"
132 "help # This screen!\n"
134 "Note: Hitting enter is the same as: run 1\n"
138 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
140 interactive_run(cmd
,args
,true);
143 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
145 interactive_run(cmd
,args
,false);
148 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
150 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
151 ctrlc_pressed
= false;
152 set_procs_debug(noisy
);
153 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
&& !htif
->done(); i
++)
157 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
162 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
165 throw trap_illegal_instruction();
167 processor_t
*p
= get_core(args
[0]);
171 void sim_t::interactive_pc(const std::string
& cmd
, const std::vector
<std::string
>& args
)
173 fprintf(stderr
, "0x%016" PRIx64
"\n", get_pc(args
));
176 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
179 throw trap_illegal_instruction();
181 processor_t
*p
= get_core(args
[0]);
183 unsigned long r
= std::find(xpr_name
, xpr_name
+ NXPR
, args
[1]) - xpr_name
;
186 r
= strtoul(args
[1].c_str(), &ptr
, 10);
188 #define DECLARE_CSR(name, number) if (args[1] == #name) return p->get_csr(number);
189 #include "encoding.h" // generates if's for all csrs
190 r
= NXPR
; // else case (csr name not found)
196 throw trap_illegal_instruction();
198 return p
->state
.XPR
[r
];
201 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
204 throw trap_illegal_instruction();
206 processor_t
*p
= get_core(args
[0]);
207 int r
= std::find(fpr_name
, fpr_name
+ NFPR
, args
[1]) - fpr_name
;
209 r
= atoi(args
[1].c_str());
211 throw trap_illegal_instruction();
213 return p
->state
.FPR
[r
];
216 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
218 if (args
.size() == 1) {
219 // Show all the regs!
220 processor_t
*p
= get_core(args
[0]);
222 for (int r
= 0; r
< NXPR
; ++r
) {
223 fprintf(stderr
, "%-4s: 0x%016" PRIx64
" ", xpr_name
[r
], p
->state
.XPR
[r
]);
224 if ((r
+ 1) % 4 == 0)
225 fprintf(stderr
, "\n");
228 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
238 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
241 f
.r
= get_freg(args
);
242 fprintf(stderr
, "%g\n",f
.s
);
245 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
248 f
.r
= get_freg(args
);
249 fprintf(stderr
, "%g\n",f
.d
);
252 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
254 if(args
.size() != 1 && args
.size() != 2)
255 throw trap_illegal_instruction();
257 std::string addr_str
= args
[0];
258 mmu_t
* mmu
= debug_mmu
;
261 processor_t
*p
= get_core(args
[0]);
266 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
268 addr
= strtoul(addr_str
.c_str(),NULL
,16);
273 val
= mmu
->load_uint64(addr
);
276 val
= mmu
->load_uint32(addr
);
280 val
= mmu
->load_uint16(addr
);
283 val
= mmu
->load_uint8(addr
);
289 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
291 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
294 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
297 throw trap_illegal_instruction();
299 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
302 while((ch
= debug_mmu
->load_uint8(addr
++)))
308 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
310 bool cmd_until
= cmd
== "until";
315 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
317 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
319 std::vector
<std::string
> args2
;
320 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
322 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
323 args
[0] == "pc" ? &sim_t::get_pc
:
324 args
[0] == "mem" ? &sim_t::get_mem
:
330 ctrlc_pressed
= false;
336 reg_t current
= (this->*func
)(args2
);
338 if (cmd_until
== (current
== val
))
345 set_procs_debug(false);