···
+
pub const VRAM_BEGIN: usize = 0x8000;
+
pub const VRAM_END: usize = 0x9FFF;
+
pub const VRAM_SIZE: usize = VRAM_END - VRAM_BEGIN + 1;
+
#[derive(Copy, Clone, Debug, PartialEq)]
+
pub enum TilePixelValue {
+
/// Convert pixel value to grayscale color (0-255)
+
pub fn to_grayscale(&self) -> u8 {
+
TilePixelValue::Zero => 255, // White
+
TilePixelValue::One => 170, // Light gray (66% brightness)
+
TilePixelValue::Two => 85, // Dark gray (33% brightness)
+
TilePixelValue::Three => 0, // Black
+
/// Convert pixel value to RGB color tuple
+
pub fn to_rgb(&self) -> (u8, u8, u8) {
+
let gray = self.to_grayscale();
+
/// Convert pixel value to classic Game Boy green colors
+
pub fn to_gameboy_green(&self) -> (u8, u8, u8) {
+
TilePixelValue::Zero => (224, 248, 208), // Lightest green
+
TilePixelValue::One => (136, 192, 112), // Light green
+
TilePixelValue::Two => (52, 104, 86), // Dark green
+
TilePixelValue::Three => (8, 24, 32), // Darkest green/black
+
type Tile = [[TilePixelValue; 8]; 8];
+
fn empty_tile() -> Tile {
+
[[TilePixelValue::Zero; 8]; 8]
+
tile_set: [Tile; 384], // 384 tiles total (256 from first set + 128 from second set)
+
tile_set: [empty_tile(); 384],
+
pub fn read_vram(&self, address: usize) -> u8 {
+
pub fn write_vram(&mut self, index: usize, value: u8) {
+
self.vram[index] = value;
+
// If our index is greater than 0x1800, we're not writing to the tile set storage
+
// so we can just return.
+
// Tiles rows are encoded in two bytes with the first byte always
+
// on an even address. Bitwise ANDing the address with 0xffe
+
// gives us the address of the first byte.
+
let normalized_index = index & 0xFFFE;
+
// First we need to get the two bytes that encode the tile row.
+
let byte1 = self.vram[normalized_index];
+
let byte2 = self.vram[normalized_index + 1];
+
// A tile is 8 rows tall. Since each row is encoded with two bytes a tile
+
// is therefore 16 bytes in total.
+
let tile_index = index / 16;
+
// Every two bytes is a new row
+
let row_index = (index % 16) / 2;
+
// Now we're going to loop 8 times to get the 8 pixels that make up a given row.
+
for pixel_index in 0..8 {
+
let mask = 1 << (7 - pixel_index);
+
let lsb = byte1 & mask;
+
let msb = byte2 & mask;
+
let value = match (lsb != 0, msb != 0) {
+
(true, true) => TilePixelValue::Three,
+
(false, true) => TilePixelValue::Two,
+
(true, false) => TilePixelValue::One,
+
(false, false) => TilePixelValue::Zero,
+
self.tile_set[tile_index][row_index][pixel_index] = value;
+
/// Get a tile by its index
+
pub fn get_tile(&self, tile_index: usize) -> Option<&Tile> {
+
if tile_index < self.tile_set.len() {
+
Some(&self.tile_set[tile_index])
+
/// Render a tile to a color buffer (64 pixels as RGB values)
+
pub fn render_tile_to_rgb(&self, tile_index: usize) -> Option<[(u8, u8, u8); 64]> {
+
let tile = self.get_tile(tile_index)?;
+
let mut color_buffer = [(0, 0, 0); 64];
+
for (row_idx, row) in tile.iter().enumerate() {
+
for (col_idx, &pixel) in row.iter().enumerate() {
+
let buffer_index = row_idx * 8 + col_idx;
+
color_buffer[buffer_index] = pixel.to_gameboy_green();
+
/// Render a tile to grayscale buffer (64 pixels as grayscale values)
+
pub fn render_tile_to_grayscale(&self, tile_index: usize) -> Option<[u8; 64]> {
+
let tile = self.get_tile(tile_index)?;
+
let mut gray_buffer = [0u8; 64];
+
for (row_idx, row) in tile.iter().enumerate() {
+
for (col_idx, &pixel) in row.iter().enumerate() {
+
let buffer_index = row_idx * 8 + col_idx;
+
gray_buffer[buffer_index] = pixel.to_grayscale();
+
/// Render multiple tiles in a grid pattern
+
pub fn render_tile_map(
+
) -> Vec<(u8, u8, u8)> {
+
let total_pixels = map_width * 8 * map_height * 8; // 8x8 pixels per tile
+
let mut color_buffer = vec![(0, 0, 0); total_pixels];
+
for (map_idx, &tile_idx) in tile_indices.iter().enumerate() {
+
if let Some(tile) = self.get_tile(tile_idx as usize) {
+
let tile_x = map_idx % map_width;
+
let tile_y = map_idx / map_width;
+
for (row_idx, row) in tile.iter().enumerate() {
+
for (col_idx, &pixel) in row.iter().enumerate() {
+
let pixel_x = tile_x * 8 + col_idx;
+
let pixel_y = tile_y * 8 + row_idx;
+
let buffer_index = pixel_y * (map_width * 8) + pixel_x;
+
if buffer_index < color_buffer.len() {
+
color_buffer[buffer_index] = pixel.to_gameboy_green();
+
/// Debug function to print a tile as ASCII art
+
pub fn print_tile_ascii(&self, tile_index: usize) {
+
if let Some(tile) = self.get_tile(tile_index) {
+
// println!("Tile {}:", tile_index);
+
let char = match pixel {
+
TilePixelValue::Zero => '░', // Light
+
TilePixelValue::One => '▒', // Light gray
+
TilePixelValue::Two => '▓', // Dark gray
+
TilePixelValue::Three => '█', // Dark
+
println!("Tile {} not found", tile_index);