X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fhexfile.c;h=5ec2055abe1769982c82fd0b48ad504c5bac43fe;hb=60f3340a9f321271f5ea96df2ccfc248f67bfd2f;hp=fe57ca98d0f68b546ad62f57035ebefb686afa8f;hpb=6a65dca9d597772744524b909f2d89b479b8bf77;p=emu8051.git diff --git a/src/hexfile.c b/src/hexfile.c index fe57ca9..5ec2055 100644 --- a/src/hexfile.c +++ b/src/hexfile.c @@ -16,7 +16,7 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ #if HAVE_CONFIG_H @@ -36,121 +36,126 @@ #include "common.h" #include "memory.h" - /* Convert an ascii string to an hexadecimal number. */ -static unsigned int -Ascii2Hex( char *istring, int length ) +unsigned int +Ascii2Hex(char *istring, int length) { - unsigned int result = 0; - int i, ascii_code; - - if ( !length || ( length > (int) strlen(istring) ) ) { - length = strlen(istring); - } - - for ( i = 0; i < length; i++ ) { - ascii_code = istring[ i ]; - if ( ascii_code > 0x39 ) { - ascii_code &= 0xDF; - } - if ( ( ascii_code >= 0x30 && ascii_code <= 0x39 ) || - ( ascii_code >= 0x41 && ascii_code <= 0x46 ) ) { - ascii_code -= 0x30; - if ( ascii_code > 9 ) { - ascii_code -= 7; - } - result <<= 4; - result += ascii_code; - } - else { - printf( "%s: In Ascii2Hex(), syntax error.\n", PACKAGE ); - } - } - return result; -} + unsigned int result = 0; + int i, ascii_code; + + if (!length || (length > (int) strlen(istring))) + length = strlen(istring); + + for (i = 0; i < length; i++) { + ascii_code = istring[i]; + if (ascii_code > 0x39) + ascii_code &= 0xDF; + + if ((ascii_code >= 0x30 && ascii_code <= 0x39) || + (ascii_code >= 0x41 && ascii_code <= 0x46)) { + ascii_code -= 0x30; + if (ascii_code > 9) + ascii_code -= 7; + result <<= 4; + result += ascii_code; + } else { + printf("%s: In Ascii2Hex(), syntax error.\n", PACKAGE); + printf(" str=%s, length=%d\n", istring, length); + } + } + return result; +} void -LoadHexFile( const char *filename ) +LoadHexFile(const char *filename) { - int i, j, RecLength, LoadOffset, RecType, Data, Checksum; - + int i, j, RecLength, LoadOffset, RecType, Data, Checksum; + #define LINE_BUFFER_LEN 256 - FILE *fp; - int status; - char line[LINE_BUFFER_LEN]; - - if( filename != NULL ) { - /* Trying to open the file. */ - fp = fopen( filename, "r" ); - if( fp == NULL ) { - perror( PACKAGE ); - /*ErrorLocation( __FILE__, __LINE__ );*/ - exit( EXIT_FAILURE ); - } - } - - /* Reading one line of data from the configuration file. */ - /* char *fgets(char *s, int size, FILE *stream); - Reading stops after an EOF or a newline. If a newline is read, it is - stored into the buffer. A '\0' is stored after the last character in - the buffer. */ - while( fgets( line, LINE_BUFFER_LEN, fp ) != NULL ) { - i = 0; - Checksum = 0; - - if ( line[ i++ ] != ':' ) { - printf( "%s: line not beginning with \":\"\n", PACKAGE ); - goto close_file; - } - - RecLength = Ascii2Hex( &line[ i ], 2 ); - i += 2; - Checksum += RecLength; - - LoadOffset = Ascii2Hex( &line[i], 4 ); - Checksum += LoadOffset / 256; - Checksum += LoadOffset % 256; - i += 4; - - RecType = Ascii2Hex( &line[i],2); - i += 2; - Checksum += RecType; - - if ( RecType == 1 ) { - Checksum += Ascii2Hex( &line[ i ], 2 ); - if ( Checksum &= 0x000000FF ) { - /* Error. */ - printf( "%s: Invalid format\n", PACKAGE ); - goto close_file; - } - else { - /* OK */ - goto close_file; - } - } - - for ( j = 0; j < RecLength; j++ ) { - Data = Ascii2Hex( &line[ i ], 2 ); - memory_write8( PGM_MEM_ID, (unsigned int)(LoadOffset + j), (unsigned char)Data ); - i += 2; - Checksum += Data; - } - - RecType = Ascii2Hex( &line[ i ], 2 ); - Checksum += RecType; - - if ( Checksum &= 0x000000FF ) { - printf( "%s: Invalid format\n", PACKAGE ); - goto close_file; - } - } - - close_file: - status = fclose( fp ); - if( status != EXIT_SUCCESS ) { - fprintf( stderr, "%s: Error closing configuration file.\n", PACKAGE ); - /*ErrorLocation( __FILE__, __LINE__ );*/ - exit( EXIT_FAILURE ); - } + FILE *fp; + int status; + char line[LINE_BUFFER_LEN]; + + if (filename == NULL) { + printf("%s: No file specified\n", PACKAGE); + exit(EXIT_FAILURE); + } + + /* Trying to open the file. */ + fp = fopen(filename, "r"); + if (fp == NULL) { + perror(PACKAGE); + /*ErrorLocation(__FILE__, __LINE__);*/ + exit(EXIT_FAILURE); + } + + /* Reading one line of data from the hex file. */ + /* char *fgets(char *s, int size, FILE *stream); + Reading stops after an EOF or a newline. If a newline is read, it is + stored into the buffer. A '\0' is stored after the last character + in the buffer. + */ + while (fgets(line, LINE_BUFFER_LEN, fp) != NULL) { + i = 0; + Checksum = 0; + + if (line[i++] != ':') { + printf("%s: line not beginning with \":\"\n", PACKAGE); + goto close_file; + } + + RecLength = Ascii2Hex(&line[i], 2); + i += 2; + Checksum += RecLength; + + LoadOffset = Ascii2Hex(&line[i], 4); + Checksum += LoadOffset / 256; + Checksum += LoadOffset % 256; + i += 4; + + RecType = Ascii2Hex(&line[i], 2); + i += 2; + Checksum += RecType; + + if (RecType == 1) { + Checksum += Ascii2Hex(&line[i], 2); + Checksum &= 0x000000FF; + + if (Checksum) { + /* Error. */ + printf("%s: Invalid format\n", PACKAGE); + goto close_file; + } else { + /* OK */ + goto close_file; + } + } + + for (j = 0; j < RecLength; j++) { + Data = Ascii2Hex(&line[i], 2); + memory_write8(PGM_MEM_ID, + (unsigned int)(LoadOffset + j), + (unsigned char)Data); + i += 2; + Checksum += Data; + } + + RecType = Ascii2Hex(&line[i], 2); + Checksum += RecType; + Checksum &= 0x000000FF; + + if (Checksum) { + printf("%s: Invalid format\n", PACKAGE); + goto close_file; + } + } + +close_file: + status = fclose(fp); + if (status != EXIT_SUCCESS) { + fprintf(stderr, "%s: Error closing hex file.\n", PACKAGE); + /*ErrorLocation(__FILE__, __LINE__);*/ + exit(EXIT_FAILURE); + } }