f8564fbd0d266373eac00247e52cdb92840f4b43
[riscv-isa-sim.git] / riscv / sim.cc
1 // See LICENSE for license details.
2
3 #include "sim.h"
4 #include "mmu.h"
5 #include "htif.h"
6 #include <map>
7 #include <iostream>
8 #include <sstream>
9 #include <climits>
10 #include <cstdlib>
11 #include <cassert>
12 #include <signal.h>
13
14 volatile bool ctrlc_pressed = false;
15 static void handle_signal(int sig)
16 {
17 if (ctrlc_pressed)
18 exit(-1);
19 ctrlc_pressed = true;
20 signal(sig, &handle_signal);
21 }
22
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)
27 {
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;
33 if (memsz0 == 0)
34 memsz0 = (size_t)((sizeof(size_t) == 8 ? 4096 : 2048) - 256) << 20;
35
36 memsz = memsz0;
37 while ((mem = (char*)calloc(1, memsz)) == NULL)
38 memsz = (size_t)(memsz*0.9)/quantum*quantum;
39
40 if (memsz != memsz0)
41 fprintf(stderr, "warning: only got %lu bytes of target mem (wanted %lu)\n",
42 (unsigned long)memsz, (unsigned long)memsz0);
43
44 debug_mmu = new mmu_t(this, NULL);
45
46 for (size_t i = 0; i < procs.size(); i++)
47 procs[i] = new processor_t(isa, this, i);
48
49 rtc.reset(new rtc_t(procs));
50 make_config_string();
51 }
52
53 sim_t::~sim_t()
54 {
55 for (size_t i = 0; i < procs.size(); i++)
56 delete procs[i];
57 delete debug_mmu;
58 free(mem);
59 }
60
61 int sim_t::run()
62 {
63 if (!debug && log)
64 set_procs_debug(true);
65 while (htif->tick())
66 {
67 if (debug || ctrlc_pressed)
68 interactive();
69 else
70 step(INTERLEAVE);
71 if (gdbserver) {
72 gdbserver->handle();
73 }
74 }
75 return htif->exit_code();
76 }
77
78 void sim_t::step(size_t n)
79 {
80 for (size_t i = 0, steps = 0; i < n; i += steps)
81 {
82 steps = std::min(n - i, INTERLEAVE - current_step);
83 procs[current_proc]->step(steps);
84
85 current_step += steps;
86 if (current_step == INTERLEAVE)
87 {
88 current_step = 0;
89 procs[current_proc]->yield_load_reservation();
90 if (++current_proc == procs.size()) {
91 current_proc = 0;
92 rtc->increment(INTERLEAVE / INSNS_PER_RTC_TICK);
93 }
94
95 htif->tick();
96 }
97 }
98 }
99
100 bool sim_t::running()
101 {
102 for (size_t i = 0; i < procs.size(); i++)
103 if (procs[i]->running())
104 return true;
105 return false;
106 }
107
108 void sim_t::set_debug(bool value)
109 {
110 debug = value;
111 }
112
113 void sim_t::set_log(bool value)
114 {
115 log = value;
116 }
117
118 void sim_t::set_histogram(bool value)
119 {
120 histogram_enabled = value;
121 for (size_t i = 0; i < procs.size(); i++) {
122 procs[i]->set_histogram(histogram_enabled);
123 }
124 }
125
126 void sim_t::set_procs_debug(bool value)
127 {
128 for (size_t i=0; i< procs.size(); i++)
129 procs[i]->set_debug(value);
130 }
131
132 bool sim_t::mmio_load(reg_t addr, size_t len, uint8_t* bytes)
133 {
134 if (addr + len < addr)
135 return false;
136 return bus.load(addr, len, bytes);
137 }
138
139 bool sim_t::mmio_store(reg_t addr, size_t len, const uint8_t* bytes)
140 {
141 if (addr + len < addr)
142 return false;
143 return bus.store(addr, len, bytes);
144 }
145
146 void sim_t::make_config_string()
147 {
148 reg_t rtc_addr = EXT_IO_BASE;
149 bus.add_device(rtc_addr, rtc.get());
150
151 const int align = 0x1000;
152 reg_t cpu_addr = rtc_addr + ((rtc->size() - 1) / align + 1) * align;
153 reg_t cpu_size = align;
154
155 uint32_t reset_vec[8] = {
156 0x297 + DRAM_BASE - DEFAULT_RSTVEC, // reset vector
157 0x00028067, // jump straight to DRAM_BASE
158 0x00000000, // reserved
159 0, // config string pointer
160 0, 0, 0, 0 // trap vector
161 };
162 reset_vec[3] = DEFAULT_RSTVEC + sizeof(reset_vec); // config string pointer
163
164 std::vector<char> rom((char*)reset_vec, (char*)reset_vec + sizeof(reset_vec));
165
166 std::stringstream s;
167 s << std::hex <<
168 "platform {\n"
169 " vendor ucb;\n"
170 " arch spike;\n"
171 "};\n"
172 "rtc {\n"
173 " addr 0x" << rtc_addr << ";\n"
174 "};\n"
175 "ram {\n"
176 " 0 {\n"
177 " addr 0x" << DRAM_BASE << ";\n"
178 " size 0x" << memsz << ";\n"
179 " };\n"
180 "};\n"
181 "core {\n";
182 for (size_t i = 0; i < procs.size(); i++) {
183 s <<
184 " " << i << " {\n"
185 " " << "0 {\n" << // hart 0 on core i
186 " isa " << procs[i]->isa_string << ";\n"
187 " timecmp 0x" << (rtc_addr + 8*(1+i)) << ";\n"
188 " ipi 0x" << cpu_addr << ";\n"
189 " };\n"
190 " };\n";
191 bus.add_device(cpu_addr, procs[i]);
192 cpu_addr += cpu_size;
193 }
194 s << "};\n";
195
196 config_string = s.str();
197 rom.insert(rom.end(), config_string.begin(), config_string.end());
198 rom.resize((rom.size() / align + 1) * align);
199
200 boot_rom.reset(new rom_device_t(rom));
201 bus.add_device(DEFAULT_RSTVEC, boot_rom.get());
202 }