1 // See LICENSE for license details.
21 processor_t
*sim_t::get_core(const std::string
& i
)
24 unsigned long p
= strtoul(i
.c_str(), &ptr
, 10);
25 if (*ptr
|| p
>= num_cores())
26 throw trap_illegal_instruction();
30 static std::string
readline(int fd
)
33 bool noncanonical
= tcgetattr(fd
, &tios
) == 0 && (tios
.c_lflag
& ICANON
) == 0;
36 for (char ch
; read(fd
, &ch
, 1) == 1; )
44 if (noncanonical
&& write(fd
, "\b \b", 3) != 3)
47 else if (noncanonical
&& write(fd
, &ch
, 1) != 1)
58 void sim_t::interactive()
60 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
61 std::map
<std::string
,interactive_func
> funcs
;
63 funcs
["run"] = &sim_t::interactive_run_noisy
;
64 funcs
["r"] = funcs
["run"];
65 funcs
["rs"] = &sim_t::interactive_run_silent
;
66 funcs
["reg"] = &sim_t::interactive_reg
;
67 funcs
["fregs"] = &sim_t::interactive_fregs
;
68 funcs
["fregd"] = &sim_t::interactive_fregd
;
69 funcs
["pc"] = &sim_t::interactive_pc
;
70 funcs
["mem"] = &sim_t::interactive_mem
;
71 funcs
["str"] = &sim_t::interactive_str
;
72 funcs
["until"] = &sim_t::interactive_until
;
73 funcs
["while"] = &sim_t::interactive_until
;
74 funcs
["quit"] = &sim_t::interactive_quit
;
75 funcs
["q"] = funcs
["quit"];
76 funcs
["help"] = &sim_t::interactive_help
;
77 funcs
["h"] = funcs
["help"];
81 std::cerr
<< ": " << std::flush
;
82 std::string s
= readline(2);
84 std::stringstream
ss(s
);
86 std::vector
<std::string
> args
;
90 set_procs_debug(true);
101 (this->*funcs
[cmd
])(cmd
, args
);
105 ctrlc_pressed
= false;
108 void sim_t::interactive_help(const std::string
& cmd
, const std::vector
<std::string
>& args
)
111 "Interactive commands:\n"
112 "reg <core> [reg] # Display [reg] (all if omitted) in <core>\n"
113 "fregs <core> <reg> # Display single precision <reg> in <core>\n"
114 "fregd <core> <reg> # Display double precision <reg> in <core>\n"
115 "pc <core> # Show current PC in <core>\n"
116 "mem <hex addr> # Show contents of physical memory\n"
117 "str <hex addr> # Show NUL-terminated C string\n"
118 "until reg <core> <reg> <val> # Stop when <reg> in <core> hits <val>\n"
119 "until pc <core> <val> # Stop when PC in <core> hits <val>\n"
120 "until mem <addr> <val> # Stop when memory <addr> becomes <val>\n"
121 "while reg <core> <reg> <val> # Run while <reg> in <core> is <val>\n"
122 "while pc <core> <val> # Run while PC in <core> is <val>\n"
123 "while mem <addr> <val> # Run while memory <addr> is <val>\n"
124 "run [count] # Resume noisy execution (until CTRL+C, or [count] insns)\n"
125 "r [count] Alias for run\n"
126 "rs [count] # Resume silent execution (until CTRL+C, or [count] insns)\n"
127 "quit # End the simulation\n"
129 "help # This screen!\n"
131 "Note: Hitting enter is the same as: run 1\n"
135 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
137 interactive_run(cmd
,args
,true);
140 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
142 interactive_run(cmd
,args
,false);
145 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
147 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
148 ctrlc_pressed
= false;
149 set_procs_debug(noisy
);
150 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
&& !htif
->done(); i
++)
154 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
159 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
162 throw trap_illegal_instruction();
164 processor_t
*p
= get_core(args
[0]);
168 void sim_t::interactive_pc(const std::string
& cmd
, const std::vector
<std::string
>& args
)
170 fprintf(stderr
, "0x%016" PRIx64
"\n", get_pc(args
));
173 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
176 throw trap_illegal_instruction();
178 processor_t
*p
= get_core(args
[0]);
180 unsigned long r
= std::find(xpr_name
, xpr_name
+ NXPR
, args
[1]) - xpr_name
;
183 r
= strtoul(args
[1].c_str(), &ptr
, 10);
185 #define DECLARE_CSR(name, number) if (args[1] == #name) return p->get_csr(number);
186 #include "encoding.h" // generates if's for all csrs
187 r
= NXPR
; // else case (csr name not found)
193 throw trap_illegal_instruction();
195 return p
->state
.XPR
[r
];
198 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
201 throw trap_illegal_instruction();
203 processor_t
*p
= get_core(args
[0]);
204 int r
= std::find(fpr_name
, fpr_name
+ NFPR
, args
[1]) - fpr_name
;
206 r
= atoi(args
[1].c_str());
208 throw trap_illegal_instruction();
210 return p
->state
.FPR
[r
];
213 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
215 if (args
.size() == 1) {
216 // Show all the regs!
217 processor_t
*p
= get_core(args
[0]);
219 for (int r
= 0; r
< NXPR
; ++r
) {
220 fprintf(stderr
, "%-4s: 0x%016" PRIx64
" ", xpr_name
[r
], p
->state
.XPR
[r
]);
221 if ((r
+ 1) % 4 == 0)
222 fprintf(stderr
, "\n");
225 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
235 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
238 f
.r
= get_freg(args
);
239 fprintf(stderr
, "%g\n",f
.s
);
242 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
245 f
.r
= get_freg(args
);
246 fprintf(stderr
, "%g\n",f
.d
);
249 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
251 if(args
.size() != 1 && args
.size() != 2)
252 throw trap_illegal_instruction();
254 std::string addr_str
= args
[0];
255 mmu_t
* mmu
= debug_mmu
;
258 processor_t
*p
= get_core(args
[0]);
263 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
265 addr
= strtoul(addr_str
.c_str(),NULL
,16);
270 val
= mmu
->load_uint64(addr
);
273 val
= mmu
->load_uint32(addr
);
277 val
= mmu
->load_uint16(addr
);
280 val
= mmu
->load_uint8(addr
);
286 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
288 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
291 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
294 throw trap_illegal_instruction();
296 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
299 while((ch
= debug_mmu
->load_uint8(addr
++)))
305 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
307 bool cmd_until
= cmd
== "until";
312 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
314 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
316 std::vector
<std::string
> args2
;
317 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
319 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
320 args
[0] == "pc" ? &sim_t::get_pc
:
321 args
[0] == "mem" ? &sim_t::get_mem
:
327 ctrlc_pressed
= false;
333 reg_t current
= (this->*func
)(args2
);
335 if (cmd_until
== (current
== val
))
342 set_procs_debug(false);