From f1e1299a0c1c59bd4be556163a964de397f5db01 Mon Sep 17 00:00:00 2001 From: julian-kominiak Date: Mon, 16 Nov 2026 19:19:43 +0100 Subject: [PATCH] Optimal-parse packer, section 4's in-place loader margin, survey of public DN2 work scripts/optpack.c: a cost-optimal parse in dnfw's aPLib format (4.6-5.3 % smaller MAIN OS streams than dnfw's greedy parse). scripts/inplace_margin.c: the margin section 4's in-place loader needs. setup.md: both, plus a 2026-11-16 survey of public projects. Co-Authored-By: Claude Opus 5.5 --- .../references/firmware.md | 4 +- .../references/setup.md | 26 ++++++ .../scripts/inplace_margin.c | 31 +++++++ .../scripts/optpack.c | 82 +++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 skills/digitone-2-modding-skill/scripts/inplace_margin.c create mode 100644 skills/digitone-2-modding-skill/scripts/optpack.c diff --git a/skills/digitone-2-modding-skill/references/firmware.md b/skills/digitone-2-modding-skill/references/firmware.md index a965530..6f41e38 100644 --- a/skills/digitone-2-modding-skill/references/firmware.md +++ b/skills/digitone-2-modding-skill/references/firmware.md @@ -11,8 +11,8 @@ One DSP block = 32 samples at 48 kHz = 1,500 blocks / s; budget ~666,667 cycles ## Container, build, sign [verified] `.syx` = SysEx (`F0 00 20 3C 15 00 …`, 8-in-7 packing) → 8-byte preamble (length, checksum) → **ELE3** container of -aPLib-compressed sections: 5 meta, 2 bootstrap (holds the signing key material — never edit), **3 MAIN OS**, 4 updater, -**7 SHARC**, 8 accessory ARM. Integrity = per-packet checksum + 32-bit content checksum + HMAC-SHA256 trailer: dnfw +aPLib-compressed sections: 5 meta, 2 bootstrap (holds the signing key material — never edit), **3 MAIN OS**, 4 "updater" +(a factory board test that can also boot the OS: setup.md "Section 4's in-place loader"), **7 SHARC**, 8 accessory ARM. Integrity = per-packet checksum + 32-bit content checksum + HMAC-SHA256 trailer: dnfw recomputes all of it (`dnfw build`, or its API as cfmod does). `dnfw extract` recovers sections to diff against stock. ## Memory a mod may use [HW-proven] diff --git a/skills/digitone-2-modding-skill/references/setup.md b/skills/digitone-2-modding-skill/references/setup.md index a942ca1..31e01ce 100644 --- a/skills/digitone-2-modding-skill/references/setup.md +++ b/skills/digitone-2-modding-skill/references/setup.md @@ -26,6 +26,18 @@ Elektron's code. padded to 101-byte packets). Over it the upload stalls and the unit keeps its old OS. Stock 1.11 uses ~1.87 MB: your mods share ~220 KB. `cfmod.flash_check` measures it (aPLib is nonlinear: measure, never estimate). Big DSP tables: store them compressible (delta-coded, byte-planar), zero what nothing reads. +- **Packer headroom [measured, not HW-proven]:** dnfw's packer parses greedily (1-step lazy). `scripts/optpack.c` + (`cc -O2 -o optpack optpack.c`; `optpack RAW OUT [CHAIN]`) does a cost-optimal parse in the same format, window and + 2,048-byte match cap. On a ~3.3 MB modded MAIN OS it came out 4.6-5.3 % smaller than dnfw at chain 192 (chain 256 / 4096), + and on a modded section 7 2.6 % smaller: tens of KB under the 2 MiB buffer. Check every stream with + `dnfw.codec.aplib.depack` and `dnfw inspect` (window) before building with it. +- **Section 4's in-place loader** (1.12; found and run under Unicorn by naturarum/dn2-cfw, `notes/T1.2`): when the power-on + panel key read times out, the bootstrap runs section 4 (a factory board test, not an updater). It stages section 3 + with its 8-byte header at 0x40200000 and depacks in place to 0x40000400, so the stream has to stay ahead of the writer: + margin = 2,096,128 + stored - unpacked. Stock 1.12 keeps +31,181 B. A mod that grows the unpacked MAIN OS by more than + that, net of stream growth, hangs on this path (a better packer makes it worse). Nothing is written to flash there, and + a power cycle with the panel answering boots normally. Measure it with `scripts/inplace_margin.c STORED 8 2096136`, on + `dnfw extract --stored --section 3`. The normal path stages at 0x40400000 and has megabytes to spare. - **Never change a section's unpacked length by in-place edits**: ColdFire code goes to the appended area (dnfw loader), DSP code into new boot-stream blocks (`dspblocks`); section 7 must stay under 1 MiB (the ColdFire's DSP loader refuses more: no DSP boot). @@ -54,3 +66,17 @@ is ignored. chunks sit at 0x467c8000..0x467fffff -- if you combine with them, keep your code / RAM elsewhere (cfmod defaults to 0x46800000). - elektronmods.com (community mods, mostly DN1 / DT1); Elekloader (irpina: cross-device loader with conflict detection). +- Survey of GitHub, 2026-11-16 (read with `gh api repos/OWNER/REPO/contents/PATH`): + - **mischa85/elektron-firmware-tool** (C, MIT): inspect, byte-exact `-r` repack and a `-c` replace for ELE3 / ELE2 / ELEK / + AD / pre-ELE containers, plus the optimal-parse packer behind `scripts/optpack.c`. + - **naturarum/dn2-cfw** (Digiwerk, 1.12): the most detailed public DN2 notes: both main-OS loaders, the upgrade validator + (no build floor), free memory, Overbridge, SHARC code size, a 12-check pre-flash gate (`tools/brickcheck`), `sharcvm`. + - **ElekLabs/digikit** (formerly m-dwyer/digikit): now a runtime that loads machines and effects as modules from the +Drive, + so they need no reflash. `docs/FINDINGS.md`, `SHARC-ADDRESS-MAP.md`, `MIDI-SYSEX-RPC.md`. + - **armio/dn2-dt2-firmware-research**: Digitakt II sampler machines on the DN2, a sample filesystem in unused flash, + 42-channel USB audio. + - **xerydian/elektron-emu**: VST3s that run the real OS. Its Unicorn 2.1.4 m68k patches fix EMAC MAC-with-load decoding + and saturation, enable ISA_C on cfv4e, and keep the TLB fast path when only a few pages carry memory hooks. + - **elekloader/telos** (mods kept on the unit, DN2 planned); DN2 1.12 patchers **terrain-dn2**, **nzimas/digirust**; + **Donmanouche/OpenBridge-Digitone-2** (Linux Overbridge); **ethernet8023/rytm-fw-re** (Analog Rytm MKII, the same ColdFire + family). diff --git a/skills/digitone-2-modding-skill/scripts/inplace_margin.c b/skills/digitone-2-modding-skill/scripts/inplace_margin.c new file mode 100644 index 0000000..31cb209 --- /dev/null +++ b/skills/digitone-2-modding-skill/scripts/inplace_margin.c @@ -0,0 +1,31 @@ +/* worst in-place margin of an Elektron aPLib stream: min over tokens of gap + read_pos - write_end. usage: margin FILE SKIP GAP */ +#include +#include +#include +static const uint8_t *d; static size_t n, pos; static uint32_t tag; +static int byte(void) { return pos < n ? d[pos++] : 0; } +static int bit(void) { tag <<= 1; if (!(tag & 0xff)) { int v = byte(); tag = (v << 1) | 1; return (v >> 7) & 1; } return (tag >> 8) & 1; } +static uint32_t gam(void) { uint32_t v = 1; do { v = (v << 1) + bit(); } while (!bit() && v <= 0x2000000); return v; } +int main(int c, char **a) { + FILE *f = fopen(a[1], "rb"); fseek(f, 0, 2); size_t len = ftell(f); rewind(f); + uint8_t *buf = malloc(len); fread(buf, 1, len, f); fclose(f); + size_t skip = atoi(a[2]); long gap = atol(a[3]); d = buf + skip; n = len - skip; + long produced = 0, worst = 1L << 40, wat = 0; uint32_t last = 1; + for (;;) { + long cnt; + if (bit()) { pos++; cnt = 1; } + else { + uint32_t g = gam(), off; + if (g == 2) off = last; + else { off = (g << 8) + byte(); if (off == 767) break; off -= 767; last = off; } + int s = 2 * bit(); s += bit(); + uint32_t ln = s ? s : gam() + 2; if (off > 3328) ln++; + cnt = ln + 1; + } + long m = gap + (long)pos - (produced + cnt); + if (m < worst) { worst = m; wat = produced; } + produced += cnt; + if (pos > n) { puts("overrun"); return 1; } + } + printf("out %ld in %zu worst %ld at out %ld\n", produced, pos, worst, wat); +} diff --git a/skills/digitone-2-modding-skill/scripts/optpack.c b/skills/digitone-2-modding-skill/scripts/optpack.c new file mode 100644 index 0000000..7100ead --- /dev/null +++ b/skills/digitone-2-modding-skill/scripts/optpack.c @@ -0,0 +1,82 @@ +/* Optimal-parse packer for Elektron's aPLib variant (the stream dnfw codec/aplib.depack reads), after the idea in + * mischa85/elektron-firmware-tool (cost-optimal forward DP). See references/setup.md "Packer headroom". + * Forward DP over positions; state per position = (cost, last offset of that path). + * usage: optpack IN OUT [chain] */ +#include +#include +#include +#include + +#define BIAS 767u +#define FAR 3328u +#define MINM 2u +#define MAXM 2048u +#define MAXOFF 1044480u +#define HS (1 << 16) +#define INF 0xFFFFFFFFu + +static int gc(uint32_t v) { int n = 0; for (; v; v >>= 1) n++; return 2 * (n - 1); } +static int mcost(uint32_t off, uint32_t L, uint32_t lo) { + int b = 1 + (off == lo ? 2 : gc((off + BIAS) >> 8) + 8); + uint32_t base = L - 1 - (off > FAR); + return b + (base <= 3 ? 2 : 2 + gc(base - 2)); +} + +static uint8_t *out; static size_t on, tagp; static int tagb; +static void bit(int v) { if (!tagb) { tagp = on; out[on++] = 0; tagb = 8; } if (v) out[tagp] |= 1 << (tagb - 1); tagb--; } +static void byte(uint8_t v) { out[on++] = v; } +static void gam(uint32_t v) { int w = 0; for (uint32_t t = v; t; t >>= 1) w++; for (int i = w - 2; i >= 0; i--) { bit((v >> i) & 1); bit(i == 0); } } + +int main(int argc, char **argv) { + FILE *f = fopen(argv[1], "rb"); fseek(f, 0, 2); size_t n = ftell(f); rewind(f); + uint8_t *d = malloc(n + 8); fread(d, 1, n, f); fclose(f); + int chain = argc > 3 ? atoi(argv[3]) : 256; + uint32_t *cost = malloc((n + 1) * 4), *lo = malloc((n + 1) * 4), *toff = malloc((n + 1) * 4), *tlen = malloc((n + 1) * 4); + int32_t *from = malloc((n + 1) * 4), *prev = malloc(n * 4), *head = malloc(HS * 4); + for (size_t i = 0; i <= n; i++) cost[i] = INF; + for (int i = 0; i < HS; i++) head[i] = -1; + cost[0] = 0; lo[0] = 1; +#define RELAX(p, c, l, fr, o, ln) do { uint32_t c_ = (c); if (c_ < cost[p]) { cost[p] = c_; lo[p] = (l); from[p] = (fr); toff[p] = (o); tlen[p] = (ln); } } while (0) + for (size_t i = 0; i < n; i++) { + uint32_t bi = cost[i], loi = lo[i], cap = n - i > MAXM ? MAXM : (uint32_t)(n - i); + RELAX(i + 1, bi + 9, loi, (int32_t)i, 0, 0); + if (i + 1 < n) { + if (loi <= i && loi <= MAXOFF) { + uint32_t r = 0; while (r < cap && d[i - loi + r] == d[i + r]) r++; + for (uint32_t L = (loi > FAR ? MINM + 1 : MINM); L <= r; L++) RELAX(i + L, bi + mcost(loi, L, loi), loi, (int32_t)i, loi, L); + } + int32_t j = head[(d[i] << 8) | d[i + 1]]; int c = chain; uint32_t pm = 1; + while (j >= 0 && c--) { + uint32_t off = (uint32_t)(i - j); + if (off > MAXOFF || pm >= cap) break; + if (d[j + pm] == d[i + pm]) { + uint32_t l = 0; while (l < cap && d[j + l] == d[i + l]) l++; + if (l > pm) { + uint32_t mn = off > FAR ? MINM + 1 : MINM, s = pm + 1 > mn ? pm + 1 : mn; + for (uint32_t L = s; L <= l; L++) RELAX(i + L, bi + mcost(off, L, loi), off, (int32_t)i, off, L); + pm = l; + } + } + j = prev[j]; + } + uint32_t h = (d[i] << 8) | d[i + 1]; prev[i] = head[h]; head[h] = (int32_t)i; + } + } + size_t np = 0; for (int32_t j = n; j > 0; j = from[j]) np++; + uint32_t *path = malloc(np * 4); size_t k = np; for (int32_t j = n; j > 0; j = from[j]) path[--k] = j; + out = malloc(n * 2 + 64); + uint32_t last = 1; + for (k = 0; k < np; k++) { + uint32_t p = path[k]; int32_t s = from[p]; + if (!tlen[p]) { bit(1); byte(d[s]); continue; } + uint32_t off = toff[p], L = tlen[p]; + bit(0); + if (off == last) gam(2); else { uint32_t raw = off + BIAS; gam(raw >> 8); byte(raw & 0xff); last = off; } + uint32_t base = L - 1 - (off > FAR); + if (base <= 3) { bit(base >> 1); bit(base & 1); } else { bit(0); bit(0); gam(base - 2); } + } + bit(0); gam(0x1000002); byte(0xff); + f = fopen(argv[2], "wb"); fwrite(out, 1, on, f); fclose(f); + printf("%s: %zu -> %zu (chain %d)\n", argv[1], n, on, chain); + return 0; +}