Skip to content

Commit

Permalink
Add emscripten support for persistent storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Spritetm committed Jun 26, 2024
1 parent 43c4b37 commit 845577b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ 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 += -lidbfs.js
EMCC_ARGS += -O2 -gsource-map --source-map-base=./

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

clean:
rm -f $(SRC:.c=.o)
Expand Down
20 changes: 20 additions & 0 deletions emscripten_env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "emscripten_env.h"
#include "emscripten.h"

void emscripten_init() {
EM_ASM(
FS.mkdir("/persist");
FS.mount(IDBFS, {}, "/persist");
FS.syncfs(true, function (err) {
assert(!err);
});
);
}

void emscripten_syncfs() {
EM_ASM(
FS.syncfs(false, function (err) {
assert(!err);
});
);
}
3 changes: 3 additions & 0 deletions emscripten_env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

void emscripten_init();
void emscripten_syncfs();
8 changes: 7 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string.h>
#include "emu.h"
#include "log.h"
#include "emscripten_env.h"

const char *log_str[]={
[LOG_SRC_UART]="uart",
Expand Down Expand Up @@ -63,14 +64,19 @@ int main(int argc, char **argv) {
#ifdef __EMSCRIPTEN__
.u15_rom="U15-MERGED.BIN",
.u17_rom="U17-MERGED.BIN",
.cow_dir="persist/cow",
.rtcram="persist/rtcram.bin",
.realtime=1,
#else
.u15_rom="../plexus-p20/ROMs/U15-MERGED.BIN",
.u17_rom="../plexus-p20/ROMs/U17-MERGED.BIN",
.rtcram="rtcram.bin",
#endif
.hd0img="plexus-sanitized.img",
.rtcram="rtcram.bin"
};
#ifdef __EMSCRIPTEN__
emscripten_init();
#endif
int error=0;
for (int i=1; i<argc; i++) {
if (strcmp(argv[i], "-u15")==0 && i+1<argc) {
Expand Down
2 changes: 1 addition & 1 deletion scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct scsi_t {
uint8_t cmd[10];
int selected;
int op_timeout_us;
uint8_t databuf[256*512]
uint8_t databuf[256*512];
};

/*
Expand Down
23 changes: 20 additions & 3 deletions scsi_dev_hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
#include "scsi.h"
#include "emu.h"
#include "log.h"
#include "emscripten_env.h"

//Might need to change if e.g. the backing file changes for the web version.
#define COW_VERSION_MAJOR 0
#define COW_VERSION_MINOR 0

typedef struct {
scsi_dev_t dev;
Expand Down Expand Up @@ -42,6 +47,8 @@ static void write_block(scsi_hd_t *hd, int lba, uint8_t *data) {
perror("opening cow file for write");
exit(1);
}
uint8_t ver[2]={COW_VERSION_MAJOR, COW_VERSION_MINOR};
fwrite(ver, 2, 1, f);
fwrite(data, 512, 1, f);
fclose(f);
} else {
Expand All @@ -54,9 +61,16 @@ static void read_block(scsi_hd_t *hd, int lba, uint8_t *data) {
if (hd->cow_dir) {
FILE *f=open_cow_file(hd, lba, "rb");
if (f) {
fread(data, 512, 1, f);
fclose(f);
return;
uint8_t ver[2];
fread(ver, 2, 1, f);
if (ver[0]==COW_VERSION_MAJOR && ver[1]==COW_VERSION_MINOR) {
fread(data, 512, 1, f);
fclose(f);
return;
} else {
//not the same version; ignore
fclose(f);
}
}
}
fseek(hd->hdfile, lba*512, SEEK_SET);
Expand Down Expand Up @@ -128,6 +142,9 @@ static void hd_handle_data_out(scsi_dev_t *dev, uint8_t *msg, int len) {
for (int i=0; i<blen/512; i++) {
write_block(hd, lba+i, &msg[i*512]);
}
#ifdef __EMSCRIPTEN__
emscripten_syncfs();
#endif
}
}

Expand Down

0 comments on commit 845577b

Please sign in to comment.