1 /* This file is part of Paul's XA51 Assembler, Copyright 1997,2002 Paul Stoffregen
3 * Paul's XA51 Assembler is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2.
7 * Foobar is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with Foobar; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* Author contact: paul@pjrc.com */
22 #define MAXHEXLINE 32 /* the maximum number of bytes to put in one line */
24 extern FILE *fhex; /* the file to put intel hex into */
27 /* produce intel hex file output */
29 void hexout(int byte, int memory_location, int end)
31 static int byte_buffer[MAXHEXLINE];
32 static int last_mem, buffer_pos, buffer_addr;
33 static int writing_in_progress=0;
36 if (!writing_in_progress) {
37 /* initial condition setup */
38 last_mem = memory_location-1;
40 buffer_addr = memory_location;
41 writing_in_progress = 1;
44 if ( (memory_location != (last_mem+1)) || (buffer_pos >= MAXHEXLINE) \
45 || ((end) && (buffer_pos > 0)) ) {
46 /* it's time to dump the buffer to a line in the file */
47 fprintf(fhex, ":%02X%04X00", buffer_pos, buffer_addr);
48 sum = buffer_pos + ((buffer_addr>>8)&255) + (buffer_addr&255);
49 for (i=0; i < buffer_pos; i++) {
50 fprintf(fhex, "%02X", byte_buffer[i]&255);
51 sum += byte_buffer[i]&255;
53 fprintf(fhex, "%02X\n", (-sum)&255);
54 buffer_addr = memory_location;
59 fprintf(fhex, ":00000001FF\n"); /* end of file marker */
61 writing_in_progress = 0;
64 last_mem = memory_location;
65 byte_buffer[buffer_pos] = byte & 255;