asmcore 160 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* Copyright 2013 Castle Technology Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Common code for dealing with code generation and pixel format conversion

   The code here is used by both PutScaled and SprTrans

 */

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "swis.h"
#include "commondefs.h"
#include "jinclude.h"
#include "putscaled.h"
#include "Global/Sprite.h"
#include "Global/VduExt.h"
31
#include "Interface/BlendTable.h"
32 33 34 35 36 37 38 39 40 41

/**************************************************************************
*                                                                         *
*    Macros.                                                              *
*                                                                         *
**************************************************************************/

#define SOURCE_32_BIT  (wp->save_inlog2bpp == 5)
#define SOURCE_16_BIT  (wp->save_inlog2bpp == 4)
#define SOURCED_16_BIT (wp->save_inlog2bpc == 4) /* like SOURCE_16_BIT but includes 16-bit double-pixels */
42 43 44 45 46 47
#define SOURCE_MASK    (ws->masktype != MaskType_None)
#define SOURCE_OLDMASK (ws->masktype == MaskType_Old)
#define SOURCE_BPPMASK (ws->masktype == MaskType_1bpp)
#define SOURCE_ALPHAMASK (ws->masktype == MaskType_8bpp)
#define SOURCE_TRICKYMASK (ws->masktype > MaskType_Old)
#define SOURCE_TABLE   (wp->ColourTTR != 0)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#define DPIXEL_INPUT   (wp->save_inlog2bpp != wp->save_inlog2bpc)
#define DPIXEL_OUTPUT  (wp->BPP != wp->BPC)

#define PLOTMASK       ((wp->spritecode & 255) == SpriteReason_PlotMaskScaled)
#define TRANSMASK       ((wp->spritecode & 255) == SpriteReason_PlotMaskTransformed)
#define ISTRANS        (TRANSMASK || ((wp->spritecode & 255) == SpriteReason_PutSpriteTransformed))

#define DEST_32_BIT    (wp->BPP == 32)
#define DEST_16_BIT    (wp->BPP == 16)
#define DEST_1_BIT     (wp->BPC == 1)
#define DESTD_16_BIT   (wp->BPC == 16) /* like DEST_16_BIT but includes 16-bit double-pixels */

/**************************************************************************
*                                                                         *
*    Low-level debugging output.                                          *
*                                                                         *
**************************************************************************/

#ifdef DEBUG
#define tracef(args)     do_sprintf(0, args)
69
#define assert(x, y)     do_assert(__FILE__, __LINE__, x, y, #x)
70 71 72 73 74
#define newline()        tracef("\n");
#define comment(ws,text) do_comment(text)
#define IFDEBUG(a) a
#include "tracing.c"
#define COMPONENT_NAME(i) ((i==0?"red":(i==1?"green":(i==2?"blue":"alpha"))))
75
#define EXIT_OSERROR(e)  { tracef("error %08x %s at %s %d\n" _ e->errnum _ e->errmess _ __FILE__ _ __LINE__); exit_oserror(e); }
76 77 78 79 80 81
#else
#define tracef(args)     /* Nothing */
#define assert(x, y)     {if (!(x)) exit_erl(y, __LINE__);}
#define newline()        /* Nothing */
#define comment(ws,text) /* Nothing */
#define IFDEBUG(a)       /* Nothing */
82
#define EXIT_OSERROR     exit_oserror
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
#endif

/* Make a string constant unique to avoid Norcroft messing it up
   This is something we can fall back to if we decide calling __RelocCode is a
   bad thing (but it would involve touching lots of strings to add the call, so
   for now laziness wins) */
#define UQ(X) (X "\000" RUNMACRO(__LINE__,TOSTRING))
#define TOSTRING(X) #X
#define RUNMACRO(X,Y) Y(X)

/**************************************************************************
*                                                                         *
*    C Workspace declarations.                                            *
*                                                                         *
**************************************************************************/

/* Code buffers */
#define NBUFFERS 8       /* Number of code buffers */
#define BUFSIZE 256      /* words per buffer */
typedef struct
{
  int key_word;              /* descriptor for this code, or -1 if empty */
  int xadd;                  /* precise scale factors compiled into this code */
  int xdiv;
  int yadd;
  int ydiv;
  int outoffset;             /* output row offset compiled into this code */
  int code[BUFSIZE];         /* the code itself */
} code_buffer;
#define FOR_EACH_BUFFER(ptr) for (ptr = &ws->buffers[0]; ptr < &ws->buffers[NBUFFERS]; ptr++)

/* Labels - there's one of these for each label in the source we generate. */
typedef struct
{
  int *def;          /* where the label is, or 0 if not yet defined. */
  int *ref;          /* a reference to the label, to be filled in when it's defined. */
#ifdef DEBUG
  char *name;        /* textual name of the label - same as field name */
#endif
} label;

/* Each label must be added as a field to this structure. */
typedef struct
{
  #define FIRST_LABEL loop_y_repeat
  label loop_y_repeat;
#ifdef TESTDEBUG
  label test1;
  label test2;
#endif
  label loop_x_enter;
  label loop_x_repeat;
  label loop_x_exit;
  label l_masked;
  label loop_put_pixel_repeat;
  label loop_put_masked_repeat;
  label y_loop;
  label y_loop_enter;
  label y_loop_exit;
  label loop_delay;

  label x_evenstart;
  label x_oddmask;
  label x_aligned_loop;
  label x_aligned_enter;
  label x_alignmask1;
  label x_alignmask2;
  label x_misaligned;
  label x_misaligned_loop;
  label x_misaligned_enter;
  label x_misalignmask1;
  label x_misalignmask2;
  label x_2atatime_exit;
  label x_lastmask;
  label loop_x_exit1;
  label loop_x_exitskip;
  label loop1;
  label loop2;
  label plot_loopa;
  label plot_loop1;
  label plot_loop1a;
  label plot_loop1b;
  label plot_loop1c;
  label plot_loop2;
  label plot_loop3;
  label plot_loop4;
  label plot_loop4a;
  label plot_loop4b;
  label plot_loop4c;

173 174 175 176
  label blend_nodestalpha;
  label translate_noalpha;
  label translate_noalpha2;

177 178 179 180 181 182 183 184 185 186
  label last;
  #define LAST_LABEL last
  /* If you add a label, add giving it a name in check_workspace */
} labels_rec;
#define FOR_EACH_LABEL(ptr) for (ptr = &ws->labels.FIRST_LABEL; ptr <= &ws->labels.LAST_LABEL; ptr++)
#define L(name) (&(ws->labels.name))

/* Register names - one for each register name (the register numbers are allocated at compile time) */
typedef struct
{
187 188 189
  int regno;     /* the physical register number, negative if not allocated/paged out */
  int flags;     /* usage flags */
  int spindex;   /* index into stack frame where register is saved, -1 if not saved */
190 191
#ifdef DEBUG
  char *name;    /* the name, for trace output */
192
  char *describe;/* description, for trace output */
193 194 195
#endif
} regname;

196 197 198 199 200 201 202 203 204 205 206 207
#define REGFLAG_TEMPORARY  0x01 /* Only used for temporary calculations, doesn't need saving/restoring */
#define REGFLAG_CONSTANT   0x02 /* Constant value - once initialised, doesn't need saving */
#define REGFLAG_GLOBAL     0x04 /* Register must always be available  */
#define REGFLAG_PERPIXEL   0x08 /* Register needed for per-pixel block */
#define REGFLAG_XLOOP      0x10 /* Register needed for x-loop block */
#define REGFLAG_YLOOP      0x20 /* Register needed for y-loop block */
#define REGFLAG_INPUT      0x40 /* Register needed for input block */
#define REGFLAG_USED       0x80 /* Dummy flag for some global regs (sp, lr, etc.) */
#define REGFLAG_XLOOPVAR  0x100 /* Restore initial value at end of x loop */

#define REGFLAG_USAGE_MASK 0x7c /* All the interesting usage flags */

208 209 210 211 212 213 214 215 216
/* Each register name must be added as a field to this structure. */
typedef struct
{
  #define FIRST_REGISTER r_pixel

  /* Common registers */
  regname r_pixel;         /* In/out pixel value */
  regname r_temp1;         /* Temp regs for pixel format conversion */
  regname r_temp2;
217 218
  regname r_expansionmask1;/* Constant for BPP expansion */
  regname r_expansionmask2;/* Secondary mask for rare situations */
219 220
  regname r_oditheradd;    /* Current dither pattern */
  regname r_table;         /* Palette/colour translation table ptr */
221 222 223 224 225
  regname r_alpha;         /* Computed alpha for sprite blending */
  regname r_translucency;  /* User-supplied translucency for sprite blending, inverted to make alpha */
  regname r_blendtable;    /* Pointer to blend table(s) */
  regname r_screenpalette; /* 8bpp -> 15bpp LUT from InverseTable */
  regname r_inversetable;  /* 15bpp -> 8bpp LUT from InverseTable */
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

  /* PutScaled registers */
  regname r_inptr;
  regname r_inshift;
  regname r_inword;
  regname r_maskinptr;
  regname r_maskinword;
  regname r_maskinshift;
  regname r_masko;
  regname r_blockroutine;
  regname r_ecfindex;
  regname r_bgcolour;
  regname r_fetchroutine;
  regname r_outptr;
  regname r_outword;
  regname r_outshift;
  regname r_outmask;
  regname r_xsize;
  regname r_xcount;
  regname r_ysize;
  regname r_ycount;
  regname r_inoffset;
  regname r_maskinoffset;
  regname r_in_pixmask;    /* only used by 2-at-a-time loop */

  /* SprTrans registers */
  regname r_xsize_spr_left;
  regname r_X;
  regname r_Y;
  regname r_inc_X_x;
  regname r_inc_Y_x;
  regname r_byte_width;
  regname r_spr_height_right;
  regname r_out_x;

  /* Generic registers */
  regname wp;
  regname sp;
  regname lr;
  regname pc;
  #define LAST_REGISTER pc
} regnames_rec;
#define FOR_EACH_REGISTER_NAME(ptr) for (ptr = &ws->regnames.FIRST_REGISTER; ptr <= &ws->regnames.LAST_REGISTER; ptr++)

270 271
#define R(reg) rr(&ws->regnames.reg)
static int rr(const regname *r)
272 273
{
  /* Assert that the register is at least set */
274 275 276
  IFDEBUG(if(r->regno & 0x80000000) tracef("%x %s\n" _ r->regno _ r->name);)
  assert(!(r->regno & 0x80000000), ERROR_FATAL);
  return r->regno;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}

/* Must be kept in sync with corresponding definitions in Sources.PutScaled! */
typedef enum
{
  /* This block is assumed to match the mode log2bpp */
  PixelFormat_1bpp=0,
  PixelFormat_2bpp=1,
  PixelFormat_4bpp=2,
  PixelFormat_8bpp=3,

  /* I can't remember if this needs to be in the middle! */
  PixelFormat_24bpp_Grey=4,

  /* This block is assumed to be in order of increasing bit count */
  PixelFormat_12bpp=5,
  PixelFormat_15bpp=6,
  PixelFormat_16bpp=7,
  PixelFormat_24bpp=8,
  PixelFormat_32bpp=9,
  PixelFormat_32bpp_Hi=10, /* &BBGGRR00, i.e. palette entry */

  PixelFormat_BPPMask = 15,

  /* Extra flags, only for the true colour entries */
  PixelFormat_RGB = 16, /* &RGB order, not &BGR */
  PixelFormat_Alpha = 32, /* Alpha, not supremacy/transfer */
} PixelFormat;

/* Must be kept in sync with corresponding definitions in Sources.PutScaled! */
typedef struct
{
  char bits[4]; /* Number of bits in this channel */
  char top[4]; /* The bit above the end of the channel */
  char hints;
  char unused_pad;
  unsigned short alphaimm12;
} PixelFormatInfo;

#define HINT_HIGHEST 1

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
typedef enum
{
  MaskType_None, /* No mask, or we're not using it */
  MaskType_Old, /* Old style mask */
  MaskType_1bpp, /* New style 1bpp mask */
  MaskType_8bpp, /* 8bpp alpha mask */
} MaskType;

typedef enum
{
  BlendImpl_None, /* No blending required */
  BlendImpl_BlendTable, /* Use single blendtable */
  BlendImpl_InverseTable, /* Use InverseTable for 8bpp -> 15bpp -> 8bpp */
  BlendImpl_True, /* True colour blend, no tables required */
  BlendImpl_BlendTables, /* Lots of blend tables for <=4bpp output */
} BlendImpl;

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/* The structure containing all workspace - essentially our static variables. */
#define CHECK_CODE 123456789
typedef struct
{
  /* Initialisation */
  int  check_code;

  /* Code buffer management */
  int  build_buffer;             /* Buffer currently being built, or next to build */
  int *compile_base;
  int *compile_ptr;              /* where to put next instruction */
  int *compile_lim;

  /* Label control and allocation */
  labels_rec labels;             /* each label, and where it is in the generated code */

  /* Register control and allocation */
  regnames_rec regnames;         /* physical assignment of each register name */
353 354
  int  regframesize;             /* size of stack frame holding regs */
  int  regframeoffset;           /* sp offset to apply to get the register stack frame */
355 356

  int  gcol;                     /* GCOL action */
357
  MaskType masktype;             /* Mask type to use */
358 359 360

  int  odither;                  /* If 0, then there's no ordered dither. If non-0, number of bits - 1 being truncated by dither. */

361
  int pixel_expansion_mask[2];   /* Pixel expansion masks. Only rarely are both needed. */
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  /* Assemble-time constants */
  int  in_bpp;
  int  in_bpc;                   /* Same as bpp unless double-pixel, in which case double bpp */
  int  in_pixmask;
  PixelFormat in_pixelformat;
  int  mask_bpp;
  int  mask_bpc;
  int  mask_pixmask;
  int  out_pixmask;              /* mask for one pixel */
  int  out_dpixmask;
  int  out_ppw;                  /* pixels per word */
  int  out_l2ppw;
  PixelFormat out_pixelformat;
  BOOL cal_table_simple;         /* If true, a simple table lookup is possible */
  PixelFormat ColourTTRFormat;   /* Input format of ColourTTR (output format assumed to be out_pixelformat) */
  int  compiled_routine_stacked; /* Offset to apply to LDR_SP/STR_SP */
379
  BlendImpl blendimpl;           /* Blending method/implementation to use */
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

  /* Space for compiled code, near the end so most field accesses have only a small offset. */
  code_buffer buffers[NBUFFERS];

  /* Check for workspace overwritten */
  int  check_code2;
} workspace;

static void check_workspace(workspace *ws)
/* Basic validity checks, and initialise if this is the first time. */
{
  assert(ws != 0, ERROR_NO_MEMORY);
  if (ws->check_code != CHECK_CODE)
  {
    code_buffer *p;
    tracef("Initialising workspace.\n");
    ws->check_code = CHECK_CODE;
    ws->check_code2 = CHECK_CODE;
    ws->build_buffer = 0;
    FOR_EACH_BUFFER(p) p->key_word = -1;

#ifdef DEBUG
    {
      label *l;

      /* Set up textual names of all the labels */
      FOR_EACH_LABEL(l) l->name = 0;
      #define LN(lname) ws->labels.lname.name = #lname;
      LN(loop_y_repeat)
#ifdef TESTDEBUG
      LN(test1)
      LN(test2)
#endif
      LN(loop_x_enter)
      LN(loop_x_repeat)
      LN(loop_x_exit)
      LN(l_masked)
      LN(loop_put_pixel_repeat)
      LN(loop_put_masked_repeat)
      LN(y_loop)
      LN(y_loop_enter)
      LN(y_loop_exit)
      LN(loop_delay)

      LN(x_evenstart)
      LN(x_oddmask)
      LN(x_aligned_loop)
      LN(x_aligned_enter)
      LN(x_alignmask1)
      LN(x_alignmask2)
      LN(x_misaligned)
      LN(x_misaligned_loop)
      LN(x_misaligned_enter)
      LN(x_misalignmask1)
      LN(x_misalignmask2)
      LN(x_2atatime_exit)
      LN(x_lastmask)
      LN(loop_x_exit1)
      LN(loop_x_exitskip)
      LN(loop1)
      LN(loop2)
      LN(plot_loopa)
      LN(plot_loop1)
      LN(plot_loop1a)
      LN(plot_loop1b)
      LN(plot_loop1c)
      LN(plot_loop2)
      LN(plot_loop3)
      LN(plot_loop4)
      LN(plot_loop4a)
      LN(plot_loop4b)
      LN(plot_loop4c)

453 454 455 456
      LN(blend_nodestalpha)
      LN(translate_noalpha)
      LN(translate_noalpha2)

457 458 459 460 461 462 463 464 465 466 467 468
      LN(last)
      /* Check he's got them all */
      FOR_EACH_LABEL(l) assert(l->name != 0, ERROR_FATAL);
    }
    {
      regname *r;

      FOR_EACH_REGISTER_NAME(r) r->name = 0;
      #define RNN(rname) ws->regnames.rname.name = #rname;
      RNN(r_pixel)
      RNN(r_temp1)
      RNN(r_temp2)
469 470
      RNN(r_expansionmask1)
      RNN(r_expansionmask2)
471
      RNN(r_oditheradd)
472 473 474 475 476
      RNN(r_alpha)
      RNN(r_translucency)
      RNN(r_blendtable)
      RNN(r_screenpalette)
      RNN(r_inversetable)
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

      RNN(r_inptr)
      RNN(r_inshift)
      RNN(r_inword)
      RNN(r_maskinptr)
      RNN(r_maskinword)
      RNN(r_maskinshift)
      RNN(r_masko)
      RNN(r_blockroutine)
      RNN(r_ecfindex)
      RNN(r_bgcolour)
      RNN(r_fetchroutine)
      RNN(r_outptr)
      RNN(r_outword)
      RNN(r_outshift)
      RNN(r_outmask)
      RNN(r_table)
      RNN(r_xsize)
      RNN(r_xcount)
      RNN(r_ysize)
      RNN(r_ycount)
      RNN(r_inoffset)
      RNN(r_maskinoffset)
      RNN(r_in_pixmask)

      RNN(r_xsize_spr_left)
      RNN(r_X)
      RNN(r_Y)
      RNN(r_inc_X_x)
      RNN(r_inc_Y_x)
      RNN(r_byte_width)
      RNN(r_spr_height_right)
      RNN(r_out_x)

      RNN(wp)
      RNN(sp)
      RNN(lr)
      RNN(pc)
      FOR_EACH_REGISTER_NAME(r) assert(r->name != 0, ERROR_FATAL);
    }
#endif
  }
  assert(ws->check_code2 == CHECK_CODE, ERROR_FATAL);
}

#ifdef DEBUG
static void dump_asm_workspace(asm_workspace *wp)
{
  /* Oddly spaced out to allow it to be easily lined up with the structure definition */
  tracef("Assembler workspace at %x:\n" _ wp);
  tracef("save_outoffset=%i        %t32. byte offset between output rows - SUBTRACT for next row.\n" _ wp->save_outoffset);
  tracef("save_inoffset=%i         %t32. byte offset between input rows - SUBTRACT for next row.\n" _ wp->save_inoffset);
  tracef("save_inptr=0x%x          %t32. word address of input pixels.\n" _ wp->save_inptr);
  tracef("save_outptr=0x%x         %t32. address of word containing first output pixel.\n" _ wp->save_outptr);
  tracef("save_ydiv=%i             %t32. subtracter value for y scale.\n" _ wp->save_ydiv);
  tracef("save_yadd=%i             %t32. adder value for y scale.\n" _ wp->save_yadd);
  tracef("save_ysize=%i            %t32. number of output rows.\n" _ wp->save_ysize);
  tracef("save_ycount=%i           %t32. total of ymag/ydiv sum, for y scale factor\n" _ wp->save_ycount);
  newline();
  
  tracef("save_inshift=%i          %t32. bit shift of first pixel.\n" _ wp->save_inshift);


  tracef("save_xsize=%i            %t32. number of output pixels per row.\n" _ wp->save_xsize);
  tracef("save_xcount=%i           %t32. total of xmag/xdiv sum, for x scale factor\n" _ wp->save_xcount);
  tracef("save_ecfptr=0x%x         %t32. ECF pointer - only useful if plotting the mask.\n" _ wp->save_ecfptr);
  tracef("save_ecflimit=0x%x       %t32. ECF limit - only useful if plotting the mask.\n" _ wp->save_ecflimit);

  tracef("save_xdiv=%i             %t32. subtracter value for x scale.\n" _ wp->save_xdiv);
  tracef("save_xadd=%i             %t32. adder value for x scale\n" _ wp->save_xadd);
  newline();
  tracef("save_masko=%i            %t32. if not 1bpp mask then this is mask data offset from inptr. Otherwise...\n" _ wp->save_masko);
  tracef("save_xcoord=%i           %t32. pixel x coordinate of first output pixel.\n" _ wp->save_xcoord);
  tracef("save_ycoord=%i           %t32. pixel y coordinate of first output pixel.\n" _ wp->save_ycoord);





  tracef("save_xmag=%i             %t32. adder value for x scale?\n" _ wp->save_xmag);
  tracef("save_ymag=%i             %t32. adder value for y scale?\n" _ wp->save_ymag);
  newline();

  tracef("save_inlog2bpp=%i        %t32. log 2 bits per pixel of input.\n" _ wp->save_inlog2bpp);
  tracef("save_inlog2bpc=%i        %t32. log 2 bits per character of input (only different for double-pixels).\n"
                                   _ wp->save_inlog2bpc);
  tracef("save_mode=%i (>>27 = %i) %t32. mode number/pointer of sprite - 1bpp sprites have hi bits set.\n" _ wp->save_mode _ wp->save_mode >> 27);
  newline();

  tracef("save_maskinshift=%i      %t32. initial bit shift within mask word.\n" _ wp->save_maskinshift);
  tracef("save_maskinptr=0x%x      %t32. word address of mask (or 0 if there isn't one).\n" _ wp->save_maskinptr);
  tracef("save_maskinoffset=%i     %t32. byte offset between mask rows - SUBTRACT for next row.\n" _ wp->save_maskinoffset);
  newline();

  tracef("BPP=%i                   %t32. bits per pixel of output.\n" _ wp->BPP);
  tracef("BPC=%i                   %t32. bits per character of output (only different for double pixels).\n" _ wp->BPC);
  tracef("ColourTTR=0x%x           %t32. translation table or palette.\n" _ wp->ColourTTR);
574
  tracef("TTRType=0x%x             %t32. translation table type.\n" _ wp->TTRType);
575
  tracef("spritecode=%i (& 255 = %i) %t32. SpriteOp - 52 for PutSpriteScaled, 50 for PlotMaskScaled.\n" _ wp->spritecode _ wp->spritecode & 255);
576
  tracef("blending=%i              %t32. b0: translucency blending, b1: alpha mask/channel blending\n" _ wp->blending);
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  newline();
}
#endif

#ifdef TESTDEBUG
static void dump_workspace(workspace *ws)
{
  code_buffer *p;

  tracef("Dumping workspace.\n");
  #define DUMPINT(field) tracef("%s = %i.\n" _ #field _ ws->field);
  DUMPINT(build_buffer)
  FOR_EACH_BUFFER(p) tracef("buffer->keyword = %i.\n" _ p->key_word);
}
#endif

/**************************************************************************
*                                                                         *
*    Low-level instruction generation.                                    *
*                                                                         *
**************************************************************************/

/* Condition codes */
#define EQ 0xf0000000      /* It's 0 really - frigged so that 0 can be 'always' - the usual case. */
#define NE 0x10000000
#define CS 0x20000000
#define CC 0x30000000
#define MI 0x40000000
#define PL 0x50000000
#define VS 0x60000000
#define VC 0x70000000
#define HI 0x80000000
#define LS 0x90000000
#define GE 0xa0000000
#define LT 0xb0000000
#define GT 0xc0000000
#define LE 0xd0000000
#define AL 0xe0000000
615 616
#define HS CS
#define LO CC
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

/* Branches */
#define B  0x0a000000
#define BL 0x0b000000
#define B_OFFSET_MASK 0x00ffffff /* and with this for negative offsets */
#define BLX(reg,str) ins(ws,0x012FFF30 | reg,str)

/* ALU ops */
#define S  (1<<20)
#define AND(dst,op1,rest,str)      ins(ws,(0x0 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define EOR(dst,op1,rest,str)      ins(ws,(0x1 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define SUB(dst,op1,rest,str)      ins(ws,(0x2 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define RSB(dst,op1,rest,str)      ins(ws,(0x3 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define ADD(dst,op1,rest,str)      ins(ws,(0x4 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define ADC(dst,op1,rest,str)      ins(ws,(0x5 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define SBC(dst,op1,rest,str)      ins(ws,(0x6 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define RSC(dst,op1,rest,str)      ins(ws,(0x7 << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define TST(op1,rest,str)          ins(ws,(0x8 << 21) | S | OP1R(op1) | (rest), str)
#define TEQ(op1,rest,str)          ins(ws,(0x9 << 21) | S | OP1R(op1) | (rest), str)
#define CMP(op1,rest,str)          ins(ws,(0xa << 21) | S | OP1R(op1) | (rest), str)
#define CMN(op1,rest,str)          ins(ws,(0xb << 21) | S | OP1R(op1) | (rest), str)
#define ORR(dst,op1,rest,str)      ins(ws,(0xc << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define MOV(dst,rest,str)          ins(ws,(0xd << 21) | DSTR(dst) | (rest), str)
#define BIC(dst,op1,rest,str)      ins(ws,(0xe << 21) | DSTR(dst) | OP1R(op1) | (rest), str)
#define MVN(dst,rest,str)          ins(ws,(0xf << 21) | DSTR(dst) | (rest), str)

#define MUL(dst,op1,op2,rest,str)  ins(ws,(9<<4) | ((dst)<<16) | (op1) | ((op2)<<8) | (rest), str)
644 645

/* CPUFlag_T2 instructions */
646
#define MOVW(dst,rest,str)         ins(ws,(3<<24) | DSTR(dst) | (rest), str)
647 648 649 650 651 652
#define MOVT(dst,rest,str)         ins(ws,(0x34<<20) | DSTR(dst) | (rest), str)
#define UBFX(dst,src,lsb,width,rest,str) ins(ws,0x07e00050 | DSTR(dst) | (src) | (((width)-1)<<16) | ((lsb)<<7) | (rest), str)
#define BFC(dst,lsb,width,rest,str) ins(ws,0x07c0001f | DSTR(dst) | (((lsb)+(width)-1)<<16) | ((lsb)<<7) | (rest), str)
#define BFI(dst,src,lsb,width,rest,str) ins(ws,0x07c00010 | DSTR(dst) | (src) | (((lsb)+(width)-1)<<16) | ((lsb)<<7) | (rest), str)

#define REV(dst,src,rest,str)      ins(ws,0x06bf0f30 | DSTR(dst) | (src) | (rest), str)
653 654 655 656 657 658 659 660 661 662 663 664 665 666

#define ADD_OPCODE (0x4 << 21)
#define SUB_OPCODE (0x2 << 21)
#define MOV_OPCODE (0xd << 21)

#define DSTR(x) ((x) << 12)          /* destination - ignored by TST/TEQ/CMP/CMN */
#define OP1R(x) ((x) << 16)          /* first operand */
#define OP2R(x) ((x) << 0)           /* if !IMM */
#define IMM(x) ((x) | (1<<25))       /* an 8-bit unsigned field */
#define IMMROR(x) ((x) << 7)         /* an EVEN number to rotate right IMM by */

static int IMM12(unsigned int imm)   /* generate immediate constant for ALU ops */
{
  int ror = 0;
667
  while(imm >= 256)
668 669 670 671 672 673 674 675
  {
    assert(ror < 32, ERROR_FATAL);
    imm = (imm<<2) | (imm>>30);
    ror += 2;
  }
  return IMM(imm) | IMMROR(ror);
}

676
#define IMM16(x) (((x) & 0xFFF) | (((x) & 0xF000)<<4)) /* encode 16 bit immediate constant for MOVW/MOVT */ 
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

#define LSLI(x) (((x) << 7) | 0x00)   /* 5-bit immed shift applied to OP2R */
#define LSRI(x) (((x) << 7) | 0x20)
#define ASRI(x) (((x) << 7) | 0x40)
#define RORI(x) (((x) << 7) | 0x60)

#define LSLR(x) (((x) << 8) | 0x10)   /* shift register applied to OP2R */
#define LSRR(x) (((x) << 8) | 0x30)
#define ASRR(x) (((x) << 8) | 0x50)
#define RORR(x) (((x) << 8) | 0x70)

/* Load and store ops */
#define LDR(reg,basereg)  (0x04100000 | ((reg) << 12)| ((basereg) << 16))
#define STR(reg,basereg)  (0x04000000 | ((reg) << 12)| ((basereg) << 16))
#define LDRB(reg,basereg) (0x04500000 | ((reg) << 12)| ((basereg) << 16))
#define STRB(reg,basereg) (0x04400000 | ((reg) << 12)| ((basereg) << 16))

#define WRITEBACK (1 << 21)
#define ADDOFFSET (1 << 23) /* else subtract */
#define PREADD (1 << 24) /* else post */

#define OFFSET(x) (PREADD | ADDOFFSET | (x))        /* normal simple index */
#define NEGOFFSET(x) (PREADD | (x))                 /* subtract offset */
#define PREINC(x) (WRITEBACK | ADDOFFSET | PREADD | (x))
#define PREDEC(x) (WRITEBACK | PREADD | (x))
#define POSTINC(x) (ADDOFFSET | (x))                /* The manual says, do not set WRITEBACK if doing post-addition */
#define POSTDEC(x) ((x))                            /* writeback will always occur, setting it is does LDRT/LDRBT */

#define PUSH (0x08000000 | (13<<16) /* register 13 */ \
                         | (1<<21) /* write-back */ \
                         | (1<<24) /* add offset before transfer */)
#define POP  (0x08000000 | (13<<16) /* register 13 */ \
                         | (1<<20) /* load from memory */ \
                         | (1<<21) /* write-back */ \
                         | (1<<23) /* add, not subtract */ )

#define LDMIA(reg) (0x08000000 | (reg<<16) /* register to load from */ \
                               | (1<<20) /* load from memory */ \
                               | (1<<23) /* add, not subtract */ )

#define STMIA(reg) (0x08000000 | (reg<<16) /* register to load from */ \
                               | (1<<23) /* add, not subtract */ )

/* Supervisor call */
#define SWI(swino) (0x0F000000 | swino)

/* Indexed load - LSL shift assumed - writeback or negative not covered */
#define INDEX(reg, shift) ((1<<25) | OFFSET(0) | OP2R(reg) | LSLI(shift))

/* Indexed load - LSR shift - writeback or negative not covered */
#define INDEX_LSR(reg, shift) ((1<<25) | OFFSET(0) | OP2R(reg) | LSRI(shift))

/* Offset in assembler workspace */
#define WP_OFFSET(field) OFFSET(((char*)&(wp->field)) - ((char*)&(wp->WP_FIRST_FIELD)))

/* Offset in stack workspace */
#define SP_OFFSET(field) OFFSET((int) (((char*)&(((stack_ws *)0)->field)) + ws->compiled_routine_stacked))

/* Define an assembler register */
736
#define RN(name,no,flags,describe) set_regname(ws, &ws->regnames.name, no, flags, describe);
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

#ifdef DEBUG
static void ldm_reg_list(workspace *ws, char *a, int regmask, BOOL lastname)
/* Construct a string in a which can be placed in curly brackets, describing
 * a LDM/STM instruction. If lastname then find the last such register name in
 * the case of duplicates - eg. the y-loop name rather than the x-loop name
 * for the same physical register.
 */
{
  int i;
  regname *r;
  BOOL found;
  char *aptr;

  a[0] = 0;
  for (i = 0; i <= 15; i++) /* for each physical register */
  {
    if ((regmask & (1<<i)) != 0) /* find a name for this register */
    {
      found = FALSE;
      aptr = a;
      while (*aptr != 0) aptr++; /* points at the null at the end of the string */
      FOR_EACH_REGISTER_NAME(r)
      {
        if (r->regno == i)
        {
          *aptr = 0; /* If lastname and finding it again, delete last one */
          if (a[0] != 0) strcat(aptr, ",");
          strcat(aptr, r->name);
          found = TRUE;
          if (!lastname) break;
        }
      }
      assert(found, ERROR_FATAL);
    }
  }
}
#endif

#ifdef DEBUG
static void ins(workspace *ws, int w, char *description)
#else
#define ins(ws,w,description) do_ins(ws,w)
static void do_ins(workspace *ws, int w)
#endif
/* Put an instruction into the output buffer.
 * When debugging an assembler listings is generated too. These can be fed through
 * objasm, and the results compared with the opcodes that I generate.
 * Columns of assembler output:
 * addressX  opcodeXX  label   opcodes regs                            comment
 * ^0        ^10       ^20     ^28     ^36                             ^68
 */
{
  int ccode = w & 0xf0000000;

  /* Handle the AL/EQ condition codes being wrong, so that 0 can be AL elsewhere. */
  if (ccode == 0xf0000000) w = w & 0x0fffffff;   /* EQ code */
  else if (ccode == 0) w = w | 0xe0000000;       /* AL code */
  /* All others are per the ARM expects */
  tracef("%x  %x  %t28.%s\n" _
    (ws->compile_ptr - ws->compile_base) * sizeof(int) _
    w _ description); /* pseudo-assembler format of output */

  assert(ws->compile_ptr < ws->compile_lim, ERROR_NO_MEMORY); /* Check the buffer is big enough */
  *(ws->compile_ptr)++ = w; /* Store at then increment P% */
}

#ifdef DEBUG
#define DEFINE_LABEL(lab,describe) define_label(ws, L(lab), describe);
static void define_label(workspace *ws, label *lab, char *description)
#else
#define DEFINE_LABEL(lab,describe) define_label(ws, L(lab));
static void define_label(workspace *ws, label *lab)
#endif
/* Define a label, and fill in a forward reference to it if necessary. */
{
   assert(lab->def == 0, ERROR_FATAL); /* Check not defined twice */
   lab->def = ws->compile_ptr;
   tracef("%t20.%s%t68.; %s\n" _ lab->name _ description);
   if (lab->ref != 0)
   {
     int newvalue = *(lab->ref) | (B_OFFSET_MASK & (lab->def - (lab->ref + 2))); /* compute offset */
     tracef("%t20.; Zapping forward ref instruction at %x to be %x.\n" _
       sizeof(int) * (lab->ref - ws->compile_base) _ newvalue);
     *(lab->ref) = newvalue;
     lab->ref = 0;
   }
}

#ifdef DEBUG
static void branch(workspace *ws, unsigned int opcode, label *lab, char *description)
#else
#define branch(ws,opcode,lab,description) do_branch(ws,opcode,lab)
static void do_branch(workspace *ws, unsigned int opcode, label *lab)
#endif
/* Compile a branch instruction to a label. The opcode includes the condition code. */
{
  if (lab->def == 0) /* Forward reference */
  {
#ifdef DEBUG
    if (lab->ref != 0)
      tracef("Already referenced at 0x%x\n" _ sizeof(int) * (lab->ref - ws->compile_base));
#endif
    assert(lab->ref == 0, ERROR_FATAL); /* Check for two forward refs to same label */
    lab->ref = ws->compile_ptr;
    ins(ws, opcode, description); /* Just give as offset 0 for now */
  }
  else
  {
    assert(lab->ref == 0, ERROR_FATAL);
    ins(ws,
      opcode | (B_OFFSET_MASK & (lab->def - (ws->compile_ptr + 2))), description);
  }
}

#ifdef DEBUG
853
static void set_regname(workspace *ws, regname *r, int regno, int flags, char *describe)
854
#else
855 856
#define set_regname(ws,r,regno,flags,describe) do_set_regname(ws,r,regno,flags)
static void do_set_regname(workspace *ws, regname *r, int regno, int flags)
857 858 859 860 861
#endif
/* Allocate a physical register number. If regno is -1 then allocate an
 * as-yet-unused one, otherwise it's a specific register number.
 */
{
862 863
  UNUSED(ws);
  if(regno != -1)
864
  {
865 866
    assert((r->regno == -1) || (r->regno == regno), ERROR_FATAL);
    r->regno = regno;
867
  }
868 869 870 871
  r->flags |= flags;
#ifdef DEBUG
  r->describe = describe;
#endif
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
}

static void align16(asm_workspace *wp, workspace *ws)
/* Align next instruction to quadword boundary */
{
  UNUSED(wp);
  while (((int) ws->compile_ptr) & 15 != 0)
    MOV(R(r_pixel), OP2R(R(r_pixel)),                        "MOV     r_pixel,r_pixel                 ; align to 16-byte boundary");
}

#if defined(DEBUG_TML) && defined(DEBUG)
static void write_reg(workspace *ws, regname *reg)
/* Sppol the register to the TML hardware */
{
    comment(ws, "Write Register to TML card");
    tracef("Register to be output is... %s\n" _ reg->name);
    ins(ws, PUSH | (1<<10) | (1<<11) | 1 | (1<<1) | (1<<14),        "STMDB   sp!,{r0,r1,r10,r11,r14}          ; prepare to call SWI");
    ins(ws, MOV_OPCODE | DSTR(1) | OP2R(reg->regno),                "MOV     r1,r_somereg");
    ins(ws, MOV_OPCODE | DSTR(0) | OP2R(1),                         "MOV     r0,r1");
    AND(0, 0, IMM(0xff),                                            "AND     r0,r0,#255 ");
    ins(ws, SWI(HostFS_WriteC),                                     "SWI     HostFS_WriteC                    ; convert r1 value");
    ins(ws, MOV_OPCODE | DSTR(0) | OP2R(1) | LSRI(8),               "MOV     r0,r1 LSR #8");
    AND(0, 0, IMM(0xff),                                            "AND     r0,r0,#255                               ");
    ins(ws, SWI(HostFS_WriteC),                                     "SWI     HostFS_WriteC                    ; convert r1 value");
    ins(ws, MOV_OPCODE | DSTR(0) | OP2R(1) | LSRI(16),              "MOV     r0,r1 LSR #16");
    AND(0, 0, IMM(0xff),                                            "AND     r0,r0,#255                               ");
    ins(ws, SWI(HostFS_WriteC),                                     "SWI     HostFS_WriteC                    ; convert r1 value");
    ins(ws, MOV_OPCODE | DSTR(0) | OP2R(1) | LSRI(24),              "MOV     r0,r1 LSR #24");
    AND(0, 0, IMM(0xff),                                            "AND     r0,r0,#255                               ");
    ins(ws, SWI(HostFS_WriteC),                                     "SWI     HostFS_WriteC                    ; convert r1 value");
    ins(ws, POP | (1<<10) | (1<<11) | 1 | (1<<1) | (1<<14),         "LDMIA   sp!,{r0,r1,r10,r11,r14}          ; restore after calling SWI");
    comment(ws, "");
}
#endif

/* Loading a constant index from the workspace pointer */
#define LDR_WP(reg,value) ins(ws, LDR(R(reg),R(wp)) + WP_OFFSET(value), \
                              "LDR     " #reg "," #value);

#ifdef DEBUG
  #define LDR_WP_C(reg,value, comment)                                \
  {                                                                   \
    char a[256];                                                      \
    do_sprintf(a, "LDR     " #reg "," #value " %t40.; " comment);        \
    ins(ws, LDR(R(reg),R(wp)) + WP_OFFSET(value), a);                 \
  }
#else
  #define LDR_WP_C(reg,value, comment) ins(ws, LDR(R(reg),R(wp)) + WP_OFFSET(value), 0);
#endif

/* Loading a constant index from a register */
#ifdef DEBUG
  #define LDR_INDEX(destreg,indexreg,offset,comment)                                      \
  {                                                                                       \
    char a[256];                                                                          \
    do_sprintf(a, "LDR     " #destreg ",[" #indexreg ", #%i] %t40.; " comment, offset);      \
    ins(ws, LDR(R(destreg),R(indexreg)) | OFFSET(offset), a);                             \
  }
#else
  #define LDR_INDEX(destreg,indexreg,offset,comment) ins(ws, LDR(R(destreg),R(indexreg)) | OFFSET(offset), 0);
#endif

/* Loading/storing a constant index from the stack */
#define LDR_SP(reg,value) ins(ws, LDR(R(reg),R(sp)) + SP_OFFSET(value), \
                              "LDR     " #reg "," #value " + compiled_routine_stacked");
#define STR_SP(reg,value) ins(ws, STR(R(reg),R(sp)) + SP_OFFSET(value), \
                              "STR     " #reg "," #value " + compiled_routine_stacked");

#define ADD_A(reg,value) arbitrary_add(ws, TRUE, FALSE, &ws->regnames.reg, value);
#define ADDS_A(reg,value) arbitrary_add(ws, TRUE, TRUE, &ws->regnames.reg, value);
#define SUB_A(reg,value) arbitrary_add(ws, FALSE, FALSE, &ws->regnames.reg, value);
#define SUBS_A(reg,value) arbitrary_add(ws, FALSE, TRUE, &ws->regnames.reg, value);

static void arbitrary_add(workspace *ws, BOOL add, BOOL s, regname *r, int value)
/* Add/subtract an arbitrary constant to a register - could be more than 8 bits. */
{
  IFDEBUG(char a[256];)

  if (value < 0) {value = -value; add = !add;}
  if (value == 0) /* special case with 0 constant */
  {
    if (s)
    {
      IFDEBUG(do_sprintf(a, "CMP     %s,#0", r->name);)
      CMP(r->regno, IMM(0), a);
    }
    /* else, nothing */
  }
  else
  {
    int opcode = add ? ADD_OPCODE : SUB_OPCODE;
    int sopcode = s ? S : 0;
    int shift_it = 0;

    while (value != 0)
    {
      BOOL last;
      int valuebyte;

      if (value > 255)
        while ((value & 3) == 0) {value >>= 2; shift_it += 2;}
      valuebyte = value & 0xff;
      value &= 0xffffff00;
      last = value == 0; /* the last instruction needed */
      IFDEBUG(
        do_sprintf(a,
          (last && sopcode ? "%sS%t8.%s,%s,#&%x" : "%s%t8.%s,%s,#&%x") _
          (add ? "ADD" : "SUB") _ r->name _ r->name _ valuebyte << shift_it);)
      ins(ws, opcode | (last ? sopcode : 0)
            | DSTR(r->regno) | OP1R(r->regno)
            | IMM(valuebyte) | IMMROR ((32 - shift_it) & 0x1e),
            a);
    }
  }
}

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
static int countbits(unsigned int bits)
/* Return count of how many bits are set */
{
  bits -= (bits & 0xaaaaaaaa)>>1;
  bits = (bits & 0x33333333) + ((bits>>2) & 0x33333333);
  bits += bits>>4;
  bits &= 0x0f0f0f0f;
  bits += bits>>8;
  bits += bits>>16;
  return (bits & 0xff);
}

/**************************************************************************
*                                                                         *
*    Register management                                                  *
*                                                                         *
**************************************************************************/
#ifdef DEBUG
static void dump_registers(asm_workspace *wp, workspace *ws)
{
  UNUSED(wp);
  regname *r;
  FOR_EACH_REGISTER_NAME(r)
  {
    tracef("%s: %x %x\n" _ r->name _ r->regno _ r->flags);
  }
}
#endif

static void allocate_registers(asm_workspace *wp, workspace *ws)
{
  UNUSED(wp);
  regname *r;
//  IFDEBUG(dump_registers(wp,ws);)
  /* Iterate through all registers and assign register numbers
     For registers that must be saved to stack, allocate stack space */
  int usedregs[15];
  for(int i=0;i<15;i++)
    usedregs[i] = 0;
  FOR_EACH_REGISTER_NAME(r)
  {
    if((r->regno >= 0) && (r->regno < 16))
    {
      usedregs[r->regno] |= r->flags & REGFLAG_USAGE_MASK;
    }
  }
  /* Allocate in order of importance - global, then per-pixel, then x-loop, etc. */
  for(int i=REGFLAG_GLOBAL;i<=REGFLAG_YLOOP;i<<=1)
  {
    FOR_EACH_REGISTER_NAME(r)
    {
      if(!(r->flags & i) || (r->regno != -1))
        continue;
      /* Look for a free register
         We prefer a completely free register, or if none is available, the first one that doesn't clash with this allocation
         Additionally, temporary registers are allocated from the top, non-temporary from the bottom, as I think this may help in some situations */
      int regno=-1,nextbest=-1;
      int blocking = (r->flags & REGFLAG_USAGE_MASK) | REGFLAG_GLOBAL;
      if(r->flags & REGFLAG_TEMPORARY)
      {
        for(int j=15;j>=0;j--)
        {
          if(!usedregs[j])
          {
            regno = j;
            break;
          }
          if(!(usedregs[j] & blocking) && (nextbest == -1))
          {
            nextbest = j;
          }
        }
      }
      else
      {
        for(int j=0;j<15;j++)
        {
          if(!usedregs[j])
          {
            regno = j;
            break;
          }
          if(!(usedregs[j] & blocking) && (nextbest == -1))
          {
            nextbest = j;
          }
        }
      }
      if(regno == -1)
      {
        regno = nextbest;
        assert(regno != -1, ERROR_FATAL);
      }
      r->regno = regno;
      usedregs[regno] |= r->flags & REGFLAG_USAGE_MASK;
      IFDEBUG(tracef("%s -> %i in %i\n" _ r->name _ r->regno _ i);)
//      IFDEBUG(dump_registers(wp,ws);)
    }
  }
  /* Work out which registers need saving on the stack */
  regname *frame[4][15];
  for(int i=0;i<4;i++)
  {
    for(int j=0;j<15;j++)
      frame[i][j] = 0;
  }
  FOR_EACH_REGISTER_NAME(r)
  {
    if((r->regno < 0) || (r->regno >= 16) || (r->flags & REGFLAG_GLOBAL))
      continue;
    for(int i=0;i<4;i++)
    {
      if(r->flags & (REGFLAG_PERPIXEL<<i))
      {
        IFDEBUG(if(frame[i][r->regno]) tracef("%s %x vs. %s %x\n" _ frame[i][r->regno]->name _ frame[i][r->regno]->regno _ r->name _ r->regno);)
        assert(!frame[i][r->regno], ERROR_FATAL);
        frame[i][r->regno] = r;
      }
    }
  }
  ws->regframesize = 0;
  for(int i=0;i<4;i++)
  {
    for(int j=0;j<15;j++)
    {
      if(!frame[i][j] || (frame[i][j]->flags & REGFLAG_TEMPORARY) || (frame[i][j]->spindex != -1))
        continue;
      for(int k=0;k<4;k++)
      {
        if((k != i) && frame[k][j] && (frame[k][j] != frame[i][j]))
        {
          /* Need to save this reg */
#ifdef DEBUG
          if(!ws->regframesize) tracef("%t20.; Stack frame layout:\n");
          tracef("%20.; +%x %s R%i\n" _ ws->regframesize _ frame[i][j]->name _ frame[i][j]->regno & 15);
#endif
          frame[i][j]->spindex = ws->regframesize;
          ws->regframesize += 4;
          break;
        }
      }
    }
  }
  FOR_EACH_REGISTER_NAME(r)
  {
    assert(!(r->flags && (r->regno == -1)), ERROR_FATAL); /* All used regs must be assigned */
    /* Resolve any aliased registers */
    if((r->regno < -1) || (r->regno >= 16))
    {
      regname *rr = (regname *) r->regno;
      r->regno = rr->regno;
      assert((r->regno & 0xff) < 16, ERROR_FATAL);
      r->flags = rr->flags;
      r->spindex = rr->spindex;
    }
    /* Set availability */
    if(!(r->flags & (REGFLAG_GLOBAL | REGFLAG_INPUT | REGFLAG_USED)))
      r->regno |= 0x80000000;
#ifdef DEBUG
    /* Output definition */
    if((r->regno & 0xff) < 16)
    {
      tracef("%t20.%s%t27 RN %t36.%i %t68.; %s\n" _ r->name _ (r->regno & 0xff) _ r->describe);
    }
#endif
  }
  IFDEBUG(dump_registers(wp,ws);)
}

static void reserve_regstackframe(asm_workspace *wp, workspace *ws)
{
  UNUSED(wp);
  /* Reserve stack space */
  if(ws->regframesize)
    SUB_A(sp, ws->regframesize)
}

static void discard_regstackframe(asm_workspace *wp, workspace *ws)
{
  UNUSED(wp);
  /* Pop all saved registers off the stack */
  if(ws->regframesize)
    ADD_A(sp, ws->regframesize)
}

static void begin_init_bank(asm_workspace *wp, workspace *ws, int bank)
{
  UNUSED(wp);
  /* Mark regs in 'bank' as switched in */
  regname *r;
  bank |= REGFLAG_GLOBAL | REGFLAG_USED;
  FOR_EACH_REGISTER_NAME(r)
  {
    if(r->flags & bank)
    {
      r->regno &= ~0x80000000;
    }
    else
    {
      r->regno |= 0x80000000;
    }
  }
  IFDEBUG(tracef("begin_init_bank %x\n" _ bank);)
//  IFDEBUG(dump_registers(wp,ws);)
}

static void save_bank(asm_workspace *wp, workspace *ws, regname **savelist, int temp, BOOL allow_auto_temp)
{
  UNUSED(wp);
  /* Optimised save of given list */
  IFDEBUG(char a[256];)
  IFDEBUG(char b[256];)
  int base=-1;
  int count=0;
  int mask=0;
  int offset = ws->regframeoffset;
  for(int i=0;i<15;i++)
  {
    if(!savelist[i])
      continue;
    if((temp == -1) && (savelist[i]->spindex+offset))
    {
      IFDEBUG(do_sprintf(a,"STR     %s,[sp,#%i+%i]",savelist[i]->name,savelist[i]->spindex,offset);)
      ins(ws, STR(savelist[i]->regno & 0xff, R(sp)) | OFFSET(savelist[i]->spindex+offset), a);
      /* If we're in end_init_bank, disallow selection of temp register here, as we assume that any regs we're saving in end_init_bank will remain valid in the future */
      if(allow_auto_temp)
      {
        temp = savelist[i]->regno;
      }
    }
    else if(base == -1)
    {
      base = i;
      count = 1;
      mask = 1<<i;
      IFDEBUG(b[0] = 0;)
      IFDEBUG(strcat(b,savelist[i]->name);)
    }
    else if(savelist[i]->spindex == savelist[base]->spindex + count*4)
    {
      count++;
      mask |= 1<<i;
      IFDEBUG(strcat(b,",");)
      IFDEBUG(strcat(b,savelist[i]->name);)
    }
    else
    {
      if(count == 1)
      {
        IFDEBUG(do_sprintf(a,"STR     %s,[sp,#%i+%i]",savelist[base]->name,savelist[base]->spindex,offset);)
        ins(ws, STR(savelist[base]->regno & 0xff, R(sp)) | OFFSET(savelist[base]->spindex+offset), a);
      }
      else if(savelist[base]->spindex+offset)
      {
        assert((savelist[base]->spindex+offset) < 256, ERROR_FATAL);
        IFDEBUG(do_sprintf(a,"ADD     R%i,sp,#%i+%i",temp,savelist[base]->spindex,offset);)
        ADD(temp,R(sp),IMM(savelist[base]->spindex+offset),a);
        IFDEBUG(do_sprintf(a,"STMIA   R%i,{%s}",temp,b);)
        ins(ws, STMIA(temp) | mask, a);
      }
      else
      {
        IFDEBUG(do_sprintf(a,"STMIA   sp,{%s}",b);)
        ins(ws, STMIA(R(sp)) | mask, a);
      }
      base = i;
      count = 1;
      mask = 1<<i;
    }
  }
  if(!count)
    return;
  if(count == 1)
  {
    IFDEBUG(do_sprintf(a,"STR     %s,[sp,#%i+%i]",savelist[base]->name,savelist[base]->spindex,offset);)
    ins(ws, STR(savelist[base]->regno & 0xff, R(sp)) | OFFSET(savelist[base]->spindex+offset), a);
  }
  else if(savelist[base]->spindex+offset)
  {
    assert((savelist[base]->spindex+offset) < 256, ERROR_FATAL);
    IFDEBUG(do_sprintf(a,"ADD     R%i,sp,#%i+%i",temp,savelist[base]->spindex,offset);)
    ADD(temp,R(sp),IMM(savelist[base]->spindex+offset),a);
    IFDEBUG(do_sprintf(a,"STMIA   R%i,{%s}",temp,b);)
    ins(ws, STMIA(temp) | mask, a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"STMIA   sp,{%s}",b);)
    ins(ws, STMIA(R(sp)) | mask, a);
  }
}

static void load_bank(asm_workspace *wp, workspace *ws, regname **loadlist)
{
  UNUSED(wp);
  /* Optimised load of given list */
  IFDEBUG(char a[256];)
  IFDEBUG(char b[256];)
  int base=-1;
  int count=0;
  int mask=0;
  int offset = ws->regframeoffset;
  for(int i=0;i<15;i++)
  {
    if(!loadlist[i])
      continue;
    if(base == -1)
    {
      base = i;
      count = 1;
      mask = 1<<i;
      IFDEBUG(b[0] = 0;)
      IFDEBUG(strcat(b,loadlist[i]->name);)
    }
    else if(loadlist[i]->spindex == loadlist[base]->spindex + count*4)
    {
      count++;
      mask |= 1<<i;
      IFDEBUG(strcat(b,",");)
      IFDEBUG(strcat(b,loadlist[i]->name);)
    }
    else
    {
      if(count == 1)
      {
        IFDEBUG(do_sprintf(a,"LDR     %s,[sp,#%i+%i]",loadlist[base]->name,loadlist[base]->spindex,offset);)
        ins(ws, LDR(loadlist[base]->regno & 0xff, R(sp)) | OFFSET(loadlist[base]->spindex+offset), a);
      }
      else if(loadlist[base]->spindex+offset)
      {
        assert((loadlist[base]->spindex+offset) < 256, ERROR_FATAL);
        IFDEBUG(do_sprintf(a,"ADD     %s,sp,#%i+%i",loadlist[base]->name,loadlist[base]->spindex,offset);)
        ADD(loadlist[base]->regno,R(sp),IMM(loadlist[base]->spindex+offset),a);
        IFDEBUG(do_sprintf(a,"LDMIA   %s,{%s}",loadlist[base]->name,b);)
        ins(ws, LDMIA(loadlist[base]->regno) | mask, a);
      }
      else
      {
        IFDEBUG(do_sprintf(a,"LDMIA   sp,{%s}",b);)
        ins(ws, LDMIA(R(sp)) | mask, a);
      }
      base = i;
      count = 1;
      mask = 1<<i;
    }
  }
  if(!count)
    return;
  if(count == 1)
  {
    IFDEBUG(do_sprintf(a,"LDR     %s,[sp,#%i+%i]",loadlist[base]->name,loadlist[base]->spindex,offset);)
    ins(ws, LDR(loadlist[base]->regno & 0xff, R(sp)) | OFFSET(loadlist[base]->spindex+offset), a);
  }
  else if(loadlist[base]->spindex+offset)
  {
    assert((loadlist[base]->spindex+offset) < 256, ERROR_FATAL);
    IFDEBUG(do_sprintf(a,"ADD     %s,sp,#%i+%i",loadlist[base]->name,loadlist[base]->spindex,offset);)
    ADD(loadlist[base]->regno,R(sp),IMM(loadlist[base]->spindex+offset),a);
    IFDEBUG(do_sprintf(a,"LDMIA   %s,{%s}",loadlist[base]->name,b);)
    ins(ws, LDMIA(loadlist[base]->regno) | mask, a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"LDMIA   sp,{%s}",b);)
    ins(ws, LDMIA(R(sp)) | mask, a);
  }
}

static void end_init_bank(asm_workspace *wp, workspace *ws, int bank)
{
  /* Save all 'bank' registers (including constants) */
  regname *r;
  regname *savelist[15];
  for(int i=0;i<15;i++)
    savelist[i] = 0;
  BOOL dosave = FALSE;
  int temp = -1;
  FOR_EACH_REGISTER_NAME(r)
  {
    if((r->flags & bank) && (r->spindex != -1) && !(r->flags & REGFLAG_INPUT))
    {
      savelist[r->regno & 15] = r;
      dosave = TRUE;
    }
    if((r->regno >= 0) && (r->regno < 16) && (r->flags & REGFLAG_TEMPORARY))
      temp = r->regno;
  }
  if(dosave)
  {
    save_bank(wp, ws, savelist, temp, FALSE);
  }
  IFDEBUG(tracef("end_init_bank %x\n" _ bank);)
//  IFDEBUG(dump_registers(wp,ws);)
}

static void switch_bank(asm_workspace *wp, workspace *ws, int oldbank, int newbank)
{
  /* Save all non-constant 'oldbank', load all 'newbank'
     Special hack: if newbank == 0, also save constant oldbank (used to save input regs) */
  regname *r;
  regname *savelist[15];
  regname *loadlist[15];
  for(int i=0;i<15;i++)
  {
    savelist[i] = 0;
    loadlist[i] = 0;
  }
  BOOL dosave = FALSE;
  BOOL doload = FALSE;
  int temp = -1;
  FOR_EACH_REGISTER_NAME(r)
  {
    if(r->spindex != -1)
    {
      if((r->flags & oldbank) /* It's in the old bank */
      && !(r->flags & newbank) /* And it's not in the new bank */
      && (!(r->flags & REGFLAG_CONSTANT) || !newbank)) /* And it's non-constant */
      {
        savelist[r->regno & 15] = r; /* Then save it */
        dosave = TRUE;
      }
      else if((r->flags & newbank) /* It's in the new bank */
          && !(r->flags & oldbank)) /* And it's not in the old bank */
      {
        loadlist[r->regno & 15] = r; /* Then load it */
        doload = TRUE;
      }
    }
    if((r->regno >= 0) && (r->regno < 16) && (r->flags & REGFLAG_TEMPORARY) && !(r->flags & newbank))
      temp = r->regno;
  }
  /* Loop round again to update the flags, and to search harder for a temp register (temp reg search needs 2nd loop as we need to know we're not about to clobber a register we need to save) */
  FOR_EACH_REGISTER_NAME(r)
  {
    if(r->flags & (newbank | REGFLAG_GLOBAL | REGFLAG_USED))
    {
      if((r->regno & 0x80000000) && !savelist[r->regno & 15] && (loadlist[r->regno & 15] || ((r->flags & REGFLAG_TEMPORARY) && !(r->flags & oldbank))))
        temp = r->regno & 0xff;
      r->regno &= ~0x80000000;
    }
    else
      r->regno |= 0x80000000;
  }
  if(dosave)
  {
    save_bank(wp, ws, savelist, temp, TRUE);
  }
  if(doload)
  {
    load_bank(wp, ws, loadlist);
  }
  IFDEBUG(tracef("switch_bank %x -> %x\n" _ oldbank _ newbank);)
//  IFDEBUG(dump_registers(wp,ws);)
}

1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
/**************************************************************************
*                                                                         *
*    Pixel format information                                             *
*                                                                         *
**************************************************************************/

extern const PixelFormatInfo *pixelformat_info(int format);

static int PIXELFORMAT_ALPHA_MASK(int format)
{
  const PixelFormatInfo *pf=pixelformat_info(format);
  return ((1<<pf->bits[3])-1)<<(pf->top[3]-pf->bits[3]);
}

#define PIXELFORMAT_ALPHA_IMM(format) (pixelformat_info(format)->alphaimm12 | (1<<25))

static PixelFormat compute_pixelformat(int ncolour,int modeflags,int log2bpp)
{
  tracef("compute_pixelformat: %x %x %d\n" _ ncolour _ modeflags _ log2bpp);
  if(log2bpp <= 3)
    return (PixelFormat) log2bpp;
  PixelFormat baseformat;
  if(log2bpp == 4)
  {
    if(modeflags & ModeFlag_64k)
    {
      baseformat = PixelFormat_16bpp;
      modeflags &= ~ModeFlag_DataFormatSub_Alpha;
    }
    else if(ncolour < 4096)
      baseformat = PixelFormat_12bpp;
    else
      baseformat = PixelFormat_15bpp;
  }
  else if(log2bpp == 6)
  {
    baseformat = PixelFormat_24bpp;
    modeflags &= ~ModeFlag_DataFormatSub_Alpha;
  }
  else
  {
    /* Assume 32bpp */
    baseformat = PixelFormat_32bpp;
  }
  if(modeflags & ModeFlag_DataFormatSub_RGB)
    baseformat = (PixelFormat) (baseformat | PixelFormat_RGB);
  if(modeflags & ModeFlag_DataFormatSub_Alpha)
    baseformat = (PixelFormat) (baseformat | PixelFormat_Alpha);
  return baseformat;
}

/**************************************************************************
*                                                                         *
1496
*    Blending utility functions                                           *
1497 1498 1499
*                                                                         *
**************************************************************************/

1500
static BlendImpl compute_blendimpl(asm_workspace *wp, workspace *ws, BOOL *use_sprite_palette)
1501
{
1502 1503 1504 1505
  if(!wp->blending)
    return BlendImpl_None;

  if((ws->out_pixelformat <= PixelFormat_8bpp) && !(wp->blending & 2))
1506
  {
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
    /* Use blend table
       If src is true colour, use ColourTTR to convert to screen first */
    if(wp->TTRType == TTRType_Normal+TTRType_Optional)
    {
      /* Ignore TTR and use sprite palette directly for a higher quality blend. */
      wp->ColourTTR = 0; /* Nuke the pointer so that we don't attempt to allocate registers for it */
      wp->TTRType = TTRType_None;
      *use_sprite_palette = TRUE;
      tracef("** ignoring ColourTTR due to BlendTable usage **\n");
    }
    return BlendImpl_BlendTable;
1518
  }
1519
  else if(ws->out_pixelformat >= PixelFormat_8bpp)
1520
  {
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    /* True colour blend */
    if(ws->out_pixelformat == PixelFormat_8bpp)
    {
      if(wp->TTRType == TTRType_32K+TTRType_Optional)
      {
        /* Ignore TTR if we're going to use InverseTable blending method with true colour source
           TODO - Allow to be ignored if palettised source, just need to look up via palette instead */
        wp->ColourTTR = 0;
        wp->TTRType = TTRType_None;
        tracef("** ignoring ColourTTR due to InverseTable usage with true colour source **\n");
      }
      return BlendImpl_InverseTable;
    }
    else
    {
      return BlendImpl_True;
    }
1538
  }
1539
  else
1540
  {
1541 1542 1543 1544 1545 1546
    /* Screen is <= 4bpp, use lots of blendtables
       If src is true colour, use ColourTTR to convert to screen
       Else use palette index directly */
    return BlendImpl_BlendTables;
  }
}
1547

1548 1549 1550
static void blendimpl_rn(asm_workspace *wp, workspace *ws)
{
  (void) wp;
1551

1552
  switch(ws->blendimpl)
1553
  {
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
  case BlendImpl_InverseTable:
  case BlendImpl_True:
  case BlendImpl_BlendTables:
    if(wp->blending & 2)
    {
      RN(r_alpha, -1, REGFLAG_PERPIXEL+REGFLAG_TEMPORARY, "alpha for pixel blending temporary values");
    }
    if(wp->blending & 1)
    {
      RN(r_translucency, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "sprite translucency value");
      if(!(wp->blending & 2))
      {
        /* Set r_alpha == r_translucency */
        RN(r_alpha, (int) &ws->regnames.r_translucency, REGFLAG_USED, "sprite translucency again");
      }
    }
    break;
  }

  switch(ws->blendimpl)
  {
  default:
    assert(0, ERROR_FATAL);
  case BlendImpl_None:
    break;
  case BlendImpl_InverseTable:
    RN(r_screenpalette, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "Screen 8bpp -> 15bpp table");
    RN(r_inversetable, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "15bpp -> screen table");
    /* Fall through... */
  case BlendImpl_True:
    assert(ws->regnames.r_outword.flags, ERROR_FATAL);
    ws->regnames.r_outword.flags |= REGFLAG_PERPIXEL;
    if(SOURCE_ALPHAMASK && !ISTRANS)
    {
      assert(ws->regnames.r_maskinword.flags, ERROR_FATAL);
      ws->regnames.r_maskinword.flags |= REGFLAG_PERPIXEL;
    }
    if(DEST_32_BIT)
    {
      assert(ws->regnames.r_outptr.flags, ERROR_FATAL);
      ws->regnames.r_outptr.flags |= REGFLAG_PERPIXEL;
    }
    break;
  case BlendImpl_BlendTable:
    RN(r_blendtable, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "Blend table ptr");
    assert(ws->regnames.r_outword.flags, ERROR_FATAL);
    ws->regnames.r_outword.flags |= REGFLAG_PERPIXEL;
    break;
  case BlendImpl_BlendTables:
    RN(r_blendtable, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "Blend tables ptr");
    assert(ws->regnames.r_outword.flags, ERROR_FATAL);
    ws->regnames.r_outword.flags |= REGFLAG_PERPIXEL;
    if(SOURCE_ALPHAMASK && !ISTRANS)
    {
      assert(ws->regnames.r_maskinword.flags, ERROR_FATAL);
      ws->regnames.r_maskinword.flags |= REGFLAG_PERPIXEL;
    }
    break;
  }

  /* Extra case: Colour mapping to <=8bpp dest needs r_inversetable */
  if((ws->blendimpl != BlendImpl_InverseTable) && (wp->TTRType == TTRType_ColourMap) && (wp->BPP <= 8))
  {
    RN(r_inversetable, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "15bpp -> screen table");
  }
}

static void blendimpl_init(asm_workspace *wp, workspace *ws)
{
  switch(ws->blendimpl)
  {
  default:
    assert(0, ERROR_FATAL);
  case BlendImpl_None:
  case BlendImpl_True:
    break;
  case BlendImpl_BlendTable:
    LDR_WP(r_blendtable, blendtables);
    break;
  case BlendImpl_InverseTable:
    LDR_WP(r_screenpalette, screenpalette);
    LDR_WP(r_inversetable, inversetable);
    break;
  case BlendImpl_BlendTables:
    MOV(R(r_blendtable), OP2R(R(wp)), "MOV     r_blendtable, wp");
    ADD_A(r_blendtable, (((char *) wp->blendtables)-((char *) wp))-4); /* Offset by -4 so that alpha levels 1-6 index the tables */
    break;
  }

  /* Extra case: Colour mapping to <=8bpp dest needs r_inversetable */
  if((ws->blendimpl != BlendImpl_InverseTable) && (wp->TTRType == TTRType_ColourMap) && (wp->BPP <= 8))
  {
    LDR_WP(r_inversetable, inversetable);
  }
}

static void blendimpl_gettables(asm_workspace *wp, workspace *ws, BOOL use_sprite_palette)
{
  _kernel_oserror *e;
  int flags,src1,src2;

  /* Protect some sensitive workspace vars which will be overwritten by recursive SpriteExtend calls */
  int spritecode = wp->spritecode;

  /* Extra case: Colour mapping to <=8bpp dest needs r_inversetable
     In 8bpp modes this will actually be an inversetable, but in < 8bpp we use
     ColourTrans, as InverseTable doesn't support < 8bpp  */
  if((ws->blendimpl != BlendImpl_InverseTable) && (wp->TTRType == TTRType_ColourMap) && (wp->BPP <= 8))
  {
    if(wp->BPP == 8)
    {
      e = _swix(InverseTable_Calculate,_OUTR(0,1),&wp->screenpalette,&wp->inversetable);
    }
    else
    {
      int table[3];
      e = _swix(ColourTrans_SelectTable,_INR(0,7),(SpriteType_New16bpp<<27)+1,-1,-1,-1,table,0,0,0);
      wp->inversetable = table[1];
    }
    if(e)
    {
      EXIT_OSERROR(e);
    }
    wp->spritecode = spritecode;
  }

  if((ws->blendimpl == BlendImpl_None) || (ws->blendimpl == BlendImpl_True))
    return;

  /* As per translate_pixel(), the only time where we don't apply the TTR for a
     BlendTable/BlendTables blend is if it's an optional normal one.
     For BlendImpl_BlendTable any table like this will have been discarded
     already (and use_sprite_palette set to TRUE), but for BlendImpl_BlendTables
     we need to detect the case here.
  */
  if(use_sprite_palette || (wp->TTRType == TTRType_Normal+TTRType_Optional))
  {
    /* Generate tables for blending from sprite to screen */
    flags = BlendTable_Lock+BlendTable_SrcSpritePointer;
    src1 = 256;
    src2 = (int) wp->save_sprite;
  }
  else
  {
    /* Generate tables using just the screen palette */
    flags = BlendTable_Lock;
    src1 = src2 = -1;
  }    

  /* Verify that recursive SpriteExtend calls aren't modifying workspace more than expected */
#ifdef DEBUG
  int backup[sizeof(asm_workspace)/4];
  for(int i=0;i<sizeof(asm_workspace)/4;i++)
    backup[i] = ((int *) wp)[i];
#endif

  switch(ws->blendimpl)
  {
  default:
    assert(0, ERROR_FATAL);
    break;
  case BlendImpl_BlendTable:
    e = _swix(BlendTable_GenerateTable,_INR(0,6)|_OUT(6),flags,src1,src2,-1,-1,wp->trns_flags2>>4,0,&wp->blendtables[0]);
    if(e)
    {
      EXIT_OSERROR(e);
    }
    break;
  case BlendImpl_InverseTable:
    e = _swix(InverseTable_Calculate,_OUTR(0,1),&wp->screenpalette,&wp->inversetable);
    if(e)
    {
      EXIT_OSERROR(e);
    }
    break;
  case BlendImpl_BlendTables:
    {
      for(int i=1;i<7;i++)
      {
        e = _swix(BlendTable_GenerateTable,_INR(0,6)|_OUT(6),flags,src1,src2,-1,-1,256-i*32,0,&wp->blendtables[i-1]);
        if(e)
        {
          EXIT_OSERROR(e);
        }
      }
    }
    break;
  }

  wp->spritecode = spritecode;
#ifdef DEBUG
  BOOL ok = TRUE;
  for(int i=0;i<sizeof(asm_workspace)/4;i++)
  {
    if((backup[i] != ((int *) wp)[i]) && ((OFFSET(i*4) < WP_OFFSET(blendtables)) || (OFFSET(i*4) > WP_OFFSET(inversetable))))
    {
      tracef("workspace modified! offset %x was %x now %x\n" _ i*4 _ backup[i] _ ((int *) wp)[i]);
      ok = FALSE;
    }
  }
  assert(ok, ERROR_FATAL);
#endif
}

/**************************************************************************
*                                                                         *
*    Register allocation                                                  *
*                                                                         *
**************************************************************************/

static int get_expansion_mask(asm_workspace *wp, workspace *ws,const PixelFormatInfo *in_fmt,const PixelFormatInfo *out_fmt,int *shift)
/* Work out pixel expansion mask and shift value for a given conversion */
{
  int pixel_expansion_mask = 0;
  int pixel_expansion_shift = 0;
  int uses = 0;
  int failures = 0;
  
  for(int i=0;i<4;i++)
  {
    /* Skip alpha channel if it doesn't exist in one or the other */
    if(!out_fmt->bits[i] || !in_fmt->bits[i])
    {
      assert(i == 3, ERROR_FATAL);
      continue;
    }
    /* If we have 1 bit alpha, and we're wanting to expand it, it's better to store it in the PSR than to mask-and-shift (especially for this first channel)
       The same technique is also worthwhile when we're shrinking down to 1bpp alpha. But not with any formats we currently support, so ignore that potential optimisation for now. */
    if((i == 3) && (in_fmt->bits[i] == 1) && (out_fmt->bits[i] >= 1))
    {
      continue;          
    }
    if(in_fmt->bits[i] < out_fmt->bits[i])
    {
      /* This is a candidate for expansion */
      if(!pixel_expansion_shift || (pixel_expansion_shift == in_fmt->bits[i]))
      {
        int bits = out_fmt->bits[i]-in_fmt->bits[i];
        int mask = (1<<bits)-1;
        int shift = out_fmt->top[i]-bits;
        pixel_expansion_mask |= mask<<shift;
        pixel_expansion_shift = in_fmt->bits[i];
        uses++;
      }
      else
        failures |= 1<<i;
    }
  }
  assert(!(failures & 5), ERROR_FATAL); /* We don't cope with red or blue being unable to use it */
  /* Was it used enough times to make setting it up worthwhile? */
  if(uses > 1)
  {
    if(shift)
      *shift = pixel_expansion_shift;
    return pixel_expansion_mask;
  }
  else
  {
    if(shift)
      *shift = 0;
    return 0;
  }
}

static int convert_pixel_rn(asm_workspace *wp, workspace *ws,PixelFormat pixelformat,const PixelFormat out_pixelformat,int need_temps)
/* Allocate registers for translating r_pixel from pixelformat to
 * out_pixelformat, without using any lookup tables etc.
 *
 * Requirements:
 * wp->is_it_jpeg valid
 */
{
  if(pixelformat == out_pixelformat)
    return need_temps;
  if((pixelformat == PixelFormat_24bpp_Grey) && (out_pixelformat <= PixelFormat_4bpp))
  {
    assert(wp->is_it_jpeg, ERROR_FATAL);
    /* Hack for JPEG data in RISC OS 3
       JPEG has produced greyscale output, but we don't have a ColourTTR to
       map it to the current palette.
       Assuming default Wimp palettes, convert the output manually */
    pixelformat = out_pixelformat;
  }
  else if((pixelformat == PixelFormat_32bpp) && (out_pixelformat == PixelFormat_8bpp))
  {
    assert(wp->is_it_jpeg, ERROR_FATAL);
    need_temps = 2;
    pixelformat = PixelFormat_8bpp;
  }
  else if((pixelformat == PixelFormat_24bpp_Grey) && (out_pixelformat >= PixelFormat_12bpp))
  {
    /* 24bpp grey is equivalent 24bpp/32bpp colour with out_pixelformat RGB order */

    /* Minor optimisation, pick 24bpp/32bpp to allow the giant if() block below to be skipped */
    if((out_pixelformat & PixelFormat_BPPMask) == PixelFormat_24bpp)
      pixelformat = PixelFormat_24bpp;
    else
      pixelformat = PixelFormat_32bpp;

    /* And pick right RGB order */
    pixelformat = (PixelFormat) (pixelformat | (out_pixelformat & PixelFormat_RGB));

    /* TODO - 24 grey handling should be folded into the if() below, so that reducing 24 grey to <=16bpp can be optimised */
  }
  
  if(pixelformat != out_pixelformat)
  {
    assert((pixelformat > PixelFormat_8bpp) && (out_pixelformat > PixelFormat_8bpp), ERROR_FATAL);
    int flags = pixelformat & (PixelFormat_Alpha | PixelFormat_RGB);
    pixelformat = (PixelFormat) (pixelformat & PixelFormat_BPPMask);
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
    int out_flags = out_pixelformat & (PixelFormat_Alpha | PixelFormat_RGB);
    PixelFormat out_format = (PixelFormat) (out_pixelformat & PixelFormat_BPPMask);
    assert(out_format != PixelFormat_32bpp_Hi, ERROR_FATAL);
    if((pixelformat != out_format) && (pixelformat >= PixelFormat_24bpp) && (out_format >= PixelFormat_24bpp))
    {
      /* Source & dest are both 24bpp/32bpp, but need converting between subformats */
      switch(pixelformat)
      {
      case PixelFormat_24bpp:
        pixelformat = PixelFormat_32bpp;
        break;
      case PixelFormat_32bpp:
        if(flags & PixelFormat_Alpha)
        {
          flags -= PixelFormat_Alpha;
        }
        pixelformat = PixelFormat_24bpp;
        break;
      case PixelFormat_32bpp_Hi:
        assert(!(flags & PixelFormat_Alpha), ERROR_FATAL);
        pixelformat = out_format;
        break;
      }
    }
    if(pixelformat == out_format)
    {
      /* Only RGB order or alpha fixup needed */
      if((flags & PixelFormat_RGB) != (out_flags & PixelFormat_RGB))
      {
        switch(pixelformat)
        {
        case PixelFormat_12bpp:
        case PixelFormat_15bpp:
        case PixelFormat_16bpp:
          if(!need_temps)
            need_temps = 1;
          flags ^= PixelFormat_RGB;
          break;
        case PixelFormat_32bpp:
          if(flags & PixelFormat_Alpha)
          {
            if(out_flags & PixelFormat_Alpha)
            {
              if(!need_temps)
                need_temps = 1;
              flags ^= PixelFormat_RGB;
              break;
            }
            flags -= PixelFormat_Alpha;
          }
          /* Else fall through to 24bpp case */
        case PixelFormat_24bpp:
          if(!need_temps)
            need_temps = 1;
          flags ^= PixelFormat_RGB;
          break;
        }
      }
      /* RGB order should be good. Now deal with alpha. */
      if((flags & PixelFormat_Alpha) != (out_flags & PixelFormat_Alpha))
      {
        flags ^= PixelFormat_Alpha;
      }
    }
    else if((pixelformat == PixelFormat_15bpp) && (out_format == PixelFormat_16bpp) && !((flags ^ out_flags) & PixelFormat_RGB))
    {
      /* Trivial case - 15bpp to 16bpp */
      if(!need_temps)
        need_temps = 1;
      pixelformat = out_format;
      flags = out_flags;
    }
    else if((pixelformat == PixelFormat_16bpp) && (out_format == PixelFormat_15bpp) && !((flags ^ out_flags) & PixelFormat_RGB))
    {
      /* Trivial case - 16bpp to 15bpp */
      if(!need_temps)
        need_temps = 1;
      pixelformat = out_format;
      flags = out_flags;
    }
    else
    {
      /* Full processing needed, so 2 temp regs */
      need_temps = 2;
  
      const PixelFormatInfo *in_fmt = pixelformat_info(pixelformat | flags);
      const PixelFormatInfo *out_fmt = pixelformat_info(out_format | out_flags);
      IFDEBUG(tracef("in format: %x { %i, %i, %i, %i }, { %i, %i, %i, %i }, %x, %x\n" _ in_fmt _ in_fmt->bits[0] _ in_fmt->bits[1] _ in_fmt->bits[2] _ in_fmt->bits[3] _ in_fmt->top[0] _ in_fmt->top[1] _ in_fmt->top[2] _ in_fmt->top[3] _ in_fmt->hints _ in_fmt->alphaimm12);)
      IFDEBUG(tracef("out format: %x { %i, %i, %i, %i }, { %i, %i, %i, %i }, %x, %x\n" _ out_fmt _ out_fmt->bits[0] _ out_fmt->bits[1] _ out_fmt->bits[2] _ out_fmt->bits[3] _ out_fmt->top[0] _ out_fmt->top[1] _ out_fmt->top[2] _ out_fmt->top[3] _ out_fmt->hints _ out_fmt->alphaimm12);)

      /* Work out if we need r_expansionmask, and if so, what it should be */
1955 1956 1957
      int pixel_expansion_mask;
      pixel_expansion_mask = get_expansion_mask(wp,ws,in_fmt,out_fmt,NULL);
      if(pixel_expansion_mask)
1958
      {
1959
        for(int i=0;i<2;i++)
1960
        {
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
          if(!ws->pixel_expansion_mask[i])
          {
            if(i)
              RN(r_expansionmask2, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "constant for colour channel expansion")
            else
              RN(r_expansionmask1, -1, REGFLAG_PERPIXEL+REGFLAG_CONSTANT, "constant for colour channel expansion")
            ws->pixel_expansion_mask[i] = pixel_expansion_mask;
            break;
          }
          else if(ws->pixel_expansion_mask[i] == pixel_expansion_mask)
1971
          {
1972 1973
            /* An existing mask matches, use it */
            break;
1974 1975
          }
          else
1976 1977 1978 1979
          {
            /* If we've run out of mask registers we're in trouble, as there's currently no fallback case */
            assert(i != 1, ERROR_FATAL);
          }
1980 1981
        }
      }
1982

1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
      /* And that should be it */
      pixelformat = out_format;
      flags = out_flags;
    }
    /* Recombine pixelformat */
    pixelformat = (PixelFormat) (pixelformat | flags);
  }

  assert(pixelformat == out_pixelformat, ERROR_FATAL); /* If this hasn't happened, we haven't completed the transformation. */

  return need_temps;
}

1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125
static void blend_extract_alpha_rn(asm_workspace *wp, workspace *ws, PixelFormat in_pixelformat,int *alpha_top,int *alpha_bits)
/* Extract the sprite alpha channel into r_alpha, ready for blending later on */
{
  const PixelFormatInfo *in_fmt = pixelformat_info(in_pixelformat);
  if(in_fmt->bits[3] == 8)
  {
    assert(in_fmt->top[3] == 32, ERROR_FATAL);
    *alpha_top = *alpha_bits = 8;
  }
  else
  {
    assert(in_fmt->top[3] == 16, ERROR_FATAL);
    *alpha_top = 16;
    *alpha_bits = in_fmt->bits[3];
  }
}

static int blend_rgb_rn(asm_workspace *wp, workspace *ws, PixelFormat in_pixelformat, int alpha_top, int alpha_bits, BOOL have_dithered, int need_temps, PixelFormat *blend_pixelformat)
{
  UNUSED(alpha_top);
  /* Mirror the structure of blend_rgb */

  /* Work out which pixel format the blend calculation is performed in */
  *blend_pixelformat = ws->out_pixelformat;
  if(ws->odither && !have_dithered)
  {
    /* Dithering requested, blend in 32bpp */
    *blend_pixelformat = (PixelFormat) (PixelFormat_32bpp | (ws->out_pixelformat & (PixelFormat_RGB | PixelFormat_Alpha)));
  }
  else if(*blend_pixelformat == PixelFormat_8bpp)
  {
    /* 8bpp output, blend in 15bpp ready for table lookup */
    *blend_pixelformat = PixelFormat_15bpp;
  }

  if((alpha_bits == 1) && !(wp->blending & 1))
  {
    /* Special 1bpp case */
    return convert_pixel_rn(wp,ws,in_pixelformat,*blend_pixelformat,need_temps);
  }

  /* Standard code, 2 temps needed */
  if(need_temps < 2)
    need_temps = 2;

  return need_temps;
}

static PixelFormat apply_dither_rn(asm_workspace *wp, workspace *ws, PixelFormat pixelformat, BOOL *have_dithered, int *need_temps)
{
  if (ws->odither && !*have_dithered)
  {
    if(((pixelformat & PixelFormat_BPPMask) == PixelFormat_12bpp)
      && (ws->out_pixelformat >= PixelFormat_4bpp))
    {
      /* When dithering 12bpp down to 4bpp/8bpp, we need to
         convert to 32bpp to make it look good, and to fix
         bugs with the dither value writing into the wrong
         colour channels */
      PixelFormat ditherformat = (PixelFormat) (PixelFormat_32bpp | (pixelformat & (PixelFormat_RGB | PixelFormat_Alpha)));
      *need_temps = convert_pixel_rn(wp,ws,pixelformat,ditherformat,*need_temps);
      pixelformat = ditherformat;
    }
  }
  return pixelformat;
}

static PixelFormat apply_ttr_rn(asm_workspace *wp, workspace *ws, PixelFormat pixelformat, BOOL *have_dithered, int *need_temps)
/* Apply the TTR, and perform any dithering if necessary */
{
  switch(wp->TTRType & ~TTRType_Optional)
  {
  case TTRType_None:
    break;
  case TTRType_Normal:
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_Wide:
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_32K:
    /* Hack - skip if this is JPEG and we're already correct */
    if(wp->is_it_jpeg && (pixelformat <= PixelFormat_8bpp))
    {
      assert(pixelformat == ws->out_pixelformat,ERROR_FATAL);
      break;
    }
    /* If we're applying a 32K table, now is our last chance to perform dithering */
    pixelformat = apply_dither_rn(wp,ws,pixelformat,have_dithered,need_temps);
    if(pixelformat != ws->ColourTTRFormat)
      *need_temps = convert_pixel_rn(wp,ws,pixelformat,ws->ColourTTRFormat,*need_temps);
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_ColourMap:
    if(pixelformat != ws->ColourTTRFormat)
      *need_temps = convert_pixel_rn(wp,ws,pixelformat,ws->ColourTTRFormat,*need_temps);
    /* 1 temp needed if we need to preserve alpha
       However for sprtrans we claim we need two temps, as r_temp1 clashes with r12 */
    if((ws->ColourTTRFormat & PixelFormat_Alpha) && (ws->out_pixelformat & PixelFormat_Alpha))
    {
      if(ISTRANS)
        *need_temps = 2;
      else if(!*need_temps)
        *need_temps = 1;
    }
    /* 1-instruction translation from 32bpp_Hi to something closer to the output format */
    if(((ws->out_pixelformat & ~PixelFormat_Alpha) == PixelFormat_32bpp + PixelFormat_RGB) && (wp->CPUFlags & CPUFlag_REV))
    {
      pixelformat = (PixelFormat) (PixelFormat_32bpp+PixelFormat_RGB);
    }
    else
    {
      pixelformat = PixelFormat_32bpp;
    }
    /* Apply r_inversetable if we have <=8bpp output */
    if(wp->BPP <= 8)
    {
      pixelformat = apply_dither_rn(wp,ws,pixelformat,have_dithered,need_temps);
      *need_temps = convert_pixel_rn(wp,ws,pixelformat,PixelFormat_15bpp,*need_temps);
      pixelformat = ws->out_pixelformat;
    }
    break;
  case TTRType_Palette:
    pixelformat = PixelFormat_32bpp_Hi;
    break;
  }

  return pixelformat;
}

2126 2127 2128 2129 2130 2131 2132
static int translate_pixel_rn(asm_workspace *wp, workspace *ws, int need_temps)
{
  /* Work out whether we need 16->32 or 32->16 transformations, with their temp registers
   * So, mirror the structure of translate_pixel
   */

  PixelFormat pixelformat = ws->in_pixelformat;
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150

  BOOL ttr_before_blend = TRUE;
  switch(ws->blendimpl)
  {
  case BlendImpl_BlendTables:
    ttr_before_blend = FALSE;
    break;
  }

  int alpha_top = 0;
  int alpha_bits = 0;
  if((wp->blending & 2) && !(wp->save_mode & 0x80000000))
  {
    blend_extract_alpha_rn(wp,ws,pixelformat,&alpha_top,&alpha_bits);
  }

  BOOL have_dithered = FALSE;
  if(ttr_before_blend && wp->TTRType)
2151
  {
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162
    pixelformat = apply_ttr_rn(wp,ws,pixelformat,&have_dithered,&need_temps);
  }  

  /* Blending */
  int alpha_shift = 0;
  switch(ws->blendimpl)
  {
  case BlendImpl_BlendTable:
    /* Single blend table */
    /* JPEG might need translating to out_pixelformat before the blend can take place */
    if((wp->is_it_jpeg) && (pixelformat > PixelFormat_8bpp))
2163
    {
2164 2165
      pixelformat = apply_dither_rn(wp,ws,pixelformat,&have_dithered,&need_temps);
      need_temps = convert_pixel_rn(wp,ws,pixelformat,ws->out_pixelformat,need_temps);
2166 2167
      pixelformat = ws->out_pixelformat;
    }
2168 2169 2170 2171 2172 2173
    if(!need_temps)
      need_temps = 1;
    pixelformat = ws->out_pixelformat;
    break;
  case BlendImpl_InverseTable:
    if(pixelformat == PixelFormat_8bpp)
2174
    {
2175
      pixelformat = PixelFormat_15bpp;
2176
    }
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
    /* Fall through... */
  case BlendImpl_True:
    /* True colour blend */
    assert(pixelformat > PixelFormat_8bpp, ERROR_FATAL);
    need_temps = blend_rgb_rn(wp,ws,pixelformat,alpha_top,alpha_bits,have_dithered,need_temps,&pixelformat);
    break;
  case BlendImpl_BlendTables:
    /* Screen is <= 4bpp, use lots of blendtables
       If src is true colour, use ColourTTR to convert to screen
       Else use palette index directly */
    /* r_translucency (if used) assumed to be 0-256 alpha */
    assert(wp->blending & 2, ERROR_FATAL);
    if (SOURCE_ALPHAMASK)
2190
    {
2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
      /* Alpha mask */
      if (wp->blending & 1)
      {
      }
      else
      {
        alpha_shift = 5;
      }
      alpha_bits = 3;  
    }
    else if (alpha_bits)
    {
      /* Alpha channel */
      unsigned int chan_mask = ((1<<alpha_bits)-1)<<(alpha_top-alpha_bits);
      if (wp->blending & 1)
      {
        if (alpha_bits == 1)
        {
        }
        else
        {
          assert(alpha_bits >= 4, ERROR_FATAL);
          if(chan_mask == 0xff)
          {
          }
          else
          {
            assert(chan_mask == 0xf000, ERROR_FATAL);
          }  
        }
        alpha_bits = 3;  
      }
      else
      {
        chan_mask &= ~(chan_mask>>3);
        alpha_shift = alpha_top-3;
        alpha_bits = (alpha_bits>3?3:alpha_bits); /* Should be 1 or 3, asserted below */
      }
2229 2230 2231
    }
    else
    {
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
      /* Ordinary translucent plotting */
      /* This shouldn't happen, should be handled by single blendtable case above */
      assert(0, ERROR_FATAL);
    }

    /* Apply TTR here, unless it's an optional normal TTR (in which case we
       only apply for full alpha pixels) */
    assert(!ttr_before_blend, ERROR_FATAL);
    if(wp->TTRType != TTRType_Normal+TTRType_Optional)
    {
      pixelformat = apply_ttr_rn(wp,ws,pixelformat,&have_dithered,&need_temps);
    }

    if(alpha_bits == 3)
    {
      if(!need_temps)
        need_temps = 1;
2249 2250
    }
    pixelformat = ws->out_pixelformat;
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
    break;
  }

  pixelformat = apply_dither_rn(wp,ws,pixelformat,&have_dithered,&need_temps);

  switch(ws->blendimpl)
  {
  case BlendImpl_InverseTable:
    /* Inverse table lookup for 15bpp -> palette */
    if(pixelformat != PixelFormat_8bpp)
    {
      if(pixelformat != PixelFormat_15bpp)
      {
        need_temps = convert_pixel_rn(wp,ws,pixelformat,PixelFormat_15bpp,need_temps);
      }
      pixelformat = PixelFormat_8bpp;              /* we've finished */
    }
    break;
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
  }

  /* Do any extra conversion necessary */
  if(pixelformat != ws->out_pixelformat)
  {
    need_temps = convert_pixel_rn(wp,ws,pixelformat,ws->out_pixelformat,need_temps);
    pixelformat = ws->out_pixelformat;
  }

  return need_temps;
}

/**************************************************************************
*                                                                         *
*    Register initialisation.                                             *
*                                                                         *
**************************************************************************/

static void dither_expansion_init(asm_workspace *wp, workspace *ws)
/* Initialise the ordered dither & pixel format expansion registers
 *
 * Requirements:
 * convert_pixel_rn() called
 * ws->odither valid
 * r_oditheradd allocated if necessary
 */
{
2296
  for(int i=0;i<2;i++)
2297
  {
2298 2299 2300 2301
    int mask = ws->pixel_expansion_mask[i];
    if(!mask)
      break;

2302 2303
#ifdef DEBUG
    char a[256];
2304
    do_sprintf(a,"Generate expansion mask &%x",mask);
2305
    comment(ws,a);
2306
    const char *regname;
2307
#endif
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
    int regno;
    if(i)
    {
      regno = R(r_expansionmask2);
      IFDEBUG(regname = "r_expansionmask2";)
    }
    else
    {
      regno = R(r_expansionmask1);
      IFDEBUG(regname = "r_expansionmask1";)
    }
 
    if(mask == 0xF0F0F0F0)
2321 2322
    {
      /* We can do this in two or three instructions */
2323
      if(wp->CPUFlags & CPUFlag_T2)
2324
      {
2325 2326
        IFDEBUG(do_sprintf(a,  "MOVW    %s,#&f0f0",regname);)
        MOVW(regno, IMM16(0xF0F0),a);
2327 2328 2329
      }
      else
      {
2330 2331 2332 2333
        IFDEBUG(do_sprintf(a,  "MOV     %s,#&f0",regname);)
        MOV(regno, IMM(15) | IMMROR(28),a);
        IFDEBUG(do_sprintf(a,  "ORR     %s,%s,LSL #8",regname,regname);)
        ORR(regno, regno, OP2R(regno) | LSLI(8),a);
2334
      }
2335 2336
      IFDEBUG(do_sprintf(a,    "ORR     %s,%s,LSL #16",regname,regname);)
      ORR(regno, regno, OP2R(regno) | LSLI(16),a);
2337 2338 2339 2340
    }
    else
    {
      /* Other masks may be anywhere between one and three instructions, do it a byte at a time */
2341 2342 2343
      assert(mask & 0xff, ERROR_FATAL); /* We expect red & blue to need expansion, and for one of them to tbe in the bottom byte */
      assert(!(mask & 0xff000000), ERROR_FATAL); /* We don't expect 32bpp alpha to need expansion. We'll either be coming from 4bpp (in which case we'll use 0xF0F0F0F0) or from 1bpp (in which case we'll use TST) */
      if(wp->CPUFlags & CPUFlag_T2)
2344
      {
2345 2346
        IFDEBUG(do_sprintf(a,  "MOVW    %s,#&%x",regname,mask & 0xffff);)
        MOVW(regno, IMM16(mask),a);
2347 2348 2349
      }
      else
      {
2350 2351 2352
        IFDEBUG(do_sprintf(a,  "MOV     %s,#&%x",regname,mask & 0xff);)
        MOV(regno, IMM(mask & 0xff),a);
        if(mask & 0xff00)
2353
        {
2354 2355
          IFDEBUG(do_sprintf(a,"ORR     %s,%s,#&%x",regname,regname,mask & 0xff00);)
          ORR(regno, regno, IMM((mask>>8)&0xff) | IMMROR(24),a);
2356 2357
        }
      }
2358
      if(mask & 0xff0000)
2359
      {
2360 2361
        IFDEBUG(do_sprintf(a,  "ORR     %s,%s,#&%x",regname,regname,mask & 0xff0000);)
        ORR(regno, regno, IMM((mask>>16)&0xff) | IMMROR(16),a);
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
      }
    }
  }

  if (ws->odither)
  {
    /* We use ordered dither to attempt to increase the output resolution by almost two bits.
     * This only happens for a 16bpp or 32bpp source that's being truncated somewhat.
     * A square of output pixels has the following binary addition values:
     *              11    01
     *              00    10
     * These values are added to the value of each or R/G/B, just before those values are
     * truncated or looked up in a table, shifted so that we add to the bits which are
     * just about to be discarded.
     * We keep the value to add in r_oditheradd.
     * To proceed along the x axis we EOR by 10 every output pixel.
     * We must also EOR by 01 every line.
     * The starting value must be aligned with the origin of the output.
     */
    comment(ws, "Compute initial dither addition value - bit 0 changes every y, bit 1 every x");
    LDR_WP(r_pixel, save_xcoord)
    AND(R(r_pixel), R(r_pixel), IMM(1),                        "AND     r_pixel,r_pixel,#1               ; least sig bit of x, for dither");
    LDR_WP(r_oditheradd, save_ycoord)
    AND(R(r_oditheradd), R(r_oditheradd), IMM(1),              "AND     r_oditheradd,r_oditheradd,#1     ; least sig bit of y, for dither");
    EOR(R(r_pixel),R(r_pixel),OP2R(R(r_oditheradd)),           "EOR     r_pixel,r_pixel,r_oditheradd     ; if we start Y off on an odd footing, invert x as well");
    ORR(R(r_oditheradd), R(r_oditheradd),
    OP2R(R(r_pixel)) | LSLI(1),                                "ORR     r_oditheradd,r_oditheradd,r_pixel,LSL #1 ; dither add value");

    /* The dither should start based on the current ECF offset */
    LDR_WP(r_pixel, ecfyoffset_ptr)
    LDR_INDEX(r_pixel,r_pixel,0,"get kernel variable ECFYOffset")
    TST(R(r_pixel),IMM(1),                                     "TST     r_pixel,#1                       ; is Y ECF offset odd?");
    EOR(R(r_oditheradd),R(r_oditheradd),NE | IMM(3),           "EORNE   r_oditheradd,r_oditheradd,#3     ; if so, change ordered dither origin to match");

    LDR_WP(r_pixel, ecfshift_ptr)
    LDR_INDEX(r_pixel,r_pixel,0,"get kernel variable ECFShift")

    TST(R(r_pixel),IMM(wp->BPP),                               "TST     r_pixel,#out_bpp                 ; is ECF Shift an odd number of pixels?");
    EOR(R(r_oditheradd),R(r_oditheradd),NE | IMM(2),           "EORNE   r_oditheradd,r_oditheradd,#2     ; if so, change ordered dither origin to match");

    /* Shift the dither value to the top of the register. */
    {
      IFDEBUG(char a[256];)
      IFDEBUG(do_sprintf(a, "MOV     r_oditheradd,r_oditheradd,LSL #%i %t40; shift to top of word", 23 + ws->odither);)
      MOV(R(r_oditheradd), OP2R(R(r_oditheradd)) | LSLI(23 + ws->odither), a);
    }
  }
}

/**************************************************************************
*                                                                         *
*    Pixel translation                                                    *
*                                                                         *
**************************************************************************/

#ifdef DEBUG
static void add_ordered_dither_gun(asm_workspace *wp, workspace *ws, int bits_per_gun, int offset, char *gun)
#else
#define add_ordered_dither_gun(a,b,c,d,e) do_add_ordered_dither_gun(a,b,c,d)
static void do_add_ordered_dither_gun(asm_workspace *wp, workspace *ws, int bits_per_gun, int offset)
#endif
/* Do one gun of the ordered dither - entirely local to add_ordered_dither below
 * Offset is the offset from bit 0 of the base of this field of the colour
 */
{
#ifdef DEBUG
  char a[128];
#endif
  int x = 32 - bits_per_gun - offset; /* amount to shift the colour field in question */
  IFDEBUG(do_sprintf(a,                                  "CMN     r_oditheradd,r_pixel,LSL #%i %t40; %s below limit?", x, gun);)
  CMN(R(r_oditheradd), OP2R(R(r_pixel)) | LSLI(x), a);

  IFDEBUG(do_sprintf(a,                                  "ADDCC   r_pixel,r_pixel,r_oditheradd,LSR #%i %t40; if not, add.", x);)
  ADD(R(r_pixel), R(r_pixel), CC | OP2R(R(r_oditheradd)) | LSRI(x), a);
  UNUSED(wp);
}

static void add_ordered_dither(asm_workspace *wp, workspace *ws,PixelFormat pixelformat)
/* The 32-bit RGB value in r_pixel should have r_oditheradd >> (32-bits_per_gun)
 * added to each of R/G/B, except that these additions should be 'sticky'
 * at 255 in each gun.
 * 
 * The resulting values are just about to be truncated somewhat, so the lo
 * bits of each answer do not matter much. Thus, if the value is currently
 * 254 we never add, but this doesn't matter.
 */
{
  int redblue_bits_per_gun, green_bits_per_gun;
2450
  switch(pixelformat & PixelFormat_BPPMask)
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
  {
  case PixelFormat_12bpp:
    redblue_bits_per_gun = green_bits_per_gun = 4;
    break;
  case PixelFormat_15bpp:
    redblue_bits_per_gun = green_bits_per_gun = 5;
    break;
  case PixelFormat_16bpp:
    redblue_bits_per_gun = 5;
    green_bits_per_gun = 6;
    break;
  default:
2463 2464 2465 2466 2467
    assert(0, ERROR_FATAL);
  case PixelFormat_24bpp_Grey:
  case PixelFormat_24bpp:
  case PixelFormat_32bpp:
/*  case PixelFormat_32bpp_Hi: - Not supported below */
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
    redblue_bits_per_gun = green_bits_per_gun = 8;
    break;
  }

  comment(ws, "Add current value for ordered dither");
  add_ordered_dither_gun(wp, ws, redblue_bits_per_gun, redblue_bits_per_gun+green_bits_per_gun, "blue");
  add_ordered_dither_gun(wp, ws, green_bits_per_gun, redblue_bits_per_gun, "green");
  add_ordered_dither_gun(wp, ws, redblue_bits_per_gun, 0, "red");
  newline();
}

static void convert_pixel(asm_workspace *wp, workspace *ws,PixelFormat pixelformat,const PixelFormat out_pixelformat)
/* Translate r_pixel from pixelformat to out_pixelformat, without using any
 * lookup tables etc.
 *
 * Requirements:
 * wp->is_it_jpeg valid
 * convert_pixel_rn() called
 * dither_expansion_init() called
 */
{
#ifdef DEBUG
  char a[256];
#endif
  if(pixelformat == out_pixelformat)
    return;
  if((pixelformat == PixelFormat_24bpp_Grey) && (out_pixelformat <= PixelFormat_4bpp))
  {
    assert(wp->is_it_jpeg, ERROR_FATAL);
    /* Hack for JPEG data in RISC OS 3
       JPEG has produced greyscale output, but we don't have a ColourTTR to
       map it to the current palette.
       Assuming default Wimp palettes, convert the output manually */
    if(out_pixelformat == PixelFormat_1bpp)
    {
      comment(ws, "Creating 0 or 1 from 24bit greyscale");
2504 2505
      MVN(R(r_pixel), OP2R(R(r_pixel)) | LSRI(7),                   "MVN     r_pixel,r_pixel,LSR #7          ; hi bit of R");
      AND(R(r_pixel), R(r_pixel), IMM(1),                           "AND     r_pixel,r_pixel,#1              ; 0->white, 1->black");
2506 2507 2508 2509 2510
      pixelformat = PixelFormat_1bpp;
    }
    else if(out_pixelformat == PixelFormat_2bpp)
    {
      comment(ws, "Creating 0,1,2 or 3 from 24bit greyscale");
2511 2512
      MVN(R(r_pixel), OP2R(R(r_pixel)) | LSRI(6),                   "MVN     r_pixel,r_pixel,LSR #6           ; hi 2 bits of R");
      AND(R(r_pixel), R(r_pixel), IMM(3),                           "AND     r_pixel,r_pixel,#3               ; 0->white, 3->black");
2513 2514 2515 2516 2517
      pixelformat = PixelFormat_2bpp;
    }
    else if (out_pixelformat == PixelFormat_4bpp)
    {
      comment(ws, "Creating wimp colour in 0..7 from 24bit greyscale");
2518 2519
      MVN(R(r_pixel), OP2R(R(r_pixel)) | LSRI(5),                   "MVN     r_pixel,r_pixel,LSR #5           ; hi 3 bits of R");
      AND(R(r_pixel), R(r_pixel), IMM(7),                           "AND     r_pixel,r_pixel,#7               ; 0->white, 7->black");
2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
      pixelformat = PixelFormat_4bpp;
    }
#if 0 /* This case is currently impossible, 24bpp greyscale output is only produced for 8bpp if a greyscale palette is in use, and a greyscale 8bpp palette is impossible on VIDC1 */
    else
    {
      /* Default 256 colour VIDC1 palette; organisation is:
       * bit 0 - tint 0
       * bit 1 - tint 1
       * bit 2 - red 2
       * bit 3 - blue 2
       * bit 4 - red 3 (high)
       * bit 5 - green 2
       * bit 6 - green 3 (high)
       * bit 7 - blue 3 (high)
       */
      comment(ws, "Creating bggrbrtt from 24bit greyscale");
      TEQ(R(r_pixel),OP2R(R(r_pixel)) | LSLI(25),                   "TEQ     r_pixel,r_pixel,LSL #25          ; check high two bits of R");
      MOV(R(r_pixel),OP2R(R(r_pixel)) | LSR(4),                     "MOV     r_pixel,r_pixel,LSR #4           ; tint bits & red 2");
      AND(R(r_pixel),R(r_pixel),IMM(7),                             "AND     r_pixel,r_pixel,#7               ; mask off the rest")
      ORR(R(r_pixel),R(r_pixel),IMM(0x28) | MI,                     "ORRMI   r_pixel,r_pixel,#&28             ; set green 2 & blue 2");
      ORR(R(r_pixel),R(r_pixel),IMM(0xd0) | CS,                     "ORRCS   r_pixel,r_pixel,#&d0             ; set RGB high bits");
      pixelformat = PixelFormat_8bpp;
    }
#endif
  }
  else if((pixelformat == PixelFormat_32bpp) && (out_pixelformat == PixelFormat_8bpp))
  {
    assert(wp->is_it_jpeg, ERROR_FATAL);
    /* Hack for JPEG data in RISC OS 3
       We're producing colour output but don't have a ColourTTR to map it
       to the current palette.
       Assume the default VIDC1 256 colour palette and map it to that. */
    /* Default 256 colour VIDC1 palette; organisation is:
     * bit 0 - tint 0
     * bit 1 - tint 1
     * bit 2 - red 2
     * bit 3 - blue 2
     * bit 4 - red 3 (high)
     * bit 5 - green 2
     * bit 6 - green 3 (high)
     * bit 7 - blue 3 (high)
     */
    comment(ws, "Creating bggrbrtt from 32bit colour");
    /* Making the tint - the average of the lower 6 of RGB isn't a bad approximation. We make this
     * by adding them all up, multiplying by 9, and dividing by 512.
     */
    AND(R(r_temp1), R(r_pixel), IMM(0x3F) | IMMROR(16),           "AND     r_temp1,r_pixel,#&3F0000         ; bottom 6 bits of B");
    MOV(R(r_temp2), OP2R(R(r_temp1)) | LSRI(16),                  "MOV     r_temp2,r_temp1,LSR #16          ; at bottom of temp2");
    AND(R(r_temp1), R(r_pixel), IMM(0x3F) | IMMROR(24),           "AND     r_temp1,r_pixel,#&3F00           ; bottom 6 bits of G");
    ADD(R(r_temp2), R(r_temp2), OP2R(R(r_temp1)) | LSRI(5),       "ADD     r_temp2,r_temp2,r_temp1,LSR #5   ; add to bottom B bits");
    AND(R(r_temp1), R(r_pixel), IMM(0x3F),                        "AND     r_temp1,r_pixel,#&3F             ; bottom 6 bits of R");
    ADD(R(r_temp2), R(r_temp2), OP2R(R(r_temp1)),                 "ADD     r_temp2,r_temp2,r_temp1          ; add to bottom B+G bits");
    ADD(R(r_temp2), R(r_temp2), OP2R(R(r_temp2)) | LSLI(3),       "ADD     r_temp2,r_temp2,r_temp2,LSL #3   ; (lo R+G+B)*9, tint value in bits 9 & 10");

    /* The hi bits are just done by extracting from the 24bpp value */
    AND(R(r_temp1), R(r_pixel), IMM(3) | IMMROR(18),              "AND     r_temp1,r_pixel,#&C000           ; both green bits");
    ORR(R(r_temp2), R(r_temp2), OP2R(R(r_temp1)),                 "ORR     r_temp2,r_temp2,r_temp1          ; merge in with tint (no shifting needed!)");

    MOV(R(r_pixel), OP2R(R(r_pixel)) | LSLI(9) | S,               "MOVS    r_pixel,r_pixel,LSL #9           ; check blue bits");
    ORR(R(r_temp2), R(r_temp2), IMM(1) | IMMROR(20) | MI,         "ORRMI   r_temp2,r_temp2,#8<<9            ; blue 2");
    ORR(R(r_temp2), R(r_temp2), IMM(1) | IMMROR(16) | CS,         "ORRCS   r_temp2,r_temp2,#128<<9          ; blue 3");

    MOV(R(r_pixel), OP2R(R(r_pixel)) | LSLI(16) | S,              "MOVS    r_pixel,r_pixel,LSL #16          ; check red bits");
    ORR(R(r_temp2), R(r_temp2), IMM(2) | IMMROR(22) | MI,         "ORRMI   r_temp2,r_temp2,#4<<9            ; red 2");
    ORR(R(r_temp2), R(r_temp2), IMM(2) | IMMROR(20) | CS,         "ORRCS   r_temp2,r_temp2,#16<<9           ; red 3");

    MOV(R(r_pixel), OP2R(R(r_temp2)) | LSRI(9),                   "MOV     r_pixel,r_temp2,LSR #9           ; shift down to final position");

    pixelformat = PixelFormat_8bpp;
  }
  else if((pixelformat == PixelFormat_24bpp_Grey) && (out_pixelformat >= PixelFormat_12bpp))
  {
    /* 24bpp grey is equivalent 24bpp/32bpp colour with out_pixelformat RGB order */

    /* Minor optimisation, pick 24bpp/32bpp to allow the giant if() block below to be skipped */
    if((out_pixelformat & PixelFormat_BPPMask) == PixelFormat_24bpp)
      pixelformat = PixelFormat_24bpp;
    else
      pixelformat = PixelFormat_32bpp;

    /* And pick right RGB order */
    pixelformat = (PixelFormat) (pixelformat | (out_pixelformat & PixelFormat_RGB));
    tracef("%t20; Treating 24bpp greyscale as %x\n" _ pixelformat);

    /* TODO - 24 grey handling should be folded into the if() below, so that reducing 24 grey to <=16bpp can be optimised */
  }

  if(pixelformat != out_pixelformat)
  {
    /* Some kind of true colour transformation needed */
    assert((pixelformat >= PixelFormat_12bpp) && (out_pixelformat >= PixelFormat_12bpp), ERROR_FATAL);
    int flags = pixelformat & (PixelFormat_Alpha | PixelFormat_RGB);
    pixelformat = (PixelFormat) (pixelformat & PixelFormat_BPPMask);
    int out_flags = out_pixelformat & (PixelFormat_Alpha | PixelFormat_RGB);
    PixelFormat out_format = (PixelFormat) (out_pixelformat & PixelFormat_BPPMask);
    assert(out_format != PixelFormat_32bpp_Hi, ERROR_FATAL);
    if((pixelformat != out_format) && (pixelformat >= PixelFormat_24bpp) && (out_format >= PixelFormat_24bpp))
    {
      /* Source & dest are both 24bpp/32bpp, but need converting between subformats */
      switch(pixelformat)
      {
      case PixelFormat_24bpp:
        pixelformat = PixelFormat_32bpp;
        break;
      case PixelFormat_32bpp:
        if(flags & PixelFormat_Alpha)
        {
          IFDEBUG(do_sprintf(a,                                  "BIC     r_pixel,r_pixel,#&%x ; Discard alpha",PIXELFORMAT_ALPHA_MASK(pixelformat | flags));)
          BIC(R(r_pixel),R(r_pixel),PIXELFORMAT_ALPHA_IMM(pixelformat | flags),a);
          flags -= PixelFormat_Alpha;
        }
        pixelformat = PixelFormat_24bpp;
        break;
      case PixelFormat_32bpp_Hi:
        assert(!(flags & PixelFormat_Alpha), ERROR_FATAL);
2635 2636 2637 2638 2639 2640 2641 2642 2643
        if((wp->CPUFlags & CPUFlag_REV) && ((flags ^ out_flags) & PixelFormat_RGB))
        {
          REV(R(r_pixel), R(r_pixel), 0,                              "REV     r_pixel,r_pixel                  ; Palette entry -> &00RRGGBB");
          flags ^= PixelFormat_RGB;
        }
        else
        {
          MOV(R(r_pixel), OP2R(R(r_pixel)) | LSRI(8),                 "MOV     r_pixel,r_pixel,LSR #8           ; Convert from palette entry");
        }
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677
        pixelformat = out_format;
        break;
      }
    }
    if(pixelformat == out_format)
    {
      /* Only RGB order or alpha fixup needed */
      if((flags & PixelFormat_RGB) != (out_flags & PixelFormat_RGB))
      {
        comment(ws, "Red/blue swap");
        switch(pixelformat)
        {
        case PixelFormat_12bpp:
          EOR(R(r_temp1), R(r_pixel), OP2R(R(r_pixel)) | LSLI(8),     "EOR     r_temp1,r_pixel,r_pixel,LSL #8   ; R ^ B");
          AND(R(r_temp1), R(r_temp1), IMM(0xF) | IMMROR(24),          "AND     r_temp1,r_temp1,#&F00");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)),               "EOR     r_pixel,r_pixel,r_temp1          ; Swap one");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(8),     "EOR     r_pixel,r_pixel,r_temp1,LSR #8   ; Swap the other");
          flags ^= PixelFormat_RGB;
          break;
        case PixelFormat_15bpp:
          EOR(R(r_temp1), R(r_pixel), OP2R(R(r_pixel)) | LSLI(10),    "EOR     r_temp1,r_pixel,r_pixel,LSL #10  ; R ^ B");
          AND(R(r_temp1), R(r_temp1), IMM(0x1F) | IMMROR(22),         "AND     r_temp1,r_temp1,#&1F<<10");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)),               "EOR     r_pixel,r_pixel,r_temp1          ; Swap one");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(10),    "EOR     r_pixel,r_pixel,r_temp1,LSR #10  ; Swap the other");
          flags ^= PixelFormat_RGB;
          break;
        case PixelFormat_16bpp:
          EOR(R(r_temp1), R(r_pixel), OP2R(R(r_pixel)) | LSLI(11),    "EOR     r_temp1,r_pixel,r_pixel,LSL #11  ; R ^ B");
          AND(R(r_temp1), R(r_temp1), IMM(0x3E) | IMMROR(22),         "AND     r_temp1,r_temp1,#&1F<<11");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)),               "EOR     r_pixel,r_pixel,r_temp1          ; Swap one");
          EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(11),    "EOR     r_pixel,r_pixel,r_temp1,LSR #11  ; Swap the other");
          flags ^= PixelFormat_RGB;
          break;
        case PixelFormat_32bpp:
2678 2679
        case PixelFormat_24bpp:
          if(wp->CPUFlags & CPUFlag_REV)
2680
          {
2681 2682
            REV(R(r_pixel), R(r_pixel), 0,                            "REV     r_pixel,r_pixel");
            if(out_flags & flags & PixelFormat_Alpha)
2683
            {
2684
              MOV(R(r_pixel), OP2R(R(r_pixel)) | RORI(8),             "MOV     r_pixel,r_pixel,ROR #8           ; Shift RGB down and preserve alpha");
2685
            }
2686 2687 2688 2689 2690 2691 2692 2693 2694
            else
            {
              MOV(R(r_pixel), OP2R(R(r_pixel)) | LSRI(8),             "MOV     r_pixel,r_pixel,LSR #8           ; Shift RGB down and discard any alpha");
              if(out_flags & PixelFormat_Alpha)
              {
                ORR(R(r_pixel), R(r_pixel), IMM(255) | IMMROR(8),     "ORR     r_pixel,r_pixel,#&FF000000       ; Set alpha");
              }
            }
            flags = out_flags;
2695
          }
2696
          else
2697
          {
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
            /* This is tricky depending on whether we have alpha or not */
            if(flags & PixelFormat_Alpha)
            {
              assert(pixelformat != PixelFormat_24bpp, ERROR_FATAL);
              if(out_flags & PixelFormat_Alpha)
              {
                /* Must preserve alpha, use the above 4-instruction sequences */
                EOR(R(r_temp1), R(r_pixel), OP2R(R(r_pixel)) | LSLI(16),  "EOR     r_temp1,r_pixel,r_pixel,LSL #16  ; R ^ B");
                AND(R(r_temp1), R(r_temp1), IMM(0xFF) | IMMROR(16),       "AND     r_temp1,r_temp1,#&FF0000");
                EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)),             "EOR     r_pixel,r_pixel,r_temp1          ; Swap one");
                EOR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(16),  "EOR     r_pixel,r_pixel,r_temp1,LSR #16  ; Swap the other");
                flags ^= PixelFormat_RGB;
                break;
              }
              /* We're discarding alpha. Get rid of it here and then use the 3 instruction R/B swap sequence */
              BIC(R(r_pixel), R(r_pixel), IMM(255) | IMMROR(8),         "BIC     r_pixel,r_pixel,#&FF000000       ; Discard alpha");
              flags -= PixelFormat_Alpha;
            }
            AND(R(r_temp1), R(r_pixel), IMM(255) | IMMROR(24),          "AND     r_temp1,r_pixel,#&FF00           ; G");
            if(!(out_flags & PixelFormat_Alpha)) /* No need to mask out this byte if we know we're overwriting it later */
            {
              BIC(R(r_pixel), R(r_pixel), IMM(255) | IMMROR(24),        "BIC     r_pixel,r_pixel,#&FF00           ; R & B remain");
            }
            ORR(R(r_pixel), R(r_temp1), OP2R(R(r_pixel)) | RORI(16),    "ORR     r_pixel,r_temp1,r_pixel,ROR #16  ; Swapped");
            flags ^= PixelFormat_RGB;
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
          }
          break;
        }
      }
      /* RGB order should be good. Now deal with alpha. */
      if((flags & PixelFormat_Alpha) != (out_flags & PixelFormat_Alpha))
      {
        if(flags & PixelFormat_Alpha)
        {
          IFDEBUG(do_sprintf(a,                                  "BIC     r_pixel,r_pixel,#&%x      ; Discard alpha",PIXELFORMAT_ALPHA_MASK(pixelformat | flags));)
          BIC(R(r_pixel),R(r_pixel),PIXELFORMAT_ALPHA_IMM(pixelformat | flags),a);
        }
        else
        {
          IFDEBUG(do_sprintf(a,                                  "ORR     r_pixel,r_pixel,#&%x      ; Set alpha",PIXELFORMAT_ALPHA_MASK(pixelformat | out_flags));)
          ORR(R(r_pixel),R(r_pixel),PIXELFORMAT_ALPHA_IMM(pixelformat | out_flags),a);
        }
        flags ^= PixelFormat_Alpha;
      }
    }
    else if((pixelformat == PixelFormat_15bpp) && (out_format == PixelFormat_16bpp) && !((flags ^ out_flags) & PixelFormat_RGB))
    {
      /* Trivial case - 15bpp to 16bpp */
      MOV(R(r_temp1), OP2R(R(r_pixel)) | LSRI(5),                "MOV     r_temp1,r_pixel,LSR #5          ; B & G");
      TST(R(r_pixel), IMM(2) | IMMROR(24),                       "TST     r_pixel,#16<<5                  ; Top bit of green");
      AND(R(r_pixel), R(r_pixel), IMM(31),                       "AND     r_pixel,r_pixel,#31             ; R");
      ORR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSLI(6),    "ORR     r_pixel,r_pixel,r_temp1,LSL #6  ; B & G shifted across one bit");
      ORR(R(r_pixel), R(r_pixel), NE | IMM(32),                  "ORRNE   r_pixel,r_pixel,#32             ; Set low green bit to right value");
      pixelformat = out_format;
      if(flags & PixelFormat_Alpha)
        BIC(R(r_pixel), R(r_pixel), IMM(1) | IMMROR(16),         "BIC     r_pixel,r_pixel,#&10000         ; Discard alpha");
      flags = out_flags;
    }
    else if((pixelformat == PixelFormat_16bpp) && (out_format == PixelFormat_15bpp) && !((flags ^ out_flags) & PixelFormat_RGB))
    {
      /* Trivial case - 16bpp to 15bpp */
      MOV(R(r_temp1), OP2R(R(r_pixel)) | LSRI(6),                "MOV     r_temp1,r_pixel,LSR #6          ; B & G");
      AND(R(r_pixel), R(r_pixel), IMM(31),                       "AND     r_pixel,r_pixel,#31             ; R");
      ORR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSLI(5),    "ORR     r_pixel,r_pixel,r_temp1,LSL #5  ; recombined");
      pixelformat = out_format;
      if(out_flags & PixelFormat_Alpha)
        ORR(R(r_pixel), R(r_pixel), IMM(2) | IMMROR(18),         "ORR     r_pixel,r_pixel,#&8000          ; Set alpha");
      flags = out_flags;
    }
    else
    {
      /* Full processing needed. Pull it apart and put it back together in the right format.
         Basic procedure is:
         * use AND to extract a component
         * ORR it into the output pixel, shifting as appropriate
         * Repeat above for all channels
         * AND with bit expansion mask, and ORR that in at correct shift
         * If alpha needs expanding, and can't use main expansion mask, expand it manually using an immediate constant
         * Also if we've upgraded from 16bpp to 24/32bpp, green needs manual expansion */

      const PixelFormatInfo *in_fmt = pixelformat_info(pixelformat | flags);
      const PixelFormatInfo *out_fmt = pixelformat_info(out_format | out_flags);
      /* Try hard to eliminate an extra instruction by choosing the right component to start with */
      int done_channels = 0;
      for(int i=3;i>=0;i--)
      {
        /* Skip alpha channel if it doesn't exist in one or the other */
        if(!out_fmt->bits[i] || !in_fmt->bits[i])
        {
          assert(i == 3, ERROR_FATAL);
          done_channels = 8;
          continue;
        }
        /* If we have 1 bit alpha, and we're wanting to expand it, it's better to store it in the PSR than to mask-and-shift (especially for this first channel)
           The same technique is also worthwhile when we're shrinking down to 1bpp alpha. But not with any formats we currently support, so ignore that potential optimisation for now. */
        if((i == 3) && (in_fmt->bits[i] == 1) && (out_fmt->bits[i] >= 1))
        {
2795
          IFDEBUG(do_sprintf(a,                                  "TST     r_pixel,#1<<%i                  ; check alpha",in_fmt->top[3]-1);)
2796 2797 2798 2799
          TST(R(r_pixel),IMM12(1<<(in_fmt->top[3]-1)),a);
          done_channels |= 1<<i;
          continue;          
        }
2800 2801
        BOOL shift_down_possible = (out_fmt->top[i] == out_fmt->bits[i]) && (out_fmt->bits[i] <= in_fmt->bits[i]);
        if(shift_down_possible && (in_fmt->hints & (HINT_HIGHEST<<i)))
2802 2803 2804 2805 2806 2807 2808
        {
          /* We can merely shift this down into place */
          IFDEBUG(do_sprintf(a,                                  "MOV     r_temp2,r_pixel,LSR #%i         ; reposition %s",in_fmt->top[i]-out_fmt->top[i],COMPONENT_NAME(i));)
          MOV(R(r_temp2), OP2R(R(r_pixel)) | LSRI(in_fmt->top[i]-out_fmt->top[i]),a);
          done_channels |= 1<<i;
          break;
        }
2809 2810 2811 2812 2813 2814 2815 2816
        else if(shift_down_possible && (wp->CPUFlags & CPUFlag_T2))
        {
          /* Use UBFX to extract and shift down */
          IFDEBUG(do_sprintf(a,                                  "UBFX    r_temp2,r_pixel,#%i,#%i     ; extract & reposition %s",in_fmt->top[i]-out_fmt->bits[i],out_fmt->bits[i],COMPONENT_NAME(i));)
          UBFX(R(r_temp2),R(r_pixel),in_fmt->top[i]-out_fmt->bits[i],out_fmt->bits[i],0,a);
          done_channels |= 1<<i;
          break;          
        }
2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862
#if 0 /* No current output formats will satisfy these conditions */
        else if((in_fmt->top[i] == in_fmt->bits[i]) && (out_fmt->top[i] == 32) && (out_fmt->bits[i] >= in_fmt->bits[i]))
        {
          /* We can shift this up into place */
          IFDEBUG(do_sprintf(a,                                  "MOV     r_temp2,r_pixel,LSL #%i         ; reposition %s",32-in_fmt->top[i],COMPONENT_NAME(i));)
          MOV(R(r_temp2), OP2R(R(r_pixel)) | LSLI(32-in_fmt->top[i]),a);
          done_channels |= 1<<i;
          break;
        }
#endif
        else if(in_fmt->top[i] == out_fmt->top[i])
        {
          /* A simple mask will do */
          int bits = MIN(in_fmt->bits[i],out_fmt->bits[i]);
          int mask = ((1<<bits)-1)<<(in_fmt->top[i]-bits);
          IFDEBUG(do_sprintf(a,                                  "AND     r_temp2,r_pixel,#&%x           ; extract %s",mask,COMPONENT_NAME(i));)
          AND(R(r_temp2), R(r_pixel), IMM12(mask), a);
          done_channels |= 1<<i;
          break;
        }
        else if(!i)
        {
          /* No other choices left, just go with this one */
          int bits = MIN(in_fmt->bits[i],out_fmt->bits[i]);
          int mask = (1<<bits)-1;
          int shift = in_fmt->top[i]-bits;
          IFDEBUG(do_sprintf(a,                                  "AND     r_temp1,r_pixel,#&%x<<%i       ; extract %s",mask,shift,COMPONENT_NAME(i));)
          AND(R(r_temp1), R(r_pixel), IMM12(mask<<shift), a);
          shift = out_fmt->top[i]-in_fmt->top[i];
          IFDEBUG(do_sprintf(a,                                  "MOV     r_temp2,r_temp1,LS%c #%d        ; reposition %s",(shift>=0?'L':'R'),(shift>=0?shift:-shift),COMPONENT_NAME(i));)
          MOV(R(r_temp2), OP2R(R(r_temp1)) | (shift>=0?LSLI(shift):LSRI(-shift)), a);
          done_channels |= 1;
        }
      }
      /* Process remaining channels */
      for(int i=3;i>=0;i--)
      {
        if(done_channels & (1<<i))
          continue;
        done_channels |= 1<<i;
        int bits = MIN(in_fmt->bits[i],out_fmt->bits[i]);
        int mask = (1<<bits)-1;
        int shift = in_fmt->top[i]-bits;
        IFDEBUG(do_sprintf(a,                                  "AND     r_temp1,r_pixel,#&%x<<%i       ; extract %s",mask,shift,COMPONENT_NAME(i));)
        AND(R(r_temp1), R(r_pixel), IMM12(mask<<shift), a);

2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308
        /* If this is the last channel to process, write to r_pixel */        
        int done = (done_channels == 15);

        shift = out_fmt->top[i]-in_fmt->top[i];
        IFDEBUG(do_sprintf(a,                                  "ORR     %s,r_temp2,r_temp1,LS%c #%d        ; reposition %s",(done?"r_pixel":"r_temp2"),(shift>=0?'L':'R'),(shift>=0?shift:-shift),COMPONENT_NAME(i));)
        ORR((done?R(r_pixel):R(r_temp2)), R(r_temp2), OP2R(R(r_temp1)) | (shift>=0?LSLI(shift):LSRI(-shift)), a);
      }  
      /* Apply expansion mask */
      int pixel_expansion_mask,pixel_expansion_shift,regno=-1;
      IFDEBUG(const char *regname;)
      pixel_expansion_mask = get_expansion_mask(wp,ws,in_fmt,out_fmt,&pixel_expansion_shift);
      if(pixel_expansion_mask)
      {
        if(ws->pixel_expansion_mask[0] == pixel_expansion_mask)
        {
          regno = R(r_expansionmask1);
          IFDEBUG(regname = "r_expansionmask1";)
        }
        else
        {
          assert(ws->pixel_expansion_mask[1] == pixel_expansion_mask, ERROR_FATAL);
          regno = R(r_expansionmask2);
          IFDEBUG(regname = "r_expansionmask2";)
        }
        IFDEBUG(do_sprintf(a,                                  "AND     r_temp1,r_pixel,%s ; get expansion bits %x",regname,pixel_expansion_mask);)
        AND(R(r_temp1), R(r_pixel), OP2R(regno), a);
        IFDEBUG(do_sprintf(a,                                  "ORR     r_pixel,r_pixel,r_temp1,LSR #%d ; apply expansion",pixel_expansion_shift);)
        ORR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(pixel_expansion_shift), a);
      }
      /* Manually expand green if needed */
      if((pixelformat == PixelFormat_16bpp) && (out_format > PixelFormat_16bpp))
      {
        assert((out_fmt->top[1] == 16) && (out_fmt->bits[1] == 8), ERROR_FATAL);
        AND(R(r_temp1), R(r_pixel), IMM(3) | IMMROR(18),       "AND     r_temp1,r_pixel,#&C000          ; get green expansion bits");
        ORR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(6),"ORR     r_pixel,r_pixel,r_temp1,LSR #6  ; apply expansion");
      }
      /* Set alpha if needed */
      if((out_flags & PixelFormat_Alpha) && !(flags & PixelFormat_Alpha))
      {
        IFDEBUG(do_sprintf(a,                                  "ORR     r_pixel,r_pixel,#&%x      ; Set alpha",PIXELFORMAT_ALPHA_MASK(out_format | out_flags));)
        ORR(R(r_pixel),R(r_pixel),out_fmt->alphaimm12 | (1<<25),a);
      }
      else if((out_fmt->bits[3] > in_fmt->bits[3]) && (in_fmt->bits[3] != pixel_expansion_shift))
      {
        /* Manual alpha expansion */
        if(in_fmt->bits[3] == 1)
        {
          /* Alpha flag will have been stored in the PSR earlier. Add it back in to the pixel. */
          IFDEBUG(do_sprintf(a,                                "ORRNE   r_pixel,r_pixel,#&%x            ; Apply alpha",PIXELFORMAT_ALPHA_MASK(out_format | out_flags));)
          ORR(R(r_pixel),R(r_pixel),NE | out_fmt->alphaimm12 | (1<<25),a);
        }
        else
        {
          int bits = out_fmt->bits[3]-in_fmt->bits[3];
          int mask = (1<<bits)-1;
          int shift = out_fmt->top[3]-bits;
          IFDEBUG(do_sprintf(a,                                  "AND     r_temp1,r_pixel,#&%x<<%i          ; extract alpha expansion bits",mask,shift);)
          AND(R(r_temp1), R(r_pixel), IMM12(mask<<shift), a);
          shift = in_fmt->bits[3];
          IFDEBUG(do_sprintf(a,                                  "ORR     r_pixel,r_pixel,r_temp1,LSR #%d   ; apply expansion",shift);)
          ORR(R(r_pixel), R(r_pixel), OP2R(R(r_temp1)) | LSRI(shift), a);
        }
      }
      /* And that should be it */
      pixelformat = out_format;
      flags = out_flags;
    }
    /* Recombine pixelformat */
    pixelformat = (PixelFormat) (pixelformat | flags);
  }

  assert(pixelformat == out_pixelformat, ERROR_FATAL); /* If this hasn't happened, we haven't completed the transformation. */
}

static void blend_extract_alpha(asm_workspace *wp, workspace *ws, PixelFormat in_pixelformat,int *alpha_top,int *alpha_bits)
/* Extract the sprite alpha channel into r_alpha, ready for blending later on */
{
  IFDEBUG(char a[128];)
  const PixelFormatInfo *in_fmt = pixelformat_info(in_pixelformat);
  /* Basic rules for extracting alpha are:
     * For 32bpp (i.e. alpha in top byte), shift it down
     * For 16bpp (i.e. alpha in top bit/nibble), just mask it
     All blending code assumes we've followed these rules!
  */
  if(in_fmt->bits[3] == 8)
  {
    assert(in_fmt->top[3] == 32, ERROR_FATAL);
    MOV(R(r_alpha), OP2R(R(r_pixel)) | LSRI(24) | S,"MOVS    r_alpha, r_pixel, LSR #24        ; Get alpha channel");
    *alpha_top = *alpha_bits = 8;
  }
  else
  {
    assert(in_fmt->top[3] == 16, ERROR_FATAL);
    IFDEBUG(do_sprintf(a,"ANDS    r_alpha, r_pixel, #&%x     ; Get alpha channel",PIXELFORMAT_ALPHA_MASK(in_pixelformat));)
    AND(R(r_alpha), R(r_pixel), in_fmt->alphaimm12 | (1<<25) | S, a);
    *alpha_top = 16;
    *alpha_bits = in_fmt->bits[3];
  }
}

static PixelFormat blend_rgb(asm_workspace *wp, workspace *ws, PixelFormat in_pixelformat, int alpha_top, int alpha_bits, BOOL have_dithered)
/* Blend true colour r_pixel with a true colour r_outword pixel.
 *
 * Requirements:
 * ws->in_pixelformat valid
 * ws->in_bpp valid
 * ws->out_pixelformat valid
 * r_pixel allocated
 * r_outword allocated
 * r_alpha allocated if necessary
 * r_translucency allocated if necessary
 */
{
  IFDEBUG(char a[128];)
  const PixelFormatInfo *in_fmt = pixelformat_info(in_pixelformat);

  /* Work out which pixel format the blend calculation is performed in */
  PixelFormat blend_pixelformat = ws->out_pixelformat;
  if(ws->odither && !have_dithered)
  {
    /* Dithering requested, blend in 32bpp */
    comment(ws, "Blending to 32bpp for dithering");
    blend_pixelformat = (PixelFormat) (PixelFormat_32bpp | (ws->out_pixelformat & (PixelFormat_RGB | PixelFormat_Alpha)));
  }
  else if(blend_pixelformat == PixelFormat_8bpp)
  {
    /* 8bpp output, blend in 15bpp ready for table lookup */
    comment(ws, "Blending to 15bpp for 8bpp output");
    blend_pixelformat = PixelFormat_15bpp;
  }
  else
  {
    comment(ws, "Blending straight to output format");
  }

  if((alpha_bits == 1) && !(wp->blending & 1))
  {
    /* Special case - 1bpp alpha, no translucency */
    /* We've already tested against zero alpha, so we know this pixel has
       full alpha. Skip the pointless blend calculation and just translate
       straight to dest.
       TODO - Skip converting to dither format if possible (i.e. dest is 16bpp)
       For that to work, we'd have to know to disable dithering when this case
       is to be taken. */
    comment(ws, "Skipping blend calculation as we only have 1bpp alpha");
    convert_pixel(wp,ws,in_pixelformat,blend_pixelformat);
    return blend_pixelformat;
  }

  /* Standard code */
  /* Calculate alpha value
     Pull apart r_pixel and r_outword one component at a time
     Blend them
     TODO - Use parallel multiply where possible */
  if(wp->blending & 2)
  {
    if(wp->save_mode & 0x80000000)
    {
      if(ISTRANS)
      {
        /* Transformed sprite alpha mask handling is somewhat sub-optimal */
        LDR_SP(r_alpha,trns_comp_mask_offset)
        LDR_SP(r_temp1,trns_comp_mask_base)
        ins(ws, LDRB(R(r_alpha),R(r_temp1)) | INDEX(R(r_alpha),0), "LDRB    r_alpha,[r_temp1,r_alpha] ; Fetch alpha mask");
        TST(R(r_alpha), IMM(128),                                 "TST     r_alpha, #128");
      }
      else
      {
        TST(R(r_maskinword), IMM(128),                            "TST     r_maskinword, #128");
        AND(R(r_alpha), R(r_maskinword), IMM(255),                "AND     r_alpha, r_maskinword, #255      ; Get alpha mask");
      }
      ADD(R(r_alpha), R(r_alpha), IMM(1) | NE,                    "ADDNE   r_alpha, r_alpha, #1             ; Expand alpha to 0-256");
      alpha_bits = 8;
      if(wp->blending & 1)
      {
        MUL(R(r_alpha), R(r_translucency), R(r_alpha), 0,         "MUL     r_alpha, r_translucency, r_alpha ; Combine with translucency");
        alpha_top = 16;
      }
      else
        alpha_top = 8;
    }
    else if(alpha_bits == 1)
    {
      /* Test against zero will have already skipped, so must be full alpha pixel. Just use translucency value as alpha. */
      assert(wp->blending & 1,ERROR_FATAL);
      MOV(R(r_alpha), OP2R(R(r_translucency)),                    "MOV     r_alpha, r_translucency");
      alpha_top = alpha_bits = 8;
    }
    else
    {
      IFDEBUG(do_sprintf(a,"TST     r_alpha, #1<<%i",alpha_top-1);)
      TST(R(r_alpha), IMM12(1<<(alpha_top-1)),a);
      
      assert((alpha_bits == 8) || (alpha_bits == 4), ERROR_FATAL);
      if(!(wp->blending & 1) && (alpha_bits == 4))
      {
        assert(alpha_top >= 4, ERROR_FATAL);
        ORR(R(r_alpha), R(r_alpha), OP2R(R(r_alpha)) | LSRI(4), "ORR     r_alpha, r_alpha, r_alpha, LSR #4");
        alpha_bits = 8;
      }
      
      IFDEBUG(do_sprintf(a,"ADDNE   r_alpha, r_alpha, #1<<%i     ; Expand alpha to 0-256",alpha_top-alpha_bits);)
      ADD(R(r_alpha), R(r_alpha), IMM12(1<<(alpha_top-alpha_bits)) | NE,a);
      if(wp->blending & 1)
      {
        MUL(R(r_alpha), R(r_translucency), R(r_alpha), 0,           "MUL     r_alpha, r_translucency, r_alpha ; Combine with translucency");
        alpha_top += 8;
      }
    }
  }
  else
  {
    assert(wp->blending & 1, ERROR_FATAL);
    assert(ws->regnames.r_alpha.regno == ws->regnames.r_translucency.regno, ERROR_FATAL);
    alpha_top = alpha_bits = 8;
  }
  /* Shift input down if it's going to cause problems
     TODO - Could skip this if we were smart enough to shift the topmost src entry down instead of masking, but that would complicate the code which tracks the calculations */
  if(in_pixelformat == PixelFormat_32bpp_Hi)
  {
    MOV(R(r_pixel), OP2R(R(r_pixel)) | LSRI(8),                     "MOV     r_pixel, r_pixel, LSR #8         ; Shift input down to avoid overflow");
    in_pixelformat = PixelFormat_32bpp;
    in_fmt = pixelformat_info(in_pixelformat);
  }
  /* Work out which pixel format the screen is in */
  PixelFormat screen_pixelformat = ws->out_pixelformat;
  if(screen_pixelformat == PixelFormat_8bpp)
  {
    screen_pixelformat = PixelFormat_15bpp;
  }
  const PixelFormatInfo *blend_fmt = pixelformat_info(blend_pixelformat);
  const PixelFormatInfo *screen_fmt = pixelformat_info(screen_pixelformat);
  /* Perform the blend */
  /* TODO use parallel multiply where possible */
  /* Assuming that green is always between the red and blue channels! */
  int topmost = MAX(in_fmt->top[0],in_fmt->top[2]);
  int bits,mask;

  /* Process red or blue first - whatever is the lowest component in dest */
  /* TODO - if alpha shifted correctly, might be fastest to start with highest in dest? could AND to get src contribution of highest, then shifted ORR to add in lowest */ 
  int toggle = (blend_pixelformat & PixelFormat_RGB)?2:0;
  topmost = MAX(topmost,blend_fmt->top[toggle]);
  bits = in_fmt->bits[toggle];
  mask = ((1<<bits)-1)<<(in_fmt->top[toggle]-bits);
  IFDEBUG(do_sprintf(a,  "AND     r_temp1,r_pixel,#&%x               ; Extract src %s",mask,COMPONENT_NAME(toggle));)
  AND(R(r_temp1),R(r_pixel),IMM12(mask),a);

  /* Shift alpha down to avoid overflow */
  if(topmost+alpha_top > 32)
  {
    assert((alpha_top > 8) && (topmost <= 24), ERROR_FATAL);
    IFDEBUG(do_sprintf(a,"MOV     r_alpha,r_alpha,LSR #%i ; Shift alpha to avoid overflow",alpha_top-8);)
    MOV(R(r_alpha),OP2R(R(r_alpha)) | LSRI(alpha_top-8),a);
    alpha_top = 8;
  }

  /* Extract the opposite component (highest in dest), compute contribution of 1st */
  mask = ((1<<bits)-1)<<(in_fmt->top[2-toggle]-bits);
  IFDEBUG(do_sprintf(a,  "AND     r_temp2,r_pixel,#&%x               ; Extract src %s",mask,COMPONENT_NAME(2-toggle));)
  AND(R(r_temp2),R(r_pixel),IMM12(mask),a);
  IFDEBUG(do_sprintf(a,  "MUL     r_temp1,r_alpha,r_temp1            ; src %s",COMPONENT_NAME(toggle));)
  MUL(R(r_temp1),R(r_alpha),R(r_temp1),0,a);

  /* Extract green */
  bits = in_fmt->bits[1];
  mask = ((1<<bits)-1)<<(in_fmt->top[1]-bits);
  IFDEBUG(do_sprintf(a,  "AND     r_pixel,r_pixel,#&%x               ; Extract src green",mask);)
  AND(R(r_pixel),R(r_pixel),IMM12(mask),a);

  /* Expand r_temp1 if necessary */
  if(in_fmt->bits[toggle] < blend_fmt->bits[toggle])
  {
    IFDEBUG(do_sprintf(a,"ADD     r_temp1,r_temp1,r_temp1,LSR #%i    ; Expand src %s",in_fmt->bits[toggle],COMPONENT_NAME(toggle));)
    ADD(R(r_temp1),R(r_temp1),OP2R(R(r_temp1)) | LSRI(in_fmt->bits[toggle]),a);
  }

  /* Compute more */
  IFDEBUG(do_sprintf(a,  "MUL     r_temp2,r_alpha,r_temp2            ; src %s",COMPONENT_NAME(2-toggle));)
  MUL(R(r_temp2),R(r_alpha),R(r_temp2),0,a);

  /* 1st component can just be shifted down, as we know it's the lowest in dest */
  bits = in_fmt->top[toggle] + alpha_top - blend_fmt->top[toggle];
  IFDEBUG(do_sprintf(a,  "MOV     r_temp1,r_temp1,LSR #%i            ; Shift src %s down",bits,COMPONENT_NAME(toggle))); /* TODO keep fractional components to increase accuracy */
  MOV(R(r_temp1),OP2R(R(r_temp1)) | LSRI(bits),a);

  MUL(R(r_pixel),R(r_alpha),R(r_pixel),0,"MUL     r_pixel,r_alpha,r_pixel ; src green");

  /* Expand other two components if necessary */
  if(in_fmt->bits[2-toggle] < blend_fmt->bits[2-toggle])
  {
    IFDEBUG(do_sprintf(a,"ADD     r_temp2,r_temp2,r_temp2,LSR #%i    ; Expand src %s",in_fmt->bits[2-toggle],COMPONENT_NAME(2-toggle));)
    ADD(R(r_temp2),R(r_temp2),OP2R(R(r_temp2)) | LSRI(in_fmt->bits[2-toggle]),a);
  }
  if(in_fmt->bits[1] < blend_fmt->bits[1])
  {
    IFDEBUG(do_sprintf(a,"ADD     r_pixel,r_pixel,r_pixel,LSR #%i    ; Expand src green",in_fmt->bits[1]);)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_pixel)) | LSRI(in_fmt->bits[1]),a);
  }

  /* Add in the other two components via mask & shift */
  bits = MIN(in_fmt->bits[2-toggle],blend_fmt->bits[2-toggle]);
  mask = ((1<<bits)-1)<<(in_fmt->top[2-toggle]+alpha_top-bits);
  IFDEBUG(do_sprintf(a,  "AND     r_temp2,r_temp2,#&%x               ; Mask src %s",mask,COMPONENT_NAME(2-toggle));)
  AND(R(r_temp2),R(r_temp2),IMM12(mask),a);

  bits = MIN(in_fmt->bits[1],blend_fmt->bits[1]);
  mask = ((1<<bits)-1)<<(in_fmt->top[1]+alpha_top-bits);
  IFDEBUG(do_sprintf(a,  "AND     r_pixel,r_pixel,#&%x               ; Mask src green",mask);)
  AND(R(r_pixel),R(r_pixel),IMM12(mask),a);

  /* Now the shift */
  bits = in_fmt->top[2-toggle] + alpha_top - blend_fmt->top[2-toggle];
  if(bits > 0)
  {
    IFDEBUG(do_sprintf(a,"ORR     r_temp2,r_temp1,r_temp2,LSR #%i    ; Add in src %s",bits,COMPONENT_NAME(2-toggle));)
    ORR(R(r_temp2),R(r_temp1),OP2R(R(r_temp2)) | LSRI(bits),a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"ORR     r_temp2,r_temp1,r_temp2,LSL #%i    ; Add in src %s",-bits,COMPONENT_NAME(2-toggle));)
    ORR(R(r_temp2),R(r_temp1),OP2R(R(r_temp2)) | LSLI(-bits),a);
  }

  /* Invert r_alpha ready for screen pixel calculation */
  IFDEBUG(do_sprintf(a,  "RSBS    r_alpha,r_alpha,#1<<%i             ; Invert alpha ready for dest calculations",alpha_top);)
  RSB(R(r_alpha),R(r_alpha),IMM12(1<<alpha_top) | S,a);

  /* We have a spare register, start extracting screen pixel components */
  if(ws->out_pixelformat == PixelFormat_8bpp)
  {
    /* Begin 8bpp -> 15bpp lookup */
    AND(R(r_temp1),R(r_outword),IMM(255) | NE,"ANDNE   r_temp1,r_outword,#255");
    ins(ws, LDR(R(r_temp1), R(r_screenpalette)) | INDEX(R(r_temp1), 2) | NE, "LDRNE   r_temp1,[r_screenpalette,r_temp1,LSL #2] ; Convert screen pixel to 15bpp");
  }
  else
  {
    /* Similar to the sprite pixel, we'll start with the lowest dest component */
    if(DEST_32_BIT)
    {
      /* r_outword not normally used. Must manually load the pixel */
      ins(ws, LDR(R(r_outword), R(r_outptr)) | OFFSET(0) | NE,"LDRNE   r_outword,[r_outptr]");
    }
    bits = screen_fmt->bits[toggle];
    mask = ((1<<bits)-1)<<(screen_fmt->top[toggle]-bits);
    IFDEBUG(do_sprintf(a,  "ANDNE   r_temp1,r_outword,#&%x             ; Extract dest %s",mask,COMPONENT_NAME(toggle));)
    AND(R(r_temp1),R(r_outword),IMM12(mask) | NE,a);
  }

  /* Add in sprite green */
  bits = in_fmt->top[1] + alpha_top - blend_fmt->top[1];
  if(bits > 0)
  {
    IFDEBUG(do_sprintf(a,"ORR     r_pixel,r_temp2,r_pixel,LSR #%i    ; Add in src green",bits);)
    ORR(R(r_pixel),R(r_temp2),OP2R(R(r_pixel)) | LSRI(bits),a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"ORR     r_pixel,r_temp2,r_pixel,LSL #%i    ; Add in src green",-bits);)
    ORR(R(r_pixel),R(r_temp2),OP2R(R(r_pixel)) | LSLI(-bits),a);
  }

  /* Skip screen calculations if alpha == 0 */
  L(blend_nodestalpha)->def = 0;
  branch(ws, B | EQ, L(blend_nodestalpha), "BEQ     blend_nodestalpha");

  if(ws->out_pixelformat == PixelFormat_8bpp)
  {
    /* 8bpp is a special case as we need to read from r_temp1, not r_outword
       So start the blend calculation here and then fall through into the general code once we've read all the components */
    AND(R(r_temp2),R(r_temp1),IMM(31),             "AND     r_temp2,r_temp1,#31                ; dest red");
    MUL(R(r_temp2),R(r_alpha),R(r_temp2),0,        "MUL     r_temp2,r_alpha,r_temp2");
    if(screen_fmt->bits[0] < blend_fmt->bits[0])
    {
      IFDEBUG(do_sprintf(a,                        "ADD     r_temp2,r_temp2,r_temp2,LSR #%i    ; Expand dest red",screen_fmt->bits[0]);)
      ADD(R(r_temp2),R(r_temp2),OP2R(R(r_temp2)) | LSRI(screen_fmt->bits[0]),a);
    }
    bits = screen_fmt->top[0] + alpha_top - blend_fmt->top[0];
    assert(bits > 0, ERROR_FATAL);
    IFDEBUG(do_sprintf(a,                          "ADD     r_pixel,r_pixel,r_temp2,LSR #%i    ; Add in dest red",bits);)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp2)) | LSRI(bits),a);

    AND(R(r_temp2),R(r_temp1),IMM(31)|IMMROR(22),  "AND     r_temp2,r_temp1,#31<<10            ; dest blue");
    AND(R(r_temp1),R(r_temp1),IMM(31*2)|IMMROR(28),"AND     r_temp1,r_temp1,#31<<5             ; dest green");
    MUL(R(r_temp2),R(r_alpha),R(r_temp2),0,        "MUL     r_temp2,r_alpha,r_temp2");
    toggle = 0;
  }
  else
  {
    /* Compute contribution of 1st screen component */
    IFDEBUG(do_sprintf(a,  "MUL     r_temp1,r_alpha,r_temp1            ; dest %s",COMPONENT_NAME(toggle));)
    MUL(R(r_temp1),R(r_alpha),R(r_temp1),0,a);
  
    /* Extract opposite screen component */
    bits = screen_fmt->bits[2-toggle];
    mask = ((1<<bits)-1)<<(screen_fmt->top[2-toggle]-bits);
    IFDEBUG(do_sprintf(a,  "AND     r_temp2,r_outword,#&%x             ; Extract dest %s",mask,COMPONENT_NAME(2-toggle));)
    AND(R(r_temp2),R(r_outword),IMM12(mask),a);

    /* Expand r_temp1 if necessary */
    if(screen_fmt->bits[toggle] < blend_fmt->bits[toggle])
    {
      IFDEBUG(do_sprintf(a,"ADD     r_temp1,r_temp1,r_temp1,LSR #%i    ; Expand dest %s",screen_fmt->bits[toggle],COMPONENT_NAME(toggle));)
      ADD(R(r_temp1),R(r_temp1),OP2R(R(r_temp1)) | LSRI(screen_fmt->bits[toggle]),a);
    }
  
    bits = screen_fmt->top[toggle] + alpha_top - blend_fmt->top[toggle];
    if(bits > 0)
    {
      IFDEBUG(do_sprintf(a,"ADD     r_pixel,r_pixel,r_temp1,LSR #%i    ; Add in dest %s",bits,COMPONENT_NAME(toggle));)
      ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp1)) | LSRI(bits),a);
    }
    else
    {
      IFDEBUG(do_sprintf(a,"ADD     r_pixel,r_pixel,r_temp1,LSL #%i    ; Add in dest %s",-bits,COMPONENT_NAME(toggle));)
      ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp1)) | LSLI(-bits),a);
    }
  
    IFDEBUG(do_sprintf(a,  "MUL     r_temp2,r_alpha,r_temp2            ; dest %s",COMPONENT_NAME(2-toggle));)
    MUL(R(r_temp2),R(r_alpha),R(r_temp2),0,a);
  
    bits = screen_fmt->bits[1];
    mask = ((1<<bits)-1)<<(screen_fmt->top[1]-bits);
    IFDEBUG(do_sprintf(a,  "AND     r_temp1,r_outword,#&%x             ; Extract dest green",mask);)
    AND(R(r_temp1),R(r_outword),IMM12(mask),a);
  }
  
  if(screen_fmt->bits[2-toggle] < blend_fmt->bits[2-toggle])
  {
    IFDEBUG(do_sprintf(a,"ADD     r_temp2,r_temp2,r_temp2,LSR #%i    ; Expand dest %s",screen_fmt->bits[2-toggle],COMPONENT_NAME(2-toggle));)
    ADD(R(r_temp2),R(r_temp2),OP2R(R(r_temp2)) | LSRI(screen_fmt->bits[2-toggle]),a);
  }

  /* Mask & shift */
  bits = blend_fmt->bits[2-toggle];
  mask = ((1<<bits)-1)<<(screen_fmt->top[2-toggle]+alpha_top-bits);
  /* Fudge - when expanding to 32bpp in order to perform dithering we
     sometimes need to generate impossible 12bit immediate constants
     (i.e. 0xff at an odd bit offset)
     Deal with this by dropping the bottom bit of the mask, the effect
     should be negligible */
  if((bits == 8) && ((screen_fmt->top[2-toggle]+alpha_top-bits) & 1))
  {
    mask = mask & (mask<<1);
  }
  IFDEBUG(do_sprintf(a,  "AND     r_temp2,r_temp2,#&%x               ; Mask dest %s",mask,COMPONENT_NAME(2-toggle));)
  AND(R(r_temp2),R(r_temp2),IMM12(mask),a);
3309

3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
  MUL(R(r_temp1),R(r_alpha),R(r_temp1),0,"MUL     r_temp1,r_alpha,r_temp1 ; dest green");

  bits = screen_fmt->top[2-toggle] + alpha_top - blend_fmt->top[2-toggle];
  if(bits > 0)
  {
    IFDEBUG(do_sprintf(a,"ADD     r_pixel,r_pixel,r_temp2,LSR #%i    ; Add in dest %s",bits,COMPONENT_NAME(2-toggle));)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp2)) | LSRI(bits),a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"ADD     r_pixel,r_pixel,r_temp2,LSL #%i    ; Add in dest %s",-bits,COMPONENT_NAME(2-toggle));)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp2)) | LSLI(-bits),a);
  }
  
  if(screen_fmt->bits[1] < blend_fmt->bits[1])
  {
    IFDEBUG(do_sprintf(a,"ADD     r_temp1,r_temp1,r_temp1,LSR #%i    ; Expand dest green",screen_fmt->bits[1]);)
    ADD(R(r_temp1),R(r_temp1),OP2R(R(r_temp1)) | LSRI(screen_fmt->bits[1]),a);
  }

  bits = blend_fmt->bits[1];
  mask = ((1<<bits)-1)<<(screen_fmt->top[1]+alpha_top-bits);
  /* Deal with impossible constants */
  if((bits == 8) && ((screen_fmt->top[1]+alpha_top-bits) & 1))
  {
    mask = mask & (mask<<1);
  }
  IFDEBUG(do_sprintf(a,  "AND     r_temp1,r_temp1,#&%x               ; Mask dest green",mask);)
  AND(R(r_temp1),R(r_temp1),IMM12(mask),a);

  /* Restore r_alpha if necessary */
  if(ws->regnames.r_alpha.regno == ws->regnames.r_translucency.regno)
  {
    DEFINE_LABEL(blend_nodestalpha,"Skip dest blend calc");
    IFDEBUG(do_sprintf(a,"RSB     r_alpha,r_alpha,#1<<%i             ; Restore r_alpha / r_translucency",alpha_top);)
    RSB(R(r_alpha),R(r_alpha),IMM12(1<<alpha_top),a);    
  }

  bits = screen_fmt->top[1] + alpha_top - blend_fmt->top[1];
  if(bits > 0)
  {
    IFDEBUG(do_sprintf(a,"ADDNE   r_pixel,r_pixel,r_temp1,LSR #%i    ; Add in dest green",bits);)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp1)) | LSRI(bits) | NE,a);
  }
  else
  {
    IFDEBUG(do_sprintf(a,"ADDNE   r_pixel,r_pixel,r_temp1,LSL #%i    ; Add in dest green",-bits);)
    ADD(R(r_pixel),R(r_pixel),OP2R(R(r_temp1)) | LSLI(-bits) | NE,a);
  }

  if(!L(blend_nodestalpha)->def)
    DEFINE_LABEL(blend_nodestalpha,"Skip dest blend calc");

  if(blend_pixelformat & PixelFormat_Alpha)
  {
    IFDEBUG(do_sprintf(a,"ORR     r_pixel, r_pixel, #&%x",PIXELFORMAT_ALPHA_MASK(blend_pixelformat));)
    ORR(R(r_pixel),R(r_pixel),blend_fmt->alphaimm12 | (1<<25),a);
  }

  /* And we're done! */

  return blend_pixelformat;
}

static PixelFormat apply_dither(asm_workspace *wp, workspace *ws, PixelFormat pixelformat, BOOL *have_dithered)
{
  if(ws->odither && !*have_dithered)
  {
    if(((pixelformat & PixelFormat_BPPMask) == PixelFormat_12bpp)
      && (ws->out_pixelformat >= PixelFormat_4bpp))
    {
      /* When dithering 12bpp down to 4bpp/8bpp, we need to
         convert to 32bpp to make it look good, and to fix
         bugs with the dither value writing into the wrong
         colour channels */
      PixelFormat ditherformat = (PixelFormat) (PixelFormat_32bpp | (pixelformat & (PixelFormat_RGB | PixelFormat_Alpha)));
      convert_pixel(wp,ws,pixelformat,ditherformat);
      pixelformat = ditherformat;
    }
    add_ordered_dither(wp, ws, pixelformat); /* do ordered dither */
    *have_dithered = TRUE;
  }
  return pixelformat;
}

static PixelFormat pick_colourmap_format(asm_workspace *wp, workspace *ws, PixelFormat in_pixelformat, PixelFormat out_pixelformat)
{
  /* Based around input and output pixel format, pick a sensible ColourTTRFormat
     value which will (ideally) allow us to perform a 1-instruction translation
     to 32bpp_Hi ready for passing to the colour mapping code */
  if((in_pixelformat & PixelFormat_Alpha) && (out_pixelformat & PixelFormat_Alpha))
  {
    /* Alpha needs preserving. Only sensible choice is 32bpp + alpha. */
    return (PixelFormat) (PixelFormat_32bpp + PixelFormat_Alpha);
  }
  else if((wp->CPUFlags & CPUFlag_REV) && ((in_pixelformat & ~PixelFormat_Alpha) == (PixelFormat_32bpp + PixelFormat_RGB)))
  {
    /* If REV is available we can do a 1-instruction translation from &TTRRGGBB to 32bpp_Hi */
    return (PixelFormat) (PixelFormat_32bpp + PixelFormat_RGB);
  }
  else
  {
    /* Just go for 32bpp, with or without alpha, as we'll shift up to discard top byte */
    return (PixelFormat) (PixelFormat_32bpp + (in_pixelformat & PixelFormat_Alpha));
  }
}

static PixelFormat apply_ttr(asm_workspace *wp, workspace *ws, PixelFormat pixelformat, BOOL *have_dithered)
/* Apply the TTR, and perform any dithering if necessary */
{
  BOOL preserve_alpha;
  switch(wp->TTRType & ~TTRType_Optional)
  {
  case TTRType_None:
    break;
  case TTRType_Normal:
    assert(pixelformat == ws->ColourTTRFormat, ERROR_FATAL);
    ins(ws, LDRB(R(r_pixel), R(r_table)) | INDEX(R(r_pixel), 0),  "LDRB    r_pixel,[r_table, r_pixel]      ; byte table lookup");
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_Wide:
    assert(pixelformat == ws->ColourTTRFormat, ERROR_FATAL);
    ins(ws, LDR(R(r_pixel), R(r_table)) | INDEX(R(r_pixel), 2),   "LDR     r_pixel,[r_table, r_pixel, LSL #2] ; word table lookup");
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_32K:
    /* Hack - skip if this is JPEG and we're already correct */
    if(wp->is_it_jpeg && (pixelformat <= PixelFormat_8bpp))
    {
      assert(pixelformat == ws->out_pixelformat,ERROR_FATAL);
      break;
    }
    /* If we're applying a 32K table, now is our last chance to perform dithering */
    pixelformat = apply_dither(wp,ws,pixelformat,have_dithered);
    if(pixelformat != ws->ColourTTRFormat)
      convert_pixel(wp,ws,pixelformat,ws->ColourTTRFormat);
    ins(ws, LDRB(R(r_pixel), R(r_table)) | INDEX(R(r_pixel), 0),  "LDRB    r_pixel,[r_table, r_pixel]      ; 32K-style table lookup");
    pixelformat = ws->out_pixelformat;
    break;
  case TTRType_ColourMap:
    if(pixelformat != ws->ColourTTRFormat)
      convert_pixel(wp,ws,pixelformat,ws->ColourTTRFormat);
    assert(R(r_pixel) == 14, ERROR_FATAL);
    /* Call the colour map code.. slightly nasty!
       This would be nicer if r0 == r_pixel, but I'm not sure how much code
       is left which assumes r_pixel == r14 (or that r14 isn't r_temp1/r_temp2)
       */
    comment(ws, "Performing colour mapping");
    ins(ws, PUSH | (1<<12) | (1<<0),                    "STMDB   sp!,{r0,r12}");
    preserve_alpha = ((ws->ColourTTRFormat & PixelFormat_Alpha) && (ws->out_pixelformat & PixelFormat_Alpha));
    if(preserve_alpha)
    {
      /* Preserve alpha. Should be in top byte. */
      assert(ws->ColourTTRFormat == PixelFormat_32bpp + PixelFormat_Alpha, ERROR_FATAL);
      if(ISTRANS)
3465
      {
3466 3467 3468
        /* r_temp2 used to avoid r12 clash with hardcoded r_temp1 */
        assert((R(r_temp2) != 0) && (R(r_temp1) == 12), ERROR_FATAL); /* Check that our hardcoding assumptions are correct */
        AND(R(r_temp2),R(r_pixel),IMM(255)|IMMROR(8),   "AND     r_temp2,r_pixel,#&ff000000 ; Preserve alpha");
3469
      }
3470
      else
3471
      {
3472 3473
        assert((R(r_temp1) != 0) && (R(r_temp1) < 12), ERROR_FATAL);
        AND(R(r_temp1),R(r_pixel),IMM(255)|IMMROR(8),   "AND     r_temp1,r_pixel,#&ff000000 ; Preserve alpha");
3474
      }
3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513
    }
    if(R(r_table) == 0)
    {
      /* r_table may sometimes be allocated to R0, which is a bit of a pain.
         Relocate to r12 instead.
         TODO - Avoid this clash when allocating registers. Mainly happens with
         sprtrans, but could conceivably happen with putscaled too.
      */
      MOV(12,OP2R(0),                                   "MOV     r12,r_table ; Protect r_table (r0)");
    }
    /* 1-instruction translation from ColourTTRFormat to 32bpp_Hi */
    switch(ws->ColourTTRFormat)
    {
    case PixelFormat_32bpp:
    case PixelFormat_32bpp + PixelFormat_Alpha:
      MOV(0,OP2R(R(r_pixel))|LSLI(8),                   "MOV     r0,r_pixel,LSL #8 ; Convert to palette entry");
      break;
    case PixelFormat_32bpp + PixelFormat_RGB:
      assert(wp->CPUFlags & CPUFlag_REV, ERROR_FATAL);
      REV(0,R(r_pixel),0,                               "REV     r0,r_pixel ; Convert to palette entry");
      break;
    default:
      assert(ws->ColourTTRFormat == PixelFormat_32bpp_Hi, ERROR_FATAL);
      MOV(0,OP2R(R(r_pixel)),                           "MOV     r0,r_pixel");
      break;
    }
    MOV(R(lr),OP2R(R(pc)),                              "MOV     lr,pc");
    if(R(r_table) == 0)
    {
      ins(ws, LDMIA(12) | (1<<12)|(1<<15),              "LDMIA   r12,{r12,pc} ; Call colour mapping code");
    }
    else
    {
      ins(ws, LDMIA(R(r_table)) | (1<<12)|(1<<15),      "LDMIA   r_table,{r12,pc} ; Call colour mapping code");
    }
    /* 1-instruction translation from 32bpp_Hi to something closer to the output format, or at least something a bit easier to work with */
    if(preserve_alpha)
    {
      if(ISTRANS)
3514
      {
3515
        ORR(R(r_pixel),R(r_temp2),0|LSRI(8),            "ORR     r_pixel,r_temp2,r0,LSR #8");
3516
      }
3517
      else
3518
      {
3519
        ORR(R(r_pixel),R(r_temp1),0|LSRI(8),            "ORR     r_pixel,r_temp1,r0,LSR #8");
3520
      }
3521
      pixelformat = (PixelFormat) (PixelFormat_32bpp+PixelFormat_Alpha);
3522
    }
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
    else if(((ws->out_pixelformat & ~PixelFormat_Alpha) == (PixelFormat_32bpp + PixelFormat_RGB)) && (wp->CPUFlags & CPUFlag_REV))
    {
      REV(R(r_pixel),0,0,                               "REV     r_pixel,r0");
      pixelformat = (PixelFormat) (PixelFormat_32bpp+PixelFormat_RGB);
    }
    else
    {
      MOV(R(r_pixel),0|LSRI(8),                         "MOV     r_pixel,r0,LSR #8");
      pixelformat = PixelFormat_32bpp;
    }
    ins(ws, POP | (1<<12) | (1<<0),                     "LDMIA   sp!,{r0,r12}");
    /* Apply r_inversetable if we have <=8bpp output */
    if(wp->BPP <= 8)
    {
      pixelformat = apply_dither(wp,ws,pixelformat,have_dithered);
      comment(ws,"Convert to 15bpp and apply inversetable");
      convert_pixel(wp,ws,pixelformat,PixelFormat_15bpp);
      ins(ws, LDRB(R(r_pixel), R(r_inversetable)) | INDEX(R(r_pixel), 0), "LDRB    r_pixel,[r_inversetable,r_pixel]");
      pixelformat = ws->out_pixelformat;
    }
    break;
  case TTRType_Palette:
    assert(pixelformat == ws->ColourTTRFormat, ERROR_FATAL);
    ins(ws, LDR(R(r_pixel), R(r_table))
          | INDEX(R(r_pixel), 3),                     "LDR     r_pixel,[r_table, r_pixel, LSL #3] ; standard palette lookup");
    pixelformat = PixelFormat_32bpp_Hi;
    break;
3550 3551
  }

3552
  return pixelformat;
3553 3554 3555 3556 3557 3558 3559
}

static void translate_pixel(asm_workspace *wp, workspace *ws)
/* Translate r_pixel from being a source pixel, to being a destination pixel.
 *
 * Requirements:
 * ws->in_pixelformat valid
3560
 * ws->in_bpp valid
3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571
 * ws->out_pixelformat valid
 * ws->gcol valid
 * ws->odither valid
 * wp->ColourTTR valid
 * ws->ColourTTRFormat valid
 * wp->BPP valid
 * wp->BPC valid
 * wp->Log2bpp valid
 * dither_expansion_init() called
 * r_pixel allocated
 * r_table allocated if necessary
3572 3573 3574 3575
 * r_blendtable allocated if necessary
 * r_outword allocated if necessary
 * r_alpha allocated if necessary
 * r_translucency allocated if necessary
3576 3577
 */
{
3578
  IFDEBUG(char a[128];)
3579 3580 3581 3582
  PixelFormat pixelformat = ws->in_pixelformat;

  if (PLOTMASK || TRANSMASK)
  {
3583
    if ((ws->gcol == 2) && ((pixelformat & PixelFormat_BPPMask) != PixelFormat_32bpp)) /* AND plot action */
3584
    {
3585
      MOV(R(r_pixel), OP2R(R(r_pixel)) | LSLI(31-(wp->BPC)),  "MOV     r_pixel, r_pixel, LSL 31-out_bpc ;a");
3586
      ORR(R(r_pixel), R(r_pixel), IMM(2) | IMMROR(2),         "ORR     r_pixel,r_pixel,#&80000000       ;a");
3587
      MOV(R(r_pixel), OP2R(R(r_pixel)) | ASRI(31-(wp->BPC)),  "MOV     r_pixel, r_pixel, ASR 31-out_bpc ;a");
3588 3589 3590 3591
    }
    return; /* No more transformation necessary */
  }

3592
  switch_bank(wp, ws, REGFLAG_XLOOP,REGFLAG_PERPIXEL);
3593 3594

  comment(ws, "Perform any transformation necessary");
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639

  /* Work out if we want to apply the ttr before or after (or during) the blend:
     * For BlendImpl_BlendTable:
       If we have an optional TTR for a palletised source, we never want to
       apply it. This is currently taken care of by compute_blendimpl
       So, if we have a table, we want to apply it now
     * For BlendImpl_BlendTables:
       If we have an optional TTR for a palletised source, we apply it during
       the blend (for full alpha pixels). Partially alpha'd pixels use LUTs
       generated from the source palette.
       Other types of TTR are applied once we've extracted and tested the source
       alpha.
     * For BlendImpl_InverseTable:
       We want to operate on true colour values, but as we have an 8bpp dest
       any TTR we have will be designed around that. So completely ignore any
       optional 32K table (handled in compute_blendimpl), and apply all others
       before the blend (as we currently don't have support for reading the
       sprite palette here)
     * For BlendImpl_True:
       We want to operate on true colour values, so always apply the TTR first
       as it'll either be a palette/wide TTR or a colour map
    */

  BOOL ttr_before_blend = TRUE;
  switch(ws->blendimpl)
  {
  case BlendImpl_BlendTables:
    ttr_before_blend = FALSE;
    break;
  }

  /* If we're performing blending using the sprite alpha channel, we must
     extract the alpha value before applying any TTR, as the TTR will destroy
     it. We also take this opportunity to test the pixel alpha against zero
     and skip the rest of this code if possible.
     TODO - Use mask skip branch address where possible.
  */
  if(wp->blending)
  {
    L(translate_noalpha)->def = 0;
    L(translate_noalpha2)->def = 0;
  }
  int alpha_top = 0;
  int alpha_bits = 0;
  if((wp->blending & 2) && !(wp->save_mode & 0x80000000))
3640
  {
3641 3642
    blend_extract_alpha(wp,ws,pixelformat,&alpha_top,&alpha_bits);
    if(wp->BPP <= 8)
3643
    {
3644 3645 3646 3647 3648
      AND(R(r_pixel), R(r_outword), IMM(ws->out_pixmask) | EQ, "ANDEQ   r_pixel, r_outword, #out_pixmask ; just use dest pixel if 0 alpha");
    }
    else if(wp->BPP == 32)
    {
      ins(ws, LDR(R(r_pixel), R(r_outptr)) | OFFSET(0) | EQ,   "LDREQ   r_pixel,[r_outptr]             ; just use dest pixel if 0 alpha");
3649 3650 3651
    }
    else
    {
3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
      assert(wp->BPP == 16, ERROR_FATAL);
      if(wp->CPUFlags & CPUFlag_T2)
      {
        UBFX(R(r_pixel),R(r_outword),0,16,EQ,                  "UBFXEQ  r_pixel,r_outword,#0,#16       ; just use dest pixel if 0 alpha");
      }
      else
      {
        MOV(R(r_pixel), OP2R(R(r_outword)) | LSLI(16) | EQ,    "MOVEQ   r_pixel,r_outword,LSL #16      ; just use dest pixel if 0 alpha");
        MOV(R(r_pixel), OP2R(R(r_pixel)) | LSRI(16) | EQ,      "MOVEQ   r_pixel,r_pixel,LSR #16");
      }
3662
    }
3663
    branch(ws, B | EQ, L(translate_noalpha),                   "BEQ     translate_noalpha                ; and skip remaining blend code");
3664 3665
  }

3666 3667 3668 3669 3670 3671 3672 3673 3674
  BOOL have_dithered = FALSE;
  if(ttr_before_blend && wp->TTRType)
  {  
    pixelformat = apply_ttr(wp,ws,pixelformat,&have_dithered);
  }  

  /* Blending */
  int alpha_shift = 0;
  switch(ws->blendimpl)
3675
  {
3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806
  case BlendImpl_BlendTable:
    /* Single blend table */
    /* JPEG might need translating to out_pixelformat before the blend can take place */
    if((wp->is_it_jpeg) && (pixelformat > PixelFormat_8bpp))
    {
      pixelformat = apply_dither(wp,ws,pixelformat,&have_dithered);
      convert_pixel(wp,ws,pixelformat,ws->out_pixelformat);
      pixelformat = ws->out_pixelformat;
    }
    comment(ws, "Use blend table");
    AND(R(r_temp1), R(r_outword), IMM((1<<wp->BPP)-1) | IMMROR(0),    "AND     r_temp1, r_outword, #(1<<out_bpp)-1");
    if((wp->is_it_jpeg) || (wp->TTRType != TTRType_None))
    {
      /* r_pixel should have been translated by ColourTTR to output bpp */
      assert(pixelformat == ws->out_pixelformat, ERROR_FATAL);
      ORR(R(r_temp1), R(r_pixel), OP2R(R(r_temp1)) | LSLI(wp->BPP),   "ORR     r_temp1, r_pixel, r_temp1, LSL #out_bpp");
    }
    else
    {
      assert(pixelformat == ws->in_pixelformat, ERROR_FATAL);
      ORR(R(r_temp1), R(r_pixel), OP2R(R(r_temp1)) | LSLI(ws->in_bpp),"ORR     r_temp1, r_pixel, r_temp1, LSL #in_bpp");
    }
    ins(ws, LDRB(R(r_pixel), R(r_blendtable)) | INDEX(R(r_temp1), 0), "LDRB    r_pixel, [r_blendtable, r_temp1]");
    pixelformat = ws->out_pixelformat;
    break;
  case BlendImpl_InverseTable:
    /* TODO should really use source palette for this, via makepalette16bpp */
    if(pixelformat == PixelFormat_8bpp)
    {
      ins(ws, LDR(R(r_pixel), R(r_screenpalette)) | INDEX(R(r_pixel), 2),"LDR     r_pixel,[r_screenpalette,r_pixel,LSL #2] ; Convert sprite pixel to 15bpp");
      pixelformat = PixelFormat_15bpp;
    }
    /* Fall through... */
  case BlendImpl_True:
    /* True colour blend */
    comment(ws, "True colour blend");
    assert(pixelformat > PixelFormat_8bpp, ERROR_FATAL);
    pixelformat = blend_rgb(wp,ws,pixelformat,alpha_top,alpha_bits,have_dithered);
    break;
  case BlendImpl_BlendTables:
    /* Screen is <= 4bpp, use lots of blendtables
       If src is true colour, use ColourTTR to convert to screen
       Else use palette index directly */
    comment(ws, "Lots of blend tables");
    /* r_translucency (if used) assumed to be 0-256 alpha */
    assert(wp->blending & 2, ERROR_FATAL);
    /* Calculate the alpha value and branch if 0
       Actual blend happens later on */
    if (SOURCE_ALPHAMASK)
    {
      /* Alpha mask */
      if(ISTRANS)
      {
        /* Transformed sprite alpha mask handling is somewhat sub-optimal */
        LDR_SP(r_alpha,trns_comp_mask_offset)
        LDR_SP(r_temp1,trns_comp_mask_base)
        ins(ws, LDRB(R(r_alpha),R(r_temp1)) | INDEX(R(r_alpha),0), "LDRB    r_alpha,[r_temp1,r_alpha]        ; Fetch alpha mask");
      }
      if (wp->blending & 1)
      {
        if(!ISTRANS)
        {
          AND(R(r_alpha), R(r_maskinword), IMM(255),             "AND     r_alpha, r_maskinword, #255      ; Alpha mask");
        }
        MUL(R(r_alpha), R(r_translucency), R(r_alpha), 0,        "MUL     r_alpha, r_translucency, r_alpha ; Combined mask + SpriteOp translucency");
        MOV(R(r_alpha), OP2R(R(r_alpha)) | LSRI(13) | S,         "MOVS    r_alpha, r_alpha, LSR #13");
      }
      else
      {
        if(ISTRANS)
        {
          AND(R(r_alpha), R(r_alpha), IMM(0xE0) | S,             "ANDS    r_alpha, r_alpha, #&E0           ; Alpha mask");
        }
        else
        {
          AND(R(r_alpha), R(r_maskinword), IMM(0xE0) | S,        "ANDS    r_alpha, r_maskinword, #&E0      ; Alpha mask");
        }
        alpha_shift = 5;
      }
      alpha_bits = 3;  
    }
    else if (alpha_bits)
    {
      /* Alpha channel */
      unsigned int chan_mask = ((1<<alpha_bits)-1)<<(alpha_top-alpha_bits);
      if (wp->blending & 1)
      {
        if (alpha_bits == 1)
        {
          MOV(R(r_alpha), OP2R(R(r_translucency))|LSRI(5)|S,     "MOVS  r_alpha, r_translucency, LSR #5");
        }
        else
        {
          assert(alpha_bits >= 4, ERROR_FATAL);
          if(chan_mask == 0xff)
          {
            MUL(R(r_alpha), R(r_translucency), R(r_alpha), 0,    "MUL     r_alpha, r_translucency, r_alpha ; Combined alpha + SpriteOp translucency");
            MOV(R(r_alpha), OP2R(R(r_alpha)) | LSRI(13) | S,     "MOVS    r_alpha, r_alpha, LSR #13");
          }
          else
          {
            assert(chan_mask == 0xf000, ERROR_FATAL);
            MUL(R(r_alpha), R(r_translucency), R(r_alpha), 0,    "MUL     r_alpha, r_translucency, r_alpha ; Combined alpha + SpriteOp translucency");
            MOV(R(r_alpha), OP2R(R(r_alpha)) | LSRI(21) | S,     "MOVS    r_alpha, r_alpha, LSR #21");
          }  
        }
        alpha_bits = 3;  
      }
      else
      {
        chan_mask &= ~(chan_mask>>3);
        IFDEBUG(do_sprintf(a,"ANDS    r_alpha, r_alpha, #&%x ; Alpha channel",chan_mask);)
        AND(R(r_alpha), R(r_alpha), IMM12(chan_mask) | S,a);
        alpha_shift = alpha_top-3;
        alpha_bits = (alpha_bits>3?3:alpha_bits); /* Should be 1 or 3, asserted below */
      }
    }
    else
    {
      /* Ordinary translucent plotting */
      /* This shouldn't happen, should be handled by single blendtable case above */
      assert(0, ERROR_FATAL);
    }
    AND(R(r_pixel), R(r_outword), IMM(ws->out_pixmask) | EQ,   "ANDEQ   r_pixel, r_outword, #out_pixmask ; just use dest pixel if 0 alpha");
    /* TODO can use mask skip branch address? */
    branch(ws, B | EQ, L(translate_noalpha2),                  "BEQ     translate_noalpha                ; and skip remaining blend code");

    /* Apply TTR here, unless it's an optional normal TTR (in which case we
       only apply for full alpha pixels) */
    assert(!ttr_before_blend, ERROR_FATAL);
    if(wp->TTRType != TTRType_Normal+TTRType_Optional)
3807
    {
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847
      pixelformat = apply_ttr(wp,ws,pixelformat,&have_dithered);
      ttr_before_blend = TRUE;
    }

    /* Assume r_alpha 0-8 or 0-1, with 8/1 case already handled with a branch */
    if(alpha_bits == 3)
    {
      assert(alpha_bits == 3,ERROR_FATAL);
      IFDEBUG(do_sprintf(a,"CMP     r_alpha,#%i<<%i",(1<<alpha_bits)-1,alpha_shift);)
      CMP(R(r_alpha), IMM12(((1<<alpha_bits)-1)<<alpha_shift),a);
      if (wp->TTRType == TTRType_Normal+TTRType_Optional)
      {
        /* Use the supplied TTR to translate any full alpha pixels */
        ins(ws, LDRB(R(r_pixel), R(r_table)) | INDEX(R(r_pixel), 0) | HS,   "LDRHSB  r_pixel,[r_table, r_pixel]      ; byte table lookup");
      }
      if(alpha_shift > 2)
      {
        IFDEBUG(do_sprintf(a,"LDRLO   r_alpha,[r_blendtable,r_alpha,LSR #%d]",alpha_shift-2);)
        ins(ws, LDR(R(r_alpha), R(r_blendtable)) | INDEX_LSR(R(r_alpha), alpha_shift-2) | LO, a);
      }
      else
      {
        IFDEBUG(do_sprintf(a,"LDRLO   r_alpha,[r_blendtable,r_alpha,LSL #%d]",2-alpha_shift);)
        ins(ws, LDR(R(r_alpha), R(r_blendtable)) | INDEX(R(r_alpha), 2-alpha_shift) | LO, a);
      }
      AND(R(r_temp1), R(r_outword), IMM((1<<wp->BPP)-1) | IMMROR(0) | LO,   "ANDLO   r_temp1, r_outword, #(1<<out_bpp)-1");
      if(ttr_before_blend)
      {
        /* r_pixel should have been translated by ColourTTR to output bpp */
        assert(pixelformat == ws->out_pixelformat, ERROR_FATAL);
        ORR(R(r_temp1), R(r_pixel), OP2R(R(r_temp1)) | LSLI(wp->BPP) | LO,  "ORRLO   r_temp1, r_pixel, r_temp1, LSL #out_bpp");
      }
      else
      {
        /* TTR will have been applied for full alpha pixels, but won't have been applied for these partial alpha ones */
        assert(pixelformat == ws->in_pixelformat, ERROR_FATAL);
        ttr_before_blend = TRUE;
        ORR(R(r_temp1), R(r_pixel), OP2R(R(r_temp1)) | LSLI(ws->in_bpp) |LO,"ORRLO   r_temp1, r_pixel, r_temp1, LSL #in_bpp");
      }
      ins(ws, LDRB(R(r_pixel), R(r_alpha)) | INDEX(R(r_temp1), 0) | LO,     "LDRLOB    r_pixel, [r_alpha, r_temp1]");
3848 3849 3850
    }
    else
    {
3851 3852 3853 3854 3855 3856
      assert(alpha_bits == 1, ERROR_FATAL);
      if ((wp->ColourTTR != 0) && (ws->ColourTTRFormat <= PixelFormat_8bpp))
      {
        /* Should be impossible, 1bpp alpha only possible with 1bpp alpha channel, which means 16bpp sprite pixels */
        assert(0, ERROR_FATAL);
      }
3857 3858
    }
    pixelformat = ws->out_pixelformat;              /* we've finished */
3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878
    break;
  }

  pixelformat = apply_dither(wp,ws,pixelformat,&have_dithered);

  switch(ws->blendimpl)
  {
  case BlendImpl_InverseTable:
    /* Inverse table lookup for 15bpp -> palette
       TODO - Could potentially use ColourTTR here if it was an optional (i.e.
       ordinary) 32K table */
    assert(ws->out_pixelformat == PixelFormat_8bpp, ERROR_FATAL);
    assert(pixelformat >= PixelFormat_12bpp, ERROR_FATAL);
    if(pixelformat != PixelFormat_15bpp)
    {
      convert_pixel(wp,ws,pixelformat,PixelFormat_15bpp);
    }
    ins(ws, LDRB(R(r_pixel), R(r_inversetable)) | INDEX(R(r_pixel), 0),  "LDRB    r_pixel,[r_inversetable, r_pixel]      ; 32K-style inversetable lookup");
    pixelformat = PixelFormat_8bpp;
    break;
3879 3880 3881 3882 3883 3884 3885 3886 3887
  }

  /* Do any extra conversion necessary */
  if(pixelformat != ws->out_pixelformat)
  {
    convert_pixel(wp,ws,pixelformat,ws->out_pixelformat);
    pixelformat = ws->out_pixelformat;
  }

3888 3889 3890 3891 3892 3893 3894
  if(wp->blending)
  {
    DEFINE_LABEL(translate_noalpha,"Skip blend calc");
    DEFINE_LABEL(translate_noalpha2,"Skip blend calc");
  }

  if ((ws->gcol == 2) && ((pixelformat & PixelFormat_BPPMask) != PixelFormat_32bpp)) /* AND plot action which did something stupid for 32bpp (GPS)*/
3895
  {
3896
    MOV(R(r_pixel), OP2R(R(r_pixel)) | LSLI(31-(wp->BPC)),  "MOV     r_pixel, r_pixel, LSL 31-out_bpc");
3897
    ORR(R(r_pixel), R(r_pixel), IMM(2) | IMMROR(2),         "ORR     r_pixel,r_pixel,#&80000000 ");
3898
    MOV(R(r_pixel), OP2R(R(r_pixel)) | ASRI(31-(wp->BPC)),  "MOV     r_pixel, r_pixel, ASR 31-out_bpc");
3899 3900 3901
  }

  comment(ws, "r_pixel is now a destination pixel.");
3902
  assert(have_dithered == (ws->odither != 0), ERROR_FATAL);
3903 3904

  if (DPIXEL_OUTPUT)
3905 3906 3907
    ORR(R(r_pixel), R(r_pixel), OP2R(R(r_pixel)) | LSLI(wp->BPP),   "ORR     r_pixel,r_pixel,r_pixel,LSL #out_bpp ; double pixel output");

  switch_bank(wp, ws, REGFLAG_PERPIXEL,REGFLAG_XLOOP);
3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947

  newline();
}

/**************************************************************************
*                                                                         *
*    Advancing the current pixel.                                         *
*                                                                         *
**************************************************************************/

static void odither_inc(asm_workspace *wp, workspace *ws, int xy)
/* Call every output pixel - alternates the ordered dither addition value
 * xy == 0 for x, 1 for y
 *
 * Requirements:
 * ws->odither valid
 * dither_expansion_init() called
 * r_oditheradd allocated if necessary
 */
{
  if (ws->odither)
    EOR(R(r_oditheradd),R(r_oditheradd), IMM(1 << (ws->odither - xy)) | IMMROR(8),
      xy == 0 ? "EOR     r_oditheradd,r_oditheradd,#odither_eorvalue ; alternate dither offset"
              : "EOR     r_oditheradd,r_oditheradd,#odither_eorvalue:SHR:1 ; alternate dither offset");
  UNUSED(wp);
}

/**************************************************************************
*                                                                         *
*    Misc                                                                 *
*                                                                         *
**************************************************************************/

static int get_key_word(asm_workspace *wp, workspace *ws)
/* Compute the low bits of the key word value */
{
  int key_word;

  key_word = ws->in_pixelformat             /* 0..5 */
               + (ws->out_pixelformat << 6) /* 6..11 */
3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
               + (ws->gcol << 12)           /* 12..14 */
               + (ws->masktype << 15)       /* 15..16 */
               + (wp->TTRType << 17);       /* 17..20 */
  if (DPIXEL_OUTPUT) key_word |= 1<<21;
  if (DPIXEL_INPUT) key_word |= 1<<22;
  if (PLOTMASK || TRANSMASK) key_word |= 1<<23;
  if (ISTRANS) key_word |= 1<<24;
  key_word |= (wp->blending << 25);         /* 25..26 */

  /* Bits 27+ are free for putscaled/sprtrans to use as they please */
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973

  return key_word;
}

static void compile_buffer_init(asm_workspace *wp, workspace *ws)
/* We intend to compile some code. Pick a buffer to use, and set up
 * for generating into it. We use a simple round-robin for reusing buffers,
 * rather than attempting to do LRU.
 */
{
  label *p;
  regname *r;
  code_buffer *b = &(ws->buffers[ws->build_buffer]);
  ws->compile_base = &(b->code[0]);
  ws->compile_ptr = ws->compile_base;
  ws->compile_lim = ws->compile_base + BUFSIZE;
3974 3975
  ws->regframeoffset = 0;
  ws->pixel_expansion_mask[0] = ws->pixel_expansion_mask[1] = 0;
3976
  FOR_EACH_LABEL(p) {p->def = 0; p->ref = 0;} /* zap all the labels to be undefined. */
3977
  FOR_EACH_REGISTER_NAME(r) { r->regno = -1; r->flags = 0; r->spindex = -1; }
3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
#ifdef DEBUG
  tracef("Compile buffer initialised.\n");
  if(ISTRANS)
  {
    tracef("%t20; Blitting code for %s\n" _
      (TRANSMASK ? "PlotMaskTransformed" : "PlotSpriteTransformed"));
  }
  else
  {
    tracef("%t20; Blitting code for %s, scale factors %i:%i,%i:%i outoffset %x\n" _
      (PLOTMASK ? "PlotMaskScaled" : "PutSpriteScaled") _
      b->xadd - b->xdiv _ b->xdiv _ b->yadd _ b->ydiv _ wp->save_outoffset);
  }
3991
  tracef("%t20; gcol action=%i in-bpp=%i out-bpp=%i in-dpix=%s out-dpix=%s masktype=%i table=%s\n" _
3992 3993
    ws->gcol _ (1<<wp->save_inlog2bpp) _ wp->BPP _
    whether(DPIXEL_INPUT) _ whether(DPIXEL_OUTPUT) _
3994 3995
    ws->masktype _
    whether(wp->ColourTTR != 0));
3996 3997 3998 3999 4000 4001
  tracef("%t20; Src format=%x Dest format=%x\n" _ ws->in_pixelformat _ ws->out_pixelformat);
  tracef("%t20.; Generated by compiler of (%s %s)\n" _ __DATE__ _ __TIME__);
  comment(ws, "Get register and workspace definitions, turn on listing");
  tracef("%t28.GET     w.GenHdr\n");
  tracef("%t28.OPT     1\n");
#endif
4002 4003 4004 4005 4006
  RN(wp, 12, REGFLAG_GLOBAL, "workspace pointer") /* TODO - HACK - Make non-global again somehow */
//  RN(wp, 12, (wp->is_it_jpeg?REGFLAG_GLOBAL:REGFLAG_INPUT), "workspace pointer")
  RN(sp, 13, REGFLAG_GLOBAL, "stack pointer")
  RN(lr, 14, REGFLAG_USED, "link register")
  RN(pc, 15, REGFLAG_GLOBAL, "program counter")
4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
}

static void compile_buffer_done(workspace *ws)
/* Finished compiling code sequence. */
{
#ifdef DEBUG
  label *p;
#endif

  tracef("%t28.END\n");
  tracef("Compile buffer done, %i words generated.\n" _ ws->compile_ptr - ws->compile_base);
  /* Increment pointer for next buffer to reuse. */
  ws->build_buffer++;
  if (ws->build_buffer >= NBUFFERS) ws->build_buffer = 0;
#ifdef DEBUG
  /* Check no unresolved references to labels */
  FOR_EACH_LABEL(p)
  {
    IFDEBUG(if(p->ref != 0) tracef("Unresolved reference to label %s at %x\n" _ p->name _ sizeof(int) * (p->ref - ws->compile_base));)
    assert(p->ref == 0, ERROR_FATAL);
  }
#endif
  /* ws->compile_base can be used as the base of the resulting procedure. */
}

4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
static void init_useful_constants(asm_workspace *wp, workspace *ws)
{
  /* Various useful constants not provided directly by wp. */
  newline();
  comment(ws, "Various useful constants");
  if (DPIXEL_INPUT)
    comment(ws, "Double-pixel input - pixels are not the same as double-pixels");
  else
    comment(ws, "Not double-pixel input - pixels are exactly the same as double-pixels");
  ws->in_bpp         = 1 << wp->save_inlog2bpp;
  ws->in_bpc         = 1 << wp->save_inlog2bpc;
  ws->in_pixmask     = (1 << ws->in_bpp) - 1;
  tracef("%t20.in_bpp  *       %i %t68; bits per input pixel\n" _ ws->in_bpp);
  tracef("%t20.in_bpc  *       %i %t68; bits per input double-pixel ('character')\n" _ ws->in_bpc);
  tracef("%t20.in_l2bpp  *     %i %t68; log base 2 of bits per input pixel\n" _ wp->save_inlog2bpp);
  if (ws->in_bpp <= 8) tracef("%t20.in_pixmask *    %i %t68; input pixel mask\n" _ ws->in_pixmask);

  if (SOURCE_MASK)
  {
    if (SOURCE_ALPHAMASK)
    {
      ws->mask_bpp     = 8;
      ws->mask_bpc     = 8;
      ws->mask_pixmask = 255;
    }
    else if (SOURCE_BPPMASK) /* a bit mask */
    {
      ws->mask_bpp     = 1;
      ws->mask_bpc     = 1;
      ws->mask_pixmask = 1;
    }
    else
    {
      ws->mask_bpp     = ws->in_bpp;
      ws->mask_bpc     = ws->in_bpc;
      ws->mask_pixmask = ws->in_pixmask;
    }
    tracef("%t20.mask_bpp *      %i %t68; bits per mask pixel\n" _ ws->mask_bpp);
    tracef("%t20.mask_bpc *      %i %t68; bits per mask double-pixel\n" _ ws->mask_bpc);
    tracef("%t20.mask_pixmask *  %i %t68; mask pixel mask\n" _ ws->mask_pixmask);
  }
  else
    comment(ws, "No input mask");

  if (DPIXEL_OUTPUT)
    comment(ws, "Double-pixel output - pixels are not the same as double-pixels");
  else
    comment(ws, "Not double-pixel output - pixels are exactly the same as double-pixels");
  ws->out_l2ppw      = 5 - wp->Log2bpc;
  ws->out_ppw        = 1 << ws->out_l2ppw;
  ws->out_pixmask    = (1 << wp->BPP) - 1;
  ws->out_dpixmask   = (1 << wp->BPC) - 1;
  tracef("%t20.out_bpp *       %i %t68; bits per output pixel\n" _ wp->BPP);
  tracef("%t20.out_bpc *       %i %t68; bits per output double-pixel\n" _ wp->BPC);
  tracef("%t20.out_l2bpp *     %i %t68; log base 2 of bits per output pixel\n" _ wp->Log2bpp);
  tracef("%t20.out_l2bpc *     %i %t68; log base 2 of bits per output double-pixel\n" _ wp->Log2bpc);
  tracef("%t20.out_ppw *       %i %t68; double-pixels per output word\n" _ ws->out_ppw);
  tracef("%t20.out_l2ppw *     %i %t68; log base 2 of double-pixels per output word\n" _ ws->out_l2ppw);
  if (wp->BPC <= 8)
  {
    tracef("%t20.out_pixmask *   %i %t68; output pixel mask\n" _ ws->out_pixmask);
    tracef("%t20.out_dpixmask *  %i %t68; output double-pixel mask\n" _ ws->out_dpixmask);
  }
}

4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121
/**************************************************************************
*                                                                         *
*    JPEG handling.                                                       *
*                                                                         *
**************************************************************************/

#ifdef ASMjpeg
#include "rojpeg.c"
#endif

/**************************************************************************
*                                                                         *
*    PutScaled                                                            *
*                                                                         *
**************************************************************************/

#include "putscaled.c"

/**************************************************************************
*                                                                         *
*    SprTrans                                                             *
*                                                                         *
**************************************************************************/

#include "sprtrans.c"