-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathemulated.cpp
More file actions
580 lines (498 loc) Β· 16.2 KB
/
Copy pathemulated.cpp
File metadata and controls
580 lines (498 loc) Β· 16.2 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#ifndef ARDUINO
/**
* This file lists the primitives of the language and stores them in the
* primitives
*
* Adding a primitive:
* 1) Bump up the NUM_PRIMITIVES constant
* 2) Define <primitive>_type
* 3) Define a function void primitive(Module* m)
* 4) Extend the install_primitives function
*
*/
#include <sys/time.h>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <thread>
#include "../Memory/mem.h"
#include "../Utils/macros.h"
#include "../Utils/util.h"
#include "primitives.h"
#define NUM_PRIMITIVES 0
#define NUM_PRIMITIVES_ARDUINO 29
#define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO)
// Global index for installing primitives
int prim_index = 0;
double sensor_emu = 0;
/*
Private macros to install a primitive
*/
#define install_primitive(prim_name) \
{ \
dbg_info("installing primitive number: %d of %d with name: %s\n", \
prim_index + 1, ALL_PRIMITIVES, #prim_name); \
if (prim_index < ALL_PRIMITIVES) { \
PrimitiveEntry *p = &primitives[prim_index++]; \
p->name = #prim_name; \
p->f = &(prim_name); \
} else { \
FATAL("pim_index out of bounds"); \
} \
}
#define def_prim(function_name, type) \
Type function_name##_type = type; \
bool function_name(Module *m)
// TODO: use fp
#define pop_args(n) m->sp -= n
#define get_arg(m, arg) m->stack[(m)->sp - (arg)].value
#define pushUInt32(arg) m->stack[++m->sp].value.uint32 = arg
#define pushInt32(arg) m->stack[++m->sp].value.int32 = arg
#define pushUInt64(arg) \
m->stack[++m->sp].value_type = I64; \
m->stack[m->sp].value.uint64 = arg
#define arg0 get_arg(m, 0)
#define arg1 get_arg(m, 1)
#define arg2 get_arg(m, 2)
#define arg3 get_arg(m, 3)
#define arg4 get_arg(m, 4)
#define arg5 get_arg(m, 5)
#define arg6 get_arg(m, 6)
#define arg7 get_arg(m, 7)
#define arg8 get_arg(m, 8)
#define arg9 get_arg(m, 9)
// The primitive table
PrimitiveEntry primitives[ALL_PRIMITIVES];
//
uint32_t param_arr_len0[0] = {};
uint32_t param_I32_arr_len1[1] = {I32};
uint32_t param_I32_arr_len2[2] = {I32, I32};
uint32_t param_I32_arr_len3[3] = {I32, I32, I32};
uint32_t param_I32_arr_len4[4] = {I32, I32, I32, I32};
uint32_t param_I32_arr_len10[10] = {I32, I32, I32, I32, I32,
I32, I32, I32, I32, I32};
uint32_t param_I64_arr_len1[1] = {I64};
Type oneToNoneU32 = {
.form = FUNC,
.param_count = 1,
.params = param_I32_arr_len1,
.result_count = 0,
.results = nullptr,
.mask = 0x8001 /* 0x800 = no return ; 1 = I32*/
};
Type twoToNoneU32 = {
.form = FUNC,
.param_count = 2,
.params = param_I32_arr_len2,
.result_count = 0,
.results = nullptr,
.mask = 0x80011 /* 0x800 = no return ; 1 = I32; 1 = I32*/
};
Type threeToNoneU32 = {
.form = FUNC,
.param_count = 3,
.params = param_I32_arr_len3,
.result_count = 0,
.results = nullptr,
.mask = 0x800111 /* 0x800 = no return ; 1=I32; 1=I32; 1=I32*/
};
Type fourToNoneU32 = {
.form = FUNC,
.param_count = 4,
.params = param_I32_arr_len4,
.result_count = 0,
.results = nullptr,
.mask =
0x8001111 /* 0x800 = no return ; 1 = I32; 1 = I32; 1 = I32; 1 = I32*/
};
Type oneToOneU32 = {
.form = FUNC,
.param_count = 1,
.params = param_I32_arr_len1,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x80011 /* 0x8 1=I32 0=endRet ; 1=I32; 1=I32*/
};
Type oneToOneI32 = {
.form = FUNC,
.param_count = 1,
.params = param_I32_arr_len1,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x80011 /* 0x8 1=I32 0=endRet ; 1=I32; 1=I32*/
};
Type twoToOneU32 = {
.form = FUNC,
.param_count = 2,
.params = param_I32_arr_len2,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x81011 /* 0x8 1=I32 0=endRet ; 1=I32; 1=I32*/
};
Type threeToOneU32 = {
.form = FUNC,
.param_count = 3,
.params = param_I32_arr_len3,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x810111 /* 0x8 1=I32 0=endRet ; 1=I32; 1=I32; 1=I32*/
};
Type fourToOneU32 = {
.form = FUNC,
.param_count = 4,
.params = param_I32_arr_len4,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x8101111 /* 0x8 1=I32 0=endRet ; 1=I32; 1=I32; 1=I32; 1=I32*/
};
Type tenToOneU32 = {
.form = FUNC,
.param_count = 10,
.params = param_I32_arr_len10,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x8101111111111 /* 0x8 1=I32 0=endRet ; 10 params 1=I32*/
};
Type NoneToNoneU32 = {.form = FUNC,
.param_count = 0,
.params = nullptr,
.result_count = 0,
.results = nullptr,
.mask = 0x80000};
Type NoneToOneU32 = {.form = FUNC,
.param_count = 0,
.params = nullptr,
.result_count = 1,
.results = param_I32_arr_len1,
.mask = 0x81000};
Type NoneToOneU64 = {.form = FUNC,
.param_count = 0,
.params = nullptr,
.result_count = 1,
.results = param_I64_arr_len1,
.mask = 0x82000};
def_prim(init_pixels, NoneToNoneU32) {
printf("init_pixels \n");
return true;
}
def_prim(set_pixel_color, fourToOneU32) {
printf("set_pixel_color \n");
pop_args(4);
return true;
}
def_prim(show_pixels, NoneToNoneU32) {
printf("show pixels \n");
return true;
}
def_prim(clear_pixels, NoneToNoneU32) {
printf("clear pixels \n");
return true;
}
def_prim(abort, NoneToNoneU32) {
debug("EMU: abort\n");
return false;
}
def_prim(millis, NoneToOneU64) {
struct timeval tv {};
gettimeofday(&tv, nullptr);
unsigned long millis = 1000 * tv.tv_sec + tv.tv_usec;
pushUInt64(millis);
return true;
}
def_prim(micros, NoneToOneU64) {
struct timeval tv {};
gettimeofday(&tv, nullptr);
unsigned long micros = 1000000 * tv.tv_sec + tv.tv_usec;
pushUInt64(micros);
return true;
}
// call callback test function (temporary)
def_prim(test, oneToNoneU32) {
uint32_t fidx = arg0.uint32;
std::string topic = "interrupt";
topic.append(std::to_string(fidx));
Callback c = Callback(m, topic, fidx);
CallbackHandler::add_callback(c);
auto *payload = reinterpret_cast<const unsigned char *>("TestPayload");
CallbackHandler::push_event(topic, (const char *)payload, 11);
pop_args(1);
return true;
}
def_prim(print_int, oneToNoneU32) {
debug("EMU: print ");
printf("%u\n", arg0.uint32);
pop_args(1);
return true;
}
def_prim(print_string, twoToNoneU32) {
uint32_t addr = arg1.uint32;
uint32_t size = arg0.uint32;
std::string text = parse_utf8_string(m->memory.bytes, size, addr);
debug("EMU: print string at %i: ", addr);
printf("%s", text.c_str());
pop_args(2);
return true;
}
def_prim(wifi_connect, fourToNoneU32) {
uint32_t ssid = arg3.uint32;
uint32_t len0 = arg2.uint32;
uint32_t pass = arg1.uint32;
uint32_t len1 = arg0.uint32;
std::string ssid_str = parse_utf8_string(m->memory.bytes, len0, ssid);
std::string pass_str = parse_utf8_string(m->memory.bytes, len1, pass);
debug("EMU: connect to %s with password %s\n", ssid_str.c_str(),
pass_str.c_str());
pop_args(4);
return true;
}
def_prim(wifi_status, NoneToOneU32) {
pushInt32(3); // return WL_CONNECTED
return true;
}
def_prim(wifi_connected, NoneToOneU32) {
pushInt32(1); // return that we are connected
return true;
}
def_prim(wifi_localip, twoToOneU32) {
uint32_t buff = arg1.uint32;
uint32_t size = arg0.uint32;
std::string ip = "192.168.0.181";
for (unsigned long i = 0; i < ip.length(); i++) {
m->memory.bytes[buff + i] = (uint32_t)ip[i];
}
pop_args(2);
pushInt32(buff);
return true;
}
def_prim(http_get, fourToOneU32) {
// Get arguments
uint32_t url = arg3.uint32;
uint32_t length = arg2.uint32;
int32_t response = arg1.uint32;
uint32_t size = arg0.uint32;
// Parse url
std::string text = parse_utf8_string(m->memory.bytes, length, url);
debug("EMU: http get request %s\n", text.c_str());
// Construct response
std::string answer = "Response code: 200.";
if (answer.length() > size) {
sprintf(exception, "GET: buffer size is too small for response.");
return false; // TRAP
}
for (unsigned long i = 0; i < answer.length(); i++) {
m->memory.bytes[response + i] = (uint32_t)answer[i];
}
// Pop args and return response address
pop_args(4);
pushInt32(response);
return true;
}
def_prim(http_post, tenToOneU32) {
// Get arguments
uint32_t url = arg9.uint32;
uint32_t url_len = arg8.uint32;
uint32_t body = arg7.uint32;
uint32_t body_len = arg6.uint32;
uint32_t content_type = arg5.uint32;
uint32_t content_type_len = arg4.uint32;
uint32_t authorization = arg3.uint32;
uint32_t authorization_len = arg2.uint32;
int32_t response = arg1.uint32;
uint32_t size = arg0.uint32;
std::string url_parsed = parse_utf8_string(m->memory.bytes, url_len, url);
std::string body_parsed =
parse_utf8_string(m->memory.bytes, body_len, body);
std::string content_type_parsed =
parse_utf8_string(m->memory.bytes, content_type_len, content_type);
std::string authorization_parsed =
parse_utf8_string(m->memory.bytes, authorization_len, authorization);
debug(
"EMU: POST %s\n\t Content-type: '%s'\n\t Authorization: '%s'\n\t "
"'%s'\n",
url_parsed.c_str(), content_type_parsed.c_str(),
authorization_parsed.c_str(), body_parsed.c_str());
pop_args(10);
pushInt32(response);
return true;
}
def_prim(chip_pin_mode, twoToNoneU32) {
debug("EMU: chip_pin_mode(%u,%u) \n", arg1.uint32, arg0.uint32);
pop_args(2);
return true;
}
def_prim(chip_digital_write, twoToNoneU32) {
debug("EMU: chip_digital_write(%u,%u) \n", arg1.uint32, arg0.uint32);
pop_args(2);
return true;
}
def_prim(chip_digital_read, oneToOneU32) {
uint8_t pin = arg0.uint32;
pop_args(1);
pushUInt32(1); // HIGH
return true;
}
def_prim(chip_analog_read, oneToOneI32) {
uint8_t pin = arg0.uint32;
pop_args(1);
pushInt32(sin(sensor_emu) * 100);
sensor_emu += .25;
return true;
}
def_prim(chip_analog_write, twoToNoneU32) {
debug("EMU: chip_analog_write(%u,%u) \n", arg1.uint32, arg0.uint32);
pop_args(2);
return true;
}
def_prim(chip_delay, oneToNoneU32) {
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
debug("EMU: chip_delay(%u) \n", arg0.uint32);
sleep_for(milliseconds(arg0.uint32));
debug("EMU: .. done\n");
pop_args(1);
return true;
}
def_prim(chip_delay_us, oneToNoneU32) {
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
debug("EMU: chip_delay(%u ms) \n", arg0.uint32);
sleep_for(microseconds(arg0.uint32));
debug("EMU: .. done\n");
pop_args(1);
return true;
}
// warning: undefined symbol: write_spi_byte
def_prim(write_spi_byte, oneToNoneU32) {
debug("EMU: write_spi_byte(%u) \n", arg0.uint32);
pop_args(1);
return true;
}
// warning: undefined symbol: spi_begin
def_prim(spi_begin, NoneToNoneU32) {
debug("EMU: spi_begin \n");
return true;
}
def_prim(write_spi_bytes_16, twoToNoneU32) {
debug("EMU: write_spi_byte_16(%u, %u) \n", arg1.uint32, arg0.uint32);
pop_args(2);
return true;
}
def_prim(subscribe_interrupt, threeToNoneU32) {
uint8_t pin = arg2.uint32; // GPIOPin
uint32_t tidx = arg1.uint32; // Table Idx pointing to Callback function
uint8_t mode = arg0.uint32;
uint8_t *maddr = m->memory.bytes + tidx;
if (maddr < m->memory.bytes) {
debug("subscribe_interrupt: memory access overflow\n", tidx);
return false;
}
memcpy(&tidx, maddr, 4);
debug("EMU: subscribe_interrupt(%u, %u, %u) \n", pin, tidx, mode);
if (tidx < 0 || m->table.size < tidx) {
debug("subscribe_interrupt: out of range table index %i\n", tidx);
return false;
}
std::string topic = "interrupt_";
topic.append(std::to_string(pin));
Callback c = Callback(m, topic, tidx);
CallbackHandler::add_callback(c);
pop_args(3);
return true;
}
// Temporary Primitives needed for analogWrite in ESP32
def_prim(chip_ledc_set_duty, threeToNoneU32) {
uint8_t channel = arg2.uint32;
uint32_t value = arg1.uint32;
uint32_t maxValue = arg0.uint32;
// calculate duty, 4095 from 2 ^ 12 - 1
printf("chip_analog_write(%u, %u, %u)\n", channel, value, maxValue);
pop_args(3);
return true;
}
def_prim(chip_ledc_setup, threeToNoneU32) {
uint32_t channel = arg2.uint32;
uint32_t freq = arg1.uint32;
uint32_t ledc_timer = arg0.uint32;
printf("chip_ledc_setup(%u, %u, %u)\n", channel, freq, ledc_timer);
pop_args(3);
return true;
}
def_prim(chip_ledc_attach_pin, twoToNoneU32) {
uint32_t pin = arg1.uint32;
uint32_t channel = arg0.uint32;
printf("chip_ledc_attach_pin(%u,%u)\n", pin, channel);
pop_args(2);
return true;
}
//------------------------------------------------------
// Installing all the primitives
//------------------------------------------------------
void install_primitives() {
dbg_info("INSTALLING PRIMITIVES\n");
dbg_info("INSTALLING FAKE ARDUINO\n");
install_primitive(abort);
install_primitive(millis);
install_primitive(micros);
install_primitive(print_int);
install_primitive(print_string);
install_primitive(wifi_connect);
install_primitive(wifi_status);
install_primitive(wifi_connected);
install_primitive(wifi_localip);
install_primitive(http_get);
install_primitive(http_post);
install_primitive(chip_pin_mode);
install_primitive(chip_digital_write);
install_primitive(chip_delay);
install_primitive(chip_digital_read);
install_primitive(chip_analog_read);
install_primitive(chip_delay_us);
install_primitive(spi_begin);
install_primitive(write_spi_byte);
install_primitive(write_spi_bytes_16);
install_primitive(subscribe_interrupt);
install_primitive(init_pixels);
install_primitive(set_pixel_color);
install_primitive(clear_pixels);
install_primitive(show_pixels);
// temporary mock primitives needed for analogWrite in ESP32
install_primitive(chip_analog_write);
install_primitive(chip_ledc_setup);
install_primitive(chip_ledc_attach_pin);
install_primitive(chip_ledc_set_duty);
}
//------------------------------------------------------
// resolving the primitives
//------------------------------------------------------
bool resolve_primitive(char *symbol, Primitive *val) {
debug("Resolve primitives (%d) for %s \n", ALL_PRIMITIVES, symbol);
for (auto &primitive : primitives) {
// printf("Checking %s = %s \n", symbol, primitive.name);
if (!strcmp(symbol, primitive.name)) {
debug("FOUND PRIMITIVE\n");
*val = primitive.f;
return true;
}
}
FATAL("Could not find primitive %s \n", symbol);
return false;
}
Memory external_mem{};
bool resolve_external_memory(char *symbol, Memory **val) {
if (!strcmp(symbol, "memory")) {
if (external_mem.bytes == nullptr) {
external_mem.initial = 256;
external_mem.maximum = 256;
external_mem.pages = 256;
external_mem.bytes = (uint8_t *)acalloc(
external_mem.pages * PAGE_SIZE, sizeof(uint32_t),
"Module->memory.bytes primitive");
}
*val = &external_mem;
return true;
}
FATAL("Could not find memory %s \n", symbol);
return false;
}
#endif // ARDUINO