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