/*private void ASM_ParallelStart(ref ProgramThread currentthread)
        {
            currentthread.inspointer++;
            currentthread.inspointer = (int)currentcode.numconstants[
                currentcode.instructions[currentthread.inspointer]];

            WrappedThreadObject tempthread = new WrappedThreadObject(this);
            ArrayList threads= new ArrayList();
            while(currentcode.instructions[currentthread.inspointer]==(ushort)ASMPROCESSOR_OPERATIONS.CREATETHREAD)
            {
                currentthread.inspointer++;
                tempthread.mthread.inspointer = (int)currentcode.numconstants[
                    currentcode.instructions[currentthread.inspointer]];
                currentthread.inspointer++;
                tempthread.mthread.endthread = (int)currentcode.numconstants[
                    currentcode.instructions[currentthread.inspointer]];
                currentthread.inspointer++;
                threads.Add(tempthread);
                tempthread.Start();
                tempthread = new WrappedThreadObject(this);
            }
            foreach(WrappedThreadObject temp in threads)
                temp.thread.Join();

            //threads = null;
        }*/
        private void ASM_ParallelStart(ref ProgramThread currentthread)
        {
            currentthread.inspointer++;
            int paralleloffset= currentthread.inspointer;
            currentthread.inspointer += (int)currentcode.numconstants[
                currentcode.instructions[currentthread.inspointer]];

            WrappedThreadObjectRS tempthread = new WrappedThreadObjectRS(this);
            ArrayList threads= new ArrayList();

            while(currentcode.instructions[currentthread.inspointer]==(ushort)ASMPROCESSOR_OPERATIONS.CREATETHREAD)
            {
                    currentthread.inspointer++;
                    tempthread.mthread.inspointer =paralleloffset + (int)currentcode.numconstants[
                        currentcode.instructions[currentthread.inspointer]];
                    currentthread.inspointer++;
                    tempthread.mthread.endthread =paralleloffset + (int)currentcode.numconstants[
                        currentcode.instructions[currentthread.inspointer]];
                    currentthread.inspointer++;

                    currentthread.inspointer += (currentcode.instructions[currentthread.inspointer]+1);
                    threads.Add(tempthread);
                    tempthread.Start();
                    tempthread = new WrappedThreadObjectRS(this);
            }
            foreach(WrappedThreadObjectRS temp in threads)
                temp.thread.Join();

            //threads = null;
        }
 private void ASM_LogicalNot(ref ProgramThread currentthread)
 {
     currentthread.cmpflag = !(currentthread.cmpflag);
 }
 public RemoteProcessor()
 {
     mthread = new ProgramThread();
     state = RemoteProcessorStates.STOP;
 }
        private void ASM_IsMatrix(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            currentthread.stackpointer++;
            if(tempvar1type == MEMTYPECONSTANTS.REFERENCE
                || tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =1;
            else
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =0;

            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;

            if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)
                FreeMatrixMemory(tempvar1);
        }
 private void ASM_JmpCmp2(ref ProgramThread currentthread)
 {
     currentthread.inspointer++;
     if(currentthread.cmpflag)currentthread.inspointer += ((int)currentcode.numconstants[currentcode.instructions [currentthread.inspointer]]);
 }
        private void ASM_GetRows(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.REFERENCE
                && tempvar1type != MEMTYPECONSTANTS.TEMPREFERENCE)
                throw new Exception("Type mismatch");

            GetMatrix(tempvar1);
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.DATA] =currentcode.matrixmemory[tempvar1].Rows;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;

            if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)
                FreeMatrixMemory(tempvar1);
        }
        private void ASM_IsLessThanEqual(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2;
            MEMTYPECONSTANTS tempvar1type,tempvar2type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type==MEMTYPECONSTANTS.VALUE && tempvar2type==MEMTYPECONSTANTS.VALUE)
                currentthread.cmpflag = (tempvar1 <= tempvar2);
            else
                throw new Exception("Type mismatch");
        }
 private void ASM_StoreExtra(ref ProgramThread currentthread)
 {
     if(currentthread.cmpflag)
     {
         //PushStack(ref currentthread,1,MEMTYPECONSTANTS.VALUE);
         //PushStack Start
         currentthread.stackpointer++;
         currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =1;
         currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
         //PushStack End
     }
     else
     {
         //PushStack(ref currentthread,0,MEMTYPECONSTANTS.VALUE);
         //PushStack Start
         currentthread.stackpointer++;
         currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =0;
         currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
         //PushStack End
     }
 }
        private void ASM_StoreMatrix(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2,tempvar3,tempvar4;
            MEMTYPECONSTANTS tempvar1type,tempvar2type,tempvar3type,tempvar4type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar3 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar3type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar4 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar4type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.REFERENCE )throw new Exception("Type mismatch");
            if(tempvar2type != MEMTYPECONSTANTS.VALUE )throw new Exception("Type mismatch");
            if(tempvar3type != MEMTYPECONSTANTS.VALUE )throw new Exception("Type mismatch");
            if(tempvar4type != MEMTYPECONSTANTS.VALUE )throw new Exception("Type mismatch");

            GetMatrix((int)tempvar1);
            currentcode.matrixmemory[(int)tempvar1].SetValue((int)tempvar2,(int)tempvar3,tempvar4);
            MatrixStateModified((int)tempvar1);
        }
        private void ASM_StartMatrix(ref ProgramThread currentthread)
        {
            currentthread.inspointer++;
            int rws = currentcode.instructions[currentthread.inspointer];
            currentthread.inspointer++;
            int cols = (int)currentcode.numconstants[currentcode.instructions[currentthread.inspointer]];
            Matrix temp =  new Matrix(rws,cols);

            for(int rw=0;rw<rws;rw++)
            {
                currentthread.inspointer++;

                if((ASMPROCESSOR_OPERATIONS)currentcode.instructions[currentthread.inspointer]==ASMPROCESSOR_OPERATIONS.ROWDEFINEDBYVALUES)
                    for(int cl=0;cl<cols;cl++)
                    {
                        currentthread.inspointer++;
                        temp.m_MatArr[rw,cl] = currentcode.numconstants[currentcode.instructions[currentthread.inspointer]];
                    }
                else
                {
                    double st,inc;
                    currentthread.inspointer++;
                    st = currentcode.numconstants[currentcode.instructions[currentthread.inspointer]];
                    currentthread.inspointer++;
                    inc = currentcode.numconstants[currentcode.instructions[currentthread.inspointer]];
                    double tmp=st;
                    for(int cl=0;cl<cols;cl++)
                    {
                        temp.m_MatArr[rw,cl] = tmp;
                        tmp=tmp+inc;
                    }
                }
            }
            //PushStack(ref currentthread,PlaceMatrix(temp),MEMTYPECONSTANTS.TEMPREFERENCE);
            //PushStack Start
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =PlaceMatrix(temp);
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.TEMPREFERENCE;
            //PushStack End
        }
        private void ASM_Store(ref ProgramThread currentthread)
        {
            ushort tempvar;
            int rmm=-2;
            currentthread.inspointer++;
            if((MEMTYPECONSTANTS)currentcode.memory[currentcode.instructions [
                currentthread.inspointer],(int)MEMCONSTANTS.TYPE]==
                MEMTYPECONSTANTS.REFERENCE ||(MEMTYPECONSTANTS)currentcode.memory[
                currentcode.instructions[currentthread.inspointer],
                (int)MEMCONSTANTS.TYPE]==MEMTYPECONSTANTS.TEMPREFERENCE)
                rmm =(int)currentcode.memory[currentcode.instructions
                    [currentthread.inspointer],(int)MEMCONSTANTS.DATA];

            tempvar =(ushort)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            if(tempvar==(ushort)MEMTYPECONSTANTS.VALUE)
            {
                tempvar = currentcode.instructions [currentthread.inspointer];
                currentcode.memory[tempvar,(int)MEMCONSTANTS.DATA] = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
                currentcode.memory[tempvar,(int)MEMCONSTANTS.TYPE] = (double)MEMTYPECONSTANTS.VALUE;
            }
            else if(tempvar==(ushort)MEMTYPECONSTANTS.REFERENCE)
            {
                tempvar = currentcode.instructions [currentthread.inspointer];
                GetMatrix((int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA]);
                Matrix temp = currentcode.matrixmemory[(int)currentthread.pstack
                    [currentthread.stackpointer,(int)MEMCONSTANTS.DATA]].ReturnClone();
                currentcode.memory[tempvar,(int)MEMCONSTANTS.DATA]= PlaceMatrix(temp);
                currentcode.memory[tempvar,(int)MEMCONSTANTS.TYPE] = (double)MEMTYPECONSTANTS.REFERENCE;
            }
            else if(tempvar==(ushort)MEMTYPECONSTANTS.TEMPREFERENCE)
            {
                tempvar = currentcode.instructions [currentthread.inspointer];
                currentcode.memory[tempvar,(int)MEMCONSTANTS.DATA]= currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
                currentcode.memory[tempvar,(int)MEMCONSTANTS.TYPE] = (double)MEMTYPECONSTANTS.REFERENCE;
            }
            currentthread.stackpointer--;
            if(rmm>=0)
                FreeMatrixMemory(rmm);
        }
        private void ASM_SetMatrixToDevice(ref ProgramThread currentthread)
        {
            int tempvar1,tempvar2;
            MEMTYPECONSTANTS tempvar1type,tempvar2type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE || (tempvar2type !=
                MEMTYPECONSTANTS.REFERENCE && tempvar2type !=
                MEMTYPECONSTANTS.TEMPREFERENCE))
                throw new Exception("Type mismatch");

            currentthread.stackpointer++;
            try
            {
                if(currentcode.bus.ExternalDevices[tempvar1].IsConnected)
                {
                    GetMatrix(tempvar2);
                    currentcode.bus.ExternalDevices[tempvar1].PutMatrix(
                        currentcode.matrixmemory[tempvar2]);
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.DATA] = 1;
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
                }
                else
                {
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.DATA] =-2;
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
                }

            }
            catch(Exception e)
            {
                currentthread.pstack[currentthread.stackpointer,
                    (int)MEMCONSTANTS.DATA] =-1;
                currentthread.pstack[currentthread.stackpointer,
                    (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
            }

            if(tempvar2type == MEMTYPECONSTANTS.TEMPREFERENCE)
                FreeMatrixMemory(tempvar2);
        }
 private void ASM_PushStack(ref ProgramThread currentthread)
 {
     currentthread.inspointer++;
     ushort tempvar = currentcode.instructions[currentthread.inspointer];
     currentthread.inspointer++;
     ushort tempvar2 = currentcode.instructions[currentthread.inspointer];
     //PushStack(ref currentthread,tempvar,(MEMTYPECONSTANTS)tempvar2);
     //PushStack Start
     currentthread.stackpointer++;
     currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] = tempvar;
     currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] = tempvar2;
     //PushStack End
 }
        private void ASM_Print(ref ProgramThread currentthread)
        {
            double tempvar1;
            MEMTYPECONSTANTS tempvar1type;
            string output="";

            currentthread.inspointer++;
            ushort argcount = currentcode.instructions[
                currentthread.inspointer];

            for(ushort tmp=1;tmp<=argcount;tmp++)
            {
                tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
                tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
                currentthread.stackpointer--;

                if(tempvar1type == MEMTYPECONSTANTS.VALUE)
                    output += tempvar1.ToString()+"     ";
                else if(tempvar1type == MEMTYPECONSTANTS.REFERENCE || tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    GetMatrix((int)tempvar1);
                    output += currentcode.matrixmemory[(int)tempvar1].Display()+"     ";
                    if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)FreeMatrixMemory((int)tempvar1);
                }
                else if(tempvar1type == MEMTYPECONSTANTS.LITERAL)
                    output += currentcode.literals[(int)tempvar1]+"     ";
                else
                    throw new Exception("Type mismatch");
            }
            currentcode.bus.Out.Output(output);
            currentcode.bus.Out.Output("\r\n");
        }
        private void ASM_GetPrimes(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE)
                throw new Exception("Type mismatch");

            if(tempvar1 <= 0)
                throw new Exception("Supplied argument cannot be less than or equal to zero.");

            long num =1;
            Matrix tempmat = new Matrix(1,tempvar1);

            for(int lpvar=1;lpvar<=tempvar1;lpvar++)
            {
                bool primeflag = false;

                while(!primeflag)
                {
                    long k=1;
                    for (int lpvar2=2;lpvar2<=num/2;lpvar2++)
                    {
                        k=num%lpvar2;
                        if(k==0)
                            break;
                    }
                    if(k!=0)
                    {
                        tempmat.m_MatArr[0,lpvar-1] = num;
                        primeflag=true;
                    }
                    num++;
                }
            }

            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.DATA] =PlaceMatrix(tempmat);;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.TEMPREFERENCE;
        }
        private void ASM_WaitForDevice(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE)
                throw new Exception("Type mismatch");

            //PushStack Start
            currentthread.stackpointer++;
            try
            {
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =
                    currentcode.bus.ExternalDevices[tempvar1].WaitForDevice();
            }
            catch(Exception e)
            {
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =-1;
            }
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
            //PushStack End
        }
        private void ASM_GetPrimesUpto(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE)
                throw new Exception("Type mismatch");

            if(tempvar1 <= 0)
                throw new Exception("Supplied argument cannot be less than or equal to zero.");

            ArrayList templist = new ArrayList();

            for(int lpvar=1;lpvar<=tempvar1;lpvar++)
            {
                long k=1;
                for (int lpvar2=2;lpvar2<=lpvar/2;lpvar2++)
                {
                    k=lpvar%lpvar2;
                    if(k==0)
                        break;
                }
                if(k!=0)
                    templist.Add(lpvar);
            }

            Matrix tempmat = new Matrix(1,templist.Count);
            int lpvar3=0;
            foreach(object tmp in templist)
            {
                tempmat.m_MatArr[0,lpvar3] =long.Parse(tmp.ToString());
                lpvar3++;
            }

            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.DATA] =PlaceMatrix(tempmat);;
            currentthread.pstack[currentthread.stackpointer,
                (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.TEMPREFERENCE;
        }
 public WrappedThreadObjectRS(ASMProcessorRS threadprocessor)
 {
     this.mthread = new ProgramThread();
     this.mthread.pstack = new double[RemoteMachine.threadstacksize,2];
     this.mthread.stackpointer = -1;
     this.threadprocessor = threadprocessor;
 }
        private void ASM_InputNumber(ref ProgramThread currentthread)
        {
            double tempvar1;
            MEMTYPECONSTANTS tempvar1type;
            string output="";

            currentthread.inspointer++;
            ushort argcount = currentcode.instructions[
                currentthread.inspointer];

            for(ushort tmp=1;tmp<=argcount;tmp++)
            {
                tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
                tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
                currentthread.stackpointer--;

                if(tempvar1type == MEMTYPECONSTANTS.VALUE)
                    output += tempvar1.ToString();
                else if(tempvar1type == MEMTYPECONSTANTS.REFERENCE || tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    GetMatrix((int)tempvar1);
                    output += currentcode.matrixmemory[(int)tempvar1].Display();
                    if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)currentcode.matrixmemory[(int)tempvar1]= null;
                }
                else if(tempvar1type == MEMTYPECONSTANTS.LITERAL)
                    output += currentcode.literals[(int)tempvar1];
                else
                    throw new Exception("Type mismatch");
            }
            lock(this)
            {
                //currentcode.bus.Out.Output(output);

                //PushStack Start
                currentthread.stackpointer++;
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =currentcode.bus.In.Input(output);
                currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
                //PushStack End
            }
        }
        private void ASM_Cos(ref ProgramThread currentthread)
        {
            double tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type == MEMTYPECONSTANTS.REFERENCE)
            {
                GetMatrix((int)tempvar1);
                tempvar1 = PlaceMatrix(currentcode.matrixmemory[(int)tempvar1].ReturnCos());
                tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
            }
            else if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE )
            {
                currentcode.matrixmemory[(int)tempvar1].Cos();
                MatrixStateModified((int)tempvar1);
            }
            else if(tempvar1type == MEMTYPECONSTANTS.VALUE )
            {
                tempvar1 = Math.Cos(tempvar1);
            }
            else
                throw new Exception("Type mismatch");

            //PushStack(ref currentthread,tempvar1,tempvar1type);
            //PushStack Start
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =tempvar1;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)tempvar1type;
            //PushStack End
        }
 public void Process(ref ProgramThread currentthread)
 {
     while(currentthread.inspointer<=currentthread.endthread)
     {
         ASMOperations[currentcode.instructions[currentthread.inspointer]](ref  currentthread);
         currentthread.inspointer++;
     }
 }
 private void ASM_GetConstant(ref ProgramThread currentthread)
 {
     currentthread.inspointer++;
     //PushStack(ref currentthread,currentcode.numconstants [
     //	currentcode.instructions [currentthread.inspointer]
     //	],MEMTYPECONSTANTS.VALUE);
     //PushStack Start
     currentthread.stackpointer++;
     currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =
         currentcode.numconstants [currentcode.instructions
         [currentthread.inspointer]];
     currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] = (double)MEMTYPECONSTANTS.VALUE;
     //PushStack End
 }
        private void ASM_IsNotEqual(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2;
            MEMTYPECONSTANTS tempvar1type,tempvar2type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type==MEMTYPECONSTANTS.VALUE && tempvar2type==MEMTYPECONSTANTS.VALUE)
                currentthread.cmpflag = (tempvar1 != tempvar2);
            else if((tempvar1type==MEMTYPECONSTANTS.REFERENCE || tempvar1type==MEMTYPECONSTANTS.TEMPREFERENCE) && (tempvar2type==MEMTYPECONSTANTS.REFERENCE || tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE))
            {
                GetMatrix((int)tempvar1);GetMatrix((int)tempvar2);
                currentthread.cmpflag = (currentcode.matrixmemory[(int)tempvar1] != currentcode.matrixmemory[(int)tempvar2]);

                if(tempvar1type == MEMTYPECONSTANTS.TEMPREFERENCE)FreeMatrixMemory((int)tempvar1);
                if(tempvar2type == MEMTYPECONSTANTS.TEMPREFERENCE)FreeMatrixMemory((int)tempvar2);
            }
            else
                throw new Exception("Type mismatch");
        }
        private void ASM_GetMatrixElement(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2,tempvar3;
            MEMTYPECONSTANTS tempvar1type,tempvar2type,tempvar3type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar3 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar3type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.REFERENCE )throw new Exception("Type mismatch");
            if(tempvar2type != MEMTYPECONSTANTS.VALUE )throw new Exception("Type mismatch");
            if(tempvar3type != MEMTYPECONSTANTS.VALUE )throw new Exception("Type mismatch");

            //PushStack(ref currentthread,currentcode.matrixmemory[(int)tempvar1].GetValue((int)tempvar2,(int)tempvar3),MEMTYPECONSTANTS.VALUE);
            //PushStack Start
            currentthread.stackpointer++;
            GetMatrix((int)tempvar1);
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =currentcode.matrixmemory[(int)tempvar1].GetValue((int)tempvar2,(int)tempvar3);
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
            //PushStack End
        }
        private void ASM_Add(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2;
            MEMTYPECONSTANTS tempvar1type,tempvar2type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type==MEMTYPECONSTANTS.VALUE && tempvar2type==MEMTYPECONSTANTS.VALUE)
                tempvar1 = tempvar1+tempvar2;
            else if(tempvar1type==MEMTYPECONSTANTS.VALUE && (tempvar2type==MEMTYPECONSTANTS.REFERENCE || tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE))
            {
                GetMatrix((int)tempvar2);
                Matrix temp = currentcode.matrixmemory[(int)tempvar2];
                temp = temp + tempvar1;
                if(tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    currentcode.matrixmemory[(int)tempvar2] = temp;
                    MatrixStateModified((int)tempvar2);
                    tempvar1 = tempvar2;
                    tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
                }
                else
                {
                    tempvar1 = PlaceMatrix(temp);
                    tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
                }
            }
            else if(tempvar2type==MEMTYPECONSTANTS.VALUE && (tempvar1type==MEMTYPECONSTANTS.REFERENCE || tempvar1type==MEMTYPECONSTANTS.TEMPREFERENCE))
            {
                GetMatrix((int)tempvar1);
                Matrix temp = currentcode.matrixmemory[(int)tempvar1];
                temp = temp + tempvar2;
                if(tempvar1type==MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    currentcode.matrixmemory[(int)tempvar1] = temp;
                    MatrixStateModified((int)tempvar1);
                }
                else
                {
                    tempvar1 = PlaceMatrix(temp);
                    tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
                }
            }
            else if((tempvar2type==MEMTYPECONSTANTS.REFERENCE || tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE) && (tempvar1type==MEMTYPECONSTANTS.REFERENCE || tempvar1type==MEMTYPECONSTANTS.TEMPREFERENCE))
            {
                GetMatrix((int)tempvar2);
                GetMatrix((int)tempvar1);
                Matrix temp1 = currentcode.matrixmemory[(int)tempvar1];
                Matrix temp2 = currentcode.matrixmemory[(int)tempvar2];
                temp1 = temp1+temp2;

                if(tempvar1type==MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    currentcode.matrixmemory[(int)tempvar1] = temp1;
                    MatrixStateModified((int)tempvar1);
                    if(tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE)FreeMatrixMemory((int)tempvar2);
                }
                else if(tempvar2type==MEMTYPECONSTANTS.TEMPREFERENCE)
                {
                    currentcode.matrixmemory[(int)tempvar2] = temp1;
                    MatrixStateModified((int)tempvar2);
                    tempvar1 = tempvar2;
                    tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
                }
                else
                {
                    tempvar1 = PlaceMatrix(temp1);
                    tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;
                }
            }
            else
                throw new Exception("Type mismatch");

            //PushStack(ref currentthread,tempvar1,tempvar1type);
            //PushStack Start
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =tempvar1;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)tempvar1type;
            //PushStack End
        }
        private void ASM_GetMatrixFromDevice(ref ProgramThread currentthread)
        {
            int tempvar1;
            MEMTYPECONSTANTS tempvar1type;

            tempvar1 = (int)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE)
                throw new Exception("Type mismatch");

            currentthread.stackpointer++;
            try
            {
                if(currentcode.bus.ExternalDevices[tempvar1].IsConnected)
                {
                    Matrix tempmat = currentcode.bus.ExternalDevices
                        [tempvar1].GetMatrix();
                    if(Object.ReferenceEquals(tempmat,null))
                    {
                        currentthread.pstack[currentthread.stackpointer,
                            (int)MEMCONSTANTS.DATA] =0;
                        currentthread.pstack[currentthread.stackpointer,
                            (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
                    }
                    else
                    {
                        currentthread.pstack[currentthread.stackpointer,
                            (int)MEMCONSTANTS.DATA] = PlaceMatrix(tempmat);
                        currentthread.pstack[currentthread.stackpointer,
                            (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.TEMPREFERENCE;
                    }
                }
                else
                {
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.DATA] =-2;
                    currentthread.pstack[currentthread.stackpointer,
                        (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
                }

            }
            catch(Exception e)
            {
                currentthread.pstack[currentthread.stackpointer,
                    (int)MEMCONSTANTS.DATA] =-1;
                currentthread.pstack[currentthread.stackpointer,
                    (int)MEMCONSTANTS.TYPE] =(double)MEMTYPECONSTANTS.VALUE;
            }
        }
        private void ASM_LogicalOR(ref ProgramThread currentthread)
        {
            bool extraflag;
            if(currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA]==1)
                extraflag = true;
            else
                extraflag = false;

            currentthread.stackpointer--;
            currentthread.cmpflag = (currentthread.cmpflag || extraflag);
        }
        private void ASM_GetMemory(ref ProgramThread currentthread)
        {
            ushort tempvar;
            currentthread.inspointer++;
            tempvar = currentcode.instructions [currentthread.inspointer];
            //PushStack(ref currentthread,currentcode.memory[tempvar,(int)MEMCONSTANTS.DATA],
            //	(MEMTYPECONSTANTS)currentcode.memory[tempvar,(int)MEMCONSTANTS.TYPE]);

            //PushStack Start
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] = currentcode.memory[tempvar,(int)MEMCONSTANTS.DATA];
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] = currentcode.memory[tempvar,(int)MEMCONSTANTS.TYPE];
            //PushStack End
        }
        public void ProcessFile(string filename)
        {
            Console.WriteLine("Loading Program --- {0}.....",filename);
            localprocessor.currentcode = LoadFile(filename);

            localprocessor.currentcode.matrixmemory = new ASMMath.Matrix[matrixmemsize];
            localprocessor.currentcode.bus = this;

            ProgramThread mthread = new ProgramThread();
            mthread.inspointer = 0;
            mthread.endthread = localprocessor.currentcode.instructions.Length-1;
            mthread.stackpointer = -1;
            mthread.pstack = new double[threadstacksize,2];

            RemoteProcessorsManager.SendNumConstants(ref localprocessor.currentcode);
            RemoteProcessorsManager.SendLiterals(ref localprocessor.currentcode);

            Console.WriteLine("Start running....");
            localprocessor.raiseerror = new RaiseError(this.ErrorEvent);
            localprocessor.Process(ref mthread);
        }
        private void ASM_NewMatrix(ref ProgramThread currentthread)
        {
            double tempvar1,tempvar2;
            MEMTYPECONSTANTS tempvar1type,tempvar2type;

            tempvar1 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar1type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;
            tempvar2 = currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA];
            tempvar2type =(MEMTYPECONSTANTS)currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE];
            currentthread.stackpointer--;

            if(tempvar1type != MEMTYPECONSTANTS.VALUE || tempvar2type != MEMTYPECONSTANTS.VALUE)throw new Exception("Type mismatch");

            tempvar1 = PlaceMatrix(new Matrix((int)tempvar1,(int)tempvar2));
            tempvar1type = MEMTYPECONSTANTS.TEMPREFERENCE;

            //PushStack(ref currentthread,tempvar1,tempvar1type);
            //PushStack Start
            currentthread.stackpointer++;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.DATA] =tempvar1;
            currentthread.pstack[currentthread.stackpointer,(int)MEMCONSTANTS.TYPE] =(double)tempvar1type;
            //PushStack End
        }