From 1f8d3d98cebee64a96e473c7d53b98a0a95ccc3e Mon Sep 17 00:00:00 2001 From: hasekawa-takumi <167335845+hasekawa-takumi@users.noreply.github.com> Date: Mon, 22 Apr 2024 18:22:47 -0400 Subject: [PATCH] Fix LOADU8 (#156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix LOADU8 It was generating wrong results * Remove zero_extend_byte and update sign_extend_byte. --------- Co-authored-by: Hasekawa-Takumi Co-authored-by: thealmarty <“thealmartyblog@gmail.com”> --- cpu/src/lib.rs | 6 ++---- machine/src/core.rs | 10 +--------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/cpu/src/lib.rs b/cpu/src/lib.rs index 8016c82..424affc 100644 --- a/cpu/src/lib.rs +++ b/cpu/src/lib.rs @@ -514,18 +514,16 @@ where // The array index of the word for the byte to read from let index_of_read = index_of_byte(read_addr.into()); // The byte from the read cell. - let cell_byte = cell[index_of_read]; + let cell_byte: u8 = cell[index_of_read]; let write_addr = (state.cpu().fp as i32 + ops.a()) as u32; // The address, converted to a multiple of 4. let write_addr_index = addr_of_word(write_addr); // The Word to write, with one byte overwritten to the read byte - let cell_to_write = Word::zero_extend_byte(cell_byte); - state .mem_mut() - .write(clk, write_addr_index, cell_to_write, true); + .write(clk, write_addr_index, Word::from_u8(cell_byte), true); state.cpu_mut().pc += 1; state.cpu_mut().push_op(Operation::LoadU8, opcode, ops); } diff --git a/machine/src/core.rs b/machine/src/core.rs index 393068a..5112000 100644 --- a/machine/src/core.rs +++ b/machine/src/core.rs @@ -35,15 +35,7 @@ impl Word { 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[0] = byte; - Self(result) - } -} - -impl Word { - pub fn zero_extend_byte(byte: u8) -> Self { - let mut result: [u8; MEMORY_CELL_BYTES] = [0; MEMORY_CELL_BYTES]; - result[0] = byte; + result[3] = byte; Self(result) } }