
CPU emulator in web browser (Arm/Mips/X86/Sparc) - hacking_news
https://alexaltea.github.io/unicorn.js/
======
a1k0n
The x86 example is particularly cute:

    
    
        xadd  eax, edx
        loop  -3
    

implements the Fibonacci series in two machine instructions (notice eax and
edx have 514229, 832040 in them after you run the example, the 29th and 30th
Fibonacci numbers). I first saw that in a book a friend got me for christmas;
a hard copy of this site:
[https://www.xorpd.net/pages/xchg_rax/snip_00.html](https://www.xorpd.net/pages/xchg_rax/snip_00.html)

~~~
AlexAltea
I'm glad someone caught the reference. Indeed, I took it from the [xchg
rax,rax] book.

Also, I have to apologize for the many bugs the demos have at the moment
(annoying editor, missing features), I intended to post a Shown HN post at a
later point in time. During the next days I will be taking care of these
matters. By the way, these demos rely in Capstone.js/Keystone.js to
assemble/disassemble machine code (if anyone is interested, similar demos can
be found at [https://phi.nz/](https://phi.nz/)).

------
saurik
i wonder how performance of this (emscripten of Unicorn) compares with this
much more flexible ARM emulator from a few years ago (which I am much more
excited to use for things as I can easily customize the instruction fetch and
memory access routines; it is even straightforward how to just hook system
calls).

[https://github.com/ozaki-r/arm-js](https://github.com/ozaki-r/arm-js)

[http://biged.github.io/arm-js/arm-js.html](http://biged.github.io/arm-js/arm-
js.html)

^ The latter link is a demo; click "Boot" in the upper left.

~~~
AlexAltea
Probably that Arm-js emulator you mention is faster: Unicorn is based on QEMU,
which internally does the translation by translating the guest machine code to
an IR. After optimizing it, this IR usually gets compiled back to the host
machine code, but thanks to the TCI [1] the IR can be directly interpreted
anywhere without the need of a compiler backend. All these steps incur on an
additional overhead that wouldn't be there if one interpreted ARM code
directly.

[1] [http://wiki.qemu.org/Features/TCI](http://wiki.qemu.org/Features/TCI)

~~~
saurik
You say "you" multiple times, but I did not implement this other emulator: I
simply found it yesterday while evaluating options for emulating armv7 on top
of JavaScriptCore.

On the other side: this CPU emulator is written using high-level JavaScript,
and I think some of its control flow is implemented using exception handling,
whereas compiling Unicorn to asm.js will get reconstituted in modern broswers
back to reasonable native code (and qemu is designed to be fast). I don't
think it is clear cut which is faster.

(I would do a benchmark, but for my use case I actually am working with
interpreted JS, which is massively different, and I had already evaluated that
Unicorn.js wasn't flexible enough for my intent, so it is more of a curiosity
than a question I would spend time to answer myself.)

~~~
AlexAltea
My bad, I realized my mistake and was editing my message right before your
answer.

Indeed, performance is not clear and probably depends a lot on the browser's
JavaScript engine. I will try to find that out in the future with some
benchmark.

------
hacking_news
Demo for ARM:
[https://alexaltea.github.io/unicorn.js/demo.html?arch=arm](https://alexaltea.github.io/unicorn.js/demo.html?arch=arm)

This is based on the Unicorn emulator (open source):
[https://github.com/unicorn-engine/unicorn](https://github.com/unicorn-
engine/unicorn)

------
gravypod
Does anyone know where you can

    
    
        1) find SPARC systems
        2) find what you need to do to initialize all components on a SPARC system
    

I'd love to write an OS for a SPARC machine one day

~~~
ttsiodras
> where you can find SPARC systems

In case by "find" you mean "where are SPARCs still being used"... It is
something of a niche, but all our missions in the European Space Agency use
LEON processors; a design based on SPARC [1].

And since you mentioned OSes, usually it is either bare-metal stuff or RTEMS
[2].

[1] [https://en.wikipedia.org/wiki/LEON](https://en.wikipedia.org/wiki/LEON)
[2] [https://www.rtems.org/](https://www.rtems.org/)

------
shmerl
_> With all that said, QEMU is an awesome project, which Unicorn was born
from. However, Unicorn is not just QEMU, but offering a lot more because it
has been designed & implemented especially for CPU emulation._

Couldn't they extend Qemu, or there was a reason for making a separate
project?

~~~
ant6n
From these slides it seems it started as a qemu fork:
[https://www.blackhat.com/docs/us-15/materials/us-15-Nguyen-U...](https://www.blackhat.com/docs/us-15/materials/us-15-Nguyen-
Unicorn-Next-Generation-CPU-Emulator-Framework.pdf)

would like to know more about unicorn as well.

------
tiatia
Apropo Emulator.

1\. Is there any way to run SGI/IRIX and some IRIX software?

2\. Would a simple IRIX Software run on BSD/MIPS? I heard they are quite
similar.

This project seems to be discontinued:
[https://www.vanheusden.com/miep/](https://www.vanheusden.com/miep/)

They claim to be able to have an emulator, but as far as I know, expensive as
f.... [http://www.vhware.co.il/](http://www.vhware.co.il/)

Any other ideas?

------
cmrdporcupine
Very cool. Needs RISC-V.

And 68000 :-)

------
hacking_news
They have a showcase page at Unicorn website, with a bunch of really exciting
tools: [http://www.unicorn-engine.org/showcase/](http://www.unicorn-
engine.org/showcase/)

