3 ms·
I had the great opportunity in my undergrad college compiler class to write an entire ecosystem to understand better how some of this stuff works. We didn't jus
by bane 16y ago
I had the great opportunity in my undergrad college compiler class to write an entire ecosystem to understand better how some of this stuff works. We didn't just write a compiler, we wrote an emulator for a simple, notional architecture. The whole thing was simple enough that we could write the compiler toolkit, the system emulator/VM and run it in a few days.
Then for extra credit we could rewrite the whole thing (or part of it) in another language. And/or get a guaranteed A if we could make the whole shebang self-hosting.
It was a really great way of understanding the basics of a system architecture.
As for the emulator, it was pretty primitive, but basically it loaded the compiled program code into memory, then would read an operation's worth of bytes, look the operation up in some kind of lookup table (or similar) that mapped the emulated system's instructions to local system instructions say, x86, and execute that operation.
Sometimes the mapping resulted in several operations on the host side the emulated system might execute in one.
eg.
ADD R1, R2, R3
might add the contents of registers r2 and r3, then put the result into r1. In x86 it would be more like
ADD AX, BX
MOV CX, AX
or some such.
(actually we did it a little higher level then that, but that's the idea).
So an emulator needs to provide all of the various registers and operations and such so that the code can execute, it likely has to have the ability emulate the memory architecture and translate I/O appropriately. Some older systems for example could only output to a printer terminal, or to a simple status display, on a modern system you have to translate calls to those output devices to equivalent ones on your system (like the screen).
More complex system may need to emulate several processors. The SNES and Amiga for example have a handful of chips that not only need to be emulated, but much of the software assumes a particular timing in the interaction of those components that can be very challenging to get right and keeping track of all that and running at reasonable performance can require fast hardware.
These days though, not many emulators use the simple execution mechanism I outlined above. Many of them have sophisticated code profiling and execution caches that can significantly speed up the emulation.
In these terms, emulating a system, or a VM (like Java) or some other runtime environment (like Javascript and the V8 runtime for example) isn't really all that different and there's huge crossover in the theoretical concepts between these areas.
- silentbicycle 16y ago_The Elements of Computing Systems_ (http://www1.idc.ac.il/tecs/ http://www1.idc.ac.il/tecs/) follows a similar approach, building an emulated computer up from NAND gates. Highly recommended!
- bane 16y agoGreat book! I really love this approach to teaching this kind of subject. Seeing the whole stack, top to bottom, really helps tie together so many different threads that are generally left untied during a CS education.
- icefox 16y agoThere is a google tech talk on the book which is really good for those curious to find out more.