-
Notifications
You must be signed in to change notification settings - Fork 5
/
as1-8080.c
364 lines (344 loc) · 6.73 KB
/
as1-8080.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
/*
* 8080/8085 assembler.
* Assemble one line of input.
*/
#include "as.h"
static int cputype = 8080;
static void require_8085(void)
{
if (!(cpu_flags & OA_8080_8085)) {
if (pass == 3)
cpu_flags |= OA_8080_8085;
if (cputype != 8085)
err('1', REQUIRE_8085);
}
}
/*
* Set up for the start of each pass
*/
int passbegin(int pass)
{
cputype = 8080;
segment = 1; /* Default to code */
/* We have no variable sized branches to fix up so
we do not do pass 1 and 2 */
if (pass == 1 || pass == 2)
return 0;
return 1;
}
void doflush(void)
{
}
/*
* 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.
*/
void getaddr(ADDR *ap)
{
int c;
/* Address descriptors in 8080 are really simple as the instruction
always explicitly implies the following type */
ap->a_type = 0;
ap->a_flags = 0;
ap->a_sym = NULL;
c = getnb();
if (c == '<')
ap->a_flags |= A_LOW;
else if (c == '>')
ap->a_flags |= A_HIGH;
else
unget(c);
expr1(ap, LOPRI, 0);
}
static void constify(ADDR *ap)
{
if ((ap->a_type & TMMODE) == (TUSER|TMINDIR))
ap->a_type = TUSER;
}
int xlat_reg(ADDR *ap, int bad)
{
int reg;
switch((ap->a_type & TMMODE))
{
case TWR:
/* AF or SP encoding 3. Only one of the two is valid
for any given instruction - PSW for stack ops, SP
for anything else */
if (ap->a_value == bad)
aerr(INVALID_REG);
return 3;
case TBR:
/* B D or H for BC DE HL pairs */
reg = ap->a_type & TMREG;
if (reg & 1)
aerr(INVALID_REG);
if (reg > H)
aerr(INVALID_REG);
return reg >> 1;
default:
aerr(INVALID_REG);
return 0;
}
}
/*
* 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 opcode;
int reg;
VALUE value;
int delim;
SYM *sp1;
char id[NCPS];
char id1[NCPS];
ADDR a1;
ADDR a2;
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);
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;
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;
}
/*
* 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.
*/
if ((sp=lookup(id, 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);
constify(&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);
constify(&a1);
istuser(&a1);
if (a1.a_segment != ABSOLUTE)
qerr(MUST_BE_ABSOLUTE);
outsegment(ABSOLUTE);
dot[segment] = a1.a_value;
/* Tell the binary generator we've got a new absolute
segment. */
outabsolute(a1.a_value);
break;
case TEXPORT:
getid(id, getnb());
sp = lookup(id, uhash, 1);
sp->s_type |= TPUBLIC;
break;
/* .code etc */
case TSEGMENT:
segment = sp->s_value;
/* Tell the binary generator about a segment switch to a non
absolute segnent */
outsegment(segment);
break;
case TDEFB:
do {
getaddr(&a1);
constify(&a1);
istuser(&a1);
outrab(&a1);
} while ((c=getnb()) == ',');
unget(c);
break;
case TDEFW:
do {
getaddr(&a1);
constify(&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:
getaddr(&a1);
constify(&a1);
istuser(&a1);
/* Write out the bytes. The BSS will deal with the rest */
for (value = 0 ; value < a1.a_value; value++)
outab(0);
break;
case TSETCPU:
getaddr(&a1);
constify(&a1);
istuser(&a1);
if (a1.a_value != 8080 && a1.a_value != 8085)
aerr(SYNTAX_ERROR);
cputype = a1.a_value;
break;
case TBPTR:
outscale(1,16);
do {
getaddr(&a1);
constify(&a1);
istuser(&a1);
outraw(&a1);
} while ((c=getnb()) == ',');
outscale(0,16);
unget(c);
break;
case TIMPL85:
require_8085();
/* Fall through */
case TIMPL:
outab(opcode);
break;
case TRST:
getaddr(&a1);
constify(&a1);
istuser(&a1);
if (a1.a_value < 0 || a1.a_value > 7)
aerr(INVALID_CONST);
else
outab(opcode|(a1.a_value << 3));
break;
case TI8_85:
require_8085();
case TI8:
getaddr(&a1);
constify(&a1);
istuser(&a1);
outab(opcode);
outrab(&a1);
break;
case TI16_85:
require_8085();
case TI16:
getaddr(&a1);
constify(&a1);
istuser(&a1);
outab(opcode);
outraw(&a1);
break;
case TREG8H:
getaddr(&a1);
reg = a1.a_type & TMREG;
if ((a1.a_type & TMMODE) == TBR)
outab(opcode | (reg << 3));
else
aerr(INVALID_REG);
break;
case TREG8:
getaddr(&a1);
reg = a1.a_type & TMREG;
if ((a1.a_type & TMMODE) == TBR)
outab(opcode | reg);
else
aerr(INVALID_REG);
break;
case TREG16:
getaddr(&a1);
reg = xlat_reg(&a1, PSW);
outab(opcode | (reg << 4));
break;
case TREG16_P:
getaddr(&a1);
reg = xlat_reg(&a1, SP);
outab(opcode | (reg << 4));
break;
case TREG16BD:
getaddr(&a1);
reg = xlat_reg(&a1, SP);
if (reg > 1)
aerr(INVALID_REG);
outab(opcode | (reg << 4));
break;
case TMOV:
getaddr(&a1);
comma();
getaddr(&a2);
if ((a1.a_type & TMMODE) != TBR ||
((a2.a_type & TMMODE) != TBR))
aerr(INVALID_REG);
if ((a1.a_type & TMREG) == M && (a2.a_type & TMREG) == M)
aerr(INVALID_REG);
outab(opcode | ((a1.a_type & TMREG) << 3) | (a2.a_type & TMREG));
break;
case TREG8_I8:
getaddr(&a1);
reg = a1.a_type & TMREG;
if ((a1.a_type & TMMODE) == TBR)
outab(opcode| (reg << 3));
else
aerr(INVALID_REG);
comma();
getaddr(&a1);
constify(&a1);
istuser(&a1);
outrab(&a1);
break;
case TREG16_I16:
getaddr(&a1);
reg = xlat_reg(&a1, PSW);
outab(opcode | (reg << 4));
comma();
getaddr(&a1);
constify(&a1);
istuser(&a1);
outraw(&a1);
break;
default:
aerr(SYNTAX_ERROR);
}
goto loop;
}