annotate lwasm/output.c @ 574:a0c9433dc0d4 default tip

Updated submission guidelines including notes about evangelism. TLDR: Don't.
author William Astle <lost@l-w.ca>
date Mon, 04 Mar 2024 10:10:38 -0700
parents e10618b48e68
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
2 output.c
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
3 Copyright © 2009, 2010 William Astle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
4
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
5 This file is part of LWASM.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
6
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
10 version.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
11
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
15 more details.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
16
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
19
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
20
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
21 Contains the code for actually outputting the assembled code
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
22 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
23 #include <errno.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
24 #include <stdio.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
25 #include <string.h>
360
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
26
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
27 #ifndef _MSC_VER
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
28 #include <unistd.h> // for unlink
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
29 #endif
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
30
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
31 #include <lw_alloc.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
32 #include <lw_expr.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
33
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
34 #include "lwasm.h"
535
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
35 #include "instab.h"
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
36
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
37 void write_code_raw(asmstate_t *as, FILE *of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
38 void write_code_decb(asmstate_t *as, FILE *of);
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
39 void write_code_BASIC(asmstate_t *as, FILE *of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
40 void write_code_rawrel(asmstate_t *as, FILE *of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
41 void write_code_obj(asmstate_t *as, FILE *of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
42 void write_code_os9(asmstate_t *as, FILE *of);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
43 void write_code_hex(asmstate_t *as, FILE *of);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
44 void write_code_srec(asmstate_t *as, FILE *of);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
45 void write_code_ihex(asmstate_t *as, FILE *of);
432
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
46 void write_code_lwmod(asmstate_t *as, FILE *of);
543
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
47 void write_code_dragon(asmstate_t *as, FILE *of);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
48 void write_code_abs(asmstate_t *as, FILE *of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
49
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
50 // this prevents warnings about not using the return value of fwrite()
221
2b784a28428e Silence a few warnings
William Astle <lost@l-w.ca>
parents: 195
diff changeset
51 // r++ prevents the "set but not used" warnings; should be optimized out
2b784a28428e Silence a few warnings
William Astle <lost@l-w.ca>
parents: 195
diff changeset
52 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); r++; } while (0)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
53
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
54 void do_output(asmstate_t *as)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
55 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
56 FILE *of;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
57
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
58 if (as -> errorcount > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
59 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
60 fprintf(stderr, "Not doing output due to assembly errors.\n");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
61 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
62 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
63
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
64 of = fopen(as -> output_file, "wb");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
65 if (!of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
66 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
67 fprintf(stderr, "Cannot open '%s' for output", as -> output_file);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
68 perror("");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
69 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
70 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
71
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
72 switch (as -> output_format)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
73 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
74 case OUTPUT_RAW:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
75 write_code_raw(as, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
76 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
77
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
78 case OUTPUT_DECB:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
79 write_code_decb(as, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
80 break;
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
81
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
82 case OUTPUT_BASIC:
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
83 write_code_BASIC(as, of);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
84 break;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
85
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
86 case OUTPUT_RAWREL:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
87 write_code_rawrel(as, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
88 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
89
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
90 case OUTPUT_OBJ:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
91 write_code_obj(as, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
92 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
93
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
94 case OUTPUT_OS9:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
95 write_code_os9(as, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
96 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
97
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
98 case OUTPUT_HEX:
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
99 write_code_hex(as, of);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
100 break;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
101
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
102 case OUTPUT_SREC:
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
103 write_code_srec(as, of);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
104 break;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
105
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
106 case OUTPUT_IHEX:
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
107 write_code_ihex(as, of);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
108 break;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
109
432
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
110 case OUTPUT_LWMOD:
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
111 write_code_lwmod(as, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
112 break;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
113
543
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
114 case OUTPUT_DRAGON:
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
115 write_code_dragon(as, of);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
116 break;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
117
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
118 case OUTPUT_ABS:
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
119 write_code_abs(as, of);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
120 break;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
121
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
122 default:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
123 fprintf(stderr, "BUG: unrecognized output format when generating output file\n");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
124 fclose(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
125 unlink(as -> output_file);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
126 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
127 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
128
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
129 fclose(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
130 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
131
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
132 int write_code_BASIC_fprintf(FILE *of, int linelength, int *linenumber, int value)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
133 {
536
33a59e232a5b Fix basic output target to keep lines below 249 characters
William Astle <lost@l-w.ca>
parents: 535
diff changeset
134 // 240 should give enough room for a 5 digit value and a comma with a bit of extra
33a59e232a5b Fix basic output target to keep lines below 249 characters
William Astle <lost@l-w.ca>
parents: 535
diff changeset
135 // space in case something unusual happens without going over the 249 character
33a59e232a5b Fix basic output target to keep lines below 249 characters
William Astle <lost@l-w.ca>
parents: 535
diff changeset
136 // limit Color Basic has on input lines.
33a59e232a5b Fix basic output target to keep lines below 249 characters
William Astle <lost@l-w.ca>
parents: 535
diff changeset
137 if (linelength > 240)
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
138 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
139 fprintf(of, "\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
140 linelength = fprintf(of, "%d DATA ", *linenumber);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
141 *linenumber += 10;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
142 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
143 else
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
144 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
145 linelength += fprintf(of, ",");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
146 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
147 linelength += fprintf(of, "%d", value);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
148
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
149 return linelength;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
150 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
151
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
152 void write_code_BASIC(asmstate_t *as, FILE *of)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
153 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
154 line_t *cl;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
155 line_t *startblock = as -> line_head;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
156 line_t *endblock;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
157 int linenumber, linelength, startaddress, lastaddress, address;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
158 int outidx;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
159
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
160 fprintf(of, "10 READ A,B\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
161 fprintf(of, "20 IF A=-1 THEN 70\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
162 fprintf(of, "30 FOR C = A TO B\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
163 fprintf(of, "40 READ D:POKE C,D\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
164 fprintf(of, "50 NEXT C\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
165 fprintf(of, "60 GOTO 10\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
166
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
167 if (as -> execaddr == 0)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
168 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
169 fprintf(of, "70 END");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
170 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
171 else
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
172 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
173 fprintf(of, "70 EXEC %d", as -> execaddr);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
174 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
175
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
176 linenumber = 80;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
177 linelength = 255;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
178
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
179 while(startblock)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
180 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
181 startaddress = -1;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
182 endblock = NULL;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
183
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
184 for (cl = startblock; cl; cl = cl -> next)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
185 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
186 if (cl -> outputl < 0)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
187 continue;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
188
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
189 address = lw_expr_intval(cl -> addr);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
190
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
191 if (startaddress == -1)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
192 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
193 startaddress = address;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
194 lastaddress = address + cl -> outputl - 1;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
195 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
196 else
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
197 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
198 if (lastaddress != address - 1)
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
199 {
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
200 endblock = cl;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
201 break;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
202 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
203
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
204 lastaddress += cl -> outputl;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
205 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
206 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
207
410
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
208 if (startaddress != -1)
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
209 {
410
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
210 linelength = write_code_BASIC_fprintf(of, linelength, &linenumber, startaddress);
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
211 linelength = write_code_BASIC_fprintf(of, linelength, &linenumber, lastaddress);
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
212
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
213 for (cl = startblock; cl != endblock; cl = cl -> next)
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
214 {
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
215 if (cl -> outputl < 0)
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
216 continue;
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
217
410
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
218 for (outidx=0; outidx<cl -> outputl; outidx++)
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
219 {
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
220 linelength = write_code_BASIC_fprintf(of, linelength, &linenumber, cl -> output[outidx]);
7f538053492c Fix a possible uninitialized variable reference
William Astle <lost@l-w.ca>
parents: 406
diff changeset
221 }
406
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
222 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
223 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
224
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
225 startblock = cl;
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
226 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
227
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
228 linelength = write_code_BASIC_fprintf(of, linelength, &linenumber, -1);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
229 linelength = write_code_BASIC_fprintf(of, linelength, &linenumber, -1);
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
230
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
231 fprintf(of, "\n");
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
232 }
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
233
4411a6123716 Add "basic" output format
William Astle <lost@l-w.ca>
parents: 360
diff changeset
234
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
235 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
236 rawrel output treats an ORG directive as an offset from the start of the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
237 file. Undefined results will occur if an ORG directive moves the output
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
238 pointer backward. This particular implementation uses "fseek" to handle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
239 ORG requests and to skip over RMBs.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
240
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
241 This simple brain damanged method simply does an fseek before outputting
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
242 each instruction.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
243 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
244 void write_code_rawrel(asmstate_t *as, FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
245 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
246 line_t *cl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
247
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
248 for (cl = as -> line_head; cl; cl = cl -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
249 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
250 if (cl -> outputl <= 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
251 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
252
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
253 fseek(of, lw_expr_intval(cl -> addr), SEEK_SET);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
254 writebytes(cl -> output, cl -> outputl, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
255 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
256 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
257
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
258 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
259 raw merely writes all the bytes directly to the file as is. ORG is just a
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
260 reference for the assembler to handle absolute references. Multiple ORG
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
261 statements will produce mostly useless results
535
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
262
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
263 However, if a run of RMBs exists at the start that is ended by an ORG
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
264 statement, that run of RMBs will not be output as a zero fill.
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
265 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
266 void write_code_raw(asmstate_t *as, FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
267 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
268 line_t *cl;
535
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
269 line_t *sl;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
270
535
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
271 sl = as -> line_head;
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
272 for (cl = sl; cl; cl = cl -> next)
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
273 {
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
274 if (cl -> outputl > 0)
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
275 break;
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
276 if (instab[cl -> insn].flags & lwasm_insn_org)
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
277 sl = cl;
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
278 }
a584b9ddffc4 Update raw output to work with RMB only definitions at the start
William Astle <lost@l-w.ca>
parents: 531
diff changeset
279 for (cl = sl; cl; cl = cl -> next)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
280 {
531
a812bb4d3a51 Correct raw output target in lwasm to NUL pad RMBs
William Astle <lost@l-w.ca>
parents: 500
diff changeset
281 if (cl -> len > 0 && cl -> outputl < 0)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
282 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
283 int i;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
284 for (i = 0; i < cl -> len; i++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
285 writebytes("\0", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
286 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
287 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
288 else if (cl -> outputl > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
289 writebytes(cl -> output, cl -> outputl, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
290 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
291 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
292
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
293
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
294 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
295 OS9 target also just writes all the bytes in order. No need for anything
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
296 else.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
297 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
298 void write_code_os9(asmstate_t *as, FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
299 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
300 line_t *cl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
301
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
302 for (cl = as -> line_head; cl; cl = cl -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
303 {
127
d92b9c968731 Removed protection from outputting code outside os9 module in os9 target
lost@l-w.ca
parents: 12
diff changeset
304 // if (cl -> inmod == 0)
d92b9c968731 Removed protection from outputting code outside os9 module in os9 target
lost@l-w.ca
parents: 12
diff changeset
305 // continue;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
306 if (cl -> len > 0 && cl -> outputl == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
307 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
308 int i;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
309 for (i = 0; i < cl -> len; i++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
310 writebytes("\0", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
311 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
312 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
313 else if (cl -> outputl > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
314 writebytes(cl -> output, cl -> outputl, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
315 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
316 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
317
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
318 void write_code_decb(asmstate_t *as, FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
319 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
320 long preambloc;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
321 line_t *cl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
322 int blocklen = -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
323 int nextcalc = -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
324 unsigned char outbuf[5];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
325 int caddr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
326
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
327 for (cl = as -> line_head; cl; cl = cl -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
328 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
329 if (cl -> outputl < 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
330 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
331 caddr = lw_expr_intval(cl -> addr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
332 if (caddr != nextcalc && cl -> outputl > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
333 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
334 // need preamble here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
335 if (blocklen > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
336 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
337 // update previous preamble if needed
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
338 fseek(of, preambloc, SEEK_SET);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
339 outbuf[0] = (blocklen >> 8) & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
340 outbuf[1] = blocklen & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
341 writebytes(outbuf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
342 fseek(of, 0, SEEK_END);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
343 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
344 blocklen = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
345 nextcalc = caddr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
346 outbuf[0] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
347 outbuf[1] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
348 outbuf[2] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
349 outbuf[3] = (nextcalc >> 8) & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
350 outbuf[4] = nextcalc & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
351 preambloc = ftell(of) + 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
352 writebytes(outbuf, 5, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
353 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
354 nextcalc += cl -> outputl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
355 writebytes(cl -> output, cl -> outputl, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
356 blocklen += cl -> outputl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
357 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
358 if (blocklen > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
359 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
360 fseek(of, preambloc, SEEK_SET);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
361 outbuf[0] = (blocklen >> 8) & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
362 outbuf[1] = blocklen & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
363 writebytes(outbuf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
364 fseek(of, 0, SEEK_END);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
365 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
366
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
367 // now write postamble
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
368 outbuf[0] = 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
369 outbuf[1] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
370 outbuf[2] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
371 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
372 outbuf[4] = (as -> execaddr) & 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
373 writebytes(outbuf, 5, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
374 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
375
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
376 int fetch_output_byte(line_t *cl, char *value, int *addr)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
377 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
378 static int outidx = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
379 static int lastaddr = -2;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
380
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
381 // try to read next byte in current line's output field
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
382 if ((cl -> outputl > 0) && (outidx < cl -> outputl))
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
383 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
384 *addr = lw_expr_intval(cl -> addr) + outidx;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
385 *value = *(cl -> output + outidx++);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
386
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
387 // this byte follows the previous byte (contiguous, rc = 1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
388 if (*addr == lastaddr + 1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
389 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
390 lastaddr = *addr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
391 return 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
392 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
393
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
394 // this byte does not follow prev byte (disjoint, rc = -1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
395 else
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
396 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
397 lastaddr = *addr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
398 return -1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
399 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
400 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
401
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
402 // no (more) output from this line (rc = 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
403 else
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
404 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
405 outidx = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
406 return 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
407 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
408 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
409
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
410
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
411 /* a simple ASCII hex file format */
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
412
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
413 void write_code_hex(asmstate_t *as, FILE *of)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
414 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
415 const int RECLEN = 16;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
416
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
417 line_t *cl;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
418 char outbyte;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
419 int outaddr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
420 int rc;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
421
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
422 for (cl = as -> line_head; cl; cl = cl -> next)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
423 do
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
424 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
425 rc = fetch_output_byte(cl, &outbyte, &outaddr);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
426
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
427 // if address jump or xxx0 address, start new line
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
428 if ((rc == -1) || ((rc == 1) && (outaddr % RECLEN == 0)))
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
429 {
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
430 fprintf(of, "\r\n%04X:", (unsigned int)(outaddr & 0xffff));
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
431 fprintf(of, "%02X", (unsigned char)outbyte);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
432 rc = -1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
433 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
434 if (rc == 1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
435 fprintf(of, ",%02X", (unsigned char)outbyte);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
436 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
437 while (rc);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
438 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
439
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
440
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
441 /* Motorola S19 hex file format */
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
442
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
443 void write_code_srec(asmstate_t *as, FILE *of)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
444 {
360
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
445 #define SRECLEN 16
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
446 #define HDRLEN 51
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
447
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
448 line_t *cl;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
449 char outbyte;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
450 int outaddr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
451 int rc;
360
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
452 unsigned int i;
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
453 int recaddr = 0;
360
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
454 unsigned int recdlen = 0;
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
455 unsigned char recdata[SRECLEN];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
456 int recsum;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
457 int reccnt = -1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
458 char rechdr[HDRLEN];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
459
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
460 for (cl = as -> line_head; cl; cl = cl -> next)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
461 do
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
462 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
463 rc = fetch_output_byte(cl, &outbyte, &outaddr);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
464
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
465 // if address jump or xxx0 address, start new S1 record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
466 if ((rc == -1) || ((rc == 1) && (outaddr % SRECLEN == 0)))
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
467 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
468 // if not already done so, emit an S0 header record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
469 if (reccnt < 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
470 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
471 // build header from version and filespec
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
472 // e.g. "[lwtools X.Y] filename.asm"
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
473 strcpy(rechdr, "[");
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
474 strcat(rechdr, PACKAGE_STRING);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
475 strcat(rechdr, "] ");
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
476 i = strlen(rechdr);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
477 strncat(rechdr, cl -> linespec, HDRLEN - 1 - i);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
478 recsum = strlen(rechdr) + 3;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
479 fprintf(of, "S0%02X0000", recsum);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
480 for (i = 0; i < strlen(rechdr); i++)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
481 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
482 fprintf(of, "%02X", (unsigned char)rechdr[i]);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
483 recsum += (unsigned char)rechdr[i];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
484 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
485 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
486 reccnt = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
487 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
488
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
489 // flush any current S1 record before starting new one
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
490 if (recdlen > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
491 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
492 recsum = recdlen + 3;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
493 fprintf(of, "S1%02X%04X", recdlen + 3, recaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
494 for (i = 0; i < recdlen; i++)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
495 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
496 fprintf(of, "%02X", (unsigned char)recdata[i]);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
497 recsum += (unsigned char)recdata[i];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
498 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
499 recsum += (recaddr >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
500 recsum += recaddr & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
501 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
502 reccnt += 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
503 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
504
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
505 // now start the new S1 record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
506 recdlen = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
507 recaddr = outaddr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
508 rc = 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
509 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
510
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
511 // for each new byte read, add to recdata[]
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
512 if (rc == 1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
513 recdata[recdlen++] = outbyte;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
514 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
515 while (rc);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
516
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
517 // done with all output lines, flush the final S1 record (if any)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
518 if (recdlen > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
519 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
520 recsum = recdlen + 3;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
521 fprintf(of, "S1%02X%04X", recdlen + 3, recaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
522 for (i = 0; i < recdlen; i++)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
523 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
524 fprintf(of, "%02X", (unsigned char)recdata[i]);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
525 recsum += (unsigned char)recdata[i];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
526 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
527 recsum += (recaddr >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
528 recsum += recaddr & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
529 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
530 reccnt += 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
531 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
532
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
533 // if any S1 records were output, close with S5 and S9 records
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
534 if (reccnt > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
535 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
536 // emit S5 count record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
537 recsum = 3;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
538 recsum += (reccnt >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
539 recsum += reccnt & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
540 fprintf(of, "S503%04X", (unsigned int)reccnt);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
541 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
542
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
543 // emit S9 end-of-file record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
544 recsum = 3;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
545 recsum += (as -> execaddr >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
546 recsum += (as -> execaddr) & 0xFF;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
547 fprintf(of, "S903%04X", as -> execaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
548 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
549 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
550 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
551
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
552
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
553 /* Intel hex file format */
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
554
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
555 void write_code_ihex(asmstate_t *as, FILE *of)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
556 {
360
ade217fd76a5 Use #define instead of const int to avoid issues with some compilers
William Astle <lost@l-w.ca>
parents: 321
diff changeset
557 #define IRECLEN 16
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
558
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
559 line_t *cl;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
560 char outbyte;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
561 int outaddr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
562 int rc;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
563 int i;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
564 int recaddr = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
565 int recdlen = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
566 unsigned char recdata[IRECLEN];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
567 int recsum;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
568 int reccnt = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
569
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
570 for (cl = as -> line_head; cl; cl = cl -> next)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
571 do
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
572 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
573 rc = fetch_output_byte(cl, &outbyte, &outaddr);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
574
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
575 // if address jump or xxx0 address, start new ihx record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
576 if ((rc == -1) || ((rc == 1) && (outaddr % IRECLEN == 0)))
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
577 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
578 // flush any current ihex record before starting new one
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
579 if (recdlen > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
580 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
581 recsum = recdlen;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
582 fprintf(of, ":%02X%04X00", recdlen, recaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
583 for (i = 0; i < recdlen; i++)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
584 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
585 fprintf(of, "%02X", (unsigned char)recdata[i]);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
586 recsum += (unsigned char)recdata[i];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
587 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
588 recsum += (recaddr >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
589 recsum += recaddr & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
590 fprintf(of, "%02X\r\n", (unsigned char)(256 - recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
591 reccnt += 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
592 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
593
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
594 // now start the new ihex record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
595 recdlen = 0;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
596 recaddr = outaddr;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
597 rc = 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
598 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
599
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
600 // for each new byte read, add to recdata[]
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
601 if (rc == 1)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
602 recdata[recdlen++] = outbyte;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
603 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
604 while (rc);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
605
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
606 // done with all output lines, flush the final ihex record (if any)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
607 if (recdlen > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
608 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
609 recsum = recdlen;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 410
diff changeset
610 fprintf(of, ":%02X%04X00", recdlen, recaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
611 for (i = 0; i < recdlen; i++)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
612 {
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
613 fprintf(of, "%02X", (unsigned char)recdata[i]);
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
614 recsum += (unsigned char)recdata[i];
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
615 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
616 recsum += (recaddr >> 8) & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
617 recsum += recaddr & 0xFF;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
618 fprintf(of, "%02X\r\n", (unsigned char)(256 - recsum));
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
619 reccnt += 1;
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
620 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
621
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
622 // if any ihex records were output, close with a "01" record
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
623 if (reccnt > 0)
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
624 {
443
999ae00d0919 Fix up execution address handling for ihex and srec output (lwasm)
William Astle <lost@l-w.ca>
parents: 432
diff changeset
625 fprintf(of, ":00%04X01FF", as -> execaddr & 0xffff);
321
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
626 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
627 }
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
628
d4ac484d0ec6 Add support for Motorola SREC and Intel Hex output formats to lwasm.
Tom LeMense <tlemense@yahoo.com>
parents: 221
diff changeset
629
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
630 void write_code_obj_sbadd(sectiontab_t *s, unsigned char b)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
631 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
632 if (s -> oblen >= s -> obsize)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
633 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
634 s -> obytes = lw_realloc(s -> obytes, s -> obsize + 128);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
635 s -> obsize += 128;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
636 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
637 s -> obytes[s -> oblen] = b;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
638 s -> oblen += 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
639 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
640
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
641
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
642 int write_code_obj_expraux(lw_expr_t e, void *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
643 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
644 int tt;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
645 int v;
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
646 int count = 1;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
647 unsigned char buf[16];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
648
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
649 tt = lw_expr_type(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
650
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
651 switch (tt)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
652 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
653 case lw_expr_type_oper:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
654 buf[0] = 0x04;
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
655
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
656 switch (lw_expr_whichop(e))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
657 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
658 case lw_expr_oper_plus:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
659 buf[1] = 0x01;
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
660 count = lw_expr_operandcount(e) - 1;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
661 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
662
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
663 case lw_expr_oper_minus:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
664 buf[1] = 0x02;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
665 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
666
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
667 case lw_expr_oper_times:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
668 buf[1] = 0x03;
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
669 count = lw_expr_operandcount(e) - 1;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
670 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
671
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
672 case lw_expr_oper_divide:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
673 buf[1] = 0x04;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
674 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
675
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
676 case lw_expr_oper_mod:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
677 buf[1] = 0x05;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
678 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
679
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
680 case lw_expr_oper_intdiv:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
681 buf[1] = 0x06;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
682 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
683
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
684 case lw_expr_oper_bwand:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
685 buf[1] = 0x07;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
686 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
687
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
688 case lw_expr_oper_bwor:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
689 buf[1] = 0x08;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
690 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
691
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
692 case lw_expr_oper_bwxor:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
693 buf[1] = 0x09;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
694 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
695
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
696 case lw_expr_oper_and:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
697 buf[1] = 0x0A;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
698 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
699
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
700 case lw_expr_oper_or:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
701 buf[1] = 0x0B;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
702 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
703
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
704 case lw_expr_oper_neg:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
705 buf[1] = 0x0C;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
706 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
707
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
708 case lw_expr_oper_com:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
709 buf[1] = 0x0D;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
710 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
711
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
712 default:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
713 buf[1] = 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
714 }
12
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
715 while (count--)
6b9991fb39b6 Brought forward patch to fix bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 2
diff changeset
716 writebytes(buf, 2, 1, of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
717 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
718
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
719 case lw_expr_type_int:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
720 v = lw_expr_intval(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
721 buf[0] = 0x01;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
722 buf[1] = (v >> 8) & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
723 buf[2] = v & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
724 writebytes(buf, 3, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
725 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
726
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
727 case lw_expr_type_special:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
728 v = lw_expr_specint(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
729 switch (v)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
730 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
731 case lwasm_expr_secbase:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
732 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
733 // replaced with a synthetic symbol
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
734 sectiontab_t *se;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
735 se = lw_expr_specptr(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
736
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
737 writebytes("\x03\x02", 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
738 writebytes(se -> name, strlen(se -> name) + 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
739 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
740 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
741 case lwasm_expr_import:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
742 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
743 importlist_t *ie;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
744 ie = lw_expr_specptr(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
745 buf[0] = 0x02;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
746 writebytes(buf, 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
747 writebytes(ie -> symbol, strlen(ie -> symbol) + 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
748 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
749 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
750 case lwasm_expr_syment:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
751 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
752 struct symtabe *se;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
753 se = lw_expr_specptr(e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
754 buf[0] = 0x03;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
755 writebytes(buf, 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
756 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
757 if (se -> context != -1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
758 {
2
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
759 sprintf((char *)buf, "\x01%d", se -> context);
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
760 writebytes(buf, strlen((char *)buf), 1, of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
761 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
762 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
763 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
764 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
765 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
766
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
767 default:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
768 // unrecognized term type - replace with integer 0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
769 // fprintf(stderr, "Unrecognized term type: %s\n", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
770 buf[0] = 0x01;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
771 buf[1] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
772 buf[2] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
773 writebytes(buf, 3, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
774 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
775 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
776 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
777 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
778
195
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
779 void write_code_obj_auxsym(asmstate_t *as, FILE *of, sectiontab_t *s, struct symtabe *se2)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
780 {
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
781 struct symtabe *se;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
782 unsigned char buf[16];
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
783
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
784 if (!se2)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
785 return;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
786 write_code_obj_auxsym(as, of, s, se2 -> left);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
787
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
788 for (se = se2; se; se = se -> nextver)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
789 {
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
790 lw_expr_t te;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
791
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
792 debug_message(as, 200, "Consider symbol %s (%p) for export in section %p", se -> symbol, se -> section, s);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
793
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
794 // ignore symbols not in this section
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
795 if (se -> section != s)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
796 continue;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
797
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
798 debug_message(as, 200, " In section");
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
799
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
800 if (se -> flags & symbol_flag_set)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
801 continue;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
802
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
803 debug_message(as, 200, " Not symbol_flag_set");
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
804
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
805 te = lw_expr_copy(se -> value);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
806 debug_message(as, 200, " Value=%s", lw_expr_print(te));
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
807 as -> exportcheck = 1;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
808 as -> csect = s;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
809 lwasm_reduce_expr(as, te);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
810 as -> exportcheck = 0;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
811
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
812 debug_message(as, 200, " Value2=%s", lw_expr_print(te));
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
813
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
814 // don't output non-constant symbols
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
815 if (!lw_expr_istype(te, lw_expr_type_int))
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
816 {
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
817 lw_expr_destroy(te);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
818 continue;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
819 }
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
820
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
821 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
822 if (se -> context >= 0)
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
823 {
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
824 writebytes("\x01", 1, 1, of);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
825 sprintf((char *)buf, "%d", se -> context);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
826 writebytes(buf, strlen((char *)buf), 1, of);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
827 }
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
828 // the "" is NOT an error
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
829 writebytes("", 1, 1, of);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
830
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
831 // write the address
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
832 buf[0] = (lw_expr_intval(te) >> 8) & 0xff;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
833 buf[1] = lw_expr_intval(te) & 0xff;
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
834 writebytes(buf, 2, 1, of);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
835 lw_expr_destroy(te);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
836 }
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
837 write_code_obj_auxsym(as, of, s, se2 -> right);
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
838 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
839
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
840 void write_code_obj(asmstate_t *as, FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
841 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
842 line_t *l;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
843 sectiontab_t *s;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
844 reloctab_t *re;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
845 exportlist_t *ex;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
846
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
847 int i;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
848 unsigned char buf[16];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
849
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
850 // output the magic number and file header
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
851 // the 8 is NOT an error
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
852 writebytes("LWOBJ16", 8, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
853
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
854 // run through the entire system and build the byte streams for each
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
855 // section; at the same time, generate a list of "local" symbols to
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
856 // output for each section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
857 // NOTE: for "local" symbols, we will append \x01 and the ascii string
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
858 // of the context identifier (so sym in context 1 would be "sym\x011"
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
859 // we can do this because the linker can handle symbols with any
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
860 // character other than NUL.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
861 // also we will generate a list of incomplete references for each
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
862 // section along with the actual definition that will be output
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
863
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
864 // once all this information is generated, we will output each section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
865 // to the file
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
866
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
867 // NOTE: we build everything in memory then output it because the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
868 // assembler accepts multiple instances of the same section but the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
869 // linker expects only one instance of each section in the object file
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
870 // so we need to collect all the various pieces of a section together
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
871 // (also, the assembler treated multiple instances of the same section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
872 // as continuations of previous sections so we would need to collect
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
873 // them together anyway.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
874
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
875 for (l = as -> line_head; l; l = l -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
876 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
877 if (l -> csect)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
878 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
879 // we're in a section - need to output some bytes
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
880 if (l -> outputl > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
881 for (i = 0; i < l -> outputl; i++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
882 write_code_obj_sbadd(l -> csect, l -> output[i]);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
883 else if (l -> outputl == 0 || l -> outputl == -1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
884 for (i = 0; i < l -> len; i++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
885 write_code_obj_sbadd(l -> csect, 0);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
886 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
887 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
888
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
889 // run through the sections
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
890 for (s = as -> sections; s; s = s -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
891 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
892 // write the name
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
893 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
894
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
895 // write the flags
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
896 if (s -> flags & section_flag_bss)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
897 writebytes("\x01", 1, 1, of);
156
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
898 if (s -> flags & section_flag_constant)
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
899 writebytes("\x02", 1, 1, of);
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
900
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
901 // indicate end of flags - the "" is NOT an error
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
902 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
903
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
904 // now the local symbols
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
905
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
906 // a symbol for section base address
156
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
907 if ((s -> flags & section_flag_constant) == 0)
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
908 {
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
909 writebytes("\x02", 1, 1, of);
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
910 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
911 // address 0; "\0" is not an error
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
912 writebytes("\0", 2, 1, of);
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
913 }
195
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
914
17bd59f045af Changed symbol table to use a binary tree.
William Astle <lost@l-w.ca>
parents: 156
diff changeset
915 write_code_obj_auxsym(as, of, s, as -> symtab.head);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
916 // flag end of local symbol table - "" is NOT an error
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
917 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
918
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
919 // now the exports -- FIXME
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
920 for (ex = as -> exportlist; ex; ex = ex -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
921 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
922 int eval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
923 lw_expr_t te;
500
733fd05ca2a8 Initialize temporary line structures properly
William Astle <lost@l-w.ca>
parents: 443
diff changeset
924 line_t tl = { 0 };
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
925
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
926 if (ex -> se == NULL)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
927 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
928 if (ex -> se -> section != s)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
929 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
930 te = lw_expr_copy(ex -> se -> value);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
931 as -> csect = ex -> se -> section;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
932 as -> exportcheck = 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
933 tl.as = as;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
934 as -> cl = &tl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
935 lwasm_reduce_expr(as, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
936 as -> exportcheck = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
937 as -> cl = NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
938 if (!lw_expr_istype(te, lw_expr_type_int))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
939 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
940 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
941 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
942 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
943 eval = lw_expr_intval(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
944 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
945 writebytes(ex -> symbol, strlen(ex -> symbol) + 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
946 buf[0] = (eval >> 8) & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
947 buf[1] = eval & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
948 writebytes(buf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
949 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
950
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
951 // flag end of exported symbols - "" is NOT an error
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
952 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
953
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
954 // FIXME - relocation table
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
955 for (re = s -> reloctab; re; re = re -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
956 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
957 int offset;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
958 lw_expr_t te;
500
733fd05ca2a8 Initialize temporary line structures properly
William Astle <lost@l-w.ca>
parents: 443
diff changeset
959 line_t tl = { 0 };
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
960
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
961 tl.as = as;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
962 as -> cl = &tl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
963 as -> csect = s;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
964 as -> exportcheck = 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
965
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
966 if (re -> expr == NULL)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
967 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
968 // this is an error but we'll simply ignore it
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
969 // and not output this expression
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
970 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
971 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
972
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
973 // work through each term in the expression and output
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
974 // the proper equivalent to the object file
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
975 if (re -> size == 1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
976 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
977 // flag an 8 bit relocation (low 8 bits will be used)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
978 buf[0] = 0xFF;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
979 buf[1] = 0x01;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
980 writebytes(buf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
981 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
982
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
983 te = lw_expr_copy(re -> offset);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
984 lwasm_reduce_expr(as, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
985 if (!lw_expr_istype(te, lw_expr_type_int))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
986 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
987 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
988 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
989 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
990 offset = lw_expr_intval(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
991 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
992
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
993 // output expression
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
994 lw_expr_testterms(re -> expr, write_code_obj_expraux, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
995
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
996 // flag end of expressions
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
997 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
998
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
999 // write the offset
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1000 buf[0] = (offset >> 8) & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1001 buf[1] = offset & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1002 writebytes(buf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1003 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1004
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1005 // flag end of incomplete references list
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1006 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1007
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1008 // now blast out the code
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1009
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1010 // length
156
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1011 if (s -> flags & section_flag_constant)
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1012 {
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1013 buf[0] = 0;
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1014 buf[1] = 0;
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1015 }
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1016 else
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1017 {
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1018 buf[0] = s -> oblen >> 8 & 0xff;
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1019 buf[1] = s -> oblen & 0xff;
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1020 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1021 writebytes(buf, 2, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1022
156
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1023
fc8386b13399 Added 'constant' sections to object file handling for lwasm and lwlink
lost@l-w.ca
parents: 127
diff changeset
1024 if (!(s -> flags & section_flag_bss) && !(s -> flags & section_flag_constant))
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1025 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1026 writebytes(s -> obytes, s -> oblen, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1027 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1028 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1029
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1030 // flag no more sections
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1031 // the "" is NOT an error
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1032 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1033 }
432
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1034
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1035
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1036 void write_code_lwmod(asmstate_t *as, FILE *of)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1037 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1038 line_t *l;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1039 sectiontab_t *s;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1040 reloctab_t *re;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1041 int initsize, bsssize, mainsize, callsnum, namesize;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1042 unsigned char *initcode, *maincode, *callscode, *namecode;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1043 int relocsize;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1044 unsigned char *reloccode;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1045 int tsize, bssoff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1046 int initaddr = -1;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1047
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1048 int i;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1049 unsigned char buf[16];
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1050
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1051 // the magic number
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1052 buf[0] = 0x8f;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1053 buf[1] = 0xcf;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1054
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1055 // run through the entire system and build the byte streams for each
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1056 // section; we will make sure we only have simple references for
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1057 // any undefined references. That means at most an ADD (or SUB) operation
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1058 // with a single BSS symbol reference and a single constant value.
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1059 // We will use the constant value in the code stream and record the
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1060 // offset in a separate code stream for the BSS relocation table.
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1061
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1062 // We build everything in memory here because we need to calculate the
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1063 // sizes of everything before we can output the complete header.
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1064
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1065 for (l = as -> line_head; l; l = l -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1066 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1067 if (l -> csect)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1068 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1069 // we're in a section - need to output some bytes
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1070 if (l -> outputl > 0)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1071 for (i = 0; i < l -> outputl; i++)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1072 write_code_obj_sbadd(l -> csect, l -> output[i]);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1073 else if (l -> outputl == 0 || l -> outputl == -1)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1074 for (i = 0; i < l -> len; i++)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1075 write_code_obj_sbadd(l -> csect, 0);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1076 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1077 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1078
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1079 // now run through sections and set various parameters
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1080 initsize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1081 bsssize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1082 mainsize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1083 callsnum = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1084 callscode = NULL;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1085 maincode = NULL;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1086 initcode = NULL;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1087 namecode = NULL;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1088 namesize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1089 relocsize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1090 for (s = as -> sections; s; s = s -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1091 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1092 if (!strcmp(s -> name, "bss"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1093 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1094 bsssize = s -> oblen;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1095 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1096 else if (!strcmp(s -> name, "main"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1097 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1098 maincode = s -> obytes;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1099 mainsize = s -> oblen;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1100 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1101 else if (!strcmp(s -> name, "init"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1102 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1103 initcode = s -> obytes;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1104 initsize = s -> oblen;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1105 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1106 else if (!strcmp(s -> name, "calls"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1107 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1108 callscode = s -> obytes;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1109 callsnum = s -> oblen / 2;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1110 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1111 else if (!strcmp(s -> name, "modname"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1112 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1113 namecode = s -> obytes;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1114 namesize = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1115 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1116 for (re = s -> reloctab; re; re = re -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1117 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1118 if (re -> expr == NULL)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1119 relocsize += 2;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1120 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1121 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1122 if (namesize == 0)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1123 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1124 namecode = (unsigned char *)(as -> output_file);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1125 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1126 else
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1127 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1128 if (namecode[namesize - 1] != '\0')
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1129 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1130 namecode[namesize - 1] = '\0';
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1131 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1132 if (!*namecode)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1133 namecode = (unsigned char *)(as -> output_file);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1134 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1135 namesize = strlen((char *)namecode);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1136
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1137 tsize = namesize + 1 + initsize + mainsize + callsnum * 2 + relocsize + 11;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1138 bssoff = namesize + 1 + mainsize + callsnum * 2 + 11;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1139 // set up section base addresses
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1140 for (s = as -> sections; s; s = s -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1141 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1142 if (!strcmp(s -> name, "main"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1143 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1144 s -> tbase = 11 + namesize + 1 + callsnum * 2;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1145 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1146 else if (!strcmp(s -> name, "init"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1147 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1148 s -> tbase = bssoff + relocsize;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1149 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1150 else if (!strcmp(s -> name, "calls"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1151 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1152 s -> tbase = 11;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1153 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1154 else if (!strcmp(s -> name, "modname"))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1155 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1156 s -> tbase = 11 + callsnum * 2;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1157 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1158 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1159
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1160 // resolve the "init" address
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1161 if (as -> execaddr_expr)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1162 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1163 // need to resolve address with proper section bases
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1164 lwasm_reduce_expr(as, as -> execaddr_expr);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1165 initaddr = lw_expr_intval(as -> execaddr_expr);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1166 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1167 else
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1168 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1169 initaddr = as -> execaddr;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1170 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1171
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1172 // build relocation data
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1173 reloccode = NULL;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1174 if (relocsize)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1175 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1176 unsigned char *tptr;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1177 reloccode = lw_alloc(relocsize);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1178 tptr = reloccode;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1179
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1180 for (s = as -> sections; s; s = s -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1181 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1182 for (re = s -> reloctab; re; re = re -> next)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1183 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1184 lw_expr_t te;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1185 line_t tl;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1186 int offset;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1187
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1188 tl.as = as;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1189 as -> cl = &tl;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1190 as -> csect = s;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1191 // as -> exportcheck = 1;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1192
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1193 if (re -> expr)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1194 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1195 int val;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1196 int x;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1197
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1198 te = lw_expr_copy(re -> expr);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1199 lwasm_reduce_expr(as, te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1200 if (!lw_expr_istype(te, lw_expr_type_int))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1201 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1202 val = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1203 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1204 else
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1205 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1206 val = lw_expr_intval(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1207 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1208 lw_expr_destroy(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1209 x = s -> tbase;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1210 s -> tbase = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1211 te = lw_expr_copy(re -> offset);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1212 lwasm_reduce_expr(as, te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1213 offset = lw_expr_intval(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1214 lw_expr_destroy(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1215 s -> tbase = x;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1216 // offset *should* be the offset in the section
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1217 s -> obytes[offset] = val >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1218 s -> obytes[offset + 1] = val & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1219 continue;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1220 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1221
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1222 offset = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1223 te = lw_expr_copy(re -> offset);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1224 lwasm_reduce_expr(as, te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1225 if (!lw_expr_istype(te, lw_expr_type_int))
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1226 {
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1227 lw_expr_destroy(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1228 offset = 0;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1229 continue;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1230 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1231 offset = lw_expr_intval(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1232 lw_expr_destroy(te);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1233 //offset += sbase;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1234
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1235 *tptr++ = offset >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1236 *tptr++ = offset & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1237 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1238 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1239 }
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1240
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1241 // total size
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1242 buf[2] = tsize >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1243 buf[3] = tsize & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1244 // offset to BSS relocs
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1245 buf[4] = bssoff >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1246 buf[5] = bssoff & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1247 // BSS size
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1248 buf[6] = bsssize >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1249 buf[7] = bsssize & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1250 // init routine offset
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1251 buf[8] = initaddr >> 8;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1252 buf[9] = initaddr & 0xff;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1253 // number of call entries
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1254 buf[10] = callsnum;
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1255 // write the header
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1256 writebytes(buf, 11, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1257 // call data
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1258 if (callsnum)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1259 writebytes(callscode, callsnum * 2, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1260 // module name
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1261 writebytes(namecode, namesize + 1, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1262 // main code
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1263 if (mainsize)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1264 writebytes(maincode, mainsize, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1265 // bss relocs
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1266 if (relocsize)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1267 writebytes(reloccode, relocsize, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1268 // init stuff
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1269 if (initsize)
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1270 writebytes(initcode, initsize, 1, of);
58cafa61ab40 Add support for undocumented custom module format (for LW)
William Astle <lost@l-w.ca>
parents: 425
diff changeset
1271 }
543
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1272
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1273 void write_code_abs_calc(asmstate_t *as, unsigned int *start, unsigned int *length)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1274 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1275 line_t *cl;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1276 unsigned int lowaddr = 65535;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1277 unsigned int highaddr = 0;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1278 char outbyte;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1279 int outaddr, rc;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1280
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1281 // if not specified, calculate
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1282 for (cl = as -> line_head; cl; cl = cl -> next)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1283 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1284 do
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1285 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1286 rc = fetch_output_byte(cl, &outbyte, &outaddr);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1287 if (rc)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1288 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1289 if (outaddr < lowaddr)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1290 lowaddr = outaddr;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1291 if (outaddr > highaddr)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1292 highaddr = outaddr;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1293 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1294 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1295 while (rc);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1296
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1297 *length = (lowaddr > highaddr) ? 0 : 1 + highaddr - lowaddr;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1298 *start = (lowaddr > highaddr ) ? 0 : lowaddr;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1299 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1300 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1301
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1302 void write_code_abs_aux(asmstate_t *as, FILE *of, unsigned int start, unsigned int header_size)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1303 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1304 line_t *cl;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1305
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1306 char outbyte;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1307 int outaddr, rc;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1308
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1309 for (cl = as -> line_head; cl; cl = cl -> next)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1310 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1311 do
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1312 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1313 rc = fetch_output_byte(cl, &outbyte, &outaddr);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1314
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1315 // if first byte to write or output stream jumps address, seek
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1316 if (rc == -1)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1317 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1318 fseek(of,(long int) header_size + outaddr - start, SEEK_SET);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1319 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1320 if (rc) fputc(outbyte,of);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1321 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1322 while (rc);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1323 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1324
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1325 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1326
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1327 /* Write a DragonDOS binary file */
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1328
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1329 void write_code_dragon(asmstate_t *as, FILE *of)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1330 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1331 unsigned char headerbuf[9];
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1332 unsigned int start, length;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1333
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1334 write_code_abs_calc(as, &start, &length);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1335
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1336 headerbuf[0] = 0x55; // magic $55
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1337 headerbuf[1] = 0x02; // binary file
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1338 headerbuf[2] = (start >> 8) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1339 headerbuf[3] = (start) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1340 headerbuf[4] = (length >> 8) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1341 headerbuf[5] = (length) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1342 headerbuf[6] = (as -> execaddr >> 8) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1343 headerbuf[7] = (as -> execaddr) & 0xFF;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1344 headerbuf[8] = 0xAA; // magic $AA
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1345
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1346 writebytes(headerbuf, 9, 1, of);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1347
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1348 write_code_abs_aux(as, of, start, 9);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1349
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1350 }
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1351
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1352 /* Write a monolithic binary block, respecting absolute address segments from ORG directives */
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1353 /* Uses fseek, requires lowest code address and header offset size */
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1354 /* Out of order ORG addresses are handled */
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1355
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1356 void write_code_abs(asmstate_t *as, FILE *of)
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1357 {
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1358 unsigned int start, length;
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1359
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1360 write_code_abs_calc(as, &start, &length);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1361 write_code_abs_aux(as, of, start, 0);
e10618b48e68 Implement support for dragon format binaries
William Astle <lost@l-w.ca>
parents: 536
diff changeset
1362 }