Skip to content

Improve SD card writes - fixes issues with some SD cards - #113

Open
max-las wants to merge 8 commits into
sd2psXtd:developfrom
max-las:improve-sd-card-writes
Open

max-las wants to merge 8 commits into
sd2psXtd:developfrom
max-las:improve-sd-card-writes

Conversation

@max-las

@max-las max-las commented Sep 19, 2026 •

Copy link
Copy Markdown

This PR fixes two bugs I had with one of my SD cards on PS1 (PS2 could be affected too but to a lesser extend). These two bugs arise from the same starting point: on dedicated SPI mode, SdFat always writes to the SD card using CMD25 (WRITE_MULTIPLE_BLOCK) only. In theory this allows to coalesce the writes issued from the memory card into a single CMD25 stream which is closed upon flush(). But the current implementation has a few issues.

The save corruption bug

A PS1 sends frames of 128 bytes which is smaller than the SD card base sector (512 bytes). Therefore in SdFat, these 128-byte writes are coalesced in a 512-byte cache, which is prepared by reading the corresponding 512-byte sector from the SD card. That means that every four 128-byte frames, a new sector is read for the SD card. However, like any other command, a read closes any ongoing CMD25 stream (SharedSpiCard::cardCommand starts with syncDevice()). Therefore on PS1 the successive 128-bytes writes turn into an chain that goes open CMD25 - write - close CMD25 - read - open CMD25 - write - close CMD25 - read an so on, ruining the benefits of using CMD25.

Now the SD card I had issues with struggles with CMD25 writes that end after only one SD sector, and with the interleaved reads between the writes. As result, it took tens of seconds to write a full save (after the game reports saving finished). Power off the console before this delay is elapsed (very legit since as far as the player can tell the save is done) and the save is corrupted.

The phantom save bug

On PS1 deleting a save takes one 128-byte frame, mutated in one 512-byte SD sector. However like I said earlier, my SD card struggles with CMD25 writes that end after only one SD sector. As a result, sometimes after deleting a save from the PS1 BIOS, the deleted save would reappear after a reboot.

The fixes

  • 128-byte PS1 frames are now coalesced into 512-byte SD sectors upfront in ps1_dirty_task(), removing the interleaved reads that were breaking the CMD25 stream. This fixes the save corruption.
  • In PS1 and PS2, the 512-byte SD sectors themselves are coalesced into a buffer before writing. This allows the detection of isolated sectors (sectors that would have been written alone using CMD25, which has been proven to be problematic so we want to handle them differently).
  • Isolated sectors are now written using CMD24 (WRITE_BLOCK), the SD card command dedicated to single block writes. This fixes the phantom save bug.

Sidenotes

  • The buffer that coalesces 512-byte sectors has been given a size of 8192 bytes, the size of a PS1 block, which is convenient. Theoretically it could be any 512-byte multiple, but choosing an optimal size arguably comes with benefits in terms of speed and SD card wear (less calls to write()).
  • The same causes to the phantom save bug might also induce save corruption (and that's where the PS2 might also be affected): if an isolated sector is emitted as part of save, the card might choke on it and result in save corruption.
  • The SD card that unveiled this issues works well on a PC, and also in SD2PSX with these fixes implemented, so it can't really be called defective and other cards on the market may have the same quirks.

@max-las
max-las force-pushed the improve-sd-card-writes branch from 4eb483c to 26320e2 Compare September 28, 2026 15:58
@max-las
max-las force-pushed the improve-sd-card-writes branch from 26320e2 to 13c62bf Compare October 18, 2026 20:17
@XxTriviumxX

XxTriviumxX commented Nov 13, 2026 •

Copy link
Copy Markdown

What brand of sd card is affected? and will the fix be implemented in 1.5.0?

@max-las

max-las commented Nov 20, 2026 •

Copy link
Copy Markdown
Author

Happened to me on a Kingston 8GB card that came bundled with Kaico's SD2PSX. Does not seem to happen on recent sandisk, Toshiba/Exercia, Samsung, or Philips cards

@max-las

max-las commented Nov 21, 2026 •

Copy link
Copy Markdown
Author

@bbsan2k If the issues covered in this PR are confirmed to only occur with low quality cards (as in cards that don't even support all SPI commands, as you said), then it might be trimmed down. The logic to force CMD24 commands and the coalescing of 512-byte sectors into a 8192 bytes buffer before writing could go. However the interleaved reads issue with 128-byte PS1 frames seems to be real, although this is more of an optimization issue. So I'd keep the 128-byte PS1 frames coalescing into 512-byte SD sectors in ps1_dirty_task(), and drop the rest. What do you think?

@max-las
max-las force-pushed the improve-sd-card-writes branch from 13c62bf to 8fe4b77 Compare November 29, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants