Files
JIANT/tests/cpu_lines_test.c
T
Claude 56e57cc5d4 V10.8.75: MATH USER, a formula of one's own on a MATH track (JIANT BYTE's formula machine); ALGO 128 .. 191 saved; the stop-side click
Juan: "Lo implementaría solo en math" (formulas of his own, not only the
factory ones).

MATH USER (firmware/src/math_user.c, new):
- The machine is JIANT BYTE's (its fvm.c, GPL-3.0, Juan's): the instructions
  BYTE's compiler makes from a text, serialized, with BYTE's operator numbers
  (FO_*) and meaning (FVM_OPS2 / FVM_OPS1, copied), run a sample at a time
  (BYTE runs each instruction over a block). Every stateless instruction:
  - INT (bytebeat): + - * / % & | ^ << >>, every comparison (unsigned and
    signed), && ||, min max;
  - FIX (floatbeat, Q16.16): MULF MULIF DIVF DIVFI MODF ITOF FTOI FLOOR;
  - NEG NOT LNOT ABS, sin tri saw sqr rnd (both kinds), SAT (clip, cos
    compile to these), ?: and the string tables;
  - the variables t, w (FLOAT's phase), a (VAR's 1..16), n v (note,
    velocity); BYTE's others read 0, as BYTE's own do where its host does
    not make them.
  Not here (refused when checked): ph() lp() hp() (a state per voice), eu(),
  FUNC's calls and func{}. DIVF uses the divider and at most 16 steps (no
  64-bit division linked); products through core.h's mul64.
- The bytes: [0] the type (bit 0 FIX) | 2 a table pool follows ([1] its
  length 1..32, its bytes); then op | md << 6 each, md 0 the stack, 1 a
  number (LEB128), 2 a variable (BYTE's FA_ / FU_ index), 3 t (no byte); TAB
  its table as a number. At most 64 bytes (MU_MAX), a stack of 12 (BYTE's
  FVM_DEPTH). B16 (65) and F43 (125) don't fit; every other of BYTE's 96
  factory texts does (14 .. 63 B).
- Checked when it arrives (mu_check): every op known, its operand whole, the
  stack never under 1 nor over 12, one left, a table inside its pool. The
  machine itself stays inside its stack (16, indexed & 15) and its bytes
  whatever they are (a set half-written while a block reads it); / and % by
  0 give 0, shifts & 31.
- One a track (mu_c, 6 x 65 B). Played at ALGO USER: P_E2 stored -4 (-4 ..
  -1), before B01 / F01. lofi_byte / lofi_float: case 63 (f = p >> 2,
  unsigned) in their jump tables, so MATH's own formulas take no test a
  sample (scene 0d's instructions below V10.8.74's).
  - BYTE (WAVE 5, 7): the result's low byte, with VAR, MOTION, LOOP, BEND,
    GRIT, CHIP, SYNC as MATH's own; a t already read is not read again (t
    moves at ~8 kHz at C4).
  - FLOAT (WAVE 6): BYTE's fvm_sample (FIX -1 .. 1; INT as bytebeat), w = x << 3,
    BYTE's phase in Q16 turns.
- The knob: lofi_desc gives a track that holds a formula (or is at USER)
  ALGO -4 .. 191, USER first (N_BYTEU / N_FLOATU: "USER\0" before B01 / F01
  in PNE, 10 B); a track without one keeps 0 .. 191 exactly as before, so
  DICE, the matrix and MIDI see the same ranges. params.c param_fit keeps
  -4 .. 191 for MATH's ALGO when a project / preset / automation is read.

Limits (Juan: "cuidá la CPU"):
- read at most 11025 times a second, at every rate (VH_SH) and hold (GRIT,
  4B/2): a bucket of two reads, so a t a little uneven under the cap is read
  as it comes; past it the value read last. Below about F4 (t under 11 kHz)
  BYTE wave is bit for bit BYTE's machine; above it, held.
- at most 4 voices of formulas of one's own sounding, every track's together
  (voice.c mu_cap: a fifth fades the oldest out); the voice admission
  (load_admit, measured) stays the backstop.
- one pass over at most 64 bytes a read, no loop but DIVF's 16 steps.

Storage:
- the project: FUNC's 'F' 'U' block after 'O' 'T' (the last: an older firmware
  stops before it), each track's length and bytes, when one holds a formula;
  checked again when read. proj_mu beside the project, not in it (the pool has
  32 B to spare): proj_scratch, the firmware's one project_t, reads and writes it.
  A project without the block loads none; CLEAR PROJECT clears them; UNDO and
  backups carry them. An older firmware loads ALGO -4 as B01.
- a user preset keeps ALGO USER but not the formula (its 192-byte record has
  ~12 bytes spare): the track's formula plays.
- a song's sections share the formulas loaded (as VOICE's lyrics).

ALGO 128 .. 191 saved (a bug since p6): MATH's ALGO above 127 (B33 .. B48,
F33 .. F48) made every project save fail with SAVE FORMAT ERROR 4 (proj_pack
refused a value over 127) and a user preset load it as B01 (its byte read
-128 .. -65). proj_pack takes MATH's ALGO to 191; projects and user presets
read -128 .. -65 back as 128 .. 191 (math_user.c mu_algo_in), param_fit keeps
it. Not fixed (separate, older): FLOAT's ALGO B (P_E0) is clamped to CHIP's
0..3 when a project loads (F01 .. F01).

Editor protocol 1.5 (editor.c):
- FORMULA (100): track, op (0 query, 1 set + pack7 of the bytes, 2 clear) ->
  track, rc (1: refused, the track's as it was), length, pack7. pack7 is
  BACKUP_PUT's groups (ed_unpack7 / ed_bk_pack).
- INFO tag 45 02 64 12 ('E', before 'C'); 'C' 43 02 01 05.
- web/EDITOR_PROTOCOL.md "1.5" (also in the site's, wt-ed formula-work).
- The emulator (web/emu) needs nothing more: web_ed carries FORMULA, so the
  editor's preview runs this machine.

The stop-side click (crash screen, UBOOT, update reset), commit 65691824 on
pop-fix, cherry-picked: fm1_audio_stop lowers PC6 first, keeps the DMA 5 ms
more, then stops (out of line); ota_commit stops the audio before its reset;
tests/audio_order_test.py (run_tests.sh). audio.c and the golden renders
unchanged.

Flash (V10.8.74: 96 B free; this work ~2.5 KB: the machine 1.7 KB, FORMULA
~0.35 KB, the project block ~0.3 KB, the ALGO fix and the descs ~0.15 KB, the
stop fix 54 B):
- MATH's 48 floatbeats (lofi_float) built for size: the sines a call each
  instead of inlined, half its size, -1682 B.
- a block's control and once-an-event sound functions built for size, none in
  HOT_FNS: the kit's sounds (dx_run_fx), the master's TAPE, a track's COMB,
  the kit's bus, DRONE CLAY's composer, the punch-in lane's step; a quiet tail
  looked for, a punch-in's SLICER let go, a fitted span, a punch-in slot, a
  kit step's hit, a note's VEL and HUMAN, a key's MIDI out, MATH USER's cap.
- tools/size_fns.py: math_user.c in SIZE_FILES (its main-loop side).
Image 204 B free (584132 of 584336), BT 36 B free (73692 of 73728), RAM
.data+.bss 184 B free (98120 of 98304: the formulas 390 B, their project copy
390 B, the cores' voices 64 B), pool spare 8224 B, RAM code 40384 of 42496 B.
audio.c is V23's. REPIN=keep; a plain build passes; tools/audio_layout.txt
and audio_map.txt recorded.

CPU (tests/cpu_lines_test.py), each scene's worst block, core 0/core 1,
V10.8.74 -> V10.8.75 (both measured now):
  101 78/44 -> 79/44   102 62/28 -> 62/28   103 85/14 -> 85/14
  104 51/14 -> 51/14   105 66/42 -> 66/41   106 52/36 -> 52/36
  107 63/11 -> 63/11   108 50/29 -> 51/29   0c  70/57 -> 69/58
  0d  71/51 -> 71/52   0e  --    -> 85/61
Instructions identical in 101 .. 108 and 0c; 0d's lower (135.5k / 160.4k ->
135.2k / 160.2k a block). The lines move by a few with the flash layout
(+-1 point, as before). New scenes: 0d the CPU TEST's song with its three
synths MATH (BYTEBEAT B02, MATH KEYS F28, MATH PAD F12: factory formulas,
what V10.8.74 runs too); 0e the same with the first two MATH USER (B03's
bytecode on BYTE, 37 B; F20's floatbeat in FIX on FLOAT, 63 B, the longest
of BYTE's factory ones that fit), their chords capped at four voices: +14 /
+9 points over 0d, opt-in.

Tests:
- tests/math_user_test.c (new, in the workflow and run_tests.sh):
  - BYTE: the 135 formulas of tests/math_user_gold.h (BYTE's own
    fvm_compile and fvm_run on BYTE's 96 factory texts and formulas of every
    operator: tools/math_user_gold.c, built in a BYTE checkout, and
    tools/math_user_gold.py) give BYTE's values, 3 x 4160 each; the two over
    64 bytes refused;
  - factory: MATH's own 47 bytebeats as bytecode give lofi_byte's values,
    every a, 2^17 + random t;
  - DIVF and MULF against 64-bit arithmetic, 2M random operands; BYTE's
    operator numbers;
  - check: lp ph eu, a push without a value, the stack under 1 / over 12 / two
    left, a number cut short or of six bytes, a header bit, a table out of its
    pool, 0 and 65 bytes refused; a refused set leaves the track's;
  - fuzz: 400000 random and mutated inputs, checked and run, and 400000 run
    unchecked (any length byte): no crash under ASan, 0.2 s;
  - voice: B01's bytecode at ALGO USER renders B01 bit for bit at C4; the cap
    holds at C7; FLOAT plays it; nine USER notes on three tracks: four sound;
  - storage: a project with two formulas, ALGO USER and ALGO 150 packs,
    loads, restores byte for byte; 'F' 'U' the last block; one without it
    loads none; user presets keep ALGO 150 and USER; the knob: USER only
    where a formula is.
- app_contract: INFO's 'E' 64 12 before 'C'; FORMULA's query, a set (pack7)
  read back, a malformed one refused (rc 1, the track's kept), no reply for
  track 9, a clear.
- project, descdump (identical to V10.8.74's), audio_order pass; editor_test
  fails the same 6 checks as V10.8.74.
- The local workflow: steps 2 .. 29 pass, the emulator job included (emsdk
  6.0.11: the wasm built, native-check, gen-web, resound-web, emu_test).
- The site's editor (wt-ed formula-work, not here): its FVM port of BYTE's
  compiler gives BYTE's bytes for all 135, its math_test and
  editor_browser_test pass against this commit's wasm (a formula sent to
  track 4 with FORMULA, ALGO -4, read back).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-30 11:12:39 +00:00

143 lines
7.7 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */
/* (V10.8.60, technique from JIANT BYTE byte-cpu-light) the scenes of the lines model (tests/cpu_lines_test.py): the
* FM-1's time follows the 32-byte lines of the flash the sound reads each block (code and constants), core by core, more
* than its instructions. Run under valgrind's lackey: each block of the scene between two stores to cl_mark, core 1's
* work (mc_job) between two to cl_c1, each part (fx.c mix_part_t) between two to cl_part[k].
* cpu_lines_test SCENE NBLK (SCENE hex: 101 GEN EXPLORE s123 ENERGY 85 with CLOUD, the delay and the chorus;
* 102 .. 108 other GEN songs; 0c SAVE > TOOLS 6 CPU TEST's song (TRACKS 4))
* CL_COST="c0,..,c5,fx" (us): each part's cost as the FM-1 measures it (mc_tk, mc_fx_tk), for the device's split */
#include <stdint.h>
#define UI_TEST_NO_MAIN 1
#define MC_HOST_ASYNC
static int cl_mc_run(void (*f)(uint32_t), uint32_t a);
#define mc_run(f, a) cl_mc_run(f, a)
#define mc_wait() 0
#define HOST_LIFE 0
volatile int cl_mark, cl_c1, cl_ex, cl_part[8];
#define CPU_LINES_PART(i, v) (cl_part[(i) & 7u] = (v))
#include "ui_test.c"
static __attribute__((noinline)) void core1_run(void (*f)(uint32_t), uint32_t a) { f(a); __asm__ volatile(""); }
static int cl_mc_run(void (*f)(uint32_t), uint32_t a)
{
if (!mc_host) return 0;
mc_host_cpu = 1;
cl_c1 = 1;
core1_run(f, a);
cl_c1 = 2;
mc_host_cpu = 0;
return 1;
}
static int32_t vo[2 * CTL];
static uint32_t blk;
static void move(void) /* the matrix's sources moving: the macros, AT, MODW */
{
uint32_t k, ph = blk++;
for (k = 0; k < NTRK; k++) { trk[k].at = (uint8_t)((ph * 5u + k * 17u) % 128u); trk[k].mw = (uint8_t)((ph * 2u) % 128u); }
}
static uint32_t cl_cost[NTRK + 1u], cl_cost_on;
static void cost_in(void) /* (the device's split: what each part took, as it measures it) */
{
uint32_t k;
if (!cl_cost_on) return;
for (k = 0; k < NTRK; k++) mc_tk[k] = cl_cost[k];
mc_fx_tk = cl_cost[NTRK];
}
static uint32_t scene(uint32_t mask, uint32_t nblk)
{
uint32_t b, q, f = 0, h = 2166136261u;
int dty;
blk = 0;
ui_power_on(); go_home(); frames(4);
if (mask >= 0x100u) { /* GEN songs, TRACKS 6 */
static const struct { uint8_t g; uint16_t s; uint8_t e; int8_t m; } GS[9] = {
{0, 123, 85, 0}, {0, 123, 85, 0}, {0, 7, 90, 50}, {7, 42, 85, 0}, {12, 2024, 85, 50}, {0, 999, 100, 0},
{0, 321, 80, -50}, {3, 77, 95, 60}, {19, 5, 60, -40}}; /* (V10.8.68) 106 EXPLORE CALM, 107 DANCE, 108 NEO CLASSIC CALM */
uint32_t k, gi = (mask & 0xFFu) < 9u ? mask & 0xFFu : 0u;
ui_rec_prefs |= PREF_SIX >> 8; rp_apply();
gp.genre = GS[gi].g ? GS[gi].g : GEN_EXPLORE; gp.seed = GS[gi].s; gp.energy = GS[gi].e; gp.form = 1;
grd.mood = GS[gi].m;
gp.complex = 70; gp.swing = GA_AUTO; gp.human = 30;
for (k = 0; k < GP_N; k++) gp.amt[k] = GA_AUTO;
gen_go();
if (mask == 0x101u) { song.g[G_RTYPE] = 4; song.g[G_DFDBK] = 70;
for (k = 0; k < NTRK; k++) { trk[k].p[P_DLY] = 70; trk[k].p[P_REV] = 80; trk[k].p[P_CHOR] = 70; } }
} else { /* SAVE > TOOLS 6 CPU TEST's song, TRACKS 4 (the device's measure) */
ui_rec_prefs &= (uint8_t)~(PREF_SIX >> 8); rp_apply();
cpu_test_song();
if (mask == 0x0du || mask == 0x0eu) { /* (V10.8.75) its three synths MATH, their chords and steps
* kept: 0d BYTEBEAT (B02), MATH KEYS (F28), MATH PAD (F12);
* 0e the first two MATH USER (B03 as BYTE's bytecode, 37 B;
* F20 in FIX, 63 B: FLOAT), four voices of them at most */
static const uint8_t PRE[3] = {0, 3, 5};
uint32_t k;
for (k = 0; k < 3u; k++) {
static step_t keep[NSTEP];
memcpy(keep, trk[k].step, sizeof keep);
set_engine_of(&trk[k], ENGI_LOFI);
apply_preset_to(&trk[k], PRE[k]);
memcpy(trk[k].step, keep, sizeof keep);
trk[k].p[P_VOICE] = V_POLY; trk[k].p[P_SLEN] = 32; trk[k].p[P_SGATE] = 120;
}
#ifdef MU_MAX
if (mask == 0x0eu) {
static const uint8_t B03[] = {0,244,33,69,255,31,244,73,13,197,194,69,255,1,2,73,12,244,73,8,69,234,1,244,
73,3,5,194,244,73,14,69,3,9,69,127,0};
static const uint8_t F20[] = {1,116,3,180,129,41,244,73,11,69,3,76,0,116,3,155,129,41,93,3,116,0,53,0,244,
73,11,69,3,76,1,116,5,155,129,41,93,5,116,0,53,0,244,73,11,69,3,76,2,116,7,
155,129,41,93,7,116,0,53,0,27,93,4};
if (mu_set(0, B03, sizeof B03) || mu_set(1, F20, sizeof F20)) { printf("cpu_lines: a formula refused\n"); exit(1); }
trk[0].p[P_E2] = MU_ALGO; trk[1].p[P_E2] = MU_ALGO;
}
#endif
}
}
song.master_q12 = 4096;
if (getenv("CL_OTT")) { /* (V10.8.71, as JIANT BYTE's) every track's OTT at this DEPTH (the
* worst case: 100) */
uint32_t k;
for (k = 0; k < NTRK; k++) ott_v[k][OT_DEPTH] = (int16_t)atoi(getenv("CL_OTT"));
}
if (getenv("CL_OTTB")) { /* (V10.8.73) one track's OTT alone at this DEPTH (100: the extreme
* zone): GEN's bass tracks; the CPU TEST's song its FM6 (T2: its
* chords sounding in the measured blocks) */
uint32_t k;
for (k = 0; k < NTRK; k++)
if (mask >= 0x100u ? gs.role[k] == RL_BASS : k == 1u) ott_v[k][OT_DEPTH] = (int16_t)atoi(getenv("CL_OTTB"));
}
if (live.mode) chain_play_ui(); else transport_req = 1;
ui_input();
dty = getenv("CL_DTYPE") ? atoi(getenv("CL_DTYPE")) : -1;
for (b = 0; b < 400u + nblk; b++) { /* (its first 400 blocks to warm: the song under way) */
if (b % 23u == 0u) { f++; ui_input(); song_poll(); host_ticks += 1000000u / 30u; fm1_ms += 1000u / 30u; }
if (dty >= 0) { /* (V10.8.72) every track's DIST at 100 on this TYPE (BYTE's formulas:
* 6 .. 11; a type's worst case), each block (the sections load theirs) */
uint32_t k;
for (k = 0; k < NTRK; k++) { trk[k].p[P_DIST] = 127; trk[k].p[P_DTYPE] = (int16_t)dty; }
}
move();
cost_in();
if (b >= 400u) cl_mark = 1;
mix_block(vo, CTL);
if (b >= 400u) cl_mark = 2;
for (q = 0; q < 2u * CTL; q++) h = (h ^ (uint32_t)vo[q]) * 16777619u;
}
(void)f;
return h;
}
int main(int argc, char **argv)
{
uint32_t m = argc > 1 ? strtoul(argv[1], 0, 16) : 0x101u, nblk = argc > 2 ? strtoul(argv[2], 0, 10) : 16u, h, k;
mc_host = 1; /* (two cores: core 1's job at once, between cl_c1's stores) */
if (getenv("CL_COST")) {
char *p = getenv("CL_COST");
cl_cost_on = 1;
for (k = 0; k <= NTRK && *p; k++) { cl_cost[k] = strtoul(p, &p, 10); if (*p == ',') p++; }
}
h = scene(m, nblk);
printf("cpu_lines: scene %03x hash %08x voices", m, h);
for (k = 0; k < NTRK; k++) { uint32_t i, n = 0; for (i = 0; i < NVOICE; i++) n += trk[k].v[i].active != 0; printf(" %u", n); }
printf("\n");
return 0;
}