openvm_circuit/system/memory/controller/
interface.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use openvm_stark_backend::p3_field::PrimeField32;

use crate::system::memory::{
    merkle::{DirectCompressionBus, MemoryMerkleChip},
    persistent::PersistentBoundaryChip,
    volatile::VolatileBoundaryChip,
    MemoryImage, CHUNK,
};

#[allow(clippy::large_enum_variant)]
pub enum MemoryInterface<F> {
    Volatile {
        boundary_chip: VolatileBoundaryChip<F>,
    },
    Persistent {
        boundary_chip: PersistentBoundaryChip<F, CHUNK>,
        merkle_chip: MemoryMerkleChip<CHUNK, F>,
        initial_memory: MemoryImage<F>,
    },
}

impl<F: PrimeField32> MemoryInterface<F> {
    pub fn touch_address(&mut self, addr_space: u32, pointer: u32) {
        match self {
            MemoryInterface::Volatile { boundary_chip } => {
                boundary_chip.touch_address(addr_space, pointer);
            }
            MemoryInterface::Persistent {
                boundary_chip,
                merkle_chip,
                ..
            } => {
                boundary_chip.touch_address(addr_space, pointer);
                merkle_chip.touch_address(addr_space, pointer);
            }
        }
    }

    pub fn touch_range(&mut self, addr_space: u32, pointer: u32, len: u32) {
        match self {
            MemoryInterface::Volatile { boundary_chip } => {
                for offset in 0..len {
                    boundary_chip.touch_address(addr_space, pointer + offset);
                }
            }
            MemoryInterface::Persistent {
                boundary_chip,
                merkle_chip,
                ..
            } => {
                for offset in 0..len {
                    boundary_chip.touch_address(addr_space, pointer + offset);
                    merkle_chip.touch_address(addr_space, pointer + offset);
                }
            }
        }
    }

    pub fn compression_bus(&self) -> Option<DirectCompressionBus> {
        match self {
            MemoryInterface::Volatile { .. } => None,
            MemoryInterface::Persistent { merkle_chip, .. } => {
                Some(merkle_chip.air.compression_bus)
            }
        }
    }
}