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