1 // See LICENSE for license details.
17 static std::string
readline()
23 assert(read(1, &ch
, 1) == 1);
32 assert(write(1, &ch
, 1) == 1);
40 void sim_t::interactive()
42 std::cout
<< ": " << std::flush
;
43 std::string s
= readline();
45 std::stringstream
ss(s
);
47 std::vector
<std::string
> args
;
50 interactive_run_noisy(std::string("r"), std::vector
<std::string
>(1,"1"));
56 typedef void (sim_t::*interactive_func
)(const std::string
&, const std::vector
<std::string
>&);
57 std::map
<std::string
,interactive_func
> funcs
;
59 funcs
["r"] = &sim_t::interactive_run_noisy
;
60 funcs
["rs"] = &sim_t::interactive_run_silent
;
61 funcs
["rp"] = &sim_t::interactive_run_proc_noisy
;
62 funcs
["rps"] = &sim_t::interactive_run_proc_silent
;
63 funcs
["reg"] = &sim_t::interactive_reg
;
64 funcs
["fregs"] = &sim_t::interactive_fregs
;
65 funcs
["fregd"] = &sim_t::interactive_fregd
;
66 funcs
["mem"] = &sim_t::interactive_mem
;
67 funcs
["str"] = &sim_t::interactive_str
;
68 funcs
["until"] = &sim_t::interactive_until
;
69 funcs
["while"] = &sim_t::interactive_until
;
70 funcs
["q"] = &sim_t::interactive_quit
;
75 (this->*funcs
[cmd
])(cmd
, args
);
80 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
82 interactive_run(cmd
,args
,true);
85 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
87 interactive_run(cmd
,args
,false);
90 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
92 size_t steps
= args
.size() ? atoll(args
[0].c_str()) : -1;
96 void sim_t::interactive_run_proc_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
98 interactive_run_proc(cmd
,args
,true);
101 void sim_t::interactive_run_proc_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
103 interactive_run_proc(cmd
,args
,false);
106 void sim_t::interactive_run_proc(const std::string
& cmd
, const std::vector
<std::string
>& a
, bool noisy
)
111 int p
= atoi(a
[0].c_str());
112 if(p
>= (int)num_cores())
115 size_t steps
= a
.size() > 1 ? atoll(a
[1].c_str()) : -1;
116 procs
[p
]->step(steps
, noisy
);
119 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
124 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
127 throw trap_illegal_instruction
;
129 int p
= atoi(args
[0].c_str());
130 if(p
>= (int)num_cores())
131 throw trap_illegal_instruction
;
136 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
139 throw trap_illegal_instruction
;
141 int p
= atoi(args
[0].c_str());
142 int r
= atoi(args
[1].c_str());
143 if(p
>= (int)num_cores() || r
>= NXPR
)
144 throw trap_illegal_instruction
;
146 return procs
[p
]->XPR
[r
];
149 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
152 throw trap_illegal_instruction
;
154 int p
= atoi(args
[0].c_str());
155 int r
= atoi(args
[1].c_str());
156 if(p
>= (int)num_cores() || r
>= NFPR
)
157 throw trap_illegal_instruction
;
159 return procs
[p
]->FPR
[r
];
162 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
164 printf("0x%016" PRIx64
"\n", get_reg(args
));
174 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
177 f
.r
= get_freg(args
);
181 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
184 f
.r
= get_freg(args
);
188 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
190 if(args
.size() != 1 && args
.size() != 2)
191 throw trap_illegal_instruction
;
193 std::string addr_str
= args
[0];
196 int p
= atoi(args
[0].c_str());
197 if(p
>= (int)num_cores())
198 throw trap_illegal_instruction
;
199 mmu
->set_sr(procs
[p
]->sr
);
200 mmu
->set_ptbr(procs
[p
]->mmu
.get_ptbr());
204 reg_t addr
= strtol(addr_str
.c_str(),NULL
,16), val
;
206 addr
= strtoul(addr_str
.c_str(),NULL
,16);
211 val
= mmu
->load_uint64(addr
);
214 val
= mmu
->load_uint32(addr
);
218 val
= mmu
->load_uint16(addr
);
221 val
= mmu
->load_uint8(addr
);
227 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
229 printf("0x%016" PRIx64
"\n", get_mem(args
));
232 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
235 throw trap_illegal_instruction
;
237 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
240 while((ch
= mmu
->load_uint8(addr
++)))
246 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
251 std::string scmd
= args
[0];
252 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
254 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
256 std::vector
<std::string
> args2
;
257 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
263 current
= get_reg(args2
);
264 else if(scmd
== "pc")
265 current
= get_pc(args2
);
266 else if(scmd
== "mem")
267 current
= get_mem(args2
);
271 if(cmd
== "until" && current
== val
)
273 if(cmd
== "while" && current
!= val
)