* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / link / mcs51 / lks19.c
1 /* lks19.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       lks19.c
24  *
25  *      The module lks19.c contains the function to
26  *      output the relocated object code in the
27  *      Motorola S19 format.
28  *
29  *      lks19.c contains the following function:
30  *              VOID    s19(i)
31  *
32  *      lks19.c contains no local variables.
33  */
34
35 /*)S19 Format
36  *      Record Type Field    -  This  field  signifies  the  start  of a
37  *                              record and  identifies  the  the  record
38  *                              type as follows:
39  *
40  *                                  Ascii S1 - Data Record
41  *                                  Ascii S9 - End of File Record
42  *
43  *      Record Length Field  -  This  field  specifies the record length
44  *                              which includes the  address,  data,  and
45  *                              checksum   fields.   The  8  bit  record
46  *                              length value is converted to  two  ascii
47  *                              characters, high digit first.
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  *      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, and the data field.  This
73  *                              sum is then  complemented  (1's  comple-
74  *                              ment)   and   converted   to  two  ascii
75  *                              characters, high digit first.
76  */
77
78 /*)Function     s19(i)
79  *
80  *              int     i               0 - process data
81  *                                      1 - end of data
82  *
83  *      The function s19() outputs the relocated data
84  *      in the standard Motorola S19 format.
85  *
86  *      local variables:
87  *              Addr_T  chksum          byte checksum
88  *
89  *      global variables:
90  *              int     hilo            byte order
91  *              FILE *  ofp             output file handle
92  *              int     rtcnt           count of data words
93  *              int     rtflg[]         output the data flag
94  *              Addr_T  rtval[]         relocated data
95  *
96  *      functions called:
97  *              int     fprintf()       c_library
98  *
99  *      side effects:
100  *              The data is output to the file defined by ofp.
101  */
102
103 VOID
104 s19(i)
105 {
106         register Addr_T chksum;
107
108         if (i) {
109                 if (hilo == 0) {
110                         chksum = rtval[0];
111                         rtval[0] = rtval[1];
112                         rtval[1] = chksum;
113                 }
114                 for (i = 0, chksum = 1; i < rtcnt; i++) {
115                         if (rtflg[i])
116                                 chksum++;
117                 }
118                 fprintf(ofp, "S1%02X", chksum);
119                 for (i = 0; i < rtcnt ; i++) {
120                         if (rtflg[i]) {
121                                 fprintf(ofp, "%02X", rtval[i]);
122                                 chksum += rtval[i];
123                         }
124                 }
125                 fprintf(ofp, "%02X\n", (0-chksum-1) & 0xff);
126         } else {
127                 fprintf(ofp, "S9030000FC\n");
128         }
129 }