xa_asm: fixed parsing of symbols used with DS directive in BSEG (pass 2&3).
[fw/sdcc] / as / xa51 / xa_dasm.c
1 /* This file is part of Paul's XA51 Assembler, Copyright 1997,2002 Paul Stoffregen
2  *
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.
6  *
7  * Paul's XA51 Assembler 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.
11  *
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
15  */
16
17 /* Author contact: paul@pjrc.com */
18
19
20 #include <stdio.h>
21
22 #define MAXHEXLINE 32   /* the maximum number of bytes to put in one line */
23
24 extern FILE *fhex;  /* the file to put intel hex into */
25
26
27 /* produce intel hex file output */
28
29 void hexout(int byte, int memory_location, int end)
30 {
31         static int byte_buffer[MAXHEXLINE];
32         static int last_mem, buffer_pos, buffer_addr;
33         static int writing_in_progress=0;
34         register int i, sum;
35
36         if (!writing_in_progress) {
37                 /* initial condition setup */
38                 last_mem = memory_location-1;
39                 buffer_pos = 0;
40                 buffer_addr = memory_location;
41                 writing_in_progress = 1;
42                 }
43
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;
52                 }
53                 fprintf(fhex, "%02X\n", (-sum)&255);
54                 buffer_addr = memory_location;
55                 buffer_pos = 0;
56         }
57
58         if (end) {
59                 fprintf(fhex, ":00000001FF\n");  /* end of file marker */
60                 fclose(fhex);
61                 writing_in_progress = 0;
62         }
63                 
64         last_mem = memory_location;
65         byte_buffer[buffer_pos] = byte & 255;
66         buffer_pos++;
67 }
68
69