Added option --code-size and --xram-size
[fw/sdcc] / as / mcs51 / lkihx.c
1 /* lkihx.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include "aslink.h"
15
16 /*)Module       lkihx.c
17  *
18  *      The module lkihx.c contains the function to
19  *      output the relocated object code in the
20  *      Intel Hex format.
21  *
22  *      lkihx.c contains the following function:
23  *              VOID    ihx(i)
24  *
25  *      lkihx.c contains no local variables.
26  */
27
28 /*Intel Hex Format
29  *      Record Mark Field    -  This  field  signifies  the  start  of a
30  *                              record, and consists of an  ascii  colon
31  *                              (:).  
32  *
33  *      Record Length Field  -  This   field   consists   of  two  ascii
34  *                              characters which indicate the number  of
35  *                              data   bytes   in   this   record.   The
36  *                              characters are the result of  converting
37  *                              the  number  of  bytes  in binary to two
38  *                              ascii characters, high digit first.   An
39  *                              End  of  File  record contains two ascii
40  *                              zeros in this field.  
41  *
42  *      Load Address Field   -  This  field  consists  of the four ascii
43  *                              characters which result from  converting
44  *                              the  the  binary value of the address in
45  *                              which to begin loading this record.  The
46  *                              order is as follows:  
47  *
48  *                                  High digit of high byte of address. 
49  *                                  Low digit of high byte of address.  
50  *                                  High digit of low byte of address.  
51  *                                  Low digit of low byte of address.  
52  *
53  *                              In an End of File record this field con-
54  *                              sists of either four ascii zeros or  the
55  *                              program  entry  address.   Currently the
56  *                              entry address option is not supported.  
57  *
58  *      Record Type Field    -  This  field  identifies the record type,
59  *                              which is either 0 for data records or  1
60  *                              for  an End of File record.  It consists
61  *                              of two ascii characters, with  the  high
62  *                              digit of the record type first, followed
63  *                              by the low digit of the record type.  
64  *
65  *      Data Field           -  This  field consists of the actual data,
66  *                              converted to two ascii characters,  high
67  *                              digit first.  There are no data bytes in
68  *                              the End of File record.  
69  *
70  *      Checksum Field       -  The  checksum  field is the 8 bit binary
71  *                              sum of the record length field, the load
72  *                              address  field,  the  record type field,
73  *                              and the data field.  This  sum  is  then
74  *                              negated  (2's  complement) and converted
75  *                              to  two  ascii  characters,  high  digit
76  *                              first.  
77  */
78
79 /*)Function     ihx(i)
80  *
81  *              int     i               0 - process data
82  *                                      1 - end of data
83  *
84  *      The function ihx() outputs the relocated data
85  *      in the standard Intel Hex format.
86  *
87  *      local variables:
88  *              Addr_T  chksum          byte checksum
89  *
90  *      global variables:
91  *              int     hilo            byte order
92  *              FILE *  ofp             output file handle
93  *              int     rtcnt           count of data words
94  *              int     rtflg[]         output the data flag
95  *              Addr_T  rtval[]         relocated data
96  *
97  *      functions called:
98  *              int     fprintf()       c_library
99  *
100  *      side effects:
101  *              The data is output to the file defined by ofp.
102  */
103
104 VOID
105 ihx(i)
106 {
107         register Addr_T chksum;
108         int byte, bytes, address=0;
109         if (i) {
110                 if (hilo == 0) {
111                         chksum = rtval[0];
112                         rtval[0] = rtval[1];
113                         rtval[1] = chksum;
114                 }
115                 for (i = 0, chksum = -2; i < rtcnt; i++) {
116                         if (rtflg[i])
117                                 chksum++;
118                 }
119                 fprintf(ofp, ":%02X", chksum);
120                 // how much bytes?
121                 for (i=0, bytes=0; i<rtcnt; i++) {
122                   if (rtflg[i]) {
123                     bytes++;
124                   }
125                 }
126                 byte=0;
127                 for (i = 0; i < rtcnt ; i++) {
128                   if (rtflg[i]) {
129                     switch (byte) {
130                     case 0: 
131                       address=rtval[0]<<8; 
132                       break;
133                     case 1: 
134                       address+=rtval[1]; 
135                       if ((address+bytes)>0xffff) {
136                         fprintf (stderr, "64k boundary cross at %04x\n", address);
137                       }
138                       break;
139                     }
140                     fprintf(ofp, "%02X", rtval[i]);
141                     chksum += rtval[i];
142                     byte++;
143                   }
144                   if (i == 1) {
145                     fprintf(ofp, "00");
146                   }
147                 }
148                 fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
149         } else {
150                 fprintf(ofp, ":00000001FF\n");
151         }
152 }
153
154 /*)Function     ihxEntendedLinearAddress(i)
155  *
156  *              Addr_T  i               16 bit extended linear address.
157  *
158  *      The function ihxEntendedLinearAddress() writes an extended
159  *      linear address record (type 04) to the output file.
160  *
161  *      local variables:
162  *              Addr_T  chksum          byte checksum
163  *
164  *      global variables:
165  *              FILE *  ofp             output file handle
166  *
167  *      functions called:
168  *              int     fprintf()       c_library
169  *
170  *      side effects:
171  *              The data is output to the file defined by ofp.
172  */
173 VOID
174 ihxEntendedLinearAddress(Addr_T a)
175 {
176     Addr_T      chksum;
177   
178     /* The checksum is the complement of the bytes in the
179      * record: the 2 is record length, 4 is the extended linear
180      * address record type, plus the two address bytes.
181      */ 
182     chksum = 2 + 4 + (a & 0xff) + ((a >> 8) & 0xff);    
183     
184     fprintf(ofp, ":02000004%04X%02X\n", a & 0xffff, (0-chksum) & 0xff);
185 }