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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Control of an EVE chip at the level of directly accessing its memory-mapped
//! peripherals.

pub(crate) mod host_commands;
pub(crate) mod registers;

use crate::display_list::DLCmd;
use crate::error::Error;
use crate::interface::Interface;
use crate::memory::{HostAccessible, MemoryRegion, Ptr};
use crate::models::Model;
use core::marker::PhantomData;

#[doc(inline)]
pub use registers::Register;

#[doc(inline)]
pub use host_commands::HostCmd;

/// The result type for operations on a `LowLevel` associated with a
/// particular interface.
///
/// The error type for this result type is always
/// [`Error`](crate::error::Error).
pub type Result<R, I> = core::result::Result<R, Error<I>>;

/// `LowLevel` is a low-level interface to EVE controllers which matches
/// the primitive operations used in Programmers Guides for the various
/// EVE controllers.
///
/// This is slightly higher-level than the `Interface` trait, providing
/// size-specific memory accesses, but doesn't have any special knowledge
/// about the memory map or command set of any particular EVE implementation.
///
/// This struct tracks a "cursor" for appending display list entries using
/// the `dl` method. Use `dl_reset` to reset that cursor to the beginning of
/// display list memory, which you'll typically (but not necessarily) do after
/// writing `REG_DLSWAP` to swap the display list double buffer. `LowLevel`
/// doesn't manage display list buffer swapping itself, only the cursor for
/// the next `dl` call.
pub struct LowLevel<M: Model, I: Interface> {
    raw: I,
    next_dl: Ptr<M::DisplayListMem>,
    _model: PhantomData<M>,
}

impl<M: Model, I: Interface> LowLevel<M, I> {
    const END_ADDR: u32 = M::DisplayListMem::BASE_ADDR + M::DisplayListMem::LENGTH;

    pub fn new(interface: I) -> Self {
        LowLevel {
            raw: interface,
            next_dl: M::DisplayListMem::ptr(0),
            _model: PhantomData,
        }
    }

    pub(crate) fn result<R>(r: core::result::Result<R, I::Error>) -> Result<R, I> {
        r.map_err(|e| Error::Interface(e))
    }

    /// Consumes the `LowLevel` object and returns the interface it was
    /// originally created with.
    pub fn take_interface(self) -> I {
        self.raw
    }

    pub fn borrow_interface<'a>(&'a mut self) -> &'a mut I {
        &mut self.raw
    }

    pub fn wr8<R: HostAccessible>(&mut self, addr: Ptr<R>, v: u8) -> Result<(), I> {
        let data: [u8; 1] = [v];
        Self::result(self.raw.write(addr.to_raw(), &data))
    }

    pub fn wr16<R: HostAccessible>(&mut self, addr: Ptr<R>, v: u16) -> Result<(), I> {
        let data: [u8; 2] = [v as u8, (v >> 8) as u8];
        Self::result(self.raw.write(addr.to_raw(), &data))
    }

    pub fn wr32<R: HostAccessible>(&mut self, addr: Ptr<R>, v: u32) -> Result<(), I> {
        //let data: [u8; 4] = [(v >> 24) as u8, (v >> 16) as u8, (v >> 8) as u8, v as u8];
        let data: [u8; 4] = [v as u8, (v >> 8) as u8, (v >> 16) as u8, (v >> 24) as u8];
        Self::result(self.raw.write(addr.to_raw(), &data))
    }

    pub fn wr8s<R: HostAccessible>(&mut self, addr: Ptr<R>, v: &[u8]) -> Result<(), I> {
        Self::result(self.raw.write(addr.to_raw(), v))
    }

    pub fn rd8<R: HostAccessible>(&mut self, addr: Ptr<R>) -> Result<u8, I> {
        let mut data: [u8; 1] = [0; 1];
        Self::result(self.raw.read(addr.to_raw(), &mut data))?;
        Ok(data[0])
    }

    pub fn rd16<R: HostAccessible>(&mut self, addr: Ptr<R>) -> Result<u16, I> {
        let mut data: [u8; 2] = [0; 2];
        Self::result(self.raw.read(addr.to_raw(), &mut data))?;
        Ok((data[0] as u16) | (data[1] as u16) << 8)
    }

    pub fn rd32<R: HostAccessible>(&mut self, addr: Ptr<R>) -> Result<u32, I> {
        let mut data: [u8; 4] = [0; 4];
        Self::result(self.raw.read(addr.to_raw(), &mut data))?;
        Ok((data[0] as u32)
            | (data[1] as u32) << 8
            | (data[2] as u32) << 16
            | (data[3] as u32) << 24)
    }

    pub fn rd8s<R: HostAccessible>(&mut self, addr: Ptr<R>, into: &mut [u8]) -> Result<(), I> {
        Self::result(self.raw.read(addr.to_raw(), into))
    }

    pub fn main_mem_ptr(&self, offset: u32) -> Ptr<M::MainMem> {
        M::MainMem::ptr(offset)
    }

    pub fn reg_ptr(&self, reg: crate::registers::Register) -> Ptr<M::RegisterMem> {
        reg.ptr::<M>()
    }

    pub fn host_command(&mut self, cmd: HostCmd, a0: u8, a1: u8) -> Result<(), I> {
        Self::result(self.raw.host_cmd(cmd.to_raw(), a0, a1))
    }

    pub fn dl_reset(&mut self) {
        self.next_dl = M::DisplayListMem::ptr(0);
    }

    pub fn dl(&mut self, cmd: DLCmd) -> Result<(), I> {
        self.wr32(self.next_dl, cmd.into())?;

        let mut next_addr = self.next_dl.to_raw() + DLCmd::LENGTH;
        // If the next write would be out of range then we'll undo
        // our increment and let the next write just overwrite the
        // final entry in the display list. This means that a well-behaved
        // program which finishes its display list with the Display
        // command will still end up with that command at its end, even
        // though some of the commands were lost due to running out of
        // display list memory.
        if next_addr >= Self::END_ADDR {
            next_addr = Self::END_ADDR - DLCmd::LENGTH;
        }

        self.next_dl = M::DisplayListMem::ptr(next_addr);
        Ok(())
    }
}

impl<M, I> crate::display_list::Builder for LowLevel<M, I>
where
    M: Model,
    I: Interface,
{
    type Error = crate::error::Error<I>;
    type Model = M;

    fn append_raw_command(&mut self, raw: u32) -> Result<(), I> {
        self.dl(DLCmd::from_raw(raw))
    }

    fn append_command(&mut self, cmd: DLCmd) -> Result<(), I> {
        self.dl(cmd)
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    use super::*;
    use crate::interface::testing::{MockInterface, MockInterfaceCall};
    use crate::models::testing::Exhaustive;
    use std::vec;

    fn test_obj<F: FnOnce(&mut MockInterface)>(setup: F) -> LowLevel<Exhaustive, MockInterface> {
        let mut interface = MockInterface::new();
        setup(&mut interface);
        LowLevel::new(interface)
    }

    #[test]
    fn test_wr8() {
        let mut eve = test_obj(|_| {});
        let addr = eve.main_mem_ptr(0x1f);
        eve.wr8(addr, 0x34).unwrap();
        let got = eve.take_interface().calls();
        let want = vec![
            MockInterfaceCall::BeginWrite(0x1f),
            MockInterfaceCall::ContinueWrite(vec![0x34 as u8]),
            MockInterfaceCall::EndWrite(0x1f),
        ];
        assert_eq!(&got[..], &want[..]);
    }

    #[test]
    fn test_wr16() {
        let mut eve = test_obj(|_| {});
        let addr = eve.main_mem_ptr(0x2f);
        eve.wr16(addr, 0x1234).unwrap();
        let got = eve.take_interface().calls();
        let want = vec![
            MockInterfaceCall::BeginWrite(0x2f),
            MockInterfaceCall::ContinueWrite(vec![0x34, 0x12 as u8]),
            MockInterfaceCall::EndWrite(0x2f),
        ];
        assert_eq!(&got[..], &want[..]);
    }

    #[test]
    fn test_wr32() {
        let mut eve = test_obj(|_| {});
        let addr = eve.main_mem_ptr(0x3f);
        eve.wr32(addr, 0x12345678).unwrap();
        let got = eve.take_interface().calls();
        let want = vec![
            MockInterfaceCall::BeginWrite(0x3f),
            MockInterfaceCall::ContinueWrite(vec![0x78, 0x56, 0x34, 0x12 as u8]),
            MockInterfaceCall::EndWrite(0x3f),
        ];
        assert_eq!(&got[..], &want[..]);
    }

    #[test]
    fn test_wr8s() {
        let mut eve = test_obj(|_| {});
        let addr = eve.main_mem_ptr(0x3f);
        let data = ['h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8];
        eve.wr8s(addr, &data[..]).unwrap();
        let got = eve.take_interface().calls();
        let want = vec![
            MockInterfaceCall::BeginWrite(0x3f),
            MockInterfaceCall::ContinueWrite(vec![
                'h' as u8, 'e' as u8, 'l' as u8, 'l' as u8, 'o' as u8,
            ]),
            MockInterfaceCall::EndWrite(0x3f),
        ];
        assert_eq!(&got[..], &want[..]);
    }

    #[test]
    fn test_rd8() {
        let mut eve = test_obj(|ei| {
            let data = [0x34 as u8];
            ei.setup_mem(0x1f, &data[..])
        });
        let addr = eve.main_mem_ptr(0x1f);
        let got = eve.rd8(addr).unwrap();
        assert_eq!(got, 0x34);

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![
            MockInterfaceCall::BeginRead(0x1f),
            MockInterfaceCall::ContinueRead(1),
            MockInterfaceCall::EndRead(0x1f),
        ];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }

    #[test]
    fn test_rd16() {
        let mut eve = test_obj(|ei| {
            let data = [0x12, 0x34 as u8];
            ei.setup_mem(0x2f, &data[..])
        });
        let addr = eve.main_mem_ptr(0x2f);
        let got = eve.rd16(addr).unwrap();
        assert_eq!(got, 0x3412); // EVE is little-endian

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![
            MockInterfaceCall::BeginRead(0x2f),
            MockInterfaceCall::ContinueRead(2),
            MockInterfaceCall::EndRead(0x2f),
        ];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }

    #[test]
    fn test_rd32() {
        let mut eve = test_obj(|ei| {
            let data = [0x12, 0x34, 0x56, 0x78 as u8];
            ei.setup_mem(0x3f, &data[..])
        });
        let addr = eve.main_mem_ptr(0x3f);
        let got = eve.rd32(addr).unwrap();
        assert_eq!(got, 0x78563412); // EVE is little-endian

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![
            MockInterfaceCall::BeginRead(0x3f),
            MockInterfaceCall::ContinueRead(4),
            MockInterfaceCall::EndRead(0x3f),
        ];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }

    #[test]
    fn test_rd8s() {
        let orig_data = b"hello";
        let mut eve = test_obj(|ei| ei.setup_mem(0x4f, &orig_data[..]));
        let addr = eve.main_mem_ptr(0x4f);
        let mut read_data: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
        eve.rd8s(addr, &mut read_data).unwrap();

        // We read more than we wrote, so we'll see some default
        // values here standing in for uninitialized memory.
        let want_data = b"hello\xff\xff\xff";
        assert_eq!(&read_data[..], &want_data[..]);

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![
            MockInterfaceCall::BeginRead(0x4f),
            MockInterfaceCall::ContinueRead(8),
            MockInterfaceCall::EndRead(0x4f),
        ];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }

    #[test]
    fn test_host_command() {
        let mut eve = test_obj(|_| {});
        eve.host_command(HostCmd::ACTIVE, 0x23, 0x45).unwrap();

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![MockInterfaceCall::Cmd(0x00, 0x23, 0x45)];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }

    #[test]
    fn test_dl() {
        let mut eve = test_obj(|_| {});

        eve.dl(DLCmd::begin(
            crate::display_list::options::GraphicsPrimitive::Points,
        ))
        .unwrap();
        eve.dl(DLCmd::alpha_test(
            crate::display_list::options::TestFunc::Never,
            3,
        ))
        .unwrap();
        eve.dl_reset();
        eve.dl(DLCmd::begin(
            crate::display_list::options::GraphicsPrimitive::Bitmaps,
        ))
        .unwrap();

        let base = <Exhaustive as Model>::DisplayListMem::BASE_ADDR;

        let got_calls = eve.take_interface().calls();
        let want_calls = vec![
            MockInterfaceCall::BeginWrite(base + 0),
            MockInterfaceCall::ContinueWrite(vec![2, 0, 0, 31 as u8]),
            MockInterfaceCall::EndWrite(base + 0),
            MockInterfaceCall::BeginWrite(base + 4),
            MockInterfaceCall::ContinueWrite(vec![3, 0, 0, 9 as u8]),
            MockInterfaceCall::EndWrite(base + 4),
            MockInterfaceCall::BeginWrite(base + 0),
            MockInterfaceCall::ContinueWrite(vec![1, 0, 0, 31 as u8]),
            MockInterfaceCall::EndWrite(base + 0),
        ];
        assert_eq!(&got_calls[..], &want_calls[..]);
    }
}