9 sim_t::sim_t(int _nprocs
, size_t _memsz
, appserver_link_t
* _applink
)
12 mem((char*)mmap64(NULL
, memsz
, PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0)),
13 procs(std::vector
<processor_t
>(_nprocs
,processor_t(this,mem
,memsz
)))
15 demand(mem
!= MAP_FAILED
, "couldn't allocate target machine's memory");
17 for(int i
= 0; i
< (int)procs
.size(); i
++)
27 void sim_t::set_tohost(reg_t val
)
31 applink
->wait_for_tohost();
34 reg_t
sim_t::get_fromhost()
36 applink
->wait_for_fromhost();
40 void sim_t::run(bool debug
)
42 applink
->wait_for_start();
47 step_all(100,100,false);
52 std::cin
.getline(s
,sizeof(s
)-1);
54 char* p
= strtok(s
," ");
57 interactive_run_noisy(std::string("r"), std::vector
<std::string
>(1,"1"));
62 std::vector
<std::string
> args
;
63 while((p
= strtok(NULL
," ")))
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
["rp"] = &sim_t::interactive_run_proc_noisy
;
73 funcs
["rps"] = &sim_t::interactive_run_proc_silent
;
74 funcs
["reg"] = &sim_t::interactive_reg
;
75 funcs
["fregs"] = &sim_t::interactive_fregs
;
76 funcs
["fregd"] = &sim_t::interactive_fregd
;
77 funcs
["mem"] = &sim_t::interactive_mem
;
78 funcs
["str"] = &sim_t::interactive_str
;
79 funcs
["until"] = &sim_t::interactive_until
;
80 funcs
["while"] = &sim_t::interactive_until
;
81 funcs
["q"] = &sim_t::interactive_quit
;
86 (this->*funcs
[cmd
])(cmd
, args
);
93 void sim_t::step_all(size_t n
, size_t interleave
, bool noisy
)
95 for(size_t j
= 0; j
< n
; j
+=interleave
)
96 for(int i
= 0; i
< (int)procs
.size(); i
++)
97 procs
[i
].step(interleave
,noisy
);
100 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
102 interactive_run(cmd
,args
,true);
105 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
107 interactive_run(cmd
,args
,false);
110 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
113 step_all(atoll(args
[0].c_str()),1,noisy
);
115 while(1) step_all(1,1,noisy
);
118 void sim_t::interactive_run_proc_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
120 interactive_run_proc(cmd
,args
,true);
123 void sim_t::interactive_run_proc_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
125 interactive_run_proc(cmd
,args
,false);
128 void sim_t::interactive_run_proc(const std::string
& cmd
, const std::vector
<std::string
>& a
, bool noisy
)
133 int p
= atoi(a
[0].c_str());
134 if(p
>= (int)procs
.size())
138 procs
[p
].step(atoi(a
[1].c_str()),noisy
);
140 while(1) procs
[p
].step(1,noisy
);
143 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
148 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
151 throw trap_illegal_instruction
;
153 int p
= atoi(args
[0].c_str());
154 if(p
>= (int)procs
.size())
155 throw trap_illegal_instruction
;
160 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
163 throw trap_illegal_instruction
;
165 int p
= atoi(args
[0].c_str());
166 int r
= atoi(args
[1].c_str());
167 if(p
>= (int)procs
.size() || r
>= NXPR
)
168 throw trap_illegal_instruction
;
170 return procs
[p
].XPR
[r
];
173 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
176 throw trap_illegal_instruction
;
178 int p
= atoi(args
[0].c_str());
179 int r
= atoi(args
[1].c_str());
180 if(p
>= (int)procs
.size() || r
>= NFPR
)
181 throw trap_illegal_instruction
;
183 return procs
[p
].FPR
[r
];
186 reg_t
sim_t::get_tohost(const std::vector
<std::string
>& args
)
189 throw trap_illegal_instruction
;
191 int p
= atoi(args
[0].c_str());
192 if(p
>= (int)procs
.size())
193 throw trap_illegal_instruction
;
195 return procs
[p
].tohost
;
198 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
200 printf("0x%016llx\n",(unsigned long long)get_reg(args
));
210 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
213 f
.r
= get_freg(args
);
217 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
220 f
.r
= get_freg(args
);
224 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
227 throw trap_illegal_instruction
;
229 reg_t addr
= strtol(args
[0].c_str(),NULL
,16), val
;
231 addr
= strtoul(args
[0].c_str(),NULL
,16);
233 mmu_t
mmu(mem
,memsz
);
237 val
= mmu
.load_uint64(addr
);
240 val
= mmu
.load_uint32(addr
);
244 val
= mmu
.load_uint16(addr
);
247 val
= mmu
.load_uint8(addr
);
253 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
255 printf("0x%016llx\n",(unsigned long long)get_mem(args
));
258 void sim_t::interactive_str(const std::string
& cmd
, const std::vector
<std::string
>& args
)
261 throw trap_illegal_instruction
;
263 reg_t addr
= strtol(args
[0].c_str(),NULL
,16);
265 mmu_t
mmu(mem
,memsz
);
268 while((ch
= mmu
.load_uint8(addr
++)))
274 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
279 std::string scmd
= args
[0];
280 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
282 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
284 std::vector
<std::string
> args2
;
285 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
291 current
= get_reg(args2
);
292 else if(scmd
== "pc")
293 current
= get_pc(args2
);
294 else if(scmd
== "mem")
295 current
= get_mem(args2
);
296 else if(scmd
== "tohost")
297 current
= get_tohost(args2
);
301 if(cmd
== "until" && current
== val
)
303 if(cmd
== "while" && current
!= val
)