Skip to content

Commit a3ebbb6

Browse files
author
Stuart Cording
committed
Added ihStat project to repo.
First commit of ihStat project code.
1 parent 6d89aeb commit a3ebbb6

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

utilities/ihStat/ihStat.cpp

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*******************************************************************************
2+
*
3+
* INTEL HEX UTILITY - IHSTAT
4+
*
5+
*******************************************************************************/
6+
7+
/*******************************************************************************
8+
* ihStat.cpp
9+
*
10+
* This utility is designed to calculate basic statistics for a given Intel HEX
11+
* format file.
12+
*
13+
* The utility generates:
14+
* - First address found
15+
* - Last address found
16+
* - Total size of this region
17+
* - Regions and/or locations that contain no data entries
18+
* - Values stored in IP/ES or EIP registers
19+
* - Total number of bytes in file
20+
*
21+
* The utility also checks the source file for issues. Regardless of the issues
22+
* potentially found, the utility will do its best to generate a list of
23+
* statistics. These issues will also be output as a list of errors and
24+
* warnings.
25+
*
26+
* For more information and the latest release, please visit this projects home
27+
* page at http://codinghead.github.com/Intel-HEX-Class
28+
* To participate in the project or for other enquiries, please contact Stuart
29+
* Cording at [email protected]
30+
*
31+
* Licensing Information
32+
* Copyright (c) 2012 Stuart Cording
33+
*
34+
* Permission is hereby granted, free of charge, to any person obtaining a copy
35+
* of this software and associated documentation files (the "Software"), to deal
36+
* in the Software without restriction, including without limitation the rights
37+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38+
* copies of the Software, and to permit persons to whom the Software is
39+
* furnished to do so, subject to the following conditions:
40+
*
41+
* The above copyright notice and this permission notice shall be included in all
42+
* copies or substantial portions of the Software.
43+
*
44+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50+
* SOFTWARE.
51+
*
52+
* Author: Stuart Cording aka CODINGHEAD
53+
*
54+
********************************************************************************
55+
* Notes:
56+
* No notes to date (9th May 2012)
57+
*******************************************************************************/
58+
59+
#include <iostream>
60+
#include <fstream>
61+
#include <cstdlib>
62+
#include <iomanip>
63+
64+
#include "intelhexclass.h"
65+
66+
using namespace std;
67+
68+
char *program_name;
69+
70+
// Usage for this program
71+
void usage()
72+
{
73+
cerr << "Usage is " << program_name <<
74+
" [input_file]" << endl;
75+
exit (EXIT_FAILURE);
76+
}
77+
78+
int main(int argc, char *argv[])
79+
{
80+
// Create an input stream
81+
ifstream intelHexInput;
82+
83+
// Create a variable for the intel hex data
84+
intelhex ihStat;
85+
86+
// Note if there were issues with decoding
87+
bool decodingIssues = false;
88+
89+
// Storage for the first and last addresses in the file
90+
unsigned long startAddress = 0UL;
91+
unsigned long endAddress = 0UL;
92+
93+
// The program name is the first argument - save for later use
94+
program_name = argv[0];
95+
96+
// Make sure there are only <command> and 1 x <file> arguments
97+
if(argc != 2) {
98+
usage();
99+
}
100+
101+
intelHexInput.open(argv[1], ifstream::in);
102+
103+
if(!intelHexInput.good())
104+
{
105+
std::cerr << "Error: couldn't open " << argv[1] << std::endl;
106+
usage();
107+
}
108+
109+
/* Output file names */
110+
cout << "Statistic for file: " << argv[1] << endl;
111+
112+
/* Decode file */
113+
intelHexInput >> ihStat;
114+
115+
#error Got here
116+
117+
/* Check for errors or warnings */
118+
if (intelHexInput.getNoWarnings() != 0)
119+
{
120+
std::cerr << "File " << argv[1] << " contained warnings." << std::endl;
121+
decodingIssues = true;
122+
}
123+
if (intelHexInput.getNoErrors() != 0)
124+
{
125+
std::cerr << "File " << argv[1] << " contained errors." << std::endl;
126+
decodingIssues = true;
127+
}
128+
129+
if (decodingIssues == true)
130+
{
131+
std::cerr << "Continuing with stat despite issues listed." << std::endl;
132+
}
133+
134+
/* Determine start and end addresses */
135+
intelHexInput.begin();
136+
startAddress = intelHexInput.currentAddress();
137+
intelHexInput.end();
138+
endAddress = intelHexInput.currentAddress();
139+
140+
cout << "Start address: 0x" << hex << setw(8) << startAddress << endl;
141+
cout << "End address : 0x" << hex << setw(8) << endAddress << endl;
142+
cout << "Address range: " << dec << ((endAddress - startAddress) +1) << \
143+
" bytes" << endl;
144+
145+
/* Determine number of bytes in file */
146+
cout << "Data bytes : " << dec << intelHexInput.size() << endl;
147+
148+
/* Two local var's to store data at the locations being analysed */
149+
unsigned char diffAData = 0;
150+
unsigned char diffBData = 0;
151+
152+
bool aFinished = false;
153+
bool bFinished = false;
154+
155+
bool complete = false;
156+
157+
do
158+
{
159+
/* Get address of data we are pointing to */
160+
diffAAddress = ihDiffA.currentAddress();
161+
diffBAddress = ihDiffB.currentAddress();
162+
163+
/* Get the data at two current addresses */
164+
ihDiffA.getData(&diffAData);
165+
ihDiffB.getData(&diffBData);
166+
167+
/* If addresses are the same, compare data values */
168+
if (diffAAddress == diffBAddress)
169+
{
170+
if (diffAData != diffBData)
171+
{
172+
cout << uppercase << "0x" \
173+
<< hex << setw(8) << setfill('0') << diffAAddress \
174+
<< " 0x" << hex << setw(2) \
175+
<< static_cast<unsigned short>(diffAData) \
176+
<< " 0x" << hex << setw(2) \
177+
<< static_cast<unsigned short>(diffBData) << endl;
178+
}
179+
/* Increment both addresses */
180+
++ihDiffA;
181+
++ihDiffB;
182+
}
183+
184+
/* If addresses are different, find out which one is lower and output */
185+
/* that this address has data where the other does not have data */
186+
else if (diffAAddress < diffBAddress)
187+
{
188+
/* Output A address as reference address since this exists and */
189+
/* the B address doesn't */
190+
cout << uppercase << "0x" \
191+
<< hex << setw(8) << setfill('0') << diffAAddress \
192+
<< " 0x" << hex << setw(2) \
193+
<< static_cast<unsigned short>(diffAData) \
194+
<< " ----" << endl;
195+
++ihDiffA;
196+
}
197+
else
198+
{
199+
/* Here we output the B address as reference address since data */
200+
/* appears at this address in file B, but not in file A */
201+
cout << uppercase << "0x" \
202+
<< hex << setw(8) << setfill('0') << diffBAddress \
203+
<< " ----" \
204+
<< " 0x" << hex << setw(2) \
205+
<< static_cast<unsigned short>(diffBData) << endl;
206+
++ihDiffB;
207+
}
208+
209+
/* Check if we got to the end of the two files */
210+
if (ihDiffA.endOfData() == true)
211+
{
212+
break;
213+
}
214+
else if (ihDiffB.endOfData() == true)
215+
{
216+
break;
217+
}
218+
219+
} while (complete != true);
220+
221+
return(0);
222+
}

0 commit comments

Comments
 (0)