A partly off-screen object should expose only the part that lies inside the viewport.

Use it in a project
Both rectangles use unsigned byte coordinates and half-open edges [left,right)×[top,bottom). Require ordered edges; equal edges are valid empty rectangles. Return the intersection and VISIBLE=1 if it has area. Empty results are all zero. Invalid reversed edges return VALID=0.
Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: OUT_LEFT, OUT_TOP, OUT_RIGHT, OUT_BOTTOM, VISIBLE, VALID, LEFT, TOP, RIGHT, BOTTOM, VIEW_LEFT, VIEW_TOP, VIEW_RIGHT, VIEW_BOTTOM. They occupy 14 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3 (4 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.
Call rectangle_clipping_init once at the intended reset boundary. It clears inputs as well as outputs and workspace. Set your starting state and configuration afterwards, then call rectangle_clipping_step whenever the described event occurs. The helper reads no device, waits for no frame and installs no interrupt handler.
| Ownership | Fields |
|---|---|
| Retain between calls | none |
| Supply before service | both sets of rectangle edges |
| Read after service | intersection edges, VISIBLE and VALID |
The intersection starts at the greater left/top edges and ends at the lesser right/bottom edges. Strictly positive width and height distinguish a visible area from touching edges.
Reusable initialisation and service
; SPDX-License-Identifier: MIT
; Clip a Rectangle to a Viewport. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
rectangle_clipping_init:
ld a,0
ld (OUT_LEFT),a
ld (OUT_TOP),a
ld (OUT_RIGHT),a
ld (OUT_BOTTOM),a
ld (VISIBLE),a
ld (VALID),a
ld (LEFT),a
ld (TOP),a
ld (RIGHT),a
ld (BOTTOM),a
ld (VIEW_LEFT),a
ld (VIEW_TOP),a
ld (VIEW_RIGHT),a
ld (VIEW_BOTTOM),a
ld (T0),a
ld (T1),a
ld (T2),a
ld (T3),a
ret
rectangle_clipping_step:
; OUT_LEFT = 0
ld a,0
ld (OUT_LEFT),a
; OUT_TOP = 0
ld a,0
ld (OUT_TOP),a
; OUT_RIGHT = 0
ld a,0
ld (OUT_RIGHT),a
; OUT_BOTTOM = 0
ld a,0
ld (OUT_BOTTOM),a
; VISIBLE = 0
ld a,0
ld (VISIBLE),a
; VALID = 0
ld a,0
ld (VALID),a
; if LEFT > RIGHT:
ld a,(LEFT)
ld b,a
ld a,(RIGHT)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L2
jp nc,rectangle_clipping_L1
jp rectangle_clipping_L2
rectangle_clipping_L1:
; return
ret
jp rectangle_clipping_L3
rectangle_clipping_L2:
rectangle_clipping_L3:
; if TOP > BOTTOM:
ld a,(TOP)
ld b,a
ld a,(BOTTOM)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L5
jp nc,rectangle_clipping_L4
jp rectangle_clipping_L5
rectangle_clipping_L4:
; return
ret
jp rectangle_clipping_L6
rectangle_clipping_L5:
rectangle_clipping_L6:
; if VIEW_LEFT > VIEW_RIGHT:
ld a,(VIEW_LEFT)
ld b,a
ld a,(VIEW_RIGHT)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L8
jp nc,rectangle_clipping_L7
jp rectangle_clipping_L8
rectangle_clipping_L7:
; return
ret
jp rectangle_clipping_L9
rectangle_clipping_L8:
rectangle_clipping_L9:
; if VIEW_TOP > VIEW_BOTTOM:
ld a,(VIEW_TOP)
ld b,a
ld a,(VIEW_BOTTOM)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L11
jp nc,rectangle_clipping_L10
jp rectangle_clipping_L11
rectangle_clipping_L10:
; return
ret
jp rectangle_clipping_L12
rectangle_clipping_L11:
rectangle_clipping_L12:
; VALID = 1
ld a,1
ld (VALID),a
; T0 = LEFT
ld a,(LEFT)
ld (T0),a
; if VIEW_LEFT > T0:
ld a,(VIEW_LEFT)
ld b,a
ld a,(T0)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L14
jp nc,rectangle_clipping_L13
jp rectangle_clipping_L14
rectangle_clipping_L13:
; T0 = VIEW_LEFT
ld a,(VIEW_LEFT)
ld (T0),a
jp rectangle_clipping_L15
rectangle_clipping_L14:
rectangle_clipping_L15:
; T1 = TOP
ld a,(TOP)
ld (T1),a
; if VIEW_TOP > T1:
ld a,(VIEW_TOP)
ld b,a
ld a,(T1)
ld c,a
ld a,b
cp c
jp z,rectangle_clipping_L17
jp nc,rectangle_clipping_L16
jp rectangle_clipping_L17
rectangle_clipping_L16:
; T1 = VIEW_TOP
ld a,(VIEW_TOP)
ld (T1),a
jp rectangle_clipping_L18
rectangle_clipping_L17:
rectangle_clipping_L18:
; T2 = RIGHT
ld a,(RIGHT)
ld (T2),a
; if VIEW_RIGHT < T2:
ld a,(VIEW_RIGHT)
ld b,a
ld a,(T2)
ld c,a
ld a,b
cp c
jp c,rectangle_clipping_L19
jp rectangle_clipping_L20
rectangle_clipping_L19:
; T2 = VIEW_RIGHT
ld a,(VIEW_RIGHT)
ld (T2),a
jp rectangle_clipping_L21
rectangle_clipping_L20:
rectangle_clipping_L21:
; T3 = BOTTOM
ld a,(BOTTOM)
ld (T3),a
; if VIEW_BOTTOM < T3:
ld a,(VIEW_BOTTOM)
ld b,a
ld a,(T3)
ld c,a
ld a,b
cp c
jp c,rectangle_clipping_L22
jp rectangle_clipping_L23
rectangle_clipping_L22:
; T3 = VIEW_BOTTOM
ld a,(VIEW_BOTTOM)
ld (T3),a
jp rectangle_clipping_L24
rectangle_clipping_L23:
rectangle_clipping_L24:
; if T0 >= T2:
ld a,(T0)
ld b,a
ld a,(T2)
ld c,a
ld a,b
cp c
jp nc,rectangle_clipping_L25
jp rectangle_clipping_L26
rectangle_clipping_L25:
; return
ret
jp rectangle_clipping_L27
rectangle_clipping_L26:
rectangle_clipping_L27:
; if T1 >= T3:
ld a,(T1)
ld b,a
ld a,(T3)
ld c,a
ld a,b
cp c
jp nc,rectangle_clipping_L28
jp rectangle_clipping_L29
rectangle_clipping_L28:
; return
ret
jp rectangle_clipping_L30
rectangle_clipping_L29:
rectangle_clipping_L30:
; OUT_LEFT = T0
ld a,(T0)
ld (OUT_LEFT),a
; OUT_TOP = T1
ld a,(T1)
ld (OUT_TOP),a
; OUT_RIGHT = T2
ld a,(T2)
ld (OUT_RIGHT),a
; OUT_BOTTOM = T3
ld a,(T3)
ld (OUT_BOTTOM),a
; VISIBLE = 1
ld a,1
ld (VISIBLE),a
ret
component_end:Machine contract
AF/BC/DE/HL may change; IX/IY are preserved. A direct CALL needs two return-address stack bytes. The helper makes no nested calls and adds no other stack use. Its named scratch is private workspace, not a pointer, a ROM variable or shared zero-page space that the game may borrow concurrently. The samples use fixed symbol bindings. Scope or rename every label and binding, including the component_end measurement marker, and allocate a separate block for each independent instance. Different helpers can also use common field names, so combining them needs separate namespaces or explicitly renamed bindings. Give each block one mutation owner.
The 48K diagnostic boots through ROM tape loading and USR, disables interrupts and owns the screen. The helper performs no port or screen writes.
Adapt the caller
demo.asm supplies the bindings and include order. exercise.inc first runs table cases, then a separate client retaining state between calls. A game can call the public step entry directly and remove the diagnostic packet and display. Do not copy the diagnostic’s hardware takeover into an existing game loop.
Build with make ASM198X=/path/to/asm198x. The Makefile produces normal, custom, deliberately wrong and repaired programs. Tools must already be installed; the sample downloads nothing.
Bindings and include order
org $8000
CUSTOM equ 0
BROKEN equ 0
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
OUT_LEFT: defb 0
OUT_TOP: defb 0
OUT_RIGHT: defb 0
OUT_BOTTOM: defb 0
VISIBLE: defb 0
VALID: defb 0
LEFT: defb 0
TOP: defb 0
RIGHT: defb 0
BOTTOM: defb 0
VIEW_LEFT: defb 0
VIEW_TOP: defb 0
VIEW_RIGHT: defb 0
VIEW_BOTTOM: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startA client retaining state between calls
preview_rules:
if CUSTOM
; LEFT = 0
ld a,0
ld (LEFT),a
; TOP = 0
ld a,0
ld (TOP),a
; RIGHT = 255
ld a,255
ld (RIGHT),a
; BOTTOM = 255
ld a,255
ld (BOTTOM),a
; VIEW_LEFT = 10
ld a,10
ld (VIEW_LEFT),a
; VIEW_TOP = 20
ld a,20
ld (VIEW_TOP),a
; VIEW_RIGHT = 30
ld a,30
ld (VIEW_RIGHT),a
; VIEW_BOTTOM = 40
ld a,40
ld (VIEW_BOTTOM),a
if BROKEN
; VIEW_RIGHT = 255
ld a,255
ld (VIEW_RIGHT),a
endif
call checked_step
call emit_state
ret
else
; LEFT = 2
ld a,2
ld (LEFT),a
; TOP = 3
ld a,3
ld (TOP),a
; RIGHT = 12
ld a,12
ld (RIGHT),a
; BOTTOM = 15
ld a,15
ld (BOTTOM),a
; VIEW_LEFT = 5
ld a,5
ld (VIEW_LEFT),a
; VIEW_TOP = 0
ld a,0
ld (VIEW_TOP),a
; VIEW_RIGHT = 10
ld a,10
ld (VIEW_RIGHT),a
; VIEW_BOTTOM = 10
ld a,10
ld (VIEW_BOTTOM),a
if BROKEN
; VIEW_RIGHT = 255
ld a,255
ld (VIEW_RIGHT),a
endif
call checked_step
call emit_state
endif
retCustomise it
Clip a maximum-size rectangle into a small inset viewport. The maintained custom caller changes its inputs or client sequence rather than hiding a different implementation behind the same label. Compare every state of the two clients, not only their final display.
Custom client selection
org $8000
CUSTOM equ 1
BROKEN equ 0
COYOTE_STEPS equ 2
BUFFER_STEPS equ 2
TEXT_CAP equ 4
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 49
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
OUT_LEFT: defb 0
OUT_TOP: defb 0
OUT_RIGHT: defb 0
OUT_BOTTOM: defb 0
VISIBLE: defb 0
VALID: defb 0
LEFT: defb 0
TOP: defb 0
RIGHT: defb 0
BOTTOM: defb 0
VIEW_LEFT: defb 0
VIEW_TOP: defb 0
VIEW_RIGHT: defb 0
VIEW_BOTTOM: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end startThe custom diagnostic row is 0A 14 1E 28 01 01 00 00. The full client trace in the source explains how it gets there.
Mistake and repair
The wrong caller replaces the viewport’s right edge with the coordinate ceiling. The clipped object leaks past the intended panel. Supply the actual viewport bounds.
The BROKEN branch lives in the sequential client, not the reusable helper. wrong.asm selects it; repaired.asm restores the normal call sequence. The repaired executable matches the normal program (for Spectrum, compare loaded CODE rather than the tape filename).
Select the deliberately wrong client
org $8000
CUSTOM equ 0
BROKEN equ 1
COYOTE_STEPS equ 4
BUFFER_STEPS equ 4
TEXT_CAP equ 8
PROPERTY_COUNT equ 16
STREAM_BYTES equ 13
CRC_POLY equ 7
include "caller.inc"
include "exercise.inc"
component_start:
include "component.inc"
include "data.inc"
include "cases.inc"
include "../../physics/rectangle-overlap/display.inc"
code_end:
STATE:
OUT_LEFT: defb 0
OUT_TOP: defb 0
OUT_RIGHT: defb 0
OUT_BOTTOM: defb 0
VISIBLE: defb 0
VALID: defb 0
LEFT: defb 0
TOP: defb 0
RIGHT: defb 0
BOTTOM: defb 0
VIEW_LEFT: defb 0
VIEW_TOP: defb 0
VIEW_RIGHT: defb 0
VIEW_BOTTOM: defb 0
T0: defb 0
T1: defb 0
T2: defb 0
T3: defb 0
input_ptr: defw 0
output_ptr: defw 0
cases_left: defb 0
ready: defb 0
preview: defs 8,0
OUTPUT: defs 1300,0
end start
Cost and alternatives
The measured helper extent is 384 code bytes; public state is 14 bytes and private scratch is 4 bytes. There are no immutable helper tables. Caller data, the test cases, return-address stack, alignment and display buffers cost additional space. These are assembled sizes, not cycle measurements.
Constant work. Clip before entering expensive pixel loops; complex shapes still require their own clipping rules.
This returns geometry, not a masked draw. Coordinates cannot represent an exclusive edge of 256; use a wider type for a full 256-pixel span.
Execution evidence
Four variants run on 48K PAL ROM tape load and USR, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 64 boundary/varied/error table cases, dirty initialisation and the separate retained-state client. An independent mathematical/container model checks every public byte after every call. This is bounded example coverage, not exhaustive correctness for every possible state.
The records retain loaded immutable code, completion, register sentinels, full packets, source/tool/firmware hashes and captures. The public gate validates these retained observations and rejects altered evidence; it does not run restricted firmware. No firmware is distributed. The eight displayed bytes are the first eight public fields, zero-padded when shorter; the packet includes every field.
Where it fits
Rectangle overlap: Half-open edge conventions must agree between collision and drawing.