空引用异常在我的微处理器模拟器中未处理

时间:2014-10-18 03:18:50

标签: c# visual-studio nullreferenceexception processor

所以我的内存类看起来像这样:

namespace GeminiCore
{
    public class Memory
    {
    public static int[] memory = new int[256];
    public string nextInstruction;
    public static int cacheSize = 8;
    public CPU myCPU;

    public struct frame
    {
        public bool dirtyBit;
        public int isEmpty;
        public int value;
        public int tag;

        public frame(int cacheSize)
        {
            dirtyBit = false;
            isEmpty = 1;
            value = 0;
            tag = 0;
        }
    }

    public int solveMemory(int value, int instr, frame[] myCache)
    {
        Console.WriteLine("I reached the solveMemory!!!");
        int block = value % cacheSize;
        if(instr == 127 || instr == 125 || instr == 124 || instr == 123 
                || instr == 122 || instr == 121|| instr == 120)
        {
            Console.WriteLine("I reached the read section!!!");
            if(myCache[block].isEmpty == 0) //Read hit
                if(myCache[block].tag == value) 
                    return myCache[block].value;
            else
            {
                myCache[block].value = memory[value]; //Read Miss
                myCache[block].tag = value;
                myCache[block].isEmpty = 0;
                Console.WriteLine("Read Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                return myCache[block].value;
            }
        }
        else
        {
            Console.WriteLine("I reached the write section!!!");
            if (myCache[block].isEmpty == 1) //Write Miss
            {
                Console.WriteLine("Write Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                memory[value] = myCPU.ACC;
            }
            else
            {
                if (myCache[block].dirtyBit == false)
                {
                    if (myCache[block].tag != value)
                    {
                        myCache[block].value = myCPU.ACC; //Write Hit
                        myCache[block].dirtyBit = true;
                        myCache[block].tag = value;
                        Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);

                    }
                }
                else
                {
                    memory[myCache[block].tag] = myCache[block].value;
                    myCache[block].value = myCPU.ACC;
                    myCache[block].tag = value;
                    myCache[block].dirtyBit = false;
                    Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                }

            }
         }

        return value;
    }
}

}

然后我有我的CPU类,我将发布一小段错误发生的地方:

public Memory myMemory;
public static Memory.frame[] myCache = new Memory.frame[Memory.cacheSize];
public void doInstruction()
    {   
        var instr = (finalCodes[i] >> 9) & 127; //Parses op code to find instruction
        var immed = (finalCodes[i] >> 8) & 1; //Parses op code to find immediate value
        var value = (finalCodes[i] & 255); //Parse op code to find value

        foreach (Memory.frame x in myCache)
        {
            Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value);
        }

        switch (instr)
        {
            case (127): //LDA instruction
                if (immed == 1) 
                    ACC = value;
                else if (immed == 0) 
                    //ACC = Memory.memory[value];
                    ACC = myMemory.solveMemory(value, instr, myCache);
                break;
            case (126): //STA instruction
                if (immed == 0)
                {
                    Console.WriteLine("The value is: " + value + " The instruction is: " + instr);
                    foreach (Memory.frame x in myCache)
                    {
                        Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value);
                    }
                    //Memory.memory[value] = ACC;
                    myMemory.solveMemory(value, instr, myCache);

                }

所以这是我的问题,当我运行我的测试时,它接收汇编代码并将其转换为二进制,然后运行代码,当我到达第二个命令" sta"它应该去行:

 myMemory.solveMemory(value, instr, myCache);

然后它到达那个点时给我一个Null Reference Exception。正如您所看到的,我有一些命令行输出来尝试查看它的去向。它会在该行之前打印出myCache的内容。它没有到达solveMemory函数中的任何调试语句,但是,甚至没有第一个调用语句:

Console.WriteLine("I reached the solveMemory!!!");

我不确定是什么导致了这个错误,而且我已经看了很长时间了。希望你们中的一个能够找到我到底搞砸的地方。谢谢您的时间。

1 个答案:

答案 0 :(得分:0)

public Memory myMemory;

应该是:

public Memory myMemory = new Memory();