2

I'm writing a chip-8 interpreter. I have my mock hardware, instruction execution, UI, etc.. all good to go, so I started actually loading and executing real chip8 programs.

In the zip archive I found on the web there are several games in there that seem to have illegal jump calls.

From CowGod's specification it says:

All instructions are 2 bytes long and are stored most-significant-byte first. In memory, the first byte of each instruction should be located at an even addresses. If a program includes sprite data, it should be padded so any instructions following it will be properly situated in RAM.

This seems very insistent that it's impossible to have an instruction that executes from an odd memory address, and therefore I coded my interpreter to fail is we end up trying to go to an odd memory address (mostly because I assume that I"m reading something wrong if this occurred).

However two of the games in my pack (INVADERS, and BLITZ) seem to immediately start with a jump call (they signed the roms with their names in ascii so the first instruction is a call to jump after the signature.

The former starts with 0x12 0x25 and the second starts with 0x12 0x17. Both of these point to jump calls to odd addresses (0x225 and 0x217 respectively) which then fail in my emulator. Looking at those addresses they are pointing to they look like they contain valid instructions (byte 25 in the former contains 0x6000 (LD V0, 00) and byte 17 in the latter points to 0xA341 (LD I, 341).

Am I just interpreting the specifications too strictly and it's expected that real applications won't be padded and can be run on odd addresses?

KallDrexx
  • 27,229
  • 33
  • 143
  • 254
  • If the programs start at an odd address, is every subsequent instruction also at an odd address? – Willis Blackburn Jan 30 '19 at 03:44
  • For the most part it's every odd bit assuming but I haven't looked at it too in depth to separate out instructions vs sprite data to be sure – KallDrexx Jan 30 '19 at 12:52
  • I looked at the specification, and to me the sentence you cite feels more like a recommendation than a requirement. It uses the word “should” instead of “must.” There is nothing about the virtual machine aborting on an unaligned instruction read. The spec says the 1nnn jumps to nnn without qualifying nnn in any way. – Willis Blackburn Jan 30 '19 at 19:41
  • It also seems unlikely to me that an 8-bit architecture with only 4K of memory would care much about instruction alignment. Finally if the architecture was serious about alignment, some of the instructions could be optimized. For example the range of the Bnnn instruction could be doubled by shifting the V0 value left before jumping. But there are apparently no such optimizations. – Willis Blackburn Jan 30 '19 at 19:48
  • Even ignoring all this, you have existing binaries with odd instruction alignment that presumably work on other implementations of this architecture. The only reasonable conclusion is that instructions at odd addresses are okay. – Willis Blackburn Jan 30 '19 at 19:50
  • Makes sense, I really was mostly making sure I was not reading the values wrong and there wasn't some other difference I was missing that allowed these roms to work. – KallDrexx Jan 30 '19 at 21:44
  • I came across another specification that says the original Chip8 required even numbered op-codes but emulators are inconsistent at enforcing it http://chip8.sourceforge.net/chip8-1.1.pdf – KallDrexx Feb 09 '19 at 15:38
  • Possible duplicate of [CHIP-8 game has an odd number of bytes](https://stackoverflow.com/questions/15787729/chip-8-game-has-an-odd-number-of-bytes) – zubergu Jul 26 '19 at 10:52
  • 1
    Cowgod's specification is not very accurate. For one thing, it perpetuates the SCHIP implementation of several opcodes, which differ from the original COSMAC VIP specs. This strange limitation of restricting instructions to start at even bytes seems to be another. There has been no such limitation in any interpreter I've come across. – tobiasvl Nov 04 '19 at 22:34

1 Answers1

2

The answer to the question is (based on the comments): the Chip-8 specification apparently intended to include the requirement that instructions start on even addresses, however, due to its not being specified very strongly (the spec mentions it only in passing using the term "should" instead of "must"), and the fact that the instruction set works fine with instructions that aren't at even addresses (e.g., the jump instruction can jump to any address, not just even ones), emulator developers tended to ignore the requirement. Consequently, a number of binaries exist for Chip-8 that have instructions at odd addresses, making it unlikely that future emulators will enforce this requirement.

Willis Blackburn
  • 8,068
  • 19
  • 36