Skip to main content

Bellard releases a PC emulator - in Javascript

Open-source coder Fabrice Bellard has unveiled his latest project: a PC emulator written in Javascript, capable of creating a virtual machine inside any modern web browser.Bellard, best known for his work on the ffmpeg codec and QEMU machine emulator and virtualiser, decided that creating an emulation infrastructure in C was too easy - and began a project to create a full x86-compatible 32-bit CPU emulation in Javascript."I did it for fun," he explained in a posting to his website,"just because newer Javascript engines are fast enough to do complicated things." Despite this, Bellard suggests some serious uses to which the code could be put, including benchmarking of Javascript engines, client-side processing using x86 libraries, and even the ability to play old DOS games that modern systems can't cope with.The program emulates a 8259 programmable interrupt controller, a 8254 programmable interrupt timer, a 16450 UART, and a 32-bit x86 CPU equivalent to a 486, but with one slight issue - it lacks a floating-point unit, a critical component of modern processors.In order to make use of the code and create a working emulated machine, Bellard chose to use a customised Linux distribution - bared down to the absolute essentials only. "The lack of FPU is not a problem when running Linux as the operating system," he explained, "because it contains a FPU emulator." This allows the operating system to function without the floating-point 'hardware.'There are other limitations in the emulated CPU that those looking to make serious use of the program may need to consider: it doesn't use segment limit and rights checks when accessing memory, it has no CS/DS/ES/SS segment overrides - although FS/GS overrides are included - and certain instructions contained in the x86 specification are missing entirely, such as BCD operations and the BOUND instruction.Despite this, it's an impressive creation - and one which has helped Bellard understand more about the modern implementation of Javascript in today's browsers. "This emulator was a way to learn how to write optimised code for recent Javascript engines," he explained, "in particular Jaeger Monkey for Firefox 4 and V8 for Chrome."A troubling thing is that the PC emulator is about two times slower using V8 than Jaeger Monkey," Bellard discovered - despite using 32-bit builds for both, which should have roughly equal performance levels. "I have no precise explanation yet because I only looked at the Jeager Monkey code so far."The Javascript PC Emulator is available for live use on Bellard's website - although you'll need a relatively modern browser to take advantage of the tool, and a modicum of Linux knowledge to be able to achieve anything once it's loaded.