public void CanComposeSingleInstruction() { var stream = new CodeStream(); var i1 = new Instruction(); var s2 = stream + i1; Assert.That(s2, Is.EquivalentTo(new[] { i1 })); }
public void Movr() { var stream = new CodeStream(); stream.AsFluent() .Mov.RR(Register.A, Register.B); var expected = new Instruction(OpCode.Mov).Source(Register.B).Destination(Register.A); Assert.That(stream.First(), Is.EqualTo(expected).Using(new InstructionComparer())); }
public void CanComposeMultipleInstructions() { var i1 = new Instruction(); var i2 = new Instruction(); var s1 = new CodeStream { i1 }; var s2 = new CodeStream {i2}; var s3 = s1 + s2; Assert.That(s3, Is.EquivalentTo(new[] { i1, i2 })); }
public static void Initialise(Cpu cpu, Instruction[] code = null) { var ms = new MemoryStream(); using (var writer = new CodeWriter(ms)) { Array.ForEach(code ?? Instructions, writer.Write); writer.Close(); var codeBlock = cpu.AllocateCodeBlock(cpu.IdleProcess, (uint )ms.Length); ms.WriteTo(codeBlock); } }
public void Write(Instruction instruction) { if (_writer == null) throw new ObjectDisposedException("CodeWriter"); var meta = OpCodeMetaInformation.Lookup[instruction.OpCode]; if (meta.Parameters.Length != instruction.Parameters.Length) throw new InvalidOperationException($"Bad instruction, not enough parameters: {meta.OpCode}"); _instructionTable.Add((ushort )_writer.BaseStream.Position); _writer.Write((byte )instruction.OpCode); _writer.Write(instruction.OpCodeMask); _writer.Write((byte )instruction.Parameters.Length); foreach (var t in instruction.Parameters) _writer.Write(t); _writer.Write(instruction.Comment ?? string.Empty); _writer.Flush(); }
private void Invoke(Instruction instruction) { _cpu.Execute(instruction); }
public void WriteInstruction() { var stream =new CodeStream(); var instruction = new Instruction(); stream.Add(instruction); Assert.That(stream.ToArray(), Is.EqualTo(new[] { instruction })); }
public void WriteMultipleInstruction() { var stream =new CodeStream(); var i1 = new Instruction(); var i2 = new Instruction(); stream.Add(i1, i2); Assert.That(stream.ToArray(), Is.EqualTo(new[] { i1, i2 })); }
public void WriteMissingParameter() { var badInstruction = new Instruction(OpCode.Mov).Source(10); Assert.That(badInstruction.ToString(), Is.StringMatching(@"^Mov")); var ms = new StringWriter(); using (var writer = new InstructionTextWriter(ms)) { writer.Write(badInstruction); } }
public void WriteExtraParameter() { var badInstruction = new Instruction(OpCode.Ret).Source(10); Assert.That(badInstruction.ToString(), Is.StringMatching(@"^Ret\s+\$10")); var ms = new StringWriter(); using (var writer = new InstructionTextWriter(ms)) writer.Write(badInstruction); }
public void WriteBadOpCode() { var badInstruction = new Instruction((OpCode) 254); Assert.That(badInstruction.ToString(), Is.StringMatching(@"^254\s+;.*")); var ms = new StringWriter(); using (var writer = new InstructionTextWriter(ms)) writer.Write(badInstruction); }
public void ParseExpression() { var instruction = new Instruction(OpCode.Mov).Destination(Register.A).Source(10); Assert.That(instruction.OpCode, Is.EqualTo(OpCode.Mov)); Assert.That(instruction.OpCodeMask, Is.EqualTo(13)); Assert.That(instruction.ToString(), Is.StringMatching(@"^Mov\s+r1\s+\$10")); }
public void Execute(Instruction instruction) { var pId = CurrentProcess.Id; CurrentProcess.Ip++; CurrentProcess.Quanta--; Action<Cpu, byte, uint[]> operation; if (_operations.TryGetValue(instruction.OpCode, out operation)) { operation(this, instruction.OpCodeMask, instruction.Parameters); Console.WriteLine("{0}) {1}", pId, instruction); return; } throw new InvalidOperationException("Bad instruction: " + instruction); }