Esempio n. 1
0
 public bool Apply(IInstruction instruction)
 {
     if (Mapping.ContainsKey(instruction.GetType()))
     {
         Mapping[instruction.GetType()].Invoke(instruction, this);
         return(true);
     }
     return(false);
 }
		public bool Apply(IInstruction instruction)
		{
			if (Mapping.ContainsKey(instruction.GetType()))
			{
				Mapping[instruction.GetType()].Invoke(instruction, this);
				return true;
			}
			return false;
		}
Esempio n. 3
0
            private bool ExecuteInstruction(IInstruction instruction)
            {
                switch (instruction)
                {
                case SendInstruction send:
                    return(ExecuteInstruction(send));

                case ReceiveInstruction receive:
                    return(ExecuteInstruction(receive));

                case SetInstruction set:
                    return(ExecuteInstruction(set));

                case AddInstruction add:
                    return(ExecuteInstruction(add));

                case MultiplyInstruction multiply:
                    return(ExecuteInstruction(multiply));

                case ModulusInstruction modulus:
                    return(ExecuteInstruction(modulus));

                case JumpInstruction jump:
                    return(ExecuteInstruction(jump));

                default:
                    throw new InvalidOperationException($"Unknown instruction: {instruction.GetType()}.");
                }
            }
Esempio n. 4
0
        public void ParseInstruction_Returns_ComputeInstruction_When_Instruction_Is_Valid(
            [Values("", "M", "D", "MD", "A", "AM", "AD", "AMD")] string dest,
            [Values("", "JGT", "JEQ", "JGE", "JLT", "JNE", "JLE", "JMP")] string jump,
            [Values("0", "1", "-1", "D", "A", "!D", "!A", "-D", "-A", "D+1", "A+1", "D-1", "A-1", "D+A", "D-A",
                    "A-D", "D&A", "D|A", "M", "!M", "-M", "M+1", "M-1", "D+M", "D-M", "M-D", "D&M", "D|M")] string comp)
        {
            // arrange
            string equals = "=", semicolon = ";";

            if (string.IsNullOrEmpty(dest))
            {
                equals = string.Empty;
            }
            if (string.IsNullOrEmpty(jump))
            {
                semicolon = string.Empty;
            }
            string line = dest + equals + comp + semicolon + jump;

            var nextParser        = Substitute.For <IInstructionParser>();
            var destinationParser = Substitute.For <IComputeDestinationParser>();
            var jumpParser        = Substitute.For <IComputeJumpParser>();
            var parser            = new ComputeInstructionParser(nextParser, destinationParser, jumpParser);

            // act
            IInstruction result = parser.ParseInstruction(line);

            // assert
            nextParser.DidNotReceive().ParseInstruction(Arg.Any <string>());
            destinationParser.Received().ParseComputeDestination(Arg.Is(dest));
            jumpParser.Received().ParseComputeJump(Arg.Is(jump));
            Assert.AreEqual(typeof(ComputeInstruction), result.GetType());
        }
Esempio n. 5
0
 public static void PrettyPrint(StreamWriter writer, IInstruction x, bool printAddress = true)
 {
     if (printAddress)
     {
         writer.Write("{0} {1}: ", (x.Address).ToString("X").PadLeft(8, '0'), x.ToInteger().ToString("X").PadLeft(8, '0'));
     }
     PrettyPrinters[x.GetType()](x, writer);
 }
Esempio n. 6
0
        public async Task NextInst()
        {
            try
            {
                Console.WriteLine($"program {_programId} running {StackPointer}");
                IInstruction instruction = _instructions[StackPointer];

                if (!DebugCounts.ContainsKey(instruction.GetType().Name))
                {
                    DebugCounts.Add(instruction.GetType().Name, 0);
                }
                DebugCounts[instruction.GetType().Name]++;
                await instruction.Execute(ReceiveQueue, SendQueue, _registers).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Terminated = true;
            }
        }
        public void ParseInstruction_Returns_LabelInstruction_When_Label_Is_Valid(string line, string expected)
        {
            // arrange
            var nextParser  = Substitute.For <IInstructionParser>();
            var labelParser = Substitute.For <ISymbolParser>();
            var parser      = new LabelInstructionParser(nextParser, labelParser);

            // act
            IInstruction result = parser.ParseInstruction(line);

            // assert
            nextParser.DidNotReceive().ParseInstruction(Arg.Any <string>());
            labelParser.Received().ParseLabel(Arg.Is(expected));
            Assert.AreEqual(typeof(LabelInstruction), result.GetType());
        }
Esempio n. 8
0
            private bool ExecuteInstruction(IInstruction instruction)
            {
                switch (instruction)
                {
                case SetInstruction set:
                    return(ExecuteInstruction(set));

                case SubstractionInstruction sub:
                    return(ExecuteInstruction(sub));

                case MultiplyInstruction multiply:
                    return(ExecuteInstruction(multiply));

                case ModulusInstruction modulus:
                    return(ExecuteInstruction(modulus));

                case JumpInstruction jump:
                    return(ExecuteInstruction(jump));

                default:
                    throw new InvalidOperationException($"Unknown instruction: {instruction.GetType()}.");
                }
            }
Esempio n. 9
0
        public void AddInstruction(int opcode, IInstruction instruction)
        {
            if (instruction is null)
            {
                throw new ArgumentNullException(nameof(instruction));
            }

            IInstruction current = instructionsTable[opcode];

            if (current == null)
            {
                instructionsTable[opcode] = instruction;
                loadedOperations++;
            }
            else
            {
                throw new ArgumentException($"Attempted to overwrite existing instruction [{current.GetType().Name}] at 0x{opcode.ToString("x", CultureInfo.InvariantCulture)} with [{instruction.GetType().Name}]");
            }
        }
Esempio n. 10
0
 public static void PrettyPrint(StreamWriter writer, IInstruction x, bool printAddress=true)
 {
     if (printAddress)
         writer.Write("{0} {1}: ", (x.Address).ToString("X").PadLeft(8, '0'), x.ToInteger().ToString("X").PadLeft(8, '0'));
     PrettyPrinters[x.GetType()](x, writer);
 }