-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfrsdos.c
515 lines (431 loc) · 14.6 KB
/
frsdos.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/****************************************************************************
*
* Code to support the Coco BASIC format
* Copyright (C) 2023 Sean Conner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: [email protected]
*
****************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "a09.h"
struct format_rsdos
{
char *basicf;
char *name;
long section_hdr;
long section_start;
uint16_t entry;
uint16_t line;
uint16_t strspace;
uint16_t staddr;
uint16_t usr;
uint16_t defusr[10];
bool endf;
bool org;
};
/**************************************************************************/
char const format_rsdos_usage[] =
"\n"
"RSDOS format options:\n"
"\t-B filename\tfilename for BASIC code output\n"
"\t-L line\t\tstarting line # (default 10)\n"
"\t-N filename\tfilename for RSDOS\n"
"\t-P size\t\tsize of string pool (default 200)\n";
/**************************************************************************/
static bool update_section_size(struct format_rsdos *format,struct opcdata *opd)
{
unsigned char datasize[2];
long pos;
long size;
assert(format != NULL);
assert(opd != NULL);
pos = ftell(opd->a09->out);
if (pos == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
if (pos < format->section_start)
return message(opd->a09,MSG_ERROR,"E0054: Internal error---no header written?");
size = pos - format->section_start;
if (size == 0)
{
if (fseek(opd->a09->out,format->section_hdr,SEEK_SET) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
return true;
}
if (size > UINT16_MAX)
return message(opd->a09,MSG_ERROR,"E0055: object size too large");
if (fseek(opd->a09->out,format->section_hdr + 1,SEEK_SET) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
datasize[0] = size >> 8;
datasize[1] = size & 255;
if (fwrite(datasize,1,2,opd->a09->out) != 2)
return message(opd->a09,MSG_ERROR,"E0040: failed writing object file");
if (fseek(opd->a09->out,pos,SEEK_SET) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
return true;
}
/**************************************************************************/
static bool block_zero_write(
struct format_rsdos *format,
struct opcdata *opd,
uint16_t bsize
)
{
assert(format != NULL);
assert(opd != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
if (opd->pass == 2)
{
if (!format->org)
return message(opd->a09,MSG_ERROR,"E0057: ORG directive missing\n");
/*----------------------------------------------------------------------
; If the alignment is less than the RSDOS program header size, then just
; pad out the current code segment with 0. Otherwise, end the current
; code segment and start a new one, to save space in the executable.
;-----------------------------------------------------------------------*/
if (bsize < 6)
{
if (fseek(opd->a09->out,bsize,SEEK_CUR) == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
}
else
{
unsigned char hdr[5];
long pos;
uint16_t addr;
if (!update_section_size(format,opd))
return false;
pos = ftell(opd->a09->out);
addr = opd->a09->pc + bsize;
hdr[0] = 0;
hdr[1] = 0;
hdr[2] = 0;
hdr[3] = addr >> 8;
hdr[4] = addr & 255;
if (fwrite(hdr,1,sizeof(hdr),opd->a09->out) != sizeof(hdr))
return message(opd->a09,MSG_ERROR,"E0040: failed writing object file");
format->section_hdr = pos;
format->section_start = ftell(opd->a09->out);
}
}
return true;
}
/**************************************************************************/
static bool frsdos_align(struct format *fmt,struct opcdata *opd)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(opd != NULL);
return block_zero_write(fmt->data,opd,opd->datasz);
}
/**************************************************************************/
static bool frsdos_end(struct format *fmt,struct opcdata *opd,struct symbol const *sym)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(opd != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
if (opd->pass == 2)
{
struct format_rsdos *format = fmt->data;
unsigned char hdr[5];
if (!format->org)
return message(opd->a09,MSG_ERROR,"E0057: ORG directive missing\n");
if (format->endf)
return message(opd->a09,MSG_ERROR,"E0056: END section already written");
if (!update_section_size(format,opd))
return false;
hdr[0] = 0xFF;
hdr[1] = 0;
hdr[2] = 0;
if (sym == NULL)
{
hdr[3] = 0;
hdr[4] = 0;
}
else
{
hdr[3] = sym->value >> 8;
hdr[4] = sym->value & 255;
}
if (fwrite(hdr,1,sizeof(hdr),opd->a09->out) != sizeof(hdr))
return message(opd->a09,MSG_ERROR,"E0040: failed writing object file");
format->endf = true;
if (format->name != NULL)
{
bool defusr = false;
char buffer[249];
char rsdfn[16];
size_t idx;
FILE *basic;
if (format->basicf == NULL)
{
char const *p = strrchr(opd->a09->outfile,'/');
if (p == NULL)
p = opd->a09->outfile;
format->basicf = rsdfn;
for ( idx = 0 ; (p[idx] != '\0') && (idx < sizeof(rsdfn)-1) ; idx++)
{
if (p[idx] == '.')
format->basicf[idx] = '/';
else
format->basicf[idx] = p[idx];
}
format->basicf[idx] = '\0';
}
for (idx = 0 ; format->basicf[idx] != '\0'; idx++)
format->basicf[idx] = toupper(format->basicf[idx]);
idx = snprintf(
buffer,
sizeof(buffer),
"%u CLEAR%u,%u:LOADM\"%s\"",
format->line,
format->strspace,
format->entry - 1,
format->basicf
);
assert(idx < sizeof(buffer));
if (format->usr != 0)
{
idx += snprintf(&buffer[idx],sizeof(buffer) - idx,":POKE275,%u:POKE276,%u",format->usr >> 8,format->usr & 255);
assert(idx < sizeof(buffer));
}
for (size_t i = 0 ; i < 10 ; i++)
{
if (format->defusr[i] != 0)
{
idx += snprintf(&buffer[idx],sizeof(buffer) - idx,":DEFUSR%zu=%u",i,format->defusr[i]);
assert(idx < sizeof(buffer));
defusr = true;
}
}
if (defusr && (format->usr != 0))
return message(opd->a09,MSG_ERROR,"E0072: can't use USR and DEFUSRn at the same time");
basic = fopen(format->name,"w");
if (basic == NULL)
return message(opd->a09,MSG_ERROR,"E0070: %s: %s",format->basicf,strerror(errno));
fwrite(buffer,1,idx,basic);
fputc('\n',basic);
fclose(basic);
}
}
return true;
}
/**************************************************************************/
static bool frsdos_org(struct format *fmt,struct opcdata *opd)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(opd != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
if (opd->pass == 2)
{
struct format_rsdos *format = fmt->data;
long pos;
unsigned char hdr[5];
format->org = true;
if (opd->value.value < format->entry)
format->entry = opd->value.value;
if (!update_section_size(format,opd))
return false;
pos = ftell(opd->a09->out);
if (pos == -1)
return message(opd->a09,MSG_ERROR,"E0038: %s",strerror(errno));
hdr[0] = 0;
hdr[1] = 0;
hdr[2] = 0;
hdr[3] = opd->value.value >> 8;
hdr[4] = opd->value.value & 255;
if (fwrite(hdr,1,sizeof(hdr),opd->a09->out) != sizeof(hdr))
return message(opd->a09,MSG_ERROR,"E0040: failed writing object file");
format->section_hdr = pos;
format->section_start = ftell(opd->a09->out);
}
opd->a09->pc = opd->value.value;
return true;
}
/**************************************************************************/
static bool frsdos_rmb(struct format *fmt,struct opcdata *opd)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(opd != NULL);
if (opd->value.value == 0)
return message(opd->a09,MSG_ERROR,"E0099: Can't reserve 0 bytes of memory");
return block_zero_write(fmt->data,opd,opd->value.value);
}
/**************************************************************************/
static bool frsdos_write(struct format *fmt,struct opcdata *opd,void const *buffer,size_t len,bool instruction)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(opd != NULL);
assert(opd->pass == 2);
assert(buffer != NULL);
struct format_rsdos *format = fmt->data;
if (!format->org)
return message(opd->a09,MSG_ERROR,"E0057: ORG directive missing\n");
return fdefault_write(fmt,opd,buffer,len,instruction);
}
/**************************************************************************/
static bool frsdos_cmdline(struct format *fmt,struct a09 *a09,struct arg *arg,char c)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(fmt->backend == BACKEND_RSDOS);
assert(a09 != NULL);
assert(arg != NULL);
assert(c != '\0');
struct format_rsdos *format = fmt->data;
switch(c)
{
case 'B':
if ((format->name = arg_arg(arg)) == NULL)
return message(a09,MSG_ERROR,"E0068: missing option argument");
break;
case 'L':
if (!arg_uint16_t(&format->line,arg,0,63999u))
return message(a09,MSG_ERROR,"E0102: line number must be between 1 and 63999");
break;
case 'N':
if ((format->basicf = arg_arg(arg)) == NULL)
return message(a09,MSG_ERROR,"E0068: missing option argument");
break;
case 'P':
if (!arg_uint16_t(&format->strspace,arg,0,22*1024))
return message(a09,MSG_ERROR,"E0103: string space must be between 0 and 22528");
break;
default:
return false;
}
return true;
}
/**************************************************************************/
static bool frsdos__opt(struct format *fmt,struct opcdata *opd,label *be)
{
assert(fmt != NULL);
assert(fmt->data != NULL);
assert(opd != NULL);
assert(opd->a09 != NULL);
assert(be != NULL);
assert((opd->pass == 1) || (opd->pass == 2));
struct format_rsdos *format = fmt->data;
struct value val;
if ((be->s != 5) && (memcmp(be->text,"BASIC",5) != 0))
return true;
if (opd->pass == 2)
{
label tmp;
char c = skip_space(opd->buffer);
read_label(opd->buffer,&tmp,c);
upper_label(&tmp);
if ((tmp.s == 3) && (memcmp(tmp.text,"USR",3) == 0))
{
if (!expr(&val,opd->a09,opd->buffer,opd->pass))
return false;
format->usr = val.value;
}
else if ((tmp.s == 7) && (memcmp(tmp.text,"DEFUSR",6) == 0))
{
if (!isdigit(tmp.text[6]))
return message(opd->a09,MSG_ERROR,"E0087: option '%.*s' not supported",tmp.s,tmp.text);
if (!expr(&val,opd->a09,opd->buffer,opd->pass))
return false;
format->defusr[(size_t)(tmp.text[6] - '0')] = val.value;
}
else if ((tmp.s == 8) && (memcmp(tmp.text,"STRSPACE",8) == 0))
{
if (!expr(&val,opd->a09,opd->buffer,opd->pass))
return false;
format->strspace = val.value;
}
else if ((tmp.s == 4) && (memcmp(tmp.text,"LINE",4) == 0))
{
if (!expr(&val,opd->a09,opd->buffer,opd->pass))
return false;
format->line = val.value;
}
else if ((tmp.s == 4) && (memcmp(tmp.text,"INCR",4) == 0))
return true;
else if ((tmp.s == 4) && (memcmp(tmp.text,"CODE",4) == 0))
{
if (!expr(&val,opd->a09,opd->buffer,opd->pass))
return false;
format->line = val.value;
}
else
return message(opd->a09,MSG_ERROR,"E0087: option '%.*s' not supported",tmp.s,tmp.text);
}
return true;
}
/**************************************************************************/
bool format_rsdos_init(struct a09 *a09)
{
static struct format const callbacks =
{
.backend = BACKEND_RSDOS,
.cmdline = frsdos_cmdline,
.pass_start = fdefault_pass,
.pass_end = fdefault_pass,
.write = frsdos_write,
.opt = frsdos__opt,
.dp = fdefault,
.code = fdefault,
.align = frsdos_align,
.end = frsdos_end,
.org = frsdos_org,
.rmb = frsdos_rmb,
.setdp = fdefault,
.test = fdefault__test,
.tron = fdefault,
.troff = fdefault,
.Assert = fdefault,
.endtst = fdefault,
.Float = freal__msfp,
.fini = fdefault_fini,
.data = NULL,
};
assert(a09 != NULL);
struct format_rsdos *data = malloc(sizeof(struct format_rsdos));
if (data != NULL)
{
data->basicf = NULL;
data->name = NULL;
data->section_hdr = 0;
data->section_start = 0;
data->entry = 65535u;
data->line = 10;
data->strspace = 200;
data->staddr = 0;
data->usr = 0;
data->endf = false;
data->org = false;
a09->format = callbacks;
a09->format.data = data;
memset(data->defusr,0,sizeof(data->defusr));
return true;
}
else
return message(a09,MSG_ERROR,"E0046: out of memory");
}
/**************************************************************************/