1 // See LICENSE for license details.
14 volatile bool ctrlc_pressed
= false;
15 static void handle_signal(int sig
)
20 signal(sig
, &handle_signal
);
23 sim_t::sim_t(const char* isa
, size_t nprocs
, size_t mem_mb
,
24 const std::vector
<std::string
>& args
)
25 : htif(new htif_isasim_t(this, args
)), procs(std::max(nprocs
, size_t(1))),
26 current_step(0), current_proc(0), debug(false)
28 signal(SIGINT
, &handle_signal
);
29 // allocate target machine's memory, shrinking it as necessary
30 // until the allocation succeeds
31 size_t memsz0
= (size_t)mem_mb
<< 20;
32 size_t quantum
= 1L << 20;
34 memsz0
= (size_t)((sizeof(size_t) == 8 ? 4096 : 2048) - 256) << 20;
37 while ((mem
= (char*)calloc(1, memsz
)) == NULL
)
38 memsz
= (size_t)(memsz
*0.9)/quantum
*quantum
;
41 fprintf(stderr
, "warning: only got %lu bytes of target mem (wanted %lu)\n",
42 (unsigned long)memsz
, (unsigned long)memsz0
);
44 debug_mmu
= new mmu_t(this, NULL
);
46 for (size_t i
= 0; i
< procs
.size(); i
++)
47 procs
[i
] = new processor_t(isa
, this, i
);
49 rtc
.reset(new rtc_t(procs
));
55 for (size_t i
= 0; i
< procs
.size(); i
++)
64 set_procs_debug(true);
67 if (debug
|| ctrlc_pressed
)
72 return htif
->exit_code();
75 void sim_t::step(size_t n
)
77 for (size_t i
= 0, steps
= 0; i
< n
; i
+= steps
)
79 steps
= std::min(n
- i
, INTERLEAVE
- current_step
);
80 procs
[current_proc
]->step(steps
);
82 current_step
+= steps
;
83 if (current_step
== INTERLEAVE
)
86 procs
[current_proc
]->yield_load_reservation();
87 if (++current_proc
== procs
.size()) {
89 rtc
->increment(INTERLEAVE
/ INSNS_PER_RTC_TICK
);
99 for (size_t i
= 0; i
< procs
.size(); i
++)
100 if (procs
[i
]->running())
105 void sim_t::set_debug(bool value
)
110 void sim_t::set_log(bool value
)
115 void sim_t::set_histogram(bool value
)
117 histogram_enabled
= value
;
118 for (size_t i
= 0; i
< procs
.size(); i
++) {
119 procs
[i
]->set_histogram(histogram_enabled
);
123 void sim_t::set_procs_debug(bool value
)
125 for (size_t i
=0; i
< procs
.size(); i
++)
126 procs
[i
]->set_debug(value
);
129 bool sim_t::mmio_load(reg_t addr
, size_t len
, uint8_t* bytes
)
131 if (addr
+ len
< addr
)
133 return bus
.load(addr
, len
, bytes
);
136 bool sim_t::mmio_store(reg_t addr
, size_t len
, const uint8_t* bytes
)
138 if (addr
+ len
< addr
)
140 return bus
.store(addr
, len
, bytes
);
143 void sim_t::make_config_string()
145 reg_t rtc_addr
= EXT_IO_BASE
;
146 bus
.add_device(rtc_addr
, rtc
.get());
148 const int align
= 0x1000;
149 reg_t cpu_addr
= rtc_addr
+ ((rtc
->size() - 1) / align
+ 1) * align
;
150 reg_t cpu_size
= align
;
152 uint32_t reset_vec
[8] = {
153 0x297 + DRAM_BASE
- DEFAULT_RSTVEC
, // reset vector
154 0x00028067, // jump straight to DRAM_BASE
155 0x00000000, // reserved
156 0, // config string pointer
157 0, 0, 0, 0 // trap vector
159 reset_vec
[3] = DEFAULT_RSTVEC
+ sizeof(reset_vec
); // config string pointer
161 std::vector
<char> rom((char*)reset_vec
, (char*)reset_vec
+ sizeof(reset_vec
));
170 " addr 0x" << rtc_addr
<< ";\n"
174 " addr 0x" << DRAM_BASE
<< ";\n"
175 " size 0x" << memsz
<< ";\n"
179 for (size_t i
= 0; i
< procs
.size(); i
++) {
182 " " << "0 {\n" << // hart 0 on core i
183 " isa " << procs
[i
]->isa_string
<< ";\n"
184 " timecmp 0x" << (rtc_addr
+ 8*(1+i
)) << ";\n"
185 " ipi 0x" << cpu_addr
<< ";\n"
188 bus
.add_device(cpu_addr
, procs
[i
]);
189 cpu_addr
+= cpu_size
;
193 config_string
= s
.str();
194 rom
.insert(rom
.end(), config_string
.begin(), config_string
.end());
195 rom
.resize((rom
.size() / align
+ 1) * align
);
197 boot_rom
.reset(new rom_device_t(rom
));
198 bus
.add_device(DEFAULT_RSTVEC
, boot_rom
.get());