Skip to content

Transpose an Eight-by-eight Bitmap on NES

Turning rows into columns is the building block behind rotating small glyphs and adapting bitmap layouts.

Turning rows into columns is the building block behind rotating small glyphs and adapting bitmap layouts.

NES shows the working diagnostic row F0 80 80 80 80 80 80 80.
A completed native caller displays F0 80 80 80 80 80 80 80. The public field order below identifies these bytes; the capture is a state diagnostic, not a recording of a finished game.

Use it in a project

C0..C7 are eight monochrome source rows, bit seven leftmost. Return R0..R7 with row and column exchanged: source pixel (x,y) becomes destination (y,x). Preserve source rows. This is a diagonal reflection; combine with mirroring for a quarter turn.

Include component.inc once (MOS callers include the shared 6502 implementation through it). Reserve the public fields in this exact order: R0, R1, R2, R3, R4, R5, R6, R7, C0, C1, C2, C3, C4, C5, C6, C7. They occupy 16 contiguous bytes. Also reserve the private workspace: T0, T1, T2, T3, T4, EXPR_SAVE (6 bytes). Array names in the explanation refer to the numbered consecutive bindings, such as C0, C1 and so on.

Call bitmap_transpose_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 bitmap_transpose_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 C0..C7
Read after service R0..R7

Each output row corresponds to one input column. Test that bit in every source row and place it at the matching output position.

Reusable initialisation and service

6502.inc
; SPDX-License-Identifier: MIT
; Transpose an Eight-by-eight Bitmap. See README.md for the byte interface and ownership.
; init clears the public fields and private workspace; set inputs afterwards.
bitmap_transpose_init:
    lda #0
    sta R0
    sta R1
    sta R2
    sta R3
    sta R4
    sta R5
    sta R6
    sta R7
    sta C0
    sta C1
    sta C2
    sta C3
    sta C4
    sta C5
    sta C6
    sta C7
    sta T0
    sta T1
    sta T2
    sta T3
    sta T4
    sta EXPR_SAVE
    rts
bitmap_transpose_step:
; T0 = 0
    lda #0
    sta T0
; while T0 < 8:
bitmap_transpose_L1:
    lda T0
    cmp #8
    bcc bitmap_transpose_L4
bitmap_transpose_L5:
    jmp bitmap_transpose_L3
bitmap_transpose_L4:
    jmp bitmap_transpose_L2
bitmap_transpose_L2:
; R[T0] = 0
    lda #0
    sta EXPR_SAVE
    ldx T0
    lda EXPR_SAVE
    sta R0,x
; T1 = 0
    lda #0
    sta T1
; T2 = 128
    lda #128
    sta T2
; while T1 < 8:
bitmap_transpose_L6:
    lda T1
    cmp #8
    bcc bitmap_transpose_L9
bitmap_transpose_L10:
    jmp bitmap_transpose_L8
bitmap_transpose_L9:
    jmp bitmap_transpose_L7
bitmap_transpose_L7:
; T3 = C[T1]
    ldx T1
    lda C0,x
    sta T3
; T4 = T0
    lda T0
    sta T4
; while T4 != 0:
bitmap_transpose_L11:
    lda T4
    cmp #0
    bne bitmap_transpose_L14
bitmap_transpose_L15:
    jmp bitmap_transpose_L13
bitmap_transpose_L14:
    jmp bitmap_transpose_L12
bitmap_transpose_L12:
; T3 = T3 << 1
    lda T3
    asl
    sta T3
; T4 = T4 - 1
    lda T4
    sec
    sbc #1
    sta T4
    jmp bitmap_transpose_L11
bitmap_transpose_L13:
; T3 = T3 & 128
    lda T3
    and #128
    sta T3
; if T3 != 0:
    lda T3
    cmp #0
    bne bitmap_transpose_L19
bitmap_transpose_L20:
    jmp bitmap_transpose_L17
bitmap_transpose_L19:
    jmp bitmap_transpose_L16
bitmap_transpose_L16:
; T3 = R[T0]
    ldx T0
    lda R0,x
    sta T3
; T3 = T3 | T2
    lda T3
    ora T2
    sta T3
; R[T0] = T3
    lda T3
    sta EXPR_SAVE
    ldx T0
    lda EXPR_SAVE
    sta R0,x
    jmp bitmap_transpose_L18
bitmap_transpose_L17:
bitmap_transpose_L18:
; T2 = T2 >> 1
    lda T2
    lsr
    sta T2
; T1 = T1 + 1
    lda T1
    clc
    adc #1
    sta T1
    jmp bitmap_transpose_L6
bitmap_transpose_L8:
; T0 = T0 + 1
    lda T0
    clc
    adc #1
    sta T0
    jmp bitmap_transpose_L1
bitmap_transpose_L3:
    rts
component_end:

Machine contract

A/X/status may change; Y is preserved. The 2A03 has no decimal arithmetic mode. A direct JSR 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 NROM caller owns PPU setup and background display, with NMI and DMC disabled. Table and packet sizes are bounded to fit the cartridge caller and internal RAM; the helper owns no PPU registers.

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

demo.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
R0=$80
R1=$81
R2=$82
R3=$83
R4=$84
R5=$85
R6=$86
R7=$87
C0=$88
C1=$89
C2=$8a
C3=$8b
C4=$8c
C5=$8d
C6=$8e
C7=$8f
T0=$90
T1=$91
T2=$92
T3=$93
T4=$94
EXPR_SAVE=$95
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=0
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
.if CUSTOM
; C0 = 128
    lda #128
    sta C0
; C1 = 64
    lda #64
    sta C1
; C2 = 32
    lda #32
    sta C2
; C3 = 16
    lda #16
    sta C3
; C4 = 8
    lda #8
    sta C4
; C5 = 4
    lda #4
    sta C5
; C6 = 2
    lda #2
    sta C6
; C7 = 1
    lda #1
    sta C7
.if BROKEN
; C0 = 0
    lda #0
    sta C0
.endif
    jsr checked_step
    jsr emit_state
    rts
.else
; C0 = 255
    lda #255
    sta C0
; C1 = 128
    lda #128
    sta C1
; C2 = 128
    lda #128
    sta C2
; C3 = 128
    lda #128
    sta C3
.if BROKEN
; C0 = 0
    lda #0
    sta C0
.endif
    jsr checked_step
    jsr emit_state
.endif
    rts

Customise it

Transpose an identity diagonal, which should remain identical. 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

custom.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
R0=$80
R1=$81
R2=$82
R3=$83
R4=$84
R5=$85
R6=$86
R7=$87
C0=$88
C1=$89
C2=$8a
C3=$8b
C4=$8c
C5=$8d
C6=$8e
C7=$8f
T0=$90
T1=$91
T2=$92
T3=$93
T4=$94
EXPR_SAVE=$95
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=1
BROKEN=0
COYOTE_STEPS=2
BUFFER_STEPS=2
TEXT_CAP=4
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=49
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0

The custom diagnostic row is 80 40 20 10 08 04 02 01. The full client trace in the source explains how it gets there.

Mistake and repair

The wrong caller clears the first row before transforming it. The destination’s first column loses those pixels. Keep all eight source rows until the transform has completed.

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

wrong.asm
.segment "HEADER"
.byte "NES",$1a,2,1,0,0,0,0,0,0,0,0,0,0
R0=$80
R1=$81
R2=$82
R3=$83
R4=$84
R5=$85
R6=$86
R7=$87
C0=$88
C1=$89
C2=$8a
C3=$8b
C4=$8c
C5=$8d
C6=$8e
C7=$8f
T0=$90
T1=$91
T2=$92
T3=$93
T4=$94
EXPR_SAVE=$95
STATE=$80
input_ptr=$70
cases_left=$72
ptr=$74
saved_y=$76
OUTPUT=$0200
preview=OUTPUT+1400
ready=OUTPUT+1408
CUSTOM=0
BROKEN=1
COYOTE_STEPS=4
BUFFER_STEPS=4
TEXT_CAP=8
PROPERTY_COUNT=16
STREAM_BYTES=13
CRC_POLY=7
.include "caller.inc"
.include "exercise.inc"
component_start:
.include "component.inc"
.include "data.inc"
.include "cases.inc"
code_end:
.segment "VECTORS"
.word nmi,reset,irq
.segment "CHARS"
.include "../../input/controller-reading/edges-font.inc"
.res 8192-272,0
NES shows the deliberate error row 70 00 00 00 00 00 00 00.
The wrong client completes with 70 00 00 00 00 00 00 00; its changed inputs and state explain the result.

Cost and alternatives

The measured helper extent is 199 code bytes; public state is 16 bytes and private scratch is 6 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.

Sixty-four pixel tests plus shifts. A butterfly bit-swap algorithm is faster but less direct; precomputed transformed art costs storage instead of runtime.

Source and destination bindings must be separate. This is a data transformation, not a machine-specific draw routine; screen attributes and colour planes need separate handling.

Execution evidence

Four variants run on NTSC NROM, background only, DMC/NMI disabled, using Asm198x 0.0.72, Emu198x 0.44.2. Each executes 62 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.

Existing alternatives

The existing full-glyph transform covers rotation and reflection with an overlap-safe pointer interface on its documented machines. This separate-buffer helper exposes transposition directly for row/column conversion. A transpose reflects across a diagonal; it must not silently substitute for a clockwise turn. Choose the existing transform for its quarter-turn flags or in-place operation.

Where it fits

Bitmap mirroring: Transpose plus a row mirror produces a quarter-turn rotation.

The Craft · Maths for Games · Source and native evidence.