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
;
66 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
67 std::map
<std::string
,interactive_func
> funcs
;
69 funcs
["r"] = &sim_t::interactive_run_noisy
;
70 funcs
["rs"] = &sim_t::interactive_run_silent
;
71 funcs
["reg"] = &sim_t::interactive_reg
;
72 funcs
["fregs"] = &sim_t::interactive_fregs
;
73 funcs
["fregd"] = &sim_t::interactive_fregd
;
74 funcs
["mem"] = &sim_t::interactive_mem
;
75 funcs
["str"] = &sim_t::interactive_str
;
76 funcs
["until"] = &sim_t::interactive_until
;
77 funcs
["while"] = &sim_t::interactive_until
;
78 funcs
["q"] = &sim_t::interactive_quit
;
83 (this->*funcs
[cmd
])(cmd
, args
);
87 ctrlc_pressed
= false;
90 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
92 interactive_run(cmd
,args
,true);
95 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
97 interactive_run(cmd
,args
,false);
100 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
102 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
103 ctrlc_pressed
= false;
104 for (size_t i
= 0; i
< steps
&& !ctrlc_pressed
; i
++)
108 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
113 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
116 throw trap_illegal_instruction();
118 int p
= atoi(args
[0].c_str());
119 if(p
>= (int)num_cores())
120 throw trap_illegal_instruction();
122 return procs
[p
]->state
.pc
;
125 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
128 throw trap_illegal_instruction();
130 int p
= atoi(args
[0].c_str());
131 int r
= atoi(args
[1].c_str());
132 if(p
>= (int)num_cores() || r
>= NXPR
)
133 throw trap_illegal_instruction();
135 return procs
[p
]->state
.XPR
[r
];
138 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
141 throw trap_illegal_instruction();
143 int p
= atoi(args
[0].c_str());
144 int r
= atoi(args
[1].c_str());
145 if(p
>= (int)num_cores() || r
>= NFPR
)
146 throw trap_illegal_instruction();
148 return procs
[p
]->state
.FPR
[r
];
151 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
153 fprintf(stderr
, "0x%016" PRIx64
"\n", get_reg(args
));
163 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
166 f
.r
= get_freg(args
);
167 fprintf(stderr
, "%g\n",f
.s
);
170 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
173 f
.r
= get_freg(args
);
174 fprintf(stderr
, "%g\n",f
.d
);
177 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
179 if(args
.size() != 1 && args
.size() != 2)
180 throw trap_illegal_instruction();
182 std::string addr_str
= args
[0];
183 mmu_t
* mmu
= debug_mmu
;
186 int p
= atoi(args
[0].c_str());
187 if(p
>= (int)num_cores())
188 throw trap_illegal_instruction();
189 mmu
= procs
[p
]->get_mmu();
193 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
195 addr
= strtoul(addr_str
.c_str(),NULL
,16);
200 val
= mmu
->load_uint64(addr
);
203 val
= mmu
->load_uint32(addr
);
207 val
= mmu
->load_uint16(addr
);
210 val
= mmu
->load_uint8(addr
);
216 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
218 fprintf(stderr
, "0x%016" PRIx64
"\n", get_mem(args
));
221 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
224 throw trap_illegal_instruction();
226 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
229 while((ch
= debug_mmu
->load_uint8(addr
++)))
235 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
237 bool cmd_until
= cmd
== "until";
242 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
244 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
246 std::vector
<std::string
> args2
;
247 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
249 auto func
= args
[0] == "reg" ? &sim_t::get_reg
:
250 args
[0] == "pc" ? &sim_t::get_pc
:
251 args
[0] == "mem" ? &sim_t::get_mem
:
257 ctrlc_pressed
= false;
261 reg_t current
= (this->*func
)(args2
);
263 if (cmd_until
== (current
== val
))