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

Author Topic: Hex to 6502 Assembly?  (Read 2111 times)

linkncb16

  • Restricted Access
  • Full Member
  • *
  • Posts: 159
  • They actually updated this site. Hallelujah.
    • View Profile
    • Patreon
Hex to 6502 Assembly?
« on: May 12, 2016, 10:46:45 am »
If I'm not mistaken, NES games are programmed in a code called 6502 Assembly and compiled into Hex when complete so the computer can read the code. So my question would be is it possible to reverse the process and view the original code?
Final Fantasy Redux is complete! Download

Disch

  • Hero Member
  • *****
  • Posts: 2711
  • NES Junkie
    • View Profile
Re: Hex to 6502 Assembly?
« Reply #1 on: May 12, 2016, 11:42:06 am »
Sort of.  It's called "disassembling"

Many 6502 disassemblers are available on this site.

It's not the "original" code though, because you lose all meta-data in the process (variable names, comments, label names, etc).  Pretty much the only thing you get is a string of instructions and raw numerical values.  Though it's still extremely helpful.

Sliver X

  • Hero Member
  • *****
  • Posts: 1036
  • EX•CINERIBVS•IGNIS•EXCITABITVR
    • View Profile
    • Panicus - A Fragment of Memory
Re: Hex to 6502 Assembly?
« Reply #2 on: May 12, 2016, 12:00:25 pm »
I typically did all my (Granted, pretty minor) 6502 routines in pure machine code/hex. I memorized quite a few of the opcodes to do this, thanks to a chart like the following:

Opcode Chart (6502.org)

The thing to keep in mind is that it's not just CPU level stuff you're dealing with in the code, but also the other components that comprise the architecture of the machine like the PPU (Graphics chip), polling inputs from the controllers, etc.

Documents on the NES as a whole and not just its CPU will help to understand those kinds of things. This will also help with the CPU too since the NES doesn't use a vanilla 6502, but a modified variant of one (Decimal math being sacrificed for audio generation capabilities, etc).

jonk

  • Sr. Member
  • ****
  • Posts: 273
    • View Profile
Re: Hex to 6502 Assembly?
« Reply #3 on: May 12, 2016, 01:11:14 pm »
.... the NES doesn't use a vanilla 6502, but a modified variant of one (Decimal math being sacrificed for audio generation capabilities, etc).
I don't think any opcodes were added, or existing ones modified (except for the decimal status bit, which is available on the NES for any program use.) Is that also your understanding? I think they added a memory mapped space for the pAPU and used pins 1 and 2 for that. But it uses stock opcodes (less the decimal calcs/status bit.) If this doesn't agree with your understanding, I'd appreciate a further clue or two. Thanks!
An equal right to an opinion isn't a right to an equal opinion. -- 1995, me
Saying religion is the source of morality is like saying a squirrel is the source of acorns.  -- 2002, me

linkncb16

  • Restricted Access
  • Full Member
  • *
  • Posts: 159
  • They actually updated this site. Hallelujah.
    • View Profile
    • Patreon
Re: Hex to 6502 Assembly?
« Reply #4 on: May 12, 2016, 03:07:45 pm »
So is there any information that disassembling a ROM is going to give me? Like new insights as to which aspects of the game are stored where? Or will it just make editing and reading the code simpler?
Final Fantasy Redux is complete! Download

Sliver X

  • Hero Member
  • *****
  • Posts: 1036
  • EX•CINERIBVS•IGNIS•EXCITABITVR
    • View Profile
    • Panicus - A Fragment of Memory
Re: Hex to 6502 Assembly?
« Reply #5 on: May 12, 2016, 03:22:44 pm »
I don't think any opcodes were added, or existing ones modified (except for the decimal status bit, which is available on the NES for any program use.) Is that also your understanding? I think they added a memory mapped space for the pAPU and used pins 1 and 2 for that. But it uses stock opcodes (less the decimal calcs/status bit.) If this doesn't agree with your understanding, I'd appreciate a further clue or two. Thanks!

The Decimal Mode flag exists, yes, but there is no transistor logic in the CPU to actually manipulate values in BCD if it's set to 1. I suppose you could use that flag for things aside from setting Decimal Mode, though.

FAST6191

  • Hero Member
  • *****
  • Posts: 2538
    • View Profile
Re: Hex to 6502 Assembly?
« Reply #6 on: May 12, 2016, 04:05:14 pm »
So is there any information that disassembling a ROM is going to give me? Like new insights as to which aspects of the game are stored where? Or will it just make editing and reading the code simpler?

If you know how to read it, and maybe help yourself out a little bit with some information from an emulator then yes it will. Simply firing the ROM through a disassembler is not going to directly give you the sort of info you find on a datacrystal page ( http://datacrystal.romhacking.net/wiki/Mega_Man_IV:ROM_map ) or anything.
A somewhat skilled assembly programmer could probably plough through it and generate something like it, it would be a somewhat tedious way of setting about it but a wholly bad way. It is called static disassembly if you just take the ROM/exe and disassemble it. Sometimes that is all you have, for a game console like this though you have an emulator, a great one even in fceux, and not using that  option would be really silly. Sometimes we can suggest doing things manually once or twice just to get used to the underlying concepts but I don't think it would do much good here. If instead you, say, had a long train commute or something and you did not have a laptop, phone... then you could make useful progress in reasonable time on a printed version of the disassembly, especially if you had a little push from an emulator to tell you where to look first of all. Compare that to say staring a printout of the hex of a ROM which would probably gain you nothing and teach you even less.

This is all somewhat abstract though, and obvious once you know what you are doing, so I will change up to avoid confusing matters. I reckon what you want to do is try converting a cheat into an assembly hack or finding out something using assembly skills.
In the game you will have a lives counter. You might have a cheat to give you infinite lives. Almost by definition that cheat will contain the memory location, and if your game does not then pick another game as at least 95% of games on any system will be that simple.
If you are doing a proper assembly hack you would the set a breakpoint on write ( http://www.fceux.com/web/help/Debugger.html ) to that location, it would then tell you when something wrote there and instruction was doing it. Depending upon the system then that instruction might just be a write but the ones preceding it will be the things doing maths on it.
On systems without that capability (we saw it many times early on in the DS) or without a debugger you can instead take the static disassembly and search for anything that writes close to or around that location. From there you can try to go backwards and see what led to it.

Finding out something then. Move on from infinite lives and go instead to health in a RPG or something. Here you will hopefully have a DEF stat which changes damage done. Work backwards from a simple health loss and hopefully you will see where the DEF stat is found in memory, try to change that (change armour or something) and work backwards from there. Pretty soon you will find where the DEF change came from in the ROM and you hopefully not have the table of values that make up the item stats. That sort of thing does get into datacrystal and can be found using assembly hacking like this. It is not the only way but it is very good, very accurate and can be very fast where having to use simpler ways can take far longer and actually be far more fiddly

jonk

  • Sr. Member
  • ****
  • Posts: 273
    • View Profile
Re: Hex to 6502 Assembly?
« Reply #7 on: May 12, 2016, 05:00:51 pm »
The Decimal Mode flag exists, yes, but there is no transistor logic in the CPU to actually manipulate values in BCD if it's set to 1. I suppose you could use that flag for things aside from setting Decimal Mode, though.
Yeah. That's what I meant. They kept the RAM flip-flop, but no longer tied it into decimal ALU operations.

May 12, 2016, 06:02:32 pm - (Auto Merged - Double Posts are not allowed before 7 days.)
So is there any information that disassembling a ROM is going to give me? Like new insights as to which aspects of the game are stored where? Or will it just make editing and reading the code simpler?
Yes. In the case my son is working on, right now, the method they used in Dragon Quest III for a "level up" event was both "interesting" to learn about as well as "flawed" (buggy) in at least two ways: (1) On occasion, for certain classes and with certain personality enhancements, a level up effect on some status values would have the high order bit simply clipped off entirely, with unexpected results of course; and, (2) A part of the algorithm that determined if a status value change exceeded some limit would then decide that the only status change allowed can be 0 or 1, at random, with a similarly unexpected result. One of these was actually a bug -- losing a high order bit was clearly not intended. The other effect might be considered a bug, in the sense that it probably also wasn't intended despite the fact that they did intend on detecting an overage calculation and wanted to contain it. It's just that their choice meant that a character yielding +12 changes consistently as they level up, let's say, would suddenly show +0 or +1 when the limit kicked in. Probably a bug. But I can see some folks debating the issue just to be argumentative about it. The result of this is that my son is "fixing" these two behaviors as well as adding some hopefully thoughtful additions there and elsewhere. Without knowing what the game did, it's hard to make an argument for changing things let alone sitting down to fix/remedy them.

Not sure if that helps any. But knowing how to dig around in the ROM can even provide insights which may help you improve your existing strategies when you don't bother to modify the ROM. You can learn things no one else knows or has talked about on the web, leaving you the option of disclosing these new tidbits if you want (or not.) As well as what I mentioned earlier. You can also learn skills that may get you a decent job, too. Though I'm not suggesting that's a greased slide of any sort -- the learning is serious work.
« Last Edit: May 12, 2016, 06:02:32 pm by jonk »
An equal right to an opinion isn't a right to an equal opinion. -- 1995, me
Saying religion is the source of morality is like saying a squirrel is the source of acorns.  -- 2002, me