private bool AnalyzeInstructionType(AssemblyCode code, List <AssembleError> errorList)
        {
            InstructionAnalyzeInfos = new InstructionAnalyzeInfo[code.Sections.Count][];

            for (int sectIdx = 0; sectIdx < code.Sections.Count; sectIdx++)
            {
                var sect = code.Sections[sectIdx];
                InstructionAnalyzeInfos[sectIdx] = new InstructionAnalyzeInfo[sect.AllInstructions.Length];

                for (int instrIdx = 0; instrIdx < sect.AllInstructions.Length; instrIdx++)
                {
                    var instr = sect.AllInstructions[instrIdx];

                    int type;
                    if (!InstructionAssembler.SearchAssemblerType(instr, out type, errorList))
                    {
                        return(false);
                    }
                    InstructionAnalyzeInfos[sectIdx][instrIdx].Type = type;

                    int  length;
                    bool needToAlign;
                    if (!InstructionAssembler.EstimateInstructionLength(instr, type, out length, out needToAlign, errorList))
                    {
                        return(false);
                    }
                    InstructionAnalyzeInfos[sectIdx][instrIdx].LengthInHalfWord = length;
                    InstructionAnalyzeInfos[sectIdx][instrIdx].NeedToAlign      = needToAlign;
                }
            }

            return(true);
        }
        private bool GenerateMemoryContents(AssemblyCode code, Execute.ExecuteSetupData setupData, List <AssembleError> errorList)
        {
            //Generate memory contents of instructions
            {
                ushort[] buffer = new ushort[InstructionAssembler.MaxLengthPerInstruction * 2];
                for (int sectIdx = 0; sectIdx < code.Sections.Count; sectIdx++)
                {
                    Section sect = code.Sections[sectIdx];

                    for (int instrIdx = 0; instrIdx < sect.AllInstructions.Length; instrIdx++)
                    {
                        Instruction instr = sect.AllInstructions[instrIdx];

                        int  length;
                        bool needToAlign;
                        if (!InstructionAssembler.AssembleInstruction(instr, InstructionAnalyzeInfos[sectIdx][instrIdx].Type, buffer, out length, out needToAlign, errorList))
                        {
                            return(false);
                        }

                        //Write (Address is specified by byte address)
                        for (int pos = 0; pos < length; pos++)
                        {
                            WriteInstructionData(instr, buffer, pos, setupData);
                        }
                    }
                }
            }


            bool failed = false;

            int[] notifier = new int[2] {
                0, 1
            };
            System.Threading.Tasks.Task genThread = new System.Threading.Tasks.Task(() =>
            {
                notifier[1] = code.VariableAnalyzeResult.Readonlys.Count + code.VariableAnalyzeResult.Readwrites.Count;
                //Assign address of variables
                {
                    //Read-only variables
                    for (int i = 0; i < code.VariableAnalyzeResult.Readonlys.Count; i++)
                    {
                        if (!GenerateMemoryContentForVariable(code.VariableAnalyzeResult.Readonlys[i], setupData, errorList))
                        {
                            failed = true;
                            return;
                        }
                        notifier[0] = i;
                    }

                    //Read-write variables
                    for (int i = 0; i < code.VariableAnalyzeResult.Readwrites.Count; i++)
                    {
                        if (!GenerateMemoryContentForVariable(code.VariableAnalyzeResult.Readwrites[i], setupData, errorList))
                        {
                            failed = true;
                            return;
                        }
                        notifier[0] = i + code.VariableAnalyzeResult.Readonlys.Count;
                    }
                }
            });

            genThread.Start();
            MessageManager.ShowLine("");
            System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
            while (!genThread.Wait(333))
            {
                Console.Write($"\r        Processing { notifier[0] } / { notifier[1] }...");
            }
            if (failed)
            {
                return(false);
            }

            return(true);
        }