-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
251 lines (222 loc) · 7.71 KB
/
api.cpp
File metadata and controls
251 lines (222 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "include/api/refcpu_api.h"
#include "include/ref.h"
#include "include/spike_ref.h"
#include "include/CSR.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
struct RefCpuContext {
Ref_cpu core;
};
namespace {
void init_empty(Ref_cpu& core, uint32_t reset_pc, uint32_t mem_size) {
if (core.memory) {
std::free(core.memory);
core.memory = nullptr;
}
core.ram_size = mem_size;
const uint32_t ram_words = mem_size / sizeof(uint32_t);
core.memory = static_cast<uint32_t*>(std::calloc(ram_words, sizeof(uint32_t)));
if (!core.memory) {
throw std::bad_alloc();
}
core.io_words.clear();
core.state = {};
core.state.pc = reset_pc;
core.state.csr[csr_misa] = 0x40141103;
core.privilege = RISCV_MODE_M;
core.Instruction = 0;
core.asy = false;
core.page_fault_inst = false;
core.page_fault_load = false;
core.page_fault_store = false;
core.illegal_exception = false;
core.M_software_interrupt = false;
core.M_timer_interrupt = false;
core.M_external_interrupt = false;
core.S_software_interrupt = false;
core.S_timer_interrupt = false;
core.S_external_interrupt = false;
core.is_br = false;
core.br_taken = false;
core.is_exception = false;
core.is_csr = false;
core.sim_end = false;
core.uart_print = false;
core.sim_time = 0;
core.is_io = false;
core.io_reg_idx = 0;
core.force_sync = false;
core.interval_inst_count = 0;
core.current_bb_head_pc = 0;
core.current_bb_len = 0;
core.difftest_started = false;
core.store_word(0x10000004, 0x00006000);
core.store_word(0x0, 0xf1402573);
core.store_word(0x4, 0x83e005b7);
core.store_word(0x8, 0x800002b7);
core.store_word(0xc, 0x00028067);
}
} // namespace
extern "C" {
RefCpuContext* refcpu_init(uint32_t reset_pc, const char* image_path, uint32_t mem_size) {
RefCpuContext* ctx = new (std::nothrow) RefCpuContext();
if (!ctx) return nullptr;
try {
if (image_path && image_path[0] != '\0') {
ctx->core.init(reset_pc, image_path, mem_size);
} else {
init_empty(ctx->core, reset_pc, mem_size);
}
} catch (...) {
delete ctx;
return nullptr;
}
return ctx;
}
void refcpu_destroy(RefCpuContext* ctx) {
if (ctx) {
delete ctx;
}
}
void refcpu_reset(RefCpuContext* ctx, uint32_t reset_pc) {
if (ctx) {
// Simple internal reset mapping
std::memset(&ctx->core.state, 0, sizeof(CPU_state));
ctx->core.state.pc = reset_pc;
ctx->core.privilege = RISCV_MODE_M;
ctx->core.sim_time = 0;
ctx->core.sim_end = false;
ctx->core.Instruction = 0;
ctx->core.page_fault_inst = false;
ctx->core.page_fault_load = false;
ctx->core.page_fault_store = false;
ctx->core.is_br = false;
ctx->core.br_taken = false;
ctx->core.is_exception = false;
ctx->core.is_csr = false;
ctx->core.is_io = false;
}
}
uint64_t refcpu_step(RefCpuContext* ctx, uint64_t n) {
if (!ctx) return 0;
uint64_t count = 0;
for (uint64_t i = 0; i < n && !ctx->core.sim_end; ++i) {
// Execute one instruction
// We'll call the RISCV() loop inner step directly, simulating basic loop
// Normally exec() handles the loop, but for step() we just invoke RISCV()
ctx->core.is_io = false;
ctx->core.is_csr = false;
ctx->core.RISCV();
ctx->core.sim_time++;
count++;
}
return count;
}
void refcpu_get_state(RefCpuContext* ctx, RefCpuState* state) {
if (!ctx || !state) return;
std::memcpy(state->gpr, ctx->core.state.gpr, sizeof(state->gpr));
std::memcpy(state->csr, ctx->core.state.csr, sizeof(state->csr));
state->pc = ctx->core.state.pc;
state->privilege = ctx->core.privilege;
// DiffTest 扩展字段同步
state->instruction = ctx->core.Instruction;
state->page_fault_inst = ctx->core.page_fault_inst;
state->page_fault_load = ctx->core.page_fault_load;
state->page_fault_store = ctx->core.page_fault_store;
state->store = ctx->core.state.store;
state->store_addr = ctx->core.state.store_addr;
if (ctx->core.state.store) {
const uint32_t offset = ctx->core.state.store_addr & 0x3u;
state->store_data = ctx->core.state.store_data << (offset * 8);
state->store_strb = ctx->core.state.store_strb << (offset * 8);
} else {
state->store_data = ctx->core.state.store_data;
state->store_strb = ctx->core.state.store_strb;
}
state->reserve_valid = ctx->core.state.reserve_valid;
state->reserve_addr = ctx->core.state.reserve_addr;
}
void refcpu_set_state(RefCpuContext* ctx, const RefCpuState* state) {
if (!ctx || !state) return;
std::memcpy(ctx->core.state.gpr, state->gpr, sizeof(state->gpr));
std::memcpy(ctx->core.state.csr, state->csr, sizeof(state->csr));
ctx->core.state.pc = state->pc;
ctx->core.privilege = state->privilege;
ctx->core.state.store = state->store;
ctx->core.state.store_addr = state->store_addr;
ctx->core.state.store_data = state->store_data;
ctx->core.state.store_strb = state->store_strb;
ctx->core.state.reserve_valid = state->reserve_valid;
ctx->core.state.reserve_addr = state->reserve_addr;
}
bool refcpu_load_image(RefCpuContext* ctx, const char* path) {
if (!ctx || !path) return false;
// Call the original init using current pc and memory size, but new image
try {
ctx->core.init(ctx->core.state.pc, path, ctx->core.ram_size);
return true;
} catch (...) {
return false;
}
}
bool refcpu_restore_checkpoint(RefCpuContext* ctx, const char* path) {
if(!ctx || !path) return false;
ctx->core.restore_checkpoint(path);
return true;
}
uint32_t* refcpu_get_ram_ptr(RefCpuContext* ctx) {
if(!ctx || !ctx->core.memory) return nullptr;
// Assuming context's core memory is a pointer to the simulated RAM
return (uint32_t*)ctx->core.memory;
}
uint32_t refcpu_get_io_word(RefCpuContext* ctx, uint32_t addr) {
if(!ctx) return 0;
auto it = ctx->core.io_words.find(addr);
if (it != ctx->core.io_words.end()) {
return it->second;
}
return 0; // Or standard unmapped memory response
}
void refcpu_sync_ram_from_dut(RefCpuContext* ctx, const uint32_t* pmem_ram_ptr, int img_size) {
if(!ctx || !pmem_ram_ptr || !ctx->core.memory || img_size <= 0) return;
memcpy(ctx->core.memory, pmem_ram_ptr, img_size);
}
void refcpu_seed_ref_io_from_backing(RefCpuContext* ctx, uint32_t addr, uint32_t data) {
if(!ctx) return;
if (data == 0) {
ctx->core.io_words.erase(addr);
} else {
ctx->core.io_words[addr] = data;
}
}
void refcpu_clear_io_words(RefCpuContext* ctx) {
if(!ctx) return;
ctx->core.io_words.clear();
}
uint32_t refcpu_load_word(RefCpuContext* ctx, uint32_t addr) {
if(!ctx) return 0;
return ctx->core.load_word(addr);
}
void refcpu_get_step_info(RefCpuContext* ctx, RefCpuStepInfo* info) {
if(!ctx || !info) return;
info->is_br = ctx->core.is_br;
info->br_taken = ctx->core.br_taken;
info->is_exception = ctx->core.is_exception;
info->is_csr = ctx->core.is_csr;
info->is_io = ctx->core.is_io;
info->sim_end = ctx->core.sim_end;
info->instruction = ctx->core.Instruction;
info->page_fault_inst = ctx->core.page_fault_inst;
info->page_fault_load = ctx->core.page_fault_load;
info->page_fault_store = ctx->core.page_fault_store;
}
void refcpu_set_uart_print(RefCpuContext* ctx, bool enable) {
if(!ctx) return;
ctx->core.uart_print = enable;
}
void refcpu_set_sim_end(RefCpuContext* ctx, bool sim_end) {
if(!ctx) return;
ctx->core.sim_end = sim_end;
}
} // extern "C"