Skip to main content

with_vram_bank

Function with_vram_bank 

Source
pub fn with_vram_bank<R>(bank: VramBank, f: impl FnOnce() -> R) -> R
Available on crate feature cgb only.
Expand description

Run f with bank mapped at 0x8000, then put back the one that was there.

The Game Boy Color banks all of 0x8000..0xA000, so the same tile or map write lands in a different place depending on this. Bracketing it keeps that visible at the call site and out of the surrounding code.

f takes nothing: the Access an enclosing Vblank::with or with_lcd_off handed out is still good here, since which bank is mapped and whether video memory is reachable are separate questions. The two scopes nest in either order.

A handler that changes the bank must put it back, the way one that switches ROM banks must, and one that writes video memory has to set the bank it means rather than assume zero: it may well have interrupted this.

An original Game Boy has no such register: the write goes nowhere and f runs against the one bank there is, overwriting what was already in it. A cartridge that runs on both machines has to ask is_cgb first, there being no second bank to fall back to.

ppu::with_lcd_off(|d| {
    tile::write_all(d, 0, &TILES);
    ppu::with_vram_bank(VramBank::One, || tile::write_all(d, 0, &MORE));
});