(c) message in header files and doc mini adjustments.
[rmac] / riscasm.c
1 //
2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // RISCA.C - GPU/DSP Assembler
4 // Copyright (C) 199x Landon Dyer, 2011-2017 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
7 //
8
9 #include "riscasm.h"
10 #include "amode.h"
11 #include "direct.h"
12 #include "error.h"
13 #include "expr.h"
14 #include "mark.h"
15 #include "procln.h"
16 #include "sect.h"
17 #include "token.h"
18
19 #define DEF_MR                          // Declare keyword values
20 #include "risckw.h"                     // Incl. generated risc keywords
21
22 #define DEF_KW                          // Declare keyword values
23 #include "kwtab.h"                      // Incl. generated keyword tables & defs
24
25
26 unsigned altbankok = 0;         // Ok to use alternate register bank
27 unsigned orgactive = 0;         // RISC/6502 org directive active
28 unsigned orgaddr = 0;           // Org'd address
29 unsigned orgwarning = 0;        // Has an ORG warning been issued
30 int lastOpcode = -1;            // Last RISC opcode assembled
31 uint8_t riscImmTokenSeen;       // The '#' (immediate) token was seen
32
33 const char reg_err[] = "missing register R0...R31";
34
35 // Jaguar jump condition names
36 const char condname[MAXINTERNCC][5] = {
37         "NZ", "Z", "NC", "NCNZ", "NCZ", "C", "CNZ", "CZ", "NN", "NNNZ", "NNZ",
38         "N", "N_NZ", "N_Z", "T", "A", "NE", "EQ", "CC", "HS", "HI", "CS", "LO",
39         "PL", "MI", "F"
40 };
41
42 // Jaguar jump condition numbers
43 const char condnumber[] = {
44         1, 2, 4, 5, 6, 8, 9, 10, 20, 21, 22, 24, 25, 26,
45         0, 0, 1, 2, 4, 4, 5,  8,  8, 20, 24, 31
46 };
47
48 const struct opcoderecord roptbl[] = {
49         { MR_ADD,     RI_TWO,    0 },
50         { MR_ADDC,    RI_TWO,    1 },
51         { MR_ADDQ,    RI_NUM_32, 2 },
52         { MR_ADDQT,   RI_NUM_32, 3 },
53         { MR_SUB,     RI_TWO,    4 },
54         { MR_SUBC,    RI_TWO,    5 },
55         { MR_SUBQ,    RI_NUM_32, 6 },
56         { MR_SUBQT,   RI_NUM_32, 7 },
57         { MR_NEG,     RI_ONE,    8 },
58         { MR_AND,     RI_TWO,    9 },
59         { MR_OR,      RI_TWO,    10 },
60         { MR_XOR,     RI_TWO,    11 },
61         { MR_NOT,     RI_ONE,    12 },
62         { MR_BTST,    RI_NUM_31, 13 },
63         { MR_BSET,    RI_NUM_31, 14 },
64         { MR_BCLR,    RI_NUM_31, 15 },
65         { MR_MULT,    RI_TWO,    16 },
66         { MR_IMULT,   RI_TWO,    17 },
67         { MR_IMULTN,  RI_TWO,    18 },
68         { MR_RESMAC,  RI_ONE,    19 },
69         { MR_IMACN,   RI_TWO,    20 },
70         { MR_DIV,     RI_TWO,    21 },
71         { MR_ABS,     RI_ONE,    22 },
72         { MR_SH,      RI_TWO,    23 },
73         { MR_SHLQ,    RI_NUM_32, 24 + SUB32 },
74         { MR_SHRQ,    RI_NUM_32, 25 },
75         { MR_SHA,     RI_TWO,    26 },
76         { MR_SHARQ,   RI_NUM_32, 27 },
77         { MR_ROR,     RI_TWO,    28 },
78         { MR_RORQ,    RI_NUM_32, 29 },
79         { MR_ROLQ,    RI_NUM_32, 29 + SUB32 },
80         { MR_CMP,     RI_TWO,    30 },
81         { MR_CMPQ,    RI_NUM_15, 31 },
82         { MR_SAT8,    RI_ONE,    32 + GPUONLY },
83         { MR_SUBQMOD, RI_NUM_32, 32 + DSPONLY },
84         { MR_SAT16,   RI_ONE,    33 + GPUONLY },
85         { MR_SAT16S,  RI_ONE,    33 + DSPONLY },
86         { MR_MOVEQ,   RI_NUM_31, 35 },
87         { MR_MOVETA,  RI_TWO,    36 },
88         { MR_MOVEFA,  RI_TWO,    37 },
89         { MR_MOVEI,   RI_MOVEI,  38 },
90         { MR_LOADB,   RI_LOADN,  39 },
91         { MR_LOADW,   RI_LOADN,  40 },
92         { MR_LOADP,   RI_LOADN,  42 + GPUONLY },
93         { MR_SAT32S,  RI_ONE,    42 + DSPONLY },
94         { MR_STOREB,  RI_STOREN, 45 },
95         { MR_STOREW,  RI_STOREN, 46 },
96         { MR_STOREP,  RI_STOREN, 48 + GPUONLY },
97         { MR_MIRROR,  RI_ONE,    48 + DSPONLY },
98         { MR_JUMP,    RI_JUMP,   52 },
99         { MR_JR,      RI_JR,     53 },
100         { MR_MMULT,   RI_TWO,    54 },
101         { MR_MTOI,    RI_TWO,    55 },
102         { MR_NORMI,   RI_TWO,    56 },
103         { MR_NOP,     RI_NONE,   57 },
104         { MR_SAT24,   RI_ONE,    62 },
105         { MR_UNPACK,  RI_ONE,    63 + GPUONLY | (0 << 6) },
106         { MR_PACK,    RI_ONE,    63 + GPUONLY | (1 << 6) },
107         { MR_ADDQMOD, RI_NUM_32, 63 + DSPONLY },
108         { MR_MOVE,    RI_MOVE,   0 },
109         { MR_LOAD,    RI_LOAD,   0 },
110         { MR_STORE,   RI_STORE,  0 }
111 };
112
113
114 //
115 // Convert a string to uppercase
116 //
117 void strtoupper(char * s)
118 {
119         while (*s)
120                 *s++ &= 0xDF;
121 }
122
123
124 //
125 // Function to return "malformed expression" error
126 // This is done mainly to remove a bunch of GOTO statements in the parser
127 //
128 static inline int MalformedOpcode(int signal)
129 {
130         char buf[16];
131         sprintf(buf, "%02X", signal);
132         return errors("Malformed opcode [internal $%s]", buf);
133 }
134
135 //
136 // Function to return "Illegal Indexed Register" error
137 // Anyone trying to index something other than R14 or R15
138 //
139 static inline int IllegalIndexedRegister(int reg)
140 {
141     char buf[16];
142     sprintf(buf, "%d", reg - KW_R0);
143     return errors("Attempted index reference with non-indexable register (r%s)", buf);
144 }
145
146 //
147 // Function to return "Illegal Indexed Register" error for EQUR scenarios
148 // Trying to use register value within EQUR that isn't 14 or 15
149 //
150 static inline int IllegalIndexedRegisterEqur(SYM *sy)
151 {
152     //char buf[160];
153     char *buf = NULL;
154     buf = (char *)malloc((strlen(sy->sname) + 7) * sizeof(char));
155     if (NULL != buf) {
156         sprintf(buf, "%s = r%d",sy->sname, sy->svalue);
157         return errors("Attempted index reference with non-indexable register within EQUR (%s)", buf);
158     }
159     return errors("Unable to allocate memory! (IllegalIndexRegisterEqur)", "OOPS");
160 }
161
162 //
163 // Build RISC instruction word
164 //
165 void BuildRISCIntructionWord(unsigned short opcode, int reg1, int reg2)
166 {
167         // Check for absolute address setting
168         if (!orgwarning && !orgactive)
169         {
170                 warn("RISC code generated with no origin defined");
171                 orgwarning = 1;
172         }
173
174         int value = ((opcode & 0x3F) << 10) + ((reg1 & 0x1F) << 5) + (reg2 & 0x1F);
175         D_word(value);
176 //printf("BuildRISC: opcode=$%X, reg1=$%X, reg2=$%X, final=$%04X\n", opcode, reg1, reg2, value);
177 }
178
179
180 //
181 // Get a RISC register
182 //
183 int GetRegister(WORD rattr)
184 {
185         VALUE eval;                                     // Expression value
186         WORD eattr;                                     // Expression attributes
187         SYM * esym;                                     // External symbol involved in expr.
188         TOKEN r_expr[EXPRSIZE];         // Expression token list
189
190         // Evaluate what's in the global "tok" buffer
191         if (expr(r_expr, &eval, &eattr, &esym) != OK)
192                 return ERROR;
193
194         if ((challoc - ch_size) < 4)
195                 chcheck(4L);
196
197         if (!(eattr & DEFINED))
198         {
199                 AddFixup((WORD)(FU_WORD | rattr), sloc, r_expr);
200                 return 0;
201         }
202
203         // If we got a register in range (0-31), return it
204         if ((eval >= 0) && (eval <= 31))
205                 return eval;
206
207         // Otherwise, it's out of range & we flag an error
208         return error(reg_err);
209 }
210
211
212 //
213 // Do RISC code generation
214 //
215 int GenerateRISCCode(int state)
216 {
217         int reg1;                                       // Register 1
218         int reg2;                                       // Register 2
219         int val = 0;                            // Constructed value
220         char scratch[80];
221         SYM * ccsym;
222         SYM * sy;
223         int i, commaFound;
224         TOKEN * t;
225         WORD attrflg;
226         int indexed;                            // Indexed register flag
227
228         VALUE eval;                                     // Expression value
229         WORD eattr;                                     // Expression attributes
230         SYM * esym;                                     // External symbol involved in expr.
231         TOKEN r_expr[EXPRSIZE];         // Expression token list
232
233         // Get opcode parameter and type
234         unsigned short parm = (WORD)(roptbl[state - 3000].parm);
235         unsigned type = roptbl[state - 3000].typ;
236         riscImmTokenSeen = 0;           // Set to "token not seen yet"
237
238         // Detect whether the opcode parmeter passed determines that the opcode is
239         // specific to only one of the RISC processors and ensure it is legal in
240         // the current code section. If not then show error and return.
241         if (((parm & GPUONLY) && rdsp) || ((parm & DSPONLY) && rgpu))
242                 return error("Opcode is not valid in this code section");
243
244         // Process RISC opcode
245         switch (type)
246         {
247         // No operand instructions
248         // NOP (57)
249         case RI_NONE:
250                 BuildRISCIntructionWord(parm, 0, 0);
251                 break;
252
253         // Single operand instructions (Rd)
254         // ABS, MIRROR, NEG, NOT, PACK, RESMAC, SAT8, SAT16, SAT16S, SAT24, SAT32S,
255         // UNPACK
256         case RI_ONE:
257                 reg2 = GetRegister(FU_REGTWO);
258                 at_eol();
259                 BuildRISCIntructionWord(parm, parm >> 6, reg2);
260                 break;
261
262         // Two operand instructions (Rs,Rd)
263         // ADD, ADDC, AND, CMP, DIV, IMACN, IMULT, IMULTN, MOVEFA, MOVETA, MULT,
264         // MMULT, MTOI, NORMI, OR, ROR, SH, SHA, SUB, SUBC, XOR
265         case RI_TWO:
266                 if (parm == 37)
267                         altbankok = 1;                      // MOVEFA
268
269                 reg1 = GetRegister(FU_REGONE);
270                 CHECK_COMMA;
271
272                 if (parm == 36)
273                         altbankok = 1;                      // MOVETA
274
275                 reg2 = GetRegister(FU_REGTWO);
276                 at_eol();
277                 BuildRISCIntructionWord(parm, reg1, reg2);
278                 break;
279
280         // Numeric operand (n,Rd) where n = -16..+15
281         // CMPQ
282         case RI_NUM_15:
283
284         // Numeric operand (n,Rd) where n = 0..31
285         // BCLR, BSET, BTST, MOVEQ
286         case RI_NUM_31:
287
288         // Numeric operand (n,Rd) where n = 1..32
289         // ADDQ, ADDQMOD, ADDQT, SHARQ, SHLQ, SHRQ, SUBQ, SUBQMOD, SUBQT, ROLQ,
290         // RORQ
291         case RI_NUM_32:
292                 switch (type)
293                 {
294                 case RI_NUM_15:
295                         reg1 = -16; reg2 = 15; attrflg = FU_NUM15;
296                         break;
297                 default:
298                 case RI_NUM_31:
299                         reg1 =   0; reg2 = 31; attrflg = FU_NUM31;
300                         break;
301                 case RI_NUM_32:
302                         reg1 =   1; reg2 = 32; attrflg = FU_NUM32;
303                         break;
304                 }
305
306                 if (parm & SUB32)
307                         attrflg |= FU_SUB32;
308
309                 if (*tok != '#')
310                         return MalformedOpcode(0x01);
311
312                 tok++;
313                 riscImmTokenSeen = 1;
314
315                 if (expr(r_expr, &eval, &eattr, &esym) != OK)
316                         return MalformedOpcode(0x02);
317
318                 if ((challoc - ch_size) < 4)
319                         chcheck(4L);
320
321                 if (!(eattr & DEFINED))
322                 {
323                         AddFixup((WORD)(FU_WORD | attrflg), sloc, r_expr);
324                         reg1 = 0;
325                 }
326                 else
327                 {
328                         if ((int)eval < reg1 || (int)eval > reg2)
329                                 return error("constant out of range");
330
331                         if (parm & SUB32)
332                                 reg1 = 32 - eval;
333                         else if (type == RI_NUM_32)
334                                 reg1 = (reg1 == 32 ? 0 : eval);
335                         else
336                                 reg1 = eval;
337                 }
338
339                 CHECK_COMMA;
340                 reg2 = GetRegister(FU_REGTWO);
341                 at_eol();
342                 BuildRISCIntructionWord(parm, reg1, reg2);
343                 break;
344
345         // Move Immediate--n,Rn--n in Second Word
346         case RI_MOVEI:
347                 if (*tok != '#')
348                         return MalformedOpcode(0x03);
349
350                 tok++;
351                 riscImmTokenSeen = 1;
352
353                 // Check for equated register after # and return error if so
354                 if (*tok == SYMBOL)
355                 {
356                         sy = lookup(string[tok[1]], LABEL, 0);
357
358                         if (sy && (sy->sattre & EQUATEDREG))
359                                 return error("equated register in 1st operand of MOVEI instruction");
360                 }
361
362                 if (expr(r_expr, &eval, &eattr, &esym) != OK)
363                         return MalformedOpcode(0x04);
364
365                 if (lastOpcode == RI_JUMP || lastOpcode == RI_JR)
366                 {
367                         if (legacy_flag)
368                         {
369                                 // User doesn't care, emit a NOP to fix
370                                 BuildRISCIntructionWord(57, 0, 0);
371                                 warn("MOVEI following JUMP, inserting NOP to fix your BROKEN CODE");
372                         }
373                         else
374                                 warn("MOVEI immediately follows JUMP");
375                 }
376
377                 if ((challoc - ch_size) < 4)
378                         chcheck(4L);
379
380                 if (!(eattr & DEFINED))
381                 {
382                         AddFixup(FU_LONG | FU_MOVEI, sloc + 2, r_expr);
383                         eval = 0;
384                 }
385                 else
386                 {
387                         if (eattr & TDB)
388 //{
389 //printf("RISCASM: Doing MarkRelocatable for RI_MOVEI (tdb=$%X)...\n", eattr & TDB);
390                                 MarkRelocatable(cursect, sloc + 2, (eattr & TDB), (MLONG | MMOVEI), NULL);
391 //}
392                 }
393
394 //              val = ((eval >> 16) & 0x0000FFFF) | ((eval << 16) & 0xFFFF0000);
395                 val = WORDSWAP32(eval);
396                 CHECK_COMMA;
397                 reg2 = GetRegister(FU_REGTWO);
398                 at_eol();
399                 D_word((((parm & 0x3F) << 10) + reg2));
400                 D_long(val);
401                 break;
402
403         // PC,Rd or Rs,Rd
404         case RI_MOVE:
405                 if (*tok == KW_PC)
406                 {
407                         parm = 51;
408                         reg1 = 0;
409                         tok++;
410                 }
411                 else
412                 {
413                         parm = 34;
414                         reg1 = GetRegister(FU_REGONE);
415                 }
416
417                 CHECK_COMMA;
418                 reg2 = GetRegister(FU_REGTWO);
419                 at_eol();
420                 BuildRISCIntructionWord(parm, reg1, reg2);
421                 break;
422
423         // (Rn),Rn = 41 / (R14/R15+n),Rn = 43/44 / (R14/R15+Rn),Rn = 58/59
424         case RI_LOAD:
425                 indexed = 0;
426                 parm = 41;
427
428                 if (*tok != '(')
429                         return MalformedOpcode(0x05);
430
431                 tok++;
432
433         if ((*(tok + 1) == '+') || (*(tok + 1) == '-')) {
434             // Trying to make indexed call
435             if ((*tok == KW_R14 || *tok == KW_R15)) {
436                 indexed = (*tok - KW_R0);
437             } else {
438                 return IllegalIndexedRegister(*tok);
439             }
440         }
441
442                 if (*tok == SYMBOL)
443                 {
444 //                      sy = lookup((char *)tok[1], LABEL, 0);
445                         sy = lookup(string[tok[1]], LABEL, 0);
446
447                         if (!sy)
448                         {
449                                 error(reg_err);
450                                 return ERROR;
451                         }
452
453                         if (sy->sattre & EQUATEDREG)
454                         {
455                                 if ((*(tok + 2) == '+') || (*(tok + 2) == '-')) {
456                                     if ((sy->svalue & 0x1F) == 14 || (sy->svalue & 0x1F) == 15) {
457                                         indexed = (sy->svalue & 0x1F);
458                         tok++;
459                                     } else {
460                                         return IllegalIndexedRegisterEqur(sy);
461                                     }
462                                 }
463                         }
464                 }
465
466                 if (!indexed)
467                 {
468                         reg1 = GetRegister(FU_REGONE);
469                 }
470                 else
471                 {
472                         reg1 = indexed;
473                         indexed = 0;
474                         tok++;
475
476                         if (*tok == '+')
477                         {
478                                 parm = (WORD)(reg1 - 14 + 58);
479                                 tok++;
480
481                                 if (*tok >= KW_R0 && *tok <= KW_R31)
482                                         indexed = 1;
483
484                                 if (*tok == SYMBOL)
485                                 {
486 //                                      sy = lookup((char *)tok[1], LABEL, 0);
487                                         sy = lookup(string[tok[1]], LABEL, 0);
488
489                                         if (!sy)
490                                         {
491                                                 error(reg_err);
492                                                 return ERROR;
493                                         }
494
495                                         if (sy->sattre & EQUATEDREG)
496                                                 indexed = 1;
497                                 }
498
499                                 if (indexed)
500                                 {
501                                         reg1 = GetRegister(FU_REGONE);
502                                 }
503                                 else
504                                 {
505                                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
506                                                 return MalformedOpcode(0x06);
507
508                                         if ((challoc - ch_size) < 4)
509                                                 chcheck(4L);
510
511                                         if (!(eattr & DEFINED))
512                                                 return error("constant expected after '+'");
513
514                                         reg1 = eval;
515
516                                         if (reg1 == 0)
517                                         {
518                                                 reg1 = 14 + (parm - 58);
519                                                 parm = 41;
520                                                 warn("NULL offset in LOAD ignored");
521                                         }
522                                         else
523                                         {
524                                                 if (reg1 < 1 || reg1 > 32)
525                                                         return error("constant in LOAD out of range");
526
527                                                 if (reg1 == 32)
528                                                         reg1 = 0;
529
530                                                 parm = (WORD)(parm - 58 + 43);
531                                         }
532                                 }
533                         }
534                         else
535                         {
536                                 reg1 = GetRegister(FU_REGONE);
537                         }
538                 }
539
540                 if (*tok != ')')
541                         return MalformedOpcode(0x07);
542
543                 tok++;
544                 CHECK_COMMA;
545                 reg2 = GetRegister(FU_REGTWO);
546                 at_eol();
547                 BuildRISCIntructionWord(parm, reg1, reg2);
548                 break;
549
550         // Rn,(Rn) = 47 / Rn,(R14/R15+n) = 49/50 / Rn,(R14/R15+Rn) = 60/61
551         case RI_STORE:
552                 parm = 47;
553                 reg1 = GetRegister(FU_REGONE);
554                 CHECK_COMMA;
555
556                 if (*tok != '(')
557                         return MalformedOpcode(0x08);
558
559                 tok++;
560                 indexed = 0;
561
562                 if ((*tok == KW_R14 || *tok == KW_R15) && (*(tok + 1) != ')'))
563                         indexed = (*tok - KW_R0);
564
565                 if (*tok == SYMBOL)
566                 {
567                         sy = lookup(string[tok[1]], LABEL, 0);
568
569                         if (!sy)
570                         {
571                                 error(reg_err);
572                                 return ERROR;
573                         }
574
575                         if (sy->sattre & EQUATEDREG)
576                         {
577                                 if (((sy->svalue & 0x1F) == 14 || (sy->svalue & 0x1F) == 15)
578                                         && (*(tok + 2) != ')'))
579                                 {
580                                         indexed = (sy->svalue & 0x1F);
581                                         tok++;
582                                 }
583                         }
584                 }
585
586                 if (!indexed)
587                 {
588                         reg2 = GetRegister(FU_REGTWO);
589                 }
590                 else
591                 {
592                         reg2 = indexed;
593                         indexed = 0;
594                         tok++;
595
596                         if (*tok == '+')
597                         {
598                                 parm = (WORD)(reg2 - 14 + 60);
599                                 tok++;
600
601                                 if (*tok >= KW_R0 && *tok <= KW_R31)
602                                         indexed = 1;
603
604                                 if (*tok == SYMBOL)
605                                 {
606                                         sy = lookup(string[tok[1]], LABEL, 0);
607
608                                         if (!sy)
609                                         {
610                                                 error(reg_err);
611                                                 return ERROR;
612                                         }
613
614                                         if (sy->sattre & EQUATEDREG)
615                                                 indexed = 1;
616                                 }
617
618                                 if (indexed)
619                                 {
620                                         reg2 = GetRegister(FU_REGTWO);
621                                 }
622                                 else
623                                 {
624                                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
625                                                 return MalformedOpcode(0x09);
626
627                                         if ((challoc - ch_size) < 4)
628                                                 chcheck(4L);
629
630                                         if (!(eattr & DEFINED))
631                                         {
632                                                 AddFixup(FU_WORD | FU_REGTWO, sloc, r_expr);
633                                                 reg2 = 0;
634                                         }
635                                         else
636                                         {
637                                                 reg2 = eval;
638
639                                                 if (reg2 == 0)
640                                                 {
641                                                         reg2 = 14 + (parm - 60);
642                                                         parm = 47;
643                                                         warn("NULL offset in STORE ignored");
644                                                 }
645                                                 else
646                                                 {
647                                                         if (reg2 < 1 || reg2 > 32)
648                                                                 return error("constant in STORE out of range");
649
650                                                         if (reg2 == 32)
651                                                                 reg2 = 0;
652
653                                                         parm = (WORD)(parm - 60 + 49);
654                                                 }
655                                         }
656                                 }
657                         }
658                         else
659                         {
660                                 reg2 = GetRegister(FU_REGTWO);
661                         }
662                 }
663
664                 if (*tok != ')')
665                         return MalformedOpcode(0x0A);
666
667                 tok++;
668                 at_eol();
669                 BuildRISCIntructionWord(parm, reg2, reg1);
670                 break;
671
672         // LOADB/LOADP/LOADW (Rn),Rn
673         case RI_LOADN:
674                 if (*tok != '(')
675                         return MalformedOpcode(0x0B);
676
677                 tok++;
678                 reg1 = GetRegister(FU_REGONE);
679
680                 if (*tok != ')')
681                         return MalformedOpcode(0x0C);
682
683                 tok++;
684                 CHECK_COMMA;
685                 reg2 = GetRegister(FU_REGTWO);
686                 at_eol();
687                 BuildRISCIntructionWord(parm, reg1, reg2);
688                 break;
689
690         // STOREB/STOREP/STOREW Rn,(Rn)
691         case RI_STOREN:
692                 reg1 = GetRegister(FU_REGONE);
693                 CHECK_COMMA;
694
695                 if (*tok != '(')
696                         return MalformedOpcode(0x0D);
697
698                 tok++;
699                 reg2 = GetRegister(FU_REGTWO);
700
701                 if (*tok != ')')
702                         return MalformedOpcode(0x0E);
703
704                 tok++;
705                 at_eol();
706                 BuildRISCIntructionWord(parm, reg2, reg1);
707                 break;
708
709         // Jump Relative - cc,n - n=-16..+15 words, reg2=cc
710         case RI_JR:
711
712         // Jump Absolute - cc,(Rs) - reg2=cc
713         case RI_JUMP:
714                 // Check to see if there is a comma in the token string. If not then
715                 // the JR or JUMP should default to 0, Jump Always
716                 commaFound = 0;
717
718                 for(t=tok; *t!=EOL; t++)
719                 {
720                         if (*t == ',')
721                         {
722                                 commaFound = 1;
723                                 break;
724                         }
725                 }
726
727                 if (commaFound)
728                 {
729                         if (*tok == CONST)
730                         {
731                                 // CC using a constant number
732                                 tok++;
733                                 val = *tok;
734                                 tok++;
735                                 CHECK_COMMA;
736                         }
737                         else if (*tok == SYMBOL)
738                         {
739                                 val = 99;
740 //                              strcpy(scratch, (char *)tok[1]);
741                                 strcpy(scratch, string[tok[1]]);
742                                 strtoupper(scratch);
743
744                                 for(i=0; i<MAXINTERNCC; i++)
745                                 {
746                                         // Look for the condition code & break if found
747                                         if (strcmp(condname[i], scratch) == 0)
748                                         {
749                                                 val = condnumber[i];
750                                                 break;
751                                         }
752                                 }
753
754                                 // Standard CC was not found, look for an equated one
755                                 if (val == 99)
756                                 {
757 //                                      ccsym = lookup((char *)tok[1], LABEL, 0);
758                                         ccsym = lookup(string[tok[1]], LABEL, 0);
759
760                                         if (ccsym && (ccsym->sattre & EQUATEDCC) && !(ccsym->sattre & UNDEF_CC))
761                                         {
762                                                 val = ccsym->svalue;
763                                         }
764                                         else
765                                                 return error("unknown condition code");
766                                 }
767
768                                 tok += 2;
769                                 CHECK_COMMA;
770                         }
771                         else if (*tok == '(')
772                         {
773                                 // Set CC to "Jump Always"
774                                 val = 0;
775                         }
776                 }
777                 else
778                 {
779                         // Set CC to "Jump Always"
780                         val = 0;
781                 }
782
783                 if (val < 0 || val > 31)
784                         return error("condition constant out of range");
785
786                 // Store condition code
787                 reg1 = val;
788
789                 if (type == RI_JR)
790                 {
791                         // JR cc,n
792                         if (expr(r_expr, &eval, &eattr, &esym) != OK)
793                                 return MalformedOpcode(0x0F);
794
795                         if ((challoc - ch_size) < 4)
796                                 chcheck(4L);
797
798                         if (!(eattr & DEFINED))
799                         {
800                                 AddFixup(FU_WORD | FU_JR, sloc, r_expr);
801                                 reg2 = 0;
802                         }
803                         else
804                         {
805                                 reg2 = ((int)(eval - ((orgactive ? orgaddr : sloc) + 2))) / 2;
806
807                                 if ((reg2 < -16) || (reg2 > 15))
808                                         error("PC relative overflow");
809                         }
810
811                         BuildRISCIntructionWord(parm, reg2, reg1);
812                 }
813                 else
814                 {
815                         // JUMP cc, (Rn)
816                         if (*tok != '(')
817                                 return MalformedOpcode(0x10);
818
819                         tok++;
820                         reg2 = GetRegister(FU_REGTWO);
821
822                         if (*tok != ')')
823                                 return MalformedOpcode(0x11);
824
825                         tok++;
826                         at_eol();
827                         BuildRISCIntructionWord(parm, reg2, reg1);
828                 }
829
830                 break;
831
832         // Should never get here :-D
833         default:
834                 return error("Unknown RISC opcode type");
835         }
836
837         lastOpcode = type;
838         return 0;
839 }
840