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

Started by
8 comments, last by starfox5194 14 years, 8 months ago
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
Advertisement
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.
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 &#106avascript.<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - Antheus on August 18, 2009 10:06:37 AM]<!--EDIT--></span><!--/EDIT-->
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]

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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 &#106avascript.<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>I fail to see how the PC indicates the location in memory and kind of command at the same time.<br><br>
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
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.
thanks man i am going to try and program a game for chip 8 and look at some source codes too. thanks for the help
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!
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)

This topic is closed to new replies.

Advertisement