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