Thursday, February 21, 2008

Capturing key inputs

Following on what I said yesterday, no emulator would be good enough without the ability to capture key inputs. This is pretty easy and can be quite useful in alot of situations. An exemple of this can be found in the form tutorial_Tetris. (Yes, there is a Tetris game in DAX for those who didn't know!)

The first thing you need to do is add a Tree control to your design and set it's "AutoDeclaration" to Yes. The Tree control allows us to capture the "keyDown" events by overwriting the method with the same name. We can then use the vKey that is passed to this method and do whatever we want when a key is pressed.

boolean keyDown(int vKey, boolean ctrl, boolean shift)
{
        boolean ret;

        ret = super(vKey, ctrl, shift);

        switch (vkey)
        {
                case 49: //1 (1)
                        keyPressed = 2;
                        break;
                case 81: //4 (Q)
                        keyPressed = 5;
                        break;
                case 50: //5 (W)
                        keyPressed = 6;
                        break;
        }
        return ret;
}

So for our emulator, we want to have the emulator focused on the Tree whenever it's not processing information. So after we execute an instruction, we give back the focus to the Tree like so :

void runEmu()
{
        int opcode;
        ;

        opcode = memory[pointer]
        this.execute(opcode);
        pointer++;

        if (emuRunning)
                this.setTimeOut(identifierStr(runEmu),0,false);
        Tree.setFocus();
}

This is just a rough example, but we could call methods from here to execute a certain peice of code or change the emu status (like to pause or play the emulator).

I almost posted today about the importance of making backups of your code but was too annoyed with the fact that I lost a day and a half worth of work :p.... So remember guys, ALWAYS backup your projects, even if automatic backups are suppose to be running every night.

Tuesday, February 19, 2008

Chip8 in DAX 4.0 (continued...)

I know I haven't posted in a while... but I'm bored and this seems like a great time to continue my little adventure with emulation in Microsoft's Dynamics AX 4.0. I will probably not go as in-dept as I originally planned since the Chip8 platform is well-documented already. I'll try to focus on the challenges that the DAX platform brings into emulating a system.

Every emulator consists of a main loop that executes instructions, also known as opcodes. A typical loop in pseudocode would look a little something like this:

while emulor is running
        fetch opcode from memory at pointer's location
        execute opcode
        increase pointer
loop

In DAX this can be something as simple as (The boolean in the while statement can be changed to a variable to allow to start/stop the emulator):

while (true)
{
        opcode = this.memory[pointer];
        this.execute(opcode);
        opcode++;
}

Unfortunatly, if you try to run a loop like this, you will notice that the DAX client seems to hang as it's running an infinite loop. This prevents anything to be updated to the screen (like the Chip8 display area) and prevents any keyboard input to be captured (capturing input will be explained in another blog entry.... which I promise will not be in another month this time!). These issues can be bypassed by doing something like this :

The memory[] array, pointer and emuRunning variable would be declared in the classDeclaration in this exemple.

void runEmu()
{
        int opcode;
        ;

        opcode = memory[pointer]
        this.execute(opcode);
        pointer++;

        if (emuRunning)
                this.setTimeOut(identifierStr(runEmu),0,false);
}

Instead of using a loop, I made use of the setTimeOut method to call the same method over and over again as long as the emulator is running. This leaves enough time for the client to update itself and allow the user to move around any window that would be opened in the client.

That's it for tonight. On a last note, using Threads should work also. I haven't had any free time lately to give it a try but it's kind of like the "Find" feature in DAX. When you search for a string through code, the client will update its result window while it is searching more results.

Monday, January 7, 2008

Chip8 emulation in Dynamics AX 4.0

First thing first, this is a project I made a few months back. There is still some work that needs to be done on it, but I doubt I will be touching it again anytime soon.

Now most of you are probably wondering what exactly is Chip8... Well think Pong, Space Invaders, Brix, etc. Get the picture?

Now some of you are probably wondering what emulation is... Emulation is replicating the functions of one system through the use of another. In this case, I'm emulating Chip8 through Windows (using Dynamics AX as my coding language and interface... granted it may not be the best of choices but I wanted to see if it could be done and what performance I would be getting).

I've been involved in the emulation scene for several years now. I'm administrator on one, if not, the biggest emulation forum and have betatested several GameCube emulation projects. While I can understand the code I read and the ideas behind emulation, I have never coded an emulator myself. I guess it was time to start somewhere.... and the best place to start is to start small.... Chip8 small that is ;)

The core component in any platform is the CPU. Everything else is one way or another connected to it, either by helping the processor or by being driven by it. The Chip8 CPU only has 35 instructions making it an easy processor to emulate since it doesn't take much time to go through all of them. The isntructions (also called opcodes) are used to manipulate the data in between the registers and the memory. The Chip8 has 16 8bit registers and 4KB memory. Shouldn't be too hard for modern computer to replicate this :p

Alright, that's it for today. Next post will contain actual code for you to see and I'll explain some of the issues we have when emulating anything in DAX.

Until then, here's a little screenshot of my emulator in action :