Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions core/pitfalls/cacr-data-cache-bit-ignored.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Clearing the data-cache enable bit does not turn the data cache off
type: pitfall
owner: WDrijver
status: draft
origin: ai-assisted
basis: [measured]
components: [cpu-68080]
applies_to:
core: "Core 12856"
os: [any]
tags: [68080, cache, cacr, cachecontrol, enabled, data-cache]
related:
- core/cpu-68080/caches-and-memory.md
- core/pitfalls/self-modifying-code-and-caches.md
sources:
- "Hardware test tools/v4/tests/dcache-enable-bit, V4 UniCorn, Core 12856, ApolloOS R10, 2026-11-17"
---

## Symptom

- Code clears the data-cache enable bit and expects uncached reads. Reads
still hit the cache and stay just as fast.
- Tools that show the data cache state from `CacheControl()` or CACR report
"data cache off" while the data cache is in use.

## Cause

- CACR bit 31 (DE, `CACRF_EnableD` through `CacheControl()`) can be written and
read back: the raw CACR changes from `$00008000` to `$80008000` and back.
- Load latency does not change with it. A dependent load that hits a 64 KB
buffer costs 3 cycles with the bit clear or set. A 4 MB buffer that cannot
fit costs about 28.6 cycles per load (M).
- So the core keeps its data cache on whatever the bit says.

## Wrong

```c
/* Read a buffer uncached, e.g. to see what a bus master wrote. */
ULONG old = CacheControl(0, CACRF_EnableD); /* "data cache off" */
check(buffer); /* may still read cached lines */
CacheControl(old, CACRF_EnableD);
```

## Right

Do not rely on the enable bit for uncached access or to slow a program
down. Assume the data cache is always on. Whether `CachePreDMA()` /
`CachePostDMA()` or `CacheClearE()` keep data written by other bus masters
coherent on the 68080 is not covered by this measurement.

## Detection

Search for `CACRF_EnableD`, `CacheControl(` with a mask that includes it,
and `movec` writes to `cacr` that clear bit 31. Measure with
`tools/v4/tests/dcache-enable-bit`.

## Evidence

| Claim | Source | Core | Method |
|---|---|---|---|
| CACR bit 31 is writable and reads back | (M) 2026-11-17 | Core 12856, V4 UniCorn | `tools/v4/tests/dcache-enable-bit`: raw CACR found `$00008000`, after `CacheControl(CACRF_EnableD, CACRF_EnableD)` `$80008000`, restored `$00008000` |
| Bit 31 does not change data-load latency | (M) 2026-11-17 | Core 12856, V4 UniCorn | Same test, under `Disable()`: pointer chase, one load per 64-byte line in random order, 16384 loads, best of 8: 49161 cycles as found (bit clear), with `CacheControl(0, CACRF_EnableD)`, and with the bit set; 5 runs, identical |
| The test can see a cache miss | (M) 2026-11-17 | Core 12856, V4 UniCorn | Same chase over 4 MB: 468071–470680 cycles (about 28.6 per load against 3 per load in 64 KB) |
| A sequential sum is not a valid test | (M) 2026-11-17 | Core 12856, V4 UniCorn | A 4-long unrolled sum of 64 KB vs a 4 MB stream: 28702 vs 28680 cycles per 64 KB. Prefetch hides the misses, so equal timings there prove nothing about the cache |
115 changes: 115 additions & 0 deletions tools/v4/tests/dcache-enable-bit/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* dcache-enable-bit: does the data-cache enable bit change data-read speed on the 68080?
*
* Pointer-chase a 64 KB fast-RAM buffer (fits the data cache) under Disable(), with
* the data cache as found, explicitly off (CacheControl(0, CACRF_EnableD))
* and explicitly on. As a baseline that cannot hit the cache, chase a 4 MB
* buffer. 16384 dependent loads per measurement, best of 8. The raw CACR is read with
* movec in supervisor mode. The old setting is restored.
*
* v4:variant default -O2 -m68080 -fomit-frame-pointer
*/
#include "../akbtest.h"
#include <exec/memory.h>

#define SMALL 65536
#define BIG (4UL * 1024 * 1024)
#define ROUNDS 8

/* Pointer chase: one long per 64-byte line, lines visited in a random
* order, so every load waits for the previous one (latency, no prefetch). */
static void build(unsigned long *p, unsigned long bytes)
{
unsigned long lines = bytes / 64, i, j, t, seed = 12345;
unsigned long *perm = (unsigned long *)p; /* use slot 1 of each line as scratch */
for (i = 0; i < lines; i++) perm[i * 16 + 1] = i;
for (i = lines - 1; i > 0; i--) {
seed = seed * 1103515245UL + 12345UL;
j = (seed >> 8) % (i + 1);
t = perm[i * 16 + 1]; perm[i * 16 + 1] = perm[j * 16 + 1]; perm[j * 16 + 1] = t;
}
for (i = 0; i < lines; i++) {
unsigned long a = perm[i * 16 + 1], b = perm[((i + 1) % lines) * 16 + 1];
p[a * 16] = (unsigned long)&p[b * 16];
}
}

static unsigned long chase(unsigned long *p, unsigned long n)
{
unsigned long *q = p;
while (n--) q = (unsigned long *)*q;
return (unsigned long)q;
}

#define LOADS 16384
static unsigned long timed(unsigned long *p, unsigned long *sum)
{
unsigned long best = ~0UL, c, r;
*sum += chase(p, LOADS); /* train, fill cache */
for (r = 0; r < ROUNDS; r++) {
c = akb_ccc();
*sum += chase(p, LOADS);
c = akb_ccc() - c;
if (c < best) best = c;
}
return best;
}

static unsigned long raw_cacr;

static void __attribute__((interrupt)) read_cacr(void)
{
unsigned long v;
__asm__ volatile ("movec %%cacr,%0" : "=d"(v));
raw_cacr = v;
}

static unsigned long cacr(void)
{
Supervisor((ULONG (*)())read_cacr);
return raw_cacr;
}

int main(void)
{
unsigned long *buf, *big, sum = 0, found, off, on, miss;
unsigned long f0, r0, roff, ron;

AKB_BEGIN("dcache-enable-bit");
akb_machine();
buf = AllocMem(SMALL, MEMF_FAST | MEMF_PUBLIC);
big = AllocMem(BIG, MEMF_FAST | MEMF_PUBLIC);
if (!buf || !big) {
AKB_FAIL("alloc", "no fast RAM");
return AKB_END();
}
build(buf, SMALL);
build(big, BIG);

f0 = CacheControl(0, 0);
r0 = cacr();
Disable();
found = timed(buf, &sum);
CacheControl(0, CACRF_EnableD);
off = timed(buf, &sum);
Enable();
roff = cacr();
Disable();
CacheControl(CACRF_EnableD, CACRF_EnableD);
on = timed(buf, &sum);
miss = timed(big, &sum);
Enable();
ron = cacr();
CacheControl(f0, CACRF_EnableD); /* back as it was */

AKB_INFO("CacheControl as found $%08lX; raw CACR found $%08lX, EnableD off $%08lX, EnableD on $%08lX, restored $%08lX",
f0, r0, roff, ron, cacr());
AKB_VALUE("cycles-as-found", found, "per-16K-loads");
AKB_VALUE("cycles-enabled-off", off, "per-16K-loads");
AKB_VALUE("cycles-enabled-on", on, "per-16K-loads");
AKB_VALUE("cycles-4MB-chase", miss, "per-16K-loads");
AKB_INFO("sum %lu", sum);
AKB_CHECK("restored", CacheControl(0, 0) == f0, "CacheControl not restored");
FreeMem(big, BIG);
FreeMem(buf, SMALL);
return AKB_END();
}
Loading