How does Linux emulator in Javascript by Fabrice Bellard work?

JavascriptLinuxEmulation

Javascript Problem Overview


Today I had a jaw dropping moment, when I saw Linux emulator in Javascript: http://bellard.org/jslinux/

It compiles C programs, it has vi and emacs, it supports all shell commands, etc etc.

How does it work?

Javascript Solutions


Solution 1 - Javascript

At first, I also thought this is just a terminal emulator connecting you to a VM but it isn't. If you watch the network connections, you can see that, after bootup, no data is transmitted anymore.

So it's real.

A CPU is not something magic; in fact all it does is read bytes from memory and modify the RAM in accordance to what the commands mean.

In this case, the CPU emulator is based on the qemu code. What he does is he creates an array of functions where the index is the next byte at the PC (program counter).

Now all you need is a simple linux distribution that doesn't need any exotic CPU commands like floating point arithmetic or MMX code and voila.

What's interesting is the speed of the beast. The whole thing is a bit sluggish but then, it's JavaScript in a browser.

Conclusion: Impressive. Can't wait to see a C64 emulator :-)

Solution 2 - Javascript

See http://www.quora.com/CPU-Emulation/How-does-bellard-org-jslinux-work > ###Simplified Explanation > jslinux is essentially a complete computer implemented in software, specifically JavaScript. This is of course known as an emulator. This particular version is setup to run Linux, but in theory it could run other operating systems instead. > The emulator (JavaScript, ~90KB minified, ~7000 lines formatted) is loaded into the browser. A version of Linux was previously compiled into machine code for an x86 processor is loaded and copied into a big array of integers that acts as the emulated computer's RAM. The emulator CPU is then pointed to the first instruction of the machine code and told to start interpreting the instructions (such as reading/writing RAM, doing arithmetic and logic operations, jumping around to different instructions, etc). Sometimes it will write data (like the system log, or a shell command prompt) to the "terminal" via another piece of JavaScript code that simulates a serial port and a terminal using the browser DOM. Your key presses are also sent back to Linux via this simulated serial port...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNikita BarsukovView Question on Stackoverflow
Solution 1 - JavascriptAaron DigullaView Answer on Stackoverflow
Solution 2 - JavascriptalexView Answer on Stackoverflow