forked from nop00/pceas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
396 lines (331 loc) · 6.51 KB
/
output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <string.h>
#include "defs.h"
#include "externs.h"
#include "protos.h"
/* ----
* println()
* ----
* prints the contents of prlnbuf
*/
void
println(void)
{
int nb, cnt;
int i;
/* check if output possible */
if (list_level == 0)
return;
if (!xlist || !asm_opt[OPT_LIST] || (expand_macro && !asm_opt[OPT_MACRO]))
return;
/* update line buffer if necessary */
if (continued_line)
strcpy(prlnbuf, tmplnbuf);
/* output */
if (data_loccnt == -1)
/* line buffer */
fprintf(lst_fp, "%s\n", prlnbuf);
else {
/* line buffer + data bytes */
loadlc(data_loccnt, 0);
/* number of bytes */
nb = loccnt - data_loccnt;
/* check level */
if ((data_level > list_level) && (nb > 3))
/* doesn't match */
fprintf(lst_fp, "%s\n", prlnbuf);
else {
/* ok */
cnt = 0;
for (i = 0; i < nb; i++) {
if (bank >= RESERVED_BANK) {
prlnbuf[16 + (3*cnt)] = '-';
prlnbuf[17 + (3*cnt)] = '-';
}
else {
hexcon(2, rom[bank][data_loccnt], &prlnbuf[16 + (3*cnt)]);
}
data_loccnt++;
cnt++;
if (cnt == data_size) {
cnt = 0;
fprintf(lst_fp, "%s\n", prlnbuf);
clearln();
loadlc(data_loccnt, 0);
}
}
if (cnt)
fprintf(lst_fp, "%s\n", prlnbuf);
}
}
}
/* ----
* clearln()
* ----
* clear prlnbuf
*/
void
clearln(void)
{
memset(prlnbuf, ' ', SFIELD);
prlnbuf[SFIELD+1] = 0;
}
/* ----
* loadlc()
* ----
* load 16 bit value in printable form into prlnbuf
*/
void
loadlc(int offset, int pos)
{
int i;
if (pos)
i = 16;
else
i = 7;
if (pos == 0) {
if (bank >= RESERVED_BANK) {
prlnbuf[i++] = '-';
prlnbuf[i++] = '-';
}
else {
hexcon(2, bank, &prlnbuf[i]);
}
prlnbuf[i++] = ':';
offset += page << 13;
}
hexcon(4, offset, &prlnbuf[i]);
}
/* ----
* hexcon()
* ----
* convert number supplied as argument to hexadecimal in out
*/
void
hexcon(int digit, int num, char *out)
{
int i;
for (i=1; i<=digit; i++) {
int nibble = num & 0x0f;
out[digit-i] = nibble + ((nibble <= 9) ? '0' : 'A');
num >>= 4;
}
}
/* ----
* putbyte()
* ----
* store a byte in the rom
*/
void
putbyte(int offset, int data)
{
if (bank >= RESERVED_BANK)
return;
if (offset < 0x2000) {
rom[bank][offset] = (data) & 0xFF;
map[bank][offset] = section + (page << 5);
/* update rom size */
if (bank > max_bank)
max_bank = bank;
}
}
/* ----
* putword()
* ----
* store a word in the rom
*/
void
putword(int offset, int data)
{
if (bank >= RESERVED_BANK)
return;
if (offset < 0x1FFF) {
/* low byte */
rom[bank][offset] = (data) & 0xFF;
map[bank][offset] = section + (page << 5);
/* high byte */
rom[bank][offset+1] = (data >> 8) & 0xFF;
map[bank][offset+1] = section + (page << 5);
/* update rom size */
if (bank > max_bank)
max_bank = bank;
}
}
/* ----
* putbuffer()
* ----
* copy a buffer at the current location
*/
void
putbuffer(void *data, int size)
{
int addr;
/* check size */
if (size == 0)
return;
/* check if the buffer will fit in the rom */
if (bank >= RESERVED_BANK) {
addr = loccnt + size;
if (addr > 0x1FFF) {
fatal_error("PROC overflow!");
return;
}
}
else {
addr = loccnt + size + (bank << 13);
if (addr > rom_limit) {
fatal_error("ROM overflow!");
return;
}
/* copy the buffer */
if (pass == LAST_PASS) {
if (data) {
memcpy(&rom[bank][loccnt], data, size);
memset(&map[bank][loccnt], section + (page << 5), size);
}
else {
memset(&rom[bank][loccnt], 0, size);
memset(&map[bank][loccnt], section + (page << 5), size);
}
}
}
/* update the location counter */
bank += (loccnt + size) >> 13;
loccnt = (loccnt + size) & 0x1FFF;
/* update rom size */
if (bank < RESERVED_BANK) {
if (bank > max_bank) {
if (loccnt)
max_bank = bank;
else
max_bank = bank - 1;
}
}
}
/* ----
* write_srec()
* ----
*/
void
write_srec(char *file, char *ext, int base)
{
unsigned char data, chksum;
char fname[128];
int addr, dump, cnt, pos, i, j;
FILE *fp;
/* status message */
if (!strcmp(ext, "mx"))
printf("writing mx file... ");
else
printf("writing s-record file... ");
/* flush output */
fflush(stdout);
/* add the file extension */
strcpy(fname, file);
strcat(fname, ".");
strcat(fname, ext);
/* open the file */
if ((fp = fopen(fname, "w")) == NULL) {
printf("can not open file '%s'!\n", fname);
return;
}
/* dump the rom */
dump = 0;
cnt = 0;
pos = 0;
for (i = 0; i <= max_bank; i++) {
for (j = 0; j < 8192; j++) {
if (map[i][j] != 0xFF) {
/* data byte */
if (cnt == 0)
pos = j;
cnt++;
if (cnt == 32)
dump = 1;
}
else {
/* free byte */
if (cnt)
dump = 1;
}
if (j == 8191)
if (cnt)
dump = 1;
/* dump */
if (dump) {
dump = 0;
addr = base + (i << 13) + pos;
chksum = cnt + ((addr >> 16) & 0xFF) +
((addr >> 8) & 0xFF) +
((addr) & 0xFF) +
4;
/* number, address */
fprintf(fp, "S2%02X%06X", cnt + 4, addr);
/* code */
while (cnt) {
data = rom[i][pos++];
chksum += data;
fprintf(fp, "%02X", data);
cnt--;
}
/* chksum */
fprintf(fp, "%02X\n", (~chksum) & 0xFF);
}
}
}
/* starting address */
addr = ((map[0][0] >> 5) << 13);
chksum = ((addr >> 8) & 0xFF) + (addr & 0xFF) + 4;
fprintf(fp, "S804%06X%02X", addr, (~chksum) & 0xFF);
/* ok */
fclose(fp);
printf("OK\n");
}
/* ----
* fatal_error()
* ----
* stop compilation
*/
void
fatal_error(char *stptr)
{
error(stptr);
stop_pass = 1;
}
/* ----
* error()
* ----
* error printing routine
*/
void
error(char *stptr)
{
warning(stptr);
errcnt++;
}
/* ----
* warning()
* ----
* warning printing routine
*/
void
warning(char *stptr)
{
int i, temp;
/* put the source line number into prlnbuf */
i = 4;
temp = slnum;
while (temp != 0) {
prlnbuf[i--] = temp % 10 + '0';
temp /= 10;
}
/* update the current file name */
if (infile_error != infile_num) {
infile_error = infile_num;
printf("#[%i] %s\n", infile_num, input_file[infile_num].name);
}
/* output the line and the error message */
loadlc(loccnt, 0);
printf("%s\n", prlnbuf);
printf(" %s\n", stptr);
}