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

Author Topic: Assembling and inserting code into Genesis ROMs  (Read 5898 times)

nightmareci

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
Assembling and inserting code into Genesis ROMs
« on: October 20, 2012, 04:06:44 pm »
I don't really know how I'd go about assembling some 68000 assembly code and inserting it into a Genesis ROM, as opposed to expanding the ROM and putting my code there. Any suggestions of tools I should use, and detailed explanations of how to use said tools to accomplish my goal, would be greatly appreciated. I'm working on a VWF for Magical Taruruuto-kun, and I've already worked out how the VWF should function algorithmically with C/SDL (my C/SDL code actually uses 4bpp linear-encoded graphics, like the Genesis), now it's just a matter of converting it to assembly and inserting it into the ROM.

Malias

  • Sr. Member
  • ****
  • Posts: 292
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #1 on: October 21, 2012, 09:49:42 pm »
Luckily for you, the 68k is supported by gcc.  However, in order to compile for the Genesis, you'll need additional tools in your toolchain.  Here is a guide for setting them up in Linux.  If you are having trouble setting it up or are running Windows, try this thread.  Actually, Spritesmind is a good place in general for any Genesis development information.

Keep in mind that it wont be as simple as compiling your code and inserting it into the game.  It will take some work to integrate your code into the existing game.
The great achievement is to lose one's reason for no reason, and to let my lady know that if I can do this without cause, what should I do if there were cause?
     ~Don Quixote~

nightmareci

  • Jr. Member
  • **
  • Posts: 6
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #2 on: October 22, 2012, 01:44:24 am »
Maybe I should have been clearer: I already do know 68000 assembly to some degree, and details of Genesis programming, and have two working assembler programs (asmx and GNU as), and I know how to use them to assemble a *.asm file into a binary, but I don't know how I'd go about inserting that generated binary into a ROM and having the code actually work, what with jsr's and such. I think I do have a clue as to how it might be possible: I can directly use constant values as jsr targets, such as "jsr $4000" (or just equate a name with the constant) in code that doesn't have the corresponding label available, and both asmx and GNU as appear to still assemble the label you wanted into the binary, so that might be just what I need. And I assume any bsr instructions referring to code I'm actually assembling would work. But that doesn't help with any bsr instructions calling functions already in the ROM; I don't think using a constant value as the label would work in that case, because bsr is relative to the current instructions' location.

BRPXQZME

  • Hero Member
  • *****
  • Posts: 4572
  • じー
    • View Profile
    • The BRPXQZME Network
Re: Assembling and inserting code into Genesis ROMs
« Reply #3 on: October 22, 2012, 04:20:00 am »
Using a constant value as the label should work because it’s the assembler’s job to determine the difference (assuming you use the org directive to place your code in the spot where it should end up). At least in the assemblers I recall using, you can also use the org directive to place labels at the correct spots (no data or code necessary).

The main thing to watch out for, once you’ve done this, is that you need to somehow get the raw bytes from your code (and only the ones you want written) into the ROM. GCC isn’t really your friend in that regard (but you can still make it work if you really want to). Once you actually have the output, though, a program that patches it in at the right spot should be trivial to write.

(N.B.: I personally avoid the hacking end of things, so while I might have the theory down, someone else might have something more practical to say)
we are in a horrible and deadly danger

Ti_

  • Jr. Member
  • **
  • Posts: 39
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #4 on: October 23, 2012, 11:23:55 am »
create hack.txt

Quote
   org   0
   incbin   "Original ROM.bin"

   org   $16246         ; adress where you want rewrite original code
   nop

   org   $8DA0         ; adress where you want make jump to your subroutine
   jsr   your_code


     org   $200000     ; free space in rom or new space where you put new code
your_code:
     move.l  d0,d1       ; you code
     rts

create build.bat file with:

Asm68k.exe /p  hack.txt,hacked_rom.bin


Karatorian

  • Sr. Member
  • ****
  • Posts: 381
  • "Gotta get get ... 6502"
    • View Profile
    • Studio Karatorian
Re: Assembling and inserting code into Genesis ROMs
« Reply #5 on: October 23, 2012, 02:14:25 pm »
Different assemblers do thing differently, but this shouldn't be too hard to pull off. As BRPXQZME said, using constant labels with BSR ought to work, as long as the assembler knows where your code is to be located (via .ORG or another method), and the distance isn't outside the range of a relative branch (I don't know 68k, but on some architectures, there is a relatively small range). So, once you know where in the ROM you intend to insert your code, it should be possible to assemble it cleanly without putting raw memory values in the main code.

Getting some assemblers (particularly GAS) to output raw binaries can be tricky sometimes, but I'll assume you've got that part figured out. If not, I can give you an objconv command that'll work with GAS's output. So, once you've got your binary and know where you want to insert it, all you have to do is get the bits in there somehow. I assume this is the part you're having trouble with. (If not, please say what the issue is and I'll try to help.)

One simple way is to use a hex editor. You'll need one with copy and paste. Open your binary blob and copy the contents. Open the ROM and seek to the location it should be inserted at and paste it in. Be careful though. Hex editors generally operate in two modes. In one mode, pasted bytes overwrite the existing content, keeping the ROM size the same. This is the mode you want. In the other mode, pasted bytes expand the file. How to set this depends on the editor, so look it up if you can't figure it out. (In some editors, they're two different sorts of paste operations instead.)

If you prefer something more automated, which will speed up the build-test-debug cycle, there are other options. There are a variety of programs that can do code insertion. I use dd, but that's a unix utility (and frankly a little weird). You can get a Win32 port (I assume you're on windows, if not, you should have dd as un*x and OS X have it), but that might be a pain. If you've got the skills and tools for even the most basic of PC programming, you can write your own, it's pretty simple. Here's some psudo-code as I don't know what language(s) you might use:

Code: [Select]
function patch(rom_name, bin_name, out_name, pos)
rom = file(rom_name, READ)
bin = file(bin_name, READ)
out = file(out_name, WRITE)

data = rom.read(pos)
out.write(data)
data = bin.read()
out.write(data)
rom.seek(pos + bin.size)
data = rom.read()
out.write(data)

Or you can use the trick Ti suggested and have the assembler and incbin to do it for you. His method will work if you're putting your code somewhere other than where the code you're replacing was. If you're dropping it inline where old code you're replacing was, you can do something like this (in GAS):

Code: [Select]
.org 0
.incbin ROM 0, INSERT_POS
.incbin HACK
.incbin ROM INSERT_POS + HACK_SIZE

Finally, you could write a custom link script and have LD (from GNU binutils, you should have it if you have GAS), put all the bits where they go. That's kinda' complicated though, but if you want to go that route, I'll try to help you out.
Current ProjectsFinal Fantasy EngineSMB Special for NESStudio Karatorian
@loop: lda (src),y — sta (dst),y — iny — bne @loop — inc src+1 — inc dst+1 — dex — bne @loop

Jigglysaint

  • Sr. Member
  • ****
  • Posts: 316
  • Corruptomancer
    • View Profile
    • Stuff Jigglysaint has done(like discover the Crocomire in MZM)
Re: Assembling and inserting code into Genesis ROMs
« Reply #6 on: October 24, 2012, 08:21:49 pm »
Ever consider just writing your code directly to the rom via a hex editor?  Works for me.

henke37

  • Hero Member
  • *****
  • Posts: 643
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #7 on: October 24, 2012, 10:44:17 pm »
That's a bad idea for anything involving more than a few lines of code.

KingMike

  • Forum Moderator
  • Hero Member
  • *****
  • Posts: 6906
  • *sigh* A changed avatar. Big deal.
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #8 on: October 24, 2012, 11:18:47 pm »
That might be possible for small pieces of code in Z80 or 6502 (which use opcodes that align to bytes) but 68000 (as well as x86 and I'd guess other CPU types) don't. I can't imagine converting those bit patterns to bytes to be fun.
"My watch says 30 chickens" Google, 2018

Disnesquick

  • Jr. Member
  • **
  • Posts: 38
    • View Profile
Re: Assembling and inserting code into Genesis ROMs
« Reply #9 on: October 25, 2012, 06:24:39 pm »
Ever consider just writing your code directly to the rom via a hex editor?  Works for me.

Never, ever do this.

You will end up screwing your future self HARD.

Jigglysaint

  • Sr. Member
  • ****
  • Posts: 316
  • Corruptomancer
    • View Profile
    • Stuff Jigglysaint has done(like discover the Crocomire in MZM)
Re: Assembling and inserting code into Genesis ROMs
« Reply #10 on: October 26, 2012, 11:08:32 pm »
That might be possible for small pieces of code in Z80 or 6502 (which use opcodes that align to bytes) but 68000 (as well as x86 and I'd guess other CPU types) don't. I can't imagine converting those bit patterns to bytes to be fun.

I could have sworn that the 68000 was readable in hex.  Now granted, all I've ever done was write a bunch of small pieces of code at a time(you should see how messy things get).  I guess I've just got a strange way of thinking that seems to work better without a compiler.

Gideon Zhi

  • IRC Staff
  • Hero Member
  • *****
  • Posts: 3505
    • View Profile
    • Aeon Genesis
Re: Assembling and inserting code into Genesis ROMs
« Reply #11 on: October 27, 2012, 12:05:28 am »
Never, ever do this.

I do this occasionally!
... very occasionally.
Generally only when I'm NOPing something or changing a branch from one type to another.

DarknessSavior

  • Hero Member
  • *****
  • Posts: 5031
  • Darkness.
    • View Profile
    • DS: No, not the Nintendo one.
Re: Assembling and inserting code into Genesis ROMs
« Reply #12 on: October 27, 2012, 01:10:12 am »
I do this occasionally!
... very occasionally.
Generally only when I'm NOPing something or changing a branch from one type to another.
I do this occasionally as well. Usually when I'm using Geiger's SNES9x and I want to test an idea without having to write it into an .asm file. Usually simple variable changes and whatnot.

~DS
Red Comet: :'( Poor DS. Nobody loves him like RC does. :'(
Sliver-X: LET ME INFRINGE UPON IT WITH MY MOUTH
DSRH - Currently working on: Demon's Blazon, Romancing SaGa, FFIV EasyType.
http://www.youtube.com/user/DarknessSavior