Skip to content

Commit

Permalink
Fix LOADU8 (#156)
Browse files Browse the repository at this point in the history
* Fix LOADU8

It was generating wrong results

* Remove zero_extend_byte and update sign_extend_byte.

---------

Co-authored-by: Hasekawa-Takumi <[email protected]>
Co-authored-by: thealmarty <“[email protected]”>
  • Loading branch information
3 people authored Apr 22, 2024
1 parent f8c192d commit 1f8d3d9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
6 changes: 2 additions & 4 deletions cpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 1 addition & 9 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ impl Word<u8> {
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<u8> {
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)
}
}
Expand Down

0 comments on commit 1f8d3d9

Please sign in to comment.