News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: Nes sprite editing with only hex editor  (Read 8833 times)

Hypnotic_rabbit

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Nes sprite editing with only hex editor
« on: August 25, 2015, 07:01:33 am »
Some please answer my question, When changing the 8x8 pixels of a tile on an nes rom with an editor this also changes the hex values at the address where it is stored. Basicly what i want to know is how can I make the change with just a hex editor? Iv tried reverse engineering it but I cant make sense of the resulting hex values.

Grimlock

  • Sr. Member
  • ****
  • Posts: 366
  • Graphics Artist, Level Designer
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #1 on: August 25, 2015, 08:15:07 am »
Altering the pixels in an app such as tile layer pro affects only the look of the tiles not the location they're stored in memory.  What approach are using to edit your graphics, also what results are you after?

Hypnotic_rabbit

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #2 on: August 25, 2015, 08:37:57 am »
It does change the hex when you edit with tile editor pro. If you open up mario 3 and go to the very first tile which is one of marios ears and make the entire thing black and then go to the address where the sprite is it changes to all 00's. Iv also been experimenting with whats being done. If you fill the first line across with one of the four colors the result is the first 00 becoming FF, and then if you do the second line across the next 00s finish off on FF. The blue color(marios face color) on the other hand does not start where the other three colors do, it starts on the second set of 8 hex values of the 16 all together. Ill figure it out eventually with trial and error but to have someone tell me an editor isnt changing the hex values in the rom makes me laugh.

August 25, 2015, 08:40:12 am - (Auto Merged - Double Posts are not allowed before 7 days.)
Also the result im after is to make a game specific tile editor, which of course I will have to know how the values are written to the rom after the changes are made in my program.
« Last Edit: August 25, 2015, 09:02:56 am by Hypnotic_rabbit »

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #3 on: August 25, 2015, 09:15:09 am »
I think Grimlock misunderstood -- he thought you were saying the address of the data was changing, not the data itself.

You are correct.  The data does in fact change.

NES graphics are stored in a 2BPP planar format


- A "bitplane" is an 8x8 image with 2 colors per pixel.
- 1 byte of data is a 8x1 image, where each bit determines the color of one pixel ($80 is left-most pixel, then $40, then $20, $10, $08, $04, $02, then $01 is right-most)
- 8 bytes of data form the full 8x8 bitplane, where each byte represents a different row of pixels.
- NES uses 16 bytes (2 bitplanes) for each 8x8 tile.  First, 8 bytes for the "low" bitplane, then 8 more bytes for the "high" bitplane.  This allows for 2*2 = 4 colors per pixel  (bitplanes * colors per pixel on one bitplane = total colors per pixel)
- The "low" bitplane has a weight of 1, the "high" bitplane has a weight of 2

Example:

Code: [Select]
16 bytes:

41 C2 44 48 10 20 40 80     01 02 04 08 16 21 42 87
|    low bitplane     |     |    high bitplane    |

  low bitplane          high bitplane
$41 = %01000001       $01 = %00000002
$C2 = %11000010       $02 = %00000020
$44 = %01000100       $04 = %00000200
$48 = %01001000       $08 = %00002000
$10 = %00010000       $16 = %00020220
$20 = %00100000       $21 = %00200002
$40 = %01000000       $42 = %02000020
$80 = %10000000       $87 = %20000222

    full image (low + high) .. 0 replaced with '.' for clarity:
   
        .1.....3
        11....3.
        .1...3..
        .1..3...
        ...3.22.
        ..3....2
        .3....2.
        3....222



EDIT:  Note that while NES graphics have to be in this form eventually, it does not mean they will always be stored this way in the ROM.  99% of the time, they will be -- but every once in a while you'll run into a game which compresses graphics to save space -- in which case you'll have to figure out how to decompress/recompress the data.
« Last Edit: August 25, 2015, 09:23:53 am by Disch »

Hypnotic_rabbit

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #4 on: August 25, 2015, 09:44:49 am »
Ahh that makes a little more sense to me, the higher and lower part anyway. So this means the first four lines across the 8x8 is the lower and the next four lines across are the higher? This works with three of the colors but when I put the fourth color across the first line of the 8x8 it changes the hex starting at the higher hex values rather then the lower, why is that? Ill look more into this bitplane concept I guess but any links will be appreciated. Also can you explain what you mean by weight?

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #5 on: August 25, 2015, 10:00:54 am »
Quote
So this means the first four lines across the 8x8 is the lower and the next four lines across are the higher?

No.  Each bitplane is an 8x8 image.  Think of it like two sheets of transparent paper that you lay on top of each other.

Quote
Also can you explain what you mean by weight?

Each tile is 16 bytes.

The first 8 bytes is the low bitplane.  So take the first 8 bytes, and use it to form an 8x8 image.  This bitplane has a weight of 1... which means that pixels in the image can either be color 0 or color 1.

The second 8 bytes is the high bitplane.  So take those bytes and form a separate 8x8 image.  This bitplane has a weight of 2... which means that pixels can be either color 0 or color 2.

This gives you two separate 8x8 images... each of which are only 2-color.  To form the final 4-color image... you add the two bitplanes together:

final color 0 = pixel is 0 on both bitplanes
final color 1 = pixel is 1 on the low bitplane, 0 on the high bitplane
final color 2 = pixel is 0 on the low bitplane, 2 on the high bitplane
final color 3 = pixel is 1 on low bitplane, 2 on high bitplane


The example in my previous post illustrates this in action.

Hypnotic_rabbit

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #6 on: August 25, 2015, 10:22:16 am »
Well from what I thought I knew about it from trial and error I would say you just made it more complicated but im sure you have giving me the right way to think about it, thank you. Just the keywords you gave me should help a lot with looking it up. Im still a little fuzzy on this concept but at least I know more now then I did before.

dougeff

  • Sr. Member
  • ****
  • Posts: 358
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #7 on: August 25, 2015, 07:50:44 pm »
You don't need to understand it. Nobody edits graphics with a hex editor. Use a tile editor.
nesdoug.com -- blog/tutorial on programming for the NES

SunGodPortal

  • Hero Member
  • *****
  • Posts: 2917
  • 2 + 2 = 5
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #8 on: August 25, 2015, 08:21:43 pm »
Quote
You don't need to understand it. Nobody edits graphics with a hex editor. Use a tile editor.

I'm with this guy. Editing graphics with a hex editor seems like the most inefficient way imaginable.
War is Peace. Freedom is Slavery. Ignorance is Strength.

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #9 on: August 25, 2015, 08:29:00 pm »
Guys... he wants to understand the format so he can write an editor.

This is a perfectly legit question & approach.  If you can do it in a hex editor, you can write a program to do it.

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
Re: Nes sprite editing with only hex editor
« Reply #10 on: August 25, 2015, 10:19:45 pm »
Here's a visualization of what Mr. Disch explained above:

« Last Edit: August 27, 2015, 12:50:58 am by Dr. Floppy »

freem

  • Jr. Member
  • **
  • Posts: 19
  • ɯǝǝɹɟ
    • View Profile
    • AJWorld
Re: Nes sprite editing with only hex editor
« Reply #11 on: August 25, 2015, 11:11:17 pm »
Here's a visualization of what Mr. Disch explained above:


this image link will only work if you have a baddesthacks account; mind re-hosting somewhere else?

SunGodPortal

  • Hero Member
  • *****
  • Posts: 2917
  • 2 + 2 = 5
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #12 on: August 25, 2015, 11:25:34 pm »
Quote
Guys... he wants to understand the format so he can write an editor.

Mayks sens. :)
War is Peace. Freedom is Slavery. Ignorance is Strength.

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
Re: Nes sprite editing with only hex editor
« Reply #13 on: August 25, 2015, 11:30:13 pm »
Quote
this image link will only work if you have a baddesthacks account; mind re-hosting somewhere else?

Not too much. What's your preference?

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #14 on: August 25, 2015, 11:38:48 pm »
imgur / tinypic are easy go-to's for free image hosting.

you might have to sign up for tinypic, but pretty sure you don't have to for imgur

freem

  • Jr. Member
  • **
  • Posts: 19
  • ɯǝǝɹɟ
    • View Profile
    • AJWorld
Re: Nes sprite editing with only hex editor
« Reply #15 on: August 26, 2015, 12:38:59 am »
imgur doesn't require a login; that's typically a good place for images since it (hopefully) isn't going to screw things up like Imageshack did :p

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
Re: Nes sprite editing with only hex editor
« Reply #16 on: August 26, 2015, 02:29:23 am »
Imgur isn't behaving, so I'ma try something else. Does this show up?


Okay, apparently it doesn't. Let's try yet another option:

http://www.baddesthacks.net/hacks/old/stories/ppu_4.png
« Last Edit: August 26, 2015, 06:26:35 pm by Dr. Floppy »

Hypnotic_rabbit

  • Jr. Member
  • **
  • Posts: 35
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #17 on: August 26, 2015, 04:40:04 am »
Ok so I have taken the time to understand what is going on thanks to Disch and the few resources I could find on the topic. Disch is right, If you can do it manually you can do it programmatically and thats my goal here. I understand how all 16 of  the hex values become what they become now and I can do it manually to get the results that I want in terms of being able to place any "color" I want anywhere I want on the tile manually with a hex editor. Im talking about the defaults colors(black,green,blue,teal) in tile editor pro and not the pallette elsewhere in the hex btw. My question now becomes how to decode which is which with code when i read the data into the program, there are way too many possibilities to try and code for each one of them. Surely there is a logic he be found here givin that each "color" has its own unique traits it gives the resulting hex values. I guess what I need would be the source to an emulator too see how it does it but I havent had any luck finding a function that I could use to do what I want. But yeah, thats where Im at now, how to decipher which colored block is where based off of the hex values I find stored in the hex at any time when i read it into my program, any help on this? Any code in any language would be nice and very helpful here.

August 26, 2015, 08:46:09 am - (Auto Merged - Double Posts are not allowed before 7 days.)
I think i understand a little better now but I just want to ask, the lower plane generates 8 hex values that each represent a line on  the 8x8 tile. Converting these hex values into binary give you the 0 or 1 that determine if it is displayed or not. Doing the same with the higher plane tells you the 0 or 1 for its version of the 8x8. When the two planes are combined on a single plane the 0s and 1 will be opposite for the lower and higher planes that tells you which one to display  and if they match on 0's its transparent?  The part I dont get is where 2 and 3 come from, im guessing these are the other two colors of the four?
« Last Edit: August 26, 2015, 08:46:09 am by Hypnotic_rabbit »

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #18 on: August 26, 2015, 10:03:20 am »
My question now becomes how to decode which is which with code when i read the data into the program, there are way too many possibilities to try and code for each one of them. Surely there is a logic he be found here givin that each "color" has its own unique traits it gives the resulting hex values.


I'm not entirely sure I understand this question.

Once you have a 4-color image, the game will assign a palette to that image, which will tell you what colors to use for each pixel value.  If you take a look at the PPU viewer in FCEUX, you'll see that it has 8 palettes at the bottom.

On the NES... "pixel 0" is always transparent, and pixels 1-3 get mapped to the desired palette entry.  But for an editor, you can usually ignore the transparent thing and assign a 4th color for "pixel 0"... unless you want to draw multiple tiles on top of each other.

Where the palette is in the ROM is dependent on the game.  I'm sure there are docs available on this site which go over changing palettes in NES games.

Quote
how to decipher which colored block is where based off of the hex values I find stored in the hex at any time when i read it into my program, any help on this? Any code in any language would be nice and very helpful here.

?? Are you asking for a code example of how to decode NES graphics?

Here's some pseudo-code:

Code: [Select]
//  'output' is a 2D array to hold generated image.
//  output[y][x] represents 1 pixel in the image, and can have a value between 0-3

for( int y = 0; y < 8; ++y )
{
    byte lo = ROM[ offset + y     ];    // low bitplane byte for this row
    byte hi = ROM[ offset + y + 8 ];    // high bitplane byte for this row
   
    // left-most pixel is in bit 7 of the lo/hi bytes.  So we need to extract that bit
    //
    // ((hi >> 6) & 2)  <- this moves bit 7 to bit position 1:  H....... becomes ......H.
    //     then masks out the interesting 'X' bit
    //
    // ((lo >> 7) & 1)  <- this moves bit 7 to bit position 0:  L....... becomes .......L
    //
    // |'ing them together combines them:  ......H. | .......L becomes ......HL  which is our desired pixel
   
    output[y][0] = ((hi >> 6) & 2) | ((lo >> 7) & 1);
    output[y][1] = ((hi >> 5) & 2) | ((lo >> 6) & 1); // <- same but with bit 6
    output[y][2] = ((hi >> 4) & 2) | ((lo >> 5) & 1); // <- bit 5
    output[y][3] = ((hi >> 3) & 2) | ((lo >> 4) & 1); // <- bit 4
    output[y][4] = ((hi >> 2) & 2) | ((lo >> 3) & 1); // <- bit 3
    output[y][5] = ((hi >> 1) & 2) | ((lo >> 2) & 1); // <- bit 2
    output[y][6] = ( hi       & 2) | ((lo >> 1) & 1); // <- bit 1
    output[y][7] = ((hi << 1) & 2) | ( lo       & 1); // <- bit 0
}

Quote
When the two planes are combined on a single plane the 0s and 1 will be opposite for the lower and higher planes

Not necessarily.  The low plane and high plane can be completely different.

Quote
The part I dont get is where 2 and 3 come from, im guessing these are the other two colors of the four?

Yes, there are 4 pixel values:  0, 1, 2, and 3

If the coresponding bit in the low plane is set, you add 1 to the pixel value
If the coresponding bit in the high plane is set, you add 2 to the pixel value

pixel 0 = bit is clear in both planes
pixel 1 = bit is set in low plane, but not in high plane
pixel 2 = bit is set in high plane, but not in low plane
pixel 3 = bit is set in both planes

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Nes sprite editing with only hex editor
« Reply #19 on: August 26, 2015, 10:12:39 am »
In other words, each pixel is a 2 bit binary value. But they have for some inane reason decided to store the bits individually in separate parts of the data. :banghead: