From 68c9e55c7c4358403bef2b153eed2b007493be10 Mon Sep 17 00:00:00 2001 From: thealmarty <“thealmartyblog@gmail.com”> Date: Wed, 17 Apr 2024 12:32:11 -0700 Subject: [PATCH] f --- cpu/src/lib.rs | 20 ++++++++++---------- machine/src/core.rs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cpu/src/lib.rs b/cpu/src/lib.rs index 509471a..01a4670 100644 --- a/cpu/src/lib.rs +++ b/cpu/src/lib.rs @@ -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}; @@ -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); @@ -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, @@ -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); @@ -627,7 +627,7 @@ 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; @@ -635,7 +635,7 @@ where 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()); diff --git a/machine/src/core.rs b/machine/src/core.rs index 5fed177..1dee9ae 100644 --- a/machine/src/core.rs +++ b/machine/src/core.rs @@ -9,11 +9,11 @@ pub struct Word(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 }