1 // See LICENSE for license details.
12 volatile bool ctrlc_pressed
= false;
13 static void handle_signal(int sig
)
18 signal(sig
, &handle_signal
);
21 sim_t::sim_t(size_t nprocs
, size_t mem_mb
, const std::vector
<std::string
>& args
)
22 : htif(new htif_isasim_t(this, args
)), procs(std::max(nprocs
, size_t(1))),
23 current_step(0), current_proc(0), debug(false)
25 signal(SIGINT
, &handle_signal
);
26 // allocate target machine's memory, shrinking it as necessary
27 // until the allocation succeeds
28 size_t memsz0
= (size_t)mem_mb
<< 20;
29 size_t quantum
= 1L << 20;
31 memsz0
= 1L << (sizeof(size_t) == 8 ? 32 : 30);
34 while ((mem
= (char*)calloc(1, memsz
)) == NULL
)
35 memsz
= memsz
*10/11/quantum
*quantum
;
38 fprintf(stderr
, "warning: only got %lu bytes of target mem (wanted %lu)\n",
39 (unsigned long)memsz
, (unsigned long)memsz0
);
41 debug_mmu
= new mmu_t(mem
, memsz
);
43 for (size_t i
= 0; i
< procs
.size(); i
++) {
44 procs
[i
] = new processor_t(this, new mmu_t(mem
, memsz
), i
);
51 for (size_t i
= 0; i
< procs
.size(); i
++)
53 mmu_t
* pmmu
= procs
[i
]->get_mmu();
61 void sim_t::send_ipi(reg_t who
)
63 if (who
< procs
.size())
64 procs
[who
]->deliver_ipi();
67 reg_t
sim_t::get_scr(int which
)
71 case 0: return procs
.size();
72 case 1: return memsz
>> 20;
81 if (debug
|| ctrlc_pressed
)
86 return htif
->exit_code();
89 void sim_t::step(size_t n
)
91 for (size_t i
= 0, steps
= 0; i
< n
; i
+= steps
)
93 steps
= std::min(n
- i
, INTERLEAVE
- current_step
);
94 procs
[current_proc
]->step(steps
);
96 current_step
+= steps
;
97 if (current_step
== INTERLEAVE
)
100 procs
[current_proc
]->yield_load_reservation();
101 if (++current_proc
== procs
.size())
109 bool sim_t::running()
111 for (size_t i
= 0; i
< procs
.size(); i
++)
112 if (procs
[i
]->running())
119 procs
[0]->state
.tohost
= 1;
124 void sim_t::set_debug(bool value
)
129 void sim_t::set_histogram(bool value
)
131 histogram_enabled
= value
;
132 for (size_t i
= 0; i
< procs
.size(); i
++) {
133 procs
[i
]->set_histogram(histogram_enabled
);
137 void sim_t::set_procs_debug(bool value
)
139 for (size_t i
=0; i
< procs
.size(); i
++)
140 procs
[i
]->set_debug(value
);