private ExecutionState <GroupState> Run(string code, InstructionExecutor <GroupState> executor = null) { (Synfron.Staxe.Matcher.Data.IMatchData matchData, bool success, int _, int?_, string _) = LanguageMatchEngine.Match(code); Assert.True(success); InstructionGenerator generator = new InstructionGenerator(); IList <Instruction <GroupState> > instructions = generator.Generate(matchData); InstructionOptimizer <GroupState> optimizer = new InstructionOptimizer <GroupState>(); instructions = optimizer.Optimize(instructions).ToArray(); GroupState groupState = new GroupState(); groupState.Group.Instructions = instructions.ToArray(); ExecutionState <GroupState> executionState = new ExecutionState <GroupState>(groupState); executor = executor ?? new InstructionExecutor <GroupState>() { ExternalDynamicPointers = NativeLocator.GetNativePointer, ValueProvider = new ValueProvider() }; PrintDiagnostics.EnableDiagnostics(code, executor, executionState, true); executor.Execute(executionState); return(executionState); }
public void InstructionExecutor_Execute_AllExecutable() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); Instruction <G> instruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody); Group <G> group = new Group <G> { Instructions = new[] { instruction, instruction, instruction, instruction, instruction, instruction, instruction } }; G groupState = Mock.Of <G>(m => m.Group == group); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); sut.Execute(executionState); Assert.Equal(7, executionState.InstructionIndex); Mock.Get(executionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Exactly(7)); }
public void InstructionExecutor_Execute_NotExecutable_StopProcessing() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); InstructionExecutionBody <G> specialExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); Instruction <G> specialInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, specialExecutionBody); Group <G> group = new Group <G> { Instructions = new[] { TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), specialInstruction, TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), } }; G groupState = Mock.Of <G>(m => m.Group == group); Mock.Get(specialExecutionBody).Setup(m => m(It.IsAny <IInstructionExecutor <G> >(), It.IsAny <E>(), It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >())).Callback((IInstructionExecutor <G> ie, ExecutionState <G> e, object[] payload, StackList <ValuePointer <G> > stackRegister, StackList <StackValuePointer <G> > stackPointers) => { e.Executable = false; }); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); sut.Execute(executionState); Assert.Equal(4, executionState.InstructionIndex); }
private static void Day12() { var instructions = LineByLine.GetLines("day12/day12_input.txt"); var instructionExecutor = new InstructionExecutor(instructions.ToArray()); var valueOfRegisterA = instructionExecutor.Execute(); Console.WriteLine("Value of register A: " + valueOfRegisterA); }
public void InstructionExecutor_Execute_WithInterrupts() { InstructionExecutionBody <G> interruptableExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); InstructionExecutionBody <G> uninterruptableExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); Instruction <G> interruptableInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, true, interruptableExecutionBody); Instruction <G> uninterruptableInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, uninterruptableExecutionBody); Group <G> group = new Group <G> { Instructions = new[] { uninterruptableInstruction, uninterruptableInstruction, uninterruptableInstruction, interruptableInstruction, uninterruptableInstruction, uninterruptableInstruction, uninterruptableInstruction } }; G groupState = Mock.Of <G>(m => m.Group == group); E executionState = new ExecutionState <G>(groupState); IInterrupt <G> inactiveInterrupt = Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == false); IInterrupt <G>[] activeInterrupts = new IInterrupt <G>[] { Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true), Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true), Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true), Mock.Of <IInterrupt <G> >(m => m.Intersects(executionState) == true) }; executionState.Interrupts.Add(inactiveInterrupt); executionState.Interrupts.Add(activeInterrupts[0]); executionState.Interrupts.Add(inactiveInterrupt); executionState.Interrupts.Add(activeInterrupts[1]); InterruptedHandler <G> interruptedHandler = Mock.Of <InterruptedHandler <G> >(); InstructionExecutor <G> sut = new InstructionExecutor <G>(); sut.Interrupts.Add(inactiveInterrupt); sut.Interrupts.Add(activeInterrupts[2]); sut.Interrupts.Add(inactiveInterrupt); sut.Interrupts.Add(activeInterrupts[3]); sut.Interrupted += interruptedHandler; Mock.Get(interruptedHandler).Setup(m => m(sut, It.IsAny <InterruptedEventArgs <G> >())).Callback((InstructionExecutor <G> sender, InterruptedEventArgs <G> args) => { Assert.Equal(executionState, args.ExecutionState); Assert.Equal(activeInterrupts.OrderBy(i => i.GetHashCode()), args.Interrupts.OrderBy(i => i.GetHashCode())); }); sut.Execute(executionState); Assert.Equal(7, executionState.InstructionIndex); Mock.Get(uninterruptableExecutionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Exactly(6)); Mock.Get(interruptableExecutionBody).Verify(m => m(sut, executionState, It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >()), Times.Once); Mock.Get(interruptedHandler).Verify(m => m(sut, It.IsAny <InterruptedEventArgs <G> >()), Times.Once); }
protected InstructionBase(string name, byte opCode, AddressingMode addressingMode, M6502Core core) { Name = name; OpCode = opCode; AddressingMode = addressingMode; Core = core; _executor = GetExecutor(); }
// Used for updating the layout of the scene when a command is valid public GameObject SetMapLayout(int x, int z, RobotController.Command command, GameObject obj) { if (command == RobotController.Command.PICKUP) { Debug.Log("Pick x: " + x + " z: " + z); layoutMap[x, z] = Layout.EMPTY; objectMap[x, z].SetActive(false); objectMap[x, z] = null; } else if (command == RobotController.Command.DROP) { Debug.Log("Drop x: " + x + " z: " + z); var oldPos = obj.GetComponent <IsoTransform>().Position; objectMap[(int)oldPos.x, (int)oldPos.z] = null; layoutMap[x, z] = Layout.ELEMENT; obj.GetComponent <IsoTransform>().Position = new Vector3(x - 1, 0.8f, z - 1); objectMap[x, z] = obj; objectMap[x, z].SetActive(true); if (((x != inputX) || (z != inputZ)) && objectMap[inputX, inputZ] == null) { NextInputElement(); } var finished = CheckAnswer(); if (finished) { Debug.Log("Finished"); instructionExecutor = FindObjectOfType <InstructionExecutor>(); instructionExecutor.Stop(); endCanvas = Resources.FindObjectsOfTypeAll <SoftwareEndScreen>()[0]; var finishedCorrectly = CompletedCorrectly(currentLevel); if (finishedCorrectly) { StartCoroutine(EndScreen()); } else { StartCoroutine(NotFinished()); } } } else if (command == RobotController.Command.SWAP) { Debug.Log("Swap x: " + x + " z: " + z); GameObject temp = objectMap[x, z]; temp.SetActive(false); objectMap[x, z] = obj; obj.GetComponent <IsoTransform>().Position = new Vector3(x - 1, 0.8f, z - 1); obj.SetActive(true); return(temp); } return(null); }
private int ExecuteNextOpcode() { if (IsHalted) { executionContext.OpcodeBytes.Add(NOP_opcode); return(InstructionExecutor.Execute(NOP_opcode)); } return(InstructionExecutor.Execute(FetchNextOpcode())); }
private int AcceptPendingInterrupt() { if (executionContext.IsEiOrDiInstruction) { return(0); } if (NmiInterruptPending) { IsHalted = false; Registers.IFF1 = 0; this.ExecuteCall(NmiServiceRoutine); return(11); } if (!InterruptsEnabled) { return(0); } var activeIntSource = InterruptSources.FirstOrDefault(s => s.IntLineIsActive); if (activeIntSource == null) { return(0); } Registers.IFF1 = 0; Registers.IFF2 = 0; IsHalted = false; switch (InterruptMode) { case 0: var opcode = activeIntSource.ValueOnDataBus.GetValueOrDefault(0xFF); InstructionExecutor.Execute(opcode); return(13); case 1: InstructionExecutor.Execute(RST38h_opcode); return(13); case 2: var pointerAddress = NumberUtils.CreateShort( lowByte: activeIntSource.ValueOnDataBus.GetValueOrDefault(0xFF), highByte: Registers.I); var callAddress = NumberUtils.CreateShort( lowByte: Memory[pointerAddress], highByte: Memory[pointerAddress.Inc()]); this.ExecuteCall(callAddress.ToUShort()); return(19); } return(0); }
private static void OnInterrupt <G>(string code, InstructionExecutor <G> _, InterruptedEventArgs <G> args) where G : IGroupState <G>, new() { PrintStackPointers(args.ExecutionState); PrintGroupPointers(args.ExecutionState); PrintStackRegister(args.ExecutionState); PrintListRegister(args.ExecutionState); Instruction <G> instruction = args.ExecutionState.GroupState.Group.Instructions[args.ExecutionState.InstructionIndex]; Debug.WriteLine($"\nInstruction: {args.ExecutionState.InstructionIndex}. {instruction.Code} {string.Join(", ", instruction.Payload?.Select(item => ToValueString(item)) ?? Enumerable.Empty<string>())} \n {(instruction.SourcePosition != null ? EngineUtils.GetLineAtPosition(code, instruction.SourcePosition.Value, 100) : null)}"); }
public void ExecutingInstruction_WhichDoesNotModifyPc_IncreasesPcBy2() { var cpu = new Cpu(); cpu.Memory[Cpu.MemoryAddressOfFirstInstruction + 1] = 0xE0; // CLS instruction Assert.That(cpu.PC, Is.EqualTo(Cpu.MemoryAddressOfFirstInstruction)); var instructionExecutor = new InstructionExecutor(cpu, _display, _keyboard, _instructionDecoder); var instruction = instructionExecutor.ExecuteSingleInstruction(); Assert.That(instruction, Is.InstanceOf <Instruction_00E0>()); Assert.That(cpu.PC, Is.EqualTo(Cpu.MemoryAddressOfFirstInstruction + 2)); }
public void ExecutingInstruction_WhichModifiesPc_DoesNotAdditionallyIncreasePcBy2() { var cpu = new Cpu(); cpu.Stack.Push(Cpu.MemoryAddressOfFirstInstruction); cpu.Memory[Cpu.MemoryAddressOfFirstInstruction + 1] = 0xEE; // RET instruction Assert.That(cpu.PC, Is.EqualTo(Cpu.MemoryAddressOfFirstInstruction)); var instructionExecutor = new InstructionExecutor(cpu, _display, _keyboard, _instructionDecoder); var instruction = instructionExecutor.ExecuteSingleInstruction(); Assert.That(instruction, Is.InstanceOf <Instruction_00EE>()); Assert.That(cpu.PC, Is.EqualTo(Cpu.MemoryAddressOfFirstInstruction)); }
public void BooleanTests(string expression, bool expectedResult) { (Synfron.Staxe.Matcher.Data.IMatchData matchData, bool success, int _, int?_, string _) = LanguageMatchEngine.Match(expression); Assert.True(success); InstructionGenerator generator = new InstructionGenerator(); IList <Instruction <GroupState> > instructions = generator.Generate(matchData); InstructionOptimizer <GroupState> optimizer = new InstructionOptimizer <GroupState>(); instructions = optimizer.Optimize(instructions); GroupState groupState = new GroupState(); groupState.Group.Instructions = instructions.ToArray(); ExecutionState <GroupState> executionState = new ExecutionState <GroupState>(groupState); InstructionExecutor <GroupState> executor = new InstructionExecutor <GroupState>(); executor.Execute(executionState); Assert.Equal(expectedResult, ((DefaultBooleanValue <GroupState>)executionState.ListRegister.Last().Value).Data); }
public void InstructionExecutor_Execute_EngineRuntimeException_RethrownAsNewEngineRuntimeException() { InstructionExecutionBody <G> executionBody = Mock.Of <InstructionExecutionBody <G> >(); InstructionExecutionBody <G> specialExecutionBody = Mock.Of <InstructionExecutionBody <G> >(); int sourcePosition = 20; string exceptionMessage = "Engine exception"; EngineRuntimeException thrownException = new EngineRuntimeException(exceptionMessage); Instruction <G> specialInstruction = TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, sourcePosition, false, specialExecutionBody); Group <G> group = new Group <G> { Instructions = new[] { TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), specialInstruction, TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody), TestInstructionProvider <G> .GetInstruction(InstructionCode.NON, null, null, false, executionBody) } }; G groupState = Mock.Of <G>(m => m.Group == group); Mock.Get(specialExecutionBody).Setup(m => m(It.IsAny <IInstructionExecutor <G> >(), It.IsAny <E>(), It.IsAny <object[]>(), It.IsAny <StackList <ValuePointer <G> > >(), It.IsAny <StackList <StackValuePointer <G> > >())).Callback((IInstructionExecutor <G> ie, ExecutionState <G> e, object[] payload, StackList <ValuePointer <G> > stackRegister, StackList <StackValuePointer <G> > stackPointers) => { throw thrownException; }); E executionState = new ExecutionState <G>(groupState); InstructionExecutor <G> sut = new InstructionExecutor <G>(); EngineRuntimeException ex = Assert.Throws <EngineRuntimeException>(() => sut.Execute(executionState)); Assert.Equal(exceptionMessage, ex.Message); Assert.Equal(thrownException, ex.InnerException); Assert.Equal(sourcePosition, ex.Position); }
public void ExecutingInstruction_WithEmptyMemory_ThrowsException() { var instructionExecutor = new InstructionExecutor(_cpu, _display, _keyboard, _instructionDecoder); Assert.Throws <InvalidOperationException>(() => instructionExecutor.ExecuteSingleInstruction()); }
public void HostedClassTest() { string pointCode = @" namespace Geometry { class Point { x; y; () { x = 0; y = 0; Shapes.count++; } } static class Shapes { count = 0; } } "; string lineCode = @" using Geometry.Point; using Geometry.Shapes; namespace Geometry { class Line { a; b; () { a = new Point(); b = new Point(); Shapes.count++; } } } using Geometry.Line; var line = new Line(); line.a.{ x = 10, y = 20 }; line.b.{ x = 70, y = 80 }; var aX = line.a.x; var aY = line.a.y; var bX = line.b.x; var bY = line.b.y; var count = Shapes.count; "; InstructionExecutor <GroupState> executor = new InstructionExecutor <GroupState>() { ExternalDynamicPointers = NativeLocator.GetNativePointer, ValueProvider = new ValueProvider() }; Run(pointCode, executor); ExecutionState <GroupState> executionState = Run(lineCode, executor); Assert.Equal(new DefaultIntValue <GroupState>(10), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aX")); Assert.Equal(new DefaultIntValue <GroupState>(20), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("aY")); Assert.Equal(new DefaultIntValue <GroupState>(70), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bX")); Assert.Equal(new DefaultIntValue <GroupState>(80), executionState.GetStackValue <ExecutionState <GroupState>, GroupState>("bY")); }