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

Add snake demo for Sonata board #23

Merged
merged 4 commits into from
Jul 31, 2024
Merged

Conversation

AlexJones0
Copy link
Contributor

I've implemented the Snake demo for the Sonata board to show off the CHERI capabilities.

Currently it is implemented as a fully functional game of Snake, where the snake attempting to exit the boundaries of the display will trigger a CHERI Capability violation, which is used to detect the end of the game. The bottom and right side of the screen trigger bounds violations as we try to access outside of a 2D array representing the game. The top and left side of the screen trigger tag violations due to using a negative index on an array, which is undefined behavior. The snake colliding with itself or winning the game currently just ends the game normally, without any CHERI capability violation.

The demo should be quite customisable - you can change things like the game speed, size and appearance using the constants defined near the top of the file to tweak it as you like.

Would appreciate any feedback regarding where improvements can be made in the code - I'm still quite unfamiliar with many of the features of C++.

Copy link
Member

@HU90m HU90m left a comment

Choose a reason for hiding this comment

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

Only did a quick skim, but looks good!

nix run .#clang-lint fix

examples/snake/xmake.lua Outdated Show resolved Hide resolved
examples/snake/xor_shift.cc Outdated Show resolved Hide resolved
libraries/lcd.cc Outdated Show resolved Hide resolved
examples/snake/snake.cc Outdated Show resolved Hide resolved
examples/snake/snake.cc Show resolved Hide resolved
examples/snake/snake.cc Outdated Show resolved Hide resolved
examples/snake/snake.cc Outdated Show resolved Hide resolved
examples/snake/xor_shift.cc Outdated Show resolved Hide resolved
libraries/lcd.cc Outdated Show resolved Hide resolved
examples/snake/snake.cc Outdated Show resolved Hide resolved
@AlexJones0 AlexJones0 force-pushed the sonata-snake-demo branch 2 times, most recently from aaa3569 to 3a99827 Compare June 21, 2024 14:54
@AlexJones0
Copy link
Contributor Author

AlexJones0 commented Jun 21, 2024

I've fixed up all the raised comments now and changed a couple things based on feedback:

  • The snake can now move in any direction when it is only 1 tile long, as this was causing some confusion
  • Rather than ending the game normally, a CHERI capability violation now occurs when the snake collides with itself. This is just done using an arbitrary out of bounds check - we originally had this working with a PLoad violation but that was causing an issue with the hacky way that we're modifying the PCC and restoring the registers to return back to the program from the error handling, so it was decided to leave that for now.
  • Rather than the original xor_shift functionality, the program now uses the superior PRNG entropy driver from https://github.com/lowRISC/cheriot-rtos/tree/ethernet (specifically lowRISC/cheriot-rtos@32cd341).
  • Added some extra clarifying comments on specific parts of code that required some more explanation.

From investigation the CI quality check appears to be failing because nix run .#clang-lint fix is not equipped to deal with changes to the cheriot-rtos submodule. I could just leave the simple randomness using xor_shift but I figure it's better to use the entropy driver to avoid the possibility of forgetting to replace this in the future when that is properly merged.

@AlexJones0 AlexJones0 requested a review from HU90m June 21, 2024 15:09
@AlexJones0
Copy link
Contributor Author

we originally had this working with a PLoad violation but that was causing an issue with the hacky way that we're modifying the PCC and restoring the registers to return back to the program from the error handling, so it was decided to leave that for now

Just in case anybody needs it for future reference this is how I was manually triggering the CHERI PLoad capability violation:

Capability<Tile *> gameCapability{gameSpace};
gameCapability.permissions() &= PermissionSet::omnipotent().without(Permission::Load);
return gameCapability[nextPosition.y][nextPosition.x] == Tile::SNAKE;

@HU90m HU90m marked this pull request as ready for review July 31, 2024 10:50
@HU90m HU90m merged commit 97c0012 into lowRISC:main Jul 31, 2024
1 check passed
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.

3 participants