Skip to content

Commit

Permalink
Add a recipe for emscripten event loop
Browse files Browse the repository at this point in the history
Use recipe_emscript in emscripten build
  • Loading branch information
kulp committed Jun 6, 2016
1 parent 46eb6e2 commit dad3b18
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ clean_FILES = $(addprefix $(BUILDDIR)/, \
common_OBJECTS = common.o $(patsubst %.c,%.o,$(notdir $(wildcard $(OS_PATHS:%=%/*.c))))
tas_OBJECTS = $(common_OBJECTS) asmif.o asm.o obj.o parser.o lexer.o param.o
tsim_OBJECTS = $(common_OBJECTS) simif.o asm.o obj.o plugin.o \
$(DEVOBJS) sim.o param.o
$(DEVOBJS) sim.o param.o emscripten.o
tld_OBJECTS = $(common_OBJECTS) obj.o

ifeq ($(USE_OWN_SEARCH),1)
Expand Down
11 changes: 11 additions & 0 deletions src/emscripten.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "common.h"

struct sim_state;

int recipe_emscript(struct sim_state *s)
{
(void)s;
fatal(0, "emscripten recipe not applicable in this build");
return -1;
}

2 changes: 1 addition & 1 deletion src/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct run_ops {
};

typedef int sim_runner(struct sim_state *s, const struct run_ops *ops, void **run_data, void *ops_data);
extern sim_runner interp_run_sim;
extern sim_runner interp_run_sim, interp_step_sim;
int load_sim(op_dispatcher *dispatch_op, void *sud, const struct format *f,
void *fud, FILE *in, int load_address);

Expand Down
3 changes: 3 additions & 0 deletions src/tsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
#include <search.h>
#include <inttypes.h>

int recipe_emscript(struct sim_state *s); // linked in externally

#define RECIPES(_) \
_(prealloc, "preallocate memory (higher memory footprint, maybe faster)") \
_(sparse , "use sparse memory (lower memory footprint, maybe slower)") \
_(serial , "enable simple serial device and connect to stdio") \
_(plugin , "load plugins specified through param mechanism") \
_(jit , "use a JIT compiler (usually faster, but no -v supported)") \
_(emscript, "change behaviour to use an event loop for emscripten") \
//

#define DEFAULT_RECIPES(_) \
Expand Down
2 changes: 1 addition & 1 deletion ui/web/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ tcc.bc: LDFLAGS += -O2
tas$(EXE_SUFFIX) tsim$(EXE_SUFFIX): common.o
tas$(EXE_SUFFIX) tld$(EXE_SUFFIX): tsearch.o lsearch.o
tas$(EXE_SUFFIX): asm.o asmif.o param.o parser.o lexer.o obj.o
tsim$(EXE_SUFFIX): sim.o simif.o param.o asm.o obj.o ram.o serial.o lsearch.o
tsim$(EXE_SUFFIX): sim.o simif.o param.o asm.o obj.o ram.o serial.o lsearch.o emscripten.o
tld$(EXE_SUFFIX): common.o obj.o

tsim.js: CC_OPT = -O2 # -O0 causes failure
Expand Down
2 changes: 1 addition & 1 deletion ui/web/general_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ set_up(tenyr_state,'tas');
set_up(tenyr_state,'tsim');

tenyr_state.tas.args = ['-ftext', '-'];
tenyr_state.tsim.args = ['-n', '-rprealloc', '-rserial', '-ftext', '-'];
tenyr_state.tsim.args = ['-n', '-rprealloc', '-rserial', '-remscript', '-ftext', '-'];

39 changes: 39 additions & 0 deletions ui/web/src/emscripten.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "emscripten.h"
#include "sim.h"

struct emscripten_wrap {
struct sim_state *s;
const struct run_ops *ops;
void **run_data;
void *ops_data;
int rc;
};

static void emscripten_wrap_step(void *data_)
{
struct emscripten_wrap *d = data_;
d->rc = interp_run_sim_block(d->s, d->ops, d->run_data, d->ops_data);
}

static int emscripten_setup(struct sim_state *s, const struct run_ops *ops,
void **run_data, void *ops_data)
{
struct emscripten_wrap data = {
.s = s,
.ops = ops,
.run_data = run_data,
.ops_data = ops_data,
};

emscripten_cancel_main_loop(); // In case we get called more than once
emscripten_set_main_loop_arg(emscripten_wrap_step, &data, 1000, 1);

return data.rc;
}

int recipe_emscript(struct sim_state *s)
{
s->run_sim = emscripten_setup;
return 0;
}

0 comments on commit dad3b18

Please sign in to comment.