Move to the phantom_fields! proc-macro

This commit is contained in:
Lokathor 2018-12-29 00:06:08 -07:00
parent bee2d16a5b
commit 6466a53475
11 changed files with 136 additions and 152 deletions

View file

@ -13,7 +13,7 @@ publish = false
[dependencies]
typenum = "1.10"
gba-proc-macro = "0.4.1"
gba-proc-macro = "0.5"
#[dev-dependencies]
#quickcheck="0.7"

View file

@ -29,8 +29,8 @@ fn main(_argc: isize, _argv: *const *const u8) -> isize {
set_bg_tile_4bpp(0, 0, ALL_TWOS);
set_bg_tile_4bpp(0, 1, ALL_THREES);
// screenblock
let light_entry = TextScreenblockEntry::from_tile_index(0);
let dark_entry = TextScreenblockEntry::from_tile_index(1);
let light_entry = TextScreenblockEntry::from_tile_id(0);
let dark_entry = TextScreenblockEntry::from_tile_id(1);
checker_screenblock(8, light_entry, dark_entry);
// bg0 control
BG0CNT.write(BackgroundControlSetting::new().with_screen_base_block(8));

View file

@ -8,7 +8,7 @@
//! whatever value is necessary for that function). Some functions also perform
//! necessary checks to save you from yourself, such as not dividing by zero.
use super::bool_bits;
use super::*;
//TODO: ALL functions in this module should have `if cfg!(test)` blocks. The
//functions that never return must panic, the functions that return nothing
@ -109,19 +109,17 @@ newtype! {
}
#[allow(missing_docs)]
impl RegisterRAMResetFlags {
bool_bits!(
u8,
[
(0, ewram),
(1, iwram),
(2, palram),
(3, vram),
(4, oam),
(5, sio),
(6, sound),
(7, other_io),
]
);
phantom_fields! {
self.0: u8,
ewram: 0,
iwram: 1,
palram: 2,
vram: 3,
oam: 4,
sio: 5,
sound: 6,
other_io: 7,
}
}
/// (`swi 0x02`) Halts the CPU until an interrupt occurs.

View file

@ -25,17 +25,16 @@ newtype! {
BackgroundControlSetting, u16
}
impl BackgroundControlSetting {
bool_bits!(u16, [(6, mosaic), (7, is_8bpp), (13, display_overflow_wrapping)]);
multi_bits!(
u16,
[
(0, 2, bg_priority),
(2, 2, char_base_block),
(8, 5, screen_base_block),
(2, 2, size, BGSize, Zero, One, Two, Three),
]
);
phantom_fields! {
self.0: u16,
bg_priority: 0-1,
char_base_block: 2-3,
mosaic: 6,
is_8bpp: 7,
screen_base_block: 8-12,
affine_display_overflow_wrapping: 13,
size: 14-15=BGSize<Zero, One, Two, Three>,
}
}
/// The size of a background.

View file

@ -31,26 +31,23 @@ newtype!(
#[allow(missing_docs)]
impl DisplayControlSetting {
bool_bits!(
u16,
[
(3, cgb_mode),
(4, frame1),
(5, hblank_interval_free),
(6, oam_memory_1d),
(7, force_vblank),
(8, bg0),
(9, bg1),
(10, bg2),
(11, bg3),
(12, obj),
(13, win0),
(14, win1),
(15, obj_window)
]
);
multi_bits!(u16, [(0, 3, mode, DisplayMode, Mode0, Mode1, Mode2, Mode3, Mode4, Mode5)]);
phantom_fields! {
self.0: u16,
mode: 0-2=DisplayMode<Mode0, Mode1, Mode2, Mode3, Mode4, Mode5>,
cgb_mode: 3,
frame1: 4,
hblank_interval_free: 5,
oam_memory_1d: 6,
force_vblank: 7,
bg0: 8,
bg1: 9,
bg2: 10,
bg3: 11,
obj: 12,
win0: 13,
win1: 14,
obj_window: 15,
}
}
/// The six display modes available on the GBA.
@ -113,19 +110,16 @@ newtype!(
#[allow(missing_docs)]
impl DisplayStatusSetting {
bool_bits!(
u16,
[
(0, vblank_flag),
(1, hblank_flag),
(2, vcounter_flag),
(3, vblank_irq_enable),
(4, hblank_irq_enable),
(5, vcounter_irq_enable),
]
);
multi_bits!(u16, [(8, 8, vcount_setting)]);
phantom_fields! {
self.0: u16,
vblank_flag: 0,
hblank_flag: 1,
vcounter_flag: 2,
vblank_irq_enable: 3,
hblank_irq_enable: 4,
vcounter_irq_enable: 5,
vcount_setting: 8-15,
}
}
/// Vertical Counter (LY). Read only.
@ -178,13 +172,11 @@ newtype! {
MosaicSetting, u16
}
impl MosaicSetting {
multi_bits!(
u16,
[
(0, 4, bg_horizontal_inc),
(4, 4, bg_vertical_inc),
(8, 4, obj_horizontal_inc),
(12, 4, obj_vertical_inc),
]
);
phantom_fields! {
self.0: u16,
bg_horizontal_inc: 0-3,
bg_vertical_inc: 4-7,
obj_horizontal_inc: 8-11,
obj_vertical_inc: 12-15,
}
}

View file

@ -68,25 +68,16 @@ newtype! {
}
#[allow(missing_docs)]
impl DMAControlSetting {
bool_bits!(u16, [(9, dma_repeat), (10, use_32bit), (14, irq_when_done), (15, enabled)]);
multi_bits!(
u16,
[
(
5,
2,
dest_address_control,
DMADestAddressControl,
Increment,
Decrement,
Fixed,
IncrementReload
),
(7, 2, source_address_control, DMASrcAddressControl, Increment, Decrement, Fixed),
(12, 2, start_time, DMAStartTiming, Immediate, VBlank, HBlank, Special)
]
);
phantom_fields! {
self.0: u16,
dest_address_control: 5-6=DMADestAddressControl<Increment, Decrement, Fixed, IncrementReload>,
source_address_control: 7-8=DMASrcAddressControl<Increment, Decrement, Fixed>,
dma_repeat: 9,
use_32bit: 10,
start_time: 12-13=DMAStartTiming<Immediate, VBlank, HBlank, Special>,
irq_when_done: 14,
enabled: 15,
}
}
/// Sets how the destination address should be adjusted per data transfer.

View file

@ -33,21 +33,19 @@ newtype! {
#[allow(missing_docs)]
impl KeyInput {
bool_bits!(
u16,
[
(0, a),
(1, b),
(2, select),
(3, start),
(4, right),
(5, left),
(6, up),
(7, down),
(8, r),
(9, l)
]
);
phantom_fields! {
self.0: u16,
a: 0,
b: 1,
select: 2,
start: 3,
right: 4,
left: 5,
up: 6,
down: 7,
r: 8,
l: 9,
}
/// Takes the set difference between these keys and another set of keys.
pub fn difference(self, other: Self) -> Self {
@ -107,23 +105,21 @@ newtype! {
}
#[allow(missing_docs)]
impl KeyInterruptSetting {
bool_bits!(
u16,
[
(0, a),
(1, b),
(2, select),
(3, start),
(4, right),
(5, left),
(6, up),
(7, down),
(8, r),
(9, l),
(14, irq_enabled),
(15, irq_logical_and)
]
);
phantom_fields! {
self.0: u16,
a: 0,
b: 1,
select: 2,
start: 3,
right: 4,
left: 5,
up: 6,
down: 7,
r: 8,
l: 9,
irq_enabled: 14,
irq_logical_and: 15,
}
}
/// Use this to configure when a keypad interrupt happens.

View file

@ -65,9 +65,12 @@ newtype! {
TimerControlSetting, u16
}
impl TimerControlSetting {
bool_bits!(u16, [(6, overflow_irq), (7, enabled)]);
multi_bits!(u16, [(0, 3, tick_rate, TimerTickRate, CPU1, CPU64, CPU256, CPU1024, Cascade),]);
phantom_fields! {
self.0: u16,
tick_rate: 0-2=TimerTickRate<CPU1, CPU64, CPU256, CPU1024, Cascade>,
overflow_irq: 6,
enabled: 7,
}
}
/// Controls how often an enabled timer ticks upward.

View file

@ -20,7 +20,7 @@
//! **Do not** use this crate in programs that aren't running on the GBA. If you
//! do, it's a giant bag of Undefined Behavior.
pub(crate) use gba_proc_macro::{bool_bits, multi_bits};
pub(crate) use gba_proc_macro::phantom_fields;
/// Assists in defining a newtype wrapper over some base type.
///

View file

@ -15,17 +15,15 @@ newtype! {
OBJAttr0, u16
}
impl OBJAttr0 {
bool_bits!(u16, [(12, mosaic), (13, is_8bpp),]);
multi_bits!(
u16,
[
(0, 8, row_coordinate),
(8, 2, obj_rendering, ObjectRender, Normal, Affine, Disabled, DoubleAreaAffine),
(10, 2, obj_mode, ObjectMode, Normal, SemiTransparent, OBJWindow),
(14, 2, obj_shape, ObjectShape, Square, Horizontal, Vertical),
]
);
phantom_fields! {
self.0: u16,
row_coordinate: 0-7,
obj_rendering: 8-9=ObjectRender<Normal, Affine, Disabled, DoubleAreaAffine>,
obj_mode: 10-11=ObjectMode<Normal, SemiTransparent, OBJWindow>,
mosaic: 12,
is_8bpp: 13,
obj_shape: 14-15=ObjectShape<Square, Horizontal, Vertical>,
}
}
/// What style of rendering for this object
@ -81,16 +79,14 @@ newtype! {
OBJAttr1, u16
}
impl OBJAttr1 {
bool_bits!(u16, [(12, hflip), (13, vflip),]);
multi_bits!(
u16,
[
(0, 9, col_coordinate),
(9, 5, affine_index),
(14, 2, obj_size, ObjectSize, Zero, One, Two, Three),
]
);
phantom_fields! {
self.0: u16,
col_coordinate: 0-8,
affine_index: 9-13,
hflip: 12,
vflip: 13,
obj_size: 14-15=ObjectSize<Zero, One, Two, Three>,
}
}
/// The object's size.
@ -127,5 +123,10 @@ newtype! {
OBJAttr2, u16
}
impl OBJAttr2 {
multi_bits!(u16, [(0, 10, tile_id), (10, 2, priority), (12, 4, palbank),]);
phantom_fields! {
self.0: u16,
tile_id: 0-9,
priority: 10-11,
palbank: 12-15,
}
}

View file

@ -9,13 +9,17 @@ newtype! {
}
impl TextScreenblockEntry {
/// Generates a default entry with the specified tile index.
pub const fn from_tile_index(index: u16) -> Self {
Self::new().with_tile_index(index)
pub const fn from_tile_id(id: u16) -> Self {
Self::new().with_tile_id(id)
}
bool_bits!(u16, [(10, hflip), (11, vflip)]);
multi_bits!(u16, [(0, 10, tile_index), (12, 4, palbank)]);
phantom_fields! {
self.0: u16,
tile_id: 0-9,
hflip: 10,
vflip: 11,
palbank: 12-15,
}
}
newtype! {