我想在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
}
當我執行的指令,我有決定哪些指令的結果將被存儲在(一個寄存器或RAM的字)
switch語句我希望能夠做到這一點:
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'? – 2013-03-03 07:43:13
很對,謝謝! – 2013-03-03 07:43:57
酷,DCPU/0x10c :) – 2013-03-03 09:11:45