* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / link / z80 / lkihx.c
1 /* lkihx.c
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include "aslink.h"
22
23 /*)Module       lkihx.c
24  *
25  *      The module lkihx.c contains the function to
26  *      output the relocated object code in the
27  *      Intel Hex format.
28  *
29  *      lkihx.c contains the following functions:
30  *              VOID    ihx(i)
31  *
32  *      lkihx.c contains no local variables.
33  */
34
35 /*Intel Hex Format
36  *      Record Mark Field    -  This  field  signifies  the  start  of a
37  *                              record, and consists of an  ascii  colon
38  *                              (:).
39  *
40  *      Record Length Field  -  This   field   consists   of  two  ascii
41  *                              characters which indicate the number  of
42  *                              data   bytes   in   this   record.   The
43  *                              characters are the result of  converting
44  *                              the  number  of  bytes  in binary to two
45  *                              ascii characters, high digit first.   An
46  *                              End  of  File  record contains two ascii
47  *                              zeros in this field.
48  *
49  *      Load Address Field   -  This  field  consists  of the four ascii
50  *                              characters which result from  converting
51  *                              the  the  binary value of the address in
52  *                              which to begin loading this record.  The
53  *                              order is as follows:
54  *
55  *                                  High digit of high byte of address.
56  *                                  Low digit of high byte of address.
57  *                                  High digit of low byte of address.
58  *                                  Low digit of low byte of address.
59  *
60  *                              In an End of File record this field con-
61  *                              sists of either four ascii zeros or  the
62  *                              program  entry  address.   Currently the
63  *                              entry address option is not supported.
64  *
65  *      Record Type Field    -  This  field  identifies the record type,
66  *                              which is either 0 for data records or  1
67  *                              for  an End of File record.  It consists
68  *                              of two ascii characters, with  the  high
69  *                              digit of the record type first, followed
70  *                              by the low digit of the record type.
71  *
72  *      Data Field           -  This  field consists of the actual data,
73  *                              converted to two ascii characters,  high
74  *                              digit first.  There are no data bytes in
75  *                              the End of File record.
76  *
77  *      Checksum Field       -  The  checksum  field is the 8 bit binary
78  *                              sum of the record length field, the load
79  *                              address  field,  the  record type field,
80  *                              and the data field.  This  sum  is  then
81  *                              negated  (2's  complement) and converted
82  *                              to  two  ascii  characters,  high  digit
83  *                              first.
84  */
85
86 /*)Function     ihx(i)
87  *
88  *              int     i               0 - process data
89  *                                      1 - end of data
90  *
91  *      The function ihx() outputs the relocated data
92  *      in the standard Intel Hex format.
93  *
94  *      local variables:
95  *              Addr_T  chksum          byte checksum
96  *
97  *      global variables:
98  *              int     hilo            byte order
99  *              FILE *  ofp             output file handle
100  *              int     rtcnt           count of data words
101  *              int     rtflg[]         output the data flag
102  *              Addr_T  rtval[]         relocated data
103  *
104  *      functions called:
105  *              int     fprintf()       c_library
106  *
107  *      side effects:
108  *              The data is output to the file defined by ofp.
109  */
110
111 VOID
112 ihx(i)
113 {
114         register Addr_T chksum;
115
116         if (i) {
117                 if (hilo == 0) {
118                         chksum = rtval[0];
119                         rtval[0] = rtval[1];
120                         rtval[1] = chksum;
121                 }
122                 for (i = 0, chksum = -2; i < rtcnt; i++) {
123                         if (rtflg[i])
124                                 chksum++;
125                 }
126                 fprintf(ofp, ":%02X", chksum);
127                 for (i = 0; i < rtcnt ; i++) {
128                         if (rtflg[i]) {
129                                 fprintf(ofp, "%02X", rtval[i]);
130                                 chksum += rtval[i];
131                         }
132                         if (i == 1) {
133                                 fprintf(ofp, "00");
134                         }
135                 }
136                 fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
137         } else {
138                 fprintf(ofp, ":00000001FF\n");
139         }
140 }