Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable run video games using WebAssembly #385

Merged
merged 1 commit into from
Mar 25, 2024

Conversation

ChinYikMing
Copy link
Collaborator

@ChinYikMing ChinYikMing commented Mar 16, 2024

To play sound effects and musics, we would use software synthesizer, here I choose Timidity. When doom and quake target are specified, the Timidity related data are downloaded and extracted. As previous dicussion in the issue forum, the web workers are not reaped after return, thus adding pthread_join to reap them. As I tested, the sound effects and musics are only playable using the emcc version 3.1.51 which will fetch specific version of SDL2_mixer library, thus warning the user if the emcc version is not 3.1.51.

We want to minimize the wasm size, thus only embed necessary files when building the wasm. For example, if doom target is specified, only doom related files are embeded to wasm. This could shorten the fetching time of wasm from the server.

Since we are running the wasm program inside web browser, we have to yield to the web browser in some interval, so emscripten_set_main_loop_arg provided by emscripten comes to help. To utilize this function, we have to update the rv_step function signature.

web-resources directory will be used to store some required static web files.

Note that this commit is not done yet, just want for quick code review before further anything wrong that not in consensus.

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename the web-resources directory to assets to allow for the inclusion of a wider range of relevant resource files.

web-resources/js/pre.js Outdated Show resolved Hide resolved
WEB_RESOURCES := web-resources
WEB_JS_RESOURCES := $(WEB_RESOURCES)/js
doom_action := (cd $(OUT); ../$(BIN) doom.elf)
ifeq ("$(CC_IS_EMCC)", "1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file mk/external.mk was meant to be related to retrieve something from external sources. Don't specify emscripten rules here.

Copy link
Collaborator Author

@ChinYikMing ChinYikMing Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file mk/external.mk was meant to be related to retrieve something from external sources. Don't specify emscripten rules here.

I want to distinguish between the steps that need to be taken when utilizing emcc. Furthermore, when emcc is not being utilized, we do not want the emcc CFLAGS to be used. Also, notice that only just emcc needs the TIMIDITY_DATA.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimize the necessary changes when possible.

Copy link
Collaborator Author

@ChinYikMing ChinYikMing Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to embed all build executable and configuration files such as doomrc into wasm. Although the size getting larger (fetching time might be longer but only once because it will be cached by the browser for further fetch), but we can select and run them in a single web page from a menu bar.

Consequently, we do not need to check the makefile build target and embed corresponding files respectively.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to embed all build executable and configuration files such as doomrc into wasm.

It sounds like a good idea.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to embed all build executable and configuration files such as doomrc into wasm. Although the size getting larger (fetching time might be longer but only once because it will be cached by the browser for further fetch), but we can select and run them in a single web page from a menu bar.

Consequently, we do not need to check the makefile build target and embed corresponding files respectively.

The final wasm is 61.40 MB, this is larger than GitHub's recommended maximum file size of 50.00 MB. I am going to test it out if this is fine to host on GitHub Pages.

Copy link
Collaborator Author

@ChinYikMing ChinYikMing Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to embed all build executable and configuration files such as doomrc into wasm. Although the size getting larger (fetching time might be longer but only once because it will be cached by the browser for further fetch), but we can select and run them in a single web page from a menu bar.
Consequently, we do not need to check the makefile build target and embed corresponding files respectively.

The final wasm is 61.40 MB, this is larger than GitHub's recommended maximum file size of 50.00 MB. I am going to test it out if this is fine to host on GitHub Pages.

Github Pages are fine even exceeds GitHub's recommended maximum file size of 50.00 MB. See here.

@ChinYikMing ChinYikMing force-pushed the emcc-port-video-games branch 2 times, most recently from b97c73c to bef66b8 Compare March 25, 2024 16:49
@ChinYikMing
Copy link
Collaborator Author

Rename the web-resources directory to assets to allow for the inclusion of a wider range of relevant resource files.

Done.

@ChinYikMing
Copy link
Collaborator Author

See the result in here.

To play sound effects and musics, we would use software synthesizer,
here I choose Timidity. When doom and quake target are specified, the
Timidity related data are downloaded and extracted. As previous
dicussion in the issue forum, the web workers are not reaped after
return, thus adding pthread_join to reap them. As I tested, the sound
effects and musics are only playable using the emcc version 3.1.51
which will fetch specific SDL2_mixer library, thus warning the user if
the emcc version is not 3.1.51.

Embed all ELF and their dependencies into single wasm when building with
emcc. Consequently, a ELF executable menu bar can be introduced to select
and run desired ELF programs.

Since we are running the wasm program inside web browser, we have to
yield to the web browser in some interval, so emscripten_set_main_loop_arg
provided by emscripten comes to help. To utilize this function, we have to
update the rv_step function signature. In order to switch running ELF
programs, we have to cancel the browser main loop using
emscripten_cancel_main_loop and do some clean up to recollect memory.
_indirect_rv_halt is introduced to halt the RISC-V instance without
exposing the RISC-V instance.

assets directory will be used to store some required static web files.
assets/html/index.html is a default main page of the ported wasm rv32emu.
In order to switch running ELF programs, we have to delay some time
for finishing emscripten_cancel_main_loop. I set delay to 1 second for now.

TODO: more clean up have to be done, e.g., clear canvas, terminal and
sound thread.

Related to: sysprog21#75
@ChinYikMing ChinYikMing force-pushed the emcc-port-video-games branch from bef66b8 to 07b86f6 Compare March 25, 2024 17:05
@jserv jserv merged commit 971b42c into sysprog21:master Mar 25, 2024
8 checks passed
@jserv
Copy link
Contributor

jserv commented Mar 25, 2024

Thank @ChinYikMing for contributing!

@ChinYikMing ChinYikMing deleted the emcc-port-video-games branch March 26, 2024 00:38
vestata pushed a commit to vestata/rv32emu that referenced this pull request Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants