/* SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments * Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */ /* Firmware save flows against NOR flash: deferred settings, retries, and failed-save rollback. */ #define main hostsim_main #include "hostsim.c" #undef main static int32_t fm1_enc_take(uint32_t e) { (void)e; return 0; } static uint32_t irq_start_race, irq_races; static void fm1_irq_off(void) { if (irq_start_race) { /* audio serviced PLAY immediately before main masks it */ irq_start_race = 0; events_block(CTL); irq_races++; } } static void fm1_irq_on(void) {} static void lcd_sync(void) {} static void lcd_power(uint32_t s) { (void)s; } /* (MENU > SCREEN OFF: lcd.c) */ static void lcd_wake_now(void) {} static void lcd_blit(uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint16_t *p) { (void)x; (void)y; (void)w; (void)h; (void)p; } #define FELUCCA_FLASH 1 #include "../firmware/src/gfx.c" #include "../firmware/src/panel.c" #include "../firmware/src/ui.c" #include "../firmware/src/menu_items.c" static void panel_setup(void) {} static uint8_t nor[0x100000], flash_ok = 1; static int erase_error, fail_after = -1; static uint32_t erases, as_erases; /* (as_erases: the autosave's two sectors, 0xE5000 / 0xE6000) */ static int st_read(uint32_t off, void *dst, uint32_t n) { memcpy(dst, nor + off, n); return 0; } static int st_erase(uint32_t off) { erases++; as_erases += off >= 0xE5000u && off < 0xE7000u; if (erase_error) return -8; memset(nor + off, 0xFF, 4096); return 0; } static int st_prog(uint32_t off, const void *src, uint32_t n) { const uint8_t *s = src; uint32_t i; if (fail_after == 0) return -9; if (fail_after > 0) fail_after--; for (i = 0; i < n; i++) nor[off + i] &= s[i]; return 0; } static uint32_t irq_save(void) { return 0; } static void irq_restore(uint32_t f) { (void)f; } static uint32_t fl_jedec_ram(void) { return 0; } static void fl_plain_window_init(void) {} #define FL_FAR(fn) (fn) #include "../firmware/src/storage.c" #include "../firmware/src/upreset.c" #include "../firmware/src/project.c" #include "legacy_proj.h" /* (V10.6s) FUN5, retired: refused */ enum { ED_UI_STATE = 34, ED_UI_SET, ED_UI_PALETTES, ED_FAV_GET, ED_FAV_SET }; static uint8_t ed_rep[64]; /* the reply's bytes (MENU_SET) */ static uint32_t ed_repn; static void ed_b(uint32_t n) { if (ed_repn < sizeof ed_rep) ed_rep[ed_repn++] = (uint8_t)(n & 127u); } static void ed_v(int32_t n) { ed_b((uint32_t)(n + 8192)); ed_b((uint32_t)(n + 8192) >> 7); } static int32_t ed_rv(const uint8_t *a) { return (int32_t)(a[0] | (uint32_t)a[1] << 7) - 8192; } static void ed_str(const char *s, uint32_t n) { (void)s; (void)n; } #include "../firmware/src/editor_preferences.c" #include "../firmware/src/editor_menu.c" static int check(const char *what, int ok) { printf("persist: %-66s %s\n", what, ok ? "ok" : "FAIL"); return ok ? 0 : 1; } static void reset(void) { song_draft = song_save_loop = 0; memset(nor, 0xFF, sizeof nor); memset(&song, 0, sizeof song); memset(trk, 0, sizeof trk); memset(&chain, 0, sizeof chain); chain_defaults(&chain_config); memset(proj_slot, 0, sizeof proj_slot); memset(up_bank, 0, sizeof up_bank); memset(&persist_saved, 0, sizeof persist_saved); memset(&settings, 0, sizeof settings); memset(&ui, 0, sizeof ui); memset(&favorites, 0, sizeof favorites); panel = PANEL_DEFAULT; host_tracks_init(); fm1_ms = 0; transport_req = 0; persist_pending = 0; persist_retry_ms = 0; flash_ok = 1; erase_error = 0; fail_after = -1; up_gen = erases = 0; irq_start_race = irq_races = 0; } /* ---- 1.0.3 retired the FM6 bank (OBJ_FM6BANK) and moved its patches into the user presets once; (V10.6s) that move * is retired too (up_fm6.c): a 1.0.2 flash boots with the bank left alone and nothing written, the presets on a B slot * the init voice (SLOT OWN), the F ones their factory patch; the patches object works as ever ---- */ static uint8_t mig_pre[0x100000]; static struct { uint32_t magic; uint16_t ver, nslot; uint32_t used, rsv; uint8_t v[27][FM6_PACKED]; } mig_bank; /* (1.0's) */ _Static_assert(sizeof mig_bank == sizeof(fm6_bank_t), "the retired bank's size"); static void mig_rec(up_bank_t *b, uint32_t i, uint32_t engine, int16_t slot, char tag) { up_rec_t *r = &b->r[i]; uint32_t k; memset(r, 0, sizeof *r); r->used = UP_USED; r->ver = UP_VER; r->engine = (uint8_t)engine; r->np = P_COUNT; r->name[0] = tag; for (k = 0; k < P_COUNT; k++) up_set_value(r, k, param_desc_of(engine, k)->def); up_set_value(r, P_E7, slot); up_set_value(r, P_E0, (int16_t)(tag & 7)); /* (each record another sound) */ } /* a 1.0.2 flash: user presets on B3 (slot 1), F2 (2), B5 empty (3), DRUM (4), B1 (18); the bank written twice */ static void mig_setup(void) { static up_bank_t b[2]; memset(nor, 0xFF, sizeof nor); memset(b, 0, sizeof b); for (uint32_t i = 0; i < 2u; i++) { b[i].magic = UP_BANK_MAGIC; b[i].rsize = sizeof(up_rec_t); b[i].nslot = UP_PER_BANK; } mig_rec(&b[0], 0, ENGI_FM6, FM6_NFACTORY + 2, 'A'); mig_rec(&b[0], 1, ENGI_FM6, 1, 'B'); mig_rec(&b[0], 2, ENGI_FM6, FM6_NFACTORY + 4, 'C'); mig_rec(&b[0], 3, ENGI_DRUM, FM6_NFACTORY + 2, 'D'); mig_rec(&b[1], 1, ENGI_FM6, FM6_NFACTORY + 0, 'E'); st_save(OBJ_UPRESET0, &b[0], sizeof b[0]); st_save(OBJ_UPRESET0 + 1, &b[1], sizeof b[1]); memset(&mig_bank, 0, sizeof mig_bank); mig_bank.magic = 0x42364D46u; mig_bank.ver = 1; mig_bank.nslot = 27; mig_bank.used = 1u | 1u << 2; memcpy(mig_bank.v[2], FM6_FACTORY[4], FM6_PACKED); memcpy(mig_bank.v[2] + 118, "OLD BANK 1", 10); st_save(OBJ_FM6BANK, &mig_bank, sizeof mig_bank); /* A: an older bank */ memcpy(mig_bank.v[0], FM6_FACTORY[6], FM6_PACKED); memcpy(mig_bank.v[0] + 118, "NEW B1 ", 10); memcpy(mig_bank.v[2] + 118, "NEW B3 ", 10); st_save(OBJ_FM6BANK, &mig_bank, sizeof mig_bank); /* B: the newest */ } static void mig_boot(void) /* a power-on: RAM lost, the stores from flash */ { memset(up_bank, 0, sizeof up_bank); memset(&upf, 0, sizeof upf); up_boot(); } /* no preset has a patch of its own (nothing moved) */ static int mig_none(void) { uint8_t pk[FM6_PACKED]; return upf_valid(&upf) && upf_get(0, pk) && upf_get(17, pk) && upf_get(1, pk) && upf_get(2, pk) && upf_get(3, pk); } static int mig_test(void) { int bad = 0; uint32_t before; uint8_t pk[FM6_PACKED]; reset(); mig_setup(); memcpy(mig_pre, nor, sizeof nor); before = erases; mig_boot(); bad += check("1.0.2 flash with an FM6 bank (V10.6s: its move retired): nothing moved, nothing written", mig_none() && erases == before && !memcmp(mig_pre, nor, sizeof nor)); up_load(0); bad += check(" .. UP_LOAD 1 (stored SLOT B3) plays the init voice, SLOT OWN", !memcmp(fm6_patch[song.sel] + FP_NAME, "INIT VOICE", 10) && TSEL->p[P_E7] == FM6_OWN); up_load(1); bad += check(" .. UP_LOAD 2 (F2) plays F2, SLOT F2", !memcmp(fm6_patch[song.sel] + FP_NAME, FM6_FACTORY[1] + 118, 10) && TSEL->p[P_E7] == 1); trk[song.sel].eng_req = ENGI_FM6; fm6_load_slot(song.sel, 3); fail_after = (int)((sizeof(up_bank_t) + 255u) / 256u + 1u + 3u); /* the preset bank's write passes, the patches' fails */ up_store(20, "CUT"); fail_after = -1; mig_boot(); bad += check("a cut while a preset's patch is written: that preset without one", upf_get(20, pk)); up_store(20, "SAVED"); mig_boot(); bad += check("saved again: its patch over a boot, the others none", !upf_get(20, pk) && !memcmp(pk + 118, FM6_FACTORY[3] + 118, 10) && mig_none()); memset(nor, 0xFF, sizeof nor); /* a device that never had a bank: nothing written */ before = erases; mig_boot(); bad += check("no bank: nothing written", erases == before && upf_valid(&upf)); return bad; } /* ---- 1.2, Discussion #130: the autosave (project.c): written only when the music changed, stopped and idle 10 s, at * most one a minute; restored at power-on (RESTORE LAST ON, a clean boot); a damaged one ignored ---- */ static void as_run(uint32_t ms) /* the main loop for ms (autosave_poll checks every 250 ms) */ { uint32_t end = fm1_ms + ms; while ((int32_t)(end - fm1_ms) > 0) { fm1_ms += 50u; autosave_poll(); } } static void as_power_on(void) /* RAM lost: the defaults, then main.c's restore */ { uint32_t k; memset(trk, 0, sizeof trk); memset(&motion, 0, sizeof motion); memset(motion_active, 0, sizeof motion_active); memset(&as, 0, sizeof as); memset(&ui, 0, sizeof ui); proj_name[0] = 0; chain_defaults(&chain_config); host_tracks_init(); for (k = 0; k < NTRK; k++) trk[k].eng_req = trk[k].engine = (uint8_t)TRK_OLD[k][0]; /* (0.9.8's parts: the autosave tests' music) */ } static void as_edit(uint32_t i) /* a change a project holds: step i's note (always another) */ { static uint32_t n; track_t *t = &trk[i % NTRK]; step_t *s = &t->step[i % 16u]; s->time = ST_NOTE; s->n = 1; s->note[0] = (uint8_t)(20u + (s->note[0] + 1u + n++ % 50u) % 100u); s->vel = 100; } static int autosave_test(void) { int bad = 0, ok; uint32_t w, k, b0, minute; step_t st[NSTEP]; project_store_t raw; reset(); ui_prefs |= PREF_RESTORE_ON; /* (JIANT 0.6.40: RESTORE LAST OFF by default) */ as_power_on(); as_erases = 0; ok = !autosave_boot(1) && !as.writes; as_run(120000u); bad += check("autosave: power-on with none: nothing restored; 2 min unchanged: no write", ok && !as.writes && !as_erases); as_edit(0); as_run(9000u); ok = !as.writes; as_run(1500u); bad += check("autosave: a change: no write within 10 s, one once idle 10 s (one erase)", ok && as.writes == 1u && as_erases == 1u && st_load(OBJ_AUTOSAVE, &raw, sizeof raw) == (int)sizeof raw); as_run(600000u); bad += check("autosave: 10 min unchanged after it: no more writes", as.writes == 1u && as_erases == 1u); song.g[G_SLOT] = 3; /* (the PROJECT page's pick, the selected track: not the music) */ song.sel = 2; as_run(120000u); bad += check("autosave: the selected track / PROJECT's slot pick alone: no write", as.writes == 1u); song.g[G_BPM] = 133; song.playing = 1; as_run(300000u); ok = as.writes == 1u; song.playing = 0; as_run(9000u); ok &= as.writes == 1u; as_run(1500u); bad += check("autosave: changed while playing: no write until stopped and idle 10 s, then one", ok && as.writes == 2u); as_edit(1); fm1_in.notes = 1u << 7; /* a key held (playing the sound, stopped) */ as_run(60000u); ok = as.writes == 2u; fm1_in.notes = 0; trk[1].v[0].active = 1; /* .. its release still sounding */ as_run(20000u); ok &= as.writes == 2u; trk[1].v[0].active = 0; as_run(9000u); ok &= as.writes == 2u; as_run(1500u); bad += check("autosave: a key held, a voice sounding: no write (an erase stops the audio); after, idle 10 s: one", ok && as.writes == 3u); as_run(60000u); as_edit(2); /* written 10 s on; then edits 15 s apart: a minute's gap */ as_run(11000u); w = as.writes; as_edit(3); as_run(15000u); ok = as.writes == w; as_edit(4); as_run(15000u); ok &= as.writes == w; as_run(40000u); bad += check("autosave: two writes at least 60 s apart", ok && as.writes == w + 1u); as_run(60000u); w = as.writes; as_edit(5); for (k = 0; k < 120u; k++) { /* an editor backup going on: holds it */ as_run(500u); autosave_hold(); } ok = as.writes == w; as_run(10500u); bad += check("autosave: none during an editor transfer (autosave_hold), one after", ok && as.writes == w + 1u); /* power-on: restored */ as_edit(6); song.g[G_BPM] = 97; str_cpy(proj_name, "MY TUNE", sizeof proj_name); as_run(70000u); memcpy(st, trk[0].step, sizeof st); b0 = as_erases; as_power_on(); ok = memcmp(st, trk[0].step, sizeof st) != 0 && song.g[G_BPM] != 97; ok &= autosave_boot(1) == 1 && !memcmp(st, trk[0].step, sizeof st) && song.g[G_BPM] == 97 && str_eq(proj_name, "MY TUNE") && ui.msg_t && str_eq(ui.msg, "RESTORED"); as_run(300000u); bad += check("autosave: power-on restores it (steps, tempo, name; RESTORED); no write after (unchanged)", ok && !as.writes && as_erases == b0); as_power_on(); ok = autosave_boot(0) == 0 && song.g[G_BPM] != 97; as_run(60000u); bad += check("autosave: after a crash / hang / failed boot: not restored, nothing written", ok && as_erases == b0); as_power_on(); ui_prefs &= (uint8_t)~PREF_RESTORE_ON; ok = autosave_boot(1) == 0 && song.g[G_BPM] != 97; as_edit(7); as_run(300000u); bad += check("autosave: RESTORE LAST OFF: none restored, none written", ok && !as.writes && as_erases == b0); ui_prefs |= PREF_RESTORE_ON; { /* a damaged copy: the other one (A/B); both damaged: none */ st_hdr_t h; int cur, other; as_power_on(); autosave_boot(1); song.g[G_BPM] = 111; /* a newer copy into the other sector */ as_run(11000u); cur = st_current(OBJ_AUTOSAVE, &h); nor[st_sector(OBJ_AUTOSAVE, (uint32_t)cur) + ST_PAYLOAD_OFF + 100u] ^= 0x10u; /* its payload: the CRC fails */ other = st_current(OBJ_AUTOSAVE, &h); as_power_on(); ok = cur >= 0 && other == !cur && autosave_boot(1) == 1 && song.g[G_BPM] == 97; bad += check("autosave: the newest copy damaged: the copy before it is restored", ok); nor[st_sector(OBJ_AUTOSAVE, (uint32_t)other) + 12u] ^= 0x01u; /* the other one's header too */ as_power_on(); ok = autosave_boot(1) == 0 && song.g[G_BPM] != 97 && song.g[G_BPM] != 111; bad += check("autosave: both copies damaged: ignored, power-on as new", ok); memset(nor + 0xE5000u, 0xFF, 0x2000u); st_save(OBJ_AUTOSAVE, "NOT A PROJECT", 13u); /* a valid record that is no project */ as_power_on(); bad += check("autosave: a record that is no project: ignored", autosave_boot(1) == 0 && song.g[G_BPM] != 97); } /* a power cut in the middle of a write: the copy before stays in charge */ memset(nor + 0xE5000u, 0xFF, 0x2000u); as_power_on(); autosave_boot(1); as_edit(8); as_run(11000u); /* (written: copy A) */ song.g[G_BPM] = 150; fail_after = 3; /* the next write stops after 3 programs */ as_run(70000u); fail_after = -1; ok = as.err; as_power_on(); ok &= autosave_boot(1) == 1 && song.g[G_BPM] != 150 && trk[8u % NTRK].step[8].n == 1u; bad += check("autosave: a write cut short (power off): the copy before is restored", ok); /* wear: a 4-hour session: 3 h of editing (a change every 3 s), pauses and playing it back, then an hour away */ as_power_on(); autosave_boot(1); as_erases = 0; for (minute = 0; minute < 240u; minute++) { uint32_t s; if (minute >= 180u) { as_run(60000u); continue; } song.playing = minute % 10u >= 7u; /* 3 minutes in 10 playing */ for (s = 0; s < 60u; s += 3u) { if (minute % 5u != 4u) /* (every fifth minute a pause, no edits) */ as_edit(minute * 20u + s); as_run(3000u); } } song.playing = 0; as_run(20000u); printf("persist: a 4-hour session (3 h editing and playing, 1 h idle): %u autosave writes\n", (unsigned)as_erases); bad += check("autosave wear: a session's writes stay far below one a minute", as_erases > 0u && as_erases <= 60u); { /* worst case: a change, then just enough idle, over and over, for 8 hours */ uint32_t i; as_erases = 0; for (i = 0; i < 8u * 3600u / 11u; i++) { as_edit(i); as_run(11000u); } printf("persist: worst case, 8 h of a change every 11 s: %u writes (%u per sector)\n", (unsigned)as_erases, (unsigned)(as_erases / 2u)); bad += check("autosave wear, worst case: at most one write a minute (480 in 8 h)", as_erases <= 8u * 60u + 1u); } return bad; } /* JIANT's songs (song_main.c): song 1 is the four project slots, songs 2..8 their own objects; switching keeps the * song left (its stored sections and rows) and brings in the other's sections, rows and section A; the index keeps * the current song over a power-off; a first boot takes song 1's rows from its section A (Felucca's song) */ static int songs_test(void) { int bad = 0, ok; reset(); song_cur = 0; song_index_load(); /* (no index in flash) */ song_index_boot(); trk[0].p[P_SLEN] = 5; chain_config.count = 1; chain_config.row[0] = (chain_row_t){0, 3}; ok = project_save(0) == 0 && song_cur == 0 && st_load(OBJ_PROJECT0, &proj_wire, sizeof proj_wire) > 0; ok &= song_select(1) == 0 && song_cur == 1 && !project_used(0) && !chain_config.count && trk[0].p[P_SLEN] == 5; /* (an empty song: the music stays) */ ok &= st_load(OBJ_SONGIDX, &song_idx, sizeof song_idx) == (int)sizeof song_idx && song_idx.cur == 1u && song_idx.rows[0].count == 1u && song_idx.rows[0].row[0].bars == 3u; bad += check("songs: song 2 in, song 1's rows and the current song in the index", ok); trk[0].p[P_SLEN] = 7; section_store(1); /* song 2, section B: RAM, then flash once stopped */ chain_config.count = 1; chain_config.row[0] = (chain_row_t){1, 2}; song_poll(); ok = !live.dirty && st_load(OBJ_SONG1 + 1, &proj_wire, sizeof proj_wire) > 0 && st_load(OBJ_PROJECT0 + 1, &proj_wire, sizeof proj_wire) < 0; fm1_ms += 3000u; song_poll(); /* (the rows rested: the index written) */ ok &= st_load(OBJ_SONGIDX, &song_idx, sizeof song_idx) == (int)sizeof song_idx && song_idx.rows[1].row[0].slot == 1u; bad += check("songs: a section stored in song 2 goes to song 2's object, its rows to the index", ok); ok = song_select(0) == 0 && song_cur == 0 && project_used(0) && !project_used(1) && trk[0].p[P_SLEN] == 5 && chain_config.count == 1u && chain_config.row[0].bars == 3u && live.cur == 0; bad += check("songs: back to song 1: its sections, its rows, its section A as the music", ok); song.playing = 1; ok = song_select(1) != 0 && song_cur == 0 && str_eq(ui.msg, "STOP TO CHANGE SONG"); song.playing = 0; bad += check("songs: no change while playing", ok); ok = song_select(1) == 0; memset(proj_slot, 0, sizeof proj_slot); /* a power-off */ song_cur = 0; chain_defaults(&chain_config); song_index_load(); for (uint32_t i = 0; i < 4u; i++) proj_fetch(i); song_index_boot(); ok &= song_cur == 1 && project_used(1) && !project_used(0) && chain_config.row[0].slot == 1u; bad += check("songs: the current song, its sections and rows back after a power-off", ok); reset(); /* Felucca's flash: a project with its song, no index */ song_cur = 0; chain_config.count = 2; chain_config.row[0] = (chain_row_t){0, 4}; chain_config.row[1] = (chain_row_t){0, 8}; ok = project_save(0) == 0; memset(proj_slot, 0, sizeof proj_slot); chain_defaults(&chain_config); song_index_load(); for (uint32_t i = 0; i < 4u; i++) proj_fetch(i); song_index_boot(); ok &= song_cur == 0 && chain_config.count == 2u && chain_config.row[1].bars == 8u; bad += check("songs: a first boot takes song 1's rows from its section A", ok); { /* (0.8.5) SAVE > LIBRARY's picture (project.c lib_scan): another project's sections from their headers, its * section A's name from its bytes; the open one from RAM; an empty one nothing */ char b[16]; uint32_t m1, m2, m0; ok = song_select(1) == 0 && project_save_as(0, "ACID 03") == 0 && project_save_as(2, "") == 0 && song_select(0) == 0; m1 = lib_scan(1, b); ok &= m1 == 5u && str_eq(b, "ACID 03"); m2 = lib_scan(2, b); ok &= m2 == 0u && !b[0]; m0 = lib_scan(0, b); ok &= (m0 & 1u) != 0u; bad += check("songs: the library's scan: project 2's sections A and C and A's name, project 3 empty", ok); } return bad; } #include "song_save_cases.h" #include "fit_storage_cases.h" #include "voice_user_cases.h" #include "split_storage_cases.h" #include "eq_save_cases.h" #ifndef PERSISTENCE_TEST_NO_MAIN /* (V10.8.51) a project of before V10.7 in flash (the old GM drum part, before 1.0): LOAD shows OLD PROJECT: OPEN IN * STUDIO (not EMPTY SLOT), nothing erased or written, the flash object byte for byte as it was, the music as it was */ static int old_project_test(void) { static project_store_t st, back; int ok; uint32_t e0, len0; reset(); song_cur = 0; trk[0].p[P_SLEN] = 6; project_capture(&proj_scratch); proj_scratch.t[3].preset = PROJ_DEF_KEEP; proj_scratch.sum = proj_sum(&proj_scratch); ok = proj_pack(&st, &proj_scratch) && !st_save(proj_obj(2), &st, sizeof st); memset(&proj_slot[2], 0, sizeof proj_slot[2]); e0 = erases; len0 = trk[0].p[P_SLEN]; project_load(2); ok &= str_eq(ui.msg, "OLD PROJECT: OPEN IN STUDIO") && erases == e0 && trk[0].p[P_SLEN] == (int)len0 && st_load(proj_obj(2), &back, sizeof back) == (int)sizeof back && !memcmp(&st, &back, sizeof st); project_load(2); /* (again: the same, still untouched) */ ok &= str_eq(ui.msg, "OLD PROJECT: OPEN IN STUDIO") && erases == e0 && st_load(proj_obj(2), &back, sizeof back) == (int)sizeof back && !memcmp(&st, &back, sizeof st); memset(&proj_slot[3], 0, sizeof proj_slot[3]); project_load(3); ok &= str_eq(ui.msg, "EMPTY SLOT"); return check("V10.8.51: an old project in flash: LOAD says OLD PROJECT: OPEN IN STUDIO, the slot never erased or " "written, the music kept; an empty slot still EMPTY SLOT", ok); } int main(void) { int bad = 0, ok; uint32_t before; persist_t p; project_store_t old, got; project_v5_t v5; up_bank_t bank; up_rec_t r; bad += old_project_test(); reset(); settings_init(); { const uint8_t amber[] = {0, 2}; song.playing = 1; bad += check("editor display change while playing is applied and reports deferred saving", ed_ui_set(amber, sizeof amber) == 4 && settings.palette == 2u && persist_pending == 1 && !erases); song.playing = 0; settings_poll(); bad += check("STOP commits the deferred palette setting", !persist_pending && erases == 1 && persist_saved.palette == palette_to_stored(2)); before = erases; bad += check("repeating an unchanged editor preference succeeds without erasing", ed_ui_set(amber, sizeof amber) == 0 && erases == before); erase_error = 1; const uint8_t mono[] = {0, 0}; bad += check("editor reports failed saving while preserving the previous flash setting", ed_ui_set(mono, sizeof mono) == 3 && settings.palette == 0u && persist_pending == 2 && persist_saved.palette == palette_to_stored(2)); erase_error = 0; fm1_ms += 1000; settings_poll(); bad += check("failed editor preference retries through the existing persistence path", !persist_pending && persist_saved.palette == palette_to_stored(0)); const uint8_t bold[] = {1, 1}; bad += check("the retired font weight is refused without a write", ed_ui_set(bold, sizeof bold) == 2); } { /* MENU_SET (editor_menu.c): the same settings record, the same deferral, retry and idempotence */ const uint8_t leds_off[] = {4, 0, 64}, large_on[] = {2, 1, 64}, hold_06[] = {5, 3, 64}, eq_bass[] = {9, 2, 64}; persist_t q; reset(); settings_init(); song.playing = 1; ed_repn = 0; ed_menu_handle(ED_MENU_SET, leds_off, 3); bad += check("MENU_SET while playing: applied, rc 4 (queued until STOP), no erase", ed_repn == 4 && ed_rep[0] == 4 && ed_rep[1] == 4 && settings_leds == LEDS_OFF && persist_pending == 1 && !erases); song.playing = 0; settings_poll(); bad += check("STOP commits the MENU setting (LEDS OFF in the settings record)", !persist_pending && erases == 1 && st_load(OBJ_SETTINGS, &q, sizeof q) == (int)sizeof q && leds_from_stored(q.zoom) == LEDS_OFF); ed_repn = 0; ed_menu_handle(ED_MENU_SET, large_on, 3); ok = ed_rep[0] == 0 && (ui_prefs & PREF_LARGE); ed_repn = 0; ed_menu_handle(ED_MENU_SET, hold_06, 3); ok &= ed_rep[0] == 0 && settings_hold == 3u; ed_repn = 0; ed_menu_handle(ED_MENU_SET, eq_bass, 3); ok &= ed_rep[0] == 0 && st_load(OBJ_SETTINGS, &q, sizeof q) == (int)sizeof q && q.lowcut == 2u && hold_from_stored(q.bold) == 3u && (q.favorites.factory[15][30] & PREF_LARGE) && leds_from_stored(q.zoom) == LEDS_OFF; bad += check("MENU_SET stopped: rc 0, saved at once (LARGE, HOLD, SPEAKER EQ), the others kept", ok); before = erases; ed_repn = 0; ed_menu_handle(ED_MENU_SET, hold_06, 3); bad += check("repeating an unchanged MENU_SET: rc 0, no erase", ed_rep[0] == 0 && erases == before); erase_error = 1; ed_repn = 0; ed_menu_handle(ED_MENU_SET, leds_off, 3); /* unchanged: still rc 0 (no write) */ ok = ed_rep[0] == 0; { const uint8_t leds_inv[] = {4, 3, 64}; ed_repn = 0; ed_menu_handle(ED_MENU_SET, leds_inv, 3); } ok &= ed_rep[0] == 3 && settings_leds == LEDS_INV && persist_pending == 2 && leds_from_stored(persist_saved.zoom) == LEDS_OFF; bad += check("MENU_SET with a failing flash: applied, rc 3, the saved setting kept", ok); erase_error = 0; fm1_ms += 1000; settings_poll(); bad += check("... and retried by the settings path", !persist_pending && leds_from_stored(persist_saved.zoom) == LEDS_INV); settings_hold = HOLD_DEF; settings_leds = LEDS_DIM; } reset(); settings.palette = 3; settings.lowcut = 2; settings.zoom = 1; persist_saved.bold = 1; /* (an earlier firmware's font weight: kept as saved) */ favorites.factory[8][0] = 1; favorites.user = 1u << 31; song.playing = 1; settings_save(); bad += check("playing: settings queued, no erase, runtime kept", persist_pending && !erases && settings.palette == 3u && settings.lowcut == 2u && settings.zoom == 1u); song.playing = 0; transport_req = 1; settings_poll(); bad += check("pending PLAY: settings still queued, no erase", persist_pending && !erases); transport_req = 0; chain.armed = 1; settings_poll(); bad += check("pending SONG: settings still queued, no erase", persist_pending && !erases); chain.armed = 0; settings.palette = 2; settings_poll(); bad += check("STOP: latest settings saved and request cleared", !persist_pending && erases == 1u && st_load(OBJ_SETTINGS, &p, sizeof p) == (int)sizeof p && p.palette == palette_to_stored(2) && p.lowcut == 2u && p.zoom == 1u && p.bold == 1u && p.favorites.factory[8][0] == 1 && p.favorites.user == (1u << 31) && !memcmp(&p.panel, &panel, sizeof panel)); settings_save(); bad += check("unchanged settings do not erase", !persist_pending && erases == 1u); settings.palette = 1; erase_error = 1; fm1_ms = 0xFFFFFF00u; settings_save(); before = erases; settings_poll(); fm1_ms += 999u; settings_poll(); bad += check("failed settings retry waits through clock wrap", persist_pending && erases == before && persist_saved.palette == palette_to_stored(2) && settings.palette == 1u); erase_error = 0; fm1_ms++; settings_poll(); bad += check("settings retry succeeds after 1000 ms", !persist_pending && erases == before + 1u && st_load(OBJ_SETTINGS, &p, sizeof p) == (int)sizeof p && p.palette == palette_to_stored(1)); reset(); trk[0].step[0] = (step_t){{60}, 1, ST_NOTE, 0, 96, 0, 0}; project_save(0); old = proj_slot[0]; trk[0].step[0].note[0] = 72; fail_after = 1; project_save(0); fail_after = -1; bad += check("failed project save keeps RAM and old flash", !strcmp(ui.msg, "SAVE ERROR") && !memcmp(&proj_slot[0], &old, sizeof old) && st_load(OBJ_PROJECT0, &got, sizeof got) == (int)sizeof got && !memcmp(&got, &old, sizeof got)); project_save(0); bad += check("successful project save publishes new RAM", !strcmp(ui.msg, "SAVED") && proj_import(&proj_scratch, &proj_slot[0], sizeof proj_slot[0]) && proj_scratch.t[0].step[0].note[0] == 72u && proj_ok(&proj_scratch)); before = erases; transport_req = 1; project_save(1); bad += check("pending PLAY: project save leaves slot and flash", !project_used(1) && erases == before && !strcmp(ui.msg, "STOP TO SAVE")); transport_req = 0; erase_error = 1; project_save(2); bad += check("failed first project save leaves slot empty", !project_used(2)); erase_error = 0; { /* names: a project's through flash; a failed rename keeps the old one in RAM and flash */ char n[16]; project_store_t keep; step_t st0 = trk[0].step[0]; project_save_as(0, "LOFI JAM"); memset(proj_slot[0].raw, 0, 8); /* (the RAM copy lost: read it back from flash) */ proj_fetch(0); bad += check("a project's name goes to flash and comes back", project_name(0, n) && !strcmp(n, "LOFI JAM") && !strcmp(proj_name, "LOFI JAM")); keep = proj_slot[0]; fail_after = 1; bad += check("failed project rename: SAVE ERROR, the slot and flash as they were", project_rename(0, "DUB") == 2 && !strcmp(ui.msg, "SAVE ERROR") && !memcmp(&proj_slot[0], &keep, sizeof keep) && st_load(OBJ_PROJECT0, &got, sizeof got) == (int)sizeof got && !memcmp(&got, &keep, sizeof got)); fail_after = -1; before = erases; transport_req = 1; bad += check("pending PLAY: no rename, no erase", project_rename(0, "DUB") == 1 && erases == before && !strcmp(ui.msg, "STOP TO SAVE")); transport_req = 0; bad += check("project rename: flash has the new name, the music as it was", project_rename(0, "DUB") == 0 && !strcmp(ui.msg, "RENAMED") && (memset(proj_slot[0].raw, 0, 8), proj_fetch(0), project_name(0, n)) && !strcmp(n, "DUB") && proj_scratch.t[0].step[0].note[0] == st0.note[0]); } { /* FUN7 itself refuses a step outside its fields: a damaged slot is empty, the tracks stay */ step_t keep = trk[0].step[0]; old = proj_slot[0]; proj_slot[0].raw[67] = 9; /* (FUNC) 9 tracks: more than it can hold */ bad += check("damaged FUN7 slot (hash ok, 9 tracks) is refused", (memcpy(proj_slot[0].raw + PROJ_STORE_SIZE - 4, &(uint32_t){proj_hash(proj_slot[0].raw, PROJ_STORE_SIZE - 4)}, 4), !project_used(0))); st_save(OBJ_PROJECT0, &proj_slot[0], sizeof proj_slot[0]); ui.msg[0] = 0; project_load(0); bad += check("loading it says EMPTY SLOT and keeps the steps", !strcmp(ui.msg, "EMPTY SLOT") && !memcmp(&trk[0].step[0], &keep, sizeof keep)); proj_slot[0] = old; st_save(OBJ_PROJECT0, &proj_slot[0], sizeof proj_slot[0]); } { /* (V10.6s) an older format (FUN5, retired) with values FUN7 cannot pack: refused, the slot empty, the tracks * as they were; the parameter ranges all fit FUN7's byte */ step_t keep; uint32_t i; memset(&v5, 0, sizeof v5); v5.magic = PROJ_MAGIC_V5; v5.size = sizeof v5; for (i = 0; i < G_COUNT; i++) v5.g[i] = GP[i].def; for (i = 0; i < PROJ_LT; i++) { v5.t[i].engine = trk[i].engine; memcpy(v5.t[i].p, trk[i].p, sizeof v5.t[i].p); } v5.t[0].p[P_LEVEL] = 900; /* out of range */ v5.t[0].step[0] = (step10_t){{255, 128, 160, 200}, 255, 255, 0, 96, 255, 5}; v5.sum = proj_hash(&v5, sizeof v5 - 4u); st_save(OBJ_PROJECT0 + 3u, &v5, sizeof v5); memset(proj_slot[3].raw, 0, 4); keep = trk[0].step[0]; ui.msg[0] = 0; project_load(3); bad += check("FUN5 (retired) in flash: refused, EMPTY SLOT, the steps kept", !project_used(3) && !strcmp(ui.msg, "EMPTY SLOT") && !memcmp(&trk[0].step[0], &keep, sizeof keep)); trk[0].step[0] = (step_t){{60, 64, 0, 0}, 2, ST_NOTE, 0, 96, 0, 0}; bad += check(" .. a save there stores today's format", project_save(3) == 0 && project_used(3)); for (i = 0; i < P_COUNT; i++) { uint32_t e; for (e = 0; e < NENGINES; e++) if (param_desc_of(e, i)->min < (i == P_M1AMT || i == P_M2AMT || i == P_M3AMT || i == P_M4AMT ? -128 : -64) || param_desc_of(e, i)->max > 127) { /* (V10.8.51: a matrix AMT -128..127, its byte 192..255 the negatives) */ printf("param %u engine %u: range %d..%d does not fit FUN7's byte\n", i, e, param_desc_of(e, i)->min, param_desc_of(e, i)->max); bad++; } } } chain_config.count = 1; chain_config.row[0] = (chain_row_t){3, 1}; bad += check("SONG sources use the same step bounds", chain_prepare() == 0 && !memcmp(&sec_stage.t[0].step[0], &trk[0].step[0], sizeof(step_t))); seq_stop(); transport_req = 0; { /* (add15) ARRANGE 2: a row's tracks from other sections */ uint8_t na, nb; trk[0].step[0].time = trk[2].step[0].time = ST_NOTE; /* (notes that sound: no FUN5 load before this now) */ trk[0].step[0].note[0] = 40; trk[2].step[0].note[0] = 41; project_save(0); trk[0].step[0].note[0] = 50; trk[2].step[0].note[0] = 51; project_save(1); na = trk[0].step[0].note[0]; chain_config.count = 1; chain_config.row[0] = (chain_row_t){0, 1}; song_idx.tsrc[song_cur][0] = 2u << 6; /* T3 from B, the rest the row's A */ ok = chain_prepare() == 0 && sec_stage.t[0].step[0].note[0] == 40 && sec_stage.t[2].step[0].note[0] == 51 && sec_stage.section == 0u; seq_stop(); transport_req = 0; song_idx.tsrc[song_cur][0] = 0; nb = (uint8_t)(chain_prepare() == 0 && sec_stage.t[2].step[0].note[0] == 41); seq_stop(); transport_req = 0; bad += check("ARRANGE 2: a track plays another section's, the rest the row's", ok && nb && na == 50); { /* (beta4) VAR: THIN, MUTE, REV, a seeded VAR the same twice */ uint32_t i, thin = 1, rev, same; static step_t a1[NSTEP]; for (i = 0; i < 16u; i++) { trk[1].step[i].time = ST_NOTE; trk[1].step[i].n = 1; trk[1].step[i].note[0] = (uint8_t)(48 + i); } trk[1].p[P_SLEN] = 16; project_save(0); chain_config.count = 1; chain_config.row[0] = (chain_row_t){0, 1}; song_idx.tvar[song_cur][0] = RV_THIN << 4 | RV_MUTE << 8; /* T2 THIN, T3 MUTE */ ok = chain_prepare() == 0; for (i = 0; i < 16u; i++) thin &= (i & 1u) ? sec_stage.t[1].step[i].time == ST_REST : sec_stage.t[1].step[i].note[0] == 48 + i; ok &= thin && sec_stage.t[2].p[P_MUTE] == 1; seq_stop(); transport_req = 0; song_idx.tvar[song_cur][0] = RV_REV << 4; rev = chain_prepare() == 0 && sec_stage.t[1].step[0].note[0] == 63 && sec_stage.t[1].step[15].note[0] == 48; seq_stop(); transport_req = 0; song_idx.tvar[song_cur][0] = (RV_MUT1 + 2u) << 4; same = chain_prepare() == 0; memcpy(a1, sec_stage.t[1].step, sizeof a1); seq_stop(); transport_req = 0; same &= chain_prepare() == 0 && !memcmp(a1, sec_stage.t[1].step, sizeof a1); seq_stop(); transport_req = 0; song_idx.tvar[song_cur][0] = 0; bad += check("ARRANGE 2 VAR: THIN the off-beats out, MUTE, REV backwards, VAR 3 the same each time", ok && rev && same); } } { /* (add15) a version 2 index in flash (1328 bytes) */ static song_index_t v2; v2 = song_idx; v2.magic = SONG_MAGIC; v2.version = 2; v2.cur = 0; v2.scene[1][3].on = 1; v2.scene[1][3].mac[0] = 77; st_save(OBJ_SONGIDX, &v2, __builtin_offsetof(song_index_t, tsrc)); song_idx.tsrc[1][3] = 0x12345u; song_index_load(); bad += check("ARRANGE 2: a version 2 song index loads as 3, its scenes kept, every track its row's", song_idx.version == SONG_IDX_V && song_idx.scene[1][3].mac[0] == 77 && !song_idx.tsrc[1][3]); } { /* (V10.6p) 6 songs: an older JIANT's index on song 8, songs 7 and 8 stored (their commit records in the sample * bank's reserve): it loads, the current song song 6, the rows of 1..6 kept; said once, the reserve never written */ static song_index_t v8; static uint8_t keep[FL_SAMPLES_SIZE]; st_hdr_t h = {ST_MAGIC, OBJ_COUNT + 5u, 1u, 1u, 0u, 0u, {0, 0}, 0u}; v8 = song_idx; v8.magic = SONG_MAGIC; v8.version = SONG_IDX_V; v8.cur = 7; v8.rsv[1] = 0; v8.rows[4].count = 1; v8.rows[4].row[0] = (chain_row_t){2, 5}; v8.rows[7].count = 1; v8.rows[7].row[0] = (chain_row_t){3, 9}; ok = st_save(OBJ_SONGIDX, &v8, sizeof v8) == 0; song_index_load(); ok &= song_cur == NSONG - 1u && song_idx.cur == NSONG - 1u && song_idx.rows[4].count == 1u && song_idx.rows[4].row[0].bars == 5u && !song_idx.rsv[1]; /* (V10.7.66) songs 7-8's notice retired (song_retired gone): only the index's move is checked */ bad += check("6 songs: an index on song 8 loads on song 6", ok); song_cur = 0; song_idx.cur = 0; } transport_req = 0; memset(&r, 0, sizeof r); r.used = UP_USED; r.ver = UP_VER; r.np = P_COUNT; memcpy(r.name, "First", 6); erase_error = 1; bank = up_bank[1]; ok = up_put(17, &r) == 2; erase_error = 0; bad += check("failed first preset save restores empty bank", ok && !memcmp(&up_bank[1], &bank, sizeof bank)); bad += check("preset initial save", up_put(3, &r) == 0 && up_used(3)); bank = up_bank[0]; before = up_gen; r.p[0] = 77; fail_after = 1; ok = up_put(3, &r) == 2; fail_after = -1; bad += check("failed preset save rolls back bank and generation", ok && up_gen == before && !memcmp(&up_bank[0], &bank, sizeof bank)); favorite_set(NENGINES, 3, 1); fail_after = 1; ok = up_put(3, 0) == 2; fail_after = -1; bad += check("failed preset erasure retains both the sound and its favorite", ok && up_used(3) && favorite_has(NENGINES, 3) && !memcmp(&up_bank[0], &bank, sizeof bank)); memset(&up_bank[0], 0, sizeof up_bank[0]); up_boot(); bad += check("failed preset save keeps old flash", !memcmp(&up_bank[0], &bank, sizeof bank)); trk[0].user = 4; erase_error = 1; ok = up_put(3, 0) == 2; erase_error = 0; bad += check("failed preset erase keeps record and loaded label", ok && trk[0].user == 4u && up_gen == before && !memcmp(&up_bank[0], &bank, sizeof bank)); bad += check("successful preset erase clears loaded label", up_put(3, 0) == 0 && !up_used(3) && !trk[0].user); before = erases; transport_req = 1; bad += check("pending PLAY: preset save blocked", up_put(3, &r) == 2 && erases == before && !up_used(3)); transport_req = 0; chain.armed = 1; bad += check("pending SONG: preset save blocked", up_put(3, &r) == 2 && erases == before && !up_used(3)); chain.armed = 0; bad += check("preset invalid slot blocked before access", up_put(UP_SLOTS, &r) == 1 && erases == before); song.playing = 0; transport_req = 1; irq_start_race = 1; bad += check("PLAY consumed before the save snapshot still blocks a project write", project_save(1) != 0 && irq_races == 1u && song.playing && !transport_req && erases == before); song.playing = 0; transport_req = 1; irq_start_race = 1; bad += check("PLAY consumed before the preset snapshot still blocks a bank write", up_put(3, &r) == 2 && irq_races == 2u && song.playing && !transport_req && erases == before); song.playing = 0; transport_req = 1; irq_start_race = 1; settings.palette = 4; settings_save(); bad += check("PLAY consumed before the settings snapshot defers the write", irq_races == 3u && song.playing && !transport_req && persist_pending && erases == before); bad += mig_test(); bad += autosave_test(); bad += songs_test(); bad += song_save_test(); bad += fit_storage_test(); bad += voice_user_test(); bad += split_storage_test(); bad += eq_save_test(); { static project_store_t follow_store; song.playing = 0; transport_req = 0; chain.armed = chain.running = 0; trk[0].p[P_M1SRC] = MS_T1 + NTRK - 1; trk[0].p[P_M1DST] = MD_LEVEL; trk[0].p[P_M1AMT] = -64; project_capture(&proj_scratch); ok = proj_pack(&follow_store, &proj_scratch) && proj_import(&proj_scratch, &follow_store, sizeof follow_store) && proj_scratch.t[0].p[P_M1SRC] == MS_T1 + NTRK - 1 && proj_scratch.t[0].p[P_M1DST] == MD_LEVEL && proj_scratch.t[0].p[P_M1AMT] == -64; bad += check("new follower and LEVEL IDs round-trip in the existing project format", ok); } { /* (V10.7.22) OCT+ / OCT- appended: the ids before them as stored */ static project_store_t oct_store; static const int16_t SL[4][3] = {{MS_OCTUP, MD_CUT, 63}, {MS_OCTDN, MD_LEVEL, -20}, {MS_FOLLOW, MD_PAN, 5}, {MS_MODW, MD_CUT, 9}}; uint32_t k; for (k = 0; k < 12u; k++) trk[1].p[P_M1SRC + k] = SL[k / 3u][k % 3u]; project_capture(&proj_scratch); ok = MS_MODW == 6 && MS_FOLLOW == 17 && MS_T1 + NTRK - 1 == 23 && MS_OCTUP == 24 && MS_OCTDN == 25 && proj_pack(&oct_store, &proj_scratch) && proj_import(&proj_scratch, &oct_store, sizeof oct_store); for (k = 0; k < 12u; k++) ok = ok && proj_scratch.t[1].p[P_M1SRC + k] == SL[k / 3u][k % 3u]; bad += check("V10.7.22: OCT+ / OCT- (24, 25) round-trip, the sources before them keep their numbers", ok); } printf("%s\n", bad ? "PERSISTENCE TEST FAILED" : "persistence test passed"); return bad != 0; } #endif