/* Copyright 2018-2022 Dmitry Isaenko This file is part of NS-USBloader. NS-USBloader is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. NS-USBloader is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with NS-USBloader. If not, see . */ package nsusbloader.Utilities.patches; import libKonogonka.Converter; import nsusbloader.NSLMain; public class BinToAsmPrinter { static { boolean notWindows = ! System.getProperty("os.name").toLowerCase().contains("windows"); if(notWindows && NSLMain.isCli){ ANSI_RESET = "\u001B[0m"; ANSI_GREEN = "\u001B[32m"; ANSI_BLUE = "\u001B[34m"; ANSI_YELLOW = "\u001B[33m"; ANSI_PURPLE = "\u001B[35m"; ANSI_CYAN = "\u001B[36m"; ANSI_RED = "\u001B[31m"; } else { ANSI_RESET = ANSI_RED = ANSI_GREEN = ANSI_BLUE = ANSI_YELLOW = ANSI_PURPLE = ANSI_CYAN = ""; } } private static final String ANSI_RESET; private static final String ANSI_RED; private static final String ANSI_GREEN; private static final String ANSI_BLUE; private static final String ANSI_YELLOW; private static final String ANSI_PURPLE; private static final String ANSI_CYAN; public static String print(int instructionExpression, int offset){ if (instructionExpression == 0xd503201f) return printNOP(instructionExpression); if ((instructionExpression & 0x7FE0FFE0) == 0x2A0003E0) { return printMOVRegister(instructionExpression); } switch ((instructionExpression >> 23 & 0b011111111)){ case 0xA5: return printMOV(instructionExpression); case 0x62: if (((instructionExpression & 0x1f) == 0x1f)){ return printCMN(instructionExpression); } } switch (instructionExpression >> 24 & 0xff) { case 0x34: case 0xb4: return printCBZ(instructionExpression, offset); case 0xb5: case 0x35: return printCBNZ(instructionExpression, offset); case 0x36: case 0xb6: return printTBZ(instructionExpression, offset); case 0x54: return printBConditional(instructionExpression, offset); } switch ((instructionExpression >> 26 & 0b111111)) { case 0x5: return printB(instructionExpression, offset); case 0x25: return printBL(instructionExpression, offset); } return printUnknown(instructionExpression); } public static String printSimplified(int instructionExpression, int offset){ if (instructionExpression == 0xd503201f) return printNOPSimplified(instructionExpression, offset); if ((instructionExpression & 0x7FE0FFE0) == 0x2A0003E0) { return printMOVRegisterSimplified(instructionExpression, offset); } switch (instructionExpression >> 22 & 0b1011111111) { case 0x2e5: return printLRDImmUnsignSimplified(instructionExpression, offset); case 0xe5: return printLRDBImmUnsignSimplified(instructionExpression, offset); } if ((instructionExpression >> 21 & 0x7FF) == 0x1C2) return printImTooLazy("LDURB", instructionExpression, offset); // same to (afterJumpExpression >> 23 & 0x1F9) != 0xA1 switch (instructionExpression >> 22 & 0x1FF){ case 0xA3: // 0b10100011 case 0xA7: // 0b10100111 case 0xA5: // 0b10100101 return printImTooLazy("LDP", instructionExpression, offset); } switch ((instructionExpression >> 23 & 0x1ff)){ case 0xA5: return printMOVSimplified(instructionExpression, offset); case 0x22: return printImTooLazy("ADD", instructionExpression, offset); case 0x62: if (((instructionExpression & 0x1f) == 0x1f)){ return printCMNSimplified(instructionExpression, offset); } case 0xA2: return printSUBSimplified(instructionExpression, offset); case 0xE2: case 0x1e2: return printCMPSimplified(instructionExpression, offset); case 0x24: case 0x124: return printANDSimplified(instructionExpression, offset); } switch (instructionExpression >> 24 & 0xff) { case 0x34: case 0xb4: return printCBZSimplified(instructionExpression, offset); case 0xb5: case 0x35: return printCBNZSimplified(instructionExpression, offset); case 0x36: case 0xb6: return printTBZSimplified(instructionExpression, offset); case 0x54: return printBConditionalSimplified(instructionExpression, offset); case 0xeb: case 0x6b: if ((instructionExpression & 0x1f) == 0b11111) return printCMPShiftedRegisterSimplified(instructionExpression, offset); } switch (instructionExpression >> 26 & 0b111111) { case 0x5: return printBSimplified(instructionExpression, offset); case 0x25: return printBLSimplified(instructionExpression, offset); } if ((instructionExpression >> 10 & 0x3FFFFF) == 0x3597c0 && ((instructionExpression & 0x1F) == 0)) return printRetSimplified(instructionExpression, offset); return printUnknownSimplified(instructionExpression, offset); } private static String printCBZ(int instructionExpression, int offset){ int conditionalJumpLocation = ((instructionExpression >> 5 & 0x7FFFF) * 4 + offset) & 0xfffff; return String.format(ANSI_YELLOW + "sf == 0 ? else \n" + "CBZ ,