Regarding Just In Time Compilation for emulation of a full computer architecture: how does the code, once recompiled, run on the host PC, while still emulating the architecture being emulated?
For example: If I have some 6502 code, and then I translate it to x86 and run it on the host, how does that emulate the architecture being emulated? Isn't it being run on the host?
Can someone please point out the oh-so-obvious-point that I'm somehow missing?
keep in mind that the whole you translate the code from a platform to x86. You in fact only translate the behaviour. The architecture you simulate by handling the peripherals via memory mapped io etc. Thus every memory read or write you check whether if it is only memory or if it is hardware and take the appropriate action.
os64dev wrote:keep in mind that the whole you translate the code from a platform to x86. You in fact only translate the behaviour. The architecture you simulate by handling the peripherals via memory mapped io etc. Thus every memory read or write you check whether if it is only memory or if it is hardware and take the appropriate action.
IIRC for 6502 there is no MMU (no segmentation and no paging) so it'd be faster to add the checks into the final code. For example, something like:
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
So the translated assembly instructions don't really run on the machine. ie. they run on the processor, but affect the emulator's data. (stack, registers, etc.) This is what I had been getting confused. I thought they acted upon the actual machine's registers and such.
An implementation question, however. Let's say I have a function, t. t takes raw opcodes from the emulated data and translates them to assembly for the host PC. How to go about assembling them? An external assembler? Or just program the x86 codes right into the program? (I'm thinking more towards the latter.)
A more minor question, however. How to execute assembled code from C? A function pointer to the code that is then called?
The entry used an array, but couldn't one use a function pointer that is directed at an array?
Implicitly, the symbol "main" is a function pointer to your entry point. I believe your compiler will complain though (nowadays compilers don't let you make main an array of shorts).
i've been programming emulators for some while now even with dynamic recompilation. The basic concept is like so: you have an byte array of N size (lets say 4 KiB) and an current translation instruction pointer.
now you have to write a parser to parse the original machine code and translate them into host machine opcodes.. as an example i just put some nop instructions and a return statement.