Padding the ROM
After you build your game the resulting file is probably just large enough to only hold the actual data in it, but in practice it'll be stored on a larger memory in a cartridge (e.g. a 4MB Flash chip). In order to get the correct size, you need to "pad" the file (i.e. add dummy bytes until it reaches the intended size).
Emulators normally will do the padding on their own, but even that isn't foolproof, as if you're really unlucky there's a chance that they mistake it for a legacy format (if it refuses to run the game for seemingly no reason and shows garbage as the game's title despite the ROM header being correct in the file, this is probably what's going on).
All that given, it's time to learn how you can pad your ROM:
With romfix
The romfix utility is part of the mdtools package. Simply running your ROM through it will pad it to the next size it considers safe:
romfix filename.bin
Beware that romfix also does other things (like fixing the ROM checksum), so bear that into mind if you don't want the ROM to be modified aside from the padding. On the other hand, if you're building homebrew you probably will benefit from its other features.
If you're curious, passing the -z option will make it show the ROM size before and after the padding:
romfix -z filename.bin
With the assembler
You can use the dcb
directive to do some simple padding
when using an assembler. The code below should pad the ROM to 4MB with
zeroes:
dcb.b $400000-*, 0
If you're using asmx (or derivative) the above may not work, because it has a tendency to not include padding at the end until something forces it to emit more code, so you'll probably need something like this instead (which pads to just two bytes away from 4MB, then manually adds those two bytes):
dcb.b $3FFFFE-*, 0
dc.w $0000
With cat
and dd
If you're on Linux or some other Unix-like system, you can use the command line utilities to create a padded file.
cat original.bin /dev/zero | dd bs=1024 count=4096 of=padded.bin