예제 #1
0
        public void ProcessorExecutePerformsNoOpOnHaltState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "HALT";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            processor.Execute();

            mockTape.ClearReceivedCalls();
            mockTable.ClearReceivedCalls();

            processor.Execute();

            mockTable.Received(0).GetInstruction(Arg.Any<String>());
            mockTape.Received(0).Read();
            mockTape.Received(0).Write(Arg.Any<char>());
            mockTape.Received(0).MoveLeft();
            mockTape.Received(0).MoveRight();
        }
예제 #2
0
        public void ProcessorDoesNotMoveTapeOnNoMoveInstruction()
        {
            ITape mockTape = Substitute.For<ITape>();
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            char symbol = 'a';

            Instruction noMoveInstruction = new Instruction();
            noMoveInstruction.State = "START";
            noMoveInstruction.WriteSymbols[symbol] = symbol;
            noMoveInstruction.MoveDirections[symbol] = MoveDirection.None;
            noMoveInstruction.NextStates[symbol] = "HALT";

            mockTape.Read().Returns(symbol);
            mockTable.GetInstruction(Arg.Any<String>()).Returns(noMoveInstruction);

            processor.Execute();

            mockTable.Received(1).GetInstruction("START");
            mockTape.Received(1).Read();
            mockTape.Received(1).Write(symbol);
            mockTape.Received(0).MoveRight();
            mockTape.Received(0).MoveLeft();
        }
예제 #3
0
 private static void DrawHead(Processor processor)
 {
     Console.WriteLine("                                      _^_");
     int stateLength = processor.NextState.Length / 2;
     for (int index = 0; index < 39 - stateLength; index++)
     {
         Console.Write(" ");
     }
     Console.WriteLine(processor.NextState + "                                  ");
 }
예제 #4
0
        public void ProcessorExecuteReturnsFalseOnHaltState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "HALT";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            Assert.IsTrue(processor.Execute());
            Assert.IsFalse(processor.Execute());
        }
예제 #5
0
        public void ProcessorExecutesStartStateFollowedByNextState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "A";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            processor.Execute();

            mockTable.Received(1).GetInstruction("START");
            mockTape.Received(1).Read();
            mockTape.Received(1).Write(' ');
            mockTape.Received(0).MoveRight();
            mockTape.Received(0).MoveLeft();

            processor.Execute();

            mockTable.Received(1).GetInstruction(testInstruction.NextStates[' ']);
        }
예제 #6
0
 public void ProcessorThrowsExceptionOnNullTape()
 {
     IInstructionTable mockTable = Substitute.For<IInstructionTable>();
     Processor processor = new Processor(null, mockTable);
 }
예제 #7
0
 public void ProcessorThrowsExceptionOnNullInstructionTable()
 {
     ITape mockTape = Substitute.For<ITape>();
     Processor processor = new Processor(mockTape, null);
 }
예제 #8
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Default;

            ITape tape = null;

            try
            {
                tape = new Tape(File.ReadAllText("./init.tap"));
            }
            catch(Exception)
            {
                tape = new Tape();
            }

            IInstructionTable instructionTable = null;

            try
            {
                instructionTable = ParseInstructions(args);
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed to load instruction table. " + ex.Message);
                Console.ReadLine();
                return;
            }

            Processor processor = new Processor(tape, instructionTable);

            Console.CursorVisible = false;

            int clockDelay = 500;

            try
            {
                clockDelay = Int32.Parse(ConfigurationManager.AppSettings.Get("ClockDelay"));
            }
            catch(Exception)
            {

            }

            while(true)
            {
                Console.SetCursorPosition(0, 0);
                DrawTape(tape);
                DrawHead(processor);
                Console.WriteLine();
                Console.WriteLine("Tick: " + processor.Tick);

                if(!processor.Execute())
                {
                    break;
                }
                Thread.Sleep(clockDelay);
            }

            Console.WriteLine("Execution completed!");
            Console.ReadLine();
        }