From d35cb75cbb4f0ac6bd2a37504ac40c5305731036 Mon Sep 17 00:00:00 2001 From: thealmarty <“thealmartyblog@gmail.com”> Date: Wed, 17 Apr 2024 12:45:38 -0700 Subject: [PATCH] Put byte at the end. --- cpu/src/lib.rs | 10 ++-------- machine/src/core.rs | 9 ++++----- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/cpu/src/lib.rs b/cpu/src/lib.rs index 01a4670..6f69701 100644 --- a/cpu/src/lib.rs +++ b/cpu/src/lib.rs @@ -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() @@ -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() diff --git a/machine/src/core.rs b/machine/src/core.rs index 2bdd914..f507c4f 100644 --- a/machine/src/core.rs +++ b/machine/src/core.rs @@ -28,19 +28,18 @@ impl Word { } impl Word { - //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 { - 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) } }