Skip to content

Commit

Permalink
Add types for tile data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed May 24, 2024
1 parent 506e2f0 commit 09fc4bb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
7 changes: 4 additions & 3 deletions examples/mode0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use gba::{
bios::VBlankIntrWait,
mmio::{BG_PALRAM, DISPCNT, DISPSTAT, IE, IME},
sample_art::decompress_sample_font_to_vram_4bpp,
mmio::{BG_PALRAM, DISPCNT, DISPSTAT, IE, IME, VRAM_BG_TILE4},
sample_art::decompress_cga_face_to_vram_4bpp,
video::{Color, DisplayControl, DisplayStatus},
IrqBits,
};
Expand All @@ -13,7 +13,8 @@ gba::panic_handler!(empty_loop);

#[no_mangle]
pub extern "C" fn main() -> ! {
decompress_sample_font_to_vram_4bpp();
decompress_cga_face_to_vram_4bpp(VRAM_BG_TILE4.as_region());

BG_PALRAM.index(1).write(Color::WHITE);
// TODO: set up the tilemap to look like something interesting

Expand Down
10 changes: 9 additions & 1 deletion src/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use voladdress::VolAddress;
use voladdress::{VolBlock, VolGrid2d, VolGrid2dStrided};

use crate::{
video::{Color, DisplayControl, DisplayStatus},
video::{Color, DisplayControl, DisplayStatus, Tile4bpp},
IrqBits, KeyInput,
};

Expand Down Expand Up @@ -88,6 +88,14 @@ pub const BG_PALRAM: VolBlock<Color, SOGBA, SOGBA, 256> =
pub const OBJ_PALRAM: VolBlock<Color, SOGBA, SOGBA, 256> =
unsafe { VolBlock::new(0x0500_0000) };

/// The VRAM's background tile view, using 4bpp tiles.
pub const VRAM_BG_TILE4: VolBlock<Tile4bpp, SOGBA, SOGBA, 2048> =
unsafe { VolBlock::new(0x0600_0000) };

/// The VRAM's background tile view, using 8bpp tiles.
pub const VRAM_BG_TILE8: VolBlock<Tile4bpp, SOGBA, SOGBA, 1024> =
unsafe { VolBlock::new(0x0600_0000) };

/// The VRAM's view in Video Mode 3.
///
/// Each location is a direct color value.
Expand Down
20 changes: 16 additions & 4 deletions src/sample_art.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
use crate::{bios::LZ77UnCompReadNormalWrite16bit, mmio::MODE3_VRAM};
//! Sample art data that the crate uses in examples.
//!
//! You can also use this if you think it looks cool.
/// Decompresses the sample font (256 tiles) into VRAM.
pub fn decompress_sample_font_to_vram_4bpp() {
use voladdress::{Safe, VolRegion};

use crate::{bios::LZ77UnCompReadNormalWrite16bit, video::Tile4bpp};

/// Decompresses the sample CGA font face into VRAM.
///
/// The decompressed data will be 4bpp.
///
/// ## Panics
/// The provided destination needs to be at least 256 tiles, or this will panic.
pub fn decompress_cga_face_to_vram_4bpp(dest: VolRegion<Tile4bpp, Safe, Safe>) {
assert!(dest.len() >= 256);
// TODO: we should allow inputting *where* in VRAM the tiles go, instead of
// always using the VRAM base address.
unsafe {
LZ77UnCompReadNormalWrite16bit(
CGA_8X8_THICK_LZ77.as_ptr(),
MODE3_VRAM.as_usize() as *mut u16,
dest.as_mut_ptr().cast(),
)
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,33 @@ impl DisplayStatus {
Self(u16_with_value(8, 15, self.0, line as u16))
}
}

/// Data for a 4-bit-per-pixel tile.
///
/// The tile is 8 pixels wide and 8 pixels tall. Each pixel is 4 bits, giving an
/// index within the palbank for this tile's visual element. An index of 0 is a
/// "transparent" pixel. For alignment purposes, all the data is bit packed as
/// `u32` values.
///
/// Generally, you are expected to make tile art on your development machine in
/// some way, and then pack it into your ROM as a static value. The data is then
/// copied from ROM into the correct VRAM location at runtime. You are not
/// expected to manipulate particular pixels within a tile at runtime.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Tile4bpp(pub [u32; 8]);

/// Data for a 8-bit-per-pixel tile.
///
/// The tile is 8 pixels wide and 8 pixels tall. Each pixel is 8 bits, giving an
/// index within the full palette for this tile's visual element. An index of 0
/// is a "transparent" pixel. For alignment purposes, all the data is bit packed
/// as `u32` values.
///
/// Generally, you are expected to make tile art on your development machine in
/// some way, and then pack it into your ROM as a static value. The data is then
/// copied from ROM into the correct VRAM location at runtime. You are not
/// expected to manipulate particular pixels within a tile at runtime.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Tile8bpp(pub [u32; 16]);

0 comments on commit 09fc4bb

Please sign in to comment.