Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word alignment checks for LOAD32 and STORE32 #154

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use core::iter;
use core::marker::Sync;
use core::mem::transmute;
use valida_bus::{MachineWithGeneralBus, MachineWithMemBus, MachineWithProgramBus};
use valida_machine::is_mul_4;
use valida_machine::{
addr_of_word, index_of_byte, instructions, AdviceProvider, Chip, Instruction, InstructionWord,
Interaction, Operands, Word,
Expand Down Expand Up @@ -433,11 +434,27 @@ where
let clk = state.cpu().clock;
let pc = state.cpu().pc;
let fp = state.cpu().fp;

let read_addr_1 = (fp as i32 + ops.c()) as u32;
assert!(
is_mul_4(read_addr_1),
"LOAD32: Read address location is not a multiple of 4!"
);

let read_addr_2 = state
.mem_mut()
.read(clk, read_addr_1, true, pc, opcode, 0, "");
assert!(
is_mul_4(read_addr_2.into()),
"LOAD32: Read address is not a multiple of 4!"
);

let write_addr = (state.cpu().fp as i32 + ops.a()) as u32;
assert!(
is_mul_4(write_addr),
"LOAD32: Write address location is not a multiple of 4!"
);

let cell = state.mem_mut().read(
clk,
read_addr_2.into(),
Expand Down Expand Up @@ -581,12 +598,29 @@ where
fn execute(state: &mut M, ops: Operands<i32>) {
let opcode = <Self as Instruction<M, F>>::OPCODE;
let clk = state.cpu().clock;

let read_addr = (state.cpu().fp as i32 + ops.c()) as u32;
assert!(
is_mul_4(read_addr),
"STORE32: Read address is not a multiple of 4!"
);

let write_addr_loc = (state.cpu().fp as i32 + ops.b()) as u32;
assert!(
is_mul_4(write_addr_loc),
"STORE32: Write address location is not a multiple of 4!"
);

let pc = state.cpu().pc;

let write_addr = state
.mem_mut()
.read(clk, write_addr_loc, true, pc, opcode, 0, "");
assert!(
is_mul_4(write_addr.into()),
"STORE32: Write address is not a multiple of 4!"
);

let cell = state
.mem_mut()
.read(clk, read_addr, true, pc, opcode, 1, "");
Expand Down
4 changes: 4 additions & 0 deletions machine/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn index_of_byte(addr: u32) -> usize {
pub fn addr_of_word(addr: u32) -> u32 {
(addr & !3) as u32
}

pub fn is_mul_4(addr: u32) -> bool {
addr.rem_euclid(4) == 0
}
//----------------------------------

impl Word<u8> {
Expand Down
Loading