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
|
/* code7720.c */
/*****************************************************************************/
/* Makroassembler AS */
/* */
/* Codegenerator NEC uPD772x */
/* */
/* Historie: 30. 8.1998 Grundsteinlegung */
/* 31. 8.1998 RET-Anweisung */
/* 2. 9.1998 Verallgemeinerung auf 77C25 */
/* 5. 9.1998 Pseudo-Anweisungen */
/* 11. 9.1998 ROMData-Segment angelegt */
/* 24. 9.1998 Korrekturen fuer DOS-Compiler */
/* 2. 1.1999 ChkPC-Anpassung */
/* */
/*****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include <ctype.h>
#include "strutil.h"
#include "nls.h"
#include "bpemu.h"
#include "asmdef.h"
#include "asmsub.h"
#include "asmpars.h"
#include "asmcode.h"
#include "asmitree.h"
#include "headids.h"
/*---------------------------------------------------------------------------*/
#define JmpOrderCnt 36
#define ALU2OrderCnt 8
#define ALU1OrderCnt 7
#define DestRegCnt 16
#define SrcRegCnt 16
#define ALUSrcRegCnt 4
typedef struct
{
LongWord Code;
} FixedOrder;
typedef struct
{
char *Name;
LongWord Code;
} TReg;
typedef enum {MoveField,ALUField,DPLField,DPHField,RPField,RetField} OpComps;
static CPUVar CPU7720,CPU7725;
static LongWord ActCode;
static Boolean InOp;
static Byte UsedOpFields;
static Byte TypePos,ImmValPos,AddrPos,ALUPos,DPLPos,AccPos,ALUSrcPos;
static IntType MemInt;
static Word ROMEnd,DROMEnd,RAMEnd;
static FixedOrder *JmpOrders,*ALU2Orders,*ALU1Orders;
static TReg *DestRegs,*SrcRegs,*ALUSrcRegs;
static PInstTable InstTable, OpTable;
/*---------------------------------------------------------------------------*/
/* Hilfsroutinen */
static Boolean DecodeReg(char *Asc, LongWord *Code, TReg *Regs, int Cnt)
BEGIN
int z;
for (z=0; z<Cnt; z++)
if (strcasecmp(Asc,Regs[z].Name)==0) break;
if (z<Cnt) *Code=Regs[z].Code;
return z<Cnt;
END
static Boolean ChkOpPresent(OpComps Comp)
BEGIN
if ((UsedOpFields&(1l<<Comp))!=0)
BEGIN
WrError(1355); return False;
END
else
BEGIN
UsedOpFields|=1l<<Comp; return True;
END
END
/*---------------------------------------------------------------------------*/
/* Dekoder */
static void DecodeJmp(Word Index)
BEGIN
Word Dest;
Boolean OK;
FixedOrder *Op=JmpOrders+Index;
if (ArgCnt!=1) WrError(1110);
else
BEGIN
Dest=EvalIntExpression(ArgStr[1],MemInt,&OK);
if (OK)
BEGIN
DAsmCode[0]=(2l<<TypePos)+(Op->Code<<13)+(Dest<<AddrPos);
CodeLen=1;
END
END
END
static void DecodeDATA(Word Index)
BEGIN
LongInt MinV,MaxV,Trans;
TempResult t;
int z,z2,Pos,Max;
Boolean OK;
if (ActPC==SegCode) MaxV=(MomCPU>=CPU7725) ? 16777215 : 8388607;
else MaxV=65535;
MinV=(-((MaxV+1) >> 1));
if (ArgCnt==0) WrError(1110);
else
BEGIN
OK=True; z=1;
while ((OK) & (z<=ArgCnt))
BEGIN
FirstPassUnknown=False;
EvalExpression(ArgStr[z],&t);
if ((FirstPassUnknown) AND (t.Typ==TempInt)) t.Contents.Int&=MaxV;
switch (t.Typ)
BEGIN
case TempInt:
if (ChkRange(t.Contents.Int,MinV,MaxV))
if (ActPC==SegCode) DAsmCode[CodeLen++]=t.Contents.Int;
else WAsmCode[CodeLen++]=t.Contents.Int;
break;
case TempFloat:
WrError(1135); OK=False;
break;
case TempString:
Max=((ActPC==SegCode) AND (MomCPU>=CPU7725)) ? 3 : 2; Pos=0;
for (z2=0; z2<strlen(t.Contents.Ascii); z2++)
BEGIN
Trans=CharTransTable[((usint) t.Contents.Ascii[z2])&0xff];
if (ActPC==SegCode)
DAsmCode[CodeLen]=(Pos==0) ? Trans : (DAsmCode[CodeLen]<<8)|Trans;
else
WAsmCode[CodeLen]=(Pos==0) ? Trans : (WAsmCode[CodeLen]<<8)|Trans;
if (++Pos==Max)
BEGIN
Pos=0; CodeLen++;
END
END
if (Pos!=0) CodeLen++;
break;
case TempNone:
OK=False;
END
z++;
END
END
END
static void DecodeRES(Word Index)
BEGIN
Word Size;
Boolean OK;
if (ArgCnt!=1) WrError(1110);
else
BEGIN
FirstPassUnknown=False;
Size=EvalIntExpression(ArgStr[1],Int16,&OK);
if (FirstPassUnknown) WrError(1820);
if ((OK) AND (NOT FirstPassUnknown))
BEGIN
DontPrint=True;
CodeLen=Size;
BookKeeping();
END
END
END
static void DecodeALU2(Word Index)
BEGIN
LongWord Acc=0xff,Src;
FixedOrder *Op=ALU2Orders+Index;
char ch;
if (NOT ChkOpPresent(ALUField)) return;
if (ArgCnt!=2) WrError(1110);
else if (NOT DecodeReg(ArgStr[2],&Src,ALUSrcRegs,ALUSrcRegCnt)) WrXError(1445,ArgStr[2]);
else
BEGIN
if ((strlen(ArgStr[1])==4) AND (strncasecmp(ArgStr[1],"ACC",3)==0))
BEGIN
ch=toupper(ArgStr[1][3]);
if ((ch>='A') AND (ch<='B')) Acc=ch-'A';
END
if (Acc==0xff) WrXError(1445,ArgStr[1]);
else ActCode|=(Op->Code<<ALUPos)+(Acc<<AccPos)+(Src<<ALUSrcPos);
END
END
static void DecodeALU1(Word Index)
BEGIN
LongWord Acc=0xff;
FixedOrder *Op=ALU1Orders+Index;
char ch;
if (NOT ChkOpPresent(ALUField)) return;
if (ArgCnt!=1) WrError(1110);
else
BEGIN
if ((strlen(ArgStr[1])==4) AND (strncasecmp(ArgStr[1],"ACC",3)==0))
BEGIN
ch=toupper(ArgStr[1][3]);
if ((ch>='A') AND (ch<='B')) Acc=ch-'A';
END
if (Acc==0xff) WrXError(1445,ArgStr[1]);
else ActCode|=(Op->Code<<ALUPos)+(Acc<<AccPos);
END
END
static void DecodeNOP(Word Index)
BEGIN
if (NOT ChkOpPresent(ALUField)) return;
END
static void DecodeDPL(Word Index)
BEGIN
if (NOT ChkOpPresent(DPLField)) return;
if (ArgCnt!=0) WrError(1110);
else ActCode|=(((LongWord)Index)<<DPLPos);
END
static void DecodeDPH(Word Index)
BEGIN
if (NOT ChkOpPresent(DPHField)) return;
if (ArgCnt!=0) WrError(1110);
else ActCode|=(((LongWord)Index)<<9);
END
static void DecodeRP(Word Index)
BEGIN
if (NOT ChkOpPresent(RPField)) return;
if (ArgCnt!=0) WrError(1110);
else ActCode|=(((LongWord)Index)<<8);
END
static void DecodeRET(Word Index)
BEGIN
if (NOT ChkOpPresent(RetField)) return;
if (ArgCnt!=0) WrError(1110);
else ActCode|=(1l<<TypePos);
END
static void DecodeLDI(Word Index)
BEGIN
LongWord Value;
LongWord Reg;
Boolean OK;
if (ArgCnt!=2) WrError(1110);
else if (NOT DecodeReg(ArgStr[1],&Reg,DestRegs,DestRegCnt)) WrXError(1445,ArgStr[1]);
else
BEGIN
Value=EvalIntExpression(ArgStr[2],Int16,&OK);
if (OK)
BEGIN
DAsmCode[0]=(3l<<TypePos)+Reg+(Value<<ImmValPos);
CodeLen=1;
END
END
END
static void DecodeOP(Word Index)
BEGIN
char *p;
int z;
UsedOpFields=0; ActCode=0;
if (ArgCnt>1)
BEGIN
p=FirstBlank(ArgStr[1]);
if (p!=NULL)
BEGIN
*p='\0'; strmaxcpy(OpPart,ArgStr[1],255);
NLS_UpString(OpPart);
strcpy(ArgStr[1],p+1); KillPrefBlanks(ArgStr[1]);
END
else
BEGIN
strmaxcpy(OpPart,ArgStr[1],255);
for (z=1; z<ArgCnt; z++)
strcpy(ArgStr[z],ArgStr[z+1]);
ArgCnt--;
END
if (NOT LookupInstTable(OpTable,OpPart)) WrXError(1200,OpPart);
END
DAsmCode[0]=ActCode; CodeLen=1;
END
static void DecodeMOV(Word Index)
BEGIN
LongWord Dest,Src;
if (NOT ChkOpPresent(MoveField)) return;
if (ArgCnt!=2) WrError(1110);
else if (NOT DecodeReg(ArgStr[1],&Dest,DestRegs,DestRegCnt)) WrXError(1445,ArgStr[1]);
else if (NOT DecodeReg(ArgStr[2],&Src,SrcRegs,SrcRegCnt)) WrXError(1445,ArgStr[2]);
else ActCode|=Dest+(Src<<4);
END
/*---------------------------------------------------------------------------*/
/* Tabellenverwaltung */
static int InstrZ;
static void AddJmp(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=JmpOrderCnt) exit(255);
if ((MomCPU<CPU7725) AND (Odd(NCode))) return;
JmpOrders[InstrZ].Code=(MomCPU==CPU7725) ? NCode : NCode>>1;
AddInstTable(InstTable, NName, InstrZ++, DecodeJmp);
END
static void AddALU2(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=ALU2OrderCnt) exit(255);
ALU2Orders[InstrZ].Code=NCode;
AddInstTable(OpTable, NName, InstrZ++, DecodeALU2);
END
static void AddALU1(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=ALU1OrderCnt) exit(255);
ALU1Orders[InstrZ].Code=NCode;
AddInstTable(OpTable, NName, InstrZ++, DecodeALU1);
END
static void AddDestReg(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=DestRegCnt) exit(255);
DestRegs[InstrZ].Name=NName;
DestRegs[InstrZ++].Code=NCode;
END
static void AddSrcReg(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=SrcRegCnt) exit(255);
SrcRegs[InstrZ].Name=NName;
SrcRegs[InstrZ++].Code=NCode;
END
static void AddALUSrcReg(char *NName, LongWord NCode)
BEGIN
if (InstrZ>=ALUSrcRegCnt) exit(255);
ALUSrcRegs[InstrZ].Name=NName;
ALUSrcRegs[InstrZ++].Code=NCode;
END
static void InitFields(void)
BEGIN
InstTable=CreateInstTable(101);
OpTable=CreateInstTable(79);
AddInstTable(InstTable,"LDI",0,DecodeLDI);
AddInstTable(InstTable,"LD",0,DecodeLDI);
AddInstTable(InstTable,"OP",0,DecodeOP);
AddInstTable(InstTable,"DATA",0,DecodeDATA);
AddInstTable(InstTable,"RES",0,DecodeRES);
AddInstTable(OpTable,"MOV",0,DecodeMOV);
AddInstTable(OpTable,"NOP",0,DecodeNOP);
AddInstTable(OpTable,"DPNOP",0,DecodeDPL);
AddInstTable(OpTable,"DPINC",1,DecodeDPL);
AddInstTable(OpTable,"DPDEC",2,DecodeDPL);
AddInstTable(OpTable,"DPCLR",3,DecodeDPL);
AddInstTable(OpTable,"M0",0,DecodeDPH);
AddInstTable(OpTable,"M1",1,DecodeDPH);
AddInstTable(OpTable,"M2",2,DecodeDPH);
AddInstTable(OpTable,"M3",3,DecodeDPH);
AddInstTable(OpTable,"M4",4,DecodeDPH);
AddInstTable(OpTable,"M5",5,DecodeDPH);
AddInstTable(OpTable,"M6",6,DecodeDPH);
AddInstTable(OpTable,"M7",7,DecodeDPH);
if (MomCPU>=CPU7725)
BEGIN
AddInstTable(OpTable,"M8",8,DecodeDPH);
AddInstTable(OpTable,"M9",9,DecodeDPH);
AddInstTable(OpTable,"MA",10,DecodeDPH);
AddInstTable(OpTable,"MB",11,DecodeDPH);
AddInstTable(OpTable,"MC",12,DecodeDPH);
AddInstTable(OpTable,"MD",13,DecodeDPH);
AddInstTable(OpTable,"ME",14,DecodeDPH);
AddInstTable(OpTable,"MF",15,DecodeDPH);
END
AddInstTable(OpTable,"RPNOP",0,DecodeRP);
AddInstTable(OpTable,"RPDEC",1,DecodeRP);
AddInstTable(OpTable,"RET",1,DecodeRET);
JmpOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*JmpOrderCnt); InstrZ=0;
AddJmp("JMP" , 0x100); AddJmp("CALL" , 0x140);
AddJmp("JNCA" , 0x080); AddJmp("JCA" , 0x082);
AddJmp("JNCB" , 0x084); AddJmp("JCB" , 0x086);
AddJmp("JNZA" , 0x088); AddJmp("JZA" , 0x08a);
AddJmp("JNZB" , 0x08c); AddJmp("JZB" , 0x08e);
AddJmp("JNOVA0", 0x090); AddJmp("JOVA0" , 0x092);
AddJmp("JNOVB0", 0x094); AddJmp("JOVB0" , 0x096);
AddJmp("JNOVA1", 0x098); AddJmp("JOVA1" , 0x09a);
AddJmp("JNOVB1", 0x09c); AddJmp("JOVB1" , 0x09e);
AddJmp("JNSA0" , 0x0a0); AddJmp("JSA0" , 0x0a2);
AddJmp("JNSB0" , 0x0a4); AddJmp("JSB0" , 0x0a6);
AddJmp("JNSA1" , 0x0a8); AddJmp("JSA1" , 0x0aa);
AddJmp("JNSB1" , 0x0ac); AddJmp("JSB1" , 0x0ae);
AddJmp("JDPL0" , 0x0b0); AddJmp("JDPLF" , 0x0b2);
AddJmp("JNSIAK", 0x0b4); AddJmp("JSIAK" , 0x0b6);
AddJmp("JNSOAK", 0x0b8); AddJmp("JSOAK" , 0x0ba);
AddJmp("JNRQM" , 0x0bc); AddJmp("JRQM" , 0x0be);
AddJmp("JDPLN0", 0x0b1); AddJmp("JDPLNF" , 0x0b3);
ALU2Orders=(FixedOrder *) malloc(sizeof(FixedOrder)*ALU2OrderCnt); InstrZ=0;
AddALU2("OR" , 1); AddALU2("AND" , 2); AddALU2("XOR" , 3);
AddALU2("SUB" , 4); AddALU2("ADD" , 5); AddALU2("SBB" , 6);
AddALU2("ADC" , 7); AddALU2("CMP" ,10);
ALU1Orders=(FixedOrder *) malloc(sizeof(FixedOrder)*ALU1OrderCnt); InstrZ=0;
AddALU1("DEC" , 8); AddALU1("INC" , 9); AddALU1("SHR1",11);
AddALU1("SHL1",12); AddALU1("SHL2",13); AddALU1("SHL4",14);
AddALU1("XCHG",15);
DestRegs=(TReg *) malloc(sizeof(TReg)*DestRegCnt); InstrZ=0;
AddDestReg("@NON", 0); AddDestReg("@A" , 1);
AddDestReg("@B" , 2); AddDestReg("@TR" , 3);
AddDestReg("@DP" , 4); AddDestReg("@RP" , 5);
AddDestReg("@DR" , 6); AddDestReg("@SR" , 7);
AddDestReg("@SOL", 8); AddDestReg("@SOM", 9);
AddDestReg("@K" , 10); AddDestReg("@KLR", 11);
AddDestReg("@KLM", 12); AddDestReg("@L" , 13);
AddDestReg((MomCPU==CPU7725)?"@TRB":"",14);
AddDestReg("@MEM", 15);
SrcRegs=(TReg *) malloc(sizeof(TReg)*SrcRegCnt); InstrZ=0;
AddSrcReg("NON" , 0); AddSrcReg("A" , 1);
AddSrcReg("B" , 2); AddSrcReg("TR" , 3);
AddSrcReg("DP" , 4); AddSrcReg("RP" , 5);
AddSrcReg("RO" , 6); AddSrcReg("SGN" , 7);
AddSrcReg("DR" , 8); AddSrcReg("DRNF", 9);
AddSrcReg("SR" , 10); AddSrcReg("SIM" , 11);
AddSrcReg("SIL" , 12); AddSrcReg("K" , 13);
AddSrcReg("L" , 14); AddSrcReg("MEM" , 15);
ALUSrcRegs=(TReg *) malloc(sizeof(TReg)*ALUSrcRegCnt); InstrZ=0;
AddALUSrcReg("RAM", 0); AddALUSrcReg("IDB", 1);
AddALUSrcReg("M" , 2); AddALUSrcReg("N" , 3);
END
static void DeinitFields(void)
BEGIN
DestroyInstTable(InstTable);
DestroyInstTable(OpTable);
free(JmpOrders);
free(ALU2Orders);
free(ALU1Orders);
free(DestRegs);
free(SrcRegs);
free(ALUSrcRegs);
END
/*---------------------------------------------------------------------------*/
/* Callbacks */
static void MakeCode_7720(void)
BEGIN
Boolean NextOp;
/* Nullanweisung */
if ((Memo("")) AND (*AttrPart=='\0') AND (ArgCnt==0)) return;
/* direkte Anweisungen */
NextOp=Memo("OP");
if (LookupInstTable(InstTable,OpPart))
BEGIN
InOp=NextOp; return;
END
/* wenn eine parallele Op-Anweisung offen ist, noch deren Komponenten testen */
if ((InOp) AND (LookupInstTable(OpTable,OpPart)))
BEGIN
RetractWords(1);
DAsmCode[0]=ActCode; CodeLen=1;
return;
END
/* Hae??? */
WrXError(1200,OpPart);
END
static Boolean IsDef_7720(void)
BEGIN
return False;
END
static void SwitchFrom_7720(void)
BEGIN
DeinitFields();
END
static void SwitchTo_7720(void)
BEGIN
PFamilyDescr FoundDescr;
TurnWords=False; ConstMode=ConstModeIntel; SetIsOccupied=False;
if (MomCPU==CPU7725)
BEGIN
FoundDescr=FindFamilyByName("7725");
MemInt=UInt11;
ROMEnd=0x7ff; DROMEnd=0x3ff; RAMEnd=0xff;
TypePos=22;
ImmValPos=6;
AddrPos=2;
ALUPos=16;
DPLPos=13;
AccPos=15;
ALUSrcPos=20;
END
else
BEGIN
FoundDescr=FindFamilyByName("7720");
MemInt=UInt9;
ROMEnd=0x1ff; DROMEnd=0x1ff; RAMEnd=0x7f;
TypePos=21;
ImmValPos=5;
AddrPos=4;
ALUPos=15;
DPLPos=12;
AccPos=14;
ALUSrcPos=19;
END
PCSymbol="$"; HeaderID=FoundDescr->Id; NOPCode=0x000000;
DivideChars=","; HasAttrs=False;
ValidSegs=(1l<<SegCode)|(1l<<SegData)|(1l<<SegRData);
Grans[SegCode ]=4; ListGrans[SegCode ]=4; SegInits[SegCode ]=0;
SegLimits[SegCode ] = ROMEnd;
Grans[SegData ]=2; ListGrans[SegData ]=2; SegInits[SegData ]=0;
SegLimits[SegData ] = RAMEnd;
Grans[SegRData]=2; ListGrans[SegRData]=2; SegInits[SegRData]=0;
SegLimits[SegRData] = DROMEnd;
MakeCode=MakeCode_7720; IsDef=IsDef_7720;
SwitchFrom=SwitchFrom_7720;
InOp=False; UsedOpFields=0; ActCode=0;
InitFields();
END
/*---------------------------------------------------------------------------*/
/* Initialisierung */
void code7720_init(void)
BEGIN
CPU7720=AddCPU("7720",SwitchTo_7720);
CPU7725=AddCPU("7725",SwitchTo_7720);
END
|