2 // Jaguar Object Processor assembler
5 // (C) 2019 Underground Software
21 #include "opkw.h" // For MO_* macros
23 // Function prototypes
24 static int HandleBitmap(void);
25 static int HandleScaledBitmap(void);
26 static int HandleGPUObject(void);
27 static int HandleBranch(void);
28 static int HandleStop(void);
29 static int HandleNOP(void);
30 static int HandleJump(void);
33 static uint8_t lastObjType;
34 static uint32_t lastSloc;
35 static char scratchbuf[4096];
36 static TOKEN fixupExpr[4] = { CONST, 0, 0, ENDEXPR };
37 //static PTR fixupPtr = { .tk = (fixupExpr + 1) }; // C99 \o/
38 static PTR fixupPtr = { (uint8_t *)(fixupExpr + 1) }; // meh, it works
41 // The main Object Processor assembler. Basically just calls the sub functions
42 // to generate the appropriate code.
44 int GenerateOPCode(int state)
47 return error("opcode only valid in OP mode");
49 // It's OK to call this before validating state. If the state is invalid, an
50 // error will be generated and no object file will be produced, so it
51 // doesn't matter if the line number symbols are a little off.
57 return HandleBitmap();
59 return HandleScaledBitmap();
61 return HandleGPUObject();
63 return HandleBranch();
72 return error("unknown OP opcode");
75 static inline void GetSymbolUCFromTokenStream(char * s)
77 strcpy(s, string[tok[1]]);
82 static inline uint64_t CheckFlags(char * s)
84 GetSymbolUCFromTokenStream(s);
86 if (strcmp(scratchbuf, "REFLECT") == 0)
88 else if (strcmp(scratchbuf, "RMW") == 0)
90 else if (strcmp(scratchbuf, "TRANS") == 0)
92 else if (strcmp(scratchbuf, "RELEASE") == 0)
99 // Define a bitmap object
100 // Form: bitmap <data>, <xloc>, <yloc>, <dwidth>, <iwidth>, <iheight>, <bpp>,
101 // <pallete idx>, <flags>, <firstpix>, <pitch>
103 static int HandleBitmap(void)
107 uint64_t iheight = 0;
113 uint64_t firstpix = 0;
120 if ((orgaddr & 0x0F) != 0)
122 warn("bitmap not on double phrase boundary");
124 // Fixup org address (by emitting a NOP)...
127 // We don't need to do a fixup here, because we're guaranteed that the
128 // last object, if it was a scaled bitmap *or* regular bitmap, will
129 // already be on the correct boundary, so we won't have to fix it up.
132 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
135 if (!(eattr & DEFINED))
136 AddFixup(FU_QUAD | FU_OBJDATA, sloc, exprbuf);
137 else if (eattr & TDB)
138 MarkRelocatable(cursect, sloc, (eattr & TDB), MQUAD, NULL);
140 uint64_t dataAddr = eval & 0xFFFFF8;
141 uint64_t linkAddr = (orgaddr + 16) & 0x3FFFF8;
143 uint64_t * vars[10] = { &xpos, &ypos, &dwidth, &iwidth, &iheight, &bpp, &index, 0, &firstpix, &pitch };
145 for(int i=0; i<10; i++)
147 // If there are less than 10 arguments after the data address, use
148 // defaults for the rest of them
149 // N.B.: Should there be a default minimum # of args? Like the first 5?
157 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
160 if (!(eattr & DEFINED))
161 return error("bad expression");
167 // Handle FLAGs. Can have at most four.
168 for(int j=0; j<4; j++)
170 if (tok[0] != SYMBOL)
171 return error("missing REFLECT, RMW, TRANS, and/or RELEASE");
173 flags |= CheckFlags(scratchbuf);
175 // Break out if no more symbols...
176 if (tok[0] != SYMBOL)
184 uint64_t p1 = 0x00 | ((ypos * 2) << 3) | (iheight << 14) | (linkAddr << 21) | (dataAddr << 40);
185 uint64_t p2 = xpos | (bpp << 12) | (pitch << 15) | (dwidth << 18) | (iwidth << 28) | (index << 38) | (flags << 45) | (firstpix << 49);
196 // Define a scaled bitmap object
197 // Form: scbitmap <data>, <xloc>, <yloc>, <dwidth>, <iwidth>, <iheight>,
198 // <xscale>, <yscale>, <remainder>, <bpp>, <pallete idx>,
199 // <flags>, <firstpix>, <pitch>
201 static int HandleScaledBitmap(void)
205 uint64_t iheight = 0;
211 uint64_t firstpix = 0;
215 uint64_t remainder = 0;
221 if ((orgaddr & 0x1F) != 0)
223 warn("scaled bitmap not on quad phrase boundary");
225 // We only have to do a fixup here if the previous object was a bitmap,
226 // as it can live on a 16-byte boundary while scaled bitmaps can't. If
227 // the previous object was a scaled bitmap, it is guaranteed to have
228 // been aligned, therefore no fixup is necessary.
229 if (lastObjType == 0)
231 *fixupPtr.u64 = (orgaddr + 0x18) & 0xFFFFFFE0;
232 AddFixup(FU_QUAD | FU_OBJLINK, lastSloc, fixupExpr);
235 switch (orgaddr & 0x1F)
237 case 0x08: HandleNOP(); // Emit 3 NOPs
238 case 0x10: HandleNOP(); // Emit 2 NOPs
239 case 0x18: HandleNOP(); // Emit 1 NOP
243 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
246 if (!(eattr & DEFINED))
247 AddFixup(FU_QUAD | FU_OBJDATA, sloc, exprbuf);
248 else if (eattr & TDB)
249 MarkRelocatable(cursect, sloc, (eattr & TDB), MQUAD, NULL);
251 uint64_t dataAddr = eval & 0xFFFFF8;
252 uint64_t linkAddr = (orgaddr + 32) & 0x3FFFF8;
254 uint64_t * vars[13] = { &xpos, &ypos, &dwidth, &iwidth, &iheight, &xscale, &yscale, &remainder, &bpp, &index, 0, &firstpix, &pitch };
256 for(int i=0; i<13; i++)
258 // If there are less than 13 arguments after the data address, use
259 // defaults for the rest of them
260 // N.B.: Should there be a default minimum # of args? Like the first 5?
268 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
271 if (!(eattr & DEFINED))
272 return error("bad expression");
274 // Handle 3.5 fixed point nums (if any)...
275 if ((i >= 5) && (i <= 7))
277 if (eattr & FLOAT) // Handle floats
279 // PTR p = { .u64 = &eval }; // C99 \o/
280 PTR p = { (uint8_t *)&eval }; // Meh, it works
281 eval = DoubleToFixedPoint(*p.dp, 3, 5);
284 eval <<= 5; // Otherwise, it's just an int...
291 // Handle FLAGs. Can have at most four.
292 for(int j=0; j<4; j++)
294 if (tok[0] != SYMBOL)
295 return error("missing REFLECT, RMW, TRANS, and/or RELEASE");
297 flags |= CheckFlags(scratchbuf);
299 // Break out if no more symbols...
300 if (tok[0] != SYMBOL)
308 uint64_t p1 = 0x01 | ((ypos * 2) << 3) | (iheight << 14) | (linkAddr << 21) | (dataAddr << 40);
309 uint64_t p2 = xpos | (bpp << 12) | (pitch << 15) | (dwidth << 18) | (iwidth << 28) | (index << 38) | (flags << 45) | (firstpix << 49);
310 uint64_t p3 = (xscale & 0xFF) | (yscale & 0xFF) << 8 | (remainder & 0xFF) << 16;
324 // Form: gpuobj <userdata> (bits 3-63 of this object)
326 static int HandleGPUObject(void)
332 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
335 if (!(eattr & DEFINED))
336 return error("bad expression in data");
340 uint64_t p1 = 0x02 | (eval << 3);
349 // Insert a branch object
350 // Form: branch VC <condition (<, =, >)> <line #>, <link addr>
351 // branch OPFLAG, <link addr>
352 // branch SECHALF, <link addr>
354 static int HandleBranch(void)
356 char missingKeyword[] = "missing VC, OPFLAG, or SECHALF in branch";
363 if (tok[0] != SYMBOL)
364 return error(missingKeyword);
366 GetSymbolUCFromTokenStream(scratchbuf);
368 if (strcmp(scratchbuf, "VC") == 0)
372 case '=': cc = 0; break;
373 case '<': cc = 1; break;
374 case '>': cc = 2; break;
376 return error("missing '<', '>', or '='");
379 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
382 if (!(eattr & DEFINED))
383 return error("bad expression");
385 ypos = (uint32_t)eval;
387 else if (strcmp(scratchbuf, "OPFLAG") == 0)
389 else if (strcmp(scratchbuf, "SECHALF") == 0)
392 return error(missingKeyword);
396 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
399 if (!(eattr & DEFINED))
400 AddFixup(FU_QUAD | FU_OBJLINK, sloc, exprbuf);
404 uint64_t p1 = 0x03 | (cc << 14) | ((ypos * 2) << 3) | ((eval & 0x3FFFF8) << 21);
413 // Insert a stop object
416 static int HandleStop(void)
425 // Insert a phrase sized "NOP" in the object list (psuedo-op)
428 static int HandleNOP(void)
430 uint64_t eval = (orgaddr + 8) & 0x3FFFF8;
431 // This is "branch if VC > 2047". Branch addr is next phrase, so either way
432 // it will pass by this phrase.
433 uint64_t p1 = 0x03 | (2 << 14) | (0x7FF << 3) | (eval << 21);
442 // Insert an unconditional jump in the object list (psuedo-op)
443 // Form: jump <link addr>
445 static int HandleJump(void)
451 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
454 if (!(eattr & DEFINED))
455 AddFixup(FU_QUAD | FU_OBJLINK, sloc, exprbuf);
459 // This is "branch if VC < 2047", which pretty much guarantees the branch.
460 uint64_t p1 = 0x03 | (1 << 14) | (0x7FF << 3) | ((eval & 0x3FFFF8) << 21);