lst2imem.cxx 1.24 KB
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <string.h>

char linebuf[1024];

void usage( void )
{
	cerr << "usage: lst2imem listfilename..." << endl;
	exit( 1 );
}

int main( int argc, char ** argv )
{
	ifstream in;
	int words = 0;
	unsigned long savedAddr;
	char savedValue[12];

	if ( argc < 2 )
		usage();
	--argc;
	while ( argc-- )
	{
		in.open( argv[1] );
		if ( in.fail() )
		{
			cerr << "Can't open " << argv[1] << endl;
			++argv;		// point to next
			continue;
		}
		while ( in.getline( linebuf, 1024 ) )
		{
			char *address, *value, *dummy;

			if ( !strlen( linebuf ) )
				continue;
			address = strtok( linebuf, " " );
			if ( strncmp( address, "0x", 2 ) )
				continue;
			dummy = strtok( NULL, " " );
			value = strtok( NULL, " " );
			if ( words & 1 )	/* write both */
				cout << "@" << hex << ((savedAddr / 8) & 0x1ff) << " " 
					<< savedValue << "_" << &value[2] << endl;
			else
			{
				savedAddr = strtoul( address, NULL, 16 );
				strcpy( savedValue, &value[2] );
			}
			++words;
		}
		if ( words & 1 )	// got a leftover
			cout << "@" << hex << ((savedAddr / 8) & 0x1ff) << " " 
				<< savedValue << "_" << "00000000" << endl;
		in.close();
		++argv;	// point to next file
	}
	return 0;
}