summaryrefslogtreecommitdiff
path: root/sdas/linksrc/lkihx.c
blob: 5a482ab81b4202aec9b875f65afe5abef1499fa8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* lkihx.c

   Copyright (C) 1989-1995 Alan R. Baldwin
   721 Berkeley St., Kent, Ohio 44240

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <string.h>
#include "sdld.h"
#include "aslink.h"

/*)Module   lkihx.c
 *
 *  The module lkihx.c contains the function to
 *  output the relocated object code in the
 *  Intel Hex format.
 *
 *  lkihx.c contains the following functions:
 *      VOID    hexRecord(addr, rtvalIndex)
 *      VOID    ihx(i)
 *      VOID    ihxExtendedLinearAddress(a)
 *
 *  local variables: hexPageOverrun, lastHexAddr
 */

/*Intel Format
 *      Record Mark Field    -  This  field  signifies  the  start  of a
 *                              record, and consists of an  ascii  colon
 *                              (:).
 *
 *      Record Length Field  -  This   field   consists   of  two  ascii
 *                              characters which indicate the number  of
 *                              data   bytes   in   this   record.   The
 *                              characters are the result of  converting
 *                              the  number  of  bytes  in binary to two
 *                              ascii characters, high digit first.   An
 *                              End  of  File  record contains two ascii
 *                              zeros in this field.
 *
 *      Load Address Field   -  This  field  consists  of the four ascii
 *                              characters which result from  converting
 *                              the  the  binary value of the address in
 *                              which to begin loading this record.  The
 *                              order is as follows:
 *
 *                                  High digit of high byte of address.
 *                                  Low digit of high byte of address.
 *                                  High digit of low byte of address.
 *                                  Low digit of low byte of address.
 *
 *                              In an End of File record this field con-
 *                              sists of four ascii zeros, in a start
 *				address record this is the program entry
 *				address (low), or in a segment record
 *				this is the high order address.
 *
 *      Record Type Field    -  This  field  identifies the record type,
 *                              which is either 0 for data records,  1
 *                              for an End of File record, 3 for a
 *				start address, or 4 for a
 *				segment record.  It consists
 *                              of two ascii characters, with  the  high
 *                              digit of the record type first, followed
 *                              by the low digit of the record type.
 *
 *      Data Field           -  This  field consists of the actual data,
 *                              converted to two ascii characters,  high
 *                              digit first.  There are no data bytes in
 *                              the End of File record.
 *
 *      Checksum Field       -  The  checksum  field is the 8 bit binary
 *                              sum of the record length field, the load
 *                              address  field,  the  record type field,
 *                              and the data field.  This  sum  is  then
 *                              negated  (2's  complement) and converted
 *                              to  two  ascii  characters,  high  digit
 *                              first.
 */

/* Static variable which holds the count of hex page overruns
 * (crossings of the 64kB boundary). Cleared at explicit extended
 * address output.
 */
static int hexPageOverrun = 0;

/* Global which holds the last (16 bit) address of hex record.
 * Cleared at begin of new area or when the extended address is output.
 */
unsigned int lastHexAddr = 0;


/*)Function hexRecord(addr, rtvalIndex)
 *
 *      unsigned addr       starting address of hex record
 *      int rtvalIndex      starting index into the rtval[] array
 *
 *  The function hexRecord() outputs the relocated data
 *  in the standard Intel Hex format (with inserting
 *  the extended address record if necessary).
 *
 *  local variables:
 *      a_uint  chksum      byte checksum
 *      int     i           index for loops
 *      int     overrun     temporary storage for hexPageOverrun
 *      int     bytes       counter for bytes written
 *
 *  global variables:
 *      FILE *  ofp         output file handle
 *      int     rtcnt       count of data words
 *      int     rtflg[]     output the data flag
 *      a_uint  rtval[]     relocated data
 *
 *  functions called:
 *      int     fprintf()           c_library
 *      ihxExtendedLinearAddress()  lkihx.c
 *      hexRecord()                 lkihx.c     (recursion)
 *
 *  side effects:
 *      hexPageOverrun is eventually incremented,
 *      lastHexAddr is updated
 */

VOID
hexRecord(unsigned addr, int rtvalIndex)
{
    a_uint chksum;
    int i, overrun, bytes;

    for (i = rtvalIndex, chksum = 0; i < rtcnt; i++) {
        if (rtflg[i]) {
            if (addr + ++chksum > 0xffff)
                break;
        }
    }
    if (chksum == 0)
        return;         // nothing to output

    /* Is this record in the same bank as previous? */
    if ((TARGET_IS_8051 && ((lastHexAddr>>16) != (addr>>16)) && (rflag)) ||
        (TARGET_IS_6808 && lastHexAddr > addr)) {
        overrun = hexPageOverrun + 1;
        ihxExtendedLinearAddress(lastExtendedAddress + overrun);
        hexPageOverrun = overrun;
        hexRecord(addr, rtvalIndex);
        return;
    }

    lastHexAddr = addr;
    fprintf(ofp, ":%02X%04X00", chksum, addr);
    chksum += (addr >> 8) + (addr & 0xff);
    for (i = rtvalIndex, bytes = 0; i < rtcnt; i++) {
        if (rtflg[i]) {
            fprintf(ofp, "%02X", rtval[i]);
            chksum += rtval[i];
            if (TARGET_IS_8051) {
                if (addr + ++bytes > 0xffff) {
                    if (rflag) {
                        fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
                        overrun = hexPageOverrun + 1;
                        ihxExtendedLinearAddress(lastExtendedAddress + overrun);
                        hexPageOverrun = overrun;
                        hexRecord(0, i + 1);
                        return;
                    } else {
                        fprintf(stderr,
                            "warning: extended linear address encountered; "
                            "you probably want the -r flag.\n");
                    }
                }
            }
        }
    }
    fprintf(ofp, "%02X\n", (0-chksum) & 0xff);
}

/*)Function ihx(i)
 *
 *      int     i           0 - process data
 *                          1 - end of data
 *
 *  The function ihx() calls the hexRecord() function for processing data
 *  or writes the End of Data record to the file defined by ofp.
 *
 *  local variables:
 *      a_uint  n           auxiliary variable
 *
 *  global variables:
 *      int     hilo        byte order
 *      FILE *  ofp         output file handle
 *      a_uint  rtval[]     relocated data
 *
 *  functions called:
 *      VOID hexRecord()    lkihx.c
 *      int fprintf()       c_library
 *
 *  side effects:
 *      The sequence of rtval[0], rtval[1] is eventually changed.
 */

VOID
ihx(int i)
{
    a_uint n;
    if (i) {
        if (TARGET_IS_6808 && ap->a_flag & A_NOLOAD)
            return;

        if (hilo == 0) {
            n = rtval[0];
            rtval[0] = rtval[1];
            rtval[1] = n;
        }
        hexRecord((rtval[0]<<8) + rtval[1], 2);
    } else {
        if (TARGET_IS_8051 && rflag) /* linear start address, hardcoded as reset vector (0x0000) */
            fprintf(ofp, ":0400000500000000F7\n");
        fprintf(ofp, ":00000001FF\n");
    }
}

/*)Function ihxNewArea(i)
 * The function ihxNewArea() is called when processing of new area is started.
 * It resets the value of lastHexAddr.
 */

VOID
ihxNewArea()
{
    lastHexAddr = 0;
}

/*)Function ihxExtendedLinearAddress(i)
 *
 *      a_uint  i           16 bit extended linear address.
 *
 *  The function ihxExtendedLinearAddress() writes an extended
 *  linear address record (type 04) to the output file.
 *
 *  local variables:
 *      a_uint  chksum      byte checksum
 *
 *  global variables:
 *      FILE *  ofp         output file handle
 *
 *  functions called:
 *      int     fprintf()   c_library
 *
 *  side effects:
 *      The data is output to the file defined by ofp.
 *      hexPageOverrun and lastHexAddr is cleared
 */
VOID
ihxExtendedLinearAddress(a_uint a)
{
    a_uint  chksum;

    /* The checksum is the complement of the bytes in the
     * record: the 2 is record length, 4 is the extended linear
     * address record type, plus the two address bytes.
     */
    chksum = 2 + 4 + (a & 0xff) + ((a >> 8) & 0xff);

    fprintf(ofp, ":02000004%04X%02X\n", a & 0xffff, (0-chksum) & 0xff);
    hexPageOverrun = 0;
    lastHexAddr = 0;
}