top | item 6567967

Show HN: My x86 emulator written in JavaScript

765 points| g3 | 12 years ago |copy.sh

190 comments

order
[+] staunch|12 years ago|reply
This may as well be magic as far as I'm concerned. When I first saw http://bellard.org/jslinux/ I just about shat myself. This is definitely another level of awesome. Well done.
[+] ghayes|12 years ago|reply
This really is extremely well done [and I'd like to see the unminified source]. The next feature I'd want to see is networking support. I love the idea to be able to run my entire dev setup in an emulated browser session [e.g. ruby, rails, redis]. That would be a game changer on how I'd develop.
[+] acqq|12 years ago|reply
I just ran Bellard's JSLinux (#) on my retina iPad with iOS 7. It works! Booting took 15 sec. Claims 20 bogoMips. And I can type the commands in it and execute them:

First I tap to the edit box on the right, the keyboard rolls in, then I tap in the "console." The commands work. I just don't know how I'd type ctrl-Keys without the physical keyboard.

Copy.sh ends with unimplemented GP handler however for anything I've tried.

#) JSLinux: the PC emulator is written in Javascript with the emulated hardware: 32 bit x86 compatible CPU, 8259 Programmble Interrupt Controller, 8254 Programmble Interrupt Timer, 16450 UART, Real Time Clock, IDE interface and hard disk. http://bellard.org/jslinux/tech.html

[+] chubot|12 years ago|reply
Wait how does this differ from JSLinux? Aren't they doing the same thing?

Also, this isn't just an x86 emulator, right? If it can boot linux, it's emulating the entire PC platform, not just x86. Very impressive of course. But I am interested in a comparison vs jslinux (also not open source).

[+] _ak|12 years ago|reply
With emscripten, anything is possible nowadays. And it blows your mind.
[+] JoshTriplett|12 years ago|reply
Very impressive.

Interesting bug: I can't seem to type '-', '=', '+', or '_' into the Linux image. Missing '-' in particular makes it hard to run commands with options.

Elaborate workaround:

    /root% eval eq$(dmesg | grep 'e820 update' | sed 's/.*) \(.\).*/\1\1/')
    /root% echo $eq
    =
    /root% eval dash$eq$(uname bad 2>&1 | grep Usage: | sed 's/.*\[\(.\).*/\1/')
    /root% echo $dash
    -
    /root% uname ${dash}a
    Linux (none) 2.6.34.14 #44 Tue Oct 15 20:50:15 CEST 2013 i586 GNU/Linux 
The first command grabs an '=' from dmesg and sets "eq" to it (without typing '='), and then the second command grabs a '-' from the usage message of uname and sets "dash" to that (without typing '-'). The last shows how to use ${dash} in a command.
[+] Sheepshow|12 years ago|reply
This is the most soul-crushing category of bug, the kind which keeps me up all night, drinking.

Build a skyscraper, and lock the keys inside for the ribbon cutting. Sorry pal you lost your funding

Land on Mars, see an alien, but your camera is out of batteries and everybody back on Earth thinks you're a quack.

Successfully perform open-heart surgery on a desert island, but muck up the stitches and cause a scar. Your patient will hold it against you the rest of his life (which you saved).

[+] g3|12 years ago|reply
I've found the bug. For now, try Chromium. I'll fix this tomorrow.
[+] laumars|12 years ago|reply
You could just use octal escape sequences in printf:

    printf '\055'
returns

    -
Just change 055 for 075 (=), 053 (+) or 137 (_).

eg your $eq code can be written like this:

    eval eq$(printf '\075\075')
[+] femto|12 years ago|reply
I found that bug too. First thing I did was think, "a system restore is a page refresh away and I've always wanted to test the effects of this", then I attempted to type "rm -r /*".
[+] darkstalker|12 years ago|reply
I can't type "/" or "\" and all the shift-<number> keys combinations are wrong. I'm using an ES layout keyboard on Firefox.
[+] Aardwolf|12 years ago|reply
I can type -, +, = and _ from the alpha area of the keyboard, but none of the keys of the numpad works (not even the numbers)
[+] hardwaresofton|12 years ago|reply
Pretty awesome, I for one don't think the constant stream of "x, rewritten completely in javascript" is tiring (this is not meant to be sarcastic).

[EDIT] - This post doesn't seem to say enough when I look back at it. Wanted to add this:

Seeing posts like this really excites me (and inspires me) about the future of web programming, and programming in general. Anyone that's excited about programming has to get excited about abstraction, and it doesn't get much more abstract that cross-coded/implemented virtualized systems like this. Even if you hate javascript.

Every step people take in blurring lines between systems like this should be exciting, given the large amount of abstraction that had to go into creating something like this.

[+] mambodog|12 years ago|reply
If like this you might also be interested in my port of the PCE Emulator to the browser: http://jamesfriend.com.au/pce-js/

At the moment I've only uploaded a demo of the Mac Plus emulator (classic 68k mac) but I'll upload IBM PC and Atari ST demos soon also.

[+] nwh|12 years ago|reply
That's very nice too, though aren't you going to be running into some legal issues by distributing their ROMs?
[+] cokernel_hacker|12 years ago|reply
cute.

  /% cat /proc/cpuinfo                                                            
  processor       : 0                                                             
  vendor_id       : GenuineIntel                                                  
  cpu family      : 5                                                             
  model           : 1                                                             
  model name      : Pentium 60/66                                                 
  stepping        : 3                                                             
  cpu MHz         : 1.301                                                         
  cache size      : 256 KB                                                        
  fdiv_bug        : no                                                            
  hlt_bug         : no                                                            
  f00f_bug        : no                                                            
  coma_bug        : no                                                            
  fpu             : yes                                                           
  fpu_exception   : yes                                                           
  cpuid level     : 2                                                             
  wp              : yes                                                           
  flags           : fpu pse tsc cx8 pge cmov                                      
  bogomips        : 2.60                                                          
  clflush size    : 32                                                            
  cache_alignment : 32                                                            
  address sizes   : 32 bits physical, 32 bits virtual                             
  power management:
Heh, 1.3 MHz on a Pentium 60.
[+] agilebyte|12 years ago|reply

  125 kB compressed JS
  262 kB uncompressed JS
  9765 lines of uncompressed JS
Wow.
[+] sramsay|12 years ago|reply
Just when you thought "I've written an x in JavaScript" can't get any more insane . . .

Still, this wins the Nobel Prize for awesome.

[+] TeMPOraL|12 years ago|reply
I'm still waiting for "Show HN: I built a self-modifying general AI in JavaScript".

(@Eliezer, how's the work going? ;))

[+] general_failure|12 years ago|reply
Now and then I see a project which is soul crushing to me when I compare myself with other programmers. This is one of them.
[+] runn1ng|12 years ago|reply
It actually boots on Chrome.

On Android.

Then it usually dies for low memory - and I can't enter anything at all without keyboard (which doesn't pop up) - but still. The fact that I can boot up a x86 emulator in javascript in browser on ARM mobile phone is crazy.

[+] pcwalton|12 years ago|reply
Works for me on Firefox for Android, no crashes yet.
[+] mVChr|12 years ago|reply
My second computer was an IBM XT 8086. That was the first time I played and fell in love with Rogue. Every time I grab a copy of Rogue nowadays for nostalgia's sake I'm left disappointed because I can never find the version of Rogue that I played as a kid.

My friend, your emulator has that version of Rogue. The kid inside me thanks you greatly.

[+] Aardwolf|12 years ago|reply
Would have been fun if it included some interface to execute assembler code on it directly in some way (without booting an OS).

In any case, super awesome! Great work :)

EDIT: Whoa, the dos one has games on it! Can you make Wolf3D work? :)

[+] Pitarou|12 years ago|reply
I doubt it, but nethack might be worth a shot.
[+] McGlockenshire|12 years ago|reply
This is awesome.

You might want to consider re-ordering the font list in the console though. Consolas isn't perfectly fixed-width apparently, and it made Rogue rather puzzling to play.

[+] sillysaurus2|12 years ago|reply
Consoles isn't perfectly fixed width? That sucks. Till now it was my go-to fixed width font.

I assumed something named consolas would be a font suitable for consoles.

Which font do you like to use?

[+] jordwalke|12 years ago|reply
Two things:

1. How can we help? How can I support you? 2. Please open source this.

[+] api|12 years ago|reply
I know this sounds like crazy pants, but I would actually use this in the real world if it were a NodeJS module.

Not for anything compute-intensive or serious, but for creating very secure very isolated VMs to run web apps or other services in an insecure environment. It could also be a great way to take a LAMP stack app and rapidly deploy it in certain cases.

Again not for high performance, but for... I can think of a few things personally and I'm sure others can too.

(Though honestly performance wouldn't be that bad...)

Then add the ability to go back and forth between client and server, and virtual networking, and you might have a commercial "virtual DOS LAN with nodes in a browser as a service" startup. What for? Supporting legacy DOS crap: point of sale systems, etc. "Run your legacy DOS stuff in your browser with persistence in the cloud." You'd be surprised how much legacy DOS crap is out there.

[+] g3|12 years ago|reply
The project is made with both browser and nodejs support in mind. There are still some issues to resolve, but I will definitely release a node version this year.
[+] dbancajas|12 years ago|reply
I want to write an emulator as a learning experience. Any tips how should I start?
[+] revelation|12 years ago|reply
You want to write an emulator, but you probably don't want to write one for x86. A nice architecture would be 8 bit AVR microprocessors, for example.
[+] JoshTriplett|12 years ago|reply
There are two sensible approaches: interpretation or dynamic translation.

If you want to write one as a learning experience for how the platform you're emulating works and how an emulator could work in principle, you probably want to write an interpreter: find out how the platform boots, build something to start reading and interpreting instructions, start adding emulated hardware devices, and you'll get an increasingly functional interpreter.

For a start, you could ignore emulation of devices, and just write a CPU core that interprets instructions.

If you want to learn how modern (but still non-hardware-assisted) emulation works, read about how qemu's dynamic code translator and other JIT compilers work, and build a CPU emulator based on that.

[+] dave84|12 years ago|reply
I'm not the OP but check out the book: "The Elements of Computing Systems: Building a Modern Computer from First Principles" It's quite comprehensive, not exactly about writing an emulator, but it should give you what you need.
[+] s-macke|12 years ago|reply
g3: Probably you have seen my emulator. http://s-macke.github.io/jor1k/ It does more or less the same, but emulates a different CPU.

Your emulator is impressive and especially fast. I think it took a long time for you to optimize it. I tried to start TinyCore, but it stops after decompressing the kernel. So still some work to do ;)

[+] s-macke|12 years ago|reply
I saw the source and I have some tips for you: Avoid the UInt32 Arrays and the >>> operator. They could be transformed by the JIT compiler into doubles and slow everything down. This does not happen with the Int32 Arrays and the >> operator. There is a plug-in called JIT-Inspector which give you this information. Unfortunately it does no longer work in Firefox 24. Firefox 22 was the last working version. Additionally worker threads gave me an incredible speed boost in Firefox. The whole GUI stuff is then separated. Especially the costly canvas update.
[+] bcoates|12 years ago|reply
Anyone know what format the floppies are expecting? It won't accept a DOS 6.22 install disk from MSDN, maybe it doesn't like DMF?

Also, swapping disks during the install might be an issue, I don't see a button...

[+] g3|12 years ago|reply
It expects one of the standard formats (720k, 1440k or 2880k). I'll add switching disks some time in the future.