public bool DisassembleFile(IECUFile m_trionicFile, string inputfile, string outputfile/*, long startaddress*/, SymbolCollection symbols) { // recursive method when jsr was found mnemonics = new MNemonicCollection(); labels = new MNemonicCollection(); /*labels = */findLabels(m_trionicFile, inputfile); _passOne = false; uint i, t, seg, adr; long addr, endaddr, adrcntr, trgaddr, trgaddr1, trgaddr2, trgaddr3, offaddr; byte ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //byte n1, n2, n3, n4; //uint infile = 0, outfile = 0, uint addoff = 0; string inname, outname, offsetval; //byte inname[80], outname[80], offsetval[40]; //byte str[80],cmd[80]; string str, cmd; str = string.Empty; for (int temp = 0; temp < 8; temp++) { A_reg.SetValue(0, temp); D_reg.SetValue(0, temp); } m_symbols = symbols; swap = 0; addr = offaddr = 0; inname = inputfile; //infile = 1; // outname = outputfile; //outfile = 1; //addr = startaddress; /********************* DISASSEMBLY STARTS HERE *********************/ /* Read all the preceding words first */ adrcntr = 0L; //StreamWriter sw = new StreamWriter(outname, false); FileStream fsbr = new FileStream(inname, FileMode.Open, FileAccess.Read); if (fsbr == null) return false; BinaryReader br = new BinaryReader(fsbr); if (br == null) { fsbr.Close(); //sw.Close(); return false; } //fsbr.Position = addr; adrcntr = addr; // iterate through all functions // first get all the pointers to work from func_count = 0; FileInfo fi = new FileInfo(inputfile); CastProgressEvent("Starting disassembly", 0, ProgressType.DisassemblingVectors); for (int vec = 1; vec <= 127; vec++) { int percentage = (vec * 100) / 127; CastProgressEvent("Disassembling vectors", percentage, ProgressType.DisassemblingVectors); long vector = m_trionicFile.GetStartVectorAddress(inputfile, vec); long len = fi.Length; if (len == 0x20000) len = 0x60000; if (vector != 0 && vector < len * 2) { //Console.WriteLine("Vector: " + vec.ToString() + " addr: " + vector.ToString("X8")); try { DisassembleFunction(vector, symbols, fsbr, br, len); } catch (Exception E) { Console.WriteLine("Failed to handle vector: " + E.Message); } } } CastProgressEvent("Translating vector labels", 0, ProgressType.TranslatingVectors); //Console.WriteLine("Translating vector labels"); long[] vectors = m_trionicFile.GetVectorAddresses(m_trionicFile.GetFileInfo().Filename); int lblcount = 0; foreach (MNemonicHelper label in labels) { int percentage = (lblcount++ * 100) / labels.Count; CastProgressEvent("Translating vector labels", percentage, ProgressType.TranslatingVectors); for (i = 0; i < 128; i++) { if (label.Address == /*m_trionicFile.GetStartVectorAddress(m_trionicFileInformation.Filename, i)*/ Convert.ToInt64(vectors.GetValue(i))) { switch (i) { case 1: label.Mnemonic = "INIT_PROGRAM:"; break; case 2: label.Mnemonic = "BUS_ERROR:"; break; case 3: label.Mnemonic = "ADDRESS_ERROR:"; break; case 4: label.Mnemonic = "ILLEGAL_INSTRUCTION:"; break; case 5: label.Mnemonic = "DIVIDE_BY_ZERO:"; break; case 6: label.Mnemonic = "CHK12_INSTR:"; break; case 7: label.Mnemonic = "TRAPx_INSTR:"; break; case 8: label.Mnemonic = "PRIV_VIOLATION:"; break; case 9: label.Mnemonic = "TRACE:"; break; case 10: label.Mnemonic = "L1010_EMUL:"; break; case 11: label.Mnemonic = "L1111_EMUL:"; break; case 12: label.Mnemonic = "HW_BREAKPOINT:"; break; case 13: label.Mnemonic = "RESERVED:"; break; case 14: label.Mnemonic = "FMT_ERR1:"; break; case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: label.Mnemonic = "UNASSIGNED:"; break; case 23: label.Mnemonic = "FFFFFFFF:"; break; case 24: label.Mnemonic = "SPURIOUS_INTERRUPT:"; break; case 25: label.Mnemonic = "LEVEL1_INTERUPT_AUTOVECTOR:"; break; case 26: label.Mnemonic = "LEVEL2_INTERUPT_AUTOVECTOR:"; break; case 27: label.Mnemonic = "LEVEL3_INTERUPT_AUTOVECTOR:"; break; case 28: label.Mnemonic = "LEVEL4_INTERUPT_AUTOVECTOR:"; break; case 29: label.Mnemonic = "LEVEL5_INTERUPT_AUTOVECTOR:"; break; case 30: label.Mnemonic = "LEVEL6_INTERUPT_AUTOVECTOR:"; break; case 31: label.Mnemonic = "LEVEL7_INTERUPT_AUTOVECTOR:"; break; case 32: label.Mnemonic = "TAP0_INSTRUCTION_VECTOR:"; break; case 33: label.Mnemonic = "TAP1_INSTRUCTION_VECTOR:"; break; case 34: label.Mnemonic = "TAP2_INSTRUCTION_VECTOR:"; break; case 35: label.Mnemonic = "TAP3_INSTRUCTION_VECTOR:"; break; case 36: label.Mnemonic = "TAP4_INSTRUCTION_VECTOR:"; break; case 37: label.Mnemonic = "TAP5_INSTRUCTION_VECTOR:"; break; case 38: label.Mnemonic = "TAP6_INSTRUCTION_VECTOR:"; break; case 39: label.Mnemonic = "TAP7_INSTRUCTION_VECTOR:"; break; case 40: label.Mnemonic = "TAP8_INSTRUCTION_VECTOR:"; break; case 41: label.Mnemonic = "TAP9_INSTRUCTION_VECTOR:"; break; case 42: label.Mnemonic = "TAP10_INSTRUCTION_VECTOR:"; break; case 43: label.Mnemonic = "TAP11_INSTRUCTION_VECTOR:"; break; case 44: label.Mnemonic = "TAP12_INSTRUCTION_VECTOR:"; break; case 45: label.Mnemonic = "TAP13_INSTRUCTION_VECTOR:"; break; case 46: label.Mnemonic = "TAP14_INSTRUCTION_VECTOR:"; break; case 47: label.Mnemonic = "TAP15_INSTRUCTION_VECTOR:"; break; default: label.Mnemonic = "VECTOR_" + i.ToString() + ":"; break; } break; } } } /* Console.WriteLine("Translating known functions"); CastProgressEvent("Translating known functions", 0, ProgressType.TranslatingLabels); lblcount = 0; foreach (MNemonicHelper label in labels) { int percentage = (lblcount++ * 100) / labels.Count; CastProgressEvent("Translating known functions", percentage, ProgressType.TranslatingLabels); foreach (MNemonicHelper mnemonic in mnemonics) { if (mnemonic.Mnemonic.Contains("JSR") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); // break; } else if (mnemonic.Mnemonic.Contains("BEQ") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BRA") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BLS") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BNE") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BHI") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BCS") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BCC") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BGE") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BLT") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BGT") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } else if (mnemonic.Mnemonic.Contains("BLE") && mnemonic.Mnemonic.Contains(label.Address.ToString("X8"))) { mnemonic.Mnemonic = mnemonic.Mnemonic.Replace(label.Address.ToString("X8"), label.Mnemonic.Replace(":", "")); } } }*/ CastProgressEvent("Adding labels", 0, ProgressType.AddingLabels); //Console.WriteLine("Adding labels"); lblcount = 0; foreach (MNemonicHelper label in labels) { int percentage = (lblcount++ * 100) / labels.Count; CastProgressEvent("Adding labels", percentage, ProgressType.AddingLabels); label.Address--; // for sequencing mnemonics.Add(label); } //Console.WriteLine("Sorting data"); CastProgressEvent("Sorting mnemonics", 0, ProgressType.SortingData); mnemonics.SortColumn = "Address"; mnemonics.SortingOrder = Trionic5Controls.GenericComparer.SortOrder.Ascending; mnemonics.Sort(); CastProgressEvent("Sorting mnemonics", 100, ProgressType.SortingData); return true; }
private MNemonicCollection findLabels(IECUFile m_trionicFile, string inputfile) { _labels = new MNemonicCollection(); uint i, t, seg, adr; long addr, endaddr, adrcntr, trgaddr, trgaddr1, trgaddr2, trgaddr3, offaddr; byte ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, ch9, ch10; //byte n1, n2, n3, n4; //uint infile = 0, outfile = 0, uint addoff = 0; string inname, outname, offsetval; //byte inname[80], outname[80], offsetval[40]; //byte str[80],cmd[80]; string str, cmd; str = string.Empty; for (int temp = 0; temp < 8; temp++) { A_reg.SetValue(0, temp); D_reg.SetValue(0, temp); } swap = 0; addr = offaddr = 0; inname = inputfile; adrcntr = 0L; FileStream fsbr = new FileStream(inname, FileMode.Open, FileAccess.Read); if (fsbr == null) return _labels; BinaryReader br = new BinaryReader(fsbr); if (br == null) { fsbr.Close(); return _labels; } fsbr.Position = addr; adrcntr = addr; // iterate through all functions // first get all the pointers to work from func_count = 0; FileInfo fi = new FileInfo(inputfile); CastProgressEvent("Start analyzing", 0, ProgressType.PassOne); for (int vec = 1; vec <= 127; vec++) { int percentage = (vec * 100) / 127; CastProgressEvent("Analyzing", percentage, ProgressType.PassOne); long vector = m_trionicFile.GetStartVectorAddress(inputfile, vec); long len = fi.Length; if (len == 0x20000) len = 0x60000; if (vector != 0 && vector < len * 2) { try { LoadLabels(vector, fsbr, br, len); } catch (Exception E) { Console.WriteLine("Failed to handle vector: " + E.Message); } } } Console.WriteLine("Found " + _labels.Count.ToString() + " in pass one"); return _labels; }