Ejemplo n.º 1
0
        public Z80Processor()
        {
            ClockSynchronizer = new ClockSynchronizer();

            ClockFrequencyInMHz = 4;
            ClockSpeedFactor    = 1;

            AutoStopOnDiPlusHalt        = true;
            AutoStopOnRetWithStackEmpty = false;
            StartOfStack = 0xFFFF.ToShort();

            SetMemoryWaitStatesForM1(0, MemorySpaceSize, 0);
            SetMemoryWaitStatesForNonM1(0, MemorySpaceSize, 0);
            SetPortWaitStates(0, PortSpaceSize, 0);

            Memory     = new PlainMemory(MemorySpaceSize);
            PortsSpace = new PlainMemory(PortSpaceSize);

            SetMemoryAccessMode(0, MemorySpaceSize, MemoryAccessMode.ReadAndWrite);
            SetPortsSpaceAccessMode(0, PortSpaceSize, MemoryAccessMode.ReadAndWrite);

            Registers        = new Z80Registers();
            InterruptSources = new List <IZ80InterruptSource>();

            InstructionExecutor = new Z80InstructionExecutor();

            StopReason = StopReason.NeverRan;
            State      = ProcessorState.Stopped;
        }
Ejemplo n.º 2
0
        private int InstructionExecutionLoopCore(bool isSingleInstruction)
        {
            ClockSynchronizer.Start();
            executionContext = new InstructionExecutionContext();
            StopReason       = StopReason.NotApplicable;
            State            = ProcessorState.Running;
            var totalTStates = 0;

            while (!executionContext.MustStop)
            {
                executionContext.StartNewInstruction();

                FireBeforeInstructionFetchEvent();
                if (executionContext.MustStop)
                {
                    break;
                }

                var executionTStates = ExecuteNextOpcode();

                totalTStates              = executionTStates + executionContext.AccummulatedMemoryWaitStates;
                TStatesElapsedSinceStart += (ulong)totalTStates;
                TStatesElapsedSinceReset += (ulong)totalTStates;

                ThrowIfNoFetchFinishedEventFired();

                if (!isSingleInstruction)
                {
                    CheckAutoStopForHaltOnDi();
                    CheckForAutoStopForRetWithStackEmpty();
                    CheckForLdSpInstruction();
                }

                FireAfterInstructionExecutionEvent(totalTStates);

                if (!IsHalted)
                {
                    IsHalted = executionContext.IsHaltInstruction;
                }

                var interruptTStates = AcceptPendingInterrupt();
                TStatesElapsedSinceStart += (ulong)interruptTStates;
                TStatesElapsedSinceReset += (ulong)interruptTStates;

                if (isSingleInstruction)
                {
                    executionContext.StopReason = StopReason.ExecuteNextInstructionInvoked;
                }
                else
                {
                    ClockSynchronizer.TryWait(totalTStates);
                }
            }

            ClockSynchronizer.Stop();
            this.StopReason = executionContext.StopReason;
            this.State      =
                StopReason == StopReason.PauseInvoked
                    ? ProcessorState.Paused
                    : ProcessorState.Stopped;

            executionContext = null;

            return(totalTStates);
        }