Sprites

Everything the Atari Lynx draws is a sprite painted by Suzy's blitter: there is no pixel-plot instruction, so a line, a box or a glyph is all built as sprite imagery. A sprite is described to the hardware by a sprite control block (SCB) — a short structure in RAM that points at packed pixel data, a following SCB, a pen palette, and the position, size, stretch and tilt to paint at. This page explains the SCB and its linking, the eight SCB struct variants and the reload bits that produce them, the eight sprite types, the pen palette, the packed pixel format, and scaling, stretch, tilt and flipping — all grounded in the structs and defines in <lynx/_suzy.h>. It is the companion to the Collision detection page, which owns the collision half of the same hardware, and the Graphics page, whose gfx_sprite paints the SCBs described here.

1. The hardware sprite model

The Lynx draws nothing a pixel at a time. Every visible thing is a sprite that Suzy paints into the video buffer, and the software's job is to hand Suzy a list of sprites to paint each frame. Two features set this engine apart from an ordinary blitter. First, sprites are sized in real time as they are painted — reduced or enlarged, horizontally and vertically by independent factors — so one image can be drawn at any scale. Second, each scan line of sprite pixels is encoded as either packed (run-length compressed) or literal (verbatim) data in RAM, and Suzy decodes it on the fly.

The hardware fixes a few limits. A sprite's vertical size is unlimited, but a single scan line of source data may be at most 254 bytes (about 508 unscaled pixels). Each pixel carries a 1, 2, 3 or 4 bit pen index (the BPP_1…BPP_4 selectors in SPRCTL0). Pixels that fall off-screen are clipped by the hardware; sprites can be flipped horizontally and/or vertically about their reference point; and any individual sprite can be skipped. Neither the SCB nor the sprite data may live in Mikey ROM — both are reached by pointer and may sit anywhere else in RAM.

In lynxcc you rarely start the engine by hand. gfx_sprite (see the Graphics page) takes a pointer to an SCB, performs the register writes, and busy-waits until Suzy has finished. This page describes the data structure gfx_sprite consumes, and what you are building when you author an SCB directly — as spritetest.c, packtest.c, spriteslice.c and spritesheet.c all do.

2. The sprite control block

A sprite control block is a small structure with a fixed field order the hardware requires. It has three parts: a five-field fixed head that is always present and always loaded; a reloadable body of position, size, stretch and tilt whose presence is governed by the reload bits in SPRCTL1 (section 2.2); and a pen palette, which the hardware treats as a separate reloadable block. <lynx/_suzy.h> (pulled in by <lynx/lynx.h>) declares one C struct for every legal shape; the fullest is SCB_REHVST_PAL:

typedef struct SCB_REHVST_PAL {   /* SCB with every attribute present   */
  unsigned char sprctl0;          /* $FC80: bpp, H/V flip, sprite type    */
  unsigned char sprctl1;          /* $FC81: pack, reload, reuse-pal, skip  */
  unsigned char sprcoll;          /* $FC82: collision number + no-collide  */
  char          *next;            /* -> next SCB in the list, 0 = last    */
  unsigned char *data;            /* -> packed sprite pixel data          */
  signed   int  hpos, vpos;       /* screen position of the reference pt   */
  unsigned int  hsize, vsize;     /* 8.8 fixed-point scale (256 = 1x)      */
  unsigned int  stretch, tilt;    /* per-scan-line H-size / H-pos adders    */
  unsigned char penpal[8];        /* pen-index palette (4bpp = 8 bytes)    */
} SCB_REHVST_PAL;

The first three bytes are control (detailed in sections 2.2, 3 and — for sprcoll — the Collision detection page). Then come two 16-bit pointers: next, the address of the following SCB, and data, the address of the packed pixel data. The body holds the signed screen position (hpos, vpos), the unsigned 8.8 scale (hsize, vsize), and the per-scan-line stretch and tilt adders. The palette closes the block.

fixed head reloadable body pen palette always present gated by SPRCTL1 reload bits separate block sprctl0 sprctl1 sprcoll next data hpos vpos hsize vsize stretch tilt penpal 1 B1 B1 B 2 B2 B 2 B2 B 2 B2 B 2 B2 B 8 B Shorter SCBs drop trailing body fields and/or the palette — see §2.2. Optional software-only bytes (collision depository, id, Z, back-pointer) may sit outside this block; the hardware ignores them.
A full SCB_REHVST_PAL: the fixed head, the reloadable body, and the separately reloadable pen palette. The penpal cell is drawn wider to hint that it is eight bytes.

2.1. Linking and painter's order

SCBs form a singly linked list. Each block's next field holds the address of the following SCB; the engine walks the chain until it reads a next of 0, which marks the last sprite. You start a frame by giving Suzy the address of the first SCB; the hardware follows the pointers from there. Because every block is reached by pointer, SCBs and their data may be scattered anywhere in RAM.

The list is painted in painter's order: the first SCB is drawn first and each later sprite overwrites whatever it overlaps, so the chain runs back-to-front in depth. This is also the order that makes collision numbers meaningful — a sprite only ever discovers things drawn before it (see the Collision detection page). One SCB is needed for each on-screen occurrence.

Each SCB carries its own data pointer, so the usual case is that every sprite in a chain points at a different pixel block — the first diagram below shows three SCBs each with its own image. Sharing is an option, not a requirement: because the data field is just a pointer, several SCBs may aim at the same block when you want the same picture more than once. A repeated tile or a sprite sheet then costs one small control block per instance and a single copy of the pixels — the second diagram shows SCB 0 and SCB 1 sharing one block while SCB 2 keeps its own (spritesheet.c and spriteslice.c show this). The two cases can be mixed freely within one chain.

painter's order — SCB 0 first (back), SCB 2 last (front) SCB 0 SCB 1 SCB 2 data block A data block B data block C next · data next · data next = 0 own pixels own pixels own pixels
The common case: three SCBs linked by next, the last terminating with 0, and each pointing at its own data block. A skipped sprite (SKIP bit) is still read far enough to follow its next, so skipping never breaks the chain.
painter's order — SCB 0 first (back), SCB 2 last (front) SCB 0 SCB 1 SCB 2 shared block data block B next · data next · data next = 0 one copy, drawn twice own pixels
The optional shared case: SCB 0 and SCB 1 point at one data block to draw the same picture twice, while SCB 2 still points at its own. Sharing and distinct data can be mixed freely in one chain.

2.2. The eight struct variants and the reload bits

Why does <lynx/_suzy.h> declare eight SCB structs? Because the hardware lets an SCB omit its trailing fields. The reload field in SPRCTL1 selects how much of the size/stretch/tilt tail this SCB carries; anything it does not reload keeps the value left in Suzy's registers by the previous sprite. A separate bit, REUSEPAL, drops the pen palette the same way. Each struct is the exact C shape of one legal combination, so picking a struct is picking the reload bits:

SPRCTL1 reloadBody fields loaded from this SCBPalette_suzy.h struct
REHVST ($30)hpos, vpos, hsize, vsize, stretch, tiltownSCB_REHVST_PAL
REHVST + REUSEPALhpos, vpos, hsize, vsize, stretch, tiltreusedSCB_REHVST
REHVS ($20)hpos, vpos, hsize, vsize, stretchownSCB_REHVS_PAL
REHVS + REUSEPALhpos, vpos, hsize, vsize, stretchreusedSCB_REHVS
REHV ($10)hpos, vpos, hsize, vsizeownSCB_REHV_PAL
REHV + REUSEPALhpos, vpos, hsize, vsizereusedSCB_REHV
RENONE ($00)hpos, vposownSCB_RENONE_PAL
RENONE + REUSEPALhpos, vposreusedSCB_RENONE

Every variant still carries the five-byte head and hpos/vpos; the ladder is only about how much of the size/stretch/tilt tail and the palette travel with this block. Shorter SCBs are smaller in RAM and paint a touch faster, so a natural pattern is: the first sprite of a group uses a _PAL struct with full reload to load the registers, and its followers reuse those values with a shorter, non-_PAL struct. The REUSEPAL half of the name is literally “reuse the previous sprite's palette”: pick the _PAL struct to carry a palette, the plain struct to inherit one. Reusing a palette that was never loaded (for instance on the very first sprite) is undefined — give the first sprite its own.

Two more SPRCTL1 bits round out the byte: the draw-direction bits DRAWUP and DRAWLEFT (which pick the starting quadrant, see section 6) and ALGO3, a legacy packing-algorithm select left at 0 for the standard data format.

Each reload level extends how far into the body the hardware loads from this SCB. present unless REUSEPAL (_PAL structs) hpos vpos hsize vsize stretch tilt penpal RENONE · SCB_RENONE[_PAL] REHV · SCB_REHV[_PAL] REHVS · SCB_REHVS[_PAL] REHVST · SCB_REHVST[_PAL]
The reload ladder: RENONE ⊂ REHV ⊂ REHVS ⊂ REHVST choose how much of the body loads; REUSEPAL independently drops the palette. Each pairing is one _suzy.h struct.

3. Sprite types

The low bits of SPRCTL0 select the sprite type (the TYPE_* constants). The type decides which pen numbers are transparent, which are collideable, and whether the video data is overwritten or combined. Pens 1–13 are always opaque and collideable; the type only changes the treatment of pen 0, pen 14 (shadow) and pen 15.

TypeConstantValueBehaviour
NormalTYPE_NORMAL$04The workhorse. Pen 0 transparent and non-colliding; all other pens opaque and collideable.
BoundaryTYPE_BOUNDARY$03Normal, but pen 15 is transparent yet still collideable — an invisible wall a ball bounces off.
ShadowTYPE_SHADOW$07Normal, but pen 14 is non-collideable yet still opaque — a drawn shadow that does not register hits.
Boundary‑shadowTYPE_BSHADOW$02Both of the above: pen 15 transparent-collideable, pen 14 opaque-non-collideable.
BackgroundTYPE_BACKGROUND$00Overwrites video and collision buffers (pens 0 and 15 no longer transparent); no collision detection. Used to lay down a scene. Paints ~¼ faster.
Background‑non‑collideTYPE_BACKNONCOLL$01Background, but leaves the collision buffer untouched entirely. Paints ~⅓ faster than a background sprite.
Non‑collideableTYPE_NONCOLL$05Never touches the collision buffer; pen 15 is non-collideable. For HUD, clouds, cockpits. Paints ~¼ faster.
Exclusive‑orTYPE_XOR$06Normal collision, but the sprite data is XORed with the video buffer. The read-modify-write can cost up to ~¼ more time.

The hardware realises these from a small set of flags. The truth table below is the authoritative version — and it already incorporates a hardware quirk: the shadow generator is missing an inverter, so TYPE_XOR and the two background types come out with shadow enabled whether they asked for it or not. The practical consequence is that pen 14 is non-collideable on those types, so a background sprite used to clear the collision buffer will not clear pixels drawn in pen 14. See the Collision detection page for how the type and its override bits gate collision.

 ShadowB‑shadowNormalBoundaryBg‑shadowBg‑no‑collNon‑collXOR‑shadow
15 is opaque10101111
14 is collideable00110000
0 is opaque & collideable00110000
collision detect11110001
collision buffer access11111001
XOR the video data00000001

The third SCB byte, sprcoll, carries the sprite's 4-bit collision number and a per-sprite don't-collide bit (NO_COLLIDE, $20). That is the whole of what this page says about collision; the numbering scheme, the depository byte and the COLLOFF register are on the Collision detection page.

4. The pen palette

A sprite's pixels do not store colours directly. Each pixel holds a small pen index — 0 to 2bpp−1 — and the sprite's own pen palette maps that index to a 4-bit pen number (0–15). The pen number then indexes the 16-entry display palette to produce the actual colour (documented on the Graphics page). So there are two lookups: the per-sprite penpal in the SCB, then the global display palette.

The palette is 2bpp nibbles long, packed two nibbles to a byte, which is exactly why _suzy.h ships four penpal structs:

DepthPensPalette sizeStruct
BPP_121 bytePENPAL_1
BPP_242 bytesPENPAL_2
BPP_384 bytesPENPAL_3
BPP_4168 bytesPENPAL_4

The generic SCB structs declare penpal[8] because that covers the 4bpp case; when you hand-build a lower-depth sprite you populate only the first 2bpp−1 bytes and the SCB is that much shorter. Within a byte, pen index 0's pen number is the upper nibble. packtest.c uses an identity penpal {0x01,0x23,…,0xEF} (pen index k maps to pen number k); spritetest.c gives pen index 0 a pen that the display palette leaves transparent.

Because the palette is a separately reloadable block, consecutive sprites can share it via REUSEPAL (section 2.2). One alignment trap: an 8-byte penpal must not begin at address $xxFA — a hardware bug drops its last two bytes, leaving pens 12–15 stale — so align or pad the SCB (gfx_sprite notes this too; the design/LYNX_GFX_DESIGN.md rationale doc analyses it).

One frame-buffer byte = 2 pixels pixel 0 pixel 1 bits 7–4 bits 3–0 paints LEFT paints right bytes ascend left → right across the line Pen index → pen number → colour pen index 0 .. 2^bpp-1 sprite penpal nibble lookup pen number 0 .. 15 display palette → colour index 0 = upper nibble of penpal byte 0
Left, the two-pixels-per-byte packing (high nibble on the left). Right, the two lookups: the sprite's penpal turns a pen index into a pen number, and the display palette turns that into a colour.

5. Pixel and data format

In the frame buffer two pixels share a byte: on an un-flipped, right-drawing sprite the high nibble is the left pixel, and bytes ascend from left to right along the line (the same order the penpal nibbles follow). The source data a sprite points at is separately encoded — a stream of offsets and pixel runs that Suzy unpacks as it paints.

Each scan line of source data is either packed or literal, chosen by the PACKED/LITERAL bit in SPRCTL1. Packed data run-length-encodes repeated pixel groups; literal data stores every pixel verbatim, which is convenient for text and score digits you rewrite often. The two produce identical output — packtest.c confirms it at all four depths. The full encoding, and how the sp65 tool emits it, are on the sp65 page; the sprpck packer produces the same packed data from BMP and ASCII SPS art. This page does not re-derive the encoding.

One authoring rule does belong here, because it is a Suzy trap you hit when building SCBs: the sprite-data shift register drops the final pixel group of any scan line whose data ends on a byte boundary. Literal lines always end that way, so every literal line needs a trailing pad byte (and its offset incremented) whose pixels resolve to pen 0 — usually value $00, but a value the penpal maps to pen 0 if you have repurposed value 0. Packed lines only need the pad in the roughly one-in-eight case where the last meaningful bit lands on bit 0 of a byte. The design/LYNX_SPRITE_PADBYTE_DESIGN.md note and the sprctl1 comment in _suzy.h have the full story; lynxcc's sp65 and sprpck converters both handle the padding for data they generate.

6. Sizing, stretch, tilt and flip

hsize and vsize are 8.8 fixed-point scale factors: $0100 is 1×, $0080 is half size, $0200 double. Horizontal and vertical scale are independent, and Suzy resizes the image as it paints — there is no pre-scaled copy in RAM.

Two 16-bit adders modify the sprite per scan line and are only applied when the reload bits carry them (REHVS for stretch, REHVST for stretch and tilt):

hsize — horizontal scale hsize $0200 = 2× wide dashed = 1× base sprite vsize — vertical scale vsize $0200 = 2× tall stretch — hsize adder / line wider each line widens down its height → trapezoid tilt — hpos adder / line shifts right each line shears sideways → parallelogram
Each panel overlays the 1× base sprite (dashed) with the result of one transform. hsize/vsize are whole-sprite 8.8 scale factors; stretch and tilt are per-scan-line adders that turn the rectangle into a trapezoid or parallelogram.
VSTRETCH off — stretch alone width grows, height fixed hsize adder only → flat-topped trapezoid VSTRETCH on — stretch + vsize same adder feeds vsize too → uniform zoom
The stretch adder alone widens the sprite each scan line (left). Setting VSTRETCH in SPRSYS applies the same adder to vsize as well, so the sprite grows in both directions down its length (right).

A sprite is painted outward from a fixed reference point — chosen when the image is compacted — into one of four quadrants. The starting quadrant is set at run time by DRAWUP and DRAWLEFT in SPRCTL1; with neither bit set the sprite paints down and to the right of the reference point. HFLIP and VFLIP in SPRCTL0 mirror the image left/right and up/down about that same point. Changing the starting quadrant shifts the sprite's position slightly, which is what the hsizeoff/vsizeoff registers exist to balance: at initialisation they are loaded with the “magic” $007F on the right and down directions (and 0 on left and up) to cancel the one-pixel bump at the reference point of a scaled multi-quadrant sprite. They are shared by all sprites, so restore them if you change them.

reference point (hpos, vpos) default paint (no DRAWUP/DRAWLEFT) HFLIP mirrors L↔R across this axis VFLIP mirrors U↔D across this axis DRAWUP / DRAWLEFT choose which quadrant painting starts in.
The reference point anchors the sprite; the four quadrants around it are selected by DRAWUP/DRAWLEFT, and HFLIP/VFLIP mirror the image about the two axes.

6.1. Where the action point sits

Because the reference point is a fixed screen pixel, it is the pivot every one of these transforms turns about: hsize, vsize, stretch and tilt all grow or shear the image outward from that pixel along the current draw direction. So the same transform paints the result in a different place depending on where the action point sits inside the artwork — the ax,ay action point you pick when the sprite is compacted. Move it, and a doubled or sheared sprite lands somewhere else even though the SCB scale fields are byte-for-byte identical.

One caveat decides what “middle” even means: a plain single-quadrant sprite can only anchor at a corner — which corner is chosen by DRAWUP/DRAWLEFT. To pivot about the centre of the image you must draw it as a multi-quadrant sprite whose four quadrants share one reference point; that is exactly the case the hsizeoff/vsizeoff $007F correction above is there to serve. The figure below applies one 2× scale and one tilt shear at three action points to show the pivot staying put while the picture moves around it.

Same transform, three action points — the pinned pivot holds while the picture moves. action point: top-left corner centre (multi-quadrant) bottom-right corner 2× scale tilt shear grows / shears down-right grows / shears symmetrically grows / shears up-left ● = pinned action point (hpos, vpos)
One 2× scale (top) and one tilt shear (bottom), each applied unchanged at three action points. The dashed 1× base and the pinned dot stay fixed in every panel; only where the action point sits in the artwork changes, and the transformed result grows or shears outward from that pixel — down-right from a top-left corner, symmetrically from a centred multi-quadrant anchor, up-left from a bottom-right corner.

The spritefx.c sample turns this figure into something you can drive: it animates hsize, vsize, stretch and tilt on a choice of shapes and lets you cycle the action point through all five spots — the four corners and the centre — with a magenta cross pinned at the anchor so you can watch each shape grow or shear around it exactly as drawn here.

6.2. Flipping about the reference point

Two bits in SPRCTL0 mirror a sprite as Suzy paints it, so one image in RAM can face either way without a second copy:

Both flips are about the reference point, not the sprite's centre or a screen axis, so the sprite stays anchored where you positioned it and only its contents mirror — the reference pixel never moves. This is the same point DRAWUP/DRAWLEFT pick a starting quadrant around (above); flipping and quadrant choice are independent, and both leave hpos /vpos untouched. Setting HFLIP and VFLIP together mirrors on both axes at once, which is a 180° rotation of the image. Flip does not interact with hsize/vsize or the stretch/tilt adders — a flipped sprite scales and shears exactly like an un-flipped one.

F normal no flip bits F HFLIP mirror L↔R F VFLIP mirror U↓D F HFLIP | VFLIP 180° rotation
The same F glyph flipped about its reference point (the teal dot). HFLIP mirrors left↔right, VFLIP up↓down, and the two together give a 180° rotation. The reference point never moves, so hpos/vpos stay put — only the image mirrors.

7. Driving the engine

Normally you never touch these registers — gfx_init does the one-time setup and gfx_sprite runs each sprite list — but the struct __suzy register map in _suzy.h shows what they do, and a custom sprite loop needs them. Once, after power-up, the engine is initialised: the control byte sprsys, the init byte sprinit, the screen-edge offsets hoff/voff (which pan the display window — see the Graphics page), the collision buffer base colbase and its offset colloff (both covered on the Collision detection page), the size offsets hsizeoff/vsizeoff, and finally suzybusen = 1 to grant Suzy the RAM bus.

To paint a frame you set the video build address (vidadr/sprbase), write the address of the first SCB into scbnext, and start the engine by writing SPRITE_GO ($01, or EVER_ON | SPRITE_GO = $05) to sprgo. Suzy then walks the chain until a next of 0. The read side of sprsys reports progress: SPRITEWORKING is set while the engine runs, SPRITETOSTOP while a requested stop is pending, and UNSAFE_ACCESS flags a register access collision; VSTRETCHING and LEFTHANDED read back the current state of the two mode bits below. The write side offers SPRITESTOP (finish the current sprite and halt), CLR_UNSAFE, the global NO_COLLIDE disable, VSTRETCH (the vertical-stretch mode of §6), and LEFTHAND. The engine only ever restarts from the beginning of the list — it cannot resume from where it stopped.

LEFTHAND (0x08) selects the console's drawing handedness. It reverses the direction Suzy fills the video buffer so the picture comes out correct when the unit is held in its flipped orientation, and it pairs with the matching flip of the joypad axes (the JOYSTICK up/down and left/right bits swap sense with this bit). It is a global console-orientation setting written once at init, not a per-sprite control; leave it as gfx_init sets it unless you are implementing screen flip. The read-back LEFTHANDED reports the bit's current value.

The SIGNMATH and ACCUMULATE bits in the same register belong to Suzy's hardware multiply/divide, documented under suzymath.h in the function reference, not to sprites; their read-side counterparts MATHWORKING, MATHWARNING, and MATHCARRY report math status, again not sprite state.

8. License

The Lynx runtime, like the rest of the package, is distributed under the cc65 zlib-style license. The full text, together with every other copyright notice in the distribution, is reproduced verbatim on the consolidated Licenses and copyright notices page.