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
196
197
198
use crate::error;

pub trait MemoryInterface {
    /// Read a 32bit word of at `address`.
    ///
    /// 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_word_32(&mut self, address: u32) -> Result<u32, error::Error>;

    /// Read an 8bit word of at `address`.
    fn read_word_8(&mut self, address: u32) -> Result<u8, error::Error>;

    /// Read a block of 32bit words at `address`.
    ///
    /// 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_32(&mut self, address: u32, data: &mut [u32]) -> Result<(), error::Error>;

    /// Read a block of 8bit words at `address`.
    fn read_8(&mut self, address: u32, data: &mut [u8]) -> Result<(), error::Error>;

    /// Write a 32bit word at `address`.
    ///
    /// 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_word_32(&mut self, address: u32, data: u32) -> Result<(), error::Error>;

    /// Write an 8bit word at `address`.
    fn write_word_8(&mut self, address: u32, data: u8) -> Result<(), error::Error>;

    /// Write a block of 32bit words at `address`.
    ///
    /// 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_32(&mut self, address: u32, data: &[u32]) -> Result<(), error::Error>;

    /// Write a block of 8bit words at `address`.
    fn write_8(&mut self, address: u32, data: &[u8]) -> Result<(), error::Error>;

    /// Flush any outstanding operations.
    ///
    /// For performance, debug probe implementations may choose to batch writes;
    /// to assure that any such batched writes have in fact been issued, `flush`
    /// can be called.  Takes no arguments, but may return failure if a batched
    /// operation fails.
    fn flush(&mut self) -> Result<(), error::Error>;
}

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

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

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

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

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

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

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

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

    fn flush(&mut self) -> Result<(), error::Error> {
        (*self).flush()
    }
}

pub struct MemoryDummy;

impl<'probe> MemoryInterface for MemoryDummy {
    fn read_word_32(&mut self, _address: u32) -> Result<u32, error::Error> {
        unimplemented!()
    }
    fn read_word_8(&mut self, _address: u32) -> Result<u8, error::Error> {
        unimplemented!()
    }
    fn read_32(&mut self, _address: u32, _data: &mut [u32]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn read_8(&mut self, _address: u32, _data: &mut [u8]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_word_32(&mut self, _address: u32, _data: u32) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_word_8(&mut self, _address: u32, _data: u8) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_32(&mut self, _address: u32, _data: &[u32]) -> Result<(), error::Error> {
        unimplemented!()
    }
    fn write_8(&mut self, _address: u32, _data: &[u8]) -> Result<(), error::Error> {
        unimplemented!()
    }

    fn flush(&mut self) -> Result<(), error::Error> {
        unimplemented!()
    }
}

pub struct Memory<'probe> {
    inner: Box<dyn MemoryInterface + 'probe>,
}

impl<'probe> Memory<'probe> {
    pub fn new(memory: impl MemoryInterface + 'probe + Sized) -> Memory<'probe> {
        Self {
            inner: Box::new(memory),
        }
    }

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

    pub fn memory_interface(&self) -> &dyn MemoryInterface {
        self.inner.as_ref()
    }

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

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

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

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

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

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

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

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

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

    pub fn flush(&mut self) -> Result<(), error::Error> {
        self.inner.flush()
    }
}

pub struct MemoryList<'probe>(Vec<Memory<'probe>>);

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

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