annotate src/output.c @ 19:925105ccf22f

small reworking of command arguments
author lost
date Fri, 02 Jan 2009 00:41:58 +0000
parents 34568fab6058
children b962cee20bf4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
1 /*
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
2 output.c
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
3 Copyright © 2008 William Astle
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
4
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
5 This file is part of LWASM.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
6
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
8 terms of the GNU General Public License as published by the Free Software
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
10 version.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
11
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
15 more details.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
16
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
17 You should have received a copy of the GNU General Public License along with
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
19
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
20
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
21 Contains the code for actually outputting the assembled code
34568fab6058 Fixed package to include all required files; also added copyright preamble to all source files
lost
parents: 0
diff changeset
22 */
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 //#include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27 //#include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 #include <string.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 #include <unistd.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 #define __output_c_seen__
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 //#include "instab.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 void write_code_raw(asmstate_t *as, FILE *of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35 void write_code_decb(asmstate_t *as, FILE *of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36 void write_code_rawrel(asmstate_t *as, FILE *of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
38 void write_code(asmstate_t *as)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40 FILE *of;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
41
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42 if (as -> errorcount > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
43 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44 fprintf(stderr, "Not doing output due to assembly errors.\n");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48 of = fopen(as -> outfile, "wb");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 if (!of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 fprintf(stderr, "Cannot open '%s' for output", as -> outfile);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 perror("");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 switch (as -> outformat)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 case OUTPUT_RAW:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59 write_code_raw(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 case OUTPUT_DECB:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 write_code_decb(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 case OUTPUT_RAWREL:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 write_code_rawrel(as, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 default:
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71 fprintf(stderr, "BUG: unrecognized output format when generating output file\n");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 fclose(of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 unlink(as -> outfile);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
75 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
76
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 fclose(of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
80 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
81 rawrel output treats an ORG directive as an offset from the start of the
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
82 file. Undefined results will occur if an ORG directive moves the output
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
83 pointer backward. This particular implementation uses "fseek" to handle
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84 ORG requests and to skip over RMBs.
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 This simple brain damanged method simply does an fseek before outputting
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 each instruction.
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 void write_code_rawrel(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91 sourceline_t *cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 for (cl = as -> source_head; cl; cl = cl -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 if (cl -> nocode)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98 fseek(of, cl -> addr, SEEK_SET);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104 raw merely writes all the bytes directly to the file as is. ORG is just a
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
105 reference for the assembler to handle absolute references. Multiple ORG
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106 statements will produce mostly useless results
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
107 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108 void write_code_raw(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
109 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 sourceline_t *cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
112 for (cl = as -> source_head; cl; cl = cl -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
113 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
114 if (cl -> nocode && cl -> len > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
115 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
116 int i;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
117 for (i = 0; i < cl -> len; i++)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
118 fwrite("\0", 1, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
119 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
120 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
121 if (cl -> nocode)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
122 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
123
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
124 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
125 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
126 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
127
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
128 void write_code_decb(asmstate_t *as, FILE *of)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
129 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
130 long preambloc;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
131 sourceline_t *cl;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
132 int blocklen = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
133 int nextcalc = -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
134 unsigned char outbuf[5];
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
135
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
136 for (cl = as -> source_head; cl; cl = cl -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
137 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
138 if (cl -> nocode)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
139 continue;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
140 if (cl -> addr != nextcalc && cl -> numcodebytes > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
141 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
142 // need preamble here
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
143 if (blocklen > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
144 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
145 fseek(of, preambloc, SEEK_SET);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
146 outbuf[0] = (blocklen >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
147 outbuf[1] = blocklen & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
148 fwrite(outbuf, 2, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
149 fseek(of, 0, SEEK_END);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
150 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
151 blocklen = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
152 nextcalc = cl -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
153 outbuf[0] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
154 outbuf[1] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
155 outbuf[2] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
156 outbuf[3] = (nextcalc >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
157 outbuf[4] = nextcalc & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
158 preambloc = ftell(of) + 1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
159 fwrite(outbuf, 5, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
160 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
161 nextcalc += cl -> numcodebytes;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
162 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
163 blocklen += cl -> numcodebytes;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
164 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
165 if (blocklen > 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
166 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
167 fseek(of, preambloc, SEEK_SET);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
168 outbuf[0] = (blocklen >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
169 outbuf[1] = blocklen & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
170 fwrite(outbuf, 2, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
171 fseek(of, 0, SEEK_END);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
172 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
173
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
174 // now write postamble
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
175 outbuf[0] = 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
176 outbuf[1] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
177 outbuf[2] = 0x00;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
178 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
179 outbuf[4] = (as -> execaddr) & 0xFF;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
180 fwrite(outbuf, 5, 1, of);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
181 }