-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcSharpExportInterface.cpp
More file actions
310 lines (254 loc) · 7.03 KB
/
cSharpExportInterface.cpp
File metadata and controls
310 lines (254 loc) · 7.03 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
#include <sstream>
#include <chrono>
#include <queue>
#include <tbb/spin_mutex.h>
#include "openVCB/openVCB.h"
#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif
using namespace openVCB;
using namespace std;
using namespace std::chrono;
const float TARGET_DT = 1.f / 60;
const float MIN_DT = 1.f / 30;
Project* proj = nullptr;
thread* simThread = nullptr;
tbb::spin_mutex simLock;
float targetTPS = 0;
double maxTPS = 0;
bool run = true;
bool breakpoint = false;
void simFunc() {
double tpsEst = 2 / TARGET_DT;
double desiredTicks = 0;
auto lastTime = high_resolution_clock::now();
while (run) {
auto curTime = high_resolution_clock::now();
desiredTicks = min(desiredTicks + duration_cast<duration<double>>(curTime - lastTime).count() * targetTPS, tpsEst * MIN_DT);
lastTime = curTime;
// Find max tick amount we can do
if (desiredTicks >= 1.) {
double maxTickAmount = max(tpsEst * TARGET_DT, 1.);
int tickAmount = (int)min(desiredTicks, maxTickAmount);
// Aquire lock, simulate, and time
simLock.lock();
auto s = high_resolution_clock::now();
auto res = proj->tick(tickAmount, 100000000ll);
auto e = high_resolution_clock::now();
simLock.unlock();
// Use timings to estimate max possible tps
desiredTicks = desiredTicks - res.numTicksProcessed;
maxTPS = res.numTicksProcessed / duration_cast<duration<double>>(e - s).count();
if (isfinite(maxTPS))
tpsEst = glm::clamp(glm::mix(maxTPS, tpsEst, 0.95), 1., 1e8);
if (res.breakpoint) {
targetTPS = 0;
desiredTicks = 0;
breakpoint = true;
}
}
// Check how much time I got left and sleep until the next check
curTime = high_resolution_clock::now();
double ms = 1000 * (TARGET_DT - duration_cast<duration<double>>(curTime - lastTime).count());
if (ms > 1.05)
std::this_thread::sleep_for(milliseconds((int)ms));
}
}
extern "C" {
/*
* Functions to control openVCB simulations
*/
EXPORT_API int getLineNumber(int addr) {
auto itr = proj->lineNumbers.find(addr);
if (itr != proj->lineNumbers.end())
return itr->second;
return 0;
}
EXPORT_API size_t getSymbol(char* buff, int size) {
auto itr = proj->assemblySymbols.find(string(buff));
if (itr != proj->assemblySymbols.end())
return itr->second;
return 0;
}
EXPORT_API size_t getNumTicks() {
return proj->tickNum;
}
EXPORT_API float getMaxTPS() {
return (float)maxTPS;
}
EXPORT_API uint32_t getVMemAddress() {
return proj->lastVMemAddr;
}
EXPORT_API void setTickRate(float tps) {
targetTPS = max(0.f, tps);
}
EXPORT_API int pollBreakpoint() {
bool res = breakpoint;
breakpoint = false;
return res;
}
EXPORT_API void tick(int tick) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->tick(tick);
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void toggleLatch(int x, int y) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->toggleLatch(glm::ivec2(x, y));
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void toggleLatchIndex(int idx) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->toggleLatch(idx);
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void addBreakpoint(int gid) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->addBreakpoint(gid);
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void removeBreakpoint(int gid) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->removeBreakpoint(gid);
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void setClockPeriod(unsigned long long period) {
proj->clockPeriod = period;
}
/*
* Functions to initialize openVCB
*/
EXPORT_API void newProject() {
fprintf(stderr, "openVCB: https://github.com/kittybupu/openVCB\n");
fprintf(stderr, "openVCB: Jerry#1058\n");
proj = new Project;
}
EXPORT_API int initProject() {
proj->preprocess(false);
// Start sim thread paused
run = true;
simThread = new thread(simFunc);
return proj->numGroups;
}
EXPORT_API void initVMem(char* assembly, int aSize, char* err, int errSize) {
proj->assembly = string(assembly);
proj->assembleVmem(err);
}
EXPORT_API void deleteProject() {
run = false;
if (simThread) {
simThread->join();
delete simThread;
simThread = nullptr;
}
if (proj) {
// These should be managed.
proj->states = nullptr;
proj->vmem = nullptr;
proj->image = nullptr;
delete proj;
proj = nullptr;
}
}
/*
* Functions to replace openVCB buffers with managed ones
*/
EXPORT_API void setStateMemory(int* data, int size) {
memcpy(data, proj->states, sizeof(int) * size);
delete[] proj->states;
proj->states = (InkState*)data;
}
EXPORT_API void addInstrumentBuffer(InkState* buff, int buffSize, int idx) {
float tps = targetTPS;
targetTPS = 0;
simLock.lock();
proj->instrumentBuffers.push_back({ buff, buffSize, idx });
simLock.unlock();
targetTPS = tps;
}
EXPORT_API void setVMemMemory(int* data, int size) {
proj->vmem = data;
proj->vmemSize = size;
}
EXPORT_API void setIndicesMemory(int* data, int size) {
memcpy(data, proj->indexImage, sizeof(int) * size);
}
EXPORT_API void setImageMemory(int* data, int width, int height) {
proj->width = width;
proj->height = height;
proj->image = (InkPixel*)data;
}
EXPORT_API void setDecoMemory(int* indices, int indLen, int* col, int colLen) {
using namespace glm;
std::vector<bool> visited(proj->width * proj->height, false);
std::queue<ivec3> queue;
for (size_t y = 0; y < proj->height; y++)
for (size_t x = 0; x < proj->width; x++) {
int idx = x + y * proj->width;
indices[idx] = -1;
if (col[idx] != 0xffffffff) continue;
auto ink = openVCB::setOff(proj->image[idx].getInk());
if (ink == Ink::LatchOff || ink == Ink::LedOff) {
queue.push(ivec3(x, y, proj->indexImage[idx]));
visited[idx] = true;
}
}
const glm::ivec2 fourNeighbors[4]{
ivec2(-1, 0),
ivec2(0, 1),
ivec2(1, 0),
ivec2(0, -1)
};
while (queue.size()) {
auto pos = queue.front();
queue.pop();
indices[pos.x + pos.y * proj->width] = pos.z;
for (int k = 0; k < 4; k++) {
ivec2 np = (ivec2)pos + fourNeighbors[k];
if (np.x < 0 || np.x >= proj->width ||
np.y < 0 || np.y >= proj->height) continue;
int nidx = np.x + np.y * proj->width;
if (visited[nidx]) continue;;
visited[nidx] = true;
if (col[nidx] != 0xffffffff) continue;
queue.push(ivec3(np.x, np.y, pos.z));
}
}
}
/*
* Functions to configure openVCB
*/
EXPORT_API void getGroupStats(int* numGroups, int* numConnections) {
*numGroups = proj->numGroups;
*numConnections = proj->writeMap.nnz;
}
EXPORT_API void setInterface(LatchInterface addr, LatchInterface data) {
proj->vmAddr = addr;
proj->vmData = data;
}
}
int main() {
printf("openVCB: https://github.com/kittybupu/openVCB\n");
}