Пример #1
0
        public void Evaluate(ParseTree tree, PowerPCMachine emulator)
        {
            foreach (var node in tree.Root.ChildNodes[0].ChildNodes)
            {
                var curNode = node.ChildNodes[0];
                switch (curNode.Term.ToString())
                {
                case "add":
                {
                    var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] + emulator.GPR[rb];
                    break;
                }

                case "addi":
                {
                    var rd   = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra   = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var simm = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] + simm;
                    break;
                }

                case "addc":
                {
                    var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] + emulator.GPR[rb];
                    //todo implement condition register functions
                    break;
                }

                case "and":
                {
                    var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] & emulator.GPR[rb];
                    break;
                }

                case "andc":
                {
                    var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] + ~emulator.GPR[rb];
                    break;
                }

                case "andi":
                {
                    var rd  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var num = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] & num;
                    break;
                }

                case "andis":
                {
                    var rd  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var num = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] & (num << 16);
                    break;
                }

                case "cmp":
                {
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    emulator.CR[0].setFlag(0, ra < rb);
                    emulator.CR[0].setFlag(1, ra > rb);
                    emulator.CR[0].setFlag(2, ra == rb);
                    break;
                }

                case "print":
                {
                    if (curNode.ChildNodes.Count == 1)
                    {
                        var reg = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                        print(String.Format("PRINT: r{0}: {1}", reg, emulator.GPR[reg]));
                    }
                    else if (curNode.ChildNodes.Count == 2)
                    {
                        var reg  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                        var reg2 = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                        if (reg2 > reg)
                        {
                            for (int i = reg; i <= reg2; i++)
                            {
                                print(String.Format("PRINT: r{0}: {1}", i, emulator.GPR[i]));
                            }
                        }
                        else
                        {
                            print("PRINT: invalid print syntax");
                        }
                    }
                    else
                    {
                        //todo print all registers
                        var regStr = "";
                        for (int i = 0; i < 32; i++)
                        {
                            regStr += String.Format("r{0}: {1}\r\n", i, emulator.GPR[i]);
                        }
                        Console.Write(
                            "MACHINE PRINT\r\n{0}",
                            regStr
                            );
                    }
                    break;
                }

                case "li":
                {
                    var rd  = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var num = NodeUtilities.ParseNumber(curNode.ChildNodes[1]);
                    emulator.GPR[rd] = num;
                    break;
                }

                case "sub":
                {
                    var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] - emulator.GPR[rb];
                    break;
                }

                case "subi":
                {
                    var rd   = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                    var ra   = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                    var simm = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                    emulator.GPR[rd] = emulator.GPR[ra] - simm;
                    break;
                }

                default:
                    break;
                }
            }
        }
Пример #2
0
 public Emulator()
 {
     Instance = new PowerPCMachine();
      parser = new Parser(language);
 }
Пример #3
0
 public void Evaluate(ParseTree tree, PowerPCMachine emulator)
 {
     foreach (var node in tree.Root.ChildNodes[0].ChildNodes)
     {
         var curNode = node.ChildNodes[0];
         switch (curNode.Term.ToString())
         {
             case "add":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] + emulator.GPR[rb];
                     break;
                 }
             case "addi":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var simm = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] + simm;
                     break;
                 }
             case "addc":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] + emulator.GPR[rb];
                     //todo implement condition register functions
                     break;
                 }
             case "and":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] & emulator.GPR[rb];
                     break;
                 }
             case "andc":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] + ~emulator.GPR[rb];
                     break;
                 }
             case "andi":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var num = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] & num;
                     break;
                 }
             case "andis":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var num = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] & (num << 16);
                     break;
                 }
             case "cmp":
                 {
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     emulator.CR[0].setFlag(0, ra < rb);
                     emulator.CR[0].setFlag(1, ra > rb);
                     emulator.CR[0].setFlag(2, ra == rb);
                     break;
                 }
             case "print":
                 {
                     if (curNode.ChildNodes.Count == 1)
                     {
                         var reg = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                         print(String.Format("PRINT: r{0}: {1}", reg, emulator.GPR[reg]));
                     }
                     else if (curNode.ChildNodes.Count == 2)
                     {
                         var reg = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                         var reg2 = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                         if (reg2 > reg)
                         {
                             for (int i = reg; i <= reg2; i++)
                             {
                                 print(String.Format("PRINT: r{0}: {1}", i, emulator.GPR[i]));
                             }
                         }
                         else
                         {
                             print("PRINT: invalid print syntax");
                         }
                     }
                     else
                     {
                         //todo print all registers
                         var regStr = "";
                         for(int i = 0; i < 32; i++)
                         {
                             regStr += String.Format("r{0}: {1}\r\n", i, emulator.GPR[i]);
                         }
                         Console.Write(
                             "MACHINE PRINT\r\n{0}",
                             regStr
                             );
                     }
                     break;
                 }
             case "li":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var num = NodeUtilities.ParseNumber(curNode.ChildNodes[1]);
                     emulator.GPR[rd] = num;
                     break;
                 }
             case "sub":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var rb = NodeUtilities.UnwrapRegister(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] - emulator.GPR[rb];
                     break;
                 }
             case "subi":
                 {
                     var rd = NodeUtilities.UnwrapRegister(curNode.ChildNodes[0]);
                     var ra = NodeUtilities.UnwrapRegister(curNode.ChildNodes[1]);
                     var simm = NodeUtilities.ParseNumber(curNode.ChildNodes[2]);
                     emulator.GPR[rd] = emulator.GPR[ra] - simm;
                     break;
                 }
             default:
                 break;
         }
     }
 }
Пример #4
0
 public Emulator()
 {
     Instance = new PowerPCMachine();
     parser   = new Parser(language);
 }