Graphics

The Lynx graphics system as the lynxcc SDK exposes it — one fixed 160 × 102 16-colour display, the complete gfx_* API, the two frame buffers and how double buffering flips them at vertical blank, the selectable refresh rates and the compute budget they leave, and the palette format.

1. The display mode

The Lynx has exactly one display mode, and lynxcc treats it as a compile-time fact rather than something you query or configure. The screen is 160 × 102 pixels, 16 colours, with 2 display pages — the constants GFX_XRES, GFX_YRES, GFX_COLORCOUNT and GFX_PAGECOUNT in include/lynx/gfx.h. There is no mode list, no resolution negotiation and no failure path: the hardware is fixed, so the mode cannot fail to initialise and (except for the display depth, section 5) cannot be changed.

The old TGI-style query functions survive only as zero-cost macros over those constants — gfx_getxres() expands to GFX_XRES, gfx_getmaxx() to GFX_XRES - 1, and so on (section 2). Prefer the constants; the macros exist so that code ported from stock cc65 keeps compiling.

There are no geometric primitives and no pixel-plot call. All drawing is done by Suzy's sprite engine: gfx_sprite renders a sprite control block, and text is drawn as sprites too. If you want a shape — a line, a box, a filled region — you build it as a sprite. See the sprite pad-byte rule and the deeper sprite material in section 8.

gfx_init() is the one required call. It enables the vertical-blank timer interrupt, sets up the collision buffer, selects 4 bits per pixel, points both the view and draw pages at page 0, loads the default palette, and sets the drawing pen to black (pen 0). So the canonical opening pair,

#include <lynx/gfx.h>

gfx_init();
gfx_clear();          /* clears in the CURRENT pen -- black -- so: black screen */

gives you a black screen. That black default is the one gotcha worth flagging up front: the old loadable driver defaulted to white, and gfx_clear / gfx_outtext now draw in the current pen. Set a colour with gfx_setcolor() before you draw, or you will draw black on black and see nothing.

2. API overview

Every entry point in the graphics library, grouped the way gfx.h groups them. Each name links to its full signature and notes in the function reference — this table is a map, not a second copy of the prototypes, so signatures live in exactly one place and cannot drift.

GroupFunctionPurpose
Init / cleargfx_initInitialise graphics; the one required call.
gfx_clearClear the draw page in the current pen.
gfx_clearrowsClear a band of rows in the current pen.
Sprites / displaygfx_spriteDraw an SCB (or SCB chain) into the draw page.
gfx_flipRotate the display 180° (left-handed mode).
gfx_setviewpageChoose the visible page (0/1).
gfx_setdrawpageChoose the page drawing renders into (0/1).
gfx_busyNonzero while a requested swap is still pending.
gfx_updatedisplayRequest a draw/view swap at the next VBL.
gfx_setframerateSelect 50, 60 or 75 Hz refresh.
gfx_setcollisiondetectionEnable/disable Suzy collision detection.
gfx_setbppSelect 4bpp (default) or 2bpp display depth.
Colour / palettegfx_setcolorSet the drawing pen (0–15).
gfx_getcolorRead the current drawing pen.
gfx_setbgcolorSet the text background pen (0 = transparent).
gfx_setpaletteLoad a 32-byte palette.
gfx_setpalette16Load a condensed 16-entry palette.
gfx_getpaletteRead the current palette.
gfx_getdefpaletteRead the default palette.
Textgfx_gotoxyMove the text cursor.
gfx_outtextDraw text at the cursor (≤ 20 chars/call).
gfx_outtextxyDraw text at a given position.
gfx_settextscaleSet 8.8 fixed-point text scaling.
gfx_settextstyleSet scale and direction together.
gfx_setfontSelect the 8x8, compact 5x5 or proportional font.
gfx_settextdirSet cursor-advance direction.
gfx_gettextwidthWidth of a string at the current scale.
gfx_gettextheightHeight of text at the current scale.
Constants / query macrosGFX_XRES, GFX_YRES160 and 102 — the display dimensions.
GFX_COLORCOUNT, GFX_PAGECOUNT16 colours, 2 pages.
GFX_FONT_*, GFX_TEXT_*Font and text-direction selectors.
gfx_getxres() …Zero-cost macros over the constants above.

3. Frame buffers and double buffering

The Lynx has no dedicated video RAM: the display is refreshed straight out of main DRAM by Mikey's video DMA. Graphics reserves two full-screen buffers — “pages” — and draws into one while the hardware scans the other out. This section states where they live and how the flip is timed.

3.1. The two pages in memory

Each page is the whole 160 × 102 screen at 4 bits per pixel: 80 bytes/line × 102 lines = 8160 bytes. There are two of them:

These addresses are not free-floating constants: they are coupled to the linker memory map in cfg/lynx*.cfg, and asminc/gfx.inc keeps them in step. If the config moves the buffers, these change with it. For the whole address-space picture — and the fact that page 0 shares its range with the I/O overlay — see the Memory page; this page owns only the two buffers.

The two display pages in DRAM Page 1 — 8160 bytes Page 0 — 8160 bytes $C038 $E017 $E018 $FFF7 upper 4080 B free in 2bpp upper 4080 B free in 2bpp lower addresses at top; page 0 shares its range with the I/O overlay
Two 8160-byte 4bpp buffers. In 2bpp mode (section 5) Mikey scans only the lower 4080 bytes of each page; the base addresses do not move.

3.2. Draw page and view page

Double buffering is just two independent pointers into those buffers:

Draw page (gfx_setdrawpage)

The page that gfx_sprite, gfx_clear, gfx_outtext and the rest render into.

View page (gfx_setviewpage)

The page Mikey scans out to the LCD (latched into its DISPADR register).

Point them at the same page and you have a single-buffered display: every draw is immediately visible. It is the simplest model, but because you are drawing into the buffer being scanned out, partial updates can tear or flicker mid-frame.

Point them at different pages and you have a double-buffered display: draw the next frame into the hidden page while the other is shown, then swap. The swap must land during vertical blank, when nothing is being scanned out, or the display tears — which is exactly what gfx_updatedisplay arranges (section 3.3).

Draw / view pointers exchange on swap before swap Page 0 Page 1 VIEW → DRAW → after swap Page 0 Page 1 DRAW → VIEW → VBL swap
The just-drawn page becomes the view page and the draw pointer moves to the other buffer, so the next frame is drawn off-screen again.

3.3. The deferred swap at VBL

gfx_updatedisplay() does not perform the swap and does not block. It sets a one-byte swap-request flag and returns immediately. The actual swap is done by the VBL interrupt handler (gfx_vbl_irq): on the next vertical-blank interrupt it makes the just-drawn page the view page, flips the draw page to the other buffer, and clears the request. It is fire-and-forget.

This corrects a long-standing description that said gfx_updatedisplay “will wait for the next VBL”. It does not wait; it defers to the next VBL and returns at once. If you must know the swap has actually happened before you start reusing the newly-hidden buffer, poll gfx_busy() — it returns nonzero while a requested swap is still pending and drops to zero once the interrupt has performed it.

Deferred swap: request now, swap at VBL t gfx_updatedisplay() set flag, return frame continues (draw / logic) VBL interrupt swap pages, clear flag
Between the request and the VBL, gfx_busy() reports the swap as pending. The handler never blocks the caller.

The VBL swap handler is linked as an .interruptor that only enters the interrupt chain when a program actually links the paging module — that is, when it uses double buffering. A single-buffered program that only ever draws to page 0 pays nothing for the swap machinery.

4. Refresh rate and frame timing

gfx_setframerate(50|60|75) selects the display refresh rate in Hz. It returns 0 on success and nonzero for any other value — it is the only fallible call left in the library. Internally it writes just the two timer backup registers HTIMBKUP and PBKUP (the safe subset of Mikey timer handling), with these values:

RateHTIMBKUPPBKUPFrame period
50 Hz$BD$3120.0 ms
60 Hz$9E$2916.67 ms
75 Hz$7E$2013.33 ms

The refresh rate sets your compute budget between frames. At the Lynx's roughly 4 MHz 65SC02 the gross CPU budget per frame is about the system clock divided by the rate:

RateFrame periodGross CPU cycles/frame (~4 MHz)
50 Hz20.0 ms≈ 80,000
60 Hz16.67 ms≈ 66,700
75 Hz13.33 ms≈ 53,300

Treat those as a ceiling, not the real budget. Two things eat into it and neither is counted above: Mikey's display DMA steals cycles on every scanline to refresh the screen, and every synchronous gfx_sprite call busy-waits while Suzy renders — from the program's point of view the drawing engine and the CPU do not run in parallel. So a higher refresh rate buys smoother motion at the cost of fewer cycles for game logic and fewer sprites you can draw per frame. The exact steal-cycle count is hardware-measurement territory and is deliberately left unquantified here (as in design/LYNX_GFX_DESIGN.md).

Frame budget shrinks as the rate rises 50 Hz 60 Hz 75 Hz ≈ 80,000 cycles ≈ 66,700 cycles ≈ 53,300 cycles VBL orange = gross compute ceiling before display-DMA / Suzy steal
Each bar is one frame period. The teal slice stands for the VBL/swap window; the orange slice is the gross compute ceiling — real available cycles are lower once DMA and Suzy take their share.

In practice: 60 Hz is the usual choice; 75 Hz gives the smoothest motion when each frame's work is light; 50 Hz buys the most compute per frame when logic or sprite counts are heavy.

5. Display depth

gfx_setbpp(4|2) selects how Mikey's display DMA reads the buffer, via DISPCTL bit 2. 4 bits per pixel is the default and the only spec-blessed mode — the 16-colour display this whole page describes.

2bpp is an advanced, experimental mode. It is a CPU-rendered framebuffer mode: Suzy's sprite engine always emits 4bpp, so sprite and text output (gfx_sprite, gfx_outtext) scans out garbled under 2bpp, and even gfx_clear only stays valid for pens 0, 5, 10 and 15 (which map onto the four 2bpp pens). It scans 40 bytes/line instead of 80, so each page needs only 4080 bytes and the upper 4080 bytes of each buffer (section 3.1) fall free — but the base addresses do not move. The mode relies on a DISPCTL bit outside the hardware spec's guarantees and is unverified on real hardware. Reach for it only if you are hand-rendering a 2bpp framebuffer and know what you are doing; the full analysis is in design/LYNX_GFX_DESIGN.md §2.7.

6. Colour and the palette

lynx/lynx.h defines 16 palette-index constants naming the entries of the built-in default palette. gfx_init clears the hardware palette to black; call gfx_setdefpalette after it to load this default set, or gfx_setpalette to install your own. A matching set of GFX_COLOR_* aliases (for example GFX_COLOR_WHITE) is provided for use with the graphics calls; each GFX_COLOR_x is simply equal to the corresponding COLOR_x.

COLOR_BLACK      0x00   COLOR_YELLOW      0x08
COLOR_RED        0x01   COLOR_LIGHTGREEN  0x09
COLOR_PINK       0x02   COLOR_GREEN       0x0A
COLOR_LIGHTGREY  0x03   COLOR_DARKBROWN   0x0B
COLOR_GREY       0x04   COLOR_VIOLET      0x0C
COLOR_DARKGREY   0x05   COLOR_BLUE        0x0D
COLOR_BROWN      0x06   COLOR_LIGHTBLUE   0x0E
COLOR_PEACH      0x07   COLOR_WHITE       0x0F
Default palette — 16 pens 0 BLACK 1 RED 2 PINK 3 LTGREY 4 GREY 5 DKGREY 6 BROWN 7 PEACH 8 YELLOW 9 LTGREEN 10 GREEN 11 DKBROWN 12 VIOLET 13 BLUE 14 LTBLUE 15 WHITE swatch colours are approximate 12-bit Lynx values scaled to sRGB
The 16 default pens with their COLOR_* names and indices, loaded by gfx_setdefpalette. On-screen colour depends on the actual LCD; these swatches are an approximation.

The palette wire format — as consumed by gfx_setpalette and returned by gfx_getpalette / gfx_getdefpalette — is 32 bytes: 16 green bytes (one green nibble per pen) followed by 16 blue/red bytes (blue in the high nibble, red in the low nibble). Each pen is thus a 12-bit colour, split across the two halves in Mikey's GCOLMAP layout.

32-byte palette layout 16 green bytes 16 blue/red bytes green nibble per pen blue = high nibble, red = low nibble byte 0 byte 16 byte 31
Greens first, then blue/red pairs — the order Mikey's GCOLMAP hardware expects.

If you would rather keep one packed entry per pen than the split-halves wire format, gfx_setpalette16 takes the condensed 16-entry form instead: an unsigned short palette[16] where each pen is a single 12-bit value of the form 0x0GBR (green nibble in bits 8–11, blue in 4–7, red in 0–3). It splits each entry into the two GCOLMAP halves as it loads, so it installs exactly the same colours as gfx_setpalette — the palette generator emits both arrays from the same set of colours, so you pick whichever matches how your source stores the palette.

Two more pen controls: gfx_setcolor / gfx_getcolor set and read the drawing pen (0–15, and the source of gfx_clear's fill colour), and gfx_setbgcolor sets the text background pen — where a value of 0 means a transparent background rather than the black pen.

Why the screen is black at boot, not garbage. The video DMA is already running before your program draws anything — crt0 enables it (DISPCTL = $0D) as part of one-time init — and the frame buffer at that point still holds cold-boot DRAM garbage plus the cartridge bootloader's secondary loader, which the Lynx leaves sitting at $FB68, inside display page 0 ($E018–$FFB7). None of it is visible, because every one of the 16 pens is mapped to black: the encrypted bootloader's very first act is to zero GCOLMAP, and neither crt0 nor gfx_init loads any colour — gfx_init clears the palette to black again and deliberately leaves it that way. So the DMA scans the buffer out as a uniformly black screen until you load a palette. The loader bytes at $FB68 are overwritten the first time you draw into page 0, so they never need clearing. The practical rule: load your palette after you have cleared or drawn the first frame — installing a non-black palette while the buffer still holds boot garbage would flash that garbage on screen for a frame.

7. Panning the display window: hoff and voff

Every hpos/vpos a sprite carries is a coordinate in an imaginary display world, not a pixel address in the frame buffer. The 160×102 display window — the RAM the video hardware actually scans out — sits somewhere inside that world, and its top-left corner is fixed by the two screen-edge offset registers hoff ($FC04) and voff ($FC06) in struct __suzy. As Suzy paints a sprite it subtracts the offsets from the sprite's reference point, so a sprite at world position (hpos, vpos) lands at buffer pixel (hpos − hoff, vpos − voff). Anything that falls outside the window is clipped away.

gfx_init loads both offsets with 0, which makes hpos/vpos plain on-screen coordinates — the common case, and why you can usually ignore these registers. Their real use is scrolling: because the offsets are subtracted from every sprite as it is drawn, changing hoff and/or voff pans the whole scene at once. You can lay out an entire world full of SCBs and move the camera over it by writing two registers, without touching a single sprite's hpos/vpos. The offsets are 16-bit (nine significant bits) and global to all sprites; a new value takes effect the next time you render, so set them before writing SPRITE_GO.

display world (sprite coordinate space) world (0,0) (hpos, vpos) display window 160×102, scanned out hoff voff larger hoff/voff → window pans
Sprites live at fixed coordinates in the display world; hoff/voff place the 160×102 display window within it. A sprite at (hpos, vpos) is drawn at buffer pixel (hpos−hoff, vpos−voff). Raising the offsets slides the window across the world, scrolling the whole scene without changing any SCB.

8. Sprites, collision and going deeper

All non-text drawing goes through gfx_sprite, which renders one sprite control block (SCB) or a chain of them synchronously — it returns only after Suzy has finished. Two hardware traps are worth remembering: an 8-byte SCB pen palette must not begin at address $xxFA (a hardware bug drops its last two bytes, leaving pens C–F stale), so align or pad the SCB; and you must not interleave raw game-cart strobes with sprite calls, because Suzy must be left alone for 12 ticks after a cart write. The sprite pad-byte rule and the $xxFA palette bug are analysed in full in design/LYNX_GFX_DESIGN.md §5, with the packtest example demonstrating packed-versus-literal output. The Sprites page documents the SCB structure and its linking, the eight SCB struct variants, the sprite types and the pen palette in full.

gfx_setcollisiondetection turns Suzy's sprite collision detection on or off (it is off by default); when it is on, gfx_clear and gfx_clearrows also erase the matching rows of the collision buffer. Collision detection needs somewhere to put that buffer, so a program that uses it must link with lynx-coll.cfg to reserve the space. The Collision detection page covers the buffer, the per-sprite depository and a worked example in full.

For material this page only points at:

9. 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.