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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::error;
use std::cell::{Ref, RefCell, RefMut};
use std::rc::Rc;

pub trait MemoryInterface {
    /// Read a 32bit word of at `addr`.
    ///
    /// The address where the read should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn read32(&mut self, address: u32) -> Result<u32, error::Error>;

    /// Read an 8bit word of at `addr`.
    ///
    /// The address where the read should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn read8(&mut self, address: u32) -> Result<u8, error::Error>;

    /// Read a block of 32bit words at `addr`.
    ///
    /// The number of words read is `data.len()`.
    /// The address where the read should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn read_block32(&mut self, address: u32, data: &mut [u32]) -> Result<(), error::Error>;

    /// Read a block of 8bit words at `addr`.
    ///
    /// The number of words read is `data.len()`.
    /// The address where the read should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn read_block8(&mut self, address: u32, data: &mut [u8]) -> Result<(), error::Error>;

    /// Write a 32bit word at `addr`.
    ///
    /// The address where the write should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn write32(&mut self, addr: u32, data: u32) -> Result<(), error::Error>;

    /// Write an 8bit word at `addr`.
    ///
    /// The address where the write should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn write8(&mut self, addr: u32, data: u8) -> Result<(), error::Error>;

    /// Write a block of 32bit words at `addr`.
    ///
    /// The number of words written is `data.len()`.
    /// The address where the write should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn write_block32(&mut self, addr: u32, data: &[u32]) -> Result<(), error::Error>;

    /// Write a block of 8bit words at `addr`.
    ///
    /// The number of words written is `data.len()`.
    /// The address where the write should be performed at has to be word aligned.
    /// Returns `AccessPortError::MemoryNotAligned` if this does not hold true.
    fn write_block8(&mut self, addr: u32, data: &[u8]) -> Result<(), error::Error>;
}

impl<T> MemoryInterface for &mut T
where
    T: MemoryInterface,
{
    fn read32(&mut self, address: u32) -> Result<u32, error::Error> {
        (*self).read32(address)
    }

    fn read8(&mut self, address: u32) -> Result<u8, error::Error> {
        (*self).read8(address)
    }

    fn read_block32(&mut self, address: u32, data: &mut [u32]) -> Result<(), error::Error> {
        (*self).read_block32(address, data)
    }

    fn read_block8(&mut self, address: u32, data: &mut [u8]) -> Result<(), error::Error> {
        (*self).read_block8(address, data)
    }

    fn write32(&mut self, addr: u32, data: u32) -> Result<(), error::Error> {
        (*self).write32(addr, data)
    }

    fn write8(&mut self, addr: u32, data: u8) -> Result<(), error::Error> {
        (*self).write8(addr, data)
    }

    fn write_block32(&mut self, addr: u32, data: &[u32]) -> Result<(), error::Error> {
        (*self).write_block32(addr, data)
    }

    fn write_block8(&mut self, addr: u32, data: &[u8]) -> Result<(), error::Error> {
        (*self).write_block8(addr, data)
    }
}

pub struct MemoryDummy;

impl MemoryInterface for MemoryDummy {
    fn read32(&mut self, _address: u32) -> Result<u32, error::Error> {
        unimplemented!()
    }
    fn read8(&mut self, _address: u32) -> Result<u8, error::Error> {
        unimplemented!()
    }
    fn read_block32(&mut self, _address: u32, _data: &mut [u32]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn read_block8(&mut self, _address: u32, _data: &mut [u8]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write32(&mut self, _address: u32, _data: u32) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write8(&mut self, _address: u32, _data: u8) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_block32(&mut self, _address: u32, _data: &[u32]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_block8(&mut self, _address: u32, _data: &[u8]) -> Result<(), error::Error> {
        unimplemented!()
    }
}

#[derive(Clone)]
pub struct Memory {
    inner: Rc<RefCell<dyn MemoryInterface>>,
}

impl Memory {
    pub fn new(memory: impl MemoryInterface + 'static) -> Self {
        Self {
            inner: Rc::new(RefCell::new(memory)),
        }
    }

    pub fn new_dummy() -> Self {
        Self::new(MemoryDummy)
    }

    pub fn memory_interface(&self) -> Ref<dyn MemoryInterface> {
        self.inner.borrow()
    }

    pub fn memory_interface_mut(&mut self) -> RefMut<dyn MemoryInterface> {
        self.inner.borrow_mut()
    }

    pub fn read32(&self, address: u32) -> Result<u32, error::Error> {
        self.inner.borrow_mut().read32(address)
    }

    pub fn read8(&self, address: u32) -> Result<u8, error::Error> {
        self.inner.borrow_mut().read8(address)
    }

    pub fn read_block32(&self, address: u32, data: &mut [u32]) -> Result<(), error::Error> {
        self.inner.borrow_mut().read_block32(address, data)
    }

    pub fn read_block8(&self, address: u32, data: &mut [u8]) -> Result<(), error::Error> {
        self.inner.borrow_mut().read_block8(address, data)
    }

    pub fn write32(&self, addr: u32, data: u32) -> Result<(), error::Error> {
        self.inner.borrow_mut().write32(addr, data)
    }

    pub fn write8(&self, addr: u32, data: u8) -> Result<(), error::Error> {
        self.inner.borrow_mut().write8(addr, data)
    }

    pub fn write_block32(&self, addr: u32, data: &[u32]) -> Result<(), error::Error> {
        self.inner.borrow_mut().write_block32(addr, data)
    }

    pub fn write_block8(&self, addr: u32, data: &[u8]) -> Result<(), error::Error> {
        self.inner.borrow_mut().write_block8(addr, data)
    }
}

pub struct MemoryList(Vec<Memory>);

impl MemoryList {
    pub fn new(memories: Vec<Memory>) -> Self {
        Self(memories)
    }
}

impl std::ops::Deref for MemoryList {
    type Target = Vec<Memory>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}