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