a3d3b0200a6cac84f231bd2b987eb61ce2a541b5
[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 "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 functions:
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
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                 for (i = 0; i < rtcnt ; i++) {
121                         if (rtflg[i]) {
122                                 fprintf(ofp, "%02X", rtval[i]);
123                                 chksum += rtval[i];
124                         }
125                         if (i == 1) {
126                                 fprintf(ofp, "00");
127                         }
128                 }
129                 fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
130         } else {
131                 fprintf(ofp, ":00000001FF\n");
132         }
133 }