Skip to content

Commit

Permalink
Attempt at emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
Spritetm committed Jun 26, 2024
1 parent fc1028e commit a27db6d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
24 changes: 23 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,29 @@ jobs:
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- uses: actions/checkout@v4
- name: Build
- name: Build C program
working-directory: ${{ github.workspace }}/
run: make


emscripten:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Checkout binaries for web version
working-directory: ${{ github.workspace }}/
run: |
git clone --depth=1 https://github.com/misterblack1/plexus-p20.git
mv plexus-p20/ROMs/U17-MERGED.BIN plexus-p20/ROMs/U15-MERGED.BIN .
gunzip < plexus-p20/disk/plexus-sanitized.img.gz > plexus-sanitized.img
- name: Get emscripten
working-directory: ${{ github.workspace }}/
run: |
git clone --depth=1 https://github.com/emscripten-core/emsdk.git
cd emsdk; ./emsdk install latest; ./emsdk activate latest
- name: Compile for web
working-directory: ${{ github.workspace }}/
run: source ./emsdk/emsdk_env.sh; make plexem.mjs

3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "xterm-pty"]
path = xterm-pty
url = https://github.com/mame/xterm-pty.git
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ Musashi/m68kops.h:
emu: $(SRC:.c=.o)
$(CC) $(CFLAGS) -o $@ $^ -lm


# Note that PROXY_TO_PTHREAD doesn't generally work as the needed
# SharedArrayBuffer needs some pretty specific server settings.
EMCC_ARGS = -s ASYNCIFY
#EMCC_ARGS = -s PROXY_TO_PTHREAD -pthread
EMCC_ARGS += --js-library xterm-pty/emscripten-pty.js
EMCC_ARGS += --preload-file plexus-sanitized.img
EMCC_ARGS += --preload-file U15-MERGED.BIN
EMCC_ARGS += --preload-file U17-MERGED.BIN
EMCC_ARGS += -O2 -gsource-map --source-map-base=./

plexem.mjs: $(SRC)
emcc -o $@ $(EMCC_ARGS) $^ -lm

clean:
rm -f $(SRC:.c=.o)
rm -f emu
Expand Down
31 changes: 31 additions & 0 deletions emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef __EMSCRIPTEN__
#include "emscripten.h"
#endif
#include "Musashi/m68k.h"
#include "uart.h"
#include "ramrom.h"
Expand Down Expand Up @@ -741,6 +746,9 @@ static void sig_hdl(int sig) {
m68k_modify_timeslice(0);
}

//if running realtime, we'll sleep for a bit every SLEEP_EVERY_US us
#define SLEEP_EVERY_US 10000 //10ms = 100Hz

void emu_start(emu_cfg_t *cfg) {
signal(SIGQUIT, sig_hdl); // ctrl+\ to dump status
tracefile=fopen("trace.txt","w");
Expand Down Expand Up @@ -784,6 +792,10 @@ void emu_start(emu_cfg_t *cfg) {
int cpu_in_reset[2]={0};
int cycles_remaining[2]={0};

struct timeval last_delay_at;
int emulated_us_since_last_delay=0;
gettimeofday(&last_delay_at, NULL);

while(1) {
for (int i=0; i<2; i++) {
m68k_set_context(cpuctx[i]);
Expand Down Expand Up @@ -824,6 +836,25 @@ void emu_start(emu_cfg_t *cfg) {
m68k_get_context(cpuctx[i]);
}
}
if (cfg->realtime) {
emulated_us_since_last_delay+=10;

struct timeval time_since_last_delay;
struct timeval now;
gettimeofday(&now, NULL);
timersub(&now, &last_delay_at, &time_since_last_delay);
if (time_since_last_delay.tv_sec!=0 || time_since_last_delay.tv_usec>=SLEEP_EVERY_US) {
int w=emulated_us_since_last_delay-time_since_last_delay.tv_usec;
if (w<1000) w=1000; //sleep at least a ms
#ifdef __EMSCRIPTEN__
emscripten_sleep(w/1000);
#else
usleep(w);
#endif
gettimeofday(&last_delay_at, NULL);
emulated_us_since_last_delay=0;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions emu.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {
const char *u17_rom;
const char *hd0img;
const char *rtcram;
int realtime;
} emu_cfg_t;

void emu_start(emu_cfg_t *cfg);
Expand Down
9 changes: 9 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ int main(int argc, char **argv) {
static_assert(sizeof(level_str)/sizeof(level_str[0])==LOG_LVL_MAX,
"level_str array out of sync");
emu_cfg_t cfg={
#ifdef __EMSCRIPTEN__
.u15_rom="U15-MERGED.BIN",
.u17_rom="U17-MERGED.BIN",
.realtime=1,
#else
.u15_rom="../plexus-p20/ROMs/U15-MERGED.BIN",
.u17_rom="../plexus-p20/ROMs/U17-MERGED.BIN",
#endif
.hd0img="plexus-sanitized.img",
.rtcram="rtcram.bin"
};
Expand All @@ -73,6 +79,8 @@ int main(int argc, char **argv) {
} else if (strcmp(argv[i], "-u17")==0 && i+1<argc) {
i++;
cfg.u15_rom=argv[i];
} else if (strcmp(argv[i], "-r")==0) {
cfg.realtime=1;
} else if (strcmp(argv[i], "-l")==0 && i+1<argc) {
i++;
error|=parse_loglvl_str(argv[i]);
Expand All @@ -87,6 +95,7 @@ int main(int argc, char **argv) {
printf("Usage: %s [args]\n", argv[0]);
printf(" -u15 Path to U15 rom file\n");
printf(" -u17 Path to U17 rom file\n");
printf(" -r Try to run at realtime speeds\n");
printf(" -l module=level - set logging level of module to specified level\n");
printf(" -l level - Set overal log level to specified level\n");
printf("Modules: ");
Expand Down
1 change: 1 addition & 0 deletions xterm-pty
Submodule xterm-pty added at 273928

0 comments on commit a27db6d

Please sign in to comment.