11

The CHIP-8 has, among its instructions, the following ones:

  • Ex9E: Skips the next instruction if the key stored in VX is pressed.
  • ExA1: Skips the next instruction if the key stored in VX isn't pressed.
  • Fx0A: A key press is awaited, and then stored in VX.

How did these interact with held-down keys? Does FX0A immediately return in that case, or would it wait for a new keypress? Does reading a key via EX9E/EXA1 somehow "clear" it, or would it take the "pressed" branch as long as the given key is held?

1 Answer 1

15

I haven't found an official reference for how instructions should behave, so I've taken the disassembly of the original CHIP-8 interpreter for the COSMAC VIP as the reference implementation.

Fortunately, this is rather well documented, and available at http://laurencescotford.co.uk/wp-content/uploads/2013/08/CHIP-8-Interpreter-Disassembly.pdf

For Fx0A instruction:

The decoding of Fxxx instructions begin at program address 0105. More precisely, instruction Fx0A is decoded and executed from address 010A. The execution of this instruction implies to call a ROM routine to read the keyboard:

010A F8 81   FX0A: LDI 0x81    Instruction FX0A -> wait for a key press and
                               store it in VX
                               0x81 is the high-order byte of the address
                               of a routine in the COSMAC VIP ROM that
                               reads the keyboard
010C BC            PHI C       Store this in RC.1
010D F8 95         LDI 0x95    0x95 is the low-order byte of the address of
                               the keyboard routine
010F AC            PLO C       Put this in RC.0 - RC now contains the full
                               address 0x8195
0110 22            DEC 2       Decrement stack pointer - the ROM routine
                               uses the stack so we need to ensure the
                               stack pointer is pointing at the next empty
                               location before calling it
0111 DC            SEP C       Call the routine to read the keyboard
                               On return the value of the key pressed will
                               be in the accumulator D
0112 12            INC 2       Increment stack pointer
0113 56            STR 6       Store the result in VX
0114 D4            SEP 4       Return to the fetch and decode routine

The behaviour of the keyboard ROM routine is documented by the same guy, Laurence Scotford. This is the flowchart taken from his web (the actual ROM code is also there) http://laurencescotford.co.uk/?p=347

Keyboard routine flowchart

As you can see, the keyboard ROM routine waits for any key to be pressed, and then, the routine waits until it has been released. Then it returns the key code that was pressed

So, according to this, if the Fx0A instruction is executed while a key has been already pressed, the instruction will wait until the key is released and then it will return the key code in register Vx.

For instructions Ex9E / ExA1:

Decoding and execution of these instructions begin at address 0199 of the interpreter code. A few instructions later, a direct keyboard reading is performed, isntead of calling a ROM routine.

019A 62        OUT 2            This will take the value in VX and output it
                                to the keyboard latch. This causes external
                                flag 3 to be set if that key is currently 
                                held down or reset if not

So these instructions will react accordingly if a key was pressed before the isntruction was executed. Both will read the current state of that key the moment the Exxx instructions are executed. These two instructions don't use the ROM, so they don't interfere with any system variable that would have been modified in response to keys pressed or released.

In other words: if key '1' is pressed and hold, and V1 stores the key code for '1'...

LD V1,1     6101   ;store key code 1 into V1

...the following sequence of instructions will behave as this:

SKP  V1     E19E   ;will skip the next instruction.
SKNP V1     E1A1   ;will not skip the next instruction.

LD V2,K     F20A   ;will wait until the user releases the '1' key, 
                   ;then it will store its keycode into 
                   ;register V2.
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .