+
+
+
+ Z–' Q–O keys
+ 1–= FX … REC
+ [ ] OCT
+ ↑ ↓ SELECT
+ ← → ALGORITHM
+ Shift-click holds a button
+
+
+
+
+
+
+
+
diff --git a/web/emu/native_check.c b/web/emu/native_check.c
new file mode 100644
index 00000000..c8a58347
--- /dev/null
+++ b/web/emu/native_check.c
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-3.0-only
+ * Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
+/* web/emu/felucca_web.c built natively (cc, as the host tests), playing emu_test.mjs's heavy song with the same
+ * gestures and the same render calls: its samples, float32 L R interleaved, to OUT. emu_test.mjs compares the
+ * WebAssembly build's against them (the browser plays what the host build plays).
+ * cc -O2 -ffp-contract=off -w -Ibuild/gen -Ifirmware/src -o build/emu/native_check web/emu/native_check.c -lm
+ * build/emu/native_check OUT.f32 */
+#include "felucca_web.c"
+
+static FILE *out;
+static int rec;
+static void render(double ms) /* as emu_test.mjs: 128-frame calls, the last one shorter */
+{
+ uint32_t n = (uint32_t)(ms * 44.1 + 0.5), k, i;
+ for (k = 0; k < n; k += 128u) {
+ uint32_t m = n - k < 128u ? n - k : 128u;
+ web_render(m);
+ for (i = 0; rec && i < m; i++) {
+ float s[2] = {out_l[i], out_r[i]};
+ fwrite(s, sizeof s, 1, out);
+ }
+ }
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 2 || !(out = fopen(argv[1], "wb")))
+ return 2;
+ web_nor_erase();
+ web_boot();
+ render(500);
+ web_test_heavy();
+ web_buttons(1u << B_PLAY);
+ render(60);
+ web_buttons(0);
+ render(60);
+ rec = 1;
+ render(2000);
+ fclose(out);
+ return 0;
+}
diff --git a/web/emu/worklet.js b/web/emu/worklet.js
new file mode 100644
index 00000000..197d74e4
--- /dev/null
+++ b/web/emu/worklet.js
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-3.0-only
+// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
+// Felucca in an AudioWorklet: felucca.wasm is the whole FM-1 firmware against a simulated FM-1
+// (web/emu/felucca_web.c). Each render quantum asks it for 128 frames, which runs the device's clock forward;
+// between quanta the page's input goes in and, about 30 times a second (4 when the page is hidden), the screen,
+// the lights and any changed flash go out. After X0X's web/emu/worklet.js (charlesvestal/fm1-x0x, GPL-3.0).
+
+// The flash the page keeps: storage.c's areas (projects, user presets and their FM6 patches, the user sample
+// slots 0x97000..0xDFFFF; the settings and the second FM6 copy 0xFC000..0xFFFFF), only the sectors that hold
+// something (an erased sector is all 0xFF). {offset: bytes}
+export const STORE_AREAS = [[0x97000, 0xE0000], [0xFC000, 0x100000]];
+export function storeSectors(nor) {
+ const out = {};
+ for (const [a, b] of STORE_AREAS)
+ for (let off = a; off < b; off += 4096) {
+ const s = nor.subarray(off, off + 4096);
+ if (s.some((v) => v !== 0xFF)) out[off] = s.slice();
+ }
+ return out;
+}
+export function restoreSectors(nor, sectors) {
+ for (const [k, v] of Object.entries(sectors || {})) {
+ const off = +k, b = v instanceof Uint8Array ? v : new Uint8Array(v);
+ if (STORE_AREAS.some(([a, e]) => off >= a && off + 4096 <= e && off % 4096 === 0) && b.length === 4096)
+ nor.set(b, off);
+ }
+}
+
+export const HOLD_MS = 24;
+
+if (typeof registerProcessor === "function") {
+ const clock = globalThis.performance ? () => globalThis.performance.now() : () => Date.now();
+ class Felucca extends AudioWorkletProcessor {
+ constructor() {
+ super();
+ this.ex = null;
+ this.sentDraws = -1;
+ this.sentWrites = -1;
+ this.sentLeds = "";
+ this.lastFrame = 0;
+ this.period = 1 / 30;
+ this.busy = 0;
+ this.frames = 0;
+ this.queue = []; // button / key changes, each held >= HOLD_MS of device time
+ this.nextOk = 0;
+ this.port.onmessage = (e) => this.onMessage(e.data);
+ }
+
+ async onMessage(m) {
+ if (m.type === "load") {
+ try {
+ const { instance } = await WebAssembly.instantiate(m.wasm, {});
+ const ex = instance.exports;
+ ex._initialize();
+ ex.web_nor_erase();
+ restoreSectors(new Uint8Array(ex.memory.buffer, ex.web_nor(), ex.web_nor_size()), m.sectors);
+ ex.web_master(m.master ?? 700);
+ ex.web_boot();
+ this.ex = ex;
+ this.sentWrites = ex.web_flash_writes_count();
+ this.port.postMessage({ type: "ready", rate: ex.web_sample_rate() });
+ } catch (err) {
+ this.port.postMessage({ type: "error", message: String(err && err.message || err) });
+ }
+ return;
+ }
+ const ex = this.ex;
+ if (!ex) return;
+ if (m.type === "buttons" || m.type === "keys") this.queue.push(m);
+ else if (m.type === "enc") ex.web_enc(m.role, m.n | 0);
+ else if (m.type === "master") ex.web_master(m.value | 0);
+ else if (m.type === "midi") ex.web_midi(m.data[0] | 0, m.data[1] | 0, m.data[2] | 0);
+ else if (m.type === "visible") this.period = m.on ? 1 / 30 : 1 / 4;
+ }
+
+ publish() {
+ const ex = this.ex, mem = ex.memory.buffer;
+ const msg = { type: "frame" };
+ const leds = [ex.web_lit_buttons(), ex.web_lit_keys(), ex.web_dim_buttons(), ex.web_dim_keys(), ex.web_dim_level()];
+ const key = leds.join(",");
+ if (key !== this.sentLeds) {
+ this.sentLeds = key;
+ msg.leds = leds;
+ }
+ if (this.frames >= 44100) { // the share of real time spent running the device
+ msg.load = this.busy / (this.frames / 44.1);
+ this.busy = this.frames = 0;
+ }
+ const transfer = [];
+ const draws = ex.web_screen_draws();
+ if (draws !== this.sentDraws) {
+ this.sentDraws = draws;
+ msg.fb = new Uint16Array(mem, ex.web_screen(), 240 * 240).slice();
+ transfer.push(msg.fb.buffer);
+ }
+ const writes = ex.web_flash_writes_count();
+ if (writes !== this.sentWrites) { // a save is done within one main-loop pass: whole here
+ this.sentWrites = writes;
+ msg.sectors = storeSectors(new Uint8Array(mem, ex.web_nor(), ex.web_nor_size()));
+ }
+ if (msg.leds || msg.fb || msg.sectors || msg.load !== undefined) this.port.postMessage(msg, transfer);
+ }
+
+ // A click's down and up can arrive within one render quantum, but the FM-1's contacts never close for less than
+ // its debounce, and the UI reads them once a frame: each change of the buttons or the keys stays HOLD_MS of the
+ // device's time before the next one goes in (a tap: down for a frame and more, then up).
+ input() {
+ const ex = this.ex, now = ex.web_now_ms();
+ while (this.queue.length && now - this.nextOk >= 0) {
+ const m = this.queue.shift();
+ if (m.type === "buttons") ex.web_buttons(m.mask >>> 0);
+ else ex.web_keys(m.mask >>> 0);
+ this.nextOk = now + HOLD_MS;
+ }
+ }
+
+ process(inputs, outputs) {
+ const ex = this.ex;
+ if (!ex) return true;
+ if (this.queue.length) this.input();
+ const out = outputs[0], n = out[0].length;
+ const t0 = clock();
+ ex.web_render(n);
+ this.busy += clock() - t0;
+ this.frames += n;
+ out[0].set(new Float32Array(ex.memory.buffer, ex.web_out_l(), n));
+ if (out[1]) out[1].set(new Float32Array(ex.memory.buffer, ex.web_out_r(), n));
+ if (currentTime - this.lastFrame >= this.period) {
+ this.lastFrame = currentTime;
+ this.publish();
+ }
+ return true;
+ }
+ }
+ registerProcessor("felucca", Felucca);
+}
diff --git a/web/test_web.mjs b/web/test_web.mjs
index 655fdd34..161a423d 100644
--- a/web/test_web.mjs
+++ b/web/test_web.mjs
@@ -38,7 +38,8 @@ const E = vm.runInNewContext(proto + `
;({ frame, unframe, parse, req, Link, parseWav, resample, normalize, takeSample, autoTrim, zoomView, rootFromName, buildSlot, makeMockDevice, CMD, SMP,
UP, bank, capturePatch, auditionPatch, startWatch, libraryFile, readLibraryFile, paramKeys, patternFromSteps, stepsFromPattern, upName,
mixer, parseNotes, parseHits, hitsText, gridFromSteps, LANE_NOTE, LANE_OF, readDevicePreferences, devicePresetRows, engineOrder, ENGINE_ORDER, aliasOf, fmtValue, FM6, enumShown, F,
- FM4, fromDigital, fromPerc, DRUM_KIT_E })`,
+ FM4, fromDigital, fromPerc, DRUM_KIT_E,
+ MENU: typeof MENU === "undefined" ? null : MENU, readDeviceMenu: typeof readDeviceMenu === "undefined" ? null : readDeviceMenu })`,
{ setTimeout, clearTimeout, setInterval, clearInterval, console });
async function editorMock() {
@@ -153,6 +154,32 @@ async function editorMock() {
}
const none = await E.readDevicePreferences(() => { throw new Error("unexpected request"); }, { uiCaps: 0 }, []);
ok(none === null, "editor: old firmware receives no unsupported preference requests");
+ if (!E.readDeviceMenu) console.log(`${"editor: MENU settings (this editor has none)".padEnd(64)} skip`);
+ else { /* the MENU settings (1.0.4): INFO 4E 01 count, MENU_DESC (72), MENU_SET (73) */
+ ok(info.menuCount === 12, "editor: INFO advertises the MENU settings (4E 01 12)");
+ const items = await E.readDeviceMenu(rq, info);
+ ok(items.length === 12 && items.map((d) => d.id).join() === "0,1,2,3,4,5,6,7,8,9,10,11" &&
+ items.map((d) => d.name).join() === "COLOR,STYLE,LARGE,ANIM,LEDS,HOLD,KNOB ACCEL,FX LATCH,BPM LOCK,SPEAKER EQ,USB LEVEL,USB SERIAL" &&
+ items.every((d) => d.kind === 0 && d.min === 0 && d.names.length === d.max - d.min + 1) &&
+ eq(items[0].names, prefs.palettes) && items[0].value === m.state.palette &&
+ items[4].names.join() === "OFF,DIM LO,DIM HI,INV" && items[4].value === 2 && items[5].value === 1,
+ "editor: MENU_DESC lists every setting, its value names and value");
+ ok(E.parse[E.CMD.MENU_DESC](await rq(E.req.menuDesc(12))).id === 127, "editor: MENU_DESC past the list answers id 127");
+ let r = E.parse[E.CMD.MENU_SET](await rq(E.req.menuSet(4, 3)));
+ ok(r.rc === 0 && r.id === 4 && r.value === 3 && E.parse[E.CMD.MENU_DESC](await rq(E.req.menuDesc(4))).value === 3,
+ "editor: MENU_SET round trip (LEDS INV)");
+ const pal = m.state.palette;
+ r = E.parse[E.CMD.MENU_SET](await rq(E.req.menuSet(0, 50)));
+ ok(r.rc === 0 && r.value === 9 && E.parse[E.CMD.UI_STATE](await rq(E.req.uiState())).palette === 9,
+ "editor: MENU_SET clamps (COLOR 50 -> 9), the same setting as UI_SET 0");
+ ok(E.parse[E.CMD.MENU_SET](await rq(E.req.menuSet(12, 1))).rc === 1, "editor: MENU_SET of an unknown id: rc 1");
+ await rq(E.req.menuSet(0, pal)); await rq(E.req.menuSet(4, 2));
+ const d = E.parse[E.CMD.MENU_DESC]([3, 20, 1, 5, 64, 0, 64, 100, 64, 88, 0, 109, 115, 0]);
+ ok(d.kind === 1 && d.value === 5 && d.max === 100 && d.name === "X" && d.unit === "ms" && d.names === null,
+ "editor: MENU_DESC kind 1 (a number): its unit");
+ ok((await E.readDeviceMenu(() => { throw new Error("unexpected request"); }, { menuCount: 0 })).length === 0,
+ "editor: firmware without MENU settings is not asked");
+ }
await rq(E.req.uiSet(3, 0));
const scale = E.parse[E.CMD.DESC](await rq(E.req.desc(0, 26)));
const scaleNames = ["CHR", "MAJ", "MIN", "DOR", "MIX", "PEN", "MPEN", "HARM", "PHRY", "LYD", "LOC", "MEL", "BLUES", "WHOLE", "DIMHW", "DIMWH"];
@@ -295,6 +322,12 @@ function mockTables() {
cmp("FM6 patches (init, factory, bank size)", T.FM6, fw.FM6);
cmp("DRUM grid: the lanes' GM notes", [...E.LANE_NOTE], fw.LANE_NOTE);
cmp("DRUM grid: the lane of GM 35..81", E.LANE_OF, fw.LANE_OF);
+ const mj = join(DESC, "../menu.json");
+ if (E.MENU && existsSync(mj)) { /* the MENU settings: tests/editor_test.c's MENU_DESC replies */
+ const fm = JSON.parse(readFileSync(mj, "utf8"));
+ cmp("MENU settings", E.MENU.map((x) => ({ id: x.id, kind: 0, min: 0, max: (x.names || m2.state.palettes).length - 1,
+ value: x.def, name: x.name, names: x.names || m2.state.palettes })), fm);
+ }
diffs.slice(0, 20).forEach((d) => console.log(" " + d));
ok(!diffs.length, `editor: mock tables == firmware (${diffs.length} differences)`);
/* #31: percent values as the firmware formats them (param_format), SWG 0..100 shows its value */