Mercurial > hg-old > index.cgi
annotate src/output.c @ 25:3b818f05dc2a
imported instruction table from older version to development version
author | lost |
---|---|
date | Fri, 02 Jan 2009 02:03:48 +0000 |
parents | 34568fab6058 |
children | b962cee20bf4 |
rev | line source |
---|---|
0 | 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 | 23 |
24 //#include <ctype.h> | |
25 #include <errno.h> | |
26 #include <stdio.h> | |
27 //#include <stdlib.h> | |
28 #include <string.h> | |
29 #include <unistd.h> | |
30 #define __output_c_seen__ | |
31 //#include "instab.h" | |
32 #include "lwasm.h" | |
33 | |
34 void write_code_raw(asmstate_t *as, FILE *of); | |
35 void write_code_decb(asmstate_t *as, FILE *of); | |
36 void write_code_rawrel(asmstate_t *as, FILE *of); | |
37 | |
38 void write_code(asmstate_t *as) | |
39 { | |
40 FILE *of; | |
41 | |
42 if (as -> errorcount > 0) | |
43 { | |
44 fprintf(stderr, "Not doing output due to assembly errors.\n"); | |
45 return; | |
46 } | |
47 | |
48 of = fopen(as -> outfile, "wb"); | |
49 if (!of) | |
50 { | |
51 fprintf(stderr, "Cannot open '%s' for output", as -> outfile); | |
52 perror(""); | |
53 return; | |
54 } | |
55 | |
56 switch (as -> outformat) | |
57 { | |
58 case OUTPUT_RAW: | |
59 write_code_raw(as, of); | |
60 break; | |
61 | |
62 case OUTPUT_DECB: | |
63 write_code_decb(as, of); | |
64 break; | |
65 | |
66 case OUTPUT_RAWREL: | |
67 write_code_rawrel(as, of); | |
68 break; | |
69 | |
70 default: | |
71 fprintf(stderr, "BUG: unrecognized output format when generating output file\n"); | |
72 fclose(of); | |
73 unlink(as -> outfile); | |
74 return; | |
75 } | |
76 | |
77 fclose(of); | |
78 } | |
79 | |
80 /* | |
81 rawrel output treats an ORG directive as an offset from the start of the | |
82 file. Undefined results will occur if an ORG directive moves the output | |
83 pointer backward. This particular implementation uses "fseek" to handle | |
84 ORG requests and to skip over RMBs. | |
85 | |
86 This simple brain damanged method simply does an fseek before outputting | |
87 each instruction. | |
88 */ | |
89 void write_code_rawrel(asmstate_t *as, FILE *of) | |
90 { | |
91 sourceline_t *cl; | |
92 | |
93 for (cl = as -> source_head; cl; cl = cl -> next) | |
94 { | |
95 if (cl -> nocode) | |
96 continue; | |
97 | |
98 fseek(of, cl -> addr, SEEK_SET); | |
99 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of); | |
100 } | |
101 } | |
102 | |
103 /* | |
104 raw merely writes all the bytes directly to the file as is. ORG is just a | |
105 reference for the assembler to handle absolute references. Multiple ORG | |
106 statements will produce mostly useless results | |
107 */ | |
108 void write_code_raw(asmstate_t *as, FILE *of) | |
109 { | |
110 sourceline_t *cl; | |
111 | |
112 for (cl = as -> source_head; cl; cl = cl -> next) | |
113 { | |
114 if (cl -> nocode && cl -> len > 0) | |
115 { | |
116 int i; | |
117 for (i = 0; i < cl -> len; i++) | |
118 fwrite("\0", 1, 1, of); | |
119 continue; | |
120 } | |
121 if (cl -> nocode) | |
122 continue; | |
123 | |
124 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of); | |
125 } | |
126 } | |
127 | |
128 void write_code_decb(asmstate_t *as, FILE *of) | |
129 { | |
130 long preambloc; | |
131 sourceline_t *cl; | |
132 int blocklen = -1; | |
133 int nextcalc = -1; | |
134 unsigned char outbuf[5]; | |
135 | |
136 for (cl = as -> source_head; cl; cl = cl -> next) | |
137 { | |
138 if (cl -> nocode) | |
139 continue; | |
140 if (cl -> addr != nextcalc && cl -> numcodebytes > 0) | |
141 { | |
142 // need preamble here | |
143 if (blocklen > 0) | |
144 { | |
145 fseek(of, preambloc, SEEK_SET); | |
146 outbuf[0] = (blocklen >> 8) & 0xFF; | |
147 outbuf[1] = blocklen & 0xFF; | |
148 fwrite(outbuf, 2, 1, of); | |
149 fseek(of, 0, SEEK_END); | |
150 } | |
151 blocklen = 0; | |
152 nextcalc = cl -> addr; | |
153 outbuf[0] = 0x00; | |
154 outbuf[1] = 0x00; | |
155 outbuf[2] = 0x00; | |
156 outbuf[3] = (nextcalc >> 8) & 0xFF; | |
157 outbuf[4] = nextcalc & 0xFF; | |
158 preambloc = ftell(of) + 1; | |
159 fwrite(outbuf, 5, 1, of); | |
160 } | |
161 nextcalc += cl -> numcodebytes; | |
162 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of); | |
163 blocklen += cl -> numcodebytes; | |
164 } | |
165 if (blocklen > 0) | |
166 { | |
167 fseek(of, preambloc, SEEK_SET); | |
168 outbuf[0] = (blocklen >> 8) & 0xFF; | |
169 outbuf[1] = blocklen & 0xFF; | |
170 fwrite(outbuf, 2, 1, of); | |
171 fseek(of, 0, SEEK_END); | |
172 } | |
173 | |
174 // now write postamble | |
175 outbuf[0] = 0xFF; | |
176 outbuf[1] = 0x00; | |
177 outbuf[2] = 0x00; | |
178 outbuf[3] = (as -> execaddr >> 8) & 0xFF; | |
179 outbuf[4] = (as -> execaddr) & 0xFF; | |
180 fwrite(outbuf, 5, 1, of); | |
181 } |