9 class memory_t
: public loader_t
12 memory_t(char* _mem
, size_t _size
) : mem(_mem
), size(_size
) {}
14 void write(size_t addr
, size_t bytes
, const void* src
= NULL
)
16 demand(addr
< size
&& addr
+ bytes
<= size
, "out of bounds!");
18 memcpy(mem
+addr
, src
, bytes
);
20 memset(mem
+addr
, 0, bytes
);
28 sim_t::sim_t(int _nprocs
, size_t _memsz
, appserver_link_t
* _applink
)
31 mem((char*)mmap64(NULL
, memsz
, PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0)),
32 procs(std::vector
<processor_t
>(_nprocs
,processor_t(this,mem
,memsz
)))
34 demand(mem
!= MAP_FAILED
, "couldn't allocate target machine's memory");
36 for(int i
= 0; i
< (int)procs
.size(); i
++)
44 void sim_t::load_elf(const char* fn
)
46 memory_t
loader(mem
, memsz
);
47 ::load_elf(fn
,&loader
);
49 void sim_t::set_tohost(reg_t val
)
55 reg_t
sim_t::get_fromhost()
58 applink
->wait_for_packet();
62 void sim_t::run(bool debug
)
64 applink
->wait_for_start();
69 step_all(100,100,false);
74 std::cin
.getline(s
,sizeof(s
)-1);
76 char* p
= strtok(s
," ");
79 interactive_run_noisy(std::vector
<std::string
>(1,"1"));
84 std::vector
<std::string
> args
;
85 while((p
= strtok(NULL
," ")))
89 typedef void (sim_t::*interactive_func
)(const std::vector
<std::string
>&);
90 std::map
<std::string
,interactive_func
> funcs
;
92 funcs
["r"] = &sim_t::interactive_run_noisy
;
93 funcs
["rs"] = &sim_t::interactive_run_silent
;
94 funcs
["rp"] = &sim_t::interactive_run_proc_noisy
;
95 funcs
["rps"] = &sim_t::interactive_run_proc_silent
;
96 funcs
["reg"] = &sim_t::interactive_reg
;
97 funcs
["mem"] = &sim_t::interactive_mem
;
98 funcs
["until"] = &sim_t::interactive_until
;
99 funcs
["q"] = &sim_t::interactive_quit
;
104 (this->*funcs
[cmd
])(args
);
111 void sim_t::step_all(size_t n
, size_t interleave
, bool noisy
)
113 for(size_t j
= 0; j
< n
; j
+=interleave
)
114 for(int i
= 0; i
< (int)procs
.size(); i
++)
115 procs
[i
].step(interleave
,noisy
);
118 void sim_t::interactive_run_noisy(const std::vector
<std::string
>& args
)
120 interactive_run(args
,true);
123 void sim_t::interactive_run_silent(const std::vector
<std::string
>& args
)
125 interactive_run(args
,false);
128 void sim_t::interactive_run(const std::vector
<std::string
>& args
, bool noisy
)
131 step_all(atoi(args
[0].c_str()),1,noisy
);
133 while(1) step_all(1,1,noisy
);
136 void sim_t::interactive_run_proc_noisy(const std::vector
<std::string
>& args
)
138 interactive_run_proc(args
,true);
141 void sim_t::interactive_run_proc_silent(const std::vector
<std::string
>& args
)
143 interactive_run_proc(args
,false);
146 void sim_t::interactive_run_proc(const std::vector
<std::string
>& a
, bool noisy
)
151 int p
= atoi(a
[0].c_str());
152 if(p
>= (int)procs
.size())
156 procs
[p
].step(atoi(a
[1].c_str()),noisy
);
158 while(1) procs
[p
].step(1,noisy
);
161 void sim_t::interactive_quit(const std::vector
<std::string
>& args
)
166 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
169 throw trap_illegal_instruction
;
171 int p
= atoi(args
[0].c_str());
172 if(p
>= (int)procs
.size())
173 throw trap_illegal_instruction
;
178 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
181 throw trap_illegal_instruction
;
183 int p
= atoi(args
[0].c_str());
184 int r
= atoi(args
[1].c_str());
185 if(p
>= (int)procs
.size() || r
>= NGPR
)
186 throw trap_illegal_instruction
;
188 return procs
[p
].R
[r
];
191 void sim_t::interactive_reg(const std::vector
<std::string
>& args
)
193 printf("0x%016llx\n",(unsigned long long)get_reg(args
));
196 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
199 throw trap_illegal_instruction
;
201 reg_t addr
= strtol(args
[0].c_str(),NULL
,16), val
;
202 mmu_t
mmu(mem
,memsz
);
206 val
= mmu
.load_uint64(addr
);
209 val
= mmu
.load_uint32(addr
);
213 val
= mmu
.load_uint16(addr
);
216 val
= mmu
.load_uint8(addr
);
222 void sim_t::interactive_mem(const std::vector
<std::string
>& args
)
224 printf("0x%016llx\n",(unsigned long long)get_mem(args
));
227 void sim_t::interactive_until(const std::vector
<std::string
>& args
)
232 std::string cmd
= args
[0];
233 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
235 std::vector
<std::string
> args2
;
236 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
242 current
= get_reg(args2
);
243 else if(args
[0] == "pc")
244 current
= get_pc(args2
);
245 else if(args
[0] == "mem")
246 current
= get_mem(args2
);