chip8 emulator keyboard

Started by
3 comments, last by Giedrius 16 years, 1 month ago
I'm not sure how to go about emulating the keyboard. Are there certain addresses I need that represent each one the 16 keys? I'm confused.
Advertisement
Chip-8 has 16 keys (0-9, A-F), and three different instructions for dealing with the keys:
  • 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.
The first two instructions are the easiest to emulate.

As there are 16 keys, each of which can be in one of two states - pressed or released - a 16-element array of booleans is a good way to represent them. You could store a pressed key as a true and a released key as a false, and your emulator's front-end could write to this array based on buttons the user is pressing.

For the first two instructions, this becomes very easy to handle (pseudo code):

// Instruction EX9E:If Keys[VX] == True Then    SkipNextInstruction()End If// Instruction EXA1If Keys[VX] == False Then    SkipNextInstruction()End If


The variation, FX0A, is rather more complex. One way of doing it would be to set the fetch-execute loop into a "waiting for key" state that polls all of the key booleans in the array, and only leaves the "waiting for key" state when one key has been pressed and its value written to the requisite register.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Thanks for your reply. So I can store 1 to 16 for vx depending on which key is pressed? There isn't a specific byte that represents each key? And doing it this way won't interfere with the rest of the code?


Can I do soemthing like this?

where key equals 1 to 16 depending on the key pressed...

sub wait_for_key()while (key_pressed==0){(listen for key)}vx=key
Quote:Original post by ed209
Thanks for your reply. So I can store 1 to 16 for vx depending on which key is pressed? There isn't a specific byte that represents each key?


Huh? You don't store anything in VX for the EX9E or EXA1 instructions, VX contains the index of what key to check. You have the right idea for the FX0A instruction, but it should be between 0 and 15, not 1 and 16.

I'm not sure what you mean by "specific byte", the CHIP-8 system does not have scancodes, it is just 16 keys numbered 0-9 and A-F(10-15).
There's an instruction, which freezes the execution until a key is pressed, and there's another instruction, which simply queries the state of the button. You can check out my chip8 emulator source code. Here's one, which uses OpenGL and MFC and another one, which is crossplatform and uses WxWidgets and SDL.

This topic is closed to new replies.

Advertisement