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
["until"] = &sim_t::interactive_until
;
79 funcs
["while"] = &sim_t::interactive_until
;
80 funcs
["q"] = &sim_t::interactive_quit
;
85 (this->*funcs
[cmd
])(cmd
, args
);
92 void sim_t::step_all(size_t n
, size_t interleave
, bool noisy
)
94 for(size_t j
= 0; j
< n
; j
+=interleave
)
95 for(int i
= 0; i
< (int)procs
.size(); i
++)
96 procs
[i
].step(interleave
,noisy
);
99 void sim_t::interactive_run_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
101 interactive_run(cmd
,args
,true);
104 void sim_t::interactive_run_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
106 interactive_run(cmd
,args
,false);
109 void sim_t::interactive_run(const std::string
& cmd
, const std::vector
<std::string
>& args
, bool noisy
)
112 step_all(atoll(args
[0].c_str()),1,noisy
);
114 while(1) step_all(1,1,noisy
);
117 void sim_t::interactive_run_proc_noisy(const std::string
& cmd
, const std::vector
<std::string
>& args
)
119 interactive_run_proc(cmd
,args
,true);
122 void sim_t::interactive_run_proc_silent(const std::string
& cmd
, const std::vector
<std::string
>& args
)
124 interactive_run_proc(cmd
,args
,false);
127 void sim_t::interactive_run_proc(const std::string
& cmd
, const std::vector
<std::string
>& a
, bool noisy
)
132 int p
= atoi(a
[0].c_str());
133 if(p
>= (int)procs
.size())
137 procs
[p
].step(atoi(a
[1].c_str()),noisy
);
139 while(1) procs
[p
].step(1,noisy
);
142 void sim_t::interactive_quit(const std::string
& cmd
, const std::vector
<std::string
>& args
)
147 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
150 throw trap_illegal_instruction
;
152 int p
= atoi(args
[0].c_str());
153 if(p
>= (int)procs
.size())
154 throw trap_illegal_instruction
;
159 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
162 throw trap_illegal_instruction
;
164 int p
= atoi(args
[0].c_str());
165 int r
= atoi(args
[1].c_str());
166 if(p
>= (int)procs
.size() || r
>= NXPR
)
167 throw trap_illegal_instruction
;
169 return procs
[p
].XPR
[r
];
172 reg_t
sim_t::get_freg(const std::vector
<std::string
>& args
)
175 throw trap_illegal_instruction
;
177 int p
= atoi(args
[0].c_str());
178 int r
= atoi(args
[1].c_str());
179 if(p
>= (int)procs
.size() || r
>= NFPR
)
180 throw trap_illegal_instruction
;
182 return procs
[p
].FPR
[r
];
185 reg_t
sim_t::get_tohost(const std::vector
<std::string
>& args
)
188 throw trap_illegal_instruction
;
190 int p
= atoi(args
[0].c_str());
191 if(p
>= (int)procs
.size())
192 throw trap_illegal_instruction
;
194 return procs
[p
].tohost
;
197 void sim_t::interactive_reg(const std::string
& cmd
, const std::vector
<std::string
>& args
)
199 printf("0x%016llx\n",(unsigned long long)get_reg(args
));
209 void sim_t::interactive_fregs(const std::string
& cmd
, const std::vector
<std::string
>& args
)
212 f
.r
= get_freg(args
);
216 void sim_t::interactive_fregd(const std::string
& cmd
, const std::vector
<std::string
>& args
)
219 f
.r
= get_freg(args
);
223 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
226 throw trap_illegal_instruction
;
228 reg_t addr
= strtol(args
[0].c_str(),NULL
,16), val
;
230 addr
= strtoul(args
[0].c_str(),NULL
,16);
232 mmu_t
mmu(mem
,memsz
);
236 val
= mmu
.load_uint64(addr
);
239 val
= mmu
.load_uint32(addr
);
243 val
= mmu
.load_uint16(addr
);
246 val
= mmu
.load_uint8(addr
);
252 void sim_t::interactive_mem(const std::string
& cmd
, const std::vector
<std::string
>& args
)
254 printf("0x%016llx\n",(unsigned long long)get_mem(args
));
257 void sim_t::interactive_until(const std::string
& cmd
, const std::vector
<std::string
>& args
)
262 std::string scmd
= args
[0];
263 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
265 val
= strtoul(args
[args
.size()-1].c_str(),NULL
,16);
267 std::vector
<std::string
> args2
;
268 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
274 current
= get_reg(args2
);
275 else if(scmd
== "pc")
276 current
= get_pc(args2
);
277 else if(scmd
== "mem")
278 current
= get_mem(args2
);
279 else if(scmd
== "tohost")
280 current
= get_tohost(args2
);
284 if(cmd
== "until" && current
== val
)
286 if(cmd
== "while" && current
!= val
)