Skip to main content

Crate pak

Crate pak 

Source
Available on crate feature pak only.
Expand description

The Game Pak’s own hardware.

The memory bank controller answers writes to the ROM address range as register writes, and one 8 KiB window at 0xA000..=0xBFFF shows whatever those registers last selected. For example SRAM, where a game saves its data.

§Sharing the window

Every peripheral here shares that window, and selecting one deselects the rest, so they do not nest:

// Wrong: the second read comes from the rtc.
FILE.open(cs, |f| {
    let a = f.gold.get();
    let t = rtc::time(cs);   // selects rtc registers
    let b = f.gold.get();    // reads the rtc, not the save
});

An interrupt handler nests the same way without the code showing it, and one racing a scope for the same bytes would be a data race. Sram::open takes a CriticalSection to rule that out, from gb::interrupt::free or critical_section::with. The rest only write registers, and the rule above is all that keeps a handler off them.

§What a cartridge has

cargo-gb derives the capabilities below from cartridge_type and ram_size in header.toml, so a program that reaches for hardware the cartridge lacks fails to compile rather than writing to a chip that is not there.

MBC1 spends the same two register bits on ROM banks above 512 KiB and on SRAM banks, so wide_banks and more than one SRAM bank cannot both be set.

ModuleNeedsPresent on
sramram_size above zeroMBC1, MBC3, MBC5
rtc+TIMERMBC3
rumble+RUMBLEMBC5
tilt, eepromMBC7MBC7

Modules§

eepromgb_pak_tilt
MBC7’s save storage: 128 words on a serial EEPROM.
rtcgb_pak_rtc
The MBC3 clock: seconds to days, running on the cartridge battery.
rumblegb_pak_rumble
The MBC5 rumble motor.
sram
The cartridge’s own RAM, usually kept alive by a battery.
tiltgb_pak_tilt
The MBC7 accelerometer.

Structs§

CriticalSection
Critical section token.

Attribute Macros§

sram
Place a value at the base of an SRAM bank.