Skip to content

Transpose an Eight-by-eight Bitmap on Amiga

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.

Amiga 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

component.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:
    move.b #0,d0
    move.b d0,R0
    move.b d0,R1
    move.b d0,R2
    move.b d0,R3
    move.b d0,R4
    move.b d0,R5
    move.b d0,R6
    move.b d0,R7
    move.b d0,C0
    move.b d0,C1
    move.b d0,C2
    move.b d0,C3
    move.b d0,C4
    move.b d0,C5
    move.b d0,C6
    move.b d0,C7
    move.b d0,T0
    move.b d0,T1
    move.b d0,T2
    move.b d0,T3
    move.b d0,T4
    move.b d0,EXPR_SAVE
    rts
bitmap_transpose_step:
; T0 = 0
    move.b #0,d0
    move.b d0,T0
; while T0 < 8:
bitmap_transpose_L1:
    move.b T0,d0
    cmp.b #8,d0
    bcs bitmap_transpose_L2
    jmp bitmap_transpose_L3
bitmap_transpose_L2:
; R[T0] = 0
    move.b #0,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea R0,a0
    move.b d2,0(a0,d1.w)
; T1 = 0
    move.b #0,d0
    move.b d0,T1
; T2 = 128
    move.b #128,d0
    move.b d0,T2
; while T1 < 8:
bitmap_transpose_L4:
    move.b T1,d0
    cmp.b #8,d0
    bcs bitmap_transpose_L5
    jmp bitmap_transpose_L6
bitmap_transpose_L5:
; T3 = C[T1]
    moveq #0,d1
    move.b T1,d1
    lea C0,a0
    move.b 0(a0,d1.w),d0
    move.b d0,T3
; T4 = T0
    move.b T0,d0
    move.b d0,T4
; while T4 != 0:
bitmap_transpose_L7:
    move.b T4,d0
    cmp.b #0,d0
    bne bitmap_transpose_L8
    jmp bitmap_transpose_L9
bitmap_transpose_L8:
; T3 = T3 << 1
    move.b T3,d0
    lsl.b #1,d0
    move.b d0,T3
; T4 = T4 - 1
    move.b T4,d0
    sub.b #1,d0
    move.b d0,T4
    jmp bitmap_transpose_L7
bitmap_transpose_L9:
; T3 = T3 & 128
    move.b T3,d0
    and.b #128,d0
    move.b d0,T3
; if T3 != 0:
    move.b T3,d0
    cmp.b #0,d0
    bne bitmap_transpose_L10
    jmp bitmap_transpose_L11
bitmap_transpose_L10:
; T3 = R[T0]
    moveq #0,d1
    move.b T0,d1
    lea R0,a0
    move.b 0(a0,d1.w),d0
    move.b d0,T3
; T3 = T3 | T2
    move.b T3,d0
    move.b T2,d1
    or.b d1,d0
    move.b d0,T3
; R[T0] = T3
    move.b T3,d0
    move.b d0,d2
    moveq #0,d1
    move.b T0,d1
    lea R0,a0
    move.b d2,0(a0,d1.w)
    jmp bitmap_transpose_L12
bitmap_transpose_L11:
bitmap_transpose_L12:
; T2 = T2 >> 1
    move.b T2,d0
    lsr.b #1,d0
    move.b d0,T2
; T1 = T1 + 1
    move.b T1,d0
    add.b #1,d0
    move.b d0,T1
    jmp bitmap_transpose_L4
bitmap_transpose_L6:
; T0 = T0 + 1
    move.b T0,d0
    add.b #1,d0
    move.b d0,T0
    jmp bitmap_transpose_L1
bitmap_transpose_L3:
    rts
component_end:

Machine contract

D0–D3/A0–A1/CCR may change; D4–D7/A2–A6 are preserved. A direct BSR/JSR needs four 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 A500 OCS diagnostic cold-boots an OFS disk, owns the Copper and bitplane and disables interrupt/DMA sources. The helper does not take over hardware or allocate Chip RAM.

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 and BUILD198X=/path/to/build198x. 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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
R0: dc.b 0
R1: dc.b 0
R2: dc.b 0
R3: dc.b 0
R4: dc.b 0
R5: dc.b 0
R6: dc.b 0
R7: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XTOOLKIT0000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0

A client retaining state between calls

exercise.inc, excerpt
preview_rules:
    ifne CUSTOM
; C0 = 128
    move.b #128,d0
    move.b d0,C0
; C1 = 64
    move.b #64,d0
    move.b d0,C1
; C2 = 32
    move.b #32,d0
    move.b d0,C2
; C3 = 16
    move.b #16,d0
    move.b d0,C3
; C4 = 8
    move.b #8,d0
    move.b d0,C4
; C5 = 4
    move.b #4,d0
    move.b d0,C5
; C6 = 2
    move.b #2,d0
    move.b d0,C6
; C7 = 1
    move.b #1,d0
    move.b d0,C7
    ifne BROKEN
; C0 = 0
    move.b #0,d0
    move.b d0,C0
    endif
    bsr checked_step
    bsr emit_state
    rts
    else
; C0 = 255
    move.b #255,d0
    move.b d0,C0
; C1 = 128
    move.b #128,d0
    move.b d0,C1
; C2 = 128
    move.b #128,d0
    move.b d0,C2
; C3 = 128
    move.b #128,d0
    move.b d0,C3
    ifne BROKEN
; C0 = 0
    move.b #0,d0
    move.b d0,C0
    endif
    bsr checked_step
    bsr 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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
R0: dc.b 0
R1: dc.b 0
R2: dc.b 0
R3: dc.b 0
R4: dc.b 0
R5: dc.b 0
R6: dc.b 0
R7: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XTOOLKIT0000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,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
    section code,code_c
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"
    even
    include "data.inc"
    include "cases.inc"
    even
    include "display.inc"
    include "../../framework/turn-deadline/font.inc"
code_end:
STATE:
R0: dc.b 0
R1: dc.b 0
R2: dc.b 0
R3: dc.b 0
R4: dc.b 0
R5: dc.b 0
R6: dc.b 0
R7: dc.b 0
C0: dc.b 0
C1: dc.b 0
C2: dc.b 0
C3: dc.b 0
C4: dc.b 0
C5: dc.b 0
C6: dc.b 0
C7: dc.b 0
T0: dc.b 0
T1: dc.b 0
T2: dc.b 0
T3: dc.b 0
T4: dc.b 0
EXPR_SAVE: dc.b 0
    even
ready: dc.w 0
preview: dcb.b 8,0
OUTPUT: dcb.b 1300,0
    even
signature: dc.b "198XTOOLKIT0000",0
    even
copperlist:
    dc.w $08e,$2c81,$090,$2cc1,$092,$0038,$094,$00d0
    dc.w $100,$1200,$102,0,$104,0,$108,0,$10a,0
    dc.w $180,$012,$182,$fff
copbpl:
    dc.w $0e0,0,$0e2,0
    dc.w $ffff,$fffe
plane: dcb.b 10240,0
Amiga 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 418 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 A500 OCS PAL, Kickstart1.3 OFS cold boot, using Asm198x 0.0.72, Emu198x 0.44.2 and Build198x 0.2.9. 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.