public string Disassemble(string operationString) { var operation = SplitOperationString(operationString); var instruction = LanguageDefinition.Instructions.Find(i => { return(i.TextDisplay.ToLower() == operation[0].ToLower()); }); if (instruction == null) { throw new Exception("The operation supplied does not match any known instruction definition."); } var binary = new BinaryString(instruction.Binary); var syntaxDefinition = new string[0]; var isMatch = false; foreach (var syntax in instruction.Syntax) { syntaxDefinition = SplitOperationString(syntax); if (operation.Length == syntaxDefinition.Length) { isMatch = true; break; } } if (!isMatch) { throw new Exception($"The operation does not match any known syntax definition for instruction {instruction.TextDisplay}"); } var bitIndex = 0; foreach (var arg in instruction.Args) { var definitionIndex = Array.FindIndex(syntaxDefinition, def => def == arg.Value); if (definitionIndex != -1) { var argBinary = GetArgByValue(arg.Value, operation[definitionIndex]); if (argBinary != null) { binary.Insert(bitIndex, argBinary.Value); } } bitIndex += arg.Size; } return(binary.ToHexString().Value); }
public string GetSyntaxArgByBinary(string placeholder, BinaryString argBinary) { if (LanguageDefinition.PlaceHolders.EERegisters.Contains(placeholder)) { return(LanguageDefinition.EERegisters .Find(r => r.Binary == argBinary.Value)?.TextDisplay); } else if (LanguageDefinition.PlaceHolders.COP0Registers.Equals(placeholder)) { return(LanguageDefinition.COP0Registers .Find(r => r.Binary == argBinary.Value)?.TextDisplay); } else if (LanguageDefinition.PlaceHolders.COP1Registers.Contains(placeholder)) { return(LanguageDefinition.COP1Registers .Find(r => r.Binary == argBinary.Value)?.TextDisplay); } else if (LanguageDefinition.PlaceHolders.JType.Equals(placeholder)) { return("$" + Convert.ToString(argBinary.ToInt() * 4, 16).PadLeft(8, '0')); } else if (LanguageDefinition.PlaceHolders.IType.Contains(placeholder)) { return("$" + argBinary.ToHexString().Value); } else if (LanguageDefinition.PlaceHolders.SA.Equals(placeholder)) { return(argBinary.ToInt().ToString()); } else if (LanguageDefinition.PlaceHolders.Code.Equals(placeholder)) { return(argBinary.ToHexString().Value.PadLeft(5, '0')); } else { return(null); } }