8 class memory_t
: public loader_t
11 memory_t(char* _mem
, size_t _size
) : mem(_mem
), size(_size
) {}
13 void write(size_t addr
, size_t bytes
, const void* src
= NULL
)
15 demand(addr
< size
&& addr
+ bytes
<= size
, "out of bounds!");
17 memcpy(mem
+addr
, src
, bytes
);
19 memset(mem
+addr
, 0, bytes
);
27 sim_t::sim_t(int _nprocs
, size_t _memsz
)
28 : nprocs(_nprocs
), memsz(_memsz
)
30 mem
= (char*)mmap(NULL
, memsz
, PROT_WRITE
, MAP_PRIVATE
| MAP_ANON
, -1, 0);
31 demand(mem
!= MAP_FAILED
, "couldn't allocate target machine's memory");
33 procs
= (processor_t
*)malloc(sizeof(*procs
)*nprocs
);
34 for(int i
= 0; i
< nprocs
; i
++)
35 new(&procs
[i
]) processor_t(i
,mem
,memsz
);
43 void sim_t::load_elf(const char* fn
)
45 memory_t
loader(mem
, memsz
);
46 ::load_elf(fn
,&loader
);
49 void sim_t::run(bool debug
)
54 step_all(100,100,false);
59 std::cin
.getline(s
,sizeof(s
)-1);
61 char* p
= strtok(s
," ");
64 interactive_run_noisy(std::vector
<std::string
>(1,"1"));
69 std::vector
<std::string
> args
;
70 while((p
= strtok(NULL
," ")))
74 typedef void (sim_t::*interactive_func
)(const std::vector
<std::string
>&);
75 std::map
<std::string
,interactive_func
> funcs
;
77 funcs
["r"] = &sim_t::interactive_run_noisy
;
78 funcs
["rs"] = &sim_t::interactive_run_silent
;
79 funcs
["rp"] = &sim_t::interactive_run_proc_noisy
;
80 funcs
["rps"] = &sim_t::interactive_run_proc_silent
;
81 funcs
["reg"] = &sim_t::interactive_reg
;
82 funcs
["mem"] = &sim_t::interactive_mem
;
83 funcs
["until"] = &sim_t::interactive_until
;
84 funcs
["q"] = &sim_t::interactive_quit
;
89 (this->*funcs
[cmd
])(args
);
96 void sim_t::step_all(size_t n
, size_t interleave
, bool noisy
)
98 for(size_t j
= 0; j
< n
; j
+=interleave
)
99 for(int i
= 0; i
< nprocs
; i
++)
100 procs
[i
].step(interleave
,noisy
);
103 void sim_t::interactive_run_noisy(const std::vector
<std::string
>& args
)
105 interactive_run(args
,true);
108 void sim_t::interactive_run_silent(const std::vector
<std::string
>& args
)
110 interactive_run(args
,false);
113 void sim_t::interactive_run(const std::vector
<std::string
>& args
, bool noisy
)
116 step_all(atoi(args
[0].c_str()),1,noisy
);
118 while(1) step_all(1,1,noisy
);
121 void sim_t::interactive_run_proc_noisy(const std::vector
<std::string
>& args
)
123 interactive_run_proc(args
,true);
126 void sim_t::interactive_run_proc_silent(const std::vector
<std::string
>& args
)
128 interactive_run_proc(args
,false);
131 void sim_t::interactive_run_proc(const std::vector
<std::string
>& a
, bool noisy
)
136 int p
= atoi(a
[0].c_str());
141 procs
[p
].step(atoi(a
[1].c_str()),noisy
);
143 while(1) procs
[p
].step(1,noisy
);
146 void sim_t::interactive_quit(const std::vector
<std::string
>& args
)
151 reg_t
sim_t::get_pc(const std::vector
<std::string
>& args
)
154 throw trap_illegal_instruction
;
156 int p
= atoi(args
[0].c_str());
158 throw trap_illegal_instruction
;
163 reg_t
sim_t::get_reg(const std::vector
<std::string
>& args
)
166 throw trap_illegal_instruction
;
168 int p
= atoi(args
[0].c_str());
169 int r
= atoi(args
[1].c_str());
170 if(p
>= nprocs
|| r
>= NGPR
)
171 throw trap_illegal_instruction
;
173 return procs
[p
].R
[r
];
176 void sim_t::interactive_reg(const std::vector
<std::string
>& args
)
178 printf("0x%016llx\n",(unsigned long long)get_reg(args
));
181 reg_t
sim_t::get_mem(const std::vector
<std::string
>& args
)
184 throw trap_illegal_instruction
;
186 reg_t addr
= strtol(args
[0].c_str(),NULL
,16), val
;
187 mmu_t
mmu(mem
,memsz
);
191 val
= mmu
.load_uint64(addr
);
194 val
= mmu
.load_uint32(addr
);
198 val
= mmu
.load_uint16(addr
);
201 val
= mmu
.load_uint8(addr
);
207 void sim_t::interactive_mem(const std::vector
<std::string
>& args
)
209 printf("0x%016llx\n",(unsigned long long)get_mem(args
));
212 void sim_t::interactive_until(const std::vector
<std::string
>& args
)
217 std::string cmd
= args
[0];
218 reg_t val
= strtol(args
[args
.size()-1].c_str(),NULL
,16);
220 std::vector
<std::string
> args2
;
221 args2
= std::vector
<std::string
>(args
.begin()+1,args
.end()-1);
227 current
= get_reg(args2
);
228 else if(args
[0] == "pc")
229 current
= get_pc(args2
);
230 else if(args
[0] == "mem")
231 current
= get_mem(args2
);