예제 #1
0
        public static bool CanApplyMovMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo)
        {
            var instructionOne = megaInstructionOne.Instruction;
            var instructionTwo = megaInstructionTwo.Instruction;

            if (instructionOne.MNEMONIC == "MOV")
            {
                if (InstructionUtil.IsLoadOrStoreInstruction(instructionTwo.FULL))
                {
                    // TODO: FIGURE SHIT OUT
                    if (instructionTwo.MNEMONIC == "ST" && instructionTwo.SOURCE1 == instructionOne.DESTINATION)
                    {
                        return(true);
                    }

                    return(false);
                }

                // TODO: take into account Source2 is null
//                if (instructionOne.DESTINATION == instructionTwo.SOURCE1)
//                {
//                    return true;
//                }
//                if(instructionOne.D)
                return(new[] { instructionTwo.SOURCE1, instructionTwo.SOURCE2 }.Contains(instructionOne.DESTINATION));
            }

            return(false);
        }
예제 #2
0
        public static bool CanApplyImmediateMerging(MegaInstruction megaInstructionOne, MegaInstruction megaInstructionTwo)
        {
            var instructionOne = megaInstructionOne.Instruction;
            var instructionTwo = megaInstructionTwo.Instruction;

            if (instructionOne.SOURCE2 == null || instructionTwo.SOURCE2 == null ||
                !instructionOne.SOURCE2.StartsWith("#") || !instructionTwo.SOURCE2.StartsWith("#"))
            {
                return(false);
            }

            if (InstructionUtil.IsLoadOrStoreInstruction(instructionTwo.FULL) ||
                InstructionUtil.IsLoadOrStoreInstruction(instructionOne.FULL))
            {
                return(false);
            }

            var combination = GenerateMnemonicCombination(instructionOne, instructionTwo);

            if (AllowedImmediateMergingCombinations.Contains(combination))
            {
                return(false);
            }

            return(instructionOne.DESTINATION == instructionTwo.SOURCE1);
        }
예제 #3
0
        public int Process(ICpu cpu, byte instruction)
        {
            var ins = Instructions.Get(instruction);

            if (ins is null)
            {
                throw new UnknownInstructionException(instruction);
            }
            var(mode, insType, cycles) = ins.OpCodes[instruction];
            var addrMode = AddressingModes.Get(mode);

            var(address, pageCrossed) = addrMode.Addressing(cpu.CpuRegisters, cpu.Bus);
            var extraCycles     = ins.Invoke(cpu, instruction, address);
            var cyclesIncrement = insType switch
            {
                InstructionType.Common => extraCycles,
                InstructionType.CrossingPage => InstructionUtil.GetCrossingPageClockCycles(pageCrossed) + extraCycles,
                InstructionType.Branch => InstructionUtil.GetBranchClockCycle(Convert.ToBoolean(extraCycles),
                                                                              pageCrossed),
                _ => extraCycles
            };

            return(cycles + cyclesIncrement);
        }
    }
        protected bool ImproveCode(List <MegaInstruction> instructions)
        {
            if (instructions.Count == 1)
            {
                return(false);
            }

            for (var index = 0; index < instructions.Count - 1; index++)
            {
                var instructionOne = instructions[index].Instruction;
                var instructionTwo = instructions[index + 1].Instruction;

                if (instructionOne.SOURCE2 == null || instructionTwo.SOURCE2 == null ||
                    !instructionOne.SOURCE2.Contains("#") || !instructionTwo.SOURCE2.Contains("#"))
                {
                    continue;
                }

                if (instructionOne.DESTINATION == instructionTwo.SOURCE1)
                {
                    if (InstructionUtil.IsLoadOrStoreInstruction(instructionTwo.FULL) ||
                        InstructionUtil.IsLoadOrStoreInstruction(instructionOne.FULL))
                    {
                        continue;
                    }

                    var mnemonicInstructionOne = instructionOne.MNEMONIC;
                    var mnemonicInstructionTwo = instructionTwo.MNEMONIC;
                    var combination            = $"{mnemonicInstructionOne} - {mnemonicInstructionTwo}";

                    if (allowedCombinations.Contains(combination))
                    {
                        continue;
                    }

                    if ((mnemonicInstructionOne == "DIV" && mnemonicInstructionTwo == "MULT") ||
                        (mnemonicInstructionOne == "MULT" && mnemonicInstructionTwo == "DIV"))
                    {
                        if (instructionOne.SOURCE2 == instructionTwo.SOURCE2)
                        {
                            var instructionLine = instructions[index].Line;
                            var indexOfLine     = instructionLine - 1;
                            AssemblyLines.RemoveRange(indexOfLine, 2);
                            var newInstruction = $"MOV {instructionTwo.SOURCE1}, {instructionOne.SOURCE1}";
                            AssemblyLines.Insert(indexOfLine, newInstruction);

                            return(true);
                        }
                    }

                    var x = 1 == 1;
                }
            }

            return(false);
        }