Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/09/00 19:15
Read: times


 
#1741 - Hex file parser

Hello!

I'm writing 8052 simulator as my final high-school project in fourth grade.
It's written in C++ with QT widget set (GUI library).
Primary development platform is Linux.
I have encouraged the following problem:
Problem is in parsing hex file (intel hex file format) produced by
AS31 assembler.

This is the algorithm:

INSTALL_RETTYPE
Simikon8052::Parse( const QString &hexFile, const QString &asmFile,
const QString &dir )
{
QFile fpHEX( fullFilePath( hexFile, dir ) );
QFile fpASM( fullFilePath( asmFile, dir ) );

asmFileOK = true;
hexFileOK = true;

INSTALL_RETTYPE retval = OK;

if ( !fpHEX.open( IO_ReadOnly ) ) {
cout << "WARNING: Simikon8052::Parse(): "
<< "Can't open HEX file for reading! n";

hexFileOK = false;
retval = HEX_ERROR;

emit signalInvalidHEX();
}

if ( !fpASM.open( IO_ReadOnly ) ) {
cout << "WARNING: Simikon8052::Parse(): "
<< "Can't open ASM file for reading! n";

asmFileOK = false;
retval = ASM_ERROR;

emit signalInvalidASM();
}

if ( !asmFileOK && !hexFileOK ) {
retval = ERROR; // :)
return retval; // nemamo sto trazit dalje :)
}

QTextStream tfpHEX( &fpHEX );
QTextStream tfpASM( &fpASM );

uint lineNoHEX = 0;
uint lineNoASM = 0;

uint opCodeAddress;
uint instructionByte = OP_CODE;
bool instructionDone = false;

QString mnemonicStr;
QString opCodeStr;

uint address = 0;
uint bytes = 0;

if ( hexFileOK ) {

uint noOfSteps = calculateNumberOfStepsForHEX( fullFilePath( hexFile, dir ) );
progress->setTotalSteps( noOfSteps );

while ( !tfpHEX.eof() ) {
uint recType;
int recLength;
char dataBytes[ MAX_LINE_SIZE ];
char *pdata;

uint byte;

QString lineStrASM( tfpHEX.readLine() );

sscanf( lineStrASM.ascii(), ":%2x%4x%2x%s",
&recLength, &address, &recType, dataBytes );

pdata = dataBytes;

switch ( recType ) {

case DATA_RECORD:

for ( ; recLength > 0; recLength-- ) {

sscanf( pdata, "%2x", &byte );
pdata += 2;
bytes++;

// update progress bar
progress->setProgress( progress->progress() + 1 );

emit ( this->*signalUpdateProgramMemory )( address, byte );

switch( instructionByte ) {

case OP_CODE:
activeCode[address].opCode = byte;
activeCode[address].cycles = instructionTable[byte].cycles;
activeCode[address].bytes = instructionTable[byte].bytes;
activeCode[address].execute = instructionTable[byte].execute;
activeCode[address].lineHEX = lineNoHEX++;

opCodeStr = "0x" + uintToHexStr8( byte );
mnemonicStr = QString( instructionTable[ byte ].mnemonic );

opCodeAddress = address;
break;

case ARG1:
activeCode[opCodeAddress].b1 = byte;

opCodeStr.append( " 0x" );
opCodeStr.append( uintToHexStr8( byte ) );

mnemonicStr =
mnemonicStr.arg( uintToHexStr8( byte ) );
break;

case ARG2:
activeCode[opCodeAddress].b2 = byte;

opCodeStr.append( " 0x" );
opCodeStr.append( uintToHexStr8( byte ) );

mnemonicStr =
mnemonicStr.arg( uintToHexStr8( byte ) );
break;
}

if ( activeCode[opCodeAddress].bytes == instructionByte ) {
instructionDone = true;
} else
instructionByte++;

if ( instructionDone ) {
emit signalAddInstruction( "0x" + uintToHexStr16( address ),
mnemonicStr, opCodeStr );

if ( asmFileOK ) {

QString m( instructionTable[activeCode[opCodeAddress].opCode].mnemonic );
m.truncate( m.find( ' ' ) );
QString lineStrASM;

do {
lineStrASM = tfpASM.readLine();

emit signalAddLine( lineStrASM );

lineNoASM++;
} while ( lineStrASM.find( m ) == -1 );

activeCode[opCodeAddress].lineASM = lineNoASM - 1;
}

address = opCodeAddress + activeCode[opCodeAddress].bytes;
activeData->address = address;

instructionDone = false;
instructionByte = OP_CODE;

mnemonicStr = "";
opCodeStr = "";
}
}

break;

case EOF_RECORD:
break;

}
}

fpHEX.close();
fpASM.close();

} else if ( asmFileOK ) {

uint noOfSteps = calculateNumberOfStepsForASM( fullFilePath( asmFile, dir ) );
progress->setTotalSteps( noOfSteps );

QString lineStrASM;

while ( !tfpASM.eof() ) {
lineStrASM = tfpASM.readLine();
emit signalAddLine( lineStrASM );
progress->setProgress( progress->progress() + 1 );
}

fpASM.close();
}

progress->reset();

programLength = bytes;

return retval;
}

So the idea is to read the opCode byte, lookup instructionTable to find
number of bytes ( arguments ) for that instruction, and then read arguments.

This works fine, however if we have the following code:

.org 0000h
ajmp START

START:

ajmp START

HelpString: .db "Help" ;;; problem

So the question is how to distinguish actual program code from
bytes assembled with .db directive.

Is there any other format besides Intel HEX, which offers more information,
and offcourse where can I get specifications?

If someone is willing to help me with this or contribute to the
project please send me mail to: akovac@jagor.srce.hr

Thanks in advance!


List of 8 messages in thread
TopicAuthorDate
Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      
RE: Hex file parser            01/01/70 00:00      

Back to Subject List