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
, bool halted
,
24 const std::vector
<std::string
>& args
)
25 : htif_t(args
), procs(std::max(nprocs
, size_t(1))),
26 current_step(0), current_proc(0), debug(false), gdbserver(NULL
)
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 %zu bytes of target mem (wanted %zu)\n",
44 bus
.add_device(DEBUG_START
, &debug_module
);
46 debug_mmu
= new mmu_t(this, NULL
);
48 for (size_t i
= 0; i
< procs
.size(); i
++) {
49 procs
[i
] = new processor_t(isa
, this, i
, halted
);
52 rtc
.reset(new rtc_t(procs
));
58 for (size_t i
= 0; i
< procs
.size(); i
++)
64 void sim_thread_main(void* arg
)
66 ((sim_t
*)arg
)->main();
72 set_procs_debug(true);
76 if (debug
|| ctrlc_pressed
)
88 host
= context_t::current();
89 target
.init(sim_thread_main
, this);
93 void sim_t::step(size_t n
)
95 for (size_t i
= 0, steps
= 0; i
< n
; i
+= steps
)
97 steps
= std::min(n
- i
, INTERLEAVE
- current_step
);
98 procs
[current_proc
]->step(steps
);
100 current_step
+= steps
;
101 if (current_step
== INTERLEAVE
)
104 procs
[current_proc
]->yield_load_reservation();
105 if (++current_proc
== procs
.size()) {
107 rtc
->increment(INTERLEAVE
/ INSNS_PER_RTC_TICK
);
115 void sim_t::set_debug(bool value
)
120 void sim_t::set_log(bool value
)
125 void sim_t::set_histogram(bool value
)
127 histogram_enabled
= value
;
128 for (size_t i
= 0; i
< procs
.size(); i
++) {
129 procs
[i
]->set_histogram(histogram_enabled
);
133 void sim_t::set_procs_debug(bool value
)
135 for (size_t i
=0; i
< procs
.size(); i
++)
136 procs
[i
]->set_debug(value
);
139 bool sim_t::mmio_load(reg_t addr
, size_t len
, uint8_t* bytes
)
141 if (addr
+ len
< addr
)
143 return bus
.load(addr
, len
, bytes
);
146 bool sim_t::mmio_store(reg_t addr
, size_t len
, const uint8_t* bytes
)
148 if (addr
+ len
< addr
)
150 return bus
.store(addr
, len
, bytes
);
153 void sim_t::make_config_string()
155 reg_t rtc_addr
= EXT_IO_BASE
;
156 bus
.add_device(rtc_addr
, rtc
.get());
158 const int align
= 0x1000;
159 reg_t cpu_addr
= rtc_addr
+ ((rtc
->size() - 1) / align
+ 1) * align
;
160 reg_t cpu_size
= align
;
162 uint32_t reset_vec
[8] = {
163 0x297 + DRAM_BASE
- DEFAULT_RSTVEC
, // reset vector
164 0x00028067, // jump straight to DRAM_BASE
165 0x00000000, // reserved
166 0, // config string pointer
167 0, 0, 0, 0 // trap vector
169 reset_vec
[3] = DEFAULT_RSTVEC
+ sizeof(reset_vec
); // config string pointer
171 std::vector
<char> rom((char*)reset_vec
, (char*)reset_vec
+ sizeof(reset_vec
));
178 " #address-cells = <2>;\n"
179 " #size-cells = <2>;\n"
180 " compatible = \"ucbbar,spike-bare-dev\";\n"
181 " model = \"ucbbar,spike-bare\";\n"
183 " #address-cells = <1>;\n"
184 " #size-cells = <0>;\n"
185 " timebase-frequency = <" << (CPU_HZ
/INSNS_PER_RTC_TICK
) << ">;\n";
186 for (size_t i
= 0; i
< procs
.size(); i
++) {
187 s
<< " CPU" << i
<< ": cpu@" << i
<< " {\n"
188 " device_type = \"cpu\";\n"
189 " reg = <" << i
<< ">;\n"
190 " status = \"okay\";\n"
191 " compatible = \"riscv\";\n"
192 " riscv,isa = \"" << procs
[i
]->isa_string
<< "\";\n"
193 " mmu-type = \"riscv," << (procs
[i
]->max_xlen
<= 32 ? "sv32" : "sv48") << "\";\n"
194 " clock-frequency = <" << CPU_HZ
<< ">;\n"
197 reg_t membs
= DRAM_BASE
;
200 " memory@" << DRAM_BASE
<< " {\n"
201 " device_type = \"memory\";\n"
202 " reg = <0x" << (membs
>> 32) << " 0x" << (membs
& (uint32_t)-1) <<
203 " 0x" << (memsz
>> 32) << " 0x" << (memsz
& (uint32_t)-1) << ">;\n"
206 " #address-cells = <2>;\n"
207 " #size-cells = <2>;\n"
208 " compatible = \"ucbbar,spike-bare-soc\";\n"
210 " clint@" << rtc_addr
<< " {\n"
211 " compatible = \"riscv,clint0\";\n"
212 " interrupts-extended = <" << std::dec
;
213 for (size_t i
= 0; i
< procs
.size(); i
++)
214 s
<< "&CPU" << i
<< " 3 &CPU" << i
<< " 7 ";
215 s
<< std::hex
<< ">;\n"
216 " reg = <0x" << (rtc_addr
>> 32) << " 0x" << (rtc_addr
& (uint32_t)-1) <<
222 config_string
= s
.str();
223 rom
.insert(rom
.end(), config_string
.begin(), config_string
.end());
224 rom
.resize((rom
.size() / align
+ 1) * align
);
226 boot_rom
.reset(new rom_device_t(rom
));
227 bus
.add_device(DEFAULT_RSTVEC
, boot_rom
.get());
237 void sim_t::read_chunk(addr_t taddr
, size_t len
, void* dst
)
240 auto data
= debug_mmu
->load_uint64(taddr
);
241 memcpy(dst
, &data
, sizeof data
);
244 void sim_t::write_chunk(addr_t taddr
, size_t len
, const void* src
)
248 memcpy(&data
, src
, sizeof data
);
249 debug_mmu
->store_uint64(taddr
, data
);