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()
53 std::cerr
<< ": " << std::flush
;
54 std::string s
= readline(2);
56 std::stringstream
ss(s
);
58 std::vector
<std::string
> args
;
62 set_procs_debug(true);
70 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
71 std::map
<std::string
,interactive_func
> funcs
;
73 funcs
["r"] = &sim_t::interactive_run_noisy
;
74 funcs
["rs"] = &sim_t::interactive_run_silent
;
75 funcs
["reg"] = &sim_t::interactive_reg
;
76 funcs
["fregs"] = &sim_t::interactive_fregs
;
77 funcs
["fregd"] = &sim_t::interactive_fregd
;
78 funcs
["mem"] = &sim_t::interactive_mem
;
79 funcs
["str"] = &sim_t::interactive_str
;
80 funcs
["until"] = &sim_t::interactive_until
;
81 funcs
["while"] = &sim_t::interactive_until
;
82 funcs
["q"] = &sim_t::interactive_quit
;
87 (this->*funcs
[cmd
])(cmd
, args
);
91 ctrlc_pressed
= false;
94 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
96 interactive_run(cmd
,args
,true);
99 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
101 interactive_run(cmd
,args
,false);
104 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
106 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
107 ctrlc_pressed
= false;
108 set_procs_debug(noisy
);
109 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
&& !htif
->done(); i
++)
113 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
118 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
121 throw trap_illegal_instruction();
123 int p
= atoi(args
[0].c_str());
124 if(p
>= (int)num_cores())
125 throw trap_illegal_instruction();
127 return procs
[p
]->state
.pc
;
130 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
133 throw trap_illegal_instruction();
135 int p
= atoi(args
[0].c_str());
136 int r
= std::find(xpr_name
, xpr_name
+ NXPR
, args
[1]) - xpr_name
;
138 r
= atoi(args
[1].c_str());
139 if(p
>= (int)num_cores() || r
>= NXPR
)
140 throw trap_illegal_instruction();
142 return procs
[p
]->state
.XPR
[r
];
145 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
148 throw trap_illegal_instruction();
150 int p
= atoi(args
[0].c_str());
151 int r
= std::find(fpr_name
, fpr_name
+ NFPR
, args
[1]) - fpr_name
;
153 r
= atoi(args
[1].c_str());
154 if(p
>= (int)num_cores() || r
>= NFPR
)
155 throw trap_illegal_instruction();
157 return procs
[p
]->state
.FPR
[r
];
160 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
162 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
172 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
175 f
.r
= get_freg(args
);
176 fprintf(stderr
, "%g\n",f
.s
);
179 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
182 f
.r
= get_freg(args
);
183 fprintf(stderr
, "%g\n",f
.d
);
186 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
188 if(args
.size() != 1 && args
.size() != 2)
189 throw trap_illegal_instruction();
191 std::string addr_str
= args
[0];
192 mmu_t
* mmu
= debug_mmu
;
195 int p
= atoi(args
[0].c_str());
196 if(p
>= (int)num_cores())
197 throw trap_illegal_instruction();
198 mmu
= procs
[p
]->get_mmu();
202 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
204 addr
= strtoul(addr_str
.c_str(),NULL
,16);
209 val
= mmu
->load_uint64(addr
);
212 val
= mmu
->load_uint32(addr
);
216 val
= mmu
->load_uint16(addr
);
219 val
= mmu
->load_uint8(addr
);
225 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
227 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
230 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
233 throw trap_illegal_instruction();
235 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
238 while((ch
= debug_mmu
->load_uint8(addr
++)))
244 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
246 bool cmd_until
= cmd
== "until";
251 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
253 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
255 std::vector
<std::string
> args2
;
256 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
258 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
259 args
[0] == "pc" ? &sim_t::get_pc
:
260 args
[0] == "mem" ? &sim_t::get_mem
:
266 ctrlc_pressed
= false;
272 reg_t current
= (this->*func
)(args2
);
274 if (cmd_until
== (current
== val
))
281 set_procs_debug(false);