aboutsummaryrefslogtreecommitdiff
path: root/src/env.h
blob: a25fec0339509cfe1ff26df3833576ee92ae16e7 (plain)
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
#ifndef PW_WENV_H
#define PW_WENV_H

#include "audio.h"
#include "cart.h"
#include "fb.h"
#include "input.h"
#include "pico8.h"
#include "wasmrt.h"

#define PW_ENV_GPIO_SIZE 128

/**
 * A host execution context.
 *
 * Contains _borrowed_ references to various components required by the
 * environment host functions.
 */
struct pw_env_ctx {
  struct pw_framebuffer *fb;
  struct pw_audio *audio;
  struct pw_input *input;
  struct pw_cart *cart;
  struct w2c_pico8 *instance;
};

/**
 * An invocation frame exception handler.
 */
struct pw_env_frame {
  struct pw_env_frame *parent;
  jmp_buf buf;
};

/**
 * The Web Assembly host environment.
 *
 * This holds the required state to implement the host functions imported by
 * the PICO-8 `.wasm` module.
 */
struct w2c_env {
  struct wasm_rt_memory_t memory;
  struct wasm_rt_funcref_table_t table;
  struct pw_env_ctx context;

  struct pw_env_data {
    struct pw_env_frame *frame;
    uint32_t dynamictop;
    uint32_t tablebase;
    uint32_t mainloop;
    uint32_t errstr;
    uint32_t tempret;
    uint32_t cartpos;
    struct {
      uint32_t index;
      uint32_t pos;
    } cartname;
    uint32_t command;
    uint32_t commandp;
    struct {
      bool pointerlock;
      bool fssync;
      volatile bool shutdown;
    } state;
    uint32_t btn;
    uint8_t gpio[PW_ENV_GPIO_SIZE];
    size_t cliplen;
  } data;
};

/**
 * The Web Assembly host globals.
 */
struct w2c_global {
  double infinity;
  double nan;
};

/**
 * Allocates a new host environment with the specified Web Assembly memory size.
 */
struct w2c_env *pw_env_init(size_t memory);

/**
 * Frees the host environement.
 */
void pw_env_free(struct w2c_env *env);

/**
 * Sets a host environment's execution context.
 */
bool pw_env_link(struct w2c_env *env, struct pw_env_ctx context);

/**
 * Returns whether or not a shutdown is requested.
 */
bool pw_env_shutdown(struct w2c_env *env);

#endif