082a5fc5ed9b3e2afdd04a9ee03f23b6fddf5b9e
[fw/sdcc] / as / link / z80 / 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 <alloc.h>
15 #include "aslink.h"
16
17 /*)Module       lkihx.c
18  *
19  *      The module lkihx.c contains the function to
20  *      output the relocated object code in the
21  *      Intel Hex format.
22  *
23  *      lkihx.c contains the following function:
24  *              VOID    ihx(i)
25  *
26  *      lkihx.c contains no local variables.
27  */
28
29 /*Intel Hex Format
30  *      Record Mark Field    -  This  field  signifies  the  start  of a
31  *                              record, and consists of an  ascii  colon
32  *                              (:).  
33  *
34  *      Record Length Field  -  This   field   consists   of  two  ascii
35  *                              characters which indicate the number  of
36  *                              data   bytes   in   this   record.   The
37  *                              characters are the result of  converting
38  *                              the  number  of  bytes  in binary to two
39  *                              ascii characters, high digit first.   An
40  *                              End  of  File  record contains two ascii
41  *                              zeros in this field.  
42  *
43  *      Load Address Field   -  This  field  consists  of the four ascii
44  *                              characters which result from  converting
45  *                              the  the  binary value of the address in
46  *                              which to begin loading this record.  The
47  *                              order is as follows:  
48  *
49  *                                  High digit of high byte of address. 
50  *                                  Low digit of high byte of address.  
51  *                                  High digit of low byte of address.  
52  *                                  Low digit of low byte of address.  
53  *
54  *                              In an End of File record this field con-
55  *                              sists of either four ascii zeros or  the
56  *                              program  entry  address.   Currently the
57  *                              entry address option is not supported.  
58  *
59  *      Record Type Field    -  This  field  identifies the record type,
60  *                              which is either 0 for data records or  1
61  *                              for  an End of File record.  It consists
62  *                              of two ascii characters, with  the  high
63  *                              digit of the record type first, followed
64  *                              by the low digit of the record type.  
65  *
66  *      Data Field           -  This  field consists of the actual data,
67  *                              converted to two ascii characters,  high
68  *                              digit first.  There are no data bytes in
69  *                              the End of File record.  
70  *
71  *      Checksum Field       -  The  checksum  field is the 8 bit binary
72  *                              sum of the record length field, the load
73  *                              address  field,  the  record type field,
74  *                              and the data field.  This  sum  is  then
75  *                              negated  (2's  complement) and converted
76  *                              to  two  ascii  characters,  high  digit
77  *                              first.  
78  */
79
80 /*)Function     ihx(i)
81  *
82  *              int     i               0 - process data
83  *                                      1 - end of data
84  *
85  *      The function ihx() outputs the relocated data
86  *      in the standard Intel Hex format.
87  *
88  *      local variables:
89  *              Addr_T  chksum          byte checksum
90  *
91  *      global variables:
92  *              int     hilo            byte order
93  *              FILE *  ofp             output file handle
94  *              int     rtcnt           count of data words
95  *              int     rtflg[]         output the data flag
96  *              Addr_T  rtval[]         relocated data
97  *
98  *      functions called:
99  *              int     fprintf()       c_library
100  *
101  *      side effects:
102  *              The data is output to the file defined by ofp.
103  */
104
105 VOID
106 ihx(i)
107 {
108         register Addr_T chksum;
109
110         if (i) {
111                 if (hilo == 0) {
112                         chksum = rtval[0];
113                         rtval[0] = rtval[1];
114                         rtval[1] = chksum;
115                 }
116                 for (i = 0, chksum = -2; i < rtcnt; i++) {
117                         if (rtflg[i])
118                                 chksum++;
119                 }
120                 fprintf(ofp, ":%02X", chksum);
121                 for (i = 0; i < rtcnt ; i++) {
122                         if (rtflg[i]) {
123                                 fprintf(ofp, "%02X", rtval[i]);
124                                 chksum += rtval[i];
125                         }
126                         if (i == 1) {
127                                 fprintf(ofp, "00");
128                         }
129                 }
130                 fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
131         } else {
132                 fprintf(ofp, ":00000001FF\n");
133         }
134 }