package com.microchip.mplab.ipelib.device; import com.microchip.crownking.Pair; import com.microchip.mplab.crownkingx.xMemoryPartition; import com.microchip.mplab.crownkingx.xPIC; import com.microchip.mplab.ipelib.common.SharedData; import com.microchip.mplab.mdbcore.OptSupport.NumberOptionValidator; import com.microchip.mplab.mdbcore.OptSupport.RuntimeProperties; import java.util.ArrayList; public class PgmRangeStringValidator extends NumberOptionValidator { public String getHint() { String hint = ""; RuntimeProperties rtp = new RuntimeProperties(SharedData.getSingletonObject().getAssemblySession()); long Boundary = rtp.getProgramChunkSize(); return "Each range must be two hex numbers, representing the start and end addresses of the range, separated by a dash. Ranges must be separated by a comma(e.g. 0-ff, 200-2ff). Ranges must be aligned on a 0x" + Long.toHexString(Boundary) + " address boundary"; } public boolean isValid(String arg0) { boolean RetVal = false; if (arg0.isEmpty()) { return true; } else { try { RuntimeProperties rtp = new RuntimeProperties(SharedData.getSingletonObject().getAssemblySession()); xPIC pic = rtp.getPic(); long Boundary = rtp.getProgramChunkSize(); ArrayList> Ranges = new ArrayList<>(); xMemoryPartition[] XMPs = new xMemoryPartition[]{ pic.getMainPartition(), (xMemoryPartition)pic.getIDOntoPartition().get("first"), (xMemoryPartition)pic.getIDOntoPartition().get("second"), (xMemoryPartition)pic.getIDOntoPartition().get("only") }; String RangeString = arg0.replace(" ", ""); RangeString = RangeString.replace("0x", ""); String[] AddrGroup = RangeString.split(","); ArrayList AddrStrings = new ArrayList<>(); for (String s : AddrGroup) { AddrStrings.add(s); } ArrayList StringsToRemove = new ArrayList<>(); if (AddrStrings == null || AddrStrings.isEmpty()) { throw new Exception(); } for (xMemoryPartition XMP : XMPs) { try { if (XMP != null) { Ranges.clear(); Pair Range = XMP.getInstRange(); if (XMP.hasSPIFlashMemory()) { Range = XMP.getSPIFlashRange(); } Ranges.add(new Pair(Range.first, Range.second)); for (Pair R : Ranges) { StringsToRemove.clear(); long Start = (Long)R.first; long End = (Long)R.second - 1L; if ((Long)Range.second != 0L) { for (String S : AddrStrings) { try { this.CheckRangesFromRangeString(S, Start, End, Boundary); StringsToRemove.add(S); } catch (InternalNumberException var27) { this.why = var27.getLocalizedMessage(); } catch (Exception var28) { } } for (String Str : StringsToRemove) { AddrStrings.remove(Str); } } } } } catch (Exception var29) { } } if (AddrStrings.isEmpty()) { RetVal = true; } else if (this.why.isEmpty()) { this.why = String.format("Improperly formatted String %s", arg0); } } catch (Exception var30) { } return RetVal; } } protected void CheckRangesFromRangeString(String RangeString, long Start, long End, long Boundary) throws Exception { if (RangeString != null && !RangeString.isEmpty()) { String[] Addrs = RangeString.split("-"); if (Addrs.length != 2) { throw new Exception(); } long UserStart = Long.parseLong(Addrs[0], 16); long UserEnd = Long.parseLong(Addrs[1], 16); if (UserEnd < UserStart) { throw new InternalNumberException(String.format("End address 0x%x < start address 0x%x", UserEnd, UserStart)); } if (UserStart < Start || UserEnd > End) { throw new InternalNumberException(String.format("Range 0x%x-0x%x is outside the range of the device", UserStart, UserEnd)); } if (UserStart % Boundary != 0L) { throw new InternalNumberException(String.format("Start address 0x%x not aligned on a proper boundary", UserStart)); } if (UserEnd % Boundary != Boundary - 1L && UserEnd != End) { throw new InternalNumberException(String.format("End address 0x%x not aligned on a proper boundary", UserEnd)); } } } }