Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

Module job

Module job 

Source
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:

  1. Probe: the directory (frames 0..16) is read into the job, one frame per step.
  2. Plan: the caller’s closure runs the ordinary Card operations (format, write, delete, …) against a Staged card, 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.
  3. 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_VBLANKS while it commits to flash; steps that land inside the wait return Step::Waiting without 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. N is the number of frame writes it can queue (see queue_frames and FORMAT_FRAMES); the job holds N frames of 128 bytes plus the 2 KiB directory, so keep it in a static. The idle job is all zero bytes, so a static one 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 Block under the Card that CardJob::plan passes.

Enums§

Step
What one CardJob::step did.

Constants§

DIR_FRAMES
Directory frames a job reads before it plans: the MC header and the fifteen entries.
FORMAT_FRAMES
Frames a Card::format queues: 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 with CardJob::with_settle_vblanks.

Functions§

queue_frames
Queue frames for one Card::write of a payload_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.