Graphics
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.
| Group | Function | Purpose |
|---|---|---|
| Init / clear | gfx_init | Initialise graphics; the one required call. |
gfx_clear | Clear the draw page in the current pen. | |
gfx_clearrows | Clear a band of rows in the current pen. | |
| Sprites / display | gfx_sprite | Draw an SCB (or SCB chain) into the draw page. |
gfx_flip | Rotate the display 180° (left-handed mode). | |
gfx_setviewpage | Choose the visible page (0/1). | |
gfx_setdrawpage | Choose the page drawing renders into (0/1). | |
gfx_busy | Nonzero while a requested swap is still pending. | |
gfx_updatedisplay | Request a draw/view swap at the next VBL. | |
gfx_setframerate | Select 50, 60 or 75 Hz refresh. | |
gfx_setcollisiondetection | Enable/disable Suzy collision detection. | |
gfx_setbpp | Select 4bpp (default) or 2bpp display depth. | |
| Colour / palette | gfx_setcolor | Set the drawing pen (0–15). |
gfx_getcolor | Read the current drawing pen. | |
gfx_setbgcolor | Set the text background pen (0 = transparent). | |
gfx_setpalette | Load a 32-byte palette. | |
gfx_setpalette16 | Load a condensed 16-entry palette. | |
gfx_getpalette | Read the current palette. | |
gfx_getdefpalette | Read the default palette. | |
| Text | gfx_gotoxy | Move the text cursor. |
gfx_outtext | Draw text at the cursor (≤ 20 chars/call). | |
gfx_outtextxy | Draw text at a given position. | |
gfx_settextscale | Set 8.8 fixed-point text scaling. | |
gfx_settextstyle | Set scale and direction together. | |
gfx_setfont | Select the 8x8, compact 5x5 or proportional font. | |
gfx_settextdir | Set cursor-advance direction. | |
gfx_gettextwidth | Width of a string at the current scale. | |
gfx_gettextheight | Height of text at the current scale. | |
| Constants / query macros | GFX_XRES, GFX_YRES | 160 and 102 — the display dimensions. |
GFX_COLORCOUNT, GFX_PAGECOUNT | 16 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:
Page 0 at base
$E018(GFX_PAGE0_ADDR), running to$FFF7.Page 1 at base
$C038(GFX_PAGE1_ADDR), running to$E017.
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.
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_outtextand the rest render into.- View page (
gfx_setviewpage) The page Mikey scans out to the LCD (latched into its
DISPADRregister).
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).
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.
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:
| Rate | HTIMBKUP | PBKUP | Frame period |
|---|---|---|---|
| 50 Hz | $BD | $31 | 20.0 ms |
| 60 Hz | $9E | $29 | 16.67 ms |
| 75 Hz | $7E | $20 | 13.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:
| Rate | Frame period | Gross CPU cycles/frame (~4 MHz) |
|---|---|---|
| 50 Hz | 20.0 ms | ≈ 80,000 |
| 60 Hz | 16.67 ms | ≈ 66,700 |
| 75 Hz | 13.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).
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
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.
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.
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:
Fonts — the three bitmap fonts and their glyph tables are on the Graphics fonts page.
Suzy math — the hardware multiply/divide the sprite engine shares is documented under
suzymath.hin the function reference.Design rationale — why TGI was removed and how this static library is laid out is in
design/LYNX_GFX_DESIGN.md.
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.