\hypertarget{classeu_1_1javaexperience_1_1electronic_1_1IhexCli}{}\section{eu.\+javaexperience.\+electronic.\+Ihex\+Cli Class Reference}
\label{classeu_1_1javaexperience_1_1electronic_1_1IhexCli}\index{eu.\+javaexperience.\+electronic.\+Ihex\+Cli@{eu.\+javaexperience.\+electronic.\+Ihex\+Cli}}
\subsection*{Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_a78615be98064c0520279d500813ee522}\label{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_a78615be98064c0520279d500813ee522}} 
void {\bfseries validate} (String file)  throws Exception 	
\item 
\mbox{\Hypertarget{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_a1b3dcbbee9020ee282bae03c2fdd18b4}\label{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_a1b3dcbbee9020ee282bae03c2fdd18b4}} 
void {\bfseries size} (String file)  throws Exception 	
\item 
void \hyperlink{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_aef1420b782f742924d36112b22a00931}{replace\+\_\+interrupts} (String... params)  throws Exception 	
\end{DoxyCompactItemize}
\subsection*{Static Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\Hypertarget{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_ad7a1aecc3085420c5a000a2883ef1851}\label{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_ad7a1aecc3085420c5a000a2883ef1851}} 
static void {\bfseries main} (String\mbox{[}$\,$\mbox{]} args)  throws Throwable 	
\end{DoxyCompactItemize}


\subsection{Detailed Description}


Definition at line 17 of file Ihex\+Cli.\+java.



\subsection{Member Function Documentation}
\mbox{\Hypertarget{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_aef1420b782f742924d36112b22a00931}\label{classeu_1_1javaexperience_1_1electronic_1_1IhexCli_aef1420b782f742924d36112b22a00931}} 
\index{eu\+::javaexperience\+::electronic\+::\+Ihex\+Cli@{eu\+::javaexperience\+::electronic\+::\+Ihex\+Cli}!replace\+\_\+interrupts@{replace\+\_\+interrupts}}
\index{replace\+\_\+interrupts@{replace\+\_\+interrupts}!eu\+::javaexperience\+::electronic\+::\+Ihex\+Cli@{eu\+::javaexperience\+::electronic\+::\+Ihex\+Cli}}
\subsubsection{\texorpdfstring{replace\+\_\+interrupts()}{replace\_interrupts()}}
{\footnotesize\ttfamily void eu.\+javaexperience.\+electronic.\+Ihex\+Cli.\+replace\+\_\+interrupts (\begin{DoxyParamCaption}\item[{String...}]{params }\end{DoxyParamCaption}) throws Exception\hspace{0.3cm}{\ttfamily [inline]}}

First version for atmega -\/i input\+File -\/o output\+File (optional) -\/d offset\+Address -\/a (address to replace) (optional, is missing the values appears most will be replaced) 

Definition at line 58 of file Ihex\+Cli.\+java.


\begin{DoxyCode}
59     \{
60         \textcolor{comment}{//-i inputFile -o outFile -d (offset address)}
61         
62         Map<String, List<String>> pa = CliTools.parseCliOpts(params);
63         
64         CliEntry<String> IN = CliEntry.createFirstArgParserEntry((e)->e, \textcolor{stringliteral}{"Input ihex format file"}, \textcolor{stringliteral}{"i"}, \textcolor{stringliteral}{"
      -input-file"});
65         CliEntry<String> OUT = CliEntry.createFirstArgParserEntry((e)->e, \textcolor{stringliteral}{"Output ihex format file"}, \textcolor{stringliteral}{"o"}, \textcolor{stringliteral}{"
      -output-file"});
66         CliEntry<Integer> OFF = CliEntry.createFirstArgParserEntry(Integer::parseInt, \textcolor{stringliteral}{"Program code offset"}
      , \textcolor{stringliteral}{"p"}, \textcolor{stringliteral}{"-program-offset"});
67         CliEntry<Integer> ADDR = CliEntry.createFirstArgParserEntry(Integer::parseInt, \textcolor{stringliteral}{"Address to replace
       (optional)"}, \textcolor{stringliteral}{"a"}, \textcolor{stringliteral}{"-address-to-replace"});
68         
69         CliEntry[] cli\_entries = \{IN, OUT, OFF, ADDR\};
70         
71         String un = CliTools.getFirstUnknownParam(pa, cli\_entries);
72         
73         \textcolor{keywordflow}{if}(null != un)
74         \{
75             System.err.println(\textcolor{stringliteral}{"Invalid Cli switch: "}+un);
76             System.err.println(\textcolor{stringliteral}{"Usage of ihex.replace\_interrupts:\(\backslash\)n"});
77             System.err.println(CliTools.renderListAllOption(cli\_entries));
78             System.exit(1);
79         \}
80         
81         String from = IN.tryParse(pa); 
82         String to = OUT.tryParseOrDefault(pa, null); 
83         
84         Integer offset = OFF.tryParse(pa);
85         
86         Integer address = ADDR.tryParseOrDefault(pa, null);
87         
88         \textcolor{keywordtype}{boolean} fail = \textcolor{keyword}{false};
89         
90         \textcolor{keywordflow}{if}(null == from)
91         \{
92             System.err.println(\textcolor{stringliteral}{"Ihex file not specified, use the -i cli switch to do that."});
93             fail = \textcolor{keyword}{true};
94         \}
95         
96         \textcolor{keywordflow}{if}(null != from && ! \textcolor{keyword}{new} File(from).exists())
97         \{
98             System.err.println(\textcolor{stringliteral}{"Ihex file `"}+from+\textcolor{stringliteral}{"` not exist!"});
99             fail = \textcolor{keyword}{true};
100         \}
101         
102         \textcolor{keywordflow}{if}(null == offset)
103         \{
104             System.err.println(\textcolor{stringliteral}{"Program relay offset not specified, use the -p cli switch to do that."});
105             fail = \textcolor{keyword}{true};
106         \}
107         
108         \textcolor{keywordflow}{if}(offset < 0 || offset >= 0x1 << 16)
109         \{
110             System.err.println(\textcolor{stringliteral}{"Specified program relay offset `+offset+` is outside of the memory region
       (0-65536)."});
111             fail = \textcolor{keyword}{true};
112         \}
113         
114         \textcolor{keywordflow}{if}(fail)
115         \{
116             System.exit(2);
117         \}
118         
119         \textcolor{keywordflow}{if}(null == to)
120         \{
121             to = from;
122         \}
123         
124         IntelHexFile file = IntelHexFile.loadFile(from);
125         
126         List<CodeSegment> codes = file.getCode();
127         CodeSegment start = null;
128         \textcolor{keywordflow}{for}(CodeSegment code:codes)
129         \{
130             \textcolor{keywordflow}{if}(0 == code.startAddress)
131             \{
132                 start = code;
133                 \textcolor{keywordflow}{break};
134             \}
135         \}
136         
137         \textcolor{keywordflow}{if}(null == start)
138         \{
139             System.err.println(\textcolor{stringliteral}{"Ihex doesn't contains code segment starts from 0 address."});
140             System.exit(3);
141         \}
142         
143         \textcolor{keywordtype}{int} rewite = -1;
144         
145         \textcolor{keywordflow}{if}(null != address)
146         \{
147             rewite = address;
148         \}
149         \textcolor{keywordflow}{else}
150         \{
151             \textcolor{comment}{//find the most used destination address to rewrite.}
152             \textcolor{comment}{//start from the 4th index because first address is the reset vector}
153             
154             SmallMap<Integer, Integer> dstAddresses = \textcolor{keyword}{new} SmallMap<>();
155             \textcolor{keywordtype}{int} addr;
156             \textcolor{keywordflow}{for}(addr = 4;addr<start.data.length;addr+=4)
157             \{
158                 \textcolor{keywordflow}{if}
159                 (
160                     ((byte) 0x0c) != start.data[addr]
161                 ||
162                     ((byte) 0x94) != start.data[addr+1]
163                 )
164                 \{
165                     \textcolor{keywordflow}{break};
166                 \}
167                 
168                 \textcolor{comment}{//little endian to big endian}
169                 \textcolor{keywordtype}{int} dst = ((start.data[addr+3] << 8) | start.data[addr+2]) * 2;
170                 
171                 MapTools.incrementCount(dstAddresses, dst);
172             \}
173             
174             \textcolor{keywordflow}{if}(dstAddresses.isEmpty())
175             \{
176                 System.err.println(\textcolor{stringliteral}{"No interrupt function found between 4 and "}+addr+\textcolor{stringliteral}{" address"});
177                 System.exit(3);
178             \}
179             
180             \textcolor{keywordtype}{int} rDstAddr = 0;
181             \textcolor{keywordtype}{int} rDstAddrCount = 0;
182             
183             \textcolor{keywordflow}{for}(Entry<Integer, Integer> kv:dstAddresses.entrySet())
184             \{
185                 \textcolor{keywordflow}{if}(kv.getValue() == rDstAddrCount)
186                 \{
187                     System.err.println(\textcolor{stringliteral}{"Multiple interrupt addresses used for the same time so specify
       explicitly the right one: "}+dstAddresses);
188                     System.exit(4);
189                     
190                 \}
191                 \textcolor{keywordflow}{else} \textcolor{keywordflow}{if}(kv.getValue() > rDstAddrCount)
192                 \{
193                     rDstAddr = kv.getKey();
194                     rDstAddrCount = kv.getValue();
195                 \}
196             \}
197             
198             \textcolor{keywordflow}{if}(0 == rDstAddrCount)
199             \{
200                 System.out.println(\textcolor{stringliteral}{"Nothing to rewrite"});
201                 System.exit(5);
202             \}
203             
204             rewite = rDstAddr;
205         \}
206         
207         System.out.println(\textcolor{stringliteral}{"Rewriting: "}+rewite);
208         
209         \textcolor{keywordflow}{for}(\textcolor{keywordtype}{int} addr = 4;addr<start.data.length;addr+=4)
210         \{
211             \textcolor{keywordflow}{if}
212             (
213                 ((byte) 0x0c) != start.data[addr]
214             ||
215                 ((byte) 0x94) != start.data[addr+1]
216             )
217             \{
218                 \textcolor{keywordflow}{break};
219             \}
220             
221             \textcolor{comment}{//little endian to big endian}
222             \textcolor{keywordtype}{int} jmp = ((start.data[addr+3] << 8) | start.data[addr+2]) * 2;
223             
224             \textcolor{keywordflow}{if}(jmp == rewite)
225             \{
226                 jmp = (addr+offset)/2;\textcolor{comment}{//because address points to a code word}
227                 start.data[addr+2] = (byte) (jmp & 0xff);
228                 start.data[addr+3] =  (byte) ((jmp >> 8) & 0xff);
229             \}
230         \}
231         
232         \textcolor{comment}{//TODO assemble ihex and write}
233         IntelHexFile dst = IntelHexFile.fromSegments(codes);
234         dst.writeToFile(to);
235     \}
\end{DoxyCode}


The documentation for this class was generated from the following file\+:\begin{DoxyCompactItemize}
\item 
/home/javaexperience/git-\/repositories/javaexperience/uartbus/source/java/uartbus/src/main/java/eu/javaexperience/electronic/Ihex\+Cli.\+java\end{DoxyCompactItemize}
