GP2X Emulator Devlopment?


Girvo

Still Fresh
Joined
Nov 9, 2006
Messages
11
Where to get started? I lvoe programming, and can program games in C/C++. However, I would LOVE to get started writing emulators.

But how? I get how emulators work, I am just curious as to how to implement them. I have decided to start on a CHIP-8 emulator for the PC, and then hopefully attempt to port it to GP2X. So, any ideas or resources for me to get started? I have all the technical details and Opcodes for CHIP-8 :)

Thanks!

-Girvo
 
If you want to write an emulator from scratch you are going to need a lot of documentation. There is some good chip8 documentation out there. The first emulator I wrote was a chip8 emulator (DrChip8), it was a great system to learn the basics of emulation on.

http://www.pdc.kth.se/~lfo/chip8/CHIP8.htm

The website above has a list of the opcodes (cpu instructions) that the chip8 used. I'm pretty sure this is the only website I used when creating DrChip8. Have a good read and you'll have no problems.

The basic process of an emulator is to read an instruction from a rom image, and then interpret what that instruction would do. If you look at the list of opcodes for the chip8 you'll see that they appear to be 16bit ( ie 2 bytes per instruction ). So in my emulator I decided to read the instructions byte by byte and then join the two bytes up to make a 16bit value. The reason for this is because I could not confirm if the instructions were always 16bit aligned and reading a 16bit value from a non aligned address breaks most ARM systems.

Below is a very basic implementation I knocked while writing this post. It gives you a basic idea of what is required. I've implemented a couple of easy instructions as examples. Some instructions draw graphics, so you would have to implement functions to do this. Other instructions call subroutines, this will require you to implement a simple stack, so that you can store the previous pc in order to be able to return the main chip8 subroutine.

Code:
unsigned char ROM[0x8000];  //memory where you would load chip8 rom image

// you need a variable to hold your current position in the rom image
// The is commonly called the Program Counter or PC
// set it to start at address 0x200 in the rom image because this is where Chip8 programs start.
unsigned short pc=0x200; 
unsigned short opcode=0;
unsigned char vx[0xF]=0;  // chip8 X registers
unsigned char vy[0xF]=0;  // chip8 Y registers
while(1)
{
	//read the next two bytes from the rom image and OR them together to create a 16bit value
	opcode=ROM[pc]|(ROM[pc+1]<<8);
	pc+=2; //increment pc by 2 because this is the amount of byte we read above
	
	//Now you need to work out what this opcode actually does
	//because there are 16 types of instruction, I do a switch on the top nibble of the opcode (0xF000)
	switch(opcode&0xF000)
	{
	   //  1NNN Jump to NNN
	   case 0x1000:
			// jump instruction.  Just set the pc to the value in NNN
			pc = opcode&0xFFF;
			break;

	   //  6XKK VX = KK
	   case 0x6000:
		   // This opcode puts the value in KK into the x register number X
		   // do this like so
		   vx[(opcode&0xF00)>>8] = opcode&0xFF;
		   break;

		//7XKK VX = VX + KK
		case 0x7000:
		   // This opcode adds the value in KK to the value in VX and stores the total in VX
		   vx[(opcode&0xF00)>>8] += opcode&0xFF;
		   break;

	 }


}
 
Blah posted on Mar 7 2007 at 04:12 AM said:
Port an existing emulator first, just to make sure you're not 20,000 miles over your head.
It is much easier to write your own the first time since you understand exactly what is going on and why it's been done. CHIP-8 is really very simple to 'emulate', though when I did it, I had an array of pointers to the functions which executed the opcodes so I could just call the function directly instead of using a switch as in Reesy's example.
 
Last edited by a moderator:
Wow... that has made thing really clear to me. I have those pages bookmarked... I hope I have less uni work soon then I can try making an emulator. :D

I'd probably do Chip8 to learn then go onto gb/gbc... since I think that emulator could use improving... anyway thanks for the info :)
 
Yeah, I'm gonna try making a chip8 emulator too, then i've ideas for porting it to an avr microcontroller and outputting to an lcd display :)
 
KungPhoo posted on Mar 23 2007 at 01:49 PM said:
Yay! Chip8 is nice. Is there any C compiler that produces Chip8 output?

I doubt it. As you may already know Chip8 was never an actual physical CPU but can be thought of as a language in and of itself (patterned after RISC machine language). Having said that it was basically written in such a way that it'd be feasible to write small games in it directly (in its "assembly" language). It was also developed before C became the mainstream entity it did. The environment just isn't that great for a compiler target, although of course it could be done.
 
Last edited by a moderator:
Back
Top