-
Notifications
You must be signed in to change notification settings - Fork 94
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
o1vm/riscv32i: define registers #2743
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
/// An interpreter for an optimised version of Keccak | ||
pub mod keccak; | ||
|
||
/// An interpreter for the MIPS instruction set. | ||
pub mod mips; | ||
|
||
/// An interpreter for the RISC-V 32I instruction set, following the specification | ||
/// on | ||
/// [riscv.org](https://riscv.org/wp-content/uploads/2019/12/riscv-spec-20191213.pdf). | ||
pub mod riscv32i; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// All the registers used by the ISA | ||
pub mod registers; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::ops::{Index, IndexMut}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
pub const N_GP_REGISTERS: usize = 32; | ||
// FIXME: | ||
pub const REGISTER_CURRENT_IP: usize = N_GP_REGISTERS + 1; | ||
pub const REGISTER_NEXT_IP: usize = N_GP_REGISTERS + 2; | ||
pub const REGISTER_HEAP_POINTER: usize = N_GP_REGISTERS + 3; | ||
|
||
/// This represents the internal state of the virtual machine. | ||
#[derive(Clone, Default, Debug, Serialize, Deserialize)] | ||
pub struct Registers<T> { | ||
/// There are 32 general purpose registers. | ||
/// - x0: hard-wired zero | ||
/// - x1: return address | ||
/// - x2: stack pointer | ||
/// - x3: global pointer | ||
/// - x4: thread pointer | ||
/// - x5: temporary/alternate register | ||
/// - x6-x7: temporaries | ||
/// - x8: saved register/frame pointer | ||
/// - x9: saved register | ||
/// - x10-x11: function arguments/results | ||
/// - x12-x17: function arguments | ||
/// - x18-x27: saved registers | ||
/// - x28-x31: temporaries | ||
pub general_purpose: [T; N_GP_REGISTERS], | ||
pub current_instruction_pointer: T, | ||
pub next_instruction_pointer: T, | ||
pub heap_pointer: T, | ||
} | ||
|
||
impl<T: Clone> Index<usize> for Registers<T> { | ||
type Output = T; | ||
|
||
fn index(&self, index: usize) -> &Self::Output { | ||
if index < N_GP_REGISTERS { | ||
&self.general_purpose[index] | ||
} else if index == REGISTER_CURRENT_IP { | ||
&self.current_instruction_pointer | ||
} else if index == REGISTER_NEXT_IP { | ||
&self.next_instruction_pointer | ||
} else if index == REGISTER_HEAP_POINTER { | ||
&self.heap_pointer | ||
} else { | ||
panic!("Index out of bounds"); | ||
} | ||
} | ||
} | ||
|
||
impl<T: Clone> IndexMut<usize> for Registers<T> { | ||
fn index_mut(&mut self, index: usize) -> &mut Self::Output { | ||
if index < N_GP_REGISTERS { | ||
&mut self.general_purpose[index] | ||
} else if index == REGISTER_CURRENT_IP { | ||
&mut self.current_instruction_pointer | ||
} else if index == REGISTER_NEXT_IP { | ||
&mut self.next_instruction_pointer | ||
} else if index == REGISTER_HEAP_POINTER { | ||
&mut self.heap_pointer | ||
} else { | ||
panic!("Index out of bounds"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nbd since ip, nip, and the heap ptr are artificial, but it is a bit awkward that 32 isn't an index in the range.