Skip to content

Commit b6aa3ca

Browse files
authored
Merge pull request #602 from ChinYikMing/sys-emu-wasm
Enable System Emulation in Web Browsers
2 parents 56d20a2 + 39ff299 commit b6aa3ca

File tree

17 files changed

+691
-87
lines changed

17 files changed

+691
-87
lines changed

.github/workflows/deploy-wasm.yml

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,108 @@ on:
1010
branches:
1111
- master
1212
repository_dispatch: # listening to rv32emu-prebuilt events
13-
types: [deploy_wasm]
13+
types: [deploy_user_wasm, deploy_system_wasm]
1414

1515
jobs:
16-
wasm-deploy:
16+
wasm-system-deploy:
1717
if: github.event.pull_request.merged == true ||
1818
github.event_name == 'workflow_dispatch' ||
19-
github.event_name == 'repository_dispatch'
19+
github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm'
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Check out the repo
23+
uses: actions/checkout@v4
24+
- name: install-dependencies
25+
run: |
26+
sudo apt-get update -q=2
27+
sudo apt-get install -q=2 device-tree-compiler
28+
- name: Verify if the JS or HTML files has been modified
29+
id: changed-files
30+
uses: tj-actions/changed-files@v46
31+
with:
32+
files: |
33+
assets/wasm/html/system.html
34+
assets/wasm/js/system-pre.js
35+
src/em_runtime.c
36+
# Files below may have a potential performance impact (reference from benchmark.yml)
37+
src/devices/*.c
38+
src/system.c
39+
src/riscv.c
40+
src/decode.c
41+
src/emulate.c
42+
src/rv32_template.c
43+
src/rv32_constopt.c
44+
- name: install emcc
45+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
46+
github.event_name == 'workflow_dispatch' ||
47+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
48+
run: |
49+
git clone https://github.com/emscripten-core/emsdk -b 3.1.51
50+
cd emsdk
51+
./emsdk install latest
52+
./emsdk activate latest
53+
source ./emsdk_env.sh
54+
echo "$PATH" >> $GITHUB_PATH
55+
shell: bash
56+
- name: fetch artifact
57+
run: |
58+
make artifact
59+
# get from rv32emu-prebuilt
60+
wget -O build/shareware_doom_iwad.zip "https://raw.githubusercontent.com/sysprog21/rv32emu-prebuilt/doom-artifact/shareware_doom_iwad.zip"
61+
unzip -d build/ build/shareware_doom_iwad.zip
62+
- name: build with emcc and move application files to /tmp
63+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
64+
github.event_name == 'workflow_dispatch' ||
65+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
66+
run: |
67+
make CC=emcc ENABLE_SYSTEM=1 ENABLE_SDL=1 INITRD_SIZE=32 -j
68+
mkdir /tmp/rv32emu-system-demo
69+
mv assets/wasm/html/system.html /tmp/rv32emu-system-demo/index.html
70+
mv assets/wasm/js/coi-serviceworker.min.js /tmp/rv32emu-system-demo
71+
mv build/rv32emu.js /tmp/rv32emu-system-demo
72+
mv build/rv32emu.wasm /tmp/rv32emu-system-demo
73+
mv build/rv32emu.worker.js /tmp/rv32emu-system-demo
74+
ls -al /tmp/rv32emu-system-demo
75+
- name: Check out the rv32emu-system-demo repo
76+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
77+
github.event_name == 'workflow_dispatch' ||
78+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
79+
uses: actions/checkout@v4
80+
with:
81+
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
82+
repository: sysprog21/rv32emu-demo
83+
- name: Create local changes
84+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
85+
github.event_name == 'workflow_dispatch' ||
86+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
87+
run: |
88+
mkdir -p system
89+
mv /tmp/rv32emu-system-demo/index.html ./system
90+
mv /tmp/rv32emu-system-demo/coi-serviceworker.min.js ./system
91+
mv /tmp/rv32emu-system-demo/rv32emu.js ./system
92+
mv /tmp/rv32emu-system-demo/rv32emu.wasm ./system
93+
mv /tmp/rv32emu-system-demo/rv32emu.worker.js ./system
94+
- name: Commit files
95+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
96+
github.event_name == 'workflow_dispatch' ||
97+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
98+
run: |
99+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
100+
git config --local user.name "github-actions[bot]"
101+
git add system/
102+
git commit -m "Add changes to system emulation"
103+
- name: Push changes
104+
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
105+
github.event_name == 'workflow_dispatch' ||
106+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_system_wasm') }}
107+
uses: ad-m/github-push-action@master
108+
with:
109+
repository: sysprog21/rv32emu-demo
110+
github_token: ${{ secrets.RV32EMU_DEMO_TOKEN }}
111+
branch: main
112+
wasm-user-deploy:
113+
needs: wasm-system-deploy # run jobs sequentially since two jobs operate on same reposity: rv32emu-demo
114+
if: always() # ensures wasm-user-deploy runs regardless of the outcome or condition of wasm-system-deploy
20115
runs-on: ubuntu-latest
21116
steps:
22117
- name: Check out the repo
@@ -26,10 +121,11 @@ jobs:
26121
uses: tj-actions/changed-files@v46
27122
with:
28123
files: |
29-
assets/wasm/html/index.html
30-
assets/wasm/js/pre.js
124+
assets/wasm/html/user.html
125+
assets/wasm/js/user-pre.js
31126
build/*.elf
32127
tools/gen-elf-list-js.py
128+
src/em_runtime.c
33129
# Files below may have a potential performance impact (reference from benchmark.yml)
34130
src/riscv.c
35131
src/decode.c
@@ -39,12 +135,10 @@ jobs:
39135
- name: install emcc
40136
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
41137
github.event_name == 'workflow_dispatch' ||
42-
github.event_name == 'repository_dispatch' }}
138+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
43139
run: |
44-
git clone https://github.com/emscripten-core/emsdk.git
140+
git clone https://github.com/emscripten-core/emsdk -b 3.1.51
45141
cd emsdk
46-
git pull
47-
git checkout 3.1.51
48142
./emsdk install latest
49143
./emsdk activate latest
50144
source ./emsdk_env.sh
@@ -53,21 +147,17 @@ jobs:
53147
- name: fetch artifact
54148
run: |
55149
make artifact
56-
# Hack Cloudflare 403 Forbidden on GitHub Runner for Doom artifact download
57-
wget --header="User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0" \
58-
--header="Referer: https://www.doomworld.com/" \
59-
--header="Accept-Language: en-US,en;q=0.9" \
60-
-O build/shareware_doom_iwad.zip \
61-
"https://www.doomworld.com/3ddownloads/ports/shareware_doom_iwad.zip"
150+
# get from rv32emu-prebuilt
151+
wget -O build/shareware_doom_iwad.zip "https://raw.githubusercontent.com/sysprog21/rv32emu-prebuilt/doom-artifact/shareware_doom_iwad.zip"
62152
unzip -d build/ build/shareware_doom_iwad.zip
63153
- name: build with emcc and move application files to /tmp
64154
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
65155
github.event_name == 'workflow_dispatch' ||
66-
github.event_name == 'repository_dispatch' }}
156+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
67157
run: |
68158
make CC=emcc ENABLE_SDL=1
69159
mkdir /tmp/rv32emu-demo
70-
mv assets/wasm/html/index.html /tmp/rv32emu-demo
160+
mv assets/wasm/html/user.html /tmp/rv32emu-demo/index.html
71161
mv assets/wasm/js/coi-serviceworker.min.js /tmp/rv32emu-demo
72162
mv build/elf_list.js /tmp/rv32emu-demo
73163
mv build/rv32emu.js /tmp/rv32emu-demo
@@ -77,15 +167,15 @@ jobs:
77167
- name: Check out the rv32emu-demo repo
78168
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
79169
github.event_name == 'workflow_dispatch' ||
80-
github.event_name == 'repository_dispatch' }}
170+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
81171
uses: actions/checkout@v4
82172
with:
83173
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
84174
repository: sysprog21/rv32emu-demo
85175
- name: Create local changes
86176
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
87177
github.event_name == 'workflow_dispatch' ||
88-
github.event_name == 'repository_dispatch' }}
178+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
89179
run: |
90180
mv /tmp/rv32emu-demo/index.html .
91181
mv /tmp/rv32emu-demo/coi-serviceworker.min.js .
@@ -96,16 +186,16 @@ jobs:
96186
- name: Commit files
97187
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
98188
github.event_name == 'workflow_dispatch' ||
99-
github.event_name == 'repository_dispatch' }}
189+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
100190
run: |
101191
git config --local user.email "github-actions[bot]@users.noreply.github.com"
102192
git config --local user.name "github-actions[bot]"
103193
git add --all
104-
git commit -m "Add changes"
194+
git commit -m "Add changes to user emulation"
105195
- name: Push changes
106196
if: ${{ steps.changed-files.outputs.any_modified == 'true' ||
107197
github.event_name == 'workflow_dispatch' ||
108-
github.event_name == 'repository_dispatch' }}
198+
(github.event_name == 'repository_dispatch' && github.event.action == 'deploy_user_wasm') }}
109199
uses: ad-m/github-push-action@master
110200
with:
111201
repository: sysprog21/rv32emu-demo

.github/workflows/main.yml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@ jobs:
8080
make artifact
8181
make ENABLE_SYSTEM=1 artifact
8282
make ENABLE_ARCH_TEST=1 artifact
83-
# Hack Cloudflare 403 Forbidden on GitHub Runner for Doom artifact download
84-
wget --header="User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0" \
85-
--header="Referer: https://www.doomworld.com/" \
86-
--header="Accept-Language: en-US,en;q=0.9" \
87-
-O build/shareware_doom_iwad.zip \
88-
"https://www.doomworld.com/3ddownloads/ports/shareware_doom_iwad.zip"
83+
# get from rv32emu-prebuilt
84+
wget -O build/shareware_doom_iwad.zip "https://raw.githubusercontent.com/sysprog21/rv32emu-prebuilt/doom-artifact/shareware_doom_iwad.zip"
8985
unzip -d build/ build/shareware_doom_iwad.zip
9086
if: ${{ always() }}
9187
- name: default build using emcc
@@ -394,12 +390,8 @@ jobs:
394390
| head -n 1 \
395391
| sed -E 's/.*"tag_name": "([^"]+)".*/\1/')
396392
make LATEST_RELEASE=$LATEST_RELEASE ENABLE_ARCH_TEST=1 artifact
397-
# Hack Cloudflare 403 Forbidden on GitHub Runner for Doom artifact download
398-
wget --header="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15" \
399-
--header="Referer: https://www.doomworld.com/" \
400-
--header="Accept-Language: en-US,en;q=0.9" \
401-
-O build/shareware_doom_iwad.zip \
402-
"https://www.doomworld.com/3ddownloads/ports/shareware_doom_iwad.zip"
393+
# get from rv32emu-prebuilt
394+
wget -O build/shareware_doom_iwad.zip "https://raw.githubusercontent.com/sysprog21/rv32emu-prebuilt/doom-artifact/shareware_doom_iwad.zip"
403395
unzip -d build/ build/shareware_doom_iwad.zip
404396
if: ${{ always() }}
405397
- name: default build using emcc

Makefile

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,25 +286,32 @@ $(OUT)/emulate.o: CFLAGS += -foptimize-sibling-calls -fomit-frame-pointer -fno-s
286286

287287
include mk/external.mk
288288
include mk/artifact.mk
289-
include mk/wasm.mk
290289
include mk/system.mk
290+
include mk/wasm.mk
291291

292292
all: config $(BUILD_DTB) $(BUILD_DTB2C) $(BIN)
293293

294294
OBJS := \
295-
map.o \
296-
utils.o \
297-
decode.o \
298-
io.o \
299-
syscall.o \
300-
emulate.o \
301-
riscv.o \
302-
log.o \
303-
elf.o \
304-
cache.o \
305-
mpool.o \
306-
$(OBJS_EXT) \
307-
main.o
295+
map.o \
296+
utils.o \
297+
decode.o \
298+
io.o \
299+
syscall.o
300+
301+
# em_runtime.o should prior to emulate.o, otherwise wasm-ld fails to link
302+
ifeq ($(CC_IS_EMCC), 1)
303+
OBJS += em_runtime.o
304+
endif
305+
306+
OBJS += \
307+
emulate.o \
308+
riscv.o \
309+
log.o \
310+
elf.o \
311+
cache.o \
312+
mpool.o \
313+
$(OBJS_EXT) \
314+
main.o
308315

309316
OBJS := $(addprefix $(OUT)/, $(OBJS))
310317
deps += $(OBJS:%.o=%.o.d) # mk/system.mk includes prior this line, so declare deps at there

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,25 @@ $ source ~/emsdk/emsdk_env.sh
378378
Change the Emscripten SDK environment path if necessary.
379379
380380
At this point, you can build and start a web server service to serve WebAssembly by running:
381+
- user space emulation:
381382
```shell
382-
$ make CC=emcc start-web
383+
$ make CC=emcc start-web -j8
384+
```
385+
- system emulation:
386+
```shell
387+
$ make CC=emcc start-web ENABLE_SYSTEM=1 INITRD_SIZE=32 -j8
383388
```
384389
You would see the server's IP:PORT in your terminal. Copy and paste it to the browsers and
385390
you just access the index page of `rv32emu`.
386391
387-
You would see a dropdown menu which you can use to select the ELF executable. Select one and
388-
click the Run button to run it.
392+
You would see a dropdown menu which you can use to select the ELF executable for user space emulation, select one and
393+
click the 'Run' button to run it. For system emulation, click the 'Run Linux' button to boot Linux.
394+
395+
Alternatively, you may want to view a hosted `rv32emu` since building takes some time.
396+
- [user space emulation demo page](https://sysprog21.github.io/rv32emu-demo/)
397+
- [system emulation demo page](https://sysprog21.github.io/rv32emu-demo/system)
389398
390-
Alternatively, you may want to view a hosted `rv32emu` [demo page](https://sysprog21.github.io/rv32emu-demo/) since building takes some time.
399+
Both pages can be easily switched using the navigation button.
391400
392401
## Contributing
393402
See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines.

0 commit comments

Comments
 (0)