Files
JIANT/tests/backup_test.c
Claude e1378c2265 V10.8.54: the creatures through one routine, Studio's project library, RESOUND on the FM-1, more banners
(the owner: "si dale" / "pasá todas esas"; "renombrar cada parte y proyecto desde Studio, management de proyectos
desde el celular"; RESOUND: "me gusta la canción pero no los sonidos")
- ANALOG WHEEL PHASE MATH/LOFI STRING's creatures through vx_gx (codex/gx-creatures 37d7a765), closer to their
  own: WHEEL's worms out of their tubes waving at their harmonic, crowns of tentacles; ANALOG's isotherms
  wobbling, spikes, cilia and the two oscillators orbiting; PHASE's body doubled, its tail curled; MATH's segments
  doubled. WEST, VOICE, ANIMAL, FM6 and DRUM-X keep their own (protected pictures untouched).
- Editor protocol, Studio's project library (editor_backup.c ed_pj_handle; app_contract_test, backup_test):
    95 PROJ_LIST  ()                          -> rc 0, NSONG, NSONG x (n, parts, flags, name[12])
                  parts: bit k = part A..D stored; flags: 1 the open project, 2 a GEN draft; name: part A's, ASCII
                  0-padded (the project's name)
    96 PROJ_NAME  (n, part 0..3, ASCII up to 12) -> n, part, rc   (part 0's name is the project's; live, in flash)
    97 PROJ_OP    (op, from, to, force)      -> op, from, to, rc
                  op 0 COPY, 1 MOVE (not the open project), 2 DELETE (from; to ignored); COPY/MOVE never onto the
                  open project; an occupied target only with force 1 (Studio: BACKUP_GET it first)
    rc: 0 done, 1 refused (bad request, the open project as target or moved, empty part), 2 flash failed
        (the target as it was), 3 playing (STOP first), 4 the target holds a project (force 0)
  No BACKUP object 72+p: the names travel in PROJ_LIST and in part A's own object (40 + 4n).
- GLO + A4: RESOUND (gen.c gen_rs_prep / gen_rs_apply, the web's web_gen_resound now calls them): new sounds
  for every track by its role (GEN's genre, or a seed's near the tempo), the notes, steps and song kept, the next
  sections keep them (a player's sound), "RESOUND #seed"; GLO + G4 the sounds back. The GLO map's A4 RESOUND.
- Banners: RESTART (GLO + PLAY playing), a DRUM sound's mute (EDIT + black: "SD MUTED"), LANE (SEQ + black),
  REC GRID n% (REC + KNOB 2), PROJECT n / LET GO: OPEN (SAVE + KNOB 1), KNOB 4: DJ FILTER / BPM (MIXER's OCT+),
  "A > B NEXT BAR". A lens page's OCT+ gestures in its footer for 1.5 s on entering and while OCT+ is held
  (EDIT 1 HOLD: K4 GAIN, FILTER K2 KEY TRK, REVERB / DLY K4 GATE, EUCLID, PRESETS, SCENE, PUNCH IN, TRACK GEN,
  ARRANGE, TAKE, SYSTEM).
- tests/ui_render.c builds and runs its scenes again (dk_keep gone in V10.8.21; STEREO's title; pages since gone
  drawn where they are).
Layout recorded again (FELUCCA_REPIN=keep); the plain build passes. Image 580784 of 584336 (3552 B free), BT
segment 72680 of 73728 (1048 B free), RAM code 32700 (the calls into the image moved; no RAM code added), pool
spare 8480. audio.c as V23's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-26 17:29:38 +00:00

491 lines
25 KiB
C

/* 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) */
/* The editor's full backup (editor_backup.c: LIST / GET / PUT) against simulated NOR flash:
* CRC before any write, stale runtime copies, USB resets and timeouts, malformed objects, older
* project formats, settings values and user preset banks kept byte for byte. */
#define main hostsim_main
#include "hostsim.c"
#undef main
static int32_t fm1_enc_take(uint32_t e) { (void)e; return 0; }
static void fm1_irq_off(void) {}
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
#define PROJ_FUNB_WRITER 1 /* (JIANT 0.6) the FUN8 image below is made of a FUNB */
#include "../firmware/src/gfx.c"
#include "../firmware/src/panel.c"
#include "../firmware/src/ui.c"
static void panel_setup(void) {}
static uint8_t nor[0x100000], flash_ok = 1;
static int erase_error;
static uint32_t erases;
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) { if (erase_error) return -8; memset(nor + off, 0xFF, 4096); erases++; return 0; }
static int st_prog(uint32_t off, const void *src, uint32_t n) { memcpy(nor + off, src, n); 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) FUN6, retired: refused */
enum { ED_BACKUP_LIST = 65, ED_BACKUP_GET, ED_BACKUP_PUT };
static uint8_t rep[4096];
static uint32_t rep_n;
static void ed_b(uint32_t v) { if (rep_n < sizeof rep) rep[rep_n++] = (uint8_t)(v & 127u); }
static uint32_t ed_unpack7(const uint8_t *a, uint32_t na, uint8_t *out, uint32_t max)
{
uint32_t n = 0;
while (na && n < max) {
uint32_t m = *a++, j;
na--;
uint32_t k = na > 7u ? 7u : na;
if (!k || n + k > max || (m >> k))
return 0;
for (j = 0; j < k; j++, na--)
out[n++] = (uint8_t)(*a++ | ((m >> j) & 1u) << 7);
}
return na ? 0u : n;
}
static uint8_t ed_smp_buf[512] __attribute__((aligned(4)));
static void fm1_wdt_feed(void) {}
static int ed_flash_stop(void) { return transport_busy(); }
#include "../firmware/src/editor_backup.c"
static int check(const char *what, int ok)
{
printf("backup: %-72s %s\n", what, ok ? "ok" : "FAIL");
return !ok;
}
static void reset(void)
{
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);
panel = PANEL_DEFAULT;
settings_init();
host_tracks_init();
fm1_ms = 0;
transport_req = 0;
usb.resets = 0;
erase_error = 0;
erases = 0;
ed_bk_valid = ed_bk_put = 0;
}
static uint32_t call(uint32_t cmd, const uint8_t *a, uint32_t n)
{
rep_n = 0;
ed_backup_handle(cmd, a, n);
return rep_n;
}
static void put32(uint8_t *a, uint32_t v) { for (uint32_t i = 0; i < 5u; i++) a[i] = (uint8_t)((v >> (7u * i)) & 127u); }
/* PUT begin: rc */
static uint32_t put_begin(uint32_t id, uint32_t len, uint32_t crc)
{
uint8_t a[12] = {0, (uint8_t)id};
put32(a + 2, len);
put32(a + 7, crc);
call(ED_BACKUP_PUT, a, sizeof a);
return rep[2];
}
/* PUT one chunk of data at off: rc */
static uint32_t put_chunk(uint32_t id, uint32_t off, const uint8_t *p, uint32_t n)
{
uint8_t a[16 + 300];
uint32_t k = 7;
a[0] = 1;
a[1] = (uint8_t)id;
put32(a + 2, off);
while (n) {
uint32_t g = n > 7u ? 7u : n, m = 0, i;
for (i = 0; i < g; i++) m |= (uint32_t)(p[i] >> 7) << i;
a[k++] = (uint8_t)m;
for (i = 0; i < g; i++) a[k++] = p[i] & 127u;
p += g;
n -= g;
}
call(ED_BACKUP_PUT, a, k);
return rep[2];
}
static uint32_t put_end(uint32_t id, uint32_t op) /* op 2 commit, 3 abort */
{
uint8_t a[2] = {(uint8_t)op, (uint8_t)id};
call(ED_BACKUP_PUT, a, 2);
return rep[2];
}
/* a whole object: begin, 256-byte chunks, commit; the first failing rc or the commit's */
static uint32_t put_all(uint32_t id, const void *v, uint32_t len, uint32_t crc)
{
const uint8_t *p = v;
uint32_t off, rc = put_begin(id, len, crc);
for (off = 0; !rc && off < len; off += 256u)
rc = put_chunk(id, off, p + off, len - off > 256u ? 256u : len - off);
return rc ? rc : put_end(id, 2);
}
/* LIST: rc; the length and CRC of object id */
static uint32_t list(uint32_t id, uint32_t *len, uint32_t *crc)
{
uint32_t i;
call(ED_BACKUP_LIST, 0, 0);
if (rep[1])
return rep[1];
for (i = 0; i < rep[2]; i++) {
const uint8_t *o = rep + 3 + i * 11u;
if (o[0] == id) {
*len = ed_bk_r32(o + 1);
*crc = ed_bk_r32(o + 6);
}
}
return 0;
}
static uint32_t get(uint32_t id, uint32_t off, uint32_t count)
{
uint8_t a[8] = {(uint8_t)id};
put32(a + 1, off);
a[6] = (uint8_t)(count & 127u);
a[7] = (uint8_t)(count >> 7);
call(ED_BACKUP_GET, a, sizeof a);
return rep[1];
}
int main(void)
{
int bad = 0;
uint32_t len = 0, crc = 0, before;
static project_store_t st;
static project_v6_t v6;
persist_t ps;
reset();
trk[0].step[0] = (step_t){{60}, 1, ST_NOTE, 0, 96, 0, 0};
bad += check("LIST captures the runtime: 13 objects (id 8 empty, id 9 the FM6 patches), runtime 3840 B (FUNB)",
list(0, &len, &crc) == 0 && rep[2] == 35u && len == sizeof(project_store_t) && len == 3840u &&
crc == st_crc32(ED_BK_RAW, len));
bad += check("an empty project slot lists as length 0", list(2, &len, &crc) == 0 && len == 0);
bad += check("GET of the runtime copy", get(0, 0, 64) == 0);
project_save(0);
bad += check("a project save after LIST reused the staging RAM: GET of the runtime is stale (5)",
get(0, 0, 64) == 5u);
bad += check("a new LIST makes it readable again", list(0, &len, &crc) == 0 && get(0, 0, 64) == 0);
usb.resets++;
bad += check("a USB reset after LIST: GET is stale (5)", get(2, 0, 16) == 5u);
/* settings */
reset();
memset(&ps, 0, sizeof ps);
settings_export(&ps);
ps.lowcut = 2; /* SPEAKER BASS+ */
ps.palette = palette_to_stored(6);
bad += check("settings with SPEAKER BASS+ restore", put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 &&
settings.lowcut == 2u && fx_lowcut == 2u && settings.palette == 6u);
ps.palette = 13; /* an older backup: its PAPER (old id 13), now CONTRAST (2) */
bad += check("settings of an older backup restore with the palette migrated",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings.palette == 2u);
ps.palette = UI_PAL_TAG + 9u; /* a backup of V10.7.18 or before: its MONO, now HUESO (5) */
bad += check("settings of a V10.7.18 backup restore with the palette on the nearest of the family",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings.palette == 5u);
ps.palette = palette_to_stored(6);
before = erases;
ps.lowcut = 3;
bad += check("settings with an unknown SPEAKER value are refused, nothing written",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 2u && erases == before && settings.lowcut == 2u);
ps.lowcut = 0;
ps.bold = hold_to_stored(0, 3); /* HOLD 0.6 s */
bad += check("settings with HOLD 0.6 s restore",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && HOLD_MS[settings_hold] == 600u);
ps.bold = 1; /* an older backup (its font weight): HOLD 0.4 s */
bad += check("settings of an older backup restore with HOLD 0.4 s",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && HOLD_MS[settings_hold] == 400u);
before = erases;
ps.bold = HOLD_TAG + 7u;
bad += check("settings with an unknown HOLD value are refused, nothing written",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 2u && erases == before);
ps.bold = 0;
ps.zoom = LEDS_TAG | LEDS_INV; /* LEDS INV */
bad += check("settings with LEDS INV restore",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings_leds == LEDS_INV);
ps.zoom = 1; /* an older backup (its large readout): LEDS DIM */
bad += check("settings of an older backup restore with LEDS DIM",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings_leds == LEDS_DIM);
before = erases;
ps.zoom = LEDS_TAG | LEDS_DIM_LO; /* LEDS DIM LO, OFF (appended) */
bad += check("settings with LEDS DIM LO restore",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings_leds == LEDS_DIM_LO);
ps.zoom = LEDS_TAG | LEDS_OFF;
bad += check("settings with LEDS OFF restore",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 0 && settings_leds == LEDS_OFF);
before = erases;
ps.zoom = LEDS_TAG + 4u;
bad += check("settings with an unknown LEDS value are refused, nothing written",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps)) == 2u && erases == before);
ps.zoom = 0;
bad += check("a wrong CRC is refused before any write",
put_all(1, &ps, sizeof ps, st_crc32(&ps, sizeof ps) ^ 1u) == 2u && erases == before);
/* malformed requests */
bad += check("an unknown object id is refused", put_begin(8, sizeof ps, 0) == 1u);
bad += check("a project of the wrong length is refused", put_begin(2, sizeof(project_store_t) - 4u, 0) == 1u);
bad += check("settings of the wrong length are refused", put_begin(1, sizeof ps + 4u, 0) == 1u);
bad += check("a chunk without a begin is refused (5)", (ed_bk_put = 0, put_chunk(1, 0, (const uint8_t *)&ps, 16)) == 5u);
put_begin(1, sizeof ps, st_crc32(&ps, sizeof ps));
bad += check("a chunk at the wrong offset is refused", put_chunk(1, 16, (const uint8_t *)&ps, 16) == 1u);
bad += check("a chunk for another object is refused (5)", put_chunk(2, 0, (const uint8_t *)&ps, 16) == 5u);
{
uint32_t off = 0, step = sizeof ps - 8u;
put_begin(1, sizeof ps, st_crc32(&ps, sizeof ps));
for (; off + 256u <= step; off += 256u)
put_chunk(1, off, (const uint8_t *)&ps + off, 256);
if (off < step) put_chunk(1, off, (const uint8_t *)&ps + off, step - off);
bad += check("a chunk past the length is refused", put_chunk(1, step, (const uint8_t *)&ps, 16) == 1u);
}
bad += check("commit before all bytes arrived is refused", put_end(1, 2) == 2u && erases == before);
put_begin(1, sizeof ps, st_crc32(&ps, sizeof ps));
put_chunk(1, 0, (const uint8_t *)&ps, 64);
usb.resets++;
bad += check("a USB reset during PUT ends it (5)", put_chunk(1, 64, (const uint8_t *)&ps + 64, 64) == 5u);
put_begin(1, sizeof ps, st_crc32(&ps, sizeof ps));
fm1_ms += 16000u;
bad += check("16 s without a chunk ends the PUT (5)", put_chunk(1, 0, (const uint8_t *)&ps, 64) == 5u);
put_begin(2, sizeof st, 0);
put_chunk(2, 0, (const uint8_t *)&ps, 64);
project_save(1);
bad += check("a project save during PUT (the staging RAM) ends it (5)",
put_chunk(2, 64, (const uint8_t *)&ps, 64) == 5u);
transport_req = 1;
bad += check("PUT while starting PLAY is refused (3)", put_begin(1, sizeof ps, 0) == 3u);
transport_req = 0;
/* projects */
reset();
trk[1].step[3] = (step_t){{64}, 1, ST_NOTE, 0, 96, 0, 0};
project_capture(&proj_scratch);
proj_pack(&st, &proj_scratch);
bad += check("a FUN9 project restores into slot 3 (flash and RAM)",
put_all(4, &st, sizeof st, st_crc32(&st, sizeof st)) == 0 && project_used(2) &&
!memcmp(&proj_slot[2], &st, sizeof st) && st_load(OBJ_PROJECT0 + 2, &proj_wire, sizeof proj_wire) == (int)sizeof st);
{ /* (JIANT) the songs: a section of song 3 (id 40 + 4 * 2 + 1) into its flash object, read back by LIST / GET; the
* current song's (song 1) from RAM; the song index (id 10) with a scene, the song playing kept */
uint32_t len = 0, crc = 0;
song_index_t si;
bad += check("JIANT: a section of song 3 restores into its flash object (not the current song's RAM)",
put_all(40u + 9u, &st, sizeof st, st_crc32(&st, sizeof st)) == 0 &&
st_load(OBJ_SONG1 + 4u + 1u, &proj_wire, sizeof proj_wire) == (int)sizeof st && !song_cur);
bad += check(" LIST shows it (length and CRC of the flash copy), song 1's from RAM (id 42 = slot 3)",
list(40u + 9u, &len, &crc) == 0 && len == sizeof st && crc == st_crc32(&st, sizeof st) &&
list(42u, &len, &crc) == 0 && len == sizeof st);
bad += check(" GET of it: its bytes", get(40u + 9u, 256u, 64u) == 0 && rep[1] == 0);
si = song_idx;
si.magic = SONG_MAGIC; si.version = SONG_IDX_V;
si.scene[2][0].on = 1; si.scene[2][0].mac[3] = 99; si.cur = 5; si.tsrc[2][0] = 4u << 3;
bad += check(" the song index restores (a scene of song 3, add15: a track's section), the song playing stays",
put_all(10u, &si, sizeof si, st_crc32(&si, sizeof si)) == 0 && song_idx.scene[2][0].mac[3] == 99 &&
song_idx.tsrc[2][0] == 4u << 3 && song_idx.cur == song_cur);
si.version = 2; si.scene[2][0].mac[3] = 98; si.tvar[2][0] = 0;
bad += check(" (add15) a version 2 index (before ARRANGE 2) restores, every track its row's section",
put_all(10u, &si, __builtin_offsetof(song_index_t, tsrc),
st_crc32(&si, __builtin_offsetof(song_index_t, tsrc))) == 0 &&
song_idx.scene[2][0].mac[3] == 98 && !song_idx.tsrc[2][0] && song_idx.version == SONG_IDX_V);
si.version = 7;
bad += check(" an index of another version is refused", put_all(10u, &si, sizeof si, st_crc32(&si, sizeof si)) == 2);
{ /* (V10.6p) 6 songs: LIST names 0..10 and 40..63 (35); songs 7, 8's ids (64..71, an older archive's) are refused
* at begin (rc 1: nothing written, fm1backup.js skips them), the sample bank's reserve untouched */
static uint8_t keep[FL_SAMPLES_SIZE];
uint32_t i, ids = 1;
call(ED_BACKUP_LIST, 0, 0);
for (i = 0; i < rep[2]; i++)
ids &= rep[3 + i * 11u] == (i < 11u ? i : 29u + i);
ids &= rep[2] == 35u && rep[3 + 34u * 11u] == 63u;
memcpy(keep, nor + FL_SAMPLES, sizeof keep);
ids &= put_begin(64u, sizeof st, st_crc32(&st, sizeof st)) == 1 && put_begin(71u, 0, 0) == 1 &&
put_all(63u, &st, sizeof st, st_crc32(&st, sizeof st)) == 0 && !memcmp(keep, nor + FL_SAMPLES, sizeof keep);
bad += check(" 6 songs: LIST 0..10, 40..63; songs 7-8's ids refused, nothing written in the sample bank's flash", ids);
}
}
{ /* FUN8 of 1.0.x (3584 bytes, 91 parameters, the patches at 3056): restores as FUN9, E0 at P_E0, lanes 100 % */
static uint8_t v8[3584];
uint32_t pos = 68u, pos9 = 68u, k, j, sum;
trk[2].p[P_E0] = 2;
project_capture(&proj_scratch);
proj_pack_vb(&st, &proj_scratch); /* (a FUNB: its fixed places) */
memcpy(v8, st.raw, 68);
v8[66] = 91;
for (k = 0; k < PROJ_LT; k++) {
for (j = 0; j < 91u; j++) v8[pos++] = st.raw[pos9 + (j < 83u ? j : P_E0 + j - 83u)];
pos9 += P_COUNT;
memcpy(v8 + pos, st.raw + pos9, 2u + NSTEP * 9u);
pos += 2u + NSTEP * 9u; pos9 += 2u + NSTEP * 9u;
}
memcpy(v8 + pos, st.raw + pos9, sizeof(chain_config_t) + sizeof(motion_store_t));
memcpy(v8 + 3056u, st.raw + PROJ_FM6_OFF, PROJ_LT * FM6_PACKED);
((uint32_t *)v8)[0] = 0x46554E38u;
((uint32_t *)v8)[1] = 3584u;
sum = proj_hash(v8, 3580u);
memcpy(v8 + 3580u, &sum, 4);
{ /* (V10.7.13+: JIANT 0.1 .. 0.5's formats, and 1.0.x's FUN8, not read) refused as FUN6 is, the slot kept */
static project_store_t was;
memcpy(&was, &proj_slot[1], sizeof was);
bad += check("a FUN8 project (1.0.x) is refused (2, V10.7.13+), the slot kept",
put_all(3, v8, sizeof v8, st_crc32(v8, sizeof v8)) == 2u && !memcmp(&was, &proj_slot[1], sizeof was));
}
}
memset(&v6, 0, sizeof v6); /* FUN6: 69 parameters, steps out of range */
v6.magic = PROJ_MAGIC_V6;
v6.size = sizeof v6;
for (uint32_t i = 0; i < G_NSTORE; i++)
v6.g[i] = GP[i].def;
for (uint32_t i = 0; i < PROJ_LT; i++)
v6.t[i].engine = trk[i].engine;
v6.t[0].step[0] = (step10_t){{255, 72}, 9, 7, 0, 96, 0, 0};
v6.t[0].p[61] = 3; /* old E0 */
chain_defaults(&v6.chain);
v6.sum = proj_hash(&v6, sizeof v6 - 4u);
{ /* (V10.6s) FUN6 is retired: refused, the slot and its flash as they were */
static project_store_t was;
memcpy(&was, &proj_slot[3], sizeof was);
bad += check("a FUN6 project (retired) is refused (2), the slot kept",
put_all(5, &v6, sizeof v6, st_crc32(&v6, sizeof v6)) == 2u && !memcmp(&was, &proj_slot[3], sizeof was));
}
{ /* 1.0.3: the FM6 patch bank (id 8) is retired; (V10.6s) an older archive's bank is taken and ignored (its move
* into the user presets is retired too), the user presets' patches are id 9 */
static up_bank_t ub;
static struct { uint32_t magic; uint16_t ver, nslot; uint32_t used, rsv; uint8_t v[27][FM6_PACKED]; } bk; /* (1.0's) */
static upf_t got;
uint8_t pk[FM6_PACKED];
static const int16_t SL[5] = {FM6_NFACTORY + 2, 1, FM6_NFACTORY + 4, FM6_NFACTORY + 2, FM6_NFACTORY + 2};
memset(&ub, 0, sizeof ub);
ub.magic = UP_BANK_MAGIC; ub.rsize = sizeof(up_rec_t); ub.nslot = UP_PER_BANK;
for (uint32_t k = 0; k < 5u; k++) { /* slots 1..5: B3, F2, B5 (empty), B3 on DRUM, B3 */
up_rec_t *r = &ub.r[k];
uint32_t e = k == 3u ? ENGI_DRUM : ENGI_FM6;
r->used = UP_USED; r->ver = UP_VER; r->engine = (uint8_t)e; r->np = P_COUNT;
r->name[0] = (char)('A' + k);
for (uint32_t i = 0; i < P_COUNT; i++) up_set_value(r, i, param_desc_of(e, i)->def);
up_set_value(r, P_E7, SL[k]); /* (the raw stored value: B slots as 1.0.2 wrote them) */
}
ub.r[4].name[1] = 'X';
up_set_value(&ub.r[4], P_E0, 3); /* (another sound than slot 1's) */
memset(&bk, 0, sizeof bk);
bk.magic = 0x42364D46u; bk.ver = 1; bk.nslot = 27; bk.used = 1u << 2; /* ("FM6B") */
_Static_assert(sizeof bk == sizeof(fm6_bank_t), "the retired bank's size");
memcpy(bk.v[2], FM6_FACTORY[5], FM6_PACKED);
memcpy(bk.v[2] + 118, "BANK B3 ", 10);
upf_empty();
bad += check("the user presets of an old archive (id 6) restore", put_all(6, &ub, sizeof ub, st_crc32(&ub, sizeof ub)) == 0);
memcpy(&got, &upf, sizeof got);
bad += check("an old archive's FM6 bank (id 8) is taken (0) and ignored: no patch moves",
put_all(8, &bk, sizeof bk, st_crc32(&bk, sizeof bk)) == 0 && !memcmp(&got, &upf, sizeof got) &&
upf_get(0, pk) && upf_get(4, pk));
bk.v[3][0] = 200;
bad += check(" .. whatever it holds (a byte above 127: taken, ignored)",
put_all(8, &bk, sizeof bk, st_crc32(&bk, sizeof bk)) == 0 && !memcmp(&got, &upf, sizeof got));
bad += check("an empty id 8 (a 1.0.3 archive) is taken and ignored", put_all(8, 0, 0, st_crc32(0, 0)) == 0 &&
!memcmp(&got, &upf, sizeof got));
up_load(1);
bad += check("the F2 preset loads the factory patch, SLOT F2", TSEL->p[P_E7] == 1 &&
!memcmp(fm6_patch[song.sel] + FP_NAME, FM6_FACTORY[1] + 118, 10));
upf_set(0, bk.v[2]); /* (slot 1's own patch, as an FM6 preset saves it) */
bad += check("id 8 lists empty, id 9 with the patches' length", list(8, &len, &crc) == 0 && len == 0 &&
list(9, &len, &crc) == 0 && len == sizeof(upf_t) && crc == st_crc32(&upf, sizeof upf));
memcpy(&got, &upf, sizeof got);
upf_empty();
bad += check("id 9 restores the user presets' FM6 patches (flash and RAM)",
put_all(9, &got, sizeof got, st_crc32(&got, sizeof got)) == 0 && !memcmp(&got, &upf, sizeof got) &&
!upf_get(0, pk) && !memcmp(pk + 118, "BANK B3", 7));
got.magic ^= 1;
bad += check("an id 9 of another layout is refused (2), the patches kept",
put_all(9, &got, sizeof got, st_crc32(&got, sizeof got)) == 2u && !upf_get(0, pk));
bad += check("an id 9 of the wrong size is refused at begin (1)", put_begin(9, sizeof got - 4u, 0) == 1u);
bad += check("an id 10 is refused (1)", put_begin(10, 0, 0) == 1u);
}
memcpy(&st, &proj_slot[2], sizeof st);
erase_error = 1;
proj_slot[2].raw[100] ^= 1; /* (marks the RAM copy, to see it is kept) */
memcpy(&v6, &proj_slot[2], sizeof v6);
bad += check("a flash error on commit (4) keeps the slot's RAM copy",
put_all(4, &st, sizeof st, st_crc32(&st, sizeof st)) == 4u && !memcmp(&proj_slot[2], &v6, sizeof v6));
erase_error = 0;
bad += check("an empty project object clears the slot", put_all(4, 0, 0, st_crc32(0, 0)) == 0 && !project_used(2));
st.raw[200] ^= 1; /* hash no longer matches */
bad += check("a project whose own hash fails is refused", put_all(4, &st, sizeof st, st_crc32(&st, sizeof st)) == 2u);
/* the runtime */
reset();
trk[2].step[5] = (step_t){{67}, 1, ST_NOTE, 0, 96, 0, 0};
trk[2].p[P_LEVEL] = 77;
project_capture(&proj_scratch);
proj_pack(&st, &proj_scratch);
host_tracks_init();
bad += check("the runtime object restores the tracks",
put_all(0, &st, sizeof st, st_crc32(&st, sizeof st)) == 0 && trk[2].step[5].note[0] == 67u &&
trk[2].p[P_LEVEL] == 77);
/* user preset banks: unknown record versions are kept as bytes */
reset();
{
static up_bank_t b;
memset(&b, 0, sizeof b);
b.magic = UP_BANK_MAGIC;
b.rsize = sizeof(up_rec_t);
b.nslot = UP_PER_BANK;
b.r[4].used = UP_USED;
b.r[4].ver = 99; /* a future record */
memcpy(b.r[4].name, "Future", 7);
b.r[4].p[0] = 55;
bad += check("a bank with a future record version restores byte for byte",
put_all(6, &b, sizeof b, st_crc32(&b, sizeof b)) == 0 &&
st_load(OBJ_UPRESET0, &proj_wire, sizeof proj_wire) == (int)sizeof b &&
!memcmp(&proj_wire, &b, sizeof b));
b.nslot = 3;
bad += check("a bank with the wrong slot count is refused", put_all(7, &b, sizeof b, st_crc32(&b, sizeof b)) == 2u);
}
{ /* (V10.8.54) Studio's project library: PROJ_LIST / PROJ_NAME / PROJ_OP */
static const uint8_t cp[4] = {0, 0, 2, 0}, mv[4] = {1, 2, 3, 0}, del[4] = {2, 3, 0, 0}, nm[5] = {2, 0, 'N', 'E', 'W'};
reset();
trk[0].step[0] = (step_t){{60}, 1, ST_NOTE, 0, 96, 0, 0};
project_save_as(0, "MYSONG");
call(ED_PROJ_OP, cp, 4);
bad += check("PROJ_OP COPY 1 -> 3: done", rep_n == 4u && rep[3] == 0u);
call(ED_PROJ_LIST, 0, 0);
bad += check("PROJ_LIST: project 3 has part A, named MYSONG", rep[0] == 0u && rep[1] == NSONG && rep[2 + 2u * 15u] == 2u &&
rep[2 + 2u * 15u + 1u] == 1u && !memcmp(&rep[2 + 2u * 15u + 3u], "MYSONG", 6));
call(ED_PROJ_OP, cp, 4);
bad += check("PROJ_OP COPY over it again, force 0: 4 (occupied)", rep[3] == 4u);
call(ED_PROJ_NAME, nm, 5);
call(ED_PROJ_LIST, 0, 0);
bad += check("PROJ_NAME 3 A NEW: renamed in flash", !memcmp(&rep[2 + 2u * 15u + 3u], "NEW", 4));
call(ED_PROJ_OP, mv, 4);
call(ED_PROJ_LIST, 0, 0);
bad += check("PROJ_OP MOVE 3 -> 4: 3 empty, 4 NEW", rep[2 + 2u * 15u + 1u] == 0u && rep[2 + 3u * 15u + 1u] == 1u &&
!memcmp(&rep[2 + 3u * 15u + 3u], "NEW", 4));
call(ED_PROJ_OP, del, 4);
call(ED_PROJ_LIST, 0, 0);
bad += check("PROJ_OP DELETE 4: empty", rep[2 + 3u * 15u + 1u] == 0u);
song.playing = 1;
call(ED_PROJ_OP, cp, 4);
bad += check("playing: 3 (STOP first)", rep[3] == 3u);
song.playing = 0;
}
printf("backup test %s\n", bad ? "FAILED" : "passed");
return bad != 0;
}