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