Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
thealmarty committed Apr 17, 2024
1 parent 45e8829 commit 68c9e55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions cpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::marker::Sync;
use core::mem::transmute;
use valida_bus::{MachineWithGeneralBus, MachineWithMemBus, MachineWithProgramBus};
use valida_machine::{
index_of_word, index_to_word, instructions, AdviceProvider, Chip, Instruction, InstructionWord,
index_of_byte, index_to_word, instructions, AdviceProvider, Chip, Instruction, InstructionWord,
Interaction, Operands, Word,
};
use valida_memory::{MachineWithMemoryChip, Operation as MemoryOperation};
Expand Down Expand Up @@ -495,16 +495,16 @@ where
);

// The array index of the word for the byte to read from
let index_of_read = index_of_word(read_addr.into());
let index_of_read = index_of_byte(read_addr.into());
// The byte from the read cell.
let cell_byte = cell[index_of_read];

let write_addr = (state.cpu().fp as i32 + ops.a()) as u32;
// The key to the memory map, converted to a multiple of 4.
// 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_word(write_addr.into());
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);
Expand Down Expand Up @@ -540,7 +540,7 @@ where
// The word from the read address.
let cell = state.mem_mut().read(
clk,
read_addr_index,
read_addr.into(), //TODO should be read_addr_index but currently it causes read before write error.
true,
pc,
opcode,
Expand All @@ -554,16 +554,16 @@ where
);

// The array index of the word for the byte to read from
let index_of_read = index_of_word(read_addr.into());
let index_of_read = index_of_byte(read_addr.into());
// The byte from the read cell.
let cell_byte = cell[index_of_read];

let write_addr = (state.cpu().fp as i32 + ops.a()) as u32;
// The key to the memory map, converted to a multiple of 4.
// 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_word(write_addr.into());
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);
Expand Down Expand Up @@ -627,15 +627,15 @@ where
.read(clk, read_addr_index, true, pc, opcode, 1, "");

// The array index of the word for the byte to read from
let index_of_read = index_of_word(read_addr);
let index_of_read = index_of_byte(read_addr);

// The word from the read address.
let cell_read = cell.0;
// The byte from the read cell.
let cell_byte = cell_read[index_of_read];

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

// The key to the memory map, converted to a multiple of 4.
let write_addr_index = index_to_word(write_addr.into());
Expand Down
4 changes: 2 additions & 2 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub struct Word<F>(pub [F; MEMORY_CELL_BYTES]);

// Functions for byte manipulations
/// Get the index of a byte in a memory cell.
pub fn index_of_word(addr: u32) -> usize {
pub fn index_of_byte(addr: u32) -> usize {
(addr & 3) as usize
}

/// Get the key to the map of the memory cells which is not empty.
/// Get the address to the map of the memory cells which is not empty (a multiple of 4).
pub fn index_to_word(addr: u32) -> u32 {
(addr & !3) as u32
}
Expand Down

0 comments on commit 68c9e55

Please sign in to comment.