  .-----------------------------------------------------------------.
 /  CHUNKY TO PLANAR (C2P)                                           \
 '-------------------------------------------------------------------'

  The fire is computed in a "chunky" buffer: one byte per block of 8x8
  pixels, 40 blocks wide and 25 blocks high, 1000 bytes. The byte is the
  color of the block, from 0 to 15: one block, one byte, one color.

             CHUNKY BUFFER                     SCREEN
             40 x 25 bytes                320 x 200 pixels

           x=0  x=1      x=39             <-8 px-> <-8 px->
         +----+----+-..-+----+           +--------+--------+-..
   y=0   | 11 |  6 | .. |  0 |           |BBBBBBBB|66666666|   ^
         +----+----+-..-+----+  1 byte   |BBBBBBBB|66666666|   |
   y=1   |  9 | 12 | .. |  3 |  ======>  |BBBBBBBB|66666666|   |
         +----+----+-..-+----+  1 block  |BBBBBBBB|66666666| 8 lines
            :    :         :             |BBBBBBBB|66666666|   |
         +----+----+-..-+----+           |BBBBBBBB|66666666|   |
   y=24  |  1 |  0 | .. |  2 |           |BBBBBBBB|66666666|   |
         +----+----+-..-+----+           |BBBBBBBB|66666666|   v
                                         +--------+--------+-..
                                         |99999999|CCCCCCCC|
                                         |   ..   |   ..   |

                               screen colors in hex: B = 11, C = 12

  The screen of the Atari ST is not chunky but "planar". In low
  resolution, 16 pixels side by side are stored in 4 words (8 bytes), one
  word per bitplane. A pixel owns the same bit in each of the 4 words
  (bit 15 for the leftmost pixel), and its color is these 4 bits put
  together:

                        16 pixels
                <----------------------------->        screen memory
     plane 0    1 . . . . . . . . . . . . . . .        word at +0
     plane 1    1 . . . . . . . . . . . . . . .        word at +2
     plane 2    0 . . . . . . . . . . . . . . .        word at +4
     plane 3    1 . . . . . . . . . . . . . . .        word at +6
                |                                      next 16 pixels
                '--> pixel 0 = %1011 = color 11        at +8
                     (bits of the planes 3 2 1 0)

  Splitting the color of each of the 64000 pixels into 4 bits, one per
  plane, is a lot of work. But a block is 8 pixels wide: it covers
  exactly one byte of each of the 4 words, and its 8 pixels share the
  same color. So each bit of the color becomes a whole byte, $FF (the 8
  pixels set) if the bit is 1, $00 if it is 0. Two instructions do it:

     lsr.b  #1,d0     shifts the color right: its lowest bit falls into
                      the carry
     scs    (a5)+     "set if carry set": writes $FF if the carry is 1,
                      $00 if it is 0, and moves to the next byte

  4 times, for the planes 0 to 3. Two blocks side by side make 16 pixels:
  the left block (d0) fills the high bytes of the 4 words, the right block
  (d1) the low bytes. With color 11 on the left and color 6 on the right:

                 d0 = 11, left block          d1 = 6, right block
                 lsr.b #1,d0      carry       lsr.b #1,d1      carry
     plane 0     %1011 -> %101      1         %0110 -> %011      0
     plane 1     %101  -> %10       1         %011  -> %01       1
     plane 2     %10   -> %1        0         %01   -> %0        1
     plane 3     %1    -> %0        1         %0    -> %0        0

  The scs write one byte per carry, in this order:

                    plane 0       plane 1       plane 2       plane 3
                   hi     lo     hi     lo     hi     lo     hi     lo
          a5 -> +------+------+------+------+------+------+------+------+
                | $FF  | $00  | $FF  | $FF  | $00  | $FF  | $FF  | $00  |
                +------+------+------+------+------+------+------+------+
                   ^      ^
                   |      '--- carry of d1, right block
                   '---------- carry of d0, left block

  And the 4 words give back both colors:

                        left block   right block
     plane 0  $FF00      11111111     00000000
     plane 1  $FFFF      11111111     11111111
     plane 2  $00FF      00000000     11111111
     plane 3  $FF00      11111111     00000000
                         --------     --------
     color               BBBBBBBB     66666666
                         %1011=11     %0110=6

  The conversion of a row of blocks: 20 pairs of blocks, 8 bytes each,
  make one line of 320 pixels (160 bytes):

     row:    movea.l a5,a4        ; a4 = this line, read back by the copy
             moveq   #20-1,d4     ; 20 pairs of blocks
     col:    move.b  (a3)+,d0     ; Left block
             move.b  (a3)+,d1     ; Right block
             moveq   #4-1,d2      ; 4 planes
     plane:  lsr.b   #1,d0
             scs     (a5)+        ; High byte: left block
             lsr.b   #1,d1
             scs     (a5)+        ; Low byte: right block
             dbra    d2,plane
             dbra    d4,col

  A block is 8 lines high, so the 7 lines under the converted line are
  the same, and copying them is much cheaper than converting them. a4
  points to the converted line and stays 160 bytes (one line) behind a5:
  each line is copied from the line above it, itself just copied.

              +----------------------------------+
     line 0   | converted by lsr/scs (160 bytes) |---.
              +----------------------------------+   | move.l (a4)+,(a5)+
     line 1   | copy of line 0                   |<--'---.
              +----------------------------------+       |
     line 2   | copy of line 1                   |<------'---.
              +----------------------------------+           |
                               ..                           ..
              +----------------------------------+           |
     line 7   | copy of line 6                   |<----------'
              +----------------------------------+

             move.w  #7*40-1,d4   ; 7 lines of 40 longs
     copy:   move.l  (a4)+,(a5)+  ; a4 stays one line behind a5
             dbra    d4,copy

  25 rows of blocks of 8 lines fill the 200 lines of the screen: only one
  line in 8 is converted, the other 7 are copies.

  .-----------------------------------------------------------------.
 /  FIRE EFFECT                                                      \
 '-------------------------------------------------------------------'

  The byte of a block is its heat, and the heat is its color: 0 is cold,
  15 the hottest. Under the 25 visible rows, the buffer holds 2 more rows
  that are never shown, the embers:

           x=0                                    x=39
          +----------------------------------------+
   y=0    |                                        |  ^
          |      25 visible rows, 1000 bytes,      |  |  the heat rises
          |      shown by the c2p                  |  |  and cools down
   y=24   |                                        |  |
          +========================================+  <-- screen bottom
   y=25   | F F 0 F F F F F 0 F F F F 0 F F F F .. |  embers, 80 bytes,
   y=26   | F 0 F F F F F F F F 0 F F F F F 0 F .. |  new every frame
          +----------------------------------------+
                                            (F = 15)

  Embers: every frame, the 80 blocks of the 2 hidden rows get a new heat,
  15 or 0, picked by a small pseudo-random generator on 16 bits,
  x = rol(x, 6) + $1DEC. The cold holes move at each frame and make the
  flames flicker.

  Propagation: each block takes its new heat from 4 blocks under it, 3 on
  the row below and 1 two rows below:

                       x-1    x    x+1
                           +-----+
          row y            |  N  |         ^
                     +-----+-----+-----+   |
          row y+1    |  A  |  B  |  C  |   |  the heat
                     +-----+-----+-----+   |  goes up
          row y+2          |  D  |         |
                           +-----+

          N = (A + B + C + D - 1) / 4        (0 if A + B + C + D = 0)

          example: (15 + 15 + 0 + 15 - 1) / 4 = 44 / 4 = 11

  The / 4 averages the 4 blocks (dropping the remainder), the - 1 cools
  the fire down: without it, an area of heat 1 would stay at
  (1 + 1 + 1 + 1) / 4 = 1 forever, with it (4 - 1) / 4 = 0. So the heat
  drops as it rises, and the flames die out on their way up.

  A single pass, from the top row down, writes the new heats over the old
  ones in the buffer itself: when row y is computed, rows y+1 and y+2 are
  not updated yet and still hold the heat of the last frame, so no second
  buffer is needed. The edges have no special case: the buffer is one long
  line of bytes, so for x = 0 the x-1 block is (39,y), and for x = 39 the
  x+1 block is (0,y+2).

  Each frame:

     +-----+   +---------+   +-------------+   +--------+
     | c2p |-->| palette |-->| propagation |-->| embers |--.
     +-----+   +---------+   +-------------+   +--------+  |
        ^                                                  |
        '--------------------------------------------------'
