-
Notifications
You must be signed in to change notification settings - Fork 5
/
as1-nova.c
661 lines (614 loc) · 13.5 KB
/
as1-nova.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* DG Nova assembler.
* Assemble one line of input.
* Knows all the dirt.
*
* TODO: Add support to the core as code to error attempts to shift a pointer
* and lose bits (eg if you attempt to make a word pointer of a byte aligned
* object).
*/
#include "as.h"
static unsigned is_bp[OSEG + 1];
int passbegin(int pass)
{
unsigned i;
segment = 1; /* Default to code */
for (i = 0; i < OSEG + 1; i++)
is_bp[i] = 1; /* Linker initially assumes byteptrs */
outscale(0,16); /* Reset internal tracking */
if (pass == 1 || pass == 2)
return 0;
return 1; /* All passes required */
}
void byteptr(unsigned n)
{
if (n == is_bp[segment])
return;
is_bp[segment] = n;
if (n == 0)
outscale(1, 16);
else
outscale(0, 16);
}
/*
* Read in an address
* descriptor, and fill in
* the supplied "ADDR" structure
* with the mode and value.
* Exits directly to "qerr" if
* there is no address field or
* if the syntax is bad.
* TODO: FIXME - who owns decoding indirect and passing it around
*/
void getaddr(ADDR *ap)
{
int c;
ap->a_sym = NULL;
ap->a_flags = 0;
ap->a_type = 0;
ap->a_value = 0;
ap->a_segment = ABSOLUTE;
/* We only have one addressing format we ever use.. an address.
Quite how we encode it is another saga because our memory ops
use register relative or pc relative */
c = getnb();
if (/*c != '#' && */c != '@') /* Until we support literal adding */
unget(c);
expr1(ap, LOPRI, 1);
switch (ap->a_type&TMMODE) {
case TUSER:
break;
default:
qerr(SYNTAX_ERROR);
}
if (c == '@') /* Indirect */
ap->a_value |= 0x8000;
}
static int accumulator(void)
{
int c = getnb();
if (c < '0' || c > '3') {
aerr(BAD_ACCUMULATOR);
unget(c);
return 0;
}
return c - '0';
}
static int carryop(char c)
{
c = toupper(c);
if (c == 'Z')
return 1;
if (c == 'O')
return 2;
if (c == 'C')
return 3;
return 0;
}
static int postop(char c)
{
c = toupper(c);
if (c == 'L')
return 1;
if (c == 'R')
return 2;
if (c == 'S')
return 3;
return 0;
}
/*
* Byte handling. We queue bytes into pairs then add them. If we
* write a word then outaw flushes any dangling byte into the left
* half of the word.
*/
void flushbyte(void)
{
if (dot[segment] & 1)
outab(0);
}
/*
* Flush any final byte in case we end with an odd sized
* segment.
*/
void doflush(void)
{
for (segment = 1; segment < OSEG; segment++)
flushbyte();
}
/*
* Word sized binary output
* We write machine words in network order
*/
void outaw(uint16_t v)
{
/* Flush pending byte and align */
flushbyte();
outab(v >> 8);
outab(v);
}
/*
* Assemble one line.
* The line in in "ib", the "ip"
* scans along it. The code is written
* right out.
*/
void asmline(void)
{
SYM *sp;
int c;
int acs, acd;
int opcode;
VALUE value;
int delim;
SYM *sp1;
char id[NCPS];
char id1[NCPS];
char iid[NCPS];
ADDR a1;
char *p;
loop:
if ((c=getnb())=='\n' || c==';')
return;
if (isalpha(c) == 0 && c != '_' && c != '.')
qerr(UNEXPECTED_CHR);
getid(id, c);
if ((c=getnb()) == ':') {
sp = lookup(id, uhash, 1);
/* We don't allow pointers to non word for now TODO */
flushbyte();
if (pass == 0) {
if ((sp->s_type&TMMODE) != TNEW
&& (sp->s_type&TMASG) == 0)
sp->s_type |= TMMDF;
sp->s_type &= ~TMMODE;
sp->s_type |= TUSER;
/* Word machine so half the byte count */
sp->s_value = dot[segment];
sp->s_segment = segment;
} else {
if ((sp->s_type&TMMDF) != 0)
err('m', MULTIPLE_DEFS);
if (sp->s_value != dot[segment])
err('p', PHASE_ERROR);
}
goto loop;
}
/* We have to do ugly things here because the Nova instruction set
merges the opcode and flags */
memcpy(iid, id, 4);
memset(iid + 3, 0, NCPS - 3);
/*
* If the first token is an
* id and not an operation code,
* assume that it is the name in front
* of an "equ" assembler directive.
*/
/* Check for .equ forms */
if ((sp = lookup(id, phash, 0)) == NULL &&
(sp = lookup(iid, phash, 0)) == NULL) {
getid(id1, c);
if ((sp1=lookup(id1, phash, 0)) == NULL
|| (sp1->s_type&TMMODE) != TEQU) {
err('o', SYNTAX_ERROR);
return;
}
getaddr(&a1);
istuser(&a1);
sp = lookup(id, uhash, 1);
if ((sp->s_type&TMMODE) != TNEW
&& (sp->s_type&TMASG) == 0)
err('m', MULTIPLE_DEFS);
sp->s_type &= ~(TMMODE|TPUBLIC);
sp->s_type |= TUSER|TMASG;
sp->s_value = a1.a_value;
sp->s_segment = a1.a_segment;
/* FIXME: review .equ to an external symbol/offset and
what should happen */
goto loop;
}
unget(c);
opcode = sp->s_value;
switch (sp->s_type&TMMODE) {
case TORG:
getaddr(&a1);
istuser(&a1);
if (a1.a_segment != ABSOLUTE)
qerr(MUST_BE_ABSOLUTE);
flushbyte(); /* Deposit any dangling byte first */
outsegment(ABSOLUTE);
dot[segment] = a1.a_value * 2; /* dot is in bytes */
/* Tell the binary generator we've got a new absolute
segment. */
outabsolute(dot[segment]);
break;
case TEXPORT:
getid(id, getnb());
sp = lookup(id, uhash, 1);
sp->s_type |= TPUBLIC;
break;
/* .code etc */
case TSEGMENT:
/* Tell the binary generator about a segment switch to a non
absolute segnent */
flushbyte();
segment = sp->s_value;
outsegment(segment);
break;
case TDEFB:
do {
getaddr(&a1);
istuser(&a1);
/* FIXME: how to integrate word machines into core nicely */
if (a1.a_segment == ABSOLUTE)
a1.a_value *= 2;
outrab(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TDEFW:
flushbyte();
byteptr(0);
do {
getaddr(&a1);
istuser(&a1);
/* FIXME: how to integrate word machines into core nicely */
if (a1.a_segment == ABSOLUTE)
a1.a_value *= 2;
outraw(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TBPTR:
flushbyte();
byteptr(1);
do {
getaddr(&a1);
istuser(&a1);
outraw(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TDEFM:
if ((delim=getnb()) == '\n')
qerr(MISSING_DELIMITER);
while ((c=get()) != delim) {
if (c == '\n')
qerr(MISSING_DELIMITER);
outab(c);
}
break;
case TDEFS:
flushbyte();
getaddr(&a1);
istuser(&a1);
/* Write out the words. The BSS will deal with the rest */
for (value = 0 ; value < a1.a_value; value++)
outaw(0);
break;
case TCPUOPT:
cpu_flags |= sp->s_value;
break;
case TMEMORY:
{
int indirect = 0;
/*
* Memory operations are either
* 0,disp Word 0-255 (zero page)
* 1,signed disp PC relative
* 2.disp ac2 + offset
* 3,disp ac3 + offset
*
* We don't enforce any rules on ac2/ac3 but encode
* them on the basis the user knows what they are doing
*
* 0,disp is encoded is an 8 bit relocation for ZP
* 1,disp FIXME needs to be encoded as an 8bit PCREL
*
* There is *no* immediate load nor is there anyway
* to load arbitrary addresses.
*
* FIXME: we need some Nova specific meta ops as a
* result
*
* .nomodify - don't sneak in data words
* .modify - allowed to sneak in data words
* .local - local data word
* .flushlocal - write locals out here
*
* local data words are placed in a queue with their
* relocation address. During pass 0 we try to place them
* by adding ,skp to TALU instructions and putting one
* after it. If we reach the point it won't fit we add a
* JMP around the data and load with data. Likewise we
* can fill after a jump.
*
* This has its own fun... a jump is itself pcrel or
* constrained. Fortunately however we can encode an
* arbitrary jump as JMP #.+1 and a word. We can't do
* this ourself with JSR but compilers can jmp 3,1
* and do it.
*
* For A2/A3 the same way to write stuff is likely to be
*
* ; a2 is loaded with foo
*
* LDA 1,bar-foo,2
*
* and the assembler with turn bar-foo into an ABSOLUTE
*/
acd = accumulator();
comma();
c = get();
if (c == '@')
indirect = 1;
else {
unget(c);
indirect = 0;
}
getaddr(&a1);
c = get();
if (c == ',')
acs = accumulator();
else {
acs = 0;
unget(c);
}
/* ,0 means zero page */
if (acs == 0) {
if (a1.a_segment == UNKNOWN)
a1.a_segment = ZP;
if (a1.a_segment != ZP && a1.a_segment != ABSOLUTE)
aerr(NEED_ZPABS);
}
/* ,2 + are indexes so we can't really police them for sanity */
/* ,1 is PC relative so must be in this segment */
if (acs == 1) {
if (a1.a_segment == UNKNOWN)
a1.a_segment = segment;
else if (a1.a_segment != ABSOLUTE && a1.a_segment != segment)
aerr(BAD_PCREL);
if (a1.a_segment != ABSOLUTE) {
a1.a_type |= TPCREL;
a1.a_value -= dot[segment];
}
}
/* Need to push this into the core somehow */
if (a1.a_segment == ABSOLUTE)
/* Turn into byte form temporarily */
a1.a_value <<= 1;
/* Insert the accumulators */
opcode |= (acd << 11);
opcode |= (acs << 8);
if (indirect)
opcode |= 0x0400;
flushbyte();
byteptr(0);
outab(opcode >> 8);
if (acs)
outrabrel(&a1); /* Signed */
else
outrab(&a1); /* Unsigned */
break;
}
/* Akin to TMEMORY but with no accumulator */
case TMEMNA:
{
int indirect = 0;
/*
* Memory operations are either
* 0,disp Word 0-255 (zero page)
* 1,signed disp PC relative
* 2.disp ac2 + offset
* 3,disp ac3 + offset
*
* We don't enforce any rules on ac2/ac3 but encode
* them on the basis the user knows what they are doing
*
* 0,disp is encoded is an 8 bit relocation for ZP
* 1,disp FIXME needs to be encoded as an 8bit PCREL
*
* There is *no* immediate load nor is there anyway
* to load arbitrary addresses.
*
* FIXME: we need some Nova specific meta ops as a
* result
*
* .nomodify - don't sneak in data words
* .modify - allowed to sneak in data words
* .local - local data word
* .flushlocal - write locals out here
*
* local data words are placed in a queue with their
* relocation address. During pass 0 we try to place them
* by adding ,skp to TALU instructions and putting one
* after it. If we reach the point it won't fit we add a
* JMP around the data and load with data. Likewise we
* can fill after a jump.
*
* This has its own fun... a jump is itself pcrel or
* constrained. Fortunately however we can encode an
* arbitrary jump as JMP #.+1 and a word. We can't do
* this ourself with JSR but compilers can jmp 3,1
* and do it.
*
* For A2/A3 the same way to write stuff is likely to be
*
* ; a2 is loaded with foo
*
* LDA 1,bar-foo,2
*
* and the assembler with turn bar-foo into an ABSOLUTE
*/
c = get();
if (c == '@')
indirect = 1;
else {
unget(c);
indirect = 0;
}
getaddr(&a1);
c = get();
if (c == ',')
acs = accumulator();
else {
acs = 0;
unget(c);
}
/* ,0 means zero page */
if (acs == 0) {
if (a1.a_segment == UNKNOWN)
a1.a_segment = ZP;
if (a1.a_segment != ZP && a1.a_segment != ABSOLUTE)
aerr(NEED_ZPABS);
}
/* ,2 + are indexes so we can't really police them for sanity */
/* ,1 is PC relative so must be in this segment */
if (acs == 1) {
if (a1.a_segment == UNKNOWN)
a1.a_segment = segment;
else if (a1.a_segment != ABSOLUTE && a1.a_segment != segment)
aerr(BAD_PCREL);
if (a1.a_segment != ABSOLUTE) {
a1.a_type |= TPCREL;
a1.a_value -= dot[segment];
}
}
if (a1.a_segment == ABSOLUTE)
/* Turn into byte form temporarily */
a1.a_value <<= 1;
/* Insert the accumulators */
opcode |= (acs << 8);
if (indirect)
opcode |= 0x0400;
flushbyte();
byteptr(0);
outab(opcode >> 8);
if (acs)
outrabrel(&a1); /* Signed */
else if (acs == 0)
outrab(&a1); /* Unsigned */
break;
}
case TALU:
{
SYM *skp = NULL;
int drop;
int cf;
int sh;
p = id + 3;
cf = carryop(*p);
if (cf)
p++;
sh = postop(*p);
if (sh)
p++;
c = getnb();
if (c == '#')
drop = 1;
else {
unget(c);
drop = 0;
}
acs = accumulator();
comma();
acd = accumulator();
c = getnb();
if (c == ',') {
getid(id1,getnb());
skp = lookup(id1, phash, 0);
if (skp == NULL || skp->s_type != TCC)
err('s',SYNTAX_ERROR);
} else
unget(c);
opcode |= (acs << 13);
opcode |= (acd << 11);
opcode |= (sh << 6);
opcode |= (cf << 4);
opcode |= (drop << 3);
if (skp)
opcode |= skp->s_value;
flushbyte();
outaw(opcode);
break;
}
case TIO:
p = id + 3;
if (*p == 's')
opcode |= 1 << 6;
else if (*p == 'c')
opcode |= 2 << 6;
else if (*p == 'p')
opcode |= 3 << 6;
else if (*p)
aerr(SYNTAX_ERROR);
acs = accumulator();
comma();
getaddr(&a1);
istuser(&a1);
if (a1.a_value > 63)
err('d', BADDEVICE);
opcode |= (acs << 11);
opcode |= a1.a_value;
flushbyte();
outaw(opcode);
break;
case TNIO:
p = id + 3;
if (*p == 's')
opcode |= 1 << 6;
else if (*p == 'c')
opcode |= 2 << 6;
else if (*p == 'p')
opcode |= 3 << 6;
else if (*p)
aerr(SYNTAX_ERROR);
/* Fall through */
case TDEV:
/* SKP is ambiguous so we list the four forms directly
in the table to avoid this */
getaddr(&a1);
istuser(&a1);
if (a1.a_value > 63)
err('d', BADDEVICE);
opcode |= a1.a_value;
flushbyte();
outaw(opcode);
break;
case TAC:
acs = accumulator();
opcode |= (acs << 11);
flushbyte();
outaw(opcode);
break;
case TIMPL:
flushbyte();
outaw(opcode);
break;
case TBYTE:
acs = accumulator();
comma();
acd = accumulator();
opcode |= (acd << 11);
opcode |= (acs << 6);
flushbyte();
outaw(opcode);
break;
case TTRAP:
acs = accumulator();
comma();
acd = accumulator();
comma();
getaddr(&a1);
/* We can't relocate these yet FIXME */
istuser(&a1);
opcode |= (acs << 15);
opcode |= (acd << 13);
opcode |= (a1.a_value << 4);
flushbyte();
outaw(opcode);
break;
default:
aerr(SYNTAX_ERROR);
}
goto loop;
}