Sign in to follow this  
starfox5194

Emulators how do they work how much expirence do you need to make one

Recommended Posts

I have a lot of background knowledge in c++, open GL, C#, and a little of Java. I am Just now learning assembly. I have just gotten past the whole hexadecimal hurdle. How much assembly do you need to know to program an emulator. I was thinking of making an emulator of c++ and assembly. I want to make something fairly simple like the atari 2600. How much assembly do i need to know to program an emulator? can someone give me a few links to read up about it. Thnaks

Share this post


Link to post
Share on other sites
You need to be intimately familiar with the hardware you're trying to emulate, and that means more than just the instruction set of a CPU; you need to emulate all of the I/O devices (memory, PPU, SPU, other MMIO devices, GPIOs, etc) that the CPU interacts with and that's where most of the work is.

If I were you I'd start by writing some programs for an Atari 2600 so you at least have some idea of how it works and what you're trying to emulate.

Share this post


Link to post
Share on other sites
Emulator is this:
int main() {
byte mem[1024*1024];
byte stack[1024];
int reg; // register

int PC = 0; // program counter
in SP = 0; // stack pointer
while (true) {
switch (mem[PC]) {
case OP_PUSH : if (SP == 1024) return 3; // stack overflow
PC++;
stack[SP] = mem[PC+1];
SP++;
break;
case OP_POP : if (SP==0) return 2; // stack underflow
reg = stack[SP];
SP--; break;
case OP_JMP : PC = PC + mem[PC+1]; // near jump
break;
default : return 1;// do general fault, unknown instruction
}
PC++;
}
};
And this is literally it.

All complexity now comes from implementing each individual instruction.

Quote:
How much assembly do i need to know to program an emulator?


As you can see above - zero. You can use Java if you wish. Or javascript.

[Edited by - Antheus on August 18, 2009 10:06:37 AM]

Share this post


Link to post
Share on other sites
Antheus has the simplicity down, and I'd second the recommendation to learn how to write basic 2600 programs too - it'll give you a better understanding of the internals of the system, and let you write test programs to run on hardware and compare in your emulator. The real difficulty comes from the way the various parts of the system interact (especially in regards to timing).

That said, the 2600 is not an easy system to emulate. I strongly recommend you start with a CHIP-8 interpreter. It is very similar to what you'd need to do to write an emulator for a physical system, but with a very small instruction set and no timing difficulties. Some of the games are quite fun, too. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Emulator is this:
int main() {
byte mem[1024*1024];
byte stack[1024];
int reg; // register

int PC; // program counter
in SP; // stack pointer
while (true) {
switch (PC) {
case OP_PUSH : stack[SP] = mem[PC+1]; SP++; PC++; break;
case OP_POP : SP--; reg = stack[SP]; PC++; break;
case OP_JMP : PC = PC + mem[PC]; PC++; break; // near jump
default : return 1;// do general fault, unknown instruction
}
PC++;
}
};
And this is literally it.

All complexity now comes from implementing each individual instruction.

Quote:
How much assembly do i need to know to program an emulator?


As you can see above - zero. You can use Java if you wish. Or javascript.


I fail to see how the PC indicates the location in memory and kind of command at the same time.

Share this post


Link to post
Share on other sites
ok i read about the opcodes for chip 8 and how they change registers, but can someone tell me what to do with this information. like how do i make an emulator out of it? there is this whole part where i just blank out on. like ok yeah i understand what the opcodes do, but how do i emulate that?

I also fail to understand what Antheus's program does exactly.

Thanks for everyone's help so far i have learned a bit

Share this post


Link to post
Share on other sites
Quote:
I fail to see how the PC indicates the location in memory and kind of command at the same time.


Yea.. Fixed... Bugs and all...

Quote:
Original post by starfox5194

I also fail to understand what Antheus's program does exactly.


Program is loaded into main memory (mem in my example at offset 0). There is no distinction between data and instructions (commonly).

Then you just advance instruction counter, look at what is at current address, and execute that operation.

So each instruction you have is a separate case statement, and you handle it as needed. Modify flags, registers, read from memory, write to memory, manipulate stack.

Share this post


Link to post
Share on other sites
Quote:
Original post by starfox5194
How much assembly do i need to know to program an emulator?
Thnaks


If you don't already, you will definitely want to know enough to understand how to program in assembly, and why an assembly coded program is constructed the way it is. How and when registers are used, how to access memory, the various ways the stack is used, jumping, calling, etc. You know... all the basic stuff. Then you'll understand all the previous replies and how what you read on CHIP-8 would be used to make an emulator, and better yet why.

Only then would you want to move on to understanding other "assembly level" stuff like hardware interrupts, timing issues, etc., which will be different on each type of CPU, and then perhaps move onto something a little higher up... something that uses a Z80 perhaps?

So yeah IMHO learning to make a couple basic programs in some arbitrary assembly language would be a really good start.

Good luck! And have fun!

Share this post


Link to post
Share on other sites
I dont know if anyone else is following this that wants to make an emulator but i found some very useful information.

http://fms.komkon.org/EMUL8/HOWTO.html

it takes code similar to Antheus's and explains it in depth. It also tells you a lot more (still reading it now =D)

Share this post


Link to post
Share on other sites

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