Skip to content

Commit

Permalink
Put byte at the end.
Browse files Browse the repository at this point in the history
  • Loading branch information
thealmarty committed Apr 17, 2024
1 parent c134477 commit d35cb75
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
10 changes: 2 additions & 8 deletions cpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,8 @@ where
// The address, converted to a multiple of 4.
let write_addr_index = index_to_word(write_addr);

// The array index of the word for the byte to write to
let index_of_write = index_of_byte(write_addr.into());

// The Word to write, with one byte overwritten to the read byte
let cell_to_write = Word::zero_extend_byte(cell_byte, index_of_write);
let cell_to_write = Word::zero_extend_byte(cell_byte);

state
.mem_mut()
Expand Down Expand Up @@ -562,11 +559,8 @@ where
// The address, converted to a multiple of 4.
let write_addr_index = index_to_word(write_addr);

// The array index of the word for the byte to write to
let index_of_write = index_of_byte(write_addr.into());

// The Word to write, with one byte overwritten to the read byte
let cell_to_write = Word::sign_extend_byte(cell_byte, index_of_write);
let cell_to_write = Word::sign_extend_byte(cell_byte);

state
.mem_mut()
Expand Down
9 changes: 4 additions & 5 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ impl Word<u8> {
}

impl Word<u8> {
//TODO if the byte isn't the lowest byte then this doesn't make sense?
pub fn sign_extend_byte(byte: u8, loc: usize) -> Self {
pub fn sign_extend_byte(byte: u8) -> Self {
let sign = byte as i8 >> 7;
let mut result: [u8; MEMORY_CELL_BYTES] = [sign as u8; MEMORY_CELL_BYTES];
result[loc] = byte;
result[3] = byte;
Self(result)
}
}

impl Word<u8> {
pub fn zero_extend_byte(byte: u8, loc: usize) -> Self {
pub fn zero_extend_byte(byte: u8) -> Self {
let mut result: [u8; MEMORY_CELL_BYTES] = [0; MEMORY_CELL_BYTES];
result[loc] = byte;
result[3] = byte;
Self(result)
}
}
Expand Down

0 comments on commit d35cb75

Please sign in to comment.