increment I in chip-8 opcode FX65
Asked Answered
B

2

6

While building a chip-8 emulator, I ran into the problem where the 2 main sources of chip-8 information seem to differ which has implications for the whole chip-8 interpreter.

On the one side we have wikipedia, which under the opcode FX65 tells us that

"Fills V0 to VX (including VX) with values from memory starting at address I. I is increased by 1 for each value written."

where "I is increased by 1 for each value written." is the important part.

Following this results in the following code:

for(int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i) {
    V[i] = memory[I];
    ++I;
}

on the other hand we have the chip-8 reference by cowgod, a reference almost every tutorial links to, which tells us the following

"The interpreter reads values from memory starting at location I into registers V0 through Vx."

Applying this logic results in the following code (this is also the implementation most chip-8 implementations use):

for(int i = 0; i <= ((opcode & 0x0F00) >> 8); ++i) {
    V[i] = memory[I + i];
}

The main difference between these two is that I is either incremented or not. Since I is the register index of the emulator, it is quite important for the program to work correctly.

What I want to know is which implementation of this opcode is correct.

Binion answered 4/7, 2018 at 18:17 Comment(0)
T
3

There doesn't seem to be a definitive answer, as there doesn't seem to be a definitive reference.

This reference seems to have the same problem with the same ambiguity

This (contemporary) reference (page 113), however, says "I = I + X + 1". Authorship is by the inventor, Joseph Weisbecker - I guess he will have known.

Twerp answered 4/7, 2018 at 18:44 Comment(0)
B
3

It depends what implementation you follow.

This question has already been answered, but I'll throw in a more historical answer for posterity.

CHIP-8 was introduced on the COSMAC VIP computer in 1977. This original CHIP-8 interpreter did increment I while executing the FX55 and FX65 instructions, as can be seen in this disassembly. I is incremented by 1 for each value written/read.

CHIP-8 was ported to several other microcomputers during the late 70s and early 80s, with this same behavior (so CHIP-8 games would be portable), but was then forgotten for a few years after more advanced computers with more memory became popular (such as the IBM PC).

In 1990, CHIP-8 was ported to the HP48 series of graphing calculators as CHIP-48. This port introduced at least three bugs: The BNNN jump instruction behaves differently (but it was rarely used), the two bit shift instructions 8XY6 and 8XYE now shift the register VX and ignore VY (on the COSMAC VIP, the value in VY was shifted and placed in VX), and more relevant to this question, FX55 and FX65 now increment I by 1 for each value written/read, minus 1. A later 1991 rewrite of and extension to CHIP-48, called SUPER-CHIP, changed FX55 and FX65 yet again. Now, those instructions did not increment I at all.

The two latter bugs meant that CHIP-48/SUPER-CHIP couldn't run many of the original CHIP-8 games from the 70s and 80s properly, but nobody seems to have noticed, probably because those old games weren't readily available on the Internet. They had been printed in computer newsletters and magazines. These modern interpreters became the basis of the renewed interest in CHIP-8, which spread on the Internet.

People still use Cowgod's reference, and make CHIP-8 "emulators" (interpreters) that rely on this changed behavior from the 1990s. Parallel to that, other people have emulated the old microcomputers from the 70s that actually run the old interpreters, and they have put up the old games on the Internet.

Some modern interpreters have configuration settings (often called "quirks") that let you choose how to execute these instructions, probably the main one (which is used to create new CHIP-8 games to this day) being Octo.

Baronetcy answered 27/2, 2020 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.