【发布时间】:2013-03-03 07:34:35
【问题描述】:
我正在尝试用 C# 编写一个 CPU 模拟器。机器的对象是这样的:
class Machine
{
short a,b,c,d; //these are registers.
short[] ram=new short[0x10000]; //RAM organised as 65536 16-bit words
public void tick() { ... } //one instruction is processed
}
当我执行一条指令时,我有一个 switch 语句,它决定指令的结果将存储在什么中(寄存器或 RAM 的一个字)
我希望能够做到这一点:
short* resultContainer;
if (destination == register)
{
switch (resultSymbol) //this is really an opcode, made a char for clarity
{
case 'a': resultContainer=&a;
case 'b': resultContainer=&b;
//etc
}
}
else
{
//must be a place in RAM
resultContainer = &RAM[location];
}
然后,当我执行完指令后,我可以简单地将结果存储为:
*resultContainer = result;
我一直在试图弄清楚如何在不破坏 C# 的情况下做到这一点。
我如何使用unsafe{} 和fixed(){ } 以及其他我不知道的东西来实现这一点?
【问题讨论】:
-
bregister?你的意思是resultContainer? -
很好,谢谢!
-
酷,DCPU / 0x10c :)
-
是的 :) DCPU 是唯一具有非奇怪寄存器名称的架构吗?
标签: c# .net pointers unsafe fixed-point