Example #1
0
 public ConditionalJumpAction(JavaValue value, ActionBlock target, JumpCondition condition, JavaValue valueCmp = null)
 {
     Value     = value ?? throw new ArgumentNullException(nameof(value));
     Target    = target ?? throw new ArgumentNullException(nameof(target));
     Condition = condition;
     ValueCmp  = valueCmp;
 }
Example #2
0
 public CALL(Z80Cpu cpu, IAddressMode <ushort> addressMode, JumpCondition jumpCondition)
 {
     _cpu           = cpu;
     _addressMode   = addressMode;
     _internalCycle = new InternalCycle(1);
     _jumpCondition = jumpCondition;
 }
Example #3
0
        /// <summary>
        /// Jump to address n if following condition is true:
        /// cc = NZ, Jump if Z flag is reset.
        /// cc = Z, Jump if Z flag is set.
        /// cc = NC, Jump if C flag is reset.
        /// cc = C, Jump if C flag is set.
        /// </summary>
        /// <param name="condition">The condition.</param>
        private void JP_cc_nn(JumpCondition condition)
        {
            var lo = memoryController.GetPosition(PC++);
            var hi = memoryController.GetPosition(PC++);

            switch (condition)
            {
            case JumpCondition.NZ:
                PC = FlagZ ? PC : (ushort)((hi << 8) | lo);
                break;

            case JumpCondition.Z:
                PC = FlagZ ? (ushort)((hi << 8) | lo) : PC;
                break;

            case JumpCondition.NC:
                PC = FlagC ? PC : (ushort)((hi << 8) | lo);
                break;

            case JumpCondition.C:
                PC = FlagC ? (ushort)((hi << 8) | lo) : PC;
                break;
            }

            ConsumeCycle(12);
        }
Example #4
0
        // TODO: RETI

        private static bool ShouldJump(JumpCondition condition, IFlags flags)
        {
            return((condition == JumpCondition.Carry && flags.Carry) ||
                   (condition == JumpCondition.NoCarry && !flags.Carry) ||
                   (condition == JumpCondition.Zero && flags.Zero) ||
                   (condition == JumpCondition.NotZero && !flags.Zero));
        }
Example #5
0
 public JumpIfCommand(Register registerOne, Register registerTwo, string label, JumpCondition condition)
 {
     RegisterOne = registerOne;
     RegisterTwo = registerTwo;
     Condition   = condition;
     Label       = label;
 }
Example #6
0
 public Jump(Z80Cpu cpu, IAddressMode <ushort> addressMode, JumpCondition condition, bool requiresConditionalInternalCycle = false, int additionalM1TCycles = 0, string mnemomic = "JP")
 {
     _cpu         = cpu;
     _addressMode = addressMode;
     _condition   = condition;
     _requiresConditionalInternalCycle = requiresConditionalInternalCycle;
     _internalCycle       = new InternalCycle(5);
     _additionalM1TCycles = additionalM1TCycles;
     _remainingM1Cycles   = additionalM1TCycles;
     Mnemonic             = mnemomic;
 }
        private double CalcValue(double value, JumpCondition cond, double currStateValue)
        {
            double tempValue = 1.0;

            if (cond.IsFirstAvailable.HasValue)
            {
                tempValue *= cond.IsFirstAvailable.Value ? 1 - firstProp : firstProp;
            }
            if (cond.IsSecondAvailable.HasValue)
            {
                tempValue *= cond.IsSecondAvailable.Value ? 1 - secondProp : secondProp;
            }
            tempValue *= currStateValue;
            return(value + tempValue);
        }
Example #8
0
        /// <summary>
        /// Return if following condition is true:
        /// Use with:
        /// cc = NZ, Return if Z flag is reset.
        /// cc = Z, Return if Z flag is set.
        /// cc = NC, Return if C flag is reset.
        /// cc = C, Return if C flag is set.
        /// </summary>
        /// <param name="condition">The condition.</param>
        private void RET_cc(JumpCondition condition)
        {
            switch (condition)
            {
            case JumpCondition.NZ:
                if (!FlagZ)
                {
                    var lo = memoryController.GetPosition(_SP++);
                    var hi = memoryController.GetPosition(_SP++);

                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.Z:
                if (FlagZ)
                {
                    var lo = memoryController.GetPosition(_SP++);
                    var hi = memoryController.GetPosition(_SP++);

                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.NC:
                if (!FlagC)
                {
                    var lo = memoryController.GetPosition(_SP++);
                    var hi = memoryController.GetPosition(_SP++);

                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.C:
                if (FlagC)
                {
                    var lo = memoryController.GetPosition(_SP++);
                    var hi = memoryController.GetPosition(_SP++);

                    PC = (ushort)((hi << 8) | lo);
                }
                break;
            }

            ConsumeCycle(8);
        }
Example #9
0
        /// <summary>
        /// Call address n if following condition is true:
        /// cc = NZ, Call if Z flag is reset.
        /// cc = Z, Call if Z flag is set.
        /// cc = NC, Call if C flag is reset.
        /// cc = C, Call if C flag is set.
        /// Use with:
        /// nn = two byte immediate value. (LS byte first.)
        /// </summary>
        /// <param name="condition">The condition.</param>
        private void CALL_cc_nn(JumpCondition condition)
        {
            var lo = memoryController.GetPosition(PC++);
            var hi = memoryController.GetPosition(PC++);

            switch (condition)
            {
            case JumpCondition.NZ:
                if (!FlagZ)
                {
                    memoryController.Write(SP, memoryController.GetPosition(PC));
                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.Z:
                if (FlagZ)
                {
                    memoryController.Write(SP, memoryController.GetPosition(PC));
                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.NC:
                if (!FlagC)
                {
                    memoryController.Write(SP, memoryController.GetPosition(PC));
                    PC = (ushort)((hi << 8) | lo);
                }
                break;

            case JumpCondition.C:
                if (FlagC)
                {
                    memoryController.Write(SP, memoryController.GetPosition(PC));
                    PC = (ushort)((hi << 8) | lo);
                }
                break;
            }

            ConsumeCycle(12);
        }
        private string AttachCondToNumFunc(string str, JumpCondition cond, double currStateValue)
        {
            var builder = new StringBuilder(str);

            if (builder.Length != 0)
            {
                builder.Append(" + ");
            }
            if (cond.IsFirstAvailable.HasValue)
            {
                builder.Append(cond.IsFirstAvailable.Value ? 1 - firstProp : firstProp);
            }
            if (cond.IsSecondAvailable.HasValue)
            {
                builder.Append(cond.IsFirstAvailable.HasValue ? "*" : "");
                builder.Append(cond.IsSecondAvailable.Value ? 1 - secondProp : secondProp);
            }
            builder.Append($"*{currStateValue:0.00000}");
            return(builder.ToString());
        }
        private string AttachCondToFunc(string str, JumpCondition cond, SystemState currState)
        {
            var builder = new StringBuilder(str);

            if (builder.Length != 0)
            {
                builder.Append(" + ");
            }
            if (cond.IsFirstAvailable.HasValue)
            {
                builder.Append(cond.IsFirstAvailable.Value ? "q1" : "π1");
            }
            if (cond.IsSecondAvailable.HasValue)
            {
                builder.Append(cond.IsFirstAvailable.HasValue ? "*" : "");
                builder.Append(cond.IsSecondAvailable.Value ? "q2" : "π2");
            }
            builder.Append("*P").Append(currState.Index);
            return(builder.ToString());
        }
Example #12
0
        /// <summary>
        /// If following condition is true then add n to current
        /// address and jump to it
        ///
        /// Use with:
        /// n = one byte signed immediate value
        ///
        /// cc = NZ, Jump if Z flag is reset.
        /// cc = Z, Jump if Z flag is set.
        /// cc = NC, Jump if C flag is reset.
        /// cc = C, Jump if C flag is set.
        /// </summary>
        /// <param name="condition">The condition.</param>
        private void JR_cc_n(JumpCondition condition)
        {
            var lo = memoryController.GetPosition(PC++);
            var hi = memoryController.GetPosition(PC++);

            var jumpValue = (short)((hi << 8) | lo);

            var updatedPC = PC;

            if (jumpValue >= 0)
            {
                updatedPC += (ushort)jumpValue;
            }
            else
            {
                updatedPC -= (ushort)-jumpValue;
            }

            switch (condition)
            {
            case JumpCondition.NZ:
                PC = FlagZ ? PC : updatedPC;
                break;

            case JumpCondition.Z:
                PC = FlagZ ? updatedPC : PC;
                break;

            case JumpCondition.NC:
                PC = FlagC ? PC : updatedPC;
                break;

            case JumpCondition.C:
                PC = FlagC ? updatedPC : PC;
                break;
            }

            ConsumeCycle(8);
        }
Example #13
0
        public static bool ShouldJump(this JumpCondition jumpCondition, Z80Cpu cpu)
        {
            var flags = cpu.Flags;

            switch (jumpCondition)
            {
            case JumpCondition.Unconditional:
                return(true);

            case JumpCondition.Carry:
                return(flags.HasFlag(Z80Flags.Carry_C));

            case JumpCondition.NonCarry:
                return(!flags.HasFlag(Z80Flags.Carry_C));

            case JumpCondition.Zero:
                return(flags.HasFlag(Z80Flags.Zero_Z));

            case JumpCondition.NonZero:
                return(!flags.HasFlag(Z80Flags.Zero_Z));

            case JumpCondition.ParityEven:
                return(flags.HasFlag(Z80Flags.ParityOverflow_PV));

            case JumpCondition.ParityOdd:
                return(!flags.HasFlag(Z80Flags.ParityOverflow_PV));

            case JumpCondition.SignNeg:
                return(flags.HasFlag(Z80Flags.Sign_S));

            case JumpCondition.SignPos:
                return(!flags.HasFlag(Z80Flags.Sign_S));

            case JumpCondition.RegBNotZero:
                return(--cpu.B != 0);
            }
            throw new InvalidOperationException("Invalid condition specified for jump");
        }
 public ComputeInstruction(Destination destinations, ComputationOption computationOptions, JumpCondition jumpConditions)
 {
     Destinations       = destinations;
     ComputationOptions = computationOptions;
     JumpConditions     = jumpConditions;
 }
Example #15
0
 public JumpOperation(Label where, JumpCondition condition)
 {
     Where     = where;
     Condition = condition;
 }
 /// <summary>
 /// Parse the jump
 /// </summary>
 /// <param name="s">Jump to be parsed</param>
 /// <returns>Standard Parser output</returns>
 public bool IsConditionalJump(string s)
 {
     return(JumpCondition.Match(s).Success);
 }
Example #17
0
 static Int64[] GetJumpInstruction(AdressInterpretation adressInterpretation, JumpCondition jumpCondition, JumpMode jumpMode, Int64 target)
 {
     Int64[] instruction=new Int64[5];
     instruction[0]=0;
     instruction[1]=(Int64)adressInterpretation;
     instruction[2]=(Int64)jumpCondition;
     instruction[3]=(Int64)jumpMode;
     instruction[4]=target;
     return instruction;
 }
Example #18
0
        /// <summary>
        /// Handles a compare instruction
        /// </summary>
        /// <param name="condition">The condition</param>
        private void HandleCompare(CompilationData compilationData, Instruction instruction, int index, JumpCondition condition)
        {
            var generatedCode      = compilationData.Assembler.GeneratedCode;
            var unsignedComparison = this.GenerateComparision(compilationData, instruction, index);
            int compareJump        = generatedCode.Count;
            int jump             = 0;
            int trueBranchStart  = 0;
            int falseBranchStart = 0;

            int target = 0;

            compilationData.Assembler.Jump(condition, target, unsignedComparison);

            //Both branches will have the same operand entry, reserve space
            compilationData.OperandStack.ReserveSpace();

            //False branch
            falseBranchStart = generatedCode.Count;
            compilationData.OperandStack.PushInt(0, false);
            jump = generatedCode.Count;
            compilationData.Assembler.Jump(JumpCondition.Always, 0);

            //True branch
            trueBranchStart = generatedCode.Count;
            compilationData.OperandStack.PushInt(1, false);

            //Set the jump targets
            NativeHelpers.SetInt(generatedCode, jump + 1, generatedCode.Count - trueBranchStart);
            NativeHelpers.SetInt(generatedCode, compareJump + 2, trueBranchStart - falseBranchStart);
        }
Example #19
0
        /// <summary>
        /// Handles a conditional branch
        /// </summary>
        /// <param name="condition">The condition</param>
        private void HandleConditionalBranch(CompilationData compilationData, Instruction instruction, int index, JumpCondition condition)
        {
            var unsignedComparison = this.GenerateComparision(compilationData, instruction, index);

            compilationData.Assembler.Jump(condition, 0, unsignedComparison);

            compilationData.UnresolvedBranches.Add(
                compilationData.Assembler.GeneratedCode.Count - 6,
                new UnresolvedBranchTarget(instruction.IntValue, 6));
        }
Example #20
0
 public RET(Z80Cpu cpu, JumpCondition condition) : base(cpu, WideRegister.PC)
 {
     _remainingM1Cycles = _additionalM1Cycles = condition == JumpCondition.Unconditional ? 0 : 1;
     _condition         = condition;
 }
Example #21
0
        private ICommand CommandFromString(string name, string[] arguments)
        {
            ICommand command = null;

            switch (name)
            {
            case "SET":
            {
                Register register = Registers[arguments[0]];
                int      value    = int.Parse(arguments[1]);

                command = new SetCommand(register, value);
                break;
            }

            case "ADD":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new AddCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "SUB":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new SubtractCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "MUL":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new MultiplyCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "DIV":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];
                Register registerOut = Registers[arguments[2]];

                command = new DivideCommand(registerOne, registerTwo, registerOut);
                break;
            }

            case "OUT":
            {
                if (Registers.ContainsKey(arguments[0]))
                {
                    Register register = Registers[arguments[0]];
                    command = new OutCommand(register);
                }
                else
                {
                    command = new OutCommand(arguments[0]);
                }

                break;
            }

            case "HLT":
            {
                command = new HaltCommand();
                break;
            }

            case "JMP":
            {
                string label = arguments[0];

                command = new JumpCommand(label);
                break;
            }

            case "JIF":
            {
                Register      registerOne     = Registers[arguments[0]];
                Register      registerTwo     = Registers[arguments[2]];
                string        conditionString = arguments[1];
                JumpCondition jumpCondition   = JumpCondition.Undefined;
                switch (conditionString)
                {
                case "==":
                    jumpCondition = JumpCondition.Equal;
                    break;

                case "<":
                    jumpCondition = JumpCondition.LessThan;
                    break;

                case "<=":
                    jumpCondition = JumpCondition.LessThanOrEqual;
                    break;

                case ">":
                    jumpCondition = JumpCondition.GreaterThan;
                    break;

                case ">=":
                    jumpCondition = JumpCondition.GreaterThanOrEqual;
                    break;

                case "!=":
                    jumpCondition = JumpCondition.NotEqual;
                    break;
                }

                string label = arguments[3];

                command = new JumpIfCommand(registerOne, registerTwo, label, jumpCondition);
                break;
            }

            case "CPY":
            {
                Register registerOne = Registers[arguments[0]];
                Register registerTwo = Registers[arguments[1]];

                command = new CopyCommand(registerOne, registerTwo);
                break;
            }

            default:
                if (name == "str:")
                {
                    string stringKey   = arguments[0];
                    string stringValue = string.Empty;
                    for (int i = 1; i < arguments.Length; i++)
                    {
                        stringValue += arguments[i];
                        if (i < arguments.Length)
                        {
                            stringValue += " ";
                        }
                    }

                    Program.RegisterString(stringKey, stringValue);
                }
                else if (name.Length > 1 && name.EndsWith(":"))
                {
                    Program.MakeLabel(name.Substring(0, name.Length - 1));
                    break;
                }
                break;
            }

            return(command);
        }
Example #22
0
 public static void ConditionalJump(IWriteonlyRegister <ushort> target, ushort address, JumpCondition condition, IFlags flags)
 {
     if (ShouldJump(condition, flags))
     {
         Load(target, address);
     }
 }
Example #23
0
 public static void ConditionalJumpByOffset(IRegister <ushort> target, sbyte offset, JumpCondition condition, IFlags flags)
 {
     ConditionalJump(target, (ushort)(target.Value + offset), condition, flags);
 }
Example #24
0
 public static void ConditionalCall(IRegister <ushort> pc, ushort address, IStack stack, JumpCondition condition, IFlags flags)
 {
     if (ShouldJump(condition, flags))
     {
         Call(pc, address, stack);
     }
 }
Example #25
0
 public static void ConditionalReturn(IWriteonlyRegister <ushort> pc, IStack stack, JumpCondition condition, IFlags flags)
 {
     if (ShouldJump(condition, flags))
     {
         Return(pc, stack);
     }
 }