Files
fmini-1/tests/target_budget.py
T
Claude 232264d571 FMini-1 v1.7.1: fmug is now fmini everywhere (files, identifiers, engine name, editor, docs)
The project's first name, FMUG, is gone. Nothing about the sound or the stored data changes.

- Files: eng_fmini.c, fmini_app.c, ui_fmini.c, editor_fmini.c, eng_fmini_filter.h, eng_fmini_presets.h, the tests, the
  tools (fmini_bank.py, fmini_filter.py, fmini_retune.py, fmini_levels.json, gen_fmini_presets.py) and
  web/make_fmini_installer.py.
- Identifiers: fmug_ / FMUG_ / FELUCCA_FMUG_ONLY became fmini_ / FMINI_ / FELUCCA_FMINI_ONLY; the editor commands are
  FMINI_GET / FMINI_PUT / FMINI_PRESET (numbers 82-84 unchanged). The engine reports the name "FMINI" (was "FMUG") on the
  device and in the editor's engine list; the editor still accepts "FMUG" from firmware up to 1.7.0.
- Short forms fmg_ / GR_FMG0..6 and FG_* are left as they are.
- Tables: tests/golden.txt and tests/target_budget.txt only had their keys renamed.
- Docs: CLAUDE.md and DEVELOPING.md say there is one branch (main) and record the rename; the test-suite notes now describe
  the second regress section (FELUCCA_CPU2=0: 41 FMINI patches differ from the golden, as on 1.7.0).
- Site: docs/emu and docs/index.html rebuilt for v1.7.1; the splash pictures show the new version.

Checked: the firmware's instructions are identical to 1.7.0 (only the addresses of string literals move); the 100 FMINI golden
hashes are unchanged; every screen of the README shots is pixel-identical except the splash; the preset lab's metrics for the
100 patches are identical; the host suite fails in the same seven sections as before (fm4, ui_render, three regress, union_test,
web pages) and nowhere else. Not tested on a real FM-1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
2026-10-20 19:35:44 -03:00

143 lines
8.2 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
"""Target-side cost estimate of the hot DSP code, from the pi32v2 objdump listing tools/build.py writes
(build/felucca.dis). For each engine's render function and the audio ISR: the instructions
inside loops (every address range closed by a backward branch: the per-sample loops and what they
contain), and the hardware divides and calls in them. cost = the loop instructions, each weighted 4 per
level of nesting (an inner loop runs several times per sample) and 1 + 8 for a divide (many cycles). A static count, not a cycle count: it changes only when the compiled code changes,
so it is exact run to run, and it catches a render loop that grew (more work per sample, a new divide,
something not inlined any more). Compared with tests/target_budget.txt at +10 %. The host
CPU check (regress.c) is the primary one; this one sees the target compiler's code.
tests/target_budget.py [DIS [BUDGET]] BUDGET_UPDATE=1 rewrites BUDGET"""
import os
import re
import sys
FUNCS = ["fmini_render", "fmini_wave", "fmini_block", "analog_render", "digital_render", "digital_render_legacy", "digital_render_custom", "phase_render", "phase_render_f", "lofi_render", "sample_render", "formant_render",
"trio_render", "trio_pass", "wheel_render", "wheel_block",
"grain_render", "grain_block", "phys_render", "drum_render", "noise_render", "fm6_render", "fm6_op_run", "fm6_op_fb", "px_modal_block", "px_modal_run", "px_memb_block",
"px_string_excite", "px_string_run", "px_symp_run",
"dv_metal_run", "dv_kick_run", "dv_snare_run", "dv_clap_run", "dv_hat_run", "dv_tom_run", # drum_voice.c
"dv_rim_run", "dv_bell_run", "dv_cym_run", "dv_out", "dv_metal_mix",
"slicer_track",
"slice_render", "slc_rev", # SLICE (eng_slice.c): the render, the reverse windows
"fm1_alnk0_irq", "fm1_timer5_irq", # the audio ISR; TIMER5: the key / LED scan (hal/fm1_input.h)
"mix_part", # a part's block (fx.c), on either core since the second
"uac_tap48", # the USB audio input at 48 kHz: the 44.1 -> 48 resampler (usb.c)
"click_render", # the metronome's click (click.c), while it sounds
"mod_begin", "mod_voice", "mod_end", # the modulation matrix (mod.c), called when active
"perf_begin", "perf_mute", "perf_pre", "perf_block", "perf_master", # the FX layer (perform.c), when busy
"perf_flanger", "perf_phaser", # .. FLANGER, PHASER (1.2), while held or fading
"perf_throw", # .. REV / ECHO THROW (1.5.1): per part, while held or gliding
"rev_room", "rev_spring", "rev_hall", "rev_side_mix", # the reverb bus (fx.c): REVERB TYPE ROOM / SPRING / HALL (and its stereo)
"spring_one", "rev_plate", "dly_block", "cho_block", "track_dist", # FMini-1's FX: a spring, PLATE, the seven delays, the three choruses, DRIVE
"track_insert", "ins_drive", "ins_swept"] # the per-track INSERT (fx.c, 1.5), while a TYPE plays
# built only with FELUCCA_FM4=1 (DIGITAL, src/eng_digital.c; not in the default build, so not in BUDGET): absent,
# they are skipped; present, checked against these (their budget lines until the engine was retired in 1.0)
OPTIONAL = {"digital_render": 12, "digital_render_legacy": 333, "digital_render_custom": 558,
"uac_tap48": 504, # (absent from a FELUCCA_UAC_48K=0 or FELUCCA_UAC=0 build)
"mix_part": 4874} # (FELUCCA_CPU2=1, the default since 1.6.1: out of line, either core may run it; 2800 until FMini-1's smoothing, TUBE and the stereo sends)
# a FELUCCA_CPU2=0 build (no mix_part: inlined into the audio ISR, as 1.6) has the one-core ISR, checked against this
ONE_CORE = {"fm1_alnk0_irq": 37502}
TOL = 0.10 # exact (no noise): small edits pass, a grown render loop does not
DIV_W = 8 # a divide weighs 1 + 8 instructions
NEST = 4 # an instruction in a loop inside a loop weighs 4, two deep 16, ...
MAXD = 4
dis = sys.argv[1] if len(sys.argv) > 1 else "build/felucca.dis"
budget = sys.argv[2] if len(sys.argv) > 2 else "tests/target_budget.txt"
LABEL = re.compile(r"^([A-Za-z_][A-Za-z_0-9.]*):$")
INSN = re.compile(r"^\s*([0-9a-f]+):\s+((?:[0-9a-f]{2} )+)\s*(.*)$")
TARGET = re.compile(r"goto -?\d+ <[^>]*: ([0-9a-f]+) >")
def functions(path):
"""{name: [(addr, text)]}: a function runs to the next label not starting with '.' (the compiler's
jump-table labels are inside functions); jump-table data ('< n : 0x.. >') is left out"""
out, cur = {}, None
with open(path) as f:
for line in f:
m = LABEL.match(line.strip())
if m:
name = m.group(1)
if not name.startswith("."):
cur = out.setdefault(name, []) if name in FUNCS else None
continue
m = INSN.match(line)
if cur is not None and m and not m.group(3).lstrip().startswith("<"):
cur.append((int(m.group(1), 16), m.group(3).strip()))
return out
def cost(insns):
"""loops: one span per loop head (the farthest backward branch to it); an instruction inside d
spans weighs NEST ** (d - 1) (an inner loop runs several times per pass of the outer one)"""
lo = insns[0][0]
heads = {}
for a, t in insns:
m = TARGET.search(t)
if m and lo <= int(m.group(1), 16) < a:
h = int(m.group(1), 16)
heads[h] = max(heads.get(h, a), a)
n = divs = calls = 0
w = 0
for a, t in insns:
d = min(sum(1 for h, e in heads.items() if h <= a <= e), MAXD)
if not d:
continue
k = NEST ** (d - 1)
n += 1
if re.search(r"= r\d+ / r\d+", t):
divs += 1
k *= 1 + DIV_W
calls += t.startswith("call")
w += k
return {"insns": len(insns), "loop": n, "div": divs, "call": calls, "cost": w}
def main():
if not os.path.exists(dis):
print(f"target: skip ({dis} missing: run ./build.sh)")
return 0
fns = functions(dis)
res = {n: cost(fns[n]) for n in FUNCS if fns.get(n)}
missing = [n for n in FUNCS if n not in res and n not in OPTIONAL]
base = {}
if os.path.exists(budget):
for line in open(budget):
p = line.split()
if len(p) >= 2 and not line.startswith("#"):
base[p[0]] = int(p[1])
if os.environ.get("BUDGET_UPDATE"):
with open(budget, "w") as f:
f.write("# FELUCCA target cost budget (tests/target_budget.py): instructions in the loops of each\n"
f"# function in build/felucca.dis, x{NEST} per nesting level, divides x{1 + DIV_W}. The check allows "
f"+{TOL * 100:.0f} %.\n# Rewritten by BUDGET_UPDATE=1.\n")
for n, r in res.items():
if n not in OPTIONAL:
f.write(f"{n} {r['cost']}\n")
print(f"target: budget {budget} rewritten ({len(res)} functions)")
fail = 0
for n in missing:
print(f"target: FAIL {n} not found in {dis} (renamed? inlined? update FUNCS)")
fail += 1
for n, r in res.items():
b = base.get(n, OPTIONAL.get(n))
if "mix_part" not in res and n in ONE_CORE:
b = ONE_CORE[n]
state = "no budget (BUDGET_UPDATE=1 adds it)" if b is None else "ok"
if b is not None and not os.environ.get("BUDGET_UPDATE"):
if r["cost"] > b * (1 + TOL):
state = f"OVER BUDGET (+{(r['cost'] / b - 1) * 100:.0f} %, limit +{TOL * 100:.0f} %)"
fail += 1
elif r["cost"] < b * (1 - TOL):
state = f"note: {(r['cost'] / b - 1) * 100:.0f} % (BUDGET_UPDATE=1 to keep it)"
print(f"target: {n:15s} {r['insns']:5d} instructions, {r['loop']:4d} in loops, {r['div']} divides, "
f"{r['call']:2d} calls there: cost {r['cost']:5d} (budget {b if b is not None else '-'}) {state}")
return 1 if fail else 0
if __name__ == "__main__":
sys.exit(main())