From 6466a5347512ed003cb023021056ef49925d546a Mon Sep 17 00:00:00 2001 From: Lokathor Date: Sat, 29 Dec 2018 00:06:08 -0700 Subject: [PATCH] Move to the `phantom_fields!` proc-macro --- Cargo.toml | 2 +- examples/bg_demo.rs | 4 +-- src/bios.rs | 26 +++++++-------- src/io/background.rs | 21 ++++++------ src/io/display.rs | 76 ++++++++++++++++++++------------------------ src/io/dma.rs | 29 ++++++----------- src/io/keypad.rs | 60 ++++++++++++++++------------------ src/io/timers.rs | 9 ++++-- src/lib.rs | 2 +- src/oam.rs | 45 +++++++++++++------------- src/vram/text.rs | 14 +++++--- 11 files changed, 136 insertions(+), 152 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index eafa519..2e7a2d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/bg_demo.rs b/examples/bg_demo.rs index 472233c..515bd7e 100644 --- a/examples/bg_demo.rs +++ b/examples/bg_demo.rs @@ -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)); diff --git a/src/bios.rs b/src/bios.rs index bfdf0da..ce01862 100644 --- a/src/bios.rs +++ b/src/bios.rs @@ -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. diff --git a/src/io/background.rs b/src/io/background.rs index abe1a31..c5df78b 100644 --- a/src/io/background.rs +++ b/src/io/background.rs @@ -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, + } } /// The size of a background. diff --git a/src/io/display.rs b/src/io/display.rs index 421c93b..b5e52ae 100644 --- a/src/io/display.rs +++ b/src/io/display.rs @@ -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, + 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, + } } diff --git a/src/io/dma.rs b/src/io/dma.rs index 3ecd98e..7dca8de 100644 --- a/src/io/dma.rs +++ b/src/io/dma.rs @@ -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, + source_address_control: 7-8=DMASrcAddressControl, + dma_repeat: 9, + use_32bit: 10, + start_time: 12-13=DMAStartTiming, + irq_when_done: 14, + enabled: 15, + } } /// Sets how the destination address should be adjusted per data transfer. diff --git a/src/io/keypad.rs b/src/io/keypad.rs index 80e5d55..ec24995 100644 --- a/src/io/keypad.rs +++ b/src/io/keypad.rs @@ -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. diff --git a/src/io/timers.rs b/src/io/timers.rs index 026409e..6086ebe 100644 --- a/src/io/timers.rs +++ b/src/io/timers.rs @@ -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, + overflow_irq: 6, + enabled: 7, + } } /// Controls how often an enabled timer ticks upward. diff --git a/src/lib.rs b/src/lib.rs index c72a2cf..9361a12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. /// diff --git a/src/oam.rs b/src/oam.rs index f9c0cef..958158b 100644 --- a/src/oam.rs +++ b/src/oam.rs @@ -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, + obj_mode: 10-11=ObjectMode, + mosaic: 12, + is_8bpp: 13, + obj_shape: 14-15=ObjectShape, + } } /// 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, + } } /// 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, + } } diff --git a/src/vram/text.rs b/src/vram/text.rs index bb433f5..44a07d0 100644 --- a/src/vram/text.rs +++ b/src/vram/text.rs @@ -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! {