|
19 | 19 | //! has one field for each 16-bit group of attributes: [ObjAttr0], [ObjAttr1], |
20 | 20 | //! [ObjAttr2]. |
21 | 21 | //! |
22 | | -//! When you've got an object's data configured how you want, use either the |
23 | | -//! [`OBJ_ATTR_ALL`] control (to write all fields at once) or the [`OBJ_ATTR0`], |
24 | | -//! [`OBJ_ATTR1`], and/or [`OBJ_ATTR2`] controls (to write just some of the |
25 | | -//! fields). |
| 22 | +//! When you've got an object's data configured how you want, use the `write` |
| 23 | +//! function on the [ObjAttr] struct, the [ObjAttrWriteExt] extension trait for |
| 24 | +//! the [`OBJ_ATTR_ALL`] control (to write all fields at once), or the |
| 25 | +//! [`OBJ_ATTR0`], [`OBJ_ATTR1`], and/or [`OBJ_ATTR2`] controls (to write just |
| 26 | +//! some of the fields). |
26 | 27 | //! |
27 | 28 | //! **Note:** When the GBA first boots, the object layer will be off but the |
28 | 29 | //! object entries in OAM will *not* be set to prevent individual objects from |
29 | 30 | //! being displayed. Before enabling the object layer you should generally set |
30 | 31 | //! the [ObjDisplayStyle] of all [ObjAttr0] fields so that any objects you're |
31 | 32 | //! not using don't appear on the screen. Otherwise, you'll end up with |
32 | 33 | //! un-configured objects appearing in the upper left corner of the display. |
| 34 | +//! |
| 35 | +//! [`OBJ_ATTR_ALL`] is defined as read-only because writing to OAM requires |
| 36 | +//! halfword alignment, and at higher compiler optimization levels the compiler |
| 37 | +//! will generate a `memcpy` call which may do byte-wise writes. To avoid this, |
| 38 | +//! use the `write` function on the [ObjAttr] struct, the `write` function on |
| 39 | +//! the [ObjAttrWriteExt] trait implemented for the [`OBJ_ATTR_ALL`] control, |
| 40 | +//! or write to the individual [`OBJ_ATTR0`], [`OBJ_ATTR1`], and [`OBJ_ATTR2`] |
| 41 | +//! controls. |
33 | 42 |
|
34 | 43 | use super::*; |
35 | 44 |
|
@@ -157,4 +166,24 @@ impl ObjAttr { |
157 | 166 | pub fn set_palbank(&mut self, palbank: u16) { |
158 | 167 | self.2 = self.2.with_palbank(palbank); |
159 | 168 | } |
| 169 | + #[inline] |
| 170 | + pub fn write(&self, addr: VolAddress<ObjAttr, Safe, ()>) { |
| 171 | + unsafe { |
| 172 | + let addr: *mut u16 = addr.as_usize() as *mut u16; |
| 173 | + core::ptr::write_volatile(addr.add(0), self.0 .0); |
| 174 | + core::ptr::write_volatile(addr.add(1), self.1 .0); |
| 175 | + core::ptr::write_volatile(addr.add(2), self.2 .0); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +pub trait ObjAttrWriteExt { |
| 181 | + fn write(&self, attr: ObjAttr); |
| 182 | +} |
| 183 | + |
| 184 | +impl ObjAttrWriteExt for VolAddress<ObjAttr, Safe, ()> { |
| 185 | + #[inline] |
| 186 | + fn write(&self, attr: ObjAttr) { |
| 187 | + attr.write(*self); |
| 188 | + } |
160 | 189 | } |
0 commit comments