0

So I have my memory class which looks like this:

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;
    }
}

}

and then I have my CPU class, and I will just post a small snippet of where the error is occurring:

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);

                }

So here is my problem, when I run my test which takes in assembly code and translates it to binary then runs through the code, when I get to the second command "sta" it should go to the line:

 myMemory.solveMemory(value, instr, myCache);

It then gives me a Null Reference Exception when it reaches that point. As you can see I have some command line output to try and see where it is going. It prints out the contents of myCache right before that line. It does not reach any of the debug statements in the solveMemory function however, not even the first one that says:

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

I'm not really sure what is causing this error and I've been looking at it for quite some time. Hopefully one of you will be able to find where exactly I am messing up. Thanks for the time.

VakarianWrex
  • 53
  • 2
  • 9
  • Well I know what a Null Reference Exception is, I just can't figure out why I'm getting it in my code – VakarianWrex Oct 18 '14 at 04:28
  • Perhaps I'm missing something, but where are you actually assigning to `myMemory`? – Matthew Haugen Oct 18 '14 at 05:12
  • Wow, i can't believe it. Just needed to say Memory myMemory = new Memory(); – VakarianWrex Oct 18 '14 at 05:19
  • You can post that as the answer so you get the credit. Thank you for noticing that – VakarianWrex Oct 18 '14 at 05:23
  • I'm glad we got it worked out for you, although I see you've already posted your own answer. It mightn't be a bad idea to mark this as a duplicate to the question I had originally linked to, though. That has a more full explanation, so it's more likely to help users in the future in a similar predicament. – Matthew Haugen Oct 18 '14 at 05:50

1 Answers1

0
public Memory myMemory;

should be:

public Memory myMemory = new Memory();
VakarianWrex
  • 53
  • 2
  • 9