aboutsummaryrefslogtreecommitdiffstats
path: root/codescmp.c
blob: 18af9611bffa3f255b9a8a303efd928066db6a8a (plain)
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
/* codescmp.c */
/*****************************************************************************/
/* AS-Portierung                                                             */
/*                                                                           */
/* Codegenerator National SC/MP                                              */
/*                                                                           */
/* Historie: 17. 2.1996 Grundsteinlegung                                     */
/*            2. 1.1998 ChkPC umgestellt                                     */
/*                                                                           */
/*****************************************************************************/

#include "stdinc.h"
#include <ctype.h>
#include <string.h>

#include "nls.h"
#include "strutil.h"
#include "chunks.h"
#include "asmdef.h"
#include "asmsub.h"
#include "asmpars.h"
#include "codepseudo.h"
#include "codevars.h"


#define FixedOrderCnt 21
#define ImmOrderCnt 8
#define RegOrderCnt 3
#define MemOrderCnt 8
#define JmpOrderCnt 4

typedef struct
         {
          char *Name;
          Byte Code;
         } FixedOrder;


static CPUVar CPUSCMP;

static FixedOrder *FixedOrders;
static FixedOrder *ImmOrders;
static FixedOrder *RegOrders;
static FixedOrder *MemOrders;
static FixedOrder *JmpOrders;

/*---------------------------------------------------------------------------*/

        static void AddFixed(char *NName, Byte NCode)
BEGIN
   if (InstrZ>=FixedOrderCnt) exit(255);
   FixedOrders[InstrZ].Name=NName;
   FixedOrders[InstrZ++].Code=NCode;
END

        static void AddImm(char *NName, Byte NCode)
BEGIN
   if (InstrZ>=ImmOrderCnt) exit(255);
   ImmOrders[InstrZ].Name=NName;
   ImmOrders[InstrZ++].Code=NCode;
END

        static void AddReg(char *NName, Byte NCode)
BEGIN
   if (InstrZ>=RegOrderCnt) exit(255);
   RegOrders[InstrZ].Name=NName;
   RegOrders[InstrZ++].Code=NCode;
END

        static void AddMem(char *NName, Byte NCode)
BEGIN
   if (InstrZ>=MemOrderCnt) exit(255);
   MemOrders[InstrZ].Name=NName;
   MemOrders[InstrZ++].Code=NCode;
END

        static void AddJmp(char *NName, Byte NCode)
BEGIN
   if (InstrZ>=JmpOrderCnt) exit(255);
   JmpOrders[InstrZ].Name=NName;
   JmpOrders[InstrZ++].Code=NCode;
END

        static void InitFields(void)
BEGIN
   FixedOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*FixedOrderCnt); InstrZ=0;
   AddFixed("LDE" ,0x40); AddFixed("XAE" ,0x01); AddFixed("ANE" ,0x50);
   AddFixed("ORE" ,0x58); AddFixed("XRE" ,0x60); AddFixed("DAE" ,0x68);
   AddFixed("ADE" ,0x70); AddFixed("CAE" ,0x78); AddFixed("SIO" ,0x19);
   AddFixed("SR"  ,0x1c); AddFixed("SRL" ,0x1d); AddFixed("RR"  ,0x1e);
   AddFixed("RRL" ,0x1f); AddFixed("HALT",0x00); AddFixed("CCL" ,0x02);
   AddFixed("SCL" ,0x03); AddFixed("DINT",0x04); AddFixed("IEN" ,0x05);
   AddFixed("CSA" ,0x06); AddFixed("CAS" ,0x07); AddFixed("NOP" ,0x08);

   ImmOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*ImmOrderCnt); InstrZ=0;
   AddImm("LDI" ,0xc4); AddImm("ANI" ,0xd4); AddImm("ORI" ,0xdc);
   AddImm("XRI" ,0xe4); AddImm("DAI" ,0xec); AddImm("ADI" ,0xf4);
   AddImm("CAI" ,0xfc); AddImm("DLY" ,0x8f);

   RegOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*RegOrderCnt); InstrZ=0;
   AddReg("XPAL",0x30); AddReg("XPAH",0x34); AddReg("XPPC",0x3c);

   MemOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*MemOrderCnt); InstrZ=0;
   AddMem("LD"  ,0xc0); AddMem("ST"  ,0xc8); AddMem("AND" ,0xd0);
   AddMem("OR"  ,0xd8); AddMem("XOR" ,0xe0); AddMem("DAD" ,0xe8);
   AddMem("ADD" ,0xf0); AddMem("CAD" ,0xf8);

   JmpOrders=(FixedOrder *) malloc(sizeof(FixedOrder)*JmpOrderCnt); InstrZ=0;
   AddJmp("JMP" ,0x90); AddJmp("JP"  ,0x94); AddJmp("JZ"  ,0x98);
   AddJmp("JNZ" ,0x9c);
END

        static void DeinitFields(void)
BEGIN
   free(FixedOrders);
   free(ImmOrders);
   free(RegOrders);
   free(MemOrders);
   free(JmpOrders);
END

/*---------------------------------------------------------------------------*/

	static Boolean DecodeReg(char *Asc, Byte *Erg)
BEGIN
   if ((strlen(Asc)!=2) OR (toupper(*Asc)!='P')) return False;

   switch (toupper(Asc[1]))
    BEGIN
     case 'C': *Erg=0; break;
     case '0': case '1': case '2':
     case '3': *Erg=Asc[1]-'0'; break;
     default: return False;
    END

   return True;
END

	static Boolean DecodeAdr(char *Asc, Boolean MayInc, Byte PCDisp, Byte *Arg)
BEGIN
   Integer Disp;
   Word PCVal;
   Boolean OK;
   int l=strlen(Asc);

   if ((l>=4) AND (Asc[l-1]==')') AND (Asc[l-4]=='('))
    BEGIN
     Asc[l-1]='\0'; 
     if (DecodeReg(Asc+l-3,Arg))
      BEGIN
       Asc[l-4]='\0';
       if (*Asc=='@')
        BEGIN
         if (NOT MayInc)
          BEGIN
           WrError(1350); return False;
          END
         strcpy(Asc,Asc+1); *Arg+=4;
        END
       if (strcasecmp(Asc,"E")==0) BAsmCode[1]=0x80;
       else if (*Arg==0)
        BEGIN
         WrXError(1445,Asc+l-3); return False;
        END
       else
        BEGIN
         BAsmCode[1]=EvalIntExpression(Asc,SInt8,&OK);
         if (NOT OK) return False;
        END
       return True;
      END
     else Asc[l-1]=')';
    END

   PCVal=(EProgCounter() & 0xf000)+((EProgCounter()+1) & 0xfff);
   Disp=EvalIntExpression(Asc,UInt16,&OK)-PCDisp-PCVal;
   if (OK)
    if ((NOT SymbolQuestionable) AND ((Disp<-128) OR (Disp>127))) WrError(1370);
    else
     BEGIN
      BAsmCode[1]=Disp & 0xff; *Arg=0; return True;
     END
   return False;
END

/*---------------------------------------------------------------------------*/

	static Boolean DecodePseudo(void)
BEGIN
   return False;
END

        static void ChkPage(void)
BEGIN
   if (((EProgCounter()) & 0xf000)!=((EProgCounter()+CodeLen) & 0xf000)) WrError(250);
END

        static void MakeCode_SCMP(void)
BEGIN
   int z;
   Boolean OK;

   CodeLen=0; DontPrint=False;

   /* zu ignorierendes */

   if (Memo("")) return;

   /* Pseudoanweisungen */

   if (DecodePseudo()) return;

   if (DecodeIntelPseudo(False)) return;

   /* ohne Argument */

   for (z=0; z<FixedOrderCnt; z++)
    if (Memo(FixedOrders[z].Name))
     BEGIN
      if (ArgCnt!=0) WrError(1110);
      else
       BEGIN
        BAsmCode[0]=FixedOrders[z].Code; CodeLen=1;
       END
      return;
     END

   /* immediate */

   for (z=0; z<ImmOrderCnt; z++)
    if (Memo(ImmOrders[z].Name))
     BEGIN
      if (ArgCnt!=1) WrError(1110);
      else
       BEGIN
        BAsmCode[1]=EvalIntExpression(ArgStr[1],Int8,&OK);
        if (OK)
         BEGIN
          BAsmCode[0]=ImmOrders[z].Code; CodeLen=2; ChkPage();
         END
       END
      return;
     END

   /* ein Register */

   for (z=0; z<RegOrderCnt; z++)
    if (Memo(RegOrders[z].Name))
     BEGIN
      if (ArgCnt!=1) WrError(1110);
      else if (NOT DecodeReg(ArgStr[1],BAsmCode+0)) WrXError(1445,ArgStr[1]);
      else
       BEGIN
        BAsmCode[0]+=RegOrders[z].Code; CodeLen=1;
       END
      return;
     END

   /* ein Speicheroperand */

   for (z=0; z<MemOrderCnt; z++)
    if (Memo(MemOrders[z].Name))
     BEGIN
      if (ArgCnt!=1) WrError(1110);
      else if (DecodeAdr(ArgStr[1],True,0,BAsmCode+0))
       BEGIN
        BAsmCode[0]+=MemOrders[z].Code; CodeLen=2; ChkPage();
       END
      return;
     END

   for (z=0; z<JmpOrderCnt; z++)
    if (Memo(JmpOrders[z].Name))
     BEGIN
      if (ArgCnt!=1) WrError(1110);
      else if (DecodeAdr(ArgStr[1],False,1,BAsmCode+0))
       BEGIN
        BAsmCode[0]+=JmpOrders[z].Code; CodeLen=2; ChkPage();
       END
      return;
     END

   if ((Memo("ILD")) OR (Memo("DLD")))
    BEGIN
     if (ArgCnt!=1) WrError(1110);
     else if (DecodeAdr(ArgStr[1],False,0,BAsmCode+0))
      BEGIN
       BAsmCode[0]+=0xa8+(Ord(Memo("DLD")) << 4); CodeLen=2; ChkPage();
      END
     return;
    END

   WrXError(1200,OpPart);
END

        static Boolean IsDef_SCMP(void)
BEGIN
   return False;
END

        static void SwitchFrom_SCMP(void)
BEGIN
   DeinitFields();
END

        static void SwitchTo_SCMP(void)
BEGIN
   TurnWords=False; ConstMode=ConstModeC; SetIsOccupied=False;

   PCSymbol="$"; HeaderID=0x6e; NOPCode=0x08;
   DivideChars=","; HasAttrs=False;

   ValidSegs=1<<SegCode;
   Grans[SegCode]=1; ListGrans[SegCode]=1; SegInits[SegCode]=0;
   SegLimits[SegCode] = 0xffff;

   MakeCode=MakeCode_SCMP; IsDef=IsDef_SCMP;
   SwitchFrom=SwitchFrom_SCMP; InitFields();
END

	void codescmp_init(void)
BEGIN
   CPUSCMP=AddCPU("SC/MP",SwitchTo_SCMP);
END