Fabrice Bellard 编写的 Javascript 中的 Linux 模拟器如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6030407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:07:06  来源:igfitidea点击:

How does Linux emulator in Javascript by Fabrice Bellard work?

javascriptlinuxemulation

提问by Nikita Barsukov

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

今天,当我在 Javascript 中看到 Linux 模拟器时,我有一个令人瞠目结舌的时刻:http: //bellard.org/jslinux/

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

它编译 C 程序,它有 vi 和 emacs,它支持所有的 shell 命令等等。

How does it work?

它是如何工作的?

采纳答案by Aaron Digulla

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.

起初,我还认为这只是一个将您连接到 VM 的终端模拟器,但事实并非如此。如果您观察网络连接,您可以看到,启动后不再传输任何数据。

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.

CPU 并不是什么神奇的东西。事实上,它所做的只是从内存中读取字节并根据命令的含义修改 RAM。

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

在这种情况下,CPU 模拟器基于 qemu 代码。他所做的是创建一个函数数组,其中索引是 PC(程序计数器)的下一个字节。

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.

现在你所需要的只是一个简单的 linux 发行版,它不需要任何奇特的 CPU 命令,如浮点运算或 MMX 代码等等。

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

有趣的是野兽的速度。整个事情有点迟钝,但是,它是浏览器中的 JavaScript。

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

结论:印象深刻。迫不及待想看到一个 C64 模拟器:-)

回答by alex

See http://www.quora.com/CPU-Emulation/How-does-bellard-org-jslinux-work

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

简化说明

jslinux 本质上是用软件实现的完整计算机,特别是 JavaScript。这当然被称为模拟器。这个特定版本被设置为运行 Linux,但理论上它可以运行其他操作系统。

模拟器(JavaScript,约 90KB 缩小,约 7000 行格式化)被加载到浏览器中。一个版本的 Linux 以前被编译成 x86 处理器的机器代码,然后被加载并复制到一个大的整数数组中,作为模拟计算机的 RAM。然后仿真器 CPU 被指向机器代码的第一条指令并被告知开始解释指令(例如读/写 RAM、进行算术和逻辑运算、跳转到不同的指令等)。有时它会通过另一段 JavaScript 代码将数据(如系统日志或 shell 命令提示符)写入“终端”,该代码使用浏览器 DOM 模拟串行端口和终端。你的按键也会通过这个模拟串口发送回 Linux...