From dca45b97e089e2a99e021dd305eab62800879c41 Mon Sep 17 00:00:00 2001 From: Willem Drijver Date: Tue, 3 Nov 2026 14:57:57 +0300 Subject: [PATCH 1/2] Core pillar content from TuKo1982's 68080 optimisation guide Split sections 0-7, 13 (ARNE) and 15.7 (Maggie registers) of the "68080 compilation and optimisation guide" into core/: measured references in cpu-68080/, best practices, pitfalls, and register files for Maggie and ARNE. Owner TuKo1982, ai-assisted, draft, with source tags (G)/(M)/(official)/(SRC) and core revisions. Co-Authored-By: Claude Opus 5.5 --- .github/CODEOWNERS | 44 +++++++ core/best-practices/align-copy-destination.md | 55 +++++++++ core/best-practices/ammx-where-it-wins.md | 63 ++++++++++ core/best-practices/close-loops-with-dbra.md | 66 +++++++++++ .../best-practices/code-size-and-placement.md | 66 +++++++++++ .../fixed-point-and-bitstreams.md | 68 +++++++++++ .../fpu-schedule-by-completion-slot.md | 64 ++++++++++ core/best-practices/fpu-when-it-pays.md | 59 ++++++++++ .../interleave-dependency-chains.md | 59 ++++++++++ core/best-practices/memory-access-patterns.md | 54 +++++++++ .../predicated-bcc-over-one-instruction.md | 58 ++++++++++ core/best-practices/use-fusable-pairs.md | 60 ++++++++++ core/cpu-68080/bank-prefix.md | 100 ++++++++++++++++ core/cpu-68080/branches-and-prediction.md | 101 ++++++++++++++++ core/cpu-68080/caches-and-memory.md | 101 ++++++++++++++++ core/cpu-68080/core-at-a-glance.md | 61 ++++++++++ core/cpu-68080/core-bugs-by-revision.md | 65 +++++++++++ core/cpu-68080/fpu-performance-model.md | 69 +++++++++++ core/cpu-68080/index.md | 22 +++- core/cpu-68080/isa-notes-measured.md | 68 +++++++++++ core/cpu-68080/measured-instruction-costs.md | 74 ++++++++++++ core/cpu-68080/performance-counters.md | 80 +++++++++++++ core/cpu-68080/pipeline-15-stages.md | 98 ++++++++++++++++ core/cpu-68080/superscalar-issue.md | 109 ++++++++++++++++++ .../alternating-branch-mispredicted.md | 67 +++++++++++ core/pitfalls/alu-to-ea-bubble.md | 85 ++++++++++++++ core/pitfalls/ammx-pc-relative-source.md | 57 +++++++++ core/pitfalls/ammx-upper-32-bits.md | 68 +++++++++++ core/pitfalls/bank-prefix-pitfalls.md | 77 +++++++++++++ core/pitfalls/fpu-47-bit-context-save.md | 66 +++++++++++ core/pitfalls/fpu-broken-instructions.md | 71 ++++++++++++ core/pitfalls/fpu-exceptions-never-trap.md | 64 ++++++++++ .../fpu-float-to-integer-conversion.md | 73 ++++++++++++ core/pitfalls/fpu-precision-and-rounding.md | 79 +++++++++++++ core/pitfalls/fused-copy-overlap.md | 66 +++++++++++ .../maggie-depth-test-colour-write.md | 54 +++++++++ core/pitfalls/maggie-span-tail.md | 61 ++++++++++ core/pitfalls/maggie-span-usage.md | 73 ++++++++++++ .../multiply-and-bfffo-block-both-pipes.md | 71 ++++++++++++ core/pitfalls/pc-relative-destination.md | 70 +++++++++++ core/pitfalls/return-address-patching.md | 66 +++++++++++ .../self-modifying-code-and-caches.md | 76 ++++++++++++ core/pitfalls/tex-does-not-match-prm.md | 62 ++++++++++ core/pitfalls/touch-misuse.md | 67 +++++++++++ core/saga-3d-maggie/index.md | 12 +- core/saga-3d-maggie/registers.md | 89 ++++++++++++++ core/saga-audio-arne/index.md | 10 +- core/saga-audio-arne/registers.md | 67 +++++++++++ 48 files changed, 3206 insertions(+), 9 deletions(-) create mode 100644 core/best-practices/align-copy-destination.md create mode 100644 core/best-practices/ammx-where-it-wins.md create mode 100644 core/best-practices/close-loops-with-dbra.md create mode 100644 core/best-practices/code-size-and-placement.md create mode 100644 core/best-practices/fixed-point-and-bitstreams.md create mode 100644 core/best-practices/fpu-schedule-by-completion-slot.md create mode 100644 core/best-practices/fpu-when-it-pays.md create mode 100644 core/best-practices/interleave-dependency-chains.md create mode 100644 core/best-practices/memory-access-patterns.md create mode 100644 core/best-practices/predicated-bcc-over-one-instruction.md create mode 100644 core/best-practices/use-fusable-pairs.md create mode 100644 core/cpu-68080/bank-prefix.md create mode 100644 core/cpu-68080/branches-and-prediction.md create mode 100644 core/cpu-68080/caches-and-memory.md create mode 100644 core/cpu-68080/core-at-a-glance.md create mode 100644 core/cpu-68080/core-bugs-by-revision.md create mode 100644 core/cpu-68080/fpu-performance-model.md create mode 100644 core/cpu-68080/isa-notes-measured.md create mode 100644 core/cpu-68080/measured-instruction-costs.md create mode 100644 core/cpu-68080/performance-counters.md create mode 100644 core/cpu-68080/pipeline-15-stages.md create mode 100644 core/cpu-68080/superscalar-issue.md create mode 100644 core/pitfalls/alternating-branch-mispredicted.md create mode 100644 core/pitfalls/alu-to-ea-bubble.md create mode 100644 core/pitfalls/ammx-pc-relative-source.md create mode 100644 core/pitfalls/ammx-upper-32-bits.md create mode 100644 core/pitfalls/bank-prefix-pitfalls.md create mode 100644 core/pitfalls/fpu-47-bit-context-save.md create mode 100644 core/pitfalls/fpu-broken-instructions.md create mode 100644 core/pitfalls/fpu-exceptions-never-trap.md create mode 100644 core/pitfalls/fpu-float-to-integer-conversion.md create mode 100644 core/pitfalls/fpu-precision-and-rounding.md create mode 100644 core/pitfalls/fused-copy-overlap.md create mode 100644 core/pitfalls/maggie-depth-test-colour-write.md create mode 100644 core/pitfalls/maggie-span-tail.md create mode 100644 core/pitfalls/maggie-span-usage.md create mode 100644 core/pitfalls/multiply-and-bfffo-block-both-pipes.md create mode 100644 core/pitfalls/pc-relative-destination.md create mode 100644 core/pitfalls/return-address-patching.md create mode 100644 core/pitfalls/self-modifying-code-and-caches.md create mode 100644 core/pitfalls/tex-does-not-match-prm.md create mode 100644 core/pitfalls/touch-misuse.md create mode 100644 core/saga-3d-maggie/registers.md create mode 100644 core/saga-audio-arne/registers.md diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 07f5379..3117549 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -15,7 +15,29 @@ /.github/ @WDrijver @TuKo1982 /.markdownlint.jsonc @WDrijver @TuKo1982 +/core/best-practices/align-copy-destination.md @TuKo1982 +/core/best-practices/ammx-where-it-wins.md @TuKo1982 +/core/best-practices/close-loops-with-dbra.md @TuKo1982 +/core/best-practices/code-size-and-placement.md @TuKo1982 +/core/best-practices/fixed-point-and-bitstreams.md @TuKo1982 +/core/best-practices/fpu-schedule-by-completion-slot.md @TuKo1982 +/core/best-practices/fpu-when-it-pays.md @TuKo1982 +/core/best-practices/interleave-dependency-chains.md @TuKo1982 +/core/best-practices/memory-access-patterns.md @TuKo1982 +/core/best-practices/predicated-bcc-over-one-instruction.md @TuKo1982 +/core/best-practices/use-fusable-pairs.md @TuKo1982 +/core/cpu-68080/bank-prefix.md @TuKo1982 +/core/cpu-68080/branches-and-prediction.md @TuKo1982 +/core/cpu-68080/caches-and-memory.md @TuKo1982 +/core/cpu-68080/core-at-a-glance.md @TuKo1982 +/core/cpu-68080/core-bugs-by-revision.md @TuKo1982 +/core/cpu-68080/fpu-performance-model.md @TuKo1982 /core/cpu-68080/index.md @WDrijver +/core/cpu-68080/isa-notes-measured.md @TuKo1982 +/core/cpu-68080/measured-instruction-costs.md @TuKo1982 +/core/cpu-68080/performance-counters.md @TuKo1982 +/core/cpu-68080/pipeline-15-stages.md @TuKo1982 +/core/cpu-68080/superscalar-issue.md @TuKo1982 /core/cpu-68080/prm/ammx-unit.md @WDrijver /core/cpu-68080/prm/apollo-bit.md @WDrijver /core/cpu-68080/prm/b-registers.md @WDrijver @@ -105,8 +127,30 @@ /core/cpu-68080/prm/integer/perm.md @WDrijver /core/cpu-68080/prm/integer/subq-bn.md @WDrijver /core/cpu-68080/prm/integer/touch.md @WDrijver +/core/pitfalls/alternating-branch-mispredicted.md @TuKo1982 +/core/pitfalls/alu-to-ea-bubble.md @TuKo1982 +/core/pitfalls/ammx-pc-relative-source.md @TuKo1982 +/core/pitfalls/ammx-upper-32-bits.md @TuKo1982 +/core/pitfalls/bank-prefix-pitfalls.md @TuKo1982 +/core/pitfalls/fpu-47-bit-context-save.md @TuKo1982 +/core/pitfalls/fpu-broken-instructions.md @TuKo1982 +/core/pitfalls/fpu-exceptions-never-trap.md @TuKo1982 +/core/pitfalls/fpu-float-to-integer-conversion.md @TuKo1982 +/core/pitfalls/fpu-precision-and-rounding.md @TuKo1982 +/core/pitfalls/fused-copy-overlap.md @TuKo1982 +/core/pitfalls/maggie-depth-test-colour-write.md @TuKo1982 +/core/pitfalls/maggie-span-tail.md @TuKo1982 +/core/pitfalls/maggie-span-usage.md @TuKo1982 +/core/pitfalls/multiply-and-bfffo-block-both-pipes.md @TuKo1982 +/core/pitfalls/pc-relative-destination.md @TuKo1982 +/core/pitfalls/return-address-patching.md @TuKo1982 +/core/pitfalls/self-modifying-code-and-caches.md @TuKo1982 +/core/pitfalls/tex-does-not-match-prm.md @TuKo1982 +/core/pitfalls/touch-misuse.md @TuKo1982 /core/saga-3d-maggie/index.md @WDrijver +/core/saga-3d-maggie/registers.md @TuKo1982 /core/saga-audio-arne/index.md @WDrijver +/core/saga-audio-arne/registers.md @TuKo1982 /core/saga-dma-anni/index.md @WDrijver /core/saga-video-isabelle/index.md @WDrijver /hardware/index.md @WDrijver diff --git a/core/best-practices/align-copy-destination.md b/core/best-practices/align-copy-destination.md new file mode 100644 index 0000000..04c94dc --- /dev/null +++ b/core/best-practices/align-copy-destination.md @@ -0,0 +1,55 @@ +--- +title: Align the destination of copies and fills +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, alignment, copy, blit, ammx, move16] +related: + - core/cpu-68080/caches-and-memory.md + - core/best-practices/ammx-where-it-wins.md + - core/pitfalls/fused-copy-overlap.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rules 12-13), 5.1 and 12" +--- + +## Goal + +Bandwidth: about 3.2–3.5 bytes per cycle for copies, instead of about 1.8. + +## Technique + +- Align the **destination** to 8 bytes (quad). A misaligned source costs only + 0–13 %; a misaligned destination 1.7–1.8x. +- Use aligned AMMX `load`/`store`, or fused quad moves + (`move.l (a0)+,(a1)+` twice), for the bulk. Handle the misaligned head and + tail separately. +- Use AMMX `storem` (masked store) for the edges: about 1.85 cycles per quad, + whatever the alignment. +- Remember that P96 `AllocBitMap` bases are only 4-byte aligned. + +## Evidence + +Copy of a 156 x 2464-byte shape (M, Core 12841): + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| `move.l` loop | 1.7–2.0 bytes/cycle | Core 12841 | CCC | +| AMMX `load`/`store`, misaligned | About 1.8 | Core 12841 | Same | +| AMMX `load`/`store`, 8-byte aligned | 3.2–3.5 | Core 12841 | Same | +| `move16`, 16-byte aligned | 3.1–3.2 | Core 12841 | Same | +| `move16`, misaligned | About 1.75 | Core 12841 | Same | + +## When not to use + +- Copies are memory-bound at about 3 bytes per cycle: beyond alignment, + optimising a blit copy gained only 3–10 %. +- `move16` is never faster than aligned AMMX or fused quad moves, so it is + not worth the extra alignment work. +- Tiny shapes: for rows of 64 bytes or less, plain 68k code wins (see + [ammx-where-it-wins.md](ammx-where-it-wins.md)). diff --git a/core/best-practices/ammx-where-it-wins.md b/core/best-practices/ammx-where-it-wins.md new file mode 100644 index 0000000..28c262f --- /dev/null +++ b/core/best-practices/ammx-where-it-wins.md @@ -0,0 +1,63 @@ +--- +title: Use AMMX where it wins +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080, saga-video-isabelle] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, ammx, simd, blit, fill, p96] +related: + - core/cpu-68080/prm/ammx-unit.md + - core/best-practices/align-copy-destination.md + - core/pitfalls/ammx-pc-relative-source.md + - core/pitfalls/ammx-upper-32-bits.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 6, 11 and 12" +--- + +## Goal + +Speed for fills, masked stores, pattern fills, format conversion and +decimation, where AMMX gives 1.8x to 11x. + +## Technique + +- Use aligned AMMX stores for fills, `storem` masked stores for template + (text) blits, and preload pattern rows into registers for pattern fills. +- Use AMMX for byte and word lanes: format conversion and decimation. +- Keep a plain 68k path for tiny shapes: + - EOR invert for rows of 64 bytes or less; + - a dedicated column path for widths of 8 pixels or less. +- Record the real call shapes before optimising: in real GUI FillRects, 86 % + of rows were 1–4 pixels wide (M). +- Prefer arithmetic over lookup tables: a table that wins in a hot benchmark + can lose when the real workload evicts it. +- Enable AMMX register saving with the Apollo bit: `ori #$800,sr` / + `andi #$f7ff,sr` (about 8 cycles for the pair, user mode). The OS + `V_EnableAMMX` pair cost 248 cycles (M). + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Aligned fills | x1.84 | Core 12841 | CCC (M) | +| P96 FillRect, real workload | 1.80 M → 0.60 M cycles | Core 12841 | Aligned AMMX stores + 68k column path | +| P96 BlitTemplate (text) | 2.38 M → 1.59 M cycles | Core 12841 | AMMX `storem` | +| P96 BlitPattern | 307 K → 28 K cycles | Core 12841 | AMMX, two pattern rows preloaded | +| 1-pixel rows, generic vs column path | 14.6 → 2.25 cycles | Core 12841 | Same | +| 6 KB colour lookup table | 1.4x faster in the benchmark, 3–10 % slower on the real console | Core 12841 | Real workload | + +## When not to use + +- **Lanes are bytes or words only.** `pmul*` is 4 x 16x16 (high, low or bits + 23..8); there is no 32-bit add and no MAC. AMMX does not suit 24/32-bit + audio maths: FLAC LPC, Rice decoding, or lossless MP3 synthesis (a 16-bit + window gave 46 dB SNR). +- There is one AMMX unit, so the floor is about IPC 1.0; the second pipe + accepts AMMX stores only. +- A 128-bit AMMX unit is in development (G, 2026-10-29); released cores + (12841) have the 64-bit unit. diff --git a/core/best-practices/close-loops-with-dbra.md b/core/best-practices/close-loops-with-dbra.md new file mode 100644 index 0000000..274b876 --- /dev/null +++ b/core/best-practices/close-loops-with-dbra.md @@ -0,0 +1,66 @@ +--- +title: Close loops with dbra or subq + bne.s +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, loops, dbra, branches, fusing] +related: + - core/cpu-68080/branches-and-prediction.md + - core/best-practices/code-size-and-placement.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 4), 2.4 and 5.3" +--- + +## Goal + +Speed: loop overhead of 1 cycle instead of 3. + +## Technique + +Close loops with `dbra`, or with `subq.l #1,Dn` followed directly by +`bne.s`, which fuse into one dbra-like instruction. A `bne.w` is not an +accelerated branch. + +```asm +loop: + ; ... body ... + dbra d0,loop ; 16-bit counter + +; or, for a 32-bit counter: +loop2: + ; ... body ... + subq.l #1,d1 + bne.s loop2 ; fuses with the subq +``` + +A counter that drives addressing can live in An, so it does not hit the +ALU-to-EA bubble: + +```asm +loop3: + ; ... body ... + subq.l #1,a6 ; EA unit, no flags + move.l a6,d0 ; sets N/Z, no write back + bne.s loop3 +``` + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Loop closed with a `bne.w` that does not fuse | 3 cycles overhead | Core 12841 | CCC counter (M) | +| Loop closed with `dbra` or `subq.l` + `bne.s` | 1 cycle overhead | Core 12841 | Same | + +`subq.l #1,dn` + `bne.s/.s+/.w` fuses; `subq` + `bpl` does not (M). Gunnar: +"DBRA is fast and nearly free" (G). + +## When not to use + +- Very short loops (1–4 turns) are expensive whatever the closing branch: one + turn cost about 12 cycles, each extra turn 3 (M). Special-case them. diff --git a/core/best-practices/code-size-and-placement.md b/core/best-practices/code-size-and-placement.md new file mode 100644 index 0000000..e5a8dd5 --- /dev/null +++ b/core/best-practices/code-size-and-placement.md @@ -0,0 +1,66 @@ +--- +title: Keep code small and hot code together; don't unroll by default +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080, toolchain] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, icache, unrolling, code-size, link-order, placement] +related: + - core/cpu-68080/core-at-a-glance.md + - core/best-practices/close-loops-with-dbra.md + - core/cpu-68080/prm/optimizing.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rules 14-15), 5.3 and 12" + - "Gunnar (core developer), statements on unrolling and padding, 2026-10-29 and 2026-10-30" +--- + +## Goal + +Speed of the whole program: hot code that stays in the I-cache. Fat code +evicts other code, and reloading it costs hundreds of cycles. + +## Technique + +- **Don't unroll by default** (G): "DBRA is fast and nearly free." "If you + can loop 512 times, loop 512 times." "10 lines made into 6 KB = big + mistake." +- **Share code** instead of duplicating variants. +- **Keep hot code contiguous.** Link the assembler routine right after the C + code that calls it, and put cold objects first. Profile builds must keep + the release link order: use `-fno-toplevel-reorder` and the same object + order. +- **Read memory in address order through `(An)+`** (G). Re-lay tables in the + order you read them instead of keeping a C layout; `d16(An)` everywhere + makes code fat. +- **Don't pad code to move it in the I-cache** (G: "a terrible idea"). Tune + the code for size; if it still does not fit, raise it with the Apollo team + (for example more I-cache ways in the release core). + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Six IMDCT copies (12.9 KB) | Baseline | Core 12841 | Full MP3 decode, CCC (M) | +| One shared transform + 3 short windows (3.8 KB) | Full decode 0.2–0.6 % faster | Core 12841 | Same | +| Profile build with default link order | Synthesis 10.1 % of time | Core 12841 | Same | +| Profile build with release link order | Synthesis 7.3 % of time | Core 12841 | Same | +| Same binary, different load addresses | Up to about 5 % apart (41.6–49.9 cycles on one loop) | Core 12841 | Same | + +- Unrolled variants that won alone (an 8 KB FPU window, a 15-row integer + window) lost in the full decoder (M). +- Shrinking one 2.4 KB routine moved the code linked after it and cost 1.7 % + on the full decode, while the routine itself got 2 % faster (M). + +## When not to use + +- A short scheduled tail can still win: turning a **multiply-bound** + scheduled tail into a dbra loop cost about 30 % per element (14 vs 11 + cycles per sample). "Never unroll" holds for duplicated copies. +- Measure any unrolling in the full program, never in a microbenchmark. +- Core 12841 has an I-cache with few ways; 1-way builds alias code bigger + than the cache by design. Note the core build with every measurement. diff --git a/core/best-practices/fixed-point-and-bitstreams.md b/core/best-practices/fixed-point-and-bitstreams.md new file mode 100644 index 0000000..fbe9a8a --- /dev/null +++ b/core/best-practices/fixed-point-and-bitstreams.md @@ -0,0 +1,68 @@ +--- +title: Fixed-point multiplies and bitstream decoding on the 68080 +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fixed-point, muls, bfextu, bfffo, huffman, codecs] +related: + - core/cpu-68080/measured-instruction-costs.md + - core/pitfalls/multiply-and-bfffo-block-both-pipes.md + - core/pitfalls/alu-to-ea-bubble.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 10 and 11" +--- + +## Goal + +Speed in codecs (MP3, FLAC) and other fixed-point or bitstream code. + +## Technique + +### Fixed point + +- Keep only the high longword of a 64-bit `muls.l` (Q30/Q31 coefficients). +- For rounding, split a constant c = m + c' with |c'| ≤ 0.5 in Q32. Rounding + then costs `add.l lo,lo` / `addx.l d7,hi` (2 instructions instead of 4) and + stays exact. +- Keep 64-bit sums in a register pair with `add`/`addx`. Plain 68020 + assembler with unrolled blocks and a computed entry beat GCC, which spills + every 64-bit product in multiply-heavy loops. + +### Bitstreams + +- Use `bfextu` at a bit position, and `bfffo` for unary codes. +- Memory bitfields cost the same as register bitfields on the 68080. Keeping + a 32-bit window in a register was slower. +- `bfextu` width 0 means 32: guard it. +- Put the sign in the table row index: it removes a dependent smi/eor/sub + chain. +- Read table fields as bytes with `moveq #0` + `move.b` (a fused zero-extending + load), and write both outputs adjacent (a fused quad store). + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Huffman pair, sign handled with smi/eor/sub | 34.6 cycles per pair | Core 12841 | CCC, MP3 decode (M) | +| Sign in the table row index | 22 cycles per pair | Core 12841 | Same | +| Plus byte fields with fused movz and adjacent outputs | 15.8 cycles per pair | Core 12841 | Same | +| A 256-byte lookup table instead of `bfffo` for Rice unary codes | Slower | Core 12841 | FLAC decode (M) | + +Results in context: MP3 decode went from 23.2 % to 15.7 % of real time at +258 kbps, FLAC 96/24 decode from 30.3 % to about 12–14 % CPU (M, V4 at +99 MHz). + +## When not to use + +- `bfffo` costs 3 cycles and blocks both pipes; `bfextu` runs in pipe 1 + only. In an offset chain they add latency (4 and 6 cycles): see + [multiply-and-bfffo-block-both-pipes.md](../pitfalls/multiply-and-bfffo-block-both-pipes.md). +- A bit position that feeds the next address hits the ALU-to-EA bubble: + `bfextu` → `move.b 2(a1,d5*4)` → `add` took 7.4 cycles per link, 9.1 when + the bit position advances. diff --git a/core/best-practices/fpu-schedule-by-completion-slot.md b/core/best-practices/fpu-schedule-by-completion-slot.md new file mode 100644 index 0000000..9ae62d0 --- /dev/null +++ b/core/best-practices/fpu-schedule-by-completion-slot.md @@ -0,0 +1,64 @@ +--- +title: Schedule FPU code by completion slot +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fpu, scheduling, mac, latency] +related: + - core/cpu-68080/fpu-performance-model.md + - core/best-practices/fpu-when-it-pays.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rules 8-9) and 4.1" +--- + +## Goal + +Speed: one FPU instruction per cycle. The FPU is pipelined, but it has one +result-write port and needs enough independent chains in flight. + +## Technique + +- Keep at least 5 independent chains for fmul and 6 for fadd (their + latencies). +- Schedule by completion slot (issue cycle + latency): two instructions that + would finish in the same cycle collide, and the second stalls. + Alternating fmul/fadd costs 1.5 cycles per instruction. +- Mix in loads and stores: `fmove.s`/`.d` stores write no FP register and + never collide; patterns LMA, ALM and MAL run at 1.0 cycle per instruction. +- For a multiply-accumulate, use this term order: + +```asm + fmove.s (a0)+,fp1 ; coefficient + fmul.s (a1)+,fp1 ; times sample + fadd.x fp1,fp0 ; accumulate +``` + +- Let integer work run alongside: integer instructions pair with FPU + instructions in pipe 2 for free. + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| MAC term, best order (above) | 3.13 cycles per term | Core 12841 | CCC, 25 patterns fitted (M) | +| MAC term, other orders | 3.5–4.5 | Core 12841 | Same | +| MAC term with an integer operand (`fmove.l`/`fmul.l`) | 4.0–5.1 | Core 12841 | Same | +| Integer MAC (`muls.l` + add/addx) | 4.27 (floor 4.0) | Core 12841 | Same | +| 24 fmul + 24 add.l | 26 cycles | Core 12841 | Same | + +Gunnar's rule (G): a pipelined unit that shows more than 1 cycle per +instruction has a wrong schedule. + +## When not to use + +- A greedy list scheduler on the FPU model did worse than a hand-designed + modulo rhythm (2222 vs 1644 cycles, M). Use the model to check a schedule, + not only to search for one. +- With only 8 FP registers and 2-address code, wide schedules spill: a + 32-point DCT on the FPU lost to integer code. diff --git a/core/best-practices/fpu-when-it-pays.md b/core/best-practices/fpu-when-it-pays.md new file mode 100644 index 0000000..3c68d46 --- /dev/null +++ b/core/best-practices/fpu-when-it-pays.md @@ -0,0 +1,59 @@ +--- +title: Use the FPU when results stay floating point +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fpu, integer, dsp, audio, mac] +related: + - core/best-practices/fpu-schedule-by-completion-slot.md + - core/pitfalls/fpu-float-to-integer-conversion.md + - core/pitfalls/fpu-47-bit-context-save.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 4.3 and 12" +--- + +## Goal + +Speed in DSP-style code (filters, synthesis windows, prediction): a +well-scheduled float MAC beats the integer one, as long as the values do not +have to become integers often. + +## Technique + +- Move multiply-accumulate loops to the FPU when the inputs can be stored as + floats (coefficient streams as `.s`) and the output stays float, or becomes + an integer only once per many operations. +- Use doubles for exact integer arithmetic only up to 47 significant bits + (see [fpu-47-bit-context-save.md](../pitfalls/fpu-47-bit-context-save.md)). +- For 16-bit output, convert with `fmove.w` straight from the FPU: it + saturates and follows the rounding mode + (see [fpu-float-to-integer-conversion.md](../pitfalls/fpu-float-to-integer-conversion.md)). + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| MP3 synthesis window (512 terms), integer `muls.l` | 2185 cycles (4.27 per term) | Core 12841 | CCC (M) | +| Same, FPU with float coefficient streams | 1729 cycles; full decode 5 % faster, quality within dither | Core 12841 | Same | +| FLAC LPC order 8, integer | 46 cycles per sample | Core 12841 | Same | +| FLAC LPC order 8, FPU doubles (values ≤ 44 bits) | 35.5 cycles per sample; full decode 10 % faster | Core 12841 | Same | + +## When not to use + +- When every output must become an integer: each FP-to-integer conversion + costs 3 cycles and blocks both pipes. That rules out IMDCT outputs. +- A 32-point DCT gains at most about 1.7 %: 2-address code and 8 registers + force about 100 spills. +- A hybrid integer + FPU window was worse than either: each FPU term costs + about as much as an integer term once operands come from memory, plus about + 4 cycles of conversion per sample. +- Interleaving float-to-integer conversion with FPU instructions that use + `(An)+` operands cost 320–440 extra cycles: they did not pair for free. +- Converting through the `1.5*2^52` magic number: +250 cycles, and unsafe + because of the 47-bit context save. diff --git a/core/best-practices/interleave-dependency-chains.md b/core/best-practices/interleave-dependency-chains.md new file mode 100644 index 0000000..b2e2fd3 --- /dev/null +++ b/core/best-practices/interleave-dependency-chains.md @@ -0,0 +1,59 @@ +--- +title: Interleave two independent dependency chains +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, superscalar, scheduling, crc, sha256] +related: + - core/cpu-68080/superscalar-issue.md + - core/cpu-68080/pipeline-15-stages.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 6) and 11" +--- + +## Goal + +Speed: keep both pipes busy. A group of instructions only issues when all its +input registers are ready, so a single dependency chain leaves the second +pipe idle. + +## Technique + +Split the work into two independent chains (two blocks, two halves of a +buffer, two lanes of a hash) and interleave their instructions, so that each +instruction pairs with one from the other chain. + +```asm +; Two independent accumulators instead of one chain +loop: + move.l (a0)+,d1 ; chain A + move.l (a1)+,d2 ; chain B + add.l d1,d3 ; chain A + add.l d2,d4 ; chain B + dbra d0,loop + add.l d4,d3 ; combine at the end +``` + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| CRC, one chain | 178 cycles | Core 12841 | CCC counter, trained loop (M) | +| CRC, two interleaved chains | 89 cycles | Core 12841 | Same | +| SHA-256, one chain | 660 cycles | Core 12841 | Same | +| SHA-256, two interleaved chains | 464 cycles | Core 12841 | Same | + +This was the biggest single win in the guide's projects. + +## When not to use + +- When the loop is already issue-bound (every slot filled), software + pipelining or interleaving gains nothing: it did not help BlitTemplate, + Huffman or Rice decoding (M). +- When the second chain needs extra registers that force spills to memory. diff --git a/core/best-practices/memory-access-patterns.md b/core/best-practices/memory-access-patterns.md new file mode 100644 index 0000000..e98b21e --- /dev/null +++ b/core/best-practices/memory-access-patterns.md @@ -0,0 +1,54 @@ +--- +title: Read memory densely and in order; keep random-access data small +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, cache, prefetch, tables, random-access] +related: + - core/cpu-68080/caches-and-memory.md + - core/pitfalls/touch-misuse.md + - core/pitfalls/alu-to-ea-bubble.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 16), 5.2 and 12" +--- + +## Goal + +Memory throughput: sequential reads are prefetched and cost almost nothing +extra; every random cache miss costs about 27 cycles, and misses do not +overlap. + +## Technique + +- Read every word of the data, in address order, through `(An)+`. +- Put data that is used together in the same 64-byte line. +- Keep lookup tables small and hot. Avoid pointer chasing over large data. +- Avoid rows exactly 64 KB apart: they alias in the cache. +- Use `touch d8(An,Dn)` 12–15 cycles ahead only for random accesses, never + on a sequential stream. +- Don't keep a loop counter or accumulator in memory with read-modify-write: + store-to-load forwarding works, but it serialises. + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| Cold sequential read of every long | 10.2 cycles per 64-byte line | Core 12841 | CCC (M) | +| Warm sequential read of every long | 9.1 cycles per line | Core 12841 | Same | +| One read per 64-byte line | 14.4 cycles per line | Core 12841 | Same | +| One read per 128 bytes | 27.2 cycles | Core 12841 | Same | +| Random miss (dependent load) | About 30 cycles total, 27 more than a hit | Core 12841 | Pointer chase over 4 MB | +| Rows 64 KB apart | About 10 % slower | Core 12841 | Same | +| A 256-byte lookup table instead of `bfffo` for Rice codes | Slower | Core 12841 | Real decode | + +## When not to use + +- A hot-cache microbenchmark of table-driven code does not predict the real + program: confirm on the real workload, three times (real-workload noise is + about ±10 %). diff --git a/core/best-practices/predicated-bcc-over-one-instruction.md b/core/best-practices/predicated-bcc-over-one-instruction.md new file mode 100644 index 0000000..e9d09f5 --- /dev/null +++ b/core/best-practices/predicated-bcc-over-one-instruction.md @@ -0,0 +1,58 @@ +--- +title: Use bcc.s over one instruction as a conditional instruction +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, branches, predication, conditional] +related: + - core/cpu-68080/branches-and-prediction.md + - core/cpu-68080/prm/optimizing.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 5) and 2.6" +--- + +## Goal + +Speed: a conditional operation that is never mispredicted, at the cost of a +correctly predicted branch. + +## Technique + +Branch with `bcc.s` over exactly one instruction. The 68080 executes the pair +as a conditional instruction when the skipped instruction: + +- is at most 8 bytes long; +- is single-cycle; +- does not change an An (so not `(An)+`, `-(An)`, `addq An` or `lea`). + +```asm + cmp.l d1,d0 + bge.s .skip + move.l d1,d0 ; executed only when d0 < d1 +.skip: +``` + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| `beq.s`/`beq.w` over 2 instructions, random pattern | 7.0 cycles per turn | Core 12841 | 65536 branches, CCC, median of 7 (M) | +| `beq.w`/`beq.s` over 2 instructions, alternating | 10.5 | Core 12841 | Same | +| `beq.s` over 1 instruction, any pattern | 3.0 | Core 12841 | Same | +| No branch | 2.0 | Core 12841 | Same | + +## When not to use + +- When the skipped code is more than one instruction, or changes an An: then + it is an ordinary branch and can be mispredicted. +- Don't insert other instructions between the `bcc.s` and the skipped + instruction when you interleave code. An FPU instruction placed there made + the branch land inside it ("CHK instruction error") (M). +- Don't replace the pattern with Scc + mask: the predicated branch is as fast + and simpler. diff --git a/core/best-practices/use-fusable-pairs.md b/core/best-practices/use-fusable-pairs.md new file mode 100644 index 0000000..a31e210 --- /dev/null +++ b/core/best-practices/use-fusable-pairs.md @@ -0,0 +1,60 @@ +--- +title: Write fusable instruction pairs back to back +type: best-practice +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fusing, 3-operand, quad-move] +related: + - core/cpu-68080/prm/instruction-fusing.md + - core/cpu-68080/superscalar-issue.md + - core/pitfalls/fused-copy-overlap.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 2.4" +--- + +## Goal + +Speed: two instructions that fuse behave as one. A copy plus an operation +becomes one 3-operand instruction; two long moves become one quad move. + +## Technique + +Write the pair adjacent, in the order of the PRM's fusing table +([prm/instruction-fusing.md](../cpu-68080/prm/instruction-fusing.md)). +Write the copy and then the operation naturally, without trying to avoid the +copy. + +```asm + move.l d0,d2 ; copy ... + add.l d1,d2 ; ... and operate: fuses to add.l d1,d0,d2 + + move.l (a0)+,(a1)+ ; two long moves ... + move.l (a0)+,(a1)+ ; ... fuse to one quad move + + moveq #0,d3 + move.b (a2),d3 ; fuses to a zero-extending byte load +``` + +## Evidence + +| Variant | Measurement | Core | Test setup | +|---|---|---|---| +| 16 x (`move.l dn,dm` + `add.l`), split apart | 9.1 cycles | Core 12841 | CCC (M) | +| Same, adjacent | 5.1 cycles | Core 12841 | Same | +| Same with E registers, adjacent | 1 pair per cycle | Core 12841 | Same | +| `fmove.x` + `fmul.x`, independent pairs, split / adjacent | 2.25 / 1.12 cycles per pair | Core 12841 | fpu3 test (M) | + +## When not to use + +- Fused `move.l (an)+,(am)+` pairs read 8 bytes before writing: never use + them for a forward copy whose destination is less than 8 bytes ahead of the + source. See [fused-copy-overlap.md](../pitfalls/fused-copy-overlap.md). +- `subq.l #1,dn` + `bpl` does not fuse; `fsub` does not fuse. +- Fused FPU pairs count as 2 instructions in IEP1/IEP2: measure them with + CCC. diff --git a/core/cpu-68080/bank-prefix.md b/core/cpu-68080/bank-prefix.md new file mode 100644 index 0000000..d3b3ae7 --- /dev/null +++ b/core/cpu-68080/bank-prefix.md @@ -0,0 +1,100 @@ +--- +title: 68080 BANK prefix and register naming (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, bank, e-registers, b-registers, encoding] +related: + - core/cpu-68080/prm/integer/bank.md + - core/cpu-68080/prm/b-registers.md + - core/pitfalls/bank-prefix-pitfalls.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 7.1" +--- + +## Summary + +The BANK prefix (`$71xx`) gives legacy instructions access to E0–E23. On a +single-register operand both bank fields must be set. Banking works for a +list of instructions and is still wrong for another list. + +## Details + +### Register naming + +- Team naming (G): D0–D31 and A0–A15, where E0–E7 = D8–D15, E8–E15 = + D16–D23, E16–E23 = D24–D31, and B0–B7 = A8–A15. +- vasm still wants `e0`–`e23` and `b0`–`b7`. + +### Encoding + +- BANK prefix `$71xx`, layout `0111 ccc1 SSCC AABB`. +- AA banks the register **source**, BB the register **destination**: + `01` = E0–E7, `10` = E8–E15, `11` = E16–E23. +- `$71xx` with SS ≠ 0 is illegal. +- BANK runs in pipe 2 at full speed and fuses. It counts as one instruction + in IEP1/IEP2 (M). + +### Single register operand + +For instructions with one register operand (tst, clr, neg, not, ext, swap, +scc, bitfields, the dbcc counter) and for `#imm,Rn`: **AA must equal BB**, +for example `$7105`, `$710A`, `$710F` (official, M). + +- AA alone: the plain Dn is used. +- BB alone: looks right on a cold pass, but in a trained loop pipe 2 reads the + plain Dn (M). + +### What works and what does not (Core 12830–12841, M) + +| Banking works | Banking does not work yet | +|---|---| +| move, moveq, addi, subi, eor, eori, ori, andi, not, neg, negx, btst, tst, bclr, clr, bset, bchg, swap, scc, bfclr, bfset, bftst, bfchg, bfffo, ext, extu, cmpi, shifts and rotates (a banked shift count too), moviw.l, perm | add, sub, and, or, cmp, addx, subx, mul, div, abcd, sbcd, pack, unpk, exg, bfextu, bfexts, bfins | + +Examples of the failures: + +- `add.l e6,d1` computed d6 + d6. +- `exg Dn,En` writes En, but Dn gets the plain register. +- `cmp.l En,Dn` (`$7104`, AA = source) uses the plain Dn on the first two + passes and the right En from the third pass on (Core 12841). +- `add.l #imm,En` works with AA = BB (`$7105`, `$D2BC`) on every pass. + +### B registers + +- Supported forms: `movea.l ,Bn`, `move.l Bn,` (ea not An/Bn), + `lea ,Bn`, `lea (Bn),An`, `addq/subq #,Bn` (CCR unchanged) and + `cmp.l Bn,Dn`. B registers are fully usable as AMMX address registers. +- On Core 12841 all these forms give the right result on every pass of a + 5-pass loop, including `addq` after `movea.l` or `lea`, and AMMX + `load (Bn)` (M). +- `add.l Bn,Dn` and `move.l An,Bn` are not supported. + +### Task switches + +E and B registers survive task switches only with the Apollo bit set (SR +bit 11, per program) or under ApolloOS. See +[prm/apollo-bit.md](prm/apollo-bit.md). + +### FPU + +The team has decided: **no BANK prefix on FPU instructions** in future cores +(G, 2026-10-26). A new FPU encoding with 32 FP registers is planned. + +## Differences from AmigaOS / AROS / 68060 + +- The 68000–68060 have no BANK prefix, no E and no B registers. +- The PRM's BANK page says AA = BB for single operands; vasm 2.0f/2.0g does + not follow that. See [bank-prefix-pitfalls.md](../pitfalls/bank-prefix-pitfalls.md). + +## Do / Don't + +- DO set AA = BB for single-register operands and `#imm,Rn`. +- DON'T bank the instructions in the "does not work yet" column. Do the + arithmetic in a plain Dn. +- DON'T put a BANK prefix on FPU instructions in new code. diff --git a/core/cpu-68080/branches-and-prediction.md b/core/cpu-68080/branches-and-prediction.md new file mode 100644 index 0000000..d39f7d2 --- /dev/null +++ b/core/cpu-68080/branches-and-prediction.md @@ -0,0 +1,101 @@ +--- +title: 68080 branch costs and prediction (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, branches, prediction, btc, rts, predication] +related: + - core/best-practices/predicated-bcc-over-one-instruction.md + - core/best-practices/close-loops-with-dbra.md + - core/pitfalls/alternating-branch-mispredicted.md + - core/pitfalls/return-address-patching.md + - core/cpu-68080/prm/integer/bcc-s-plus.md + - core/cpu-68080/prm/integer/dbcc-l.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 2.6" +--- + +## Summary + +A mispredicted Bcc costs about 7 cycles, a mispredicted RTS about 9.5. The +predictor does not learn a strict alternation. A `bcc.s` over one +single-cycle instruction is executed as a conditional instruction and is +never mispredicted. + +## Details + +### Costs + +Test: forward `beq` over 2 instructions, flag from `tst.b (a0)+` on a table, +65536 branches, CCC counter, median of 7 (M, Core 12841, 2026-10-29). + +| Direction pattern | `beq.w` / `beq.s` over 2 instructions | `beq.s` over 1 instruction | No branch | +|---|---|---|---| +| Always taken | 3.0 cycles per turn | 3.0 | 2.0 | +| Never taken | 4.0 | 3.0 | 2.0 | +| Random (50 %) | 7.0 (+3.5 = half of 7) | 3.0 | 2.0 | +| Alternating T/N/T/N | **10.5** (+7: every one mispredicted) | 3.0 | 2.0 | + +- `.w` and `.s` cost the same here. +- A misprediction is detected after the ALU stage (G); a mispredicted RTS is + caught there too. +- **Mispredicted RTS: about 9.5 cycles.** A `bsr` + `rts` turn took 4.6 + cycles normally and 14.0 when the routine moved its return address + (`addq.l #2,(sp)`) (M). +- An inner `dbra` loop of random length 1–4 cost no more than the average of + the fixed lengths. But each extra turn of such a tiny loop cost 3 cycles, + and one turn about 12 (M). +- `rtd` used as a jump cost about 45 cycles (M). +- Computed and indirect jumps mispredict more. A table of `bra.s` through + `jmp (table,pc,dn.w)`, or an address read from memory, is predicted; + compute the index early. + +### Predication + +A `bcc.s` over **one** instruction is executed as a conditional instruction +when the skipped instruction: + +- is at most 8 bytes long; +- is single-cycle; +- does not change an An. So `(An)+`, `-(An)`, `addq An` and `lea` are not + predicated. + +Measured: 3.0 cycles whatever the pattern, never mispredicted (M). + +### 080 branch sizes + +- `bcc.s+`: an odd displacement byte gives +128..+254 and −256..−132. −130 is + impossible (`$FF` means `.l`). Byte = displacement − 127 forward, + + 129 backward. +- `dbcc.l`: an odd displacement word means a 32-bit counter (displacement + word = displacement + 1). + +## Differences from AmigaOS / AROS / 68060 + +- A loop closed with `bne.w` is not accelerated on the 68080. Close loops + with `dbra` or a fused `subq.l #1,Dn` + `bne.s`: loop overhead goes from 3 + cycles to 1 (M). Do not carry over 68060 loop-closing habits unmeasured. +- A `bcc.s` over one instruction becoming a conditional instruction is 68080 + specific. +- The return cache makes RTS cheap only when the return address is not + changed. + +## Do / Don't + +- DO leave a `bcc.s` over one single-cycle instruction alone: do not replace + it with Scc + mask. +- DO keep a `bcc.s *+4` and its skipped instruction adjacent when you + interleave code. An FPU instruction inserted between them made the branch + land inside it ("CHK instruction error"). +- DON'T write branches that flip every iteration: unroll by 2, or compute the + value without a branch. +- DON'T patch return addresses, `pea` + `rts`, or use `rtd` as a jump. To fall + back to an original routine, use `jmp ([ptr,pc])`, which is predicted. +- DO special-case very short loops (1–4 turns) and pull small cases out of + size-dispatch code: benchmark with random sizes. diff --git a/core/cpu-68080/caches-and-memory.md b/core/cpu-68080/caches-and-memory.md new file mode 100644 index 0000000..d054100 --- /dev/null +++ b/core/cpu-68080/caches-and-memory.md @@ -0,0 +1,101 @@ +--- +title: 68080 caches and memory access (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, cache, line-size, prefetch, alignment, miss] +related: + - core/cpu-68080/prm/specs.md + - core/best-practices/memory-access-patterns.md + - core/best-practices/align-copy-destination.md + - core/pitfalls/self-modifying-code-and-caches.md + - core/pitfalls/touch-misuse.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 5.1 and 5.2" +--- + +## Summary + +Cache lines are 64 bytes. The data cache serves one 8-byte access per cycle. +Sequential reads are prefetched and nearly as fast as warm data; a random +miss costs about 27 extra cycles and misses do not overlap. Misaligned reads +are nearly free; misaligned writes are not. + +## Details + +### Line size + +Measured with a random-order pointer chase over 4 MB, 8192 blocks, CCC +counter, median of 7, two identical runs (M, Core 12841, 2026-10-29): + +| Block | 1st read (offset 0) | 2nd read (offset block/2) | Means | +|---|---|---|---| +| 32 B | 30.3 cycles | +3.0 (hit) | Same line | +| 64 B | 30.6 | +3.0 (hit) | Same line: **the line is 64 bytes** | +| 128 B | 30.6 | +16.5 | Next line, already partly on its way | +| 256 B | 30.6 | +29.4 | Full miss | + +- A miss on one line seems to bring the next 64-byte line in early: reading + it right after cost about half a miss. +- Two lines 128 bytes or more apart are fully independent misses. +- Older notes (Core 12798–12824) gave "sequential about 2 cycles per line, + random about 60". They assumed 32-byte lines and their method is lost; the + figures here replace them. + +### Streaming and random access + +- Reading every long of cold memory: about 10.2 cycles per line, nearly the + warm speed (9.1). +- One long per line: 14.4 cycles per line. One per 128 bytes: 27.2. +- Random access: about 27 cycles per new line, and misses do not overlap. +- Aliasing: rows exactly 64 KB apart cost about 10 %, and 8 lookups 64 KB + apart gave 8 misses. +- Store-to-load forwarding works, but a counter kept in memory with + read-modify-write in a loop serialises. + +### Alignment + +Copy of a 156 x 2464-byte shape (M): + +| Method | Aligned | Misaligned | +|---|---|---| +| AMMX `load`/`store` | 3.2–3.5 bytes/cycle (8-byte aligned) | About 1.8 | +| `move16` | 3.1–3.2 (16-byte aligned) | About 1.75 | +| `move.l` loop | 1.7–2.0 | | +| AMMX `storem` (masked) | About 1.85 cycles per quad, whatever the alignment | | + +- Alignment is not needed for correctness: misaligned reads that hit the + cache are free (official). +- A misaligned **destination** costs 1.7–1.8x; a misaligned source only + 0–13 %. +- Copies are memory-bound at about 3 bytes per cycle: optimising a blit copy + gained only 3–10 %. +- A `.b`/`.w` write into a register also reads its old value: an extra + dependency. Prefer `.l` or quad register updates. + +### Cache control + +- `CacheClearU`/`CacheClearE` do nothing on the 68080 under AmigaOS; see + [self-modifying-code-and-caches.md](../pitfalls/self-modifying-code-and-caches.md). +- Evicting the D-cache takes 128–256 KB of reads. + +## Differences from AmigaOS / AROS / 68060 + +- 64-byte lines instead of 16 bytes on the 68040/68060. Data used together + should sit in the same 64-byte line. +- On the 68080 a misaligned read that hits the cache is free; only the write + side needs alignment for speed. + +## Do / Don't + +- DO align the destination of copies and fills. +- DO read memory densely and in address order. +- DON'T chase pointers over large data, or use big lookup tables read at + random. +- DON'T keep a loop counter in memory. diff --git a/core/cpu-68080/core-at-a-glance.md b/core/cpu-68080/core-at-a-glance.md new file mode 100644 index 0000000..cc04aed --- /dev/null +++ b/core/cpu-68080/core-at-a-glance.md @@ -0,0 +1,61 @@ +--- +title: 68080 core at a glance (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12798-12847" + os: [any] +tags: [68080, pipes, caches, clock, attnflags, overview] +related: + - core/cpu-68080/pipeline-15-stages.md + - core/cpu-68080/superscalar-issue.md + - core/cpu-68080/caches-and-memory.md + - core/cpu-68080/prm/specs.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 1" +--- + +## Summary + +The key numbers of the 68080 as measured on V4 cores 12798 to 12847. Where +they differ from the PRM, the measured value or the core developer's +statement wins. + +## Details + +| Item | Value | +|---|---| +| Clock (V4) | 14 x 7.09379 MHz = 99.31 MHz. The PAL colour clock of 3546895 Hz drives the Paula/ARNE periods | +| Pipeline | The PRM summarizes it in 7 steps (I-fetch, decode, register fetch, EA, D-cache, ALU, write back). The real depth is 15 stages, see [pipeline-15-stages.md](pipeline-15-stages.md) | +| Pipes | PRM: 3 (Anton, Bruno, Cesar). **2 enabled on Core 12798-12841** (M): IEP3 always reads 0. A third pipe comes in a future core (G) | +| Decoders | 3. Only the first takes multi-cycle instructions (mul, div, movem, bitfields, FPU) and addx/subx/negx, which need the flags of the previous instruction | +| I-cache | 32 KB, **64-byte line** (G, 2026-10-29), 32 bytes fetched per cycle. Few ways on Core 12841 (G); the number of ways is a build option: 4, 2 or 1 (G, 2026-10-30). Note the core build with every measurement | +| D-cache | 128 KB, **64-byte line** (G, 2026-10-29; M). 3-ported, but in practice **1 access of 8 bytes per cycle**: 1 read, or 1 write, or 1 read-modify-write | +| Bus and memory | 64-bit, 64-byte bursts (one cache line), about 12 cycles latency, automatic prefetch of sequential streams | +| Branch cache | 1024-entry BTC, a loop predictor, and a return cache for rts/rtd/rtr | +| MMU | None. PMMU and cp4–cp6 opcodes are illegal (`lpstop` remains) | +| FPU | 64-bit (double) internally, pipelined. Microcoded functions (trig, log, exp, fmod, frem, fscale, fgetexp/fgetman) take 100–200+ cycles and clobber E23 | +| AttnFlags | `$847f` with the FPU on, `$040f` with it off. **Bit 10 = 68080** | +| Context frames | AMMX 256 bytes, FPU 116 bytes | + +Older notes and processor.resource up to version 44.6 give a 32-byte D-cache +line; 44.7 gives 64 bytes, which matches the measurement. + +## Differences from AmigaOS / AROS / 68060 + +- The 68060 has two pipes and an MMU. Released V4 cores also run two pipes, + but have no MMU: PMMU instructions are illegal. +- The cache lines are 64 bytes, not 16 as on the 68040/68060. +- 68080 detection uses AttnFlags bit 10, which classic AmigaOS does not + define. + +## Do / Don't + +- DO gate 68080 code paths on AttnFlags bit 10, and FPU paths on the 68881 + flag at run time: the FPU can be switched off. +- DON'T assume three pipes because the PRM says so: measure IEP3 on your core. +- DON'T use MMU instructions on the 68080. diff --git a/core/cpu-68080/core-bugs-by-revision.md b/core/cpu-68080/core-bugs-by-revision.md new file mode 100644 index 0000000..bce249f --- /dev/null +++ b/core/cpu-68080/core-bugs-by-revision.md @@ -0,0 +1,65 @@ +--- +title: 68080 core bugs by core revision +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080, saga-3d-maggie] +applies_to: + core: "Core 12001-12847" + os: [any] +tags: [68080, bugs, core-revision, regression] +related: + - core/pitfalls/fpu-broken-instructions.md + - core/pitfalls/ammx-pc-relative-source.md + - core/pitfalls/ammx-upper-32-bits.md + - core/pitfalls/bank-prefix-pitfalls.md + - core/pitfalls/tex-does-not-match-prm.md + - core/pitfalls/maggie-depth-test-colour-write.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 4.2, 6, 7.3 and 15.7" +--- + +## Summary + +Core bugs seen on V4 cores 12001 to 12847, and which are fixed. Each open bug +has its own pitfall file. Check this list against your core: a new core can +fix or add bugs. + +## Details + +### Fixed + +| Bug | Fixed in | +|---|---| +| `cmp.l Bn,Dn` ignored Bn | Core 12840 | +| `move En,Dn` in pipe 2 paired with a pipe-1 instruction writing the same register | Fixed (core not recorded) | +| `FDBcc` never decremented and always branched (Core 12830/12840) | Core 12841 | +| `move2.l (An),Dx:Dy` loaded the second register wrong (Core 12001) | Checked fixed on Core 12841 | +| `addq.l #n,Bn` added to a hidden register (Core 12001) | Checked fixed on Core 12841 | +| `lea (Bn),An`, AMMX `(Bn)` wrong (Core 12001) | Checked fixed on Core 12841 | + +### Still wrong on Core 12841 + +| Area | Bug | Pitfall | +|---|---|---| +| FPU | FScc to `d16(An)`/`d8(An,Xn)`, FTRAPcc, `flognp1`, dynamic `fmovem` list, `fmove.x` memory operands | [fpu-broken-instructions.md](../pitfalls/fpu-broken-instructions.md) | +| FPU | Transcendental, `fdiv` and `fsqrt` precision; `fadd` rounds toward zero; FPCR precision ignored; `fint` ties | [fpu-precision-and-rounding.md](../pitfalls/fpu-precision-and-rounding.md) | +| FPU | FPSR exception bytes and traps never set | [fpu-exceptions-never-trap.md](../pitfalls/fpu-exceptions-never-trap.md) | +| AMMX | PC-relative sources read 2 bytes early | [ammx-pc-relative-source.md](../pitfalls/ammx-pc-relative-source.md) | +| AMMX | `move.l #imm` clears the upper 32 bits on trained passes | [ammx-upper-32-bits.md](../pitfalls/ammx-upper-32-bits.md) | +| BANK | `cmp.l En,Dn` uses the plain Dn on cold passes | [bank-prefix-pitfalls.md](../pitfalls/bank-prefix-pitfalls.md) | +| TEX | Does not match the PRM's byte form (Core 12846) | [tex-does-not-match-prm.md](../pitfalls/tex-does-not-match-prm.md) | +| Maggie | Depth test does not gate the colour write; the last 8 bytes of a span stay in the unit (Core 12847) | [maggie-depth-test-colour-write.md](../pitfalls/maggie-depth-test-colour-write.md), [maggie-span-tail.md](../pitfalls/maggie-span-tail.md) | + +## Differences from AmigaOS / AROS / 68060 + +These are 68080 core bugs. The same code runs correctly on a 68060; do not +work around them on other CPUs. + +## Do / Don't + +- DO note the core revision with every measurement and bug report. +- DO recheck this list when you move to a new core. +- DON'T rely on a fix from a core you have not tested on. diff --git a/core/cpu-68080/fpu-performance-model.md b/core/cpu-68080/fpu-performance-model.md new file mode 100644 index 0000000..adc590d --- /dev/null +++ b/core/cpu-68080/fpu-performance-model.md @@ -0,0 +1,69 @@ +--- +title: 68080 FPU performance model (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fpu, latency, throughput, scheduling, mac] +related: + - core/cpu-68080/prm/fpu-unit.md + - core/best-practices/fpu-schedule-by-completion-slot.md + - core/best-practices/fpu-when-it-pays.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 4.1" +--- + +## Summary + +The FPU issues in order, one instruction per cycle, in pipe 1 only, and has +one result-write port. fmul has latency 5, fadd/fsub 6. The model below +predicted all 25 measured patterns exactly. + +## Details + +- Issue is in order, 1 FPU instruction per cycle, pipe 1 only. Integer + instructions pair in pipe 2 for free: 24 fmul + 24 add.l = 26 cycles (M). +- **One FP register write per cycle.** An instruction whose completion cycle + (issue + latency) is already taken stalls. +- Latencies: fmul 5, fadd/fsub 6, fmove load 1, fmove.x copy 1. `fmove.s`/`.d` + stores write no FP register and never collide. +- So the mix matters, not only the dependencies. Alternating fmul/fadd costs + 1.5 cycles per instruction. +- Measured patterns (L = load, M = mul, A = add), cycles per instruction: + + | Pattern | Cycles | + |---|---| + | MMA | 1.33 | + | LA | 1.17 | + | LMA, ALM, MAL | 1.0 | + | AML, MLA | 1.33 | + +- Best multiply-accumulate term order: `fmove.s coef,fpT` / `fmul.s mem,fpT` / + `fadd fpT,acc` = **3.13 cycles per term**. Other orders cost 3.5–4.5. An + integer operand (`fmove.l` / `fmul.l`) costs 4.0–5.1. +- The float MAC floor is 3 FPU instructions per term (about 33 M MAC/s at + 99 MHz). The integer MAC (`muls.l` + add/addx) measured 4.27 per term, with + a floor of 4.0. +- Gunnar's rule (G): **a pipelined unit showing more than 1 cycle per + instruction means the schedule is wrong.** + +## Differences from AmigaOS / AROS / 68060 + +- The 68881/68882 are not pipelined. The 68080 FPU accepts one instruction + per cycle, but only if enough independent chains are in flight (at least + 5 for fmul, 6 for fadd). +- The single write port is 68080 specific: instructions with different + latencies can collide even when they are independent. + +## Do / Don't + +- DO benchmark latency with 1 chain and throughput with at least "latency" + chains, and report both. +- DO schedule by completion slot, not only by dependency. +- DON'T expect FPU instructions to pair with each other: only integer + instructions pair with them. diff --git a/core/cpu-68080/index.md b/core/cpu-68080/index.md index 39e4e0d..10f1a6d 100644 --- a/core/cpu-68080/index.md +++ b/core/cpu-68080/index.md @@ -36,6 +36,25 @@ converted in [prm/](prm/index.md). | Cycle counts, benchmark, optimizing | [prm/instruction-timing.md](prm/instruction-timing.md), [prm/instruction-efficiency.md](prm/instruction-efficiency.md), [prm/optimizing.md](prm/optimizing.md) | | Caches, bus, trace | [prm/specs.md](prm/specs.md), [prm/trace.md](prm/trace.md) | +### Measured on V4 cores (rank above the PRM when they disagree) + +| Topic | File | +|---|---| +| Key numbers: clock, pipes, caches, AttnFlags | [core-at-a-glance.md](core-at-a-glance.md) | +| The real 15-stage pipeline | [pipeline-15-stages.md](pipeline-15-stages.md) | +| Second pipe, pairing, learning, fusing measured | [superscalar-issue.md](superscalar-issue.md) | +| Branch costs, prediction, predication | [branches-and-prediction.md](branches-and-prediction.md) | +| Cycle costs per instruction | [measured-instruction-costs.md](measured-instruction-costs.md) | +| FPU performance model | [fpu-performance-model.md](fpu-performance-model.md) | +| Cache lines, streaming, misses, alignment | [caches-and-memory.md](caches-and-memory.md) | +| BANK prefix, E and B registers | [bank-prefix.md](bank-prefix.md) | +| ISA notes beyond the PRM | [isa-notes-measured.md](isa-notes-measured.md) | +| CCC and IEP counters | [performance-counters.md](performance-counters.md) | +| Core bugs, by core revision | [core-bugs-by-revision.md](core-bugs-by-revision.md) | + +Best practices and pitfalls for the 68080 are in `core/best-practices/` and +`core/pitfalls/`. + The files in `prm/` are generated from the original manual in `sources/official/` and are never edited by hand. Other 68080 content goes next to `prm/` in this folder. @@ -46,7 +65,8 @@ next to `prm/` in this folder. 68080 adds to the 68000 family. For the classic instructions, the Motorola manuals apply. - `differences-from-68060.md`: what a 68060 programmer would wrongly assume, - collected in one place. + collected in one place. Until then, see the "Differences" section of each + file. ### For contributors diff --git a/core/cpu-68080/isa-notes-measured.md b/core/cpu-68080/isa-notes-measured.md new file mode 100644 index 0000000..9f0bbd5 --- /dev/null +++ b/core/cpu-68080/isa-notes-measured.md @@ -0,0 +1,68 @@ +--- +title: 68080 ISA notes beyond the PRM (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, isa, instructions, exceptions, linea] +related: + - core/cpu-68080/prm/index.md + - core/cpu-68080/bank-prefix.md + - core/pitfalls/pc-relative-destination.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 7.2" +--- + +## Summary + +What the 68080 instruction set does in practice, where it adds to or +corrects the PRM: no 64-bit integer arithmetic, no explicit 3-operand +integer form, instructions that do nothing, and 68060-style exception frames. + +## Details + +- **New instructions in the PRM** ([prm/index.md](prm/index.md)): + `addiw.l` (no subiw: use a negative value), `cmpiw.l`, `moviw.l`, + `extub.l`/`extuw.l`, `move2`/`movz2 ,Da:Db` (memory only; movz2 is not + on the V2), `movex`, `moveh`, `perm`, `touch`, `bcc.s+`, `dbcc.l`, `tex`, + `rex`. +- `move2.l (An),Dx:Dy` loads both registers correctly on Core 12841 (on Core + 12001 the second was wrong) (M). +- **LineA instructions** (`clr.q`, `mov3q`, `movs`, `movz`) exist only on + cores 10280–10904 (official): don't use them. Use the fused pairs instead + (`moveq #0` + `move.b`, `move.w` + `ext.l`). +- **abs/min/max/andn** (`$0E81 $20xx`) do nothing and raise no exception + (M). vasm still assembles them. +- **No 64-bit integer arithmetic.** The "quad" add/sub described on the + website does not exist. For 64-bit work, use AMMX or the 64-bit multiply. +- **No explicit 3-operand integer form yet.** You get 3-operand behaviour + through fusing. +- `move sr,` and `movec Rc,Rn` (reads) work in user mode. A `movec` read + does not sync the pipeline; a write waits. +- **PC-relative destination:** PC = the address of the destination's + extension word. It is computed wrongly after a full-extension-format + source, see [pc-relative-destination.md](../pitfalls/pc-relative-destination.md). +- Shift and rotate register counts are taken modulo 64. +- Exceptions use 68060-style frames. For divide by zero and `chk`, the frame + PC points to the next instruction. +- **No emulator implements the 68080.** Only the V2 and V4 run 080 code, so + test 080 code paths on hardware. + +## Differences from AmigaOS / AROS / 68060 + +- The 68000–68060 have no 64-bit integer arithmetic either; the 68080 does + not add it, despite what some web pages suggest. +- abs/min/max/andn are accepted by vasm but have no effect: a silent bug, not + an illegal instruction trap. + +## Do / Don't + +- DON'T use LineA instructions, abs/min/max/andn, or the website's "quad" + add/sub. +- DON'T rely on an emulator to test 68080-specific code. +- DO expect 68060-style exception frames in handlers. diff --git a/core/cpu-68080/measured-instruction-costs.md b/core/cpu-68080/measured-instruction-costs.md new file mode 100644 index 0000000..7d9cdbd --- /dev/null +++ b/core/cpu-68080/measured-instruction-costs.md @@ -0,0 +1,74 @@ +--- +title: 68080 instruction costs measured on Core 12841 +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, timing, cycles, multiply, bitfield, cache] +related: + - core/cpu-68080/prm/instruction-timing.md + - core/cpu-68080/prm/instruction-efficiency.md + - core/cpu-68080/fpu-performance-model.md + - core/pitfalls/multiply-and-bfffo-block-both-pipes.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 3" +--- + +## Summary + +Cycle costs measured with the CPU counters on trained loops. Several are +higher than the PRM states: multiply is about 3 cycles and blocks both pipes, +FP-to-integer conversion 3 cycles, a random cache miss about 27 extra cycles. + +## Details + +Method (M): `movec` CCC/IEP1/IEP2 in user mode, under `Disable()`, trained +loops, best of 5, Core 12841. + +| Operation | Cost | +|---|---| +| Simple ALU/EA operations | Up to 2 per cycle (2 pipes) | +| add/addx pairs (64-bit sums) | About 1 per cycle (addx in pipe 1 only) | +| `muls.l`/`mulu.l` 32 and 64-bit, `muls.w`, `mulu.w` | **3.04–3.13 cycles each, issue-bound** (same with 1 or 7 independent chains). Nothing pairs with it: mul + an independent op = 4.0–4.08. Result usable about 3 cycles later | +| mul + add/addx rounding chain | About 5–6 cycles per product | +| `bfextu` (memory or register) | 1.5 cycles throughput, pipe 1 only, latency 4 in an offset chain | +| `bfffo` (memory or register) | 3 cycles and **blocks both pipes**, latency 6 when it feeds the next offset | +| Mispredicted Bcc / RTS | About 7 / about 9.5 cycles (see [branches-and-prediction.md](branches-and-prediction.md)) | +| `bcc.s` over 1 instruction (predicated) | No penalty, whatever the pattern | +| Dependent indexed loads | About 4 cycles each | +| One memory access per cycle | 16 x `add.l d16(sp),Dn` = 15 cycles | +| Store then reload of the same slot | No extra penalty | +| `move2.l (An)+,d0:d4` in a muls-bound loop | 11 % slower than two `move.l (An)+` | +| fmul / fadd | Latency 5 / 6; 1 per cycle with at least 5 / 6 independent chains | +| FPU source conversion | Free for .s/.d/.x; about +1 latency for integer sizes (.l/.w) | +| `fmove.s`/`.d` FP to memory | 1 per cycle, pairs with integer instructions | +| `fmove.l`/`.w` FP to integer | 3.08 cycles, blocks both pipes | +| `movem` | 1 + n/2 (official) | +| `div` | Under 18 cycles (official). Divide by zero traps unless a NoMoreDiv0 patch is loaded | +| `move16` | 4 cycles, no alignment required (official). Never faster than aligned AMMX or quad moves (M) | +| `ori/andi #$800,sr` (Apollo bit, user mode) | About 8 cycles for the pair | +| D-cache hit (dependent load) | About 3.6–3.7 cycles | +| D-cache miss, random line (dependent load) | About 30 cycles in total, about 27 more than a hit. Independent random misses do not overlap: also 27 extra cycles each | +| Streaming cold reads, every long | 10.2 cycles per 64-byte line (about 6.2 bytes per cycle), about 1.2 cycles more than the same loop on warm data | +| One read per 64-byte line, sequential | 14.4 cycles (the prefetcher helps less with sparse reads) | +| One read per 128 bytes, sequential | 27.2 cycles, almost a random miss | + +## Differences from AmigaOS / AROS / 68060 + +- The PRM gives MUL = 2 or 3 cycles ([prm/instruction-timing.md](prm/instruction-timing.md)). + Measured: 3.04–3.13, and it blocks both pipes. +- The 68060 emulates 64-bit `muls.l`/`mulu.l` in software; the 68080 runs them + in hardware at the same cost as 32-bit. + +## Do / Don't + +- DO count multiply, `bfffo` and FP-to-integer conversion as about 3 cycles + that nothing pairs with. +- DO keep data that is read at random small and hot: every new line costs + about 27 cycles. +- DON'T use `move2` or `move16` for speed without measuring. diff --git a/core/cpu-68080/performance-counters.md b/core/cpu-68080/performance-counters.md new file mode 100644 index 0000000..ca3c545 --- /dev/null +++ b/core/cpu-68080/performance-counters.md @@ -0,0 +1,80 @@ +--- +title: 68080 performance counters +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12830-12841" + os: [any] +tags: [68080, counters, ccc, iep, movec, benchmark] +related: + - core/cpu-68080/prm/integer/movec.md + - core/cpu-68080/prm/optimizing.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 7.3 and 8.1" +--- + +## Summary + +The clock cycle counter (CCC, `$809`) and the per-pipe instruction counters +(IEP1–IEP3) are readable with `movec` in user mode. They are the most +reliable way to time code. The other event counters read 0 on release cores. + +## Details + +### Counters + +| Rc | Name | Use | +|---|---|---| +| `$809` | CCC | Clock cycles | +| `$80A` | IEP1 | Instructions issued in pipe 1 | +| `$80B` | IEP2 | Instructions issued in pipe 2 | +| `$00C` | IEP3 | Instructions issued in pipe 3 (0 on cores with 2 pipes) | +| `$808` | PCR | id `$0440`. Bit 0 = second pipe, bit 1 = FPU off. The core ignores PCR's ESS bit (M) | + +- The debug and event counters (BPC/BPW, DCH/DCM `$80E/$80F`, + STR/STC/STB/MWR) read 0 on release cores (M). +- On Core 12830–12841 the counters are readable with `movec` in user mode. + +### Reading the cycle counter + +```asm + movec $809,a6 ; CCC before + ; ... code to time ... + movec $809,d7 + sub.l a6,d7 ; cycles, stable to less than 1 % +``` + +- Measure under exec `Disable()`. +- For whole-program timing, sum CCC deltas around each call in 64 bits: 32 + bits wrap after about 43 s at 99 MHz. +- CCC was more reliable than EClock for benchmarks (M). +- From GCC inline assembler, use GNU syntax: `.short 0x4e7a,0x0809` + (`movec ccc,d0`), not `dc.w`. + +### Counting pipes + +Run 6 independent `addq`/`subq` + `dbf`, 32001 turns (n instructions): + +- IEP2 < n/16: 1 pipe. +- IEP3 ≥ n/16 and IEP1 + IEP2 < 7/8 n: 3 pipes. +- Otherwise: 2 pipes. + +## Differences from AmigaOS / AROS / 68060 + +- These counters are 68080 specific. `movec` reads of them work in user + mode, unlike classic control registers. +- The PRM lists event counters (BPC, BPW, DCH, DCM, STR, STC, STB, MWR) that + read 0 on release cores. + +## Do / Don't + +- DO time code with CCC, on trained loops, under `Disable()`. +- DON'T measure in supervisor mode with interrupts off: a fault there gurus. +- DON'T write a register the loop uses while under `Disable()`: this hung the + V4 once. +- DO probe an unknown core safely: install a temporary illegal-instruction + trap first. Reading 0 means no counters. diff --git a/core/cpu-68080/pipeline-15-stages.md b/core/cpu-68080/pipeline-15-stages.md new file mode 100644 index 0000000..4ec136e --- /dev/null +++ b/core/cpu-68080/pipeline-15-stages.md @@ -0,0 +1,98 @@ +--- +title: 68080 pipeline, 15 stages +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, pipeline, stages, hazards, latency] +related: + - core/cpu-68080/prm/pipeline.md + - core/pitfalls/alu-to-ea-bubble.md + - core/cpu-68080/superscalar-issue.md +sources: + - "Apollo team diagram \"APOLLO 68080 CPU Architecture\", 2026-10-23" + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 1.1" +--- + +## Summary + +The PRM's 7 pipeline stages are a summary. The Apollo team's architecture +diagram shows 15 stages (L00 to L14). They explain the one memory access per +cycle, the ALU-to-EA bubble, and why FPU and AMMX run in pipe 1 only. + +## Details + +### Stages + +| Stage | What happens there | +|---|---| +| 0 | BTC + PC → I-cache read (can stall) | +| 1 | Mux and rotate the instruction words | +| 2 | 3 decoders in parallel: operation size (decoder 1 can stall the front end) | +| 3 | Pre-mux, sort the EA | +| 4 | Decode the operands | +| 5 | Immediate mux, sort the #imm | +| 6 | **Schedule unit** (can stall): one group of up to 3 instructions goes out | +| 7 | 3 address-register reads | +| 8 | **3 EA units (A, B, C) + one EAMEM**, the single memory address of the cycle | +| 9 | **Hazard unit** (can stall) | +| 10 | 3 data-register reads, **D-cache read**, FP register read, vector register read | +| 11 | **3 ALUs (A, B, C)**, FPU (64-bit), AMMX (drawn as 128-bit: a beta unit not in released cores) | +| 12 | **Write stage** (G): all registers are written here (address, data, FP, vector), plus the **D-cache write**. The diagram labels the address boxes "ADDR REG RE", but they are writes | +| 13 | Write combine | +| 14 | Write FIFO → memory (can stall) | + +The stall points are the I-cache read, decoder 1, the schedule unit, the +hazard unit, the D-cache read and the write FIFO. + +### What the diagram explains + +- **One memory access per cycle.** There is one EAMEM and one D-cache + read/write port, shared by all pipes. Two loads in one cycle are impossible + by design. +- **The ALU-to-EA bubble.** Addresses are computed at stage 8; an ALU result + arrives at stage 11/12. A Dn produced by the ALU and used for an address by + a following instruction arrives 3–4 stages too late (M: write it about 4 + cycles earlier). A load into An used as an address waits the same way + (D-cache read at 10, register write at 12): load → indexed load measured + about 4 cycles (M). See [alu-to-ea-bubble.md](../pitfalls/alu-to-ea-bubble.md). +- **Load then use is cheap.** The D-cache read (10) sits right before the ALU + (11), so a loaded value feeds the ALU without a bubble. +- **FPU and AMMX only in pipe 1.** Only decoder 1 feeds the FPU and AMMX. This + matches the counters: every FPU instruction counts in IEP1 (M). +- **Three ALUs, three EA units.** The hardware for 3 pipes is drawn; Core + 12798–12841 enables 2 (IEP3 = 0, M). +- **Stores are combined.** Writes go through write-combine and a write FIFO, + which fits quad-store fusing and aligned, contiguous writes. +- **Branches are checked after the ALU** (G). There the CPU redirects the PC + when a Bcc or an RTS was mispredicted, and feeds the outcome back to the + BTC. + +### How a group is issued (G, 2026-10-29) + +- Several instructions travel down the pipeline together as one group. +- The schedule stage checks that every register the group uses is ready. If + one is not, the whole group waits. The hazard unit repeats the check before + the ALU. +- So one late register (a load or multiply in flight, or a Dn needed by the + EA stage) holds back every instruction paired with it. Dependency chains and + the ALU-to-EA bubble cost whole cycles, not single slots. + +## Differences from AmigaOS / AROS / 68060 + +- The PRM ([prm/pipeline.md](prm/pipeline.md)) lists 7 stages and a 2-cycle + ALU-to-EA bubble. The diagram and measurements show 15 stages and about 4 + cycles (G says "more than 2"). +- The 68060's pipelines are not a model for the 68080: its EA and ALU stages + and its memory port differ. + +## Do / Don't + +- DO compute addresses at least 4 cycles before use, or keep them in An. +- DON'T plan for two memory accesses in one cycle. +- DON'T expect FPU or AMMX instructions in pipe 2. diff --git a/core/cpu-68080/superscalar-issue.md b/core/cpu-68080/superscalar-issue.md new file mode 100644 index 0000000..9a7bba8 --- /dev/null +++ b/core/cpu-68080/superscalar-issue.md @@ -0,0 +1,109 @@ +--- +title: 68080 superscalar issue and pairing (measured) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, superscalar, pairing, pipes, fusing, learning] +related: + - core/cpu-68080/prm/superscalar.md + - core/cpu-68080/prm/instruction-fusing.md + - core/cpu-68080/pipeline-15-stages.md + - core/best-practices/use-fusable-pairs.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 2.1-2.4" +--- + +## Summary + +On released V4 cores, two pipes issue instructions. The second pipe takes +short single-cycle instructions only, two instructions pair only when they +are independent (or fuse) and need at most one data-cache access, and pairing +only starts on the second pass over the code. + +## Details + +### What can go in the second pipe (G) + +- At most 8 bytes long, BANK prefix included. +- Single-cycle: no mul, div, movem or fmovem. +- Not a system instruction (reset, stop, cache flush, move to/from ccr/sr). +- In practice: move, and, or, add, sub, cmp, ori, andi, subi, eori, cmpi, + btst/bclr/bset/bchg, movea, jsr, jmp, rts, bsr, not, neg, swap, pea, clr, + ext, lea, dbra, bcc, bra, moveq, suba, adda, cmpa, tst and the shifts. +- Never in pipe 2: + - addx, subx, negx (they need the flags from pipe 1, not ready mid-cycle); + - bitfields; + - memory-indirect modes `([bd,An],Xn,od)`; + - all FPU instructions (M) and all AMMX instructions except stores (M). +- `jmp`/`jsr` to a PC-relative or abs.l target go to pipe 2. Through `(An)`, + `d16(An)` or `(d8,An,Xn)` they stay in pipe 1 and cost about +5 cycles (+6 + on Core 12840); `([An])` costs +7 (M). + +### Pairing conditions + +- The two instructions are fully independent, or form a fused pair (see + [prm/instruction-fusing.md](prm/instruction-fusing.md)). +- Together they fit in one 32-byte fetch. +- Together they need at most one data-cache access. +- Measured pairs with an independent `add.l` (M): `smi`, `extb.l`, `ext.l`, + `swap`, `asr/lsr #n`, `rol.l`, `move.w`, `move.b`, `eor`, `tst.b`, + `and.l #imm`, `moveq`, `neg`, `addq`, `cmp.l An,Dn`, indexed loads, + `move.l Dn,(An)+`. +- `nop` is a pipeline barrier. + +### Learning + +- The first time a piece of code runs, it issues on one pipe. From the second + pass of a loop, the other pipe is used. +- Straight-line code that runs once (init code, a single table pass) never + pairs. +- Routines called many times pair fine: 2048 straight `add`s, called + repeatedly, ran at about 1.9 IPC (M). + +### Fusing, measured + +The PRM's fusing table holds, with these measurements (M, Core 12841): + +- 16 instructions of `move.l dn,dm` + `add.l` ran in 5.1 cycles when adjacent + and 9.1 cycles when split apart. The same pair with E registers fused at 1 + per cycle. +- `subq.l #1,dn` + `bne.s/.s+/.w` fuses; `bpl` does not. +- `moveq #0,dn` + `move.b/w (ea),dn` fuses into a zero-extending load. +- FPU: `fmove.x fpn,fpm` + `fmul.x`/`fadd.x fpx,fpm` fuses (registers only) + with correct values and FPSR flags. Cycles per pair, adjacent / split: + + | | Independent pairs | Dependent chain | + |---|---|---| + | fmul | 1.12 / 2.25 | 5.00 / 6.00 (fuses) | + | fadd | 1.12 / 2.62 | 6.00 / 7.00 (fuses) | + | fsub | 2.62 / 2.62 | 7.00 / 7.00 (does not fuse; results still right) | + +- IEP1/IEP2 count a fused FPU pair as 2 instructions: detect FPU fusing with + CCC, not with IEP. +- Two fused `move.l (an)+,(am)+` read 8 bytes before writing. See + [fused-copy-overlap.md](../pitfalls/fused-copy-overlap.md). + +## Differences from AmigaOS / AROS / 68060 + +- The PRM ([prm/superscalar.md](prm/superscalar.md)) describes three pipes + (Anton, Bruno, Cesar). Released cores up to 12841 enable two (M). +- Unlike the 68060, the 68080 pairs only after it has seen the code once. +- `fsub` does not fuse, although it is an FPU arithmetic instruction like + fadd and fmul. + +## Do / Don't + +- DO judge pairing only on trained (second and later) passes. +- DO write the copy and the operation next to each other (`move.l d0,d1` + + `add.l d2,d1`): they fuse into one 3-operand instruction. +- DON'T put `nop` in hot code: it is a pipeline barrier. +- DON'T call through `(An)` or `d16(An)` in hot code when a PC-relative or + absolute target works: it costs about 5 cycles more. +- DON'T use `fsub` in a 3-operand fused pair: negate the operand and use + `fadd`. diff --git a/core/pitfalls/alternating-branch-mispredicted.md b/core/pitfalls/alternating-branch-mispredicted.md new file mode 100644 index 0000000..0d8c678 --- /dev/null +++ b/core/pitfalls/alternating-branch-mispredicted.md @@ -0,0 +1,67 @@ +--- +title: A branch that alternates every iteration is always mispredicted +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, branches, prediction, misprediction] +related: + - core/cpu-68080/branches-and-prediction.md + - core/best-practices/predicated-bcc-over-one-instruction.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 5) and 2.6" +--- + +## Symptom + +A loop with a branch that goes taken, not taken, taken, not taken runs +slower than the same loop with random branch directions: about 7 extra +cycles per iteration. + +## Cause + +The 68080 branch predictor does not learn a strict alternation: it +mispredicts it every time. A mispredicted Bcc costs about 7 cycles. + +Loop benchmarks hide a related problem: a size-dispatch branch is +unpredictable when the sizes in real use are random, but predictable in a +benchmark with a fixed size. + +## Wrong + +```asm +loop: + btst #0,d7 ; flips every iteration + beq.s .even + ; ... odd case, more than one instruction ... + bra.s .next +.even: + ; ... even case ... +.next: + dbra d7,loop +``` + +## Right + +- Unroll by 2, so each copy handles one case without a branch. +- Or compute the value without a branch. +- Or, if one case is a single short single-cycle instruction, use a + predicated `bcc.s` over it (see + [predicated-bcc-over-one-instruction.md](../best-practices/predicated-bcc-over-one-instruction.md)). + +## Detection + +Look for branches on a loop counter bit or on toggling state. Benchmark +dispatch code with random sizes, and pull the small cases out first. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Alternating pattern: 10.5 cycles per turn vs 3.0 always taken, 7.0 random | (M) | Core 12841 | `beq` over 2 instructions, 65536 branches, CCC, median of 7 | +| Mispredicted Bcc costs about 7 cycles | (M) | Core 12841 | Same | diff --git a/core/pitfalls/alu-to-ea-bubble.md b/core/pitfalls/alu-to-ea-bubble.md new file mode 100644 index 0000000..0a31fc6 --- /dev/null +++ b/core/pitfalls/alu-to-ea-bubble.md @@ -0,0 +1,85 @@ +--- +title: The ALU-to-EA bubble +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, pipeline, addressing, latency, index] +related: + - core/cpu-68080/pipeline-15-stages.md + - core/cpu-68080/prm/pipeline.md + - core/best-practices/close-loops-with-dbra.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rules 2-3) and 2.5" +--- + +## Symptom + +Code that computes an index or address in a data register and uses it right +away runs 2–3 cycles slower per use than expected, and the instructions +paired with it wait too. It is the most common hidden cost on the 68080. + +## Cause + +The EA unit owns the address registers and computes addresses at stage 8; +the ALU owns the data registers and delivers its result at stage 11/12. A Dn +written by any instruction and then used for addressing arrives 3–4 stages +too late. EA to EA and ALU to ALU have no latency. + +- Producers that cause it: add, moveq, `move.l Dn,Dn`, a load into Dn, + bfextu, any write to Dn. +- Consumers that suffer: indexed loads, `lea (An,Dn)`, `move.l Dn,An`, a `*4` + scaled index, the Dn offset of a memory bitfield. + +## Wrong + +```asm +loop: + addq.l #4,d0 ; ALU writes d0 ... + move.l (a0,d0.l),d1 ; ... EA needs d0 now: bubble + add.l d1,d2 + dbra d7,loop +``` + +## Right + +Keep pointers and counters that drive addressing in An, and update them with +`addq`/`subq`/`lea`/`adda`, or walk memory with `(An)+`: + +```asm +loop: + move.l (a0)+,d1 ; address register updated in the EA unit + add.l d1,d2 + dbra d7,loop +``` + +Other fixes: + +- Write `clr.l (a0)+`, or use `(a0,a1)` + `addq.l #4,a1`. +- Compute offsets at least 4 cycles (about 8 paired instructions) before + use, or have the caller pass ready-made pointers loaded straight into An. +- Test an An with `tst.l An` or `move.l An,Dn`. + +## Detection + +Look for a Dn written by an ALU instruction and used as an index, base or +bitfield offset within the next few instructions. Measure with CCC: a chain +that costs 3 or more cycles per link instead of 1 points here. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| ALU to EA costs a bubble of 2 cycles | (official) PRM pipeline page | any | Manual | +| The bubble is "more than 2" | (G) | Core 12841 | Statement | +| Adjacent: +2 to 3 cycles; 4 paired fillers: +2; 6: +1; 8 or more: 0 | (M) | Core 12841 | Producer + consumer with fillers, CCC | +| add → `(a,d0)` load → add: 3 cycles per link | (M) | Core 12841 | Chain, CCC | +| load → indexed load: 4 cycles per link | (M) | Core 12841 | Chain, CCC | +| add d0 → `bfextu (a0){d0:w}` → add: 5 cycles per link | (M) | Core 12841 | Chain, CCC | +| A table lookup whose index was just computed: about 5.4 cycles per iteration | (M) | Core 12841 | Hot loop, CCC | +| Keeping values in a3–a6 inside generated arithmetic code cost about 6 % (992 vs 931 cycles, IMDCT) | (M) | Core 12841 | CCC | diff --git a/core/pitfalls/ammx-pc-relative-source.md b/core/pitfalls/ammx-pc-relative-source.md new file mode 100644 index 0000000..b91505e --- /dev/null +++ b/core/pitfalls/ammx-pc-relative-source.md @@ -0,0 +1,57 @@ +--- +title: AMMX PC-relative sources read 2 bytes early +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080, toolchain] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, ammx, pc-relative, vasm, bug] +related: + - core/cpu-68080/prm/ammx/ammx-format.md + - core/cpu-68080/core-bugs-by-revision.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 6 and 9.4" +--- + +## Symptom + +An AMMX instruction with a PC-relative source (`load .k(pc),d0`, +`paddw .k(pc),...`) reads data 2 bytes before the label. + +## Cause + +vasm counts the displacement from the third word of the instruction; the +core counts it from the second word. The two disagree by 2 bytes. The bug is +open on Core 12841. + +## Wrong + +```asm + load .k(pc),d0 ; reads .k - 2 + ; ... +.k: dc.w $0001,$0002,$0003,$0004 +``` + +## Right + +Load the address into a register first: + +```asm + lea .k(pc),a0 + load (a0),d0 +``` + +## Detection + +Search for AMMX instructions with a `(pc)` source operand. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| PC-relative AMMX sources read 2 bytes early | (M) | Core 12841 | Test program, 2026-10-30 | +| vasm counts from the third word, the core from the second | (M) | Core 12841 | Comparing vasm output with core behaviour | diff --git a/core/pitfalls/ammx-upper-32-bits.md b/core/pitfalls/ammx-upper-32-bits.md new file mode 100644 index 0000000..93547dd --- /dev/null +++ b/core/pitfalls/ammx-upper-32-bits.md @@ -0,0 +1,68 @@ +--- +title: 32-bit writes can clear the upper half of a 64-bit register +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, ammx, e-registers, 64-bit, move, moveq, bug] +related: + - core/cpu-68080/prm/programming-model.md + - core/cpu-68080/bank-prefix.md + - core/cpu-68080/core-bugs-by-revision.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 6" + - "Gunnar (core developer), move.l must keep the upper 32 bits, 2026-10-30" +--- + +## Symptom + +An AMMX operation on a D or E register that was last written with +`move.l #imm` sees zeros in the upper 32 bits, but only on trained passes of +a loop. + +## Cause + +`move.l` must keep the upper 32 bits of a D or E register untouched (G, +2026-10-30). On Core 12841: + +- `move.l Dn,Dn` and `move.l (An),Dn` keep them; +- `move.l #imm` (D or E register) clears them on trained passes (bug, + regression test "test-upper"); +- `moveq`, `ext.l` and `extb.l` behave like `move.l #imm`, and a banked + `moveq` changes from pass to pass; +- `clr.l` always keeps them. + +Whether `moveq`, `ext` and `clr` should follow the `move.l` rule has been +asked of the core developer. + +## Wrong + +```asm + load.w #$1111,e0 ; e0 = $1111111111111111 + move.l #$22222222,e0 ; upper half may become 0 when trained + paddw e0,d1,d2 ; uses all 64 bits +``` + +## Right + +Write all 64 bits with an AMMX instruction (`load`, `peor`, or another AMMX +op) before any 64-bit use, and don't rely on a 32-bit write leaving the upper +half alone. + +## Detection + +Look for 32-bit writes (`move.l #imm`, `moveq`, `ext.l`, `extb.l`) to +registers that AMMX code later reads as 64 bits. Test over several passes: +the cold pass hides the bug. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| `move.l` must keep the upper 32 bits | (G, 2026-10-30) | any | Statement | +| `move.l #imm`, `moveq`, `ext.l`, `extb.l` clear them on trained passes; `clr.l` keeps them | (M) | Core 12841 | Regression test "test-upper" | diff --git a/core/pitfalls/bank-prefix-pitfalls.md b/core/pitfalls/bank-prefix-pitfalls.md new file mode 100644 index 0000000..0266096 --- /dev/null +++ b/core/pitfalls/bank-prefix-pitfalls.md @@ -0,0 +1,77 @@ +--- +title: BANK prefix traps (single operands, unsupported instructions, B registers) +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080, toolchain] +applies_to: + core: "Core 12830-12841" + os: [any] +tags: [68080, bank, e-registers, b-registers, vasm, encoding] +related: + - core/cpu-68080/bank-prefix.md + - core/cpu-68080/prm/integer/bank.md + - core/cpu-68080/prm/b-registers.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 23), 7.1 and 9.4" +--- + +## Symptom + +Code with E registers silently uses a plain D register instead: `tst e6` +tests d6, `add.l e6,d1` computes d6 + d6, or a loop gives the right result on +its first pass and the wrong one when trained. + +## Cause + +- For a single-register operand (tst, clr, neg, not, ext, swap, scc, + bitfields, the dbcc counter) and for `#imm,Rn`, both bank fields must be + set: AA = BB (official). With AA alone the plain Dn is used; with BB alone + the cold pass looks right, but in a trained loop pipe 2 reads the plain Dn. +- **vasm 2.0f/2.0g puts single operands in AA only**, so `tst e6` silently + becomes `tst d6`. For `add.l #imm,En` it sets BB only (`$7101`), which + writes plain Dn + imm into En. +- Banking does not work yet for add, sub, and, or, cmp, addx, subx, mul, + div, abcd, sbcd, pack, unpk, exg, bfextu, bfexts, bfins (official list, + confirmed on Core 12830/12840). +- `cmp.l En,Dn` uses the plain Dn on the first two passes and the right En + from the third pass on (Core 12841). +- `add.l Bn,Dn` and `move.l An,Bn` are not supported B-register forms. vasm + 2.0g rejects `move.l a0,b1` and turns `add.l b1,d0` into `$71F1`, a BANK + word with SS ≠ 0, which is illegal. + +## Wrong + +```asm + tst.l e6 ; vasm: AA only -> tests d6 + add.l #1,e3 ; vasm: BB only -> e3 = d3 + 1 + add.l e6,d1 ; banking not supported -> d1 = d6 + d6 ... wrong + cmp.l e2,d0 ; plain d0 on cold passes +``` + +## Right + +- Hand-encode single-operand and `#imm,Rn` BANK forms with AA = BB, for + example `$7105`, `$710A`, `$710F` as the prefix word, and `$7105`, `$D2BC` + for `add.l #imm,En`. +- Do arithmetic and compares in a plain Dn: move the E register into a Dn, + operate, move it back. +- Use only the supported B-register forms (see + [bank-prefix.md](../cpu-68080/bank-prefix.md)). + +## Detection + +Decode every hand-encoded `dc.w` with an 080-aware disassembler and check its +length. Check vasm listings (`-L`) for BANK words on single-operand and +`#imm` instructions. Test banked code over several passes: the cold pass +hides the error. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| AA = BB for single operands; list of instructions where banking is not handled correctly | (official) PRM, BANK and integer-unit pages | any | Manual | +| BB alone fails in trained loops; `add.l e6,d1` = d6 + d6; `cmp.l En,Dn` wrong on cold passes | (M) | Core 12830-12841 | Regression tests, cold and trained passes | +| vasm 2.0f/2.0g encodes single operands with AA only, `add.l #imm,En` with BB only | (M) | any | vasm listings | diff --git a/core/pitfalls/fpu-47-bit-context-save.md b/core/pitfalls/fpu-47-bit-context-save.md new file mode 100644 index 0000000..72eb6a3 --- /dev/null +++ b/core/pitfalls/fpu-47-bit-context-save.md @@ -0,0 +1,66 @@ +--- +title: FPU values above 47 bits change at task switches +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12830-12841" + os: [any] +tags: [68080, fpu, fmovem, context-switch, precision, double] +related: + - core/cpu-68080/prm/fpu/fmovem.md + - core/best-practices/fpu-when-it-pays.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 11) and 4.2" + - "Gunnar (core developer), new FMOVEM encoding, 2026-10-30" +--- + +## Symptom + +Doubles with 48 to 53 significant bits change at random while a program +runs: rarely, and only when interrupts or task switches happen in between. +Exact integer arithmetic in doubles and the `x + 1.5*2^52` floor trick give +occasional wrong results. + +## Cause + +Task switches and interrupts save the FP registers with `fmovem.x`, which +stores the Apollo extended format: about 48 mantissa bits. Values that need +more bits lose them when the registers are restored. The PRM documents the +Apollo extended layout ([prm/fpu/fmovem.md](../cpu-68080/prm/fpu/fmovem.md)). + +## Wrong + +```asm + ; floor() by adding 1.5 * 2^52: relies on all 53 bits surviving + fadd.d #6755399441055744.0,fp0 + fsub.d #6755399441055744.0,fp0 +``` + +## Right + +- Keep exact integer arithmetic in doubles at 47 significant bits or less. +- Don't use the `1.5*2^52` trick; convert with `fmove.l` in RZ mode instead + (see [fpu-float-to-integer-conversion.md](fpu-float-to-integer-conversion.md)). +- Exchange values through memory with `fmove.d`, which keeps all bits. + +Gunnar (G, 2026-10-30): a new `FMOVEM` in the 3-operand encoding will save +and restore all 32 FPU registers in 64 bits, and only that one is to be used +in the future. The PRM says newer cores will use the normal extended format. + +## Detection + +Run the code under load (other tasks, interrupts) for millions of iterations +and compare with a reference. Check every double that must hold more than 47 +significant bits. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Apollo extended format in memory differs from Motorola's | (official) PRM, FMOVEM page | any | Manual | +| About 3150 changed values per 20 M checks in a busy loop; values of 47 bits or less never change | (M) | Core 12830-12841 | Busy-loop check | +| New 64-bit FMOVEM in the 3-operand encoding planned | (G, 2026-10-30) | Future core | Statement | diff --git a/core/pitfalls/fpu-broken-instructions.md b/core/pitfalls/fpu-broken-instructions.md new file mode 100644 index 0000000..b821bbe --- /dev/null +++ b/core/pitfalls/fpu-broken-instructions.md @@ -0,0 +1,71 @@ +--- +title: FPU instructions that give wrong results on Core 12841 +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12001-12841" + os: [any] +tags: [68080, fpu, fscc, ftrapcc, flognp1, fmovem, fmove, fdbcc, bugs] +related: + - core/cpu-68080/core-bugs-by-revision.md + - core/pitfalls/fpu-precision-and-rounding.md + - core/cpu-68080/prm/fpu/fdbcc-l.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 4.2 (bugs first reported by Morten on Core 12001, rechecked on Core 12841)" +--- + +## Symptom + +Some FPU instructions run without an exception but do the wrong thing: an +extension word runs as an instruction, a trap ignores the flags, a function +returns a different function, or memory operands are read partly. + +## Cause + +Core bugs or unsupported forms, still present on Core 12841 unless noted: + +| Instruction | What happens | +|---|---| +| `FScc` to `d16(An)` or `d8(An,Xn)` | The byte is stored, then the extension word runs as an instruction. `fst 95(a0)` leaves `$005F`, which runs as `ori.b #x,(a7)+` and hangs | +| `FTRAPcc` | Ignores the FPU flags: conditions 0, 3, 4, 7, 8, A, C and F always trap (vector 7), the others never do | +| `flognp1` | Returns ln(x), not ln(1+x): `flognp1 0.5` = −0.693 | +| `fmovem.x` with a dynamic list (`fmovem.x Dn,` / `,Dn`) | Not supported (G), as on the 68060. No exception: the store writes wrong values, the load leaves the registers unchanged | +| `fmove.x` load of a 12-byte Motorola value | Ignores the low long of the mantissa: about 32 bits (1+2^−40 loads as 1.0) | +| `fmove.x` store to memory | Writes only the first 8 bytes; the third long is left untouched | +| `FDBcc` (Core 12830 and 12840 only) | Never decremented and always branched. Fixed in Core 12841 | + +## Wrong + +```asm + fseq 95(a0) ; extension word runs as an instruction + ftrapeq ; trap decision ignores the compare + fmovem.x d0,-(sp) ; dynamic list: wrong values + fmove.x fp0,(a1) ; stores 8 of 12 bytes +``` + +## Right + +- `FScc` to `Dn`, `(An)`, `(An)+` or `-(An)`, which work. +- `FBcc` instead of `FTRAPcc` (FBcc is correct). +- `fadd #1` + `flogn` instead of `flognp1` (mind the precision). +- A static register list in `fmovem`. +- `fmove.d` to exchange values with memory. +- Avoid `FDBcc` on cores before 12841. + +## Detection + +Search the source and the disassembly for FScc with displacement modes, +FTRAPcc, flognp1, dynamic fmovem lists and `fmove.x` memory operands. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Dynamic fmovem lists are not supported, as on the 68060 | (G) | any | Statement | +| FScc, FTRAPcc, fmove.x behaviour as in the table | (M) | Core 12001, rechecked Core 12841 | FPU test suite | +| `flognp1` returns ln(x) | (M) | Core 12841 | Found while rechecking | +| FDBcc fixed: word and long counters, all conditions | (M) | Core 12841 | FPU test suite | diff --git a/core/pitfalls/fpu-exceptions-never-trap.md b/core/pitfalls/fpu-exceptions-never-trap.md new file mode 100644 index 0000000..9c4ec9d --- /dev/null +++ b/core/pitfalls/fpu-exceptions-never-trap.md @@ -0,0 +1,64 @@ +--- +title: FPU exceptions never trap and FPSR exception bytes stay 0 +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fpu, exceptions, fpsr, fpcr, divide-by-zero] +related: + - core/pitfalls/fpu-broken-instructions.md + - core/pitfalls/fpu-precision-and-rounding.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 4.2" +--- + +## Symptom + +Code that enables FPU exception traps in FPCR (DZ, OPERR, INEX2) never sees +a trap, and code that checks the FPSR exception or accrued bytes never sees +an error. + +## Cause + +On Core 12841 FPU exceptions never trap, even with the FPCR enable bits set. +In FPSR, only the condition byte (N, Z, I, NAN) is ever set. The exception +byte, the accrued byte and the `fmod`/`frem` quotient byte stay 0. There is +no packed decimal format either. + +## Wrong + +```asm + fmove.l #$00000400,fpcr ; enable divide-by-zero trap + fdiv.x fp1,fp0 ; fp1 = 0: no trap + fmove.l fpsr,d0 + btst #4,d0 ; accrued DZ: always 0 +``` + +## Right + +Test the result itself with `fcmp`/`ftst` + `FBcc`: + +```asm + fdiv.x fp1,fp0 + fbun.w .nan ; NAN result (condition byte NAN bit) + fmove.l fpsr,d0 + btst #25,d0 ; condition byte I bit: result is infinity + bne.s .inf +``` + +## Detection + +Search for writes to FPCR enable bits, FPSR exception or accrued byte tests, +and uses of the `fmod`/`frem` quotient byte. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| DZ, OPERR and INEX2 traps never taken with the FPCR enable bits set | (M) | Core 12841 | FPU test suite | +| Only the FPSR condition byte is ever set | (M) | Core 12841 | Same | diff --git a/core/pitfalls/fpu-float-to-integer-conversion.md b/core/pitfalls/fpu-float-to-integer-conversion.md new file mode 100644 index 0000000..a6d2922 --- /dev/null +++ b/core/pitfalls/fpu-float-to-integer-conversion.md @@ -0,0 +1,73 @@ +--- +title: FP-to-integer conversion rounds wrongly except in RZ mode +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12830-12841" + os: [any] +tags: [68080, fpu, fmove, rounding, conversion, fpcr] +related: + - core/cpu-68080/prm/fpu/fmove-dn.md + - core/cpu-68080/prm/fpu/fmoverz.md + - core/pitfalls/multiply-and-bfffo-block-both-pipes.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 4.2" +--- + +## Symptom + +`fmove.l` from FP to an integer gives off-by-one results in round-to-nearest, +round-down and round-up modes: 4678528.501 becomes 4678528 in RN, −0.1 +becomes 0 in RM, 0.1 becomes 0 in RP. + +## Cause + +`fmove.l` decides the rounding from only the 2 bits below the binary point; +the sticky bit is lost. RZ (round toward zero) is always right, and exact +integers always convert right. + +Related behaviour: + +- `fmove.w` FP to integer follows the FPCR rounding mode correctly (RN + half-even, RZ, RM, RP) and **saturates** to 32767/−32768 without OPERR. +- `fmoveurz.l` of 3000000000.75 gives `$FFFFFFFF`. +- `fmoverz`/`fmoveurz` do not exist on the V2. +- Default task FPCR seen: `$0` or `$10`. + +## Wrong + +```asm + fmove.l #0,fpcr ; RN + fmove.l fp0,d0 ; 4678528.501 -> 4678528 +``` + +## Right + +```asm + fmove.l #$10,fpcr ; RZ: always correct (floor for positive values) + fmove.l fp0,d0 +``` + +- Convert values that have a fraction only in RZ mode. For round-to-nearest, + add 0.5 first (positive values) and convert in RZ. +- For 16-bit PCM output, `fmove.w` with FPCR = 0 (RN) is correct and + saturates: handy, and set FPCR explicitly around the routine. +- Set FPCR explicitly when your routine depends on it; don't rely on the task + default. + +## Detection + +Test conversions of values just above .5 and small negative and positive +fractions in each rounding mode. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| RN 4678528.501 → 4678528; RM −0.1 → 0; RP 0.1 → 0; RZ always right | (M) | Core 12830-12841 | Conversion tests | +| `fmove.w` follows FPCR and saturates without OPERR | (M) | Core 12830-12841 | Same | +| `fmoveurz.l` 3000000000.75 → `$FFFFFFFF` | (M) | Core 12830-12841 | Same | diff --git a/core/pitfalls/fpu-precision-and-rounding.md b/core/pitfalls/fpu-precision-and-rounding.md new file mode 100644 index 0000000..b93d656 --- /dev/null +++ b/core/pitfalls/fpu-precision-and-rounding.md @@ -0,0 +1,79 @@ +--- +title: 68080 FPU precision and rounding differ from the 6888x +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fpu, precision, rounding, fdiv, fsqrt, transcendental] +related: + - core/cpu-68080/prm/fpu-unit.md + - core/pitfalls/fpu-float-to-integer-conversion.md + - core/pitfalls/fpu-broken-instructions.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 4.2" + - "Gunnar (core developer), FPU precision targets, 2026-10-30" +--- + +## Symptom + +Floating-point results differ from a 68881/68882 or from C on another +platform: transcendentals are accurate to about 32 bits, `fdiv` and `fsqrt` +to about 33–42 bits, `fadd` is 1 ulp low, single-precision instructions +return double results, and `fint` rounds ties away from zero. + +## Cause + +Precision is a design choice (G, 2026-10-30): most games need 23 bits, and +more precision costs cycles and FPGA space. The targets are full 64 bits on +`fmul` and `fadd`, and about 40 bits on `fsqrt` and `fdiv`. Measured on Core +12841: + +| Operation | Behaviour | +|---|---| +| `fmul` | Correctly rounded | +| `fadd` | Rounds toward zero: 1 ulp low when the exact sum needs rounding up (0.1 + 0.2 = `...3333`, not `...3334`). Asked whether intended | +| FPU arithmetic and `fmove.s` stores | Truncate, whatever the FPCR mode. `fmove.s` differs from C round-to-nearest by 1 ulp | +| `fdiv` | 40.6–41.8 good bits (1/3 = `$3FD5555555555000`, about 1400 ulp) | +| `fsqrt` | 32.6–36.4 good bits (sqrt 2 = `$3FF6A09E66800000`); exact squares are exact | +| Transcendentals (fsin, fcos, ftan, fasin, facos, fatan, fsinh, fcosh, ftanh, fatanh, fetox, ftwotox, ftentox, flogn, flog10, flog2) | About 31–33 good bits (10^5 to 4x10^6 ulp) on ordinary arguments; only tiny arguments come out exact | +| FPCR rounding precision, and `fs*`/`fd*` (`fsmove`, `fsmul`, `fsadd`, `fsdiv`, `fssqrt`) | Ignored: all return the double result | +| `fint` in RN | Ties away from zero: 2.5 → 3, −2.5 → −3, 0.5 → 1 (68881: 2, −2, 0) | +| `fmovecr` | Within 1 ulp, truncated. 1e512 to 1e4096 (`$3C`–`$3F`) give +inf: they do not fit in a double | +| Older cores (official) | Older V4 cores use 11 nibbles of fraction; V2 core 2.17 about 8 | + +## Wrong + +```asm + fmove.d #2.0,fp0 + fsqrt.x fp0 ; assumes 53 correct bits + fsmul.x fp1,fp0 ; assumes single-precision rounding +``` + +## Right + +- Add a Newton step after `fsqrt`/`fdiv` when you need more than about 33/40 + bits. +- Use your own polynomial or table for transcendentals when you need 53 + bits. +- Round to single with an `fmove.s` store and reload. +- Use your own rounding when `fint` ties matter. +- Discuss precision requirements with the Apollo team (G). + +## Detection + +Compare results with a reference implementation on the full input range, in +ulp. Check code that relies on exact rounding (interval arithmetic, financial +code, bit-exact codecs). + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Precision targets: 64 bits on fmul/fadd, about 40 on fsqrt/fdiv | (G, 2026-10-30) | Core 12841 | Statement | +| The values in the table above | (M) | Core 12841 | FPU test suite, against reference values | +| Older V4 cores 11 nibbles fraction, V2 2.17 about 8 | (official) PRM, FPU unit page | Older cores | Manual | diff --git a/core/pitfalls/fused-copy-overlap.md b/core/pitfalls/fused-copy-overlap.md new file mode 100644 index 0000000..7146ca8 --- /dev/null +++ b/core/pitfalls/fused-copy-overlap.md @@ -0,0 +1,66 @@ +--- +title: Fused quad moves break overlapping forward copies +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, fusing, copy, overlap, memmove] +related: + - core/best-practices/use-fusable-pairs.md + - core/cpu-68080/prm/instruction-fusing.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 24) and 2.4" +--- + +## Symptom + +A forward copy whose destination is less than 8 bytes ahead of its source +(for example the `dst = src + 4` "replicate" trick) gives wrong data once the +loop is trained: L0, L0, L1 instead of L0, L0, L0. The first, cold pass is +correct, which makes the bug hard to see. + +## Cause + +Two fused `move.l (an)+,(am)+` act as one quad move: they read both longs +(8 bytes) before writing (G: by design). On the cold pass the instructions do +not fuse yet, so the result differs between the first and later passes. + +## Wrong + +```asm + lea 4(a0),a1 ; destination 4 bytes ahead: replicate the first long +loop: + move.l (a0)+,(a1)+ + move.l (a0)+,(a1)+ ; fuses: reads 8 bytes before writing + dbra d0,loop +``` + +## Right + +- Copy backwards, or keep source and destination at least 8 bytes apart. +- To replicate a value, load it into a register once and store it. + +```asm + move.l (a0),d1 +loop: + move.l d1,(a1)+ + move.l d1,(a1)+ ; fuses to a quad store + dbra d0,loop +``` + +## Detection + +Look for memmove-style or replicate copies with a forward distance under 8 +bytes. Test copies over more than one pass: the cold pass hides the bug. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Fused `move.l (an)+,(am)+` pairs read 8 bytes before writing, by design | (G) | Core 12841 | Statement | +| `dst = src + 4` gives L0,L0,L1 trained, L0,L0,L0 cold | (M) | Core 12841 | Loop test, cold vs trained pass | diff --git a/core/pitfalls/maggie-depth-test-colour-write.md b/core/pitfalls/maggie-depth-test-colour-write.md new file mode 100644 index 0000000..7622077 --- /dev/null +++ b/core/pitfalls/maggie-depth-test-colour-write.md @@ -0,0 +1,54 @@ +--- +title: Maggie's depth test does not gate the colour write +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [saga-3d-maggie] +applies_to: + core: "Core 12847" + os: [any] +tags: [maggie, 3d, depth-buffer, z-buffer, bug] +related: + - core/saga-3d-maggie/registers.md + - core/cpu-68080/core-bugs-by-revision.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 15.7 (regression test test-maggiez, 2026-11-01)" +--- + +## Symptom + +Depth-buffered scenes drawn in arbitrary order show far polygons on top of +near ones: draw near, then far, and far wins. + +## Cause + +On Core 12847 the depth write is gated correctly (smaller = nearer wins), but +the colour is written on every pixel, whatever the depth test says. Core bug, +reported to the core developer. + +## Wrong + +```c +/* relies on the depth buffer for visibility */ +magSetDrawMode(MAG_DRAWMODE_DEPTHBUFFER); +draw_objects_in_any_order(); +``` + +## Right + +Until the bug is fixed, sort polygons back to front (painter's algorithm) so +that the colour of the nearest polygon is written last. This is a general +workaround, not tested in the guide. + +## Detection + +Draw a near quad, then a far quad over it, with the depth buffer on. If the +far quad is visible, the core has this bug. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Depth write gated, colour write not | (M) | Core 12847 | Register-level test test-maggiez, 2026-11-01 | diff --git a/core/pitfalls/maggie-span-tail.md b/core/pitfalls/maggie-span-tail.md new file mode 100644 index 0000000..e53de4e --- /dev/null +++ b/core/pitfalls/maggie-span-tail.md @@ -0,0 +1,61 @@ +--- +title: The last 8 bytes of a Maggie span stay in the unit +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [saga-3d-maggie] +applies_to: + core: "Core 12847" + os: [any] +tags: [maggie, 3d, span, flush, memory-corruption, bug] +related: + - core/saga-3d-maggie/registers.md + - core/pitfalls/maggie-span-usage.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 15.7 (regression test test-maggieend, 2026-11-01)" +--- + +## Symptom + +- A CPU read-back right after the last span misses up to 2 pixels (32-bit), + 4 pixels (16-bit) or 4 depth words. +- After a program that used Maggie exits, the next Maggie user's first span + writes 8 bytes into memory that may have been freed. On the V4 this + overwrote the next program's code. + +## Cause + +On Core 12847 the last 8-byte group of a span stays inside Maggie, separately +for the colour stream and the depth stream. It is written only when a later +span writes elsewhere; for depth, that must be a depth-mode span. Waiting +does not flush it, and neither does reading a register. + +## Wrong + +```c +draw_frame(); /* last span leaves 8 bytes in Maggie */ +FreeMem(target, size); /* the next user's first span writes into it */ +``` + +## Right + +Not yet tested; derived from the measured behaviour: + +- After the last real span, start a flush span to a small scratch buffer you + own: one with depth mode on if you used depth. Free that scratch buffer + only after the flush span. +- Before reading back a target, start such a flush span first. + +## Detection + +Read back the last pixels of a frame right after drawing. Check for memory +corruption after a Maggie program exits. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| The last 8-byte group stays in the unit; waiting or a register read does not flush it | (M) | Core 12847 | Register-level test test-maggieend, 2026-11-01 | +| The leftover bytes overwrote the next program's code | (M) | Core 12847 | Observed on the V4 | diff --git a/core/pitfalls/maggie-span-usage.md b/core/pitfalls/maggie-span-usage.md new file mode 100644 index 0000000..7763dfd --- /dev/null +++ b/core/pitfalls/maggie-span-usage.md @@ -0,0 +1,73 @@ +--- +title: Maggie span traps (zero length, no cache snoop, one user, counters) +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [saga-3d-maggie] +applies_to: + core: "Core 12847" + os: [any] +tags: [maggie, 3d, span, cache, arbitration, counters] +related: + - core/saga-3d-maggie/registers.md + - core/pitfalls/maggie-span-tail.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 15.7" +--- + +## Symptom + +- Memory gets overwritten after a span with length 0. +- The CPU reads stale pixels or depth values from a buffer Maggie has just + drawn. +- Two programs drawing with Maggie at once spoil each other's spans. +- The performance counters at `$DFF300` never change. + +## Cause + +- A span with length 0 overwrote memory on the V4, probably as 65536 pixels + (M). +- Maggie does not snoop the CPU data cache (SRC): a CPU read of the depth + buffer or of a target Maggie has just drawn can hit stale cache lines. +- There is one register set. Neither AmigaOS nor maggie.library arbitrates + between programs; the library's semaphore covers only its own callers. +- The counters at `$DFF300` read 0 on Core 12847 (M). + +## Wrong + +```asm + move.w d0,14(a0) ; d0 may be 0: never start a span with length 0 +``` + +## Right + +```asm + tst.w d0 + beq.s .skip ; skip empty spans + move.w d0,14(a0) +.skip: +``` + +- Make sure the CPU does not read a Maggie target or the depth buffer + through stale data-cache lines. The guide gives no tested method. Note that + `CacheClearU` does nothing on the 68080 (see + [self-modifying-code-and-caches.md](self-modifying-code-and-caches.md)) + and that evicting the data cache takes 128–256 KB of reads. +- Let one program at a time use Maggie. +- Don't use the counters as a sync source. + +## Detection + +Check every write to the start + length register for a possible 0. Look for +CPU reads of Maggie targets or the depth buffer without a cache flush. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| A length-0 span overwrote memory | (M) | Core 12847 | Observed on the V4 | +| Maggie does not snoop the CPU data cache | (SRC) | any | maggie.library source and docs | +| Counters at `$DFF300` read 0 | (M) | Core 12847 | Register-level test test-maggiecnt, 2026-11-01 | +| No arbitration between programs | (SRC), (M) | Core 12847 | Library source; two programs observed | diff --git a/core/pitfalls/multiply-and-bfffo-block-both-pipes.md b/core/pitfalls/multiply-and-bfffo-block-both-pipes.md new file mode 100644 index 0000000..97aa351 --- /dev/null +++ b/core/pitfalls/multiply-and-bfffo-block-both-pipes.md @@ -0,0 +1,71 @@ +--- +title: Multiply, bfffo and FP-to-integer block both pipes +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, muls, mulu, bfffo, fmove, timing, pairing] +related: + - core/cpu-68080/measured-instruction-costs.md + - core/cpu-68080/prm/instruction-timing.md + - core/best-practices/fpu-when-it-pays.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rules 7 and 10) and 3" +--- + +## Symptom + +Multiply-heavy loops run slower than the PRM's timing predicts, and adding +independent integer work next to a multiply makes the loop longer instead of +filling a free slot. + +## Cause + +On Core 12841 these instructions issue in about 3 cycles each and block both +pipes, so nothing pairs with them: + +- `muls.l`/`mulu.l` (32 and 64-bit), `muls.w`, `mulu.w`: 3.04–3.13 cycles, + result usable about 3 cycles later; +- `bfffo`: 3 cycles, latency 6 when it feeds the next offset; +- `fmove.l`/`fmove.w` FP to integer: 3.08 cycles. + +The PRM gives MUL = 2 or 3 cycles. + +## Wrong + +```asm +loop: + muls.l (a0)+,d1 ; 3 cycles, blocks both pipes + addq.l #1,d5 ; does not pair: +1 cycle + add.l d1,d2 ; uses the product right away + dbra d0,loop +``` + +## Right + +- Count each of these as about 3 cycles that nothing pairs with; put + independent work before or after the run of multiplies, not between. +- Use the product at least 3 cycles later. +- Consider the FPU for long multiply-accumulate chains (see + [fpu-when-it-pays.md](../best-practices/fpu-when-it-pays.md)). +- Keep FP-to-integer conversions out of inner loops. + +## Detection + +Measure with CCC on a trained loop. A loop body around a `muls` that costs +the sum of its instructions instead of the longest chain points here. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| MUL = 2 or 3 cycles | (official) PRM timing page | any | Manual | +| `muls.l` 3.04–3.13 cycles with 1 or 7 chains; mul + independent op = 4.0–4.08 | (M) | Core 12841 | CCC/IEP, trained loops, best of 5 | +| `bfffo` 3 cycles, blocks both pipes | (M) | Core 12841 | Same | +| `fmove.l`/`.w` FP to integer 3.08 cycles, blocks both pipes | (M) | Core 12841 | Same | +| mul + add/addx rounding chain: 5–6 cycles per product | (M) | Core 12841 | Same | diff --git a/core/pitfalls/pc-relative-destination.md b/core/pitfalls/pc-relative-destination.md new file mode 100644 index 0000000..454d722 --- /dev/null +++ b/core/pitfalls/pc-relative-destination.md @@ -0,0 +1,70 @@ +--- +title: PC-relative destinations are miscomputed after a full-extension source +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080, toolchain] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, pc-relative, addressing, memory-corruption, vasm] +related: + - core/cpu-68080/prm/integer-unit.md + - core/cpu-68080/isa-notes-measured.md + - toolchain/vasm-assembler.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 22), 7.2 and 9.4" +--- + +## Symptom + +Silent memory corruption: a write meant for a variable lands somewhere else. +It happens only in instructions that combine a full-extension-format source +with a PC-relative destination. + +## Cause + +The 68080 allows PC-relative addressing for destinations (the 68000–68060 +only for sources). The PC used is the address of the destination's extension +word, and it is computed wrongly when the source uses the full extension +format, for example `(table.l,d0.w)` or memory indirect. + +vasm with `-m68080` makes such destinations by itself: it rewrites an +absolute destination `move.x ,label` into `label(pc)` by default (also +for addq/subq, bchg/bclr/bset, eor, fmove and AMMX stores). The +toolchain side is described in [toolchain/vasm-assembler.md](../../toolchain/vasm-assembler.md). + +## Wrong + +```asm + move.b (table.l,d0.w),result(pc) ; destination miscomputed + ; or, written with an absolute destination, rewritten by vasm -m68080: + move.b (table.l,d0.w),result +``` + +## Right + +Address the destination through a register: + +```asm + lea result(pc),a1 + move.b (table.l,d0.w),(a1) +``` + +Or assemble the file with vasm `-no-opt` (this disables all vasm +optimizations). + +## Detection + +Scan the assembler listing for MOVE (and the other instructions above) with +destination mode 7/2 (`d16(pc)`) and a full-format source. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| PC-relative destination is not calculated correctly with some extended source formats | (official) PRM, integer unit page | any | Manual | +| Miscomputed after a full-extension source; silent corruption | (M) | Core 12841 | Test program | +| vasm `-m68080` creates `(d16,PC)` destinations by default; `-nodpc` and `opt` don't stop it, `-no-opt` does | (M) | any | vasm 2.0f/2.0g listings | diff --git a/core/pitfalls/return-address-patching.md b/core/pitfalls/return-address-patching.md new file mode 100644 index 0000000..b836c01 --- /dev/null +++ b/core/pitfalls/return-address-patching.md @@ -0,0 +1,66 @@ +--- +title: Changing a return address defeats the return cache +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, rts, return-cache, jump, patching] +related: + - core/cpu-68080/branches-and-prediction.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 2.6 and 12" +--- + +## Symptom + +Subroutine calls or patch stubs are several times slower than expected: +about 9.5 extra cycles per return, or about 45 cycles for an `rtd` used as a +jump. + +## Cause + +Returns are predicted by a return cache for rts/rtd/rtr. When code changes +the return address on the stack, or uses a return instruction as a jump, the +prediction is wrong and the CPU redirects after the ALU stage. + +## Wrong + +```asm +routine: + addq.l #2,(sp) ; skip an inline parameter word + rts ; mispredicted + +dispatch: + pea target ; "jump" through the stack + rts ; mispredicted +``` + +## Right + +- Pass parameters in registers, and return to the real return address. +- Jump directly with `jmp` or `bra`. +- To fall back to an original (patched) routine, use `jmp ([ptr,pc])`, which + is predicted. + +```asm + jmp ([oldvector,pc]) +``` + +## Detection + +Look for writes to `(sp)` before `rts`, `pea` + `rts`, and `rtd` used for +dispatch. Measure a call with CCC: more than about 5 cycles per bsr + rts +points here. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| bsr + rts: 4.6 cycles normally, 14.0 when the routine did `addq.l #2,(sp)` | (M) | Core 12841 | CCC | +| `rtd` used as a jump: about 45 cycles | (M) | Core 12841 | CCC | +| A computed-jump CopyMem was slower than a plain one | (M) | Core 12841 | Real workload | diff --git a/core/pitfalls/self-modifying-code-and-caches.md b/core/pitfalls/self-modifying-code-and-caches.md new file mode 100644 index 0000000..5a88253 --- /dev/null +++ b/core/pitfalls/self-modifying-code-and-caches.md @@ -0,0 +1,76 @@ +--- +title: CacheClearU does nothing; writes near code are slow +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [amigaos-3.9, amigaos-3.2] +tags: [68080, cache, self-modifying-code, cacheclearu, cinva, 68040-library] +related: + - core/cpu-68080/caches-and-memory.md + - core/cpu-68080/prm/specs.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 0 (rule 18) and 5.2" +--- + +## Symptom + +- Patched or generated code runs the old instructions, although the program + called `CacheClearU()` after writing it. +- Writes to buffers placed next to running code are about 3 times slower + than expected. + +## Cause + +- Under AmigaOS the 68080's 68040.library patches `CacheClearU` and + `CacheClearE` to a bare `rts`. They also never flush the 68080 data cache. +- The core detects writes into cached or in-flight code (official: "080 has + selfmodify detection"), but patched code still ran stale without an + instruction-cache flush (M). +- The self-modify detection slows every write near running code. + +## Wrong + +```asm + move.w #$4e71,(a0) ; patch code + movea.l 4.w,a6 + jsr _LVOCacheClearU(a6) ; does nothing on the 68080 + jmp (a0) ; may run the old instruction +``` + +## Right + +Flush the instruction cache with `cinva ic` in supervisor mode, for example +through `Supervisor()`: + +```asm + lea .flush(pc),a5 + movea.l 4.w,a6 + jsr _LVOSupervisor(a6) + ; ... +.flush: + cinva ic + rte +``` + +- Keep benchmark buffers and written data away from running code. +- Evicting the data cache takes 128–256 KB of reads. + +## Detection + +Search for `CacheClearU`/`CacheClearE` used after writing code. Check that +data written in hot loops is not linked into the code section next to the +loop. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| The 080 has self-modify detection | (official) PRM, specs page | any | Manual | +| `CacheClearU`/`CacheClearE` are a bare `rts` under the 080 68040.library | (M) | Core 12841 | AmigaOS 3.9 with the 080 68040.library | +| Patched code ran stale without `cinva ic` | (M) | Core 12841 | Test program | +| A store + 2 `add.l` took 9 cycles next to running code instead of about 3 | (M) | Core 12841 | CCC | diff --git a/core/pitfalls/tex-does-not-match-prm.md b/core/pitfalls/tex-does-not-match-prm.md new file mode 100644 index 0000000..27c5dd3 --- /dev/null +++ b/core/pitfalls/tex-does-not-match-prm.md @@ -0,0 +1,62 @@ +--- +title: TEX does not match the PRM +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12846" + os: [any] +tags: [68080, ammx, tex, texture, bug] +related: + - core/cpu-68080/prm/ammx/tex.md + - core/cpu-68080/core-bugs-by-revision.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 6 (measured 2026-10-31)" +--- + +## Symptom + +`TEX (An,Ax,Ay*Dm),Dn` reads the wrong texel: x/y coordinates in 16.16 and +the line width have no effect. `x 1, y 1, m 256` reads offset 0. + +## Cause + +On Core 12846 the third word of the instruction acts as a plain brief +extension word: bit 15 D/A, bits 14–12 register, bit 11 W/L, bits 10–9 +scale. The core reads the byte at `An + Xn.W/L * scale`. + +- Ax (word 3 bits 2–0), Dm (word 2 bits 14–12) and the displacement byte are + not used. +- The older `TEX8/16/24.size (An,(Av,Au)),Dn` encodings decode the same way. + There is no word, 24-bit, DXT1 or 32-bit fetch, and no modular map. +- The texel replaces the low byte of Dn. The other 56 bits are kept, not + shifted up. +- vasm 2.0g has no syntax for TEX: it needs `dc.w`. + +## Wrong + +```asm + ; per the PRM: byte at (a0) + y*width + x, with x in a1, y in a2 (16.16) + ; and the line width in d7. Hand-encoded with dc.w (vasm has no syntax). + ; On Core 12846 the coordinates and width are ignored. + ; tex (a0,a1,a2*d7),d4 +``` + +## Right + +Don't use TEX until the Apollo team settles its behaviour (question Q6 to the +core developer). Compute the address yourself and use `move.b`. + +## Detection + +Search for TEX, REX and `dc.w` sequences starting with `$FE`. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| TEX byte form with 16.16 coordinates and line width Dm | (official) PRM, TEX page | Core 12540+ | Manual | +| The core reads `An + Xn.W/L * scale`; Ax, Dm and displacement unused | (M) | Core 12846 | mh groups 25/26, 2026-10-31 | diff --git a/core/pitfalls/touch-misuse.md b/core/pitfalls/touch-misuse.md new file mode 100644 index 0000000..eb0e842 --- /dev/null +++ b/core/pitfalls/touch-misuse.md @@ -0,0 +1,67 @@ +--- +title: TOUCH slows sequential streams, and only one form is valid +type: pitfall +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [core-developer, measured, official] +components: [cpu-68080] +applies_to: + core: "Core 12841" + os: [any] +tags: [68080, touch, prefetch, cache, encoding] +related: + - core/cpu-68080/prm/integer/touch.md + - core/best-practices/memory-access-patterns.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 5.2 and 12" +--- + +## Symptom + +- A loop with `touch` on a sequential stream takes twice as long. +- A program crashes after a `touch` with a full extension word, or a + `touch (An)` writes memory. + +## Cause + +- The CPU already prefetches sequential streams; `touch` only adds work. +- The only valid form is the 2-word `(An,Xn)` brief extension (G). A + full-extension form is decoded as 2 words, so the next word runs as an + instruction. +- The PRM's `touch (An)` encoding (`$F61x`) is `move16 (An),abs.l` (M). + +## Wrong + +```asm +loop: + touch 64(a0,d1.l) ; on a sequential stream: 2x slower + move.l (a0)+,d2 + ; ... +``` + +## Right + +Use `touch d8(An,Dn)` only for random accesses, about 12–15 cycles before the +data is needed: + +```asm + touch 0(a1,d1.l) ; random lookup, issued early + ; ... 12-15 cycles of other work ... + move.b 0(a1,d1.l),d2 +``` + +## Detection + +Search for `touch` in loops that walk memory with `(An)+` or a fixed stride, +and for `touch (An)` or full-extension `touch` forms. + +## Evidence + +| Claim | Source | Core | Method | +|---|---|---|---| +| Only mode 6, brief extension, no multiplier | (official) PRM, TOUCH page | any | Manual | +| Only the 2-word `(An,Xn)` brief extension is valid | (G) | Core 12841 | Statement | +| `touch` on a sequential stream doubles the cycles | (M) | Core 12841 | CCC | +| `$F61x` (`touch (An)` in the PRM) is `move16 (An),abs.l` | (M) | Core 12841 | Test program | +| A full-extension form runs the next word as an instruction (crash) | (M) | Core 12841 | Test program | diff --git a/core/saga-3d-maggie/index.md b/core/saga-3d-maggie/index.md index 35befa4..025533f 100644 --- a/core/saga-3d-maggie/index.md +++ b/core/saga-3d-maggie/index.md @@ -16,16 +16,20 @@ sources: Maggie, the SAGA 3D chip. -**This folder has no content yet.** If your task depends on it, tell the -user that the knowledge base does not cover the SAGA 3D (Maggie) yet. Do not fill the -gap with classic Amiga or 68060 knowledge. +**This folder is only partly covered.** It has the span registers as +maggie.library uses them (`registers.md`) and the pitfalls in +`core/pitfalls/maggie-*.md`. For anything else, tell the user that the +knowledge base does not cover it yet. Do not fill the gap with classic Amiga +or 68060 knowledge. ## Details ### Planned files -- `registers.md`: register map with offsets, bits and access rules +- `registers.md`: the span unit registers as maggie.library uses them, with + measured behaviour (exists; no official register documentation yet) - One file per feature, for example texture mapping +- Using maggie.library: planned in `os/libraries/` ### For contributors diff --git a/core/saga-3d-maggie/registers.md b/core/saga-3d-maggie/registers.md new file mode 100644 index 0000000..3ec83e7 --- /dev/null +++ b/core/saga-3d-maggie/registers.md @@ -0,0 +1,89 @@ +--- +title: Maggie span unit registers (as used by maggie.library) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured, source-code] +components: [saga-3d-maggie] +applies_to: + core: "Core 12847" + os: [any] +tags: [maggie, 3d, registers, span, texture, dxt1] +related: + - core/saga-3d-maggie/index.md + - core/pitfalls/maggie-depth-test-colour-write.md + - core/pitfalls/maggie-span-tail.md + - core/pitfalls/maggie-span-usage.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), section 15.7" + - "maggie.library source, github.com/RMortenB/MaggieLibrary, commit 64c78ef (2026-08-17)" + - "Regression tests test-maggie, test-maggiez, test-maggieend, test-maggiecnt, Core 12847, 2026-11-01" +--- + +## Summary + +Maggie is the V4's texture-mapping span unit: the CPU sets up one span per +scanline in registers at `$DFF250`, and writing the length starts it. There +is no public documentation beyond this; the layout below is how +maggie.library uses it (SRC), checked by register-level tests on Core 12847 +(M). + +## Details + +### Span registers at `$DFF250` + +| Offset | Size | Register | +|---|---|---| +| 0 | L | Texture address (DXT1, mip chain) | +| 4 | L | Destination pixel address (start of span) | +| 8 | L | Depth buffer address (start of span) | +| 14 | W | **Start + length: writing it starts the span**, so write it last | +| 16 | W | Texture size log2 (5–10); + `$10` = mipmapping | +| 18 | W | Mode: bit 0 bilinear, 1 depth, 2 16-bit output, 3 depth mask, 6 add, 7 multiply | +| 22 | W | Bytes per pixel (2 or 4) | +| 24 / 28 | L | Second-order dU / dV. Zero them once: they keep stale values otherwise | +| 32 / 36 | L | U / V, 8:24, normalised to the texture | +| 40 / 44 | L | dU / dV per pixel, 8:24 | +| 48 / 50 | W | Light 8:8 / dLight per pixel | +| 52 | L | Light colour (ARGB) = the draw tint | +| 56 / 60 | L | Depth 16:16 / delta 16:16 | + +### Performance counters at `$DFF300` + +Read-only and free-running in the library's description (SRC): `+0` texture +fetches, `+4` depth fetches, `+8` destination fetches, `+12` pixels started +(including pixels later killed by the depth test or alpha). **On Core 12847 +they read 0 and never move** (M). + +### Usage + +- Per span: convert with `fmove.l` (RZ) straight into the U/V registers, set + destination, U, V and light, then `move.w len,14(a0)`. +- Per polygon or batch: dU, dV, dLight, mode, texture and size. +- The library's perspective path splits each span into runs of 16 pixels + with affine interpolation inside each run. + +### Behaviour measured on Core 12847 (test-maggie: 0 failures) + +- Length 1 to 64 writes exactly that many pixels, at any start pixel. +- U/V: 8:24, 1.0 = one texture width, point sampled. Negative dU works; U and + V wrap at 1.0. +- DXT1 blocks are row-major, colours and indices little-endian. Index 3 with + col0 ≤ col1 leaves the target pixel unchanged. +- Light 0 gives black. 16-bit output is RGB565. +- 32-bit colours with light `$FFFF` and tint `$FFFFFFFF`: RGB565 `$F800` + gives `$00FC0000`, white gives `$00FCFCFC`. The alpha byte is 0; full light + gives `$FC`, not `$FF`. + +## Differences from AmigaOS / AROS / 68060 + +Classic Amiga chipsets have no texture-mapping unit. Maggie is SAGA specific +and not covered by the classic hardware reference. + +## Do / Don't + +- DO write the start + length register last: it starts the span. +- DO zero the second-order dU/dV registers once. +- DON'T use the counters at `$DFF300` for timing or sync on Core 12847. +- See the pitfalls: depth test, the last 8 bytes of a span, and span usage. diff --git a/core/saga-audio-arne/index.md b/core/saga-audio-arne/index.md index 71a1f82..1aa9555 100644 --- a/core/saga-audio-arne/index.md +++ b/core/saga-audio-arne/index.md @@ -16,15 +16,17 @@ sources: Arne, the SAGA audio chip. -**This folder has no content yet.** If your task depends on it, tell the -user that the knowledge base does not cover the SAGA audio (Arne) yet. Do not fill the -gap with classic Amiga or 68060 knowledge. +**This folder is only partly covered.** `registers.md` has the registers and +values used in V4 audio driver work. For anything else, tell the user that +the knowledge base does not cover it yet. Do not fill the gap with classic +Amiga (Paula) or 68060 knowledge. ## Details ### Planned files -- `registers.md`: register map with offsets, bits and access rules +- `registers.md`: partial register reference (exists; the full map is still + missing) - One file per feature, for example channels or sample formats ### For contributors diff --git a/core/saga-audio-arne/registers.md b/core/saga-audio-arne/registers.md new file mode 100644 index 0000000..ee83715 --- /dev/null +++ b/core/saga-audio-arne/registers.md @@ -0,0 +1,67 @@ +--- +title: ARNE audio registers (partial) +type: reference +owner: TuKo1982 +status: draft +origin: ai-assisted +basis: [measured] +components: [saga-audio-arne] +applies_to: + core: "Core 12841" + os: [any] +tags: [arne, audio, saga, registers, dma, interrupts] +related: + - core/saga-audio-arne/index.md + - core/cpu-68080/core-at-a-glance.md +sources: + - "TuKo1982, 68080 compilation and optimisation guide (2026-10-30), sections 1 and 13" +--- + +## Summary + +A partial register reference for SAGA audio (ARNE), from driver work on the +V4. It covers the values and registers the guide uses; the full register map +is not documented here yet. + +## Details + +### Channel registers + +| Register | Meaning | +|---|---| +| PER (period) | Minimum period (MINPER) is 65 | +| LEN | 32-bit count of **sample pairs** | +| VOL | Left volume in bits 15–8, right volume in bits 7–0 | + +The base addresses of the channel registers are not given in the source. + +### Control and interrupt registers + +| Register | Address | Meaning | +|---|---|---| +| DMACON2R | `$DFF202` | DMA control, read | +| INTENA2R | `$DFF21C` | Interrupt enable, read | +| INTREQ2R | `$DFF21E` | Interrupt request, read | + +- In `INTENA2`/`INTREQ2`, bit n is the interrupt of channel AUD(n+4). +- The ARNE audio interrupt vector offset is `$50`. + +### Behaviour + +- The high-quality rate caps at about 66 kHz: decimate input above 64 kHz. +- ARNE can DMA from Fast RAM. +- Periods are based on the PAL colour clock of 3546895 Hz, as on Paula. + +## Differences from AmigaOS / AROS / 68060 + +- Paula has 4 channels with registers for channels 0–3; ARNE's extra + channels are controlled through the second set of DMA and interrupt + registers (`DMACON2`, `INTENA2`, `INTREQ2`). +- Paula can only DMA from Chip RAM; ARNE can DMA from Fast RAM. + +## Do / Don't + +- DO keep the period at 65 or higher. +- DO give LEN in sample pairs, not words. +- DON'T guess the remaining ARNE registers: the knowledge base does not + cover them yet. From 0fc65dce56b85eefb2214d1d2d7913830f246fba Mon Sep 17 00:00:00 2001 From: Willem Drijver Date: Tue, 3 Nov 2026 14:58:51 +0300 Subject: [PATCH 2/2] Fix a line that Markdown read as a list item Co-Authored-By: Claude Opus 5.5 --- core/cpu-68080/branches-and-prediction.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/cpu-68080/branches-and-prediction.md b/core/cpu-68080/branches-and-prediction.md index d39f7d2..362b9fa 100644 --- a/core/cpu-68080/branches-and-prediction.md +++ b/core/cpu-68080/branches-and-prediction.md @@ -71,8 +71,8 @@ Measured: 3.0 cycles whatever the pattern, never mispredicted (M). ### 080 branch sizes - `bcc.s+`: an odd displacement byte gives +128..+254 and −256..−132. −130 is - impossible (`$FF` means `.l`). Byte = displacement − 127 forward, - + 129 backward. + impossible (`$FF` means `.l`). Byte = displacement − 127 forward, and + displacement + 129 backward. - `dbcc.l`: an odd displacement word means a 32-bit counter (displacement word = displacement + 1).