Parallax XOR Scroller
SCROLL.PAS · Turbo Pascal + 386 asm · VGA Mode X 360×480

The four-layer XOR scroller

One line of text drawn four times at four depths. Each copy writes its own bit of the pixel value, and the palette turns every combination of bits into an additive colour blend. Below is a live reconstruction, followed by how each part of the code works.

Hover the band to inspect a pixel.

This uses the scene's real layer constants, colour schemes and palette code (360×480 mode, shift = 145), and the message from the commented-out line 620. The original font is replaced by Anton, and the timer is assumed to tick 60 times a second. The animated objects and the shine sprite are left out. "Planes drawn" simulates the early exit the engine takes on slow machines.

Overview

Three ideas carry the whole effect

XOR edge fill

Letters are stored as outlines. A running XOR down each column fills them, and clears the scratch buffer at the same time.

One bit per layer

Each of the four copies XORs its own bit into the pixel, so overlaps combine with no z-sorting and no read-modify-write.

Additive palette

Each bit is a coloured light. The palette is built so that index i is the clamped sum of the lights for the bits set in i.

The parallax is the same text drawn four times, each copy scaled toward a vanishing point at the centre of the screen.

Screen setup · lines 587–605, 795–813

Mode X, double-height lines and three pages

  • The screen is Mode X (planar VGA) at 360 pixels wide, so one plane row is 90 bytes. One byte column in a plane covers 4 screen pixels, and most of the code works in that unit.
  • Lines 804–813 set CRTC register 9 (Max Scan Line) to 1, so every line is shown twice. 480 lines become 240 logical lines.
  • The text band is 128 rows tall and starts at SCREEN_Y, which is row 50 (90*50).
  • Three pages live at 0, 90*240 and 90*480. setVisiblePage, presumably called from the timer or retrace interrupt, points the CRTC start address at whichever page is ready.
free→drawing→ready→display→free
DrawEdge · lines 124–214  /  xorfill_xmode · lines 37–64

Drawing a letter: XOR edge fill

A glyph is a list of 16-bit words: the low byte is y and the high byte is x (in plane byte columns). Each word marks a spot where the glyph's outline crosses a column vertically, where a solid run starts or ends.

DrawEdge only XORs the layer's value into those edge pixels of a scratch buffer. It has a fast path with no clipping (@n1) and a clipped path (@n0). In the clipped path, cmp dh,90 / jae rejects both x < 0 and x ≥ 90 with one unsigned compare.

xorfill_xmode then walks down each column keeping an accumulator. It toggles on at a top edge and off at a bottom edge, writes the result to video memory, and zeroes the scratch buffer as it goes:

; for each 4-byte strip, for each of 128 rows
xor eax, ds:[si]      ; toggle on the edges in this row
mov es:[di], eax      ; write 4 filled bytes to VGA
mov ds:[si], ebx      ; ebx = 0: clear the scratch buffer for next frame
Two glyphs from different layers share one buffer: a ring from layer 2 (value 4, red) and a T from layer 1 (value 2, green). Left: the edge values DrawEdge wrote, faded once the fill has read and cleared them. Right: what reaches the screen. Where both shapes overlap, the accumulator holds 6 and the pixel turns yellow. Since each layer owns its own bit, the two fills never disturb each other.

The scratch buffer is stored strip by strip so the fill can read it sequentially, 32 bits at a time:

offset = y*4 + (x shr 2)*512 + (x and 3)     { line 133 }
get_xx_w · lines 296–323

Planes and sub-pixel positioning

In Mode X, pixel x lives in plane x mod 4. The scene renders each plane separately into buf[0..3]. A glyph placed at screen x contributes only its columns gx where (x + gx) mod 4 = p to plane p.

So the font stores four pre-decimated variants of every glyph, each with a 6-byte header (offset, length, width). That is why the lookup is (ascii-65)*24 + w*6. get_xx_w picks the variant w = (p − x) and 3 and the starting byte column xx.

A 9-pixel-wide glyph at screen x. Only the highlighted columns land in the selected plane. Those columns make up glyph variant w, and their first one lands in plane byte xx. This gives smooth one-pixel scrolling even though each plane is addressed in 4-pixel steps.

Progressive refinement

Planes are written with a growing map mask, so each pass overwrites the planes above it:

order[0]:=15;  // 1111  plane 0's image goes to all 4 planes
order[1]:=14;  // 1110
order[2]:=12;  // 1100
order[3]:=8;   // 1000

When the display is waiting for a new frame (if find(ready)=-1 then break, line 973), the loop stops early. The frame still looks complete, just at a quarter or half of the horizontal resolution. A slow machine loses detail instead of frames. Drag "Planes drawn" in the demo above to see it.

Parallax · lines 615–618, 933, 949, 270

Four copies of one line, at four depths

Layerprlxwidth (spacing)width / 42depth ≈ 1/prlx

Each frame computes:

text_x := 280 - (q shl 1);                    { 2 px per timer tick }
x      := round(text_x*prlx + txt[l].shift);
x      := text_chars_x[ch, layer] + o;        { o = byte column from get_xx_w }

text_chars_x is precomputed and loaded from textw480.bin. The commented-out precalc_text (lines 223–253) shows how: cumulative letter positions using each layer's width, with narrow and wide factors for I, L, M, W and others. The spacing ratio matches prlx, so character i of layer k lands at about:

screen_x ≈ shift + prlx_k × (text_x + 4·42·i)

That is a perspective projection with the vanishing point at x = shift, about the centre of the screen. Speed and letter spacing both shrink with distance, so the copies drift apart and line up again as they scroll.

Top-down view. The four rows hold the same letters at the same world positions, placed behind the screen at depth 1/prlx − 1. Rays from the eye through one letter hit the screen at four different places. A point at x = shift never moves, so it is the vanishing point.

Two simplifications:

  • All layers use the same glyphs (the lookup has no layer term) and the same y (DrawEdge(x, 0, …)). Only the horizontal direction is 3D: letters don't shrink and don't move toward a horizon.
  • Only 7 characters per layer are drawn, starting at ch_start. When a layer-3 character is fully off the left edge (x < -50, line 272), ch_start moves forward. Layer 3 is the slowest, so it is always the last to leave and the skip is safe for every layer.
text · lines 259–293  /  color_scheme · lines 595–600

Colour is a bitmask, chosen per word

The "colour" that text passes to DrawEdge is a set of bits. It comes from color_scheme[segment, layer], where the segment depends on the character's position in the message. With the message on line 620, the segment boundaries fall exactly on word boundaries, so every word gets its own colour arrangement:

CharsWordL0 frontL1L2L3 back

Within a segment no two layers share a bit. After the fill, each pixel's value is the set of layers covering it, so the drawing order doesn't matter. Bits 5–7 (32, 64, 128) are reserved for the animated objects and the shine sprite (32 shl l, line 416). The scheme variable on line 256 is never used.

mix_col · set_palette_segment · lines 72–120, 816–850

A palette built like stage lights

mix_col mode 0 is a per-channel add, clamped at 63 (the VGA DAC is 6-bit). Modes 1 and 2, alpha and 70/30 blends, are never called. set_palette_segment(src, dst, arr, c) sets eight entries: palette[dst+n] := arr[src+n] + c. Each step copies everything built so far and adds one new light:

Each strip is the palette range one step produces. Its colours are the previous entries plus that step's light. The last step's 128 entries wrap onto four rows.

As a result, palette[i] is the sum of the lights for the bits set in i, clamped at 63. Overlapping layers blend additively, like coloured gels in front of lamps. Try any combination:

B = bit 0 blueG = bit 1 greenR = bit 2 redM = bit 3 magentaC = bit 4 cyan
Indices 0–31 are every combination of the five text bits. The outlined entry is the pixel value you get where the selected layers overlap.
forced to black on the DAC
The full 256-entry palette as uploaded. Lines 840–850 then set every index made only of bits 5–7 to black: 32, 64, 128, 96, 160, 192 and 224 (lines 848–850 all set 224). An object alone on the background is invisible, and over text it shows as a grey brightening.
Default VGA palette · mode 13h

The word colours next to the stock VGA palette

Each colour from ANOTHER, SCROLLER and ENJOY, matched to the closest of the 256 colours a VGA card loads by default in mode 13h. In each pair, the left half is the scene's colour and the right half is the nearest default entry. Δ is the straight-line RGB distance in 6-bit DAC units, where 0 is an exact match.

Every single-layer colour has a close match. Three overlap colours don't: olive yellow (40,40,0), dusty pink (40,30,30) and sage (30,40,30). The default palette has no mid-bright yellow and no low-saturation colours at that brightness, so they fall to a darker olive or plain grey.

The default palette here is generated from its standard layout: 16 EGA colours, 16 greys, nine 24-hue rings at three brightness and saturation levels, and 8 blacks. Hex values convert 6-bit values with v × 255 / 63.

Caveat

Where two words meet

Bits are only clash-free within one segment. The layers are spread out horizontally, so layer 1 can be showing a letter of SCROLLER (value 3 = bits 0+1) while layer 3 shows a letter of ANOTHER in the same spot (value 9 = bits 0+3). Both use bit 0, so where they overlap that bit cancels under XOR:

The overlap shows a slightly different colour. It is easy to miss in motion and was probably accepted on purpose.