#include <stdio.h>
#include "hex.h"
#include "mylibc.h"
void usage()
{
printf("viewhex: a tool to view the intel hex format file\n");
printf("usage: viewhex filename.hex\n");
printf("where the filename.hex is the hex file you want to view\n");
}
void printData(char* line, int lineLength)
{
int iCount = 0;
for(iCount = 9; iCount < lineLength-2; iCount++)
putchar(line[iCount]);
printf("; Dec = %d", ahex2int(line+9, 8) );
}
int main(int argc, char* argv[])
{
unsigned char line[MAX_LINE_LENGTH];
FILE* fp;
int retLength = 0;
int lineNumber = 0;
if(2 != argc) {
usage();
return -1;
}
fp = fopen(argv[1], "r");
if(NULL == fp) {
printf("fail open file %s", argv[1]);
return -1;
}
lineNumber = 0;
while((retLength = fgetline(fp, line, MAX_LINE_LENGTH)) > 0 ) {
lineNumber++;
if( retLength == 1 ) {
printf("Line:%d empty\n", lineNumber);
continue;
}
if( retLength < 12 ) {
printf("error record length %d at line %d\n", retLength, lineNumber);
continue;
}
if( checkValidHexRecord(line, retLength - 1) ) {
switch( getRecordType(line, retLength - 1) ) {
case 0:
printf("Line:%d Type: Data, DataLen = %d, Data = ",lineNumber, ahex2int(line+1, 2));
printData(line, retLength-1);
printf("\n");
break;
case 1:
printf("Line:%d Type: End, End of File Record\n", lineNumber);
break;
case 2:
printf("Line:%d Type: Extended Segment Address, USBA = 0x", lineNumber);
printData(line, retLength-1);
printf("\n");
break;
case 3:
printf("Line:%d Type: Start Segment Address , CS/IP = 0x", lineNumber);
printData(line, retLength-1);
printf("\n");
break;
case 4:
printf("Line:%d Type: Extended Linear Address , ULBA = 0x", lineNumber);
printData(line, retLength-1);
printf("\n");
break;
case 5:
printf("Line:%d Type: Start Linear Address , EIP = 0x", lineNumber);
printData(line, retLength-1);
printf("\n");
break;
default:
printf("error record Type at line %d\n", lineNumber);
break;
}
}
else
printf("not valid record at line %d\n", lineNumber);
}
if(retLength < 0)
printf("Too long at line: %d\n", lineNumber);
fclose(fp);
return 0;
}
|