减少模拟器中switch语句的大小? [英] Reducing size of switch statement in emulator?

查看:90
本文介绍了减少模拟器中switch语句的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用此v1.7规范编写DCPU-16仿真器。我开始放下架构,但我不喜欢使用很长的switch语句这一事实。这是我第一次编写模拟器,所以我不知道是否有更好的方法来做。尽管由于DCPU的操作码数量很少(以及我尚未实际实现指令的事实),这些开关的 并不大,但我可以想象是否正在编写用于以下目的的仿真器:较大的指令集将使switch语句巨大



Anywhom,这是我的代码



编辑:我忘记了我的问题:




  • 有没有比使用大型开关更好的仿真器设计方法?


解决方案

这种方法对我来说似乎很合理。我肯定会这样做(我已经写了一些CPU仿真器和类似类型的代码)。



最近的替代方法是一组函数指针,但是您的某些情况可能很简单(例如 cpu_regs.flags& =〜 ,如果(cpu_regs.flags& CARRY)do_rel_jump(next_byte()); ,则使用函数指针会使您减速。 p>

您可以将所有未指定任何操作集中到一个地方,这样行数会短很多,但是案例数当然仍然是相同的[除非您将其放在默认值:]。


I started writing a DCPU-16 emulator using this v1.7 spec. I started laying down the architecture, and I don't like the fact that I'm using very long switch statements. This is my first time writing an emulator, and so I don't know if there's better way to be doing it. While the switches aren't that large, due to the DCPU's small number of opcodes (and the fact that I haven't actually implemented the instructions yet), I can imagine if I were writing an emulator for a larger instruction set the switch statements would be huge.

Anywhom, here's my code.

EDIT: I forgot to get my question across:

  • Is there a better way to design an emulator than using a massive switch?

解决方案

This approach seems reasonable to me. It is certainly how I would do it (I have written a few CPU emulators and similar types of code).

The nearest alternative is a set of function pointers, but some of your cases will probably be rather simple (e.g. cpu_regs.flags &= ~CARRY or if (cpu_regs.flags & CARRY) do_rel_jump(next_byte());, so using function pointers will slow you down.

You can bunch all the "No Operation Specified yet" to one place, that will make it a lot shorter in number of lines, but the number of cases will of course still be the same [unless you put it in default:].

这篇关于减少模拟器中switch语句的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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