-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32s3_engine.c
More file actions
210 lines (168 loc) · 5.76 KB
/
esp32s3_engine.c
File metadata and controls
210 lines (168 loc) · 5.76 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
#include "mcp_forth.h"
#include "global.h"
#ifndef M4_NO_THREAD
#include <pthread.h>
#include <alloca.h>
#endif
#define STACK_CELL_COUNT 100
typedef int (*callback_target_t)(int arg1, ...);
typedef struct {
m4_stack_t stack;
const m4_runtime_cb_pair_t ** runtime_cbs;
const m4_runtime_cb_pair_t * const * special_runtime_cbs;
uint8_t * memory;
const uint8_t * data;
uint8_t * callback_info;
const uint8_t ** callback_word_locations;
int * stack_base;
int ** variables;
const callback_target_t * callback_targets;
const int * literals;
int * return_stack;
const uint8_t * bin_start;
int total_extra_memory_size;
} ctx_t;
void m4_esp32s3_engine_run_asm(ctx_t * asm_struct, const uint8_t * program_start);
#define MCP_FORTH_GENERATED_ESP32S3_ENGINE
#include "mcp_forth_generated.h"
#undef MCP_FORTH_GENERATED_ESP32S3_ENGINE
#ifndef M4_NO_THREAD
static int thread_extra_stack_space_get(m4_stack_t * stack)
{
ctx_t * root = (ctx_t *) stack;
return sizeof(ctx_t) + STACK_CELL_COUNT * 4 * 2 + root->total_extra_memory_size;
}
static void * thread_entry(void * arg_void)
{
int res;
m4_engine_thread_entry_arg_t * entry_arg = arg_void;
ctx_t * root = (ctx_t *) entry_arg->stack;
int arg = entry_arg->arg;
const uint8_t * target = entry_arg->target;
res = sem_post(&entry_arg->done_with_arg_sem);
assert(res == 0);
int memory_len = thread_extra_stack_space_get((m4_stack_t *) root);
uint8_t * memory_start = alloca(memory_len);
uint8_t * memory_p = memory_start;
ctx_t * c = (ctx_t *) memory_p;
memory_p += sizeof(ctx_t);
c->stack.data = (int *) memory_p;
c->stack_base = (int *) memory_p;
memory_p += STACK_CELL_COUNT * 4;
c->stack.max = STACK_CELL_COUNT;
c->stack.len = 0;
c->return_stack = (int *) memory_p;
memory_p += STACK_CELL_COUNT * 4;
/* inherit these from the root */
c->runtime_cbs = root->runtime_cbs;
c->special_runtime_cbs = root->special_runtime_cbs;
c->data = root->data;
c->callback_info = root->callback_info;
c->callback_word_locations = root->callback_word_locations;
c->variables = root->variables;
c->callback_targets = root->callback_targets;
c->literals = root->literals;
c->bin_start = root->bin_start;
assert(root->total_extra_memory_size == m4_bytes_remaining(memory_start, memory_p, memory_len));
c->total_extra_memory_size = root->total_extra_memory_size;
c->memory = memory_p;
global_thread_t global_thread;
m4_global_thread_set(&global_thread, c);
c->stack.data[0] = arg;
c->stack.data += 1;
c->stack.len += 1;
m4_esp32s3_engine_run_asm(c, target);
assert(c->stack.len >= 1);
return (void *) c->stack.data[-1];
}
static const m4_engine_thread_create_param_t engine_thread_create_param = {
.extra_stack_space_get_cb = thread_extra_stack_space_get,
.thread_entry = thread_entry,
};
#else /*M4_NO_THREAD*/
int m4_engine_thread_create(void * param, m4_stack_t * stack)
{
assert(0);
}
#endif
static int thread_join(void * param, m4_stack_t * stack)
{
#ifndef M4_NO_THREAD
assert(stack->len >= 1);
void * retval;
int res = pthread_join((pthread_t) stack->data[-1], &retval);
assert(res == 0);
stack->data[-1] = (int) retval;
#else
assert(0);
#endif
return 0;
}
#ifndef M4_NO_THREAD
static const m4_runtime_cb_pair_t thread_create_cb_pair = {m4_engine_thread_create, (void *) &engine_thread_create_param};
#else
static const m4_runtime_cb_pair_t thread_create_cb_pair = {m4_engine_thread_create, NULL};
#endif
static const m4_runtime_cb_pair_t thread_join_cb_pair = {thread_join, NULL};
static const m4_runtime_cb_pair_t * const special_runtime_cbs[] = {
&thread_create_cb_pair,
&thread_join_cb_pair,
};
int m4_esp32s3_engine_run(
const uint8_t * bin,
const uint8_t * code,
uint8_t * memory_start,
int memory_len,
const m4_runtime_cb_array_t * const * cb_arrays,
const char ** missing_runtime_word_dst
)
{
int res;
int callbacks_used;
res = m4_global_main_get_callbacks_used(&callbacks_used);
if(res) return res;
uint8_t * memory_p = m4_align(memory_start);
ctx_t * c = (ctx_t *) memory_p;
memory_p += sizeof(ctx_t);
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
c->stack.data = (int *) memory_p;
c->stack_base = (int *) memory_p;
memory_p += STACK_CELL_COUNT * 4;
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
c->stack.max = STACK_CELL_COUNT;
c->stack.len = 0;
c->return_stack = (int *) memory_p;
memory_p += STACK_CELL_COUNT * 4;
if(m4_bytes_remaining(memory_start, memory_p, memory_len) < 0) return M4_OUT_OF_MEMORY_ERROR;
int callback_count;
res = m4_unpack_binary_header(
bin,
memory_p,
m4_bytes_remaining(memory_start, memory_p, memory_len),
cb_arrays,
missing_runtime_word_dst,
M4_MAX_CALLBACKS - callbacks_used,
&callback_count,
&c->variables,
&c->runtime_cbs,
&c->data,
&c->callback_info,
&c->callback_word_locations,
&c->literals,
&c->bin_start,
&c->memory
);
if(res) return res;
if(code) {
for(int i = 0; i < callback_count; i++) {
c->callback_word_locations[i] = code + (c->callback_word_locations[i] - c->bin_start);
}
c->bin_start = code;
}
c->callback_targets = callback_targets + callbacks_used;
c->special_runtime_cbs = special_runtime_cbs;
c->total_extra_memory_size = m4_bytes_remaining(memory_start, c->memory, memory_len);
m4_global_main_callbacks_set_ctx(callback_count, c);
m4_esp32s3_engine_run_asm(c, c->bin_start);
return 0;
}