Exemplo n.º 1
0
        public static void Cmpi(Cpu cpu, uint rX, uint iValue)
        {
            var lValue = cpu.Registers[rX];

            cpu.Sf = (lValue < iValue);
            cpu.Zf = (lValue == iValue);
        }
Exemplo n.º 2
0
 public static void Addr(Cpu cpu, uint rX, uint rY)
 {
     unchecked
     {
         cpu.Registers[rX] += cpu.Registers[rY];
     }
 }
Exemplo n.º 3
0
        public void Compile()
        {
            var cpu = new Cpu(2048, 256);
            cpu.IdleProcess.Code.Append(cpu.Ram.Allocate(cpu.IdleProcess));

            var ms = cpu.GetMemoryStream(cpu.IdleProcess.Code);
            var writer = new CodeWriter(ms);
            Array.ForEach(IdleProcess.TerminatingIdle, writer.Write);
            writer.Close();

            var codeReader = new CodeReader(cpu.GetMemoryStream(cpu.IdleProcess.Code));

            var actual = codeReader.GetEnumerator();
            var expected = IdleProcess.TerminatingIdle.Cast<Instruction>().GetEnumerator();

            while ( actual.MoveNext() )
            {
                if ( expected.MoveNext() )
                {
                    Assert.That(actual.Current.OpCode, Is.EqualTo(expected.Current.OpCode));
                    Assert.That(actual.Current.Parameters, Is.EqualTo(expected.Current.Parameters));
                    Assert.That(actual.Current.Comment, Is.EqualTo(expected.Current.Comment));
                    continue;
                }

                Assert.That(actual.Current.OpCode, Is.EqualTo(OpCode.Noop));
            }
        }
Exemplo n.º 4
0
 public static void Addi(Cpu cpu, uint rX, uint iValue)
 {
     unchecked
     {
         cpu.Registers[rX] += iValue;
     }
 }
Exemplo n.º 5
0
        public static void Cmp(Cpu cpu, byte flag, uint destination, uint source)
        {
            var lValue = ReadValue(cpu, flag, source);
            var rValue = ReadValue(cpu, Dest(flag), destination);

            cpu.Sf = (rValue < lValue);
            cpu.Zf = (lValue == rValue);

            Console.WriteLine($"{rValue} < {lValue} : Sf: {cpu.Sf} Zf: {cpu.Zf}");
        }
Exemplo n.º 6
0
        public void RunCpu()
        {
            var cpu = new Cpu(2048, 256);
            IdleProcess.Initialise(cpu, IdleProcess.TerminatingIdle);

            while (cpu.Running)
            {
                cpu.Tick();
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
0
        public void RunRealCpu()
        {
            var cpu = new Cpu(2048, 256);
            IdleProcess.Initialise(cpu, IdleProcess.Instructions);

             			cpu.Tick();
             			Assert.That(cpu.CurrentProcess, Is.EqualTo(cpu.IdleProcess));
            Assert.That(cpu.CurrentProcess.Ip, Is.EqualTo(1));

            cpu.Tick();
            Assert.That(cpu.CurrentProcess.Registers[0], Is.EqualTo(20));
        }
Exemplo n.º 9
0
        public void RunCpuWithProgram()
        {
            var cpu = new Cpu(2048, 256);
            IdleProcess.Initialise(cpu, IdleProcess.Instructions);

            var prog1 = cpu.Load();

            cpu.Compile(prog1, TestPrograms.ReallySimpleProgram);
            cpu.Run(prog1);

            while ( prog1.IsRunning )
            {
                cpu.Tick();
            }

            Assert.That(prog1.ExitCode, Is.EqualTo(1));
        }
Exemplo n.º 10
0
        public void ControlFlow(string source, uint result)
        {
            var grammar = new AndyStructuralGrammar();
            var parserState = new ParserState(source);

            var parseResult = grammar.block.Match(parserState);
            Assert.That(parseResult, Is.True);

            var compiler = new Andy.TinyOS.Compiler.Compiler(grammar, parserState);
            var program = compiler.Compile();

            var cpu = new Cpu();
            var r = Run(program, cpu);
            Assert.That(r.ExitCode, Is.EqualTo(result));

            var variable = cpu.Ram.GetStream(r.GlobalData.Pages.First());
            using (var reader = new BinaryReader(variable))
            {
                var value = reader.ReadUInt32();
                Assert.That(value, Is.EqualTo(result));
            }
        }
Exemplo n.º 11
0
        public void ProgramSignals()
        {
            var cpu = new Cpu(2048, 256);
            IdleProcess.Initialise(cpu, IdleProcess.Instructions);

            var prog1 = cpu.Load();
            var prog2 = cpu.Load();

            cpu.Compile(prog1, TestPrograms.P1);
            cpu.Compile(prog2, TestPrograms.P2);

            cpu.Run(prog1);
            cpu.Run(prog2);

            while (prog1.IsRunning || prog2.IsRunning)
            {
                cpu.Tick();
            }

            Assert.That(prog1.ExitCode, Is.EqualTo(1));
            Assert.That(prog2.ExitCode, Is.EqualTo(1));
        }
Exemplo n.º 12
0
 public static void Printm(Cpu cpu, uint rX)
 {
     var vAddr = cpu.Registers[rX];
     var value = cpu.Read(vAddr);
     cpu.Print(value);
 }
Exemplo n.º 13
0
        public static void Cmpr(Cpu cpu, uint rX, uint rY)
        {
            var lValue = cpu.Registers[rX];
            var rValue = cpu.Registers[rY];

            cpu.Sf = (lValue < rValue);
            cpu.Zf = (lValue == rValue);
        }
Exemplo n.º 14
0
 public static void Call(Cpu cpu, uint rX)
 {
     var value = cpu.Registers[rX];
     cpu.Call(value);
 }
Exemplo n.º 15
0
 public static void Callm(Cpu cpu, uint rX)
 {
     var vAddr = cpu.Registers[rX];
     var value = cpu.Read(vAddr);
     cpu.Call(value);
 }
Exemplo n.º 16
0
 public static void Pushr(Cpu cpu, uint rX)
 {
     cpu.Push(cpu.Registers[rX]);
 }
Exemplo n.º 17
0
 public static void And(Cpu cpu, uint rX, uint rY)
 {
     var value = cpu.Registers[rY];
     cpu.Registers[rX] &= value;
 }
Exemplo n.º 18
0
 public static void TerminateProcess(Cpu cpu, uint rX, uint rY)
 {
     var pId = cpu.Registers[rX];
     var exitCode = cpu.Registers[rY];
     cpu.TerminateProcess(pId, exitCode);
 }
Exemplo n.º 19
0
        public void SetUp()
        {
            const uint pId = 10;

            var ram = new Ram(_ram, 16);
            _sharedOffset  = (int) ram.AllocateShared(256);

            _cpu = new Cpu(ram);
            _cpu.InputDevice = new InputDevice();

            var p = CreateProcess(pId);
            _cpu.CurrentProcess = p;
            _cpu.CurrentProcess.Registers[Register.H] = p.GlobalData.Offset;

            var heap = _cpu.Ram.Allocate(_cpu.IdleProcess);
            _heapOffset = (int) _cpu.Ram.ToPhysicalAddress(_cpu.IdleProcess.Id, heap.VirtualAddress);
            _cpu.Ram.Free(heap);
        }
Exemplo n.º 20
0
 public static void SignalEvent(Cpu cpu, uint rX)
 {
     var eventNo = cpu.Registers[rX];
     cpu.SignalEvent(eventNo);
 }
Exemplo n.º 21
0
 public static void Sleep(Cpu cpu, uint rX)
 {
     var sleep = cpu.Registers[rX];
     cpu.Sleep(sleep);
 }
Exemplo n.º 22
0
 public static void Printr(Cpu cpu, uint rX)
 {
     var value = cpu.Registers[rX];
     cpu.Print(value);
 }
Exemplo n.º 23
0
 public static void Return(Cpu cpu)
 {
     cpu.Return();
 }
Exemplo n.º 24
0
 public static void ReleaseLock(Cpu cpu, uint rX)
 {
     var lockNo = cpu.Registers[rX];
     cpu.ReleaseLock(lockNo);
 }
Exemplo n.º 25
0
        public void SetUp()
        {
            const uint pId = 10;

            _cpu = new Cpu(new Ram(_ram, 128));
            _cpu.InputDevice = new InputDevice();

            var p = CreateProcess(pId);
            _cpu.CurrentProcess = p;

            var heap = _cpu.Ram.Allocate(_cpu.IdleProcess);
            _heapOffset = (int) _cpu.Ram.ToPhysicalAddress(_cpu.IdleProcess.Id, heap.VirtualAddress);
            _cpu.Ram.Free(heap);
        }
Exemplo n.º 26
0
 public static void WaitEvent(Cpu cpu, uint rX)
 {
     var eventNo = cpu.Registers[rX];
     cpu.WaitEvent(eventNo);
 }
Exemplo n.º 27
0
 public static void Popr(Cpu cpu, uint rX)
 {
     cpu.Registers[rX] = cpu.Pop();
 }
Exemplo n.º 28
0
 public static void Alloc(Cpu cpu, uint rX, uint rY)
 {
     cpu.Registers[rY] = cpu.Allocate(cpu.Registers[rX]);
 }
Exemplo n.º 29
0
 public static void SetPriority(Cpu cpu, uint rX)
 {
     var priority = cpu.Registers[rX];
     cpu.AdjustPriority((byte) (priority & 0xFF));
 }
Exemplo n.º 30
0
 public static void Pushi(Cpu cpu, uint iValue)
 {
     cpu.Push(iValue);
 }