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");
52 return HandleBitmap();
54 return HandleScaledBitmap();
56 return HandleGPUObject();
58 return HandleBranch();
67 return error("unknown OP opcode");
70 static inline void GetSymbolUCFromTokenStream(char * s)
72 strcpy(s, string[tok[1]]);
77 static inline uint64_t CheckFlags(char * s)
79 GetSymbolUCFromTokenStream(s);
81 if (strcmp(scratchbuf, "REFLECT") == 0)
83 else if (strcmp(scratchbuf, "RMW") == 0)
85 else if (strcmp(scratchbuf, "TRANS") == 0)
87 else if (strcmp(scratchbuf, "RELEASE") == 0)
94 // Define a bitmap object
95 // Form: bitmap <data>, <xloc>, <yloc>, <dwidth>, <iwidth>, <iheight>, <bpp>,
96 // <pallete idx>, <flags>, <firstpix>, <pitch>
98 static int HandleBitmap(void)
102 uint64_t iheight = 0;
108 uint64_t firstpix = 0;
115 if ((orgaddr & 0x0F) != 0)
117 warn("bitmap not on double phrase boundary");
119 // Fixup org address (by emitting a NOP)...
122 // We don't need to do a fixup here, because we're guaranteed that the
123 // last object, if it was a scaled bitmap *or* regular bitmap, will
124 // already be on the correct boundary, so we won't have to fix it up.
127 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
130 if (!(eattr & DEFINED))
131 AddFixup(FU_QUAD | FU_OBJDATA, sloc, exprbuf);
132 else if (eattr & TDB)
133 MarkRelocatable(cursect, sloc, (eattr & TDB), MQUAD, NULL);
135 uint64_t dataAddr = eval & 0xFFFFF8;
136 uint64_t linkAddr = (orgaddr + 16) & 0x3FFFF8;
138 uint64_t * vars[10] = { &xpos, &ypos, &dwidth, &iwidth, &iheight, &bpp, &index, 0, &firstpix, &pitch };
140 for(int i=0; i<10; i++)
142 // If there are less than 10 arguments after the data address, use
143 // defaults for the rest of them
144 // N.B.: Should there be a default minimum # of args? Like the first 5?
152 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
155 if (!(eattr & DEFINED))
156 return error("bad expression");
162 // Handle FLAGs. Can have at most four.
163 for(int j=0; j<4; j++)
165 if (tok[0] != SYMBOL)
166 return error("missing REFLECT, RMW, TRANS, and/or RELEASE");
168 flags |= CheckFlags(scratchbuf);
170 // Break out if no more symbols...
171 if (tok[0] != SYMBOL)
179 uint64_t p1 = 0x00 | ((ypos * 2) << 3) | (iheight << 14) | (linkAddr << 21) | (dataAddr << 40);
180 uint64_t p2 = xpos | (bpp << 12) | (pitch << 15) | (dwidth << 18) | (iwidth << 28) | (index << 38) | (flags << 45) | (firstpix << 49);
191 // Define a scaled bitmap object
192 // Form: scbitmap <data>, <xloc>, <yloc>, <dwidth>, <iwidth>, <iheight>,
193 // <xscale>, <yscale>, <remainder>, <bpp>, <pallete idx>,
194 // <flags>, <firstpix>, <pitch>
196 static int HandleScaledBitmap(void)
200 uint64_t iheight = 0;
206 uint64_t firstpix = 0;
210 uint64_t remainder = 0;
216 if ((orgaddr & 0x1F) != 0)
218 warn("scaled bitmap not on quad phrase boundary");
220 // We only have to do a fixup here if the previous object was a bitmap,
221 // as it can live on a 16-byte boundary while scaled bitmaps can't. If
222 // the previous object was a scaled bitmap, it is guaranteed to have
223 // been aligned, therefore no fixup is necessary.
224 if (lastObjType == 0)
226 *fixupPtr.u64 = (orgaddr + 0x18) & 0xFFFFFFE0;
227 AddFixup(FU_QUAD | FU_OBJLINK, lastSloc, fixupExpr);
230 switch (orgaddr & 0x1F)
232 case 0x08: HandleNOP(); // Emit 3 NOPs
233 case 0x10: HandleNOP(); // Emit 2 NOPs
234 case 0x18: HandleNOP(); // Emit 1 NOP
238 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
241 if (!(eattr & DEFINED))
242 AddFixup(FU_QUAD | FU_OBJDATA, sloc, exprbuf);
243 else if (eattr & TDB)
244 MarkRelocatable(cursect, sloc, (eattr & TDB), MQUAD, NULL);
246 uint64_t dataAddr = eval & 0xFFFFF8;
247 uint64_t linkAddr = (orgaddr + 32) & 0x3FFFF8;
249 uint64_t * vars[13] = { &xpos, &ypos, &dwidth, &iwidth, &iheight, &xscale, &yscale, &remainder, &bpp, &index, 0, &firstpix, &pitch };
251 for(int i=0; i<13; i++)
253 // If there are less than 13 arguments after the data address, use
254 // defaults for the rest of them
255 // N.B.: Should there be a default minimum # of args? Like the first 5?
263 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
266 if (!(eattr & DEFINED))
267 return error("bad expression");
269 // Handle 3.5 fixed point nums (if any)...
270 if ((i >= 5) && (i <= 7))
272 if (eattr & FLOAT) // Handle floats
274 // PTR p = { .u64 = &eval }; // C99 \o/
275 PTR p = { (uint8_t *)&eval }; // Meh, it works
276 eval = DoubleToFixedPoint(*p.dp, 3, 5);
279 eval <<= 5; // Otherwise, it's just an int...
286 // Handle FLAGs. Can have at most four.
287 for(int j=0; j<4; j++)
289 if (tok[0] != SYMBOL)
290 return error("missing REFLECT, RMW, TRANS, and/or RELEASE");
292 flags |= CheckFlags(scratchbuf);
294 // Break out if no more symbols...
295 if (tok[0] != SYMBOL)
303 uint64_t p1 = 0x01 | ((ypos * 2) << 3) | (iheight << 14) | (linkAddr << 21) | (dataAddr << 40);
304 uint64_t p2 = xpos | (bpp << 12) | (pitch << 15) | (dwidth << 18) | (iwidth << 28) | (index << 38) | (flags << 45) | (firstpix << 49);
305 uint64_t p3 = (xscale & 0xFF) | (yscale & 0xFF) << 8 | (remainder & 0xFF) << 16;
319 // Form: gpuobj <userdata> (bits 3-63 of this object)
321 static int HandleGPUObject(void)
327 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
330 if (!(eattr & DEFINED))
331 return error("bad expression in data");
335 uint64_t p1 = 0x02 | (eval << 3);
344 // Insert a branch object
345 // Form: branch VC <condition (<, =, >)> <line #>, <link addr>
346 // branch OPFLAG, <link addr>
347 // branch SECHALF, <link addr>
349 static int HandleBranch(void)
351 char missingKeyword[] = "missing VC, OPFLAG, or SECHALF in branch";
358 if (tok[0] != SYMBOL)
359 return error(missingKeyword);
361 GetSymbolUCFromTokenStream(scratchbuf);
363 if (strcmp(scratchbuf, "VC") == 0)
367 case '=': cc = 0; break;
368 case '<': cc = 1; break;
369 case '>': cc = 2; break;
371 return error("missing '<', '>', or '='");
374 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
377 if (!(eattr & DEFINED))
378 return error("bad expression");
380 ypos = (uint32_t)eval;
382 else if (strcmp(scratchbuf, "OPFLAG") == 0)
384 else if (strcmp(scratchbuf, "SECHALF") == 0)
387 return error(missingKeyword);
391 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
394 if (!(eattr & DEFINED))
395 AddFixup(FU_QUAD | FU_OBJLINK, sloc, exprbuf);
399 uint64_t p1 = 0x03 | (cc << 14) | ((ypos * 2) << 3) | ((eval & 0x3FFFF8) << 21);
408 // Insert a stop object
411 static int HandleStop(void)
420 // Insert a phrase sized "NOP" in the object list (psuedo-op)
423 static int HandleNOP(void)
425 uint64_t eval = (orgaddr + 8) & 0x3FFFF8;
426 // This is "branch if VC > 2047". Branch addr is next phrase, so either way
427 // it will pass by this phrase.
428 uint64_t p1 = 0x03 | (2 << 14) | (0x7FF << 3) | (eval << 21);
437 // Insert an unconditional jump in the object list (psuedo-op)
438 // Form: jump <link addr>
440 static int HandleJump(void)
446 if (expr(exprbuf, &eval, &eattr, &esym) != OK)
449 if (!(eattr & DEFINED))
450 AddFixup(FU_QUAD | FU_OBJLINK, sloc, exprbuf);
454 // This is "branch if VC < 2047", which pretty much guarantees the branch.
455 uint64_t p1 = 0x03 | (1 << 14) | (0x7FF << 3) | ((eval & 0x3FFFF8) << 21);