模拟器是如何工作的?当我看到NES/SNES或C64模拟器时,我很震惊。

您是否必须通过解释特定的组装指令来模拟这些机器的处理器?还有什么?它们通常是如何设计的?

你能给那些对编写模拟器(特别是游戏系统)感兴趣的人一些建议吗?


当前回答

模拟可能看起来令人生畏,但实际上比模拟容易得多。

任何处理器通常都有一个编写良好的描述状态、交互等的规范。

如果您根本不关心性能,那么您可以使用非常优雅的面向对象程序轻松地模拟大多数老式处理器。例如,X86处理器需要一些东西来维护寄存器的状态(简单),需要一些东西来维护内存的状态(简单),还需要一些东西来接收每个传入的命令并将其应用到机器的当前状态。如果您真的需要准确性,您还可以模拟内存转换、缓存等,但这是可行的。

In fact, many microchip and CPU manufacturers test programs against an emulator of the chip and then against the chip itself, which helps them find out if there are issues in the specifications of the chip, or in the actual implementation of the chip in hardware. For example, it is possible to write a chip specification that would result in deadlocks, and when a deadline occurs in the hardware it's important to see if it could be reproduced in the specification since that indicates a greater problem than something in the chip implementation.

当然,电子游戏的模拟器通常关心性能,所以它们不会使用简单的实现,它们还包括与主机系统的操作系统接口的代码,例如使用绘图和声音。

考虑到老式电子游戏(NES/SNES等)非常缓慢的性能,在现代系统上进行仿真相当容易。事实上,更令人惊讶的是,你可以下载一套SNES游戏或Atari 2600游戏,考虑到当这些系统流行时,免费访问每个卡带都是一个梦想。

其他回答

我写过一篇关于用JavaScript模拟Chip-8系统的文章。

这是一个很好的开始,因为系统不是很复杂,但你仍然了解操作码、堆栈、寄存器等是如何工作的。

我将很快为NES写一篇更长的指南。

在创建了我自己的80年代BBC微型计算机模拟器(类型VBeeb到谷歌)后,有许多事情要知道。

You're not emulating the real thing as such, that would be a replica. Instead, you're emulating State. A good example is a calculator, the real thing has buttons, screen, case etc. But to emulate a calculator you only need to emulate whether buttons are up or down, which segments of LCD are on, etc. Basically, a set of numbers representing all the possible combinations of things that can change in a calculator. You only need the interface of the emulator to appear and behave like the real thing. The more convincing this is the closer the emulation is. What goes on behind the scenes can be anything you like. But, for ease of writing an emulator, there is a mental mapping that happens between the real system, i.e. chips, displays, keyboards, circuit boards, and the abstract computer code. To emulate a computer system, it's easiest to break it up into smaller chunks and emulate those chunks individually. Then string the whole lot together for the finished product. Much like a set of black boxes with inputs and outputs, which lends itself beautifully to object oriented programming. You can further subdivide these chunks to make life easier.

Practically speaking, you're generally looking to write for speed and fidelity of emulation. This is because software on the target system will (may) run more slowly than the original hardware on the source system. That may constrain the choice of programming language, compilers, target system etc. Further to that you have to circumscribe what you're prepared to emulate, for example its not necessary to emulate the voltage state of transistors in a microprocessor, but its probably necessary to emulate the state of the register set of the microprocessor. Generally speaking the smaller the level of detail of emulation, the more fidelity you'll get to the original system. Finally, information for older systems may be incomplete or non-existent. So getting hold of original equipment is essential, or at least prising apart another good emulator that someone else has written!

添加@Cody Brocious提供的答案 在虚拟化环境中,您正在向虚拟机模拟一个新系统(CPU、I/O等),我们可以看到以下类别的模拟器。

解释:bochs是解释器的一个例子,它是一个x86 PC模拟器,它把来自客户系统的每条指令翻译成另一组指令(主机ISA的指令)来产生预期的效果。是的,它非常慢,它不缓存任何东西,所以每条指令都经过相同的周期。

动态仿真器:Qemu是一个动态仿真器。它可以实时翻译客户指令,也可以缓存结果。最好的部分是直接在主机系统上执行尽可能多的指令,这样模拟就更快了。正如Cody所提到的,它将代码划分为块(一个单独的执行流)。

静态模拟器:据我所知,没有静态模拟器可以帮助虚拟化。

模拟可能看起来令人生畏,但实际上比模拟容易得多。

任何处理器通常都有一个编写良好的描述状态、交互等的规范。

如果您根本不关心性能,那么您可以使用非常优雅的面向对象程序轻松地模拟大多数老式处理器。例如,X86处理器需要一些东西来维护寄存器的状态(简单),需要一些东西来维护内存的状态(简单),还需要一些东西来接收每个传入的命令并将其应用到机器的当前状态。如果您真的需要准确性,您还可以模拟内存转换、缓存等,但这是可行的。

In fact, many microchip and CPU manufacturers test programs against an emulator of the chip and then against the chip itself, which helps them find out if there are issues in the specifications of the chip, or in the actual implementation of the chip in hardware. For example, it is possible to write a chip specification that would result in deadlocks, and when a deadline occurs in the hardware it's important to see if it could be reproduced in the specification since that indicates a greater problem than something in the chip implementation.

当然,电子游戏的模拟器通常关心性能,所以它们不会使用简单的实现,它们还包括与主机系统的操作系统接口的代码,例如使用绘图和声音。

考虑到老式电子游戏(NES/SNES等)非常缓慢的性能,在现代系统上进行仿真相当容易。事实上,更令人惊讶的是,你可以下载一套SNES游戏或Atari 2600游戏,考虑到当这些系统流行时,免费访问每个卡带都是一个梦想。

我将如何开始模仿。

1.买一些关于低级编程的书籍,你将需要它来“模拟”任天堂的操作系统……游戏的男孩……

2.专门找一些模拟方面的书,也许还有操作系统开发方面的。(你不会做一个操作系统,但最接近它。

3.看看一些开源仿真器,特别是那些您想为其创建仿真器的系统。

4.将更复杂的代码片段复制到IDE/编译器中。这将节省编写长代码的时间。这就是我为操作系统开发所做的,使用linux的一个区域