Beispiel #1
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                _instructions = Aoc.Framework.Input.GetStringVector(this);
                Cpu cpu = new Cpu(-1);
                cpu.OnExecute += OnExecute;
                Cpu.CpuState state = Cpu.CpuState.Running;
                cpu.Registers["a"] = 7;
                while (state == Cpu.CpuState.Running)
                {
                    state = cpu.Execute(_instructions);
                }
                return(cpu.Registers["a"].ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                _instructions = Aoc.Framework.Input.GetStringVector(this);
                Cpu cpu = new Cpu(-1);
                cpu.OnExecute += OnExecute;
                Cpu.CpuState state = Cpu.CpuState.Running;
                cpu.Registers["a"] = 12;
                while (state == Cpu.CpuState.Running)
                {
                    state = cpu.Execute(_instructions);
                }
                return(cpu.Registers["a"].ToString());
            }

            return("");
        }
Beispiel #2
0
        private bool IsClockSignal(long seed, long testLength)
        {
            // Run CPU
            Cpu cpu = new Cpu(0);

            cpu.OnExecute += OnExecute;
            Cpu.CpuState state = Cpu.CpuState.Running;
            cpu.Registers["a"] = seed;
            while ((state == Cpu.CpuState.Running) && testLength >= 0)
            {
                state = cpu.Execute(_instructions);
                testLength--;
            }

            // Test output
            if (cpu.Outbox.Count == 0)
            {
                return(false);
            }

            long expected = 0;

            while (cpu.Outbox.TryDequeue(out var tick))
            {
                if (tick != expected)
                {
                    return(false);
                }
                expected = 1 - expected;
            }
            return(true);
        }
Beispiel #3
0
 public void Start()
 {
     Cpu.Freeze();
     Memory.Load(_bootAddress, HardDrive.Read(_bootSector, _sectorSize));
     Cpu.Jump(_bootAddress);
     Cpu.Execute();
 }
Beispiel #4
0
        private void doRun()
        {
            Thread screenthread = new Thread(RefreshScreen);
            Thread soundthread1 = new Thread(Sound.PlayBeep1); // channel 1
            Thread soundthread2 = new Thread(Sound.PlayBeep2); // channel 2

            try
            {
                screenthread.SetApartmentState(ApartmentState.STA);
                screenthread.Start();
                soundthread1.Start();
                soundthread2.Start();
                while (Cpu.isRunning)
                {
                    if (pauseEmulation)
                    {
                        Thread.Sleep(0);
                        continue;
                    }
                    Cpu.Execute();
                }
                screenthread.Join();
                soundthread1.Join();
                soundthread2.Join();
                Application.DoEvents();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(@"CRASHED! Program Counter: {0} (0x0{1})

Exception: {2}", State.PC, Convert.ToString(State.PC, 16).PadLeft(4, '0'), ex.Message), "Undefined Instruction", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            finally
            {
                Cpu.isRunning = false;
                if (RunThread != null)
                {
                    RunThread.Abort();
                }
                if (screenthread != null)
                {
                    screenthread.Abort();
                }
                if (soundthread1 != null)
                {
                    soundthread1.Abort();
                }
                if (soundthread2 != null)
                {
                    soundthread2.Abort();
                }
                Cpu.Reset();
            }
        }
Beispiel #5
0
 public void Init()
 {
     _instructions   = Aoc.Framework.Input.GetStringVector(this);
     _cpu            = new Cpu(-1);
     _cpu.OnExecute += this.Execute;
     _highest        = Int64.MinValue;
     Cpu.CpuState state = Cpu.CpuState.Running;
     while (state == Cpu.CpuState.Running)
     {
         state = _cpu.Execute(_instructions);
     }
 }
Beispiel #6
0
        public void StxZeroPage()
        {
            Cpu cpu = new Cpu();

            cpu.X = 0x10;
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.StxZeroPage);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0xF);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x10, cpu.Ram.ReadByte(0xF));
        }
Beispiel #7
0
        public void Immediate()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdaImmediate);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 41);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(41, cpu.A);
        }
Beispiel #8
0
        public void NegativeFlagSet()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdaImmediate);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 255);

            // Act
            cpu.Execute();

            // Assert
            Assert.IsTrue(cpu.NegativeFlag);
        }
Beispiel #9
0
        public void ZeroFlagNotSet()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdaImmediate);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 1);

            // Act
            cpu.Execute();

            // Assert
            Assert.IsFalse(cpu.ZeroFlag);
        }
Beispiel #10
0
        public void ZeroPage()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(0xF, 0x40);
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.IncZeroPage);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0xF);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x41, cpu.Ram.ReadByte(0xF));
        }
Beispiel #11
0
        public void StxAbsolute()
        {
            Cpu cpu = new Cpu();

            cpu.X = 0xF0;
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.StxAbsolute);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x20);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 2), 0x30);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0xF0, cpu.Ram.ReadByte(0x3020));
        }
Beispiel #12
0
        private void CheckExecute(Word[] program, Boolean success, String message)
        {
            SetupMemory(program);
            SetupRegisterSet();

            try
            {
                m_cpu.Execute();
                Assert.IsTrue(success, message);
            }
            catch (Casl2SimulatorException)
            {
                Assert.IsFalse(success, message);
            }
        }
Beispiel #13
0
        private ulong ExecuteCpu()
        {
            ulong executedCycles = Cpu.Execute(1);

            if (Cpu.IsAsleep)
            {
                executedCycles += NextTimerEvent - SystemClock.CompatibleCycleCount;
                // Added this line
                if (NextTimerEvent > Cpu.ScheduledWakeUpTime)
                {
                    NextTimerEvent = Cpu.ScheduledWakeUpTime;
                }
                SystemClock.CompatibleCycleCount = NextTimerEvent;
            }
            return(executedCycles);
        }
Beispiel #14
0
        public void ZeroPageX()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(0x1F, 0x5);
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdyZeroPageX);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x1E);
            cpu.X = 0x1;

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x5, cpu.Y);
        }
Beispiel #15
0
        public void Absolute()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(0x2310, 0x5);
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdaAbsolute);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x10);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 2), 0x23);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x5, cpu.A);
        }
Beispiel #16
0
        public void Absolute()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(0x2030, 0x40);
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.IncAbsolute);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x30);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 2), 0x20);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x41, cpu.Ram.ReadByte(0x2030));
        }
Beispiel #17
0
        public void Execute()
        {
            Debug.Assert(ActiveThread != null);

            this.HandleCompletedTimers();

            // Execute active thread
            bool breakFlag;
            uint instructionsExecuted;

            Cpu.Execute(out breakFlag, out instructionsExecuted);
            if (breakFlag == false)
            {
                // Only if not broken by choice
                //Log.WriteLine( Verbosity.Verbose, Feature.Bios, "CPU returned to us after {0} instructions", instructionsExecuted );
            }
        }
Beispiel #18
0
        public string Run(Aoc.Framework.Part part)
        {
            _part = part;
            if (part == Aoc.Framework.Part.Part1)
            {
                Cpu a = new Cpu(-1);
                a.OnExecute += Execute;

                // Run the program
                while (a.State == Cpu.CpuState.Running)
                {
                    a.Execute(_instructions);
                }
                return(a.Registers["freq"].ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                Cpu a = new Cpu(0);
                Cpu b = new Cpu(1);
                a.OnExecute += Execute;
                b.OnExecute += Execute;

                // Run the program
                bool  deadlock   = false;
                Int64 sendsCount = 0;
                while (!deadlock)
                {
                    while (a.Outbox.Count > 0)
                    {
                        Int64 v = a.Outbox.Dequeue();
                        b.Inbox.Enqueue(v);
                    }
                    while (b.Outbox.Count > 0)
                    {
                        Int64 v = b.Outbox.Dequeue();
                        a.Inbox.Enqueue(v);
                        sendsCount++;
                    }
                    deadlock = (a.Execute(_instructions) == Cpu.CpuState.Waiting) && (b.Execute(_instructions) == Cpu.CpuState.Waiting);
                }
                return(sendsCount.ToString());
            }

            return("");
        }
Beispiel #19
0
        public void StaIndirectX()
        {
            Cpu cpu = new Cpu();

            cpu.A = 0xF0;
            cpu.X = 0x1;
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.StaIndirectX);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x10);
            cpu.Ram.WriteByte(0x11, 0x20);
            cpu.Ram.WriteByte(0x12, 0x30);

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0xF0, cpu.Ram.ReadByte(0x3020));
        }
Beispiel #20
0
        public void IndirectY()
        {
            // Arrange
            Cpu cpu = new Cpu();

            cpu.Ram.WriteByte(0x1C, 0x10);
            cpu.Ram.WriteByte(0x1D, 0x23);
            cpu.Ram.WriteByte(0x2311, 0x78);
            cpu.Ram.WriteByte(cpu.ProgramCounter, Mnemonic.LdaIndirectY);
            cpu.Ram.WriteByte((Int16)(cpu.ProgramCounter + 1), 0x1C);
            cpu.Y = 0x1;

            // Act
            cpu.Execute();

            // Assert
            Assert.AreEqual(0x78, cpu.A);
        }
Beispiel #21
0
        public void Run()
        {
            Console.WriteLine("Interpreter is running...");

            var    reader = Console.In;
            string line   = null;

            do
            {
                Print();
                line = reader.ReadLine();
                var command = Parse(line);
                if (command != null)
                {
                    _cpu.Execute(command.Opcode, command.Parameter);
                }
            } while (line != "exit");
        }
Beispiel #22
0
        public static void Main(string[] args)
        {
            var cpu = new Cpu(CpuConfigLibrary.Cpu32Bit.Build());

            const string code = @"
in

_start  call_eq_v   0       done
        sub_v       1
	    call        start

_done   out
        halt
";

            cpu.Compile(code);
            cpu.Execute();
        }
Beispiel #23
0
        private static long Execute4C00(Cpu cpu, int startAddr, int endAddr, int A, int X, int quotient, int rest)
        {
            cpu.RPC = startAddr;
            cpu.RA  = A;
            cpu.RX  = X;

            cpu.Cycles = 0;

            while (cpu.RPC != endAddr)
            {
                cpu.Execute();
            }

            Debug.Assert(cpu.RA == quotient);
            Debug.Assert(cpu.RX == rest);

            return(cpu.Cycles);

            //Trace.WriteLine( $"A={A}, X={X} => A={cpu.RA} X={cpu.RX}" );
        }
Beispiel #24
0
        public string Run(Aoc.Framework.Part part)
        {
            if (part == Aoc.Framework.Part.Part1)
            {
                _mulCount = 0;
                Cpu a = new Cpu(-1);
                a.OnExecute += Execute;

                // Run the program
                while (a.State == Cpu.CpuState.Running)
                {
                    a.Execute(_instructions);
                }
                return(_mulCount.ToString());
            }

            if (part == Aoc.Framework.Part.Part2)
            {
                int key    = Int32.Parse(_instructions[0].Split(" ")[2]);
                int result = 0;
                int start  = (key * 100) + 100000;
                int stop   = start + 17000;
                for (int b = start; b <= stop; b += 17)
                {
                    for (int d = 2; d <= Math.Sqrt(b); ++d)
                    {
                        if (b % d == 0)
                        {
                            result++;
                            break;
                        }
                    }
                }
                return(result.ToString());
            }

            return("");
        }
Beispiel #25
0
    void CpuExecutionThread()
    {
        Debug.Log("CPU is waiting for hardware to become ready");

        while (!mem.isReady)
        {
            Thread.Sleep(10);
        }

        while (!screen.isReady)
        {
            Thread.Sleep(10);
        }

        Debug.Log("CPU started");
        Cpu.Reset();
        dasm = Cpu.Disassemble(Cpu.Registers.PC);

        Cpu.ExecutionInfo ei;
        int cycles;

        while (!stopped)
        {
            if (triggerReset)
            {
                triggerReset = false;
                mem.LoadRom();
                Cpu.Reset();
            }

            if (stepMode)
            {
                if (!nextStep)
                {
                    Thread.Sleep(10);
                    continue;
                }
                else
                {
                    nextStep        = false;
                    cycles          = Cpu.ExecuteSingle();
                    cyclesExecuted += cycles;
                    dasm            = Cpu.Disassemble(Cpu.Registers.PC);
                }
            }
            else
            {
                ei = Cpu.Execute(cyclesPerExec);
                if (ei.cyclesExecuted == 0)
                {
                    stopped = true; // breakpoint probably
                }

                if (autoAdjustCycles)
                {
                    if (ei.cyclesSlept > 5)
                    {
                        cyclesPerExec -= (ei.cyclesSlept - 5) * 1000;
                        if (cyclesPerExec < 1000)
                        {
                            cyclesPerExec = 1000;
                        }
                    }
                    else if (ei.cyclesSlept < 1)
                    {
                        cyclesPerExec += 5000;
                    }
                }

                cyclesExecuted += ei.cyclesExecuted;
                dasm            = Cpu.Disassemble(Cpu.Registers.PC);
            }
        }

        Debug.Log("CPU has stopped");
    }
 private void Invoke(Instruction instruction)
 {
     _cpu.Execute(instruction);
 }
Beispiel #27
0
        static void Main(string[] args)
        {
            const int memorySize = 32 * 1024 * 1024;
            var       mmu        = new Mmu(memorySize);
            var       decoder    = new InstructionDecoder();
            var       cpu        = new Cpu <PhysicalMemoryAccessor, Tracing>(mmu, decoder);

            if (false /*cpu is Cpu<VirtualMemoryAccessor>*/)
            {
                var(location, data) = TlbHelper.PrepareIdentityMap(memorySize);
                mmu.CopyToPhysical(location, data);
                mmu._pageDirectory = location;
            }
            else
            {
                mmu._pageDirectory = memorySize - 4;
            }

            var elf              = ELFReader.Load <uint>("D:\\fibo");
            var startAddress     = elf.EntryPoint;
            var loadableSegments = elf.Segments.Where(s => s.Type == SegmentType.Load);

            foreach (var segment in loadableSegments)
            {
                var content = segment.GetMemoryContents();
                var address = segment.PhysicalAddress;
                mmu.CopyToPhysical(address, content);
            }

            cpu.Execute((int)startAddress);
            var stoper = Stopwatch.StartNew();

            for (var i = 0; i < 10; ++i)
            {
                cpu.Execute((int)startAddress);
            }
            stoper.Stop();
            Console.WriteLine(stoper.ElapsedMilliseconds);
            Console.WriteLine(cpu.InstructionsExecuted);
            Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS");
            cpu.Reset();


            stoper = Stopwatch.StartNew();
            for (var i = 0; i < 100; ++i)
            {
                cpu.Execute((int)startAddress);
            }
            stoper.Stop();
            Console.WriteLine(stoper.ElapsedMilliseconds);
            Console.WriteLine(cpu.InstructionsExecuted);
            Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS");
            cpu.Reset();


            stoper = Stopwatch.StartNew();
            for (var i = 0; i < 100; ++i)
            {
                cpu.Execute((int)startAddress);
            }
            stoper.Stop();
            Console.WriteLine(stoper.ElapsedMilliseconds);
            Console.WriteLine(cpu.InstructionsExecuted);
            Console.WriteLine($"{(double)cpu.InstructionsExecuted / stoper.ElapsedMilliseconds / 1000} MIPS");
        }
Beispiel #28
0
 public void Disabled()
 {
     cpu2.Execute((int)startAddress);
 }
Beispiel #29
0
 public void Enabled()
 {
     cpu1.Execute((int)startAddress);
 }
 public void StartComputer()
 {
     cpu.Freeze();
     memory.Load(BootAddress, hardDrive.Read(BootAddress));
     cpu.Execute();
 }