1 // See LICENSE for license details.
18 static std::string
readline(int fd
)
21 bool noncanonical
= tcgetattr(fd
, &tios
) == 0 && (tios
.c_lflag
& ICANON
) == 0;
24 for (char ch
; read(fd
, &ch
, 1) == 1; )
32 if (noncanonical
&& write(fd
, "\b \b", 3) != 3)
35 else if (noncanonical
&& write(fd
, &ch
, 1) != 1)
46 void sim_t::interactive()
50 std::cerr
<< ": " << std::flush
;
51 std::string s
= readline(2);
53 std::stringstream
ss(s
);
55 std::vector
<std::string
> args
;
59 set_procs_debug(true);
67 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
68 std::map
<std::string
,interactive_func
> funcs
;
70 funcs
["r"] = &sim_t::interactive_run_noisy
;
71 funcs
["rs"] = &sim_t::interactive_run_silent
;
72 funcs
["reg"] = &sim_t::interactive_reg
;
73 funcs
["fregs"] = &sim_t::interactive_fregs
;
74 funcs
["fregd"] = &sim_t::interactive_fregd
;
75 funcs
["mem"] = &sim_t::interactive_mem
;
76 funcs
["str"] = &sim_t::interactive_str
;
77 funcs
["until"] = &sim_t::interactive_until
;
78 funcs
["while"] = &sim_t::interactive_until
;
79 funcs
["q"] = &sim_t::interactive_quit
;
84 (this->*funcs
[cmd
])(cmd
, args
);
88 ctrlc_pressed
= false;
91 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
93 interactive_run(cmd
,args
,true);
96 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
98 interactive_run(cmd
,args
,false);
101 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
103 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
104 ctrlc_pressed
= false;
105 set_procs_debug(noisy
);
106 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
; i
++)
110 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
115 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
118 throw trap_illegal_instruction();
120 int p
= atoi(args
[0].c_str());
121 if(p
>= (int)num_cores())
122 throw trap_illegal_instruction();
124 return procs
[p
]->state
.pc
;
127 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
130 throw trap_illegal_instruction();
132 int p
= atoi(args
[0].c_str());
133 int r
= atoi(args
[1].c_str());
134 if(p
>= (int)num_cores() || r
>= NXPR
)
135 throw trap_illegal_instruction();
137 return procs
[p
]->state
.XPR
[r
];
140 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
143 throw trap_illegal_instruction();
145 int p
= atoi(args
[0].c_str());
146 int r
= atoi(args
[1].c_str());
147 if(p
>= (int)num_cores() || r
>= NFPR
)
148 throw trap_illegal_instruction();
150 return procs
[p
]->state
.FPR
[r
];
153 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
155 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
165 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
168 f
.r
= get_freg(args
);
169 fprintf(stderr
, "%g\n",f
.s
);
172 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
175 f
.r
= get_freg(args
);
176 fprintf(stderr
, "%g\n",f
.d
);
179 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
181 if(args
.size() != 1 && args
.size() != 2)
182 throw trap_illegal_instruction();
184 std::string addr_str
= args
[0];
185 mmu_t
* mmu
= debug_mmu
;
188 int p
= atoi(args
[0].c_str());
189 if(p
>= (int)num_cores())
190 throw trap_illegal_instruction();
191 mmu
= procs
[p
]->get_mmu();
195 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
197 addr
= strtoul(addr_str
.c_str(),NULL
,16);
202 val
= mmu
->load_uint64(addr
);
205 val
= mmu
->load_uint32(addr
);
209 val
= mmu
->load_uint16(addr
);
212 val
= mmu
->load_uint8(addr
);
218 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
220 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
223 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
226 throw trap_illegal_instruction();
228 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
231 while((ch
= debug_mmu
->load_uint8(addr
++)))
237 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
239 bool cmd_until
= cmd
== "until";
244 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
246 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
248 std::vector
<std::string
> args2
;
249 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
251 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
252 args
[0] == "pc" ? &sim_t::get_pc
:
253 args
[0] == "mem" ? &sim_t::get_mem
:
259 ctrlc_pressed
= false;
265 reg_t current
= (this->*func
)(args2
);
267 if (cmd_until
== (current
== val
))
274 set_procs_debug(false);