예제 #1
0
        public void CanComposeSingleInstruction()
        {
            var stream = new CodeStream();
            var i1 = new Instruction();
            var s2 = stream + i1;

            Assert.That(s2, Is.EquivalentTo(new[] { i1 }));
        }
예제 #2
0
        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()));
        }
예제 #3
0
        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 }));
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        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();
        }
예제 #6
0
 private void Invoke(Instruction instruction)
 {
     _cpu.Execute(instruction);
 }
예제 #7
0
        public void WriteInstruction()
        {
            var stream =new CodeStream();
            var instruction = new Instruction();
            stream.Add(instruction);

            Assert.That(stream.ToArray(), Is.EqualTo(new[] { instruction }));
        }
예제 #8
0
        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 }));
        }
예제 #9
0
        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);
            }
        }
예제 #10
0
        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);
        }
예제 #11
0
        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);
        }
예제 #12
0
 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"));
 }
예제 #13
0
파일: Cpu.cs 프로젝트: andy-uq/TinyOS
        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);
        }