Beispiel #1
0
        public void LoadPrograme(string programeFilePath)
        {
            instructions.Clear();

            //get programe start address(default:1024)
            int programeStartAddress = 0;

            try
            {
                programeStartAddress = Convert.ToInt32(ProgrameStartAddressTextBox.Text);
                if (programeStartAddress % 4 != 0)
                {
                    programeStartAddress             = programeStartAddress - programeStartAddress % 4;
                    ProgrameStartAddressTextBox.Text = programeStartAddress.ToString();
                    System.Windows.MessageBox.Show("代码段导入首地址必须为4的倍数!已自动修改为不大于输入地址的最大的4的倍数!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("代码段导入首地址出错!请检查是否有效!\n详细信息:" + ex.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Error);
                ProgrameStartAddressTextBox.Text = "1024";
                return;
            }

            //initialize PC
            registers[32].Value = (uint)programeStartAddress;

            //save programe into address begin from programeStartAddress
            FileStream programeFile;

            byte[] programeFileBytes;
            string programeFileContent;

            try
            {
                programeFile      = new FileStream(programeFilePath, FileMode.Open, FileAccess.Read);
                programeFileBytes = new byte[programeFile.Length];
                programeFile.Read(programeFileBytes, 0, (int)programeFile.Length);
                programeFileContent = Encoding.Default.GetString(programeFileBytes);

                //ignore annotation
                string pattern = @"(/\*)((.|\n)*?)(\*/)";
                Regex  rgx     = new Regex(pattern);
                programeFileContent = rgx.Replace(programeFileContent, " ");

                int           count = 0;
                StringBuilder tmpSB = new StringBuilder();
                foreach (var item in programeFileContent)
                {
                    if (item >= '0' && item <= '1')
                    {
                        tmpSB.Append(item);
                        count++;
                        if (count == 32)
                        {
                            Instruction instruction = new Instruction();
                            instruction.MachineCode  = ConvertFromBinaryStringToUInt(tmpSB.ToString());
                            instruction.AssemblyCode = Decode(instruction.MachineCode);
                            instruction.Address      = programeStartAddress;
                            instructions.Add(instruction);
                            ram.Set4Bit(programeStartAddress, instruction.MachineCode);
                            programeStartAddress += 4;
                            count = 0;
                            tmpSB.Clear();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("加载程序文件出错!请检查文件是否有效!\n详细信息:" + ex.Message, "警告", MessageBoxButton.OK, MessageBoxImage.Error);
                ProgrameFilePathTextBox.Text = "";
                return;
            }
        }