gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > [HowTo] Read data from DS-Slot ROM [emulators only]

#172842 - Ruben - Mon Mar 08, 2010 8:51 am

Hi all!

I just figured out how to read data from DS-Slot ROM [which I believe only works with emulators as I think flash software can only patch official stuff] so...

Setting up a framework

This step is quite important as it will define how you will setup your file reading routines.

Do you need directories? How long are the filenames allowed to be? Will they get exclusive rights/permissions?
Those are some questions you need to ask yourself.
For me, these are my answers:
-I do not need directories, merely names
-Filenames should only be 32-64 at most
-No exclusive permissions

Making a pseudo filesystem

Now that you have your answers, you will need to make a program that adds files to your NDS binary. Something simple would be similar to this..

-Pad NDS file to 200h bytes
-Find magic key for allocation table
-Setup parameters in said allocation table [things like filename, filesize [padded to 200h bytes], permissions, etc]
-Append files to NDS file, making sure to PAD EVERY FILE to 200h bytes

My program is rather simple, and can be found here: link

Accessing your filesystem

Now that you have your files appended to the binary [there to no be loaded into RAM automatically] and have your FAT set up, you now need a way to read from ROM to get the right data.

The first part is rather simple: You look through your FAT until you find the file you're after and load its parameters needed for loading.

Now the tricky part is using the registers correctly [this took me AGES to figure out].

The first thing to do is enable the NDS-slot. To do this, you merely ORR the right bit into AUXSPICNT
Code:
REG_AUXSPICNT |= 0x8000; //alternatively, you can write 0x80
                         //to the top byte

Now that the DS-slot is enabled you have to tell it what to do. This is done by issuing a read command like so..

B7aaaaaaaa000000 <- Read command [from gbaTek]

The B7 is the 'read' command. aaaaaaaa is the address/offset from which we want to read [aligned by 200h]. So the first thing to do is write the command to the register.
Code:
REG_DSCARD_CMD[0] = 0xB7,
REG_DSCARD_CMD[1] = offset>>24,
REG_DSCARD_CMD[2] = offset>>16,
REG_DSCARD_CMD[3] = offset>> 8,
REG_DSCARD_CMD[4] = offset,
REG_DSCARD_CMD[5] = 0,
REG_DSCARD_CMD[6] = 0,
REG_DSCARD_CMD[7] = 0;

Notice that the offset is MSB first.

Now that the command has been sent, we have to enable the transfer. This is done by writing to ROMCTRL:
Code:
REG_ROMCTRL = START + BIT(29) + SIZE(1); //A1000000h


Now the DS will read from ROM and give you the right data. How do we read it? Simple.
We wait until the block word is ready [bit 23, ROMCTRL] and read a word from 0x04100010. This is done until we've read the full 200h block.
If your file is bigger than 200h then you will need to do this more than once until all the 200h blocks are done.

My source can be found here: link

Final notes

Well, this article may have been a bit confusing but hopefully the source code will help you figure out the parts that make no sense ;D

I'd like to thank DekuTree for putting up with me for ages about this and whoever it was that made gbaTek for its specs. ^-^'

Lastly, the dsfsLoad function can be used and/or distributed freely. No credit required though it would be nice. Also, it only takes a single argument which is the filename; it mallocs the length of the file and returns its address.

Until later!

#172846 - wintermute - Mon Mar 08, 2010 1:20 pm

What's wrong with nitroFS?

Honestly, what's with all the wheel reinvention lately?
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#172848 - Ruben - Mon Mar 08, 2010 1:32 pm

Well, I personally hadn't seen nitroFS mainly cos I compile with my own cart+linkscripts so I don't really know about any updates on libnds >_>'

EDIT:
Also if I'm reading correctly, it reads from GBA-slot ROM rather than DS-slot ROM

#172851 - wintermute - Mon Mar 08, 2010 2:03 pm

Currently it reads from GBA slot on emulators and old style GBA flashcarts yes. On slot1 cards it reads via libfat.

If you *really* want to help then submit a patch which allows it to read from slot1 in desmume, that might even work on some recent slot1 flashcards.

The homebrew community needs yet another filesystem like it needs a hole in the head.

And just to make this absolutely clear, I really don't care what you do in your own applications for your own amusement. When it comes to releasing code for the community to use please, please work with the devkitPro supplied tools and libraries. You can provide the greatest benefit to the community as a whole by helping to improve those.

http://en.wikipedia.org/wiki/The_Paradox_of_Choice:_Why_More_Is_Less
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog