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

Author Topic: Using C to read NES source code?  (Read 8555 times)

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Using C to read NES source code?
« on: July 28, 2012, 07:20:06 pm »
I don't have much experience with ASM but I do have a lot of experience with C/C++. I do know the basics on 16-bit asm like labels and input and output and some interrupts like int 21 and variables too.

So my question is, could I decompile NES files to C code? If so, I hear a lot of RPGs use structs and classes instead of standard variables. So those are the only things I really care about changing.

Klarth

  • Sr. Member
  • ****
  • Posts: 495
    • View Profile
Re: Using C to read NES source code?
« Reply #1 on: July 28, 2012, 07:31:58 pm »
So my question is, could I decompile NES files to C code? If so, I hear a lot of RPGs use structs and classes instead of standard variables.
No, it's a useless pursuit.  While a program like Hex Rays can decompile some code sensibly to C, the NES doesn't even have standard libraries to get contextual information from for library calls.  All of your variables will have meaningless names and your code won't be much more understandable than assembly code.  NES programmers didn't use any OOP constructs (no need to), so there are no classes.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Using C to read NES source code?
« Reply #2 on: July 28, 2012, 07:43:11 pm »
Are there structs? I've seen structs in 32-bit asm.

Klarth

  • Sr. Member
  • ****
  • Posts: 495
    • View Profile
Re: Using C to read NES source code?
« Reply #3 on: July 28, 2012, 07:56:32 pm »
Are there structs? I've seen structs in 32-bit asm.
Well, you can do both structs AND classes in assembly, since the compiler has to target an assembly language.  Classes are very unnatural looking in assembly, so they aren't used.  There is nothing built-in to aid in the process either.  Much like how you can still use OOP design patterns in C, though it has no OOP support.  Structs are just a way of arranging memory, much like an array.  So the same concept is used in assembly.  But you're missing the point.  You have no variable names, so you don't have any context as to what the data actually is.

Imagine opening up a large C project that deals with image processing and uses no standard libraries.  You then rename every variable to a randomly generated string, such as "d080000".  Then you rename every function in the same fashion.  Try to understand the source code.  This is the amount of context lost when a program is compiled and then you try to "decompile" it.

Bregalad

  • Hero Member
  • *****
  • Posts: 2751
    • View Profile
Re: Using C to read NES source code?
« Reply #4 on: July 29, 2012, 04:23:48 am »
Nope.
This will simply never be possible.

The majority of games were probably not even written in C for the first place.

danke

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 2067
    • View Profile
Re: Using C to read NES source code?
« Reply #5 on: July 29, 2012, 05:15:45 am »
Even NES games with leftover code in them are coded in pure 6502 ASM. You'd have to disassemble the ROM into ASM, and then translate it to C, adding your own labels as you figure out what they do. It's simply not worth it as the others have pointed out.

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Using C to read NES source code?
« Reply #6 on: July 29, 2012, 09:16:26 am »
It's doable, but time consuming and mostly pointless.

And changing the format of structs in already compiled code is very difficult. Because the compiled code will have sizes and offsets hardcoded into it, which is a mess to undo, especially since all identifiers are lost.

InfamousKnight

  • Sr. Member
  • ****
  • Posts: 321
    • View Profile
Re: Using C to read NES source code?
« Reply #7 on: August 03, 2012, 02:50:56 pm »
Wait, so those JRPGs like final fantasy didn't use structs or classes? Man, that would be tedious. Unless theres some strategy I don't know.

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: Using C to read NES source code?
« Reply #8 on: August 03, 2012, 04:09:51 pm »
C structs don’t exist in machine code. They’re just an abstraction that the compiler turns into sizes and offsets. You can make sizes and offsets by hand, too, and that’s what coders generally do if they need a struct but they’re using pure assembly (... buuuut then there’s Konami Krack). But on a limited system like the NES you tend to just put variables at whatever address in RAM you feel like, and any tables that would warrant any sort of “struct” are going to be pretty small anyway.

The general strategy is to label and comment things well when you code (or when you disassemble), because all that abstraction goes away at assembly/compile time (unless, say, you have debug mode on...). Also, in the NES era, the programming paradigm programmers tended to learn was not OOP, but structured programming, which basically boils down to “don’t write spaghetti code”. The kind of organization you get with OOP is a bit wasteful of resources when it comes to the size and scope of NES games.

If you want to see the real strategies programmers used in real games, you can find some disassemblies in the Documents section....
we are in a horrible and deadly danger

syntax error

  • Full Member
  • ***
  • Posts: 227
    • View Profile
Re: Using C to read NES source code?
« Reply #9 on: August 03, 2012, 04:36:05 pm »
You can write own 6502 code using slightly limited C compilers like cc65,Atalan language or BASIC.