Terminate the simulation on exit() from firmware #380
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.
The firmware can end the simulation by sleeping with interrupts disabled. While this way of hanging the CPU makes a lot of sense, it is not the most intuitive.
The standard way to terminate a C program is by either calling
exit()
or returning frommain()
. It turns out the AVR C runtime supports stopping the firmware in both these standard ways. Returning frommain()
jumps toexit()
, which disables interrupts and hangs the CPU in an infinite loop. The implementation is roughly like this:This pull request makes run_avr gracefully quit if the firmware executes the CPU-hanging instruction
rjmp .-2
with interrupts disabled.The actual condition used in the code is:
Some of those tests may be redundant but, as I am not sure to understand all the subtleties of the simulation, I opted for defensive programming.
The feature has been tested with the following program: