2

I have been writing a chip8 Emulator -- http://en.wikipedia.org/wiki/CHIP-8

I have tested all of the opcodes as well as the graphics calculations and I am now struggling with the user input. I have the following method monitoring user input and altering the registers as needed (When using the chip8 the user input alters the corresponding memory register - E.G. hitting '0' sets the V0 register to 0.

My problem is that I have the following code fetching and calculating each opcode and its operation contained in a while loop. And while this is running my application cannot detect user input. So the ROMS start and just stay locked in place waiting on a register change or user input. It keeps getting stuck in an infinite loop, I tried to implement a global Boolean RUN, and it is not detected after the while loop is initiated. I'm assuming it is out of the scope of the loop, but from what I have read, the keyboard event triggers an interrupt that should be visible from almost anywhere, any Thoughts?

This is what calculates and parses the opcode

 private void button1_Click(object sender, EventArgs e)
    {
        // This will become the run function 
         do{
        for (int i = 0; i < 2; i++)
        {
            opc[i] = mem[mc]; // fetching the instruction from the memory array
            mc++;
        }
        cibox.Clear(); // Just clearing Debugging text boxes in the UI
        pcbox.Clear();
        pc++;
        pcbox.Text += pc;
        cibox.Text += opc[0].ToString("X2") + "-" + opc[1].ToString("X2");
        calculations(opc); // Parses the Opcode and does the corresponding operation
         }while(run);
    }

And this method is controlling the user input...

  protected override void OnKeyDown(KeyEventArgs keyEvent) // Listens for Keyboard events! Read More: http://www.geekpedia.com/tutorial53_Getting-input-from-keyboard.html
    {
        keyPress = true;
        //Gets the key code found at keyEvent...
        MessageBox.Show("KeyCode: " + keyEvent.KeyCode.ToString());
        String register = keyEvent.KeyCode.ToString();
        if (register == "Escape")
        {

            Application.Exit();
            run = false;
        }
        try
        {
            registerVal = int.Parse(register, System.Globalization.NumberStyles.HexNumber); //  Second Nibble! --> Int Format 
        }
        catch (System.ArgumentNullException e)
        {
            return;
        }
        catch (System.ArgumentException)
        {
            return;
        }
        catch (System.FormatException)
        {
            return;
        }
        catch (System.OverflowException)
        {
            return;
        }
        if (registerVal >= 208)
        {
            registerVal = registerVal - 208;
        }
        if (registerVal <= 15)
        {
            mem[registerVal] = (byte)registerVal;
        }
        display(); // Alters UI to display state of registers, etc
    }

So I have now tried the Game Loop Idea, but I cannot find a way to make a method in C# that will return a key press. Maybe I am missing something here, but I cannot seem to figure it out!

I also tried another method involving running the CPU calculations in a separate thread, and this is causing a slight delay issue.

I would really like to see an example of a method in C# that returns the value of a key being pressed that I can call within the While loop!

Bubo
  • 768
  • 4
  • 18
  • 34

1 Answers1

4

I suggest you set up your emulator's loop like a game loop. You have one global loop which triggers updates to each module in your emulator. In the loop, you call your input (or generic "event") processor, then your opcode/hardware emulation, then your screen update.

Here's some pseudo-code.

while (True):
    processInput()
    updateCPU()
    outputGraphics()

I don't know how the key input works in C#, if it's an asynchronous event trigger (i.e. the "OnKeyDown" method gets called outside of your loop), you can set up "OnKeyDown" to just send the key events to an intermediate event manager, then in "processInput()" you actually resolve the events and use them to update registers on the emulated CPU etc.

Laurence Dougal Myers
  • 1,004
  • 1
  • 8
  • 17
  • Thank you! This looks very promising, I just need to work out the details of handling the keyboard event and how to process it. This looks like a very good plan! I will post later on with updates – Bubo Nov 14 '12 at 21:46