public void Reset(ushort addr) { PC = Memory.GetWord(addr); A = X = Y = 0; SP = 0xFD; P = ProcessorStatus.InterruptDisabled; }
public static ProcessorStatus Clr(this ProcessorStatus self, ProcessorStatus flag) { var copy = self; copy &= ~flag; return(copy); }
public static ProcessorStatus Set(this ProcessorStatus self, ProcessorStatus flag) { var copy = self; copy |= flag; return(copy); }
public void Interrupt(InterruptType interruptType) { if ((P & ProcessorStatus.InterruptDisabled) != 0 && interruptType != InterruptType.NMI && interruptType != InterruptType.BRK) { return; } if (interruptType == InterruptType.BRK) { P &= ProcessorStatus.BreakCommand; ++PC; } Push((byte)(PC >> 8)); Push((byte)(PC & 0xFF)); Push((byte)P); P &= ProcessorStatus.InterruptDisabled; switch (interruptType) { case InterruptType.IRQ: case InterruptType.BRK: PC = Memory.GetWord(Utils.IRQVector); break; case InterruptType.NMI: PC = Memory.GetWord(Utils.NMIVector); break; } }
/// <summary> /// Compare (CPM, CPY, CPX) are always ***Unsigned*** comparsion. /// - Carry: Register >= Value. /// - Zero: Register == Value /// - Negative: Bit 7 set in (Register-Value) /// /// Ex: /// Register: 130 (0x82) - If it was treated as signed would be -126... /// Value: 26 /// Carry: 1 - Because 130 >= 26. If the values were treated as signed (which it's not!), carry would have been 0. /// Zero: 0 /// Negative: 0 - Because bit 7 in (130-26) is not set /// </summary> /// <param name="register"></param> /// <param name="value"></param> /// <param name="processorStatus"></param> public static void SetFlagsAfterCompare(byte register, byte value, ProcessorStatus processorStatus) { processorStatus.Carry = register >= value; processorStatus.Zero = register == value; byte regMinusValue = (byte)(register - value); processorStatus.Negative = ((byte)regMinusValue).IsBitSet(7); }
public Cpu6502(MemoryManager memoryManager) { A = new(this); X = new(this); Y = new(this); _memoryManager = memoryManager; PS = new(); }
public static byte PerformLSRAndSetStatusRegisters(byte register, ProcessorStatus processorStatus) { processorStatus.Carry = register.IsBitSet(0); var shiftedRegister = (byte)(register >> 1); SetFlagsAfterRegisterLoadIncDec(shiftedRegister, processorStatus); return(shiftedRegister); }
public static ProcessorStatus SetNumericFlags(this ProcessorStatus status, byte register) { var result = status; result = result.SetFlag(ProcessorStatus.NegativeFlag, (register & 1 << 7) != 0); result = result.SetFlag(ProcessorStatus.ZeroFlag, (register == 0)); return(result); }
public async Task ShutDown() { semaphore.Wait(); Status = ProcessorStatus.ShuttingDown; await Task.FromResult(false); Status = ProcessorStatus.ShutDown; semaphore.Release(); }
public async Task SpinUp() { semaphore.Wait(); Status = ProcessorStatus.Starting; await Task.FromResult(false); Status = ProcessorStatus.Idle; CreatedOn = DateTime.Now; semaphore.Release(); }
//ToDo: check Enum and get rid of all the casting public void setProcessorStatusBit(ProcessorStatus s, bool isSet = true) { if (isSet) { P = (byte)(P | (byte)s); } else { P = (byte)(P & (byte)(0xff ^ (byte)s)); } }
public static byte PerformRORAndSetStatusRegisters(byte register, ProcessorStatus processorStatus) { bool originalCarry = processorStatus.Carry; processorStatus.Carry = register.IsBitSet(0); var shiftedRegister = (byte)(register >> 1); shiftedRegister.ChangeBit(7, originalCarry); SetFlagsAfterRegisterLoadIncDec(shiftedRegister, processorStatus); return(shiftedRegister); }
public Cpu(Memory memory) { Memory = memory; PC = 0x0000; S = 0xff; P = ProcessorStatus._; A = 0; X = 0; Y = 0; _currentInstructionState = InstructionState.FetchOpCode; _currentCycle = 0; }
internal void Reset() { // Processor starts at zero AF = 0x0000; BC = 0x0000; DE = 0x0000; HL = 0x0000; SP = 0x0000; PC = 0x0000; InterruptMasterEnable = false; status = ProcessorStatus.Running; }
internal void Reset() { // Processor starts at zero AF = 0x0000; BC = 0x0000; DE = 0x0000; HL = 0x0000; SP = 0x0000; PC = 0x0000; InterruptMasterEnable = false; status = ProcessorStatus.Running; }
public void Initial() { var p = new ProcessorStatus(); Assert.False(p.HasFlag(ProcessorStatusFlag.E)); Assert.False(p.HasFlag(ProcessorStatusFlag.N)); Assert.False(p.HasFlag(ProcessorStatusFlag.V)); Assert.False(p.HasFlag(ProcessorStatusFlag.M)); Assert.False(p.HasFlag(ProcessorStatusFlag.X)); Assert.False(p.HasFlag(ProcessorStatusFlag.D)); Assert.False(p.HasFlag(ProcessorStatusFlag.I)); Assert.False(p.HasFlag(ProcessorStatusFlag.Z)); Assert.False(p.HasFlag(ProcessorStatusFlag.C)); }
public static ProcessorStatus SetOrClrIf(this ProcessorStatus self, ProcessorStatus flag, bool set) { var copy = self; if (set) { copy |= flag; } else { copy &= ~flag; } return(copy); }
public void SetFromValue() { var p = new ProcessorStatus(); p.Value |= (ProcessorStatusFlag)0x8055; Assert.True(p.HasFlag(ProcessorStatusFlag.E)); Assert.False(p.HasFlag(ProcessorStatusFlag.N)); Assert.True(p.HasFlag(ProcessorStatusFlag.V)); Assert.False(p.HasFlag(ProcessorStatusFlag.M)); Assert.True(p.HasFlag(ProcessorStatusFlag.X)); Assert.False(p.HasFlag(ProcessorStatusFlag.D)); Assert.True(p.HasFlag(ProcessorStatusFlag.I)); Assert.False(p.HasFlag(ProcessorStatusFlag.Z)); Assert.True(p.HasFlag(ProcessorStatusFlag.C)); }
/// <summary> /// "Processes" a document for the preset length of time. /// A semaphore prevents the processor from being shut down while this is happening, or from processing documents while it's spinning up. /// </summary> /// <param name="document"></param> /// <returns></returns> public async Task Process(Document document) { await Task.Run(() => { semaphore.Wait(); if (document != null) { Status = ProcessorStatus.Processing; Console.WriteLine("Starting processing document " + document.Id); Thread.Sleep(Constants.TimeToProcessOneDocument); document.Processed = DateTime.Now; Status = ProcessorStatus.Idle; DocumentService.FinishDocument(document); Console.WriteLine("Finished processing document " + document.Id); } semaphore.Release(); }); }
/// <summary> /// Adds two byte values (unsigned or signed). /// /// Uses ProcessorStatus.Carry flag as input to calculation, and sets it after calculation as appropriate. /// /// Also sets ProcessorStatus.Overflow flag after calculation if /// - value1 and value2 both had sign bit (7) clear, but added result has sign bit set. /// - value1 and value2 both had sign bit (7) set , but added result has sign bit clear. /// </summary> /// <param name="value1"></param> /// <param name="value"></param> /// <param name="PC"></param> /// <returns></returns> public static byte AddWithCarryAndOverflow(byte value1, byte value2, ProcessorStatus processorStatus) { // Perform add with carry ushort sum = (ushort)(value1 + value2 + (byte)(processorStatus.Carry?1:0)); byte result = (byte)(sum & 0xff); // Set carry if there was a overflow when adding two bytes processorStatus.Carry = (byte)(sum >> 8) == 1; processorStatus.Zero = result == 0; processorStatus.Negative = result.IsBitSet(7); // Overflow flag should be set in these situations // If both original values were signed and negative (bit 7 set), but the result was not negative (bit 7 clear) // or If both original values were signed and positive (bit 7 clear), but the result was negative (bit 7 set) processorStatus.Overflow = ((value1.IsBitSet(7) && value2.IsBitSet(7) && !result.IsBitSet(7)) || (!value1.IsBitSet(7) && !value2.IsBitSet(7) && result.IsBitSet(7))); return(result); }
public JobExecutionSummary ExecuteJob(string jobId, string jobTypeName, object param) { var currentDir = Environment.CurrentDirectory; var output = string.Empty; var executionResult = JobExecutionResult.Failed; _jobType = jobTypeName; _currentJobId = jobId; _status = ProcessorStatus.Processing; try { Environment.CurrentDirectory = GetJobExecutionFolder(jobTypeName); var jobType = Type.GetType(jobTypeName); var job = (IJob)Activator.CreateInstance(jobType); job.Execute(param); executionResult = JobExecutionResult.Success; } catch (Exception ex) { Console.WriteLine("----- BEGIN ERROR -----"); Console.WriteLine("Unhandled Exception - " + ex.ToString()); Console.WriteLine("----- END ERROR -----"); } finally { _jobType = null; _currentJobId = null; _status = ProcessorStatus.Idle; Environment.CurrentDirectory = currentDir; output = ConsoleIOManager.ReadAll(); ConsoleIOManager.ResetIO(); } return(new JobExecutionSummary { JobId = jobId, JobType = jobTypeName, Output = output, ProcessorID = this.ProcessorID, Result = executionResult }); }
public void SetFromFlag() { var p = new ProcessorStatus(); p.Value |= ProcessorStatusFlag.E; p.Value |= ProcessorStatusFlag.V; p.Value |= ProcessorStatusFlag.X; p.Value |= ProcessorStatusFlag.I; p.Value |= ProcessorStatusFlag.C; Assert.True(p.HasFlag(ProcessorStatusFlag.E)); Assert.False(p.HasFlag(ProcessorStatusFlag.N)); Assert.True(p.HasFlag(ProcessorStatusFlag.V)); Assert.False(p.HasFlag(ProcessorStatusFlag.M)); Assert.True(p.HasFlag(ProcessorStatusFlag.X)); Assert.False(p.HasFlag(ProcessorStatusFlag.D)); Assert.True(p.HasFlag(ProcessorStatusFlag.I)); Assert.False(p.HasFlag(ProcessorStatusFlag.Z)); Assert.True(p.HasFlag(ProcessorStatusFlag.C)); }
public void UpdateFlag() { var p = new ProcessorStatus(); // All set p.Value |= (ProcessorStatusFlag)0x00aa; p.UpdateFlag(ProcessorStatusFlag.E, true); p.UpdateFlag(ProcessorStatusFlag.V, true); p.UpdateFlag(ProcessorStatusFlag.X, true); p.UpdateFlag(ProcessorStatusFlag.I, true); p.UpdateFlag(ProcessorStatusFlag.C, true); Assert.True(p.HasFlag(ProcessorStatusFlag.E)); Assert.True(p.HasFlag(ProcessorStatusFlag.N)); Assert.True(p.HasFlag(ProcessorStatusFlag.V)); Assert.True(p.HasFlag(ProcessorStatusFlag.M)); Assert.True(p.HasFlag(ProcessorStatusFlag.X)); Assert.True(p.HasFlag(ProcessorStatusFlag.D)); Assert.True(p.HasFlag(ProcessorStatusFlag.I)); Assert.True(p.HasFlag(ProcessorStatusFlag.Z)); Assert.True(p.HasFlag(ProcessorStatusFlag.C)); // All clear p.Value &= (ProcessorStatusFlag)0x00aa; p.UpdateFlag(ProcessorStatusFlag.N, false); p.UpdateFlag(ProcessorStatusFlag.M, false); p.UpdateFlag(ProcessorStatusFlag.D, false); p.UpdateFlag(ProcessorStatusFlag.Z, false); Assert.False(p.HasFlag(ProcessorStatusFlag.E)); Assert.False(p.HasFlag(ProcessorStatusFlag.N)); Assert.False(p.HasFlag(ProcessorStatusFlag.V)); Assert.False(p.HasFlag(ProcessorStatusFlag.M)); Assert.False(p.HasFlag(ProcessorStatusFlag.X)); Assert.False(p.HasFlag(ProcessorStatusFlag.D)); Assert.False(p.HasFlag(ProcessorStatusFlag.I)); Assert.False(p.HasFlag(ProcessorStatusFlag.Z)); Assert.False(p.HasFlag(ProcessorStatusFlag.C)); }
public static ProcessorStatus UnsetFlag(this ProcessorStatus a, ProcessorStatus b) { return(a & (~b)); }
public static void SetFlagsAfterRegisterLoadIncDec(byte register, ProcessorStatus processorStatus) { processorStatus.Zero = register == 0x00; processorStatus.Negative = register.IsBitSet(7); }
// Add with carry examples // // Ex 1: // Carry: 0 // 0000 0001 // 0000 0001 // --------- // 0000 0010 // Carry: 0 // Ex 1: // Carry: 1 // 0000 0001 // 0000 0001 // --------- // 0000 0011 // Carry: 0 // Ex 3: // Carry: 0 // 1111 1111 // 0000 0001 // --------- // 0000 0001 // Carry: 1 // Ex 3: // Carry: 0 // 1111 1111 // 0000 0001 // --------- // 0000 0001 // Carry: 1 // Ex 4: // Carry: 0 // C: 1111 11 // V1:0100 0010 // v2:1111 1111 // --------- // R: 0100 0001 // Carry: 1 // Ex: 5 // Carry: 0 // C: // V1 0000 0001 (1) // V2 1100 1110 (-50) // --------- // R: 1100 1111 (-49) // Carry: 0 // Ex: 6 // Carry: 0 // C: 1111 11 // V1 0000 0010 (2) // V2 1111 1111 (-1) // --------- // R: 0000 0001 (+1) // Carry: 1 (Why Carry when we haven't exceeded -128 to +127 number space for the result (1)? // The algorithm above also sets 1, and other emulators also sets it to 1, ) public static byte SubtractWithCarryAndOverflow(byte value1, byte value2, ProcessorStatus processorStatus) { byte invertedValue2 = (byte)~value2; return(AddWithCarryAndOverflow(value1, invertedValue2, processorStatus)); }
public static ProcessorStatus SetFlag(this ProcessorStatus a, ProcessorStatus b) { return(a | b); }
/// <summary> /// Creates a new document processor /// </summary> /// <param name="slaClass">The SLA class that this processor processes for</param> public DocumentProcessor(SLAClass slaClass) { this.slaClass = slaClass; Status = ProcessorStatus.NotStarted; }
public static ProcessorStatus SetFlag(this ProcessorStatus a, ProcessorStatus b, bool isSet) { return(isSet ? (a | b) : (a & (~b))); }
public static void PerformBITAndSetStatusRegisters(byte register, byte memoryValue, ProcessorStatus processorStatus) { processorStatus.Zero = (register & memoryValue) == 0; processorStatus.Overflow = memoryValue.IsBitSet(StatusFlagBits.Overflow); processorStatus.Negative = memoryValue.IsBitSet(StatusFlagBits.Negative); }
public static ProcessorStatus ToogleFlag(this ProcessorStatus a, ProcessorStatus b) { return(a ^ b); }