Mercurial > hg-old > index.cgi
annotate src/output.c @ 46:b962cee20bf4
Ported output modules forward from old version
author | lost |
---|---|
date | Sun, 04 Jan 2009 07:07:00 +0000 |
parents | 34568fab6058 |
children | 6de358e7903f |
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 |
46 | 3 Copyright © 2009 William Astle |
4
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 | |
46 | 38 void lwasm_output(asmstate_t *as) |
0 | 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 { | |
46 | 91 lwasm_line_t *cl; |
0 | 92 |
46 | 93 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 94 { |
46 | 95 if (cl -> codelen == 0) |
0 | 96 continue; |
97 | |
46 | 98 fseek(of, cl -> codeaddr, SEEK_SET); |
99 fwrite(cl -> bytes, cl -> codelen, 1, of); | |
0 | 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 { | |
46 | 110 lwasm_line_t *cl; |
0 | 111 |
46 | 112 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 113 { |
46 | 114 if (cl -> nocodelen) |
0 | 115 { |
116 int i; | |
46 | 117 for (i = 0; i < cl -> nocodelen; i++) |
0 | 118 fwrite("\0", 1, 1, of); |
119 continue; | |
120 } | |
46 | 121 fwrite(cl -> bytes, cl -> codelen, 1, of); |
0 | 122 } |
123 } | |
124 | |
125 void write_code_decb(asmstate_t *as, FILE *of) | |
126 { | |
127 long preambloc; | |
46 | 128 lwasm_line_t *cl; |
0 | 129 int blocklen = -1; |
130 int nextcalc = -1; | |
131 unsigned char outbuf[5]; | |
132 | |
46 | 133 for (cl = as -> lineshead; cl; cl = cl -> next) |
0 | 134 { |
46 | 135 if (cl -> nocodelen) |
0 | 136 continue; |
46 | 137 if (cl -> codeaddr != nextcalc && cl -> codelen > 0) |
0 | 138 { |
139 // need preamble here | |
140 if (blocklen > 0) | |
141 { | |
46 | 142 // update previous preamble if needed |
0 | 143 fseek(of, preambloc, SEEK_SET); |
144 outbuf[0] = (blocklen >> 8) & 0xFF; | |
145 outbuf[1] = blocklen & 0xFF; | |
146 fwrite(outbuf, 2, 1, of); | |
147 fseek(of, 0, SEEK_END); | |
148 } | |
149 blocklen = 0; | |
46 | 150 nextcalc = cl -> codeaddr; |
0 | 151 outbuf[0] = 0x00; |
152 outbuf[1] = 0x00; | |
153 outbuf[2] = 0x00; | |
154 outbuf[3] = (nextcalc >> 8) & 0xFF; | |
155 outbuf[4] = nextcalc & 0xFF; | |
156 preambloc = ftell(of) + 1; | |
157 fwrite(outbuf, 5, 1, of); | |
158 } | |
46 | 159 nextcalc += cl -> codelen; |
160 fwrite(cl -> bytes, cl -> codelen, 1, of); | |
161 blocklen += cl -> codelen; | |
0 | 162 } |
163 if (blocklen > 0) | |
164 { | |
165 fseek(of, preambloc, SEEK_SET); | |
166 outbuf[0] = (blocklen >> 8) & 0xFF; | |
167 outbuf[1] = blocklen & 0xFF; | |
168 fwrite(outbuf, 2, 1, of); | |
169 fseek(of, 0, SEEK_END); | |
170 } | |
171 | |
172 // now write postamble | |
173 outbuf[0] = 0xFF; | |
174 outbuf[1] = 0x00; | |
175 outbuf[2] = 0x00; | |
176 outbuf[3] = (as -> execaddr >> 8) & 0xFF; | |
177 outbuf[4] = (as -> execaddr) & 0xFF; | |
178 fwrite(outbuf, 5, 1, of); | |
179 } |