/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use cameleon_impl::memory::*; #[memory] pub struct Memory { le: LE, be: BE, } #[register_map(base = 0, endianness = LE)] pub enum LE { #[register(len = 1, access = RO, ty = BitField)] U8Bit = 0b1, #[register(len = 1, access = RO, ty = BitField)] U8 = 0b1011, #[register(len = 1, access = RO, ty = BitField)] U8Full = 0xff, #[register(len = 1, access = RO, ty = BitField)] I8 = -3, #[register(len = 1, access = RO, ty = BitField)] I8Full = -128, #[register(len = 4, access = RO, ty = BitField)] U32Bit = 0b1, #[register(len = 4, access = RO, ty = BitField)] U32 = 0b1_0010_1110_1101, #[register(len = 4, access = RO, ty = BitField)] I32 = -324, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned1 = 0b1_0011, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned2 = 0b0_1000, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned3 = 0b11_1111, #[register(len = 2, access = RO, ty = BitField, offset=22)] OverlapSigned1 = -1, #[register(len = 2, access = RO, ty = BitField, offset=22)] OverlapSigned2 = 0, } #[register_map(base = 100, endianness = BE)] pub enum BE { #[register(len = 1, access = RO, ty = BitField)] U8Bit = 0b1, #[register(len = 1, access = RO, ty = BitField)] U8 = 0b1011, #[register(len = 1, access = RO, ty = BitField)] U8Full = 0xff, #[register(len = 1, access = RO, ty = BitField)] I8 = -3, #[register(len = 1, access = RO, ty = BitField)] I8Full = -128, #[register(len = 4, access = RO, ty = BitField)] U32Bit = 0b1, #[register(len = 4, access = RO, ty = BitField)] U32 = 0b1_0010_1110_1101, #[register(len = 4, access = RO, ty = BitField)] I32 = -324, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned1 = 0b1_0011, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned2 = 0b0_1000, #[register(len = 2, access = RO, ty = BitField, offset=20)] OverlapUnsigned3 = 0b11_1111, #[register(len = 2, access = RO, ty = BitField, offset=22)] OverlapSigned1 = -1, #[register(len = 2, access = RO, ty = BitField, offset=22)] OverlapSigned2 = 0, } fn main() { // Test LE. assert_eq!(LE::size(), 22 + 2); let mut memory = Memory::new(); assert_eq!(memory.read::().unwrap(), 0b1); memory.write::(0).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0); assert_eq!(LE::U8Bit::LSB, 1); assert_eq!(LE::U8Bit::MSB, 1); assert_eq!(memory.read::().unwrap(), 0b1011); memory.write::(0b0110).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0110); assert_eq!(LE::U8::LSB, 1); assert_eq!(LE::U8::MSB, 4); assert_eq!(memory.read::().unwrap(), 0xff); memory.write::(0xb3).unwrap(); assert_eq!(memory.read::().unwrap(), 0xb3); assert_eq!(memory.read::().unwrap(), -3); memory.write::(4).unwrap(); assert_eq!(memory.read::().unwrap(), 4); assert_eq!(memory.read::().unwrap(), -128); memory.write::(127).unwrap(); assert_eq!(memory.read::().unwrap(), 127); assert_eq!(memory.read::().unwrap(), 0b1); memory.write::(0).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0); assert_eq!(memory.read::().unwrap(), 0b1_0010_1110_1101); memory.write::(0b0_0100_0011_1000).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0_0100_0011_1000); assert_eq!(memory.read::().unwrap(), -324); memory.write::(241).unwrap(); assert_eq!(memory.read::().unwrap(), 241); assert_eq!(memory.read::().unwrap(), 0b1_0011); assert_eq!(memory.read::().unwrap(), 0b0_1000); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b1_1100).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_1000); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b0_0111).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_0111); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b00_0000).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_0111); assert_eq!(memory.read::().unwrap(), 0b00_0000); assert_eq!(memory.read::().unwrap(), -1); assert_eq!(memory.read::().unwrap(), 0); memory.write::(103).unwrap(); assert_eq!(memory.read::().unwrap(), 103); assert_eq!(memory.read::().unwrap(), 0); memory.write::(-1).unwrap(); assert_eq!(memory.read::().unwrap(), 103); assert_eq!(memory.read::().unwrap(), -1); // Test BE. assert_eq!(BE::size(), 22 + 2); let mut memory = Memory::new(); assert_eq!(memory.read::().unwrap(), 0b1); memory.write::(0).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0); assert_eq!(memory.read::().unwrap(), 0b1011); memory.write::(0b0110).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0110); assert_eq!(BE::U8::LSB, 4); assert_eq!(BE::U8::MSB, 1); assert_eq!(memory.read::().unwrap(), 0xff); memory.write::(0xb3).unwrap(); assert_eq!(memory.read::().unwrap(), 0xb3); assert_eq!(memory.read::().unwrap(), -3); memory.write::(4).unwrap(); assert_eq!(memory.read::().unwrap(), 4); assert_eq!(memory.read::().unwrap(), -128); memory.write::(127).unwrap(); assert_eq!(memory.read::().unwrap(), 127); assert_eq!(memory.read::().unwrap(), 0b1); memory.write::(0).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0); assert_eq!(memory.read::().unwrap(), 0b1_0010_1110_1101); memory.write::(0b0_0100_0011_1000).unwrap(); assert_eq!(memory.read::().unwrap(), 0b0_0100_0011_1000); assert_eq!(memory.read::().unwrap(), -324); memory.write::(241).unwrap(); assert_eq!(memory.read::().unwrap(), 241); assert_eq!(memory.read::().unwrap(), 0b1_0011); assert_eq!(memory.read::().unwrap(), 0b0_1000); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b1_1100).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_1000); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b0_0111).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_0111); assert_eq!(memory.read::().unwrap(), 0b11_1111); memory.write::(0b00_0000).unwrap(); assert_eq!(memory.read::().unwrap(), 0b1_1100); assert_eq!(memory.read::().unwrap(), 0b0_0111); assert_eq!(memory.read::().unwrap(), 0b00_0000); assert_eq!(memory.read::().unwrap(), -1); assert_eq!(memory.read::().unwrap(), 0); memory.write::(103).unwrap(); assert_eq!(memory.read::().unwrap(), 103); assert_eq!(memory.read::().unwrap(), 0); memory.write::(-1).unwrap(); assert_eq!(memory.read::().unwrap(), 103); assert_eq!(memory.read::().unwrap(), -1); }