限制模拟CPU速度的方法 [英] Methods of limiting emulated cpu speed

查看:247
本文介绍了限制模拟CPU速度的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写MOS 6502处理器仿真器,这是我在业余时间从事的一个较大项目的一部分.该仿真器是用Java编写的,在您说出它之前,我知道它不会像用c或汇编语言编写的那样高效和优化,但是目标是使其在各种平台上运行并获得2.5MHZ的性能.在1GHZ处理器上,对于解释型模拟器来说非常不错.我的问题恰恰相反,我需要将周期数限制为1MHZ.香港专业教育学院环顾四周,但没有看到很多这样做的策略. Ive尝试了一些方法,包括检查多个周期后的时间,以及休眠预期时间与实际经过时间之间的差异,但是检查时间会使仿真速度降低8倍,因此任何人都有更好的建议或也许可以通过优化Java时间轮询以降低速度的方法?

I'm writing a MOS 6502 processor emulator as part of a larger project I've undertaken in my spare time. The emulator is written in Java, and before you say it, I know its not going to be as efficient and optimized as if it was written in c or assembly, but the goal is to make it run on various platforms and its pulling 2.5MHZ on a 1GHZ processor which is pretty good for an interpreted emulator. My problem is quite to the contrary, I need to limit the number of cycles to 1MHZ. Ive looked around but not seen many strategies for doing this. Ive tried a few things including checking the time after a number of cycles and sleeping for the difference between the expected time and the actual time elapsed, but checking the time slows down the emulation by a factor of 8 so does anyone have any better suggestions or perhaps ways to optimize time polling in java to reduce the slowdown?

推荐答案

使用sleep()的问题在于,您通常只能获得1ms的粒度,并且您将获得的实际睡眠不一定准确到最接近的1毫秒,这取决于系统其余部分的工作情况.有几个建议可以尝试(我的头顶上-我实际上没有用Java编写CPU仿真器):

The problem with using sleep() is that you generally only get a granularity of 1ms, and the actual sleep that you will get isn't necessarily even accurate to the nearest 1ms as it depends on what the rest of the system is doing. A couple of suggestions to try (off the top of my head-- I've not actually written a CPU emulator in Java):

  • 请遵循您的想法,但要检查大量仿真指令之间的时间(无论如何,执行都会有点笨拙",尤其是在单处理器计算机上,因为操作系统可能会占用大量的时间)一次从您线程中获取CPU的时间为几毫秒);

  • stick to your idea, but check the time between a large-ish number of emulated instructions (execution is going to be a bit "lumpy" anyway especially on a uniprocessor machine, because the OS can potentially take away the CPU from your thread for several milliseconds at a time);

由于您希望每毫秒执行1000条仿真指令,因此您也可以尝试仅在指令"之间挂在CPU上:让您的程序通过反复试验定期计算出多少次运行它需要在指令之间进行循环,以浪费"足够的CPU,以使计时平均以每秒100万条仿真指令的速度进行计算(在这种情况下,您可能需要查看将线程设置为低优先级是否有助于系统性能).

as you want to execute in the order of 1000 emulated instructions per millisecond, you could also try just hanging on to the CPU between "instructions": have your program periodically work out by trial and error how many runs through a loop it needs to go between instructions to "waste" enough CPU to make the timing work out at 1 million emulated instructions / sec on average (you may want to see if setting your thread to low priority helps system performance in this case).

这篇关于限制模拟CPU速度的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆