Beispiel #1
0
        public void CopyToAccumulatorInstruction_TakesValueUnderCurrentPosition_CopiesValueToAccumulator()
        {
            var context = CreateContext(new MemoryPosition(3, 2));
            var sut     = _factory.CreateInstruction("A");

            sut.Execute(context);

            context.Accumulator.ShouldBe(0xE);
            context.MarkedEntry.ShouldBe(0xE);
        }
Beispiel #2
0
        public void Execute_PointsToSelf_Throws()
        {
            var context = CreateContext(5, 2);
            var sut     = _factory.CreateInstruction("C0");

            Assert.Throws <InvalidComparatorJumpError>(
                () => sut.Execute(context));

            context.ExecutionStackPointer.Value.ShouldBe(2);
            context.ExecutionStackPointer.Step.ShouldBe(1);
            context.Accumulator.ShouldBe(5);
        }
Beispiel #3
0
        public void InstructionsInitialized_WithComment()
        {
            InstructionFactory instrFact = new InstructionFactory();
            Instruction        expected  = instrFact.CreateInstruction(0x201d003c);

            Assert.AreEqual(expected.ToString(), target.Mips.InstrMem.GetInstruction(0).ToString());
        }
        public void ExecuteMoveRightInstruction_CanMoveRight_ExecutesProperly(int row, int expectedValue)
        {
            var context = CreateContext(new MemoryPosition(row, 0));
            var sut     = _factory.CreateInstruction("R");

            sut.Execute(context);

            context.Position.Row.ShouldBe(row);
            context.Position.Column.ShouldBe(1);
            context.MarkedEntry.ShouldBe(expectedValue);
        }
 public void Run()
 {
     State = new EmulatorState();
     State.Initialize(instructions);
     do
     {
         Instruction currentInstruction = InstructionFactory.CreateInstruction(State.GetCurrentInstruction());
         State = currentInstruction.Run(State);
     } while (!State.TerminateRun);
 }
Beispiel #6
0
        public void Process()
        {
            var         instructionFactory = new InstructionFactory(_input);
            var         reading            = true;
            List <long> errorProgram       = new List <long>();

            do
            {
                var          opCode      = softwareProgram[InstructionPointer];
                IInstruction instruction = instructionFactory.CreateInstruction(opCode, InstructionPointer, RelativeBase);
                if (instruction != null)
                {
                    if (instruction is StopInstruction)
                    {
                        if (errorProgram.Count > 0 && errorProgram[0] != Output)
                        {
                            ErrorProgram = new SoftwareProgram(errorProgram.ToArray());
                        }

                        reading    = false;
                        HasStopped = true;
                    }
                    else if (instruction is RelativeBaseOffsetInstruction)
                    {
                        RelativeBase = ((RelativeBaseOffsetInstruction)instruction).GetRelativeBase(ref softwareProgram);
                    }
                    else if (instruction is OutputInstruction)
                    {
                        Output = ((OutputInstruction)instruction).GetOutput(ref softwareProgram);
                        if (OutputReceiver != null)
                        {
                            OutputReceiver.ReceiveInput(Output);
                            reading = false;
                        }
                        else if (_outputReceiverIsSelf)
                        {
                            reading = false;
                        }
                        else
                        {
                            errorProgram.Add(Output);
                        }
                    }
                    else
                    {
                        softwareProgram.ProcessInstruction(instruction);
                    }
                    InstructionPointer = instruction.MoveInstructionPointer();
                }
                else
                {
                    reading = false;
                }
            } while (reading && InstructionPointer < SoftwareProgram.Length);
        }
Beispiel #7
0
        //[Ignore("TEST NOT YET NEEDED")]
        public void TestInitInstructionMemory()
        {
            uint[] instructions = new uint[] {
                0x201d0f3c, 0x3c08ffff, 0x3508ffff, 0x2009ffff, 0x1509001b, 0x00084600, 0x3508f000, 0x00084203, 0x00084102,
                0x340a0003, 0x01495022, 0x01484004, 0x010a582a, 0x010a582b, 0x20080005, 0x2d0b000a, 0x2d0b0004, 0x2008fffb,
                0x2d0b0005, 0x3c0b1010, 0x356b1010, 0x3c0c0101, 0x218c1010, 0x016c6824, 0x016c6825, 0x016c6826, 0x016c6827,
                0x8c040004, 0x20840002, 0x2484fffe, 0x0c000021, 0xac020000, 0x08000020, 0x23bdfff8, 0xafbf0004, 0xafa40000,
                0x28880002, 0x11000002, 0x00041020, 0x0800002e, 0x2084ffff, 0x0c000021, 0x8fa40000, 0x00441020, 0x00441020,
                0x2042ffff, 0x8fbf0004, 0x23bd0008, 0x03e00008
            };

            InstructionFactory instrFact = new InstructionFactory();

            Instruction[] instrs = new Instruction[instructions.Length];
            int           i      = 0;

            foreach (uint instr in instructions)
            {
                instrs[i] = instrFact.CreateInstruction(instr);
                i++;
            }

            target = new InstructionMemory(instrs);

            uint pc = 0;

            var dataMem  = new DataMemory(10000);
            var map      = new MappedMemoryUnit(dataMem, 0);
            var unitList = new List <MappedMemoryUnit> {
                map
            };
            var dataMemory = new MemoryMapper(unitList);
            var registers  = new Registers();

            int icount = 0;

            uint a = 99;

            dataMemory[4] = a;

            while (pc < instructions.Length * 4 && icount < 1000000)
            {
                //Console.WriteLine($"{pc:X8}: {target[pc]}");
                target.GetInstruction(pc).Execute(ref pc, dataMemory, registers);
                icount++;
            }

            Console.WriteLine(dataMemory[0]);

            Assert.AreEqual(a * a, dataMemory[0]);
        }
        public void ExecuteBackwardJump_ExecutionStackPointerPointsAtStart_Throws()
        {
            var context = CreateContext(5, 0);
            var sut     = _factory.CreateInstruction("J5");

            Assert.Throws <InvalidJumpError>(
                () => sut.Execute(context));

            context.ExecutionStackPointer.Value.ShouldBe(0);
            context.ExecutionStackPointer.Step.ShouldBe(1);
            context.Accumulator.ShouldBe(5);
        }
Beispiel #9
0
        public void SetUp()
        {
            uint[] instructions = new uint[] {
                0x201d0f3c, 0x3c08ffff, 0x3508ffff, 0x2009ffff, 0x1509001b, 0x00084600, 0x3508f000, 0x00084203, 0x00084102,
                0x340a0003, 0x01495022, 0x01484004, 0x010a582a, 0x010a582b, 0x20080005, 0x2d0b000a, 0x2d0b0004, 0x2008fffb,
                0x2d0b0005, 0x3c0b1010, 0x356b1010, 0x3c0c0101, 0x218c1010, 0x016c6824, 0x016c6825, 0x016c6826, 0x016c6827,
                0x8c040004, 0x20840002, 0x2484fffe, 0x0c000021, 0xac020000, 0x08F00020, 0x23bdfff8, 0xafbf0004, 0xafa40000,
                0x28880002, 0x11000002, 0x00041020, 0x0800002e, 0x2084ffff, 0x0c000021, 0x8fa40000, 0x00441020, 0x00441020,
                0x2042ffff, 0x8fbf0004, 0x23bd0008, 0x03e00008
            };
            InstructionFactory instrFact = new InstructionFactory();

            Instruction[] instrs = new Instruction[instructions.Length];
            for (int i = 0; i < instructions.Length; i++)
            {
                instrs[i] = instrFact.CreateInstruction(instructions[i]);
            }

            InstructionMemory instrMem = new InstructionMemory(instrs);
            var dataMem = new DataMemory(10000);

            dataMem[4] = 99;
            var map      = new MappedMemoryUnit(dataMem, 0);
            var unitList = new List <MappedMemoryUnit> {
                map
            };
            var mem = new MemoryMapper(unitList);

            memDict = new Dictionary <Type, List <MemoryUnit> >();
            memDict.Add(instrMem.GetType(), new List <MemoryUnit> {
                instrMem
            });
            memDict.Add(mem.GetType(), new List <MemoryUnit> {
                mem
            });

            target = new Mips(0x0, memDict);
        }
Beispiel #10
0
 public static IInstruction Parse(Token token)
 => InstructionFactory.CreateInstruction(Map[token]);
Beispiel #11
0
 public void CreateInstructionCalled_UnknownMnemonic_Throws(string mnemonic)
 {
     Assert.Throws <UnknownInstructionError>(
         () => _sut.CreateInstruction(mnemonic));
 }
        public void CreateInstruction_ADD()
        {
            Instruction i = target.CreateInstruction(0x00000020);

            Assert.AreEqual(typeof(AddInstruction), i.GetType());
        }
        public void TestInstructionInstanciation()
        {
            List <IDefinition> empty   = new List <IDefinition>();
            List <IDefinition> one_int = new List <IDefinition> {
                Scalar.Integer
            };
            List <IDefinition> dbl_int = new List <IDefinition> {
                Scalar.Integer, Scalar.Integer
            };
            List <IDefinition> trp_int = new List <IDefinition> {
                Scalar.Integer, Scalar.Integer, Scalar.Integer
            };

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.AND, empty).GetType() == typeof(And));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.OR, empty).GetType() == typeof(Or));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.DIFFERENT, dbl_int).GetType() == typeof(Different));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.EQUAL, dbl_int).GetType() == typeof(Equal));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.GREATER, dbl_int).GetType() == typeof(Greater));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.GREATER_EQUAL, dbl_int).GetType() == typeof(GreaterEqual));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.LOWER, dbl_int).GetType() == typeof(Less));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.LOWER_EQUAL, dbl_int).GetType() == typeof(LessEqual));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.ACCESS, trp_int).GetType() == typeof(Access));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.BINARY_AND, trp_int).GetType() == typeof(BinaryAnd));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.BINARY_OR, trp_int).GetType() == typeof(BinaryOr));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.XOR, trp_int).GetType() == typeof(Xor));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.ADD, trp_int).GetType() == typeof(Add));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.SUB, trp_int).GetType() == typeof(Substract));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.DIV, trp_int).GetType() == typeof(Divide));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.MUL, trp_int).GetType() == typeof(Multiplicate));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.MOD, trp_int).GetType() == typeof(Modulo));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.LEFT_SHIFT, trp_int).GetType() == typeof(LeftShift));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.RIGHT_SHIFT, trp_int).GetType() == typeof(RightShift));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.BINARY_NOT, dbl_int).GetType() == typeof(BinaryNot));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.NOT, one_int).GetType() == typeof(Not));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.INVERSE, dbl_int).GetType() == typeof(Inverse));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.ENUM_SPLITTER, new List <IDefinition> {
                new EnumType()
            }).GetType() == typeof(EnumSplitter));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.GETTER, new List <IDefinition> {
                new Variable()
            }).GetType() == typeof(Getter));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.SETTER, new List <IDefinition> {
                new Variable()
            }).GetType() == typeof(Setter));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.FUNCTION_CALL, new List <IDefinition> {
                new Function()
            }).GetType() == typeof(FunctionCall));

            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.IF, empty).GetType() == typeof(If));
            Assert.IsTrue(InstructionFactory.CreateInstruction(InstructionFactory.INSTRUCTION_ID.WHILE, empty).GetType() == typeof(While));
        }