forked from bbcmicrobit/micropython
-
Notifications
You must be signed in to change notification settings - Fork 2
Use emscripten to run a fully working MicroPython in the browser. #4
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
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Files are removed separately to the initial simulator commit in order to make it clearer what has been changed.
The simulator uses emscripten to run in the browser, meaning that the nRF hardware API can no longer be used. Though the mbed API along with its HAL is reimplemented using emscripten and javascript. Instead of plugging into hardware, EM_ASM is used at points to call directly into javascript. Persistent memory is also removed and replaced with local javascript memory. Inline assembly is no longer supported. Due to javascript being single threaded, in order to yield to the browser and "hardware" threads, mp_hal_delay_ms is used to call emscripten_sleep in the VM and throughout. The initial stack usage is simulated by initialising the stack top using a dummy value at the start of main. Javascript is used in order to import any MicroPython script from the simulator web page that exists on soft reset. The stack may ascend on the machine running the browser and so the stack_usage now accounts for that. The yielding within the VM uses a counter to periodically check whether enough time has passed for it to yield yet, this time checking prevents fast CPUs yielding to the browser more frequently than necessary.
6fb54d3
to
bd9fbdf
Compare
The nRF API is partially implemented in order to support the ticker, with most methods being stubbed out. The ticker is implemented using a javascript timeout function that calls the handler. Due to the unreliability of javascript's timing, the handler is called more frequently than is necessary for the slow ticker, and the time that has passed is checked. The nrf_gpio API calls directly into mbed's gpio API. Implementing the pin API allows the display to run, though the light sensor is not yet implemented.
Asyncify seems to prevent ccalls such as the ticker during emscripten_sleep. To get around this issue, only briefly call emscripten_sleep in mp_hal_delay_ms, and check ticks passed to return at correct time.
Explicitly handle exceptions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR includes the previous simulator PRs (#1, #2, #3) along with a few additional commits to give the latest fully working simulator.