Ejemplo n.º 1
0
        } // PVM.Trace

        static void PostMortem(OutFile results, int pcNow)
        {
            // Reports run time error and position
            results.WriteLine();
            switch (ps)
            {
            case badMem:  results.Write("Memory violation"); break;

            case badData: results.Write("Invalid data"); break;

            case noData:  results.Write("No more data"); break;

            case divZero: results.Write("Division by zero"); break;

            case badOp:   results.Write("Illegal opcode"); break;

            case badInd:  results.Write("Subscript out of range"); break;

            case badVal:  results.Write("Value out of range"); break;

            case badAdr:  results.Write("Bad address"); break;

            case badAll:  results.Write("Heap allocation error"); break;

            case nullRef: results.Write("Null reference"); break;

            default:      results.Write("Interpreter error!"); break;
            }
            results.WriteLine(" at " + pcNow);
        } // PVM.PostMortem
Ejemplo n.º 2
0
        } // PVM.QuickInterpret

        public static void Interpret(int codeLen, int initSP)
        {
            // Interactively opens data and results files.  Then interprets the codeLen
            // instructions stored in mem, with stack pointer initialized to initSP
            Console.Write("\nTrace execution (y/N/q)? ");
            char reply = (Console.ReadLine() + " ").ToUpper()[0];
            bool traceStack = false, traceHeap = false;

            if (reply != 'Q')
            {
                bool tracing = reply == 'Y';
                if (tracing)
                {
                    Console.Write("\nTrace Stack (y/N)? ");
                    traceStack = (Console.ReadLine() + " ").ToUpper()[0] == 'Y';
                    Console.Write("\nTrace Heap (y/N)? ");
                    traceHeap = (Console.ReadLine() + " ").ToUpper()[0] == 'Y';
                }
                Console.Write("\nData file [STDIN] ? ");
                InFile data = new InFile(Console.ReadLine());
                Console.Write("\nResults file [STDOUT] ? ");
                OutFile results = new OutFile(Console.ReadLine());
                Emulator(0, codeLen, initSP, data, results, tracing, traceStack, traceHeap);
                results.Close();
                data.Close();
            }
        } // PVM.Interpret
Ejemplo n.º 3
0
        } // PVM.HeapDump

        static void Trace(OutFile results, int pcNow)
        {
            // Simple trace facility for run time debugging
            results.Write(" PC:"); results.Write(pcNow, 5);
            results.Write(" FP:"); results.Write(cpu.fp, 5);
            results.Write(" SP:"); results.Write(cpu.sp, 5);
            results.Write(" HP:"); results.Write(cpu.hp, 5);
            results.Write(" TOS:");
            if (cpu.sp < memSize)
            {
                results.Write(mem[cpu.sp], 5);
            }
            else
            {
                results.Write(" ????");
            }
            results.Write("  " + mnemonics[cpu.ir], -8);
            switch (cpu.ir)
            {
            case PVM.brn:
            case PVM.bze:
            case PVM.dsp:
            case PVM.lda:
            case PVM.ldc:
            case PVM.ldl:  //++
            case PVM.stl:  //++
            case PVM.stlc: //++
            case PVM.prns:
                results.Write(mem[cpu.pc], 7); break;

            default: break;
            }
            results.WriteLine();
        } // PVM.Trace
Ejemplo n.º 4
0
/*
 * PRODUCTIONS
 * Cdecls = { DecList } EOF .
 * DecList = Type OneDecl { "," OneDecl } ";" .
 * Type = "int" | "void" | "bool" | "char" .
 * OneDecl = "*" OneDecl | Direct .
 * Direct = ( ident | "(" OneDecl ")" ) [ Suffix ] .
 * Suffix = Array { Array } | Params .
 * Params = "(" [ OneParam { "," OneParam } ] ")" .
 * OneParam = Type [ OneDecl ] .
 * Array = "[" [ number ] "]" .
 * END Cdecls.
 */

/*  ++++++ */

    // +++++++++++++++++++++ Main driver function +++++++++++++++++++++++++++++++

    public static void Main(string[] args)
    {
        // Open input and output files from command line arguments
        if (args.Length == 0)
        {
            Console.WriteLine("Usage: Declarations FileName");
            System.Environment.Exit(1);
        }
        input  = new InFile(args[0]);
        output = new OutFile(NewFileName(args[0], ".out"));

        GetChar();                                // Lookahead character

        //  To test the scanner we can use a loop like the following:

        /*
         *  do {
         *    GetSym();                                 // Lookahead symbol
         *    OutFile.StdOut.Write(sym.kind, 3);
         *    OutFile.StdOut.WriteLine(" " + sym.val);  // See what we got
         *  } while (sym.kind != EOFSym);
         */
        /*  After the scanner is debugged we shall substitute this code: */

        GetSym();                                 // Lookahead symbol
        CDecls();                                 // Start to parse from the goal symbol
        // if we get back here everything must have been satisfactory
        Console.WriteLine("Parsed correctly");

        output.Close();
    } // Main
Ejemplo n.º 5
0
    public static void Main(string[] args)
    {
        //                                        check that arguments have been supplied
        if (args.Length != 2)
        {
            Console.WriteLine("missing args");
            System.Environment.Exit(1);
        }
        //                                        attempt to open data file
        InFile data = new InFile(args[0]);

        if (data.OpenError())
        {
            Console.WriteLine("cannot open " + args[0]);
            System.Environment.Exit(1);
        }
        //                                        attempt to open results file
        OutFile results = new OutFile(args[1]);

        if (results.OpenError())
        {
            Console.WriteLine("cannot open " + args[1]);
            System.Environment.Exit(1);
        }
        //                                        various initializations
        int    total       = 0;
        IntSet mySet       = new IntSet();
        IntSet smallSet    = new IntSet(1, 2, 3, 4, 5);
        string smallSetStr = smallSet.ToString();
        //                                        read and process data file
        int item = data.ReadInt();

        while (!data.NoMoreData())
        {
            total = total + item;
            if (item > 0)
            {
                mySet.Incl(item);
            }
            item = data.ReadInt();
        }
        //                                        write various results to output file
        results.Write("total = ");
        results.WriteLine(total, 5);
        results.WriteLine("unique positive numbers " + mySet.ToString());
        results.WriteLine("union with " + smallSetStr
                          + " = " + mySet.Union(smallSet).ToString());
        results.WriteLine("intersection with " + smallSetStr
                          + " = " + mySet.Intersection(smallSet).ToString());

        /* or simply
         * results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet));
         * results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet));
         */

        results.Close();
    } // Main
Ejemplo n.º 6
0
        } // Types.Name

        public static void Show(OutFile lst)
        {
            // For use in debugging
            foreach (string s in typeNames)
            {
                lst.Write(s + " ");
            }
            lst.WriteLine();
        } // Types.Show
Ejemplo n.º 7
0
    } // PVM.Emulator

    public static void QuickInterpret(int codeLen, int initSP) {
    // Interprets the codeLen instructions stored in mem, with stack pointer
    // initialized to initSP.  Use StdIn and StdOut without asking
      Console.WriteLine("\nHit <Enter> to start");
      Console.ReadLine();
      bool tracing = false;
      InFile data = new InFile("");
      OutFile results = new OutFile("");
      Emulator(0, codeLen, initSP, data, results, false, false, false);
    } // PVM.QuickInterpret
Ejemplo n.º 8
0
    } // FindOffset

    public static void ListReferences(OutFile output) {
    // Cross reference list of all variables on output file
      IO.WriteLine("\nVariables:\n");
      for (int i = 0; i < list.Count; i++){
        string reflist = list[i].name;
        reflist += "  - OFFSET ";
        foreach (int r in list[i].refs)
            reflist += " " + r;
        IO.WriteLine(reflist);
      }
    } // ListReferences
Ejemplo n.º 9
0
        public void ShouldReturn3SellersWhenParse3Sellers()
        {
            const string sourcePath = "C:\\test.dat";
            var          content    = "001ç1234567891234çPedroç50000 001ç3245678865434çPauloç40000.99 001ç3445678865434çJoseç60000.99" + Environment.NewLine +
                                      "002ç2345675434544345çJose da SilvaçRural 002ç2345675433444345çEduardo PereiraçRural" + Environment.NewLine +
                                      "003ç10ç[1-10-100,2-30-2.50,3-40-3.10]çPedro 003ç08ç[1-34-10,2-33-1.50,3-40-0.10]çPaulo 003ç08ç[1-1-1000]çJose";

            var inFile = new InFile(Guid.NewGuid(), sourcePath, content);
            var sut    = new OutFile(Guid.NewGuid(), inFile);

            Assert.Equal(3, sut.SellersCount);
        }
Ejemplo n.º 10
0
    } // CheckLabels

    public static void ListReferences(OutFile output) {
    // Cross reference list of all labels used on output file
      IO.WriteLine("Labels:\n");
      for (int i = 0; i < list.Count; i++){
        string reflist = list[i].name;
        if (list[i].label.IsDefined())
          reflist += "  (DEFINED) ";
        foreach (int r in list[i].refs)
            reflist += " " + r;
        IO.WriteLine(reflist);
      }
    } // ListReferences
Ejemplo n.º 11
0
 static void StackDump(OutFile results, int pcNow) {
 // Dump local variable and stack area - useful for debugging
   int onLine = 0;
   results.Write("\nStack dump at " + pcNow);
   results.Write(" FP:"); results.Write(cpu.fp, 4);
   results.Write(" SP:"); results.WriteLine(cpu.sp, 4);
   for (int i = stackBase - 1; i >= cpu.sp; i--) {
     results.Write(i, 7); results.Write(mem[i], 5);
     onLine++; if (onLine % 8 == 0) results.WriteLine();
   }
   results.WriteLine();
 } // PVM.StackDump
Ejemplo n.º 12
0
    } // PVM.StackDump

    static void HeapDump(OutFile results, int pcNow) {
    // Dump heap area - useful for debugging
      if (heapBase == cpu.hp)
        results.WriteLine("Empty Heap");
      else {
        int onLine = 0;
        results.Write("\nHeap dump at " + pcNow);
        results.Write(" HP:"); results.Write(cpu.hp, 4);
        results.Write(" HB:"); results.WriteLine(heapBase, 4);
        for (int i = heapBase; i < cpu.hp; i++) {
          results.Write(i, 7); results.Write(mem[i], 5);
          onLine++; if (onLine % 8 == 0) results.WriteLine();
        }
        results.WriteLine();
      }
    } // PVM.HeapDump
Ejemplo n.º 13
0
        } // PVM.HeapDump

        static void Trace(OutFile results, int pcNow, bool traceStack, bool traceHeap)
        {
            // Simple trace facility for run time debugging
            if (traceStack)
            {
                StackDump(results, pcNow);
            }
            if (traceHeap)
            {
                HeapDump(results, pcNow);
            }
            results.Write(" PC:"); results.Write(pcNow, 5);
            results.Write(" FP:"); results.Write(cpu.fp, 5);
            results.Write(" SP:"); results.Write(cpu.sp, 5);
            results.Write(" HP:"); results.Write(cpu.hp, 5);
            results.Write(" TOS:");
            if (cpu.sp < memSize)
            {
                results.Write(mem[cpu.sp], 5);
            }
            else
            {
                results.Write(" ????");
            }
            results.Write("  " + mnemonics[cpu.ir], -8);
            switch (cpu.ir) // two word opcodes
            {
            case PVM.call:
            case PVM.bfalse:
            case PVM.btrue:
            case PVM.brn:
            case PVM.bze:
            case PVM.dsp:
            case PVM.lda:
            case PVM.ldga:
            case PVM.ldc:
            case PVM.ldl:
            case PVM.stl:
            case PVM.stlc:
            case PVM.prns:
            case PVM.jal:
                results.Write(mem[cpu.pc], 7); break;

            default: break;
            }
            results.WriteLine();
        } // PVM.Trace
Ejemplo n.º 14
0
        public static void ProcessFiles()
        {
            var inFiles = new List <InFile>();

            // Caminhos
            var homeDir    = Environment.GetEnvironmentVariable("HOMEPATH");
            var inPath     = $"{homeDir}\\data\\in";
            var inPathRead = $"{inPath}\\read";

            // Cria as pastas, caso não existam
            if (!Directory.Exists(inPath))
            {
                Directory.CreateDirectory(inPath);
            }

            if (!Directory.Exists(inPathRead))
            {
                Directory.CreateDirectory(inPathRead);
            }

            var files = new DirectoryInfo(inPath).GetFiles("*.dat");

            if (files.Length < 1)
            {
                Console.WriteLine($"{inPath} is empty.");
                return;
            }
            // Lista os arquivos
            foreach (var file in new DirectoryInfo(inPath).GetFiles("*.dat"))
            {
                // Cria um novo objeto com o arquivo e conteúdo
                var inFile = new InFile(Guid.NewGuid(),
                                        file.FullName,
                                        File.ReadAllText(file.FullName));

                inFiles.Add(inFile);
                file.MoveTo($"{inPathRead}\\{file.Name}");

                var outFile = new OutFile(Guid.NewGuid(), inFile);
                var outPath = $"{homeDir}\\data\\out";

                // Criar o arquivo de saída
                File.WriteAllText($"{outPath}\\" + file.Name.Replace(".dat", ".done.dat"), outFile.Out);
            }

            WriteInConsole(inFiles);
        }
Ejemplo n.º 15
0
        protected override void Execute(CodeActivityContext context)
        {
            var inFile  = InFile.Get(context);
            var outFile = OutFile.Get(context);

            Xdwapi.XDW_DOCUMENT_HANDLE documentHandle = new Xdwapi.XDW_DOCUMENT_HANDLE();
            Xdwapi.XDW_OPEN_MODE_EX    mode           = new Xdwapi.XDW_OPEN_MODE_EX();
            mode.Option   = Xdwapi.XDW_OPEN_UPDATE;
            mode.AuthMode = Xdwapi.XDW_AUTH_NODIALOGUE;
            //api_result =
            Xdwapi.XDW_OpenDocumentHandle(outFile, ref documentHandle, mode);
            Xdwapi.XDW_DeletePage(documentHandle, 1);

            // inputPath
            Xdwapi.XDW_InsertDocument(documentHandle, 1, inFile);
            Xdwapi.XDW_SaveDocument(documentHandle);
            Xdwapi.XDW_CloseDocumentHandle(documentHandle);
        }
Ejemplo n.º 16
0
        public Task Consume(ConsumeContext <IFileProcess1Finished> context)
        {
            Console.WriteLine($"Have new message. Process started.");
            Console.WriteLine("context.Message.CompanyId " + context.Message.CompanyId);
            Console.WriteLine("context.Message.CompanyId " + context.Message.FileID);
            OutFile outFile = outFileRepository.GetById(context.Message.FileID);

            outFile.State = (int)FileState.Delivered;
            outFileRepository.Update(outFile);
            OutFileDocumentModel outFileDocument = outFileDocumentRepository.Get(context.Message.FileID);
            var outFileBytes = outFileDocumentRepository.GetFile(outFileDocument.FileId);

            Console.WriteLine("File Size : " + outFileBytes.Length);
            CompanyAccount companyAccount = companyAccountRepository.GetById(Convert.ToInt32(context.Message.CompanyId));

            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("FileProcess3 İşlemi Tamamlandı");
            return(Task.CompletedTask);
        }
Ejemplo n.º 17
0
        } // Table.Truncate

        public static void PrintTable(OutFile lst)
        {
            // Prints symbol table for diagnostic/debugging purposes
            lst.WriteLine();
            lst.WriteLine("Symbol table");
            Scope scope = topScope;

            while (scope != null)
            {
                Entry entry = scope.firstInScope;
                while (entry != sentinelEntry)
                {
                    lst.Write(Truncate(entry.name) + " ");
                    lst.Write(entry.level, -3);
                    lst.Write(Truncate(Types.Name(entry.type)));
                    switch (entry.kind)
                    {
                    case Kinds.Con:
                        lst.Write(" Constant  ");
                        lst.WriteLine(entry.value, 0);
                        break;

                    case Kinds.Var:
                        lst.Write(" Variable ");
                        lst.Write(entry.offset, 3);
                        lst.WriteLine();
                        break;

                    case Kinds.Fun:
                        lst.Write(" Function ");
                        lst.Write(entry.entryPoint, 3);
                        lst.WriteLine(entry.nParams, 3);
                        break;
                    }
                    entry = entry.nextInScope;
                }
                scope = scope.outer;
            }
            lst.WriteLine();
        } // Table.PrintTable
Ejemplo n.º 18
0
        public static void WriteInConsole(List <InFile> inFiles)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Files: {inFiles.Count}\n");

            foreach (var inFile in inFiles)
            {
                Console.WriteLine($"Id: {inFile.Id}");
                Console.WriteLine($"SourcePath: {inFile.SourcePath}");
                Console.WriteLine($"Content: {inFile.Content}\n");

                foreach (var salesman in inFile.Sellers)
                {
                    Console.WriteLine($"Salesman: {salesman.Id}, {salesman.Name}, {salesman.Salary}");
                }

                foreach (var customer in inFile.Customers)
                {
                    Console.WriteLine($"Customer: {customer.Id}, {customer.Name}, {customer.BusinessArea}");
                }

                foreach (var sale in inFile.Sales)
                {
                    Console.WriteLine($"\nSale: {sale.Id}, {sale.SaleId}, {sale.SalesmanId}, {sale.Total}");
                    foreach (var saleItem in sale.SaleItems)
                    {
                        Console.WriteLine($"    Item: {saleItem.Id}, {saleItem.ProductId}, {saleItem.Quantity}, {saleItem.Price}");
                    }
                }

                var outFile = new OutFile(Guid.NewGuid(), inFile);
                Console.WriteLine();
                Console.WriteLine(outFile.Out);
            }

            Console.ResetColor();
        }
Ejemplo n.º 19
0
    } // PVM.InBounds

    public static void Emulator(int initPC, int codeLen, int initSP,
                                InFile data, OutFile results, bool tracing,
                                bool traceStack, bool traceHeap) {
    // Emulates action of the codeLen instructions stored in mem[0 .. codeLen-1], with
    // program counter initialized to initPC, stack pointer initialized to initSP.
    // data and results are used for I/O.  Tracing at the code level may be requested

      int pcNow;                  // current program counter
      int loop;                   // internal loops
      int tos, sos;               // values popped from stack
      int adr;                    // effective address for memory accesses
      int target;                 // destination for branches

      stackBase = initSP;
      heapBase = codeLen;         // initialize boundaries
      cpu.hp = heapBase;          // initialize registers
      cpu.sp = stackBase;
      cpu.gp = stackBase;
      cpu.mp = stackBase;
      cpu.fp = stackBase;
      cpu.pc = initPC;            // initialize program counter
      for (int i = heapBase; i < stackBase; i++)
        mem[i] = 0;               // set entire memory to null or 0

      ps = running;               // prepare to execute
      int ops = 0;
      timer.Start();
      do {
        ops++;
        pcNow = cpu.pc;           // retain for tracing/postmortem
        if (cpu.pc < 0 || cpu.pc >= codeLen) {
          ps = badAdr;
          break;
        }
        cpu.ir = Next();          // fetch
        if (tracing) Trace(results, pcNow, traceStack, traceHeap);
        switch (cpu.ir) {         // execute
          case PVM.nop:           // no operation
            break;
          case PVM.dsp:           // decrement stack pointer (allocate space for variables)
            int localSpace = Next();
            cpu.sp -= localSpace;
            if (InBounds(cpu.sp)) // initialize all local variables to zero/null
              for (loop = 0; loop < localSpace; loop++)
                mem[cpu.sp + loop] = 0;
            break;
          case PVM.ldc:           // push constant value
            Push(Next());
            break;
          case PVM.ldc_m1:        // push constant -1
            Push(-1);
            break;
          case PVM.ldc_0:         // push constant 0
            Push(0);
            break;
          case PVM.ldc_1:         // push constant 1
            Push(1);
            break;
          case PVM.ldc_2:         // push constant 2
            Push(2);
            break;
          case PVM.ldc_3:         // push constant 3
            Push(3);
            break;
          case PVM.ldc_4:         // push constant 4
            Push(4);
            break;
          case PVM.ldc_5:         // push constant 5
            Push(5);
            break;
          case PVM.dup:           // duplicate top of stack
            tos = Pop();
            Push(tos); Push(tos);
            break;
          case PVM.lda:           // push local address
            adr = cpu.fp - 1 - Next();
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_0:         // push local address 0
            adr = cpu.fp - 1;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_1:         // push local address 1
            adr = cpu.fp - 2;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_2:         // push local address 2
            adr = cpu.fp - 3;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_3:         // push local address 3
            adr = cpu.fp - 4;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_4:         // push local address 4
            adr = cpu.fp - 5;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.lda_5:         // push local address 5
            adr = cpu.fp - 6;
            if (InBounds(adr)) Push(adr);
            break;
          case PVM.ldl:           // push local value
            adr = cpu.fp - 1 - Next();
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_0:         // push value of local variable 0
            adr = cpu.fp - 1;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_1:         // push value of local variable 1
            adr = cpu.fp - 2;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_2:         // push value of local variable 2
            adr = cpu.fp - 3;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_3:         // push value of local variable 3
            adr = cpu.fp - 4;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_4:         // push value of local variable 4
            adr = cpu.fp - 5;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.ldl_5:         // push value of local variable 5
            adr = cpu.fp - 6;
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.stl:           // store local value
            adr = cpu.fp - 1 - Next();
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stlc:          // character checked pop to local variable
            tos = Pop(); adr = cpu.fp - 1 - Next();
            if (InBounds(adr))
              if (tos >= 0 && tos <= maxChar) mem[adr] = tos;
              else ps = badVal;
            break;
          case PVM.stl_0:         // pop to local variable 0
            adr = cpu.fp - 1;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stl_1:         // pop to local variable 1
            adr = cpu.fp - 2;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stl_2:         // pop to local variable 2
            adr = cpu.fp - 3;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stl_3:         // pop to local variable 3
            adr = cpu.fp - 4;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stl_4:         // pop to local variable 4
            adr = cpu.fp - 5;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.stl_5:         // pop to local variable 5
            adr = cpu.fp - 6;
            if (InBounds(adr)) mem[adr] = Pop();
            break;
          case PVM.ldv:           // dereference
            adr = Pop();
            if (InBounds(adr)) Push(mem[adr]);
            break;
          case PVM.sto:           // store
            tos = Pop(); adr = Pop();
            if (InBounds(adr)) mem[adr] = tos;
            break;
          case PVM.stoc:          // character checked store
            tos = Pop(); adr = Pop();
            if (InBounds(adr))
              if (tos >= 0 && tos <= maxChar) mem[adr] = tos;
              else ps = badVal;
            break;
          case PVM.ldxa:          // heap array indexing
            adr = Pop();
            int heapPtr = Pop();
            if (heapPtr == 0) ps = nullRef;
            else if (heapPtr < heapBase || heapPtr >= cpu.hp) ps = badMem;
            else if (adr < 0 || adr >= mem[heapPtr]) ps = badInd;
            else Push(heapPtr + adr + 1); 
            break;
          case PVM.inpi:          // integer input
            adr = Pop();
            if (InBounds(adr)) {
              mem[adr] = data.ReadInt();
              if (data.Error()) ps = badData;
            }
            break;
          case PVM.inpb:          // boolean input
            adr = Pop();
            if (InBounds(adr)) {
              mem[adr] = data.ReadBool() ? 1 : 0;
              if (data.Error()) ps = badData;
            }
            break;
          case PVM.inpc:          // character input
            adr = Pop();
            if (InBounds(adr)) {
              mem[adr] = data.ReadChar();
              if (data.Error()) ps = badData;
            }
            break;
          case PVM.inpl:          // skip to end of input line
            data.ReadLine();
            break;
          case PVM.i2c:           // check (char) cast is in range
            if (mem[cpu.sp] < 0 || mem[cpu.sp] > maxChar) ps = badVal;
            break;
          case PVM.prni:          // integer output
            if (tracing) results.Write(padding);
            results.Write(Pop(), 0);
            if (tracing) results.WriteLine();
            break;
          case PVM.prnb:          // boolean output
            if (tracing) results.Write(padding);
            if (Pop() != 0) results.Write(" true  "); else results.Write(" false ");
            if (tracing) results.WriteLine();
            break;
          case PVM.prnc:          // character output
            if (tracing) results.Write(padding);
            results.Write((char) (Math.Abs(Pop()) % (maxChar + 1)), 1);
            if (tracing) results.WriteLine();
            break;
          case PVM.prns:          // string output
            if (tracing) results.Write(padding);
            loop = Next();
            while (ps == running && mem[loop] != 0) {
              results.Write((char) mem[loop]); loop--;
              if (loop < stackBase) ps = badMem;
            }
            if (tracing) results.WriteLine();
            break;
          case PVM.prnl:          // newline
            results.WriteLine();
            break;
          case PVM.neg:           // integer negation
            Push(-Pop());
            break;
          case PVM.add:           // integer addition
            tos = Pop(); Push(Pop() + tos);
            break;
          case PVM.sub:           // integer subtraction
            tos = Pop(); Push(Pop() - tos);
            break;
          case PVM.mul:           // integer multiplication
            tos = Pop();
            sos = Pop();
            if (tos != 0 && Math.Abs(sos) > maxInt / Math.Abs(tos)) ps = badVal;
            else Push(sos * tos);
            break;
          case PVM.div:           // integer division (quotient)
            tos = Pop();
            if (tos == 0) ps = divZero;
            else Push(Pop() / tos);
            break;
          case PVM.rem:           // integer division (remainder)
            tos = Pop();
            if (tos == 0) ps = divZero;
            else Push(Pop() % tos);
            break;
          case PVM.not:           // logical negation
            Push(Pop() == 0 ? 1 : 0);
            break;
          case PVM.and:           // logical and
            tos = Pop(); Push(Pop() & tos);
            break;
          case PVM.or:            // logical or
            tos = Pop(); Push(Pop() | tos);
            break;
          case PVM.ceq:           // logical equality
            tos = Pop(); Push(Pop() == tos ? 1 : 0);
            break;
          case PVM.cne:           // logical inequality
            tos = Pop(); Push(Pop() != tos ? 1 : 0);
            break;
          case PVM.clt:           // logical less
            tos = Pop(); Push(Pop() <  tos ? 1 : 0);
            break;
          case PVM.cle:           // logical less or equal
            tos = Pop(); Push(Pop() <= tos ? 1 : 0);
            break;
          case PVM.cgt:           // logical greater
            tos = Pop(); Push(Pop() >  tos ? 1 : 0);
            break;
          case PVM.cge:           // logical greater or equal
            tos = Pop(); Push(Pop() >= tos ? 1 : 0);
            break;
          case PVM.brn:           // unconditional branch
            cpu.pc = Next();
            if (cpu.pc < 0 || cpu.pc >= codeLen) ps = badAdr;
            break;
          case PVM.bze:           // pop top of stack, branch if false
            target = Next();
            if (Pop() == 0) {
              cpu.pc = target;
              if (cpu.pc < 0 || cpu.pc >= codeLen) ps = badAdr;
            }
            break;
          case PVM.bfalse:        // conditional short circuit "and" branch
            target = Next();
            if (mem[cpu.sp] == 0) cpu.pc = target; else cpu.sp++;
            break;
          case PVM.btrue:         // conditional short circuit "or" branch
            target = Next();
            if (mem[cpu.sp] == 1) cpu.pc = target; else cpu.sp++;
            break;
          case PVM.anew:          // heap array allocation
            int size = Pop();
            if (size <= 0 || size + 1 > cpu.sp - cpu.hp - 2)
              ps = badAll;
            else {
              mem[cpu.hp] = size; // first element stores size for bounds checking
              Push(cpu.hp);
              cpu.hp += size + 1; // bump heap pointer
                                  // elements are already initialized to 0 / null (why?)
            }
            break;
          case PVM.halt:          // halt
            ps = finished;
            break;
          case PVM.inc:           // integer ++
            adr = Pop();
            if (InBounds(adr)) mem[adr]++;
            break;
          case PVM.dec:           // integer --
            adr = Pop();
            if (InBounds(adr)) mem[adr]--;
            break;
          case PVM.incc:          // ++ characters
            adr = Pop();
            if (InBounds(adr))
              if (mem[adr] < maxChar) mem[adr]++;
              else ps = badVal;
            break;
          case PVM.decc:          // -- characters
            adr = Pop();
            if (InBounds(adr))
              if (mem[adr] > 0) mem[adr]--;
              else ps = badVal;
            break;
          case PVM.stack:         // stack dump (debugging)
            StackDump(results, pcNow);
            break;
          case PVM.heap:          // heap dump (debugging)
            HeapDump(results, pcNow);
            break;
          case PVM.fhdr:          // allocate frame header
            if (InBounds(cpu.sp - headerSize)) {
              mem[cpu.sp - headerSize] = cpu.mp;
              cpu.mp = cpu.sp;
              cpu.sp -= headerSize;
            }
            break;
          case PVM.call:          // call function
            if (mem[cpu.pc] < 0) ps = badAdr;
            else {
              mem[cpu.mp - 2] = cpu.fp;
              mem[cpu.mp - 3] = cpu.pc + 1;
              cpu.fp = cpu.mp;
              cpu.pc = mem[cpu.pc];
            }
            break;
          case PVM.retv:          // return from void function
            cpu.sp = cpu.fp;
            cpu.mp = mem[cpu.fp - 4];
            cpu.pc = mem[cpu.fp - 3];
            cpu.fp = mem[cpu.fp - 2];
            break;
		  case PVM.max:
			tos = Pop();
			sos = Pop();
			if (tos > sos) Push(tos);				
			else Push(sos);
		    break;
		  case PVM.min:
			tos = Pop();
			sos = Pop();
			if (tos < sos) Push(tos);				
			else Push(sos);
		    break;
		  case PVM.sqr:
			tos = Pop();
			Push((int)Math.Sqrt(tos));
		    break;
		  case PVM.aceq:
		    tos =Pop(); //address of array 1
			sos = Pop(); //address of array 1
			int len_1 = mem[tos];
			int len_2 = mem[sos];
			if(len_1!=len_2){ 
				Push(0); // if lengths aren't equal then arrays can't be equal
			}
			else{
				int i=0;
				while(i<len_1 && mem[tos+1+i] == mem[sos+1+i]){
					i++;
				}
				if(i==len_1) 
					Push(1);
				else
					Push(0);	  		
			}
			break;
			case PVM.acpy:
			    tos =Pop(); //address to be copied
			    sos = Pop(); //address of array to be assigned 
				if(mem[tos]==mem[sos]){ //if the lengths are not the same leave it
				  int i=0;
				  while(i<mem[tos]){
					  mem[sos+1+i] = mem[tos+1+i];
				  }
				
				}
				/*
				else{
					IO.WriteLine("Arrays must be the same size to be copied!");
				    ps=badVal;
				}
				*/
			break;
			case PVM.wprni:          // integer output with wdith
				if (tracing) results.Write(padding);
				tos=Pop();
				results.Write(Pop(),tos);
				if (tracing) results.WriteLine();
            break;
          case PVM.wprnb:          // boolean output with width
            if (tracing) results.Write(padding);
			tos = Pop();
            if (Pop() != 0) results.Write("true",tos); else results.Write("false",tos);
            if (tracing) results.WriteLine();
            break;
          case PVM.wprnc:          // character output with width
            if (tracing) results.Write(padding);
			tos = Pop();
            results.Write((char) (Math.Abs(Pop()) % (maxChar + 1)), tos);
            if (tracing) results.WriteLine();
			break;
          default:                // unrecognized opcode
            ps = badOp;
            break;
        } // switch(cpu.ir)
      } while (ps == running);
      if (ps != finished) PostMortem(results, pcNow);
      TimeSpan ts = timer.Elapsed;
      string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                         ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
      Console.WriteLine("\n" + ops + " operations.  Run Time " + elapsedTime);
      timer.Reset();
      timer.Stop();
    } // PVM.Emulator
Ejemplo n.º 20
0
        } // PVM.InBounds

        public static void Emulator(int initPC, int codeLen, int initSP,
                                    InFile data, OutFile results, bool tracing,
                                    bool traceStack, bool traceHeap)
        {
            // Emulates action of the codeLen instructions stored in mem[0 .. codeLen-1], with
            // program counter initialized to initPC, stack pointer initialized to initSP.
            // data and results are used for I/O.  Tracing at the code level may be requested

            int pcNow;            // current program counter
            int loop;             // internal loops
            int tos, sos;         // values popped from stack
            int adr;              // effective address for memory accesses
            int target;           // destination for branches

            stackBase = initSP;
            heapBase  = codeLen;  // initialize boundaries
            cpu.hp    = heapBase; // initialize registers
            cpu.sp    = stackBase;
            cpu.gp    = stackBase;
            cpu.mp    = stackBase;
            cpu.fp    = stackBase;
            cpu.pc    = initPC;   // initialize program counter
            for (int i = heapBase; i < stackBase; i++)
            {
                mem[i] = 0;       // set entire memory to null or 0
            }
            ps = running;         // prepare to execute
            int ops = 0;

            timer.Start();
            do
            {
                ops++;
                pcNow = cpu.pc;   // retain for tracing/postmortem
                if (cpu.pc < 0 || cpu.pc >= codeLen)
                {
                    ps = badAdr;
                    break;
                }
                cpu.ir = Next();  // fetch
                if (tracing)
                {
                    Trace(results, pcNow, traceStack, traceHeap);
                }
                switch (cpu.ir)   // execute
                {
                case PVM.nop:     // no operation
                    break;

                case PVM.dsp:     // decrement stack pointer (allocate space for variables)
                    int localSpace = Next();
                    cpu.sp -= localSpace;
                    if (InBounds(cpu.sp)) // initialize all local variables to zero/null
                    {
                        for (loop = 0; loop < localSpace; loop++)
                        {
                            mem[cpu.sp + loop] = 0;
                        }
                    }
                    break;

                case PVM.ldc:     // push constant value
                    Push(Next());
                    break;

                case PVM.ldc_m1:  // push constant -1
                    Push(-1);
                    break;

                case PVM.ldc_0:   // push constant 0
                    Push(0);
                    break;

                case PVM.ldc_1:   // push constant 1
                    Push(1);
                    break;

                case PVM.ldc_2:   // push constant 2
                    Push(2);
                    break;

                case PVM.ldc_3:   // push constant 3
                    Push(3);
                    break;

                case PVM.ldc_4:   // push constant 4
                    Push(4);
                    break;

                case PVM.ldc_5:   // push constant 5
                    Push(5);
                    break;

                case PVM.dup:     // duplicate top of stack
                    tos = Pop();
                    Push(tos); Push(tos);
                    break;

                case PVM.rdup:    // Remove top of stack
                    tos = Pop();
                    break;

                case PVM.lda:     // push local address
                    adr = cpu.fp - 1 - Next();
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_0:   // push local address 0
                    adr = cpu.fp - 1;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_1:   // push local address 1
                    adr = cpu.fp - 2;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_2:   // push local address 2
                    adr = cpu.fp - 3;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_3:   // push local address 3
                    adr = cpu.fp - 4;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_4:   // push local address 4
                    adr = cpu.fp - 5;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.lda_5:   // push local address 5
                    adr = cpu.fp - 6;
                    if (InBounds(adr))
                    {
                        Push(adr);
                    }
                    break;

                case PVM.ldl:     // push local value
                    adr = cpu.fp - 1 - Next();
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_0:   // push value of local variable 0
                    adr = cpu.fp - 1;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_1:   // push value of local variable 1
                    adr = cpu.fp - 2;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_2:   // push value of local variable 2
                    adr = cpu.fp - 3;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_3:   // push value of local variable 3
                    adr = cpu.fp - 4;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_4:   // push value of local variable 4
                    adr = cpu.fp - 5;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.ldl_5:   // push value of local variable 5
                    adr = cpu.fp - 6;
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.stl:     // store local value
                    adr = cpu.fp - 1 - Next();
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stlc:    // character checked pop to local variable
                    tos = Pop(); adr = cpu.fp - 1 - Next();
                    if (InBounds(adr))
                    {
                        if (tos >= 0 && tos <= maxChar)
                        {
                            mem[adr] = tos;
                        }
                        else
                        {
                            ps = badVal;
                        }
                    }
                    break;

                case PVM.stl_0:   // pop to local variable 0
                    adr = cpu.fp - 1;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stl_1:   // pop to local variable 1
                    adr = cpu.fp - 2;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stl_2:   // pop to local variable 2
                    adr = cpu.fp - 3;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stl_3:   // pop to local variable 3
                    adr = cpu.fp - 4;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stl_4:   // pop to local variable 4
                    adr = cpu.fp - 5;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.stl_5:   // pop to local variable 5
                    adr = cpu.fp - 6;
                    if (InBounds(adr))
                    {
                        mem[adr] = Pop();
                    }
                    break;

                case PVM.ldv:     // dereference
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        Push(mem[adr]);
                    }
                    break;

                case PVM.sto:     // store
                    tos = Pop(); adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr] = tos;
                    }
                    break;

                case PVM.stoc:    // character checked store
                    tos = Pop(); adr = Pop();
                    if (InBounds(adr))
                    {
                        if (tos >= 0 && tos <= maxChar)
                        {
                            mem[adr] = tos;
                        }
                        else
                        {
                            ps = badVal;
                        }
                    }
                    break;

                case PVM.ldxa:    // heap array indexing
                    adr = Pop();
                    int heapPtr = Pop();
                    if (heapPtr == 0)
                    {
                        ps = nullRef;
                    }
                    else if (heapPtr < heapBase || heapPtr >= cpu.hp)
                    {
                        ps = badMem;
                    }
                    else if (adr < 0 || adr >= mem[heapPtr])
                    {
                        ps = badInd;
                    }
                    else
                    {
                        Push(heapPtr + adr + 1);
                    }
                    break;

                case PVM.inpi:    // integer input
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr] = data.ReadInt();
                        if (data.Error())
                        {
                            ps = badData;
                        }
                    }
                    break;

                case PVM.inpb:    // boolean input
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr] = data.ReadBool() ? 1 : 0;
                        if (data.Error())
                        {
                            ps = badData;
                        }
                    }
                    break;

                case PVM.inpc:    // character input
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr] = data.ReadChar();
                        if (data.Error())
                        {
                            ps = badData;
                        }
                    }
                    break;

                case PVM.inpl:    // skip to end of input line
                    data.ReadLine();
                    break;

                case PVM.i2c:     // check (char) cast is in range
                    if (mem[cpu.sp] < 0 || mem[cpu.sp] > maxChar)
                    {
                        ps = badVal;
                    }
                    break;

                case PVM.prni:    // integer output
                    if (tracing)
                    {
                        results.Write(padding);
                    }
                    results.Write(Pop(), 0);
                    if (tracing)
                    {
                        results.WriteLine();
                    }
                    break;

                case PVM.prnb:    // boolean output
                    if (tracing)
                    {
                        results.Write(padding);
                    }
                    if (Pop() != 0)
                    {
                        results.Write(" true  ");
                    }
                    else
                    {
                        results.Write(" false ");
                    }
                    if (tracing)
                    {
                        results.WriteLine();
                    }
                    break;

                case PVM.prnc:    // character output
                    if (tracing)
                    {
                        results.Write(padding);
                    }
                    results.Write((char)(Math.Abs(Pop()) % (maxChar + 1)), 1);
                    if (tracing)
                    {
                        results.WriteLine();
                    }
                    break;

                case PVM.prns:    // string output
                    if (tracing)
                    {
                        results.Write(padding);
                    }
                    loop = Next();
                    while (ps == running && mem[loop] != 0)
                    {
                        results.Write((char)mem[loop]); loop--;
                        if (loop < stackBase)
                        {
                            ps = badMem;
                        }
                    }
                    if (tracing)
                    {
                        results.WriteLine();
                    }
                    break;

                case PVM.fprns:    // string output
                    if (tracing)
                    {
                        results.Write(padding);
                    }
                    loop = Next();
                    StringBuilder str = new StringBuilder();
                    while (ps == running && mem[loop] != 0)
                    {
                        //results.Write((char) mem[loop]); loop--;
                        str.Append((char)mem[loop]); loop--;
                        if (loop < stackBase)
                        {
                            ps = badMem;
                        }
                    }
                    results.Write(str.ToString(), Pop());
                    if (tracing)
                    {
                        results.WriteLine();
                    }
                    break;

                case PVM.prnl:    // newline
                    results.WriteLine();
                    break;

                case PVM.fprint:
                    tos = Pop();
                    sos = Pop();
                    results.Write(sos, tos);
                    break;

                case PVM.neg:     // integer negation
                    Push(-Pop());
                    break;

                case PVM.add:     // integer addition
                    tos = Pop(); Push(Pop() + tos);
                    break;

                case PVM.sub:     // integer subtraction
                    tos = Pop(); Push(Pop() - tos);
                    break;

                case PVM.mul:     // integer multiplication
                    tos = Pop();
                    sos = Pop();
                    if (tos != 0 && Math.Abs(sos) > maxInt / Math.Abs(tos))
                    {
                        ps = badVal;
                    }
                    else
                    {
                        Push(sos * tos);
                    }
                    break;

                case PVM.div:     // integer division (quotient)
                    tos = Pop();
                    if (tos == 0)
                    {
                        ps = divZero;
                    }
                    else
                    {
                        Push(Pop() / tos);
                    }
                    break;

                case PVM.max:     // Finds max. First Pops elements off stack
                                  // to see how many elements.
                    int numElements = Pop();
                    if (numElements == 0)
                    {
                        ps = noData;
                    }
                    else
                    {
                        List <int> maxElements = new List <int> ();
                        for (int i = 0; i < numElements; i++)
                        {
                            maxElements.Add(Pop());
                        }
                        maxElements.Sort();
                        maxElements.Reverse();
                        Push(maxElements[0]);
                    }
                    break;

                case PVM.min:     // Finds min. First Pops elements off stack
                                  // to see how many elements
                    int numMin = Pop();
                    if (numMin == 0)
                    {
                        ps = noData;
                    }
                    else
                    {
                        List <int> minElements = new List <int> ();
                        for (int i = 0; i < numMin; i++)
                        {
                            minElements.Add(Pop());
                        }
                        minElements.Sort();
                        Push(minElements[0]);
                    }
                    break;

                case PVM.sqrt:     // Square root
                    tos = Pop();
                    Push(Convert.ToInt32(System.Math.Sqrt(tos)));
                    break;

                case PVM.rem:     // integer division (remainder)
                    tos = Pop();
                    if (tos == 0)
                    {
                        ps = divZero;
                    }
                    else
                    {
                        Push(Pop() % tos);
                    }
                    break;

                case PVM.not:     // logical negation
                    Push(Pop() == 0 ? 1 : 0);
                    break;

                case PVM.and:     // logical and
                    tos = Pop(); Push(Pop() & tos);
                    break;

                case PVM.or:      // logical or
                    tos = Pop(); Push(Pop() | tos);
                    break;

                case PVM.ceq:     // logical equality
                    tos = Pop(); Push(Pop() == tos ? 1 : 0);
                    break;

                case PVM.cne:     // logical inequality
                    tos = Pop(); Push(Pop() != tos ? 1 : 0);
                    break;

                case PVM.clt:     // logical less
                    tos = Pop(); Push(Pop() < tos ? 1 : 0);
                    break;

                case PVM.cle:     // logical less or equal
                    tos = Pop(); Push(Pop() <= tos ? 1 : 0);
                    break;

                case PVM.cgt:     // logical greater
                    tos = Pop(); Push(Pop() > tos ? 1 : 0);
                    break;

                case PVM.cge:     // logical greater or equal
                    tos = Pop(); Push(Pop() >= tos ? 1 : 0);
                    break;

                case PVM.brn:     // unconditional branch
                    cpu.pc = Next();
                    if (cpu.pc < 0 || cpu.pc >= codeLen)
                    {
                        ps = badAdr;
                    }
                    break;

                case PVM.bze:     // pop top of stack, branch if false
                    target = Next();
                    if (Pop() == 0)
                    {
                        cpu.pc = target;
                        if (cpu.pc < 0 || cpu.pc >= codeLen)
                        {
                            ps = badAdr;
                        }
                    }
                    break;

                case PVM.bfalse:  // conditional short circuit "and" branch
                    target = Next();
                    if (mem[cpu.sp] == 0)
                    {
                        cpu.pc = target;
                    }
                    else
                    {
                        cpu.sp++;
                    }
                    break;

                case PVM.btrue:   // conditional short circuit "or" branch
                    target = Next();
                    if (mem[cpu.sp] == 1)
                    {
                        cpu.pc = target;
                    }
                    else
                    {
                        cpu.sp++;
                    }
                    break;

                case PVM.anew:    // heap array allocation
                    int size = Pop();
                    if (size <= 0 || size + 1 > cpu.sp - cpu.hp - 2)
                    {
                        ps = badAll;
                    }
                    else
                    {
                        mem[cpu.hp] = size; // first element stores size for bounds checking
                        Push(cpu.hp);
                        cpu.hp += size + 1; // bump heap pointer
                                            // elements are already initialized to 0 / null (why?)
                    }
                    break;

                case PVM.halt:    // halt
                    ps = finished;
                    break;

                case PVM.inc:     // integer ++
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr]++;
                    }
                    break;

                case PVM.dec:     // integer --
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        mem[adr]--;
                    }
                    break;

                case PVM.incc:    // ++ characters
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        if (mem[adr] < maxChar)
                        {
                            mem[adr]++;
                        }
                        else
                        {
                            ps = badVal;
                        }
                    }
                    break;

                case PVM.decc:    // -- characters
                    adr = Pop();
                    if (InBounds(adr))
                    {
                        if (mem[adr] > 0)
                        {
                            mem[adr]--;
                        }
                        else
                        {
                            ps = badVal;
                        }
                    }
                    break;

                case cpy:

                    int addrC1       = Pop();
                    int addrC2       = Pop();
                    int addrLengthC1 = mem[mem[addrC1]];
                    int addrLengthC2 = mem[mem[addrC2]];
                    if (addrLengthC1 != addrLengthC2)
                    {
                        ps = badVal;
                    }
                    else
                    {
                        int i = 1;
                        while ((i <= addrLengthC1))
                        {
                            mem[mem[addrC2] + i] = mem[mem[addrC1] + i];
                            i++;
                        }
                    }
                    break;

                case eql:
                    int addr1       = Pop();
                    int addr2       = Pop();
                    int addrLength1 = mem[mem[addr1]];
                    int addrLength2 = mem[mem[addr2]];
                    if (addrLength1 != addrLength2)
                    {
                        Push(0); // push flase if lenghts arnt ==
                    }
                    else
                    {
                        bool isEqual = true;
                        int  i       = 1;
                        while (i < addrLength1 + 1)
                        {
                            if (mem[mem[addr1] + i] != mem[mem[addr2] + i])
                            {
                                isEqual = false;
                            }
                            i++;
                        }
                        if (isEqual)
                        {
                            Push(1);
                        }
                        else
                        {
                            Push(0);
                        }
                    }
                    break;

                case PVM.stack:   // stack dump (debugging)
                    StackDump(results, pcNow);
                    break;

                case PVM.heap:    // heap dump (debugging)
                    HeapDump(results, pcNow);
                    break;

                case PVM.fhdr:    // allocate frame header
                    if (InBounds(cpu.sp - headerSize))
                    {
                        mem[cpu.sp - headerSize] = cpu.mp;
                        cpu.mp  = cpu.sp;
                        cpu.sp -= headerSize;
                    }
                    break;

                case PVM.call:    // call function
                    if (mem[cpu.pc] < 0)
                    {
                        ps = badAdr;
                    }
                    else
                    {
                        mem[cpu.mp - 2] = cpu.fp;
                        mem[cpu.mp - 3] = cpu.pc + 1;
                        cpu.fp          = cpu.mp;
                        cpu.pc          = mem[cpu.pc];
                    }
                    break;

                case PVM.retv:    // return from void function
                    cpu.sp = cpu.fp;
                    cpu.mp = mem[cpu.fp - 4];
                    cpu.pc = mem[cpu.fp - 3];
                    cpu.fp = mem[cpu.fp - 2];
                    break;

                default:          // unrecognized opcode
                    ps = badOp;
                    break;
                } // switch(cpu.ir)
            } while (ps == running);
            if (ps != finished)
            {
                PostMortem(results, pcNow);
            }
            TimeSpan ts          = timer.Elapsed;
            string   elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                 ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

            Console.WriteLine("\n" + ops + " operations.  Run Time " + elapsedTime);
            timer.Reset();
            timer.Stop();
        } // PVM.Emulator
Ejemplo n.º 21
0
        } // PVM.Interpret

        public static void ListCode(string fileName, int codeLen)
        {
            // Lists the codeLen instructions stored in mem on a named output file
            int i, j;

            if (fileName == null)
            {
                return;
            }
            OutFile codeFile = new OutFile(fileName);

            /* ------------- The following may be useful for debugging the interpreter
             * i = 0;
             * while (i < codeLen) {
             * codeFile.Write(mem[i], 5);
             * if ((i + 1) % 15 == 0) codeFile.WriteLine();
             * i++;
             * }
             * codeFile.WriteLine();
             *
             * ------------- */

            i = 0;
            codeFile.WriteLine("ASSEM\nBEGIN");
            while (i < codeLen && mem[i] != PVM.nul)
            {
                int o = mem[i] % (PVM.nul + 1); // force in range
                codeFile.Write("  {");
                codeFile.Write(i, 5);
                codeFile.Write(" } ");
                codeFile.Write(mnemonics[o], -8);
                switch (o)              // two word opcodes
                {
                case PVM.call:
                case PVM.bfalse:
                case PVM.btrue:
                case PVM.brn:
                case PVM.bze:
                case PVM.dsp:
                case PVM.lda:
                case PVM.ldc:
                case PVM.ldl:
                case PVM.stl:
                case PVM.stlc:
                    i = (i + 1) % memSize; codeFile.Write(mem[i]);
                    break;

                case PVM.prns:          // special case
                    i = (i + 1) % memSize;
                    j = mem[i]; codeFile.Write(" \"");
                    while (mem[j] != 0)
                    {
                        switch (mem[j])
                        {
                        case '\\': codeFile.Write("\\\\"); break;

                        case '\"': codeFile.Write("\\\""); break;

                        case '\'': codeFile.Write("\\\'"); break;

                        case '\b': codeFile.Write("\\b");  break;

                        case '\t': codeFile.Write("\\t");  break;

                        case '\n': codeFile.Write("\\n");  break;

                        case '\f': codeFile.Write("\\f");  break;

                        case '\r': codeFile.Write("\\r");  break;

                        default: codeFile.Write((char)mem[j]); break;
                        }
                        j--;
                    }
                    codeFile.Write("\"");
                    break;
                } // switch
                i = (i + 1) % memSize;
                codeFile.WriteLine();
            } // while (i < codeLen)
            codeFile.WriteLine("END.");
            codeFile.Close();
        } // PVM.ListCode
Ejemplo n.º 22
0
        //----------------------------------------

        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.Default;
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); // For . instead , in Convert

            var IsNormalMode  = false;
            var IsRedirectOut = false;

            var    IsDiagnosticTime = false;
            var    watch            = new Stopwatch();
            string FileName         = "";
            string OutFileName      = "";

            for (var i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-script":
                    if (i + 1 < args.Length)
                    {
                        IsNormalMode = true;
                        FileName     = args[i++ + 1];
                        while (i + 1 < args.Length && args[i + 1][0] != '+' && args[i + 1][0] != '-')
                        {
                            i++;
                            FileName += " " + args[i];
                        }
                    }
                    break;

                case "-dtime":
                    IsDiagnosticTime = true;
                    break;

                case "-out":
                    if (i + 1 < args.Length)
                    {
                        IsRedirectOut = true;
                        OutFileName   = args[i++ + 1];
                        while (i + 1 < args.Length && args[i + 1][0] != '+' && args[i + 1][0] != '-')
                        {
                            i++;
                            OutFileName += " " + args[i];
                        }
                    }
                    break;
                }
            }

            StreamWriter OutFile = null;

            if (IsRedirectOut)
            {
                OutFile = new StreamWriter(OutFileName, false, Encoding.Default);
            }

            if (!IsNormalMode)
            {
                Console.WriteLine("--------------------------");
                Console.WriteLine("Віртуальна машина CEurope");
                Console.WriteLine("--------------------------");
                Console.WriteLine("Щоб запустити скрипт передайте в параметри:");
                Console.WriteLine("-script <шлях>");
                Console.ReadKey();
            }
            else
            {
                Interpreter.SendInfoLine("Виконую " + FileName, OutFile);
                if (File.Exists(FileName))
                {
                    using (var script_file = new StreamReader(FileName, Encoding.Default))
                    {
                        var script = script_file.ReadToEnd();
                        watch.Start();
                        Interpreter.Interpret(script, OutFile);
                        watch.Stop();
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Interpreter.SendInfoLine("Файл не знайдено", OutFile);
                    Console.ReadKey();
                    return;
                }
                if (IsDiagnosticTime)
                {
                    Interpreter.SendInfoLine(string.Format("Програма завершена за {0} ...", watch.Elapsed), OutFile);
                }
                else
                {
                    Interpreter.SendInfoLine("Програма завершена...", OutFile);
                }
            }
            if (!IsRedirectOut)
            {
                Console.ReadKey();
            }
            else
            {
                OutFile.Close();
            }
        }
Ejemplo n.º 23
0
 public static void LogError(string msg)
 {
     OutFile.WriteLine($"ERROR@{DateTime.Now}@{msg}");
     OutFile.Flush();
 }
Ejemplo n.º 24
0
 public static void LogInfo(string msg)
 {
     OutFile.WriteLine($"INFO@{DateTime.Now}@{msg}");
     OutFile.Flush();
 }
Ejemplo n.º 25
0
 public static void LogDebug(string msg)
 {
     OutFile.WriteLine($"DEBUG@{DateTime.Now}@{msg}");
     OutFile.Flush();
 }
Ejemplo n.º 26
0
        } // PVM.PostMortem

        // The interpreters and utility methods

        public static void Emulator(int initPC, int codeLen, int initSP,
                                    InFile data, OutFile results, bool tracing,
                                    bool traceStack, bool traceHeap)
        {
            // Emulates action of the codeLen instructions stored in mem[0 .. codeLen-1], with
            // program counter initialized to initPC, stack pointer initialized to initSP.
            // data and results are used for I/O.  Tracing at the code level may be requested

            int pcNow;            // current program counter
            int loop;             // internal loops
            int tos, sos;         // value popped from stack

            stackBase = initSP;
            heapBase  = codeLen;  // initialize boundaries
            cpu.hp    = heapBase; // initialize registers
            cpu.sp    = stackBase;
            cpu.gp    = stackBase;
            cpu.mp    = stackBase;
            cpu.fp    = stackBase;
            cpu.pc    = initPC;   // initialize program counter
            ps        = running;  // prepare to execute
            int ops = 0;

            timer.Start();
            do
            {
                ops++;
                pcNow  = cpu.pc;        // retain for tracing/postmortem
                cpu.ir = mem[cpu.pc++]; // fetch
//        if (tracing) Trace(results, pcNow, traceStack, traceHeap);
                switch (cpu.ir)         // execute
                {
                case PVM.nop:           // no operation
                    break;

                case PVM.dsp:     // decrement stack pointer (allocate space for variables)
                    cpu.sp -= mem[cpu.pc++];
                    break;

                case PVM.ldc:     // push constant value
                    mem[--cpu.sp] = mem[cpu.pc++];
                    break;

                case PVM.lda:     // push local address
                    mem[--cpu.sp] = cpu.fp - 1 - mem[cpu.pc++];
                    break;

                case PVM.ldv:     // dereference
                    mem[cpu.sp] = mem[mem[cpu.sp]];
                    break;

                case PVM.sto:     // store
                    tos = mem[cpu.sp++]; mem[mem[cpu.sp++]] = tos;
                    break;

                case PVM.ldxa:    // heap array indexing
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] + tos;
                    break;

                case PVM.inpi:    // integer input
                    mem[mem[cpu.sp++]] = data.ReadInt();
                    break;

                case PVM.inpc:    // char input
                    mem[mem[cpu.sp++]] = data.ReadChar();
                    break;

                case PVM.prni:    // integer output
//            if (tracing) results.Write(padding);
                    results.Write(mem[cpu.sp++], 0);
//            if (tracing) results.WriteLine();
                    break;

                case PVM.prnc:    // integer output
                    results.Write((char)mem[cpu.sp++], 0);
                    break;

                case PVM.inpb:    // boolean input
                    mem[mem[cpu.sp++]] = data.ReadBool() ? 1 : 0;
                    break;

                case PVM.prnb:    // boolean output
//            if (tracing) results.Write(padding);
                    if (mem[cpu.sp++] != 0)
                    {
                        results.Write(" true  ");
                    }
                    else
                    {
                        results.Write(" false ");
                    }
//            if (tracing) results.WriteLine();
                    break;

                case PVM.prns:    // string output
//            if (tracing) results.Write(padding);
                    loop = mem[cpu.pc++];
                    while (mem[loop] != 0)
                    {
                        results.Write((char)mem[loop]); loop--;
                    }
//            if (tracing) results.WriteLine();
                    break;

                case PVM.prnl:    // newline
                    results.WriteLine();
                    break;

                case PVM.neg:     // integer negation
                    mem[cpu.sp] = -mem[cpu.sp];
                    break;

                case PVM.add:     // integer addition
                    tos = mem[cpu.sp++]; mem[cpu.sp] += tos;
                    break;

                case PVM.sub:     // integer subtraction
                    tos = mem[cpu.sp++]; mem[cpu.sp] -= tos;
                    break;

                case PVM.mul:
                    tos = mem[cpu.sp++];
                    sos = mem[cpu.sp];   // integer multiplication
                    int freeSpace = (memSize - cpu.hp - (memSize - cpu.sp));
                    if ((freeSpace / tos) > sos)
                    {
                        mem[cpu.sp] *= tos;
                    }
                    else
                    {
                        ps = badVal;
                    }
                    break;

                case PVM.div:     // integer division (quotient)
                    tos = mem[cpu.sp++];
                    if (tos == 0)
                    {
                        ps = divZero;
                    }
                    else
                    {
                        mem[cpu.sp] /= tos;
                    }
                    break;

                case PVM.rem:     // integer division (remainder)
                    tos = mem[cpu.sp++]; mem[cpu.sp] %= tos;
                    break;

                case PVM.not:     // logical negation
                    mem[cpu.sp] = mem[cpu.sp] == 0 ? 1 : 0;
                    break;

                case PVM.and:     // logical and
                    tos = mem[cpu.sp++]; mem[cpu.sp] &= tos;
                    break;

                case PVM.or:      // logical or
                    tos = mem[cpu.sp++]; mem[cpu.sp] |= tos;
                    break;

                case PVM.ceq:     // logical equality
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] == tos ? 1 : 0;
                    break;

                case PVM.cne:     // logical inequality
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] != tos ? 1 : 0;
                    break;

                case PVM.clt:     // logical less
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] < tos ? 1 : 0;
                    break;

                case PVM.cle:     // logical less or equal
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] <= tos ? 1 : 0;
                    break;

                case PVM.cgt:     // logical greater
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] > tos ? 1 : 0;
                    break;

                case PVM.cge:     // logical greater or equal
                    tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] >= tos ? 1 : 0;
                    break;

                case PVM.brn:     // unconditional branch
                    cpu.pc = mem[cpu.pc++];
                    break;

                case PVM.bze:     // pop top of stack, branch if false
                    int target = mem[cpu.pc++];
                    if (mem[cpu.sp++] == 0)
                    {
                        cpu.pc = target;
                    }
                    break;

                case PVM.anew:    // heap array allocation
                    int size = mem[cpu.sp];
                    mem[cpu.sp] = cpu.hp;
                    cpu.hp     += size;
                    break;

                case PVM.halt:    // halt
                    ps = finished;
                    break;

                case PVM.stk:     // stack dump (debugging)
                    StackDump(results, pcNow);
                    break;

                case PVM.heap:     // heap dump (debugging)
                    HeapDump(results, pcNow);
                    break;

                case PVM.ldc_0:   // push constant 0
                    mem[--cpu.sp] = 0;
                    break;

                case PVM.ldc_1:   // push constant 1
                    mem[--cpu.sp] = 1;
                    break;

                case PVM.ldc_2:   // push constant 2
                    mem[--cpu.sp] = 2;
                    break;

                case PVM.ldc_3:   // push constant 3
                    mem[--cpu.sp] = 3;
                    break;

                case PVM.lda_0:   // push local address 0
                    mem[--cpu.sp] = cpu.fp - 1 - 0;
                    break;

                case PVM.lda_1:   // push local address 1
                    mem[--cpu.sp] = cpu.fp - 1 - 1;
                    break;

                case PVM.lda_2:   // push local address 2
                    mem[--cpu.sp] = cpu.fp - 1 - 2;
                    break;

                case PVM.lda_3:   // push local address 3
                    mem[--cpu.sp] = cpu.fp - 1 - 3;
                    break;

                case PVM.ldl:     // push local value
                    mem[--cpu.sp] = cpu.fp - 1 - mem[cpu.pc++];
                    mem[cpu.sp]   = mem[mem[cpu.sp]];
                    break;

                case PVM.ldl_0:   // push value of local variable 0
                    mem[--cpu.sp] = cpu.fp - 1 - 0;
                    mem[cpu.sp]   = mem[mem[cpu.sp]];
                    break;

                case PVM.ldl_1:   // push value of local variable 1
                    mem[--cpu.sp] = cpu.fp - 1 - 1;
                    mem[cpu.sp]   = mem[mem[cpu.sp]];
                    break;

                case PVM.ldl_2:   // push value of local variable 2
                    mem[--cpu.sp] = cpu.fp - 1 - 2;
                    mem[cpu.sp]   = mem[mem[cpu.sp]];
                    break;

                case PVM.ldl_3:   // push value of local variable 3
                    mem[--cpu.sp] = cpu.fp - 1 - 3;
                    mem[cpu.sp]   = mem[mem[cpu.sp]];
                    break;

                case PVM.stl:     // store local value
                    mem[--cpu.sp]      = cpu.fp - 1 - mem[cpu.pc++];
                    mem[mem[cpu.sp++]] = mem[cpu.sp++];
                    break;

                case PVM.stlc:    // store local value
                case PVM.stl_0:   // pop to local variable 0
                    mem[--cpu.sp]      = cpu.fp - 1 - 0;
                    mem[mem[cpu.sp++]] = mem[cpu.sp++];
                    break;

                case PVM.stl_1:   // pop to local variable 1
                    mem[--cpu.sp]      = cpu.fp - 1 - 1;
                    mem[mem[cpu.sp++]] = mem[cpu.sp++];
                    break;

                case PVM.stl_2:   // pop to local variable 2
                    mem[--cpu.sp]      = cpu.fp - 1 - 2;
                    mem[mem[cpu.sp++]] = mem[cpu.sp++];
                    break;

                case PVM.stl_3:   // pop to local variable 3
                    mem[--cpu.sp]      = cpu.fp - 1 - 3;
                    mem[mem[cpu.sp++]] = mem[cpu.sp++];
                    break;

                case PVM.stoc:    // character checked store
                //case PVM.inpc:          // character input         // character output
                case PVM.cap:
                    tos = mem[cpu.sp++];
                    if (96 < tos && tos < 123)
                    {
                        mem[--cpu.sp] = (tos - 32);
                    }
                    else
                    {
                        mem[--cpu.sp] = (tos);
                    }
                    break;

                // toUpperCase
                case PVM.low:
                    tos = mem[cpu.sp++];
                    if (64 < tos && tos < 91)
                    {
                        mem[--cpu.sp] = (tos + 31);
                    }
                    else
                    {
                        ps = badOp;
                    }
                    break; // toLowerCase

                case PVM.islet:
                    tos = mem[cpu.sp++];
                    if (64 < tos && tos < 91 || 96 < tos && tos < 123)
                    {
                        mem[--cpu.sp] = 1;
                    }
                    else
                    {
                        mem[--cpu.sp] = 0;
                    } // isLetter
                    break;

                case PVM.inc:                      //NOT DONE PROPERLY
                    tos           = mem[cpu.sp++]; // ++  //NOT DONE PROPERLY
                    mem[--cpu.sp] = (tos += 1);    //NOT DONE PROPERLY
                    break;                         //NOT DONE PROPERLY

                case PVM.dec:                      //NOT DONE PROPERLY
                    tos           = mem[cpu.sp++]; // --  //NOT DONE PROPERLY
                    mem[--cpu.sp] = (tos -= 1);    //NOT DONE PROPERLY
                    break;

                default:          // unrecognized opcode
                    ps = badOp;
                    break;
                }
            } while (ps == running);
            TimeSpan ts = timer.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);

            Console.WriteLine("\n\n" + ops + " operations.  Run Time " + elapsedTime + "\n\n");
            if (ps != finished)
            {
                PostMortem(results, pcNow);
            }
            timer.Reset();
            timer.Stop();
        } // PVM.Emulator
Ejemplo n.º 27
0
        } // FindOffset

        public static void ListReferences(OutFile output)
        {
            // Cross reference list of all variables on output file
        } // ListReferences
Ejemplo n.º 28
0
        } // CheckLabels

        public static void ListReferences(OutFile output)
        {
            // Cross reference list of all labels used on output file
        } // ListReferences
Ejemplo n.º 29
0
        } // PVM.PostMortem

        // The interpreters and utility methods

        public static void Emulator(int initPC, int codeLen, int initSP,
                                    InFile data, OutFile results, bool tracing,
                                    bool traceStack, bool traceHeap)
        {
            // Emulates action of the codeLen instructions stored in mem[0 .. codeLen-1], with
            // program counter initialized to initPC, stack pointer initialized to initSP.
            // data and results are used for I/O.  Tracing at the code level may be requested

            int pcNow;                  // current program counter
            int loop;                   // internal loops
            int tos, sos;               // value popped from stack
            int adr;
            stackBase = initSP;
            heapBase = codeLen;         // initialize boundaries
            cpu.hp = heapBase;          // initialize registers
            cpu.sp = stackBase;
            cpu.gp = stackBase;
            cpu.mp = stackBase;
            cpu.fp = stackBase;
            cpu.pc = initPC;            // initialize program counter
            ps = running;               // prepare to execute
            int ops = 0;
            timer.Start();
            do
            {
                ops++;
                pcNow = cpu.pc;           // retain for tracing/postmortem
                cpu.ir = mem[cpu.pc++];          // fetch
                                                 //        if (tracing) Trace(results, pcNow, traceStack, traceHeap);
                switch (cpu.ir)
                {         // execute
                    case PVM.nop:           // no operation
                        break;

                    case PVM.dsp:           // decrement stack pointer (allocate space for variables)
                        cpu.sp -= mem[cpu.pc++];
                        break;

                    case PVM.ldc:           // push constant value
                        mem[--cpu.sp] = mem[cpu.pc++];
                        break;

                    case PVM.lda:           // push local address
                        mem[--cpu.sp] = cpu.fp - 1 - mem[cpu.pc++];
                        break;

                    case PVM.ldv:           // dereference
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;

                    case PVM.sto:           // store
                        tos = mem[cpu.sp++]; mem[mem[cpu.sp++]] = tos;
                        break;

                    case PVM.ldxa:          // heap array indexing
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] + tos;
                        break;

                    case PVM.inpi:          // integer input
                        mem[mem[cpu.sp++]] = data.ReadInt();
                        break;

                    case PVM.prni:          // integer output
                                            //            if (tracing) results.Write(padding);
                        results.Write(mem[cpu.sp++], 0);
                        //            if (tracing) results.WriteLine();
                        break;

                    case PVM.inpb:          // boolean input
                        mem[mem[cpu.sp++]] = data.ReadBool() ? 1 : 0;
                        break;

                    case PVM.prnb:          // boolean output
                                            //            if (tracing) results.Write(padding);
                        if (mem[cpu.sp++] != 0) results.Write(" true  "); else results.Write(" false ");
                        //            if (tracing) results.WriteLine();
                        break;

                    case PVM.prns:          // string output
                                            //            if (tracing) results.Write(padding);
                        loop = mem[cpu.pc++];
                        while (mem[loop] != 0)
                        {
                            results.Write((char)mem[loop]); loop--;
                        }
                        //            if (tracing) results.WriteLine();
                        break;

                    case PVM.prnl:          // newline
                        results.WriteLine();
                        break;

                    case PVM.neg:           // integer negation
                        mem[cpu.sp] = -mem[cpu.sp];
                        break;

                    case PVM.add:           // integer addition
                        tos = mem[cpu.sp++]; mem[cpu.sp] += tos;
                        break;

                    case PVM.sub:           // integer subtraction
                        tos = mem[cpu.sp++]; mem[cpu.sp] -= tos;
                        break;

                    case PVM.mul:           // integer multiplication
                        tos = mem[cpu.sp++];
                        int temp = mem[cpu.sp];
                        int answer = temp * tos;
                        if (((tos > 0 && temp > 0) && answer < 0) || ((tos < 0 && temp < 0) && answer > 0) || ((tos > 0 || temp > 0) && answer < 0) || ((tos < 0 || temp < 0) && answer > 0) || answer > maxInt || answer < -maxInt)
                        {
                            PostMortem(results, pcNow);
                            break;
                        }
                        mem[cpu.sp] *= tos;
                        break;

                    case PVM.div:           // integer division (quotient)
                        tos = mem[cpu.sp++];
                        if (tos == 0)
                        {
                            PostMortem(results, pcNow);
                            break;
                        }
                        mem[cpu.sp] /= tos;
                        break;

                    case PVM.rem:           // integer division (remainder)
                        tos = mem[cpu.sp++]; mem[cpu.sp] %= tos;
                        break;

                    case PVM.not:           // logical negation
                        mem[cpu.sp] = mem[cpu.sp] == 0 ? 1 : 0;
                        break;

                    case PVM.and:           // logical and
                        tos = mem[cpu.sp++]; mem[cpu.sp] &= tos;
                        break;

                    case PVM.or:            // logical or
                        tos = mem[cpu.sp++]; mem[cpu.sp] |= tos;
                        break;

                    case PVM.ceq:           // logical equality
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] == tos ? 1 : 0;
                        break;

                    case PVM.cne:           // logical inequality
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] != tos ? 1 : 0;
                        break;

                    case PVM.clt:           // logical less
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] < tos ? 1 : 0;
                        break;

                    case PVM.cle:           // logical less or equal
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] <= tos ? 1 : 0;
                        break;

                    case PVM.cgt:           // logical greater
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] > tos ? 1 : 0;
                        break;

                    case PVM.cge:           // logical greater or equal
                        tos = mem[cpu.sp++]; mem[cpu.sp] = mem[cpu.sp] >= tos ? 1 : 0;
                        break;

                    case PVM.brn:           // unconditional branch
                        cpu.pc = mem[cpu.pc++];
                        break;
                    case PVM.bze:           // pop top of stack, branch if false
                        int target = mem[cpu.pc++];
                        if (mem[cpu.sp++] == 0) cpu.pc = target;
                        break;
                    case PVM.anew:          // heap array allocation
                        int size = mem[cpu.sp];
                        mem[cpu.sp] = cpu.hp;
                        cpu.hp += size;
                        break;
                    case PVM.halt:          // halt
                        ps = finished;
                        break;
                    case PVM.stk:           // stack dump (debugging)
                        StackDump(results, pcNow);
                        break;
                    case PVM.heap:           // heap dump (debugging)
                        HeapDump(results, pcNow);
                        break;
                    case PVM.ldc_0:         // push constant 0
                        mem[--cpu.sp] = 0;
                        break;
                    case PVM.ldc_1:         // push constant 1
                        mem[--cpu.sp] = 1;
                        break;
                    case PVM.ldc_2:         // push constant 2
                        mem[--cpu.sp] = 2;
                        break;
                    case PVM.ldc_3:         // push constant 3
                        mem[--cpu.sp] = 3;
                        break;
                    case PVM.lda_0:         // push local address 0
                     mem[--cpu.sp] = cpu.fp - mem[cpu.pc++];
                     break;
                    case PVM.lda_1:         // push local address 1
                        mem[--cpu.sp] = cpu.fp -1 -mem[cpu.pc++];
                        break;
                    case PVM.lda_2:         // push local address 2
                        mem[--cpu.sp] = cpu.fp -2- mem[cpu.pc++];
                        break;
                    case PVM.lda_3:         // push local address 3
                        mem[--cpu.sp] = cpu.fp -3- mem[cpu.pc++];
                        break;
                    case PVM.ldl_0:         // push value of local variable 0
                        mem[--cpu.sp] = cpu.fp - mem[cpu.pc++];
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;
                    case PVM.ldl_1:         // push value of local variable 1
                        mem[--cpu.sp] = cpu.fp -1- mem[cpu.pc++];
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;
                    case PVM.ldl_2:         // push value of local variable 2
                        mem[--cpu.sp] = cpu.fp -2- mem[cpu.pc++];
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;
                    case PVM.ldl_3:         // push value of local variable 3
                        mem[--cpu.sp] = cpu.fp -3- mem[cpu.pc++];
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;
                    case PVM.stl:           // store local value
                        // we have bug here with PushPop, test here shows:
                        adr = cpu.fp - 1 - mem[cpu.pc++];
                        mem[cpu.sp++] = mem[adr];
                        break;
                    case PVM.stl_0:         // pop to local variable 0
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        mem[--cpu.sp] = cpu.fp - mem[cpu.pc++];
                        break;
                    case PVM.stl_1:         // pop to local variable 1
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        mem[--cpu.sp] = cpu.fp -1- mem[cpu.pc++];
                        break;
                    case PVM.stl_2:         // pop to local variable 2
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        mem[--cpu.sp] = cpu.fp -2- mem[cpu.pc++];
                        break;
                    case PVM.stl_3:         // pop to local variable 3
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        mem[--cpu.sp] = cpu.fp -3-mem[cpu.pc++];
                        break;
                    case PVM.inpc: // character input
                        var line = data.ReadLine();
                        char tempChar = string.IsNullOrEmpty(line) ? ' ' : line[0];
                        mem[cpu.sp++] = tempChar;
                        if (data.Error()) ps = badData;
                        break;
                    case PVM.prnc:          // character output
                        if (tracing) results.WriteLine(padding);
                        int readIn = mem[mem[cpu.sp++]];
                        results.WriteLine((char)readIn);
                        if (tracing) results.WriteLine();
                        break;

                    case PVM.cap:           // toUpperCase
                        int cUp = mem[cpu.sp++];
                        cUp = (int)char.ToUpper((char)cUp);
                        mem[cpu.sp++] = cUp;
                        break;
                    case PVM.low:           // toLowerCase
                        int cLow = mem[cpu.sp++];
                        cLow = (int)char.ToLower((char)cLow);
                        mem[cpu.sp++] = cLow;
                        break;

                    case PVM.islet:         // isLetter
                        int val = mem[mem[cpu.sp]];
                        results.WriteLine("Character readIn: " + ((char)val).ToString());
                        if (Char.IsLetter((char)val)) mem[cpu.sp] = 1; else mem[cpu.sp] = 0;
                        break;

                    case PVM.inc:           // ++
                        int inc_a = mem[cpu.sp++] + 1;
                        mem[cpu.sp++] = inc_a;
                        break;
                    case PVM.dec:           // --
                        int dec_a = mem[cpu.sp++] - 1;
                        mem[cpu.sp++] = dec_a;
                        break;

                    case PVM.ldl: // push local value
                        mem[--cpu.sp] = mem[cpu.pc++];
                        var N = mem[cpu.sp];
                        mem[--cpu.sp] = cpu.fp - 1 - N;
                        mem[cpu.sp] = mem[mem[cpu.sp]];
                        break;
                    case PVM.stlc:          // store local value
                        tos = mem[cpu.sp++]; mem[mem[cpu.sp++]] = tos;
                        break;
                    case PVM.stoc:          // character checked store
                    default:                // unrecognized opcode
                        ps = badOp;
                        break;
                }
            } while (ps == running);
            TimeSpan ts = timer.Elapsed;

            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            Console.WriteLine("\n\n" + ops + " operations.  Run Time " + elapsedTime + "\n\n");
            if (ps != finished) PostMortem(results, pcNow);
            timer.Reset();
            timer.Stop();
        } // PVM.Emulator
        public override void Run()
        {
            var limit      = 10;//Per min
            var limitCount = 0;
            var limitOn    = true;

            //var md = Fetch("").Result;
            using (var writer = new StreamWriter(Path.Combine(OutFolder.FullName, "MalwareData.json")))
            {
                writer.AutoFlush = true;
                foreach (var item in storedDls)
                {
                    var key     = item.Key;
                    var newItem = item;

                    if (item.Value.malwareDTO != null)
                    {
                        dls.Add(key, newItem.Value);
                        writer.WriteLine(JsonConvert.SerializeObject(item.Value, Formatting.None));
                        continue;
                    }

                    if (limitCount > limit && limitOn)
                    {
                        limitCount = 0; Thread.Sleep(1000 * 60);
                    }

                    var res = Fetch(key).Result;

                    var md = res.Item1;

                    Trace.Write(key);
                    if (res.Item2 == HttpStatusCode.NotFound)
                    {
                        Trace.WriteLine(" -> Not found");
                    }
                    else if (md.data == null)
                    {
                        limit      = 4;
                        limitCount = limit;
                        Trace.WriteLine(" -> Error " + res.Item2.ToString() + " If 429, wait until the next hour starts");
                        var nt = (60 - DateTime.Now.Minute) * 60 * 1000;
                        Trace.WriteLine("Sleeping for " + (60 - DateTime.Now.Minute) + " min");
                        Thread.Sleep(nt);
                        continue;
                    }
                    else
                    {
                        Trace.WriteLine(" -> Content");
                        limitCount++;
                        var t = md.engineResults(md.data.attributes.last_analysis_results);
                    }
                    newItem.Value.malwareDTO = md;

                    foreach (var i in dls)
                    {
                        if (i.Value.malwareDTO.data.attributes.size == newItem.Value.malwareDTO.data.attributes.size)
                        {
                            i.Value.ips.AddRange(newItem.Value.ips);
                        }
                    }

                    dls.Add(key, newItem.Value);
                    writer.WriteLine(JsonConvert.SerializeObject(item.Value, Formatting.None));
                }
            }

            var cowriePath = Directory.CreateDirectory(@"C:\Users\thelu\OneDrive\Skrivebord\honeypotlogs 26_04\cowrie");

            ReadFilesWriteToFile(cowriePath.FullName, "cowrie.json.*", OutFile.Substring(0, OutFile.Length - 5) + ".json");
        }