Skip to content

M3 DS Real

lifehackerhansol edited this page May 4, 2023 · 5 revisions

M3 DS Real

This page documents a subset of extra card commands supported by the M3 DS Real (herein M3). This page was written by lifehackerhansol based on previous documentation by Taiju Yamada (nee Xenon++).

Commands: what are they, what do they do (or at least some of them)

M3's original DLDI uses several different commands for reading and writing. There are single-sector read commands which align with the implemenation from the Original R4, as well as multi-sector reads.

Below are commands documented by Taiju Yamada in u64, as follows:

XXXXXXXX XXXXXXXX

Each two digits, from left to right, is REG_CARD_COMMAND[0] to REG_CARD_COMMAND[7] as defined in libnds.
Lowercase x will imply that this is unused. Any others are explained.

B0xxxxxx xxxxxxxx

  • Initialization / Get jumper pins
  • This is identical to the original R4's implementation.

BDaaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • preparation to read from SD/TF sector. wait until reading zero.

BAaaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • read from SD/TF sector

BEaaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • write to SD/TF sector

BCaaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • completion to write SD/TF sector. wait until reading zero.

CDxxxxxx xxxxxxxx

  • Get cart region
  • M3 has JP/EU/US regions; the only difference appears to be that the encryption key for the boot file are different.

Below are another set of commands, this time for multi-sector reading:

B1aaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • preparation to read multiple sectors from SD/TF. wait until reading zero.

B2aaaaaaa aaxxxxxx

  • aaaaaaaa: sector address requested in B1 (Don't need to x512)
  • preparation to read next sector from SD/TF. wait until reading zero.

B3aaaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512) // might not be needed
  • end multi-sector read or write from SD/TF.

B4aaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512)
  • write first sector of multi-sectors to SD/TF

B6aaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512) // might not be needed
  • completion to write one sector of multi-sectors to SD/TF. wait until reading zero.
  • it appears this is also needed after a B3 cmd to end multi-sector writes

B5aaaaaa aaxxxxxx

  • aaaaaaaa: sector address (Don't need to x512) // might not be needed
  • write next sector of multi-sectors to SD/TF

Basic FAT pseudocode(?)

Below describe basic SD card read/write routines, just enough to get a FAT file system up and running.

Here, we also use cardPolledTransfer from libnds. See here for reference: https://github.com/devkitPro/libnds/blob/master/source/common/card.c

Read

The following card command only supports single-sector reads of size 0x200 bytes. Unlike the original R4, sector addresses need not be << 9.

Multi-sector reads are also possible; it won't be added to the examples below.

Read 1 sector:

CARD_COMMAND = BDaaaaaa aaxxxxxx
aaaaaaaa = sector address (highest bit to lowest bit)
REG_ROMCTRL = 0xa7586000
loop command until REG_CARD_DATA_RD == 0
// zero means card is ready for transfer

CARD_COMMAND = BAaaaaaa aaxxxxxx
aaaaaaaa = sector address (highest bit to lowest bit) // unsure if required
REG_ROMCTRL = 0xa1586000
With the above control flag and command, do cardPolledTransfer to 0x200 byte buffer

To read more than 0x200 bytes, simply loop the above.

NOTE: devkitPro's cardPolledTransfer does not support unaligned buffers. Write a version of cardPolledTransfer yourself that handles it.

Write

Writing has the exact same restriction as read under these commands.

Writing 1 sector:

CARD_COMMAND = BEaaaaaa aaxxxxxx
aaaaaaaa = sector address (highest bit to lowest bit)

REG_ROMCTRL = 0xe1586000;
// from here, we need a reverse of cardPolledTransfer that will write u32 chunks to REG_CARD_DATA_RD
// make sure it handles unaligned buffers!
// loop until 0x200 bytes (128 u32s) are written

// instruct cart that data end
CARD_COMMAND = BCaaaaaa aaxxxxxx
aaaaaaaa = u32 sector (highest bit to lowest bit) // it is unknown whether this is needed
REG_ROMCTRL = 0xa7586000
loop command until REG_CARD_DATA_RD == 0
// zero means card is now idle

To write more than 0x200 bytes, simply loop the above.

Clone this wiki locally