comparison 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
comparison
equal deleted inserted replaced
45:be459d69f481 46:b962cee20bf4
1 /* 1 /*
2 output.c 2 output.c
3 Copyright © 2008 William Astle 3 Copyright © 2009 William Astle
4 4
5 This file is part of LWASM. 5 This file is part of LWASM.
6 6
7 LWASM is free software: you can redistribute it and/or modify it under the 7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software 8 terms of the GNU General Public License as published by the Free Software
33 33
34 void write_code_raw(asmstate_t *as, FILE *of); 34 void write_code_raw(asmstate_t *as, FILE *of);
35 void write_code_decb(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); 36 void write_code_rawrel(asmstate_t *as, FILE *of);
37 37
38 void write_code(asmstate_t *as) 38 void lwasm_output(asmstate_t *as)
39 { 39 {
40 FILE *of; 40 FILE *of;
41 41
42 if (as -> errorcount > 0) 42 if (as -> errorcount > 0)
43 { 43 {
86 This simple brain damanged method simply does an fseek before outputting 86 This simple brain damanged method simply does an fseek before outputting
87 each instruction. 87 each instruction.
88 */ 88 */
89 void write_code_rawrel(asmstate_t *as, FILE *of) 89 void write_code_rawrel(asmstate_t *as, FILE *of)
90 { 90 {
91 sourceline_t *cl; 91 lwasm_line_t *cl;
92 92
93 for (cl = as -> source_head; cl; cl = cl -> next) 93 for (cl = as -> lineshead; cl; cl = cl -> next)
94 { 94 {
95 if (cl -> nocode) 95 if (cl -> codelen == 0)
96 continue; 96 continue;
97 97
98 fseek(of, cl -> addr, SEEK_SET); 98 fseek(of, cl -> codeaddr, SEEK_SET);
99 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of); 99 fwrite(cl -> bytes, cl -> codelen, 1, of);
100 } 100 }
101 } 101 }
102 102
103 /* 103 /*
104 raw merely writes all the bytes directly to the file as is. ORG is just a 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 105 reference for the assembler to handle absolute references. Multiple ORG
106 statements will produce mostly useless results 106 statements will produce mostly useless results
107 */ 107 */
108 void write_code_raw(asmstate_t *as, FILE *of) 108 void write_code_raw(asmstate_t *as, FILE *of)
109 { 109 {
110 sourceline_t *cl; 110 lwasm_line_t *cl;
111 111
112 for (cl = as -> source_head; cl; cl = cl -> next) 112 for (cl = as -> lineshead; cl; cl = cl -> next)
113 { 113 {
114 if (cl -> nocode && cl -> len > 0) 114 if (cl -> nocodelen)
115 { 115 {
116 int i; 116 int i;
117 for (i = 0; i < cl -> len; i++) 117 for (i = 0; i < cl -> nocodelen; i++)
118 fwrite("\0", 1, 1, of); 118 fwrite("\0", 1, 1, of);
119 continue; 119 continue;
120 } 120 }
121 if (cl -> nocode) 121 fwrite(cl -> bytes, cl -> codelen, 1, of);
122 continue;
123
124 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of);
125 } 122 }
126 } 123 }
127 124
128 void write_code_decb(asmstate_t *as, FILE *of) 125 void write_code_decb(asmstate_t *as, FILE *of)
129 { 126 {
130 long preambloc; 127 long preambloc;
131 sourceline_t *cl; 128 lwasm_line_t *cl;
132 int blocklen = -1; 129 int blocklen = -1;
133 int nextcalc = -1; 130 int nextcalc = -1;
134 unsigned char outbuf[5]; 131 unsigned char outbuf[5];
135 132
136 for (cl = as -> source_head; cl; cl = cl -> next) 133 for (cl = as -> lineshead; cl; cl = cl -> next)
137 { 134 {
138 if (cl -> nocode) 135 if (cl -> nocodelen)
139 continue; 136 continue;
140 if (cl -> addr != nextcalc && cl -> numcodebytes > 0) 137 if (cl -> codeaddr != nextcalc && cl -> codelen > 0)
141 { 138 {
142 // need preamble here 139 // need preamble here
143 if (blocklen > 0) 140 if (blocklen > 0)
144 { 141 {
142 // update previous preamble if needed
145 fseek(of, preambloc, SEEK_SET); 143 fseek(of, preambloc, SEEK_SET);
146 outbuf[0] = (blocklen >> 8) & 0xFF; 144 outbuf[0] = (blocklen >> 8) & 0xFF;
147 outbuf[1] = blocklen & 0xFF; 145 outbuf[1] = blocklen & 0xFF;
148 fwrite(outbuf, 2, 1, of); 146 fwrite(outbuf, 2, 1, of);
149 fseek(of, 0, SEEK_END); 147 fseek(of, 0, SEEK_END);
150 } 148 }
151 blocklen = 0; 149 blocklen = 0;
152 nextcalc = cl -> addr; 150 nextcalc = cl -> codeaddr;
153 outbuf[0] = 0x00; 151 outbuf[0] = 0x00;
154 outbuf[1] = 0x00; 152 outbuf[1] = 0x00;
155 outbuf[2] = 0x00; 153 outbuf[2] = 0x00;
156 outbuf[3] = (nextcalc >> 8) & 0xFF; 154 outbuf[3] = (nextcalc >> 8) & 0xFF;
157 outbuf[4] = nextcalc & 0xFF; 155 outbuf[4] = nextcalc & 0xFF;
158 preambloc = ftell(of) + 1; 156 preambloc = ftell(of) + 1;
159 fwrite(outbuf, 5, 1, of); 157 fwrite(outbuf, 5, 1, of);
160 } 158 }
161 nextcalc += cl -> numcodebytes; 159 nextcalc += cl -> codelen;
162 fwrite(cl -> codebytes, cl -> numcodebytes, 1, of); 160 fwrite(cl -> bytes, cl -> codelen, 1, of);
163 blocklen += cl -> numcodebytes; 161 blocklen += cl -> codelen;
164 } 162 }
165 if (blocklen > 0) 163 if (blocklen > 0)
166 { 164 {
167 fseek(of, preambloc, SEEK_SET); 165 fseek(of, preambloc, SEEK_SET);
168 outbuf[0] = (blocklen >> 8) & 0xFF; 166 outbuf[0] = (blocklen >> 8) & 0xFF;