Sign in to follow this  

chip8 emulator keyboard

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 EXA1
If 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 3571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this