Expand description
A card operation spread over game frames.
Card::write on a HardwareCard holds the CPU
for the whole operation: a directory read is a frame transaction of about
5 ms, a write is one plus the card’s commit time, and a save is dozens of
them. Called from a game loop that is a freeze of seconds.
A CardJob runs the same filesystem code in three bounded phases and
never does more than one card transaction per step:
- Probe: the directory (frames
0..16) is read into the job, one frame per step. - Plan: the caller’s closure runs the ordinary
Cardoperations (format,write,delete, …) against aStagedcard, which serves reads from the cached directory and the writes already queued, and queues every write instead of sending it. No card is touched and the cost is CPU only, in one step. - Write: the queued frames are sent in the order the filesystem code
wrote them, one per step. After each, the card is left alone for
SETTLE_VBLANKSwhile it commits to flash; steps that land inside the wait returnStep::Waitingwithout touching the port.
The queue keeps the filesystem’s write order, so the guarantees of
Card::write hold at every point: the old save stays whole until the new
one is complete, and the new file appears with its first directory entry
last. A card pulled between steps is in the same state it would be in if it
had been pulled during the blocking call.
The job does not own the port. Each step is handed the
device for that call alone, so a game keeps polling its pads between steps:
static mut JOB: CardJob<{ queue_frames(2048) }> = CardJob::new();
job.begin();
// every game frame:
let mut card = HardwareCard::on_port(&mut port, Slot::One);
match job.step(&mut card, vblank_count())? {
Step::NeedsPlan => job.plan(|c| c.write(NAME, TITLE, &save_bytes))?,
Step::Done => { /* saved */ }
_ => {}
}Structs§
- CardJob
- A save, format or delete run a little at a time.
Nis the number of frame writes it can queue (seequeue_framesandFORMAT_FRAMES); the job holdsNframes of 128 bytes plus the 2 KiB directory, so keep it in astatic. The idle job is all zero bytes, so astaticone costs no space in the executable. - Staged
- The card as the planning closure sees it. Reads come from the writes
queued so far, then from the cached directory; writes are queued, not
sent. It is the
Blockunder theCardthatCardJob::planpasses.
Enums§
- Step
- What one
CardJob::stepdid.
Constants§
- DIR_
FRAMES - Directory frames a job reads before it plans: the
MCheader and the fifteen entries. - FORMAT_
FRAMES - Frames a
Card::formatqueues: the header, fifteen entries, the broken-sector list and the cleared tail of the directory block. - SETTLE_
VBLANKS - Video periods the card is left alone after each frame write, while it
commits to flash. The blocking path (
HardwareCard::write_frame) spins 400,000 status reads there, five to six video periods in the emulator and at least two by the reference drivers’ rule; a job keeps the same wait, rounded up, by counting vblanks, so a game can spend it drawing instead. A job’s wait can be changed withCardJob::with_settle_vblanks.
Functions§
- queue_
frames - Queue frames for one
Card::writeof apayload_len-byte save over an older copy of the same size: the title and icon frames, the data frames (container header included), one directory entry per block written, and one per block of the copy released afterwards. A format (FORMAT_FRAMES) or the deletion of another file is on top.