71

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?

3
  • 9
    That is pretty awesome. You did note the 'technical details' link, did you?
    – sehe
    May 17, 2011 at 12:05
  • 13
    Looking at Fabrice Bellard's accomplishments is a very humbling experience for any programmer.
    – Julian Go
    May 17, 2011 at 13:22
  • Someone's been reading /. I guess :p.
    – wimvds
    May 17, 2011 at 13:23

2 Answers 2

37

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 :-)

1
  • there is one which can emulate the SID and VIC chips? May 17, 2011 at 13:44
32

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...

1
  • 48
    Oh, Quora community actually produced a better answer that SO. May 18, 2011 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.