Hacking NDS ROM File Format

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
Hello again,

Continuing on my quest to create a ROM Manager I am trying to find a good specification of the .nds ROM format.

What I found up until now is the following sites:

http://dsibrew.org/wiki/NDS_Format
http://www.bottledlight.com/ds/index.php/F...rmats/NDSFormat
http://imrannazar.com/The-Smallest-NDS-File

Although I had some luck reading the game title and some other data (which I verified using NDS Header Tool 2.0), I have problems of what type of data I am expecting to read, what are they for etc.

I also looked through the DeSMuME source code but had little luck on that.

Any ideas of where can I find more information about the format? Anyone here knows?

Thanks again!
 

Gryphon93

Well-Known Member
Member
Joined
Nov 30, 2008
Messages
145
Trophies
0
XP
252
Country
Wow! Why didn't I think of that? Of course the GBATek has all that information. Thanks a lot, FAST6191! (If you're wondering, I was also looking for this...)
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
FAST6191 said:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

You are my hero!!!!
biggrin.gif
:D:D:D:D:D

I just can't thank you enough really!
 

DanTheManMS

aka Ricochet Otter
Member
Joined
Jun 2, 2007
Messages
4,453
Trophies
1
Age
34
Location
Georgia
XP
752
Country
United States
I had to draw a picture to figure it out, but I think I understand it. Okay, so let's just take a look at a single tile first. This tile is defined by a block of bytes, 4 wide by 8 tall. If you write this out as a series of bits, at 8 bits to a byte this gives you a block that is 8 rows of 32 bits each. At 4 bits to a pixel ("4bit depth") this gives you 8 rows of 8 pixels, or in other words, an 8x8 block of pixels (a "tile"). You then have a 4x4 arrangement of these tiles, which gives you a 32x32 pixel image.

This diagram I whipped together might help explain what I'm trying to say here:

Code:
Here's the 4x8 arrangement of bytes for a single tile:

B B B B
B B B B
B B B B
B B B B
B B B B
B B B B
B B B B
B B B B

If we expand that out into bits, we get something like:

11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111

Then space it out into pixels, 4 bits per pixel:

1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111

Which turns into pixels of various colors:

Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl

So then a single tile looks like this in the end, 8x8 pixels:

- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 

Then a 4x4 grid of those:

- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + + 
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +

+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 

- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + + 
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +

+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -

Hope this is correct, and if it is, hope it helped.
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
DanTheManMS said:
I had to draw a picture to figure it out, but I think I understand it. Okay, so let's just take a look at a single tile first. This tile is defined by a block of bytes, 4 wide by 8 tall. If you write this out as a series of bits, at 8 bits to a byte this gives you a block that is 8 rows of 32 bits each. At 4 bits to a pixel ("4bit depth") this gives you 8 rows of 8 pixels, or in other words, an 8x8 block of pixels (a "tile"). You then have a 4x4 arrangement of these tiles, which gives you a 32x32 pixel image.

This diagram I whipped together might help explain what I'm trying to say here:

Code:
Here's the 4x8 arrangement of bytes for a single tile:

B B B B
B B B B
B B B B
B B B B
B B B B
B B B B
B B B B
B B B B

If we expand that out into bits, we get something like:

11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111
11110000 11110000 11110000 11110000
00001111 00001111 00001111 00001111

Then space it out into pixels, 4 bits per pixel:

1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111
1111 0000 1111 0000 1111 0000 1111 0000
0000 1111 0000 1111 0000 1111 0000 1111

Which turns into pixels of various colors:

Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl
Pixl Pixl Pixl Pixl Pixl Pixl Pixl Pixl

So then a single tile looks like this in the end, 8x8 pixels:

- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 
- - - - - - - - 

Then a 4x4 grid of those:

- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + + 
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +

+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 

- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + + 
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +
- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +

+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - - 
+ + + + + + + +ÂÂ- - - - - - - -ÂÂ+ + + + + + + +ÂÂ- - - - - - - -

Hope this is correct, and if it is, hope it helped.

...............

Wow! I'm absolutely blown away.... Only problem is, I have no idea how to split a byte into bits.. :S I am using Java.... I can do byte manipulation but not bit... I think it has something to do with shifting.... Which means I got a lot of reading to do right now... :/
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
DanTheManMS said:
http://www.exampledepot.com/egs/java.util/Bits2Array.html might help. It looks like it takes in a sequence of bytes and returns a byte array of the equivalent bits. There's probably an easier way, but I honestly haven't had enough experience with Java to really know what I'm doing in that regard.

I don't remember how many times I said this in this thread, but, Thank you!
tongue.gif
 

DanTheManMS

aka Ricochet Otter
Member
Joined
Jun 2, 2007
Messages
4,453
Trophies
1
Age
34
Location
Georgia
XP
752
Country
United States
More brainstorming here, but if it's 4 bits to a pixel, I doubt each pixel is absolutely defined in RGB (red green blue) terms. Do you happen to know if there's a set palette that all DS icons use or something? Or if the palette is defined elsewhere in the rom? Because with 4 bits you can represent the decimal numbers 0 through 15, so my guess is that there's a 16-entry palette somewhere and every 4-bit pixel merely references a palette index.

For comparison, the GBA in mode 3 uses 16 bits to represent a single pixel. 5 bits for the red component, 5 for the green, 5 for the blue, and the last one goes unused, giving you 32*32*32 = 32,768 possible color combinations. In mode 4, you only have 8 bits per pixel, so rather than splitting it into 2 red 2 green 2 blue (which would give you 4*4*4 = 64 color combinations) they use the palette method to give you up to 2^8 = 256 possible colors on screen at once.

If this icon is 4 bits per pixel, if you wanted to use the red/green/blue approach you'd only have 2*2*2 = 8 possible colors (white, black, red, green, blue, and 3 in-between), which is why I'm guessing they use the palette method to get up to 16 colors.

Again, this is all purely a guess, and I doubt it will help you much, but it's interesting to think about at least.
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
DanTheManMS said:
More brainstorming here, but if it's 4 bits to a pixel, I doubt each pixel is absolutely defined in RGB (red green blue) terms. Do you happen to know if there's a set palette that all DS icons use or something? Or if the palette is defined elsewhere in the rom? Because with 4 bits you can represent the decimal numbers 0 through 15, so my guess is that there's a 16-entry palette somewhere and every 4-bit pixel merely references a palette index.

For comparison, the GBA in mode 3 uses 16 bits to represent a single pixel. 5 bits for the red component, 5 for the green, 5 for the blue, and the last one goes unused, giving you 32*32*32 = 32,768 possible color combinations. In mode 4, you only have 8 bits per pixel, so rather than splitting it into 2 red 2 green 2 blue (which would give you 4*4*4 = 64 color combinations) they use the palette method to give you up to 2^8 = 256 possible colors on screen at once.

If this icon is 4 bits per pixel, if you wanted to use the red/green/blue approach you'd only have 2*2*2 = 8 possible colors (white, black, red, green, blue, and 3 in-between), which is why I'm guessing they use the palette method to get up to 16 colors.

Again, this is all purely a guess, and I doubt it will help you much, but it's interesting to think about at least.

Actually yes, there is the data of the color palette exactly after the image data. There are 16 colors of 16bit depth (32 bytes in total, 2 bytes each color). The first one is the transparent color.
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
DanTheManMS said:
More brainstorming here, but if it's 4 bits to a pixel, I doubt each pixel is absolutely defined in RGB (red green blue) terms. Do you happen to know if there's a set palette that all DS icons use or something? Or if the palette is defined elsewhere in the rom? Because with 4 bits you can represent the decimal numbers 0 through 15, so my guess is that there's a 16-entry palette somewhere and every 4-bit pixel merely references a palette index.

For comparison, the GBA in mode 3 uses 16 bits to represent a single pixel. 5 bits for the red component, 5 for the green, 5 for the blue, and the last one goes unused, giving you 32*32*32 = 32,768 possible color combinations. In mode 4, you only have 8 bits per pixel, so rather than splitting it into 2 red 2 green 2 blue (which would give you 4*4*4 = 64 color combinations) they use the palette method to give you up to 2^8 = 256 possible colors on screen at once.

If this icon is 4 bits per pixel, if you wanted to use the red/green/blue approach you'd only have 2*2*2 = 8 possible colors (white, black, red, green, blue, and 3 in-between), which is why I'm guessing they use the palette method to get up to 16 colors.

Again, this is all purely a guess, and I doubt it will help you much, but it's interesting to think about at least.

Now that you mentioned it, does anybody here knows how is the color encoded? Is it the same as the GBA mode 3? 5 bits for each component and the last one is unused?
 

pekalicious

Member
OP
Newcomer
Joined
Jan 12, 2010
Messages
21
Trophies
0
Age
40
Website
pekalicious.com
XP
79
Country
Greece
Gryphon93 said:
I guess (but I'm not sure...) that you'll find an answer to your question in the topic FAST6191 referred to earlier, namely this one: http://gbatemp.net/index.php?showtopic=45360. There's a lot of things mentioned there about how to read the palette and stuff, so I think you better check it out
wink.gif
.

OK.. After many hours of reading and hacking around I think I have finally found how to do it. My problem is that I am developing in Java and this is the first time I am developing in such low level image and bit manipulations. I am currently waiting from two communities to help me out with this. One in JavaRanch and the other in StackOverflow. If there is someone here that can also help I would be grateful (once again).

At this stage I have read most header information and made my first steps into decoding the filesystem. Currently my goal is to create a Java library that will decode a ROM. Once this is done I will definitely write a blog post about everything I learned and release the source code. Of course you will be the first to read about this since I own you everything.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    They're like chugging down two cans of gfuel
  • Xdqwerty @ Xdqwerty:
    Hi rez
    +1
  • Sicklyboy @ Sicklyboy:
    Hangovers are awful. Luckily I don't drink much these days to begin with, but they're the reason I don't drink - a lot- anymore
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Hey there!
    +1
  • K3Nv2 @ K3Nv2:
    Fucking energy drinks may as well be alcohol I remember chugging down 40oz half my body went numb
  • SylverReZ @ SylverReZ:
    @Sicklyboy, I don't even drink alcohol either.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, my throat still hurts
  • Xdqwerty @ Xdqwerty:
    Slightly less tho
  • SylverReZ @ SylverReZ:
    Same
  • SylverReZ @ SylverReZ:
    Get well soon
    +1
  • BakerMan @ BakerMan:
    i hope both of you guys get better
    +2
  • SylverReZ @ SylverReZ:
    Thanks mate.
  • Sicklyboy @ Sicklyboy:
    I used to drink alcohol fairly often. Never to the point of it being a problem, but like 2-3 beers with dinner each night, or a few cocktails or glasses of Scotch or something. Started smoking/vaping weed a lot a few years back which killed 90% of my interest in booze. Now I stopped smoking/vaping weed as much and just deal with life the boring way most of the time
    +1
  • Xdqwerty @ Xdqwerty:
    I only drank alcohol once and it was by accident
  • Xdqwerty @ Xdqwerty:
    I didnt know it was beer, it was on a juice bottle
  • SylverReZ @ SylverReZ:
    Yeah, I'm addicted to smoking, sadly. It's very addictive but I wish I didn't start.
  • K3Nv2 @ K3Nv2:
    May just order a 5700g for a nas/emulation set up tbh
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, atleast you were asleep on 4/20
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, you played that Mario flash game called Mario 63?
  • SylverReZ @ SylverReZ:
    @Xdqwerty, No, but I've seen it on Vinesauce's stream.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, that game is one of the reasons i met newgrounds bc the full versión of it is in that site
  • Xdqwerty @ Xdqwerty:
    Also somebody is remaking it
    Xdqwerty @ Xdqwerty: Also somebody is remaking it