Set default RV32 RAM size to 4 GiB - 256 MiB
[riscv-isa-sim.git] / riscv / sim.cc
1 // See LICENSE for license details.
2
3 #include "sim.h"
4 #include "htif.h"
5 #include "devicetree.h"
6 #include <map>
7 #include <iostream>
8 #include <climits>
9 #include <cstdlib>
10 #include <cassert>
11 #include <signal.h>
12
13 volatile bool ctrlc_pressed = false;
14 static void handle_signal(int sig)
15 {
16 if (ctrlc_pressed)
17 exit(-1);
18 ctrlc_pressed = true;
19 signal(sig, &handle_signal);
20 }
21
22 sim_t::sim_t(const char* isa, size_t nprocs, size_t mem_mb,
23 const std::vector<std::string>& args)
24 : htif(new htif_isasim_t(this, args)), procs(std::max(nprocs, size_t(1))),
25 rtc(0), current_step(0), current_proc(0), debug(false)
26 {
27 signal(SIGINT, &handle_signal);
28 // allocate target machine's memory, shrinking it as necessary
29 // until the allocation succeeds
30 size_t memsz0 = (size_t)mem_mb << 20;
31 size_t quantum = 1L << 20;
32 if (memsz0 == 0)
33 memsz0 = (size_t)((sizeof(size_t) == 8 ? 4096 : 2048) - 256) << 20;
34
35 memsz = memsz0;
36 while ((mem = (char*)calloc(1, memsz)) == NULL)
37 memsz = (size_t)(memsz*0.9)/quantum*quantum;
38
39 if (memsz != memsz0)
40 fprintf(stderr, "warning: only got %lu bytes of target mem (wanted %lu)\n",
41 (unsigned long)memsz, (unsigned long)memsz0);
42
43 debug_mmu = new mmu_t(mem, memsz);
44
45 for (size_t i = 0; i < procs.size(); i++)
46 procs[i] = new processor_t(isa, this, i);
47
48 make_device_tree();
49 }
50
51 sim_t::~sim_t()
52 {
53 for (size_t i = 0; i < procs.size(); i++)
54 delete procs[i];
55 delete debug_mmu;
56 free(mem);
57 }
58
59 reg_t sim_t::get_scr(int which)
60 {
61 switch (which)
62 {
63 case 0: return procs.size();
64 case 1: return memsz >> 20;
65 default: return -1;
66 }
67 }
68
69 int sim_t::run()
70 {
71 if (!debug && log)
72 set_procs_debug(true);
73 while (htif->tick())
74 {
75 if (debug || ctrlc_pressed)
76 interactive();
77 else
78 step(INTERLEAVE);
79 }
80 return htif->exit_code();
81 }
82
83 void sim_t::step(size_t n)
84 {
85 for (size_t i = 0, steps = 0; i < n; i += steps)
86 {
87 steps = std::min(n - i, INTERLEAVE - current_step);
88 procs[current_proc]->step(steps);
89
90 current_step += steps;
91 if (current_step == INTERLEAVE)
92 {
93 current_step = 0;
94 procs[current_proc]->yield_load_reservation();
95 if (++current_proc == procs.size()) {
96 current_proc = 0;
97 rtc += INTERLEAVE / INSNS_PER_RTC_TICK;
98 }
99
100 htif->tick();
101 }
102 }
103 }
104
105 bool sim_t::running()
106 {
107 for (size_t i = 0; i < procs.size(); i++)
108 if (procs[i]->running())
109 return true;
110 return false;
111 }
112
113 void sim_t::stop()
114 {
115 procs[0]->state.tohost = 1;
116 while (htif->tick())
117 ;
118 }
119
120 void sim_t::set_debug(bool value)
121 {
122 debug = value;
123 }
124
125 void sim_t::set_log(bool value)
126 {
127 log = value;
128 }
129
130 void sim_t::set_histogram(bool value)
131 {
132 histogram_enabled = value;
133 for (size_t i = 0; i < procs.size(); i++) {
134 procs[i]->set_histogram(histogram_enabled);
135 }
136 }
137
138 void sim_t::set_procs_debug(bool value)
139 {
140 for (size_t i=0; i< procs.size(); i++)
141 procs[i]->set_debug(value);
142 }
143
144 bool sim_t::mmio_load(reg_t addr, size_t len, uint8_t* bytes)
145 {
146 if (addr + len < addr)
147 return false;
148 return bus.load(addr, len, bytes);
149 }
150
151 bool sim_t::mmio_store(reg_t addr, size_t len, const uint8_t* bytes)
152 {
153 if (addr + len < addr)
154 return false;
155 return bus.store(addr, len, bytes);
156 }
157
158 void sim_t::make_device_tree()
159 {
160 char buf[32];
161 size_t max_devtree_size = procs.size() * 4096; // sloppy upper bound
162 size_t cpu_size = NCSR * procs[0]->max_xlen / 8;
163 reg_t cpu_addr = memsz + max_devtree_size;
164
165 device_tree dt;
166 dt.begin_node("");
167 dt.add_prop("#address-cells", 2);
168 dt.add_prop("#size-cells", 2);
169 dt.add_prop("model", "Spike");
170 dt.begin_node("memory@0");
171 dt.add_prop("device_type", "memory");
172 dt.add_reg({0, memsz});
173 dt.end_node();
174 dt.begin_node("cpus");
175 dt.add_prop("#address-cells", 2);
176 dt.add_prop("#size-cells", 2);
177 for (size_t i = 0; i < procs.size(); i++) {
178 sprintf(buf, "cpu@%" PRIx64, cpu_addr);
179 dt.begin_node(buf);
180 dt.add_prop("device_type", "cpu");
181 dt.add_prop("compatible", "riscv");
182 dt.add_prop("isa", procs[i]->isa);
183 dt.add_reg({cpu_addr});
184 dt.end_node();
185
186 bus.add_device(cpu_addr, procs[i]);
187 cpu_addr += cpu_size;
188 }
189 dt.end_node();
190 dt.end_node();
191
192 devicetree.reset(new rom_device_t(dt.finalize()));
193 bus.add_device(memsz, devicetree.get());
194 }