forked from Macroassembler-AS/asl-releases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code17c4x.c
456 lines (401 loc) · 10.5 KB
/
code17c4x.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
/* code17c4x.c */
/*****************************************************************************/
/* AS-Portierung */
/* */
/* Codegenerator PIC17C4x */
/* */
/* Historie: 21.8.1996 Grundsteinlegung */
/* 7. 7.1998 Fix Zugriffe auf CharTransTable wg. signed chars */
/* 18. 8.1998 Bookkeeping-Aufruf in RES */
/* 3. 1.1999 ChkPC-Anpassung */
/* 9. 3.2000 'ambiguous else'-Warnungen beseitigt */
/* */
/*****************************************************************************/
/* $Id: code17c4x.c,v 1.6 2016/09/29 16:43:36 alfred Exp $ */
/*****************************************************************************
* $Log: code17c4x.c,v $
* Revision 1.6 2016/09/29 16:43:36 alfred
* - introduce common DecodeDATA/DecodeRES functions
*
* Revision 1.5 2014/10/03 13:18:15 alfred
* - rework to current style
*
* Revision 1.4 2013/12/21 19:46:51 alfred
* - dynamically resize code buffer
*
* Revision 1.3 2008/11/23 10:39:16 alfred
* - allow strings with NUL characters
*
* Revision 1.2 2005/09/08 17:31:03 alfred
* - add missing include
*
* Revision 1.1 2003/11/06 02:49:19 alfred
* - recreated
*
* Revision 1.3 2003/05/02 21:23:09 alfred
* - strlen() updates
*
* Revision 1.2 2002/08/14 18:43:48 alfred
* - warn null allocation, remove some warnings
*
*****************************************************************************/
#include "stdinc.h"
#include <string.h>
#include <ctype.h>
#include "bpemu.h"
#include "strutil.h"
#include "chunks.h"
#include "asmdef.h"
#include "asmsub.h"
#include "asmitree.h"
#include "asmpars.h"
#include "codepseudo.h"
#include "fourpseudo.h"
#include "codevars.h"
#include "errmsg.h"
#include "code17c4x.h"
/*---------------------------------------------------------------------------*/
static CPUVar CPU17C42;
/*---------------------------------------------------------------------------*/
static void DecodeFixed(Word Code)
{
if (ChkArgCnt(0, 0))
{
CodeLen = 1;
WAsmCode[0] = Code;
}
}
static void DecodeLitt(Word Code)
{
if (ChkArgCnt(1, 1))
{
Boolean OK;
Word AdrWord = EvalStrIntExpression(&ArgStr[1], Int8, &OK);
if (OK)
{
WAsmCode[0] = Code + (AdrWord & 0xff);
CodeLen = 1;
}
}
}
static void DecodeAri(Word Code)
{
Word DefaultDir = (Code >> 7) & 0x100;
Code &= 0x7fff;
if (ChkArgCnt(1, 2))
{
Boolean OK;
Word AdrWord = EvalStrIntExpression(&ArgStr[1], Int8, &OK);
if (OK)
{
ChkSpace(SegData);
WAsmCode[0] = Code + (AdrWord & 0xff);
if (ArgCnt == 1)
{
CodeLen = 1;
WAsmCode[0] += DefaultDir;
}
else if (strcasecmp(ArgStr[2].Str, "W") == 0)
CodeLen = 1;
else if (strcasecmp(ArgStr[2].Str, "F") == 0)
{
CodeLen = 1;
WAsmCode[0] += 0x100;
}
else
{
AdrWord = EvalStrIntExpression(&ArgStr[2], UInt1, &OK);
if (OK)
{
CodeLen = 1;
WAsmCode[0] += (AdrWord << 8);
}
}
}
}
}
static void DecodeBit(Word Code)
{
if (ChkArgCnt(2, 2))
{
Boolean OK;
Word AdrWord = EvalStrIntExpression(&ArgStr[2], UInt3, &OK);
if (OK)
{
WAsmCode[0] = EvalStrIntExpression(&ArgStr[1], Int8, &OK);
if (OK)
{
CodeLen = 1;
WAsmCode[0] += Code + (AdrWord << 8);
ChkSpace(SegData);
}
}
}
}
static void DecodeF(Word Code)
{
if (ChkArgCnt(1, 1))
{
Boolean OK;
Word AdrWord = EvalStrIntExpression(&ArgStr[1], Int8, &OK);
if (OK)
{
CodeLen = 1;
WAsmCode[0] = Code + AdrWord;
ChkSpace(SegData);
}
}
}
static void DecodeMOVFP_MOVPF(Word Code)
{
if (ChkArgCnt(2, 2))
{
tStrComp *pArg1 = (Code & 0x2000) ? &ArgStr[2] : &ArgStr[1],
*pArg2 = (Code & 0x2000) ? &ArgStr[1] : &ArgStr[2];
Boolean OK;
Word AdrWord;
AdrWord = EvalStrIntExpression(pArg1, UInt5, &OK);
if (OK)
{
WAsmCode[0] = EvalStrIntExpression(pArg2, Int8, &OK);
if (OK)
{
WAsmCode[0] = Code + Lo(WAsmCode[0]) + (AdrWord << 8);
CodeLen = 1;
}
}
}
}
static void DecodeTABLRD_TABLWT(Word Code)
{
if (ChkArgCnt(3, 3))
{
Boolean OK;
WAsmCode[0] = Lo(EvalStrIntExpression(&ArgStr[3], Int8, &OK));
if (OK)
{
Word AdrWord = EvalStrIntExpression(&ArgStr[2], UInt1, &OK);
if (OK)
{
WAsmCode[0] += AdrWord << 8;
AdrWord = EvalStrIntExpression(&ArgStr[1], UInt1, &OK);
if (OK)
{
WAsmCode[0] += Code + (AdrWord << 9);
CodeLen = 1;
}
}
}
}
}
static void DecodeTLRD_TLWT(Word Code)
{
if (ChkArgCnt(2, 2))
{
Boolean OK;
WAsmCode[0] = Lo(EvalStrIntExpression(&ArgStr[2], Int8, &OK));
if (OK)
{
Word AdrWord = EvalStrIntExpression(&ArgStr[1], UInt1, &OK);
if (OK)
{
WAsmCode[0] += (AdrWord << 9) + Code;
CodeLen = 1;
}
}
}
return;
}
static void DecodeCALL_GOTO(Word Code)
{
if (ChkArgCnt(1, 1))
{
Boolean OK;
Word AdrWord;
FirstPassUnknown = False;
AdrWord = EvalStrIntExpression(&ArgStr[1], UInt16, &OK);
if (OK && ChkSamePage(ProgCounter(), AdrWord, 13))
{
WAsmCode[0] = Code + (AdrWord & 0x1fff);
CodeLen = 1;
}
}
}
static void DecodeLCALL(Word Code)
{
UNUSED(Code);
if (ChkArgCnt(1, 1))
{
Boolean OK;
Word AdrWord = EvalStrIntExpression(&ArgStr[1], UInt16, &OK);
if (OK)
{
CodeLen = 3;
WAsmCode[0] = 0xb000 + Hi(AdrWord);
WAsmCode[1] = 0x0103;
WAsmCode[2] = 0xb700 + Lo(AdrWord);
}
}
}
static void DecodeSFR(Word Code)
{
UNUSED(Code);
CodeEquate(SegData, 0, 0xff);
}
static void DecodeDATA_17C4x(Word Code)
{
UNUSED(Code);
DecodeDATA(Int16, Int8);
}
static void DecodeZERO(Word Code)
{
Word Size;
Boolean ValOK;
UNUSED(Code);
if (ChkArgCnt(1, 1))
{
FirstPassUnknown = False;
Size = EvalStrIntExpression(&ArgStr[1], Int16, &ValOK);
if (FirstPassUnknown) WrError(ErrNum_FirstPassCalc);
if ((ValOK) && (!FirstPassUnknown))
{
if (SetMaxCodeLen(Size << 1)) WrError(ErrNum_CodeOverflow);
else
{
CodeLen = Size;
memset(WAsmCode, 0, 2 * Size);
}
}
}
}
/*---------------------------------------------------------------------------*/
static void AddFixed(char *NName, Word NCode)
{
AddInstTable(InstTable, NName, NCode, DecodeFixed);
}
static void AddLitt(char *NName, Word NCode)
{
AddInstTable(InstTable, NName, NCode, DecodeLitt);
}
static void AddAri(char *NName, Word NDef, Word NCode)
{
AddInstTable(InstTable, NName, NCode | (NDef << 15), DecodeAri);
}
static void AddBit(char *NName, Word NCode)
{
AddInstTable(InstTable, NName, NCode, DecodeBit);
}
static void AddF(char *NName, Word NCode)
{
AddInstTable(InstTable, NName, NCode, DecodeF);
}
static void InitFields(void)
{
InstTable = CreateInstTable(203);
AddInstTable(InstTable, "MOVFP", 0x6000, DecodeMOVFP_MOVPF);
AddInstTable(InstTable, "MOVPF", 0x4000, DecodeMOVFP_MOVPF);
AddInstTable(InstTable, "TABLRD", 0xa800, DecodeTABLRD_TABLWT);
AddInstTable(InstTable, "TABLWT", 0xac00, DecodeTABLRD_TABLWT);
AddInstTable(InstTable, "TLRD", 0xa000, DecodeTLRD_TLWT);
AddInstTable(InstTable, "TLWT", 0xa400, DecodeTLRD_TLWT);
AddInstTable(InstTable, "CALL", 0xe000, DecodeCALL_GOTO);
AddInstTable(InstTable, "GOTO", 0xc000, DecodeCALL_GOTO);
AddInstTable(InstTable, "LCALL", 0, DecodeLCALL);
AddInstTable(InstTable, "SFR", 0, DecodeSFR);
AddInstTable(InstTable, "RES", 0, DecodeRES);
AddInstTable(InstTable, "DATA", 0, DecodeDATA_17C4x);
AddInstTable(InstTable, "ZERO", 0, DecodeZERO);
AddFixed("RETFIE", 0x0005);
AddFixed("RETURN", 0x0002);
AddFixed("CLRWDT", 0x0004);
AddFixed("NOP" , 0x0000);
AddFixed("SLEEP" , 0x0003);
AddLitt("MOVLB", 0xb800);
AddLitt("ADDLW", 0xb100);
AddLitt("ANDLW", 0xb500);
AddLitt("IORLW", 0xb300);
AddLitt("MOVLW", 0xb000);
AddLitt("SUBLW", 0xb200);
AddLitt("XORLW", 0xb400);
AddLitt("RETLW", 0xb600);
AddAri("ADDWF" , 0, 0x0e00);
AddAri("ADDWFC", 0, 0x1000);
AddAri("ANDWF" , 0, 0x0a00);
AddAri("CLRF" , 1, 0x2800);
AddAri("COMF" , 1, 0x1200);
AddAri("DAW" , 1, 0x2e00);
AddAri("DECF" , 1, 0x0600);
AddAri("INCF" , 1, 0x1400);
AddAri("IORWF" , 0, 0x0800);
AddAri("NEGW" , 1, 0x2c00);
AddAri("RLCF" , 1, 0x1a00);
AddAri("RLNCF" , 1, 0x2200);
AddAri("RRCF" , 1, 0x1800);
AddAri("RRNCF" , 1, 0x2000);
AddAri("SETF" , 1, 0x2a00);
AddAri("SUBWF" , 0, 0x0400);
AddAri("SUBWFB", 0, 0x0200);
AddAri("SWAPF" , 1, 0x1c00);
AddAri("XORWF" , 0, 0x0c00);
AddAri("DECFSZ", 1, 0x1600);
AddAri("DCFSNZ", 1, 0x2600);
AddAri("INCFSZ", 1, 0x1e00);
AddAri("INFSNZ", 1, 0x2400);
AddBit("BCF" , 0x8800);
AddBit("BSF" , 0x8000);
AddBit("BTFSC", 0x9800);
AddBit("BTFSS", 0x9000);
AddBit("BTG" , 0x3800);
AddF("MOVWF" , 0x0100);
AddF("CPFSEQ", 0x3100);
AddF("CPFSGT", 0x3200);
AddF("CPFSLT", 0x3000);
AddF("TSTFSZ", 0x3300);
}
static void DeinitFields(void)
{
DestroyInstTable(InstTable);
}
/*---------------------------------------------------------------------------*/
static void MakeCode_17c4x(void)
{
CodeLen = 0;
DontPrint = False;
/* zu ignorierendes */
if (Memo(""))
return;
if (!LookupInstTable(InstTable, OpPart.Str))
WrStrErrorPos(ErrNum_UnknownInstruction, &OpPart);
}
static Boolean IsDef_17c4x(void)
{
return Memo("SFR");
}
static void SwitchFrom_17c4x(void)
{
DeinitFields();
}
static void SwitchTo_17c4x(void)
{
TurnWords = False;
ConstMode = ConstModeMoto;
SetIsOccupied = False;
PCSymbol = "*";
HeaderID = 0x72;
NOPCode = 0x0000;
DivideChars = ",";
HasAttrs = False;
ValidSegs = (1 << SegCode) + (1 << SegData);
Grans[SegCode] = 2; ListGrans[SegCode] = 2; SegInits[SegCode] = 0;
SegLimits[SegCode] = 0xffff;
Grans[SegData] = 1; ListGrans[SegData] = 1; SegInits[SegData] = 0;
SegLimits[SegData] = 0xff;
MakeCode = MakeCode_17c4x;
IsDef = IsDef_17c4x;
SwitchFrom = SwitchFrom_17c4x;
InitFields();
}
void code17c4x_init(void)
{
CPU17C42 = AddCPU("17C42", SwitchTo_17c4x);
}