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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
|
#include <math.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_time.h>
#include <SDL3/SDL_timer.h>
#include "env.h"
#include "pico8.h"
// PICO-8 allocates a fix 128 Mb heap unconditinoally; but for some platforms,
// that may be too much. Specify a lower bound, in 64Kb pages, for the memory
// that we allocate in our Web Assembly environment. This _technically_ does
// not respect the module's minimum memory size. This lower bound corresponds
// to 32 Mb, and is large enough to write the data at the fixed memory offsets
// (such as the dynamic top value, and the time data).
#define ENV_MIN_PAGES 512
#define ENV_DYNAMICTOP_PTR 3199024
#define ENV_DYNAMIC_BASE 8441936
#define ENV_ERROR_MAX_SIZE 256
#define ENV_KEYDOWN 0x300
#define ENV_KEYUP 0x301
#define ENV_MOUSEMOTION 0x400
// PICO-8 engine is compiled with the Emscripten built-in SDL1.3 library, which
// has a different pixel format encoding than SDL3 which is used on the host.
// The pixel format used by the engine is:
#define ENV_PIXELFORMAT_ABGR8888 0x86762004
#define ENV_TM_CURRENT 3198880
#define ENV_TM_TIMEZONE 3198928
#define ENV_SECONDS_PER_DAY (1000 * 60 * 60 * 24)
#define ENV_NS_PER_YEAR 31556952000000000
struct w2c_env *pw_env_init(size_t memory) {
size_t bytes = memory << 20;
size_t pages = bytes / wasm2c_pico8_pagesize_env_memory;
if (bytes < memory || pages < ENV_MIN_PAGES ||
pages > wasm2c_pico8_max_env_memory) {
SDL_InvalidParamError("memory");
return NULL;
}
struct w2c_env *env = SDL_calloc(1, sizeof(*env));
if (!env) {
return NULL;
}
env->memory.data = SDL_calloc(pages, wasm2c_pico8_pagesize_env_memory);
if (!env->memory.data) {
goto error;
}
env->memory.size = pages * wasm2c_pico8_pagesize_env_memory;
env->memory.data_end = env->memory.data + env->memory.size;
env->table.data =
SDL_calloc(wasm2c_pico8_min_env_table, sizeof(*env->table.data));
if (!env->table.data) {
goto error;
}
env->table.size = wasm2c_pico8_min_env_table;
return env;
error:
if (env->memory.data) {
SDL_free(env->memory.data);
}
if (env->table.data) {
SDL_free(env->table.data);
}
SDL_free(env);
return NULL;
}
void pw_env_free(struct w2c_env *env) {
if (!env) {
SDL_InvalidParamError("env");
return;
}
SDL_free(env->memory.data);
SDL_free(env->table.data);
SDL_free(env);
}
bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context) {
if (!env) {
return SDL_InvalidParamError("env");
}
if (env->memory.size < ENV_MIN_PAGES * wasm2c_pico8_pagesize_env_memory) {
return SDL_SetError("cannot write to fixed memory offsets");
}
if (!context.fb || !context.input || !context.cartdat || !context.instance) {
return SDL_InvalidParamError("context");
}
SDL_memset(env->memory.data, 0, env->memory.size);
SDL_memset(env->table.data, 0, env->table.size * sizeof(*env->table.data));
env->context = context;
SDL_zerop(&env->data);
// Prepare the memory at fixed offsets that come from the host:
env->data.dynamictop = ENV_DYNAMICTOP_PTR;
wasm_rt_memory_store32_unchecked(&env->memory, env->data.dynamictop,
ENV_DYNAMIC_BASE);
char tz[] = "GMT";
wasm_rt_memory_store_unchecked(&env->memory, ENV_TM_TIMEZONE, tz, sizeof(tz));
return true;
}
/**
* Convenience macro for writing to wasm memory and moving the wasm pointer
* forward for that amount.
*/
#define MEM_WRITE(n, e, a, v) \
do { \
wasm_rt_memory_store##n##_unchecked(&e->memory, a, v); \
a += (n / 8); \
} while (0)
static uint32_t env_swap32(uint32_t *slot, uint32_t value) {
uint32_t result = *slot;
*slot = value;
return result;
}
static uint32_t env_strndup(struct w2c_env *env, char const *s, uint32_t sz) {
if (!sz) {
size_t ssz = SDL_strlen(s) + 1;
if (ssz > UINT32_MAX) {
wasm_rt_trap(WASM_RT_TRAP_OOB);
}
sz = (uint32_t)ssz;
}
uint32_t ptr = w2c_pico8_0x5Fmalloc(env->context.instance, sz);
wasm_rt_memory_store(&env->memory, ptr, s, sz);
return ptr;
}
static uint32_t env_ticks(void) {
return (uint32_t)(SDL_GetTicks() & UINT32_MAX);
}
static uint32_t env_timetm(struct w2c_env *env, uint32_t time,
bool local_time) {
int64_t ns = SDL_SECONDS_TO_NS(time);
SDL_DateTime dt;
if (!SDL_TimeToDateTime(ns, &dt, local_time)) {
return 0;
}
SDL_DateTime jan = {
.year = dt.year,
.month = 1,
.day = 1,
.hour = 0,
.minute = 0,
.second = 0,
.nanosecond = 0,
.day_of_week = 0,
.utc_offset = dt.utc_offset,
};
if (!SDL_DateTimeToTime(&jan, &ns)) {
return 0;
}
int day_of_year = (time - SDL_NS_TO_SECONDS(ns)) / ENV_SECONDS_PER_DAY;
bool is_dst;
if (local_time) {
SDL_DateTime jul;
if (!SDL_TimeToDateTime(ns, &jan, true) ||
!SDL_TimeToDateTime(ns + ENV_NS_PER_YEAR / 2, &jul, true)) {
return 0;
}
is_dst = jan.utc_offset != jul.utc_offset &&
dt.utc_offset == SDL_min(jan.utc_offset, jul.utc_offset);
} else {
is_dst = false;
}
uint32_t tm = ENV_TM_CURRENT;
MEM_WRITE(32, env, tm, dt.second);
MEM_WRITE(32, env, tm, dt.minute);
MEM_WRITE(32, env, tm, dt.hour);
MEM_WRITE(32, env, tm, dt.day);
MEM_WRITE(32, env, tm, dt.month);
MEM_WRITE(32, env, tm, dt.year - 1900);
MEM_WRITE(32, env, tm, dt.day_of_week);
MEM_WRITE(32, env, tm, day_of_year);
MEM_WRITE(32, env, tm, (uint32_t)is_dst);
MEM_WRITE(32, env, tm, dt.utc_offset);
if (local_time) {
uint32_t tz = w2c_pico8_0x5F_get_timezone(env->context.instance);
wasm_rt_memory_store32(&env->memory, tz, dt.utc_offset);
uint32_t ds = w2c_pico8_0x5F_get_timezone(env->context.instance);
wasm_rt_memory_store32(&env->memory, ds, dt.utc_offset);
// SDL does not provide an API for getting a timezone name, and we do not
// want to rely on POSIX for this. Just use a fake `LT` and `LST` for
// "local time" and "local daylight savings time" respectively.
uint32_t tn = w2c_pico8_0x5F_get_tzname(env->context.instance);
if (is_dst) {
char const name[] = "LST";
wasm_rt_memory_store(&env->memory, tn, name, sizeof(name));
} else {
char const name[] = "LT";
wasm_rt_memory_store(&env->memory, tn, name, sizeof(name));
};
MEM_WRITE(32, env, tm, tn);
} else {
MEM_WRITE(32, env, tm, ENV_TM_TIMEZONE);
}
return ENV_TM_CURRENT;
}
static inline SDL_NORETURN void env_abort() {
wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
}
static SDL_NORETURN void env_unimplemented(char const *name) {
SDL_LogCritical(0, "called unimplemented function: %s", name);
env_abort();
}
static SDL_NORETURN void env_badasm(char const *i, uint32_t code) {
SDL_LogCritical(0, "unknown ASM (%s) code %x", i, code);
env_abort();
}
uint32_t *w2c_env_DYNAMICTOP_PTR(struct w2c_env *env) {
return &env->data.dynamictop;
}
uint32_t *w2c_env_0x5F_table_base(struct w2c_env *env) {
return &env->data.tablebase;
}
struct wasm_rt_memory_t *w2c_env_memory(struct w2c_env *env) {
return &env->memory;
}
struct wasm_rt_funcref_table_t *w2c_env_table(struct w2c_env *env) {
return &env->table;
}
void w2c_env_0x5FSDL_ClearError(struct w2c_env *env) {
(void)env;
SDL_ClearError();
}
uint32_t w2c_env_0x5FSDL_GetError(struct w2c_env *env) {
char err[ENV_ERROR_MAX_SIZE] = {0};
SDL_strlcpy(err, SDL_GetError(), ENV_ERROR_MAX_SIZE);
wasm_rt_memory_store(&env->memory, env->data.errstr, err, ENV_ERROR_MAX_SIZE);
return env->data.errstr;
}
uint32_t w2c_env_0x5FSDL_GetModState(struct w2c_env *env) {
return (uint32_t)pw_input_keymod(env->context.input);
}
uint32_t w2c_env_0x5FSDL_GetMouseState(struct w2c_env *env, uint32_t x,
uint32_t y) {
struct pw_coordinates pos;
enum pw_mouse_button button = pw_input_mouse(env->context.input, &pos);
if (x) {
wasm_rt_memory_store32(&env->memory, x, pos.x);
}
if (y) {
wasm_rt_memory_store32(&env->memory, y, pos.y);
}
return (uint32_t)button;
}
uint32_t w2c_env_0x5FSDL_GetTicks(struct w2c_env *env) {
(void)env;
return env_ticks();
}
void w2c_env_0x5FSDL_GetWindowSize(struct w2c_env *env, uint32_t window,
uint32_t x, uint32_t y) {
(void)window;
wasm_rt_memory_store32(&env->memory, x, PW_FB_WIDTH);
wasm_rt_memory_store32(&env->memory, y, PW_FB_HEIGHT);
}
uint32_t w2c_env_0x5FSDL_Init(struct w2c_env *env, uint32_t flags) {
(void)flags;
env->data.errstr =
w2c_pico8_0x5Fmalloc(env->context.instance, ENV_ERROR_MAX_SIZE);
return 0;
}
void w2c_env_0x5FSDL_LockAudio(struct w2c_env *env) {
(void)env;
env_unimplemented("_SDL_LockAudio");
}
uint32_t w2c_env_0x5FSDL_LockSurface(struct w2c_env *env, uint32_t surface) {
if (!surface) {
SDL_InvalidParamError("surface");
return (uint32_t)(-1);
}
wasm_rt_memory_store32(&env->memory, surface + 28, 1);
return 0;
}
uint32_t w2c_env_0x5FSDL_PollEvent(struct w2c_env *env, uint32_t event) {
if (!event) {
return (uint32_t)pw_input_poll(env->context.input, NULL);
}
union pw_input_event input;
bool remaining = pw_input_poll(env->context.input, &input);
if (remaining) {
wasm_rt_memory_check(&env->memory, event, 36);
switch (input.type) {
case PW_INPUT_EVENT_KEY:
if (input.key.pressed) {
MEM_WRITE(32, env, event, ENV_KEYDOWN);
} else {
MEM_WRITE(32, env, event, ENV_KEYUP);
}
MEM_WRITE(32, env, event, 1); // windowID
MEM_WRITE(8, env, event, (uint8_t)input.key.pressed);
MEM_WRITE(8, env, event, (uint8_t)input.key.repeat);
MEM_WRITE(8, env, event, 0); // padding2
MEM_WRITE(8, env, event, 0); // padding3
MEM_WRITE(32, env, event, input.key.scancode);
MEM_WRITE(32, env, event, input.key.keycode);
MEM_WRITE(16, env, event, (uint16_t)input.key.mod);
MEM_WRITE(16, env, event, 0); // keysym.{padding}
MEM_WRITE(32, env, event, 0); // keysym.unicode (deprecated)
break;
case PW_INPUT_EVENT_MOUSE:
MEM_WRITE(32, env, event, ENV_MOUSEMOTION);
MEM_WRITE(32, env, event, env_ticks());
MEM_WRITE(32, env, event, 1); // windowID
MEM_WRITE(32, env, event, 1); // which
MEM_WRITE(32, env, event, (uint32_t)input.mouse.button);
MEM_WRITE(32, env, event, input.mouse.x);
MEM_WRITE(32, env, event, input.mouse.y);
MEM_WRITE(32, env, event, input.mouse.xrel);
MEM_WRITE(32, env, event, input.mouse.yrel);
break;
};
}
return (uint32_t)remaining;
}
uint32_t w2c_env_0x5FSDL_SetVideoMode(struct w2c_env *env, uint32_t w,
uint32_t h, uint32_t bpp,
uint32_t flags) {
if (w != PW_FB_WIDTH || h != PW_FB_HEIGHT || bpp != 32 || flags != 0) {
SDL_SetError("unsupported video mode");
return 0;
}
uint32_t result =
w2c_pico8_0x5Fmalloc(env->context.instance, 60 + 44 + PW_FB_SIZE);
if (!result) {
SDL_SetError("failed to allocate surface");
return 0;
}
uint32_t surface = result;
uint32_t format = surface + 60;
uint32_t pixels = format + 44;
wasm_rt_memory_check(&env->memory, pixels, PW_FB_SIZE);
{
MEM_WRITE(32, env, surface, flags);
MEM_WRITE(32, env, surface, format);
MEM_WRITE(32, env, surface, w);
MEM_WRITE(32, env, surface, h);
MEM_WRITE(32, env, surface, (w * bpp) / 8);
MEM_WRITE(32, env, surface, pixels);
MEM_WRITE(32, env, surface, 0); // userdata
MEM_WRITE(32, env, surface, 0); // locked
MEM_WRITE(32, env, surface, 0); // lock_data
MEM_WRITE(32, env, surface, 0); // clip_rect.x
MEM_WRITE(32, env, surface, 0); // clip_rect.y
MEM_WRITE(32, env, surface, w); // clip_rect.w
MEM_WRITE(32, env, surface, h); // clip_rect.h
MEM_WRITE(32, env, surface, 0); // map
MEM_WRITE(32, env, surface, 1); // refcount
}
{
MEM_WRITE(32, env, format, ENV_PIXELFORMAT_ABGR8888);
MEM_WRITE(32, env, format, 0); // palette
MEM_WRITE(8, env, format, (uint8_t)bpp);
MEM_WRITE(8, env, format, (uint8_t)bpp / 8);
MEM_WRITE(16, env, format, 0); // padding
MEM_WRITE(32, env, format, 0x000000ff); // rmask
MEM_WRITE(32, env, format, 0x0000ff00); // gmask
MEM_WRITE(32, env, format, 0x00ff0000); // bmask
MEM_WRITE(32, env, format, 0xff000000); // amask
MEM_WRITE(8, env, format, 0); // rloss
MEM_WRITE(8, env, format, 0); // gloss
MEM_WRITE(8, env, format, 0); // bloss
MEM_WRITE(8, env, format, 0); // aloss
MEM_WRITE(8, env, format, 0); // rshift
MEM_WRITE(8, env, format, 8); // gshift
MEM_WRITE(8, env, format, 16); // bshift
MEM_WRITE(8, env, format, 24); // ashift
MEM_WRITE(32, env, format, 1); // refcount
MEM_WRITE(32, env, format, 0); // next
}
{
wasm_rt_memory_set(&env->memory, pixels, PW_FB_SIZE, 0);
}
return result;
}
void w2c_env_0x5FSDL_UnlockAudio(struct w2c_env *env) {
(void)env;
env_unimplemented("_SDL_UnlockAudio");
}
void w2c_env_0x5FSDL_UnlockSurface(struct w2c_env *env, uint32_t surface) {
if (!surface) {
SDL_InvalidParamError("surface");
return;
}
wasm_rt_memory_check(&env->memory, surface, 60);
// Always clear the locked flag as unlocking should never fail.
wasm_rt_memory_store32_unchecked(&env->memory, surface + 28, 0);
// Now copy over the screen from wasm memory to our framebuffer. Note that
// we bounds check _before_ locking the framebuffer, this prevents OOB access
// to trap whitout properly unlocking it.
uint32_t pixels = wasm_rt_memory_load32_unchecked(&env->memory, surface + 20);
wasm_rt_memory_check(&env->memory, pixels, PW_FB_SIZE);
uint8_t *buffer = pw_fb_lock(env->context.fb);
if (!buffer) {
SDL_SetError("unable to lock framebuffer");
return;
}
wasm_rt_memory_load_unchecked(&env->memory, pixels, buffer, PW_FB_SIZE);
pw_fb_unlock(env->context.fb);
}
void w2c_env_0x5F_0x5FbuildEnvironment(struct w2c_env *env, uint32_t environ) {
char const *vars[] = {"USER=picow", "LOGNAME=picow", "PATH=/",
"PWD=/", "HOME=/home/picow", "LANG=C.UTF-8",
"_=./picow"};
size_t count = SDL_arraysize(vars);
uint32_t array = w2c_pico8_0x5Fmalloc(env->context.instance,
(count + 1) * sizeof(uint32_t));
for (uint32_t i = 0; i < count; ++i) {
uint32_t addr = array + i * sizeof(uint32_t);
uint32_t str = env_strndup(env, vars[i], 0);
wasm_rt_memory_store32(&env->memory, addr, str);
}
wasm_rt_memory_store32(&env->memory, array + count * sizeof(uint32_t), 0);
wasm_rt_memory_store32(&env->memory, environ, array);
}
void w2c_env_0x5F_0x5Flock(struct w2c_env *env, uint32_t ptr) {
(void)env;
(void)ptr;
}
void w2c_env_0x5F_0x5FsetErrNo(struct w2c_env *env, uint32_t value) {
uint32_t errno = w2c_pico8_0x5F_0x5Ferrno_location(env->context.instance);
wasm_rt_memory_store32(&env->memory, errno, value);
}
uint32_t w2c_env_0x5F_0x5Fsyscall140(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall140");
}
uint32_t w2c_env_0x5F_0x5Fsyscall145(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall145");
}
uint32_t w2c_env_0x5F_0x5Fsyscall146(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall146");
}
uint32_t w2c_env_0x5F_0x5Fsyscall195(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall195");
}
uint32_t w2c_env_0x5F_0x5Fsyscall220(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall220");
}
uint32_t w2c_env_0x5F_0x5Fsyscall221(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall221");
}
uint32_t w2c_env_0x5F_0x5Fsyscall5(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall5");
}
uint32_t w2c_env_0x5F_0x5Fsyscall54(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall54");
};
uint32_t w2c_env_0x5F_0x5Fsyscall6(struct w2c_env *env, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
env_unimplemented("___syscall6");
}
void w2c_env_0x5F_0x5Funlock(struct w2c_env *env, uint32_t ptr) {
(void)env;
(void)ptr;
}
void w2c_env_0x5Fabort(struct w2c_env *env) {
(void)env;
SDL_LogCritical(0, "aborting");
env_abort();
}
uint32_t w2c_env_0x5Femscripten_asm_const_i(struct w2c_env *env,
uint32_t code) {
switch (code) {
case 1:
// Whether or not there is embedded cart data. Since we _only_ run with
// embedded cart data, this is always 1 (i.e. true).
return 1;
case 6:
// Command setup, no-op for us.
return 0;
case 11:
return env_swap32(&env->data.command, 0);
case 12:
return env_swap32(&env->data.commandp, 0);
case 16:
// On module load callback, no-op.
return 0;
default:
env_badasm("i", code);
}
}
uint32_t w2c_env_0x5Femscripten_asm_const_ii(struct w2c_env *env, uint32_t code,
uint32_t a) {
(void)env;
(void)a;
switch (code) {
default:
env_badasm("ii", code);
}
}
uint32_t w2c_env_0x5Femscripten_asm_const_iii(struct w2c_env *env,
uint32_t code, uint32_t a,
uint32_t b) {
(void)env;
(void)a;
(void)b;
switch (code) {
default:
env_badasm("iii", code);
}
}
uint32_t w2c_env_0x5Femscripten_asm_const_iiiii(struct w2c_env *env,
uint32_t code, uint32_t a,
uint32_t b, uint32_t c,
uint32_t d) {
(void)env;
(void)a;
(void)b;
(void)c;
(void)d;
switch (code) {
default:
env_badasm("iiiii", code);
}
}
uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiii(struct w2c_env *env,
uint32_t code, uint32_t a,
uint32_t b, uint32_t c,
uint32_t d, uint32_t e,
uint32_t f) {
(void)env;
(void)a;
(void)b;
(void)c;
(void)d;
(void)e;
(void)f;
switch (code) {
default:
env_badasm("iiiiiii", code);
}
}
uint32_t w2c_env_0x5Femscripten_asm_const_iiiiiiiii(
struct w2c_env *env, uint32_t code, uint32_t a, uint32_t b, uint32_t c,
uint32_t d, uint32_t e, uint32_t f, uint32_t g, uint32_t h) {
(void)env;
(void)a;
(void)b;
(void)c;
(void)d;
(void)e;
(void)f;
(void)g;
(void)h;
switch (code) {
default:
env_badasm("iiiiiiiii", code);
}
}
void w2c_env_0x5Femscripten_async_wget_data(struct w2c_env *env, uint32_t url,
uint32_t userdata, uint32_t onload,
uint32_t onerror) {
(void)onload;
char urlstr[256] = {0};
wasm_rt_memory_load(&env->memory, url, urlstr, sizeof(url) - 1);
SDL_LogDebug(0, "ignoring attempt to fetch %s", urlstr);
w2c_pico8_dynCall_vi(env->context.instance, onerror, userdata);
}
uint32_t w2c_env_0x5Femscripten_get_heap_size(struct w2c_env *env) {
return env->memory.size;
}
uint32_t w2c_env_0x5Femscripten_memcpy_big(struct w2c_env *env, uint32_t dst,
uint32_t src, uint32_t len) {
wasm_rt_memory_copy(&env->memory, dst, src, len);
return dst;
}
uint32_t w2c_env_0x5Femscripten_resize_heap(struct w2c_env *env,
uint32_t size) {
// Don't allow heap resizing.
(void)env;
(void)size;
return 0;
}
void w2c_env_0x5Femscripten_run_script(struct w2c_env *env, uint32_t src) {
(void)env;
(void)src;
SDL_LogDebug(0, "ignoring attempt to run script on host");
}
void w2c_env_0x5Femscripten_set_main_loop(struct w2c_env *env, uint32_t func,
uint32_t fps,
uint32_t simulate_infinite_loop) {
env->data.mainloop = func;
// Ignore the fps and simulate infinite loop parameters, they are always 0
// for PICO-8 anyway.
(void)fps;
(void)simulate_infinite_loop;
}
void w2c_env_0x5Fexit(struct w2c_env *env, uint32_t code) {
// Simulate an exit by signaling that an exit was requested and then trap,
// and handling this in the host.
SDL_LogDebug(0, "exiting with code: %d", code);
env->data.exit = true;
wasm_rt_trap(WASM_RT_TRAP_UNREACHABLE);
}
uint32_t w2c_env_0x5Fgmtime(struct w2c_env *env, uint32_t time) {
return env_timetm(env, time, false);
}
uint32_t w2c_env_0x5Flocaltime(struct w2c_env *env, uint32_t time) {
return env_timetm(env, time, true);
}
void w2c_env_0x5Flongjmp(struct w2c_env *env, uint32_t jmpbuf, uint32_t value) {
(void)env;
(void)jmpbuf;
(void)value;
env_unimplemented("_longjmp");
}
uint32_t w2c_env_0x5Fstrftime(struct w2c_env *env, uint32_t buf, uint32_t sz,
uint32_t fmt, uint32_t tm) {
// AFAICT, PICO-8 only ever formats `struct tm`s with a single format string,
// so instead of relying on `strftime` (which has quite a few portability
// papercuts), just implement the bear minimum of what we need.
char buffer[64] = {0};
// Check that the format matches the single one we support, and it not, write
// the empty string and return a written length of 0.
wasm_rt_memory_load(&env->memory, fmt, buffer, sizeof(buffer) - 1);
if (SDL_strcmp(buffer, "%Y-%m-%d %H:%M:%S")) {
wasm_rt_memory_store8(&env->memory, buf, 0);
return 0;
}
wasm_rt_memory_check(&env->memory, tm, 44);
SDL_DateTime dt = {
.second = wasm_rt_memory_load32_unchecked(&env->memory, tm + 0),
.minute = wasm_rt_memory_load32_unchecked(&env->memory, tm + 4),
.hour = wasm_rt_memory_load32_unchecked(&env->memory, tm + 8),
.day = wasm_rt_memory_load32_unchecked(&env->memory, tm + 12),
.month = wasm_rt_memory_load32_unchecked(&env->memory, tm + 16),
.year = wasm_rt_memory_load32_unchecked(&env->memory, tm + 20) + 1900,
};
int n =
SDL_snprintf(buffer, sizeof(buffer), "%d-%02d-%02d %02d:%02d:%02d",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
if (n < 0 || (size_t)n >= sizeof(buffer)) {
wasm_rt_memory_store8(&env->memory, buf, 0);
return 0;
}
if (sz) {
wasm_rt_memory_store(&env->memory, buf, buffer, sz - 1);
wasm_rt_memory_store8(&env->memory, buf + sz - 1, 0);
}
return n;
}
uint32_t w2c_env_0x5Ftime(struct w2c_env *env, uint32_t time) {
SDL_Time ns;
if (!SDL_GetCurrentTime(&ns)) {
return (uint32_t)(-1);
}
uint32_t s = SDL_NS_TO_SECONDS(ns);
if (time) {
wasm_rt_memory_store32(&env->memory, time, s);
}
return s;
}
void w2c_env_abort(struct w2c_env *env, uint32_t code) {
(void)env;
SDL_LogCritical(0, "aborting with code 0x%08x", code);
env_abort();
}
uint32_t w2c_env_abortOnCannotGrowMemory(struct w2c_env *env, uint32_t size) {
(void)env;
SDL_LogCritical(0, "aborting unable to grow memory to size %u", size);
env_abort();
}
uint32_t w2c_env_getTempRet0(struct w2c_env *env) { return env->data.tempret; }
void w2c_env_invoke_vii(struct w2c_env *env, uint32_t a, uint32_t b,
uint32_t c) {
(void)env;
(void)a;
(void)b;
(void)c;
env_unimplemented("invoke_vii");
}
void w2c_env_setTempRet0(struct w2c_env *env, uint32_t value) {
env->data.tempret = value;
}
static struct w2c_global shared_global = {
.infinity = INFINITY,
.nan = NAN,
};
double *w2c_global_Infinity(struct w2c_global *global) {
if (!global) {
global = &shared_global;
}
return &global->infinity;
}
double *w2c_global_NaN(struct w2c_global *global) {
if (!global) {
global = &shared_global;
}
return &global->nan;
}
|