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
#![no_std]
#![allow(dead_code)]
#![allow(unused)]
#![allow(non_upper_case_globals)]
#![warn(missing_docs)]

//! GBA Memory-mapped IO (MMIO) stuff.
//!
//! The GBA is controlled via MMIO. An MMIO location is called a "register",
//! similar to the CPU having registers. The different registers are all
//! globally accessible, so user code must have its own convention for who is
//! supposed to touch what values when.
//!
//! This crate defines newtypes that describe the data layout of each register,
//! and if you enable the `unsafe_addresses` feature it also defines the
//! addresses of each register. The address type used for each register lets you
//! know about the safety (or not) of accessing a given register. The safety
//! assessment relies on the code being run only on a GBA, so if you use this
//! crate on your desktop or something that's your fault.
//!
//! This is only "work in progress" status, but please [file an
//! issue](https://github.com/Lokathor/rubidium/issues) if things are unclear.
//! While you're waiting you can also check
//! [GBATEK](https://problemkaputt.de/gbatek.htm), which is the standard
//! resource for GBA info (and it's most of where this crate's docs come from).
//!
//! ## Naming Conventions
//!
//! The names used for the address of each register is generally the same as, or
//! close to, the the name given for that register in GBATEK.
//!
//! For each setting within a register data newtype there will be three methods
//! per setting contained within the newtype:
//! * A const "getter" named the same as the setting (eg: `foo`).
//! * A const "with-er" which takes `self` and returns a new value (eg:
//!   `with_foo`).
//! * A "setter" which takes `&mut self` and updates the value in place (eg:
//!   `set_foo`). This will hopefully also be const in a future version of Rust.

macro_rules! pub_const_fn_new_zero {
  () => {
    pub const fn new() -> Self {
      Self(0)
    }
  };
}

//

#[rustfmt::skip]
macro_rules! bit {
   (0) => { 0b1 };
   (1) => { 0b10 };
   (2) => { 0b100 };
   (3) => { 0b1000 };
   (4) => { 0b1_0000 };
   (5) => { 0b10_0000 };
   (6) => { 0b100_0000 };
   (7) => { 0b1000_0000 };
   (8) => { 0b1_0000_0000 };
   (9) => { 0b10_0000_0000 };
  (10) => { 0b100_0000_0000 };
  (11) => { 0b1000_0000_0000 };
  (12) => { 0b1_0000_0000_0000 };
  (13) => { 0b10_0000_0000_0000 };
  (14) => { 0b100_0000_0000_0000 };
  (15) => { 0b1000_0000_0000_0000 };
}

macro_rules! bool_bit_u16 {
  ($bit:tt, $get:ident, $with:ident, $set:ident) => {
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub const fn $get(self) -> bool {
      any_in_mask(self.0 as u32, bit!($bit))
    }
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub const fn $with(self, b: bool) -> Self {
      Self(force_mask_bits(self.0 as u32, bit!($bit), b) as u16)
    }
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub fn $set(&mut self, b: bool) {
      *self = self.$with(b);
    }
  };
}

macro_rules! const_enum_bits_u16 {
  ($the_type:tt, $mask:literal, $get:ident, $with:ident, $set:ident) => {
    #[must_use]
    #[inline(always)]
    pub const fn $get(self) -> $the_type {
      $the_type(self.0 & $mask)
    }
    #[must_use]
    #[inline(always)]
    pub const fn $with(self, t: $the_type) -> Self {
      Self(merge_mask_bits(self.0 as u32, t.0 as u32, $mask) as u16)
    }
    #[must_use]
    #[inline(always)]
    pub fn $set(&mut self, t: $the_type) {
      *self = self.$with(t);
    }
  };
}

macro_rules! unsigned_bits_u16 {
  ($low_bit:literal ..= $high_bit:literal, $get:ident, $with:ident, $set:ident) => {
    #[must_use]
    #[inline(always)]
    pub const fn $get(self) -> u16 {
      const MASK: u32 = (0b1 << ($high_bit - $low_bit + 1)) - 1;
      const SHIFT: u32 = u16::trailing_zeros(MASK as u16);
      const BASE_MASK: u32 = (MASK >> SHIFT) as u32;
      ((self.0 as u32 >> SHIFT) & BASE_MASK) as u16
    }
    #[must_use]
    #[inline(always)]
    pub const fn $with(self, u: u16) -> Self {
      const MASK: u32 = (0b1 << ($high_bit - $low_bit + 1)) - 1;
      const SHIFT: u32 = u16::trailing_zeros(MASK as u16);
      const BASE_MASK: u32 = MASK >> SHIFT;
      let bits = (u as u32 & BASE_MASK) << SHIFT;
      Self(merge_mask_bits(self.0 as u32, bits, MASK) as u16)
    }
    #[must_use]
    #[inline(always)]
    pub fn $set(&mut self, u: u16) {
      *self = self.$with(u);
    }
  };
}

//

#[rustfmt::skip]
macro_rules! bit32 {
   (0) => { 0b1 };
   (1) => { 0b10 };
   (2) => { 0b100 };
   (3) => { 0b1000 };
   (4) => { 0b1_0000 };
   (5) => { 0b10_0000 };
   (6) => { 0b100_0000 };
   (7) => { 0b1000_0000 };
   (8) => { 0b1_0000_0000 };
   (9) => { 0b10_0000_0000 };
  (10) => { 0b100_0000_0000 };
  (11) => { 0b1000_0000_0000 };
  (12) => { 0b1_0000_0000_0000 };
  (13) => { 0b10_0000_0000_0000 };
  (14) => { 0b100_0000_0000_0000 };
  (15) => { 0b1000_0000_0000_0000 };
  (16) => { 0b1_0000_0000_0000_0000 };
  (17) => { 0b10_0000_0000_0000_0000 };
  (18) => { 0b100_0000_0000_0000_0000 };
  (19) => { 0b1000_0000_0000_0000_0000 };
  (20) => { 0b1_0000_0000_0000_0000_0000 };
  (21) => { 0b10_0000_0000_0000_0000_0000 };
  (22) => { 0b100_0000_0000_0000_0000_0000 };
  (23) => { 0b1000_0000_0000_0000_0000_0000 };
  (24) => { 0b1_0000_0000_0000_0000_0000_0000 };
  (25) => { 0b10_0000_0000_0000_0000_0000_0000 };
  (26) => { 0b100_0000_0000_0000_0000_0000_0000 };
  (27) => { 0b1000_0000_0000_0000_0000_0000_0000 };
  (28) => { 0b1_0000_0000_0000_0000_0000_0000_0000 };
  (29) => { 0b10_0000_0000_0000_0000_0000_0000_0000 };
  (30) => { 0b100_0000_0000_0000_0000_0000_0000_0000 };
  (31) => { 0b1000_0000_0000_0000_0000_0000_0000_0000 };
}

macro_rules! bool_bit_u32 {
  ($bit:tt, $get:ident, $with:ident, $set:ident) => {
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub const fn $get(self) -> bool {
      any_in_mask(self.0, bit32!($bit))
    }
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub const fn $with(self, b: bool) -> Self {
      Self(force_mask_bits(self.0, bit32!($bit), b))
    }
    #[must_use]
    #[inline(always)]
    #[allow(missing_docs)]
    pub fn $set(&mut self, b: bool) {
      *self = self.$with(b);
    }
  };
}

macro_rules! unsigned_bits_u32 {
  ($low_bit:literal ..= $high_bit:literal, $get:ident, $with:ident, $set:ident) => {
    #[must_use]
    #[inline(always)]
    pub const fn $get(self) -> u32 {
      const MASK: u32 = (0b1 << ($high_bit - $low_bit + 1)) - 1;
      const SHIFT: u32 = u32::trailing_zeros(MASK);
      const BASE_MASK: u32 = (MASK >> SHIFT);
      ((self.0 >> SHIFT) & BASE_MASK)
    }
    #[must_use]
    #[inline(always)]
    pub const fn $with(self, u: u32) -> Self {
      const MASK: u32 = (0b1 << ($high_bit - $low_bit + 1)) - 1;
      const SHIFT: u32 = u32::trailing_zeros(MASK);
      const BASE_MASK: u32 = MASK >> SHIFT;
      let bits = (u & BASE_MASK) << SHIFT;
      Self(merge_mask_bits(self.0, bits, MASK))
    }
    #[must_use]
    #[inline(always)]
    pub fn $set(&mut self, u: u32) {
      *self = self.$with(u);
    }
  };
}

//

macro_rules! submodule {
  ($v:vis $name:ident) => {
    mod $name;
    $v use $name::*;
  };
  ($v:vis $name:ident { $($content:tt)* }) => {
    mod $name { $($content)* }
    $v use $name::*;
  };
}

submodule!(pub video {
  use super::*;

  submodule!(pub bg_affine);
  submodule!(pub bg_cnt);
  submodule!(pub bg_ofs);
  submodule!(pub blending);
  submodule!(pub dispcnt);
  submodule!(pub dispstat);
  submodule!(pub mosaic);
  submodule!(pub vcount);
  submodule!(pub video_mode);
  submodule!(pub window);
});

submodule!(pub volatile {
  use super::*;

  use core::{
    marker::PhantomData,
    num::NonZeroUsize,
    ptr::{read_volatile, write_volatile},
  };

  submodule!(pub danger_write_vol_addr);
  submodule!(pub read_only_vol_addr);
  submodule!(pub simple_vol_addr);
  submodule!(pub write_only_vol_addr);
});

/// Returns `true` if any bits within the mask bits are active.
#[must_use]
#[inline(always)]
pub(crate) const fn any_in_mask(val: u32, mask: u32) -> bool {
  val & mask > 0
}

/// Forces all bits within the mask to be the bool given.
#[must_use]
#[inline(always)]
pub(crate) const fn force_mask_bits(val: u32, mask: u32, b: bool) -> u32 {
  let f = b as u32;
  let neg_f = f.wrapping_neg();
  val ^ ((neg_f ^ val) & mask)
}

/// Merge the bits of two values according to the mask.
#[must_use]
#[inline(always)]
pub(crate) const fn merge_mask_bits(
  not_in_mask: u32,
  in_mask: u32,
  mask: u32,
) -> u32 {
  let a = not_in_mask;
  let b = in_mask;
  a ^ ((a ^ b) & mask)
}