#include "stdio.h" // sscanf(), printf(), fopen(), fclose(), fgets() #include "stdlib.h" // strtol(), min(), max() #include "string.h" // strcpy(), strlen(), strstr() functions #include "viewer.h" // // Map file format stuff - if MPLINK format changes, change these! // // The variable lines contain the word 'data' after this column #define MAX_LINE_IN 200 #define MAP_DATA_COLUMN 40 // // The start of the sorted variable listings // #define SORTED_BY_NAME "Sorted by Name" #define SORTED_BY_ADDR "Sorted by Address" static int SymbolNext; // // MapParseData // // Scans a line of an MPLINK file formatted as follows: // variableName ADDRESS data filename // // Determines the length of the variable. If length can not be // determined, it is defaulted to 1. // // Parameters: lineIn - pointer to string read from map file // sortedBy - Address(0) or Name(1) // // Globals: Variables - global symbol table of data in map file // SymbolNext - index of location to store data in table // int MapParseData( char *lineIn, int sortedBy ) { char nameIn[ MAX_LINE_IN ]; char addrIn[ MAX_LINE_IN ]; long addr; int lenIn; static bankNow = 0; // load variable name and address if ( sscanf( lineIn, "%s%s", nameIn, addrIn ) != 2 ) return( 0 ); // update internal symbol table name and address strcpy( Variables[ SymbolNext ].name, nameIn ); addr = strtol( addrIn, 0, 0 ); Variables[ SymbolNext ].address = addr; // calculate length of the symbol if (( sortedBy & 0x01 ) > 0 ) { // we don't know how big variables are when not sorted by address! Variables[ SymbolNext ].length = 1; } else if ( SymbolNext > 0 ) { lenIn = Variables[ SymbolNext ].address - Variables[ SymbolNext - 1 ].address; if (( bankNow == 0 ) && ( addr > BANK_0_END )) { bankNow = 1; lenIn = 1; // default length of last variable in the bank } if ( lenIn == 0 ) { // overlaid variables calculate to 0 lenIn = 1; } Variables[ SymbolNext - 1 ].length = lenIn; } return( 1 ); } // // MapLoadFile // // Opens the given map file, scans for the given sorted data section // (sorted by name or sorted by address) and records all variables // defined therein. // // parameters: sortedBy - Address (0) or Name (1) // int MapLoadFile( char *mapFileName, int sortedBy ) { FILE *mapFile; char lineIn[ MAX_LINE_IN ]; char *linePtr; char *strPtr; SymbolNext = 0; mapFile = fopen( mapFileName, "rt" ); if ( mapFile == NULL ) return( 0 ); // locate sorted variable names data space fseek( mapFile, 0L, SEEK_SET); do { linePtr = fgets( lineIn, MAX_LINE_IN, mapFile ); if ( linePtr != NULL ) { if ( (sortedBy & 0x01) == 0 ) strPtr = strstr( lineIn, SORTED_BY_ADDR ); else strPtr = strstr( lineIn, SORTED_BY_NAME ); if ( strPtr != NULL ) break; // found the sorted section } } while ( linePtr != NULL ); if ( linePtr == NULL ) return( 0 ); // map file error // locate variable names for on-chip data do { strPtr = strstr( &lineIn[ 0 ], "data" ); if ( strPtr != NULL ) // look out for variable names using 'data' strPtr = strstr( &lineIn[ MAP_DATA_COLUMN ], "data" ); if ( strPtr == NULL ) { // "data" not found on this line, read next one if ( fgets( lineIn, MAX_LINE_IN, mapFile ) == NULL ) { break; } } } while ( strPtr == NULL ); // load each variable into structure while ( strPtr != NULL ) { strPtr = strstr( &lineIn[ 0 ], "data" ); if ( strPtr != NULL ) { MapParseData( lineIn, sortedBy ); SymbolNext = SymbolNext + 1; // read next line - if at EOF, strPtr gets set to NULL strPtr = fgets( lineIn, MAX_LINE_IN, mapFile ); } } fclose( mapFile ); SymbolNext = SymbolNext - 1; return( SymbolNext ); }